1 /*
2  * Copyright (c) 2014 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief ARCv2 interrupt management
10  *
11  *
12  * Interrupt management:
13  *
14  * - enabling/disabling
15  *
16  * An IRQ number passed to the @a irq parameters found in this file is a
17  * number from 16 to last IRQ number on the platform.
18  */
19 
20 #include <kernel.h>
21 #include <arch/cpu.h>
22 #include <sys/__assert.h>
23 #include <toolchain.h>
24 #include <linker/sections.h>
25 #include <sw_isr_table.h>
26 #include <irq.h>
27 #include <sys/printk.h>
28 
29 
30 /*
31  * storage space for the interrupt stack of fast_irq
32  */
33 #if defined(CONFIG_ARC_FIRQ_STACK)
34 #if defined(CONFIG_SMP)
35 K_KERNEL_STACK_ARRAY_DEFINE(_firq_interrupt_stack, CONFIG_MP_NUM_CPUS,
36 			    CONFIG_ARC_FIRQ_STACK_SIZE);
37 #else
38 K_KERNEL_STACK_DEFINE(_firq_interrupt_stack, CONFIG_ARC_FIRQ_STACK_SIZE);
39 #endif
40 
41 /*
42  * @brief Set the stack pointer for firq handling
43  *
44  * @return N/A
45  */
z_arc_firq_stack_set(void)46 void z_arc_firq_stack_set(void)
47 {
48 #ifdef CONFIG_SMP
49 	char *firq_sp = Z_KERNEL_STACK_BUFFER(
50 		  _firq_interrupt_stack[z_arc_v2_core_id()]) +
51 		  CONFIG_ARC_FIRQ_STACK_SIZE;
52 #else
53 	char *firq_sp = Z_KERNEL_STACK_BUFFER(_firq_interrupt_stack) +
54 		  CONFIG_ARC_FIRQ_STACK_SIZE;
55 #endif
56 
57 /* the z_arc_firq_stack_set must be called when irq diasbled, as
58  * it can be called not only in the init phase but also other places
59  */
60 	unsigned int key = arch_irq_lock();
61 
62 	__asm__ volatile (
63 /* only ilink will not be banked, so use ilink as channel
64  * between 2 banks
65  */
66 	"mov %%ilink, %0\n\t"
67 	"lr %0, [%1]\n\t"
68 	"or %0, %0, %2\n\t"
69 	"kflag %0\n\t"
70 	"mov %%sp, %%ilink\n\t"
71 /* switch back to bank0, use ilink to avoid the pollution of
72  * bank1's gp regs.
73  */
74 	"lr %%ilink, [%1]\n\t"
75 	"and %%ilink, %%ilink, %3\n\t"
76 	"kflag %%ilink\n\t"
77 	:
78 	: "r"(firq_sp), "i"(_ARC_V2_STATUS32),
79 	  "i"(_ARC_V2_STATUS32_RB(1)),
80 	  "i"(~_ARC_V2_STATUS32_RB(7))
81 	);
82 	arch_irq_unlock(key);
83 }
84 #endif
85 
86 /*
87  * @brief Enable an interrupt line
88  *
89  * Clear possible pending interrupts on the line, and enable the interrupt
90  * line. After this call, the CPU will receive interrupts for the specified
91  * @a irq.
92  *
93  * @return N/A
94  */
95 
arch_irq_enable(unsigned int irq)96 void arch_irq_enable(unsigned int irq)
97 {
98 	z_arc_v2_irq_unit_int_enable(irq);
99 }
100 
101 /*
102  * @brief Disable an interrupt line
103  *
104  * Disable an interrupt line. After this call, the CPU will stop receiving
105  * interrupts for the specified @a irq.
106  *
107  * @return N/A
108  */
109 
arch_irq_disable(unsigned int irq)110 void arch_irq_disable(unsigned int irq)
111 {
112 	z_arc_v2_irq_unit_int_disable(irq);
113 }
114 
115 /**
116  * @brief Return IRQ enable state
117  *
118  * @param irq IRQ line
119  * @return interrupt enable state, true or false
120  */
arch_irq_is_enabled(unsigned int irq)121 int arch_irq_is_enabled(unsigned int irq)
122 {
123 	return z_arc_v2_irq_unit_int_enabled(irq);
124 }
125 
126 /*
127  * @internal
128  *
129  * @brief Set an interrupt's priority
130  *
131  * Lower values take priority over higher values. Special case priorities are
132  * expressed via mutually exclusive flags.
133 
134  * The priority is verified if ASSERT_ON is enabled; max priority level
135  * depends on CONFIG_NUM_IRQ_PRIO_LEVELS.
136  *
137  * @return N/A
138  */
139 
z_irq_priority_set(unsigned int irq,unsigned int prio,uint32_t flags)140 void z_irq_priority_set(unsigned int irq, unsigned int prio, uint32_t flags)
141 {
142 	ARG_UNUSED(flags);
143 
144 	__ASSERT(prio < CONFIG_NUM_IRQ_PRIO_LEVELS,
145 		 "invalid priority %d for irq %d", prio, irq);
146 /* 0 -> CONFIG_NUM_IRQ_PRIO_LEVELS allocted to secure world
147  * left prio levels allocated to normal world
148  */
149 #if defined(CONFIG_ARC_SECURE_FIRMWARE)
150 	prio = prio < ARC_N_IRQ_START_LEVEL ?
151 		prio : (ARC_N_IRQ_START_LEVEL - 1);
152 #elif defined(CONFIG_ARC_NORMAL_FIRMWARE)
153 	prio = prio < ARC_N_IRQ_START_LEVEL ?
154 		 ARC_N_IRQ_START_LEVEL : prio;
155 #endif
156 	z_arc_v2_irq_unit_prio_set(irq, prio);
157 }
158 
159 /*
160  * @brief Spurious interrupt handler
161  *
162  * Installed in all dynamic interrupt slots at boot time. Throws an error if
163  * called.
164  *
165  * @return N/A
166  */
167 
z_irq_spurious(const void * unused)168 void z_irq_spurious(const void *unused)
169 {
170 	ARG_UNUSED(unused);
171 	z_fatal_error(K_ERR_SPURIOUS_IRQ, NULL);
172 }
173 
174 #ifdef CONFIG_DYNAMIC_INTERRUPTS
arch_irq_connect_dynamic(unsigned int irq,unsigned int priority,void (* routine)(const void * parameter),const void * parameter,uint32_t flags)175 int arch_irq_connect_dynamic(unsigned int irq, unsigned int priority,
176 			     void (*routine)(const void *parameter),
177 			     const void *parameter, uint32_t flags)
178 {
179 	z_isr_install(irq, routine, parameter);
180 	z_irq_priority_set(irq, priority, flags);
181 	return irq;
182 }
183 #endif /* CONFIG_DYNAMIC_INTERRUPTS */
184