blob: 8da4512e5a491f370752e6e4a3f0b1cb259987be [file] [log] [blame]
Achin Gupta92712a52015-09-03 14:18:02 +01001/*
Roberto Vargas2ca18d92018-02-12 12:36:17 +00002 * Copyright (c) 2015-2018, 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>
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +010012#include <interrupt_props.h>
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +010013#include <spinlock.h>
Achin Gupta92712a52015-09-03 14:18:02 +010014#include "gicv3_private.h"
15
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +000016const gicv3_driver_data_t *gicv3_driver_data;
Achin Gupta92712a52015-09-03 14:18:02 +010017static unsigned int gicv2_compat;
18
Jeenu Viswambharan76647d52016-12-09 11:03:15 +000019/*
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +010020 * Spinlock to guard registers needing read-modify-write. APIs protected by this
21 * spinlock are used either at boot time (when only a single CPU is active), or
22 * when the system is fully coherent.
23 */
Roberto Vargas2ca18d92018-02-12 12:36:17 +000024static spinlock_t gic_lock;
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +010025
26/*
Jeenu Viswambharan76647d52016-12-09 11:03:15 +000027 * Redistributor power operations are weakly bound so that they can be
28 * overridden
29 */
30#pragma weak gicv3_rdistif_off
31#pragma weak gicv3_rdistif_on
32
Soby Mathew327548c2017-07-13 15:19:51 +010033
34/* Helper macros to save and restore GICD registers to and from the context */
35#define RESTORE_GICD_REGS(base, ctx, intr_num, reg, REG) \
36 do { \
Antonio Nino Diazca994e72018-08-21 10:02:33 +010037 for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num); \
38 int_id += (1U << REG##_SHIFT)) { \
Soby Mathew327548c2017-07-13 15:19:51 +010039 gicd_write_##reg(base, int_id, \
40 ctx->gicd_##reg[(int_id - MIN_SPI_ID) >> REG##_SHIFT]); \
41 } \
Antonio Nino Diazca994e72018-08-21 10:02:33 +010042 } while (false)
Soby Mathew327548c2017-07-13 15:19:51 +010043
44#define SAVE_GICD_REGS(base, ctx, intr_num, reg, REG) \
45 do { \
Antonio Nino Diazca994e72018-08-21 10:02:33 +010046 for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num); \
47 int_id += (1U << REG##_SHIFT)) { \
Soby Mathew327548c2017-07-13 15:19:51 +010048 ctx->gicd_##reg[(int_id - MIN_SPI_ID) >> REG##_SHIFT] =\
49 gicd_read_##reg(base, int_id); \
50 } \
Antonio Nino Diazca994e72018-08-21 10:02:33 +010051 } while (false)
Soby Mathew327548c2017-07-13 15:19:51 +010052
53
Achin Gupta92712a52015-09-03 14:18:02 +010054/*******************************************************************************
55 * This function initialises the ARM GICv3 driver in EL3 with provided platform
56 * inputs.
57 ******************************************************************************/
58void gicv3_driver_init(const gicv3_driver_data_t *plat_driver_data)
59{
60 unsigned int gic_version;
61
Antonio Nino Diazca994e72018-08-21 10:02:33 +010062 assert(plat_driver_data != NULL);
63 assert(plat_driver_data->gicd_base != 0U);
64 assert(plat_driver_data->gicr_base != 0U);
65 assert(plat_driver_data->rdistif_num != 0U);
66 assert(plat_driver_data->rdistif_base_addrs != NULL);
Achin Gupta92712a52015-09-03 14:18:02 +010067
68 assert(IS_IN_EL3());
69
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +010070#if !ERROR_DEPRECATED
71 if (plat_driver_data->interrupt_props == NULL) {
72 /* Interrupt properties array size must be 0 */
73 assert(plat_driver_data->interrupt_props_num == 0);
Achin Gupta92712a52015-09-03 14:18:02 +010074
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +010075 /*
Dan Handley4d408b52018-03-01 16:00:15 +000076 * Suppress deprecated declaration warnings in compatibility
77 * function
78 */
79#pragma GCC diagnostic push
80#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
81
82 /*
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +010083 * The platform should provide a list of at least one type of
84 * interrupt.
85 */
86 assert(plat_driver_data->g0_interrupt_array ||
87 plat_driver_data->g1s_interrupt_array);
88
89 /*
90 * If there are no interrupts of a particular type, then the
91 * number of interrupts of that type should be 0 and vice-versa.
92 */
93 assert(plat_driver_data->g0_interrupt_array ?
94 plat_driver_data->g0_interrupt_num :
95 plat_driver_data->g0_interrupt_num == 0);
96 assert(plat_driver_data->g1s_interrupt_array ?
97 plat_driver_data->g1s_interrupt_num :
98 plat_driver_data->g1s_interrupt_num == 0);
Dan Handley4d408b52018-03-01 16:00:15 +000099#pragma GCC diagnostic pop
100
101 WARN("Using deprecated integer interrupt arrays in "
102 "gicv3_driver_data_t\n");
103 WARN("Please migrate to using interrupt_prop_t arrays\n");
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100104 }
105#else
Andre Przywarae8e94e82017-11-09 12:08:00 -0600106 assert(plat_driver_data->interrupt_props_num > 0 ?
107 plat_driver_data->interrupt_props != NULL : 1);
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100108#endif
Achin Gupta92712a52015-09-03 14:18:02 +0100109
110 /* Check for system register support */
Soby Mathewd6452322016-05-05 13:59:07 +0100111#ifdef AARCH32
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100112 assert((read_id_pfr1() & (ID_PFR1_GIC_MASK << ID_PFR1_GIC_SHIFT)) != 0U);
Soby Mathewd6452322016-05-05 13:59:07 +0100113#else
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100114 assert((read_id_aa64pfr0_el1() &
115 (ID_AA64PFR0_GIC_MASK << ID_AA64PFR0_GIC_SHIFT)) != 0U);
Soby Mathewd6452322016-05-05 13:59:07 +0100116#endif /* AARCH32 */
Achin Gupta92712a52015-09-03 14:18:02 +0100117
118 /* The GIC version should be 3.0 */
119 gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
120 gic_version >>= PIDR2_ARCH_REV_SHIFT;
121 gic_version &= PIDR2_ARCH_REV_MASK;
122 assert(gic_version == ARCH_REV_GICV3);
123
124 /*
125 * Find out whether the GIC supports the GICv2 compatibility mode. The
126 * ARE_S bit resets to 0 if supported
127 */
128 gicv2_compat = gicd_read_ctlr(plat_driver_data->gicd_base);
129 gicv2_compat >>= CTLR_ARE_S_SHIFT;
130 gicv2_compat = !(gicv2_compat & CTLR_ARE_S_MASK);
131
132 /*
133 * Find the base address of each implemented Redistributor interface.
134 * The number of interfaces should be equal to the number of CPUs in the
135 * system. The memory for saving these addresses has to be allocated by
136 * the platform port
137 */
138 gicv3_rdistif_base_addrs_probe(plat_driver_data->rdistif_base_addrs,
139 plat_driver_data->rdistif_num,
140 plat_driver_data->gicr_base,
141 plat_driver_data->mpidr_to_core_pos);
142
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000143 gicv3_driver_data = plat_driver_data;
Achin Gupta92712a52015-09-03 14:18:02 +0100144
Soby Mathew72645132017-02-14 10:11:52 +0000145 /*
146 * The GIC driver data is initialized by the primary CPU with caches
147 * enabled. When the secondary CPU boots up, it initializes the
148 * GICC/GICR interface with the caches disabled. Hence flush the
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000149 * driver data to ensure coherency. This is not required if the
Andrew F. Davis4d23a642018-07-26 13:50:14 -0500150 * platform has HW_ASSISTED_COHERENCY or WARMBOOT_ENABLE_DCACHE_EARLY
151 * enabled.
Soby Mathew72645132017-02-14 10:11:52 +0000152 */
Andrew F. Davis4d23a642018-07-26 13:50:14 -0500153#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000154 flush_dcache_range((uintptr_t) &gicv3_driver_data,
155 sizeof(gicv3_driver_data));
156 flush_dcache_range((uintptr_t) gicv3_driver_data,
157 sizeof(*gicv3_driver_data));
Soby Mathew72645132017-02-14 10:11:52 +0000158#endif
159
Achin Gupta92712a52015-09-03 14:18:02 +0100160 INFO("GICv3 %s legacy support detected."
161 " ARM GICV3 driver initialized in EL3\n",
162 gicv2_compat ? "with" : "without");
163}
164
165/*******************************************************************************
166 * This function initialises the GIC distributor interface based upon the data
167 * provided by the platform while initialising the driver.
168 ******************************************************************************/
169void gicv3_distif_init(void)
170{
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100171 unsigned int bitmap = 0;
172
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100173 assert(gicv3_driver_data != NULL);
174 assert(gicv3_driver_data->gicd_base != 0U);
Achin Gupta92712a52015-09-03 14:18:02 +0100175
176 assert(IS_IN_EL3());
177
178 /*
179 * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
180 * the ARE_S bit. The Distributor might generate a system error
181 * otherwise.
182 */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000183 gicd_clr_ctlr(gicv3_driver_data->gicd_base,
Achin Gupta92712a52015-09-03 14:18:02 +0100184 CTLR_ENABLE_G0_BIT |
185 CTLR_ENABLE_G1S_BIT |
186 CTLR_ENABLE_G1NS_BIT,
187 RWP_TRUE);
188
189 /* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000190 gicd_set_ctlr(gicv3_driver_data->gicd_base,
Achin Gupta92712a52015-09-03 14:18:02 +0100191 CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);
192
193 /* Set the default attribute of all SPIs */
Daniel Boulby4e83abb2018-05-01 15:15:34 +0100194 gicv3_spis_config_defaults(gicv3_driver_data->gicd_base);
Achin Gupta92712a52015-09-03 14:18:02 +0100195
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100196#if !ERROR_DEPRECATED
197 if (gicv3_driver_data->interrupt_props != NULL) {
198#endif
Daniel Boulby4e83abb2018-05-01 15:15:34 +0100199 bitmap = gicv3_secure_spis_config_props(
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100200 gicv3_driver_data->gicd_base,
201 gicv3_driver_data->interrupt_props,
202 gicv3_driver_data->interrupt_props_num);
203#if !ERROR_DEPRECATED
204 } else {
Dan Handley4d408b52018-03-01 16:00:15 +0000205 /*
206 * Suppress deprecated declaration warnings in compatibility
207 * function
208 */
209#pragma GCC diagnostic push
210#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
211
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100212 assert(gicv3_driver_data->g1s_interrupt_array ||
213 gicv3_driver_data->g0_interrupt_array);
214
215 /* Configure the G1S SPIs */
216 if (gicv3_driver_data->g1s_interrupt_array) {
Daniel Boulby4e83abb2018-05-01 15:15:34 +0100217 gicv3_secure_spis_config(gicv3_driver_data->gicd_base,
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000218 gicv3_driver_data->g1s_interrupt_num,
219 gicv3_driver_data->g1s_interrupt_array,
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000220 INTR_GROUP1S);
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100221 bitmap |= CTLR_ENABLE_G1S_BIT;
222 }
Achin Gupta92712a52015-09-03 14:18:02 +0100223
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100224 /* Configure the G0 SPIs */
225 if (gicv3_driver_data->g0_interrupt_array) {
Daniel Boulby4e83abb2018-05-01 15:15:34 +0100226 gicv3_secure_spis_config(gicv3_driver_data->gicd_base,
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000227 gicv3_driver_data->g0_interrupt_num,
228 gicv3_driver_data->g0_interrupt_array,
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000229 INTR_GROUP0);
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100230 bitmap |= CTLR_ENABLE_G0_BIT;
231 }
Dan Handley4d408b52018-03-01 16:00:15 +0000232#pragma GCC diagnostic pop
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100233 }
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100234#endif
Achin Gupta92712a52015-09-03 14:18:02 +0100235
236 /* Enable the secure SPIs now that they have been configured */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000237 gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
Achin Gupta92712a52015-09-03 14:18:02 +0100238}
239
240/*******************************************************************************
241 * This function initialises the GIC Redistributor interface of the calling CPU
242 * (identified by the 'proc_num' parameter) based upon the data provided by the
243 * platform while initialising the driver.
244 ******************************************************************************/
245void gicv3_rdistif_init(unsigned int proc_num)
246{
247 uintptr_t gicr_base;
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100248 unsigned int bitmap = 0U;
Jeenu Viswambharan88d8f452017-11-07 08:38:23 +0000249 uint32_t ctlr;
Achin Gupta92712a52015-09-03 14:18:02 +0100250
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100251 assert(gicv3_driver_data != NULL);
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000252 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100253 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
254 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan88d8f452017-11-07 08:38:23 +0000255
256 ctlr = gicd_read_ctlr(gicv3_driver_data->gicd_base);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100257 assert((ctlr & CTLR_ARE_S_BIT) != 0U);
Achin Gupta92712a52015-09-03 14:18:02 +0100258
259 assert(IS_IN_EL3());
260
Jeenu Viswambharan76647d52016-12-09 11:03:15 +0000261 /* Power on redistributor */
262 gicv3_rdistif_on(proc_num);
263
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000264 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Gupta92712a52015-09-03 14:18:02 +0100265
266 /* Set the default attribute of all SGIs and PPIs */
Daniel Boulby4e83abb2018-05-01 15:15:34 +0100267 gicv3_ppi_sgi_config_defaults(gicr_base);
Achin Gupta92712a52015-09-03 14:18:02 +0100268
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100269#if !ERROR_DEPRECATED
270 if (gicv3_driver_data->interrupt_props != NULL) {
271#endif
Daniel Boulby4e83abb2018-05-01 15:15:34 +0100272 bitmap = gicv3_secure_ppi_sgi_config_props(gicr_base,
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100273 gicv3_driver_data->interrupt_props,
274 gicv3_driver_data->interrupt_props_num);
275#if !ERROR_DEPRECATED
276 } else {
Dan Handley4d408b52018-03-01 16:00:15 +0000277 /*
278 * Suppress deprecated declaration warnings in compatibility
279 * function
280 */
281#pragma GCC diagnostic push
282#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
283
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100284 assert(gicv3_driver_data->g1s_interrupt_array ||
285 gicv3_driver_data->g0_interrupt_array);
286
287 /* Configure the G1S SGIs/PPIs */
288 if (gicv3_driver_data->g1s_interrupt_array) {
Daniel Boulby4e83abb2018-05-01 15:15:34 +0100289 gicv3_secure_ppi_sgi_config(gicr_base,
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000290 gicv3_driver_data->g1s_interrupt_num,
291 gicv3_driver_data->g1s_interrupt_array,
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100292 INTR_GROUP1S);
Jeenu Viswambharan88d8f452017-11-07 08:38:23 +0000293 bitmap |= CTLR_ENABLE_G1S_BIT;
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100294 }
Achin Gupta92712a52015-09-03 14:18:02 +0100295
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100296 /* Configure the G0 SGIs/PPIs */
297 if (gicv3_driver_data->g0_interrupt_array) {
Daniel Boulby4e83abb2018-05-01 15:15:34 +0100298 gicv3_secure_ppi_sgi_config(gicr_base,
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000299 gicv3_driver_data->g0_interrupt_num,
300 gicv3_driver_data->g0_interrupt_array,
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100301 INTR_GROUP0);
Jeenu Viswambharan88d8f452017-11-07 08:38:23 +0000302 bitmap |= CTLR_ENABLE_G0_BIT;
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100303 }
Dan Handley4d408b52018-03-01 16:00:15 +0000304#pragma GCC diagnostic pop
Yatharth Kochar3f00a892016-09-06 11:48:05 +0100305 }
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100306#endif
Jeenu Viswambharan88d8f452017-11-07 08:38:23 +0000307
308 /* Enable interrupt groups as required, if not already */
309 if ((ctlr & bitmap) != bitmap)
310 gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
Achin Gupta92712a52015-09-03 14:18:02 +0100311}
312
313/*******************************************************************************
Jeenu Viswambharan76647d52016-12-09 11:03:15 +0000314 * Functions to perform power operations on GIC Redistributor
315 ******************************************************************************/
316void gicv3_rdistif_off(unsigned int proc_num)
317{
318 return;
319}
320
321void gicv3_rdistif_on(unsigned int proc_num)
322{
323 return;
324}
325
326/*******************************************************************************
Achin Gupta92712a52015-09-03 14:18:02 +0100327 * This function enables the GIC CPU interface of the calling CPU using only
328 * system register accesses.
329 ******************************************************************************/
330void gicv3_cpuif_enable(unsigned int proc_num)
331{
332 uintptr_t gicr_base;
333 unsigned int scr_el3;
334 unsigned int icc_sre_el3;
335
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100336 assert(gicv3_driver_data != NULL);
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000337 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100338 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Achin Gupta92712a52015-09-03 14:18:02 +0100339 assert(IS_IN_EL3());
340
341 /* Mark the connected core as awake */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000342 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Gupta92712a52015-09-03 14:18:02 +0100343 gicv3_rdistif_mark_core_awake(gicr_base);
344
345 /* Disable the legacy interrupt bypass */
346 icc_sre_el3 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT;
347
348 /*
349 * Enable system register access for EL3 and allow lower exception
350 * levels to configure the same for themselves. If the legacy mode is
351 * not supported, the SRE bit is RAO/WI
352 */
353 icc_sre_el3 |= (ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT);
354 write_icc_sre_el3(read_icc_sre_el3() | icc_sre_el3);
355
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100356 scr_el3 = (uint32_t) read_scr_el3();
Achin Gupta92712a52015-09-03 14:18:02 +0100357
358 /*
359 * Switch to NS state to write Non secure ICC_SRE_EL1 and
360 * ICC_SRE_EL2 registers.
361 */
362 write_scr_el3(scr_el3 | SCR_NS_BIT);
363 isb();
364
365 write_icc_sre_el2(read_icc_sre_el2() | icc_sre_el3);
366 write_icc_sre_el1(ICC_SRE_SRE_BIT);
367 isb();
368
369 /* Switch to secure state. */
370 write_scr_el3(scr_el3 & (~SCR_NS_BIT));
371 isb();
372
373 /* Program the idle priority in the PMR */
374 write_icc_pmr_el1(GIC_PRI_MASK);
375
376 /* Enable Group0 interrupts */
377 write_icc_igrpen0_el1(IGRPEN1_EL1_ENABLE_G0_BIT);
378
379 /* Enable Group1 Secure interrupts */
380 write_icc_igrpen1_el3(read_icc_igrpen1_el3() |
381 IGRPEN1_EL3_ENABLE_G1S_BIT);
382
383 /* Write the secure ICC_SRE_EL1 register */
384 write_icc_sre_el1(ICC_SRE_SRE_BIT);
385 isb();
386}
387
388/*******************************************************************************
389 * This function disables the GIC CPU interface of the calling CPU using
390 * only system register accesses.
391 ******************************************************************************/
392void gicv3_cpuif_disable(unsigned int proc_num)
393{
394 uintptr_t gicr_base;
395
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100396 assert(gicv3_driver_data != NULL);
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000397 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100398 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Achin Gupta92712a52015-09-03 14:18:02 +0100399
400 assert(IS_IN_EL3());
401
402 /* Disable legacy interrupt bypass */
403 write_icc_sre_el3(read_icc_sre_el3() |
404 (ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT));
405
406 /* Disable Group0 interrupts */
407 write_icc_igrpen0_el1(read_icc_igrpen0_el1() &
408 ~IGRPEN1_EL1_ENABLE_G0_BIT);
409
Sudeep Holla869e3db2016-08-04 16:14:50 +0100410 /* Disable Group1 Secure and Non-Secure interrupts */
Achin Gupta92712a52015-09-03 14:18:02 +0100411 write_icc_igrpen1_el3(read_icc_igrpen1_el3() &
Sudeep Holla869e3db2016-08-04 16:14:50 +0100412 ~(IGRPEN1_EL3_ENABLE_G1NS_BIT |
413 IGRPEN1_EL3_ENABLE_G1S_BIT));
Achin Gupta92712a52015-09-03 14:18:02 +0100414
415 /* Synchronise accesses to group enable registers */
416 isb();
417
418 /* Mark the connected core as asleep */
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000419 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Gupta92712a52015-09-03 14:18:02 +0100420 gicv3_rdistif_mark_core_asleep(gicr_base);
421}
422
423/*******************************************************************************
424 * This function returns the id of the highest priority pending interrupt at
425 * the GIC cpu interface.
426 ******************************************************************************/
427unsigned int gicv3_get_pending_interrupt_id(void)
428{
429 unsigned int id;
430
431 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100432 id = (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
Achin Gupta92712a52015-09-03 14:18:02 +0100433
434 /*
435 * If the ID is special identifier corresponding to G1S or G1NS
436 * interrupt, then read the highest pending group 1 interrupt.
437 */
438 if ((id == PENDING_G1S_INTID) || (id == PENDING_G1NS_INTID))
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100439 return (uint32_t)read_icc_hppir1_el1() & HPPIR1_EL1_INTID_MASK;
Achin Gupta92712a52015-09-03 14:18:02 +0100440
441 return id;
442}
443
444/*******************************************************************************
445 * This function returns the type of the highest priority pending interrupt at
446 * the GIC cpu interface. The return values can be one of the following :
447 * PENDING_G1S_INTID : The interrupt type is secure Group 1.
448 * PENDING_G1NS_INTID : The interrupt type is non secure Group 1.
449 * 0 - 1019 : The interrupt type is secure Group 0.
450 * GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
451 * sufficient priority to be signaled
452 ******************************************************************************/
453unsigned int gicv3_get_pending_interrupt_type(void)
454{
455 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100456 return (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
Achin Gupta92712a52015-09-03 14:18:02 +0100457}
458
459/*******************************************************************************
460 * This function returns the type of the interrupt id depending upon the group
461 * this interrupt has been configured under by the interrupt controller i.e.
462 * group0 or group1 Secure / Non Secure. The return value can be one of the
463 * following :
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000464 * INTR_GROUP0 : The interrupt type is a Secure Group 0 interrupt
465 * INTR_GROUP1S : The interrupt type is a Secure Group 1 secure interrupt
466 * INTR_GROUP1NS: The interrupt type is a Secure Group 1 non secure
Achin Gupta92712a52015-09-03 14:18:02 +0100467 * interrupt.
468 ******************************************************************************/
469unsigned int gicv3_get_interrupt_type(unsigned int id,
470 unsigned int proc_num)
471{
472 unsigned int igroup, grpmodr;
473 uintptr_t gicr_base;
474
475 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100476 assert(gicv3_driver_data != NULL);
Achin Gupta92712a52015-09-03 14:18:02 +0100477
478 /* Ensure the parameters are valid */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100479 assert((id < PENDING_G1S_INTID) || (id >= MIN_LPI_ID));
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000480 assert(proc_num < gicv3_driver_data->rdistif_num);
Achin Gupta92712a52015-09-03 14:18:02 +0100481
482 /* All LPI interrupts are Group 1 non secure */
483 if (id >= MIN_LPI_ID)
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000484 return INTR_GROUP1NS;
Achin Gupta92712a52015-09-03 14:18:02 +0100485
486 if (id < MIN_SPI_ID) {
Andrew F. Davis25a17a22018-08-30 14:30:54 -0500487 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000488 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Gupta92712a52015-09-03 14:18:02 +0100489 igroup = gicr_get_igroupr0(gicr_base, id);
490 grpmodr = gicr_get_igrpmodr0(gicr_base, id);
491 } else {
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100492 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharand7a901e2016-12-06 16:15:22 +0000493 igroup = gicd_get_igroupr(gicv3_driver_data->gicd_base, id);
494 grpmodr = gicd_get_igrpmodr(gicv3_driver_data->gicd_base, id);
Achin Gupta92712a52015-09-03 14:18:02 +0100495 }
496
497 /*
498 * If the IGROUP bit is set, then it is a Group 1 Non secure
499 * interrupt
500 */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100501 if (igroup != 0U)
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000502 return INTR_GROUP1NS;
Achin Gupta92712a52015-09-03 14:18:02 +0100503
504 /* If the GRPMOD bit is set, then it is a Group 1 Secure interrupt */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100505 if (grpmodr != 0U)
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000506 return INTR_GROUP1S;
Achin Gupta92712a52015-09-03 14:18:02 +0100507
508 /* Else it is a Group 0 Secure interrupt */
Soby Mathew5c5c36b2015-12-03 14:12:54 +0000509 return INTR_GROUP0;
Achin Gupta92712a52015-09-03 14:18:02 +0100510}
Soby Mathew327548c2017-07-13 15:19:51 +0100511
512/*****************************************************************************
Soby Mathewf6f1a322017-07-18 16:12:45 +0100513 * Function to save and disable the GIC ITS register context. The power
514 * management of GIC ITS is implementation-defined and this function doesn't
515 * save any memory structures required to support ITS. As the sequence to save
516 * this state is implementation defined, it should be executed in platform
517 * specific code. Calling this function alone and then powering down the GIC and
518 * ITS without implementing the aforementioned platform specific code will
519 * corrupt the ITS state.
520 *
521 * This function must be invoked after the GIC CPU interface is disabled.
522 *****************************************************************************/
523void gicv3_its_save_disable(uintptr_t gits_base, gicv3_its_ctx_t * const its_ctx)
524{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100525 unsigned int i;
Soby Mathewf6f1a322017-07-18 16:12:45 +0100526
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100527 assert(gicv3_driver_data != NULL);
Soby Mathewf6f1a322017-07-18 16:12:45 +0100528 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100529 assert(its_ctx != NULL);
530 assert(gits_base != 0U);
Soby Mathewf6f1a322017-07-18 16:12:45 +0100531
532 its_ctx->gits_ctlr = gits_read_ctlr(gits_base);
533
534 /* Disable the ITS */
535 gits_write_ctlr(gits_base, its_ctx->gits_ctlr &
536 (~GITS_CTLR_ENABLED_BIT));
537
538 /* Wait for quiescent state */
539 gits_wait_for_quiescent_bit(gits_base);
540
541 its_ctx->gits_cbaser = gits_read_cbaser(gits_base);
542 its_ctx->gits_cwriter = gits_read_cwriter(gits_base);
543
544 for (i = 0; i < ARRAY_SIZE(its_ctx->gits_baser); i++)
545 its_ctx->gits_baser[i] = gits_read_baser(gits_base, i);
546}
547
548/*****************************************************************************
549 * Function to restore the GIC ITS register context. The power
550 * management of GIC ITS is implementation defined and this function doesn't
551 * restore any memory structures required to support ITS. The assumption is
552 * that these structures are in memory and are retained during system suspend.
553 *
554 * This must be invoked before the GIC CPU interface is enabled.
555 *****************************************************************************/
556void gicv3_its_restore(uintptr_t gits_base, const gicv3_its_ctx_t * const its_ctx)
557{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100558 unsigned int i;
Soby Mathewf6f1a322017-07-18 16:12:45 +0100559
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100560 assert(gicv3_driver_data != NULL);
Soby Mathewf6f1a322017-07-18 16:12:45 +0100561 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100562 assert(its_ctx != NULL);
563 assert(gits_base != 0U);
Soby Mathewf6f1a322017-07-18 16:12:45 +0100564
565 /* Assert that the GITS is disabled and quiescent */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100566 assert((gits_read_ctlr(gits_base) & GITS_CTLR_ENABLED_BIT) == 0U);
567 assert((gits_read_ctlr(gits_base) & GITS_CTLR_QUIESCENT_BIT) != 0U);
Soby Mathewf6f1a322017-07-18 16:12:45 +0100568
569 gits_write_cbaser(gits_base, its_ctx->gits_cbaser);
570 gits_write_cwriter(gits_base, its_ctx->gits_cwriter);
571
572 for (i = 0; i < ARRAY_SIZE(its_ctx->gits_baser); i++)
573 gits_write_baser(gits_base, i, its_ctx->gits_baser[i]);
574
575 /* Restore the ITS CTLR but leave the ITS disabled */
576 gits_write_ctlr(gits_base, its_ctx->gits_ctlr &
577 (~GITS_CTLR_ENABLED_BIT));
578}
579
580/*****************************************************************************
Soby Mathew327548c2017-07-13 15:19:51 +0100581 * Function to save the GIC Redistributor register context. This function
582 * must be invoked after CPU interface disable and prior to Distributor save.
583 *****************************************************************************/
584void gicv3_rdistif_save(unsigned int proc_num, gicv3_redist_ctx_t * const rdist_ctx)
585{
586 uintptr_t gicr_base;
587 unsigned int int_id;
588
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100589 assert(gicv3_driver_data != NULL);
Soby Mathew327548c2017-07-13 15:19:51 +0100590 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100591 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Soby Mathew327548c2017-07-13 15:19:51 +0100592 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100593 assert(rdist_ctx != NULL);
Soby Mathew327548c2017-07-13 15:19:51 +0100594
595 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
596
597 /*
598 * Wait for any write to GICR_CTLR to complete before trying to save any
599 * state.
600 */
601 gicr_wait_for_pending_write(gicr_base);
602
603 rdist_ctx->gicr_ctlr = gicr_read_ctlr(gicr_base);
604
605 rdist_ctx->gicr_propbaser = gicr_read_propbaser(gicr_base);
606 rdist_ctx->gicr_pendbaser = gicr_read_pendbaser(gicr_base);
607
608 rdist_ctx->gicr_igroupr0 = gicr_read_igroupr0(gicr_base);
609 rdist_ctx->gicr_isenabler0 = gicr_read_isenabler0(gicr_base);
610 rdist_ctx->gicr_ispendr0 = gicr_read_ispendr0(gicr_base);
611 rdist_ctx->gicr_isactiver0 = gicr_read_isactiver0(gicr_base);
612 rdist_ctx->gicr_icfgr0 = gicr_read_icfgr0(gicr_base);
613 rdist_ctx->gicr_icfgr1 = gicr_read_icfgr1(gicr_base);
614 rdist_ctx->gicr_igrpmodr0 = gicr_read_igrpmodr0(gicr_base);
615 rdist_ctx->gicr_nsacr = gicr_read_nsacr(gicr_base);
616 for (int_id = MIN_SGI_ID; int_id < TOTAL_PCPU_INTR_NUM;
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100617 int_id += (1U << IPRIORITYR_SHIFT)) {
Soby Mathew327548c2017-07-13 15:19:51 +0100618 rdist_ctx->gicr_ipriorityr[(int_id - MIN_SGI_ID) >> IPRIORITYR_SHIFT] =
619 gicr_read_ipriorityr(gicr_base, int_id);
620 }
621
622
623 /*
624 * Call the pre-save hook that implements the IMP DEF sequence that may
625 * be required on some GIC implementations. As this may need to access
626 * the Redistributor registers, we pass it proc_num.
627 */
628 gicv3_distif_pre_save(proc_num);
629}
630
631/*****************************************************************************
632 * Function to restore the GIC Redistributor register context. We disable
633 * LPI and per-cpu interrupts before we start restore of the Redistributor.
634 * This function must be invoked after Distributor restore but prior to
635 * CPU interface enable. The pending and active interrupts are restored
636 * after the interrupts are fully configured and enabled.
637 *****************************************************************************/
638void gicv3_rdistif_init_restore(unsigned int proc_num,
639 const gicv3_redist_ctx_t * const rdist_ctx)
640{
641 uintptr_t gicr_base;
642 unsigned int int_id;
643
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100644 assert(gicv3_driver_data != NULL);
Soby Mathew327548c2017-07-13 15:19:51 +0100645 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100646 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Soby Mathew327548c2017-07-13 15:19:51 +0100647 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100648 assert(rdist_ctx != NULL);
Soby Mathew327548c2017-07-13 15:19:51 +0100649
650 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
651
652 /* Power on redistributor */
653 gicv3_rdistif_on(proc_num);
654
655 /*
656 * Call the post-restore hook that implements the IMP DEF sequence that
657 * may be required on some GIC implementations. As this may need to
658 * access the Redistributor registers, we pass it proc_num.
659 */
660 gicv3_distif_post_restore(proc_num);
661
662 /*
663 * Disable all SGIs (imp. def.)/PPIs before configuring them. This is a
664 * more scalable approach as it avoids clearing the enable bits in the
665 * GICD_CTLR
666 */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100667 gicr_write_icenabler0(gicr_base, ~0U);
Soby Mathew327548c2017-07-13 15:19:51 +0100668 /* Wait for pending writes to GICR_ICENABLER */
669 gicr_wait_for_pending_write(gicr_base);
670
671 /*
672 * Disable the LPIs to avoid unpredictable behavior when writing to
673 * GICR_PROPBASER and GICR_PENDBASER.
674 */
675 gicr_write_ctlr(gicr_base,
676 rdist_ctx->gicr_ctlr & ~(GICR_CTLR_EN_LPIS_BIT));
677
678 /* Restore registers' content */
679 gicr_write_propbaser(gicr_base, rdist_ctx->gicr_propbaser);
680 gicr_write_pendbaser(gicr_base, rdist_ctx->gicr_pendbaser);
681
682 gicr_write_igroupr0(gicr_base, rdist_ctx->gicr_igroupr0);
683
684 for (int_id = MIN_SGI_ID; int_id < TOTAL_PCPU_INTR_NUM;
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100685 int_id += (1U << IPRIORITYR_SHIFT)) {
Soby Mathew327548c2017-07-13 15:19:51 +0100686 gicr_write_ipriorityr(gicr_base, int_id,
687 rdist_ctx->gicr_ipriorityr[
688 (int_id - MIN_SGI_ID) >> IPRIORITYR_SHIFT]);
689 }
690
691 gicr_write_icfgr0(gicr_base, rdist_ctx->gicr_icfgr0);
692 gicr_write_icfgr1(gicr_base, rdist_ctx->gicr_icfgr1);
693 gicr_write_igrpmodr0(gicr_base, rdist_ctx->gicr_igrpmodr0);
694 gicr_write_nsacr(gicr_base, rdist_ctx->gicr_nsacr);
695
696 /* Restore after group and priorities are set */
697 gicr_write_ispendr0(gicr_base, rdist_ctx->gicr_ispendr0);
698 gicr_write_isactiver0(gicr_base, rdist_ctx->gicr_isactiver0);
699
700 /*
701 * Wait for all writes to the Distributor to complete before enabling
702 * the SGI and PPIs.
703 */
704 gicr_wait_for_upstream_pending_write(gicr_base);
705 gicr_write_isenabler0(gicr_base, rdist_ctx->gicr_isenabler0);
706
707 /*
708 * Restore GICR_CTLR.Enable_LPIs bit and wait for pending writes in case
709 * the first write to GICR_CTLR was still in flight (this write only
710 * restores GICR_CTLR.Enable_LPIs and no waiting is required for this
711 * bit).
712 */
713 gicr_write_ctlr(gicr_base, rdist_ctx->gicr_ctlr);
714 gicr_wait_for_pending_write(gicr_base);
715}
716
717/*****************************************************************************
718 * Function to save the GIC Distributor register context. This function
719 * must be invoked after CPU interface disable and Redistributor save.
720 *****************************************************************************/
721void gicv3_distif_save(gicv3_dist_ctx_t * const dist_ctx)
722{
723 unsigned int num_ints;
724
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100725 assert(gicv3_driver_data != NULL);
726 assert(gicv3_driver_data->gicd_base != 0U);
Soby Mathew327548c2017-07-13 15:19:51 +0100727 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100728 assert(dist_ctx != NULL);
Soby Mathew327548c2017-07-13 15:19:51 +0100729
730 uintptr_t gicd_base = gicv3_driver_data->gicd_base;
731
732 num_ints = gicd_read_typer(gicd_base);
733 num_ints &= TYPER_IT_LINES_NO_MASK;
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100734 num_ints = (num_ints + 1U) << 5;
Soby Mathew327548c2017-07-13 15:19:51 +0100735
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100736 assert(num_ints <= (MAX_SPI_ID + 1U));
Soby Mathew327548c2017-07-13 15:19:51 +0100737
738 /* Wait for pending write to complete */
739 gicd_wait_for_pending_write(gicd_base);
740
741 /* Save the GICD_CTLR */
742 dist_ctx->gicd_ctlr = gicd_read_ctlr(gicd_base);
743
744 /* Save GICD_IGROUPR for INTIDs 32 - 1020 */
745 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUPR);
746
747 /* Save GICD_ISENABLER for INT_IDs 32 - 1020 */
748 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLER);
749
750 /* Save GICD_ISPENDR for INTIDs 32 - 1020 */
751 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPENDR);
752
753 /* Save GICD_ISACTIVER for INTIDs 32 - 1020 */
754 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVER);
755
756 /* Save GICD_IPRIORITYR for INTIDs 32 - 1020 */
757 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITYR);
758
759 /* Save GICD_ICFGR for INTIDs 32 - 1020 */
760 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFGR);
761
762 /* Save GICD_IGRPMODR for INTIDs 32 - 1020 */
763 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMODR);
764
765 /* Save GICD_NSACR for INTIDs 32 - 1020 */
766 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSACR);
767
768 /* Save GICD_IROUTER for INTIDs 32 - 1024 */
769 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTER);
770
771 /*
772 * GICD_ITARGETSR<n> and GICD_SPENDSGIR<n> are RAZ/WI when
773 * GICD_CTLR.ARE_(S|NS) bits are set which is the case for our GICv3
774 * driver.
775 */
776}
777
778/*****************************************************************************
779 * Function to restore the GIC Distributor register context. We disable G0, G1S
780 * and G1NS interrupt groups before we start restore of the Distributor. This
781 * function must be invoked prior to Redistributor restore and CPU interface
782 * enable. The pending and active interrupts are restored after the interrupts
783 * are fully configured and enabled.
784 *****************************************************************************/
785void gicv3_distif_init_restore(const gicv3_dist_ctx_t * const dist_ctx)
786{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100787 unsigned int num_ints = 0U;
Soby Mathew327548c2017-07-13 15:19:51 +0100788
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100789 assert(gicv3_driver_data != NULL);
790 assert(gicv3_driver_data->gicd_base != 0U);
Soby Mathew327548c2017-07-13 15:19:51 +0100791 assert(IS_IN_EL3());
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100792 assert(dist_ctx != NULL);
Soby Mathew327548c2017-07-13 15:19:51 +0100793
794 uintptr_t gicd_base = gicv3_driver_data->gicd_base;
795
796 /*
797 * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
798 * the ARE_S bit. The Distributor might generate a system error
799 * otherwise.
800 */
801 gicd_clr_ctlr(gicd_base,
802 CTLR_ENABLE_G0_BIT |
803 CTLR_ENABLE_G1S_BIT |
804 CTLR_ENABLE_G1NS_BIT,
805 RWP_TRUE);
806
807 /* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
808 gicd_set_ctlr(gicd_base, CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);
809
810 num_ints = gicd_read_typer(gicd_base);
811 num_ints &= TYPER_IT_LINES_NO_MASK;
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100812 num_ints = (num_ints + 1U) << 5;
Soby Mathew327548c2017-07-13 15:19:51 +0100813
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100814 assert(num_ints <= (MAX_SPI_ID + 1U));
Soby Mathew327548c2017-07-13 15:19:51 +0100815
816 /* Restore GICD_IGROUPR for INTIDs 32 - 1020 */
817 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUPR);
818
819 /* Restore GICD_IPRIORITYR for INTIDs 32 - 1020 */
820 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITYR);
821
822 /* Restore GICD_ICFGR for INTIDs 32 - 1020 */
823 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFGR);
824
825 /* Restore GICD_IGRPMODR for INTIDs 32 - 1020 */
826 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMODR);
827
828 /* Restore GICD_NSACR for INTIDs 32 - 1020 */
829 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSACR);
830
831 /* Restore GICD_IROUTER for INTIDs 32 - 1020 */
832 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTER);
833
834 /*
835 * Restore ISENABLER, ISPENDR and ISACTIVER after the interrupts are
836 * configured.
837 */
838
839 /* Restore GICD_ISENABLER for INT_IDs 32 - 1020 */
840 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLER);
841
842 /* Restore GICD_ISPENDR for INTIDs 32 - 1020 */
843 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPENDR);
844
845 /* Restore GICD_ISACTIVER for INTIDs 32 - 1020 */
846 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVER);
847
848 /* Restore the GICD_CTLR */
849 gicd_write_ctlr(gicd_base, dist_ctx->gicd_ctlr);
850 gicd_wait_for_pending_write(gicd_base);
851
852}
Jeenu Viswambharanb1e957e2017-09-22 08:32:09 +0100853
854/*******************************************************************************
855 * This function gets the priority of the interrupt the processor is currently
856 * servicing.
857 ******************************************************************************/
858unsigned int gicv3_get_running_priority(void)
859{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100860 return (unsigned int)read_icc_rpr_el1();
Jeenu Viswambharanb1e957e2017-09-22 08:32:09 +0100861}
Jeenu Viswambharan24e70292017-09-22 08:32:09 +0100862
863/*******************************************************************************
864 * This function checks if the interrupt identified by id is active (whether the
865 * state is either active, or active and pending). The proc_num is used if the
866 * interrupt is SGI or PPI and programs the corresponding Redistributor
867 * interface.
868 ******************************************************************************/
869unsigned int gicv3_get_interrupt_active(unsigned int id, unsigned int proc_num)
870{
871 unsigned int value;
872
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100873 assert(gicv3_driver_data != NULL);
874 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan24e70292017-09-22 08:32:09 +0100875 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100876 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharan24e70292017-09-22 08:32:09 +0100877 assert(id <= MAX_SPI_ID);
878
879 if (id < MIN_SPI_ID) {
880 /* For SGIs and PPIs */
881 value = gicr_get_isactiver0(
882 gicv3_driver_data->rdistif_base_addrs[proc_num], id);
883 } else {
884 value = gicd_get_isactiver(gicv3_driver_data->gicd_base, id);
885 }
886
887 return value;
888}
Jeenu Viswambharan0fcdfff2017-09-22 08:32:09 +0100889
890/*******************************************************************************
891 * This function enables the interrupt identified by id. The proc_num
892 * is used if the interrupt is SGI or PPI, and programs the corresponding
893 * Redistributor interface.
894 ******************************************************************************/
895void gicv3_enable_interrupt(unsigned int id, unsigned int proc_num)
896{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100897 assert(gicv3_driver_data != NULL);
898 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan0fcdfff2017-09-22 08:32:09 +0100899 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100900 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharan0fcdfff2017-09-22 08:32:09 +0100901 assert(id <= MAX_SPI_ID);
902
903 /*
904 * Ensure that any shared variable updates depending on out of band
905 * interrupt trigger are observed before enabling interrupt.
906 */
907 dsbishst();
908 if (id < MIN_SPI_ID) {
909 /* For SGIs and PPIs */
910 gicr_set_isenabler0(
911 gicv3_driver_data->rdistif_base_addrs[proc_num],
912 id);
913 } else {
914 gicd_set_isenabler(gicv3_driver_data->gicd_base, id);
915 }
916}
917
918/*******************************************************************************
919 * This function disables the interrupt identified by id. The proc_num
920 * is used if the interrupt is SGI or PPI, and programs the corresponding
921 * Redistributor interface.
922 ******************************************************************************/
923void gicv3_disable_interrupt(unsigned int id, unsigned int proc_num)
924{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100925 assert(gicv3_driver_data != NULL);
926 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan0fcdfff2017-09-22 08:32:09 +0100927 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100928 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharan0fcdfff2017-09-22 08:32:09 +0100929 assert(id <= MAX_SPI_ID);
930
931 /*
932 * Disable interrupt, and ensure that any shared variable updates
933 * depending on out of band interrupt trigger are observed afterwards.
934 */
935 if (id < MIN_SPI_ID) {
936 /* For SGIs and PPIs */
937 gicr_set_icenabler0(
938 gicv3_driver_data->rdistif_base_addrs[proc_num],
939 id);
940
941 /* Write to clear enable requires waiting for pending writes */
942 gicr_wait_for_pending_write(
943 gicv3_driver_data->rdistif_base_addrs[proc_num]);
944 } else {
945 gicd_set_icenabler(gicv3_driver_data->gicd_base, id);
946
947 /* Write to clear enable requires waiting for pending writes */
948 gicd_wait_for_pending_write(gicv3_driver_data->gicd_base);
949 }
950
951 dsbishst();
952}
Jeenu Viswambharan447b89d2017-09-22 08:32:09 +0100953
954/*******************************************************************************
955 * This function sets the interrupt priority as supplied for the given interrupt
956 * id.
957 ******************************************************************************/
958void gicv3_set_interrupt_priority(unsigned int id, unsigned int proc_num,
959 unsigned int priority)
960{
961 uintptr_t gicr_base;
962
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100963 assert(gicv3_driver_data != NULL);
964 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan447b89d2017-09-22 08:32:09 +0100965 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100966 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharan447b89d2017-09-22 08:32:09 +0100967 assert(id <= MAX_SPI_ID);
968
969 if (id < MIN_SPI_ID) {
970 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
971 gicr_set_ipriorityr(gicr_base, id, priority);
972 } else {
973 gicd_set_ipriorityr(gicv3_driver_data->gicd_base, id, priority);
974 }
975}
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +0100976
977/*******************************************************************************
978 * This function assigns group for the interrupt identified by id. The proc_num
979 * is used if the interrupt is SGI or PPI, and programs the corresponding
980 * Redistributor interface. The group can be any of GICV3_INTR_GROUP*
981 ******************************************************************************/
982void gicv3_set_interrupt_type(unsigned int id, unsigned int proc_num,
983 unsigned int type)
984{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100985 bool igroup = false, grpmod = false;
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +0100986 uintptr_t gicr_base;
987
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100988 assert(gicv3_driver_data != NULL);
989 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +0100990 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100991 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +0100992
993 switch (type) {
994 case INTR_GROUP1S:
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100995 igroup = false;
996 grpmod = true;
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +0100997 break;
998 case INTR_GROUP0:
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100999 igroup = false;
1000 grpmod = false;
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +01001001 break;
1002 case INTR_GROUP1NS:
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001003 igroup = true;
1004 grpmod = false;
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +01001005 break;
1006 default:
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001007 assert(false);
Jonathan Wright39b42212018-03-13 15:24:29 +00001008 break;
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +01001009 }
1010
1011 if (id < MIN_SPI_ID) {
1012 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
1013 if (igroup)
1014 gicr_set_igroupr0(gicr_base, id);
1015 else
1016 gicr_clr_igroupr0(gicr_base, id);
1017
1018 if (grpmod)
1019 gicr_set_igrpmodr0(gicr_base, id);
1020 else
1021 gicr_clr_igrpmodr0(gicr_base, id);
1022 } else {
1023 /* Serialize read-modify-write to Distributor registers */
1024 spin_lock(&gic_lock);
1025 if (igroup)
1026 gicd_set_igroupr(gicv3_driver_data->gicd_base, id);
1027 else
1028 gicd_clr_igroupr(gicv3_driver_data->gicd_base, id);
1029
1030 if (grpmod)
1031 gicd_set_igrpmodr(gicv3_driver_data->gicd_base, id);
1032 else
1033 gicd_clr_igrpmodr(gicv3_driver_data->gicd_base, id);
1034 spin_unlock(&gic_lock);
1035 }
1036}
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +01001037
1038/*******************************************************************************
1039 * This function raises the specified Secure Group 0 SGI.
1040 *
1041 * The target parameter must be a valid MPIDR in the system.
1042 ******************************************************************************/
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001043void gicv3_raise_secure_g0_sgi(unsigned int sgi_num, u_register_t target)
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +01001044{
1045 unsigned int tgt, aff3, aff2, aff1, aff0;
1046 uint64_t sgi_val;
1047
1048 /* Verify interrupt number is in the SGI range */
1049 assert((sgi_num >= MIN_SGI_ID) && (sgi_num < MIN_PPI_ID));
1050
1051 /* Extract affinity fields from target */
1052 aff0 = MPIDR_AFFLVL0_VAL(target);
1053 aff1 = MPIDR_AFFLVL1_VAL(target);
1054 aff2 = MPIDR_AFFLVL2_VAL(target);
1055 aff3 = MPIDR_AFFLVL3_VAL(target);
1056
1057 /*
1058 * Make target list from affinity 0, and ensure GICv3 SGI can target
1059 * this PE.
1060 */
1061 assert(aff0 < GICV3_MAX_SGI_TARGETS);
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001062 tgt = BIT_32(aff0);
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +01001063
1064 /* Raise SGI to PE specified by its affinity */
1065 sgi_val = GICV3_SGIR_VALUE(aff3, aff2, aff1, sgi_num, SGIR_IRM_TO_AFF,
1066 tgt);
1067
1068 /*
1069 * Ensure that any shared variable updates depending on out of band
1070 * interrupt trigger are observed before raising SGI.
1071 */
1072 dsbishst();
1073 write_icc_sgi0r_el1(sgi_val);
1074 isb();
1075}
Jeenu Viswambharandce70b32017-09-22 08:32:09 +01001076
1077/*******************************************************************************
1078 * This function sets the interrupt routing for the given SPI interrupt id.
1079 * The interrupt routing is specified in routing mode and mpidr.
1080 *
1081 * The routing mode can be either of:
1082 * - GICV3_IRM_ANY
1083 * - GICV3_IRM_PE
1084 *
1085 * The mpidr is the affinity of the PE to which the interrupt will be routed,
1086 * and is ignored for routing mode GICV3_IRM_ANY.
1087 ******************************************************************************/
1088void gicv3_set_spi_routing(unsigned int id, unsigned int irm, u_register_t mpidr)
1089{
1090 unsigned long long aff;
1091 uint64_t router;
1092
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001093 assert(gicv3_driver_data != NULL);
1094 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharandce70b32017-09-22 08:32:09 +01001095
1096 assert((irm == GICV3_IRM_ANY) || (irm == GICV3_IRM_PE));
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001097 assert((id >= MIN_SPI_ID) && (id <= MAX_SPI_ID));
Jeenu Viswambharandce70b32017-09-22 08:32:09 +01001098
1099 aff = gicd_irouter_val_from_mpidr(mpidr, irm);
1100 gicd_write_irouter(gicv3_driver_data->gicd_base, id, aff);
1101
1102 /*
1103 * In implementations that do not require 1 of N distribution of SPIs,
1104 * IRM might be RAZ/WI. Read back and verify IRM bit.
1105 */
1106 if (irm == GICV3_IRM_ANY) {
1107 router = gicd_read_irouter(gicv3_driver_data->gicd_base, id);
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001108 if (((router >> IROUTER_IRM_SHIFT) & IROUTER_IRM_MASK) == 0U) {
Jeenu Viswambharandce70b32017-09-22 08:32:09 +01001109 ERROR("GICv3 implementation doesn't support routing ANY\n");
1110 panic();
1111 }
1112 }
1113}
Jeenu Viswambharaneb1c12c2017-09-22 08:32:09 +01001114
1115/*******************************************************************************
1116 * This function clears the pending status of an interrupt identified by id.
1117 * The proc_num is used if the interrupt is SGI or PPI, and programs the
1118 * corresponding Redistributor interface.
1119 ******************************************************************************/
1120void gicv3_clear_interrupt_pending(unsigned int id, unsigned int proc_num)
1121{
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001122 assert(gicv3_driver_data != NULL);
1123 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharaneb1c12c2017-09-22 08:32:09 +01001124 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001125 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharaneb1c12c2017-09-22 08:32:09 +01001126
1127 /*
1128 * Clear pending interrupt, and ensure that any shared variable updates
1129 * depending on out of band interrupt trigger are observed afterwards.
1130 */
1131 if (id < MIN_SPI_ID) {
1132 /* For SGIs and PPIs */
1133 gicr_set_icpendr0(gicv3_driver_data->rdistif_base_addrs[proc_num],
1134 id);
1135 } else {
1136 gicd_set_icpendr(gicv3_driver_data->gicd_base, id);
1137 }
1138 dsbishst();
1139}
1140
1141/*******************************************************************************
1142 * This function sets the pending status of an interrupt identified by id.
1143 * The proc_num is used if the interrupt is SGI or PPI and programs the
1144 * corresponding Redistributor interface.
1145 ******************************************************************************/
1146void gicv3_set_interrupt_pending(unsigned int id, unsigned int proc_num)
1147{
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001148 assert(gicv3_driver_data != NULL);
1149 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharaneb1c12c2017-09-22 08:32:09 +01001150 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001151 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharaneb1c12c2017-09-22 08:32:09 +01001152
1153 /*
1154 * Ensure that any shared variable updates depending on out of band
1155 * interrupt trigger are observed before setting interrupt pending.
1156 */
1157 dsbishst();
1158 if (id < MIN_SPI_ID) {
1159 /* For SGIs and PPIs */
1160 gicr_set_ispendr0(gicv3_driver_data->rdistif_base_addrs[proc_num],
1161 id);
1162 } else {
1163 gicd_set_ispendr(gicv3_driver_data->gicd_base, id);
1164 }
1165}
Jeenu Viswambharan62505072017-09-22 08:32:09 +01001166
1167/*******************************************************************************
1168 * This function sets the PMR register with the supplied value. Returns the
1169 * original PMR.
1170 ******************************************************************************/
1171unsigned int gicv3_set_pmr(unsigned int mask)
1172{
1173 unsigned int old_mask;
1174
Antonio Nino Diazca994e72018-08-21 10:02:33 +01001175 old_mask = (uint32_t) read_icc_pmr_el1();
Jeenu Viswambharan62505072017-09-22 08:32:09 +01001176
1177 /*
1178 * Order memory updates w.r.t. PMR write, and ensure they're visible
1179 * before potential out of band interrupt trigger because of PMR update.
1180 * PMR system register writes are self-synchronizing, so no ISB required
1181 * thereafter.
1182 */
1183 dsbishst();
1184 write_icc_pmr_el1(mask);
1185
1186 return old_mask;
1187}