1 /**************************************************************************//**
3 * @brief Interrupt controller handling implementation for GIC
6 ******************************************************************************/
8 * Copyright (c) 2017 ARM Limited. All rights reserved.
10 * SPDX-License-Identifier: Apache-2.0
12 * Licensed under the Apache License, Version 2.0 (the License); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
27 #include "RTE_Components.h"
28 #include CMSIS_device_header
32 #if defined(__GIC_PRESENT) && (__GIC_PRESENT == 1U)
34 /// Number of implemented interrupt lines
35 #ifndef IRQ_GIC_LINE_COUNT
36 #define IRQ_GIC_LINE_COUNT (1020U)
39 static IRQHandler_t IRQTable[IRQ_GIC_LINE_COUNT] = { 0U };
40 static uint32_t IRQ_ID0;
42 /// Initialize interrupt controller.
43 __WEAK int32_t IRQ_Initialize (void) {
46 for (i = 0U; i < IRQ_GIC_LINE_COUNT; i++) {
47 IRQTable[i] = (IRQHandler_t)NULL;
54 /// Register interrupt handler.
55 __WEAK int32_t IRQ_SetHandler (IRQn_ID_t irqn, IRQHandler_t handler) {
58 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
59 IRQTable[irqn] = handler;
69 /// Get the registered interrupt handler.
70 __WEAK IRQHandler_t IRQ_GetHandler (IRQn_ID_t irqn) {
73 // Ignore CPUID field (software generated interrupts)
76 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
87 __WEAK int32_t IRQ_Enable (IRQn_ID_t irqn) {
90 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
91 GIC_EnableIRQ ((IRQn_Type)irqn);
101 /// Disable interrupt.
102 __WEAK int32_t IRQ_Disable (IRQn_ID_t irqn) {
105 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
106 GIC_DisableIRQ ((IRQn_Type)irqn);
116 /// Get interrupt enable state.
117 __WEAK uint32_t IRQ_GetEnableState (IRQn_ID_t irqn) {
120 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
121 enable = GIC_GetEnableIRQ((IRQn_Type)irqn);
130 /// Configure interrupt request mode.
131 __WEAK int32_t IRQ_SetMode (IRQn_ID_t irqn, uint32_t mode) {
138 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
139 // Check triggering mode
140 val = (mode & IRQ_MODE_TRIG_Msk);
142 if (val == IRQ_MODE_TRIG_LEVEL) {
144 } else if (val == IRQ_MODE_TRIG_EDGE) {
151 // Check interrupt type
152 val = mode & IRQ_MODE_TYPE_Msk;
154 if (val != IRQ_MODE_TYPE_IRQ) {
158 // Check interrupt domain
159 val = mode & IRQ_MODE_DOMAIN_Msk;
161 if (val == IRQ_MODE_DOMAIN_NONSECURE) {
164 // Check security extensions support
165 val = GIC_DistributorInfo() & (1UL << 10U);
168 // Security extensions are supported
176 // Check interrupt CPU targets
177 val = mode & IRQ_MODE_CPU_Msk;
179 if (val == IRQ_MODE_CPU_ALL) {
182 cpu = val >> IRQ_MODE_CPU_Pos;
185 // Apply configuration if no mode error
187 GIC_SetConfiguration((IRQn_Type)irqn, cfg);
188 GIC_SetTarget ((IRQn_Type)irqn, cpu);
191 GIC_SetGroup ((IRQn_Type)irqn, secure);
200 /// Get interrupt mode configuration.
201 __WEAK uint32_t IRQ_GetMode (IRQn_ID_t irqn) {
205 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
206 mode = IRQ_MODE_TYPE_IRQ;
209 val = GIC_GetConfiguration((IRQn_Type)irqn);
211 if ((val & 2U) != 0U) {
212 // Corresponding interrupt is edge triggered
213 mode |= IRQ_MODE_TRIG_EDGE;
215 // Corresponding interrupt is level triggered
216 mode |= IRQ_MODE_TRIG_LEVEL;
219 // Get interrupt CPU targets
220 mode |= GIC_GetTarget ((IRQn_Type)irqn) << IRQ_MODE_CPU_Pos;
223 mode = IRQ_MODE_ERROR;
230 /// Get ID number of current interrupt request (IRQ).
231 __WEAK IRQn_ID_t IRQ_GetActiveIRQ (void) {
235 /* Dummy read to avoid GIC 390 errata 801120 */
236 GIC_GetHighPendingIRQ();
238 irqn = GIC_AcknowledgePending();
242 /* Workaround GIC 390 errata 733075 (GIC-390_Errata_Notice_v6.pdf, 09-Jul-2014) */
243 /* The following workaround code is for a single-core system. It would be */
244 /* different in a multi-core system. */
245 /* If the ID is 0 or 0x3FE or 0x3FF, then the GIC CPU interface may be locked-up */
246 /* so unlock it, otherwise service the interrupt as normal. */
247 /* Special IDs 1020=0x3FC and 1021=0x3FD are reserved values in GICv1 and GICv2 */
248 /* so will not occur here. */
250 if ((irqn == 0) || (irqn >= 0x3FE)) {
251 /* Unlock the CPU interface with a dummy write to Interrupt Priority Register */
252 prio = GIC_GetPriority((IRQn_Type)0);
253 GIC_SetPriority ((IRQn_Type)0, prio);
257 if ((irqn == 0U) && ((GIC_GetIRQStatus ((IRQn_Type)irqn) & 1U) != 0U) && (IRQ_ID0 == 0U)) {
258 /* If the ID is 0, is active and has not been seen before */
261 /* End of Workaround GIC 390 errata 733075 */
268 /// Get ID number of current fast interrupt request (FIQ).
269 __WEAK IRQn_ID_t IRQ_GetActiveFIQ (void) {
270 return ((IRQn_ID_t)-1);
274 /// Signal end of interrupt processing.
275 __WEAK int32_t IRQ_EndOfInterrupt (IRQn_ID_t irqn) {
277 IRQn_Type irq = (IRQn_Type)irqn;
281 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
282 GIC_EndInterrupt (irq);
297 /// Set interrupt pending flag.
298 __WEAK int32_t IRQ_SetPending (IRQn_ID_t irqn) {
301 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
302 GIC_SetPendingIRQ ((IRQn_Type)irqn);
311 /// Get interrupt pending flag.
312 __WEAK uint32_t IRQ_GetPending (IRQn_ID_t irqn) {
315 if ((irqn >= 16) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
316 pending = GIC_GetPendingIRQ ((IRQn_Type)irqn);
321 return (pending & 1U);
325 /// Clear interrupt pending flag.
326 __WEAK int32_t IRQ_ClearPending (IRQn_ID_t irqn) {
329 if ((irqn >= 16) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
330 GIC_ClearPendingIRQ ((IRQn_Type)irqn);
340 /// Set interrupt priority value.
341 __WEAK int32_t IRQ_SetPriority (IRQn_ID_t irqn, uint32_t priority) {
344 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
345 GIC_SetPriority ((IRQn_Type)irqn, priority);
355 /// Get interrupt priority.
356 __WEAK uint32_t IRQ_GetPriority (IRQn_ID_t irqn) {
359 if ((irqn >= 0) && (irqn < (IRQn_ID_t)IRQ_GIC_LINE_COUNT)) {
360 priority = GIC_GetPriority ((IRQn_Type)irqn);
362 priority = IRQ_PRIORITY_ERROR;
369 /// Set priority masking threshold.
370 __WEAK int32_t IRQ_SetPriorityMask (uint32_t priority) {
371 GIC_SetInterfacePriorityMask (priority);
376 /// Get priority masking threshold
377 __WEAK uint32_t IRQ_GetPriorityMask (void) {
378 return GIC_GetInterfacePriorityMask();
382 /// Set priority grouping field split point
383 __WEAK int32_t IRQ_SetPriorityGroupBits (uint32_t bits) {
386 if (bits == IRQ_PRIORITY_Msk) {
391 GIC_SetBinaryPoint (7U - bits);
401 /// Get priority grouping field split point
402 __WEAK uint32_t IRQ_GetPriorityGroupBits (void) {
405 bp = GIC_GetBinaryPoint() & 0x07U;