2 * arch/arm/common/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 #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"
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;
61 bool wakeup_irq_no_set_wake;
63 struct fiq_debugger_pdata *pdata;
64 struct platform_device *pdev;
66 char debug_cmd[DEBUG_MAX];
70 char debug_buf[DEBUG_MAX];
75 bool ignore_next_wakeup_irq;
76 struct timer_list sleep_timer;
77 spinlock_t sleep_timer_lock;
79 struct wake_lock debugger_wake_lock;
82 atomic_t unhandled_fiq_count;
85 struct work_struct work;
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;
97 unsigned int last_irqs[NR_IRQS];
98 unsigned int last_local_timer_irqs[NR_CPUS];
101 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
102 struct tty_driver *fiq_tty_driver;
105 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
106 static bool initial_no_sleep = true;
108 static bool initial_no_sleep;
111 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
112 static bool initial_debug_enable = true;
113 static bool initial_console_enable = true;
115 static bool initial_debug_enable;
116 static bool initial_console_enable;
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) {}
130 static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
132 if (state->wakeup_irq < 0)
134 enable_irq(state->wakeup_irq);
135 if (!state->wakeup_irq_no_set_wake)
136 enable_irq_wake(state->wakeup_irq);
138 static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
140 if (state->wakeup_irq < 0)
142 disable_irq_nosync(state->wakeup_irq);
143 if (!state->wakeup_irq_no_set_wake)
144 disable_irq_wake(state->wakeup_irq);
148 static bool inline debug_have_fiq(struct fiq_debugger_state *state)
150 return (state->fiq >= 0);
153 static void debug_force_irq(struct fiq_debugger_state *state)
155 unsigned int irq = state->signal_irq;
157 if (WARN_ON(!debug_have_fiq(state)))
159 if (state->pdata->force_irq) {
160 state->pdata->force_irq(state->pdev, irq);
162 struct irq_chip *chip = irq_get_chip(irq);
163 if (chip && chip->irq_retrigger)
164 chip->irq_retrigger(irq_get_irq_data(irq));
168 static void debug_uart_enable(struct fiq_debugger_state *state)
171 clk_enable(state->clk);
172 if (state->pdata->uart_enable)
173 state->pdata->uart_enable(state->pdev);
176 static void debug_uart_disable(struct fiq_debugger_state *state)
178 if (state->pdata->uart_disable)
179 state->pdata->uart_disable(state->pdev);
181 clk_disable(state->clk);
184 static void debug_uart_flush(struct fiq_debugger_state *state)
186 if (state->pdata->uart_flush)
187 state->pdata->uart_flush(state->pdev);
190 static void debug_putc(struct fiq_debugger_state *state, char c)
192 state->pdata->uart_putc(state->pdev, c);
195 static void debug_puts(struct fiq_debugger_state *state, char *s)
200 debug_putc(state, '\r');
201 debug_putc(state, c);
205 static void debug_prompt(struct fiq_debugger_state *state)
207 debug_puts(state, "debug> ");
210 static void dump_kernel_log(struct fiq_debugger_state *state)
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)) {
221 debug_puts(state, buf);
225 static char *mode_name(unsigned cpsr)
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 "???";
239 static int debug_printf(void *cookie, const char *fmt, ...)
241 struct fiq_debugger_state *state = cookie;
246 vsnprintf(buf, sizeof(buf), fmt, ap);
249 debug_puts(state, buf);
250 return state->debug_abort;
253 /* Safe outside fiq context */
254 static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
256 struct fiq_debugger_state *state = cookie;
259 unsigned long irq_flags;
262 vsnprintf(buf, 128, fmt, 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;
272 static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
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],
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]);
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;
318 void __naked get_mode_regs(struct mode_regs *regs)
322 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
323 "stmia r0!, {r13 - r14}\n"
325 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
326 "stmia r0!, {r2, r13 - r14}\n"
328 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
329 "stmia r0!, {r2, r13 - r14}\n"
331 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
332 "stmia r0!, {r2, r13 - r14}\n"
334 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
335 "stmia r0!, {r2, r8 - r14}\n"
343 static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
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 "
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);
364 static void dump_irqs(struct fiq_debugger_state *state)
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))
374 debug_printf(state, "%5d: %10u %11u %8x %s\n", 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);
383 struct stacktrace_state {
384 struct fiq_debugger_state *state;
388 static int report_trace(struct stackframe *frame, void *d)
390 struct stacktrace_state *sts = d;
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);
400 debug_printf(sts->state, " ...\n");
402 return sts->depth == 0;
406 struct frame_tail *fp;
409 } __attribute__((packed));
411 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
412 struct frame_tail *tail)
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);
421 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
423 " failed to copy frame pointer %p\n", tail);
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)
434 return buftail[0].fp-1;
437 void dump_stacktrace(struct fiq_debugger_state *state,
438 struct pt_regs * const regs, unsigned int depth, void *ssp)
440 struct frame_tail *tail;
441 struct thread_info *real_thread_info = THREAD_INFO(ssp);
442 struct stacktrace_state sts;
446 *current_thread_info() = *real_thread_info;
449 debug_printf(state, "current NULL\n");
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;
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);
469 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
470 while (depth-- && tail && !((unsigned long) tail & 3))
471 tail = user_backtrace(state, tail);
474 static void do_ps(struct fiq_debugger_state *state)
476 struct task_struct *g;
477 struct task_struct *p;
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;
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");
492 debug_printf(state, " %08lx\n", thread_saved_pc(p));
493 } while_each_thread(g, p);
494 read_unlock(&tasklist_lock);
497 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
498 static void begin_syslog_dump(struct fiq_debugger_state *state)
500 state->syslog_dumping = true;
503 static void end_syslog_dump(struct fiq_debugger_state *state)
505 state->syslog_dumping = false;
508 extern int do_syslog(int type, char __user *bug, int count);
509 static void begin_syslog_dump(struct fiq_debugger_state *state)
511 do_syslog(5 /* clear */, NULL, 0);
514 static void end_syslog_dump(struct fiq_debugger_state *state)
516 dump_kernel_log(state);
520 static void do_sysrq(struct fiq_debugger_state *state, char rq)
522 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
523 debug_printf(state, "sysrq-g blocked\n");
526 begin_syslog_dump(state);
528 end_syslog_dump(state);
532 static void do_kgdb(struct fiq_debugger_state *state)
534 if (!fiq_kgdb_enable) {
535 debug_printf(state, "kgdb through fiq debugger not enabled\n");
539 debug_printf(state, "enabling console and triggering kgdb\n");
540 state->console_enable = true;
545 static void debug_schedule_work(struct fiq_debugger_state *state, char *cmd)
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);
556 strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
557 spin_unlock_irqrestore(&state->work_lock, flags);
559 schedule_work(&state->work);
562 static void debug_work(struct work_struct *work)
564 struct fiq_debugger_state *state;
565 char work_cmd[DEBUG_MAX];
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);
579 if (!strncmp(cmd, "reboot", 6)) {
586 kernel_restart(NULL);
588 debug_printf(state, "unknown work command '%s'\n", work_cmd);
592 /* This function CANNOT be called in FIQ context */
593 static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
595 if (!strcmp(cmd, "ps"))
597 if (!strcmp(cmd, "sysrq"))
598 do_sysrq(state, 'h');
599 if (!strncmp(cmd, "sysrq ", 6))
600 do_sysrq(state, cmd[6]);
602 if (!strcmp(cmd, "kgdb"))
605 if (!strncmp(cmd, "reboot", 6))
606 debug_schedule_work(state, cmd);
609 static void debug_help(struct fiq_debugger_state *state)
611 debug_printf(state, "FIQ Debugger commands:\n"
613 " regs Register dump\n"
614 " allregs Extended Register dump\n"
616 " reboot [<c>] Reboot with command <c>\n"
617 " reset [<c>] Hard reset with command <c>\n"
618 " irqs Interupt status\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"
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");
630 debug_printf(state, " kgdb Enter kernel debugger\n");
634 static void take_affinity(void *info)
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);
645 static void switch_cpu(struct fiq_debugger_state *state, int cpu)
647 if (!debug_have_fiq(state))
648 smp_call_function_single(cpu, take_affinity, state, false);
649 state->current_cpu = cpu;
652 static bool debug_fiq_exec(struct fiq_debugger_state *state,
653 const char *cmd, unsigned *regs, void *svc_sp)
655 bool signal_helper = false;
657 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
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)) {
674 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
675 machine_restart(tmp_cmd);
677 machine_restart(NULL);
679 } else if (!strcmp(cmd, "irqs")) {
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);
702 debug_printf(state, "invalid cpu\n");
703 debug_printf(state, "cpu %d\n", state->current_cpu);
705 if (state->debug_busy) {
707 "command processor busy. trying to abort.\n");
708 state->debug_abort = -1;
710 strcpy(state->debug_cmd, cmd);
711 state->debug_busy = 1;
716 if (!state->console_enable)
719 return signal_helper;
722 static void sleep_timer_expired(unsigned long data)
724 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
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");
733 state->ignore_next_wakeup_irq = true;
734 debug_uart_disable(state);
735 state->uart_enabled = false;
736 enable_wakeup_irq(state);
738 wake_unlock(&state->debugger_wake_lock);
739 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
742 static void handle_wakeup(struct fiq_debugger_state *state)
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);
756 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
759 static irqreturn_t wakeup_irq_handler(int irq, void *dev)
761 struct fiq_debugger_state *state = dev;
763 if (!state->no_sleep)
764 debug_puts(state, "WAKEUP\n");
765 handle_wakeup(state);
770 static void debug_handle_console_irq_context(struct fiq_debugger_state *state)
772 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
773 if (state->tty_port.ops) {
775 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
776 for (i = 0; i < count; i++) {
777 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
778 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
779 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
780 pr_warn("fiq tty failed to consume byte\n");
782 tty_flip_buffer_push(&state->tty_port);
787 static void debug_handle_irq_context(struct fiq_debugger_state *state)
789 if (!state->no_sleep) {
792 spin_lock_irqsave(&state->sleep_timer_lock, flags);
793 wake_lock(&state->debugger_wake_lock);
794 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
795 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
797 debug_handle_console_irq_context(state);
798 if (state->debug_busy) {
799 debug_irq_exec(state, state->debug_cmd);
800 if (!state->console_enable)
802 state->debug_busy = 0;
806 static int debug_getc(struct fiq_debugger_state *state)
808 return state->pdata->uart_getc(state->pdev);
811 static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
812 int this_cpu, void *regs, void *svc_sp)
817 bool signal_helper = false;
819 if (this_cpu != state->current_cpu) {
823 if (atomic_inc_return(&state->unhandled_fiq_count) !=
824 MAX_UNHANDLED_FIQ_COUNT)
827 debug_printf(state, "fiq_debugger: cpu %d not responding, "
828 "reverting to cpu %d\n", state->current_cpu,
831 atomic_set(&state->unhandled_fiq_count, 0);
832 switch_cpu(state, this_cpu);
836 state->in_fiq = true;
838 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
840 if (!state->debug_enable) {
841 if ((c == 13) || (c == 10)) {
842 state->debug_enable = true;
843 state->debug_count = 0;
846 } else if (c == FIQ_DEBUGGER_BREAK) {
847 state->console_enable = false;
848 debug_puts(state, "fiq debugger mode\n");
849 state->debug_count = 0;
851 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
852 } else if (state->console_enable && state->tty_rbuf) {
853 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
854 signal_helper = true;
856 } else if ((c >= ' ') && (c < 127)) {
857 if (state->debug_count < (DEBUG_MAX - 1)) {
858 state->debug_buf[state->debug_count++] = c;
859 debug_putc(state, c);
861 } else if ((c == 8) || (c == 127)) {
862 if (state->debug_count > 0) {
863 state->debug_count--;
864 debug_putc(state, 8);
865 debug_putc(state, ' ');
866 debug_putc(state, 8);
868 } else if ((c == 13) || (c == 10)) {
869 if (c == '\r' || (c == '\n' && last_c != '\r')) {
870 debug_putc(state, '\r');
871 debug_putc(state, '\n');
873 if (state->debug_count) {
874 state->debug_buf[state->debug_count] = 0;
875 state->debug_count = 0;
877 debug_fiq_exec(state, state->debug_buf,
885 if (!state->console_enable)
886 debug_uart_flush(state);
887 if (state->pdata->fiq_ack)
888 state->pdata->fiq_ack(state->pdev, state->fiq);
890 /* poke sleep timer if necessary */
891 if (state->debug_enable && !state->no_sleep)
892 signal_helper = true;
894 atomic_set(&state->unhandled_fiq_count, 0);
895 state->in_fiq = false;
897 return signal_helper;
900 static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
902 struct fiq_debugger_state *state =
903 container_of(h, struct fiq_debugger_state, handler);
904 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
907 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
909 debug_force_irq(state);
913 * When not using FIQs, we only use this single interrupt as an entry point.
914 * This just effectively takes over the UART interrupt and does all the work
917 static irqreturn_t debug_uart_irq(int irq, void *dev)
919 struct fiq_debugger_state *state = dev;
922 handle_wakeup(state);
924 /* handle the debugger irq in regular context */
925 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
927 current_thread_info());
929 debug_handle_irq_context(state);
935 * If FIQs are used, not everything can happen in fiq context.
936 * FIQ handler does what it can and then signals this interrupt to finish the
937 * job in irq context.
939 static irqreturn_t debug_signal_irq(int irq, void *dev)
941 struct fiq_debugger_state *state = dev;
943 if (state->pdata->force_irq_ack)
944 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
946 debug_handle_irq_context(state);
951 static void debug_resume(struct fiq_glue_handler *h)
953 struct fiq_debugger_state *state =
954 container_of(h, struct fiq_debugger_state, handler);
955 if (state->pdata->uart_resume)
956 state->pdata->uart_resume(state->pdev);
959 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
960 struct tty_driver *debug_console_device(struct console *co, int *index)
963 return fiq_tty_driver;
966 static void debug_console_write(struct console *co,
967 const char *s, unsigned int count)
969 struct fiq_debugger_state *state;
972 state = container_of(co, struct fiq_debugger_state, console);
974 if (!state->console_enable && !state->syslog_dumping)
977 debug_uart_enable(state);
978 spin_lock_irqsave(&state->console_lock, flags);
981 debug_putc(state, '\r');
982 debug_putc(state, *s++);
984 debug_uart_flush(state);
985 spin_unlock_irqrestore(&state->console_lock, flags);
986 debug_uart_disable(state);
989 static struct console fiq_debugger_console = {
991 .device = debug_console_device,
992 .write = debug_console_write,
993 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
996 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
998 int line = tty->index;
999 struct fiq_debugger_state **states = tty->driver->driver_state;
1000 struct fiq_debugger_state *state = states[line];
1002 return tty_port_open(&state->tty_port, tty, filp);
1005 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
1007 tty_port_close(tty->port, tty, filp);
1010 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1013 int line = tty->index;
1014 struct fiq_debugger_state **states = tty->driver->driver_state;
1015 struct fiq_debugger_state *state = states[line];
1017 if (!state->console_enable)
1020 debug_uart_enable(state);
1021 spin_lock_irq(&state->console_lock);
1022 for (i = 0; i < count; i++)
1023 debug_putc(state, *buf++);
1024 spin_unlock_irq(&state->console_lock);
1025 debug_uart_disable(state);
1030 int fiq_tty_write_room(struct tty_struct *tty)
1035 #ifdef CONFIG_CONSOLE_POLL
1036 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
1041 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
1043 struct fiq_debugger_state **states = driver->driver_state;
1044 struct fiq_debugger_state *state = states[line];
1045 int c = NO_POLL_CHAR;
1047 debug_uart_enable(state);
1048 if (debug_have_fiq(state)) {
1049 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1051 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1052 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1055 c = debug_getc(state);
1056 if (c == FIQ_DEBUGGER_NO_CHAR)
1059 debug_uart_disable(state);
1064 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1066 struct fiq_debugger_state **states = driver->driver_state;
1067 struct fiq_debugger_state *state = states[line];
1068 debug_uart_enable(state);
1069 debug_putc(state, ch);
1070 debug_uart_disable(state);
1074 static const struct tty_port_operations fiq_tty_port_ops;
1076 static const struct tty_operations fiq_tty_driver_ops = {
1077 .write = fiq_tty_write,
1078 .write_room = fiq_tty_write_room,
1079 .open = fiq_tty_open,
1080 .close = fiq_tty_close,
1081 #ifdef CONFIG_CONSOLE_POLL
1082 .poll_init = fiq_tty_poll_init,
1083 .poll_get_char = fiq_tty_poll_get_char,
1084 .poll_put_char = fiq_tty_poll_put_char,
1088 static int fiq_debugger_tty_init(void)
1091 struct fiq_debugger_state **states = NULL;
1093 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
1095 pr_err("Failed to allocate fiq debugger state structres\n");
1099 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
1100 if (!fiq_tty_driver) {
1101 pr_err("Failed to allocate fiq debugger tty\n");
1103 goto err_free_state;
1106 fiq_tty_driver->owner = THIS_MODULE;
1107 fiq_tty_driver->driver_name = "fiq-debugger";
1108 fiq_tty_driver->name = "ttyFIQ";
1109 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1110 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1111 fiq_tty_driver->init_termios = tty_std_termios;
1112 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1113 TTY_DRIVER_DYNAMIC_DEV;
1114 fiq_tty_driver->driver_state = states;
1116 fiq_tty_driver->init_termios.c_cflag =
1117 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1118 fiq_tty_driver->init_termios.c_ispeed = 115200;
1119 fiq_tty_driver->init_termios.c_ospeed = 115200;
1121 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
1123 ret = tty_register_driver(fiq_tty_driver);
1125 pr_err("Failed to register fiq tty: %d\n", ret);
1129 pr_info("Registered FIQ tty driver\n");
1133 put_tty_driver(fiq_tty_driver);
1134 fiq_tty_driver = NULL;
1140 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
1143 struct device *tty_dev;
1144 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
1146 states[state->pdev->id] = state;
1148 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1149 if (!state->tty_rbuf) {
1150 pr_err("Failed to allocate fiq debugger ringbuf\n");
1155 tty_port_init(&state->tty_port);
1156 state->tty_port.ops = &fiq_tty_port_ops;
1158 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
1159 state->pdev->id, &state->pdev->dev);
1160 if (IS_ERR(tty_dev)) {
1161 pr_err("Failed to register fiq debugger tty device\n");
1162 ret = PTR_ERR(tty_dev);
1166 device_set_wakeup_capable(tty_dev, 1);
1168 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1173 fiq_debugger_ringbuf_free(state->tty_rbuf);
1174 state->tty_rbuf = NULL;
1179 static int fiq_debugger_dev_suspend(struct device *dev)
1181 struct platform_device *pdev = to_platform_device(dev);
1182 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1184 if (state->pdata->uart_dev_suspend)
1185 return state->pdata->uart_dev_suspend(pdev);
1189 static int fiq_debugger_dev_resume(struct device *dev)
1191 struct platform_device *pdev = to_platform_device(dev);
1192 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1194 if (state->pdata->uart_dev_resume)
1195 return state->pdata->uart_dev_resume(pdev);
1199 static int fiq_debugger_probe(struct platform_device *pdev)
1202 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1203 struct fiq_debugger_state *state;
1207 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1210 if (!pdata->uart_getc || !pdata->uart_putc)
1212 if ((pdata->uart_enable && !pdata->uart_disable) ||
1213 (!pdata->uart_enable && pdata->uart_disable))
1216 fiq = platform_get_irq_byname(pdev, "fiq");
1217 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1219 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1221 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1223 if (fiq >= 0 && !pdata->fiq_enable)
1226 state = kzalloc(sizeof(*state), GFP_KERNEL);
1227 setup_timer(&state->sleep_timer, sleep_timer_expired,
1228 (unsigned long)state);
1229 state->pdata = pdata;
1231 state->no_sleep = initial_no_sleep;
1232 state->debug_enable = initial_debug_enable;
1233 state->console_enable = initial_console_enable;
1236 state->uart_irq = uart_irq;
1237 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1238 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1240 INIT_WORK(&state->work, debug_work);
1241 spin_lock_init(&state->work_lock);
1243 platform_set_drvdata(pdev, state);
1245 spin_lock_init(&state->sleep_timer_lock);
1247 if (state->wakeup_irq < 0 && debug_have_fiq(state))
1248 state->no_sleep = true;
1249 state->ignore_next_wakeup_irq = !state->no_sleep;
1251 wake_lock_init(&state->debugger_wake_lock,
1252 WAKE_LOCK_SUSPEND, "serial-debug");
1254 state->clk = clk_get(&pdev->dev, NULL);
1255 if (IS_ERR(state->clk))
1258 /* do not call pdata->uart_enable here since uart_init may still
1259 * need to do some initialization before uart_enable can work.
1260 * So, only try to manage the clock during init.
1263 clk_enable(state->clk);
1265 if (pdata->uart_init) {
1266 ret = pdata->uart_init(pdev);
1271 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1272 state->no_sleep ? "" : "twice ");
1274 if (debug_have_fiq(state)) {
1275 state->handler.fiq = debug_fiq;
1276 state->handler.resume = debug_resume;
1277 ret = fiq_glue_register_handler(&state->handler);
1279 pr_err("%s: could not install fiq handler\n", __func__);
1280 goto err_register_fiq;
1283 pdata->fiq_enable(pdev, state->fiq, 1);
1285 ret = request_irq(state->uart_irq, debug_uart_irq,
1286 IRQF_NO_SUSPEND, "debug", state);
1288 pr_err("%s: could not install irq handler\n", __func__);
1289 goto err_register_irq;
1292 /* for irq-only mode, we want this irq to wake us up, if it
1295 enable_irq_wake(state->uart_irq);
1299 clk_disable(state->clk);
1301 if (state->signal_irq >= 0) {
1302 ret = request_irq(state->signal_irq, debug_signal_irq,
1303 IRQF_TRIGGER_RISING, "debug-signal", state);
1305 pr_err("serial_debugger: could not install signal_irq");
1308 if (state->wakeup_irq >= 0) {
1309 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1310 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1311 "debug-wakeup", state);
1313 pr_err("serial_debugger: "
1314 "could not install wakeup irq\n");
1315 state->wakeup_irq = -1;
1317 ret = enable_irq_wake(state->wakeup_irq);
1319 pr_err("serial_debugger: "
1320 "could not enable wakeup\n");
1321 state->wakeup_irq_no_set_wake = true;
1325 if (state->no_sleep)
1326 handle_wakeup(state);
1328 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1329 spin_lock_init(&state->console_lock);
1330 state->console = fiq_debugger_console;
1331 state->console.index = pdev->id;
1332 if (!console_set_on_cmdline)
1333 add_preferred_console(state->console.name,
1334 state->console.index, NULL);
1335 register_console(&state->console);
1336 fiq_debugger_tty_init_one(state);
1342 if (pdata->uart_free)
1343 pdata->uart_free(pdev);
1346 clk_disable(state->clk);
1348 clk_put(state->clk);
1349 wake_lock_destroy(&state->debugger_wake_lock);
1350 platform_set_drvdata(pdev, NULL);
1355 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1356 .suspend = fiq_debugger_dev_suspend,
1357 .resume = fiq_debugger_dev_resume,
1360 static struct platform_driver fiq_debugger_driver = {
1361 .probe = fiq_debugger_probe,
1363 .name = "fiq_debugger",
1364 .pm = &fiq_debugger_dev_pm_ops,
1368 static int __init fiq_debugger_init(void)
1370 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1371 fiq_debugger_tty_init();
1373 return platform_driver_register(&fiq_debugger_driver);
1376 postcore_initcall(fiq_debugger_init);