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