1 /*
2 * arch/arm/common/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 */
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.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/wakelock.h>
38 #include <asm/fiq_debugger.h>
39 #include <asm/fiq_glue.h>
40 #include <asm/stacktrace.h>
42 #include <linux/uaccess.h>
44 #include "fiq_debugger_ringbuf.h"
46 #define DEBUG_MAX 64
47 #define MAX_UNHANDLED_FIQ_COUNT 1000000
49 #define MAX_FIQ_DEBUGGER_PORTS 4
51 #define THREAD_INFO(sp) ((struct thread_info *) \
52 ((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
54 struct fiq_debugger_state {
55 struct fiq_glue_handler handler;
57 int fiq;
58 int uart_irq;
59 int signal_irq;
60 int wakeup_irq;
61 bool wakeup_irq_no_set_wake;
62 struct clk *clk;
63 struct fiq_debugger_pdata *pdata;
64 struct platform_device *pdev;
66 char debug_cmd[DEBUG_MAX];
67 int debug_busy;
68 int debug_abort;
70 char debug_buf[DEBUG_MAX];
71 int debug_count;
73 bool no_sleep;
74 bool debug_enable;
75 bool ignore_next_wakeup_irq;
76 struct timer_list sleep_timer;
77 spinlock_t sleep_timer_lock;
78 bool uart_enabled;
79 struct wake_lock debugger_wake_lock;
80 bool console_enable;
81 int current_cpu;
82 atomic_t unhandled_fiq_count;
83 bool in_fiq;
85 struct work_struct work;
86 spinlock_t work_lock;
87 char work_cmd[DEBUG_MAX];
89 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
90 spinlock_t console_lock;
91 struct console console;
92 struct tty_port tty_port;
93 struct fiq_debugger_ringbuf *tty_rbuf;
94 bool syslog_dumping;
95 #endif
97 unsigned int last_irqs[NR_IRQS];
98 unsigned int last_local_timer_irqs[NR_CPUS];
99 };
101 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
102 struct tty_driver *fiq_tty_driver;
103 #endif
105 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
106 static bool initial_no_sleep = true;
107 #else
108 static bool initial_no_sleep;
109 #endif
111 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
112 static bool initial_debug_enable = true;
113 static bool initial_console_enable = true;
114 #else
115 static bool initial_debug_enable;
116 static bool initial_console_enable;
117 #endif
119 static bool fiq_kgdb_enable;
121 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
122 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
123 module_param_named(console_enable, initial_console_enable, bool, 0644);
124 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
126 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
127 static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
128 static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
129 #else
130 static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
131 {
132 if (state->wakeup_irq < 0)
133 return;
134 enable_irq(state->wakeup_irq);
135 if (!state->wakeup_irq_no_set_wake)
136 enable_irq_wake(state->wakeup_irq);
137 }
138 static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
139 {
140 if (state->wakeup_irq < 0)
141 return;
142 disable_irq_nosync(state->wakeup_irq);
143 if (!state->wakeup_irq_no_set_wake)
144 disable_irq_wake(state->wakeup_irq);
145 }
146 #endif
148 static bool inline debug_have_fiq(struct fiq_debugger_state *state)
149 {
150 return (state->fiq >= 0);
151 }
153 static void debug_force_irq(struct fiq_debugger_state *state)
154 {
155 unsigned int irq = state->signal_irq;
157 if (WARN_ON(!debug_have_fiq(state)))
158 return;
159 if (state->pdata->force_irq) {
160 state->pdata->force_irq(state->pdev, irq);
161 } else {
162 struct irq_chip *chip = irq_get_chip(irq);
163 if (chip && chip->irq_retrigger)
164 chip->irq_retrigger(irq_get_irq_data(irq));
165 }
166 }
168 static void debug_uart_enable(struct fiq_debugger_state *state)
169 {
170 if (state->clk)
171 clk_enable(state->clk);
172 if (state->pdata->uart_enable)
173 state->pdata->uart_enable(state->pdev);
174 }
176 static void debug_uart_disable(struct fiq_debugger_state *state)
177 {
178 if (state->pdata->uart_disable)
179 state->pdata->uart_disable(state->pdev);
180 if (state->clk)
181 clk_disable(state->clk);
182 }
184 static void debug_uart_flush(struct fiq_debugger_state *state)
185 {
186 if (state->pdata->uart_flush)
187 state->pdata->uart_flush(state->pdev);
188 }
190 static void debug_putc(struct fiq_debugger_state *state, char c)
191 {
192 state->pdata->uart_putc(state->pdev, c);
193 }
195 static void debug_puts(struct fiq_debugger_state *state, char *s)
196 {
197 unsigned c;
198 while ((c = *s++)) {
199 if (c == '\n')
200 debug_putc(state, '\r');
201 debug_putc(state, c);
202 }
203 }
205 static void debug_prompt(struct fiq_debugger_state *state)
206 {
207 debug_puts(state, "debug> ");
208 }
210 static void dump_kernel_log(struct fiq_debugger_state *state)
211 {
212 char buf[512];
213 size_t len;
214 struct kmsg_dumper dumper = { .active = true };
217 kmsg_dump_rewind_nolock(&dumper);
218 while (kmsg_dump_get_line_nolock(&dumper, true, buf,
219 sizeof(buf) - 1, &len)) {
220 buf[len] = 0;
221 debug_puts(state, buf);
222 }
223 }
225 static char *mode_name(unsigned cpsr)
226 {
227 switch (cpsr & MODE_MASK) {
228 case USR_MODE: return "USR";
229 case FIQ_MODE: return "FIQ";
230 case IRQ_MODE: return "IRQ";
231 case SVC_MODE: return "SVC";
232 case ABT_MODE: return "ABT";
233 case UND_MODE: return "UND";
234 case SYSTEM_MODE: return "SYS";
235 default: return "???";
236 }
237 }
239 static int debug_printf(void *cookie, const char *fmt, ...)
240 {
241 struct fiq_debugger_state *state = cookie;
242 char buf[256];
243 va_list ap;
245 va_start(ap, fmt);
246 vsnprintf(buf, sizeof(buf), fmt, ap);
247 va_end(ap);
249 debug_puts(state, buf);
250 return state->debug_abort;
251 }
253 /* Safe outside fiq context */
254 static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
255 {
256 struct fiq_debugger_state *state = cookie;
257 char buf[256];
258 va_list ap;
259 unsigned long irq_flags;
261 va_start(ap, fmt);
262 vsnprintf(buf, 128, fmt, ap);
263 va_end(ap);
265 local_irq_save(irq_flags);
266 debug_puts(state, buf);
267 debug_uart_flush(state);
268 local_irq_restore(irq_flags);
269 return state->debug_abort;
270 }
272 static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
273 {
274 debug_printf(state, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
275 regs[0], regs[1], regs[2], regs[3]);
276 debug_printf(state, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
277 regs[4], regs[5], regs[6], regs[7]);
278 debug_printf(state, " r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
279 regs[8], regs[9], regs[10], regs[11],
280 mode_name(regs[16]));
281 if ((regs[16] & MODE_MASK) == USR_MODE)
282 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
283 "cpsr %08x\n", regs[12], regs[13], regs[14],
284 regs[15], regs[16]);
285 else
286 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
287 "cpsr %08x spsr %08x\n", regs[12], regs[13],
288 regs[14], regs[15], regs[16], regs[17]);
289 }
291 struct mode_regs {
292 unsigned long sp_svc;
293 unsigned long lr_svc;
294 unsigned long spsr_svc;
296 unsigned long sp_abt;
297 unsigned long lr_abt;
298 unsigned long spsr_abt;
300 unsigned long sp_und;
301 unsigned long lr_und;
302 unsigned long spsr_und;
304 unsigned long sp_irq;
305 unsigned long lr_irq;
306 unsigned long spsr_irq;
308 unsigned long r8_fiq;
309 unsigned long r9_fiq;
310 unsigned long r10_fiq;
311 unsigned long r11_fiq;
312 unsigned long r12_fiq;
313 unsigned long sp_fiq;
314 unsigned long lr_fiq;
315 unsigned long spsr_fiq;
316 };
318 void __naked get_mode_regs(struct mode_regs *regs)
319 {
320 asm volatile (
321 "mrs r1, cpsr\n"
322 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
323 "stmia r0!, {r13 - r14}\n"
324 "mrs r2, spsr\n"
325 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
326 "stmia r0!, {r2, r13 - r14}\n"
327 "mrs r2, spsr\n"
328 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
329 "stmia r0!, {r2, r13 - r14}\n"
330 "mrs r2, spsr\n"
331 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
332 "stmia r0!, {r2, r13 - r14}\n"
333 "mrs r2, spsr\n"
334 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
335 "stmia r0!, {r2, r8 - r14}\n"
336 "mrs r2, spsr\n"
337 "stmia r0!, {r2}\n"
338 "msr cpsr_c, r1\n"
339 "bx lr\n");
340 }
343 static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
344 {
345 struct mode_regs mode_regs;
346 dump_regs(state, regs);
347 get_mode_regs(&mode_regs);
348 debug_printf(state, " svc: sp %08x lr %08x spsr %08x\n",
349 mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
350 debug_printf(state, " abt: sp %08x lr %08x spsr %08x\n",
351 mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
352 debug_printf(state, " und: sp %08x lr %08x spsr %08x\n",
353 mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
354 debug_printf(state, " irq: sp %08x lr %08x spsr %08x\n",
355 mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
356 debug_printf(state, " fiq: r8 %08x r9 %08x r10 %08x r11 %08x "
357 "r12 %08x\n",
358 mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
359 mode_regs.r11_fiq, mode_regs.r12_fiq);
360 debug_printf(state, " fiq: sp %08x lr %08x spsr %08x\n",
361 mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
362 }
364 static void dump_irqs(struct fiq_debugger_state *state)
365 {
366 int n;
367 struct irq_desc *desc;
369 debug_printf(state, "irqnr total since-last status name\n");
370 for_each_irq_desc(n, desc) {
371 struct irqaction *act = desc->action;
372 if (!act && !kstat_irqs(n))
373 continue;
374 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
375 kstat_irqs(n),
376 kstat_irqs(n) - state->last_irqs[n],
377 desc->status_use_accessors,
378 (act && act->name) ? act->name : "???");
379 state->last_irqs[n] = kstat_irqs(n);
380 }
381 }
383 struct stacktrace_state {
384 struct fiq_debugger_state *state;
385 unsigned int depth;
386 };
388 static int report_trace(struct stackframe *frame, void *d)
389 {
390 struct stacktrace_state *sts = d;
392 if (sts->depth) {
393 debug_printf(sts->state,
394 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
395 frame->pc, frame->pc, frame->lr, frame->lr,
396 frame->sp, frame->fp);
397 sts->depth--;
398 return 0;
399 }
400 debug_printf(sts->state, " ...\n");
402 return sts->depth == 0;
403 }
405 struct frame_tail {
406 struct frame_tail *fp;
407 unsigned long sp;
408 unsigned long lr;
409 } __attribute__((packed));
411 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
412 struct frame_tail *tail)
413 {
414 struct frame_tail buftail[2];
416 /* Also check accessibility of one struct frame_tail beyond */
417 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
418 debug_printf(state, " invalid frame pointer %p\n", tail);
419 return NULL;
420 }
421 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
422 debug_printf(state,
423 " failed to copy frame pointer %p\n", tail);
424 return NULL;
425 }
427 debug_printf(state, " %p\n", buftail[0].lr);
429 /* frame pointers should strictly progress back up the stack
430 * (towards higher addresses) */
431 if (tail >= buftail[0].fp)
432 return NULL;
434 return buftail[0].fp-1;
435 }
437 void dump_stacktrace(struct fiq_debugger_state *state,
438 struct pt_regs * const regs, unsigned int depth, void *ssp)
439 {
440 struct frame_tail *tail;
441 struct thread_info *real_thread_info = THREAD_INFO(ssp);
442 struct stacktrace_state sts;
444 sts.depth = depth;
445 sts.state = state;
446 *current_thread_info() = *real_thread_info;
448 if (!current)
449 debug_printf(state, "current NULL\n");
450 else
451 debug_printf(state, "pid: %d comm: %s\n",
452 current->pid, current->comm);
453 dump_regs(state, (unsigned *)regs);
455 if (!user_mode(regs)) {
456 struct stackframe frame;
457 frame.fp = regs->ARM_fp;
458 frame.sp = regs->ARM_sp;
459 frame.lr = regs->ARM_lr;
460 frame.pc = regs->ARM_pc;
461 debug_printf(state,
462 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
463 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
464 regs->ARM_sp, regs->ARM_fp);
465 walk_stackframe(&frame, report_trace, &sts);
466 return;
467 }
469 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
470 while (depth-- && tail && !((unsigned long) tail & 3))
471 tail = user_backtrace(state, tail);
472 }
474 static void do_ps(struct fiq_debugger_state *state)
475 {
476 struct task_struct *g;
477 struct task_struct *p;
478 unsigned task_state;
479 static const char stat_nam[] = "RSDTtZX";
481 debug_printf(state, "pid ppid prio task pc\n");
482 read_lock(&tasklist_lock);
483 do_each_thread(g, p) {
484 task_state = p->state ? __ffs(p->state) + 1 : 0;
485 debug_printf(state,
486 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
487 debug_printf(state, "%-13.13s %c", p->comm,
488 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
489 if (task_state == TASK_RUNNING)
490 debug_printf(state, " running\n");
491 else
492 debug_printf(state, " %08lx\n", thread_saved_pc(p));
493 } while_each_thread(g, p);
494 read_unlock(&tasklist_lock);
495 }
497 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
498 static void begin_syslog_dump(struct fiq_debugger_state *state)
499 {
500 state->syslog_dumping = true;
501 }
503 static void end_syslog_dump(struct fiq_debugger_state *state)
504 {
505 state->syslog_dumping = false;
506 }
507 #else
508 extern int do_syslog(int type, char __user *bug, int count);
509 static void begin_syslog_dump(struct fiq_debugger_state *state)
510 {
511 do_syslog(5 /* clear */, NULL, 0);
512 }
514 static void end_syslog_dump(struct fiq_debugger_state *state)
515 {
516 dump_kernel_log(state);
517 }
518 #endif
520 static void do_sysrq(struct fiq_debugger_state *state, char rq)
521 {
522 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
523 debug_printf(state, "sysrq-g blocked\n");
524 return;
525 }
526 begin_syslog_dump(state);
527 handle_sysrq(rq);
528 end_syslog_dump(state);
529 }
531 #ifdef CONFIG_KGDB
532 static void do_kgdb(struct fiq_debugger_state *state)
533 {
534 if (!fiq_kgdb_enable) {
535 debug_printf(state, "kgdb through fiq debugger not enabled\n");
536 return;
537 }
539 debug_printf(state, "enabling console and triggering kgdb\n");
540 state->console_enable = true;
541 handle_sysrq('g');
542 }
543 #endif
545 static void debug_schedule_work(struct fiq_debugger_state *state, char *cmd)
546 {
547 unsigned long flags;
549 spin_lock_irqsave(&state->work_lock, flags);
550 if (state->work_cmd[0] != '\0') {
551 debug_printf(state, "work command processor busy\n");
552 spin_unlock_irqrestore(&state->work_lock, flags);
553 return;
554 }
556 strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
557 spin_unlock_irqrestore(&state->work_lock, flags);
559 schedule_work(&state->work);
560 }
562 static void debug_work(struct work_struct *work)
563 {
564 struct fiq_debugger_state *state;
565 char work_cmd[DEBUG_MAX];
566 char *cmd;
567 unsigned long flags;
569 state = container_of(work, struct fiq_debugger_state, work);
571 spin_lock_irqsave(&state->work_lock, flags);
573 strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
574 state->work_cmd[0] = '\0';
576 spin_unlock_irqrestore(&state->work_lock, flags);
578 cmd = work_cmd;
579 if (!strncmp(cmd, "reboot", 6)) {
580 cmd += 6;
581 while (*cmd == ' ')
582 cmd++;
583 if (cmd != '\0')
584 kernel_restart(cmd);
585 else
586 kernel_restart(NULL);
587 } else {
588 debug_printf(state, "unknown work command '%s'\n", work_cmd);
589 }
590 }
592 /* This function CANNOT be called in FIQ context */
593 static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
594 {
595 if (!strcmp(cmd, "ps"))
596 do_ps(state);
597 if (!strcmp(cmd, "sysrq"))
598 do_sysrq(state, 'h');
599 if (!strncmp(cmd, "sysrq ", 6))
600 do_sysrq(state, cmd[6]);
601 #ifdef CONFIG_KGDB
602 if (!strcmp(cmd, "kgdb"))
603 do_kgdb(state);
604 #endif
605 if (!strncmp(cmd, "reboot", 6))
606 debug_schedule_work(state, cmd);
607 }
609 static void debug_help(struct fiq_debugger_state *state)
610 {
611 debug_printf(state, "FIQ Debugger commands:\n"
612 " pc PC status\n"
613 " regs Register dump\n"
614 " allregs Extended Register dump\n"
615 " bt Stack trace\n"
616 " reboot [<c>] Reboot with command <c>\n"
617 " reset [<c>] Hard reset with command <c>\n"
618 " irqs Interupt status\n"
619 " kmsg Kernel log\n"
620 " version Kernel version\n");
621 debug_printf(state, " sleep Allow sleep while in FIQ\n"
622 " nosleep Disable sleep while in FIQ\n"
623 " console Switch terminal to console\n"
624 " cpu Current CPU\n"
625 " cpu <number> Switch to CPU<number>\n");
626 debug_printf(state, " ps Process list\n"
627 " sysrq sysrq options\n"
628 " sysrq <param> Execute sysrq with <param>\n");
629 #ifdef CONFIG_KGDB
630 debug_printf(state, " kgdb Enter kernel debugger\n");
631 #endif
632 }
634 static void take_affinity(void *info)
635 {
636 struct fiq_debugger_state *state = info;
637 struct cpumask cpumask;
639 cpumask_clear(&cpumask);
640 cpumask_set_cpu(get_cpu(), &cpumask);
642 irq_set_affinity(state->uart_irq, &cpumask);
643 }
645 static void switch_cpu(struct fiq_debugger_state *state, int cpu)
646 {
647 if (!debug_have_fiq(state))
648 smp_call_function_single(cpu, take_affinity, state, false);
649 state->current_cpu = cpu;
650 }
652 static bool debug_fiq_exec(struct fiq_debugger_state *state,
653 const char *cmd, unsigned *regs, void *svc_sp)
654 {
655 bool signal_helper = false;
657 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
658 debug_help(state);
659 } else if (!strcmp(cmd, "pc")) {
660 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
661 regs[15], regs[16], mode_name(regs[16]));
662 } else if (!strcmp(cmd, "regs")) {
663 dump_regs(state, regs);
664 } else if (!strcmp(cmd, "allregs")) {
665 dump_allregs(state, regs);
666 } else if (!strcmp(cmd, "bt")) {
667 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
668 } else if (!strncmp(cmd, "reset", 5)) {
669 cmd += 5;
670 while (*cmd == ' ')
671 cmd++;
672 if (*cmd) {
673 char tmp_cmd[32];
674 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
675 machine_restart(tmp_cmd);
676 } else {
677 machine_restart(NULL);
678 }
679 } else if (!strcmp(cmd, "irqs")) {
680 dump_irqs(state);
681 } else if (!strcmp(cmd, "kmsg")) {
682 dump_kernel_log(state);
683 } else if (!strcmp(cmd, "version")) {
684 debug_printf(state, "%s\n", linux_banner);
685 } else if (!strcmp(cmd, "sleep")) {
686 state->no_sleep = false;
687 debug_printf(state, "enabling sleep\n");
688 } else if (!strcmp(cmd, "nosleep")) {
689 state->no_sleep = true;
690 debug_printf(state, "disabling sleep\n");
691 } else if (!strcmp(cmd, "console")) {
692 debug_printf(state, "console mode\n");
693 debug_uart_flush(state);
694 state->console_enable = true;
695 } else if (!strcmp(cmd, "cpu")) {
696 debug_printf(state, "cpu %d\n", state->current_cpu);
697 } else if (!strncmp(cmd, "cpu ", 4)) {
698 unsigned long cpu = 0;
699 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
700 switch_cpu(state, cpu);
701 else
702 debug_printf(state, "invalid cpu\n");
703 debug_printf(state, "cpu %d\n", state->current_cpu);
704 } else {
705 if (state->debug_busy) {
706 debug_printf(state,
707 "command processor busy. trying to abort.\n");
708 state->debug_abort = -1;
709 } else {
710 strcpy(state->debug_cmd, cmd);
711 state->debug_busy = 1;
712 }
714 return true;
715 }
716 if (!state->console_enable)
717 debug_prompt(state);
719 return signal_helper;
720 }
722 static void sleep_timer_expired(unsigned long data)
723 {
724 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
725 unsigned long flags;
727 spin_lock_irqsave(&state->sleep_timer_lock, flags);
728 if (state->uart_enabled && !state->no_sleep) {
729 if (state->debug_enable && !state->console_enable) {
730 state->debug_enable = false;
731 debug_printf_nfiq(state, "suspending fiq debugger\n");
732 }
733 state->ignore_next_wakeup_irq = true;
734 debug_uart_disable(state);
735 state->uart_enabled = false;
736 enable_wakeup_irq(state);
737 }
738 wake_unlock(&state->debugger_wake_lock);
739 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
740 }
742 static void handle_wakeup(struct fiq_debugger_state *state)
743 {
744 unsigned long flags;
746 spin_lock_irqsave(&state->sleep_timer_lock, flags);
747 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
748 state->ignore_next_wakeup_irq = false;
749 } else if (!state->uart_enabled) {
750 wake_lock(&state->debugger_wake_lock);
751 debug_uart_enable(state);
752 state->uart_enabled = true;
753 disable_wakeup_irq(state);
754 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
755 }
756 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
757 }
759 static irqreturn_t wakeup_irq_handler(int irq, void *dev)
760 {
761 struct fiq_debugger_state *state = dev;
763 if (!state->no_sleep)
764 debug_puts(state, "WAKEUP\n");
765 handle_wakeup(state);
767 return IRQ_HANDLED;
768 }
770 static void debug_handle_console_irq_context(struct fiq_debugger_state *state)
771 {
772 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
773 struct tty_struct *tty;
775 tty = tty_port_tty_get(&state->tty_port);
776 if (tty) {
777 int i;
778 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
779 for (i = 0; i < count; i++) {
780 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
781 tty_insert_flip_char(tty, c, TTY_NORMAL);
782 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
783 pr_warn("fiq tty failed to consume byte\n");
784 }
785 tty_flip_buffer_push(tty);
786 tty_kref_put(tty);
787 }
788 #endif
789 }
791 static void debug_handle_irq_context(struct fiq_debugger_state *state)
792 {
793 if (!state->no_sleep) {
794 unsigned long flags;
796 spin_lock_irqsave(&state->sleep_timer_lock, flags);
797 wake_lock(&state->debugger_wake_lock);
798 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
799 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
800 }
801 debug_handle_console_irq_context(state);
802 if (state->debug_busy) {
803 debug_irq_exec(state, state->debug_cmd);
804 if (!state->console_enable)
805 debug_prompt(state);
806 state->debug_busy = 0;
807 }
808 }
810 static int debug_getc(struct fiq_debugger_state *state)
811 {
812 return state->pdata->uart_getc(state->pdev);
813 }
815 static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
816 int this_cpu, void *regs, void *svc_sp)
817 {
818 int c;
819 static int last_c;
820 int count = 0;
821 bool signal_helper = false;
823 if (this_cpu != state->current_cpu) {
824 if (state->in_fiq)
825 return false;
827 if (atomic_inc_return(&state->unhandled_fiq_count) !=
828 MAX_UNHANDLED_FIQ_COUNT)
829 return false;
831 debug_printf(state, "fiq_debugger: cpu %d not responding, "
832 "reverting to cpu %d\n", state->current_cpu,
833 this_cpu);
835 atomic_set(&state->unhandled_fiq_count, 0);
836 switch_cpu(state, this_cpu);
837 return false;
838 }
840 state->in_fiq = true;
842 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
843 count++;
844 if (!state->debug_enable) {
845 if ((c == 13) || (c == 10)) {
846 state->debug_enable = true;
847 state->debug_count = 0;
848 debug_prompt(state);
849 }
850 } else if (c == FIQ_DEBUGGER_BREAK) {
851 state->console_enable = false;
852 debug_puts(state, "fiq debugger mode\n");
853 state->debug_count = 0;
854 debug_prompt(state);
855 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
856 } else if (state->console_enable && state->tty_rbuf) {
857 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
858 signal_helper = true;
859 #endif
860 } else if ((c >= ' ') && (c < 127)) {
861 if (state->debug_count < (DEBUG_MAX - 1)) {
862 state->debug_buf[state->debug_count++] = c;
863 debug_putc(state, c);
864 }
865 } else if ((c == 8) || (c == 127)) {
866 if (state->debug_count > 0) {
867 state->debug_count--;
868 debug_putc(state, 8);
869 debug_putc(state, ' ');
870 debug_putc(state, 8);
871 }
872 } else if ((c == 13) || (c == 10)) {
873 if (c == '\r' || (c == '\n' && last_c != '\r')) {
874 debug_putc(state, '\r');
875 debug_putc(state, '\n');
876 }
877 if (state->debug_count) {
878 state->debug_buf[state->debug_count] = 0;
879 state->debug_count = 0;
880 signal_helper |=
881 debug_fiq_exec(state, state->debug_buf,
882 regs, svc_sp);
883 } else {
884 debug_prompt(state);
885 }
886 }
887 last_c = c;
888 }
889 if (!state->console_enable)
890 debug_uart_flush(state);
891 if (state->pdata->fiq_ack)
892 state->pdata->fiq_ack(state->pdev, state->fiq);
894 /* poke sleep timer if necessary */
895 if (state->debug_enable && !state->no_sleep)
896 signal_helper = true;
898 atomic_set(&state->unhandled_fiq_count, 0);
899 state->in_fiq = false;
901 return signal_helper;
902 }
904 static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
905 {
906 struct fiq_debugger_state *state =
907 container_of(h, struct fiq_debugger_state, handler);
908 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
909 bool need_irq;
911 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
912 if (need_irq)
913 debug_force_irq(state);
914 }
916 /*
917 * When not using FIQs, we only use this single interrupt as an entry point.
918 * This just effectively takes over the UART interrupt and does all the work
919 * in this context.
920 */
921 static irqreturn_t debug_uart_irq(int irq, void *dev)
922 {
923 struct fiq_debugger_state *state = dev;
924 bool not_done;
926 handle_wakeup(state);
928 /* handle the debugger irq in regular context */
929 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
930 get_irq_regs(),
931 current_thread_info());
932 if (not_done)
933 debug_handle_irq_context(state);
935 return IRQ_HANDLED;
936 }
938 /*
939 * If FIQs are used, not everything can happen in fiq context.
940 * FIQ handler does what it can and then signals this interrupt to finish the
941 * job in irq context.
942 */
943 static irqreturn_t debug_signal_irq(int irq, void *dev)
944 {
945 struct fiq_debugger_state *state = dev;
947 if (state->pdata->force_irq_ack)
948 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
950 debug_handle_irq_context(state);
952 return IRQ_HANDLED;
953 }
955 static void debug_resume(struct fiq_glue_handler *h)
956 {
957 struct fiq_debugger_state *state =
958 container_of(h, struct fiq_debugger_state, handler);
959 if (state->pdata->uart_resume)
960 state->pdata->uart_resume(state->pdev);
961 }
963 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
964 struct tty_driver *debug_console_device(struct console *co, int *index)
965 {
966 *index = co->index;
967 return fiq_tty_driver;
968 }
970 static void debug_console_write(struct console *co,
971 const char *s, unsigned int count)
972 {
973 struct fiq_debugger_state *state;
974 unsigned long flags;
976 state = container_of(co, struct fiq_debugger_state, console);
978 if (!state->console_enable && !state->syslog_dumping)
979 return;
981 debug_uart_enable(state);
982 spin_lock_irqsave(&state->console_lock, flags);
983 while (count--) {
984 if (*s == '\n')
985 debug_putc(state, '\r');
986 debug_putc(state, *s++);
987 }
988 debug_uart_flush(state);
989 spin_unlock_irqrestore(&state->console_lock, flags);
990 debug_uart_disable(state);
991 }
993 static struct console fiq_debugger_console = {
994 .name = "ttyFIQ",
995 .device = debug_console_device,
996 .write = debug_console_write,
997 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
998 };
1000 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
1001 {
1002 int line = tty->index;
1003 struct fiq_debugger_state **states = tty->driver->driver_state;
1004 struct fiq_debugger_state *state = states[line];
1006 return tty_port_open(&state->tty_port, tty, filp);
1007 }
1009 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
1010 {
1011 tty_port_close(tty->port, tty, filp);
1012 }
1014 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1015 {
1016 int i;
1017 int line = tty->index;
1018 struct fiq_debugger_state **states = tty->driver->driver_state;
1019 struct fiq_debugger_state *state = states[line];
1021 if (!state->console_enable)
1022 return count;
1024 debug_uart_enable(state);
1025 spin_lock_irq(&state->console_lock);
1026 for (i = 0; i < count; i++)
1027 debug_putc(state, *buf++);
1028 spin_unlock_irq(&state->console_lock);
1029 debug_uart_disable(state);
1031 return count;
1032 }
1034 int fiq_tty_write_room(struct tty_struct *tty)
1035 {
1036 return 16;
1037 }
1039 #ifdef CONFIG_CONSOLE_POLL
1040 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
1041 {
1042 return 0;
1043 }
1045 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
1046 {
1047 struct fiq_debugger_state **states = driver->driver_state;
1048 struct fiq_debugger_state *state = states[line];
1049 int c = NO_POLL_CHAR;
1051 debug_uart_enable(state);
1052 if (debug_have_fiq(state)) {
1053 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1054 if (count > 0) {
1055 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1056 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1057 }
1058 } else {
1059 c = debug_getc(state);
1060 if (c == FIQ_DEBUGGER_NO_CHAR)
1061 c = NO_POLL_CHAR;
1062 }
1063 debug_uart_disable(state);
1065 return c;
1066 }
1068 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1069 {
1070 struct fiq_debugger_state **states = driver->driver_state;
1071 struct fiq_debugger_state *state = states[line];
1072 debug_uart_enable(state);
1073 debug_putc(state, ch);
1074 debug_uart_disable(state);
1075 }
1076 #endif
1078 static const struct tty_port_operations fiq_tty_port_ops;
1080 static const struct tty_operations fiq_tty_driver_ops = {
1081 .write = fiq_tty_write,
1082 .write_room = fiq_tty_write_room,
1083 .open = fiq_tty_open,
1084 .close = fiq_tty_close,
1085 #ifdef CONFIG_CONSOLE_POLL
1086 .poll_init = fiq_tty_poll_init,
1087 .poll_get_char = fiq_tty_poll_get_char,
1088 .poll_put_char = fiq_tty_poll_put_char,
1089 #endif
1090 };
1092 static int fiq_debugger_tty_init(void)
1093 {
1094 int ret;
1095 struct fiq_debugger_state **states = NULL;
1097 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
1098 if (!states) {
1099 pr_err("Failed to allocate fiq debugger state structres\n");
1100 return -ENOMEM;
1101 }
1103 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
1104 if (!fiq_tty_driver) {
1105 pr_err("Failed to allocate fiq debugger tty\n");
1106 ret = -ENOMEM;
1107 goto err_free_state;
1108 }
1110 fiq_tty_driver->owner = THIS_MODULE;
1111 fiq_tty_driver->driver_name = "fiq-debugger";
1112 fiq_tty_driver->name = "ttyFIQ";
1113 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1114 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1115 fiq_tty_driver->init_termios = tty_std_termios;
1116 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1117 TTY_DRIVER_DYNAMIC_DEV;
1118 fiq_tty_driver->driver_state = states;
1120 fiq_tty_driver->init_termios.c_cflag =
1121 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1122 fiq_tty_driver->init_termios.c_ispeed = 115200;
1123 fiq_tty_driver->init_termios.c_ospeed = 115200;
1125 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
1127 ret = tty_register_driver(fiq_tty_driver);
1128 if (ret) {
1129 pr_err("Failed to register fiq tty: %d\n", ret);
1130 goto err_free_tty;
1131 }
1133 pr_info("Registered FIQ tty driver\n");
1134 return 0;
1136 err_free_tty:
1137 put_tty_driver(fiq_tty_driver);
1138 fiq_tty_driver = NULL;
1139 err_free_state:
1140 kfree(states);
1141 return ret;
1142 }
1144 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
1145 {
1146 int ret;
1147 struct device *tty_dev;
1148 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
1150 states[state->pdev->id] = state;
1152 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1153 if (!state->tty_rbuf) {
1154 pr_err("Failed to allocate fiq debugger ringbuf\n");
1155 ret = -ENOMEM;
1156 goto err;
1157 }
1159 tty_port_init(&state->tty_port);
1160 state->tty_port.ops = &fiq_tty_port_ops;
1162 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
1163 state->pdev->id, &state->pdev->dev);
1164 if (IS_ERR(tty_dev)) {
1165 pr_err("Failed to register fiq debugger tty device\n");
1166 ret = PTR_ERR(tty_dev);
1167 goto err;
1168 }
1170 device_set_wakeup_capable(tty_dev, 1);
1172 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1174 return 0;
1176 err:
1177 fiq_debugger_ringbuf_free(state->tty_rbuf);
1178 state->tty_rbuf = NULL;
1179 return ret;
1180 }
1181 #endif
1183 static int fiq_debugger_dev_suspend(struct device *dev)
1184 {
1185 struct platform_device *pdev = to_platform_device(dev);
1186 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1188 if (state->pdata->uart_dev_suspend)
1189 return state->pdata->uart_dev_suspend(pdev);
1190 return 0;
1191 }
1193 static int fiq_debugger_dev_resume(struct device *dev)
1194 {
1195 struct platform_device *pdev = to_platform_device(dev);
1196 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1198 if (state->pdata->uart_dev_resume)
1199 return state->pdata->uart_dev_resume(pdev);
1200 return 0;
1201 }
1203 static int fiq_debugger_probe(struct platform_device *pdev)
1204 {
1205 int ret;
1206 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1207 struct fiq_debugger_state *state;
1208 int fiq;
1209 int uart_irq;
1211 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1212 return -EINVAL;
1214 if (!pdata->uart_getc || !pdata->uart_putc)
1215 return -EINVAL;
1216 if ((pdata->uart_enable && !pdata->uart_disable) ||
1217 (!pdata->uart_enable && pdata->uart_disable))
1218 return -EINVAL;
1220 fiq = platform_get_irq_byname(pdev, "fiq");
1221 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1223 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1224 * is required */
1225 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1226 return -EINVAL;
1227 if (fiq >= 0 && !pdata->fiq_enable)
1228 return -EINVAL;
1230 state = kzalloc(sizeof(*state), GFP_KERNEL);
1231 setup_timer(&state->sleep_timer, sleep_timer_expired,
1232 (unsigned long)state);
1233 state->pdata = pdata;
1234 state->pdev = pdev;
1235 state->no_sleep = initial_no_sleep;
1236 state->debug_enable = initial_debug_enable;
1237 state->console_enable = initial_console_enable;
1239 state->fiq = fiq;
1240 state->uart_irq = uart_irq;
1241 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1242 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1244 INIT_WORK(&state->work, debug_work);
1245 spin_lock_init(&state->work_lock);
1247 platform_set_drvdata(pdev, state);
1249 spin_lock_init(&state->sleep_timer_lock);
1251 if (state->wakeup_irq < 0 && debug_have_fiq(state))
1252 state->no_sleep = true;
1253 state->ignore_next_wakeup_irq = !state->no_sleep;
1255 wake_lock_init(&state->debugger_wake_lock,
1256 WAKE_LOCK_SUSPEND, "serial-debug");
1258 state->clk = clk_get(&pdev->dev, NULL);
1259 if (IS_ERR(state->clk))
1260 state->clk = NULL;
1262 /* do not call pdata->uart_enable here since uart_init may still
1263 * need to do some initialization before uart_enable can work.
1264 * So, only try to manage the clock during init.
1265 */
1266 if (state->clk)
1267 clk_enable(state->clk);
1269 if (pdata->uart_init) {
1270 ret = pdata->uart_init(pdev);
1271 if (ret)
1272 goto err_uart_init;
1273 }
1275 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1276 state->no_sleep ? "" : "twice ");
1278 if (debug_have_fiq(state)) {
1279 state->handler.fiq = debug_fiq;
1280 state->handler.resume = debug_resume;
1281 ret = fiq_glue_register_handler(&state->handler);
1282 if (ret) {
1283 pr_err("%s: could not install fiq handler\n", __func__);
1284 goto err_register_fiq;
1285 }
1287 pdata->fiq_enable(pdev, state->fiq, 1);
1288 } else {
1289 ret = request_irq(state->uart_irq, debug_uart_irq,
1290 IRQF_NO_SUSPEND, "debug", state);
1291 if (ret) {
1292 pr_err("%s: could not install irq handler\n", __func__);
1293 goto err_register_irq;
1294 }
1296 /* for irq-only mode, we want this irq to wake us up, if it
1297 * can.
1298 */
1299 enable_irq_wake(state->uart_irq);
1300 }
1302 if (state->clk)
1303 clk_disable(state->clk);
1305 if (state->signal_irq >= 0) {
1306 ret = request_irq(state->signal_irq, debug_signal_irq,
1307 IRQF_TRIGGER_RISING, "debug-signal", state);
1308 if (ret)
1309 pr_err("serial_debugger: could not install signal_irq");
1310 }
1312 if (state->wakeup_irq >= 0) {
1313 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1314 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1315 "debug-wakeup", state);
1316 if (ret) {
1317 pr_err("serial_debugger: "
1318 "could not install wakeup irq\n");
1319 state->wakeup_irq = -1;
1320 } else {
1321 ret = enable_irq_wake(state->wakeup_irq);
1322 if (ret) {
1323 pr_err("serial_debugger: "
1324 "could not enable wakeup\n");
1325 state->wakeup_irq_no_set_wake = true;
1326 }
1327 }
1328 }
1329 if (state->no_sleep)
1330 handle_wakeup(state);
1332 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1333 spin_lock_init(&state->console_lock);
1334 state->console = fiq_debugger_console;
1335 state->console.index = pdev->id;
1336 if (!console_set_on_cmdline)
1337 add_preferred_console(state->console.name,
1338 state->console.index, NULL);
1339 register_console(&state->console);
1340 fiq_debugger_tty_init_one(state);
1341 #endif
1342 return 0;
1344 err_register_irq:
1345 err_register_fiq:
1346 if (pdata->uart_free)
1347 pdata->uart_free(pdev);
1348 err_uart_init:
1349 if (state->clk)
1350 clk_disable(state->clk);
1351 if (state->clk)
1352 clk_put(state->clk);
1353 wake_lock_destroy(&state->debugger_wake_lock);
1354 platform_set_drvdata(pdev, NULL);
1355 kfree(state);
1356 return ret;
1357 }
1359 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1360 .suspend = fiq_debugger_dev_suspend,
1361 .resume = fiq_debugger_dev_resume,
1362 };
1364 static struct platform_driver fiq_debugger_driver = {
1365 .probe = fiq_debugger_probe,
1366 .driver = {
1367 .name = "fiq_debugger",
1368 .pm = &fiq_debugger_dev_pm_ops,
1369 },
1370 };
1372 static int __init fiq_debugger_init(void)
1373 {
1374 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1375 fiq_debugger_tty_init();
1376 #endif
1377 return platform_driver_register(&fiq_debugger_driver);
1378 }
1380 postcore_initcall(fiq_debugger_init);