blob: b68d99882485279670a07626c0b25f5de09c3a39 [file] [log] [blame]
Achin Gupta92712a52015-09-03 14:18:02 +01001/*
Soby Mathew72645132017-02-14 10:11:52 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Achin Gupta92712a52015-09-03 14:18:02 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta92712a52015-09-03 14:18:02 +01005 */
6
7#include <arch.h>
8#include <arch_helpers.h>
9#include <assert.h>
10#include <debug.h>
Achin Gupta92712a52015-09-03 14:18:02 +010011#include <gicv3.h>
12#include "gicv3_private.h"
13
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +000014const gicv3_driver_data_t *gicv3_driver_data;
Achin Gupta92712a52015-09-03 14:18:02 +010015static unsigned int gicv2_compat;
16
Jeenu Viswambharan76647d52016-12-09 11:03:15 +000017/*
18 * Redistributor power operations are weakly bound so that they can be
19 * overridden
20 */
21#pragma weak gicv3_rdistif_off
22#pragma weak gicv3_rdistif_on
23
Achin Gupta92712a52015-09-03 14:18:02 +010024/*******************************************************************************
25 * This function initialises the ARM GICv3 driver in EL3 with provided platform
26 * inputs.
27 ******************************************************************************/
28void gicv3_driver_init(const gicv3_driver_data_t *plat_driver_data)
29{
30 unsigned int gic_version;
31
32 assert(plat_driver_data);
33 assert(plat_driver_data->gicd_base);
34 assert(plat_driver_data->gicr_base);
35 assert(plat_driver_data->rdistif_num);
36 assert(plat_driver_data->rdistif_base_addrs);
37
38 assert(IS_IN_EL3());
39
40 /*
41 * The platform should provide a list of at least one type of
42 * interrupts
43 */
44 assert(plat_driver_data->g0_interrupt_array ||
45 plat_driver_data->g1s_interrupt_array);
46
47 /*
48 * If there are no interrupts of a particular type, then the number of
49 * interrupts of that type should be 0 and vice-versa.
50 */
51 assert(plat_driver_data->g0_interrupt_array ?
52 plat_driver_data->g0_interrupt_num :
53 plat_driver_data->g0_interrupt_num == 0);
54 assert(plat_driver_data->g1s_interrupt_array ?
55 plat_driver_data->g1s_interrupt_num :
56 plat_driver_data->g1s_interrupt_num == 0);
57
58 /* Check for system register support */
Soby Mathewd6452322016-05-05 13:59:07 +010059#ifdef AARCH32
60 assert(read_id_pfr1() & (ID_PFR1_GIC_MASK << ID_PFR1_GIC_SHIFT));
61#else
Achin Gupta92712a52015-09-03 14:18:02 +010062 assert(read_id_aa64pfr0_el1() &
63 (ID_AA64PFR0_GIC_MASK << ID_AA64PFR0_GIC_SHIFT));
Soby Mathewd6452322016-05-05 13:59:07 +010064#endif /* AARCH32 */
Achin Gupta92712a52015-09-03 14:18:02 +010065
66 /* The GIC version should be 3.0 */
67 gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
68 gic_version >>= PIDR2_ARCH_REV_SHIFT;
69 gic_version &= PIDR2_ARCH_REV_MASK;
70 assert(gic_version == ARCH_REV_GICV3);
71
72 /*
73 * Find out whether the GIC supports the GICv2 compatibility mode. The
74 * ARE_S bit resets to 0 if supported
75 */
76 gicv2_compat = gicd_read_ctlr(plat_driver_data->gicd_base);
77 gicv2_compat >>= CTLR_ARE_S_SHIFT;
78 gicv2_compat = !(gicv2_compat & CTLR_ARE_S_MASK);
79
80 /*
81 * Find the base address of each implemented Redistributor interface.
82 * The number of interfaces should be equal to the number of CPUs in the
83 * system. The memory for saving these addresses has to be allocated by
84 * the platform port
85 */
86 gicv3_rdistif_base_addrs_probe(plat_driver_data->rdistif_base_addrs,
87 plat_driver_data->rdistif_num,
88 plat_driver_data->gicr_base,
89 plat_driver_data->mpidr_to_core_pos);
90
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +000091 gicv3_driver_data = plat_driver_data;
Achin Gupta92712a52015-09-03 14:18:02 +010092
Soby Mathew72645132017-02-14 10:11:52 +000093 /*
94 * The GIC driver data is initialized by the primary CPU with caches
95 * enabled. When the secondary CPU boots up, it initializes the
96 * GICC/GICR interface with the caches disabled. Hence flush the
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +000097 * driver data to ensure coherency. This is not required if the
Soby Mathew72645132017-02-14 10:11:52 +000098 * platform has HW_ASSISTED_COHERENCY enabled.
99 */
100#if !HW_ASSISTED_COHERENCY
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000101 flush_dcache_range((uintptr_t) &gicv3_driver_data,
102 sizeof(gicv3_driver_data));
103 flush_dcache_range((uintptr_t) gicv3_driver_data,
104 sizeof(*gicv3_driver_data));
Soby Mathew72645132017-02-14 10:11:52 +0000105#endif
106
Achin Gupta92712a52015-09-03 14:18:02 +0100107 INFO("GICv3 %s legacy support detected."
108 " ARM GICV3 driver initialized in EL3\n",
109 gicv2_compat ? "with" : "without");
110}
111
112/*******************************************************************************
113 * This function initialises the GIC distributor interface based upon the data
114 * provided by the platform while initialising the driver.
115 ******************************************************************************/
116void gicv3_distif_init(void)
117{
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100118 unsigned int bitmap = 0;
119
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000120 assert(gicv3_driver_data);
121 assert(gicv3_driver_data->gicd_base);
122 assert(gicv3_driver_data->g1s_interrupt_array ||
123 gicv3_driver_data->g0_interrupt_array);
Achin Gupta92712a52015-09-03 14:18:02 +0100124
125 assert(IS_IN_EL3());
126
127 /*
128 * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
129 * the ARE_S bit. The Distributor might generate a system error
130 * otherwise.
131 */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000132 gicd_clr_ctlr(gicv3_driver_data->gicd_base,
Achin Gupta92712a52015-09-03 14:18:02 +0100133 CTLR_ENABLE_G0_BIT |
134 CTLR_ENABLE_G1S_BIT |
135 CTLR_ENABLE_G1NS_BIT,
136 RWP_TRUE);
137
138 /* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000139 gicd_set_ctlr(gicv3_driver_data->gicd_base,
Achin Gupta92712a52015-09-03 14:18:02 +0100140 CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);
141
142 /* Set the default attribute of all SPIs */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000143 gicv3_spis_configure_defaults(gicv3_driver_data->gicd_base);
Achin Gupta92712a52015-09-03 14:18:02 +0100144
145 /* Configure the G1S SPIs */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000146 if (gicv3_driver_data->g1s_interrupt_array) {
147 gicv3_secure_spis_configure(gicv3_driver_data->gicd_base,
148 gicv3_driver_data->g1s_interrupt_num,
149 gicv3_driver_data->g1s_interrupt_array,
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000150 INTR_GROUP1S);
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100151 bitmap |= CTLR_ENABLE_G1S_BIT;
152 }
Achin Gupta92712a52015-09-03 14:18:02 +0100153
154 /* Configure the G0 SPIs */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000155 if (gicv3_driver_data->g0_interrupt_array) {
156 gicv3_secure_spis_configure(gicv3_driver_data->gicd_base,
157 gicv3_driver_data->g0_interrupt_num,
158 gicv3_driver_data->g0_interrupt_array,
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000159 INTR_GROUP0);
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100160 bitmap |= CTLR_ENABLE_G0_BIT;
161 }
Achin Gupta92712a52015-09-03 14:18:02 +0100162
163 /* Enable the secure SPIs now that they have been configured */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000164 gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
Achin Gupta92712a52015-09-03 14:18:02 +0100165}
166
167/*******************************************************************************
168 * This function initialises the GIC Redistributor interface of the calling CPU
169 * (identified by the 'proc_num' parameter) based upon the data provided by the
170 * platform while initialising the driver.
171 ******************************************************************************/
172void gicv3_rdistif_init(unsigned int proc_num)
173{
174 uintptr_t gicr_base;
175
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000176 assert(gicv3_driver_data);
177 assert(proc_num < gicv3_driver_data->rdistif_num);
178 assert(gicv3_driver_data->rdistif_base_addrs);
179 assert(gicv3_driver_data->gicd_base);
180 assert(gicd_read_ctlr(gicv3_driver_data->gicd_base) & CTLR_ARE_S_BIT);
181 assert(gicv3_driver_data->g1s_interrupt_array ||
182 gicv3_driver_data->g0_interrupt_array);
Achin Gupta92712a52015-09-03 14:18:02 +0100183
184 assert(IS_IN_EL3());
185
Jeenu Viswambharan76647d52016-12-09 11:03:15 +0000186 /* Power on redistributor */
187 gicv3_rdistif_on(proc_num);
188
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000189 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Gupta92712a52015-09-03 14:18:02 +0100190
191 /* Set the default attribute of all SGIs and PPIs */
192 gicv3_ppi_sgi_configure_defaults(gicr_base);
193
194 /* Configure the G1S SGIs/PPIs */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000195 if (gicv3_driver_data->g1s_interrupt_array) {
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100196 gicv3_secure_ppi_sgi_configure(gicr_base,
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000197 gicv3_driver_data->g1s_interrupt_num,
198 gicv3_driver_data->g1s_interrupt_array,
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100199 INTR_GROUP1S);
200 }
Achin Gupta92712a52015-09-03 14:18:02 +0100201
202 /* Configure the G0 SGIs/PPIs */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000203 if (gicv3_driver_data->g0_interrupt_array) {
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100204 gicv3_secure_ppi_sgi_configure(gicr_base,
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000205 gicv3_driver_data->g0_interrupt_num,
206 gicv3_driver_data->g0_interrupt_array,
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100207 INTR_GROUP0);
208 }
Achin Gupta92712a52015-09-03 14:18:02 +0100209}
210
211/*******************************************************************************
Jeenu Viswambharan76647d52016-12-09 11:03:15 +0000212 * Functions to perform power operations on GIC Redistributor
213 ******************************************************************************/
214void gicv3_rdistif_off(unsigned int proc_num)
215{
216 return;
217}
218
219void gicv3_rdistif_on(unsigned int proc_num)
220{
221 return;
222}
223
224/*******************************************************************************
Achin Gupta92712a52015-09-03 14:18:02 +0100225 * This function enables the GIC CPU interface of the calling CPU using only
226 * system register accesses.
227 ******************************************************************************/
228void gicv3_cpuif_enable(unsigned int proc_num)
229{
230 uintptr_t gicr_base;
231 unsigned int scr_el3;
232 unsigned int icc_sre_el3;
233
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000234 assert(gicv3_driver_data);
235 assert(proc_num < gicv3_driver_data->rdistif_num);
236 assert(gicv3_driver_data->rdistif_base_addrs);
Achin Gupta92712a52015-09-03 14:18:02 +0100237 assert(IS_IN_EL3());
238
239 /* Mark the connected core as awake */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000240 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Gupta92712a52015-09-03 14:18:02 +0100241 gicv3_rdistif_mark_core_awake(gicr_base);
242
243 /* Disable the legacy interrupt bypass */
244 icc_sre_el3 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT;
245
246 /*
247 * Enable system register access for EL3 and allow lower exception
248 * levels to configure the same for themselves. If the legacy mode is
249 * not supported, the SRE bit is RAO/WI
250 */
251 icc_sre_el3 |= (ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT);
252 write_icc_sre_el3(read_icc_sre_el3() | icc_sre_el3);
253
254 scr_el3 = read_scr_el3();
255
256 /*
257 * Switch to NS state to write Non secure ICC_SRE_EL1 and
258 * ICC_SRE_EL2 registers.
259 */
260 write_scr_el3(scr_el3 | SCR_NS_BIT);
261 isb();
262
263 write_icc_sre_el2(read_icc_sre_el2() | icc_sre_el3);
264 write_icc_sre_el1(ICC_SRE_SRE_BIT);
265 isb();
266
267 /* Switch to secure state. */
268 write_scr_el3(scr_el3 & (~SCR_NS_BIT));
269 isb();
270
271 /* Program the idle priority in the PMR */
272 write_icc_pmr_el1(GIC_PRI_MASK);
273
274 /* Enable Group0 interrupts */
275 write_icc_igrpen0_el1(IGRPEN1_EL1_ENABLE_G0_BIT);
276
277 /* Enable Group1 Secure interrupts */
278 write_icc_igrpen1_el3(read_icc_igrpen1_el3() |
279 IGRPEN1_EL3_ENABLE_G1S_BIT);
280
281 /* Write the secure ICC_SRE_EL1 register */
282 write_icc_sre_el1(ICC_SRE_SRE_BIT);
283 isb();
284}
285
286/*******************************************************************************
287 * This function disables the GIC CPU interface of the calling CPU using
288 * only system register accesses.
289 ******************************************************************************/
290void gicv3_cpuif_disable(unsigned int proc_num)
291{
292 uintptr_t gicr_base;
293
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000294 assert(gicv3_driver_data);
295 assert(proc_num < gicv3_driver_data->rdistif_num);
296 assert(gicv3_driver_data->rdistif_base_addrs);
Achin Gupta92712a52015-09-03 14:18:02 +0100297
298 assert(IS_IN_EL3());
299
300 /* Disable legacy interrupt bypass */
301 write_icc_sre_el3(read_icc_sre_el3() |
302 (ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT));
303
304 /* Disable Group0 interrupts */
305 write_icc_igrpen0_el1(read_icc_igrpen0_el1() &
306 ~IGRPEN1_EL1_ENABLE_G0_BIT);
307
Sudeep Holla869e3db2016-08-04 16:14:50 +0100308 /* Disable Group1 Secure and Non-Secure interrupts */
Achin Gupta92712a52015-09-03 14:18:02 +0100309 write_icc_igrpen1_el3(read_icc_igrpen1_el3() &
Sudeep Holla869e3db2016-08-04 16:14:50 +0100310 ~(IGRPEN1_EL3_ENABLE_G1NS_BIT |
311 IGRPEN1_EL3_ENABLE_G1S_BIT));
Achin Gupta92712a52015-09-03 14:18:02 +0100312
313 /* Synchronise accesses to group enable registers */
314 isb();
315
316 /* Mark the connected core as asleep */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000317 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Gupta92712a52015-09-03 14:18:02 +0100318 gicv3_rdistif_mark_core_asleep(gicr_base);
319}
320
321/*******************************************************************************
322 * This function returns the id of the highest priority pending interrupt at
323 * the GIC cpu interface.
324 ******************************************************************************/
325unsigned int gicv3_get_pending_interrupt_id(void)
326{
327 unsigned int id;
328
329 assert(IS_IN_EL3());
330 id = read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
331
332 /*
333 * If the ID is special identifier corresponding to G1S or G1NS
334 * interrupt, then read the highest pending group 1 interrupt.
335 */
336 if ((id == PENDING_G1S_INTID) || (id == PENDING_G1NS_INTID))
337 return read_icc_hppir1_el1() & HPPIR1_EL1_INTID_MASK;
338
339 return id;
340}
341
342/*******************************************************************************
343 * This function returns the type of the highest priority pending interrupt at
344 * the GIC cpu interface. The return values can be one of the following :
345 * PENDING_G1S_INTID : The interrupt type is secure Group 1.
346 * PENDING_G1NS_INTID : The interrupt type is non secure Group 1.
347 * 0 - 1019 : The interrupt type is secure Group 0.
348 * GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
349 * sufficient priority to be signaled
350 ******************************************************************************/
351unsigned int gicv3_get_pending_interrupt_type(void)
352{
353 assert(IS_IN_EL3());
354 return read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
355}
356
357/*******************************************************************************
358 * This function returns the type of the interrupt id depending upon the group
359 * this interrupt has been configured under by the interrupt controller i.e.
360 * group0 or group1 Secure / Non Secure. The return value can be one of the
361 * following :
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000362 * INTR_GROUP0 : The interrupt type is a Secure Group 0 interrupt
363 * INTR_GROUP1S : The interrupt type is a Secure Group 1 secure interrupt
364 * INTR_GROUP1NS: The interrupt type is a Secure Group 1 non secure
Achin Gupta92712a52015-09-03 14:18:02 +0100365 * interrupt.
366 ******************************************************************************/
367unsigned int gicv3_get_interrupt_type(unsigned int id,
368 unsigned int proc_num)
369{
370 unsigned int igroup, grpmodr;
371 uintptr_t gicr_base;
372
373 assert(IS_IN_EL3());
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000374 assert(gicv3_driver_data);
Achin Gupta92712a52015-09-03 14:18:02 +0100375
376 /* Ensure the parameters are valid */
377 assert(id < PENDING_G1S_INTID || id >= MIN_LPI_ID);
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000378 assert(proc_num < gicv3_driver_data->rdistif_num);
Achin Gupta92712a52015-09-03 14:18:02 +0100379
380 /* All LPI interrupts are Group 1 non secure */
381 if (id >= MIN_LPI_ID)
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000382 return INTR_GROUP1NS;
Achin Gupta92712a52015-09-03 14:18:02 +0100383
384 if (id < MIN_SPI_ID) {
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000385 assert(gicv3_driver_data->rdistif_base_addrs);
386 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Gupta92712a52015-09-03 14:18:02 +0100387 igroup = gicr_get_igroupr0(gicr_base, id);
388 grpmodr = gicr_get_igrpmodr0(gicr_base, id);
389 } else {
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000390 assert(gicv3_driver_data->gicd_base);
391 igroup = gicd_get_igroupr(gicv3_driver_data->gicd_base, id);
392 grpmodr = gicd_get_igrpmodr(gicv3_driver_data->gicd_base, id);
Achin Gupta92712a52015-09-03 14:18:02 +0100393 }
394
395 /*
396 * If the IGROUP bit is set, then it is a Group 1 Non secure
397 * interrupt
398 */
399 if (igroup)
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000400 return INTR_GROUP1NS;
Achin Gupta92712a52015-09-03 14:18:02 +0100401
402 /* If the GRPMOD bit is set, then it is a Group 1 Secure interrupt */
403 if (grpmodr)
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000404 return INTR_GROUP1S;
Achin Gupta92712a52015-09-03 14:18:02 +0100405
406 /* Else it is a Group 0 Secure interrupt */
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000407 return INTR_GROUP0;
Achin Gupta92712a52015-09-03 14:18:02 +0100408}