1 /*
2 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6 #include <arch_helpers.h>
7 #include <assert.h>
8 #include <bl_common.h>
9 #include <cassert.h>
10 #include <gic_common.h>
11 #include <gicv3.h>
12 #include <interrupt_mgmt.h>
13 #include <platform.h>
14
15 #ifdef IMAGE_BL31
16
17 /*
18 * The following platform GIC functions are weakly defined. They
19 * provide typical implementations that may be re-used by multiple
20 * platforms but may also be overridden by a platform if required.
21 */
22 #pragma weak plat_ic_get_pending_interrupt_id
23 #pragma weak plat_ic_get_pending_interrupt_type
24 #pragma weak plat_ic_acknowledge_interrupt
25 #pragma weak plat_ic_get_interrupt_type
26 #pragma weak plat_ic_end_of_interrupt
27 #pragma weak plat_interrupt_type_to_line
28
29 #pragma weak plat_ic_get_running_priority
30 #pragma weak plat_ic_is_spi
31 #pragma weak plat_ic_is_ppi
32 #pragma weak plat_ic_is_sgi
33 #pragma weak plat_ic_get_interrupt_active
34 #pragma weak plat_ic_enable_interrupt
35 #pragma weak plat_ic_disable_interrupt
36 #pragma weak plat_ic_set_interrupt_priority
37 #pragma weak plat_ic_set_interrupt_type
38 #pragma weak plat_ic_raise_el3_sgi
39 #pragma weak plat_ic_set_spi_routing
40 #pragma weak plat_ic_set_interrupt_pending
41 #pragma weak plat_ic_clear_interrupt_pending
42
43 CASSERT((INTR_TYPE_S_EL1 == INTR_GROUP1S) &&
44 (INTR_TYPE_NS == INTR_GROUP1NS) &&
45 (INTR_TYPE_EL3 == INTR_GROUP0), assert_interrupt_type_mismatch);
46
47 /*
48 * This function returns the highest priority pending interrupt at
49 * the Interrupt controller
50 */
plat_ic_get_pending_interrupt_id(void)51 uint32_t plat_ic_get_pending_interrupt_id(void)
52 {
53 unsigned int irqnr;
54
55 assert(IS_IN_EL3());
56 irqnr = gicv3_get_pending_interrupt_id();
57 return (gicv3_is_intr_id_special_identifier(irqnr)) ?
58 INTR_ID_UNAVAILABLE : irqnr;
59 }
60
61 /*
62 * This function returns the type of the highest priority pending interrupt
63 * at the Interrupt controller. In the case of GICv3, the Highest Priority
64 * Pending interrupt system register (`ICC_HPPIR0_EL1`) is read to determine
65 * the id of the pending interrupt. The type of interrupt depends upon the
66 * id value as follows.
67 * 1. id = PENDING_G1S_INTID (1020) is reported as a S-EL1 interrupt
68 * 2. id = PENDING_G1NS_INTID (1021) is reported as a Non-secure interrupt.
69 * 3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt
70 * type.
71 * 4. All other interrupt id's are reported as EL3 interrupt.
72 */
plat_ic_get_pending_interrupt_type(void)73 uint32_t plat_ic_get_pending_interrupt_type(void)
74 {
75 unsigned int irqnr;
76
77 assert(IS_IN_EL3());
78 irqnr = gicv3_get_pending_interrupt_type();
79
80 switch (irqnr) {
81 case PENDING_G1S_INTID:
82 return INTR_TYPE_S_EL1;
83 case PENDING_G1NS_INTID:
84 return INTR_TYPE_NS;
85 case GIC_SPURIOUS_INTERRUPT:
86 return INTR_TYPE_INVAL;
87 default:
88 return INTR_TYPE_EL3;
89 }
90 }
91
92 /*
93 * This function returns the highest priority pending interrupt at
94 * the Interrupt controller and indicates to the Interrupt controller
95 * that the interrupt processing has started.
96 */
plat_ic_acknowledge_interrupt(void)97 uint32_t plat_ic_acknowledge_interrupt(void)
98 {
99 assert(IS_IN_EL3());
100 return gicv3_acknowledge_interrupt();
101 }
102
103 /*
104 * This function returns the type of the interrupt `id`, depending on how
105 * the interrupt has been configured in the interrupt controller
106 */
plat_ic_get_interrupt_type(uint32_t id)107 uint32_t plat_ic_get_interrupt_type(uint32_t id)
108 {
109 assert(IS_IN_EL3());
110 return gicv3_get_interrupt_type(id, plat_my_core_pos());
111 }
112
113 /*
114 * This functions is used to indicate to the interrupt controller that
115 * the processing of the interrupt corresponding to the `id` has
116 * finished.
117 */
plat_ic_end_of_interrupt(uint32_t id)118 void plat_ic_end_of_interrupt(uint32_t id)
119 {
120 assert(IS_IN_EL3());
121 gicv3_end_of_interrupt(id);
122 }
123
124 /*
125 * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
126 * The interrupt controller knows which pin/line it uses to signal a type of
127 * interrupt. It lets the interrupt management framework determine for a type of
128 * interrupt and security state, which line should be used in the SCR_EL3 to
129 * control its routing to EL3. The interrupt line is represented as the bit
130 * position of the IRQ or FIQ bit in the SCR_EL3.
131 */
plat_interrupt_type_to_line(uint32_t type,uint32_t security_state)132 uint32_t plat_interrupt_type_to_line(uint32_t type,
133 uint32_t security_state)
134 {
135 assert(type == INTR_TYPE_S_EL1 ||
136 type == INTR_TYPE_EL3 ||
137 type == INTR_TYPE_NS);
138
139 assert(sec_state_is_valid(security_state));
140 assert(IS_IN_EL3());
141
142 switch (type) {
143 case INTR_TYPE_S_EL1:
144 /*
145 * The S-EL1 interrupts are signaled as IRQ in S-EL0/1 contexts
146 * and as FIQ in the NS-EL0/1/2 contexts
147 */
148 if (security_state == SECURE)
149 return __builtin_ctz(SCR_IRQ_BIT);
150 else
151 return __builtin_ctz(SCR_FIQ_BIT);
152 case INTR_TYPE_NS:
153 /*
154 * The Non secure interrupts will be signaled as FIQ in S-EL0/1
155 * contexts and as IRQ in the NS-EL0/1/2 contexts.
156 */
157 if (security_state == SECURE)
158 return __builtin_ctz(SCR_FIQ_BIT);
159 else
160 return __builtin_ctz(SCR_IRQ_BIT);
161 default:
162 assert(0);
163 /* Fall through in the release build */
164 case INTR_TYPE_EL3:
165 /*
166 * The EL3 interrupts are signaled as FIQ in both S-EL0/1 and
167 * NS-EL0/1/2 contexts
168 */
169 return __builtin_ctz(SCR_FIQ_BIT);
170 }
171 }
172
plat_ic_get_running_priority(void)173 unsigned int plat_ic_get_running_priority(void)
174 {
175 return gicv3_get_running_priority();
176 }
177
plat_ic_is_spi(unsigned int id)178 int plat_ic_is_spi(unsigned int id)
179 {
180 return (id >= MIN_SPI_ID) && (id <= MAX_SPI_ID);
181 }
182
plat_ic_is_ppi(unsigned int id)183 int plat_ic_is_ppi(unsigned int id)
184 {
185 return (id >= MIN_PPI_ID) && (id < MIN_SPI_ID);
186 }
187
plat_ic_is_sgi(unsigned int id)188 int plat_ic_is_sgi(unsigned int id)
189 {
190 return (id >= MIN_SGI_ID) && (id < MIN_PPI_ID);
191 }
192
plat_ic_get_interrupt_active(unsigned int id)193 unsigned int plat_ic_get_interrupt_active(unsigned int id)
194 {
195 return gicv3_get_interrupt_active(id, plat_my_core_pos());
196 }
197
plat_ic_enable_interrupt(unsigned int id)198 void plat_ic_enable_interrupt(unsigned int id)
199 {
200 gicv3_enable_interrupt(id, plat_my_core_pos());
201 }
202
plat_ic_disable_interrupt(unsigned int id)203 void plat_ic_disable_interrupt(unsigned int id)
204 {
205 gicv3_disable_interrupt(id, plat_my_core_pos());
206 }
207
plat_ic_set_interrupt_priority(unsigned int id,unsigned int priority)208 void plat_ic_set_interrupt_priority(unsigned int id, unsigned int priority)
209 {
210 gicv3_set_interrupt_priority(id, plat_my_core_pos(), priority);
211 }
212
plat_ic_has_interrupt_type(unsigned int type)213 int plat_ic_has_interrupt_type(unsigned int type)
214 {
215 assert((type == INTR_TYPE_EL3) || (type == INTR_TYPE_S_EL1) ||
216 (type == INTR_TYPE_NS));
217 return 1;
218 }
219
plat_ic_set_interrupt_type(unsigned int id,unsigned int type)220 void plat_ic_set_interrupt_type(unsigned int id, unsigned int type)
221 {
222 gicv3_set_interrupt_type(id, plat_my_core_pos(), type);
223 }
224
plat_ic_raise_el3_sgi(int sgi_num,u_register_t target)225 void plat_ic_raise_el3_sgi(int sgi_num, u_register_t target)
226 {
227 /* Target must be a valid MPIDR in the system */
228 assert(plat_core_pos_by_mpidr(target) >= 0);
229
230 /* Verify that this is a secure EL3 SGI */
231 assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_EL3);
232
233 gicv3_raise_secure_g0_sgi(sgi_num, target);
234 }
235
plat_ic_set_spi_routing(unsigned int id,unsigned int routing_mode,u_register_t mpidr)236 void plat_ic_set_spi_routing(unsigned int id, unsigned int routing_mode,
237 u_register_t mpidr)
238 {
239 unsigned int irm = 0;
240
241 switch (routing_mode) {
242 case INTR_ROUTING_MODE_PE:
243 assert(plat_core_pos_by_mpidr(mpidr) >= 0);
244 irm = GICV3_IRM_PE;
245 break;
246 case INTR_ROUTING_MODE_ANY:
247 irm = GICV3_IRM_ANY;
248 break;
249 default:
250 assert(0);
251 }
252
253 gicv3_set_spi_routing(id, irm, mpidr);
254 }
255
plat_ic_set_interrupt_pending(unsigned int id)256 void plat_ic_set_interrupt_pending(unsigned int id)
257 {
258 /* Disallow setting SGIs pending */
259 assert(id >= MIN_PPI_ID);
260 gicv3_set_interrupt_pending(id, plat_my_core_pos());
261 }
262
plat_ic_clear_interrupt_pending(unsigned int id)263 void plat_ic_clear_interrupt_pending(unsigned int id)
264 {
265 /* Disallow setting SGIs pending */
266 assert(id >= MIN_PPI_ID);
267 gicv3_clear_interrupt_pending(id, plat_my_core_pos());
268 }
269
plat_ic_set_priority_mask(unsigned int mask)270 unsigned int plat_ic_set_priority_mask(unsigned int mask)
271 {
272 return gicv3_set_pmr(mask);
273 }
274 #endif
275 #ifdef IMAGE_BL32
276
277 #pragma weak plat_ic_get_pending_interrupt_id
278 #pragma weak plat_ic_acknowledge_interrupt
279 #pragma weak plat_ic_end_of_interrupt
280
281 /* In AArch32, the secure group1 interrupts are targeted to Secure PL1 */
282 #ifdef AARCH32
283 #define IS_IN_EL1() IS_IN_SECURE()
284 #endif
285
286 /*
287 * This function returns the highest priority pending interrupt at
288 * the Interrupt controller
289 */
plat_ic_get_pending_interrupt_id(void)290 uint32_t plat_ic_get_pending_interrupt_id(void)
291 {
292 unsigned int irqnr;
293
294 assert(IS_IN_EL1());
295 irqnr = gicv3_get_pending_interrupt_id_sel1();
296 return (irqnr == GIC_SPURIOUS_INTERRUPT) ?
297 INTR_ID_UNAVAILABLE : irqnr;
298 }
299
300 /*
301 * This function returns the highest priority pending interrupt at
302 * the Interrupt controller and indicates to the Interrupt controller
303 * that the interrupt processing has started.
304 */
plat_ic_acknowledge_interrupt(void)305 uint32_t plat_ic_acknowledge_interrupt(void)
306 {
307 assert(IS_IN_EL1());
308 return gicv3_acknowledge_interrupt_sel1();
309 }
310
311 /*
312 * This functions is used to indicate to the interrupt controller that
313 * the processing of the interrupt corresponding to the `id` has
314 * finished.
315 */
plat_ic_end_of_interrupt(uint32_t id)316 void plat_ic_end_of_interrupt(uint32_t id)
317 {
318 assert(IS_IN_EL1());
319 gicv3_end_of_interrupt_sel1(id);
320 }
321 #endif
322