1 /*
2 * drivers/soc/rockchip/rk_fiq_debugger.c
3 *
4 * Serial Debugger Interface for Rockchip
5 *
6 * Copyright (C) 2012 ROCKCHIP, Inc.
7 * Copyright (C) 2008 Google, Inc.
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19 #include <stdarg.h>
20 #include <linux/cpu.h>
21 #include <linux/cpu_pm.h>
22 #include <linux/module.h>
23 #include <linux/io.h>
24 #include <linux/of.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/interrupt.h>
28 #include <linux/clk.h>
29 #include <linux/platform_device.h>
30 #include <linux/irq.h>
31 #include <linux/serial_reg.h>
32 #include <linux/slab.h>
33 #include <linux/stacktrace.h>
34 #include <linux/uaccess.h>
35 #include <linux/kfifo.h>
36 #include <linux/kthread.h>
37 #include <linux/sched/rt.h>
38 #include "fiq_debugger.h"
39 #include <linux/irqchip/arm-gic.h>
40 #include <linux/clk.h>
41 #include <linux/delay.h>
42 #include "rk_fiq_debugger.h"
43 #include <linux/console.h>
44
45 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
46 #include <linux/rockchip/rockchip_sip.h>
47 #endif
48 #define UART_USR 0x1f /* In: UART Status Register */
49 #define UART_USR_RX_FIFO_FULL 0x10 /* Receive FIFO full */
50 #define UART_USR_RX_FIFO_NOT_EMPTY 0x08 /* Receive FIFO not empty */
51 #define UART_USR_TX_FIFO_EMPTY 0x04 /* Transmit FIFO empty */
52 #define UART_USR_TX_FIFO_NOT_FULL 0x02 /* Transmit FIFO not full */
53 #define UART_USR_BUSY 0x01 /* UART busy indicator */
54 #define UART_SRR 0x22 /* software reset register */
55 #define RK_UART_RFL 0x21 /* UART Receive Fifo Level Register */
56
57 struct rk_fiq_debugger {
58 int irq;
59 int baudrate;
60 struct fiq_debugger_pdata pdata;
61 void __iomem *debug_port_base;
62 bool break_seen;
63 #ifdef CONFIG_RK_CONSOLE_THREAD
64 struct task_struct *console_task;
65 #endif
66 };
67
68 static int rk_fiq_debugger_id;
69 static int serial_hwirq;
70
71 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
72 static bool tf_fiq_sup;
73 #endif
74
rk_fiq_write(struct rk_fiq_debugger * t,unsigned int val,unsigned int off)75 static inline void rk_fiq_write(struct rk_fiq_debugger *t,
76 unsigned int val, unsigned int off)
77 {
78 __raw_writel(val, t->debug_port_base + off * 4);
79 }
80
rk_fiq_read(struct rk_fiq_debugger * t,unsigned int off)81 static inline unsigned int rk_fiq_read(struct rk_fiq_debugger *t,
82 unsigned int off)
83 {
84 return __raw_readl(t->debug_port_base + off * 4);
85 }
86
rk_fiq_read_lsr(struct rk_fiq_debugger * t)87 static inline unsigned int rk_fiq_read_lsr(struct rk_fiq_debugger *t)
88 {
89 unsigned int lsr;
90
91 lsr = rk_fiq_read(t, UART_LSR);
92 if (lsr & UART_LSR_BI)
93 t->break_seen = true;
94
95 return lsr;
96 }
97
debug_port_init(struct platform_device * pdev)98 static int debug_port_init(struct platform_device *pdev)
99 {
100 int dll = 0, dlm = 0;
101 struct rk_fiq_debugger *t;
102
103 console_lock();
104
105 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
106
107 if (rk_fiq_read(t, UART_LSR) & UART_LSR_DR)
108 (void)rk_fiq_read(t, UART_RX);
109
110 switch (t->baudrate) {
111 case 1500000:
112 dll = 0x1;
113 break;
114 case 115200:
115 default:
116 dll = 0xd;
117 break;
118 }
119 /* reset uart */
120 rk_fiq_write(t, 0x07, UART_SRR);
121 udelay(10);
122 /* set uart to loop back mode */
123 rk_fiq_write(t, 0x10, UART_MCR);
124
125 rk_fiq_write(t, 0x83, UART_LCR);
126 /* set baud rate */
127 rk_fiq_write(t, dll, UART_DLL);
128 rk_fiq_write(t, dlm, UART_DLM);
129 rk_fiq_write(t, 0x03, UART_LCR);
130
131 /* enable rx interrupt */
132 rk_fiq_write(t, UART_IER_RDI, UART_IER);
133
134 /*
135 * Interrupt on every character when received, but we can enable fifo for TX
136 * I found that if we enable the RX fifo, some problem may vanish such as when
137 * you continuously input characters in the command line the uart irq may be disable
138 * because of the uart irq is served when CPU is at IRQ exception, but it is
139 * found unregistered, so it is disable.
140 */
141 rk_fiq_write(t, 0x01, UART_FCR);
142
143 /* disbale loop back mode */
144 rk_fiq_write(t, 0x0, UART_MCR);
145
146 console_unlock();
147
148 return 0;
149 }
150
debug_getc(struct platform_device * pdev)151 static int debug_getc(struct platform_device *pdev)
152 {
153 unsigned int lsr, usr, rfl, iir;
154 struct rk_fiq_debugger *t;
155 unsigned int temp;
156 static unsigned int n;
157 static char buf[32];
158
159 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
160 /*
161 * Clear uart interrupt status
162 */
163 iir = rk_fiq_read(t, UART_IIR);
164 usr = rk_fiq_read(t, UART_USR);
165 lsr = rk_fiq_read_lsr(t);
166
167 /*
168 * There are ways to get Designware-based UARTs into a state where
169 * they are asserting UART_IIR_RX_TIMEOUT but there is no actual
170 * data available. If we see such a case then we'll do a bogus
171 * read. If we don't do this then the "RX TIMEOUT" interrupt will
172 * fire forever.
173 */
174 if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT) {
175 rfl = rk_fiq_read(t, RK_UART_RFL);
176 if (!(lsr & (UART_LSR_DR | UART_LSR_BI)) && !(usr & 0x1) && (rfl == 0))
177 rk_fiq_read(t, UART_RX);
178 }
179
180 if (lsr & UART_LSR_DR) {
181 temp = rk_fiq_read(t, UART_RX);
182 buf[n & 0x1f] = temp;
183 n++;
184 if (temp == 'q' && n > 2) {
185 if ((buf[(n - 2) & 0x1f] == 'i') &&
186 (buf[(n - 3) & 0x1f] == 'f'))
187 return FIQ_DEBUGGER_BREAK;
188 else
189 return temp;
190 } else {
191 return temp;
192 }
193 }
194
195 return FIQ_DEBUGGER_NO_CHAR;
196 }
197
debug_putc(struct platform_device * pdev,unsigned int c)198 static void debug_putc(struct platform_device *pdev, unsigned int c)
199 {
200 struct rk_fiq_debugger *t;
201 unsigned int count = 10000;
202
203 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
204
205 while (!(rk_fiq_read(t, UART_USR) & UART_USR_TX_FIFO_NOT_FULL) && count--)
206 udelay(10);
207
208 rk_fiq_write(t, c, UART_TX);
209 }
210
debug_getc_dummy(struct platform_device * pdev)211 static int debug_getc_dummy(struct platform_device *pdev)
212 {
213 return FIQ_DEBUGGER_NO_CHAR;
214 }
215
debug_putc_dummy(struct platform_device * pdev,unsigned int c)216 static void debug_putc_dummy(struct platform_device *pdev, unsigned int c)
217 {
218 }
219
debug_flush(struct platform_device * pdev)220 static void debug_flush(struct platform_device *pdev)
221 {
222 struct rk_fiq_debugger *t;
223 unsigned int count = 10000;
224 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
225
226 while (!(rk_fiq_read_lsr(t) & UART_LSR_TEMT) && count--)
227 udelay(10);
228 }
229
230 #ifdef CONFIG_RK_CONSOLE_THREAD
231 #define FIFO_SIZE SZ_64K
232 #define TTY_FIFO_SIZE SZ_64K
233 static DEFINE_KFIFO(fifo, unsigned char, FIFO_SIZE);
234 static DEFINE_KFIFO(tty_fifo, unsigned char, TTY_FIFO_SIZE);
235 static bool console_thread_stop; /* write on console_write */
236 static bool console_thread_running; /* write on console_thread */
237 static unsigned int console_dropped_messages;
238
write_room(struct platform_device * pdev)239 static int write_room(struct platform_device *pdev)
240 {
241 return (TTY_FIFO_SIZE - kfifo_len(&tty_fifo));
242 }
243
console_putc(struct platform_device * pdev,unsigned int c)244 static void console_putc(struct platform_device *pdev, unsigned int c)
245 {
246 struct rk_fiq_debugger *t;
247 unsigned int count = 2; /* loop 2 times is enough */
248 unsigned long us = 400; /* the time to send 60 byte for baudrate 1500000 */
249
250 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
251
252 if (t->baudrate == 115200)
253 us = 5160; /* the time to send 60 byte for baudrate 115200 */
254
255 while (!(rk_fiq_read(t, UART_USR) & UART_USR_TX_FIFO_NOT_FULL) &&
256 count--)
257 usleep_range(us, us + us / 20);
258
259 rk_fiq_write(t, c, UART_TX);
260 }
261
console_flush(struct platform_device * pdev)262 static void console_flush(struct platform_device *pdev)
263 {
264 struct rk_fiq_debugger *t;
265 unsigned int count = 2; /* loop 2 times is enough */
266 unsigned long us = 428; /* the time to send 64 byte for baudrate 1500000 */
267
268 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
269
270 if (t->baudrate == 115200)
271 us = 5500; /* the time to send 64 byte for baudrate 115200 */
272
273 while (!(rk_fiq_read_lsr(t) & UART_LSR_TEMT) && count--)
274 usleep_range(us, us + us / 20);
275 }
276
console_put(struct platform_device * pdev,const char * s,unsigned int count)277 static void console_put(struct platform_device *pdev,
278 const char *s, unsigned int count)
279 {
280 while (count--) {
281 if (*s == '\n')
282 console_putc(pdev, '\r');
283 console_putc(pdev, *s++);
284 }
285 }
286
debug_put(struct platform_device * pdev,const char * s,unsigned int count)287 static void debug_put(struct platform_device *pdev,
288 const char *s, unsigned int count)
289 {
290 while (count--) {
291 if (*s == '\n')
292 debug_putc(pdev, '\r');
293 debug_putc(pdev, *s++);
294 }
295 }
296
wake_up_console_thread(struct task_struct * console_task)297 static void wake_up_console_thread(struct task_struct *console_task)
298 {
299 /*
300 * Avoid dead lock on console_task->pi_lock and console_lock
301 * when call printk() in try_to_wake_up().
302 *
303 * cpu0 hold console_lock, then try lock pi_lock fail:
304 * printk()->vprintk_emit()->console_unlock()->try_to_wake_up()
305 * ->lock(pi_lock)->deadlock
306 *
307 * cpu1 hold pi_lock, then try lock console_lock fail:
308 * console_thread()->console_put()->usleep_range()->run_hrtimer()
309 * ->hrtimer_wakeup()->try_to_wake_up()[hold_pi_lock]->printk()
310 * ->vprintk_emit()->console_trylock_spining()->cpu_relax()->deadlock
311 *
312 * if cpu0 does not hold console_lock, cpu1 also deadlock on pi_lock:
313 * ...->hrtimer_wakeup()->try_to_wake_up()[hold_pi_lock]->printk()
314 * ->vprintk_emit()->console_unlock()->try_to_wake_up()
315 * ->lock(pi_lock)->deadlock
316 *
317 * so when console_task is running on usleep_range(), printk()
318 * should not wakeup console_task to avoid lock(pi_lock) again,
319 * as run_hrtimer() will wakeup console_task later.
320 * console_thread_running==false guarantee that console_task
321 * is not running on usleep_range().
322 */
323 if (!READ_ONCE(console_thread_running))
324 wake_up_process(console_task);
325 }
326
console_thread(void * data)327 static int console_thread(void *data)
328 {
329 struct platform_device *pdev = data;
330 char buf[64], c = 0;
331 unsigned int len = 0, len_tty = 0;
332
333 while (1) {
334 unsigned int dropped;
335
336 set_current_state(TASK_INTERRUPTIBLE);
337 if (console_thread_stop || (kfifo_is_empty(&fifo) && kfifo_is_empty(&tty_fifo))) {
338 smp_store_mb(console_thread_running, false);
339 schedule();
340 smp_store_mb(console_thread_running, true);
341 }
342 if (kthread_should_stop())
343 break;
344 set_current_state(TASK_RUNNING);
345
346 while (!console_thread_stop && (!kfifo_is_empty(&fifo) || !kfifo_is_empty(&tty_fifo))) {
347 while (!console_thread_stop && kfifo_get(&fifo, &c)) {
348 console_put(pdev, &c, 1);
349 if (c == '\n')
350 break;
351 }
352
353 while (!console_thread_stop && kfifo_get(&tty_fifo, &c)) {
354 console_putc(pdev, c);
355 len_tty++;
356 if (c == '\n')
357 break;
358 }
359 }
360
361 if (len_tty > 0)
362 fiq_tty_wake_up(pdev);
363 len_tty = 0;
364
365 dropped = console_dropped_messages;
366 if (dropped && !console_thread_stop) {
367 console_dropped_messages = 0;
368 smp_wmb();
369 len = sprintf(buf, "** %u console messages dropped **\n",
370 dropped);
371 console_put(pdev, buf, len);
372 }
373 if (!console_thread_stop)
374 console_flush(pdev);
375 }
376
377 return 0;
378 }
379
console_write(struct platform_device * pdev,const char * s,unsigned int count)380 static void console_write(struct platform_device *pdev, const char *s, unsigned int count)
381 {
382 unsigned int fifo_count = FIFO_SIZE;
383 unsigned char c;
384 struct rk_fiq_debugger *t;
385
386 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
387
388 if (console_thread_stop ||
389 oops_in_progress ||
390 system_state == SYSTEM_HALT ||
391 system_state == SYSTEM_POWER_OFF ||
392 system_state == SYSTEM_RESTART) {
393 if (!console_thread_stop) {
394 console_thread_stop = true;
395 smp_wmb();
396 debug_flush(pdev);
397 while (fifo_count-- && kfifo_get(&fifo, &c))
398 debug_put(pdev, &c, 1);
399 }
400 debug_put(pdev, s, count);
401 debug_flush(pdev);
402 } else if (count) {
403 unsigned int ret = 0;
404
405 if (kfifo_len(&fifo) + count <= FIFO_SIZE)
406 ret = kfifo_in(&fifo, s, count);
407 if (!ret) {
408 console_dropped_messages++;
409 smp_wmb();
410 } else {
411 wake_up_console_thread(t->console_task);
412 }
413 }
414 }
415
tty_write(struct platform_device * pdev,const char * s,int count)416 static int tty_write(struct platform_device *pdev, const char *s, int count)
417 {
418 unsigned int ret = 0;
419 struct rk_fiq_debugger *t;
420
421 if (console_thread_stop)
422 return count;
423 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
424
425 if (count > 0) {
426 if (kfifo_len(&tty_fifo) + count <= TTY_FIFO_SIZE)
427 ret = kfifo_in(&tty_fifo, s, count);
428
429 if (ret <= 0)
430 return 0;
431 wake_up_console_thread(t->console_task);
432 }
433 return count;
434 }
435 #endif
436
fiq_enable(struct platform_device * pdev,unsigned int irq,bool on)437 static void fiq_enable(struct platform_device *pdev, unsigned int irq, bool on)
438 {
439 if (on)
440 enable_irq(irq);
441 else
442 disable_irq(irq);
443 }
444
445 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
446 #ifdef CONFIG_ARM_SDE_INTERFACE
447 #include <linux/arm_sdei.h>
448 #include <asm/smp_plat.h>
449 #include <linux/suspend.h>
450 void fiq_debugger_fiq_get_(const char *fmt, ...);
451
452 static struct rk_fiq_sdei_st {
453 u32 cur_cpu;
454 u32 sw_cpu;
455 u32 cpu_can_sw;
456 int fiq_en;
457 u32 event_id;
458 u32 cpu_off_sw;
459 u32 cpu_sw_event_id;
460 } rk_fiq_sdei;
461
sdei_fiq_debugger_is_enabled(void)462 int sdei_fiq_debugger_is_enabled(void)
463 {
464 return rk_fiq_sdei.fiq_en;
465 }
466
fiq_sdei_event_callback(u32 event,struct pt_regs * regs,void * arg)467 static int fiq_sdei_event_callback(u32 event, struct pt_regs *regs, void *arg)
468 {
469 int cpu_id = get_logical_index(read_cpuid_mpidr() &
470 MPIDR_HWID_BITMASK);
471 fiq_debugger_fiq(regs, cpu_id);
472
473 return 0;
474 }
475
rk_fiq_sdei_event_sw_cpu(int wait_disable)476 static void rk_fiq_sdei_event_sw_cpu(int wait_disable)
477 {
478 unsigned long affinity;
479 int cnt = 100000;
480 int ret = 0;
481
482 do {
483 ret = sdei_event_disable_nolock(rk_fiq_sdei.event_id);
484 if (!ret)
485 break;
486 cnt--;
487 udelay(20);
488 } while (wait_disable && cnt);
489
490 affinity = cpu_logical_map(rk_fiq_sdei.sw_cpu) & MPIDR_HWID_BITMASK;
491 ret = sdei_event_routing_set_nolock(rk_fiq_sdei.event_id,
492 SDEI_EVENT_REGISTER_RM_PE,
493 affinity);
494 ret = sdei_event_enable_nolock(rk_fiq_sdei.event_id);
495 rk_fiq_sdei.cur_cpu = rk_fiq_sdei.sw_cpu;
496 }
497
fiq_sdei_sw_cpu_event_callback(u32 event,struct pt_regs * regs,void * arg)498 static int fiq_sdei_sw_cpu_event_callback(u32 event, struct pt_regs *regs, void *arg)
499 {
500 int cnt = 10000;
501 int ret = 0;
502 int cpu_id = event - rk_fiq_sdei.cpu_sw_event_id;
503
504 WARN_ON(cpu_id !=
505 get_logical_index(read_cpuid_mpidr() & MPIDR_HWID_BITMASK));
506
507 if (cpu_id == rk_fiq_sdei.sw_cpu) {
508 if (!rk_fiq_sdei.cpu_off_sw) {
509 rk_fiq_sdei.cpu_can_sw = 1;
510 } else {
511 rk_fiq_sdei_event_sw_cpu(1);
512 rk_fiq_sdei.cpu_off_sw = 0;
513 }
514 } else if (cpu_id == rk_fiq_sdei.cur_cpu && !rk_fiq_sdei.cpu_off_sw) {
515 while (!rk_fiq_sdei.cpu_can_sw && cnt) {
516 udelay(10);
517 cnt--;
518 };
519
520 if (rk_fiq_sdei.cpu_can_sw) {
521 rk_fiq_sdei_event_sw_cpu(0);
522 rk_fiq_sdei.cpu_can_sw = 0;
523 }
524 }
525 return ret;
526 }
527
_rk_fiq_dbg_sdei_switch_cpu(unsigned int cpu,int cpu_off)528 static void _rk_fiq_dbg_sdei_switch_cpu(unsigned int cpu, int cpu_off)
529 {
530 if (cpu == rk_fiq_sdei.cur_cpu)
531 return;
532 rk_fiq_sdei.sw_cpu = cpu;
533 rk_fiq_sdei.cpu_can_sw = 0;
534 rk_fiq_sdei.cpu_off_sw = cpu_off;
535 sip_fiq_debugger_sdei_switch_cpu(rk_fiq_sdei.cur_cpu, cpu, cpu_off);
536 }
537
rk_fiq_dbg_sdei_switch_cpu(struct platform_device * pdev,unsigned int cpu)538 static void rk_fiq_dbg_sdei_switch_cpu(struct platform_device *pdev,
539 unsigned int cpu)
540 {
541 _rk_fiq_dbg_sdei_switch_cpu(cpu, 0);
542 }
543
fiq_dbg_sdei_cpu_off_migrate_fiq(unsigned int cpu)544 static int fiq_dbg_sdei_cpu_off_migrate_fiq(unsigned int cpu)
545 {
546 unsigned int target_cpu;
547 int cnt = 10000;
548
549 if (rk_fiq_sdei.cur_cpu == cpu) {
550 target_cpu = cpumask_any_but(cpu_online_mask, cpu);
551 _rk_fiq_dbg_sdei_switch_cpu(target_cpu, 1);
552
553 while (rk_fiq_sdei.cur_cpu == cpu && cnt) {
554 udelay(10);
555 cnt--;
556 };
557 if (!cnt)
558 pr_err("%s: from %d to %d err!\n",
559 __func__, cpu, target_cpu);
560 }
561
562 return 0;
563 }
564
fiq_dbg_sdei_pm_callback(struct notifier_block * nb,unsigned long mode,void * _unused)565 static int fiq_dbg_sdei_pm_callback(struct notifier_block *nb,
566 unsigned long mode, void *_unused)
567 {
568 unsigned int target_cpu;
569
570 switch (mode) {
571 case PM_SUSPEND_PREPARE:
572 target_cpu = cpumask_first(cpu_online_mask);
573 if (target_cpu != 0)
574 pr_err("%s: fiq for core !\n", __func__);
575 else
576 _rk_fiq_dbg_sdei_switch_cpu(target_cpu, 1);
577 break;
578 default:
579 break;
580 }
581 return 0;
582 }
583
584 static struct notifier_block fiq_dbg_sdei_pm_nb = {
585 .notifier_call = fiq_dbg_sdei_pm_callback,
586 };
587
fiq_debugger_sdei_enable(struct rk_fiq_debugger * t)588 static int fiq_debugger_sdei_enable(struct rk_fiq_debugger *t)
589 {
590 int ret, cpu, i;
591 int is_dyn_event = false;
592
593 ret = sip_fiq_debugger_sdei_get_event_id(&rk_fiq_sdei.event_id,
594 &rk_fiq_sdei.cpu_sw_event_id,
595 NULL);
596
597 if (ret) {
598 pr_err("%s: get event id error!\n", __func__);
599 return ret;
600 }
601
602 /* If we can't get a valid fiq event, use dynamic event instead */
603 if (rk_fiq_sdei.event_id == 0) {
604 ret = sdei_interrupt_bind(serial_hwirq, &rk_fiq_sdei.event_id);
605 if (ret) {
606 pr_err("%s: bind intr:%d error!\n", __func__, serial_hwirq);
607 return ret;
608 }
609
610 is_dyn_event = true;
611 }
612
613 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
614 "soc/rk_sdei_fiq_debugger",
615 NULL,
616 fiq_dbg_sdei_cpu_off_migrate_fiq);
617 if (ret < 0) {
618 pr_err("%s: cpuhp_setup_state_nocalls error! %d\n",
619 __func__, ret);
620 return ret;
621 }
622
623 if (register_pm_notifier(&fiq_dbg_sdei_pm_nb)) {
624 pr_err("%s: register pm notify error: %d!\n", __func__, ret);
625 return ret;
626 }
627
628 ret = sdei_event_register(rk_fiq_sdei.event_id,
629 fiq_sdei_event_callback, NULL);
630
631 if (ret) {
632 pr_err("%s: sdei_event_register error!\n", __func__);
633 unregister_pm_notifier(&fiq_dbg_sdei_pm_nb);
634 return ret;
635 }
636
637 rk_fiq_sdei.cur_cpu = 0;
638
639 ret = sdei_event_routing_set(rk_fiq_sdei.event_id,
640 SDEI_EVENT_REGISTER_RM_PE,
641 cpu_logical_map(rk_fiq_sdei.cur_cpu));
642
643 if (ret) {
644 pr_err("%s: sdei_event_routing_set error!\n", __func__);
645 goto err;
646 }
647
648 ret = sdei_event_enable(rk_fiq_sdei.event_id);
649 if (ret) {
650 pr_err("%s: sdei_event_enable error!\n", __func__);
651 goto err;
652 }
653
654 for (cpu = 0; cpu < num_possible_cpus(); cpu++) {
655 ret = sdei_event_register(rk_fiq_sdei.cpu_sw_event_id + cpu,
656 fiq_sdei_sw_cpu_event_callback,
657 NULL);
658 if (ret) {
659 pr_err("%s: cpu %d sdei_event_register error!\n",
660 __func__, cpu);
661 goto cpu_sw_err;
662 }
663 ret = sdei_event_routing_set(rk_fiq_sdei.cpu_sw_event_id + cpu,
664 SDEI_EVENT_REGISTER_RM_PE,
665 cpu_logical_map(cpu));
666
667 if (ret) {
668 pr_err("%s:cpu %d fiq_sdei_event_routing_set error!\n",
669 __func__, cpu);
670 goto cpu_sw_err;
671 }
672
673 ret = sdei_event_enable(rk_fiq_sdei.cpu_sw_event_id + cpu);
674 if (ret) {
675 pr_err("%s: cpu %d sdei_event_enable error!\n",
676 __func__, cpu);
677 goto cpu_sw_err;
678 }
679 }
680
681 t->pdata.switch_cpu = rk_fiq_dbg_sdei_switch_cpu;
682 rk_fiq_sdei.fiq_en = 1;
683 return 0;
684 cpu_sw_err:
685 for (i = 0; i < cpu; i++)
686 sdei_event_unregister(rk_fiq_sdei.cpu_sw_event_id + i);
687 err:
688 unregister_pm_notifier(&fiq_dbg_sdei_pm_nb);
689 sdei_event_unregister(rk_fiq_sdei.event_id);
690
691 if (is_dyn_event)
692 sdei_interrupt_release(rk_fiq_sdei.event_id);
693
694 return ret;
695 }
696
697 #else
fiq_debugger_sdei_enable(struct rk_fiq_debugger * t)698 static inline int fiq_debugger_sdei_enable(struct rk_fiq_debugger *t)
699 {
700 return -EINVAL;
701 }
702 #endif
703
rk_fiq_debugger_switch_cpu(struct platform_device * pdev,unsigned int cpu)704 static void rk_fiq_debugger_switch_cpu(struct platform_device *pdev,
705 unsigned int cpu)
706 {
707 sip_fiq_debugger_switch_cpu(cpu);
708 }
709
rk_fiq_debugger_enable_debug(struct platform_device * pdev,bool val)710 static void rk_fiq_debugger_enable_debug(struct platform_device *pdev, bool val)
711 {
712 sip_fiq_debugger_enable_debug(val);
713 }
714
fiq_debugger_uart_irq_tf(struct pt_regs * _pt_regs,unsigned long cpu)715 static void fiq_debugger_uart_irq_tf(struct pt_regs *_pt_regs, unsigned long cpu)
716 {
717 fiq_debugger_fiq(_pt_regs, cpu);
718 }
719
rk_fiq_debugger_uart_dev_resume(struct platform_device * pdev)720 static int rk_fiq_debugger_uart_dev_resume(struct platform_device *pdev)
721 {
722 struct rk_fiq_debugger *t;
723
724 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
725 sip_fiq_debugger_uart_irq_tf_init(serial_hwirq,
726 fiq_debugger_uart_irq_tf);
727 return 0;
728 }
729
730 /*
731 * We don't need to migrate fiq before cpuidle, because EL3 can promise to
732 * resume all fiq configure. We don't want fiq to break kernel cpu_resume(),
733 * so that fiq would be disabled in EL3 on purpose when cpu resume. We enable
734 * it here since everything is okay.
735 */
fiq_debugger_cpuidle_resume_fiq(struct notifier_block * nb,unsigned long action,void * hcpu)736 static int fiq_debugger_cpuidle_resume_fiq(struct notifier_block *nb,
737 unsigned long action, void *hcpu)
738 {
739 switch (action) {
740 case CPU_PM_EXIT:
741 if ((sip_fiq_debugger_is_enabled()) &&
742 (sip_fiq_debugger_get_target_cpu() == smp_processor_id()))
743 sip_fiq_debugger_enable_fiq(true, smp_processor_id());
744 break;
745 default:
746 break;
747 }
748
749 return NOTIFY_OK;
750 }
751
752 /*
753 * We must migrate fiq before cpu offline, because EL3 doesn't promise to
754 * resume all fiq configure at this sisutation. Here, we migrate fiq to any
755 * online cpu.
756 */
fiq_debugger_cpu_offine_migrate_fiq(unsigned int cpu)757 static int fiq_debugger_cpu_offine_migrate_fiq(unsigned int cpu)
758 {
759 unsigned int target_cpu;
760
761 if ((sip_fiq_debugger_is_enabled()) &&
762 (sip_fiq_debugger_get_target_cpu() == cpu)) {
763 target_cpu = cpumask_any_but(cpu_online_mask, cpu);
764 sip_fiq_debugger_switch_cpu(target_cpu);
765 }
766
767 return 0;
768 }
769
770 static struct notifier_block fiq_debugger_pm_notifier = {
771 .notifier_call = fiq_debugger_cpuidle_resume_fiq,
772 .priority = 100,
773 };
774
rk_fiq_debugger_register_cpu_pm_notify(void)775 static int rk_fiq_debugger_register_cpu_pm_notify(void)
776 {
777 int err;
778
779 err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
780 "soc/rk_fiq_debugger",
781 NULL,
782 fiq_debugger_cpu_offine_migrate_fiq);
783 if (err < 0) {
784 pr_err("fiq debugger register cpu notifier failed!\n");
785 return err;
786 }
787
788 err = cpu_pm_register_notifier(&fiq_debugger_pm_notifier);
789 if (err) {
790 pr_err("fiq debugger register pm notifier failed!\n");
791 return err;
792 }
793
794 return 0;
795 }
796
fiq_debugger_bind_sip_smc(struct rk_fiq_debugger * t,phys_addr_t phy_base,int hwirq,int signal_irq,unsigned int baudrate)797 static int fiq_debugger_bind_sip_smc(struct rk_fiq_debugger *t,
798 phys_addr_t phy_base,
799 int hwirq,
800 int signal_irq,
801 unsigned int baudrate)
802 {
803 int err;
804
805 err = sip_fiq_debugger_request_share_memory();
806 if (err) {
807 pr_err("fiq debugger request share memory failed: %d\n", err);
808 goto exit;
809 }
810
811 err = rk_fiq_debugger_register_cpu_pm_notify();
812 if (err) {
813 pr_err("fiq debugger register cpu pm notify failed: %d\n", err);
814 goto exit;
815 }
816
817 err = sip_fiq_debugger_uart_irq_tf_init(hwirq,
818 fiq_debugger_uart_irq_tf);
819 if (err) {
820 pr_err("fiq debugger bind fiq to trustzone failed: %d\n", err);
821 goto exit;
822 }
823
824 t->pdata.uart_dev_resume = rk_fiq_debugger_uart_dev_resume;
825 t->pdata.switch_cpu = rk_fiq_debugger_switch_cpu;
826 t->pdata.enable_debug = rk_fiq_debugger_enable_debug;
827 sip_fiq_debugger_set_print_port(phy_base, baudrate);
828
829 pr_info("fiq debugger fiq mode enabled\n");
830
831 return 0;
832
833 exit:
834 t->pdata.switch_cpu = NULL;
835 t->pdata.enable_debug = NULL;
836
837 return err;
838 }
839 #endif
840
rk_serial_debug_init(void __iomem * base,phys_addr_t phy_base,int irq,int signal_irq,int wakeup_irq,unsigned int baudrate)841 static void rk_serial_debug_init(void __iomem *base, phys_addr_t phy_base,
842 int irq, int signal_irq,
843 int wakeup_irq, unsigned int baudrate)
844 {
845 struct rk_fiq_debugger *t = NULL;
846 struct platform_device *pdev = NULL;
847 struct resource *res = NULL;
848 int res_count = 0;
849 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
850 int ret = 0;
851 #endif
852
853 if (!base) {
854 pr_err("Invalid fiq debugger uart base\n");
855 return;
856 }
857
858 t = kzalloc(sizeof(struct rk_fiq_debugger), GFP_KERNEL);
859 if (!t) {
860 pr_err("Failed to allocate for fiq debugger\n");
861 return;
862 }
863
864 t->irq = irq;
865 t->baudrate = baudrate;
866 t->pdata.uart_init = debug_port_init;
867 t->pdata.uart_getc = debug_getc;
868 t->pdata.uart_putc = debug_putc;
869 #ifndef CONFIG_RK_CONSOLE_THREAD
870 t->pdata.uart_flush = debug_flush;
871 #endif
872 t->pdata.fiq_enable = fiq_enable;
873 t->pdata.force_irq = NULL;
874 t->debug_port_base = base;
875
876 res = kzalloc(sizeof(struct resource) * 3, GFP_KERNEL);
877 if (!res) {
878 pr_err("Failed to alloc fiq debugger resources\n");
879 goto out2;
880 }
881
882 pdev = kzalloc(sizeof(struct platform_device), GFP_KERNEL);
883 if (!pdev) {
884 pr_err("Failed to alloc fiq debugger platform device\n");
885 goto out3;
886 }
887
888 /* clear busy interrupt, make sure all interrupts are disabled */
889 rk_fiq_read(t, UART_USR);
890 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
891 if ((signal_irq > 0) && (serial_hwirq > 0)) {
892 ret = fiq_debugger_sdei_enable(t);
893 if (ret)
894 ret = fiq_debugger_bind_sip_smc(t, phy_base,
895 serial_hwirq,
896 signal_irq, baudrate);
897 if (ret)
898 tf_fiq_sup = false;
899 else
900 tf_fiq_sup = true;
901 }
902 #endif
903
904 if (irq > 0) {
905 res[0].flags = IORESOURCE_IRQ;
906 res[0].start = irq;
907 res[0].end = irq;
908 #if defined(CONFIG_FIQ_GLUE)
909 if (signal_irq > 0)
910 res[0].name = "fiq";
911 else
912 res[0].name = "uart_irq";
913 #elif defined(CONFIG_FIQ_DEBUGGER_TRUST_ZONE)
914 if (tf_fiq_sup && (signal_irq > 0))
915 res[0].name = "fiq";
916 else
917 res[0].name = "uart_irq";
918 #else
919 res[0].name = "uart_irq";
920 #endif
921 res_count++;
922 }
923
924 if (signal_irq > 0) {
925 res[1].flags = IORESOURCE_IRQ;
926 res[1].start = signal_irq;
927 res[1].end = signal_irq;
928 res[1].name = "signal";
929 res_count++;
930 }
931
932 if (wakeup_irq > 0) {
933 res[2].flags = IORESOURCE_IRQ;
934 res[2].start = wakeup_irq;
935 res[2].end = wakeup_irq;
936 res[2].name = "wakeup";
937 res_count++;
938 }
939
940 #ifdef CONFIG_RK_CONSOLE_THREAD
941 t->console_task = kthread_run(console_thread, pdev, "kconsole");
942 if (!IS_ERR(t->console_task)) {
943 t->pdata.console_write = console_write;
944 t->pdata.tty_write = tty_write;
945 t->pdata.write_room = write_room;
946 }
947 #endif
948
949 pdev->name = "fiq_debugger";
950 pdev->id = rk_fiq_debugger_id++;
951 pdev->dev.platform_data = &t->pdata;
952 pdev->resource = res;
953 pdev->num_resources = res_count;
954 if (platform_device_register(pdev)) {
955 pr_err("Failed to register fiq debugger\n");
956 goto out4;
957 }
958 return;
959
960 out4:
961 kfree(pdev);
962 out3:
963 kfree(res);
964 out2:
965 kfree(t);
966 }
967
rk_serial_debug_init_dummy(void)968 static void rk_serial_debug_init_dummy(void)
969 {
970 struct rk_fiq_debugger *t = NULL;
971 struct platform_device *pdev = NULL;
972
973 t = kzalloc(sizeof(*t), GFP_KERNEL);
974 if (!t) {
975 pr_err("Failed to allocate for fiq debugger\n");
976 return;
977 }
978
979 t->pdata.uart_getc = debug_getc_dummy;
980 t->pdata.uart_putc = debug_putc_dummy;
981
982 pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
983 if (!pdev) {
984 pr_err("Failed to alloc fiq debugger platform device\n");
985 goto out2;
986 }
987
988 pdev->name = "fiq_debugger";
989 pdev->id = rk_fiq_debugger_id++;
990 pdev->dev.platform_data = &t->pdata;
991 if (platform_device_register(pdev)) {
992 pr_err("Failed to register fiq debugger\n");
993 goto out3;
994 }
995 return;
996
997 out3:
998 kfree(pdev);
999 out2:
1000 kfree(t);
1001 }
1002
1003 #if defined(CONFIG_OF)
1004 static const struct of_device_id rk_fiqdbg_of_match[] = {
1005 { .compatible = "rockchip,fiq-debugger", },
1006 {},
1007 };
1008 MODULE_DEVICE_TABLE(of, rk_fiqdbg_of_match);
1009 #endif
1010
rk_fiqdbg_probe(struct platform_device * pdev)1011 static int __init rk_fiqdbg_probe(struct platform_device *pdev)
1012 {
1013 void __iomem *base;
1014 struct device_node *np = pdev->dev.of_node;
1015 unsigned int id, ok = 0;
1016 int irq, signal_irq = -1, wake_irq = -1;
1017 unsigned int baudrate = 0, irq_mode = 0;
1018 phys_addr_t phy_base = 0;
1019 int serial_id;
1020 struct clk *clk;
1021 struct clk *pclk;
1022 struct of_phandle_args oirq;
1023 struct resource res;
1024
1025 if (!of_device_is_available(np)) {
1026 pr_err("fiq-debugger is disabled in device tree\n");
1027 return -ENODEV;
1028 }
1029
1030 if (of_property_read_u32(np, "rockchip,serial-id", &serial_id))
1031 return -EINVAL;
1032
1033 if (serial_id == -1) {
1034 rk_serial_debug_init_dummy();
1035 return 0;
1036 }
1037
1038 if (of_property_read_u32(np, "rockchip,irq-mode-enable", &irq_mode))
1039 irq_mode = -1;
1040
1041 signal_irq = irq_of_parse_and_map(np, 0);
1042 if (!signal_irq)
1043 return -EINVAL;
1044
1045 if (of_property_read_u32(np, "rockchip,wake-irq", &wake_irq))
1046 wake_irq = -1;
1047
1048 if (of_property_read_u32(np, "rockchip,baudrate", &baudrate))
1049 baudrate = -1;
1050
1051 np = NULL;
1052
1053 do {
1054 np = of_find_node_by_name(np, "serial");
1055 if (np) {
1056 id = of_alias_get_id(np, "serial");
1057 if (id == serial_id) {
1058 ok = 1;
1059 break;
1060 }
1061 }
1062 } while(np);
1063
1064 if (!ok)
1065 return -EINVAL;
1066
1067 if (of_device_is_available(np)) {
1068 pr_err("uart%d is enabled, please disable it\n", serial_id);
1069 return -EINVAL;
1070 }
1071
1072 /* parse serial hw irq */
1073 if (irq_mode != 1 && !of_irq_parse_one(np, 0, &oirq))
1074 serial_hwirq = oirq.args[1] + 32;
1075
1076 /* parse serial phy base address */
1077 if (!of_address_to_resource(np, 0, &res))
1078 phy_base = res.start;
1079
1080 pclk = of_clk_get_by_name(np, "apb_pclk");
1081 clk = of_clk_get_by_name(np, "baudclk");
1082 if (unlikely(IS_ERR(clk)) || unlikely(IS_ERR(pclk))) {
1083 pr_err("fiq-debugger get clock fail\n");
1084 return -EINVAL;
1085 }
1086
1087 clk_prepare_enable(clk);
1088 clk_prepare_enable(pclk);
1089
1090 irq = irq_of_parse_and_map(np, 0);
1091 if (!irq)
1092 return -EINVAL;
1093
1094 base = of_iomap(np, 0);
1095 if (base)
1096 rk_serial_debug_init(base, phy_base,
1097 irq, signal_irq, wake_irq, baudrate);
1098 return 0;
1099 }
1100
1101 static struct platform_driver rk_fiqdbg_driver = {
1102 .driver = {
1103 .name = "rk-fiq-debugger",
1104 .of_match_table = of_match_ptr(rk_fiqdbg_of_match),
1105 },
1106 };
1107
rk_fiqdbg_init(void)1108 static int __init rk_fiqdbg_init(void)
1109 {
1110 return platform_driver_probe(&rk_fiqdbg_driver,
1111 rk_fiqdbg_probe);
1112 }
1113
1114 #if defined(CONFIG_FIQ_DEBUGGER_TRUST_ZONE) && defined(CONFIG_ARM_SDE_INTERFACE)
1115 fs_initcall(rk_fiqdbg_init);
1116 #else
1117 subsys_initcall(rk_fiqdbg_init); /* after of_platform_default_populate_init */
1118 #endif
1119
rk_fiqdbg_exit(void)1120 static void __exit rk_fiqdbg_exit(void)
1121 {
1122 platform_driver_unregister(&rk_fiqdbg_driver);
1123 }
1124 module_exit(rk_fiqdbg_exit);
1125
1126 MODULE_AUTHOR("Huibin Hong <huibin.hong@rock-chips.com>");
1127 MODULE_DESCRIPTION("Rockchip FIQ Debugger");
1128 MODULE_LICENSE("GPL");
1129 MODULE_ALIAS("platform:rk-fiq-debugger");
1130