]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - arch/arm/common/fiq_debugger.c
FIQ: Implement WDT FIQ debugger
[sojka/nv-tegra/linux-3.10.git] / arch / arm / common / fiq_debugger.c
1 /*
2  * drivers/staging/android/fiq_debugger.c
3  *
4  * Serial Debugger Interface accessed through an FIQ interrupt.
5  *
6  * Copyright (C) 2008 Google, Inc.
7  * Copyright (C) 2010-2015 NVIDIA Corporation.  All rights reserved.
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  */
18
19 #include <stdarg.h>
20 #include <linux/module.h>
21 #include <linux/io.h>
22 #include <linux/console.h>
23 #include <linux/interrupt.h>
24 #include <linux/clk.h>
25 #include <linux/platform_device.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/kmsg_dump.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <linux/reboot.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/smp.h>
34 #include <linux/timer.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/wakelock.h>
38
39 #ifdef CONFIG_FIQ_GLUE
40 #include <asm/fiq_glue.h>
41 #endif
42
43 #include <linux/uaccess.h>
44
45 #include "fiq_debugger.h"
46 #include "fiq_debugger_priv.h"
47 #include "fiq_debugger_ringbuf.h"
48
49 #define DEBUG_MAX 64
50 #define MAX_UNHANDLED_FIQ_COUNT 1000000
51
52 #define MAX_FIQ_DEBUGGER_PORTS 4
53 struct fiq_debugger_state {
54 #ifdef CONFIG_FIQ_GLUE
55         struct fiq_glue_handler handler;
56 #endif
57         struct fiq_debugger_output output;
58
59         int fiq;
60         int uart_irq;
61         int signal_irq;
62         int wakeup_irq;
63         bool wakeup_irq_no_set_wake;
64         struct clk *clk;
65         struct fiq_debugger_pdata *pdata;
66         struct platform_device *pdev;
67
68         char debug_cmd[DEBUG_MAX];
69         int debug_busy;
70         int debug_abort;
71
72         char debug_buf[DEBUG_MAX];
73         int debug_count;
74
75         bool no_sleep;
76         bool debug_enable;
77         bool ignore_next_wakeup_irq;
78         struct timer_list sleep_timer;
79         spinlock_t sleep_timer_lock;
80         bool uart_enabled;
81         struct wake_lock debugger_wake_lock;
82         bool console_enable;
83         int current_cpu;
84         atomic_t unhandled_fiq_count;
85         bool in_fiq;
86
87         struct work_struct work;
88         spinlock_t work_lock;
89         char work_cmd[DEBUG_MAX];
90
91 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
92         spinlock_t console_lock;
93         struct console console;
94         struct tty_port tty_port;
95         struct fiq_debugger_ringbuf *tty_rbuf;
96         bool syslog_dumping;
97 #endif
98
99         struct console *ram_console;
100         unsigned int last_irqs[NR_IRQS];
101         unsigned int last_local_timer_irqs[NR_CPUS];
102 };
103
104 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
105 struct tty_driver *fiq_tty_driver;
106 #endif
107
108 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
109 static bool initial_no_sleep = true;
110 #else
111 static bool initial_no_sleep;
112 #endif
113
114 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
115 static bool initial_debug_enable = true;
116 static bool initial_console_enable = true;
117 #else
118 static bool initial_debug_enable;
119 static bool initial_console_enable;
120 #endif
121
122 static bool fiq_kgdb_enable;
123
124 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
125 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
126 module_param_named(console_enable, initial_console_enable, bool, 0644);
127 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
128
129 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
130 static inline
131 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
132 static inline
133 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
134 #else
135 static inline
136 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
137 {
138         if (state->wakeup_irq < 0)
139                 return;
140         enable_irq(state->wakeup_irq);
141         if (!state->wakeup_irq_no_set_wake)
142                 enable_irq_wake(state->wakeup_irq);
143 }
144 static inline
145 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
146 {
147         if (state->wakeup_irq < 0)
148                 return;
149         disable_irq_nosync(state->wakeup_irq);
150         if (!state->wakeup_irq_no_set_wake)
151                 disable_irq_wake(state->wakeup_irq);
152 }
153 #endif
154
155 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
156 {
157         return (state->fiq >= 0);
158 }
159
160 #ifdef CONFIG_FIQ_GLUE
161 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
162 {
163         unsigned int irq = state->signal_irq;
164
165         if (WARN_ON(!fiq_debugger_have_fiq(state)))
166                 return;
167         if (state->pdata->force_irq) {
168                 state->pdata->force_irq(state->pdev, irq);
169         } else {
170                 struct irq_chip *chip = irq_get_chip(irq);
171                 if (chip && chip->irq_retrigger)
172                         chip->irq_retrigger(irq_get_irq_data(irq));
173         }
174 }
175 #endif
176
177 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
178 {
179         if (state->clk)
180                 clk_enable(state->clk);
181         if (state->pdata->uart_enable)
182                 state->pdata->uart_enable(state->pdev);
183 }
184
185 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
186 {
187         if (state->pdata->uart_disable)
188                 state->pdata->uart_disable(state->pdev);
189         if (state->clk)
190                 clk_disable(state->clk);
191 }
192
193 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
194 {
195         if (state->pdata->uart_flush)
196                 state->pdata->uart_flush(state->pdev);
197 }
198
199 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
200 {
201         state->pdata->uart_putc(state->pdev, c);
202 }
203
204 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
205 {
206         unsigned c;
207         while ((c = *s++)) {
208                 if (c == '\n')
209                         fiq_debugger_putc(state, '\r');
210                 fiq_debugger_putc(state, c);
211         }
212 }
213
214 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
215 {
216         fiq_debugger_puts(state, "debug> ");
217 }
218
219 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
220 {
221         char buf[512];
222         size_t len;
223         struct kmsg_dumper dumper = { .active = true };
224
225
226         kmsg_dump_rewind_nolock(&dumper);
227         while (kmsg_dump_get_line_nolock(&dumper, true, buf,
228                                          sizeof(buf) - 1, &len)) {
229                 buf[len] = 0;
230                 fiq_debugger_puts(state, buf);
231         }
232 }
233
234 static void fiq_debugger_printf(struct fiq_debugger_output *output,
235                                const char *fmt, ...)
236 {
237         struct fiq_debugger_state *state;
238         char buf[256];
239         va_list ap;
240         int n;
241         state = container_of(output, struct fiq_debugger_state, output);
242         va_start(ap, fmt);
243         n = vsnprintf(buf, sizeof(buf), fmt, ap);
244         if (NULL != state->ram_console)
245                 state->ram_console->write(state->ram_console, buf, n);
246         va_end(ap);
247
248         fiq_debugger_puts(state, buf);
249 }
250
251 /* Safe outside fiq context */
252 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
253 {
254         struct fiq_debugger_state *state = cookie;
255         char buf[256];
256         va_list ap;
257         unsigned long irq_flags;
258
259         va_start(ap, fmt);
260         vsnprintf(buf, 128, fmt, ap);
261         va_end(ap);
262
263         local_irq_save(irq_flags);
264         fiq_debugger_puts(state, buf);
265         fiq_debugger_uart_flush(state);
266         local_irq_restore(irq_flags);
267         return state->debug_abort;
268 }
269
270 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
271 {
272         int n;
273         struct irq_desc *desc;
274
275         fiq_debugger_printf(&state->output,
276                         "irqnr       total  since-last   status  name\n");
277         for_each_irq_desc(n, desc) {
278                 struct irqaction *act = desc->action;
279                 if (!act && !kstat_irqs(n))
280                         continue;
281                 fiq_debugger_printf(&state->output, "%5d: %10u %11u %8x  %s\n", n,
282                         kstat_irqs(n),
283                         kstat_irqs(n) - state->last_irqs[n],
284                         desc->status_use_accessors,
285                         (act && act->name) ? act->name : "???");
286                 state->last_irqs[n] = kstat_irqs(n);
287         }
288 }
289
290 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
291 {
292         struct task_struct *g;
293         struct task_struct *p;
294         unsigned task_state;
295         static const char stat_nam[] = "RSDTtZX";
296
297         fiq_debugger_printf(&state->output, "pid   ppid  prio task            pc\n");
298         read_lock(&tasklist_lock);
299         do_each_thread(g, p) {
300                 task_state = p->state ? __ffs(p->state) + 1 : 0;
301                 fiq_debugger_printf(&state->output,
302                              "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
303                 fiq_debugger_printf(&state->output, "%-13.13s %c", p->comm,
304                              task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
305                 if (task_state == TASK_RUNNING)
306                         fiq_debugger_printf(&state->output, " running\n");
307                 else
308                         fiq_debugger_printf(&state->output, " %08lx\n",
309                                         thread_saved_pc(p));
310         } while_each_thread(g, p);
311         read_unlock(&tasklist_lock);
312 }
313
314 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
315 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
316 {
317         state->syslog_dumping = true;
318 }
319
320 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
321 {
322         state->syslog_dumping = false;
323 }
324 #else
325 extern int do_syslog(int type, char __user *bug, int count);
326 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
327 {
328         do_syslog(5 /* clear */, NULL, 0);
329 }
330
331 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
332 {
333         fiq_debugger_dump_kernel_log(state);
334 }
335 #endif
336
337 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
338 {
339         if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
340                 fiq_debugger_printf(&state->output, "sysrq-g blocked\n");
341                 return;
342         }
343         fiq_debugger_begin_syslog_dump(state);
344         handle_sysrq(rq);
345         fiq_debugger_end_syslog_dump(state);
346 }
347
348 #ifdef CONFIG_KGDB
349 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
350 {
351         if (!fiq_kgdb_enable) {
352                 fiq_debugger_printf(&state->output, "kgdb through fiq debugger not enabled\n");
353                 return;
354         }
355
356         fiq_debugger_printf(&state->output, "enabling console and triggering kgdb\n");
357         state->console_enable = true;
358         handle_sysrq('g');
359 }
360 #endif
361
362 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
363                 char *cmd)
364 {
365         unsigned long flags;
366
367         spin_lock_irqsave(&state->work_lock, flags);
368         if (state->work_cmd[0] != '\0') {
369                 fiq_debugger_printf(&state->output, "work command processor busy\n");
370                 spin_unlock_irqrestore(&state->work_lock, flags);
371                 return;
372         }
373
374         strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
375         spin_unlock_irqrestore(&state->work_lock, flags);
376
377         schedule_work(&state->work);
378 }
379
380 static void fiq_debugger_work(struct work_struct *work)
381 {
382         struct fiq_debugger_state *state;
383         char work_cmd[DEBUG_MAX];
384         char *cmd;
385         unsigned long flags;
386
387         state = container_of(work, struct fiq_debugger_state, work);
388
389         spin_lock_irqsave(&state->work_lock, flags);
390
391         strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
392         state->work_cmd[0] = '\0';
393
394         spin_unlock_irqrestore(&state->work_lock, flags);
395
396         cmd = work_cmd;
397         if (!strncmp(cmd, "reboot", 6)) {
398                 cmd += 6;
399                 while (*cmd == ' ')
400                         cmd++;
401                 if (cmd != '\0')
402                         kernel_restart(cmd);
403                 else
404                         kernel_restart(NULL);
405         } else {
406                 fiq_debugger_printf(&state->output, "unknown work command '%s'\n",
407                                 work_cmd);
408         }
409 }
410
411 /* This function CANNOT be called in FIQ context */
412 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
413 {
414         if (!strcmp(cmd, "ps"))
415                 fiq_debugger_do_ps(state);
416         if (!strcmp(cmd, "sysrq"))
417                 fiq_debugger_do_sysrq(state, 'h');
418         if (!strncmp(cmd, "sysrq ", 6))
419                 fiq_debugger_do_sysrq(state, cmd[6]);
420 #ifdef CONFIG_KGDB
421         if (!strcmp(cmd, "kgdb"))
422                 fiq_debugger_do_kgdb(state);
423 #endif
424         if (!strncmp(cmd, "reboot", 6))
425                 fiq_debugger_schedule_work(state, cmd);
426 }
427
428 static void fiq_debugger_help(struct fiq_debugger_state *state)
429 {
430         fiq_debugger_printf(&state->output,
431                                 "FIQ Debugger commands:\n"
432                                 " pc            PC status\n"
433                                 " regs          Register dump\n"
434                                 " allregs       Extended Register dump\n"
435                                 " bt            Stack trace\n"
436                                 " reboot [<c>]  Reboot with command <c>\n"
437                                 " reset [<c>]   Hard reset with command <c>\n"
438                                 " irqs          Interupt status\n"
439                                 " kmsg          Kernel log\n"
440                                 " version       Kernel version\n");
441         fiq_debugger_printf(&state->output,
442                                 " sleep         Allow sleep while in FIQ\n"
443                                 " nosleep       Disable sleep while in FIQ\n"
444                                 " console       Switch terminal to console\n"
445                                 " cpu           Current CPU\n"
446                                 " cpu <number>  Switch to CPU<number>\n");
447         fiq_debugger_printf(&state->output,
448                                 " ps            Process list\n"
449                                 " sysrq         sysrq options\n"
450                                 " sysrq <param> Execute sysrq with <param>\n");
451 #ifdef CONFIG_KGDB
452         fiq_debugger_printf(&state->output,
453                                 " kgdb          Enter kernel debugger\n");
454 #endif
455 }
456
457 static void fiq_debugger_take_affinity(void *info)
458 {
459         struct fiq_debugger_state *state = info;
460         struct cpumask cpumask;
461
462         cpumask_clear(&cpumask);
463         cpumask_set_cpu(get_cpu(), &cpumask);
464
465         irq_set_affinity(state->uart_irq, &cpumask);
466 }
467
468 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
469 {
470         if (!fiq_debugger_have_fiq(state))
471                 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
472                                 false);
473         state->current_cpu = cpu;
474 }
475
476 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
477                         const char *cmd, const struct pt_regs *regs,
478                         void *svc_sp)
479 {
480         bool signal_helper = false;
481
482         fiq_debugger_dump_allregs(&state->output, regs);
483
484         if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
485                 fiq_debugger_help(state);
486         } else if (!strcmp(cmd, "pc")) {
487                 fiq_debugger_dump_pc(&state->output, regs);
488         } else if (!strcmp(cmd, "regs")) {
489                 fiq_debugger_dump_regs(&state->output, regs);
490         } else if (!strcmp(cmd, "allregs")) {
491                 fiq_debugger_dump_allregs(&state->output, regs);
492         } else if (!strcmp(cmd, "bt")) {
493                 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
494         } else if (!strncmp(cmd, "reset", 5)) {
495                 cmd += 5;
496                 while (*cmd == ' ')
497                         cmd++;
498                 if (*cmd) {
499                         char tmp_cmd[32];
500                         strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
501                         machine_restart(tmp_cmd);
502                 } else {
503                         machine_restart(NULL);
504                 }
505         } else if (!strcmp(cmd, "irqs")) {
506                 fiq_debugger_dump_irqs(state);
507         } else if (!strcmp(cmd, "kmsg")) {
508                 fiq_debugger_dump_kernel_log(state);
509         } else if (!strcmp(cmd, "version")) {
510                 fiq_debugger_printf(&state->output, "%s\n", linux_banner);
511         } else if (!strcmp(cmd, "sleep")) {
512                 state->no_sleep = false;
513                 fiq_debugger_printf(&state->output, "enabling sleep\n");
514         } else if (!strcmp(cmd, "nosleep")) {
515                 state->no_sleep = true;
516                 fiq_debugger_printf(&state->output, "disabling sleep\n");
517         } else if (!strcmp(cmd, "console")) {
518                 fiq_debugger_printf(&state->output, "console mode\n");
519                 fiq_debugger_uart_flush(state);
520                 state->console_enable = true;
521         } else if (!strcmp(cmd, "cpu")) {
522                 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
523         } else if (!strncmp(cmd, "cpu ", 4)) {
524                 unsigned long cpu = 0;
525                 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
526                         fiq_debugger_switch_cpu(state, cpu);
527                 else
528                         fiq_debugger_printf(&state->output, "invalid cpu\n");
529                 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
530         } else {
531                 if (state->debug_busy) {
532                         fiq_debugger_printf(&state->output,
533                                 "command processor busy. trying to abort.\n");
534                         state->debug_abort = -1;
535                 } else {
536                         strcpy(state->debug_cmd, cmd);
537                         state->debug_busy = 1;
538                 }
539
540                 return true;
541         }
542         if (!state->console_enable)
543                 fiq_debugger_prompt(state);
544
545         return signal_helper;
546 }
547
548 static void fiq_debugger_sleep_timer_expired(unsigned long data)
549 {
550         struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
551         unsigned long flags;
552
553         spin_lock_irqsave(&state->sleep_timer_lock, flags);
554         if (state->uart_enabled && !state->no_sleep) {
555                 if (state->debug_enable && !state->console_enable) {
556                         state->debug_enable = false;
557                         fiq_debugger_printf_nfiq(state,
558                                         "suspending fiq debugger\n");
559                 }
560                 state->ignore_next_wakeup_irq = true;
561                 fiq_debugger_uart_disable(state);
562                 state->uart_enabled = false;
563                 fiq_debugger_enable_wakeup_irq(state);
564         }
565         wake_unlock(&state->debugger_wake_lock);
566         spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
567 }
568
569 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
570 {
571         unsigned long flags;
572
573         spin_lock_irqsave(&state->sleep_timer_lock, flags);
574         if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
575                 state->ignore_next_wakeup_irq = false;
576         } else if (!state->uart_enabled) {
577                 wake_lock(&state->debugger_wake_lock);
578                 fiq_debugger_uart_enable(state);
579                 state->uart_enabled = true;
580                 fiq_debugger_disable_wakeup_irq(state);
581                 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
582         }
583         spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
584 }
585
586 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
587 {
588         struct fiq_debugger_state *state = dev;
589
590         if (!state->no_sleep)
591                 fiq_debugger_puts(state, "WAKEUP\n");
592         fiq_debugger_handle_wakeup(state);
593
594         return IRQ_HANDLED;
595 }
596
597 static
598 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
599 {
600 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
601         if (state->tty_port.ops) {
602                 int i;
603                 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
604                 for (i = 0; i < count; i++) {
605                         int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
606                         tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
607                         if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
608                                 pr_warn("fiq tty failed to consume byte\n");
609                 }
610                 tty_flip_buffer_push(&state->tty_port);
611         }
612 #endif
613 }
614
615 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
616 {
617         if (!state->no_sleep) {
618                 unsigned long flags;
619
620                 spin_lock_irqsave(&state->sleep_timer_lock, flags);
621                 wake_lock(&state->debugger_wake_lock);
622                 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
623                 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
624         }
625         fiq_debugger_handle_console_irq_context(state);
626         if (state->debug_busy) {
627                 fiq_debugger_irq_exec(state, state->debug_cmd);
628                 if (!state->console_enable)
629                         fiq_debugger_prompt(state);
630                 state->debug_busy = 0;
631         }
632 }
633
634 static int fiq_debugger_getc(struct fiq_debugger_state *state)
635 {
636         return state->pdata->uart_getc(state->pdev);
637 }
638
639 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
640                         int this_cpu, const struct pt_regs *regs, void *svc_sp)
641 {
642         int c;
643         static int last_c;
644         int count = 0;
645         bool signal_helper = false;
646
647         if (this_cpu != state->current_cpu) {
648                 if (state->in_fiq)
649                         return false;
650
651                 if (atomic_inc_return(&state->unhandled_fiq_count) !=
652                                         MAX_UNHANDLED_FIQ_COUNT)
653                         return false;
654
655                 fiq_debugger_printf(&state->output,
656                         "fiq_debugger: cpu %d not responding, "
657                         "reverting to cpu %d\n", state->current_cpu,
658                         this_cpu);
659
660                 atomic_set(&state->unhandled_fiq_count, 0);
661                 fiq_debugger_switch_cpu(state, this_cpu);
662                 return false;
663         }
664
665         state->in_fiq = true;
666
667         while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
668                 count++;
669                 if (!state->debug_enable) {
670                         if ((c == 13) || (c == 10)) {
671                                 state->debug_enable = true;
672                                 state->debug_count = 0;
673                                 fiq_debugger_prompt(state);
674                         }
675                 } else if (c == FIQ_DEBUGGER_BREAK) {
676                         state->console_enable = false;
677                         fiq_debugger_puts(state, "fiq debugger mode\n");
678                         state->debug_count = 0;
679                         fiq_debugger_prompt(state);
680 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
681                 } else if (state->console_enable && state->tty_rbuf) {
682                         fiq_debugger_ringbuf_push(state->tty_rbuf, c);
683                         signal_helper = true;
684 #endif
685                 } else if ((c >= ' ') && (c < 127)) {
686                         if (state->debug_count < (DEBUG_MAX - 1)) {
687                                 state->debug_buf[state->debug_count++] = c;
688                                 fiq_debugger_putc(state, c);
689                         }
690                 } else if ((c == 8) || (c == 127)) {
691                         if (state->debug_count > 0) {
692                                 state->debug_count--;
693                                 fiq_debugger_putc(state, 8);
694                                 fiq_debugger_putc(state, ' ');
695                                 fiq_debugger_putc(state, 8);
696                         }
697                 } else if ((c == 13) || (c == 10)) {
698                         if (c == '\r' || (c == '\n' && last_c != '\r')) {
699                                 fiq_debugger_putc(state, '\r');
700                                 fiq_debugger_putc(state, '\n');
701                         }
702                         if (state->debug_count) {
703                                 state->debug_buf[state->debug_count] = 0;
704                                 state->debug_count = 0;
705                                 signal_helper |=
706                                         fiq_debugger_fiq_exec(state,
707                                                         state->debug_buf,
708                                                         regs, svc_sp);
709                         } else {
710                                 fiq_debugger_prompt(state);
711                         }
712                 }
713                 last_c = c;
714         }
715         if (!state->console_enable)
716                 fiq_debugger_uart_flush(state);
717         if (state->pdata->fiq_ack)
718                 state->pdata->fiq_ack(state->pdev, state->fiq);
719
720         fiq_debugger_dump_allregs(&state->output, regs);
721         fiq_debugger_dump_pc(&state->output, regs);
722         fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
723         /* poke sleep timer if necessary */
724         if (state->debug_enable && !state->no_sleep)
725                 signal_helper = true;
726
727         atomic_set(&state->unhandled_fiq_count, 0);
728         state->in_fiq = false;
729
730         return signal_helper;
731 }
732
733 #ifdef CONFIG_FIQ_GLUE
734 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
735                 const struct pt_regs *regs, void *svc_sp)
736 {
737         struct fiq_debugger_state *state =
738                 container_of(h, struct fiq_debugger_state, handler);
739
740         unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
741         bool need_irq;
742
743         need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
744                         svc_sp);
745         if (need_irq)
746                 fiq_debugger_force_irq(state);
747 }
748 #endif
749
750 /*
751  * When not using FIQs, we only use this single interrupt as an entry point.
752  * This just effectively takes over the UART interrupt and does all the work
753  * in this context.
754  */
755 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
756 {
757         struct fiq_debugger_state *state = dev;
758         bool not_done;
759
760         fiq_debugger_handle_wakeup(state);
761
762         /* handle the debugger irq in regular context */
763         not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
764                                               get_irq_regs(),
765                                               current_thread_info());
766         if (not_done)
767                 fiq_debugger_handle_irq_context(state);
768
769         return IRQ_HANDLED;
770 }
771
772 /*
773  * If FIQs are used, not everything can happen in fiq context.
774  * FIQ handler does what it can and then signals this interrupt to finish the
775  * job in irq context.
776  */
777 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
778 {
779         struct fiq_debugger_state *state = dev;
780
781         if (state->pdata->force_irq_ack)
782                 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
783
784         fiq_debugger_handle_irq_context(state);
785
786         return IRQ_HANDLED;
787 }
788
789 #ifdef CONFIG_FIQ_GLUE
790 static void fiq_debugger_resume(struct fiq_glue_handler *h)
791 {
792         struct fiq_debugger_state *state =
793                 container_of(h, struct fiq_debugger_state, handler);
794         if (state->pdata->uart_resume)
795                 state->pdata->uart_resume(state->pdev);
796 }
797 #endif
798
799 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
800 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
801 {
802         *index = co->index;
803         return fiq_tty_driver;
804 }
805
806 static void fiq_debugger_console_write(struct console *co,
807                                 const char *s, unsigned int count)
808 {
809         struct fiq_debugger_state *state;
810         unsigned long flags;
811
812         state = container_of(co, struct fiq_debugger_state, console);
813
814         if (!state->console_enable && !state->syslog_dumping)
815                 return;
816
817         fiq_debugger_uart_enable(state);
818         spin_lock_irqsave(&state->console_lock, flags);
819         while (count--) {
820                 if (*s == '\n')
821                         fiq_debugger_putc(state, '\r');
822                 fiq_debugger_putc(state, *s++);
823         }
824         fiq_debugger_uart_flush(state);
825         spin_unlock_irqrestore(&state->console_lock, flags);
826         fiq_debugger_uart_disable(state);
827 }
828
829 static struct console fiq_debugger_console = {
830         .name = "ttyFIQ",
831         .device = fiq_debugger_console_device,
832         .write = fiq_debugger_console_write,
833         .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
834 };
835
836 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
837 {
838         int line = tty->index;
839         struct fiq_debugger_state **states = tty->driver->driver_state;
840         struct fiq_debugger_state *state = states[line];
841
842         return tty_port_open(&state->tty_port, tty, filp);
843 }
844
845 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
846 {
847         tty_port_close(tty->port, tty, filp);
848 }
849
850 int  fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
851 {
852         int i;
853         int line = tty->index;
854         struct fiq_debugger_state **states = tty->driver->driver_state;
855         struct fiq_debugger_state *state = states[line];
856
857         if (!state->console_enable)
858                 return count;
859
860         fiq_debugger_uart_enable(state);
861         spin_lock_irq(&state->console_lock);
862         for (i = 0; i < count; i++)
863                 fiq_debugger_putc(state, *buf++);
864         spin_unlock_irq(&state->console_lock);
865         fiq_debugger_uart_disable(state);
866
867         return count;
868 }
869
870 int  fiq_tty_write_room(struct tty_struct *tty)
871 {
872         return 16;
873 }
874
875 #ifdef CONFIG_CONSOLE_POLL
876 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
877 {
878         return 0;
879 }
880
881 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
882 {
883         struct fiq_debugger_state **states = driver->driver_state;
884         struct fiq_debugger_state *state = states[line];
885         int c = NO_POLL_CHAR;
886
887         fiq_debugger_uart_enable(state);
888         if (fiq_debugger_have_fiq(state)) {
889                 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
890                 if (count > 0) {
891                         c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
892                         fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
893                 }
894         } else {
895                 c = fiq_debugger_getc(state);
896                 if (c == FIQ_DEBUGGER_NO_CHAR)
897                         c = NO_POLL_CHAR;
898         }
899         fiq_debugger_uart_disable(state);
900
901         return c;
902 }
903
904 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
905 {
906         struct fiq_debugger_state **states = driver->driver_state;
907         struct fiq_debugger_state *state = states[line];
908         fiq_debugger_uart_enable(state);
909         fiq_debugger_putc(state, ch);
910         fiq_debugger_uart_disable(state);
911 }
912 #endif
913
914 static const struct tty_port_operations fiq_tty_port_ops;
915
916 static const struct tty_operations fiq_tty_driver_ops = {
917         .write = fiq_tty_write,
918         .write_room = fiq_tty_write_room,
919         .open = fiq_tty_open,
920         .close = fiq_tty_close,
921 #ifdef CONFIG_CONSOLE_POLL
922         .poll_init = fiq_tty_poll_init,
923         .poll_get_char = fiq_tty_poll_get_char,
924         .poll_put_char = fiq_tty_poll_put_char,
925 #endif
926 };
927
928 static int fiq_debugger_tty_init(void)
929 {
930         int ret;
931         struct fiq_debugger_state **states = NULL;
932
933         states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
934         if (!states) {
935                 pr_err("Failed to allocate fiq debugger state structres\n");
936                 return -ENOMEM;
937         }
938
939         fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
940         if (!fiq_tty_driver) {
941                 pr_err("Failed to allocate fiq debugger tty\n");
942                 ret = -ENOMEM;
943                 goto err_free_state;
944         }
945
946         fiq_tty_driver->owner           = THIS_MODULE;
947         fiq_tty_driver->driver_name     = "fiq-debugger";
948         fiq_tty_driver->name            = "ttyFIQ";
949         fiq_tty_driver->type            = TTY_DRIVER_TYPE_SERIAL;
950         fiq_tty_driver->subtype         = SERIAL_TYPE_NORMAL;
951         fiq_tty_driver->init_termios    = tty_std_termios;
952         fiq_tty_driver->flags           = TTY_DRIVER_REAL_RAW |
953                                           TTY_DRIVER_DYNAMIC_DEV;
954         fiq_tty_driver->driver_state    = states;
955
956         fiq_tty_driver->init_termios.c_cflag =
957                                         B115200 | CS8 | CREAD | HUPCL | CLOCAL;
958         fiq_tty_driver->init_termios.c_ispeed = 115200;
959         fiq_tty_driver->init_termios.c_ospeed = 115200;
960
961         tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
962
963         ret = tty_register_driver(fiq_tty_driver);
964         if (ret) {
965                 pr_err("Failed to register fiq tty: %d\n", ret);
966                 goto err_free_tty;
967         }
968
969         pr_info("Registered FIQ tty driver\n");
970         return 0;
971
972 err_free_tty:
973         put_tty_driver(fiq_tty_driver);
974         fiq_tty_driver = NULL;
975 err_free_state:
976         kfree(states);
977         return ret;
978 }
979
980 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
981 {
982         int ret;
983         struct device *tty_dev;
984         struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
985
986         states[state->pdev->id] = state;
987
988         state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
989         if (!state->tty_rbuf) {
990                 pr_err("Failed to allocate fiq debugger ringbuf\n");
991                 ret = -ENOMEM;
992                 goto err;
993         }
994
995         tty_port_init(&state->tty_port);
996         state->tty_port.ops = &fiq_tty_port_ops;
997
998         tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
999                                            state->pdev->id, &state->pdev->dev);
1000         if (IS_ERR(tty_dev)) {
1001                 pr_err("Failed to register fiq debugger tty device\n");
1002                 ret = PTR_ERR(tty_dev);
1003                 goto err;
1004         }
1005
1006         device_set_wakeup_capable(tty_dev, 1);
1007
1008         pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1009
1010         return 0;
1011
1012 err:
1013         fiq_debugger_ringbuf_free(state->tty_rbuf);
1014         state->tty_rbuf = NULL;
1015         return ret;
1016 }
1017 #endif
1018
1019 static int fiq_debugger_dev_suspend(struct device *dev)
1020 {
1021         struct platform_device *pdev = to_platform_device(dev);
1022         struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1023
1024         if (state->pdata->uart_dev_suspend)
1025                 return state->pdata->uart_dev_suspend(pdev);
1026         return 0;
1027 }
1028
1029 static int fiq_debugger_dev_resume(struct device *dev)
1030 {
1031         struct platform_device *pdev = to_platform_device(dev);
1032         struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1033
1034         if (state->pdata->uart_dev_resume)
1035                 return state->pdata->uart_dev_resume(pdev);
1036         return 0;
1037 }
1038
1039 static int fiq_debugger_probe(struct platform_device *pdev)
1040 {
1041         int ret;
1042         struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1043         struct fiq_debugger_state *state;
1044         int fiq;
1045         int uart_irq;
1046         struct console *console;
1047         if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1048                 return -EINVAL;
1049
1050         if (!pdata->uart_getc || !pdata->uart_putc)
1051                 return -EINVAL;
1052         if ((pdata->uart_enable && !pdata->uart_disable) ||
1053             (!pdata->uart_enable && pdata->uart_disable))
1054                 return -EINVAL;
1055
1056         fiq = platform_get_irq_byname(pdev, "fiq");
1057         uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1058
1059         /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1060          * is required */
1061         if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1062                 return -EINVAL;
1063         if (fiq >= 0 && !pdata->fiq_enable)
1064                 return -EINVAL;
1065
1066         state = kzalloc(sizeof(*state), GFP_KERNEL);
1067         state->output.printf = fiq_debugger_printf;
1068         setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1069                     (unsigned long)state);
1070         state->pdata = pdata;
1071         state->pdev = pdev;
1072         state->no_sleep = initial_no_sleep;
1073         state->debug_enable = initial_debug_enable;
1074         state->console_enable = initial_console_enable;
1075
1076         state->fiq = fiq;
1077         state->uart_irq = uart_irq;
1078         state->signal_irq = platform_get_irq_byname(pdev, "signal");
1079         state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1080
1081         INIT_WORK(&state->work, fiq_debugger_work);
1082         spin_lock_init(&state->work_lock);
1083
1084         platform_set_drvdata(pdev, state);
1085
1086         spin_lock_init(&state->sleep_timer_lock);
1087
1088         if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1089                 state->no_sleep = true;
1090         state->ignore_next_wakeup_irq = !state->no_sleep;
1091
1092         wake_lock_init(&state->debugger_wake_lock,
1093                         WAKE_LOCK_SUSPEND, "serial-debug");
1094
1095         state->clk = clk_get(&pdev->dev, NULL);
1096         if (IS_ERR(state->clk))
1097                 state->clk = NULL;
1098
1099         /* do not call pdata->uart_enable here since uart_init may still
1100          * need to do some initialization before uart_enable can work.
1101          * So, only try to manage the clock during init.
1102          */
1103         if (state->clk)
1104                 clk_enable(state->clk);
1105
1106         if (pdata->uart_init) {
1107                 ret = pdata->uart_init(pdev);
1108                 if (ret)
1109                         goto err_uart_init;
1110         }
1111
1112         fiq_debugger_printf_nfiq(state,
1113                                 "<hit enter %sto activate fiq debugger>\n",
1114                                 state->no_sleep ? "" : "twice ");
1115
1116 #ifdef CONFIG_FIQ_GLUE
1117         if (fiq_debugger_have_fiq(state)) {
1118                 state->handler.fiq = fiq_debugger_fiq;
1119                 state->handler.resume = fiq_debugger_resume;
1120                 ret = fiq_glue_register_handler(&state->handler);
1121                 if (ret) {
1122                         pr_err("%s: could not install fiq handler\n", __func__);
1123                         goto err_register_irq;
1124                 }
1125
1126                 pdata->fiq_enable(pdev, state->fiq, 1);
1127         } else
1128 #endif
1129         {
1130                 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1131                                   IRQF_NO_SUSPEND, "debug", state);
1132                 if (ret) {
1133                         pr_err("%s: could not install irq handler\n", __func__);
1134                         goto err_register_irq;
1135                 }
1136
1137                 /* for irq-only mode, we want this irq to wake us up, if it
1138                  * can.
1139                  */
1140                 enable_irq_wake(state->uart_irq);
1141         }
1142
1143         if (state->clk)
1144                 clk_disable(state->clk);
1145
1146         if (state->signal_irq >= 0) {
1147                 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1148                           IRQF_TRIGGER_RISING, "debug-signal", state);
1149                 if (ret)
1150                         pr_err("serial_debugger: could not install signal_irq");
1151         }
1152
1153         if (state->wakeup_irq >= 0) {
1154                 ret = request_irq(state->wakeup_irq,
1155                                   fiq_debugger_wakeup_irq_handler,
1156                                   IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1157                                   "debug-wakeup", state);
1158                 if (ret) {
1159                         pr_err("serial_debugger: "
1160                                 "could not install wakeup irq\n");
1161                         state->wakeup_irq = -1;
1162                 } else {
1163                         ret = enable_irq_wake(state->wakeup_irq);
1164                         if (ret) {
1165                                 pr_err("serial_debugger: "
1166                                         "could not enable wakeup\n");
1167                                 state->wakeup_irq_no_set_wake = true;
1168                         }
1169                 }
1170         }
1171         if (state->no_sleep)
1172                 fiq_debugger_handle_wakeup(state);
1173
1174 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1175         spin_lock_init(&state->console_lock);
1176         state->console = fiq_debugger_console;
1177         state->console.index = pdev->id;
1178         if (!console_set_on_cmdline)
1179                 add_preferred_console(state->console.name,
1180                         state->console.index, NULL);
1181         register_console(&state->console);
1182         fiq_debugger_tty_init_one(state);
1183 #endif
1184         for_each_console(console)
1185                 if (0 == strcmp(console->name, "pstore")) {
1186                         state->ram_console = console;
1187                         break;
1188                 }
1189
1190         return 0;
1191
1192 err_register_irq:
1193         if (pdata->uart_free)
1194                 pdata->uart_free(pdev);
1195 err_uart_init:
1196         if (state->clk)
1197                 clk_disable(state->clk);
1198         if (state->clk)
1199                 clk_put(state->clk);
1200         wake_lock_destroy(&state->debugger_wake_lock);
1201         platform_set_drvdata(pdev, NULL);
1202         kfree(state);
1203         return ret;
1204 }
1205
1206 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1207         .suspend        = fiq_debugger_dev_suspend,
1208         .resume         = fiq_debugger_dev_resume,
1209 };
1210
1211 static struct platform_driver fiq_debugger_driver = {
1212         .probe  = fiq_debugger_probe,
1213         .driver = {
1214                 .name   = "fiq_debugger",
1215                 .pm     = &fiq_debugger_dev_pm_ops,
1216         },
1217 };
1218
1219 static int __init fiq_debugger_init(void)
1220 {
1221 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1222         fiq_debugger_tty_init();
1223 #endif
1224         return platform_driver_register(&fiq_debugger_driver);
1225 }
1226
1227 postcore_initcall(fiq_debugger_init);