2 * drivers/staging/android/fiq_debugger.c
4 * Serial Debugger Interface accessed through an FIQ interrupt.
6 * Copyright (C) 2008 Google, Inc.
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.
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.
19 #include <linux/module.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 #ifdef CONFIG_FIQ_GLUE
39 #include <asm/fiq_glue.h>
41 #include <asm/stacktrace.h>
43 #include <linux/uaccess.h>
45 #include "fiq_debugger.h"
46 #include "fiq_debugger_ringbuf.h"
49 #define MAX_UNHANDLED_FIQ_COUNT 1000000
51 #define MAX_FIQ_DEBUGGER_PORTS 4
53 #define THREAD_INFO(sp) ((struct thread_info *) \
54 ((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
56 struct fiq_debugger_state {
57 #ifdef CONFIG_FIQ_GLUE
58 struct fiq_glue_handler handler;
65 bool wakeup_irq_no_set_wake;
67 struct fiq_debugger_pdata *pdata;
68 struct platform_device *pdev;
70 char debug_cmd[DEBUG_MAX];
74 char debug_buf[DEBUG_MAX];
79 bool ignore_next_wakeup_irq;
80 struct timer_list sleep_timer;
81 spinlock_t sleep_timer_lock;
83 struct wake_lock debugger_wake_lock;
86 atomic_t unhandled_fiq_count;
89 struct work_struct work;
91 char work_cmd[DEBUG_MAX];
93 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
94 spinlock_t console_lock;
95 struct console console;
96 struct tty_port tty_port;
97 struct fiq_debugger_ringbuf *tty_rbuf;
101 unsigned int last_irqs[NR_IRQS];
102 unsigned int last_local_timer_irqs[NR_CPUS];
105 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
106 struct tty_driver *fiq_tty_driver;
109 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
110 static bool initial_no_sleep = true;
112 static bool initial_no_sleep;
115 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
116 static bool initial_debug_enable = true;
117 static bool initial_console_enable = true;
119 static bool initial_debug_enable;
120 static bool initial_console_enable;
123 static bool fiq_kgdb_enable;
125 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
126 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
127 module_param_named(console_enable, initial_console_enable, bool, 0644);
128 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
130 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
132 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
134 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
137 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
139 if (state->wakeup_irq < 0)
141 enable_irq(state->wakeup_irq);
142 if (!state->wakeup_irq_no_set_wake)
143 enable_irq_wake(state->wakeup_irq);
146 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
148 if (state->wakeup_irq < 0)
150 disable_irq_nosync(state->wakeup_irq);
151 if (!state->wakeup_irq_no_set_wake)
152 disable_irq_wake(state->wakeup_irq);
156 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
158 return (state->fiq >= 0);
161 #ifdef CONFIG_FIQ_GLUE
162 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
164 unsigned int irq = state->signal_irq;
166 if (WARN_ON(!fiq_debugger_have_fiq(state)))
168 if (state->pdata->force_irq) {
169 state->pdata->force_irq(state->pdev, irq);
171 struct irq_chip *chip = irq_get_chip(irq);
172 if (chip && chip->irq_retrigger)
173 chip->irq_retrigger(irq_get_irq_data(irq));
178 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
181 clk_enable(state->clk);
182 if (state->pdata->uart_enable)
183 state->pdata->uart_enable(state->pdev);
186 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
188 if (state->pdata->uart_disable)
189 state->pdata->uart_disable(state->pdev);
191 clk_disable(state->clk);
194 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
196 if (state->pdata->uart_flush)
197 state->pdata->uart_flush(state->pdev);
200 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
202 state->pdata->uart_putc(state->pdev, c);
205 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
210 fiq_debugger_putc(state, '\r');
211 fiq_debugger_putc(state, c);
215 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
217 fiq_debugger_puts(state, "debug> ");
220 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
224 struct kmsg_dumper dumper = { .active = true };
227 kmsg_dump_rewind_nolock(&dumper);
228 while (kmsg_dump_get_line_nolock(&dumper, true, buf,
229 sizeof(buf) - 1, &len)) {
231 fiq_debugger_puts(state, buf);
235 static char *mode_name(unsigned cpsr)
237 switch (cpsr & MODE_MASK) {
238 case USR_MODE: return "USR";
239 case FIQ_MODE: return "FIQ";
240 case IRQ_MODE: return "IRQ";
241 case SVC_MODE: return "SVC";
242 case ABT_MODE: return "ABT";
243 case UND_MODE: return "UND";
244 case SYSTEM_MODE: return "SYS";
245 default: return "???";
249 static int fiq_debugger_printf(void *cookie, const char *fmt, ...)
251 struct fiq_debugger_state *state = cookie;
256 vsnprintf(buf, sizeof(buf), fmt, ap);
259 fiq_debugger_puts(state, buf);
260 return state->debug_abort;
263 /* Safe outside fiq context */
264 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
266 struct fiq_debugger_state *state = cookie;
269 unsigned long irq_flags;
272 vsnprintf(buf, 128, fmt, ap);
275 local_irq_save(irq_flags);
276 fiq_debugger_puts(state, buf);
277 fiq_debugger_uart_flush(state);
278 local_irq_restore(irq_flags);
279 return state->debug_abort;
282 static void fiq_debugger_dump_regs(struct fiq_debugger_state *state,
283 const struct pt_regs *regs)
285 fiq_debugger_printf(state,
286 " r0 %08x r1 %08x r2 %08x r3 %08x\n",
287 regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3);
288 fiq_debugger_printf(state,
289 " r4 %08x r5 %08x r6 %08x r7 %08x\n",
290 regs->ARM_r4, regs->ARM_r5, regs->ARM_r6, regs->ARM_r7);
291 fiq_debugger_printf(state,
292 " r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
293 regs->ARM_r8, regs->ARM_r9, regs->ARM_r10, regs->ARM_fp,
294 mode_name(regs->ARM_cpsr));
295 fiq_debugger_printf(state,
296 " ip %08x sp %08x lr %08x pc %08x cpsr %08x\n",
297 regs->ARM_ip, regs->ARM_sp, regs->ARM_lr, regs->ARM_pc,
302 unsigned long sp_svc;
303 unsigned long lr_svc;
304 unsigned long spsr_svc;
306 unsigned long sp_abt;
307 unsigned long lr_abt;
308 unsigned long spsr_abt;
310 unsigned long sp_und;
311 unsigned long lr_und;
312 unsigned long spsr_und;
314 unsigned long sp_irq;
315 unsigned long lr_irq;
316 unsigned long spsr_irq;
318 unsigned long r8_fiq;
319 unsigned long r9_fiq;
320 unsigned long r10_fiq;
321 unsigned long r11_fiq;
322 unsigned long r12_fiq;
323 unsigned long sp_fiq;
324 unsigned long lr_fiq;
325 unsigned long spsr_fiq;
328 void __naked get_mode_regs(struct mode_regs *regs)
332 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
333 "stmia r0!, {r13 - r14}\n"
335 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
336 "stmia r0!, {r2, r13 - r14}\n"
338 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
339 "stmia r0!, {r2, r13 - r14}\n"
341 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
342 "stmia r0!, {r2, r13 - r14}\n"
344 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
345 "stmia r0!, {r2, r8 - r14}\n"
353 static void fiq_debugger_dump_allregs(struct fiq_debugger_state *state,
354 const struct pt_regs *regs)
356 struct mode_regs mode_regs;
357 unsigned long mode = regs->ARM_cpsr & MODE_MASK;
359 fiq_debugger_dump_regs(state, regs);
360 get_mode_regs(&mode_regs);
362 fiq_debugger_printf(state,
363 "%csvc: sp %08x lr %08x spsr %08x\n",
364 mode == SVC_MODE ? '*' : ' ',
365 mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
366 fiq_debugger_printf(state,
367 "%cabt: sp %08x lr %08x spsr %08x\n",
368 mode == ABT_MODE ? '*' : ' ',
369 mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
370 fiq_debugger_printf(state,
371 "%cund: sp %08x lr %08x spsr %08x\n",
372 mode == UND_MODE ? '*' : ' ',
373 mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
374 fiq_debugger_printf(state,
375 "%cirq: sp %08x lr %08x spsr %08x\n",
376 mode == IRQ_MODE ? '*' : ' ',
377 mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
378 fiq_debugger_printf(state,
379 "%cfiq: r8 %08x r9 %08x r10 %08x r11 %08x r12 %08x\n",
380 mode == FIQ_MODE ? '*' : ' ',
381 mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
382 mode_regs.r11_fiq, mode_regs.r12_fiq);
383 fiq_debugger_printf(state,
384 " fiq: sp %08x lr %08x spsr %08x\n",
385 mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
388 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
391 struct irq_desc *desc;
393 fiq_debugger_printf(state,
394 "irqnr total since-last status name\n");
395 for_each_irq_desc(n, desc) {
396 struct irqaction *act = desc->action;
397 if (!act && !kstat_irqs(n))
399 fiq_debugger_printf(state, "%5d: %10u %11u %8x %s\n", n,
401 kstat_irqs(n) - state->last_irqs[n],
402 desc->status_use_accessors,
403 (act && act->name) ? act->name : "???");
404 state->last_irqs[n] = kstat_irqs(n);
408 struct stacktrace_state {
409 struct fiq_debugger_state *state;
413 static int report_trace(struct stackframe *frame, void *d)
415 struct stacktrace_state *sts = d;
418 fiq_debugger_printf(sts->state,
419 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
420 frame->pc, frame->pc, frame->lr, frame->lr,
421 frame->sp, frame->fp);
425 fiq_debugger_printf(sts->state, " ...\n");
427 return sts->depth == 0;
431 struct frame_tail *fp;
434 } __attribute__((packed));
436 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
437 struct frame_tail *tail)
439 struct frame_tail buftail[2];
441 /* Also check accessibility of one struct frame_tail beyond */
442 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
443 fiq_debugger_printf(state, " invalid frame pointer %p\n",
447 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
448 fiq_debugger_printf(state,
449 " failed to copy frame pointer %p\n", tail);
453 fiq_debugger_printf(state, " %p\n", buftail[0].lr);
455 /* frame pointers should strictly progress back up the stack
456 * (towards higher addresses) */
457 if (tail >= buftail[0].fp)
460 return buftail[0].fp-1;
463 void fiq_debugger_dump_stacktrace(struct fiq_debugger_state *state,
464 const struct pt_regs *regs, unsigned int depth, void *ssp)
466 struct frame_tail *tail;
467 struct thread_info *real_thread_info = THREAD_INFO(ssp);
468 struct stacktrace_state sts;
472 *current_thread_info() = *real_thread_info;
475 fiq_debugger_printf(state, "current NULL\n");
477 fiq_debugger_printf(state, "pid: %d comm: %s\n",
478 current->pid, current->comm);
479 fiq_debugger_dump_regs(state, regs);
481 if (!user_mode(regs)) {
482 struct stackframe frame;
483 frame.fp = regs->ARM_fp;
484 frame.sp = regs->ARM_sp;
485 frame.lr = regs->ARM_lr;
486 frame.pc = regs->ARM_pc;
487 fiq_debugger_printf(state,
488 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
489 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
490 regs->ARM_sp, regs->ARM_fp);
491 walk_stackframe(&frame, report_trace, &sts);
495 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
496 while (depth-- && tail && !((unsigned long) tail & 3))
497 tail = user_backtrace(state, tail);
500 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
502 struct task_struct *g;
503 struct task_struct *p;
505 static const char stat_nam[] = "RSDTtZX";
507 fiq_debugger_printf(state, "pid ppid prio task pc\n");
508 read_lock(&tasklist_lock);
509 do_each_thread(g, p) {
510 task_state = p->state ? __ffs(p->state) + 1 : 0;
511 fiq_debugger_printf(state,
512 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
513 fiq_debugger_printf(state, "%-13.13s %c", p->comm,
514 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
515 if (task_state == TASK_RUNNING)
516 fiq_debugger_printf(state, " running\n");
518 fiq_debugger_printf(state, " %08lx\n",
520 } while_each_thread(g, p);
521 read_unlock(&tasklist_lock);
524 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
525 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
527 state->syslog_dumping = true;
530 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
532 state->syslog_dumping = false;
535 extern int do_syslog(int type, char __user *bug, int count);
536 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
538 do_syslog(5 /* clear */, NULL, 0);
541 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
543 fiq_debugger_dump_kernel_log(state);
547 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
549 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
550 fiq_debugger_printf(state, "sysrq-g blocked\n");
553 fiq_debugger_begin_syslog_dump(state);
555 fiq_debugger_end_syslog_dump(state);
559 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
561 if (!fiq_kgdb_enable) {
562 fiq_debugger_printf(state, "kgdb through fiq debugger not enabled\n");
566 fiq_debugger_printf(state, "enabling console and triggering kgdb\n");
567 state->console_enable = true;
572 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
577 spin_lock_irqsave(&state->work_lock, flags);
578 if (state->work_cmd[0] != '\0') {
579 fiq_debugger_printf(state, "work command processor busy\n");
580 spin_unlock_irqrestore(&state->work_lock, flags);
584 strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
585 spin_unlock_irqrestore(&state->work_lock, flags);
587 schedule_work(&state->work);
590 static void fiq_debugger_work(struct work_struct *work)
592 struct fiq_debugger_state *state;
593 char work_cmd[DEBUG_MAX];
597 state = container_of(work, struct fiq_debugger_state, work);
599 spin_lock_irqsave(&state->work_lock, flags);
601 strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
602 state->work_cmd[0] = '\0';
604 spin_unlock_irqrestore(&state->work_lock, flags);
607 if (!strncmp(cmd, "reboot", 6)) {
614 kernel_restart(NULL);
616 fiq_debugger_printf(state, "unknown work command '%s'\n",
621 /* This function CANNOT be called in FIQ context */
622 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
624 if (!strcmp(cmd, "ps"))
625 fiq_debugger_do_ps(state);
626 if (!strcmp(cmd, "sysrq"))
627 fiq_debugger_do_sysrq(state, 'h');
628 if (!strncmp(cmd, "sysrq ", 6))
629 fiq_debugger_do_sysrq(state, cmd[6]);
631 if (!strcmp(cmd, "kgdb"))
632 fiq_debugger_do_kgdb(state);
634 if (!strncmp(cmd, "reboot", 6))
635 fiq_debugger_schedule_work(state, cmd);
638 static void fiq_debugger_help(struct fiq_debugger_state *state)
640 fiq_debugger_printf(state,
641 "FIQ Debugger commands:\n"
643 " regs Register dump\n"
644 " allregs Extended Register dump\n"
646 " reboot [<c>] Reboot with command <c>\n"
647 " reset [<c>] Hard reset with command <c>\n"
648 " irqs Interupt status\n"
650 " version Kernel version\n");
651 fiq_debugger_printf(state,
652 " sleep Allow sleep while in FIQ\n"
653 " nosleep Disable sleep while in FIQ\n"
654 " console Switch terminal to console\n"
656 " cpu <number> Switch to CPU<number>\n");
657 fiq_debugger_printf(state,
659 " sysrq sysrq options\n"
660 " sysrq <param> Execute sysrq with <param>\n");
662 fiq_debugger_printf(state,
663 " kgdb Enter kernel debugger\n");
667 static void fiq_debugger_take_affinity(void *info)
669 struct fiq_debugger_state *state = info;
670 struct cpumask cpumask;
672 cpumask_clear(&cpumask);
673 cpumask_set_cpu(get_cpu(), &cpumask);
675 irq_set_affinity(state->uart_irq, &cpumask);
678 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
680 if (!fiq_debugger_have_fiq(state))
681 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
683 state->current_cpu = cpu;
686 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
687 const char *cmd, const struct pt_regs *regs,
690 bool signal_helper = false;
692 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
693 fiq_debugger_help(state);
694 } else if (!strcmp(cmd, "pc")) {
695 fiq_debugger_printf(state, " pc %08x cpsr %08x mode %s\n",
696 regs->ARM_pc, regs->ARM_cpsr,
697 mode_name(regs->ARM_cpsr));
698 } else if (!strcmp(cmd, "regs")) {
699 fiq_debugger_dump_regs(state, regs);
700 } else if (!strcmp(cmd, "allregs")) {
701 fiq_debugger_dump_allregs(state, regs);
702 } else if (!strcmp(cmd, "bt")) {
703 fiq_debugger_dump_stacktrace(state, regs, 100, svc_sp);
704 } else if (!strncmp(cmd, "reset", 5)) {
710 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
711 machine_restart(tmp_cmd);
713 machine_restart(NULL);
715 } else if (!strcmp(cmd, "irqs")) {
716 fiq_debugger_dump_irqs(state);
717 } else if (!strcmp(cmd, "kmsg")) {
718 fiq_debugger_dump_kernel_log(state);
719 } else if (!strcmp(cmd, "version")) {
720 fiq_debugger_printf(state, "%s\n", linux_banner);
721 } else if (!strcmp(cmd, "sleep")) {
722 state->no_sleep = false;
723 fiq_debugger_printf(state, "enabling sleep\n");
724 } else if (!strcmp(cmd, "nosleep")) {
725 state->no_sleep = true;
726 fiq_debugger_printf(state, "disabling sleep\n");
727 } else if (!strcmp(cmd, "console")) {
728 fiq_debugger_printf(state, "console mode\n");
729 fiq_debugger_uart_flush(state);
730 state->console_enable = true;
731 } else if (!strcmp(cmd, "cpu")) {
732 fiq_debugger_printf(state, "cpu %d\n", state->current_cpu);
733 } else if (!strncmp(cmd, "cpu ", 4)) {
734 unsigned long cpu = 0;
735 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
736 fiq_debugger_switch_cpu(state, cpu);
738 fiq_debugger_printf(state, "invalid cpu\n");
739 fiq_debugger_printf(state, "cpu %d\n", state->current_cpu);
741 if (state->debug_busy) {
742 fiq_debugger_printf(state,
743 "command processor busy. trying to abort.\n");
744 state->debug_abort = -1;
746 strcpy(state->debug_cmd, cmd);
747 state->debug_busy = 1;
752 if (!state->console_enable)
753 fiq_debugger_prompt(state);
755 return signal_helper;
758 static void fiq_debugger_sleep_timer_expired(unsigned long data)
760 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
763 spin_lock_irqsave(&state->sleep_timer_lock, flags);
764 if (state->uart_enabled && !state->no_sleep) {
765 if (state->debug_enable && !state->console_enable) {
766 state->debug_enable = false;
767 fiq_debugger_printf_nfiq(state,
768 "suspending fiq debugger\n");
770 state->ignore_next_wakeup_irq = true;
771 fiq_debugger_uart_disable(state);
772 state->uart_enabled = false;
773 fiq_debugger_enable_wakeup_irq(state);
775 wake_unlock(&state->debugger_wake_lock);
776 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
779 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
783 spin_lock_irqsave(&state->sleep_timer_lock, flags);
784 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
785 state->ignore_next_wakeup_irq = false;
786 } else if (!state->uart_enabled) {
787 wake_lock(&state->debugger_wake_lock);
788 fiq_debugger_uart_enable(state);
789 state->uart_enabled = true;
790 fiq_debugger_disable_wakeup_irq(state);
791 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
793 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
796 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
798 struct fiq_debugger_state *state = dev;
800 if (!state->no_sleep)
801 fiq_debugger_puts(state, "WAKEUP\n");
802 fiq_debugger_handle_wakeup(state);
808 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
810 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
811 if (state->tty_port.ops) {
813 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
814 for (i = 0; i < count; i++) {
815 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
816 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
817 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
818 pr_warn("fiq tty failed to consume byte\n");
820 tty_flip_buffer_push(&state->tty_port);
825 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
827 if (!state->no_sleep) {
830 spin_lock_irqsave(&state->sleep_timer_lock, flags);
831 wake_lock(&state->debugger_wake_lock);
832 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
833 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
835 fiq_debugger_handle_console_irq_context(state);
836 if (state->debug_busy) {
837 fiq_debugger_irq_exec(state, state->debug_cmd);
838 if (!state->console_enable)
839 fiq_debugger_prompt(state);
840 state->debug_busy = 0;
844 static int fiq_debugger_getc(struct fiq_debugger_state *state)
846 return state->pdata->uart_getc(state->pdev);
849 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
850 int this_cpu, const struct pt_regs *regs, void *svc_sp)
855 bool signal_helper = false;
857 if (this_cpu != state->current_cpu) {
861 if (atomic_inc_return(&state->unhandled_fiq_count) !=
862 MAX_UNHANDLED_FIQ_COUNT)
865 fiq_debugger_printf(state,
866 "fiq_debugger: cpu %d not responding, "
867 "reverting to cpu %d\n", state->current_cpu,
870 atomic_set(&state->unhandled_fiq_count, 0);
871 fiq_debugger_switch_cpu(state, this_cpu);
875 state->in_fiq = true;
877 while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
879 if (!state->debug_enable) {
880 if ((c == 13) || (c == 10)) {
881 state->debug_enable = true;
882 state->debug_count = 0;
883 fiq_debugger_prompt(state);
885 } else if (c == FIQ_DEBUGGER_BREAK) {
886 state->console_enable = false;
887 fiq_debugger_puts(state, "fiq debugger mode\n");
888 state->debug_count = 0;
889 fiq_debugger_prompt(state);
890 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
891 } else if (state->console_enable && state->tty_rbuf) {
892 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
893 signal_helper = true;
895 } else if ((c >= ' ') && (c < 127)) {
896 if (state->debug_count < (DEBUG_MAX - 1)) {
897 state->debug_buf[state->debug_count++] = c;
898 fiq_debugger_putc(state, c);
900 } else if ((c == 8) || (c == 127)) {
901 if (state->debug_count > 0) {
902 state->debug_count--;
903 fiq_debugger_putc(state, 8);
904 fiq_debugger_putc(state, ' ');
905 fiq_debugger_putc(state, 8);
907 } else if ((c == 13) || (c == 10)) {
908 if (c == '\r' || (c == '\n' && last_c != '\r')) {
909 fiq_debugger_putc(state, '\r');
910 fiq_debugger_putc(state, '\n');
912 if (state->debug_count) {
913 state->debug_buf[state->debug_count] = 0;
914 state->debug_count = 0;
916 fiq_debugger_fiq_exec(state,
920 fiq_debugger_prompt(state);
925 if (!state->console_enable)
926 fiq_debugger_uart_flush(state);
927 if (state->pdata->fiq_ack)
928 state->pdata->fiq_ack(state->pdev, state->fiq);
930 /* poke sleep timer if necessary */
931 if (state->debug_enable && !state->no_sleep)
932 signal_helper = true;
934 atomic_set(&state->unhandled_fiq_count, 0);
935 state->in_fiq = false;
937 return signal_helper;
940 #ifdef CONFIG_FIQ_GLUE
941 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
942 const struct pt_regs *regs, void *svc_sp)
944 struct fiq_debugger_state *state =
945 container_of(h, struct fiq_debugger_state, handler);
946 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
949 need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
952 fiq_debugger_force_irq(state);
957 * When not using FIQs, we only use this single interrupt as an entry point.
958 * This just effectively takes over the UART interrupt and does all the work
961 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
963 struct fiq_debugger_state *state = dev;
966 fiq_debugger_handle_wakeup(state);
968 /* handle the debugger irq in regular context */
969 not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
971 current_thread_info());
973 fiq_debugger_handle_irq_context(state);
979 * If FIQs are used, not everything can happen in fiq context.
980 * FIQ handler does what it can and then signals this interrupt to finish the
981 * job in irq context.
983 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
985 struct fiq_debugger_state *state = dev;
987 if (state->pdata->force_irq_ack)
988 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
990 fiq_debugger_handle_irq_context(state);
995 #ifdef CONFIG_FIQ_GLUE
996 static void fiq_debugger_resume(struct fiq_glue_handler *h)
998 struct fiq_debugger_state *state =
999 container_of(h, struct fiq_debugger_state, handler);
1000 if (state->pdata->uart_resume)
1001 state->pdata->uart_resume(state->pdev);
1005 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1006 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
1009 return fiq_tty_driver;
1012 static void fiq_debugger_console_write(struct console *co,
1013 const char *s, unsigned int count)
1015 struct fiq_debugger_state *state;
1016 unsigned long flags;
1018 state = container_of(co, struct fiq_debugger_state, console);
1020 if (!state->console_enable && !state->syslog_dumping)
1023 fiq_debugger_uart_enable(state);
1024 spin_lock_irqsave(&state->console_lock, flags);
1027 fiq_debugger_putc(state, '\r');
1028 fiq_debugger_putc(state, *s++);
1030 fiq_debugger_uart_flush(state);
1031 spin_unlock_irqrestore(&state->console_lock, flags);
1032 fiq_debugger_uart_disable(state);
1035 static struct console fiq_debugger_console = {
1037 .device = fiq_debugger_console_device,
1038 .write = fiq_debugger_console_write,
1039 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
1042 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
1044 int line = tty->index;
1045 struct fiq_debugger_state **states = tty->driver->driver_state;
1046 struct fiq_debugger_state *state = states[line];
1048 return tty_port_open(&state->tty_port, tty, filp);
1051 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
1053 tty_port_close(tty->port, tty, filp);
1056 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1059 int line = tty->index;
1060 struct fiq_debugger_state **states = tty->driver->driver_state;
1061 struct fiq_debugger_state *state = states[line];
1063 if (!state->console_enable)
1066 fiq_debugger_uart_enable(state);
1067 spin_lock_irq(&state->console_lock);
1068 for (i = 0; i < count; i++)
1069 fiq_debugger_putc(state, *buf++);
1070 spin_unlock_irq(&state->console_lock);
1071 fiq_debugger_uart_disable(state);
1076 int fiq_tty_write_room(struct tty_struct *tty)
1081 #ifdef CONFIG_CONSOLE_POLL
1082 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
1087 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
1089 struct fiq_debugger_state **states = driver->driver_state;
1090 struct fiq_debugger_state *state = states[line];
1091 int c = NO_POLL_CHAR;
1093 fiq_debugger_uart_enable(state);
1094 if (fiq_debugger_have_fiq(state)) {
1095 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1097 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1098 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1101 c = fiq_debugger_getc(state);
1102 if (c == FIQ_DEBUGGER_NO_CHAR)
1105 fiq_debugger_uart_disable(state);
1110 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1112 struct fiq_debugger_state **states = driver->driver_state;
1113 struct fiq_debugger_state *state = states[line];
1114 fiq_debugger_uart_enable(state);
1115 fiq_debugger_putc(state, ch);
1116 fiq_debugger_uart_disable(state);
1120 static const struct tty_port_operations fiq_tty_port_ops;
1122 static const struct tty_operations fiq_tty_driver_ops = {
1123 .write = fiq_tty_write,
1124 .write_room = fiq_tty_write_room,
1125 .open = fiq_tty_open,
1126 .close = fiq_tty_close,
1127 #ifdef CONFIG_CONSOLE_POLL
1128 .poll_init = fiq_tty_poll_init,
1129 .poll_get_char = fiq_tty_poll_get_char,
1130 .poll_put_char = fiq_tty_poll_put_char,
1134 static int fiq_debugger_tty_init(void)
1137 struct fiq_debugger_state **states = NULL;
1139 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
1141 pr_err("Failed to allocate fiq debugger state structres\n");
1145 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
1146 if (!fiq_tty_driver) {
1147 pr_err("Failed to allocate fiq debugger tty\n");
1149 goto err_free_state;
1152 fiq_tty_driver->owner = THIS_MODULE;
1153 fiq_tty_driver->driver_name = "fiq-debugger";
1154 fiq_tty_driver->name = "ttyFIQ";
1155 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1156 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1157 fiq_tty_driver->init_termios = tty_std_termios;
1158 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1159 TTY_DRIVER_DYNAMIC_DEV;
1160 fiq_tty_driver->driver_state = states;
1162 fiq_tty_driver->init_termios.c_cflag =
1163 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1164 fiq_tty_driver->init_termios.c_ispeed = 115200;
1165 fiq_tty_driver->init_termios.c_ospeed = 115200;
1167 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
1169 ret = tty_register_driver(fiq_tty_driver);
1171 pr_err("Failed to register fiq tty: %d\n", ret);
1175 pr_info("Registered FIQ tty driver\n");
1179 put_tty_driver(fiq_tty_driver);
1180 fiq_tty_driver = NULL;
1186 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
1189 struct device *tty_dev;
1190 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
1192 states[state->pdev->id] = state;
1194 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1195 if (!state->tty_rbuf) {
1196 pr_err("Failed to allocate fiq debugger ringbuf\n");
1201 tty_port_init(&state->tty_port);
1202 state->tty_port.ops = &fiq_tty_port_ops;
1204 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
1205 state->pdev->id, &state->pdev->dev);
1206 if (IS_ERR(tty_dev)) {
1207 pr_err("Failed to register fiq debugger tty device\n");
1208 ret = PTR_ERR(tty_dev);
1212 device_set_wakeup_capable(tty_dev, 1);
1214 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1219 fiq_debugger_ringbuf_free(state->tty_rbuf);
1220 state->tty_rbuf = NULL;
1225 static int fiq_debugger_dev_suspend(struct device *dev)
1227 struct platform_device *pdev = to_platform_device(dev);
1228 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1230 if (state->pdata->uart_dev_suspend)
1231 return state->pdata->uart_dev_suspend(pdev);
1235 static int fiq_debugger_dev_resume(struct device *dev)
1237 struct platform_device *pdev = to_platform_device(dev);
1238 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1240 if (state->pdata->uart_dev_resume)
1241 return state->pdata->uart_dev_resume(pdev);
1245 static int fiq_debugger_probe(struct platform_device *pdev)
1248 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1249 struct fiq_debugger_state *state;
1253 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1256 if (!pdata->uart_getc || !pdata->uart_putc)
1258 if ((pdata->uart_enable && !pdata->uart_disable) ||
1259 (!pdata->uart_enable && pdata->uart_disable))
1262 fiq = platform_get_irq_byname(pdev, "fiq");
1263 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1265 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1267 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1269 if (fiq >= 0 && !pdata->fiq_enable)
1272 state = kzalloc(sizeof(*state), GFP_KERNEL);
1273 setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1274 (unsigned long)state);
1275 state->pdata = pdata;
1277 state->no_sleep = initial_no_sleep;
1278 state->debug_enable = initial_debug_enable;
1279 state->console_enable = initial_console_enable;
1282 state->uart_irq = uart_irq;
1283 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1284 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1286 INIT_WORK(&state->work, fiq_debugger_work);
1287 spin_lock_init(&state->work_lock);
1289 platform_set_drvdata(pdev, state);
1291 spin_lock_init(&state->sleep_timer_lock);
1293 if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1294 state->no_sleep = true;
1295 state->ignore_next_wakeup_irq = !state->no_sleep;
1297 wake_lock_init(&state->debugger_wake_lock,
1298 WAKE_LOCK_SUSPEND, "serial-debug");
1300 state->clk = clk_get(&pdev->dev, NULL);
1301 if (IS_ERR(state->clk))
1304 /* do not call pdata->uart_enable here since uart_init may still
1305 * need to do some initialization before uart_enable can work.
1306 * So, only try to manage the clock during init.
1309 clk_enable(state->clk);
1311 if (pdata->uart_init) {
1312 ret = pdata->uart_init(pdev);
1317 fiq_debugger_printf_nfiq(state,
1318 "<hit enter %sto activate fiq debugger>\n",
1319 state->no_sleep ? "" : "twice ");
1321 #ifdef CONFIG_FIQ_GLUE
1322 if (fiq_debugger_have_fiq(state)) {
1323 state->handler.fiq = fiq_debugger_fiq;
1324 state->handler.resume = fiq_debugger_resume;
1325 ret = fiq_glue_register_handler(&state->handler);
1327 pr_err("%s: could not install fiq handler\n", __func__);
1328 goto err_register_irq;
1331 pdata->fiq_enable(pdev, state->fiq, 1);
1335 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1336 IRQF_NO_SUSPEND, "debug", state);
1338 pr_err("%s: could not install irq handler\n", __func__);
1339 goto err_register_irq;
1342 /* for irq-only mode, we want this irq to wake us up, if it
1345 enable_irq_wake(state->uart_irq);
1349 clk_disable(state->clk);
1351 if (state->signal_irq >= 0) {
1352 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1353 IRQF_TRIGGER_RISING, "debug-signal", state);
1355 pr_err("serial_debugger: could not install signal_irq");
1358 if (state->wakeup_irq >= 0) {
1359 ret = request_irq(state->wakeup_irq,
1360 fiq_debugger_wakeup_irq_handler,
1361 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1362 "debug-wakeup", state);
1364 pr_err("serial_debugger: "
1365 "could not install wakeup irq\n");
1366 state->wakeup_irq = -1;
1368 ret = enable_irq_wake(state->wakeup_irq);
1370 pr_err("serial_debugger: "
1371 "could not enable wakeup\n");
1372 state->wakeup_irq_no_set_wake = true;
1376 if (state->no_sleep)
1377 fiq_debugger_handle_wakeup(state);
1379 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1380 spin_lock_init(&state->console_lock);
1381 state->console = fiq_debugger_console;
1382 state->console.index = pdev->id;
1383 if (!console_set_on_cmdline)
1384 add_preferred_console(state->console.name,
1385 state->console.index, NULL);
1386 register_console(&state->console);
1387 fiq_debugger_tty_init_one(state);
1392 if (pdata->uart_free)
1393 pdata->uart_free(pdev);
1396 clk_disable(state->clk);
1398 clk_put(state->clk);
1399 wake_lock_destroy(&state->debugger_wake_lock);
1400 platform_set_drvdata(pdev, NULL);
1405 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1406 .suspend = fiq_debugger_dev_suspend,
1407 .resume = fiq_debugger_dev_resume,
1410 static struct platform_driver fiq_debugger_driver = {
1411 .probe = fiq_debugger_probe,
1413 .name = "fiq_debugger",
1414 .pm = &fiq_debugger_dev_pm_ops,
1418 static int __init fiq_debugger_init(void)
1420 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1421 fiq_debugger_tty_init();
1423 return platform_driver_register(&fiq_debugger_driver);
1426 postcore_initcall(fiq_debugger_init);