ARM: fiq_debugger: add support for reboot commands
[firefly-linux-kernel-4.4.55.git] / arch / arm / common / fiq_debugger.c
1 /*
2  * arch/arm/common/fiq_debugger.c
3  *
4  * Serial Debugger Interface accessed through an FIQ interrupt.
5  *
6  * Copyright (C) 2008 Google, Inc.
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
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>
28 #include <linux/reboot.h>
29 #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
41 #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
51 struct 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
96 static bool initial_no_sleep = true;
97 #else
98 static bool initial_no_sleep;
99 #endif
100
101 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
102 static bool initial_debug_enable = true;
103 static bool initial_console_enable = true;
104 #else
105 static bool initial_debug_enable;
106 static bool initial_console_enable;
107 #endif
108
109 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
110 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
111 module_param_named(console_enable, initial_console_enable, bool, 0644);
112
113 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
114 static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
115 static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
116 #else
117 static 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 }
125 static 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
135 static bool inline debug_have_fiq(struct fiq_debugger_state *state)
136 {
137         return (state->fiq >= 0);
138 }
139
140 static 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
155 static 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
163 static 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
171 static 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
177 static 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
187 static void debug_prompt(struct fiq_debugger_state *state)
188 {
189         debug_puts(state, "debug> ");
190 }
191
192 int log_buf_copy(char *dest, int idx, int len);
193 static 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
217 static 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
231 static 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 */
246 static 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
264 static 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
283 struct 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
310 void __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
335 static 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
356 static void dump_irqs(struct fiq_debugger_state *state)
357 {
358         int n;
359
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         }
372 }
373
374 struct stacktrace_state {
375         struct fiq_debugger_state *state;
376         unsigned int depth;
377 };
378
379 static 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
396 struct frame_tail {
397         struct frame_tail *fp;
398         unsigned long sp;
399         unsigned long lr;
400 } __attribute__((packed));
401
402 static 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
428 void 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
465 static 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
489 static void begin_syslog_dump(struct fiq_debugger_state *state)
490 {
491         state->syslog_dumping = true;
492 }
493
494 static void end_syslog_dump(struct fiq_debugger_state *state)
495 {
496         state->syslog_dumping = false;
497 }
498 #else
499 extern int do_syslog(int type, char __user *bug, int count);
500 static void begin_syslog_dump(struct fiq_debugger_state *state)
501 {
502         do_syslog(5 /* clear */, NULL, 0);
503 }
504
505 static 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
522 static 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 */
530 static 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
540 static 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
561 static 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
572 static 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
579 static 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 (!strncmp(cmd, "reboot", 6)) {
596                 cmd += 6;
597                 while (*cmd == ' ')
598                         cmd++;
599                 if (*cmd) {
600                         char tmp_cmd[32];
601                         strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
602                         kernel_restart(tmp_cmd);
603                 } else {
604                         kernel_restart(NULL);
605                 }
606         } else if (!strcmp(cmd, "irqs")) {
607                 dump_irqs(state);
608         } else if (!strcmp(cmd, "kmsg")) {
609                 dump_kernel_log(state);
610         } else if (!strcmp(cmd, "version")) {
611                 debug_printf(state, "%s\n", linux_banner);
612         } else if (!strcmp(cmd, "sleep")) {
613                 state->no_sleep = false;
614                 debug_printf(state, "enabling sleep\n");
615         } else if (!strcmp(cmd, "nosleep")) {
616                 state->no_sleep = true;
617                 debug_printf(state, "disabling sleep\n");
618         } else if (!strcmp(cmd, "console")) {
619                 state->console_enable = true;
620                 debug_printf(state, "console mode\n");
621         } else if (!strcmp(cmd, "cpu")) {
622                 debug_printf(state, "cpu %d\n", state->current_cpu);
623         } else if (!strncmp(cmd, "cpu ", 4)) {
624                 unsigned long cpu = 0;
625                 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
626                         switch_cpu(state, cpu);
627                 else
628                         debug_printf(state, "invalid cpu\n");
629                 debug_printf(state, "cpu %d\n", state->current_cpu);
630         } else {
631                 if (state->debug_busy) {
632                         debug_printf(state,
633                                 "command processor busy. trying to abort.\n");
634                         state->debug_abort = -1;
635                 } else {
636                         strcpy(state->debug_cmd, cmd);
637                         state->debug_busy = 1;
638                 }
639
640                 return true;
641         }
642         if (!state->console_enable)
643                 debug_prompt(state);
644
645         return signal_helper;
646 }
647
648 static void sleep_timer_expired(unsigned long data)
649 {
650         struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
651         unsigned long flags;
652
653         spin_lock_irqsave(&state->sleep_timer_lock, flags);
654         if (state->uart_enabled && !state->no_sleep) {
655                 if (state->debug_enable && !state->console_enable) {
656                         state->debug_enable = false;
657                         debug_printf_nfiq(state, "suspending fiq debugger\n");
658                 }
659                 state->ignore_next_wakeup_irq = true;
660                 debug_uart_disable(state);
661                 state->uart_enabled = false;
662                 enable_wakeup_irq(state);
663         }
664         wake_unlock(&state->debugger_wake_lock);
665         spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
666 }
667
668 static void handle_wakeup(struct fiq_debugger_state *state)
669 {
670         unsigned long flags;
671
672         spin_lock_irqsave(&state->sleep_timer_lock, flags);
673         if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
674                 state->ignore_next_wakeup_irq = false;
675         } else if (!state->uart_enabled) {
676                 wake_lock(&state->debugger_wake_lock);
677                 debug_uart_enable(state);
678                 state->uart_enabled = true;
679                 disable_wakeup_irq(state);
680                 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
681         }
682         spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
683 }
684
685 static irqreturn_t wakeup_irq_handler(int irq, void *dev)
686 {
687         struct fiq_debugger_state *state = dev;
688
689         if (!state->no_sleep)
690                 debug_puts(state, "WAKEUP\n");
691         handle_wakeup(state);
692
693         return IRQ_HANDLED;
694 }
695
696
697 static void debug_handle_irq_context(struct fiq_debugger_state *state)
698 {
699         if (!state->no_sleep) {
700                 unsigned long flags;
701
702                 spin_lock_irqsave(&state->sleep_timer_lock, flags);
703                 wake_lock(&state->debugger_wake_lock);
704                 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
705                 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
706         }
707 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
708         if (state->tty) {
709                 int i;
710                 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
711                 for (i = 0; i < count; i++) {
712                         int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
713                         tty_insert_flip_char(state->tty, c, TTY_NORMAL);
714                         if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
715                                 pr_warn("fiq tty failed to consume byte\n");
716                 }
717                 tty_flip_buffer_push(state->tty);
718         }
719 #endif
720         if (state->debug_busy) {
721                 debug_irq_exec(state, state->debug_cmd);
722                 debug_prompt(state);
723                 state->debug_busy = 0;
724         }
725 }
726
727 static int debug_getc(struct fiq_debugger_state *state)
728 {
729         return state->pdata->uart_getc(state->pdev);
730 }
731
732 static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
733                         int this_cpu, void *regs, void *svc_sp)
734 {
735         int c;
736         static int last_c;
737         int count = 0;
738         bool signal_helper = false;
739
740         if (this_cpu != state->current_cpu) {
741                 if (state->in_fiq)
742                         return false;
743
744                 if (atomic_inc_return(&state->unhandled_fiq_count) !=
745                                         MAX_UNHANDLED_FIQ_COUNT)
746                         return false;
747
748                 debug_printf(state, "fiq_debugger: cpu %d not responding, "
749                         "reverting to cpu %d\n", state->current_cpu,
750                         this_cpu);
751
752                 atomic_set(&state->unhandled_fiq_count, 0);
753                 switch_cpu(state, this_cpu);
754                 return false;
755         }
756
757         state->in_fiq = true;
758
759         while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
760                 count++;
761                 if (!state->debug_enable) {
762                         if ((c == 13) || (c == 10)) {
763                                 state->debug_enable = true;
764                                 state->debug_count = 0;
765                                 debug_prompt(state);
766                         }
767                 } else if (c == FIQ_DEBUGGER_BREAK) {
768                         state->console_enable = false;
769                         debug_puts(state, "fiq debugger mode\n");
770                         state->debug_count = 0;
771                         debug_prompt(state);
772 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
773                 } else if (state->console_enable && state->tty_rbuf) {
774                         fiq_debugger_ringbuf_push(state->tty_rbuf, c);
775                         signal_helper = true;
776 #endif
777                 } else if ((c >= ' ') && (c < 127)) {
778                         if (state->debug_count < (DEBUG_MAX - 1)) {
779                                 state->debug_buf[state->debug_count++] = c;
780                                 state->pdata->uart_putc(state->pdev, c);
781                         }
782                 } else if ((c == 8) || (c == 127)) {
783                         if (state->debug_count > 0) {
784                                 state->debug_count--;
785                                 state->pdata->uart_putc(state->pdev, 8);
786                                 state->pdata->uart_putc(state->pdev, ' ');
787                                 state->pdata->uart_putc(state->pdev, 8);
788                         }
789                 } else if ((c == 13) || (c == 10)) {
790                         if (c == '\r' || (c == '\n' && last_c != '\r')) {
791                                 state->pdata->uart_putc(state->pdev, '\r');
792                                 state->pdata->uart_putc(state->pdev, '\n');
793                         }
794                         if (state->debug_count) {
795                                 state->debug_buf[state->debug_count] = 0;
796                                 state->debug_count = 0;
797                                 signal_helper |=
798                                         debug_fiq_exec(state, state->debug_buf,
799                                                        regs, svc_sp);
800                         } else {
801                                 debug_prompt(state);
802                         }
803                 }
804                 last_c = c;
805         }
806         debug_uart_flush(state);
807         if (state->pdata->fiq_ack)
808                 state->pdata->fiq_ack(state->pdev, state->fiq);
809
810         /* poke sleep timer if necessary */
811         if (state->debug_enable && !state->no_sleep)
812                 signal_helper = true;
813
814         atomic_set(&state->unhandled_fiq_count, 0);
815         state->in_fiq = false;
816
817         return signal_helper;
818 }
819
820 static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
821 {
822         struct fiq_debugger_state *state =
823                 container_of(h, struct fiq_debugger_state, handler);
824         unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
825         bool need_irq;
826
827         need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
828         if (need_irq)
829                 debug_force_irq(state);
830 }
831
832 /*
833  * When not using FIQs, we only use this single interrupt as an entry point.
834  * This just effectively takes over the UART interrupt and does all the work
835  * in this context.
836  */
837 static irqreturn_t debug_uart_irq(int irq, void *dev)
838 {
839         struct fiq_debugger_state *state = dev;
840         bool not_done;
841
842         handle_wakeup(state);
843
844         /* handle the debugger irq in regular context */
845         not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
846                                               get_irq_regs(),
847                                               current_thread_info());
848         if (not_done)
849                 debug_handle_irq_context(state);
850
851         return IRQ_HANDLED;
852 }
853
854 /*
855  * If FIQs are used, not everything can happen in fiq context.
856  * FIQ handler does what it can and then signals this interrupt to finish the
857  * job in irq context.
858  */
859 static irqreturn_t debug_signal_irq(int irq, void *dev)
860 {
861         struct fiq_debugger_state *state = dev;
862
863         if (state->pdata->force_irq_ack)
864                 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
865
866         debug_handle_irq_context(state);
867
868         return IRQ_HANDLED;
869 }
870
871 static void debug_resume(struct fiq_glue_handler *h)
872 {
873         struct fiq_debugger_state *state =
874                 container_of(h, struct fiq_debugger_state, handler);
875         if (state->pdata->uart_resume)
876                 state->pdata->uart_resume(state->pdev);
877 }
878
879 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
880 struct tty_driver *debug_console_device(struct console *co, int *index)
881 {
882         struct fiq_debugger_state *state;
883         state = container_of(co, struct fiq_debugger_state, console);
884         *index = 0;
885         return state->tty_driver;
886 }
887
888 static void debug_console_write(struct console *co,
889                                 const char *s, unsigned int count)
890 {
891         struct fiq_debugger_state *state;
892
893         state = container_of(co, struct fiq_debugger_state, console);
894
895         if (!state->console_enable && !state->syslog_dumping)
896                 return;
897
898         debug_uart_enable(state);
899         while (count--) {
900                 if (*s == '\n')
901                         state->pdata->uart_putc(state->pdev, '\r');
902                 state->pdata->uart_putc(state->pdev, *s++);
903         }
904         debug_uart_flush(state);
905         debug_uart_disable(state);
906 }
907
908 static struct console fiq_debugger_console = {
909         .name = "ttyFIQ",
910         .device = debug_console_device,
911         .write = debug_console_write,
912         .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
913 };
914
915 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
916 {
917         struct fiq_debugger_state *state = tty->driver->driver_state;
918         if (state->tty_open_count++)
919                 return 0;
920
921         tty->driver_data = state;
922         state->tty = tty;
923         return 0;
924 }
925
926 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
927 {
928         struct fiq_debugger_state *state = tty->driver_data;
929         if (--state->tty_open_count)
930                 return;
931         state->tty = NULL;
932 }
933
934 int  fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
935 {
936         int i;
937         struct fiq_debugger_state *state = tty->driver_data;
938
939         if (!state->console_enable)
940                 return count;
941
942         debug_uart_enable(state);
943         for (i = 0; i < count; i++)
944                 state->pdata->uart_putc(state->pdev, *buf++);
945         debug_uart_disable(state);
946
947         return count;
948 }
949
950 int  fiq_tty_write_room(struct tty_struct *tty)
951 {
952         return 1024;
953 }
954
955 static const struct tty_operations fiq_tty_driver_ops = {
956         .write = fiq_tty_write,
957         .write_room = fiq_tty_write_room,
958         .open = fiq_tty_open,
959         .close = fiq_tty_close,
960 };
961
962 static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
963 {
964         int ret = -EINVAL;
965
966         state->tty_driver = alloc_tty_driver(1);
967         if (!state->tty_driver) {
968                 pr_err("Failed to allocate fiq debugger tty\n");
969                 return -ENOMEM;
970         }
971
972         state->tty_driver->owner                = THIS_MODULE;
973         state->tty_driver->driver_name  = "fiq-debugger";
974         state->tty_driver->name         = "ttyFIQ";
975         state->tty_driver->type         = TTY_DRIVER_TYPE_SERIAL;
976         state->tty_driver->subtype      = SERIAL_TYPE_NORMAL;
977         state->tty_driver->init_termios = tty_std_termios;
978         state->tty_driver->init_termios.c_cflag =
979                                         B115200 | CS8 | CREAD | HUPCL | CLOCAL;
980         state->tty_driver->init_termios.c_ispeed =
981                 state->tty_driver->init_termios.c_ospeed = 115200;
982         state->tty_driver->flags                = TTY_DRIVER_REAL_RAW;
983         tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
984         state->tty_driver->driver_state = state;
985
986         ret = tty_register_driver(state->tty_driver);
987         if (ret) {
988                 pr_err("Failed to register fiq tty: %d\n", ret);
989                 goto err;
990         }
991
992         state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
993         if (!state->tty_rbuf) {
994                 pr_err("Failed to allocate fiq debugger ringbuf\n");
995                 ret = -ENOMEM;
996                 goto err;
997         }
998
999         pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
1000         return 0;
1001
1002 err:
1003         fiq_debugger_ringbuf_free(state->tty_rbuf);
1004         state->tty_rbuf = NULL;
1005         put_tty_driver(state->tty_driver);
1006         return ret;
1007 }
1008 #endif
1009
1010 static int fiq_debugger_dev_suspend(struct device *dev)
1011 {
1012         struct platform_device *pdev = to_platform_device(dev);
1013         struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1014
1015         if (state->pdata->uart_dev_suspend)
1016                 return state->pdata->uart_dev_suspend(pdev);
1017         return 0;
1018 }
1019
1020 static int fiq_debugger_dev_resume(struct device *dev)
1021 {
1022         struct platform_device *pdev = to_platform_device(dev);
1023         struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1024
1025         if (state->pdata->uart_dev_resume)
1026                 return state->pdata->uart_dev_resume(pdev);
1027         return 0;
1028 }
1029
1030 static int fiq_debugger_probe(struct platform_device *pdev)
1031 {
1032         int ret;
1033         struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1034         struct fiq_debugger_state *state;
1035         int fiq;
1036         int uart_irq;
1037
1038         if (!pdata->uart_getc || !pdata->uart_putc)
1039                 return -EINVAL;
1040         if ((pdata->uart_enable && !pdata->uart_disable) ||
1041             (!pdata->uart_enable && pdata->uart_disable))
1042                 return -EINVAL;
1043
1044         fiq = platform_get_irq_byname(pdev, "fiq");
1045         uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1046
1047         /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1048          * is required */
1049         if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1050                 return -EINVAL;
1051         if (fiq >= 0 && !pdata->fiq_enable)
1052                 return -EINVAL;
1053
1054         state = kzalloc(sizeof(*state), GFP_KERNEL);
1055         setup_timer(&state->sleep_timer, sleep_timer_expired,
1056                     (unsigned long)state);
1057         state->pdata = pdata;
1058         state->pdev = pdev;
1059         state->no_sleep = initial_no_sleep;
1060         state->debug_enable = initial_debug_enable;
1061         state->console_enable = initial_console_enable;
1062
1063         state->fiq = fiq;
1064         state->uart_irq = uart_irq;
1065         state->signal_irq = platform_get_irq_byname(pdev, "signal");
1066         state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1067
1068         platform_set_drvdata(pdev, state);
1069
1070         spin_lock_init(&state->sleep_timer_lock);
1071
1072         if (state->wakeup_irq < 0 && debug_have_fiq(state))
1073                 state->no_sleep = true;
1074         state->ignore_next_wakeup_irq = !state->no_sleep;
1075
1076         wake_lock_init(&state->debugger_wake_lock,
1077                         WAKE_LOCK_SUSPEND, "serial-debug");
1078
1079         state->clk = clk_get(&pdev->dev, NULL);
1080         if (IS_ERR(state->clk))
1081                 state->clk = NULL;
1082
1083         /* do not call pdata->uart_enable here since uart_init may still
1084          * need to do some initialization before uart_enable can work.
1085          * So, only try to manage the clock during init.
1086          */
1087         if (state->clk)
1088                 clk_enable(state->clk);
1089
1090         if (pdata->uart_init) {
1091                 ret = pdata->uart_init(pdev);
1092                 if (ret)
1093                         goto err_uart_init;
1094         }
1095
1096         debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1097                                 state->no_sleep ? "" : "twice ");
1098
1099         if (debug_have_fiq(state)) {
1100                 state->handler.fiq = debug_fiq;
1101                 state->handler.resume = debug_resume;
1102                 ret = fiq_glue_register_handler(&state->handler);
1103                 if (ret) {
1104                         pr_err("%s: could not install fiq handler\n", __func__);
1105                         goto err_register_fiq;
1106                 }
1107
1108                 pdata->fiq_enable(pdev, state->fiq, 1);
1109         } else {
1110                 ret = request_irq(state->uart_irq, debug_uart_irq,
1111                                   IRQF_NO_SUSPEND, "debug", state);
1112                 if (ret) {
1113                         pr_err("%s: could not install irq handler\n", __func__);
1114                         goto err_register_irq;
1115                 }
1116
1117                 /* for irq-only mode, we want this irq to wake us up, if it
1118                  * can.
1119                  */
1120                 enable_irq_wake(state->uart_irq);
1121         }
1122
1123         if (state->clk)
1124                 clk_disable(state->clk);
1125
1126         if (state->signal_irq >= 0) {
1127                 ret = request_irq(state->signal_irq, debug_signal_irq,
1128                           IRQF_TRIGGER_RISING, "debug-signal", state);
1129                 if (ret)
1130                         pr_err("serial_debugger: could not install signal_irq");
1131         }
1132
1133         if (state->wakeup_irq >= 0) {
1134                 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1135                                   IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1136                                   "debug-wakeup", state);
1137                 if (ret) {
1138                         pr_err("serial_debugger: "
1139                                 "could not install wakeup irq\n");
1140                         state->wakeup_irq = -1;
1141                 } else {
1142                         ret = enable_irq_wake(state->wakeup_irq);
1143                         if (ret) {
1144                                 pr_err("serial_debugger: "
1145                                         "could not enable wakeup\n");
1146                                 state->wakeup_irq_no_set_wake = true;
1147                         }
1148                 }
1149         }
1150         if (state->no_sleep)
1151                 handle_wakeup(state);
1152
1153 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1154         state->console = fiq_debugger_console;
1155         register_console(&state->console);
1156         fiq_debugger_tty_init(state);
1157 #endif
1158         return 0;
1159
1160 err_register_irq:
1161 err_register_fiq:
1162         if (pdata->uart_free)
1163                 pdata->uart_free(pdev);
1164 err_uart_init:
1165         if (state->clk)
1166                 clk_disable(state->clk);
1167         if (state->clk)
1168                 clk_put(state->clk);
1169         wake_lock_destroy(&state->debugger_wake_lock);
1170         platform_set_drvdata(pdev, NULL);
1171         kfree(state);
1172         return ret;
1173 }
1174
1175 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1176         .suspend        = fiq_debugger_dev_suspend,
1177         .resume         = fiq_debugger_dev_resume,
1178 };
1179
1180 static struct platform_driver fiq_debugger_driver = {
1181         .probe  = fiq_debugger_probe,
1182         .driver = {
1183                 .name   = "fiq_debugger",
1184                 .pm     = &fiq_debugger_dev_pm_ops,
1185         },
1186 };
1187
1188 static int __init fiq_debugger_init(void)
1189 {
1190         return platform_driver_register(&fiq_debugger_driver);
1191 }
1192
1193 postcore_initcall(fiq_debugger_init);