fiq_debugger.c revision 63351df85f7ece011618955054c1b0d15b393691
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 53struct 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 104struct tty_driver *fiq_tty_driver; 105#endif 106 107#ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP 108static bool initial_no_sleep = true; 109#else 110static bool initial_no_sleep; 111#endif 112 113#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE 114static bool initial_debug_enable = true; 115static bool initial_console_enable = true; 116#else 117static bool initial_debug_enable; 118static bool initial_console_enable; 119#endif 120 121static bool fiq_kgdb_enable; 122 123module_param_named(no_sleep, initial_no_sleep, bool, 0644); 124module_param_named(debug_enable, initial_debug_enable, bool, 0644); 125module_param_named(console_enable, initial_console_enable, bool, 0644); 126module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644); 127 128#ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON 129static inline 130void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {} 131static inline 132void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {} 133#else 134static inline 135void 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} 143static inline 144void 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 154static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state) 155{ 156 return (state->fiq >= 0); 157} 158 159#ifdef CONFIG_FIQ_GLUE 160static 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 176static 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 184static 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 192static 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 198static void fiq_debugger_putc(struct fiq_debugger_state *state, char c) 199{ 200 state->pdata->uart_putc(state->pdev, c); 201} 202 203static 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 213static void fiq_debugger_prompt(struct fiq_debugger_state *state) 214{ 215 fiq_debugger_puts(state, "debug> "); 216} 217 218static 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 233static 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 */ 249static 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 267static 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 287static 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 312static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state) 313{ 314 state->syslog_dumping = true; 315} 316 317static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state) 318{ 319 state->syslog_dumping = false; 320} 321#else 322extern int do_syslog(int type, char __user *bug, int count); 323static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state) 324{ 325 do_syslog(5 /* clear */, NULL, 0); 326} 327 328static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state) 329{ 330 fiq_debugger_dump_kernel_log(state); 331} 332#endif 333 334static 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 346static 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 359static 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 377static 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 */ 409static 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 425static 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 " reboot [<c>] Reboot with command <c>\n" 434 " reset [<c>] Hard reset with command <c>\n" 435 " irqs Interupt status\n" 436 " kmsg Kernel log\n" 437 " version Kernel version\n"); 438 fiq_debugger_printf(&state->output, 439 " sleep Allow sleep while in FIQ\n" 440 " nosleep Disable sleep while in FIQ\n" 441 " console Switch terminal to console\n" 442 " cpu Current CPU\n" 443 " cpu <number> Switch to CPU<number>\n"); 444 fiq_debugger_printf(&state->output, 445 " ps Process list\n" 446 " sysrq sysrq options\n" 447 " sysrq <param> Execute sysrq with <param>\n"); 448#ifdef CONFIG_KGDB 449 fiq_debugger_printf(&state->output, 450 " kgdb Enter kernel debugger\n"); 451#endif 452} 453 454static void fiq_debugger_take_affinity(void *info) 455{ 456 struct fiq_debugger_state *state = info; 457 struct cpumask cpumask; 458 459 cpumask_clear(&cpumask); 460 cpumask_set_cpu(get_cpu(), &cpumask); 461 462 irq_set_affinity(state->uart_irq, &cpumask); 463} 464 465static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu) 466{ 467 if (!fiq_debugger_have_fiq(state)) 468 smp_call_function_single(cpu, fiq_debugger_take_affinity, state, 469 false); 470 state->current_cpu = cpu; 471} 472 473static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state, 474 const char *cmd, const struct pt_regs *regs, 475 void *svc_sp) 476{ 477 bool signal_helper = false; 478 479 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) { 480 fiq_debugger_help(state); 481 } else if (!strcmp(cmd, "pc")) { 482 fiq_debugger_dump_pc(&state->output, regs); 483 } else if (!strcmp(cmd, "regs")) { 484 fiq_debugger_dump_regs(&state->output, regs); 485 } else if (!strcmp(cmd, "allregs")) { 486 fiq_debugger_dump_allregs(&state->output, regs); 487 } else if (!strcmp(cmd, "bt")) { 488 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp); 489 } else if (!strncmp(cmd, "reset", 5)) { 490 cmd += 5; 491 while (*cmd == ' ') 492 cmd++; 493 if (*cmd) { 494 char tmp_cmd[32]; 495 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd)); 496 machine_restart(tmp_cmd); 497 } else { 498 machine_restart(NULL); 499 } 500 } else if (!strcmp(cmd, "irqs")) { 501 fiq_debugger_dump_irqs(state); 502 } else if (!strcmp(cmd, "kmsg")) { 503 fiq_debugger_dump_kernel_log(state); 504 } else if (!strcmp(cmd, "version")) { 505 fiq_debugger_printf(&state->output, "%s\n", linux_banner); 506 } else if (!strcmp(cmd, "sleep")) { 507 state->no_sleep = false; 508 fiq_debugger_printf(&state->output, "enabling sleep\n"); 509 } else if (!strcmp(cmd, "nosleep")) { 510 state->no_sleep = true; 511 fiq_debugger_printf(&state->output, "disabling sleep\n"); 512 } else if (!strcmp(cmd, "console")) { 513 fiq_debugger_printf(&state->output, "console mode\n"); 514 fiq_debugger_uart_flush(state); 515 state->console_enable = true; 516 } else if (!strcmp(cmd, "cpu")) { 517 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu); 518 } else if (!strncmp(cmd, "cpu ", 4)) { 519 unsigned long cpu = 0; 520 if (kstrtoul(cmd + 4, 10, &cpu) == 0) 521 fiq_debugger_switch_cpu(state, cpu); 522 else 523 fiq_debugger_printf(&state->output, "invalid cpu\n"); 524 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu); 525 } else { 526 if (state->debug_busy) { 527 fiq_debugger_printf(&state->output, 528 "command processor busy. trying to abort.\n"); 529 state->debug_abort = -1; 530 } else { 531 strcpy(state->debug_cmd, cmd); 532 state->debug_busy = 1; 533 } 534 535 return true; 536 } 537 if (!state->console_enable) 538 fiq_debugger_prompt(state); 539 540 return signal_helper; 541} 542 543static void fiq_debugger_sleep_timer_expired(unsigned long data) 544{ 545 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data; 546 unsigned long flags; 547 548 spin_lock_irqsave(&state->sleep_timer_lock, flags); 549 if (state->uart_enabled && !state->no_sleep) { 550 if (state->debug_enable && !state->console_enable) { 551 state->debug_enable = false; 552 fiq_debugger_printf_nfiq(state, 553 "suspending fiq debugger\n"); 554 } 555 state->ignore_next_wakeup_irq = true; 556 fiq_debugger_uart_disable(state); 557 state->uart_enabled = false; 558 fiq_debugger_enable_wakeup_irq(state); 559 } 560 wake_unlock(&state->debugger_wake_lock); 561 spin_unlock_irqrestore(&state->sleep_timer_lock, flags); 562} 563 564static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state) 565{ 566 unsigned long flags; 567 568 spin_lock_irqsave(&state->sleep_timer_lock, flags); 569 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) { 570 state->ignore_next_wakeup_irq = false; 571 } else if (!state->uart_enabled) { 572 wake_lock(&state->debugger_wake_lock); 573 fiq_debugger_uart_enable(state); 574 state->uart_enabled = true; 575 fiq_debugger_disable_wakeup_irq(state); 576 mod_timer(&state->sleep_timer, jiffies + HZ / 2); 577 } 578 spin_unlock_irqrestore(&state->sleep_timer_lock, flags); 579} 580 581static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev) 582{ 583 struct fiq_debugger_state *state = dev; 584 585 if (!state->no_sleep) 586 fiq_debugger_puts(state, "WAKEUP\n"); 587 fiq_debugger_handle_wakeup(state); 588 589 return IRQ_HANDLED; 590} 591 592static 593void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state) 594{ 595#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE) 596 if (state->tty_port.ops) { 597 int i; 598 int count = fiq_debugger_ringbuf_level(state->tty_rbuf); 599 for (i = 0; i < count; i++) { 600 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0); 601 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL); 602 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1)) 603 pr_warn("fiq tty failed to consume byte\n"); 604 } 605 tty_flip_buffer_push(&state->tty_port); 606 } 607#endif 608} 609 610static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state) 611{ 612 if (!state->no_sleep) { 613 unsigned long flags; 614 615 spin_lock_irqsave(&state->sleep_timer_lock, flags); 616 wake_lock(&state->debugger_wake_lock); 617 mod_timer(&state->sleep_timer, jiffies + HZ * 5); 618 spin_unlock_irqrestore(&state->sleep_timer_lock, flags); 619 } 620 fiq_debugger_handle_console_irq_context(state); 621 if (state->debug_busy) { 622 fiq_debugger_irq_exec(state, state->debug_cmd); 623 if (!state->console_enable) 624 fiq_debugger_prompt(state); 625 state->debug_busy = 0; 626 } 627} 628 629static int fiq_debugger_getc(struct fiq_debugger_state *state) 630{ 631 return state->pdata->uart_getc(state->pdev); 632} 633 634static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state, 635 int this_cpu, const struct pt_regs *regs, void *svc_sp) 636{ 637 int c; 638 static int last_c; 639 int count = 0; 640 bool signal_helper = false; 641 642 if (this_cpu != state->current_cpu) { 643 if (state->in_fiq) 644 return false; 645 646 if (atomic_inc_return(&state->unhandled_fiq_count) != 647 MAX_UNHANDLED_FIQ_COUNT) 648 return false; 649 650 fiq_debugger_printf(&state->output, 651 "fiq_debugger: cpu %d not responding, " 652 "reverting to cpu %d\n", state->current_cpu, 653 this_cpu); 654 655 atomic_set(&state->unhandled_fiq_count, 0); 656 fiq_debugger_switch_cpu(state, this_cpu); 657 return false; 658 } 659 660 state->in_fiq = true; 661 662 while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) { 663 count++; 664 if (!state->debug_enable) { 665 if ((c == 13) || (c == 10)) { 666 state->debug_enable = true; 667 state->debug_count = 0; 668 fiq_debugger_prompt(state); 669 } 670 } else if (c == FIQ_DEBUGGER_BREAK) { 671 state->console_enable = false; 672 fiq_debugger_puts(state, "fiq debugger mode\n"); 673 state->debug_count = 0; 674 fiq_debugger_prompt(state); 675#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE 676 } else if (state->console_enable && state->tty_rbuf) { 677 fiq_debugger_ringbuf_push(state->tty_rbuf, c); 678 signal_helper = true; 679#endif 680 } else if ((c >= ' ') && (c < 127)) { 681 if (state->debug_count < (DEBUG_MAX - 1)) { 682 state->debug_buf[state->debug_count++] = c; 683 fiq_debugger_putc(state, c); 684 } 685 } else if ((c == 8) || (c == 127)) { 686 if (state->debug_count > 0) { 687 state->debug_count--; 688 fiq_debugger_putc(state, 8); 689 fiq_debugger_putc(state, ' '); 690 fiq_debugger_putc(state, 8); 691 } 692 } else if ((c == 13) || (c == 10)) { 693 if (c == '\r' || (c == '\n' && last_c != '\r')) { 694 fiq_debugger_putc(state, '\r'); 695 fiq_debugger_putc(state, '\n'); 696 } 697 if (state->debug_count) { 698 state->debug_buf[state->debug_count] = 0; 699 state->debug_count = 0; 700 signal_helper |= 701 fiq_debugger_fiq_exec(state, 702 state->debug_buf, 703 regs, svc_sp); 704 } else { 705 fiq_debugger_prompt(state); 706 } 707 } 708 last_c = c; 709 } 710 if (!state->console_enable) 711 fiq_debugger_uart_flush(state); 712 if (state->pdata->fiq_ack) 713 state->pdata->fiq_ack(state->pdev, state->fiq); 714 715 /* poke sleep timer if necessary */ 716 if (state->debug_enable && !state->no_sleep) 717 signal_helper = true; 718 719 atomic_set(&state->unhandled_fiq_count, 0); 720 state->in_fiq = false; 721 722 return signal_helper; 723} 724 725#ifdef CONFIG_FIQ_GLUE 726static void fiq_debugger_fiq(struct fiq_glue_handler *h, 727 const struct pt_regs *regs, void *svc_sp) 728{ 729 struct fiq_debugger_state *state = 730 container_of(h, struct fiq_debugger_state, handler); 731 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu; 732 bool need_irq; 733 734 need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs, 735 svc_sp); 736 if (need_irq) 737 fiq_debugger_force_irq(state); 738} 739#endif 740 741/* 742 * When not using FIQs, we only use this single interrupt as an entry point. 743 * This just effectively takes over the UART interrupt and does all the work 744 * in this context. 745 */ 746static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev) 747{ 748 struct fiq_debugger_state *state = dev; 749 bool not_done; 750 751 fiq_debugger_handle_wakeup(state); 752 753 /* handle the debugger irq in regular context */ 754 not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(), 755 get_irq_regs(), 756 current_thread_info()); 757 if (not_done) 758 fiq_debugger_handle_irq_context(state); 759 760 return IRQ_HANDLED; 761} 762 763/* 764 * If FIQs are used, not everything can happen in fiq context. 765 * FIQ handler does what it can and then signals this interrupt to finish the 766 * job in irq context. 767 */ 768static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev) 769{ 770 struct fiq_debugger_state *state = dev; 771 772 if (state->pdata->force_irq_ack) 773 state->pdata->force_irq_ack(state->pdev, state->signal_irq); 774 775 fiq_debugger_handle_irq_context(state); 776 777 return IRQ_HANDLED; 778} 779 780#ifdef CONFIG_FIQ_GLUE 781static void fiq_debugger_resume(struct fiq_glue_handler *h) 782{ 783 struct fiq_debugger_state *state = 784 container_of(h, struct fiq_debugger_state, handler); 785 if (state->pdata->uart_resume) 786 state->pdata->uart_resume(state->pdev); 787} 788#endif 789 790#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE) 791struct tty_driver *fiq_debugger_console_device(struct console *co, int *index) 792{ 793 *index = co->index; 794 return fiq_tty_driver; 795} 796 797static void fiq_debugger_console_write(struct console *co, 798 const char *s, unsigned int count) 799{ 800 struct fiq_debugger_state *state; 801 unsigned long flags; 802 803 state = container_of(co, struct fiq_debugger_state, console); 804 805 if (!state->console_enable && !state->syslog_dumping) 806 return; 807 808 fiq_debugger_uart_enable(state); 809 spin_lock_irqsave(&state->console_lock, flags); 810 while (count--) { 811 if (*s == '\n') 812 fiq_debugger_putc(state, '\r'); 813 fiq_debugger_putc(state, *s++); 814 } 815 fiq_debugger_uart_flush(state); 816 spin_unlock_irqrestore(&state->console_lock, flags); 817 fiq_debugger_uart_disable(state); 818} 819 820static struct console fiq_debugger_console = { 821 .name = "ttyFIQ", 822 .device = fiq_debugger_console_device, 823 .write = fiq_debugger_console_write, 824 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED, 825}; 826 827int fiq_tty_open(struct tty_struct *tty, struct file *filp) 828{ 829 int line = tty->index; 830 struct fiq_debugger_state **states = tty->driver->driver_state; 831 struct fiq_debugger_state *state = states[line]; 832 833 return tty_port_open(&state->tty_port, tty, filp); 834} 835 836void fiq_tty_close(struct tty_struct *tty, struct file *filp) 837{ 838 tty_port_close(tty->port, tty, filp); 839} 840 841int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) 842{ 843 int i; 844 int line = tty->index; 845 struct fiq_debugger_state **states = tty->driver->driver_state; 846 struct fiq_debugger_state *state = states[line]; 847 848 if (!state->console_enable) 849 return count; 850 851 fiq_debugger_uart_enable(state); 852 spin_lock_irq(&state->console_lock); 853 for (i = 0; i < count; i++) 854 fiq_debugger_putc(state, *buf++); 855 spin_unlock_irq(&state->console_lock); 856 fiq_debugger_uart_disable(state); 857 858 return count; 859} 860 861int fiq_tty_write_room(struct tty_struct *tty) 862{ 863 return 16; 864} 865 866#ifdef CONFIG_CONSOLE_POLL 867static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options) 868{ 869 return 0; 870} 871 872static int fiq_tty_poll_get_char(struct tty_driver *driver, int line) 873{ 874 struct fiq_debugger_state **states = driver->driver_state; 875 struct fiq_debugger_state *state = states[line]; 876 int c = NO_POLL_CHAR; 877 878 fiq_debugger_uart_enable(state); 879 if (fiq_debugger_have_fiq(state)) { 880 int count = fiq_debugger_ringbuf_level(state->tty_rbuf); 881 if (count > 0) { 882 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0); 883 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1); 884 } 885 } else { 886 c = fiq_debugger_getc(state); 887 if (c == FIQ_DEBUGGER_NO_CHAR) 888 c = NO_POLL_CHAR; 889 } 890 fiq_debugger_uart_disable(state); 891 892 return c; 893} 894 895static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch) 896{ 897 struct fiq_debugger_state **states = driver->driver_state; 898 struct fiq_debugger_state *state = states[line]; 899 fiq_debugger_uart_enable(state); 900 fiq_debugger_putc(state, ch); 901 fiq_debugger_uart_disable(state); 902} 903#endif 904 905static const struct tty_port_operations fiq_tty_port_ops; 906 907static const struct tty_operations fiq_tty_driver_ops = { 908 .write = fiq_tty_write, 909 .write_room = fiq_tty_write_room, 910 .open = fiq_tty_open, 911 .close = fiq_tty_close, 912#ifdef CONFIG_CONSOLE_POLL 913 .poll_init = fiq_tty_poll_init, 914 .poll_get_char = fiq_tty_poll_get_char, 915 .poll_put_char = fiq_tty_poll_put_char, 916#endif 917}; 918 919static int fiq_debugger_tty_init(void) 920{ 921 int ret; 922 struct fiq_debugger_state **states = NULL; 923 924 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL); 925 if (!states) { 926 pr_err("Failed to allocate fiq debugger state structres\n"); 927 return -ENOMEM; 928 } 929 930 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS); 931 if (!fiq_tty_driver) { 932 pr_err("Failed to allocate fiq debugger tty\n"); 933 ret = -ENOMEM; 934 goto err_free_state; 935 } 936 937 fiq_tty_driver->owner = THIS_MODULE; 938 fiq_tty_driver->driver_name = "fiq-debugger"; 939 fiq_tty_driver->name = "ttyFIQ"; 940 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; 941 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL; 942 fiq_tty_driver->init_termios = tty_std_termios; 943 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW | 944 TTY_DRIVER_DYNAMIC_DEV; 945 fiq_tty_driver->driver_state = states; 946 947 fiq_tty_driver->init_termios.c_cflag = 948 B115200 | CS8 | CREAD | HUPCL | CLOCAL; 949 fiq_tty_driver->init_termios.c_ispeed = 115200; 950 fiq_tty_driver->init_termios.c_ospeed = 115200; 951 952 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops); 953 954 ret = tty_register_driver(fiq_tty_driver); 955 if (ret) { 956 pr_err("Failed to register fiq tty: %d\n", ret); 957 goto err_free_tty; 958 } 959 960 pr_info("Registered FIQ tty driver\n"); 961 return 0; 962 963err_free_tty: 964 put_tty_driver(fiq_tty_driver); 965 fiq_tty_driver = NULL; 966err_free_state: 967 kfree(states); 968 return ret; 969} 970 971static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state) 972{ 973 int ret; 974 struct device *tty_dev; 975 struct fiq_debugger_state **states = fiq_tty_driver->driver_state; 976 977 states[state->pdev->id] = state; 978 979 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024); 980 if (!state->tty_rbuf) { 981 pr_err("Failed to allocate fiq debugger ringbuf\n"); 982 ret = -ENOMEM; 983 goto err; 984 } 985 986 tty_port_init(&state->tty_port); 987 state->tty_port.ops = &fiq_tty_port_ops; 988 989 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver, 990 state->pdev->id, &state->pdev->dev); 991 if (IS_ERR(tty_dev)) { 992 pr_err("Failed to register fiq debugger tty device\n"); 993 ret = PTR_ERR(tty_dev); 994 goto err; 995 } 996 997 device_set_wakeup_capable(tty_dev, 1); 998 999 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id); 1000 1001 return 0; 1002 1003err: 1004 fiq_debugger_ringbuf_free(state->tty_rbuf); 1005 state->tty_rbuf = NULL; 1006 return ret; 1007} 1008#endif 1009 1010static int fiq_debugger_dev_suspend(struct device *dev) 1011{ 1012 struct platform_device *pdev = to_platform_device(dev); 1013 struct fiq_debugger_state *state = platform_get_drvdata(pdev); 1014 1015 if (state->pdata->uart_dev_suspend) 1016 return state->pdata->uart_dev_suspend(pdev); 1017 return 0; 1018} 1019 1020static int fiq_debugger_dev_resume(struct device *dev) 1021{ 1022 struct platform_device *pdev = to_platform_device(dev); 1023 struct fiq_debugger_state *state = platform_get_drvdata(pdev); 1024 1025 if (state->pdata->uart_dev_resume) 1026 return state->pdata->uart_dev_resume(pdev); 1027 return 0; 1028} 1029 1030static int fiq_debugger_probe(struct platform_device *pdev) 1031{ 1032 int ret; 1033 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev); 1034 struct fiq_debugger_state *state; 1035 int fiq; 1036 int uart_irq; 1037 1038 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS) 1039 return -EINVAL; 1040 1041 if (!pdata->uart_getc || !pdata->uart_putc) 1042 return -EINVAL; 1043 if ((pdata->uart_enable && !pdata->uart_disable) || 1044 (!pdata->uart_enable && pdata->uart_disable)) 1045 return -EINVAL; 1046 1047 fiq = platform_get_irq_byname(pdev, "fiq"); 1048 uart_irq = platform_get_irq_byname(pdev, "uart_irq"); 1049 1050 /* uart_irq mode and fiq mode are mutually exclusive, but one of them 1051 * is required */ 1052 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0)) 1053 return -EINVAL; 1054 if (fiq >= 0 && !pdata->fiq_enable) 1055 return -EINVAL; 1056 1057 state = kzalloc(sizeof(*state), GFP_KERNEL); 1058 state->output.printf = fiq_debugger_printf; 1059 setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired, 1060 (unsigned long)state); 1061 state->pdata = pdata; 1062 state->pdev = pdev; 1063 state->no_sleep = initial_no_sleep; 1064 state->debug_enable = initial_debug_enable; 1065 state->console_enable = initial_console_enable; 1066 1067 state->fiq = fiq; 1068 state->uart_irq = uart_irq; 1069 state->signal_irq = platform_get_irq_byname(pdev, "signal"); 1070 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup"); 1071 1072 INIT_WORK(&state->work, fiq_debugger_work); 1073 spin_lock_init(&state->work_lock); 1074 1075 platform_set_drvdata(pdev, state); 1076 1077 spin_lock_init(&state->sleep_timer_lock); 1078 1079 if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state)) 1080 state->no_sleep = true; 1081 state->ignore_next_wakeup_irq = !state->no_sleep; 1082 1083 wake_lock_init(&state->debugger_wake_lock, 1084 WAKE_LOCK_SUSPEND, "serial-debug"); 1085 1086 state->clk = clk_get(&pdev->dev, NULL); 1087 if (IS_ERR(state->clk)) 1088 state->clk = NULL; 1089 1090 /* do not call pdata->uart_enable here since uart_init may still 1091 * need to do some initialization before uart_enable can work. 1092 * So, only try to manage the clock during init. 1093 */ 1094 if (state->clk) 1095 clk_enable(state->clk); 1096 1097 if (pdata->uart_init) { 1098 ret = pdata->uart_init(pdev); 1099 if (ret) 1100 goto err_uart_init; 1101 } 1102 1103 fiq_debugger_printf_nfiq(state, 1104 "<hit enter %sto activate fiq debugger>\n", 1105 state->no_sleep ? "" : "twice "); 1106 1107#ifdef CONFIG_FIQ_GLUE 1108 if (fiq_debugger_have_fiq(state)) { 1109 state->handler.fiq = fiq_debugger_fiq; 1110 state->handler.resume = fiq_debugger_resume; 1111 ret = fiq_glue_register_handler(&state->handler); 1112 if (ret) { 1113 pr_err("%s: could not install fiq handler\n", __func__); 1114 goto err_register_irq; 1115 } 1116 1117 pdata->fiq_enable(pdev, state->fiq, 1); 1118 } else 1119#endif 1120 { 1121 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq, 1122 IRQF_NO_SUSPEND, "debug", state); 1123 if (ret) { 1124 pr_err("%s: could not install irq handler\n", __func__); 1125 goto err_register_irq; 1126 } 1127 1128 /* for irq-only mode, we want this irq to wake us up, if it 1129 * can. 1130 */ 1131 enable_irq_wake(state->uart_irq); 1132 } 1133 1134 if (state->clk) 1135 clk_disable(state->clk); 1136 1137 if (state->signal_irq >= 0) { 1138 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq, 1139 IRQF_TRIGGER_RISING, "debug-signal", state); 1140 if (ret) 1141 pr_err("serial_debugger: could not install signal_irq"); 1142 } 1143 1144 if (state->wakeup_irq >= 0) { 1145 ret = request_irq(state->wakeup_irq, 1146 fiq_debugger_wakeup_irq_handler, 1147 IRQF_TRIGGER_FALLING | IRQF_DISABLED, 1148 "debug-wakeup", state); 1149 if (ret) { 1150 pr_err("serial_debugger: " 1151 "could not install wakeup irq\n"); 1152 state->wakeup_irq = -1; 1153 } else { 1154 ret = enable_irq_wake(state->wakeup_irq); 1155 if (ret) { 1156 pr_err("serial_debugger: " 1157 "could not enable wakeup\n"); 1158 state->wakeup_irq_no_set_wake = true; 1159 } 1160 } 1161 } 1162 if (state->no_sleep) 1163 fiq_debugger_handle_wakeup(state); 1164 1165#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE) 1166 spin_lock_init(&state->console_lock); 1167 state->console = fiq_debugger_console; 1168 state->console.index = pdev->id; 1169 if (!console_set_on_cmdline) 1170 add_preferred_console(state->console.name, 1171 state->console.index, NULL); 1172 register_console(&state->console); 1173 fiq_debugger_tty_init_one(state); 1174#endif 1175 return 0; 1176 1177err_register_irq: 1178 if (pdata->uart_free) 1179 pdata->uart_free(pdev); 1180err_uart_init: 1181 if (state->clk) 1182 clk_disable(state->clk); 1183 if (state->clk) 1184 clk_put(state->clk); 1185 wake_lock_destroy(&state->debugger_wake_lock); 1186 platform_set_drvdata(pdev, NULL); 1187 kfree(state); 1188 return ret; 1189} 1190 1191static const struct dev_pm_ops fiq_debugger_dev_pm_ops = { 1192 .suspend = fiq_debugger_dev_suspend, 1193 .resume = fiq_debugger_dev_resume, 1194}; 1195 1196static struct platform_driver fiq_debugger_driver = { 1197 .probe = fiq_debugger_probe, 1198 .driver = { 1199 .name = "fiq_debugger", 1200 .pm = &fiq_debugger_dev_pm_ops, 1201 }, 1202}; 1203 1204static int __init fiq_debugger_init(void) 1205{ 1206#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE) 1207 fiq_debugger_tty_init(); 1208#endif 1209 return platform_driver_register(&fiq_debugger_driver); 1210} 1211 1212postcore_initcall(fiq_debugger_init); 1213