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