1 /*
2  * Copyright (c) 2015 Intel corporation
3  * Copyright (c) 2022 Synopsys
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @file Software interrupts utility code - ARC implementation
10  */
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/irq_offload.h>
14 #include <zephyr/init.h>
15 
16 /* Choose a reasonable default for interrupt line which is used for irq_offload with the option
17  * to override it by setting interrupt line via device tree.
18  */
19 #if DT_NODE_EXISTS(DT_NODELABEL(test_irq_offload_line_0))
20 #define IRQ_OFFLOAD_LINE	DT_IRQN(DT_NODELABEL(test_irq_offload_line_0))
21 #else
22 /* Last two lined are already used in the IRQ tests, so we choose 3rd from the end line */
23 #define IRQ_OFFLOAD_LINE	(CONFIG_NUM_IRQS - 3)
24 #endif
25 
26 #define IRQ_OFFLOAD_PRIO	0
27 
28 #define CURR_CPU (IS_ENABLED(CONFIG_SMP) ? arch_curr_cpu()->id : 0)
29 
30 static struct {
31 	volatile irq_offload_routine_t fn;
32 	const void *volatile arg;
33 } offload_params[CONFIG_MP_MAX_NUM_CPUS];
34 
arc_irq_offload_handler(const void * unused)35 static void arc_irq_offload_handler(const void *unused)
36 {
37 	ARG_UNUSED(unused);
38 
39 	offload_params[CURR_CPU].fn(offload_params[CURR_CPU].arg);
40 }
41 
arch_irq_offload(irq_offload_routine_t routine,const void * parameter)42 void arch_irq_offload(irq_offload_routine_t routine, const void *parameter)
43 {
44 	offload_params[CURR_CPU].fn = routine;
45 	offload_params[CURR_CPU].arg = parameter;
46 	compiler_barrier();
47 
48 	z_arc_v2_aux_reg_write(_ARC_V2_AUX_IRQ_HINT, IRQ_OFFLOAD_LINE);
49 
50 	__asm__ volatile("sync");
51 
52 	/* If _current was aborted in the offload routine, we shouldn't be here */
53 	__ASSERT_NO_MSG((_current->base.thread_state & _THREAD_DEAD) == 0);
54 }
55 
56 /* need to be executed on every core in the system */
arc_irq_offload_init(void)57 int arc_irq_offload_init(void)
58 {
59 
60 	IRQ_CONNECT(IRQ_OFFLOAD_LINE, IRQ_OFFLOAD_PRIO, arc_irq_offload_handler, NULL, 0);
61 
62 	/* The line is triggered and controlled with core private interrupt controller,
63 	 * so even in case common (IDU) interrupt line usage on SMP we need to enable it not
64 	 * with generic irq_enable() but via z_arc_v2_irq_unit_int_enable().
65 	 */
66 	z_arc_v2_irq_unit_int_enable(IRQ_OFFLOAD_LINE);
67 
68 	return 0;
69 }
70 
71 SYS_INIT(arc_irq_offload_init, POST_KERNEL, 0);
72