blob: 7a67384925a233feb7650ece234b998f5f169435 [file] [log] [blame]
Achin Guptadf373732015-09-03 14:18:02 +01001/*
Manish V Badarkhee1b15b02022-05-09 21:55:19 +01002 * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved.
Varun Wadekar2e482842023-03-08 16:47:38 +00003 * Copyright (c) 2023, NVIDIA Corporation. All rights reserved.
Achin Guptadf373732015-09-03 14:18:02 +01004 *
dp-arm82cb2c12017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Achin Guptadf373732015-09-03 14:18:02 +01006 */
7
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00008#include <assert.h>
9
Achin Guptadf373732015-09-03 14:18:02 +010010#include <arch.h>
11#include <arch_helpers.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000012#include <common/debug.h>
13#include <common/interrupt_props.h>
Varun Wadekar2e482842023-03-08 16:47:38 +000014#include <drivers/arm/gic600_multichip.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000015#include <drivers/arm/gicv3.h>
16#include <lib/spinlock.h>
17
Achin Guptadf373732015-09-03 14:18:02 +010018#include "gicv3_private.h"
19
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +000020const gicv3_driver_data_t *gicv3_driver_data;
Achin Guptadf373732015-09-03 14:18:02 +010021
Jeenu Viswambharand7806992016-12-09 11:03:15 +000022/*
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010023 * Spinlock to guard registers needing read-modify-write. APIs protected by this
24 * spinlock are used either at boot time (when only a single CPU is active), or
25 * when the system is fully coherent.
26 */
Roberto Vargas1af540e2018-02-12 12:36:17 +000027static spinlock_t gic_lock;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010028
29/*
Jeenu Viswambharand7806992016-12-09 11:03:15 +000030 * Redistributor power operations are weakly bound so that they can be
31 * overridden
32 */
33#pragma weak gicv3_rdistif_off
34#pragma weak gicv3_rdistif_on
35
Alexei Fedorov8f3ad762020-04-06 16:27:54 +010036/* Check interrupt ID for SGI/(E)PPI and (E)SPIs */
37static bool is_sgi_ppi(unsigned int id);
38
39/*
40 * Helper macros to save and restore GICR and GICD registers
41 * corresponding to their numbers to and from the context
42 */
43#define RESTORE_GICR_REG(base, ctx, name, i) \
44 gicr_write_##name((base), (i), (ctx)->gicr_##name[(i)])
45
46#define SAVE_GICR_REG(base, ctx, name, i) \
47 (ctx)->gicr_##name[(i)] = gicr_read_##name((base), (i))
Soby Mathewebf1ca12017-07-13 15:19:51 +010048
49/* Helper macros to save and restore GICD registers to and from the context */
50#define RESTORE_GICD_REGS(base, ctx, intr_num, reg, REG) \
51 do { \
Alexei Fedorov8f3ad762020-04-06 16:27:54 +010052 for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num);\
53 int_id += (1U << REG##R_SHIFT)) { \
54 gicd_write_##reg((base), int_id, \
55 (ctx)->gicd_##reg[(int_id - MIN_SPI_ID) >> \
56 REG##R_SHIFT]); \
Soby Mathewebf1ca12017-07-13 15:19:51 +010057 } \
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +010058 } while (false)
Soby Mathewebf1ca12017-07-13 15:19:51 +010059
60#define SAVE_GICD_REGS(base, ctx, intr_num, reg, REG) \
61 do { \
Alexei Fedorov8f3ad762020-04-06 16:27:54 +010062 for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num);\
63 int_id += (1U << REG##R_SHIFT)) { \
64 (ctx)->gicd_##reg[(int_id - MIN_SPI_ID) >> \
65 REG##R_SHIFT] = gicd_read_##reg((base), int_id); \
Soby Mathewebf1ca12017-07-13 15:19:51 +010066 } \
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +010067 } while (false)
Soby Mathewebf1ca12017-07-13 15:19:51 +010068
Alexei Fedorov8f3ad762020-04-06 16:27:54 +010069#if GIC_EXT_INTID
70#define RESTORE_GICD_EREGS(base, ctx, intr_num, reg, REG) \
71 do { \
72 for (unsigned int int_id = MIN_ESPI_ID; int_id < (intr_num);\
73 int_id += (1U << REG##R_SHIFT)) { \
74 gicd_write_##reg((base), int_id, \
Heyi Guodeb18902021-01-14 22:16:18 +080075 (ctx)->gicd_##reg[(int_id - (MIN_ESPI_ID - \
76 round_up(TOTAL_SPI_INTR_NUM, 1U << REG##R_SHIFT)))\
Alexei Fedorov8f3ad762020-04-06 16:27:54 +010077 >> REG##R_SHIFT]); \
78 } \
79 } while (false)
80
81#define SAVE_GICD_EREGS(base, ctx, intr_num, reg, REG) \
82 do { \
83 for (unsigned int int_id = MIN_ESPI_ID; int_id < (intr_num);\
84 int_id += (1U << REG##R_SHIFT)) { \
Heyi Guodeb18902021-01-14 22:16:18 +080085 (ctx)->gicd_##reg[(int_id - (MIN_ESPI_ID - \
86 round_up(TOTAL_SPI_INTR_NUM, 1U << REG##R_SHIFT)))\
Alexei Fedorov8f3ad762020-04-06 16:27:54 +010087 >> REG##R_SHIFT] = gicd_read_##reg((base), int_id);\
88 } \
89 } while (false)
90#else
91#define SAVE_GICD_EREGS(base, ctx, intr_num, reg, REG)
92#define RESTORE_GICD_EREGS(base, ctx, intr_num, reg, REG)
93#endif /* GIC_EXT_INTID */
Soby Mathewebf1ca12017-07-13 15:19:51 +010094
Achin Guptadf373732015-09-03 14:18:02 +010095/*******************************************************************************
96 * This function initialises the ARM GICv3 driver in EL3 with provided platform
97 * inputs.
98 ******************************************************************************/
Daniel Boulbyc9263e62018-09-18 13:36:39 +010099void __init gicv3_driver_init(const gicv3_driver_data_t *plat_driver_data)
Achin Guptadf373732015-09-03 14:18:02 +0100100{
101 unsigned int gic_version;
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500102 unsigned int gicv2_compat;
Achin Guptadf373732015-09-03 14:18:02 +0100103
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100104 assert(plat_driver_data != NULL);
105 assert(plat_driver_data->gicd_base != 0U);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100106 assert(plat_driver_data->rdistif_num != 0U);
107 assert(plat_driver_data->rdistif_base_addrs != NULL);
Achin Guptadf373732015-09-03 14:18:02 +0100108
109 assert(IS_IN_EL3());
110
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500111 assert((plat_driver_data->interrupt_props_num != 0U) ?
112 (plat_driver_data->interrupt_props != NULL) : 1);
Achin Guptadf373732015-09-03 14:18:02 +0100113
114 /* Check for system register support */
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500115#ifndef __aarch64__
116 assert((read_id_pfr1() &
117 (ID_PFR1_GIC_MASK << ID_PFR1_GIC_SHIFT)) != 0U);
118#else
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100119 assert((read_id_aa64pfr0_el1() &
120 (ID_AA64PFR0_GIC_MASK << ID_AA64PFR0_GIC_SHIFT)) != 0U);
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500121#endif /* !__aarch64__ */
Achin Guptadf373732015-09-03 14:18:02 +0100122
Achin Guptadf373732015-09-03 14:18:02 +0100123 gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500124 gic_version >>= PIDR2_ARCH_REV_SHIFT;
Achin Guptadf373732015-09-03 14:18:02 +0100125 gic_version &= PIDR2_ARCH_REV_MASK;
Achin Guptadf373732015-09-03 14:18:02 +0100126
Alexei Fedorov5875f262020-04-06 19:00:35 +0100127 /* Check GIC version */
Andre Przywara858f40e2021-05-18 15:51:06 +0100128#if !GIC_ENABLE_V4_EXTN
Alexei Fedorov5875f262020-04-06 19:00:35 +0100129 assert(gic_version == ARCH_REV_GICV3);
130#endif
Achin Guptadf373732015-09-03 14:18:02 +0100131 /*
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500132 * Find out whether the GIC supports the GICv2 compatibility mode.
133 * The ARE_S bit resets to 0 if supported
Achin Guptadf373732015-09-03 14:18:02 +0100134 */
135 gicv2_compat = gicd_read_ctlr(plat_driver_data->gicd_base);
136 gicv2_compat >>= CTLR_ARE_S_SHIFT;
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500137 gicv2_compat = gicv2_compat & CTLR_ARE_S_MASK;
Achin Guptadf373732015-09-03 14:18:02 +0100138
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500139 if (plat_driver_data->gicr_base != 0U) {
140 /*
141 * Find the base address of each implemented Redistributor interface.
142 * The number of interfaces should be equal to the number of CPUs in the
143 * system. The memory for saving these addresses has to be allocated by
144 * the platform port
145 */
146 gicv3_rdistif_base_addrs_probe(plat_driver_data->rdistif_base_addrs,
147 plat_driver_data->rdistif_num,
148 plat_driver_data->gicr_base,
149 plat_driver_data->mpidr_to_core_pos);
150#if !HW_ASSISTED_COHERENCY
151 /*
152 * Flush the rdistif_base_addrs[] contents linked to the GICv3 driver.
153 */
154 flush_dcache_range((uintptr_t)(plat_driver_data->rdistif_base_addrs),
155 plat_driver_data->rdistif_num *
156 sizeof(*(plat_driver_data->rdistif_base_addrs)));
157#endif
158 }
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000159 gicv3_driver_data = plat_driver_data;
Achin Guptadf373732015-09-03 14:18:02 +0100160
Soby Mathew311b1772017-02-14 10:11:52 +0000161 /*
162 * The GIC driver data is initialized by the primary CPU with caches
163 * enabled. When the secondary CPU boots up, it initializes the
164 * GICC/GICR interface with the caches disabled. Hence flush the
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000165 * driver data to ensure coherency. This is not required if the
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500166 * platform has HW_ASSISTED_COHERENCY enabled.
Soby Mathew311b1772017-02-14 10:11:52 +0000167 */
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500168#if !HW_ASSISTED_COHERENCY
169 flush_dcache_range((uintptr_t)&gicv3_driver_data,
170 sizeof(gicv3_driver_data));
171 flush_dcache_range((uintptr_t)gicv3_driver_data,
172 sizeof(*gicv3_driver_data));
Soby Mathew311b1772017-02-14 10:11:52 +0000173#endif
Manish V Badarkhee1b15b02022-05-09 21:55:19 +0100174 gicv3_check_erratas_applies(plat_driver_data->gicd_base);
175
Alexei Fedorov5875f262020-04-06 19:00:35 +0100176 INFO("GICv%u with%s legacy support detected.\n", gic_version,
177 (gicv2_compat == 0U) ? "" : "out");
178 INFO("ARM GICv%u driver initialized in EL3\n", gic_version);
Achin Guptadf373732015-09-03 14:18:02 +0100179}
180
181/*******************************************************************************
182 * This function initialises the GIC distributor interface based upon the data
183 * provided by the platform while initialising the driver.
184 ******************************************************************************/
Daniel Boulbyc9263e62018-09-18 13:36:39 +0100185void __init gicv3_distif_init(void)
Achin Guptadf373732015-09-03 14:18:02 +0100186{
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100187 unsigned int bitmap;
Yatharth Kochar6083c842016-09-06 11:48:05 +0100188
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100189 assert(gicv3_driver_data != NULL);
190 assert(gicv3_driver_data->gicd_base != 0U);
Achin Guptadf373732015-09-03 14:18:02 +0100191
192 assert(IS_IN_EL3());
193
194 /*
195 * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
196 * the ARE_S bit. The Distributor might generate a system error
197 * otherwise.
198 */
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000199 gicd_clr_ctlr(gicv3_driver_data->gicd_base,
Achin Guptadf373732015-09-03 14:18:02 +0100200 CTLR_ENABLE_G0_BIT |
201 CTLR_ENABLE_G1S_BIT |
202 CTLR_ENABLE_G1NS_BIT,
203 RWP_TRUE);
204
205 /* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000206 gicd_set_ctlr(gicv3_driver_data->gicd_base,
Achin Guptadf373732015-09-03 14:18:02 +0100207 CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);
208
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100209 /* Set the default attribute of all (E)SPIs */
Daniel Boulby87d3aac2018-05-01 15:15:34 +0100210 gicv3_spis_config_defaults(gicv3_driver_data->gicd_base);
Achin Guptadf373732015-09-03 14:18:02 +0100211
Antonio Nino Diazf9ed3cb2018-09-24 17:23:24 +0100212 bitmap = gicv3_secure_spis_config_props(
213 gicv3_driver_data->gicd_base,
214 gicv3_driver_data->interrupt_props,
215 gicv3_driver_data->interrupt_props_num);
Achin Guptadf373732015-09-03 14:18:02 +0100216
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100217 /* Enable the secure (E)SPIs now that they have been configured */
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000218 gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
Achin Guptadf373732015-09-03 14:18:02 +0100219}
220
221/*******************************************************************************
222 * This function initialises the GIC Redistributor interface of the calling CPU
223 * (identified by the 'proc_num' parameter) based upon the data provided by the
224 * platform while initialising the driver.
225 ******************************************************************************/
226void gicv3_rdistif_init(unsigned int proc_num)
227{
228 uintptr_t gicr_base;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100229 unsigned int bitmap;
Jeenu Viswambharan385f1db2017-11-07 08:38:23 +0000230 uint32_t ctlr;
Achin Guptadf373732015-09-03 14:18:02 +0100231
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100232 assert(gicv3_driver_data != NULL);
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000233 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100234 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
235 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan385f1db2017-11-07 08:38:23 +0000236
237 ctlr = gicd_read_ctlr(gicv3_driver_data->gicd_base);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100238 assert((ctlr & CTLR_ARE_S_BIT) != 0U);
Achin Guptadf373732015-09-03 14:18:02 +0100239
240 assert(IS_IN_EL3());
241
Jeenu Viswambharand7806992016-12-09 11:03:15 +0000242 /* Power on redistributor */
243 gicv3_rdistif_on(proc_num);
244
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000245 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Madhukar Pappireddyec834922019-05-15 18:25:41 -0500246 assert(gicr_base != 0U);
Achin Guptadf373732015-09-03 14:18:02 +0100247
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100248 /* Set the default attribute of all SGIs and (E)PPIs */
Daniel Boulby87d3aac2018-05-01 15:15:34 +0100249 gicv3_ppi_sgi_config_defaults(gicr_base);
Achin Guptadf373732015-09-03 14:18:02 +0100250
Antonio Nino Diazf9ed3cb2018-09-24 17:23:24 +0100251 bitmap = gicv3_secure_ppi_sgi_config_props(gicr_base,
252 gicv3_driver_data->interrupt_props,
253 gicv3_driver_data->interrupt_props_num);
Jeenu Viswambharan385f1db2017-11-07 08:38:23 +0000254
255 /* Enable interrupt groups as required, if not already */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100256 if ((ctlr & bitmap) != bitmap) {
Jeenu Viswambharan385f1db2017-11-07 08:38:23 +0000257 gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100258 }
Achin Guptadf373732015-09-03 14:18:02 +0100259}
260
261/*******************************************************************************
Jeenu Viswambharand7806992016-12-09 11:03:15 +0000262 * Functions to perform power operations on GIC Redistributor
263 ******************************************************************************/
264void gicv3_rdistif_off(unsigned int proc_num)
265{
Jeenu Viswambharand7806992016-12-09 11:03:15 +0000266}
267
268void gicv3_rdistif_on(unsigned int proc_num)
269{
Jeenu Viswambharand7806992016-12-09 11:03:15 +0000270}
271
272/*******************************************************************************
Achin Guptadf373732015-09-03 14:18:02 +0100273 * This function enables the GIC CPU interface of the calling CPU using only
274 * system register accesses.
275 ******************************************************************************/
276void gicv3_cpuif_enable(unsigned int proc_num)
277{
278 uintptr_t gicr_base;
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000279 u_register_t scr_el3;
Achin Guptadf373732015-09-03 14:18:02 +0100280 unsigned int icc_sre_el3;
281
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100282 assert(gicv3_driver_data != NULL);
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000283 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100284 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Achin Guptadf373732015-09-03 14:18:02 +0100285 assert(IS_IN_EL3());
286
287 /* Mark the connected core as awake */
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000288 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Achin Guptadf373732015-09-03 14:18:02 +0100289 gicv3_rdistif_mark_core_awake(gicr_base);
290
291 /* Disable the legacy interrupt bypass */
292 icc_sre_el3 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT;
293
294 /*
295 * Enable system register access for EL3 and allow lower exception
296 * levels to configure the same for themselves. If the legacy mode is
297 * not supported, the SRE bit is RAO/WI
298 */
299 icc_sre_el3 |= (ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT);
300 write_icc_sre_el3(read_icc_sre_el3() | icc_sre_el3);
301
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000302 scr_el3 = read_scr_el3();
Achin Guptadf373732015-09-03 14:18:02 +0100303
304 /*
305 * Switch to NS state to write Non secure ICC_SRE_EL1 and
306 * ICC_SRE_EL2 registers.
307 */
308 write_scr_el3(scr_el3 | SCR_NS_BIT);
309 isb();
310
311 write_icc_sre_el2(read_icc_sre_el2() | icc_sre_el3);
312 write_icc_sre_el1(ICC_SRE_SRE_BIT);
313 isb();
314
315 /* Switch to secure state. */
316 write_scr_el3(scr_el3 & (~SCR_NS_BIT));
317 isb();
318
James kungacc29852019-05-31 15:40:05 +0800319 /* Write the secure ICC_SRE_EL1 register */
320 write_icc_sre_el1(ICC_SRE_SRE_BIT);
321 isb();
322
Achin Guptadf373732015-09-03 14:18:02 +0100323 /* Program the idle priority in the PMR */
324 write_icc_pmr_el1(GIC_PRI_MASK);
325
326 /* Enable Group0 interrupts */
327 write_icc_igrpen0_el1(IGRPEN1_EL1_ENABLE_G0_BIT);
328
329 /* Enable Group1 Secure interrupts */
330 write_icc_igrpen1_el3(read_icc_igrpen1_el3() |
331 IGRPEN1_EL3_ENABLE_G1S_BIT);
Achin Guptadf373732015-09-03 14:18:02 +0100332 isb();
Ming Huang5a5e0aa2021-06-04 16:23:22 +0800333 /* Add DSB to ensure visibility of System register writes */
334 dsb();
Achin Guptadf373732015-09-03 14:18:02 +0100335}
336
337/*******************************************************************************
338 * This function disables the GIC CPU interface of the calling CPU using
339 * only system register accesses.
340 ******************************************************************************/
341void gicv3_cpuif_disable(unsigned int proc_num)
342{
343 uintptr_t gicr_base;
344
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100345 assert(gicv3_driver_data != NULL);
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000346 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100347 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Achin Guptadf373732015-09-03 14:18:02 +0100348
349 assert(IS_IN_EL3());
350
351 /* Disable legacy interrupt bypass */
352 write_icc_sre_el3(read_icc_sre_el3() |
353 (ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT));
354
355 /* Disable Group0 interrupts */
356 write_icc_igrpen0_el1(read_icc_igrpen0_el1() &
357 ~IGRPEN1_EL1_ENABLE_G0_BIT);
358
Sudeep Holla65d68ca2016-08-04 16:14:50 +0100359 /* Disable Group1 Secure and Non-Secure interrupts */
Achin Guptadf373732015-09-03 14:18:02 +0100360 write_icc_igrpen1_el3(read_icc_igrpen1_el3() &
Sudeep Holla65d68ca2016-08-04 16:14:50 +0100361 ~(IGRPEN1_EL3_ENABLE_G1NS_BIT |
362 IGRPEN1_EL3_ENABLE_G1S_BIT));
Achin Guptadf373732015-09-03 14:18:02 +0100363
364 /* Synchronise accesses to group enable registers */
365 isb();
Ming Huang5a5e0aa2021-06-04 16:23:22 +0800366 /* Add DSB to ensure visibility of System register writes */
367 dsb();
Achin Guptadf373732015-09-03 14:18:02 +0100368
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000369 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Manish V Badarkhee1b15b02022-05-09 21:55:19 +0100370 assert(gicr_base != 0UL);
371
372 /*
373 * dsb() already issued previously after clearing the CPU group
374 * enabled, apply below workaround to toggle the "DPG*"
375 * bits of GICR_CTLR register for unblocking event.
376 */
377 gicv3_apply_errata_wa_2384374(gicr_base);
378
379 /* Mark the connected core as asleep */
Achin Guptadf373732015-09-03 14:18:02 +0100380 gicv3_rdistif_mark_core_asleep(gicr_base);
381}
382
383/*******************************************************************************
384 * This function returns the id of the highest priority pending interrupt at
385 * the GIC cpu interface.
386 ******************************************************************************/
387unsigned int gicv3_get_pending_interrupt_id(void)
388{
389 unsigned int id;
390
391 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100392 id = (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
Achin Guptadf373732015-09-03 14:18:02 +0100393
394 /*
395 * If the ID is special identifier corresponding to G1S or G1NS
396 * interrupt, then read the highest pending group 1 interrupt.
397 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100398 if ((id == PENDING_G1S_INTID) || (id == PENDING_G1NS_INTID)) {
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100399 return (uint32_t)read_icc_hppir1_el1() & HPPIR1_EL1_INTID_MASK;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100400 }
Achin Guptadf373732015-09-03 14:18:02 +0100401
402 return id;
403}
404
405/*******************************************************************************
406 * This function returns the type of the highest priority pending interrupt at
407 * the GIC cpu interface. The return values can be one of the following :
408 * PENDING_G1S_INTID : The interrupt type is secure Group 1.
409 * PENDING_G1NS_INTID : The interrupt type is non secure Group 1.
410 * 0 - 1019 : The interrupt type is secure Group 0.
411 * GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
412 * sufficient priority to be signaled
413 ******************************************************************************/
414unsigned int gicv3_get_pending_interrupt_type(void)
415{
416 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100417 return (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
Achin Guptadf373732015-09-03 14:18:02 +0100418}
419
420/*******************************************************************************
421 * This function returns the type of the interrupt id depending upon the group
422 * this interrupt has been configured under by the interrupt controller i.e.
423 * group0 or group1 Secure / Non Secure. The return value can be one of the
424 * following :
Soby Mathew03ffb6b2015-12-03 14:12:54 +0000425 * INTR_GROUP0 : The interrupt type is a Secure Group 0 interrupt
426 * INTR_GROUP1S : The interrupt type is a Secure Group 1 secure interrupt
427 * INTR_GROUP1NS: The interrupt type is a Secure Group 1 non secure
Achin Guptadf373732015-09-03 14:18:02 +0100428 * interrupt.
429 ******************************************************************************/
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100430unsigned int gicv3_get_interrupt_type(unsigned int id, unsigned int proc_num)
Achin Guptadf373732015-09-03 14:18:02 +0100431{
432 unsigned int igroup, grpmodr;
433 uintptr_t gicr_base;
Varun Wadekar2e482842023-03-08 16:47:38 +0000434 uintptr_t gicd_base;
Achin Guptadf373732015-09-03 14:18:02 +0100435
436 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100437 assert(gicv3_driver_data != NULL);
Achin Guptadf373732015-09-03 14:18:02 +0100438
439 /* Ensure the parameters are valid */
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100440 assert((id < PENDING_G1S_INTID) || (id >= MIN_LPI_ID));
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000441 assert(proc_num < gicv3_driver_data->rdistif_num);
Achin Guptadf373732015-09-03 14:18:02 +0100442
443 /* All LPI interrupts are Group 1 non secure */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100444 if (id >= MIN_LPI_ID) {
Soby Mathew03ffb6b2015-12-03 14:12:54 +0000445 return INTR_GROUP1NS;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100446 }
Achin Guptadf373732015-09-03 14:18:02 +0100447
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100448 /* Check interrupt ID */
449 if (is_sgi_ppi(id)) {
450 /* SGIs: 0-15, PPIs: 16-31, EPPIs: 1056-1119 */
Andrew F. Davis2ac50022018-08-30 14:30:54 -0500451 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharane1c59ab2016-12-06 16:15:22 +0000452 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100453 igroup = gicr_get_igroupr(gicr_base, id);
454 grpmodr = gicr_get_igrpmodr(gicr_base, id);
Achin Guptadf373732015-09-03 14:18:02 +0100455 } else {
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100456 /* SPIs: 32-1019, ESPIs: 4096-5119 */
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100457 assert(gicv3_driver_data->gicd_base != 0U);
Varun Wadekar2e482842023-03-08 16:47:38 +0000458 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
459 igroup = gicd_get_igroupr(gicd_base, id);
460 grpmodr = gicd_get_igrpmodr(gicd_base, id);
Achin Guptadf373732015-09-03 14:18:02 +0100461 }
462
463 /*
464 * If the IGROUP bit is set, then it is a Group 1 Non secure
465 * interrupt
466 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100467 if (igroup != 0U) {
Soby Mathew03ffb6b2015-12-03 14:12:54 +0000468 return INTR_GROUP1NS;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100469 }
Achin Guptadf373732015-09-03 14:18:02 +0100470
471 /* If the GRPMOD bit is set, then it is a Group 1 Secure interrupt */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100472 if (grpmodr != 0U) {
Soby Mathew03ffb6b2015-12-03 14:12:54 +0000473 return INTR_GROUP1S;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100474 }
Achin Guptadf373732015-09-03 14:18:02 +0100475
476 /* Else it is a Group 0 Secure interrupt */
Soby Mathew03ffb6b2015-12-03 14:12:54 +0000477 return INTR_GROUP0;
Achin Guptadf373732015-09-03 14:18:02 +0100478}
Soby Mathewebf1ca12017-07-13 15:19:51 +0100479
480/*****************************************************************************
Soby Mathewb2582782017-07-18 16:12:45 +0100481 * Function to save and disable the GIC ITS register context. The power
482 * management of GIC ITS is implementation-defined and this function doesn't
483 * save any memory structures required to support ITS. As the sequence to save
484 * this state is implementation defined, it should be executed in platform
485 * specific code. Calling this function alone and then powering down the GIC and
486 * ITS without implementing the aforementioned platform specific code will
487 * corrupt the ITS state.
488 *
489 * This function must be invoked after the GIC CPU interface is disabled.
490 *****************************************************************************/
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100491void gicv3_its_save_disable(uintptr_t gits_base,
492 gicv3_its_ctx_t * const its_ctx)
Soby Mathewb2582782017-07-18 16:12:45 +0100493{
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100494 unsigned int i;
Soby Mathewb2582782017-07-18 16:12:45 +0100495
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100496 assert(gicv3_driver_data != NULL);
Soby Mathewb2582782017-07-18 16:12:45 +0100497 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100498 assert(its_ctx != NULL);
499 assert(gits_base != 0U);
Soby Mathewb2582782017-07-18 16:12:45 +0100500
501 its_ctx->gits_ctlr = gits_read_ctlr(gits_base);
502
503 /* Disable the ITS */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100504 gits_write_ctlr(gits_base, its_ctx->gits_ctlr & ~GITS_CTLR_ENABLED_BIT);
Soby Mathewb2582782017-07-18 16:12:45 +0100505
506 /* Wait for quiescent state */
507 gits_wait_for_quiescent_bit(gits_base);
508
509 its_ctx->gits_cbaser = gits_read_cbaser(gits_base);
510 its_ctx->gits_cwriter = gits_read_cwriter(gits_base);
511
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100512 for (i = 0U; i < ARRAY_SIZE(its_ctx->gits_baser); i++) {
Soby Mathewb2582782017-07-18 16:12:45 +0100513 its_ctx->gits_baser[i] = gits_read_baser(gits_base, i);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100514 }
Soby Mathewb2582782017-07-18 16:12:45 +0100515}
516
517/*****************************************************************************
518 * Function to restore the GIC ITS register context. The power
519 * management of GIC ITS is implementation defined and this function doesn't
520 * restore any memory structures required to support ITS. The assumption is
521 * that these structures are in memory and are retained during system suspend.
522 *
523 * This must be invoked before the GIC CPU interface is enabled.
524 *****************************************************************************/
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100525void gicv3_its_restore(uintptr_t gits_base,
526 const gicv3_its_ctx_t * const its_ctx)
Soby Mathewb2582782017-07-18 16:12:45 +0100527{
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100528 unsigned int i;
Soby Mathewb2582782017-07-18 16:12:45 +0100529
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100530 assert(gicv3_driver_data != NULL);
Soby Mathewb2582782017-07-18 16:12:45 +0100531 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100532 assert(its_ctx != NULL);
533 assert(gits_base != 0U);
Soby Mathewb2582782017-07-18 16:12:45 +0100534
535 /* Assert that the GITS is disabled and quiescent */
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100536 assert((gits_read_ctlr(gits_base) & GITS_CTLR_ENABLED_BIT) == 0U);
537 assert((gits_read_ctlr(gits_base) & GITS_CTLR_QUIESCENT_BIT) != 0U);
Soby Mathewb2582782017-07-18 16:12:45 +0100538
539 gits_write_cbaser(gits_base, its_ctx->gits_cbaser);
540 gits_write_cwriter(gits_base, its_ctx->gits_cwriter);
541
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100542 for (i = 0U; i < ARRAY_SIZE(its_ctx->gits_baser); i++) {
Soby Mathewb2582782017-07-18 16:12:45 +0100543 gits_write_baser(gits_base, i, its_ctx->gits_baser[i]);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100544 }
Soby Mathewb2582782017-07-18 16:12:45 +0100545
546 /* Restore the ITS CTLR but leave the ITS disabled */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100547 gits_write_ctlr(gits_base, its_ctx->gits_ctlr & ~GITS_CTLR_ENABLED_BIT);
Soby Mathewb2582782017-07-18 16:12:45 +0100548}
549
550/*****************************************************************************
Soby Mathewebf1ca12017-07-13 15:19:51 +0100551 * Function to save the GIC Redistributor register context. This function
552 * must be invoked after CPU interface disable and prior to Distributor save.
553 *****************************************************************************/
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100554void gicv3_rdistif_save(unsigned int proc_num,
555 gicv3_redist_ctx_t * const rdist_ctx)
Soby Mathewebf1ca12017-07-13 15:19:51 +0100556{
557 uintptr_t gicr_base;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100558 unsigned int i, ppi_regs_num, regs_num;
Soby Mathewebf1ca12017-07-13 15:19:51 +0100559
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100560 assert(gicv3_driver_data != NULL);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100561 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100562 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100563 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100564 assert(rdist_ctx != NULL);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100565
566 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
567
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100568#if GIC_EXT_INTID
569 /* Calculate number of PPI registers */
570 ppi_regs_num = (unsigned int)((gicr_read_typer(gicr_base) >>
571 TYPER_PPI_NUM_SHIFT) & TYPER_PPI_NUM_MASK) + 1;
572 /* All other values except PPInum [0-2] are reserved */
573 if (ppi_regs_num > 3U) {
574 ppi_regs_num = 1U;
575 }
576#else
577 ppi_regs_num = 1U;
578#endif
Soby Mathewebf1ca12017-07-13 15:19:51 +0100579 /*
580 * Wait for any write to GICR_CTLR to complete before trying to save any
581 * state.
582 */
583 gicr_wait_for_pending_write(gicr_base);
584
585 rdist_ctx->gicr_ctlr = gicr_read_ctlr(gicr_base);
586
587 rdist_ctx->gicr_propbaser = gicr_read_propbaser(gicr_base);
588 rdist_ctx->gicr_pendbaser = gicr_read_pendbaser(gicr_base);
589
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100590 /* 32 interrupt IDs per register */
591 for (i = 0U; i < ppi_regs_num; ++i) {
592 SAVE_GICR_REG(gicr_base, rdist_ctx, igroupr, i);
593 SAVE_GICR_REG(gicr_base, rdist_ctx, isenabler, i);
594 SAVE_GICR_REG(gicr_base, rdist_ctx, ispendr, i);
595 SAVE_GICR_REG(gicr_base, rdist_ctx, isactiver, i);
596 SAVE_GICR_REG(gicr_base, rdist_ctx, igrpmodr, i);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100597 }
598
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100599 /* 16 interrupt IDs per GICR_ICFGR register */
600 regs_num = ppi_regs_num << 1;
601 for (i = 0U; i < regs_num; ++i) {
602 SAVE_GICR_REG(gicr_base, rdist_ctx, icfgr, i);
603 }
604
605 rdist_ctx->gicr_nsacr = gicr_read_nsacr(gicr_base);
606
607 /* 4 interrupt IDs per GICR_IPRIORITYR register */
608 regs_num = ppi_regs_num << 3;
609 for (i = 0U; i < regs_num; ++i) {
Alexei Fedorove2a40272020-04-07 18:16:18 +0100610 rdist_ctx->gicr_ipriorityr[i] =
611 gicr_ipriorityr_read(gicr_base, i);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100612 }
Soby Mathewebf1ca12017-07-13 15:19:51 +0100613
614 /*
615 * Call the pre-save hook that implements the IMP DEF sequence that may
616 * be required on some GIC implementations. As this may need to access
617 * the Redistributor registers, we pass it proc_num.
618 */
619 gicv3_distif_pre_save(proc_num);
620}
621
622/*****************************************************************************
623 * Function to restore the GIC Redistributor register context. We disable
624 * LPI and per-cpu interrupts before we start restore of the Redistributor.
625 * This function must be invoked after Distributor restore but prior to
626 * CPU interface enable. The pending and active interrupts are restored
627 * after the interrupts are fully configured and enabled.
628 *****************************************************************************/
629void gicv3_rdistif_init_restore(unsigned int proc_num,
630 const gicv3_redist_ctx_t * const rdist_ctx)
631{
632 uintptr_t gicr_base;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100633 unsigned int i, ppi_regs_num, regs_num;
Soby Mathewebf1ca12017-07-13 15:19:51 +0100634
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100635 assert(gicv3_driver_data != NULL);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100636 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100637 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100638 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100639 assert(rdist_ctx != NULL);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100640
641 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
642
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100643#if GIC_EXT_INTID
644 /* Calculate number of PPI registers */
645 ppi_regs_num = (unsigned int)((gicr_read_typer(gicr_base) >>
646 TYPER_PPI_NUM_SHIFT) & TYPER_PPI_NUM_MASK) + 1;
647 /* All other values except PPInum [0-2] are reserved */
648 if (ppi_regs_num > 3U) {
649 ppi_regs_num = 1U;
650 }
651#else
652 ppi_regs_num = 1U;
653#endif
Soby Mathewebf1ca12017-07-13 15:19:51 +0100654 /* Power on redistributor */
655 gicv3_rdistif_on(proc_num);
656
657 /*
658 * Call the post-restore hook that implements the IMP DEF sequence that
659 * may be required on some GIC implementations. As this may need to
660 * access the Redistributor registers, we pass it proc_num.
661 */
662 gicv3_distif_post_restore(proc_num);
663
664 /*
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100665 * Disable all SGIs (imp. def.)/(E)PPIs before configuring them.
666 * This is a more scalable approach as it avoids clearing the enable
667 * bits in the GICD_CTLR.
Soby Mathewebf1ca12017-07-13 15:19:51 +0100668 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100669 for (i = 0U; i < ppi_regs_num; ++i) {
670 gicr_write_icenabler(gicr_base, i, ~0U);
671 }
672
Soby Mathewebf1ca12017-07-13 15:19:51 +0100673 /* Wait for pending writes to GICR_ICENABLER */
674 gicr_wait_for_pending_write(gicr_base);
675
676 /*
677 * Disable the LPIs to avoid unpredictable behavior when writing to
678 * GICR_PROPBASER and GICR_PENDBASER.
679 */
680 gicr_write_ctlr(gicr_base,
681 rdist_ctx->gicr_ctlr & ~(GICR_CTLR_EN_LPIS_BIT));
682
683 /* Restore registers' content */
684 gicr_write_propbaser(gicr_base, rdist_ctx->gicr_propbaser);
685 gicr_write_pendbaser(gicr_base, rdist_ctx->gicr_pendbaser);
686
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100687 /* 32 interrupt IDs per register */
688 for (i = 0U; i < ppi_regs_num; ++i) {
689 RESTORE_GICR_REG(gicr_base, rdist_ctx, igroupr, i);
690 RESTORE_GICR_REG(gicr_base, rdist_ctx, igrpmodr, i);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100691 }
692
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100693 /* 4 interrupt IDs per GICR_IPRIORITYR register */
694 regs_num = ppi_regs_num << 3;
695 for (i = 0U; i < regs_num; ++i) {
Alexei Fedorove2a40272020-04-07 18:16:18 +0100696 gicr_ipriorityr_write(gicr_base, i,
697 rdist_ctx->gicr_ipriorityr[i]);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100698 }
699
700 /* 16 interrupt IDs per GICR_ICFGR register */
701 regs_num = ppi_regs_num << 1;
702 for (i = 0U; i < regs_num; ++i) {
703 RESTORE_GICR_REG(gicr_base, rdist_ctx, icfgr, i);
704 }
705
Soby Mathewebf1ca12017-07-13 15:19:51 +0100706 gicr_write_nsacr(gicr_base, rdist_ctx->gicr_nsacr);
707
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100708 /* Restore after group and priorities are set.
709 * 32 interrupt IDs per register
710 */
711 for (i = 0U; i < ppi_regs_num; ++i) {
712 RESTORE_GICR_REG(gicr_base, rdist_ctx, ispendr, i);
713 RESTORE_GICR_REG(gicr_base, rdist_ctx, isactiver, i);
714 }
Soby Mathewebf1ca12017-07-13 15:19:51 +0100715
716 /*
717 * Wait for all writes to the Distributor to complete before enabling
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100718 * the SGI and (E)PPIs.
Soby Mathewebf1ca12017-07-13 15:19:51 +0100719 */
720 gicr_wait_for_upstream_pending_write(gicr_base);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100721
722 /* 32 interrupt IDs per GICR_ISENABLER register */
723 for (i = 0U; i < ppi_regs_num; ++i) {
724 RESTORE_GICR_REG(gicr_base, rdist_ctx, isenabler, i);
725 }
Soby Mathewebf1ca12017-07-13 15:19:51 +0100726
727 /*
728 * Restore GICR_CTLR.Enable_LPIs bit and wait for pending writes in case
729 * the first write to GICR_CTLR was still in flight (this write only
730 * restores GICR_CTLR.Enable_LPIs and no waiting is required for this
731 * bit).
732 */
733 gicr_write_ctlr(gicr_base, rdist_ctx->gicr_ctlr);
734 gicr_wait_for_pending_write(gicr_base);
735}
736
737/*****************************************************************************
738 * Function to save the GIC Distributor register context. This function
739 * must be invoked after CPU interface disable and Redistributor save.
740 *****************************************************************************/
741void gicv3_distif_save(gicv3_dist_ctx_t * const dist_ctx)
742{
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100743 assert(gicv3_driver_data != NULL);
744 assert(gicv3_driver_data->gicd_base != 0U);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100745 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100746 assert(dist_ctx != NULL);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100747
748 uintptr_t gicd_base = gicv3_driver_data->gicd_base;
Heyi Guoce2b49b2021-01-20 19:05:51 +0800749 unsigned int num_ints = gicv3_get_spi_limit(gicd_base);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100750#if GIC_EXT_INTID
Heyi Guoce2b49b2021-01-20 19:05:51 +0800751 unsigned int num_eints = gicv3_get_espi_limit(gicd_base);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100752#endif
Heyi Guoce2b49b2021-01-20 19:05:51 +0800753
Soby Mathewebf1ca12017-07-13 15:19:51 +0100754 /* Wait for pending write to complete */
755 gicd_wait_for_pending_write(gicd_base);
756
757 /* Save the GICD_CTLR */
758 dist_ctx->gicd_ctlr = gicd_read_ctlr(gicd_base);
759
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100760 /* Save GICD_IGROUPR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100761 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUP);
762
763 /* Save GICD_IGROUPRE for INTIDs 4096 - 5119 */
764 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igroupr, IGROUP);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100765
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100766 /* Save GICD_ISENABLER for INT_IDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100767 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLE);
768
769 /* Save GICD_ISENABLERE for INT_IDs 4096 - 5119 */
770 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isenabler, ISENABLE);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100771
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100772 /* Save GICD_ISPENDR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100773 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPEND);
774
775 /* Save GICD_ISPENDRE for INTIDs 4096 - 5119 */
776 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ispendr, ISPEND);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100777
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100778 /* Save GICD_ISACTIVER for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100779 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVE);
780
781 /* Save GICD_ISACTIVERE for INTIDs 4096 - 5119 */
782 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isactiver, ISACTIVE);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100783
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100784 /* Save GICD_IPRIORITYR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100785 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITY);
786
787 /* Save GICD_IPRIORITYRE for INTIDs 4096 - 5119 */
788 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ipriorityr, IPRIORITY);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100789
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100790 /* Save GICD_ICFGR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100791 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFG);
792
793 /* Save GICD_ICFGRE for INTIDs 4096 - 5119 */
794 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, icfgr, ICFG);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100795
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100796 /* Save GICD_IGRPMODR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100797 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMOD);
798
799 /* Save GICD_IGRPMODRE for INTIDs 4096 - 5119 */
800 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igrpmodr, IGRPMOD);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100801
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100802 /* Save GICD_NSACR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100803 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSAC);
804
805 /* Save GICD_NSACRE for INTIDs 4096 - 5119 */
806 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, nsacr, NSAC);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100807
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100808 /* Save GICD_IROUTER for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100809 SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTE);
810
811 /* Save GICD_IROUTERE for INTIDs 4096 - 5119 */
812 SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, irouter, IROUTE);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100813
814 /*
815 * GICD_ITARGETSR<n> and GICD_SPENDSGIR<n> are RAZ/WI when
816 * GICD_CTLR.ARE_(S|NS) bits are set which is the case for our GICv3
817 * driver.
818 */
819}
820
821/*****************************************************************************
822 * Function to restore the GIC Distributor register context. We disable G0, G1S
823 * and G1NS interrupt groups before we start restore of the Distributor. This
824 * function must be invoked prior to Redistributor restore and CPU interface
825 * enable. The pending and active interrupts are restored after the interrupts
826 * are fully configured and enabled.
827 *****************************************************************************/
828void gicv3_distif_init_restore(const gicv3_dist_ctx_t * const dist_ctx)
829{
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100830 assert(gicv3_driver_data != NULL);
831 assert(gicv3_driver_data->gicd_base != 0U);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100832 assert(IS_IN_EL3());
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100833 assert(dist_ctx != NULL);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100834
835 uintptr_t gicd_base = gicv3_driver_data->gicd_base;
836
837 /*
838 * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
839 * the ARE_S bit. The Distributor might generate a system error
840 * otherwise.
841 */
842 gicd_clr_ctlr(gicd_base,
843 CTLR_ENABLE_G0_BIT |
844 CTLR_ENABLE_G1S_BIT |
845 CTLR_ENABLE_G1NS_BIT,
846 RWP_TRUE);
847
848 /* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
849 gicd_set_ctlr(gicd_base, CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);
850
Heyi Guoce2b49b2021-01-20 19:05:51 +0800851 unsigned int num_ints = gicv3_get_spi_limit(gicd_base);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100852#if GIC_EXT_INTID
Heyi Guoce2b49b2021-01-20 19:05:51 +0800853 unsigned int num_eints = gicv3_get_espi_limit(gicd_base);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100854#endif
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100855 /* Restore GICD_IGROUPR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100856 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUP);
857
858 /* Restore GICD_IGROUPRE for INTIDs 4096 - 5119 */
859 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igroupr, IGROUP);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100860
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100861 /* Restore GICD_IPRIORITYR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100862 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITY);
863
864 /* Restore GICD_IPRIORITYRE for INTIDs 4096 - 5119 */
865 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ipriorityr, IPRIORITY);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100866
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100867 /* Restore GICD_ICFGR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100868 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFG);
869
870 /* Restore GICD_ICFGRE for INTIDs 4096 - 5119 */
871 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, icfgr, ICFG);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100872
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100873 /* Restore GICD_IGRPMODR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100874 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMOD);
875
876 /* Restore GICD_IGRPMODRE for INTIDs 4096 - 5119 */
877 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igrpmodr, IGRPMOD);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100878
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100879 /* Restore GICD_NSACR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100880 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSAC);
881
882 /* Restore GICD_NSACRE for INTIDs 4096 - 5119 */
883 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, nsacr, NSAC);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100884
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100885 /* Restore GICD_IROUTER for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100886 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTE);
887
888 /* Restore GICD_IROUTERE for INTIDs 4096 - 5119 */
889 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, irouter, IROUTE);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100890
891 /*
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100892 * Restore ISENABLER(E), ISPENDR(E) and ISACTIVER(E) after
893 * the interrupts are configured.
Soby Mathewebf1ca12017-07-13 15:19:51 +0100894 */
895
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100896 /* Restore GICD_ISENABLER for INT_IDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100897 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLE);
898
899 /* Restore GICD_ISENABLERE for INT_IDs 4096 - 5119 */
900 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isenabler, ISENABLE);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100901
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100902 /* Restore GICD_ISPENDR for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100903 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPEND);
904
905 /* Restore GICD_ISPENDRE for INTIDs 4096 - 5119 */
906 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ispendr, ISPEND);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100907
Alexei Fedoroveb5f0ba2019-09-13 15:47:13 +0100908 /* Restore GICD_ISACTIVER for INTIDs 32 - 1019 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100909 RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVE);
910
911 /* Restore GICD_ISACTIVERE for INTIDs 4096 - 5119 */
912 RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isactiver, ISACTIVE);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100913
914 /* Restore the GICD_CTLR */
915 gicd_write_ctlr(gicd_base, dist_ctx->gicd_ctlr);
916 gicd_wait_for_pending_write(gicd_base);
Soby Mathewebf1ca12017-07-13 15:19:51 +0100917}
Jeenu Viswambharaneb68ea92017-09-22 08:32:09 +0100918
919/*******************************************************************************
920 * This function gets the priority of the interrupt the processor is currently
921 * servicing.
922 ******************************************************************************/
923unsigned int gicv3_get_running_priority(void)
924{
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100925 return (unsigned int)read_icc_rpr_el1();
Jeenu Viswambharaneb68ea92017-09-22 08:32:09 +0100926}
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +0100927
928/*******************************************************************************
929 * This function checks if the interrupt identified by id is active (whether the
930 * state is either active, or active and pending). The proc_num is used if the
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100931 * interrupt is SGI or (E)PPI and programs the corresponding Redistributor
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +0100932 * interface.
933 ******************************************************************************/
934unsigned int gicv3_get_interrupt_active(unsigned int id, unsigned int proc_num)
935{
Varun Wadekar2e482842023-03-08 16:47:38 +0000936 uintptr_t gicd_base;
937
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100938 assert(gicv3_driver_data != NULL);
939 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +0100940 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100941 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +0100942
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100943 /* Check interrupt ID */
944 if (is_sgi_ppi(id)) {
945 /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
946 return gicr_get_isactiver(
947 gicv3_driver_data->rdistif_base_addrs[proc_num], id);
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +0100948 }
949
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100950 /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
Varun Wadekar2e482842023-03-08 16:47:38 +0000951 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
952 return gicd_get_isactiver(gicd_base, id);
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +0100953}
Jeenu Viswambharan979225f2017-09-22 08:32:09 +0100954
955/*******************************************************************************
956 * This function enables the interrupt identified by id. The proc_num
957 * is used if the interrupt is SGI or PPI, and programs the corresponding
958 * Redistributor interface.
959 ******************************************************************************/
960void gicv3_enable_interrupt(unsigned int id, unsigned int proc_num)
961{
Varun Wadekar2e482842023-03-08 16:47:38 +0000962 uintptr_t gicd_base;
963
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100964 assert(gicv3_driver_data != NULL);
965 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +0100966 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100967 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +0100968
969 /*
970 * Ensure that any shared variable updates depending on out of band
971 * interrupt trigger are observed before enabling interrupt.
972 */
973 dsbishst();
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100974
975 /* Check interrupt ID */
976 if (is_sgi_ppi(id)) {
977 /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
978 gicr_set_isenabler(
979 gicv3_driver_data->rdistif_base_addrs[proc_num], id);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +0100980 } else {
Alexei Fedorov8f3ad762020-04-06 16:27:54 +0100981 /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
Varun Wadekar2e482842023-03-08 16:47:38 +0000982 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
983 gicd_set_isenabler(gicd_base, id);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +0100984 }
985}
986
987/*******************************************************************************
988 * This function disables the interrupt identified by id. The proc_num
989 * is used if the interrupt is SGI or PPI, and programs the corresponding
990 * Redistributor interface.
991 ******************************************************************************/
992void gicv3_disable_interrupt(unsigned int id, unsigned int proc_num)
993{
Varun Wadekar2e482842023-03-08 16:47:38 +0000994 uintptr_t gicd_base;
995
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100996 assert(gicv3_driver_data != NULL);
997 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +0100998 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +0100999 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +01001000
1001 /*
1002 * Disable interrupt, and ensure that any shared variable updates
1003 * depending on out of band interrupt trigger are observed afterwards.
1004 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001005
1006 /* Check interrupt ID */
1007 if (is_sgi_ppi(id)) {
1008 /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
1009 gicr_set_icenabler(
1010 gicv3_driver_data->rdistif_base_addrs[proc_num], id);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +01001011
1012 /* Write to clear enable requires waiting for pending writes */
1013 gicr_wait_for_pending_write(
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001014 gicv3_driver_data->rdistif_base_addrs[proc_num]);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +01001015 } else {
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001016 /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
Varun Wadekar2e482842023-03-08 16:47:38 +00001017 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
1018 gicd_set_icenabler(gicd_base, id);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +01001019
1020 /* Write to clear enable requires waiting for pending writes */
Varun Wadekar2e482842023-03-08 16:47:38 +00001021 gicd_wait_for_pending_write(gicd_base);
Jeenu Viswambharan979225f2017-09-22 08:32:09 +01001022 }
1023
1024 dsbishst();
1025}
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +01001026
1027/*******************************************************************************
1028 * This function sets the interrupt priority as supplied for the given interrupt
1029 * id.
1030 ******************************************************************************/
1031void gicv3_set_interrupt_priority(unsigned int id, unsigned int proc_num,
1032 unsigned int priority)
1033{
1034 uintptr_t gicr_base;
Varun Wadekar2e482842023-03-08 16:47:38 +00001035 uintptr_t gicd_base;
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +01001036
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001037 assert(gicv3_driver_data != NULL);
1038 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +01001039 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001040 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +01001041
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001042 /* Check interrupt ID */
1043 if (is_sgi_ppi(id)) {
1044 /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +01001045 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
1046 gicr_set_ipriorityr(gicr_base, id, priority);
1047 } else {
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001048 /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
Varun Wadekar2e482842023-03-08 16:47:38 +00001049 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
1050 gicd_set_ipriorityr(gicd_base, id, priority);
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +01001051 }
1052}
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001053
1054/*******************************************************************************
1055 * This function assigns group for the interrupt identified by id. The proc_num
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001056 * is used if the interrupt is SGI or (E)PPI, and programs the corresponding
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001057 * Redistributor interface. The group can be any of GICV3_INTR_GROUP*
1058 ******************************************************************************/
1059void gicv3_set_interrupt_type(unsigned int id, unsigned int proc_num,
1060 unsigned int type)
1061{
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001062 bool igroup = false, grpmod = false;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001063 uintptr_t gicr_base;
Varun Wadekar2e482842023-03-08 16:47:38 +00001064 uintptr_t gicd_base;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001065
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001066 assert(gicv3_driver_data != NULL);
1067 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001068 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001069 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001070
1071 switch (type) {
1072 case INTR_GROUP1S:
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001073 igroup = false;
1074 grpmod = true;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001075 break;
1076 case INTR_GROUP0:
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001077 igroup = false;
1078 grpmod = false;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001079 break;
1080 case INTR_GROUP1NS:
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001081 igroup = true;
1082 grpmod = false;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001083 break;
1084 default:
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001085 assert(false);
Jonathan Wright5aa74982018-03-13 15:24:29 +00001086 break;
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001087 }
1088
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001089 /* Check interrupt ID */
1090 if (is_sgi_ppi(id)) {
1091 /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001092 gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001093
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001094 igroup ? gicr_set_igroupr(gicr_base, id) :
1095 gicr_clr_igroupr(gicr_base, id);
1096 grpmod ? gicr_set_igrpmodr(gicr_base, id) :
1097 gicr_clr_igrpmodr(gicr_base, id);
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001098 } else {
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001099 /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
1100
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001101 /* Serialize read-modify-write to Distributor registers */
1102 spin_lock(&gic_lock);
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001103
Varun Wadekar2e482842023-03-08 16:47:38 +00001104 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
1105
1106 igroup ? gicd_set_igroupr(gicd_base, id) :
1107 gicd_clr_igroupr(gicd_base, id);
1108 grpmod ? gicd_set_igrpmodr(gicd_base, id) :
1109 gicd_clr_igrpmodr(gicd_base, id);
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001110
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +01001111 spin_unlock(&gic_lock);
1112 }
1113}
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +01001114
1115/*******************************************************************************
Florian Lugoudcb31ff2021-09-08 12:40:24 +02001116 * This function raises the specified SGI of the specified group.
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +01001117 *
1118 * The target parameter must be a valid MPIDR in the system.
1119 ******************************************************************************/
Florian Lugoudcb31ff2021-09-08 12:40:24 +02001120void gicv3_raise_sgi(unsigned int sgi_num, gicv3_irq_group_t group,
1121 u_register_t target)
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +01001122{
1123 unsigned int tgt, aff3, aff2, aff1, aff0;
1124 uint64_t sgi_val;
1125
1126 /* Verify interrupt number is in the SGI range */
1127 assert((sgi_num >= MIN_SGI_ID) && (sgi_num < MIN_PPI_ID));
1128
1129 /* Extract affinity fields from target */
1130 aff0 = MPIDR_AFFLVL0_VAL(target);
1131 aff1 = MPIDR_AFFLVL1_VAL(target);
1132 aff2 = MPIDR_AFFLVL2_VAL(target);
1133 aff3 = MPIDR_AFFLVL3_VAL(target);
1134
1135 /*
1136 * Make target list from affinity 0, and ensure GICv3 SGI can target
1137 * this PE.
1138 */
1139 assert(aff0 < GICV3_MAX_SGI_TARGETS);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001140 tgt = BIT_32(aff0);
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +01001141
1142 /* Raise SGI to PE specified by its affinity */
1143 sgi_val = GICV3_SGIR_VALUE(aff3, aff2, aff1, sgi_num, SGIR_IRM_TO_AFF,
1144 tgt);
1145
1146 /*
1147 * Ensure that any shared variable updates depending on out of band
1148 * interrupt trigger are observed before raising SGI.
1149 */
1150 dsbishst();
Florian Lugoudcb31ff2021-09-08 12:40:24 +02001151
1152 switch (group) {
1153 case GICV3_G0:
1154 write_icc_sgi0r_el1(sgi_val);
1155 break;
1156 case GICV3_G1NS:
1157 write_icc_asgi1r(sgi_val);
1158 break;
1159 case GICV3_G1S:
1160 write_icc_sgi1r(sgi_val);
1161 break;
1162 default:
1163 assert(false);
1164 break;
1165 }
1166
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +01001167 isb();
1168}
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +01001169
1170/*******************************************************************************
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001171 * This function sets the interrupt routing for the given (E)SPI interrupt id.
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +01001172 * The interrupt routing is specified in routing mode and mpidr.
1173 *
1174 * The routing mode can be either of:
1175 * - GICV3_IRM_ANY
1176 * - GICV3_IRM_PE
1177 *
1178 * The mpidr is the affinity of the PE to which the interrupt will be routed,
1179 * and is ignored for routing mode GICV3_IRM_ANY.
1180 ******************************************************************************/
1181void gicv3_set_spi_routing(unsigned int id, unsigned int irm, u_register_t mpidr)
1182{
1183 unsigned long long aff;
1184 uint64_t router;
Varun Wadekar2e482842023-03-08 16:47:38 +00001185 uintptr_t gicd_base;
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +01001186
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001187 assert(gicv3_driver_data != NULL);
1188 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +01001189
1190 assert((irm == GICV3_IRM_ANY) || (irm == GICV3_IRM_PE));
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001191
1192 assert(IS_SPI(id));
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +01001193
1194 aff = gicd_irouter_val_from_mpidr(mpidr, irm);
Varun Wadekar2e482842023-03-08 16:47:38 +00001195 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
1196 gicd_write_irouter(gicd_base, id, aff);
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +01001197
1198 /*
1199 * In implementations that do not require 1 of N distribution of SPIs,
1200 * IRM might be RAZ/WI. Read back and verify IRM bit.
1201 */
1202 if (irm == GICV3_IRM_ANY) {
Varun Wadekar2e482842023-03-08 16:47:38 +00001203 router = gicd_read_irouter(gicd_base, id);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001204 if (((router >> IROUTER_IRM_SHIFT) & IROUTER_IRM_MASK) == 0U) {
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +01001205 ERROR("GICv3 implementation doesn't support routing ANY\n");
1206 panic();
1207 }
1208 }
1209}
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001210
1211/*******************************************************************************
1212 * This function clears the pending status of an interrupt identified by id.
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001213 * The proc_num is used if the interrupt is SGI or (E)PPI, and programs the
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001214 * corresponding Redistributor interface.
1215 ******************************************************************************/
1216void gicv3_clear_interrupt_pending(unsigned int id, unsigned int proc_num)
1217{
Varun Wadekar2e482842023-03-08 16:47:38 +00001218 uintptr_t gicd_base;
1219
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001220 assert(gicv3_driver_data != NULL);
1221 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001222 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001223 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001224
1225 /*
1226 * Clear pending interrupt, and ensure that any shared variable updates
1227 * depending on out of band interrupt trigger are observed afterwards.
1228 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001229
1230 /* Check interrupt ID */
1231 if (is_sgi_ppi(id)) {
1232 /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
1233 gicr_set_icpendr(
1234 gicv3_driver_data->rdistif_base_addrs[proc_num], id);
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001235 } else {
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001236 /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
Varun Wadekar2e482842023-03-08 16:47:38 +00001237 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
1238 gicd_set_icpendr(gicd_base, id);
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001239 }
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001240
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001241 dsbishst();
1242}
1243
1244/*******************************************************************************
1245 * This function sets the pending status of an interrupt identified by id.
1246 * The proc_num is used if the interrupt is SGI or PPI and programs the
1247 * corresponding Redistributor interface.
1248 ******************************************************************************/
1249void gicv3_set_interrupt_pending(unsigned int id, unsigned int proc_num)
1250{
Varun Wadekar2e482842023-03-08 16:47:38 +00001251 uintptr_t gicd_base;
1252
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001253 assert(gicv3_driver_data != NULL);
1254 assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001255 assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz3fea9c82018-08-21 10:02:33 +01001256 assert(gicv3_driver_data->rdistif_base_addrs != NULL);
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001257
1258 /*
1259 * Ensure that any shared variable updates depending on out of band
1260 * interrupt trigger are observed before setting interrupt pending.
1261 */
1262 dsbishst();
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001263
1264 /* Check interrupt ID */
1265 if (is_sgi_ppi(id)) {
1266 /* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
1267 gicr_set_ispendr(
1268 gicv3_driver_data->rdistif_base_addrs[proc_num], id);
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001269 } else {
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001270 /* For SPIs: 32-1019 and ESPIs: 4096-5119 */
Varun Wadekar2e482842023-03-08 16:47:38 +00001271 gicd_base = gicv3_get_multichip_base(id, gicv3_driver_data->gicd_base);
1272 gicd_set_ispendr(gicd_base, id);
Jeenu Viswambharana2816a12017-09-22 08:32:09 +01001273 }
1274}
Jeenu Viswambharand55a4452017-09-22 08:32:09 +01001275
1276/*******************************************************************************
1277 * This function sets the PMR register with the supplied value. Returns the
1278 * original PMR.
1279 ******************************************************************************/
1280unsigned int gicv3_set_pmr(unsigned int mask)
1281{
1282 unsigned int old_mask;
1283
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001284 old_mask = (unsigned int)read_icc_pmr_el1();
Jeenu Viswambharand55a4452017-09-22 08:32:09 +01001285
1286 /*
1287 * Order memory updates w.r.t. PMR write, and ensure they're visible
1288 * before potential out of band interrupt trigger because of PMR update.
1289 * PMR system register writes are self-synchronizing, so no ISB required
1290 * thereafter.
1291 */
1292 dsbishst();
1293 write_icc_pmr_el1(mask);
1294
1295 return old_mask;
1296}
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001297
1298/*******************************************************************************
1299 * This function delegates the responsibility of discovering the corresponding
1300 * Redistributor frames to each CPU itself. It is a modified version of
1301 * gicv3_rdistif_base_addrs_probe() and is executed by each CPU in the platform
1302 * unlike the previous way in which only the Primary CPU did the discovery of
1303 * all the Redistributor frames for every CPU. It also handles the scenario in
1304 * which the frames of various CPUs are not contiguous in physical memory.
1305 ******************************************************************************/
1306int gicv3_rdistif_probe(const uintptr_t gicr_frame)
1307{
Heyi Guo60cd8032020-05-19 11:50:40 +08001308 u_register_t mpidr, mpidr_self;
1309 unsigned int proc_num;
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001310 uint64_t typer_val;
1311 uintptr_t rdistif_base;
1312 bool gicr_frame_found = false;
1313
1314 assert(gicv3_driver_data->gicr_base == 0U);
1315
1316 /* Ensure this function is called with Data Cache enabled */
1317#ifndef __aarch64__
1318 assert((read_sctlr() & SCTLR_C_BIT) != 0U);
1319#else
1320 assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
1321#endif /* !__aarch64__ */
1322
Heyi Guo60cd8032020-05-19 11:50:40 +08001323 mpidr_self = read_mpidr_el1() & MPIDR_AFFINITY_MASK;
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001324 rdistif_base = gicr_frame;
1325 do {
1326 typer_val = gicr_read_typer(rdistif_base);
Heyi Guo60cd8032020-05-19 11:50:40 +08001327 mpidr = mpidr_from_gicr_typer(typer_val);
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001328 if (gicv3_driver_data->mpidr_to_core_pos != NULL) {
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001329 proc_num = gicv3_driver_data->mpidr_to_core_pos(mpidr);
1330 } else {
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001331 proc_num = (unsigned int)(typer_val >>
1332 TYPER_PROC_NUM_SHIFT) & TYPER_PROC_NUM_MASK;
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001333 }
Heyi Guo60cd8032020-05-19 11:50:40 +08001334 if (mpidr == mpidr_self) {
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001335 /* The base address doesn't need to be initialized on
1336 * every warm boot.
1337 */
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001338 if (gicv3_driver_data->rdistif_base_addrs[proc_num]
1339 != 0U) {
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001340 return 0;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001341 }
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001342 gicv3_driver_data->rdistif_base_addrs[proc_num] =
1343 rdistif_base;
1344 gicr_frame_found = true;
1345 break;
1346 }
Andre Przywara858f40e2021-05-18 15:51:06 +01001347 rdistif_base += gicv3_redist_size(typer_val);
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001348 } while ((typer_val & TYPER_LAST_BIT) == 0U);
1349
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001350 if (!gicr_frame_found) {
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001351 return -1;
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001352 }
Madhukar Pappireddyec834922019-05-15 18:25:41 -05001353
1354 /*
1355 * Flush the driver data to ensure coherency. This is
1356 * not required if platform has HW_ASSISTED_COHERENCY
1357 * enabled.
1358 */
1359#if !HW_ASSISTED_COHERENCY
1360 /*
1361 * Flush the rdistif_base_addrs[] contents linked to the GICv3 driver.
1362 */
1363 flush_dcache_range((uintptr_t)&(gicv3_driver_data->rdistif_base_addrs[proc_num]),
1364 sizeof(*(gicv3_driver_data->rdistif_base_addrs)));
1365#endif
1366 return 0; /* Found matching GICR frame */
1367}
Alexei Fedorov8f3ad762020-04-06 16:27:54 +01001368
1369/******************************************************************************
1370 * This function checks the interrupt ID and returns true for SGIs and (E)PPIs
1371 * and false for (E)SPIs IDs.
1372 *****************************************************************************/
1373static bool is_sgi_ppi(unsigned int id)
1374{
1375 /* SGIs: 0-15, PPIs: 16-31, EPPIs: 1056-1119 */
1376 if (IS_SGI_PPI(id)) {
1377 return true;
1378 }
1379
1380 /* SPIs: 32-1019, ESPIs: 4096-5119 */
1381 if (IS_SPI(id)) {
1382 return false;
1383 }
1384
1385 assert(false);
1386 panic();
1387}