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