blob: 1f64d7dc83b4fb4ae604d69ba2216b877de565dd [file] [log] [blame]
Iliyan Malchev09688d12010-06-05 17:36:24 -07001/*
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 */
17
18#include <stdarg.h>
19#include <linux/module.h>
20#include <linux/io.h>
21#include <linux/console.h>
22#include <linux/interrupt.h>
23#include <linux/clk.h>
24#include <linux/platform_device.h>
25#include <linux/kernel_stat.h>
26#include <linux/irq.h>
27#include <linux/delay.h>
Colin Crossb9b3a062012-03-14 16:28:45 -070028#include <linux/reboot.h>
Iliyan Malchev09688d12010-06-05 17:36:24 -070029#include <linux/sched.h>
30#include <linux/slab.h>
31#include <linux/smp.h>
32#include <linux/timer.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/wakelock.h>
36
37#include <asm/fiq_debugger.h>
38#include <asm/fiq_glue.h>
39#include <asm/stacktrace.h>
40
Iliyan Malchev09688d12010-06-05 17:36:24 -070041#include <linux/uaccess.h>
42
43#include "fiq_debugger_ringbuf.h"
44
45#define DEBUG_MAX 64
46#define MAX_UNHANDLED_FIQ_COUNT 1000000
47
48#define THREAD_INFO(sp) ((struct thread_info *) \
49 ((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
50
51struct fiq_debugger_state {
52 struct fiq_glue_handler handler;
53
54 int fiq;
55 int uart_irq;
56 int signal_irq;
57 int wakeup_irq;
58 bool wakeup_irq_no_set_wake;
59 struct clk *clk;
60 struct fiq_debugger_pdata *pdata;
61 struct platform_device *pdev;
62
63 char debug_cmd[DEBUG_MAX];
64 int debug_busy;
65 int debug_abort;
66
67 char debug_buf[DEBUG_MAX];
68 int debug_count;
69
70 bool no_sleep;
71 bool debug_enable;
72 bool ignore_next_wakeup_irq;
73 struct timer_list sleep_timer;
74 spinlock_t sleep_timer_lock;
75 bool uart_enabled;
76 struct wake_lock debugger_wake_lock;
77 bool console_enable;
78 int current_cpu;
79 atomic_t unhandled_fiq_count;
80 bool in_fiq;
81
82#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
83 struct console console;
84 struct tty_driver *tty_driver;
85 struct tty_struct *tty;
86 int tty_open_count;
87 struct fiq_debugger_ringbuf *tty_rbuf;
88 bool syslog_dumping;
89#endif
90
91 unsigned int last_irqs[NR_IRQS];
92 unsigned int last_local_timer_irqs[NR_CPUS];
93};
94
95#ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
96static bool initial_no_sleep = true;
97#else
98static bool initial_no_sleep;
99#endif
100
101#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
102static bool initial_debug_enable = true;
103static bool initial_console_enable = true;
104#else
105static bool initial_debug_enable;
106static bool initial_console_enable;
107#endif
108
Colin Cross688a9012012-03-14 19:23:29 -0700109static bool fiq_kgdb_enable;
110
Iliyan Malchev09688d12010-06-05 17:36:24 -0700111module_param_named(no_sleep, initial_no_sleep, bool, 0644);
112module_param_named(debug_enable, initial_debug_enable, bool, 0644);
113module_param_named(console_enable, initial_console_enable, bool, 0644);
Colin Cross688a9012012-03-14 19:23:29 -0700114module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
Iliyan Malchev09688d12010-06-05 17:36:24 -0700115
116#ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
117static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
118static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
119#else
120static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
121{
122 if (state->wakeup_irq < 0)
123 return;
124 enable_irq(state->wakeup_irq);
125 if (!state->wakeup_irq_no_set_wake)
126 enable_irq_wake(state->wakeup_irq);
127}
128static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
129{
130 if (state->wakeup_irq < 0)
131 return;
132 disable_irq_nosync(state->wakeup_irq);
133 if (!state->wakeup_irq_no_set_wake)
134 disable_irq_wake(state->wakeup_irq);
135}
136#endif
137
138static bool inline debug_have_fiq(struct fiq_debugger_state *state)
139{
140 return (state->fiq >= 0);
141}
142
143static void debug_force_irq(struct fiq_debugger_state *state)
144{
145 unsigned int irq = state->signal_irq;
146
147 if (WARN_ON(!debug_have_fiq(state)))
148 return;
149 if (state->pdata->force_irq) {
150 state->pdata->force_irq(state->pdev, irq);
151 } else {
152 struct irq_chip *chip = irq_get_chip(irq);
153 if (chip && chip->irq_retrigger)
154 chip->irq_retrigger(irq_get_irq_data(irq));
155 }
156}
157
158static void debug_uart_enable(struct fiq_debugger_state *state)
159{
160 if (state->clk)
161 clk_enable(state->clk);
162 if (state->pdata->uart_enable)
163 state->pdata->uart_enable(state->pdev);
164}
165
166static void debug_uart_disable(struct fiq_debugger_state *state)
167{
168 if (state->pdata->uart_disable)
169 state->pdata->uart_disable(state->pdev);
170 if (state->clk)
171 clk_disable(state->clk);
172}
173
174static void debug_uart_flush(struct fiq_debugger_state *state)
175{
176 if (state->pdata->uart_flush)
177 state->pdata->uart_flush(state->pdev);
178}
179
Colin Crossd72d89e2012-03-15 12:57:20 -0700180static void debug_putc(struct fiq_debugger_state *state, char c)
181{
182 state->pdata->uart_putc(state->pdev, c);
183}
184
Iliyan Malchev09688d12010-06-05 17:36:24 -0700185static void debug_puts(struct fiq_debugger_state *state, char *s)
186{
187 unsigned c;
188 while ((c = *s++)) {
189 if (c == '\n')
Colin Crossd72d89e2012-03-15 12:57:20 -0700190 debug_putc(state, '\r');
191 debug_putc(state, c);
Iliyan Malchev09688d12010-06-05 17:36:24 -0700192 }
193}
194
195static void debug_prompt(struct fiq_debugger_state *state)
196{
197 debug_puts(state, "debug> ");
198}
199
200int log_buf_copy(char *dest, int idx, int len);
201static void dump_kernel_log(struct fiq_debugger_state *state)
202{
203 char buf[1024];
204 int idx = 0;
205 int ret;
206 int saved_oip;
207
208 /* setting oops_in_progress prevents log_buf_copy()
209 * from trying to take a spinlock which will make it
210 * very unhappy in some cases...
211 */
212 saved_oip = oops_in_progress;
213 oops_in_progress = 1;
214 for (;;) {
215 ret = log_buf_copy(buf, idx, 1023);
216 if (ret <= 0)
217 break;
218 buf[ret] = 0;
219 debug_puts(state, buf);
220 idx += ret;
221 }
222 oops_in_progress = saved_oip;
223}
224
225static 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}
238
239static int debug_printf(void *cookie, const char *fmt, ...)
240{
241 struct fiq_debugger_state *state = cookie;
242 char buf[256];
243 va_list ap;
244
245 va_start(ap, fmt);
246 vsnprintf(buf, sizeof(buf), fmt, ap);
247 va_end(ap);
248
249 debug_puts(state, buf);
250 return state->debug_abort;
251}
252
253/* Safe outside fiq context */
254static 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;
260
261 va_start(ap, fmt);
262 vsnprintf(buf, 128, fmt, ap);
263 va_end(ap);
264
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}
271
272static 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}
290
291struct mode_regs {
292 unsigned long sp_svc;
293 unsigned long lr_svc;
294 unsigned long spsr_svc;
295
296 unsigned long sp_abt;
297 unsigned long lr_abt;
298 unsigned long spsr_abt;
299
300 unsigned long sp_und;
301 unsigned long lr_und;
302 unsigned long spsr_und;
303
304 unsigned long sp_irq;
305 unsigned long lr_irq;
306 unsigned long spsr_irq;
307
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};
317
318void __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}
341
342
343static 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}
363
364static void dump_irqs(struct fiq_debugger_state *state)
365{
366 int n;
Iliyan Malchev09688d12010-06-05 17:36:24 -0700367
368 debug_printf(state, "irqnr total since-last status name\n");
369 for (n = 0; n < NR_IRQS; n++) {
370 struct irqaction *act = irq_desc[n].action;
371 if (!act && !kstat_irqs(n))
372 continue;
373 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
374 kstat_irqs(n),
375 kstat_irqs(n) - state->last_irqs[n],
376 irq_desc[n].status_use_accessors,
377 (act && act->name) ? act->name : "???");
378 state->last_irqs[n] = kstat_irqs(n);
379 }
Iliyan Malchev09688d12010-06-05 17:36:24 -0700380}
381
382struct stacktrace_state {
383 struct fiq_debugger_state *state;
384 unsigned int depth;
385};
386
387static int report_trace(struct stackframe *frame, void *d)
388{
389 struct stacktrace_state *sts = d;
390
391 if (sts->depth) {
392 debug_printf(sts->state,
393 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
394 frame->pc, frame->pc, frame->lr, frame->lr,
395 frame->sp, frame->fp);
396 sts->depth--;
397 return 0;
398 }
399 debug_printf(sts->state, " ...\n");
400
401 return sts->depth == 0;
402}
403
404struct frame_tail {
405 struct frame_tail *fp;
406 unsigned long sp;
407 unsigned long lr;
408} __attribute__((packed));
409
410static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
411 struct frame_tail *tail)
412{
413 struct frame_tail buftail[2];
414
415 /* Also check accessibility of one struct frame_tail beyond */
416 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
417 debug_printf(state, " invalid frame pointer %p\n", tail);
418 return NULL;
419 }
420 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
421 debug_printf(state,
422 " failed to copy frame pointer %p\n", tail);
423 return NULL;
424 }
425
426 debug_printf(state, " %p\n", buftail[0].lr);
427
428 /* frame pointers should strictly progress back up the stack
429 * (towards higher addresses) */
430 if (tail >= buftail[0].fp)
431 return NULL;
432
433 return buftail[0].fp-1;
434}
435
436void dump_stacktrace(struct fiq_debugger_state *state,
437 struct pt_regs * const regs, unsigned int depth, void *ssp)
438{
439 struct frame_tail *tail;
440 struct thread_info *real_thread_info = THREAD_INFO(ssp);
441 struct stacktrace_state sts;
442
443 sts.depth = depth;
444 sts.state = state;
445 *current_thread_info() = *real_thread_info;
446
447 if (!current)
448 debug_printf(state, "current NULL\n");
449 else
450 debug_printf(state, "pid: %d comm: %s\n",
451 current->pid, current->comm);
452 dump_regs(state, (unsigned *)regs);
453
454 if (!user_mode(regs)) {
455 struct stackframe frame;
456 frame.fp = regs->ARM_fp;
457 frame.sp = regs->ARM_sp;
458 frame.lr = regs->ARM_lr;
459 frame.pc = regs->ARM_pc;
460 debug_printf(state,
461 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
462 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
463 regs->ARM_sp, regs->ARM_fp);
464 walk_stackframe(&frame, report_trace, &sts);
465 return;
466 }
467
468 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
469 while (depth-- && tail && !((unsigned long) tail & 3))
470 tail = user_backtrace(state, tail);
471}
472
473static void do_ps(struct fiq_debugger_state *state)
474{
475 struct task_struct *g;
476 struct task_struct *p;
477 unsigned task_state;
478 static const char stat_nam[] = "RSDTtZX";
479
480 debug_printf(state, "pid ppid prio task pc\n");
481 read_lock(&tasklist_lock);
482 do_each_thread(g, p) {
483 task_state = p->state ? __ffs(p->state) + 1 : 0;
484 debug_printf(state,
485 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
486 debug_printf(state, "%-13.13s %c", p->comm,
487 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
488 if (task_state == TASK_RUNNING)
489 debug_printf(state, " running\n");
490 else
491 debug_printf(state, " %08lx\n", thread_saved_pc(p));
492 } while_each_thread(g, p);
493 read_unlock(&tasklist_lock);
494}
495
496#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
497static void begin_syslog_dump(struct fiq_debugger_state *state)
498{
499 state->syslog_dumping = true;
500}
501
502static void end_syslog_dump(struct fiq_debugger_state *state)
503{
504 state->syslog_dumping = false;
505}
506#else
507extern int do_syslog(int type, char __user *bug, int count);
508static void begin_syslog_dump(struct fiq_debugger_state *state)
509{
510 do_syslog(5 /* clear */, NULL, 0);
511}
512
513static void end_syslog_dump(struct fiq_debugger_state *state)
514{
515 char buf[128];
516 int ret;
517 int idx = 0;
518
519 while (1) {
520 ret = log_buf_copy(buf, idx, sizeof(buf) - 1);
521 if (ret <= 0)
522 break;
523 buf[ret] = 0;
524 debug_printf(state, "%s", buf);
525 idx += ret;
526 }
527}
528#endif
529
530static void do_sysrq(struct fiq_debugger_state *state, char rq)
531{
Colin Cross688a9012012-03-14 19:23:29 -0700532 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
533 debug_printf(state, "sysrq-g blocked\n");
534 return;
535 }
Iliyan Malchev09688d12010-06-05 17:36:24 -0700536 begin_syslog_dump(state);
537 handle_sysrq(rq);
538 end_syslog_dump(state);
539}
540
Colin Cross688a9012012-03-14 19:23:29 -0700541#ifdef CONFIG_KGDB
542static void do_kgdb(struct fiq_debugger_state *state)
543{
544 if (!fiq_kgdb_enable) {
545 debug_printf(state, "kgdb through fiq debugger not enabled\n");
546 return;
547 }
548
549 debug_printf(state, "enabling console and triggering kgdb\n");
550 state->console_enable = true;
551 handle_sysrq('g');
552}
553#endif
554
Iliyan Malchev09688d12010-06-05 17:36:24 -0700555/* This function CANNOT be called in FIQ context */
556static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
557{
558 if (!strcmp(cmd, "ps"))
559 do_ps(state);
560 if (!strcmp(cmd, "sysrq"))
561 do_sysrq(state, 'h');
562 if (!strncmp(cmd, "sysrq ", 6))
563 do_sysrq(state, cmd[6]);
Colin Cross688a9012012-03-14 19:23:29 -0700564#ifdef CONFIG_KGDB
565 if (!strcmp(cmd, "kgdb"))
566 do_kgdb(state);
567#endif
Iliyan Malchev09688d12010-06-05 17:36:24 -0700568}
569
570static void debug_help(struct fiq_debugger_state *state)
571{
572 debug_printf(state, "FIQ Debugger commands:\n"
573 " pc PC status\n"
574 " regs Register dump\n"
575 " allregs Extended Register dump\n"
576 " bt Stack trace\n"
577 " reboot Reboot\n"
578 " irqs Interupt status\n"
579 " kmsg Kernel log\n"
580 " version Kernel version\n");
581 debug_printf(state, " sleep Allow sleep while in FIQ\n"
582 " nosleep Disable sleep while in FIQ\n"
583 " console Switch terminal to console\n"
584 " cpu Current CPU\n"
585 " cpu <number> Switch to CPU<number>\n");
586 debug_printf(state, " ps Process list\n"
587 " sysrq sysrq options\n"
588 " sysrq <param> Execute sysrq with <param>\n");
Colin Cross688a9012012-03-14 19:23:29 -0700589#ifdef CONFIG_KGDB
590 debug_printf(state, " kgdb Enter kernel debugger\n");
591#endif
Iliyan Malchev09688d12010-06-05 17:36:24 -0700592}
593
594static void take_affinity(void *info)
595{
596 struct fiq_debugger_state *state = info;
597 struct cpumask cpumask;
598
599 cpumask_clear(&cpumask);
600 cpumask_set_cpu(get_cpu(), &cpumask);
601
602 irq_set_affinity(state->uart_irq, &cpumask);
603}
604
605static void switch_cpu(struct fiq_debugger_state *state, int cpu)
606{
607 if (!debug_have_fiq(state))
608 smp_call_function_single(cpu, take_affinity, state, false);
609 state->current_cpu = cpu;
610}
611
612static bool debug_fiq_exec(struct fiq_debugger_state *state,
613 const char *cmd, unsigned *regs, void *svc_sp)
614{
615 bool signal_helper = false;
616
617 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
618 debug_help(state);
619 } else if (!strcmp(cmd, "pc")) {
620 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
621 regs[15], regs[16], mode_name(regs[16]));
622 } else if (!strcmp(cmd, "regs")) {
623 dump_regs(state, regs);
624 } else if (!strcmp(cmd, "allregs")) {
625 dump_allregs(state, regs);
626 } else if (!strcmp(cmd, "bt")) {
627 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
Colin Crosse4602252012-03-14 16:29:47 -0700628 } else if (!strncmp(cmd, "reboot", 6)) {
629 cmd += 6;
630 while (*cmd == ' ')
631 cmd++;
632 if (*cmd) {
633 char tmp_cmd[32];
634 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
635 kernel_restart(tmp_cmd);
636 } else {
637 kernel_restart(NULL);
638 }
Iliyan Malchev09688d12010-06-05 17:36:24 -0700639 } else if (!strcmp(cmd, "irqs")) {
640 dump_irqs(state);
641 } else if (!strcmp(cmd, "kmsg")) {
642 dump_kernel_log(state);
643 } else if (!strcmp(cmd, "version")) {
644 debug_printf(state, "%s\n", linux_banner);
645 } else if (!strcmp(cmd, "sleep")) {
646 state->no_sleep = false;
647 debug_printf(state, "enabling sleep\n");
648 } else if (!strcmp(cmd, "nosleep")) {
649 state->no_sleep = true;
650 debug_printf(state, "disabling sleep\n");
651 } else if (!strcmp(cmd, "console")) {
652 state->console_enable = true;
653 debug_printf(state, "console mode\n");
654 } else if (!strcmp(cmd, "cpu")) {
655 debug_printf(state, "cpu %d\n", state->current_cpu);
656 } else if (!strncmp(cmd, "cpu ", 4)) {
657 unsigned long cpu = 0;
658 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
659 switch_cpu(state, cpu);
660 else
661 debug_printf(state, "invalid cpu\n");
662 debug_printf(state, "cpu %d\n", state->current_cpu);
663 } else {
664 if (state->debug_busy) {
665 debug_printf(state,
666 "command processor busy. trying to abort.\n");
667 state->debug_abort = -1;
668 } else {
669 strcpy(state->debug_cmd, cmd);
670 state->debug_busy = 1;
671 }
672
673 return true;
674 }
675 if (!state->console_enable)
676 debug_prompt(state);
677
678 return signal_helper;
679}
680
681static void sleep_timer_expired(unsigned long data)
682{
683 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
684 unsigned long flags;
685
686 spin_lock_irqsave(&state->sleep_timer_lock, flags);
687 if (state->uart_enabled && !state->no_sleep) {
688 if (state->debug_enable && !state->console_enable) {
689 state->debug_enable = false;
690 debug_printf_nfiq(state, "suspending fiq debugger\n");
691 }
692 state->ignore_next_wakeup_irq = true;
693 debug_uart_disable(state);
694 state->uart_enabled = false;
695 enable_wakeup_irq(state);
696 }
697 wake_unlock(&state->debugger_wake_lock);
698 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
699}
700
701static void handle_wakeup(struct fiq_debugger_state *state)
702{
703 unsigned long flags;
704
705 spin_lock_irqsave(&state->sleep_timer_lock, flags);
706 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
707 state->ignore_next_wakeup_irq = false;
708 } else if (!state->uart_enabled) {
709 wake_lock(&state->debugger_wake_lock);
710 debug_uart_enable(state);
711 state->uart_enabled = true;
712 disable_wakeup_irq(state);
713 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
714 }
715 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
716}
717
718static irqreturn_t wakeup_irq_handler(int irq, void *dev)
719{
720 struct fiq_debugger_state *state = dev;
721
722 if (!state->no_sleep)
723 debug_puts(state, "WAKEUP\n");
724 handle_wakeup(state);
725
726 return IRQ_HANDLED;
727}
728
729
730static void debug_handle_irq_context(struct fiq_debugger_state *state)
731{
732 if (!state->no_sleep) {
733 unsigned long flags;
734
735 spin_lock_irqsave(&state->sleep_timer_lock, flags);
736 wake_lock(&state->debugger_wake_lock);
737 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
738 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
739 }
740#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
741 if (state->tty) {
742 int i;
743 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
744 for (i = 0; i < count; i++) {
745 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
746 tty_insert_flip_char(state->tty, c, TTY_NORMAL);
747 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
748 pr_warn("fiq tty failed to consume byte\n");
749 }
750 tty_flip_buffer_push(state->tty);
751 }
752#endif
753 if (state->debug_busy) {
754 debug_irq_exec(state, state->debug_cmd);
Colin Cross688a9012012-03-14 19:23:29 -0700755 if (!state->console_enable)
756 debug_prompt(state);
Iliyan Malchev09688d12010-06-05 17:36:24 -0700757 state->debug_busy = 0;
758 }
759}
760
761static int debug_getc(struct fiq_debugger_state *state)
762{
763 return state->pdata->uart_getc(state->pdev);
764}
765
766static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
767 int this_cpu, void *regs, void *svc_sp)
768{
769 int c;
770 static int last_c;
771 int count = 0;
772 bool signal_helper = false;
773
774 if (this_cpu != state->current_cpu) {
775 if (state->in_fiq)
776 return false;
777
778 if (atomic_inc_return(&state->unhandled_fiq_count) !=
779 MAX_UNHANDLED_FIQ_COUNT)
780 return false;
781
782 debug_printf(state, "fiq_debugger: cpu %d not responding, "
783 "reverting to cpu %d\n", state->current_cpu,
784 this_cpu);
785
786 atomic_set(&state->unhandled_fiq_count, 0);
787 switch_cpu(state, this_cpu);
788 return false;
789 }
790
791 state->in_fiq = true;
792
793 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
794 count++;
795 if (!state->debug_enable) {
796 if ((c == 13) || (c == 10)) {
797 state->debug_enable = true;
798 state->debug_count = 0;
799 debug_prompt(state);
800 }
801 } else if (c == FIQ_DEBUGGER_BREAK) {
802 state->console_enable = false;
803 debug_puts(state, "fiq debugger mode\n");
804 state->debug_count = 0;
805 debug_prompt(state);
806#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
807 } else if (state->console_enable && state->tty_rbuf) {
808 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
809 signal_helper = true;
810#endif
811 } else if ((c >= ' ') && (c < 127)) {
812 if (state->debug_count < (DEBUG_MAX - 1)) {
813 state->debug_buf[state->debug_count++] = c;
Colin Crossd72d89e2012-03-15 12:57:20 -0700814 debug_putc(state, c);
Iliyan Malchev09688d12010-06-05 17:36:24 -0700815 }
816 } else if ((c == 8) || (c == 127)) {
817 if (state->debug_count > 0) {
818 state->debug_count--;
Colin Crossd72d89e2012-03-15 12:57:20 -0700819 debug_putc(state, 8);
820 debug_putc(state, ' ');
821 debug_putc(state, 8);
Iliyan Malchev09688d12010-06-05 17:36:24 -0700822 }
823 } else if ((c == 13) || (c == 10)) {
824 if (c == '\r' || (c == '\n' && last_c != '\r')) {
Colin Crossd72d89e2012-03-15 12:57:20 -0700825 debug_putc(state, '\r');
826 debug_putc(state, '\n');
Iliyan Malchev09688d12010-06-05 17:36:24 -0700827 }
828 if (state->debug_count) {
829 state->debug_buf[state->debug_count] = 0;
830 state->debug_count = 0;
831 signal_helper |=
832 debug_fiq_exec(state, state->debug_buf,
833 regs, svc_sp);
834 } else {
835 debug_prompt(state);
836 }
837 }
838 last_c = c;
839 }
840 debug_uart_flush(state);
841 if (state->pdata->fiq_ack)
842 state->pdata->fiq_ack(state->pdev, state->fiq);
843
844 /* poke sleep timer if necessary */
845 if (state->debug_enable && !state->no_sleep)
846 signal_helper = true;
847
848 atomic_set(&state->unhandled_fiq_count, 0);
849 state->in_fiq = false;
850
851 return signal_helper;
852}
853
854static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
855{
856 struct fiq_debugger_state *state =
857 container_of(h, struct fiq_debugger_state, handler);
858 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
859 bool need_irq;
860
861 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
862 if (need_irq)
863 debug_force_irq(state);
864}
865
866/*
867 * When not using FIQs, we only use this single interrupt as an entry point.
868 * This just effectively takes over the UART interrupt and does all the work
869 * in this context.
870 */
871static irqreturn_t debug_uart_irq(int irq, void *dev)
872{
873 struct fiq_debugger_state *state = dev;
874 bool not_done;
875
876 handle_wakeup(state);
877
878 /* handle the debugger irq in regular context */
879 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
880 get_irq_regs(),
881 current_thread_info());
882 if (not_done)
883 debug_handle_irq_context(state);
884
885 return IRQ_HANDLED;
886}
887
888/*
889 * If FIQs are used, not everything can happen in fiq context.
890 * FIQ handler does what it can and then signals this interrupt to finish the
891 * job in irq context.
892 */
893static irqreturn_t debug_signal_irq(int irq, void *dev)
894{
895 struct fiq_debugger_state *state = dev;
896
897 if (state->pdata->force_irq_ack)
898 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
899
900 debug_handle_irq_context(state);
901
902 return IRQ_HANDLED;
903}
904
905static void debug_resume(struct fiq_glue_handler *h)
906{
907 struct fiq_debugger_state *state =
908 container_of(h, struct fiq_debugger_state, handler);
909 if (state->pdata->uart_resume)
910 state->pdata->uart_resume(state->pdev);
911}
912
913#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
914struct tty_driver *debug_console_device(struct console *co, int *index)
915{
916 struct fiq_debugger_state *state;
917 state = container_of(co, struct fiq_debugger_state, console);
918 *index = 0;
919 return state->tty_driver;
920}
921
922static void debug_console_write(struct console *co,
923 const char *s, unsigned int count)
924{
925 struct fiq_debugger_state *state;
926
927 state = container_of(co, struct fiq_debugger_state, console);
928
929 if (!state->console_enable && !state->syslog_dumping)
930 return;
931
932 debug_uart_enable(state);
933 while (count--) {
934 if (*s == '\n')
Colin Crossd72d89e2012-03-15 12:57:20 -0700935 debug_putc(state, '\r');
936 debug_putc(state, *s++);
Iliyan Malchev09688d12010-06-05 17:36:24 -0700937 }
938 debug_uart_flush(state);
939 debug_uart_disable(state);
940}
941
942static struct console fiq_debugger_console = {
943 .name = "ttyFIQ",
944 .device = debug_console_device,
945 .write = debug_console_write,
946 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
947};
948
949int fiq_tty_open(struct tty_struct *tty, struct file *filp)
950{
951 struct fiq_debugger_state *state = tty->driver->driver_state;
952 if (state->tty_open_count++)
953 return 0;
954
955 tty->driver_data = state;
956 state->tty = tty;
957 return 0;
958}
959
960void fiq_tty_close(struct tty_struct *tty, struct file *filp)
961{
962 struct fiq_debugger_state *state = tty->driver_data;
963 if (--state->tty_open_count)
964 return;
965 state->tty = NULL;
966}
967
968int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
969{
970 int i;
971 struct fiq_debugger_state *state = tty->driver_data;
972
973 if (!state->console_enable)
974 return count;
975
976 debug_uart_enable(state);
977 for (i = 0; i < count; i++)
Colin Crossd72d89e2012-03-15 12:57:20 -0700978 debug_putc(state, *buf++);
Iliyan Malchev09688d12010-06-05 17:36:24 -0700979 debug_uart_disable(state);
980
981 return count;
982}
983
984int fiq_tty_write_room(struct tty_struct *tty)
985{
986 return 1024;
987}
988
Colin Cross688a9012012-03-14 19:23:29 -0700989#ifdef CONFIG_CONSOLE_POLL
990static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
991{
992 return 0;
993}
994
995static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
996{
997 struct fiq_debugger_state *state = driver->ttys[line]->driver_data;
998 int c = NO_POLL_CHAR;
999
1000 debug_uart_enable(state);
1001 if (debug_have_fiq(state)) {
1002 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1003 if (count > 0) {
1004 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1005 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1006 }
1007 } else {
1008 c = debug_getc(state);
1009 if (c == FIQ_DEBUGGER_NO_CHAR)
1010 c = NO_POLL_CHAR;
1011 }
1012 debug_uart_disable(state);
1013
1014 return c;
1015}
1016
1017static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1018{
1019 struct fiq_debugger_state *state = driver->ttys[line]->driver_data;
1020 debug_uart_enable(state);
1021 debug_putc(state, ch);
1022 debug_uart_disable(state);
1023}
1024#endif
1025
Iliyan Malchev09688d12010-06-05 17:36:24 -07001026static const struct tty_operations fiq_tty_driver_ops = {
1027 .write = fiq_tty_write,
1028 .write_room = fiq_tty_write_room,
1029 .open = fiq_tty_open,
1030 .close = fiq_tty_close,
Colin Cross688a9012012-03-14 19:23:29 -07001031#ifdef CONFIG_CONSOLE_POLL
1032 .poll_init = fiq_tty_poll_init,
1033 .poll_get_char = fiq_tty_poll_get_char,
1034 .poll_put_char = fiq_tty_poll_put_char,
1035#endif
Iliyan Malchev09688d12010-06-05 17:36:24 -07001036};
1037
1038static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
1039{
1040 int ret = -EINVAL;
1041
1042 state->tty_driver = alloc_tty_driver(1);
1043 if (!state->tty_driver) {
1044 pr_err("Failed to allocate fiq debugger tty\n");
1045 return -ENOMEM;
1046 }
1047
1048 state->tty_driver->owner = THIS_MODULE;
1049 state->tty_driver->driver_name = "fiq-debugger";
1050 state->tty_driver->name = "ttyFIQ";
1051 state->tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1052 state->tty_driver->subtype = SERIAL_TYPE_NORMAL;
1053 state->tty_driver->init_termios = tty_std_termios;
1054 state->tty_driver->init_termios.c_cflag =
1055 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1056 state->tty_driver->init_termios.c_ispeed =
1057 state->tty_driver->init_termios.c_ospeed = 115200;
1058 state->tty_driver->flags = TTY_DRIVER_REAL_RAW;
1059 tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
1060 state->tty_driver->driver_state = state;
1061
1062 ret = tty_register_driver(state->tty_driver);
1063 if (ret) {
1064 pr_err("Failed to register fiq tty: %d\n", ret);
1065 goto err;
1066 }
1067
1068 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1069 if (!state->tty_rbuf) {
1070 pr_err("Failed to allocate fiq debugger ringbuf\n");
1071 ret = -ENOMEM;
1072 goto err;
1073 }
1074
1075 pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
1076 return 0;
1077
1078err:
1079 fiq_debugger_ringbuf_free(state->tty_rbuf);
1080 state->tty_rbuf = NULL;
1081 put_tty_driver(state->tty_driver);
1082 return ret;
1083}
1084#endif
1085
1086static int fiq_debugger_dev_suspend(struct device *dev)
1087{
1088 struct platform_device *pdev = to_platform_device(dev);
1089 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1090
1091 if (state->pdata->uart_dev_suspend)
1092 return state->pdata->uart_dev_suspend(pdev);
1093 return 0;
1094}
1095
1096static int fiq_debugger_dev_resume(struct device *dev)
1097{
1098 struct platform_device *pdev = to_platform_device(dev);
1099 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1100
1101 if (state->pdata->uart_dev_resume)
1102 return state->pdata->uart_dev_resume(pdev);
1103 return 0;
1104}
1105
1106static int fiq_debugger_probe(struct platform_device *pdev)
1107{
1108 int ret;
1109 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1110 struct fiq_debugger_state *state;
1111 int fiq;
1112 int uart_irq;
1113
1114 if (!pdata->uart_getc || !pdata->uart_putc)
1115 return -EINVAL;
1116 if ((pdata->uart_enable && !pdata->uart_disable) ||
1117 (!pdata->uart_enable && pdata->uart_disable))
1118 return -EINVAL;
1119
1120 fiq = platform_get_irq_byname(pdev, "fiq");
1121 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1122
1123 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1124 * is required */
1125 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1126 return -EINVAL;
1127 if (fiq >= 0 && !pdata->fiq_enable)
1128 return -EINVAL;
1129
1130 state = kzalloc(sizeof(*state), GFP_KERNEL);
1131 setup_timer(&state->sleep_timer, sleep_timer_expired,
1132 (unsigned long)state);
1133 state->pdata = pdata;
1134 state->pdev = pdev;
1135 state->no_sleep = initial_no_sleep;
1136 state->debug_enable = initial_debug_enable;
1137 state->console_enable = initial_console_enable;
1138
1139 state->fiq = fiq;
1140 state->uart_irq = uart_irq;
1141 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1142 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1143
1144 platform_set_drvdata(pdev, state);
1145
1146 spin_lock_init(&state->sleep_timer_lock);
1147
1148 if (state->wakeup_irq < 0 && debug_have_fiq(state))
1149 state->no_sleep = true;
1150 state->ignore_next_wakeup_irq = !state->no_sleep;
1151
1152 wake_lock_init(&state->debugger_wake_lock,
1153 WAKE_LOCK_SUSPEND, "serial-debug");
1154
1155 state->clk = clk_get(&pdev->dev, NULL);
1156 if (IS_ERR(state->clk))
1157 state->clk = NULL;
1158
1159 /* do not call pdata->uart_enable here since uart_init may still
1160 * need to do some initialization before uart_enable can work.
1161 * So, only try to manage the clock during init.
1162 */
1163 if (state->clk)
1164 clk_enable(state->clk);
1165
1166 if (pdata->uart_init) {
1167 ret = pdata->uart_init(pdev);
1168 if (ret)
1169 goto err_uart_init;
1170 }
1171
1172 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1173 state->no_sleep ? "" : "twice ");
1174
1175 if (debug_have_fiq(state)) {
1176 state->handler.fiq = debug_fiq;
1177 state->handler.resume = debug_resume;
1178 ret = fiq_glue_register_handler(&state->handler);
1179 if (ret) {
1180 pr_err("%s: could not install fiq handler\n", __func__);
1181 goto err_register_fiq;
1182 }
1183
1184 pdata->fiq_enable(pdev, state->fiq, 1);
1185 } else {
1186 ret = request_irq(state->uart_irq, debug_uart_irq,
1187 IRQF_NO_SUSPEND, "debug", state);
1188 if (ret) {
1189 pr_err("%s: could not install irq handler\n", __func__);
1190 goto err_register_irq;
1191 }
1192
1193 /* for irq-only mode, we want this irq to wake us up, if it
1194 * can.
1195 */
1196 enable_irq_wake(state->uart_irq);
1197 }
1198
1199 if (state->clk)
1200 clk_disable(state->clk);
1201
1202 if (state->signal_irq >= 0) {
1203 ret = request_irq(state->signal_irq, debug_signal_irq,
1204 IRQF_TRIGGER_RISING, "debug-signal", state);
1205 if (ret)
1206 pr_err("serial_debugger: could not install signal_irq");
1207 }
1208
1209 if (state->wakeup_irq >= 0) {
1210 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1211 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1212 "debug-wakeup", state);
1213 if (ret) {
1214 pr_err("serial_debugger: "
1215 "could not install wakeup irq\n");
1216 state->wakeup_irq = -1;
1217 } else {
1218 ret = enable_irq_wake(state->wakeup_irq);
1219 if (ret) {
1220 pr_err("serial_debugger: "
1221 "could not enable wakeup\n");
1222 state->wakeup_irq_no_set_wake = true;
1223 }
1224 }
1225 }
1226 if (state->no_sleep)
1227 handle_wakeup(state);
1228
1229#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1230 state->console = fiq_debugger_console;
1231 register_console(&state->console);
1232 fiq_debugger_tty_init(state);
1233#endif
1234 return 0;
1235
1236err_register_irq:
1237err_register_fiq:
1238 if (pdata->uart_free)
1239 pdata->uart_free(pdev);
1240err_uart_init:
1241 if (state->clk)
1242 clk_disable(state->clk);
1243 if (state->clk)
1244 clk_put(state->clk);
1245 wake_lock_destroy(&state->debugger_wake_lock);
1246 platform_set_drvdata(pdev, NULL);
1247 kfree(state);
1248 return ret;
1249}
1250
1251static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1252 .suspend = fiq_debugger_dev_suspend,
1253 .resume = fiq_debugger_dev_resume,
1254};
1255
1256static struct platform_driver fiq_debugger_driver = {
1257 .probe = fiq_debugger_probe,
1258 .driver = {
1259 .name = "fiq_debugger",
1260 .pm = &fiq_debugger_dev_pm_ops,
1261 },
1262};
1263
1264static int __init fiq_debugger_init(void)
1265{
1266 return platform_driver_register(&fiq_debugger_driver);
1267}
1268
1269postcore_initcall(fiq_debugger_init);