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