blob: 0e33748edd6080a860cdc07988a2ac29bb1e5f61 [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
109module_param_named(no_sleep, initial_no_sleep, bool, 0644);
110module_param_named(debug_enable, initial_debug_enable, bool, 0644);
111module_param_named(console_enable, initial_console_enable, bool, 0644);
112
113#ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
114static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
115static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
116#else
117static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
118{
119 if (state->wakeup_irq < 0)
120 return;
121 enable_irq(state->wakeup_irq);
122 if (!state->wakeup_irq_no_set_wake)
123 enable_irq_wake(state->wakeup_irq);
124}
125static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
126{
127 if (state->wakeup_irq < 0)
128 return;
129 disable_irq_nosync(state->wakeup_irq);
130 if (!state->wakeup_irq_no_set_wake)
131 disable_irq_wake(state->wakeup_irq);
132}
133#endif
134
135static bool inline debug_have_fiq(struct fiq_debugger_state *state)
136{
137 return (state->fiq >= 0);
138}
139
140static void debug_force_irq(struct fiq_debugger_state *state)
141{
142 unsigned int irq = state->signal_irq;
143
144 if (WARN_ON(!debug_have_fiq(state)))
145 return;
146 if (state->pdata->force_irq) {
147 state->pdata->force_irq(state->pdev, irq);
148 } else {
149 struct irq_chip *chip = irq_get_chip(irq);
150 if (chip && chip->irq_retrigger)
151 chip->irq_retrigger(irq_get_irq_data(irq));
152 }
153}
154
155static void debug_uart_enable(struct fiq_debugger_state *state)
156{
157 if (state->clk)
158 clk_enable(state->clk);
159 if (state->pdata->uart_enable)
160 state->pdata->uart_enable(state->pdev);
161}
162
163static void debug_uart_disable(struct fiq_debugger_state *state)
164{
165 if (state->pdata->uart_disable)
166 state->pdata->uart_disable(state->pdev);
167 if (state->clk)
168 clk_disable(state->clk);
169}
170
171static void debug_uart_flush(struct fiq_debugger_state *state)
172{
173 if (state->pdata->uart_flush)
174 state->pdata->uart_flush(state->pdev);
175}
176
177static void debug_puts(struct fiq_debugger_state *state, char *s)
178{
179 unsigned c;
180 while ((c = *s++)) {
181 if (c == '\n')
182 state->pdata->uart_putc(state->pdev, '\r');
183 state->pdata->uart_putc(state->pdev, c);
184 }
185}
186
187static void debug_prompt(struct fiq_debugger_state *state)
188{
189 debug_puts(state, "debug> ");
190}
191
192int log_buf_copy(char *dest, int idx, int len);
193static void dump_kernel_log(struct fiq_debugger_state *state)
194{
195 char buf[1024];
196 int idx = 0;
197 int ret;
198 int saved_oip;
199
200 /* setting oops_in_progress prevents log_buf_copy()
201 * from trying to take a spinlock which will make it
202 * very unhappy in some cases...
203 */
204 saved_oip = oops_in_progress;
205 oops_in_progress = 1;
206 for (;;) {
207 ret = log_buf_copy(buf, idx, 1023);
208 if (ret <= 0)
209 break;
210 buf[ret] = 0;
211 debug_puts(state, buf);
212 idx += ret;
213 }
214 oops_in_progress = saved_oip;
215}
216
217static char *mode_name(unsigned cpsr)
218{
219 switch (cpsr & MODE_MASK) {
220 case USR_MODE: return "USR";
221 case FIQ_MODE: return "FIQ";
222 case IRQ_MODE: return "IRQ";
223 case SVC_MODE: return "SVC";
224 case ABT_MODE: return "ABT";
225 case UND_MODE: return "UND";
226 case SYSTEM_MODE: return "SYS";
227 default: return "???";
228 }
229}
230
231static int debug_printf(void *cookie, const char *fmt, ...)
232{
233 struct fiq_debugger_state *state = cookie;
234 char buf[256];
235 va_list ap;
236
237 va_start(ap, fmt);
238 vsnprintf(buf, sizeof(buf), fmt, ap);
239 va_end(ap);
240
241 debug_puts(state, buf);
242 return state->debug_abort;
243}
244
245/* Safe outside fiq context */
246static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
247{
248 struct fiq_debugger_state *state = cookie;
249 char buf[256];
250 va_list ap;
251 unsigned long irq_flags;
252
253 va_start(ap, fmt);
254 vsnprintf(buf, 128, fmt, ap);
255 va_end(ap);
256
257 local_irq_save(irq_flags);
258 debug_puts(state, buf);
259 debug_uart_flush(state);
260 local_irq_restore(irq_flags);
261 return state->debug_abort;
262}
263
264static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
265{
266 debug_printf(state, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
267 regs[0], regs[1], regs[2], regs[3]);
268 debug_printf(state, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
269 regs[4], regs[5], regs[6], regs[7]);
270 debug_printf(state, " r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
271 regs[8], regs[9], regs[10], regs[11],
272 mode_name(regs[16]));
273 if ((regs[16] & MODE_MASK) == USR_MODE)
274 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
275 "cpsr %08x\n", regs[12], regs[13], regs[14],
276 regs[15], regs[16]);
277 else
278 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
279 "cpsr %08x spsr %08x\n", regs[12], regs[13],
280 regs[14], regs[15], regs[16], regs[17]);
281}
282
283struct mode_regs {
284 unsigned long sp_svc;
285 unsigned long lr_svc;
286 unsigned long spsr_svc;
287
288 unsigned long sp_abt;
289 unsigned long lr_abt;
290 unsigned long spsr_abt;
291
292 unsigned long sp_und;
293 unsigned long lr_und;
294 unsigned long spsr_und;
295
296 unsigned long sp_irq;
297 unsigned long lr_irq;
298 unsigned long spsr_irq;
299
300 unsigned long r8_fiq;
301 unsigned long r9_fiq;
302 unsigned long r10_fiq;
303 unsigned long r11_fiq;
304 unsigned long r12_fiq;
305 unsigned long sp_fiq;
306 unsigned long lr_fiq;
307 unsigned long spsr_fiq;
308};
309
310void __naked get_mode_regs(struct mode_regs *regs)
311{
312 asm volatile (
313 "mrs r1, cpsr\n"
314 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
315 "stmia r0!, {r13 - r14}\n"
316 "mrs r2, spsr\n"
317 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
318 "stmia r0!, {r2, r13 - r14}\n"
319 "mrs r2, spsr\n"
320 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
321 "stmia r0!, {r2, r13 - r14}\n"
322 "mrs r2, spsr\n"
323 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
324 "stmia r0!, {r2, r13 - r14}\n"
325 "mrs r2, spsr\n"
326 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
327 "stmia r0!, {r2, r8 - r14}\n"
328 "mrs r2, spsr\n"
329 "stmia r0!, {r2}\n"
330 "msr cpsr_c, r1\n"
331 "bx lr\n");
332}
333
334
335static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
336{
337 struct mode_regs mode_regs;
338 dump_regs(state, regs);
339 get_mode_regs(&mode_regs);
340 debug_printf(state, " svc: sp %08x lr %08x spsr %08x\n",
341 mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
342 debug_printf(state, " abt: sp %08x lr %08x spsr %08x\n",
343 mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
344 debug_printf(state, " und: sp %08x lr %08x spsr %08x\n",
345 mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
346 debug_printf(state, " irq: sp %08x lr %08x spsr %08x\n",
347 mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
348 debug_printf(state, " fiq: r8 %08x r9 %08x r10 %08x r11 %08x "
349 "r12 %08x\n",
350 mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
351 mode_regs.r11_fiq, mode_regs.r12_fiq);
352 debug_printf(state, " fiq: sp %08x lr %08x spsr %08x\n",
353 mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
354}
355
356static void dump_irqs(struct fiq_debugger_state *state)
357{
358 int n;
Iliyan Malchev09688d12010-06-05 17:36:24 -0700359
360 debug_printf(state, "irqnr total since-last status name\n");
361 for (n = 0; n < NR_IRQS; n++) {
362 struct irqaction *act = irq_desc[n].action;
363 if (!act && !kstat_irqs(n))
364 continue;
365 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
366 kstat_irqs(n),
367 kstat_irqs(n) - state->last_irqs[n],
368 irq_desc[n].status_use_accessors,
369 (act && act->name) ? act->name : "???");
370 state->last_irqs[n] = kstat_irqs(n);
371 }
Iliyan Malchev09688d12010-06-05 17:36:24 -0700372}
373
374struct stacktrace_state {
375 struct fiq_debugger_state *state;
376 unsigned int depth;
377};
378
379static int report_trace(struct stackframe *frame, void *d)
380{
381 struct stacktrace_state *sts = d;
382
383 if (sts->depth) {
384 debug_printf(sts->state,
385 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
386 frame->pc, frame->pc, frame->lr, frame->lr,
387 frame->sp, frame->fp);
388 sts->depth--;
389 return 0;
390 }
391 debug_printf(sts->state, " ...\n");
392
393 return sts->depth == 0;
394}
395
396struct frame_tail {
397 struct frame_tail *fp;
398 unsigned long sp;
399 unsigned long lr;
400} __attribute__((packed));
401
402static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
403 struct frame_tail *tail)
404{
405 struct frame_tail buftail[2];
406
407 /* Also check accessibility of one struct frame_tail beyond */
408 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
409 debug_printf(state, " invalid frame pointer %p\n", tail);
410 return NULL;
411 }
412 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
413 debug_printf(state,
414 " failed to copy frame pointer %p\n", tail);
415 return NULL;
416 }
417
418 debug_printf(state, " %p\n", buftail[0].lr);
419
420 /* frame pointers should strictly progress back up the stack
421 * (towards higher addresses) */
422 if (tail >= buftail[0].fp)
423 return NULL;
424
425 return buftail[0].fp-1;
426}
427
428void dump_stacktrace(struct fiq_debugger_state *state,
429 struct pt_regs * const regs, unsigned int depth, void *ssp)
430{
431 struct frame_tail *tail;
432 struct thread_info *real_thread_info = THREAD_INFO(ssp);
433 struct stacktrace_state sts;
434
435 sts.depth = depth;
436 sts.state = state;
437 *current_thread_info() = *real_thread_info;
438
439 if (!current)
440 debug_printf(state, "current NULL\n");
441 else
442 debug_printf(state, "pid: %d comm: %s\n",
443 current->pid, current->comm);
444 dump_regs(state, (unsigned *)regs);
445
446 if (!user_mode(regs)) {
447 struct stackframe frame;
448 frame.fp = regs->ARM_fp;
449 frame.sp = regs->ARM_sp;
450 frame.lr = regs->ARM_lr;
451 frame.pc = regs->ARM_pc;
452 debug_printf(state,
453 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
454 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
455 regs->ARM_sp, regs->ARM_fp);
456 walk_stackframe(&frame, report_trace, &sts);
457 return;
458 }
459
460 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
461 while (depth-- && tail && !((unsigned long) tail & 3))
462 tail = user_backtrace(state, tail);
463}
464
465static void do_ps(struct fiq_debugger_state *state)
466{
467 struct task_struct *g;
468 struct task_struct *p;
469 unsigned task_state;
470 static const char stat_nam[] = "RSDTtZX";
471
472 debug_printf(state, "pid ppid prio task pc\n");
473 read_lock(&tasklist_lock);
474 do_each_thread(g, p) {
475 task_state = p->state ? __ffs(p->state) + 1 : 0;
476 debug_printf(state,
477 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
478 debug_printf(state, "%-13.13s %c", p->comm,
479 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
480 if (task_state == TASK_RUNNING)
481 debug_printf(state, " running\n");
482 else
483 debug_printf(state, " %08lx\n", thread_saved_pc(p));
484 } while_each_thread(g, p);
485 read_unlock(&tasklist_lock);
486}
487
488#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
489static void begin_syslog_dump(struct fiq_debugger_state *state)
490{
491 state->syslog_dumping = true;
492}
493
494static void end_syslog_dump(struct fiq_debugger_state *state)
495{
496 state->syslog_dumping = false;
497}
498#else
499extern int do_syslog(int type, char __user *bug, int count);
500static void begin_syslog_dump(struct fiq_debugger_state *state)
501{
502 do_syslog(5 /* clear */, NULL, 0);
503}
504
505static void end_syslog_dump(struct fiq_debugger_state *state)
506{
507 char buf[128];
508 int ret;
509 int idx = 0;
510
511 while (1) {
512 ret = log_buf_copy(buf, idx, sizeof(buf) - 1);
513 if (ret <= 0)
514 break;
515 buf[ret] = 0;
516 debug_printf(state, "%s", buf);
517 idx += ret;
518 }
519}
520#endif
521
522static void do_sysrq(struct fiq_debugger_state *state, char rq)
523{
524 begin_syslog_dump(state);
525 handle_sysrq(rq);
526 end_syslog_dump(state);
527}
528
529/* This function CANNOT be called in FIQ context */
530static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
531{
532 if (!strcmp(cmd, "ps"))
533 do_ps(state);
534 if (!strcmp(cmd, "sysrq"))
535 do_sysrq(state, 'h');
536 if (!strncmp(cmd, "sysrq ", 6))
537 do_sysrq(state, cmd[6]);
538}
539
540static void debug_help(struct fiq_debugger_state *state)
541{
542 debug_printf(state, "FIQ Debugger commands:\n"
543 " pc PC status\n"
544 " regs Register dump\n"
545 " allregs Extended Register dump\n"
546 " bt Stack trace\n"
547 " reboot Reboot\n"
548 " irqs Interupt status\n"
549 " kmsg Kernel log\n"
550 " version Kernel version\n");
551 debug_printf(state, " sleep Allow sleep while in FIQ\n"
552 " nosleep Disable sleep while in FIQ\n"
553 " console Switch terminal to console\n"
554 " cpu Current CPU\n"
555 " cpu <number> Switch to CPU<number>\n");
556 debug_printf(state, " ps Process list\n"
557 " sysrq sysrq options\n"
558 " sysrq <param> Execute sysrq with <param>\n");
559}
560
561static void take_affinity(void *info)
562{
563 struct fiq_debugger_state *state = info;
564 struct cpumask cpumask;
565
566 cpumask_clear(&cpumask);
567 cpumask_set_cpu(get_cpu(), &cpumask);
568
569 irq_set_affinity(state->uart_irq, &cpumask);
570}
571
572static void switch_cpu(struct fiq_debugger_state *state, int cpu)
573{
574 if (!debug_have_fiq(state))
575 smp_call_function_single(cpu, take_affinity, state, false);
576 state->current_cpu = cpu;
577}
578
579static bool debug_fiq_exec(struct fiq_debugger_state *state,
580 const char *cmd, unsigned *regs, void *svc_sp)
581{
582 bool signal_helper = false;
583
584 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
585 debug_help(state);
586 } else if (!strcmp(cmd, "pc")) {
587 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
588 regs[15], regs[16], mode_name(regs[16]));
589 } else if (!strcmp(cmd, "regs")) {
590 dump_regs(state, regs);
591 } else if (!strcmp(cmd, "allregs")) {
592 dump_allregs(state, regs);
593 } else if (!strcmp(cmd, "bt")) {
594 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
595 } else if (!strcmp(cmd, "reboot")) {
Colin Crossb9b3a062012-03-14 16:28:45 -0700596 kernel_restart(NULL);
Iliyan Malchev09688d12010-06-05 17:36:24 -0700597 } else if (!strcmp(cmd, "irqs")) {
598 dump_irqs(state);
599 } else if (!strcmp(cmd, "kmsg")) {
600 dump_kernel_log(state);
601 } else if (!strcmp(cmd, "version")) {
602 debug_printf(state, "%s\n", linux_banner);
603 } else if (!strcmp(cmd, "sleep")) {
604 state->no_sleep = false;
605 debug_printf(state, "enabling sleep\n");
606 } else if (!strcmp(cmd, "nosleep")) {
607 state->no_sleep = true;
608 debug_printf(state, "disabling sleep\n");
609 } else if (!strcmp(cmd, "console")) {
610 state->console_enable = true;
611 debug_printf(state, "console mode\n");
612 } else if (!strcmp(cmd, "cpu")) {
613 debug_printf(state, "cpu %d\n", state->current_cpu);
614 } else if (!strncmp(cmd, "cpu ", 4)) {
615 unsigned long cpu = 0;
616 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
617 switch_cpu(state, cpu);
618 else
619 debug_printf(state, "invalid cpu\n");
620 debug_printf(state, "cpu %d\n", state->current_cpu);
621 } else {
622 if (state->debug_busy) {
623 debug_printf(state,
624 "command processor busy. trying to abort.\n");
625 state->debug_abort = -1;
626 } else {
627 strcpy(state->debug_cmd, cmd);
628 state->debug_busy = 1;
629 }
630
631 return true;
632 }
633 if (!state->console_enable)
634 debug_prompt(state);
635
636 return signal_helper;
637}
638
639static void sleep_timer_expired(unsigned long data)
640{
641 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
642 unsigned long flags;
643
644 spin_lock_irqsave(&state->sleep_timer_lock, flags);
645 if (state->uart_enabled && !state->no_sleep) {
646 if (state->debug_enable && !state->console_enable) {
647 state->debug_enable = false;
648 debug_printf_nfiq(state, "suspending fiq debugger\n");
649 }
650 state->ignore_next_wakeup_irq = true;
651 debug_uart_disable(state);
652 state->uart_enabled = false;
653 enable_wakeup_irq(state);
654 }
655 wake_unlock(&state->debugger_wake_lock);
656 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
657}
658
659static void handle_wakeup(struct fiq_debugger_state *state)
660{
661 unsigned long flags;
662
663 spin_lock_irqsave(&state->sleep_timer_lock, flags);
664 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
665 state->ignore_next_wakeup_irq = false;
666 } else if (!state->uart_enabled) {
667 wake_lock(&state->debugger_wake_lock);
668 debug_uart_enable(state);
669 state->uart_enabled = true;
670 disable_wakeup_irq(state);
671 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
672 }
673 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
674}
675
676static irqreturn_t wakeup_irq_handler(int irq, void *dev)
677{
678 struct fiq_debugger_state *state = dev;
679
680 if (!state->no_sleep)
681 debug_puts(state, "WAKEUP\n");
682 handle_wakeup(state);
683
684 return IRQ_HANDLED;
685}
686
687
688static void debug_handle_irq_context(struct fiq_debugger_state *state)
689{
690 if (!state->no_sleep) {
691 unsigned long flags;
692
693 spin_lock_irqsave(&state->sleep_timer_lock, flags);
694 wake_lock(&state->debugger_wake_lock);
695 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
696 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
697 }
698#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
699 if (state->tty) {
700 int i;
701 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
702 for (i = 0; i < count; i++) {
703 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
704 tty_insert_flip_char(state->tty, c, TTY_NORMAL);
705 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
706 pr_warn("fiq tty failed to consume byte\n");
707 }
708 tty_flip_buffer_push(state->tty);
709 }
710#endif
711 if (state->debug_busy) {
712 debug_irq_exec(state, state->debug_cmd);
713 debug_prompt(state);
714 state->debug_busy = 0;
715 }
716}
717
718static int debug_getc(struct fiq_debugger_state *state)
719{
720 return state->pdata->uart_getc(state->pdev);
721}
722
723static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
724 int this_cpu, void *regs, void *svc_sp)
725{
726 int c;
727 static int last_c;
728 int count = 0;
729 bool signal_helper = false;
730
731 if (this_cpu != state->current_cpu) {
732 if (state->in_fiq)
733 return false;
734
735 if (atomic_inc_return(&state->unhandled_fiq_count) !=
736 MAX_UNHANDLED_FIQ_COUNT)
737 return false;
738
739 debug_printf(state, "fiq_debugger: cpu %d not responding, "
740 "reverting to cpu %d\n", state->current_cpu,
741 this_cpu);
742
743 atomic_set(&state->unhandled_fiq_count, 0);
744 switch_cpu(state, this_cpu);
745 return false;
746 }
747
748 state->in_fiq = true;
749
750 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
751 count++;
752 if (!state->debug_enable) {
753 if ((c == 13) || (c == 10)) {
754 state->debug_enable = true;
755 state->debug_count = 0;
756 debug_prompt(state);
757 }
758 } else if (c == FIQ_DEBUGGER_BREAK) {
759 state->console_enable = false;
760 debug_puts(state, "fiq debugger mode\n");
761 state->debug_count = 0;
762 debug_prompt(state);
763#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
764 } else if (state->console_enable && state->tty_rbuf) {
765 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
766 signal_helper = true;
767#endif
768 } else if ((c >= ' ') && (c < 127)) {
769 if (state->debug_count < (DEBUG_MAX - 1)) {
770 state->debug_buf[state->debug_count++] = c;
771 state->pdata->uart_putc(state->pdev, c);
772 }
773 } else if ((c == 8) || (c == 127)) {
774 if (state->debug_count > 0) {
775 state->debug_count--;
776 state->pdata->uart_putc(state->pdev, 8);
777 state->pdata->uart_putc(state->pdev, ' ');
778 state->pdata->uart_putc(state->pdev, 8);
779 }
780 } else if ((c == 13) || (c == 10)) {
781 if (c == '\r' || (c == '\n' && last_c != '\r')) {
782 state->pdata->uart_putc(state->pdev, '\r');
783 state->pdata->uart_putc(state->pdev, '\n');
784 }
785 if (state->debug_count) {
786 state->debug_buf[state->debug_count] = 0;
787 state->debug_count = 0;
788 signal_helper |=
789 debug_fiq_exec(state, state->debug_buf,
790 regs, svc_sp);
791 } else {
792 debug_prompt(state);
793 }
794 }
795 last_c = c;
796 }
797 debug_uart_flush(state);
798 if (state->pdata->fiq_ack)
799 state->pdata->fiq_ack(state->pdev, state->fiq);
800
801 /* poke sleep timer if necessary */
802 if (state->debug_enable && !state->no_sleep)
803 signal_helper = true;
804
805 atomic_set(&state->unhandled_fiq_count, 0);
806 state->in_fiq = false;
807
808 return signal_helper;
809}
810
811static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
812{
813 struct fiq_debugger_state *state =
814 container_of(h, struct fiq_debugger_state, handler);
815 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
816 bool need_irq;
817
818 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
819 if (need_irq)
820 debug_force_irq(state);
821}
822
823/*
824 * When not using FIQs, we only use this single interrupt as an entry point.
825 * This just effectively takes over the UART interrupt and does all the work
826 * in this context.
827 */
828static irqreturn_t debug_uart_irq(int irq, void *dev)
829{
830 struct fiq_debugger_state *state = dev;
831 bool not_done;
832
833 handle_wakeup(state);
834
835 /* handle the debugger irq in regular context */
836 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
837 get_irq_regs(),
838 current_thread_info());
839 if (not_done)
840 debug_handle_irq_context(state);
841
842 return IRQ_HANDLED;
843}
844
845/*
846 * If FIQs are used, not everything can happen in fiq context.
847 * FIQ handler does what it can and then signals this interrupt to finish the
848 * job in irq context.
849 */
850static irqreturn_t debug_signal_irq(int irq, void *dev)
851{
852 struct fiq_debugger_state *state = dev;
853
854 if (state->pdata->force_irq_ack)
855 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
856
857 debug_handle_irq_context(state);
858
859 return IRQ_HANDLED;
860}
861
862static void debug_resume(struct fiq_glue_handler *h)
863{
864 struct fiq_debugger_state *state =
865 container_of(h, struct fiq_debugger_state, handler);
866 if (state->pdata->uart_resume)
867 state->pdata->uart_resume(state->pdev);
868}
869
870#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
871struct tty_driver *debug_console_device(struct console *co, int *index)
872{
873 struct fiq_debugger_state *state;
874 state = container_of(co, struct fiq_debugger_state, console);
875 *index = 0;
876 return state->tty_driver;
877}
878
879static void debug_console_write(struct console *co,
880 const char *s, unsigned int count)
881{
882 struct fiq_debugger_state *state;
883
884 state = container_of(co, struct fiq_debugger_state, console);
885
886 if (!state->console_enable && !state->syslog_dumping)
887 return;
888
889 debug_uart_enable(state);
890 while (count--) {
891 if (*s == '\n')
892 state->pdata->uart_putc(state->pdev, '\r');
893 state->pdata->uart_putc(state->pdev, *s++);
894 }
895 debug_uart_flush(state);
896 debug_uart_disable(state);
897}
898
899static struct console fiq_debugger_console = {
900 .name = "ttyFIQ",
901 .device = debug_console_device,
902 .write = debug_console_write,
903 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
904};
905
906int fiq_tty_open(struct tty_struct *tty, struct file *filp)
907{
908 struct fiq_debugger_state *state = tty->driver->driver_state;
909 if (state->tty_open_count++)
910 return 0;
911
912 tty->driver_data = state;
913 state->tty = tty;
914 return 0;
915}
916
917void fiq_tty_close(struct tty_struct *tty, struct file *filp)
918{
919 struct fiq_debugger_state *state = tty->driver_data;
920 if (--state->tty_open_count)
921 return;
922 state->tty = NULL;
923}
924
925int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
926{
927 int i;
928 struct fiq_debugger_state *state = tty->driver_data;
929
930 if (!state->console_enable)
931 return count;
932
933 debug_uart_enable(state);
934 for (i = 0; i < count; i++)
935 state->pdata->uart_putc(state->pdev, *buf++);
936 debug_uart_disable(state);
937
938 return count;
939}
940
941int fiq_tty_write_room(struct tty_struct *tty)
942{
943 return 1024;
944}
945
946static const struct tty_operations fiq_tty_driver_ops = {
947 .write = fiq_tty_write,
948 .write_room = fiq_tty_write_room,
949 .open = fiq_tty_open,
950 .close = fiq_tty_close,
951};
952
953static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
954{
955 int ret = -EINVAL;
956
957 state->tty_driver = alloc_tty_driver(1);
958 if (!state->tty_driver) {
959 pr_err("Failed to allocate fiq debugger tty\n");
960 return -ENOMEM;
961 }
962
963 state->tty_driver->owner = THIS_MODULE;
964 state->tty_driver->driver_name = "fiq-debugger";
965 state->tty_driver->name = "ttyFIQ";
966 state->tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
967 state->tty_driver->subtype = SERIAL_TYPE_NORMAL;
968 state->tty_driver->init_termios = tty_std_termios;
969 state->tty_driver->init_termios.c_cflag =
970 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
971 state->tty_driver->init_termios.c_ispeed =
972 state->tty_driver->init_termios.c_ospeed = 115200;
973 state->tty_driver->flags = TTY_DRIVER_REAL_RAW;
974 tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
975 state->tty_driver->driver_state = state;
976
977 ret = tty_register_driver(state->tty_driver);
978 if (ret) {
979 pr_err("Failed to register fiq tty: %d\n", ret);
980 goto err;
981 }
982
983 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
984 if (!state->tty_rbuf) {
985 pr_err("Failed to allocate fiq debugger ringbuf\n");
986 ret = -ENOMEM;
987 goto err;
988 }
989
990 pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
991 return 0;
992
993err:
994 fiq_debugger_ringbuf_free(state->tty_rbuf);
995 state->tty_rbuf = NULL;
996 put_tty_driver(state->tty_driver);
997 return ret;
998}
999#endif
1000
1001static int fiq_debugger_dev_suspend(struct device *dev)
1002{
1003 struct platform_device *pdev = to_platform_device(dev);
1004 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1005
1006 if (state->pdata->uart_dev_suspend)
1007 return state->pdata->uart_dev_suspend(pdev);
1008 return 0;
1009}
1010
1011static int fiq_debugger_dev_resume(struct device *dev)
1012{
1013 struct platform_device *pdev = to_platform_device(dev);
1014 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1015
1016 if (state->pdata->uart_dev_resume)
1017 return state->pdata->uart_dev_resume(pdev);
1018 return 0;
1019}
1020
1021static int fiq_debugger_probe(struct platform_device *pdev)
1022{
1023 int ret;
1024 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1025 struct fiq_debugger_state *state;
1026 int fiq;
1027 int uart_irq;
1028
1029 if (!pdata->uart_getc || !pdata->uart_putc)
1030 return -EINVAL;
1031 if ((pdata->uart_enable && !pdata->uart_disable) ||
1032 (!pdata->uart_enable && pdata->uart_disable))
1033 return -EINVAL;
1034
1035 fiq = platform_get_irq_byname(pdev, "fiq");
1036 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1037
1038 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1039 * is required */
1040 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1041 return -EINVAL;
1042 if (fiq >= 0 && !pdata->fiq_enable)
1043 return -EINVAL;
1044
1045 state = kzalloc(sizeof(*state), GFP_KERNEL);
1046 setup_timer(&state->sleep_timer, sleep_timer_expired,
1047 (unsigned long)state);
1048 state->pdata = pdata;
1049 state->pdev = pdev;
1050 state->no_sleep = initial_no_sleep;
1051 state->debug_enable = initial_debug_enable;
1052 state->console_enable = initial_console_enable;
1053
1054 state->fiq = fiq;
1055 state->uart_irq = uart_irq;
1056 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1057 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1058
1059 platform_set_drvdata(pdev, state);
1060
1061 spin_lock_init(&state->sleep_timer_lock);
1062
1063 if (state->wakeup_irq < 0 && debug_have_fiq(state))
1064 state->no_sleep = true;
1065 state->ignore_next_wakeup_irq = !state->no_sleep;
1066
1067 wake_lock_init(&state->debugger_wake_lock,
1068 WAKE_LOCK_SUSPEND, "serial-debug");
1069
1070 state->clk = clk_get(&pdev->dev, NULL);
1071 if (IS_ERR(state->clk))
1072 state->clk = NULL;
1073
1074 /* do not call pdata->uart_enable here since uart_init may still
1075 * need to do some initialization before uart_enable can work.
1076 * So, only try to manage the clock during init.
1077 */
1078 if (state->clk)
1079 clk_enable(state->clk);
1080
1081 if (pdata->uart_init) {
1082 ret = pdata->uart_init(pdev);
1083 if (ret)
1084 goto err_uart_init;
1085 }
1086
1087 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1088 state->no_sleep ? "" : "twice ");
1089
1090 if (debug_have_fiq(state)) {
1091 state->handler.fiq = debug_fiq;
1092 state->handler.resume = debug_resume;
1093 ret = fiq_glue_register_handler(&state->handler);
1094 if (ret) {
1095 pr_err("%s: could not install fiq handler\n", __func__);
1096 goto err_register_fiq;
1097 }
1098
1099 pdata->fiq_enable(pdev, state->fiq, 1);
1100 } else {
1101 ret = request_irq(state->uart_irq, debug_uart_irq,
1102 IRQF_NO_SUSPEND, "debug", state);
1103 if (ret) {
1104 pr_err("%s: could not install irq handler\n", __func__);
1105 goto err_register_irq;
1106 }
1107
1108 /* for irq-only mode, we want this irq to wake us up, if it
1109 * can.
1110 */
1111 enable_irq_wake(state->uart_irq);
1112 }
1113
1114 if (state->clk)
1115 clk_disable(state->clk);
1116
1117 if (state->signal_irq >= 0) {
1118 ret = request_irq(state->signal_irq, debug_signal_irq,
1119 IRQF_TRIGGER_RISING, "debug-signal", state);
1120 if (ret)
1121 pr_err("serial_debugger: could not install signal_irq");
1122 }
1123
1124 if (state->wakeup_irq >= 0) {
1125 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1126 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1127 "debug-wakeup", state);
1128 if (ret) {
1129 pr_err("serial_debugger: "
1130 "could not install wakeup irq\n");
1131 state->wakeup_irq = -1;
1132 } else {
1133 ret = enable_irq_wake(state->wakeup_irq);
1134 if (ret) {
1135 pr_err("serial_debugger: "
1136 "could not enable wakeup\n");
1137 state->wakeup_irq_no_set_wake = true;
1138 }
1139 }
1140 }
1141 if (state->no_sleep)
1142 handle_wakeup(state);
1143
1144#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1145 state->console = fiq_debugger_console;
1146 register_console(&state->console);
1147 fiq_debugger_tty_init(state);
1148#endif
1149 return 0;
1150
1151err_register_irq:
1152err_register_fiq:
1153 if (pdata->uart_free)
1154 pdata->uart_free(pdev);
1155err_uart_init:
1156 if (state->clk)
1157 clk_disable(state->clk);
1158 if (state->clk)
1159 clk_put(state->clk);
1160 wake_lock_destroy(&state->debugger_wake_lock);
1161 platform_set_drvdata(pdev, NULL);
1162 kfree(state);
1163 return ret;
1164}
1165
1166static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1167 .suspend = fiq_debugger_dev_suspend,
1168 .resume = fiq_debugger_dev_resume,
1169};
1170
1171static struct platform_driver fiq_debugger_driver = {
1172 .probe = fiq_debugger_probe,
1173 .driver = {
1174 .name = "fiq_debugger",
1175 .pm = &fiq_debugger_dev_pm_ops,
1176 },
1177};
1178
1179static int __init fiq_debugger_init(void)
1180{
1181 return platform_driver_register(&fiq_debugger_driver);
1182}
1183
1184postcore_initcall(fiq_debugger_init);