blob: 6a2d090327abf394d729c57811c329b594b7778a [file] [log] [blame]
Soby Mathewf14d1882015-10-26 14:01:53 +00001/*
Madhukar Pappireddyab80cf32023-08-03 12:13:27 -05002 * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
Florian Lugoudcb31ff2021-09-08 12:40:24 +02003 * Portions copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved.
Soby Mathewf14d1882015-10-26 14:01:53 +00004 *
dp-arm82cb2c12017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewf14d1882015-10-26 14:01:53 +00006 */
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00007
Soby Mathewf14d1882015-10-26 14:01:53 +00008#include <assert.h>
Antonio Nino Diaze0ced7a2018-08-21 09:44:43 +01009#include <stdbool.h>
Soby Mathewf14d1882015-10-26 14:01:53 +000010
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000011#include <bl31/interrupt_mgmt.h>
12#include <drivers/arm/gic_common.h>
13#include <drivers/arm/gicv2.h>
14#include <plat/common/platform.h>
15
Soby Mathewf14d1882015-10-26 14:01:53 +000016/*
17 * The following platform GIC functions are weakly defined. They
18 * provide typical implementations that may be re-used by multiple
19 * platforms but may also be overridden by a platform if required.
20 */
21#pragma weak plat_ic_get_pending_interrupt_id
22#pragma weak plat_ic_get_pending_interrupt_type
23#pragma weak plat_ic_acknowledge_interrupt
24#pragma weak plat_ic_get_interrupt_type
25#pragma weak plat_ic_end_of_interrupt
26#pragma weak plat_interrupt_type_to_line
27
Jeenu Viswambharaneb68ea92017-09-22 08:32:09 +010028#pragma weak plat_ic_get_running_priority
Jeenu Viswambharanca43b552017-09-22 08:32:09 +010029#pragma weak plat_ic_is_spi
30#pragma weak plat_ic_is_ppi
31#pragma weak plat_ic_is_sgi
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +010032#pragma weak plat_ic_get_interrupt_active
Jeenu Viswambharan979225f2017-09-22 08:32:09 +010033#pragma weak plat_ic_enable_interrupt
34#pragma weak plat_ic_disable_interrupt
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +010035#pragma weak plat_ic_set_interrupt_priority
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010036#pragma weak plat_ic_set_interrupt_type
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +010037#pragma weak plat_ic_raise_el3_sgi
Florian Lugoudcb31ff2021-09-08 12:40:24 +020038#pragma weak plat_ic_raise_ns_sgi
39#pragma weak plat_ic_raise_s_el1_sgi
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +010040#pragma weak plat_ic_set_spi_routing
Jeenu Viswambharaneb68ea92017-09-22 08:32:09 +010041
Soby Mathewf14d1882015-10-26 14:01:53 +000042/*
43 * This function returns the highest priority pending interrupt at
44 * the Interrupt controller
45 */
46uint32_t plat_ic_get_pending_interrupt_id(void)
47{
48 unsigned int id;
49
50 id = gicv2_get_pending_interrupt_id();
Maheedhar Bollapalli50029b92024-04-25 14:46:28 +053051 if (id == GIC_SPURIOUS_INTERRUPT) {
52 id = INTR_ID_UNAVAILABLE;
53 }
Soby Mathewf14d1882015-10-26 14:01:53 +000054
55 return id;
56}
57
58/*
59 * This function returns the type of the highest priority pending interrupt
60 * at the Interrupt controller. In the case of GICv2, the Highest Priority
61 * Pending interrupt register (`GICC_HPPIR`) is read to determine the id of
62 * the pending interrupt. The type of interrupt depends upon the id value
63 * as follows.
64 * 1. id < PENDING_G1_INTID (1022) is reported as a S-EL1 interrupt
65 * 2. id = PENDING_G1_INTID (1022) is reported as a Non-secure interrupt.
66 * 3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt
67 * type.
68 */
69uint32_t plat_ic_get_pending_interrupt_type(void)
70{
71 unsigned int id;
Maheedhar Bollapalli50029b92024-04-25 14:46:28 +053072 uint32_t interrupt_type;
Soby Mathewf14d1882015-10-26 14:01:53 +000073
74 id = gicv2_get_pending_interrupt_type();
75
76 /* Assume that all secure interrupts are S-EL1 interrupts */
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010077 if (id < PENDING_G1_INTID) {
78#if GICV2_G0_FOR_EL3
Maheedhar Bollapalli50029b92024-04-25 14:46:28 +053079 interrupt_type = INTR_TYPE_EL3;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010080#else
Maheedhar Bollapalli50029b92024-04-25 14:46:28 +053081 interrupt_type = INTR_TYPE_S_EL1;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010082#endif
Maheedhar Bollapalli50029b92024-04-25 14:46:28 +053083 } else {
84
85 if (id == GIC_SPURIOUS_INTERRUPT) {
86 interrupt_type = INTR_TYPE_INVAL;
87 } else {
88 interrupt_type = INTR_TYPE_NS;
89 }
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010090 }
Soby Mathewf14d1882015-10-26 14:01:53 +000091
Maheedhar Bollapalli50029b92024-04-25 14:46:28 +053092 return interrupt_type;
Soby Mathewf14d1882015-10-26 14:01:53 +000093}
94
95/*
96 * This function returns the highest priority pending interrupt at
97 * the Interrupt controller and indicates to the Interrupt controller
98 * that the interrupt processing has started.
99 */
100uint32_t plat_ic_acknowledge_interrupt(void)
101{
102 return gicv2_acknowledge_interrupt();
103}
104
105/*
106 * This function returns the type of the interrupt `id`, depending on how
107 * the interrupt has been configured in the interrupt controller
108 */
109uint32_t plat_ic_get_interrupt_type(uint32_t id)
110{
111 unsigned int type;
112
113 type = gicv2_get_interrupt_group(id);
114
115 /* Assume that all secure interrupts are S-EL1 interrupts */
Antonio Nino Diaze0ced7a2018-08-21 09:44:43 +0100116 return (type == GICV2_INTR_GROUP1) ? INTR_TYPE_NS :
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100117#if GICV2_G0_FOR_EL3
118 INTR_TYPE_EL3;
119#else
120 INTR_TYPE_S_EL1;
121#endif
Soby Mathewf14d1882015-10-26 14:01:53 +0000122}
123
124/*
125 * This functions is used to indicate to the interrupt controller that
126 * the processing of the interrupt corresponding to the `id` has
127 * finished.
128 */
129void plat_ic_end_of_interrupt(uint32_t id)
130{
131 gicv2_end_of_interrupt(id);
132}
133
134/*
135 * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
136 * The interrupt controller knows which pin/line it uses to signal a type of
137 * interrupt. It lets the interrupt management framework determine
138 * for a type of interrupt and security state, which line should be used in the
139 * SCR_EL3 to control its routing to EL3. The interrupt line is represented
140 * as the bit position of the IRQ or FIQ bit in the SCR_EL3.
141 */
142uint32_t plat_interrupt_type_to_line(uint32_t type,
143 uint32_t security_state)
144{
Antonio Nino Diaze0ced7a2018-08-21 09:44:43 +0100145 assert((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_EL3) ||
146 (type == INTR_TYPE_NS));
Soby Mathewf14d1882015-10-26 14:01:53 +0000147
Santeri Salko53a98be2018-02-08 22:01:26 +0200148 assert(sec_state_is_valid(security_state));
149
Soby Mathewf14d1882015-10-26 14:01:53 +0000150 /* Non-secure interrupts are signaled on the IRQ line always */
Maheedhar Bollapalli7e288d12024-04-25 10:34:02 +0530151 if (type == INTR_TYPE_NS) {
Soby Mathewf14d1882015-10-26 14:01:53 +0000152 return __builtin_ctz(SCR_IRQ_BIT);
Maheedhar Bollapalli7e288d12024-04-25 10:34:02 +0530153 }
Soby Mathewf14d1882015-10-26 14:01:53 +0000154
155 /*
156 * Secure interrupts are signaled using the IRQ line if the FIQ is
157 * not enabled else they are signaled using the FIQ line.
158 */
Antonio Nino Diaze0ced7a2018-08-21 09:44:43 +0100159 return ((gicv2_is_fiq_enabled() != 0U) ? __builtin_ctz(SCR_FIQ_BIT) :
160 __builtin_ctz(SCR_IRQ_BIT));
Soby Mathewf14d1882015-10-26 14:01:53 +0000161}
Jeenu Viswambharaneb68ea92017-09-22 08:32:09 +0100162
163unsigned int plat_ic_get_running_priority(void)
164{
165 return gicv2_get_running_priority();
166}
Jeenu Viswambharanca43b552017-09-22 08:32:09 +0100167
168int plat_ic_is_spi(unsigned int id)
169{
170 return (id >= MIN_SPI_ID) && (id <= MAX_SPI_ID);
171}
172
173int plat_ic_is_ppi(unsigned int id)
174{
175 return (id >= MIN_PPI_ID) && (id < MIN_SPI_ID);
176}
177
178int plat_ic_is_sgi(unsigned int id)
179{
180 return (id >= MIN_SGI_ID) && (id < MIN_PPI_ID);
181}
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +0100182
183unsigned int plat_ic_get_interrupt_active(unsigned int id)
184{
185 return gicv2_get_interrupt_active(id);
186}
Jeenu Viswambharan979225f2017-09-22 08:32:09 +0100187
188void plat_ic_enable_interrupt(unsigned int id)
189{
190 gicv2_enable_interrupt(id);
191}
192
193void plat_ic_disable_interrupt(unsigned int id)
194{
195 gicv2_disable_interrupt(id);
196}
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +0100197
198void plat_ic_set_interrupt_priority(unsigned int id, unsigned int priority)
199{
200 gicv2_set_interrupt_priority(id, priority);
201}
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100202
Madhukar Pappireddy1f6bb412023-09-06 16:50:22 -0500203bool plat_ic_has_interrupt_type(unsigned int type)
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100204{
Madhukar Pappireddy1f6bb412023-09-06 16:50:22 -0500205 bool has_interrupt_type = false;
Jonathan Wright649c48f2018-03-14 15:24:00 +0000206
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100207 switch (type) {
208#if GICV2_G0_FOR_EL3
209 case INTR_TYPE_EL3:
210#else
211 case INTR_TYPE_S_EL1:
212#endif
213 case INTR_TYPE_NS:
Madhukar Pappireddy1f6bb412023-09-06 16:50:22 -0500214 has_interrupt_type = true;
Jonathan Wright649c48f2018-03-14 15:24:00 +0000215 break;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100216 default:
Jonathan Wright649c48f2018-03-14 15:24:00 +0000217 /* Do nothing in default case */
218 break;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100219 }
Jonathan Wright649c48f2018-03-14 15:24:00 +0000220
221 return has_interrupt_type;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100222}
223
224void plat_ic_set_interrupt_type(unsigned int id, unsigned int type)
225{
Madhukar Pappireddyab80cf32023-08-03 12:13:27 -0500226 unsigned int gicv2_group = 0U;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100227
228 /* Map canonical interrupt type to GICv2 type */
229 switch (type) {
230#if GICV2_G0_FOR_EL3
231 case INTR_TYPE_EL3:
232#else
233 case INTR_TYPE_S_EL1:
234#endif
Madhukar Pappireddyab80cf32023-08-03 12:13:27 -0500235 gicv2_group = GICV2_INTR_GROUP0;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100236 break;
237 case INTR_TYPE_NS:
Madhukar Pappireddyab80cf32023-08-03 12:13:27 -0500238 gicv2_group = GICV2_INTR_GROUP1;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100239 break;
240 default:
Madhukar Pappireddyab80cf32023-08-03 12:13:27 -0500241 assert(false); /* Unreachable */
Jonathan Wright649c48f2018-03-14 15:24:00 +0000242 break;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100243 }
244
Madhukar Pappireddyab80cf32023-08-03 12:13:27 -0500245 gicv2_set_interrupt_group(id, gicv2_group);
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100246}
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +0100247
248void plat_ic_raise_el3_sgi(int sgi_num, u_register_t target)
249{
250#if GICV2_G0_FOR_EL3
251 int id;
252
253 /* Target must be a valid MPIDR in the system */
254 id = plat_core_pos_by_mpidr(target);
255 assert(id >= 0);
256
257 /* Verify that this is a secure SGI */
258 assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_EL3);
259
Florian Lugoudcb31ff2021-09-08 12:40:24 +0200260 gicv2_raise_sgi(sgi_num, false, id);
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +0100261#else
Antonio Nino Diaze0ced7a2018-08-21 09:44:43 +0100262 assert(false);
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +0100263#endif
264}
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +0100265
Florian Lugoudcb31ff2021-09-08 12:40:24 +0200266void plat_ic_raise_ns_sgi(int sgi_num, u_register_t target)
267{
268 int id;
269
270 /* Target must be a valid MPIDR in the system */
271 id = plat_core_pos_by_mpidr(target);
272 assert(id >= 0);
273
274 /* Verify that this is a non-secure SGI */
275 assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_NS);
276
277 gicv2_raise_sgi(sgi_num, true, id);
278}
279
280void plat_ic_raise_s_el1_sgi(int sgi_num, u_register_t target)
281{
282#if GICV2_G0_FOR_EL3
283 assert(false);
284#else
285 int id;
286
287 /* Target must be a valid MPIDR in the system */
288 id = plat_core_pos_by_mpidr(target);
289 assert(id >= 0);
290
291 /* Verify that this is a secure EL1 SGI */
292 assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_S_EL1);
293
294 gicv2_raise_sgi(sgi_num, false, id);
295#endif
296}
297
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +0100298void plat_ic_set_spi_routing(unsigned int id, unsigned int routing_mode,
299 u_register_t mpidr)
300{
301 int proc_num = 0;
302
303 switch (routing_mode) {
304 case INTR_ROUTING_MODE_PE:
305 proc_num = plat_core_pos_by_mpidr(mpidr);
306 assert(proc_num >= 0);
307 break;
308 case INTR_ROUTING_MODE_ANY:
309 /* Bit mask selecting all 8 CPUs as candidates */
310 proc_num = -1;
311 break;
312 default:
Daniel Boulbya08a2012018-06-22 14:16:03 +0100313 assert(0); /* Unreachable */
Jonathan Wright649c48f2018-03-14 15:24:00 +0000314 break;
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +0100315 }
316
317 gicv2_set_spi_routing(id, proc_num);
318}
Jeenu Viswambharana2816a12017-09-22 08:32:09 +0100319
320void plat_ic_set_interrupt_pending(unsigned int id)
321{
322 gicv2_set_interrupt_pending(id);
323}
324
325void plat_ic_clear_interrupt_pending(unsigned int id)
326{
327 gicv2_clear_interrupt_pending(id);
328}
Jeenu Viswambharand55a4452017-09-22 08:32:09 +0100329
330unsigned int plat_ic_set_priority_mask(unsigned int mask)
331{
332 return gicv2_set_pmr(mask);
333}
Jeenu Viswambharan4ee8d0b2017-10-24 15:13:59 +0100334
335unsigned int plat_ic_get_interrupt_id(unsigned int raw)
336{
337 unsigned int id = (raw & INT_ID_MASK);
338
Maheedhar Bollapalli7e288d12024-04-25 10:34:02 +0530339 if (id == GIC_SPURIOUS_INTERRUPT) {
Jeenu Viswambharan4ee8d0b2017-10-24 15:13:59 +0100340 id = INTR_ID_UNAVAILABLE;
Maheedhar Bollapalli7e288d12024-04-25 10:34:02 +0530341 }
Jeenu Viswambharan4ee8d0b2017-10-24 15:13:59 +0100342
343 return id;
344}