xref: /arm-trusted-firmware/plat/arm/common/arm_gicv3.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <platform_def.h>
9*91f16700Schasinglulu 
10*91f16700Schasinglulu #include <common/debug.h>
11*91f16700Schasinglulu #include <common/interrupt_props.h>
12*91f16700Schasinglulu #include <drivers/arm/gicv3.h>
13*91f16700Schasinglulu #include <lib/utils.h>
14*91f16700Schasinglulu #include <plat/arm/common/plat_arm.h>
15*91f16700Schasinglulu #include <plat/common/platform.h>
16*91f16700Schasinglulu 
17*91f16700Schasinglulu /******************************************************************************
18*91f16700Schasinglulu  * The following functions are defined as weak to allow a platform to override
19*91f16700Schasinglulu  * the way the GICv3 driver is initialised and used.
20*91f16700Schasinglulu  *****************************************************************************/
21*91f16700Schasinglulu #pragma weak plat_arm_gic_driver_init
22*91f16700Schasinglulu #pragma weak plat_arm_gic_init
23*91f16700Schasinglulu #pragma weak plat_arm_gic_cpuif_enable
24*91f16700Schasinglulu #pragma weak plat_arm_gic_cpuif_disable
25*91f16700Schasinglulu #pragma weak plat_arm_gic_pcpu_init
26*91f16700Schasinglulu #pragma weak plat_arm_gic_redistif_on
27*91f16700Schasinglulu #pragma weak plat_arm_gic_redistif_off
28*91f16700Schasinglulu 
29*91f16700Schasinglulu /* The GICv3 driver only needs to be initialized in EL3 */
30*91f16700Schasinglulu static uintptr_t rdistif_base_addrs[PLATFORM_CORE_COUNT];
31*91f16700Schasinglulu 
32*91f16700Schasinglulu /* Default GICR base address to be used for GICR probe. */
33*91f16700Schasinglulu static const uintptr_t gicr_base_addrs[2] = {
34*91f16700Schasinglulu 	PLAT_ARM_GICR_BASE,	/* GICR Base address of the primary CPU */
35*91f16700Schasinglulu 	0U			/* Zero Termination */
36*91f16700Schasinglulu };
37*91f16700Schasinglulu 
38*91f16700Schasinglulu /* List of zero terminated GICR frame addresses which CPUs will probe */
39*91f16700Schasinglulu static const uintptr_t *gicr_frames = gicr_base_addrs;
40*91f16700Schasinglulu 
41*91f16700Schasinglulu static const interrupt_prop_t arm_interrupt_props[] = {
42*91f16700Schasinglulu 	PLAT_ARM_G1S_IRQ_PROPS(INTR_GROUP1S),
43*91f16700Schasinglulu 	PLAT_ARM_G0_IRQ_PROPS(INTR_GROUP0),
44*91f16700Schasinglulu #if ENABLE_FEAT_RAS && FFH_SUPPORT
45*91f16700Schasinglulu 	INTR_PROP_DESC(PLAT_CORE_FAULT_IRQ, PLAT_RAS_PRI, INTR_GROUP0,
46*91f16700Schasinglulu 			GIC_INTR_CFG_LEVEL)
47*91f16700Schasinglulu #endif
48*91f16700Schasinglulu };
49*91f16700Schasinglulu 
50*91f16700Schasinglulu /*
51*91f16700Schasinglulu  * We save and restore the GICv3 context on system suspend. Allocate the
52*91f16700Schasinglulu  * data in the designated EL3 Secure carve-out memory. The `used` attribute
53*91f16700Schasinglulu  * is used to prevent the compiler from removing the gicv3 contexts.
54*91f16700Schasinglulu  */
55*91f16700Schasinglulu static gicv3_redist_ctx_t rdist_ctx __section(".arm_el3_tzc_dram") __used;
56*91f16700Schasinglulu static gicv3_dist_ctx_t dist_ctx __section(".arm_el3_tzc_dram") __used;
57*91f16700Schasinglulu 
58*91f16700Schasinglulu /* Define accessor function to get reference to the GICv3 context */
59*91f16700Schasinglulu DEFINE_LOAD_SYM_ADDR(rdist_ctx)
60*91f16700Schasinglulu DEFINE_LOAD_SYM_ADDR(dist_ctx)
61*91f16700Schasinglulu 
62*91f16700Schasinglulu /*
63*91f16700Schasinglulu  * MPIDR hashing function for translating MPIDRs read from GICR_TYPER register
64*91f16700Schasinglulu  * to core position.
65*91f16700Schasinglulu  *
66*91f16700Schasinglulu  * Calculating core position is dependent on MPIDR_EL1.MT bit. However, affinity
67*91f16700Schasinglulu  * values read from GICR_TYPER don't have an MT field. To reuse the same
68*91f16700Schasinglulu  * translation used for CPUs, we insert MT bit read from the PE's MPIDR into
69*91f16700Schasinglulu  * that read from GICR_TYPER.
70*91f16700Schasinglulu  *
71*91f16700Schasinglulu  * Assumptions:
72*91f16700Schasinglulu  *
73*91f16700Schasinglulu  *   - All CPUs implemented in the system have MPIDR_EL1.MT bit set;
74*91f16700Schasinglulu  *   - No CPUs implemented in the system use affinity level 3.
75*91f16700Schasinglulu  */
76*91f16700Schasinglulu static unsigned int arm_gicv3_mpidr_hash(u_register_t mpidr)
77*91f16700Schasinglulu {
78*91f16700Schasinglulu 	mpidr |= (read_mpidr_el1() & MPIDR_MT_MASK);
79*91f16700Schasinglulu 	return plat_arm_calc_core_pos(mpidr);
80*91f16700Schasinglulu }
81*91f16700Schasinglulu 
82*91f16700Schasinglulu static const gicv3_driver_data_t arm_gic_data __unused = {
83*91f16700Schasinglulu 	.gicd_base = PLAT_ARM_GICD_BASE,
84*91f16700Schasinglulu 	.gicr_base = 0U,
85*91f16700Schasinglulu 	.interrupt_props = arm_interrupt_props,
86*91f16700Schasinglulu 	.interrupt_props_num = ARRAY_SIZE(arm_interrupt_props),
87*91f16700Schasinglulu 	.rdistif_num = PLATFORM_CORE_COUNT,
88*91f16700Schasinglulu 	.rdistif_base_addrs = rdistif_base_addrs,
89*91f16700Schasinglulu 	.mpidr_to_core_pos = arm_gicv3_mpidr_hash
90*91f16700Schasinglulu };
91*91f16700Schasinglulu 
92*91f16700Schasinglulu /*
93*91f16700Schasinglulu  * By default, gicr_frames will be pointing to gicr_base_addrs. If
94*91f16700Schasinglulu  * the platform supports a non-contiguous GICR frames (GICR frames located
95*91f16700Schasinglulu  * at uneven offset), plat_arm_override_gicr_frames function can be used by
96*91f16700Schasinglulu  * such platform to override the gicr_frames.
97*91f16700Schasinglulu  */
98*91f16700Schasinglulu void plat_arm_override_gicr_frames(const uintptr_t *plat_gicr_frames)
99*91f16700Schasinglulu {
100*91f16700Schasinglulu 	assert(plat_gicr_frames != NULL);
101*91f16700Schasinglulu 	gicr_frames = plat_gicr_frames;
102*91f16700Schasinglulu }
103*91f16700Schasinglulu 
104*91f16700Schasinglulu void __init plat_arm_gic_driver_init(void)
105*91f16700Schasinglulu {
106*91f16700Schasinglulu 	/*
107*91f16700Schasinglulu 	 * The GICv3 driver is initialized in EL3 and does not need
108*91f16700Schasinglulu 	 * to be initialized again in SEL1. This is because the S-EL1
109*91f16700Schasinglulu 	 * can use GIC system registers to manage interrupts and does
110*91f16700Schasinglulu 	 * not need GIC interface base addresses to be configured.
111*91f16700Schasinglulu 	 */
112*91f16700Schasinglulu #if (!defined(__aarch64__) && defined(IMAGE_BL32)) || \
113*91f16700Schasinglulu 	(defined(__aarch64__) && defined(IMAGE_BL31))
114*91f16700Schasinglulu 	gicv3_driver_init(&arm_gic_data);
115*91f16700Schasinglulu 
116*91f16700Schasinglulu 	if (gicv3_rdistif_probe(gicr_base_addrs[0]) == -1) {
117*91f16700Schasinglulu 		ERROR("No GICR base frame found for Primary CPU\n");
118*91f16700Schasinglulu 		panic();
119*91f16700Schasinglulu 	}
120*91f16700Schasinglulu #endif
121*91f16700Schasinglulu }
122*91f16700Schasinglulu 
123*91f16700Schasinglulu /******************************************************************************
124*91f16700Schasinglulu  * ARM common helper to initialize the GIC. Only invoked by BL31
125*91f16700Schasinglulu  *****************************************************************************/
126*91f16700Schasinglulu void __init plat_arm_gic_init(void)
127*91f16700Schasinglulu {
128*91f16700Schasinglulu 	gicv3_distif_init();
129*91f16700Schasinglulu 	gicv3_rdistif_init(plat_my_core_pos());
130*91f16700Schasinglulu 	gicv3_cpuif_enable(plat_my_core_pos());
131*91f16700Schasinglulu }
132*91f16700Schasinglulu 
133*91f16700Schasinglulu /******************************************************************************
134*91f16700Schasinglulu  * ARM common helper to enable the GIC CPU interface
135*91f16700Schasinglulu  *****************************************************************************/
136*91f16700Schasinglulu void plat_arm_gic_cpuif_enable(void)
137*91f16700Schasinglulu {
138*91f16700Schasinglulu 	gicv3_cpuif_enable(plat_my_core_pos());
139*91f16700Schasinglulu }
140*91f16700Schasinglulu 
141*91f16700Schasinglulu /******************************************************************************
142*91f16700Schasinglulu  * ARM common helper to disable the GIC CPU interface
143*91f16700Schasinglulu  *****************************************************************************/
144*91f16700Schasinglulu void plat_arm_gic_cpuif_disable(void)
145*91f16700Schasinglulu {
146*91f16700Schasinglulu 	gicv3_cpuif_disable(plat_my_core_pos());
147*91f16700Schasinglulu }
148*91f16700Schasinglulu 
149*91f16700Schasinglulu /******************************************************************************
150*91f16700Schasinglulu  * ARM common helper function to iterate over all GICR frames and discover the
151*91f16700Schasinglulu  * corresponding per-cpu redistributor frame as well as initialize the
152*91f16700Schasinglulu  * corresponding interface in GICv3.
153*91f16700Schasinglulu  *****************************************************************************/
154*91f16700Schasinglulu void plat_arm_gic_pcpu_init(void)
155*91f16700Schasinglulu {
156*91f16700Schasinglulu 	int result;
157*91f16700Schasinglulu 	const uintptr_t *plat_gicr_frames = gicr_frames;
158*91f16700Schasinglulu 
159*91f16700Schasinglulu 	do {
160*91f16700Schasinglulu 		result = gicv3_rdistif_probe(*plat_gicr_frames);
161*91f16700Schasinglulu 
162*91f16700Schasinglulu 		/* If the probe is successful, no need to proceed further */
163*91f16700Schasinglulu 		if (result == 0)
164*91f16700Schasinglulu 			break;
165*91f16700Schasinglulu 
166*91f16700Schasinglulu 		plat_gicr_frames++;
167*91f16700Schasinglulu 	} while (*plat_gicr_frames != 0U);
168*91f16700Schasinglulu 
169*91f16700Schasinglulu 	if (result == -1) {
170*91f16700Schasinglulu 		ERROR("No GICR base frame found for CPU 0x%lx\n", read_mpidr());
171*91f16700Schasinglulu 		panic();
172*91f16700Schasinglulu 	}
173*91f16700Schasinglulu 	gicv3_rdistif_init(plat_my_core_pos());
174*91f16700Schasinglulu }
175*91f16700Schasinglulu 
176*91f16700Schasinglulu /******************************************************************************
177*91f16700Schasinglulu  * ARM common helpers to power GIC redistributor interface
178*91f16700Schasinglulu  *****************************************************************************/
179*91f16700Schasinglulu void plat_arm_gic_redistif_on(void)
180*91f16700Schasinglulu {
181*91f16700Schasinglulu 	gicv3_rdistif_on(plat_my_core_pos());
182*91f16700Schasinglulu }
183*91f16700Schasinglulu 
184*91f16700Schasinglulu void plat_arm_gic_redistif_off(void)
185*91f16700Schasinglulu {
186*91f16700Schasinglulu 	gicv3_rdistif_off(plat_my_core_pos());
187*91f16700Schasinglulu }
188*91f16700Schasinglulu 
189*91f16700Schasinglulu /******************************************************************************
190*91f16700Schasinglulu  * ARM common helper to save & restore the GICv3 on resume from system suspend
191*91f16700Schasinglulu  *****************************************************************************/
192*91f16700Schasinglulu void plat_arm_gic_save(void)
193*91f16700Schasinglulu {
194*91f16700Schasinglulu 	gicv3_redist_ctx_t * const rdist_context =
195*91f16700Schasinglulu 			(gicv3_redist_ctx_t *)LOAD_ADDR_OF(rdist_ctx);
196*91f16700Schasinglulu 	gicv3_dist_ctx_t * const dist_context =
197*91f16700Schasinglulu 			(gicv3_dist_ctx_t *)LOAD_ADDR_OF(dist_ctx);
198*91f16700Schasinglulu 
199*91f16700Schasinglulu 	/*
200*91f16700Schasinglulu 	 * If an ITS is available, save its context before
201*91f16700Schasinglulu 	 * the Redistributor using:
202*91f16700Schasinglulu 	 * gicv3_its_save_disable(gits_base, &its_ctx[i])
203*91f16700Schasinglulu 	 * Additionally, an implementation-defined sequence may
204*91f16700Schasinglulu 	 * be required to save the whole ITS state.
205*91f16700Schasinglulu 	 */
206*91f16700Schasinglulu 
207*91f16700Schasinglulu 	/*
208*91f16700Schasinglulu 	 * Save the GIC Redistributors and ITS contexts before the
209*91f16700Schasinglulu 	 * Distributor context. As we only handle SYSTEM SUSPEND API,
210*91f16700Schasinglulu 	 * we only need to save the context of the CPU that is issuing
211*91f16700Schasinglulu 	 * the SYSTEM SUSPEND call, i.e. the current CPU.
212*91f16700Schasinglulu 	 */
213*91f16700Schasinglulu 	gicv3_rdistif_save(plat_my_core_pos(), rdist_context);
214*91f16700Schasinglulu 
215*91f16700Schasinglulu 	/* Save the GIC Distributor context */
216*91f16700Schasinglulu 	gicv3_distif_save(dist_context);
217*91f16700Schasinglulu 
218*91f16700Schasinglulu 	/*
219*91f16700Schasinglulu 	 * From here, all the components of the GIC can be safely powered down
220*91f16700Schasinglulu 	 * as long as there is an alternate way to handle wakeup interrupt
221*91f16700Schasinglulu 	 * sources.
222*91f16700Schasinglulu 	 */
223*91f16700Schasinglulu }
224*91f16700Schasinglulu 
225*91f16700Schasinglulu void plat_arm_gic_resume(void)
226*91f16700Schasinglulu {
227*91f16700Schasinglulu 	const gicv3_redist_ctx_t *rdist_context =
228*91f16700Schasinglulu 			(gicv3_redist_ctx_t *)LOAD_ADDR_OF(rdist_ctx);
229*91f16700Schasinglulu 	const gicv3_dist_ctx_t *dist_context =
230*91f16700Schasinglulu 			(gicv3_dist_ctx_t *)LOAD_ADDR_OF(dist_ctx);
231*91f16700Schasinglulu 
232*91f16700Schasinglulu 	/* Restore the GIC Distributor context */
233*91f16700Schasinglulu 	gicv3_distif_init_restore(dist_context);
234*91f16700Schasinglulu 
235*91f16700Schasinglulu 	/*
236*91f16700Schasinglulu 	 * Restore the GIC Redistributor and ITS contexts after the
237*91f16700Schasinglulu 	 * Distributor context. As we only handle SYSTEM SUSPEND API,
238*91f16700Schasinglulu 	 * we only need to restore the context of the CPU that issued
239*91f16700Schasinglulu 	 * the SYSTEM SUSPEND call.
240*91f16700Schasinglulu 	 */
241*91f16700Schasinglulu 	gicv3_rdistif_init_restore(plat_my_core_pos(), rdist_context);
242*91f16700Schasinglulu 
243*91f16700Schasinglulu 	/*
244*91f16700Schasinglulu 	 * If an ITS is available, restore its context after
245*91f16700Schasinglulu 	 * the Redistributor using:
246*91f16700Schasinglulu 	 * gicv3_its_restore(gits_base, &its_ctx[i])
247*91f16700Schasinglulu 	 * An implementation-defined sequence may be required to
248*91f16700Schasinglulu 	 * restore the whole ITS state. The ITS must also be
249*91f16700Schasinglulu 	 * re-enabled after this sequence has been executed.
250*91f16700Schasinglulu 	 */
251*91f16700Schasinglulu }
252