• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <../drivers/staging/android/fiq_debugger/fiq_debugger.h>
39 #include <linux/irqchip/arm-gic.h>
40 #include <linux/clk.h>
41 #include <linux/delay.h>
42 #include <linux/soc/rockchip/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 
49 #define UART_USR	0x1f	/* In: UART Status Register */
50 #define UART_USR_RX_FIFO_FULL		0x10 /* Receive FIFO full */
51 #define UART_USR_RX_FIFO_NOT_EMPTY	0x08 /* Receive FIFO not empty */
52 #define UART_USR_TX_FIFO_EMPTY		0x04 /* Transmit FIFO empty */
53 #define UART_USR_TX_FIFO_NOT_FULL	0x02 /* Transmit FIFO not full */
54 #define UART_USR_BUSY			0x01 /* UART busy indicator */
55 #define UART_SRR			0x22 /* software reset 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;
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 	rk_fiq_read(t, UART_USR);
164 	lsr = rk_fiq_read_lsr(t);
165 
166 	if (lsr & UART_LSR_DR) {
167 		temp = rk_fiq_read(t, UART_RX);
168 		buf[n & 0x1f] = temp;
169 		n++;
170 		if (temp == 'q' && n > 2) {
171 			if ((buf[(n - 2) & 0x1f] == 'i') &&
172 			    (buf[(n - 3) & 0x1f] == 'f'))
173 				return FIQ_DEBUGGER_BREAK;
174 			else
175 				return temp;
176 		} else {
177 			return temp;
178 		}
179 	}
180 
181 	return FIQ_DEBUGGER_NO_CHAR;
182 }
183 
debug_putc(struct platform_device * pdev,unsigned int c)184 static void debug_putc(struct platform_device *pdev, unsigned int c)
185 {
186 	struct rk_fiq_debugger *t;
187 	unsigned int count = 10000;
188 
189 	t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
190 
191 	while (!(rk_fiq_read(t, UART_USR) & UART_USR_TX_FIFO_NOT_FULL) && count--)
192 		udelay(10);
193 
194 	rk_fiq_write(t, c, UART_TX);
195 }
196 
debug_getc_dummy(struct platform_device * pdev)197 static int debug_getc_dummy(struct platform_device *pdev)
198 {
199 	return FIQ_DEBUGGER_NO_CHAR;
200 }
201 
debug_putc_dummy(struct platform_device * pdev,unsigned int c)202 static void debug_putc_dummy(struct platform_device *pdev, unsigned int c)
203 {
204 }
205 
debug_flush(struct platform_device * pdev)206 static void debug_flush(struct platform_device *pdev)
207 {
208 	struct rk_fiq_debugger *t;
209 	unsigned int count = 10000;
210 	t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
211 
212 	while (!(rk_fiq_read_lsr(t) & UART_LSR_TEMT) && count--)
213 		udelay(10);
214 }
215 
216 #ifdef CONFIG_RK_CONSOLE_THREAD
217 #define FIFO_SIZE SZ_64K
218 #define LINE_MAX 1024
219 static DEFINE_KFIFO(fifo, unsigned char, FIFO_SIZE);
220 static char console_buf[LINE_MAX]; /* avoid FRAME WARN */
221 static bool console_thread_stop; /* write on console_write */
222 static bool console_thread_running; /* write on console_thread */
223 static unsigned int console_dropped_messages;
224 
console_putc(struct platform_device * pdev,unsigned int c)225 static void console_putc(struct platform_device *pdev, unsigned int c)
226 {
227 	struct rk_fiq_debugger *t;
228 	unsigned int count = 500;
229 
230 	t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
231 
232 	while (!(rk_fiq_read(t, UART_USR) & UART_USR_TX_FIFO_NOT_FULL) &&
233 	       count--)
234 		usleep_range(200, 210);
235 
236 	rk_fiq_write(t, c, UART_TX);
237 }
238 
console_flush(struct platform_device * pdev)239 static void console_flush(struct platform_device *pdev)
240 {
241 	struct rk_fiq_debugger *t;
242 	unsigned int count = 500;
243 
244 	t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
245 
246 	while (!(rk_fiq_read_lsr(t) & UART_LSR_TEMT) && count--)
247 		usleep_range(200, 210);
248 }
249 
console_put(struct platform_device * pdev,const char * s,unsigned int count)250 static void console_put(struct platform_device *pdev,
251 			const char *s, unsigned int count)
252 {
253 	while (count--) {
254 		if (*s == '\n')
255 			console_putc(pdev, '\r');
256 		console_putc(pdev, *s++);
257 	}
258 }
259 
debug_put(struct platform_device * pdev,const char * s,unsigned int count)260 static void debug_put(struct platform_device *pdev,
261 		      const char *s, unsigned int count)
262 {
263 	while (count--) {
264 		if (*s == '\n')
265 			debug_putc(pdev, '\r');
266 		debug_putc(pdev, *s++);
267 	}
268 }
269 
console_thread(void * data)270 static int console_thread(void *data)
271 {
272 	struct platform_device *pdev = data;
273 	char *buf = console_buf;
274 	unsigned int len;
275 
276 	while (1) {
277 		unsigned int dropped;
278 
279 		set_current_state(TASK_INTERRUPTIBLE);
280 		if (kfifo_is_empty(&fifo)) {
281 			smp_store_mb(console_thread_running, false);
282 			schedule();
283 			smp_store_mb(console_thread_running, true);
284 		}
285 		if (kthread_should_stop())
286 			break;
287 		set_current_state(TASK_RUNNING);
288 		while (!console_thread_stop) {
289 			len = kfifo_out(&fifo, buf, LINE_MAX);
290 			if (!len)
291 				break;
292 			console_put(pdev, buf, len);
293 		}
294 		dropped = console_dropped_messages;
295 		if (dropped && !console_thread_stop) {
296 			console_dropped_messages = 0;
297 			smp_wmb();
298 			len = snprintf(buf, LINE_MAX,
299 				       "** %u console messages dropped **\n",
300 				       dropped);
301 			console_put(pdev, buf, len);
302 		}
303 		if (!console_thread_stop)
304 			console_flush(pdev);
305 	}
306 
307 	return 0;
308 }
309 
console_write(struct platform_device * pdev,const char * s,unsigned int count)310 static void console_write(struct platform_device *pdev, const char *s, unsigned int count)
311 {
312 	unsigned int fifo_count = FIFO_SIZE;
313 	unsigned char c;
314 	struct rk_fiq_debugger *t;
315 
316 	t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
317 
318 	if (console_thread_stop ||
319 	    oops_in_progress ||
320 	    system_state == SYSTEM_HALT ||
321 	    system_state == SYSTEM_POWER_OFF ||
322 	    system_state == SYSTEM_RESTART) {
323 		if (!console_thread_stop) {
324 			console_thread_stop = true;
325 			smp_wmb();
326 			debug_flush(pdev);
327 			while (fifo_count-- && kfifo_get(&fifo, &c))
328 				debug_put(pdev, &c, 1);
329 		}
330 		debug_put(pdev, s, count);
331 		debug_flush(pdev);
332 	} else if (count) {
333 		unsigned int ret = 0;
334 
335 		if (kfifo_len(&fifo) + count < FIFO_SIZE)
336 			ret = kfifo_in(&fifo, s, count);
337 		if (!ret) {
338 			console_dropped_messages++;
339 			smp_wmb();
340 		} else {
341 			/*
342 			 * Avoid dead lock on console_task->pi_lock and console_lock
343 			 * when call printk() in try_to_wake_up().
344 			 *
345 			 * cpu0 hold console_lock, then try lock pi_lock fail:
346 			 *   printk()->vprintk_emit()->console_unlock()->try_to_wake_up()
347 			 *   ->lock(pi_lock)->deadlock
348 			 *
349 			 * cpu1 hold pi_lock, then try lock console_lock fail:
350 			 *   console_thread()->console_put()->usleep_range()->run_hrtimer()
351 			 *   ->hrtimer_wakeup()->try_to_wake_up()[hold_pi_lock]->printk()
352 			 *   ->vprintk_emit()->console_trylock_spining()->cpu_relax()->deadlock
353 			 *
354 			 * if cpu0 does not hold console_lock, cpu1 also deadlock on pi_lock:
355 			 *   ...->hrtimer_wakeup()->try_to_wake_up()[hold_pi_lock]->printk()
356 			 *   ->vprintk_emit()->console_unlock()->try_to_wake_up()
357 			 *   ->lock(pi_lock)->deadlock
358 			 *
359 			 * so when console_task is running on usleep_range(), printk()
360 			 * should not wakeup console_task to avoid lock(pi_lock) again,
361 			 * as run_hrtimer() will wakeup console_task later.
362 			 * console_thread_running==false guarantee that console_task
363 			 * is not running on usleep_range().
364 			 */
365 			if (!READ_ONCE(console_thread_running))
366 				wake_up_process(t->console_task);
367 		}
368 	}
369 }
370 #endif
371 
372 
fiq_enable(struct platform_device * pdev,unsigned int irq,bool on)373 static void fiq_enable(struct platform_device *pdev, unsigned int irq, bool on)
374 {
375 	if (on)
376 		enable_irq(irq);
377 	else
378 		disable_irq(irq);
379 }
380 
381 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
382 #ifdef CONFIG_ARM_SDE_INTERFACE
383 #include <linux/arm_sdei.h>
384 #include <asm/smp_plat.h>
385 #include <linux/suspend.h>
386 void fiq_debugger_fiq_get_(const char *fmt, ...);
387 
388 static struct rk_fiq_sdei_st {
389 	u32 cur_cpu;
390 	u32 sw_cpu;
391 	u32 cpu_can_sw;
392 	int fiq_en;
393 	u32 event_id;
394 	u32 cpu_off_sw;
395 	u32 cpu_sw_event_id;
396 } rk_fiq_sdei;
397 
sdei_fiq_debugger_is_enabled(void)398 int sdei_fiq_debugger_is_enabled(void)
399 {
400 	return rk_fiq_sdei.fiq_en;
401 }
402 
fiq_sdei_event_callback(u32 event,struct pt_regs * regs,void * arg)403 int fiq_sdei_event_callback(u32 event, struct pt_regs *regs, void *arg)
404 {
405 	int cpu_id = get_logical_index(read_cpuid_mpidr() &
406 				       MPIDR_HWID_BITMASK);
407 	fiq_debugger_fiq(regs, cpu_id);
408 
409 	return 0;
410 }
411 
rk_fiq_sdei_event_sw_cpu(int wait_disable)412 void rk_fiq_sdei_event_sw_cpu(int wait_disable)
413 {
414 	unsigned long affinity;
415 	int cnt = 100000;
416 	int ret = 0;
417 
418 	do {
419 		ret = sdei_event_disable_nolock(rk_fiq_sdei.event_id);
420 		if (!ret)
421 			break;
422 		cnt--;
423 		udelay(20);
424 	} while (wait_disable && cnt);
425 
426 	affinity = cpu_logical_map(rk_fiq_sdei.sw_cpu) & MPIDR_HWID_BITMASK;
427 	ret = sdei_event_routing_set_nolock(rk_fiq_sdei.event_id,
428 					    SDEI_EVENT_REGISTER_RM_PE,
429 					    affinity);
430 	ret = sdei_event_enable_nolock(rk_fiq_sdei.event_id);
431 	rk_fiq_sdei.cur_cpu = rk_fiq_sdei.sw_cpu;
432 }
433 
fiq_sdei_sw_cpu_event_callback(u32 event,struct pt_regs * regs,void * arg)434 int fiq_sdei_sw_cpu_event_callback(u32 event, struct pt_regs *regs, void *arg)
435 {
436 	int cnt = 10000;
437 	int ret = 0;
438 	int cpu_id = event - rk_fiq_sdei.cpu_sw_event_id;
439 
440 	WARN_ON(cpu_id !=
441 		get_logical_index(read_cpuid_mpidr() & MPIDR_HWID_BITMASK));
442 
443 	if (cpu_id == rk_fiq_sdei.sw_cpu) {
444 		if (!rk_fiq_sdei.cpu_off_sw) {
445 			rk_fiq_sdei.cpu_can_sw = 1;
446 		} else {
447 			rk_fiq_sdei_event_sw_cpu(1);
448 			rk_fiq_sdei.cpu_off_sw = 0;
449 		}
450 	} else if (cpu_id == rk_fiq_sdei.cur_cpu && !rk_fiq_sdei.cpu_off_sw) {
451 		while (!rk_fiq_sdei.cpu_can_sw && cnt) {
452 			udelay(10);
453 			cnt--;
454 		};
455 
456 		if (rk_fiq_sdei.cpu_can_sw) {
457 			rk_fiq_sdei_event_sw_cpu(0);
458 			rk_fiq_sdei.cpu_can_sw = 0;
459 		}
460 	}
461 	return ret;
462 }
463 
_rk_fiq_dbg_sdei_switch_cpu(unsigned int cpu,int cpu_off)464 static void _rk_fiq_dbg_sdei_switch_cpu(unsigned int cpu, int cpu_off)
465 {
466 	if (cpu == rk_fiq_sdei.cur_cpu)
467 		return;
468 	rk_fiq_sdei.sw_cpu = cpu;
469 	rk_fiq_sdei.cpu_can_sw = 0;
470 	rk_fiq_sdei.cpu_off_sw = cpu_off;
471 	sip_fiq_debugger_sdei_switch_cpu(rk_fiq_sdei.cur_cpu, cpu, cpu_off);
472 }
473 
rk_fiq_dbg_sdei_switch_cpu(struct platform_device * pdev,unsigned int cpu)474 static void rk_fiq_dbg_sdei_switch_cpu(struct platform_device *pdev,
475 				       unsigned int cpu)
476 {
477 	_rk_fiq_dbg_sdei_switch_cpu(cpu, 0);
478 }
479 
fiq_dbg_sdei_cpu_off_migrate_fiq(unsigned int cpu)480 static int fiq_dbg_sdei_cpu_off_migrate_fiq(unsigned int cpu)
481 {
482 	unsigned int target_cpu;
483 	int cnt = 10000;
484 
485 	if (rk_fiq_sdei.cur_cpu == cpu) {
486 		target_cpu = cpumask_first(cpu_online_mask);
487 		_rk_fiq_dbg_sdei_switch_cpu(target_cpu, 1);
488 
489 		while (rk_fiq_sdei.cur_cpu == cpu && cnt) {
490 			udelay(10);
491 			cnt--;
492 		};
493 		if (!cnt)
494 			pr_err("%s: from %d to %d err!\n",
495 			       __func__, cpu, target_cpu);
496 	}
497 
498 	return 0;
499 }
500 
fiq_dbg_sdei_pm_callback(struct notifier_block * nb,unsigned long mode,void * _unused)501 static int fiq_dbg_sdei_pm_callback(struct notifier_block *nb,
502 				    unsigned long mode, void *_unused)
503 {
504 	unsigned int target_cpu;
505 
506 	switch (mode) {
507 	case PM_SUSPEND_PREPARE:
508 		target_cpu = cpumask_first(cpu_online_mask);
509 		if (target_cpu != 0)
510 			pr_err("%s: fiq for core !\n", __func__);
511 		else
512 			_rk_fiq_dbg_sdei_switch_cpu(target_cpu, 1);
513 		break;
514 	default:
515 	break;
516 	}
517 	return 0;
518 }
519 
520 static struct notifier_block fiq_dbg_sdei_pm_nb = {
521 	.notifier_call = fiq_dbg_sdei_pm_callback,
522 };
523 
fiq_debugger_sdei_enable(struct rk_fiq_debugger * t)524 static int fiq_debugger_sdei_enable(struct rk_fiq_debugger *t)
525 {
526 	int ret, cpu, i;
527 
528 	ret = sip_fiq_debugger_sdei_get_event_id(&rk_fiq_sdei.event_id,
529 						 &rk_fiq_sdei.cpu_sw_event_id,
530 						 NULL);
531 
532 	if (ret) {
533 		pr_err("%s: get event id error!\n", __func__);
534 		return ret;
535 	}
536 
537 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
538 					"soc/rk_sdei_fiq_debugger",
539 					NULL,
540 					fiq_dbg_sdei_cpu_off_migrate_fiq);
541 	if (ret < 0) {
542 		pr_err("%s: cpuhp_setup_state_nocalls error! %d\n",
543 		       __func__, ret);
544 		return ret;
545 	}
546 
547 	if (register_pm_notifier(&fiq_dbg_sdei_pm_nb)) {
548 		pr_err("%s: register pm notify error: %d!\n", __func__, ret);
549 		return ret;
550 	}
551 
552 	ret = sdei_event_register(rk_fiq_sdei.event_id,
553 				  fiq_sdei_event_callback, NULL);
554 
555 	if (ret) {
556 		pr_err("%s: sdei_event_register error!\n", __func__);
557 		unregister_pm_notifier(&fiq_dbg_sdei_pm_nb);
558 		return ret;
559 	}
560 
561 	rk_fiq_sdei.cur_cpu = 0;
562 
563 	ret = sdei_event_routing_set(rk_fiq_sdei.event_id,
564 				     SDEI_EVENT_REGISTER_RM_PE,
565 				     cpu_logical_map(rk_fiq_sdei.cur_cpu));
566 
567 	if (ret) {
568 		pr_err("%s: sdei_event_routing_set error!\n", __func__);
569 		goto err;
570 	}
571 
572 	ret = sdei_event_enable(rk_fiq_sdei.event_id);
573 	if (ret) {
574 		pr_err("%s: sdei_event_enable error!\n", __func__);
575 		goto err;
576 	}
577 
578 	for (cpu = 0; cpu < num_possible_cpus(); cpu++) {
579 		ret = sdei_event_register(rk_fiq_sdei.cpu_sw_event_id + cpu,
580 					  fiq_sdei_sw_cpu_event_callback,
581 					  NULL);
582 		if (ret) {
583 			pr_err("%s: cpu %d sdei_event_register error!\n",
584 			       __func__, cpu);
585 			goto cpu_sw_err;
586 		}
587 		ret = sdei_event_routing_set(rk_fiq_sdei.cpu_sw_event_id + cpu,
588 					     SDEI_EVENT_REGISTER_RM_PE,
589 					     cpu_logical_map(cpu));
590 
591 		if (ret) {
592 			pr_err("%s:cpu %d fiq_sdei_event_routing_set error!\n",
593 			       __func__, cpu);
594 			goto cpu_sw_err;
595 		}
596 
597 		ret = sdei_event_enable(rk_fiq_sdei.cpu_sw_event_id + cpu);
598 		if (ret) {
599 			pr_err("%s: cpu %d sdei_event_enable error!\n",
600 			       __func__, cpu);
601 			goto cpu_sw_err;
602 		}
603 	}
604 
605 	t->pdata.switch_cpu = rk_fiq_dbg_sdei_switch_cpu;
606 	rk_fiq_sdei.fiq_en = 1;
607 	return 0;
608 cpu_sw_err:
609 	for (i = 0; i < cpu; i++)
610 		sdei_event_unregister(rk_fiq_sdei.cpu_sw_event_id + i);
611 err:
612 	unregister_pm_notifier(&fiq_dbg_sdei_pm_nb);
613 	sdei_event_unregister(rk_fiq_sdei.event_id);
614 
615 	return ret;
616 }
617 
618 #else
fiq_debugger_sdei_enable(struct rk_fiq_debugger * t)619 static inline int fiq_debugger_sdei_enable(struct rk_fiq_debugger *t)
620 {
621 	return -EINVAL;
622 }
623 #endif
624 
625 static struct pt_regs fiq_pt_regs;
626 
rk_fiq_debugger_switch_cpu(struct platform_device * pdev,unsigned int cpu)627 static void rk_fiq_debugger_switch_cpu(struct platform_device *pdev,
628 				       unsigned int cpu)
629 {
630 	sip_fiq_debugger_switch_cpu(cpu);
631 }
632 
rk_fiq_debugger_enable_debug(struct platform_device * pdev,bool val)633 static void rk_fiq_debugger_enable_debug(struct platform_device *pdev, bool val)
634 {
635 	sip_fiq_debugger_enable_debug(val);
636 }
637 
fiq_debugger_uart_irq_tf(struct pt_regs _pt_regs,u64 cpu)638 static void fiq_debugger_uart_irq_tf(struct pt_regs _pt_regs, u64 cpu)
639 {
640 	fiq_pt_regs = _pt_regs;
641 
642 	fiq_debugger_fiq(&fiq_pt_regs, cpu);
643 }
644 
rk_fiq_debugger_uart_dev_resume(struct platform_device * pdev)645 static int rk_fiq_debugger_uart_dev_resume(struct platform_device *pdev)
646 {
647 	struct rk_fiq_debugger *t;
648 
649 	t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
650 	sip_fiq_debugger_uart_irq_tf_init(serial_hwirq,
651 					  fiq_debugger_uart_irq_tf);
652 	return 0;
653 }
654 
655 /*
656  * We don't need to migrate fiq before cpuidle, because EL3 can promise to
657  * resume all fiq configure. We don't want fiq to break kernel cpu_resume(),
658  * so that fiq would be disabled in EL3 on purpose when cpu resume. We enable
659  * it here since everything is okay.
660  */
fiq_debugger_cpuidle_resume_fiq(struct notifier_block * nb,unsigned long action,void * hcpu)661 static int fiq_debugger_cpuidle_resume_fiq(struct notifier_block *nb,
662 					   unsigned long action, void *hcpu)
663 {
664 	switch (action) {
665 	case CPU_PM_EXIT:
666 		if ((sip_fiq_debugger_is_enabled()) &&
667 		    (sip_fiq_debugger_get_target_cpu() == smp_processor_id()))
668 			sip_fiq_debugger_enable_fiq(true, smp_processor_id());
669 		break;
670 	default:
671 		break;
672 	}
673 
674 	return NOTIFY_OK;
675 }
676 
677 /*
678  * We must migrate fiq before cpu offline, because EL3 doesn't promise to
679  * resume all fiq configure at this sisutation. Here, we migrate fiq to any
680  * online cpu.
681  */
fiq_debugger_cpu_offine_migrate_fiq(unsigned int cpu)682 static int fiq_debugger_cpu_offine_migrate_fiq(unsigned int cpu)
683 {
684 	unsigned int target_cpu;
685 
686 	if ((sip_fiq_debugger_is_enabled()) &&
687 	    (sip_fiq_debugger_get_target_cpu() == cpu)) {
688 		target_cpu = cpumask_first(cpu_online_mask);
689 		sip_fiq_debugger_switch_cpu(target_cpu);
690 	}
691 
692 	return 0;
693 }
694 
695 static struct notifier_block fiq_debugger_pm_notifier = {
696 	.notifier_call = fiq_debugger_cpuidle_resume_fiq,
697 	.priority = 100,
698 };
699 
rk_fiq_debugger_register_cpu_pm_notify(void)700 static int rk_fiq_debugger_register_cpu_pm_notify(void)
701 {
702 	int err;
703 
704 	err = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
705 					"soc/rk_fiq_debugger",
706 					NULL,
707 					fiq_debugger_cpu_offine_migrate_fiq);
708 	if (err < 0) {
709 		pr_err("fiq debugger register cpu notifier failed!\n");
710 		return err;
711 	}
712 
713 	err = cpu_pm_register_notifier(&fiq_debugger_pm_notifier);
714 	if (err) {
715 		pr_err("fiq debugger register pm notifier failed!\n");
716 		return err;
717 	}
718 
719 	return 0;
720 }
721 
fiq_debugger_bind_sip_smc(struct rk_fiq_debugger * t,phys_addr_t phy_base,int hwirq,int signal_irq,unsigned int baudrate)722 static int fiq_debugger_bind_sip_smc(struct rk_fiq_debugger *t,
723 				     phys_addr_t phy_base,
724 				     int hwirq,
725 				     int signal_irq,
726 				     unsigned int baudrate)
727 {
728 	int err;
729 
730 	err = sip_fiq_debugger_request_share_memory();
731 	if (err) {
732 		pr_err("fiq debugger request share memory failed: %d\n", err);
733 		goto exit;
734 	}
735 
736 	err = rk_fiq_debugger_register_cpu_pm_notify();
737 	if (err) {
738 		pr_err("fiq debugger register cpu pm notify failed: %d\n", err);
739 		goto exit;
740 	}
741 
742 	err = sip_fiq_debugger_uart_irq_tf_init(hwirq,
743 				fiq_debugger_uart_irq_tf);
744 	if (err) {
745 		pr_err("fiq debugger bind fiq to trustzone failed: %d\n", err);
746 		goto exit;
747 	}
748 
749 	t->pdata.uart_dev_resume = rk_fiq_debugger_uart_dev_resume;
750 	t->pdata.switch_cpu = rk_fiq_debugger_switch_cpu;
751 	t->pdata.enable_debug = rk_fiq_debugger_enable_debug;
752 	sip_fiq_debugger_set_print_port(phy_base, baudrate);
753 
754 	pr_info("fiq debugger fiq mode enabled\n");
755 
756 	return 0;
757 
758 exit:
759 	t->pdata.switch_cpu = NULL;
760 	t->pdata.enable_debug = NULL;
761 
762 	return err;
763 }
764 #endif
765 
rk_serial_debug_init(void __iomem * base,phys_addr_t phy_base,int irq,int signal_irq,int wakeup_irq,unsigned int baudrate)766 void rk_serial_debug_init(void __iomem *base, phys_addr_t phy_base,
767 			  int irq, int signal_irq,
768 			  int wakeup_irq, unsigned int baudrate)
769 {
770 	struct rk_fiq_debugger *t = NULL;
771 	struct platform_device *pdev = NULL;
772 	struct resource *res = NULL;
773 	int res_count = 0;
774 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
775 	int ret = 0;
776 #endif
777 
778 	if (!base) {
779 		pr_err("Invalid fiq debugger uart base\n");
780 		return;
781 	}
782 
783 	t = kzalloc(sizeof(struct rk_fiq_debugger), GFP_KERNEL);
784 	if (!t) {
785 		pr_err("Failed to allocate for fiq debugger\n");
786 		return;
787 	}
788 
789 	t->irq = irq;
790 	t->baudrate = baudrate;
791 	t->pdata.uart_init = debug_port_init;
792 	t->pdata.uart_getc = debug_getc;
793 	t->pdata.uart_putc = debug_putc;
794 #ifndef CONFIG_RK_CONSOLE_THREAD
795 	t->pdata.uart_flush = debug_flush;
796 #endif
797 	t->pdata.fiq_enable = fiq_enable;
798 	t->pdata.force_irq = NULL;
799 	t->debug_port_base = base;
800 
801 	res = kzalloc(sizeof(struct resource) * 3, GFP_KERNEL);
802 	if (!res) {
803 		pr_err("Failed to alloc fiq debugger resources\n");
804 		goto out2;
805 	}
806 
807 	pdev = kzalloc(sizeof(struct platform_device), GFP_KERNEL);
808 	if (!pdev) {
809 		pr_err("Failed to alloc fiq debugger platform device\n");
810 		goto out3;
811 	}
812 
813 	/* clear busy interrupt, make sure all interrupts are disabled */
814 	rk_fiq_read(t, UART_USR);
815 #ifdef CONFIG_FIQ_DEBUGGER_TRUST_ZONE
816 	if ((signal_irq > 0) && (serial_hwirq > 0)) {
817 		ret = fiq_debugger_sdei_enable(t);
818 		if (ret)
819 			ret = fiq_debugger_bind_sip_smc(t, phy_base,
820 							serial_hwirq,
821 							signal_irq, baudrate);
822 		if (ret)
823 			tf_fiq_sup = false;
824 		else
825 			tf_fiq_sup = true;
826 	}
827 #endif
828 
829 	if (irq > 0) {
830 		res[0].flags = IORESOURCE_IRQ;
831 		res[0].start = irq;
832 		res[0].end = irq;
833 #if defined(CONFIG_FIQ_GLUE)
834 		if (signal_irq > 0)
835 			res[0].name = "fiq";
836 		else
837 			res[0].name = "uart_irq";
838 #elif defined(CONFIG_FIQ_DEBUGGER_TRUST_ZONE)
839 		if (tf_fiq_sup && (signal_irq > 0))
840 			res[0].name = "fiq";
841 		else
842 			res[0].name = "uart_irq";
843 #else
844 		res[0].name = "uart_irq";
845 #endif
846 		res_count++;
847 	}
848 
849 	if (signal_irq > 0) {
850 		res[1].flags = IORESOURCE_IRQ;
851 		res[1].start = signal_irq;
852 		res[1].end = signal_irq;
853 		res[1].name = "signal";
854 		res_count++;
855 	}
856 
857 	if (wakeup_irq > 0) {
858 		res[2].flags = IORESOURCE_IRQ;
859 		res[2].start = wakeup_irq;
860 		res[2].end = wakeup_irq;
861 		res[2].name = "wakeup";
862 		res_count++;
863 	}
864 
865 #ifdef CONFIG_RK_CONSOLE_THREAD
866 	t->console_task = kthread_run(console_thread, pdev, "kconsole");
867 	if (!IS_ERR(t->console_task))
868 		t->pdata.console_write = console_write;
869 #endif
870 
871 	pdev->name = "fiq_debugger";
872 	pdev->id = rk_fiq_debugger_id++;
873 	pdev->dev.platform_data = &t->pdata;
874 	pdev->resource = res;
875 	pdev->num_resources = res_count;
876 	if (platform_device_register(pdev)) {
877 		pr_err("Failed to register fiq debugger\n");
878 		goto out4;
879 	}
880 	return;
881 
882 out4:
883 	kfree(pdev);
884 out3:
885 	kfree(res);
886 out2:
887 	kfree(t);
888 }
889 
rk_serial_debug_init_dummy(void)890 void rk_serial_debug_init_dummy(void)
891 {
892 	struct rk_fiq_debugger *t = NULL;
893 	struct platform_device *pdev = NULL;
894 
895 	t = kzalloc(sizeof(*t), GFP_KERNEL);
896 	if (!t) {
897 		pr_err("Failed to allocate for fiq debugger\n");
898 		return;
899 	}
900 
901 	t->pdata.uart_getc = debug_getc_dummy;
902 	t->pdata.uart_putc = debug_putc_dummy;
903 
904 	pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
905 	if (!pdev) {
906 		pr_err("Failed to alloc fiq debugger platform device\n");
907 		goto out2;
908 	}
909 
910 	pdev->name = "fiq_debugger";
911 	pdev->id = rk_fiq_debugger_id++;
912 	pdev->dev.platform_data = &t->pdata;
913 	if (platform_device_register(pdev)) {
914 		pr_err("Failed to register fiq debugger\n");
915 		goto out3;
916 	}
917 	return;
918 
919 out3:
920 	kfree(pdev);
921 out2:
922 	kfree(t);
923 }
924 
925 #if defined(CONFIG_OF)
926 static const struct of_device_id rk_fiqdbg_of_match[] = {
927 	{ .compatible = "rockchip,fiq-debugger", },
928 	{},
929 };
930 MODULE_DEVICE_TABLE(of, rk_fiqdbg_of_match);
931 #endif
932 
rk_fiqdbg_probe(struct platform_device * pdev)933 static int __init rk_fiqdbg_probe(struct platform_device *pdev)
934 {
935 	void __iomem *base;
936 	struct device_node *np = pdev->dev.of_node;
937 	unsigned int id, ok = 0;
938 	int irq, signal_irq = -1, wake_irq = -1;
939 	unsigned int baudrate = 0, irq_mode = 0;
940 	phys_addr_t phy_base = 0;
941 	int serial_id;
942 	struct clk *clk;
943 	struct clk *pclk;
944 	struct of_phandle_args oirq;
945 	struct resource res;
946 
947 	if (!of_device_is_available(np)) {
948 		pr_err("fiq-debugger is disabled in device tree\n");
949 		return -ENODEV;
950 	}
951 
952 	if (of_property_read_u32(np, "rockchip,serial-id", &serial_id))
953 		return -EINVAL;
954 
955 	if (serial_id == -1) {
956 		rk_serial_debug_init_dummy();
957 		return 0;
958 	}
959 
960 	if (of_property_read_u32(np, "rockchip,irq-mode-enable", &irq_mode))
961 		irq_mode = -1;
962 
963 	signal_irq = irq_of_parse_and_map(np, 0);
964 	if (!signal_irq)
965 		return -EINVAL;
966 
967 	if (of_property_read_u32(np, "rockchip,wake-irq", &wake_irq))
968 		wake_irq = -1;
969 
970 	if (of_property_read_u32(np, "rockchip,baudrate", &baudrate))
971 		baudrate = -1;
972 
973 	np = NULL;
974 
975 	do {
976 		np = of_find_node_by_name(np, "serial");
977 		if (np) {
978 			id = of_alias_get_id(np, "serial");
979 			if (id == serial_id) {
980 				ok = 1;
981 				break;
982 			}
983 		}
984 	} while(np);
985 
986 	if (!ok)
987 		return -EINVAL;
988 
989 	if (of_device_is_available(np)) {
990 		pr_err("uart%d is enabled, please disable it\n", serial_id);
991 		return -EINVAL;
992 	}
993 
994 	/* parse serial hw irq */
995 	if (irq_mode != 1 && !of_irq_parse_one(np, 0, &oirq))
996 		serial_hwirq = oirq.args[1] + 32;
997 
998 	/* parse serial phy base address */
999 	if (!of_address_to_resource(np, 0, &res))
1000 		phy_base = res.start;
1001 
1002 	pclk = of_clk_get_by_name(np, "apb_pclk");
1003 	clk = of_clk_get_by_name(np, "baudclk");
1004 	if (unlikely(IS_ERR(clk)) || unlikely(IS_ERR(pclk))) {
1005 		pr_err("fiq-debugger get clock fail\n");
1006 		return -EINVAL;
1007 	}
1008 
1009 	clk_prepare_enable(clk);
1010 	clk_prepare_enable(pclk);
1011 
1012 	irq = irq_of_parse_and_map(np, 0);
1013 	if (!irq)
1014 		return -EINVAL;
1015 
1016 	base = of_iomap(np, 0);
1017 	if (base)
1018 		rk_serial_debug_init(base, phy_base,
1019 				     irq, signal_irq, wake_irq, baudrate);
1020 	return 0;
1021 }
1022 
1023 static struct platform_driver rk_fiqdbg_driver = {
1024 	.driver = {
1025 		.name   = "rk-fiq-debugger",
1026 		.of_match_table = of_match_ptr(rk_fiqdbg_of_match),
1027 	},
1028 };
1029 
rk_fiqdbg_init(void)1030 static int __init rk_fiqdbg_init(void)
1031 {
1032 	return platform_driver_probe(&rk_fiqdbg_driver,
1033 				     rk_fiqdbg_probe);
1034 }
1035 
1036 #if defined(CONFIG_FIQ_DEBUGGER_TRUST_ZONE) && defined(CONFIG_ARM_SDE_INTERFACE)
1037 fs_initcall(rk_fiqdbg_init);
1038 #else
1039 subsys_initcall(rk_fiqdbg_init); /* after of_platform_default_populate_init */
1040 #endif
1041 
rk_fiqdbg_exit(void)1042 static void __exit rk_fiqdbg_exit(void)
1043 {
1044 	platform_driver_unregister(&rk_fiqdbg_driver);
1045 }
1046 module_exit(rk_fiqdbg_exit);
1047 
1048 MODULE_AUTHOR("Huibin Hong <huibin.hong@rock-chips.com>");
1049 MODULE_DESCRIPTION("Rockchip FIQ Debugger");
1050 MODULE_LICENSE("GPL");
1051 MODULE_ALIAS("platform:rk-fiq-debugger");
1052