xref: /OK3568_Linux_fs/kernel/drivers/soc/rockchip/fiq_debugger/fiq_debugger.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * drivers/staging/android/fiq_debugger.c
3  *
4  * Serial Debugger Interface accessed through an FIQ interrupt.
5  *
6  * Copyright (C) 2008 Google, Inc.
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 #include <stdarg.h>
19 #include <linux/module.h>
20 #include <linux/io.h>
21 #include <linux/console.h>
22 #include <linux/interrupt.h>
23 #include <linux/clk.h>
24 #include <linux/platform_device.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/kmsg_dump.h>
27 #include <linux/irq.h>
28 #include <linux/delay.h>
29 #include <linux/reboot.h>
30 #include <linux/sched/signal.h>
31 #include <linux/slab.h>
32 #include <linux/smp.h>
33 #include <linux/timer.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/ptrace.h>
37 #include <linux/proc_fs.h>
38 
39 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
40 #include <linux/rockchip/rockchip_sip.h>
41 #include "rk_fiq_debugger.h"
42 #endif
43 
44 #ifdef CONFIG_FIQ_GLUE
45 #include <asm/fiq_glue.h>
46 #endif
47 
48 #ifdef CONFIG_FIQ_DEBUGGER_UART_OVERLAY
49 #include <linux/of.h>
50 #endif
51 
52 #include <linux/uaccess.h>
53 
54 #include "fiq_debugger.h"
55 #include "fiq_debugger_priv.h"
56 #include "fiq_debugger_ringbuf.h"
57 
58 #ifdef CONFIG_ROCKCHIP_DEBUG
59 #include "../rockchip_debug.h"
60 #endif
61 
62 #define DEBUG_MAX 64
63 #define CMD_COUNT 0x0f
64 #define MAX_UNHANDLED_FIQ_COUNT 1000000
65 
66 #ifdef CONFIG_ARCH_ROCKCHIP
67 #define MAX_FIQ_DEBUGGER_PORTS 1
68 #else
69 #define MAX_FIQ_DEBUGGER_PORTS 4
70 #endif
71 
72 struct fiq_debugger_state {
73 #ifdef CONFIG_FIQ_GLUE
74 	struct fiq_glue_handler handler;
75 #endif
76 	struct fiq_debugger_output output;
77 
78 	int fiq;
79 	int uart_irq;
80 	int signal_irq;
81 	int wakeup_irq;
82 	bool wakeup_irq_no_set_wake;
83 	struct clk *clk;
84 	struct fiq_debugger_pdata *pdata;
85 	struct platform_device *pdev;
86 
87 	char debug_cmd[DEBUG_MAX];
88 	int debug_busy;
89 	int debug_abort;
90 
91 	char debug_buf[DEBUG_MAX];
92 	int debug_count;
93 
94 #ifdef CONFIG_ARCH_ROCKCHIP
95 	char cmd_buf[CMD_COUNT + 1][DEBUG_MAX];
96 	int back_pointer;
97 	int current_pointer;
98 #endif
99 
100 	bool no_sleep;
101 	bool debug_enable;
102 	bool ignore_next_wakeup_irq;
103 	struct timer_list sleep_timer;
104 	spinlock_t sleep_timer_lock;
105 	bool uart_enabled;
106 	struct wakeup_source debugger_wake_src;
107 	bool console_enable;
108 	int current_cpu;
109 	atomic_t unhandled_fiq_count;
110 	bool in_fiq;
111 
112 	struct work_struct work;
113 	spinlock_t work_lock;
114 	char work_cmd[DEBUG_MAX];
115 
116 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
117 	spinlock_t console_lock;
118 	struct console console;
119 	struct tty_port tty_port;
120 	struct fiq_debugger_ringbuf *tty_rbuf;
121 	bool syslog_dumping;
122 #endif
123 
124 #ifdef CONFIG_ARCH_ROCKCHIP
125 	unsigned int last_irqs[1024];
126 	unsigned int last_local_irqs[NR_CPUS][32];
127 #else
128 	unsigned int last_irqs[NR_IRQS];
129 	unsigned int last_local_timer_irqs[NR_CPUS];
130 #endif
131 };
132 
133 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
134 static struct tty_driver *fiq_tty_driver;
135 #endif
136 
137 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
138 static bool initial_no_sleep = true;
139 #else
140 static bool initial_no_sleep;
141 #endif
142 
143 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
144 static bool initial_debug_enable = true;
145 static bool initial_console_enable = true;
146 #else
147 static bool initial_debug_enable;
148 static bool initial_console_enable;
149 #endif
150 
151 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
152 static struct fiq_debugger_state *state_tf;
153 #endif
154 
155 static bool fiq_kgdb_enable;
156 static bool fiq_debugger_disable;
157 
158 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
159 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
160 module_param_named(console_enable, initial_console_enable, bool, 0644);
161 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
162 module_param_named(disable, fiq_debugger_disable, bool, 0644);
163 
164 #ifdef CONFIG_ARM64
165 #include "fiq_debugger_arm64.c"
166 #else
167 #include "fiq_debugger_arm.c"
168 #endif
169 
170 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
171 static inline
fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state * state)172 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
173 static inline
fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state * state)174 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
175 #else
176 static inline
fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state * state)177 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
178 {
179 	if (state->wakeup_irq < 0)
180 		return;
181 	enable_irq(state->wakeup_irq);
182 	if (!state->wakeup_irq_no_set_wake)
183 		enable_irq_wake(state->wakeup_irq);
184 }
185 static inline
fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state * state)186 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
187 {
188 	if (state->wakeup_irq < 0)
189 		return;
190 	disable_irq_nosync(state->wakeup_irq);
191 	if (!state->wakeup_irq_no_set_wake)
192 		disable_irq_wake(state->wakeup_irq);
193 }
194 #endif
195 
fiq_debugger_have_fiq(struct fiq_debugger_state * state)196 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
197 {
198 	return (state->fiq >= 0);
199 }
200 
fiq_debugger_force_irq(struct fiq_debugger_state * state)201 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
202 {
203 	unsigned int irq = state->signal_irq;
204 
205 	if (irq < 0)
206 		return;
207 
208 	if (state->pdata->force_irq) {
209 		state->pdata->force_irq(state->pdev, irq);
210 	} else {
211 		struct irq_chip *chip = irq_get_chip(irq);
212 
213 		if (chip && chip->irq_retrigger)
214 			chip->irq_retrigger(irq_get_irq_data(irq));
215 	}
216 }
217 
fiq_debugger_uart_enable(struct fiq_debugger_state * state)218 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
219 {
220 	if (state->clk)
221 		clk_enable(state->clk);
222 	if (state->pdata->uart_enable)
223 		state->pdata->uart_enable(state->pdev);
224 }
225 
fiq_debugger_uart_disable(struct fiq_debugger_state * state)226 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
227 {
228 	if (state->pdata->uart_disable)
229 		state->pdata->uart_disable(state->pdev);
230 	if (state->clk)
231 		clk_disable(state->clk);
232 }
233 
fiq_debugger_uart_flush(struct fiq_debugger_state * state)234 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
235 {
236 	if (state->pdata->uart_flush)
237 		state->pdata->uart_flush(state->pdev);
238 }
239 
fiq_debugger_putc(struct fiq_debugger_state * state,char c)240 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
241 {
242 	if (state->pdata->uart_putc)
243 		state->pdata->uart_putc(state->pdev, c);
244 }
245 
fiq_debugger_puts(struct fiq_debugger_state * state,char * s)246 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
247 {
248 	unsigned c;
249 	while ((c = *s++)) {
250 		if (c == '\n')
251 			fiq_debugger_putc(state, '\r');
252 		fiq_debugger_putc(state, c);
253 	}
254 }
255 
fiq_debugger_prompt(struct fiq_debugger_state * state)256 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
257 {
258 	fiq_debugger_puts(state, "debug> ");
259 }
260 
261 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
fiq_debugger_dump_kernel_log(struct fiq_debugger_state * state)262 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
263 {
264 	char buf[512];
265 	size_t len;
266 	struct kmsg_dumper dumper = { .active = true };
267 
268 
269 	kmsg_dump_rewind_nolock(&dumper);
270 	while (kmsg_dump_get_line_nolock(&dumper, true, buf,
271 					 sizeof(buf) - 1, &len)) {
272 		buf[len] = 0;
273 		fiq_debugger_puts(state, buf);
274 	}
275 }
276 #endif
277 
278 __printf(2, 3)
fiq_debugger_printf(struct fiq_debugger_output * output,const char * fmt,...)279 static void fiq_debugger_printf(struct fiq_debugger_output *output,
280 			       const char *fmt, ...)
281 {
282 	struct fiq_debugger_state *state;
283 	char buf[256];
284 	va_list ap;
285 
286 	state = container_of(output, struct fiq_debugger_state, output);
287 	va_start(ap, fmt);
288 	vsnprintf(buf, sizeof(buf), fmt, ap);
289 	va_end(ap);
290 
291 	fiq_debugger_puts(state, buf);
292 }
293 
294 /* Safe outside fiq context */
295 __printf(2, 3)
fiq_debugger_printf_nfiq(void * cookie,const char * fmt,...)296 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
297 {
298 	struct fiq_debugger_state *state = cookie;
299 	char buf[256];
300 	va_list ap;
301 	unsigned long irq_flags;
302 
303 	va_start(ap, fmt);
304 	vsnprintf(buf, 128, fmt, ap);
305 	va_end(ap);
306 
307 	local_irq_save(irq_flags);
308 	fiq_debugger_puts(state, buf);
309 	fiq_debugger_uart_flush(state);
310 	local_irq_restore(irq_flags);
311 	return state->debug_abort;
312 }
313 
314 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
fiq_debugger_dump_irqs(struct fiq_debugger_state * state)315 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
316 {
317 	int n;
318 	struct irq_desc *desc;
319 
320 	fiq_debugger_printf(&state->output,
321 			"irqnr       total  since-last   status  name\n");
322 	for_each_irq_desc(n, desc) {
323 		struct irqaction *act = desc->action;
324 		if (!act && !kstat_irqs(n))
325 			continue;
326 		fiq_debugger_printf(&state->output, "%5d: %10u %11u %8x  %s\n", n,
327 			kstat_irqs(n),
328 			kstat_irqs(n) - state->last_irqs[n],
329 			desc->status_use_accessors,
330 			(act && act->name) ? act->name : "???");
331 		state->last_irqs[n] = kstat_irqs(n);
332 	}
333 }
334 #endif
335 
336 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
fiq_debugger_do_ps(struct fiq_debugger_state * state)337 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
338 {
339 	struct task_struct *g;
340 	struct task_struct *p;
341 	unsigned task_state;
342 	static const char stat_nam[] = "RSDTtZX";
343 
344 	fiq_debugger_printf(&state->output, "pid   ppid  prio task            pc\n");
345 	read_lock(&tasklist_lock);
346 	do_each_thread(g, p) {
347 		task_state = p->state ? __ffs(p->state) + 1 : 0;
348 		fiq_debugger_printf(&state->output,
349 			     "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
350 		fiq_debugger_printf(&state->output, "%-13.13s %c", p->comm,
351 			     task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
352 		if (task_state == TASK_RUNNING)
353 			fiq_debugger_printf(&state->output, " running\n");
354 		else
355 			fiq_debugger_printf(&state->output, " %08lx\n",
356 					thread_saved_pc(p));
357 	} while_each_thread(g, p);
358 	read_unlock(&tasklist_lock);
359 }
360 #endif
361 
362 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
363 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
fiq_debugger_begin_syslog_dump(struct fiq_debugger_state * state)364 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
365 {
366 	state->syslog_dumping = true;
367 }
368 
fiq_debugger_end_syslog_dump(struct fiq_debugger_state * state)369 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
370 {
371 	state->syslog_dumping = false;
372 }
373 #else
374 extern int do_syslog(int type, char __user *bug, int count);
fiq_debugger_begin_syslog_dump(struct fiq_debugger_state * state)375 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
376 {
377 	do_syslog(5 /* clear */, NULL, 0);
378 }
379 
fiq_debugger_end_syslog_dump(struct fiq_debugger_state * state)380 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
381 {
382 	fiq_debugger_dump_kernel_log(state);
383 }
384 #endif
385 #endif
386 
387 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
fiq_debugger_do_sysrq(struct fiq_debugger_state * state,char rq)388 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
389 {
390 	if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
391 		fiq_debugger_printf(&state->output, "sysrq-g blocked\n");
392 		return;
393 	}
394 	fiq_debugger_begin_syslog_dump(state);
395 	__handle_sysrq(rq, false);
396 	fiq_debugger_end_syslog_dump(state);
397 }
398 #endif
399 
400 #ifdef CONFIG_KGDB
fiq_debugger_do_kgdb(struct fiq_debugger_state * state)401 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
402 {
403 	if (!fiq_kgdb_enable) {
404 		fiq_debugger_printf(&state->output, "kgdb through fiq debugger not enabled\n");
405 		return;
406 	}
407 
408 	fiq_debugger_printf(&state->output, "enabling console and triggering kgdb\n");
409 	state->console_enable = true;
410 	handle_sysrq('g');
411 }
412 #endif
413 
fiq_debugger_schedule_work(struct fiq_debugger_state * state,char * cmd)414 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
415 		char *cmd)
416 {
417 	unsigned long flags;
418 
419 	spin_lock_irqsave(&state->work_lock, flags);
420 	if (state->work_cmd[0] != '\0') {
421 		fiq_debugger_printf(&state->output, "work command processor busy\n");
422 		spin_unlock_irqrestore(&state->work_lock, flags);
423 		return;
424 	}
425 
426 	strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
427 	spin_unlock_irqrestore(&state->work_lock, flags);
428 
429 	schedule_work(&state->work);
430 }
431 
fiq_debugger_work(struct work_struct * work)432 static void fiq_debugger_work(struct work_struct *work)
433 {
434 	struct fiq_debugger_state *state;
435 	char work_cmd[DEBUG_MAX];
436 	char *cmd;
437 	unsigned long flags;
438 
439 	state = container_of(work, struct fiq_debugger_state, work);
440 
441 	spin_lock_irqsave(&state->work_lock, flags);
442 
443 	strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
444 	state->work_cmd[0] = '\0';
445 
446 	spin_unlock_irqrestore(&state->work_lock, flags);
447 
448 	cmd = work_cmd;
449 	if (!strncmp(cmd, "reboot", 6)) {
450 		cmd += 6;
451 		while (*cmd == ' ')
452 			cmd++;
453 		if (*cmd != '\0')
454 			kernel_restart(cmd);
455 		else
456 			kernel_restart(NULL);
457 	} else {
458 		fiq_debugger_printf(&state->output, "unknown work command '%s'\n",
459 				work_cmd);
460 	}
461 }
462 
463 /* This function CANNOT be called in FIQ context */
fiq_debugger_irq_exec(struct fiq_debugger_state * state,char * cmd)464 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
465 {
466 	int invalid_cmd = 0;
467 
468 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
469 	if (!strcmp(cmd, "ps"))
470 		fiq_debugger_do_ps(state);
471 	if (!strcmp(cmd, "sysrq"))
472 		fiq_debugger_do_sysrq(state, 'h');
473 	if (!strncmp(cmd, "sysrq ", 6))
474 		fiq_debugger_do_sysrq(state, cmd[6]);
475 #endif
476 #ifdef CONFIG_KGDB
477 	if (!strcmp(cmd, "kgdb"))
478 		fiq_debugger_do_kgdb(state);
479 #endif
480 	if (!strncmp(cmd, "reboot", 6))
481 		fiq_debugger_schedule_work(state, cmd);
482 #ifdef CONFIG_ARCH_ROCKCHIP
483 	else {
484 		invalid_cmd = 1;
485 		memset(state->debug_buf, 0, DEBUG_MAX);
486 	}
487 
488 	if (invalid_cmd == 0) {
489 		state->current_pointer =
490 				(state->current_pointer - 1) & CMD_COUNT;
491 		if (strcmp(state->cmd_buf[state->current_pointer], state->debug_buf)) {
492 			state->current_pointer =
493 				(state->current_pointer + 1) & CMD_COUNT;
494 			memset(state->cmd_buf[state->current_pointer], 0, DEBUG_MAX);
495 			strcpy(state->cmd_buf[state->current_pointer], state->debug_buf);
496 		}
497 		memset(state->debug_buf, 0, DEBUG_MAX);
498 		state->current_pointer = (state->current_pointer + 1) & CMD_COUNT;
499 		state->back_pointer = state->current_pointer;
500 	}
501 #endif
502 }
503 
504 #ifdef CONFIG_ARCH_ROCKCHIP
505 static char cmd_buf[][16] = {
506 		{"pc"},
507 		{"regs"},
508 		{"allregs"},
509 		{"bt"},
510 		{"reboot"},
511 #ifdef CONFIG_ROCKCHIP_DEBUG
512 		{"pcsr"},
513 #endif
514 		{"sleep"},
515 		{"nosleep"},
516 		{"console"},
517 		{"cpu"},
518 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
519 		{"reset"},
520 		{"irqs"},
521 		{"kmsg"},
522 		{"version"},
523 		{"ps"},
524 		{"sysrq"},
525 #endif
526 
527 #ifdef CONFIG_KGDB
528 		{"kgdb"},
529 #endif
530 };
531 #endif
532 
fiq_debugger_help(struct fiq_debugger_state * state)533 static void fiq_debugger_help(struct fiq_debugger_state *state)
534 {
535 	fiq_debugger_printf(&state->output,
536 				"FIQ Debugger commands:\n"
537 				" pc            PC status\n"
538 				" regs          Register dump\n"
539 				" allregs       Extended Register dump\n"
540 				" bt            Stack trace\n"
541 #ifdef CONFIG_ROCKCHIP_DEBUG
542 				" pcsr          Dump all cpus pc by DBGPCSR\n"
543 #endif
544 				" cpu           Current CPU\n"
545 				" cpu <number>  Switch to CPU<number>\n"
546 				" reboot [<c>]  Reboot with command <c>\n");
547 
548 	fiq_debugger_printf(&state->output,
549 				" sleep         Allow sleep while in FIQ\n"
550 				" nosleep       Disable sleep while in FIQ\n"
551 				" console       Switch terminal to console\n");
552 
553 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
554 	fiq_debugger_printf(&state->output,
555 				" reset [<c>]   Hard reset with command <c>\n"
556 				" irqs          Interrupt status\n"
557 				" kmsg          Kernel log\n"
558 				" version       Kernel version\n");
559 
560 	fiq_debugger_printf(&state->output,
561 				" ps            Process list\n"
562 				" sysrq         sysrq options\n"
563 				" sysrq <param> Execute sysrq with <param>\n");
564 #endif
565 
566 #ifdef CONFIG_KGDB
567 	fiq_debugger_printf(&state->output,
568 				" kgdb          Enter kernel debugger\n");
569 #endif
570 }
571 
572 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
fiq_debugger_take_affinity(struct fiq_debugger_state * state,int cpu)573 static void fiq_debugger_take_affinity(struct fiq_debugger_state *state, int cpu)
574 {
575 	struct cpumask cpumask;
576 
577 	cpumask_clear(&cpumask);
578 	cpumask_set_cpu(cpu, &cpumask);
579 	irq_set_affinity(state->uart_irq, &cpumask);
580 }
581 #else
fiq_debugger_take_affinity(struct fiq_debugger_state * state,int cpu)582 static void fiq_debugger_take_affinity(struct fiq_debugger_state *state, int cpu)
583 {
584 }
585 #endif
586 
fiq_debugger_switch_cpu(struct fiq_debugger_state * state,int cpu)587 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
588 {
589 	if (!cpu_online(cpu)) {
590 		fiq_debugger_printf(&state->output, "cpu %d offline\n", cpu);
591 		return;
592 	}
593 
594 	if (!fiq_debugger_have_fiq(state)) {
595 		fiq_debugger_take_affinity(state, cpu);
596 	}
597 #ifdef CONFIG_ARCH_ROCKCHIP
598 	else {
599 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
600 		if (sip_fiq_debugger_is_enabled() ||
601 		    sdei_fiq_debugger_is_enabled()) {
602 			if (state->pdata->switch_cpu) {
603 				state->pdata->switch_cpu(state->pdev, cpu);
604 				state->current_cpu = cpu;
605 			}
606 			return;
607 		}
608 #else
609 		struct cpumask cpumask;
610 
611 		cpumask_clear(&cpumask);
612 		cpumask_set_cpu(cpu, &cpumask);
613 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
614 		irq_set_affinity(state->fiq, &cpumask);
615 		irq_set_affinity(state->uart_irq, &cpumask);
616 #endif
617 #endif
618 	}
619 #endif
620 	state->current_cpu = cpu;
621 }
622 
fiq_debugger_fiq_exec(struct fiq_debugger_state * state,const char * cmd,const struct pt_regs * regs,void * svc_sp)623 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
624 			const char *cmd, const struct pt_regs *regs,
625 			void *svc_sp)
626 {
627 	bool signal_helper = false;
628 
629 	if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
630 		fiq_debugger_help(state);
631 	} else if (!strcmp(cmd, "pc")) {
632 		fiq_debugger_dump_pc(&state->output, regs);
633 	} else if (!strcmp(cmd, "regs")) {
634 		fiq_debugger_dump_regs(&state->output, regs);
635 	} else if (!strcmp(cmd, "allregs")) {
636 		fiq_debugger_dump_allregs(&state->output, regs);
637 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
638 	} else if (!strcmp(cmd, "bt")) {
639 		/*
640 		 * ARM64:
641 		 * Cpu is at ELx(1 or 2), but EL0_SP(svc_sp) may be user space.
642 		 * If EL0_SP.63 is 0, use TTBR0.
643 		 */
644 		if (user_mode((struct pt_regs *)regs) ||
645 		    (IS_ENABLED(CONFIG_ARM64) && (((unsigned long)svc_sp & 0x8000000000000000) == 0)) ||
646 		    (IS_ENABLED(CONFIG_ARM) && (((unsigned long)svc_sp < PAGE_OFFSET) || ((unsigned long)svc_sp > -256UL))))
647 			fiq_debugger_printf(&state->output, "User mode\n");
648 		else
649 			fiq_debugger_dump_stacktrace(&state->output, regs,
650 						     100, svc_sp);
651 	} else if (!strncmp(cmd, "reset", 5)) {
652 		cmd += 5;
653 		while (*cmd == ' ')
654 			cmd++;
655 		if (*cmd) {
656 			char tmp_cmd[32];
657 			strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
658 			machine_restart(tmp_cmd);
659 		} else {
660 			machine_restart(NULL);
661 		}
662 	} else if (!strcmp(cmd, "irqs")) {
663 		fiq_debugger_dump_irqs(state);
664 	} else if (!strcmp(cmd, "kmsg")) {
665 		fiq_debugger_dump_kernel_log(state);
666 #endif
667 #ifdef CONFIG_ROCKCHIP_DEBUG
668 	} else if (!strcmp(cmd, "pcsr")) {
669 		rockchip_debug_dump_pcsr(&state->output);
670 #endif
671 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
672 	} else if (!strcmp(cmd, "version")) {
673 		fiq_debugger_printf(&state->output, "%s\n", linux_banner);
674 #endif
675 	} else if (!strcmp(cmd, "sleep")) {
676 		state->no_sleep = false;
677 		fiq_debugger_printf(&state->output, "enabling sleep\n");
678 	} else if (!strcmp(cmd, "nosleep")) {
679 		state->no_sleep = true;
680 		fiq_debugger_printf(&state->output, "disabling sleep\n");
681 	} else if (!strcmp(cmd, "console")) {
682 		fiq_debugger_printf(&state->output, "console mode\n");
683 		fiq_debugger_uart_flush(state);
684 		state->console_enable = true;
685 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
686 		if (sip_fiq_debugger_is_enabled()) {
687 			if (state->pdata->enable_debug)
688 				state->pdata->enable_debug(state->pdev, false);
689 		}
690 #endif
691 	} else if (!strcmp(cmd, "cpu")) {
692 		fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
693 	} else if (!strncmp(cmd, "cpu ", 4)) {
694 		unsigned long cpu = 0;
695 		if (kstrtoul(cmd + 4, 10, &cpu) == 0)
696 			fiq_debugger_switch_cpu(state, cpu);
697 		else
698 			fiq_debugger_printf(&state->output, "invalid cpu\n");
699 
700 		fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
701 	} else {
702 		if (state->debug_busy) {
703 			fiq_debugger_printf(&state->output,
704 				"command processor busy. trying to abort.\n");
705 			state->debug_abort = -1;
706 		} else {
707 			strcpy(state->debug_cmd, cmd);
708 			state->debug_busy = 1;
709 		}
710 
711 		return true;
712 	}
713 	if (!state->console_enable)
714 		fiq_debugger_prompt(state);
715 
716 	return signal_helper;
717 }
718 
fiq_debugger_sleep_timer_expired(struct timer_list * t)719 static void fiq_debugger_sleep_timer_expired(struct timer_list *t)
720 {
721 	struct fiq_debugger_state *state = from_timer(state, t, sleep_timer);
722 	unsigned long flags;
723 
724 	spin_lock_irqsave(&state->sleep_timer_lock, flags);
725 	if (state->uart_enabled && !state->no_sleep) {
726 		if (state->debug_enable && !state->console_enable) {
727 			state->debug_enable = false;
728 			fiq_debugger_printf_nfiq(state,
729 					"suspending fiq debugger\n");
730 		}
731 		state->ignore_next_wakeup_irq = true;
732 		fiq_debugger_uart_disable(state);
733 		state->uart_enabled = false;
734 		fiq_debugger_enable_wakeup_irq(state);
735 	}
736 	__pm_relax(&state->debugger_wake_src);
737 	spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
738 }
739 
fiq_debugger_handle_wakeup(struct fiq_debugger_state * state)740 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
741 {
742 	unsigned long flags;
743 
744 	spin_lock_irqsave(&state->sleep_timer_lock, flags);
745 	if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
746 		state->ignore_next_wakeup_irq = false;
747 	} else if (!state->uart_enabled) {
748 		__pm_stay_awake(&state->debugger_wake_src);
749 		fiq_debugger_uart_enable(state);
750 		state->uart_enabled = true;
751 		fiq_debugger_disable_wakeup_irq(state);
752 		mod_timer(&state->sleep_timer, jiffies + HZ / 2);
753 	}
754 	spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
755 }
756 
fiq_debugger_wakeup_irq_handler(int irq,void * dev)757 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
758 {
759 	struct fiq_debugger_state *state = dev;
760 
761 	if (!state->no_sleep)
762 		fiq_debugger_puts(state, "WAKEUP\n");
763 	fiq_debugger_handle_wakeup(state);
764 
765 	return IRQ_HANDLED;
766 }
767 
768 static
fiq_debugger_handle_console_irq_context(struct fiq_debugger_state * state)769 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
770 {
771 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
772 	if (state->tty_port.ops) {
773 		int i;
774 		int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
775 		for (i = 0; i < count; i++) {
776 			int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
777 			tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
778 			if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
779 				pr_warn("fiq tty failed to consume byte\n");
780 		}
781 		tty_flip_buffer_push(&state->tty_port);
782 	}
783 #endif
784 }
785 
fiq_debugger_handle_irq_context(struct fiq_debugger_state * state)786 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
787 {
788 	if (!state->no_sleep) {
789 		unsigned long flags;
790 
791 		spin_lock_irqsave(&state->sleep_timer_lock, flags);
792 		__pm_stay_awake(&state->debugger_wake_src);
793 		mod_timer(&state->sleep_timer, jiffies + HZ * 5);
794 		spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
795 	}
796 	fiq_debugger_handle_console_irq_context(state);
797 	if (state->debug_busy) {
798 		fiq_debugger_irq_exec(state, state->debug_cmd);
799 		if (!state->console_enable)
800 			fiq_debugger_prompt(state);
801 		state->debug_busy = 0;
802 	}
803 }
804 
fiq_debugger_getc(struct fiq_debugger_state * state)805 static int fiq_debugger_getc(struct fiq_debugger_state *state)
806 {
807 	if (state->pdata->uart_getc)
808 		return state->pdata->uart_getc(state->pdev);
809 	else
810 		return FIQ_DEBUGGER_NO_CHAR;
811 }
812 
fiq_debugger_cmd_check_back(struct fiq_debugger_state * state,char c)813 static int fiq_debugger_cmd_check_back(struct fiq_debugger_state *state, char c)
814 {
815 	char *s;
816 	int i = 0;
817 
818 	if (c == 'A') {
819 		state->back_pointer = (state->back_pointer - 1) & CMD_COUNT;
820 		if (state->back_pointer != state->current_pointer) {
821 			s = state->cmd_buf[state->back_pointer];
822 			if (*s != 0) {
823 				for (i = 0; i < strlen(state->debug_buf) - 1; i++) {
824 					fiq_debugger_putc(state, 8);
825 					fiq_debugger_putc(state, ' ');
826 					fiq_debugger_putc(state, 8);
827 				}
828 				memset(state->debug_buf, 0, DEBUG_MAX);
829 				strcpy(state->debug_buf, s);
830 				state->debug_count = strlen(state->debug_buf);
831 				fiq_debugger_printf(&state->output, state->debug_buf);
832 			} else {
833 				state->back_pointer = (state->back_pointer + 1) & CMD_COUNT;
834 			}
835 
836 		} else {
837 			state->back_pointer = (state->back_pointer + 1) & CMD_COUNT;
838 		}
839 	} else if (c == 'B') {
840 		if (state->back_pointer != state->current_pointer) {
841 			state->back_pointer = (state->back_pointer + 1) & CMD_COUNT;
842 			if (state->back_pointer == state->current_pointer) {
843 				goto cmd_clear;
844 			} else {
845 				s = state->cmd_buf[state->back_pointer];
846 				if (*s != 0) {
847 					for (i = 0; i < strlen(state->debug_buf) - 1; i++) {
848 						fiq_debugger_putc(state, 8);
849 						fiq_debugger_putc(state, ' ');
850 						fiq_debugger_putc(state, 8);
851 					}
852 					memset(state->debug_buf, 0, DEBUG_MAX);
853 					strcpy(state->debug_buf, s);
854 					state->debug_count = strlen(state->debug_buf);
855 					fiq_debugger_printf(&state->output, state->debug_buf);
856 				}
857 			}
858 		} else {
859 cmd_clear:
860 			for (i = 0; i < strlen(state->debug_buf) - 1; i++) {
861 				fiq_debugger_putc(state, 8);
862 				fiq_debugger_putc(state, ' ');
863 				fiq_debugger_putc(state, 8);
864 			}
865 			memset(state->debug_buf, 0, DEBUG_MAX);
866 			state->debug_count = 0;
867 		}
868 	}
869 	return 0;
870 }
871 
fiq_debugger_cmd_tab(struct fiq_debugger_state * state)872 static void fiq_debugger_cmd_tab(struct fiq_debugger_state *state)
873 {
874 	int i, j;
875 	int count = 0;
876 
877 	for (i = 0; i < ARRAY_SIZE(cmd_buf); i++)
878 		cmd_buf[i][15] = 1;
879 
880 	for (j = 1; j <= strlen(state->debug_buf); j++) {
881 		count = 0;
882 		for (i = 0; i < ARRAY_SIZE(cmd_buf); i++) {
883 			if (cmd_buf[i][15] == 1) {
884 				if (strncmp(state->debug_buf, cmd_buf[i], j))
885 					cmd_buf[i][15] = 0;
886 				else
887 					count++;
888 			}
889 		}
890 		if (count == 0)
891 			break;
892 	}
893 
894 	if (count == 1) {
895 		for (i = 0; i < ARRAY_SIZE(cmd_buf); i++) {
896 			if (cmd_buf[i][15] == 1)
897 				break;
898 		}
899 
900 		for (j = 0; j < strlen(state->debug_buf); j++) {
901 			fiq_debugger_putc(state, 8);
902 			fiq_debugger_putc(state, ' ');
903 			fiq_debugger_putc(state, 8);
904 		}
905 		memset(state->debug_buf, 0, DEBUG_MAX);
906 		strcpy(state->debug_buf, cmd_buf[i]);
907 		state->debug_count = strlen(state->debug_buf);
908 		fiq_debugger_printf(&state->output, state->debug_buf);
909 	}
910 }
911 
fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state * state,int this_cpu,const struct pt_regs * regs,void * svc_sp)912 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
913 			int this_cpu, const struct pt_regs *regs, void *svc_sp)
914 {
915 	int c;
916 	static int last_c;
917 	int count = 0;
918 	bool signal_helper = false;
919 
920 	if (state->current_cpu == -1)
921 		state->current_cpu = this_cpu;
922 
923 	if (this_cpu != state->current_cpu) {
924 		if (state->in_fiq)
925 			return false;
926 
927 		if (atomic_inc_return(&state->unhandled_fiq_count) !=
928 					MAX_UNHANDLED_FIQ_COUNT)
929 			return false;
930 
931 		fiq_debugger_printf(&state->output,
932 			"fiq_debugger: cpu %d not responding, "
933 			"reverting to cpu %d\n", state->current_cpu,
934 			this_cpu);
935 
936 		atomic_set(&state->unhandled_fiq_count, 0);
937 		state->current_cpu = this_cpu;
938 		return false;
939 	}
940 
941 	state->in_fiq = true;
942 
943 	while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
944 		count++;
945 		if (!state->debug_enable) {
946 			if ((c == 13) || (c == 10)) {
947 				state->debug_enable = true;
948 				state->debug_count = 0;
949 				fiq_debugger_prompt(state);
950 			}
951 		} else if (c == FIQ_DEBUGGER_BREAK) {
952 			state->console_enable = false;
953 #ifdef CONFIG_ARCH_ROCKCHIP
954 			fiq_debugger_puts(state, "\nWelcome to ");
955 #endif
956 			if (fiq_debugger_have_fiq(state))
957 				fiq_debugger_puts(state,
958 						  "fiq debugger mode\n");
959 			else
960 				fiq_debugger_puts(state,
961 						  "irq debugger mode\n");
962 			state->debug_count = 0;
963 #ifdef CONFIG_ARCH_ROCKCHIP
964 			fiq_debugger_puts(state, "Enter ? to get command help\n");
965 			state->back_pointer = CMD_COUNT;
966 			state->current_pointer = CMD_COUNT;
967 			memset(state->cmd_buf, 0, (CMD_COUNT + 1) * DEBUG_MAX);
968 #endif
969 
970 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
971 			if (sip_fiq_debugger_is_enabled()) {
972 				if (state->pdata->enable_debug)
973 					state->pdata->enable_debug(state->pdev,
974 								   true);
975 			}
976 #endif
977 			fiq_debugger_prompt(state);
978 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
979 			fiq_debugger_ringbuf_push(state->tty_rbuf, 8);
980 			fiq_debugger_ringbuf_push(state->tty_rbuf, 8);
981 #endif
982 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
983 		} else if (state->console_enable && state->tty_rbuf) {
984 			fiq_debugger_ringbuf_push(state->tty_rbuf, c);
985 			signal_helper = true;
986 #endif
987 #ifdef CONFIG_ARCH_ROCKCHIP
988 		} else if (last_c == '[' && (c == 'A' || c == 'B' || c == 'C' || c == 'D')) {
989 			if (state->debug_count > 0) {
990 				state->debug_count--;
991 				fiq_debugger_putc(state, 8);
992 				fiq_debugger_putc(state, ' ');
993 				fiq_debugger_putc(state, 8);
994 			}
995 			fiq_debugger_cmd_check_back(state, c);
996 		} else if (c == 9) {
997 			fiq_debugger_cmd_tab(state);
998 #endif
999 		} else if ((c >= ' ') && (c < 127)) {
1000 			if (state->debug_count < (DEBUG_MAX - 1)) {
1001 				state->debug_buf[state->debug_count++] = c;
1002 				fiq_debugger_putc(state, c);
1003 			}
1004 		} else if ((c == 8) || (c == 127)) {
1005 			if (state->debug_count > 0) {
1006 				state->debug_count--;
1007 				fiq_debugger_putc(state, 8);
1008 				fiq_debugger_putc(state, ' ');
1009 				fiq_debugger_putc(state, 8);
1010 			}
1011 		} else if ((c == 13) || (c == 10)) {
1012 			if (c == '\r' || (c == '\n' && last_c != '\r')) {
1013 				fiq_debugger_putc(state, '\r');
1014 				fiq_debugger_putc(state, '\n');
1015 			}
1016 			if (state->debug_count) {
1017 				state->debug_buf[state->debug_count] = 0;
1018 				state->debug_count = 0;
1019 				signal_helper |=
1020 					fiq_debugger_fiq_exec(state,
1021 							state->debug_buf,
1022 							regs, svc_sp);
1023 #ifdef CONFIG_ARCH_ROCKCHIP
1024 				if (signal_helper == false) {
1025 					state->current_pointer =
1026 							(state->current_pointer - 1) & CMD_COUNT;
1027 					if (strcmp(state->cmd_buf[state->current_pointer], state->debug_buf)) {
1028 						state->current_pointer =
1029 							(state->current_pointer + 1) & CMD_COUNT;
1030 						memset(state->cmd_buf[state->current_pointer], 0, DEBUG_MAX);
1031 						strcpy(state->cmd_buf[state->current_pointer], state->debug_buf);
1032 					}
1033 					memset(state->debug_buf, 0, DEBUG_MAX);
1034 					state->current_pointer =
1035 						(state->current_pointer + 1) & CMD_COUNT;
1036 					state->back_pointer =
1037 						state->current_pointer;
1038 				}
1039 #endif
1040 			} else {
1041 				fiq_debugger_prompt(state);
1042 			}
1043 		}
1044 		last_c = c;
1045 	}
1046 	if (!state->console_enable)
1047 		fiq_debugger_uart_flush(state);
1048 	if (state->pdata->fiq_ack)
1049 		state->pdata->fiq_ack(state->pdev, state->fiq);
1050 
1051 	/* poke sleep timer if necessary */
1052 	if (state->debug_enable && !state->no_sleep)
1053 		signal_helper = true;
1054 
1055 	atomic_set(&state->unhandled_fiq_count, 0);
1056 	state->in_fiq = false;
1057 
1058 	return signal_helper;
1059 }
1060 
1061 #ifdef CONFIG_FIQ_GLUE
fiq_debugger_fiq(struct fiq_glue_handler * h,void * regs,void * svc_sp)1062 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
1063 		void *regs, void *svc_sp)
1064 {
1065 	struct fiq_debugger_state *state =
1066 		container_of(h, struct fiq_debugger_state, handler);
1067 	unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
1068 	bool need_irq;
1069 
1070 	need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
1071 			svc_sp);
1072 	if (need_irq)
1073 		fiq_debugger_force_irq(state);
1074 }
1075 #endif
1076 
1077 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
fiq_debugger_fiq(void * regs,u32 cpu)1078 void fiq_debugger_fiq(void *regs, u32 cpu)
1079 {
1080 	struct fiq_debugger_state *state = state_tf;
1081 	bool need_irq;
1082 
1083 	if (!state)
1084 		return;
1085 
1086 	need_irq = fiq_debugger_handle_uart_interrupt(state, cpu, regs,
1087 						      current_thread_info());
1088 	if (need_irq)
1089 		fiq_debugger_force_irq(state);
1090 }
1091 EXPORT_SYMBOL(fiq_debugger_fiq);
1092 #endif
1093 
1094 /*
1095  * When not using FIQs, we only use this single interrupt as an entry point.
1096  * This just effectively takes over the UART interrupt and does all the work
1097  * in this context.
1098  */
fiq_debugger_uart_irq(int irq,void * dev)1099 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
1100 {
1101 	struct fiq_debugger_state *state = dev;
1102 	bool not_done;
1103 
1104 	fiq_debugger_handle_wakeup(state);
1105 
1106 	/* handle the debugger irq in regular context */
1107 	not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
1108 #ifdef CONFIG_NO_GKI
1109 					      get_irq_regs(),
1110 #else
1111 					      NULL,
1112 #endif
1113 					      current_thread_info());
1114 	if (not_done)
1115 		fiq_debugger_force_irq(state);
1116 
1117 	return IRQ_HANDLED;
1118 }
1119 
1120 /*
1121  * If FIQs are used, not everything can happen in fiq context.
1122  * FIQ handler does what it can and then signals this interrupt to finish the
1123  * job in irq context.
1124  */
fiq_debugger_signal_irq(int irq,void * dev)1125 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
1126 {
1127 	struct fiq_debugger_state *state = dev;
1128 
1129 	if (state->pdata->force_irq_ack)
1130 		state->pdata->force_irq_ack(state->pdev, state->signal_irq);
1131 
1132 	fiq_debugger_handle_irq_context(state);
1133 
1134 	return IRQ_HANDLED;
1135 }
1136 
1137 #ifdef CONFIG_FIQ_GLUE
fiq_debugger_resume(struct fiq_glue_handler * h)1138 static void fiq_debugger_resume(struct fiq_glue_handler *h)
1139 {
1140 	struct fiq_debugger_state *state =
1141 		container_of(h, struct fiq_debugger_state, handler);
1142 	if (state->pdata->uart_resume)
1143 		state->pdata->uart_resume(state->pdev);
1144 }
1145 #endif
1146 
1147 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
fiq_debugger_console_device(struct console * co,int * index)1148 static struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
1149 {
1150 	*index = co->index;
1151 	return fiq_tty_driver;
1152 }
1153 
fiq_debugger_console_write(struct console * co,const char * s,unsigned int count)1154 static void fiq_debugger_console_write(struct console *co,
1155 				const char *s, unsigned int count)
1156 {
1157 	struct fiq_debugger_state *state;
1158 	unsigned long flags;
1159 
1160 	state = container_of(co, struct fiq_debugger_state, console);
1161 
1162 	if (!state->console_enable && !state->syslog_dumping)
1163 		return;
1164 
1165 #ifdef CONFIG_RK_CONSOLE_THREAD
1166 	if (state->pdata->console_write) {
1167 		state->pdata->console_write(state->pdev, s, count);
1168 		return;
1169 	}
1170 #endif
1171 
1172 	fiq_debugger_uart_enable(state);
1173 	spin_lock_irqsave(&state->console_lock, flags);
1174 	while (count--) {
1175 		if (*s == '\n')
1176 			fiq_debugger_putc(state, '\r');
1177 		fiq_debugger_putc(state, *s++);
1178 	}
1179 	fiq_debugger_uart_flush(state);
1180 	spin_unlock_irqrestore(&state->console_lock, flags);
1181 	fiq_debugger_uart_disable(state);
1182 }
1183 
1184 static struct console fiq_debugger_console = {
1185 	.name = "ttyFIQ",
1186 	.device = fiq_debugger_console_device,
1187 	.write = fiq_debugger_console_write,
1188 	.flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
1189 };
1190 
fiq_tty_open(struct tty_struct * tty,struct file * filp)1191 static int fiq_tty_open(struct tty_struct *tty, struct file *filp)
1192 {
1193 	int line = tty->index;
1194 	struct fiq_debugger_state **states = tty->driver->driver_state;
1195 	struct fiq_debugger_state *state = states[line];
1196 
1197 	return tty_port_open(&state->tty_port, tty, filp);
1198 }
1199 
fiq_tty_close(struct tty_struct * tty,struct file * filp)1200 static void fiq_tty_close(struct tty_struct *tty, struct file *filp)
1201 {
1202 	tty_port_close(tty->port, tty, filp);
1203 }
1204 
fiq_tty_wake_up(struct platform_device * pdev)1205 void fiq_tty_wake_up(struct platform_device *pdev)
1206 {
1207 	struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1208 
1209 	if (tty_port_initialized(&state->tty_port))
1210 		tty_port_tty_wakeup(&state->tty_port);
1211 }
1212 EXPORT_SYMBOL_GPL(fiq_tty_wake_up);
1213 
fiq_tty_write(struct tty_struct * tty,const unsigned char * buf,int count)1214 static int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1215 {
1216 	int i;
1217 	int line = tty->index;
1218 	struct fiq_debugger_state **states = tty->driver->driver_state;
1219 	struct fiq_debugger_state *state = states[line];
1220 
1221 	if (!state->console_enable)
1222 		return count;
1223 
1224 #ifdef CONFIG_RK_CONSOLE_THREAD
1225 	if (state->pdata->tty_write)
1226 		return state->pdata->tty_write(state->pdev, buf, count);
1227 #endif
1228 
1229 	fiq_debugger_uart_enable(state);
1230 #ifndef CONFIG_RK_CONSOLE_THREAD
1231 	spin_lock_irq(&state->console_lock);
1232 #endif
1233 	for (i = 0; i < count; i++)
1234 		fiq_debugger_putc(state, *buf++);
1235 #ifndef CONFIG_RK_CONSOLE_THREAD
1236 	spin_unlock_irq(&state->console_lock);
1237 #endif
1238 	fiq_debugger_uart_disable(state);
1239 
1240 	return count;
1241 }
1242 
fiq_tty_write_room(struct tty_struct * tty)1243 static int fiq_tty_write_room(struct tty_struct *tty)
1244 {
1245 #ifdef CONFIG_RK_CONSOLE_THREAD
1246 	int line = tty->index;
1247 	struct fiq_debugger_state **states = tty->driver->driver_state;
1248 	struct fiq_debugger_state *state = states[line];
1249 
1250 	if (state->pdata->write_room)
1251 		return state->pdata->write_room(state->pdev);
1252 #endif
1253 	return 2048;
1254 }
1255 
1256 #ifdef CONFIG_CONSOLE_POLL
fiq_tty_poll_init(struct tty_driver * driver,int line,char * options)1257 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
1258 {
1259 	return 0;
1260 }
1261 
fiq_tty_poll_get_char(struct tty_driver * driver,int line)1262 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
1263 {
1264 	struct fiq_debugger_state **states = driver->driver_state;
1265 	struct fiq_debugger_state *state = states[line];
1266 	int c = NO_POLL_CHAR;
1267 
1268 	fiq_debugger_uart_enable(state);
1269 	if (fiq_debugger_have_fiq(state)) {
1270 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
1271 		int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1272 		if (count > 0) {
1273 			c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1274 			fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1275 		}
1276 #endif
1277 	} else {
1278 		c = fiq_debugger_getc(state);
1279 		if (c == FIQ_DEBUGGER_NO_CHAR)
1280 			c = NO_POLL_CHAR;
1281 	}
1282 	fiq_debugger_uart_disable(state);
1283 
1284 	return c;
1285 }
1286 
fiq_tty_poll_put_char(struct tty_driver * driver,int line,char ch)1287 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1288 {
1289 	struct fiq_debugger_state **states = driver->driver_state;
1290 	struct fiq_debugger_state *state = states[line];
1291 	fiq_debugger_uart_enable(state);
1292 	fiq_debugger_putc(state, ch);
1293 	fiq_debugger_uart_disable(state);
1294 }
1295 #endif
1296 
1297 #ifdef CONFIG_PROC_FS
fiq_tty_proc_show(struct seq_file * m,void * v)1298 static int fiq_tty_proc_show(struct seq_file *m, void *v)
1299 {
1300 	struct tty_driver *driver = m->private;
1301 	struct fiq_debugger_state **states = driver->driver_state;
1302 	struct fiq_debugger_state *state;
1303 	int i;
1304 
1305 	seq_puts(m, "fiq-debugger driver\n");
1306 	for (i = 0; i < MAX_FIQ_DEBUGGER_PORTS; i++) {
1307 		state = states[i];
1308 		if (!state)
1309 			continue;
1310 
1311 		seq_printf(m, "%d:", i);
1312 		seq_printf(m, " state:%d", state->console_enable);
1313 		seq_putc(m, '\n');
1314 	}
1315 	return 0;
1316 }
1317 #endif
1318 
1319 static const struct tty_port_operations fiq_tty_port_ops;
1320 
1321 static const struct tty_operations fiq_tty_driver_ops = {
1322 	.write = fiq_tty_write,
1323 	.write_room = fiq_tty_write_room,
1324 	.open = fiq_tty_open,
1325 	.close = fiq_tty_close,
1326 #ifdef CONFIG_CONSOLE_POLL
1327 	.poll_init = fiq_tty_poll_init,
1328 	.poll_get_char = fiq_tty_poll_get_char,
1329 	.poll_put_char = fiq_tty_poll_put_char,
1330 #endif
1331 #ifdef CONFIG_PROC_FS
1332 	.proc_show = fiq_tty_proc_show,
1333 #endif
1334 };
1335 
fiq_debugger_tty_init(void)1336 static int fiq_debugger_tty_init(void)
1337 {
1338 	int ret;
1339 	struct fiq_debugger_state **states = NULL;
1340 	struct tty_driver *drv;
1341 
1342 	states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
1343 	if (!states) {
1344 		pr_err("Failed to allocate fiq debugger state structres\n");
1345 		return -ENOMEM;
1346 	}
1347 
1348 	drv = tty_alloc_driver(MAX_FIQ_DEBUGGER_PORTS, TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV);
1349 	if (IS_ERR(drv)) {
1350 		pr_err("Failed to allocate fiq debugger tty\n");
1351 		ret = -ENOMEM;
1352 		goto err_free_state;
1353 	}
1354 	fiq_tty_driver = drv;
1355 
1356 	fiq_tty_driver->owner		= THIS_MODULE;
1357 	fiq_tty_driver->driver_name	= "fiq-debugger";
1358 	fiq_tty_driver->name		= "ttyFIQ";
1359 	fiq_tty_driver->type		= TTY_DRIVER_TYPE_SERIAL;
1360 	fiq_tty_driver->subtype		= SERIAL_TYPE_NORMAL;
1361 	fiq_tty_driver->init_termios	= tty_std_termios;
1362 	fiq_tty_driver->driver_state	= states;
1363 
1364 	fiq_tty_driver->init_termios.c_cflag =
1365 					B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1366 	fiq_tty_driver->init_termios.c_ispeed = 115200;
1367 	fiq_tty_driver->init_termios.c_ospeed = 115200;
1368 
1369 	tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
1370 
1371 	ret = tty_register_driver(fiq_tty_driver);
1372 	if (ret) {
1373 		pr_err("Failed to register fiq tty: %d\n", ret);
1374 		goto err_free_tty;
1375 	}
1376 
1377 	pr_info("Registered FIQ tty driver\n");
1378 	return 0;
1379 
1380 err_free_tty:
1381 	put_tty_driver(fiq_tty_driver);
1382 	fiq_tty_driver = NULL;
1383 err_free_state:
1384 	kfree(states);
1385 	return ret;
1386 }
1387 
fiq_debugger_tty_init_one(struct fiq_debugger_state * state)1388 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
1389 {
1390 	int ret;
1391 	struct device *tty_dev;
1392 	struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
1393 
1394 	states[state->pdev->id] = state;
1395 
1396 	state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1397 	if (!state->tty_rbuf) {
1398 		pr_err("Failed to allocate fiq debugger ringbuf\n");
1399 		ret = -ENOMEM;
1400 		goto err;
1401 	}
1402 
1403 	tty_port_init(&state->tty_port);
1404 	state->tty_port.ops = &fiq_tty_port_ops;
1405 
1406 	tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
1407 					   state->pdev->id, &state->pdev->dev);
1408 	if (IS_ERR(tty_dev)) {
1409 		pr_err("Failed to register fiq debugger tty device\n");
1410 		ret = PTR_ERR(tty_dev);
1411 		goto err;
1412 	}
1413 
1414 	device_set_wakeup_capable(tty_dev, 1);
1415 
1416 	pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1417 
1418 	return 0;
1419 
1420 err:
1421 	fiq_debugger_ringbuf_free(state->tty_rbuf);
1422 	state->tty_rbuf = NULL;
1423 	return ret;
1424 }
1425 #endif
1426 
fiq_debugger_dev_suspend(struct device * dev)1427 static int fiq_debugger_dev_suspend(struct device *dev)
1428 {
1429 	struct platform_device *pdev = to_platform_device(dev);
1430 	struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1431 
1432 	if (state->pdata->uart_dev_suspend)
1433 		return state->pdata->uart_dev_suspend(pdev);
1434 	return 0;
1435 }
1436 
fiq_debugger_dev_resume(struct device * dev)1437 static int fiq_debugger_dev_resume(struct device *dev)
1438 {
1439 	struct platform_device *pdev = to_platform_device(dev);
1440 	struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1441 
1442 	if (state->pdata->uart_dev_resume)
1443 		return state->pdata->uart_dev_resume(pdev);
1444 	return 0;
1445 }
1446 
fiq_debugger_probe(struct platform_device * pdev)1447 static int fiq_debugger_probe(struct platform_device *pdev)
1448 {
1449 	int ret;
1450 	struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1451 	struct fiq_debugger_state *state;
1452 	int fiq;
1453 	int uart_irq;
1454 
1455 	if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1456 		return -EINVAL;
1457 
1458 	if (!pdata->uart_getc || !pdata->uart_putc)
1459 		return -EINVAL;
1460 	if ((pdata->uart_enable && !pdata->uart_disable) ||
1461 	    (!pdata->uart_enable && pdata->uart_disable))
1462 		return -EINVAL;
1463 
1464 	fiq = platform_get_irq_byname(pdev, "fiq");
1465 	uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1466 
1467 #ifndef CONFIG_ARCH_ROCKCHIP
1468 	/* uart_irq mode and fiq mode are mutually exclusive, but one of them
1469 	 * is required */
1470 	if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1471 		return -EINVAL;
1472 	if (fiq >= 0 && !pdata->fiq_enable)
1473 		return -EINVAL;
1474 #endif
1475 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1476 	state->output.printf = fiq_debugger_printf;
1477 	timer_setup(&state->sleep_timer, fiq_debugger_sleep_timer_expired, 0);
1478 	state->pdata = pdata;
1479 	state->pdev = pdev;
1480 	state->no_sleep = initial_no_sleep;
1481 	state->debug_enable = initial_debug_enable;
1482 	state->console_enable = initial_console_enable;
1483 	state->current_cpu = -1;
1484 
1485 	state->fiq = fiq;
1486 	state->uart_irq = uart_irq;
1487 	state->signal_irq = platform_get_irq_byname(pdev, "signal");
1488 	state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1489 
1490 	INIT_WORK(&state->work, fiq_debugger_work);
1491 	spin_lock_init(&state->work_lock);
1492 
1493 	platform_set_drvdata(pdev, state);
1494 
1495 	spin_lock_init(&state->sleep_timer_lock);
1496 
1497 	if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1498 		state->no_sleep = true;
1499 	state->ignore_next_wakeup_irq = !state->no_sleep;
1500 
1501 	state->debugger_wake_src.name = "serial-debug";
1502 	wakeup_source_add(&state->debugger_wake_src);
1503 
1504 #ifdef CONFIG_ARCH_ROCKCHIP
1505 	if (uart_irq < 0 && fiq < 0)
1506 		goto console_out;
1507 #endif
1508 
1509 	state->clk = clk_get(&pdev->dev, NULL);
1510 	if (IS_ERR(state->clk))
1511 		state->clk = NULL;
1512 
1513 	/* do not call pdata->uart_enable here since uart_init may still
1514 	 * need to do some initialization before uart_enable can work.
1515 	 * So, only try to manage the clock during init.
1516 	 */
1517 	if (state->clk)
1518 		clk_enable(state->clk);
1519 
1520 	if (fiq_debugger_have_fiq(state)) {
1521 #ifdef CONFIG_FIQ_GLUE
1522 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
1523 		if (sip_fiq_debugger_is_enabled() ||
1524 		    sdei_fiq_debugger_is_enabled()) {
1525 		} else
1526 #endif
1527 		{
1528 		state->handler.fiq = fiq_debugger_fiq;
1529 		state->handler.resume = fiq_debugger_resume;
1530 		ret = fiq_glue_register_handler(&state->handler);
1531 		if (ret) {
1532 			pr_err("%s: could not install fiq handler\n", __func__);
1533 			goto err_register_irq;
1534 		}
1535 #ifdef CONFIG_ARCH_ROCKCHIP
1536 		/* set state->fiq to secure state, so fiq is available */
1537 		gic_set_irq_secure(irq_get_irq_data(state->fiq));
1538 		/*
1539 		* set state->fiq priority a little higher than other
1540 		* interrupts (normal is 0xa0)
1541 		*/
1542 		gic_set_irq_priority(irq_get_irq_data(state->fiq), 0x90);
1543 #endif
1544 		pdata->fiq_enable(pdev, state->fiq, 1);
1545 		}
1546 #endif
1547 	} else {
1548 		irq_set_status_flags(state->uart_irq, IRQ_NOAUTOEN);
1549 
1550 		if (IS_ENABLED(CONFIG_NO_GKI))
1551 			ret = request_nmi(state->uart_irq, fiq_debugger_uart_irq,
1552 					  IRQF_PERCPU, "debug", state);
1553 		else
1554 			ret = -EINVAL;
1555 		if (ret) {
1556 			pr_err("%s: could not install nmi irq handler\n", __func__);
1557 			irq_clear_status_flags(state->uart_irq, IRQ_NOAUTOEN);
1558 			ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1559 					  IRQF_NO_SUSPEND, "debug", state);
1560 		} else {
1561 			enable_nmi(state->uart_irq);
1562 		}
1563 
1564 		if (ret) {
1565 			pr_err("%s: could not install irq handler\n", __func__);
1566 			goto err_register_irq;
1567 		}
1568 
1569 		/* for irq-only mode, we want this irq to wake us up, if it
1570 		 * can.
1571 		 */
1572 		enable_irq_wake(state->uart_irq);
1573 	}
1574 
1575 	if (state->signal_irq >= 0) {
1576 		ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1577 			  IRQF_TRIGGER_RISING, "debug-signal", state);
1578 		if (ret)
1579 			pr_err("serial_debugger: could not install signal_irq");
1580 	}
1581 
1582 	if (state->wakeup_irq >= 0) {
1583 		ret = request_irq(state->wakeup_irq,
1584 				  fiq_debugger_wakeup_irq_handler,
1585 				  IRQF_TRIGGER_FALLING,
1586 				  "debug-wakeup", state);
1587 		if (ret) {
1588 			pr_err("serial_debugger: "
1589 				"could not install wakeup irq\n");
1590 			state->wakeup_irq = -1;
1591 		} else {
1592 			ret = enable_irq_wake(state->wakeup_irq);
1593 			if (ret) {
1594 				pr_err("serial_debugger: "
1595 					"could not enable wakeup\n");
1596 				state->wakeup_irq_no_set_wake = true;
1597 			}
1598 		}
1599 	}
1600 	if (state->no_sleep)
1601 		fiq_debugger_handle_wakeup(state);
1602 
1603 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
1604 	state_tf = state;
1605 #endif
1606 
1607 	if (pdata->uart_init) {
1608 		ret = pdata->uart_init(pdev);
1609 		if (ret)
1610 			goto err_uart_init;
1611 	}
1612 
1613 	if (state->clk)
1614 		clk_disable(state->clk);
1615 #ifdef CONFIG_ARCH_ROCKCHIP
1616 console_out:
1617 #endif
1618 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1619 	spin_lock_init(&state->console_lock);
1620 	state->console = fiq_debugger_console;
1621 	state->console.index = pdev->id;
1622 #ifndef CONFIG_FIQ_DEBUGGER_MODULE
1623 	if (!console_set_on_cmdline)
1624 		add_preferred_console(state->console.name,
1625 			state->console.index, NULL);
1626 #endif
1627 	register_console(&state->console);
1628 	fiq_debugger_tty_init_one(state);
1629 #endif
1630 
1631 	/* switch to cpu0 default */
1632 	fiq_debugger_switch_cpu(state, 0);
1633 
1634 	return 0;
1635 
1636 err_register_irq:
1637 	if (pdata->uart_free)
1638 		pdata->uart_free(pdev);
1639 err_uart_init:
1640 	if (state->clk)
1641 		clk_disable(state->clk);
1642 	if (state->clk)
1643 		clk_put(state->clk);
1644 	wakeup_source_remove(&state->debugger_wake_src);
1645 	__pm_relax(&state->debugger_wake_src);
1646 	platform_set_drvdata(pdev, NULL);
1647 	kfree(state);
1648 	return ret;
1649 }
1650 
1651 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1652 	.suspend	= fiq_debugger_dev_suspend,
1653 	.resume		= fiq_debugger_dev_resume,
1654 };
1655 
1656 static struct platform_driver fiq_debugger_driver = {
1657 	.probe	= fiq_debugger_probe,
1658 	.driver	= {
1659 		.name	= "fiq_debugger",
1660 		.pm	= &fiq_debugger_dev_pm_ops,
1661 	},
1662 };
1663 
1664 #if defined(CONFIG_FIQ_DEBUGGER_UART_OVERLAY)
fiq_debugger_uart_overlay(void)1665 int fiq_debugger_uart_overlay(void)
1666 {
1667 	struct device_node *onp = of_find_node_by_path("/uart_overlay@0");
1668 	int ret;
1669 
1670 	if (!onp) {
1671 		pr_err("serial_debugger: uart overlay not found\n");
1672 		return -ENODEV;
1673 	}
1674 
1675 	ret = of_overlay_create(onp);
1676 	if (ret < 0) {
1677 		pr_err("serial_debugger: fail to create overlay: %d\n", ret);
1678 		of_node_put(onp);
1679 		return ret;
1680 	}
1681 
1682 	pr_info("serial_debugger: uart overlay applied\n");
1683 	return 0;
1684 }
1685 #endif
1686 
fiq_debugger_init(void)1687 static int __init fiq_debugger_init(void)
1688 {
1689 	if (fiq_debugger_disable) {
1690 		pr_err("serial_debugger: disabled\n");
1691 		return -ENODEV;
1692 	}
1693 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1694 	fiq_debugger_tty_init();
1695 #endif
1696 #if defined(CONFIG_FIQ_DEBUGGER_UART_OVERLAY)
1697 	fiq_debugger_uart_overlay();
1698 #endif
1699 	return platform_driver_register(&fiq_debugger_driver);
1700 }
1701 
1702 postcore_initcall(fiq_debugger_init);
1703 
fiq_debugger_exit(void)1704 static void __exit fiq_debugger_exit(void)
1705 {
1706 	platform_driver_unregister(&fiq_debugger_driver);
1707 }
1708 module_exit(fiq_debugger_exit);
1709 
1710 MODULE_DESCRIPTION("FIQ Debugger");
1711 MODULE_LICENSE("GPL");
1712 MODULE_ALIAS("platform:fiq-debugger");
1713