1 /*
2  * Copyright (c) 2013-2014 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief ARM Cortex-M interrupt management
10  *
11  *
12  * Interrupt management: enabling/disabling and dynamic ISR
13  * connecting/replacing.  SW_ISR_TABLE_DYNAMIC has to be enabled for
14  * connecting ISRs at runtime.
15  */
16 
17 #include <zephyr/kernel.h>
18 #include <zephyr/arch/cpu.h>
19 #include <cmsis_core.h>
20 #include <zephyr/sys/__assert.h>
21 #include <zephyr/sys/barrier.h>
22 #include <zephyr/toolchain.h>
23 #include <zephyr/linker/sections.h>
24 #include <zephyr/sw_isr_table.h>
25 #include <zephyr/irq.h>
26 #include <zephyr/tracing/tracing.h>
27 #include <zephyr/pm/pm.h>
28 
29 extern void z_arm_reserved(void);
30 
31 #define NUM_IRQS_PER_REG 32
32 #define REG_FROM_IRQ(irq) (irq / NUM_IRQS_PER_REG)
33 #define BIT_FROM_IRQ(irq) (irq % NUM_IRQS_PER_REG)
34 
35 #if !defined(CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER)
36 
arch_irq_enable(unsigned int irq)37 void arch_irq_enable(unsigned int irq)
38 {
39 	NVIC_EnableIRQ((IRQn_Type)irq);
40 }
41 
arch_irq_disable(unsigned int irq)42 void arch_irq_disable(unsigned int irq)
43 {
44 	NVIC_DisableIRQ((IRQn_Type)irq);
45 }
46 
arch_irq_is_enabled(unsigned int irq)47 int arch_irq_is_enabled(unsigned int irq)
48 {
49 	return NVIC->ISER[REG_FROM_IRQ(irq)] & BIT(BIT_FROM_IRQ(irq));
50 }
51 
52 /**
53  * @internal
54  *
55  * @brief Set an interrupt's priority
56  *
57  * The priority is verified if ASSERT_ON is enabled. The maximum number
58  * of priority levels is a little complex, as there are some hardware
59  * priority levels which are reserved.
60  */
z_arm_irq_priority_set(unsigned int irq,unsigned int prio,uint32_t flags)61 void z_arm_irq_priority_set(unsigned int irq, unsigned int prio, uint32_t flags)
62 {
63 	/* The kernel may reserve some of the highest priority levels.
64 	 * So we offset the requested priority level with the number
65 	 * of priority levels reserved by the kernel.
66 	 */
67 
68 	/* If we have zero latency interrupts, those interrupts will
69 	 * run at a priority level which is not masked by irq_lock().
70 	 * Our policy is to express priority levels with special properties
71 	 * via flags
72 	 */
73 	if (IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) && (flags & IRQ_ZERO_LATENCY)) {
74 		if (ZERO_LATENCY_LEVELS == 1) {
75 			prio = _EXC_ZERO_LATENCY_IRQS_PRIO;
76 		} else {
77 			/* Use caller supplied prio level as-is */
78 		}
79 	} else {
80 		prio += _IRQ_PRIO_OFFSET;
81 	}
82 
83 	/* The last priority level is also used by PendSV exception, but
84 	 * allow other interrupts to use the same level, even if it ends up
85 	 * affecting performance (can still be useful on systems with a
86 	 * reduced set of priorities, like Cortex-M0/M0+).
87 	 */
88 	__ASSERT(prio <= (BIT(NUM_IRQ_PRIO_BITS) - 1),
89 		 "invalid priority %d for %d irq! values must be less than %lu\n",
90 		 prio - _IRQ_PRIO_OFFSET, irq,
91 		 BIT(NUM_IRQ_PRIO_BITS) - (_IRQ_PRIO_OFFSET));
92 	NVIC_SetPriority((IRQn_Type)irq, prio);
93 }
94 
95 #endif /* !defined(CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER) */
96 
97 void z_arm_fatal_error(unsigned int reason, const struct arch_esf *esf);
98 
99 /**
100  *
101  * @brief Spurious interrupt handler
102  *
103  * Installed in all _sw_isr_table slots at boot time. Throws an error if
104  * called.
105  *
106  */
z_irq_spurious(const void * unused)107 void z_irq_spurious(const void *unused)
108 {
109 	ARG_UNUSED(unused);
110 
111 	z_arm_fatal_error(K_ERR_SPURIOUS_IRQ, NULL);
112 }
113 
114 #ifdef CONFIG_PM
_arch_isr_direct_pm(void)115 void _arch_isr_direct_pm(void)
116 {
117 #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
118 	unsigned int key;
119 
120 	/* irq_lock() does what we want for this CPU */
121 	key = irq_lock();
122 #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
123 	/* Lock all interrupts. irq_lock() will on this CPU only disable those
124 	 * lower than BASEPRI, which is not what we want. See comments in
125 	 * arch/arm/core/cortex_m/isr_wrapper.c
126 	 */
127 	__asm__ volatile("cpsid i" : : : "memory");
128 #else
129 #error Unknown ARM architecture
130 #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
131 
132 	if (_kernel.idle) {
133 		_kernel.idle = 0;
134 		pm_system_resume();
135 	}
136 
137 #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
138 	irq_unlock(key);
139 #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
140 	__asm__ volatile("cpsie i" : : : "memory");
141 #else
142 #error Unknown ARM architecture
143 #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
144 
145 }
146 #endif
147 
148 #if defined(CONFIG_ARM_SECURE_FIRMWARE)
149 /**
150  *
151  * @brief Set the target security state for the given IRQ
152  *
153  * Function sets the security state (Secure or Non-Secure) targeted
154  * by the given irq. It requires ARMv8-M MCU.
155  * It is only compiled if ARM_SECURE_FIRMWARE is defined.
156  * It should only be called while in Secure state, otherwise, a write attempt
157  * to NVIC.ITNS register is write-ignored(WI), as the ITNS register is not
158  * banked between security states and, therefore, has no Non-Secure instance.
159  *
160  * It shall return the resulting target state of the given IRQ, indicating
161  * whether the operation has been performed successfully.
162  *
163  * @param irq IRQ line
164  * @param irq_target_state the desired IRQ target state
165  *
166  * @return The resulting target state of the given IRQ
167  */
irq_target_state_set(unsigned int irq,irq_target_state_t irq_target_state)168 irq_target_state_t irq_target_state_set(unsigned int irq,
169 	irq_target_state_t irq_target_state)
170 {
171 	uint32_t result;
172 
173 	if (irq_target_state == IRQ_TARGET_STATE_SECURE) {
174 		/* Set target to Secure */
175 		result = NVIC_ClearTargetState(irq);
176 	} else {
177 		/* Set target to Non-Secure */
178 		result = NVIC_SetTargetState(irq);
179 	}
180 
181 	if (result) {
182 		return IRQ_TARGET_STATE_NON_SECURE;
183 	} else {
184 		return IRQ_TARGET_STATE_SECURE;
185 	}
186 }
187 
188 /**
189  *
190  * @brief Determine whether the given IRQ targets the Secure state
191  *
192  * Function determines whether the given irq targets the Secure state
193  * or not (i.e. targets the Non-Secure state). It requires ARMv8-M MCU.
194  * It is only compiled if ARM_SECURE_FIRMWARE is defined.
195  * It should only be called while in Secure state, otherwise, a read attempt
196  * to NVIC.ITNS register is read-as-zero(RAZ), as the ITNS register is not
197  * banked between security states and, therefore, has no Non-Secure instance.
198  *
199  * @param irq IRQ line
200  *
201  * @return 1 if target state is Secure, 0 otherwise.
202  */
irq_target_state_is_secure(unsigned int irq)203 int irq_target_state_is_secure(unsigned int irq)
204 {
205 	return NVIC_GetTargetState(irq) == 0;
206 }
207 
208 /**
209  *
210  * @brief Disable and set all interrupt lines to target Non-Secure state.
211  *
212  * The function is used to set all HW NVIC interrupt lines to target the
213  * Non-Secure state. The function shall only be called fron Secure state.
214  *
215  * Notes:
216  * - All NVIC interrupts are disabled before being routed to Non-Secure.
217  * - Bits corresponding to un-implemented interrupts are RES0, so writes
218  *   will be ignored.
219  *
220 */
irq_target_state_set_all_non_secure(void)221 void irq_target_state_set_all_non_secure(void)
222 {
223 	int i;
224 
225 	/* Disable (Clear) all NVIC interrupt lines. */
226 	for (i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) {
227 		NVIC->ICER[i] = 0xFFFFFFFF;
228 	}
229 
230 	barrier_dsync_fence_full();
231 	barrier_isync_fence_full();
232 
233 	/* Set all NVIC interrupt lines to target Non-Secure */
234 	for (i = 0; i < sizeof(NVIC->ITNS) / sizeof(NVIC->ITNS[0]); i++) {
235 		NVIC->ITNS[i] = 0xFFFFFFFF;
236 	}
237 }
238 
239 #endif /* CONFIG_ARM_SECURE_FIRMWARE */
240 
241 #ifdef CONFIG_DYNAMIC_INTERRUPTS
242 #ifdef CONFIG_GEN_ISR_TABLES
arch_irq_connect_dynamic(unsigned int irq,unsigned int priority,void (* routine)(const void * parameter),const void * parameter,uint32_t flags)243 int arch_irq_connect_dynamic(unsigned int irq, unsigned int priority,
244 			     void (*routine)(const void *parameter),
245 			     const void *parameter, uint32_t flags)
246 {
247 	z_isr_install(irq, routine, parameter);
248 	z_arm_irq_priority_set(irq, priority, flags);
249 	return irq;
250 }
251 #endif /* CONFIG_GEN_ISR_TABLES */
252 
253 #ifdef CONFIG_DYNAMIC_DIRECT_INTERRUPTS
z_arm_irq_dynamic_direct_isr_dispatch(void)254 static inline void z_arm_irq_dynamic_direct_isr_dispatch(void)
255 {
256 	uint32_t irq = __get_IPSR() - 16;
257 
258 	if (irq < IRQ_TABLE_SIZE) {
259 		struct _isr_table_entry *isr_entry = &_sw_isr_table[irq];
260 
261 		isr_entry->isr(isr_entry->arg);
262 	}
263 }
264 
ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_reschedule)265 ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_reschedule)
266 {
267 	z_arm_irq_dynamic_direct_isr_dispatch();
268 
269 	return 1;
270 }
271 
ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_no_reschedule)272 ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_no_reschedule)
273 {
274 	z_arm_irq_dynamic_direct_isr_dispatch();
275 
276 	return 0;
277 }
278 
279 #endif /* CONFIG_DYNAMIC_DIRECT_INTERRUPTS */
280 
281 #endif /* CONFIG_DYNAMIC_INTERRUPTS */
282