xref: /rk3399_ARM-atf/drivers/arm/gic/v2/gicv2_main.c (revision 3fea9c8b8e8e255fc68c273defb5fc846ea2a689)
1464ce2bbSSoby Mathew /*
27fabe1a8SRoberto Vargas  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3464ce2bbSSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5464ce2bbSSoby Mathew  */
6464ce2bbSSoby Mathew 
7464ce2bbSSoby Mathew #include <arch.h>
8464ce2bbSSoby Mathew #include <arch_helpers.h>
9464ce2bbSSoby Mathew #include <assert.h>
10464ce2bbSSoby Mathew #include <debug.h>
11464ce2bbSSoby Mathew #include <gic_common.h>
12464ce2bbSSoby Mathew #include <gicv2.h>
13c639e8ebSJeenu Viswambharan #include <interrupt_props.h>
1474dce7faSJeenu Viswambharan #include <spinlock.h>
15*3fea9c8bSAntonio Nino Diaz #include <stdbool.h>
16*3fea9c8bSAntonio Nino Diaz 
17e9ec3cecSSoby Mathew #include "../common/gic_common_private.h"
18464ce2bbSSoby Mathew #include "gicv2_private.h"
19464ce2bbSSoby Mathew 
20464ce2bbSSoby Mathew static const gicv2_driver_data_t *driver_data;
21464ce2bbSSoby Mathew 
2274dce7faSJeenu Viswambharan /*
2374dce7faSJeenu Viswambharan  * Spinlock to guard registers needing read-modify-write. APIs protected by this
2474dce7faSJeenu Viswambharan  * spinlock are used either at boot time (when only a single CPU is active), or
2574dce7faSJeenu Viswambharan  * when the system is fully coherent.
2674dce7faSJeenu Viswambharan  */
277fabe1a8SRoberto Vargas static spinlock_t gic_lock;
2874dce7faSJeenu Viswambharan 
29464ce2bbSSoby Mathew /*******************************************************************************
30464ce2bbSSoby Mathew  * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
31464ce2bbSSoby Mathew  * and set the priority mask register to allow all interrupts to trickle in.
32464ce2bbSSoby Mathew  ******************************************************************************/
33464ce2bbSSoby Mathew void gicv2_cpuif_enable(void)
34464ce2bbSSoby Mathew {
35464ce2bbSSoby Mathew 	unsigned int val;
36464ce2bbSSoby Mathew 
37*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
38*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
39464ce2bbSSoby Mathew 
40464ce2bbSSoby Mathew 	/*
41464ce2bbSSoby Mathew 	 * Enable the Group 0 interrupts, FIQEn and disable Group 0/1
42464ce2bbSSoby Mathew 	 * bypass.
43464ce2bbSSoby Mathew 	 */
44464ce2bbSSoby Mathew 	val = CTLR_ENABLE_G0_BIT | FIQ_EN_BIT | FIQ_BYP_DIS_GRP0;
45464ce2bbSSoby Mathew 	val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
46464ce2bbSSoby Mathew 
47464ce2bbSSoby Mathew 	/* Program the idle priority in the PMR */
48464ce2bbSSoby Mathew 	gicc_write_pmr(driver_data->gicc_base, GIC_PRI_MASK);
49464ce2bbSSoby Mathew 	gicc_write_ctlr(driver_data->gicc_base, val);
50464ce2bbSSoby Mathew }
51464ce2bbSSoby Mathew 
52464ce2bbSSoby Mathew /*******************************************************************************
53464ce2bbSSoby Mathew  * Place the cpu interface in a state where it can never make a cpu exit wfi as
54464ce2bbSSoby Mathew  * as result of an asserted interrupt. This is critical for powering down a cpu
55464ce2bbSSoby Mathew  ******************************************************************************/
56464ce2bbSSoby Mathew void gicv2_cpuif_disable(void)
57464ce2bbSSoby Mathew {
58464ce2bbSSoby Mathew 	unsigned int val;
59464ce2bbSSoby Mathew 
60*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
61*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
62464ce2bbSSoby Mathew 
63464ce2bbSSoby Mathew 	/* Disable secure, non-secure interrupts and disable their bypass */
64464ce2bbSSoby Mathew 	val = gicc_read_ctlr(driver_data->gicc_base);
65464ce2bbSSoby Mathew 	val &= ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT);
66464ce2bbSSoby Mathew 	val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
67464ce2bbSSoby Mathew 	val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
68464ce2bbSSoby Mathew 	gicc_write_ctlr(driver_data->gicc_base, val);
69464ce2bbSSoby Mathew }
70464ce2bbSSoby Mathew 
71464ce2bbSSoby Mathew /*******************************************************************************
72464ce2bbSSoby Mathew  * Per cpu gic distributor setup which will be done by all cpus after a cold
73464ce2bbSSoby Mathew  * boot/hotplug. This marks out the secure SPIs and PPIs & enables them.
74464ce2bbSSoby Mathew  ******************************************************************************/
75464ce2bbSSoby Mathew void gicv2_pcpu_distif_init(void)
76464ce2bbSSoby Mathew {
77385f1dbbSJeenu Viswambharan 	unsigned int ctlr;
78385f1dbbSJeenu Viswambharan 
79*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
80*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
81464ce2bbSSoby Mathew 
82c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
83c639e8ebSJeenu Viswambharan 	if (driver_data->interrupt_props != NULL) {
84c639e8ebSJeenu Viswambharan #endif
85c639e8ebSJeenu Viswambharan 		gicv2_secure_ppi_sgi_setup_props(driver_data->gicd_base,
86c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props,
87c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props_num);
88c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
89c639e8ebSJeenu Viswambharan 	} else {
90dcf01a0aSDan Handley 		/*
91dcf01a0aSDan Handley 		 * Suppress deprecated declaration warnings in compatibility
92dcf01a0aSDan Handley 		 * function
93dcf01a0aSDan Handley 		 */
94dcf01a0aSDan Handley #pragma GCC diagnostic push
95dcf01a0aSDan Handley #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
96c639e8ebSJeenu Viswambharan 		assert(driver_data->g0_interrupt_array);
97464ce2bbSSoby Mathew 		gicv2_secure_ppi_sgi_setup(driver_data->gicd_base,
98464ce2bbSSoby Mathew 				driver_data->g0_interrupt_num,
99464ce2bbSSoby Mathew 				driver_data->g0_interrupt_array);
100dcf01a0aSDan Handley #pragma GCC diagnostic pop
101464ce2bbSSoby Mathew 	}
102c639e8ebSJeenu Viswambharan #endif
103385f1dbbSJeenu Viswambharan 
104385f1dbbSJeenu Viswambharan 	/* Enable G0 interrupts if not already */
105385f1dbbSJeenu Viswambharan 	ctlr = gicd_read_ctlr(driver_data->gicd_base);
106*3fea9c8bSAntonio Nino Diaz 	if ((ctlr & CTLR_ENABLE_G0_BIT) == 0U) {
107385f1dbbSJeenu Viswambharan 		gicd_write_ctlr(driver_data->gicd_base,
108385f1dbbSJeenu Viswambharan 				ctlr | CTLR_ENABLE_G0_BIT);
109385f1dbbSJeenu Viswambharan 	}
110c639e8ebSJeenu Viswambharan }
111464ce2bbSSoby Mathew 
112464ce2bbSSoby Mathew /*******************************************************************************
113464ce2bbSSoby Mathew  * Global gic distributor init which will be done by the primary cpu after a
114464ce2bbSSoby Mathew  * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
115464ce2bbSSoby Mathew  * then enables the secure GIC distributor interface.
116464ce2bbSSoby Mathew  ******************************************************************************/
117464ce2bbSSoby Mathew void gicv2_distif_init(void)
118464ce2bbSSoby Mathew {
119464ce2bbSSoby Mathew 	unsigned int ctlr;
120464ce2bbSSoby Mathew 
121*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
122*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
123464ce2bbSSoby Mathew 
124464ce2bbSSoby Mathew 	/* Disable the distributor before going further */
125464ce2bbSSoby Mathew 	ctlr = gicd_read_ctlr(driver_data->gicd_base);
126464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base,
127464ce2bbSSoby Mathew 			ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT));
128464ce2bbSSoby Mathew 
129464ce2bbSSoby Mathew 	/* Set the default attribute of all SPIs */
130464ce2bbSSoby Mathew 	gicv2_spis_configure_defaults(driver_data->gicd_base);
131464ce2bbSSoby Mathew 
132c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
133c639e8ebSJeenu Viswambharan 	if (driver_data->interrupt_props != NULL) {
134c639e8ebSJeenu Viswambharan #endif
135c639e8ebSJeenu Viswambharan 		gicv2_secure_spis_configure_props(driver_data->gicd_base,
136c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props,
137c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props_num);
138c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
139c639e8ebSJeenu Viswambharan 	} else {
140dcf01a0aSDan Handley 		/*
141dcf01a0aSDan Handley 		 * Suppress deprecated declaration warnings in compatibility
142dcf01a0aSDan Handley 		 * function
143dcf01a0aSDan Handley 		 */
144dcf01a0aSDan Handley #pragma GCC diagnostic push
145dcf01a0aSDan Handley #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
146dcf01a0aSDan Handley 
147c639e8ebSJeenu Viswambharan 		assert(driver_data->g0_interrupt_array);
148c639e8ebSJeenu Viswambharan 
149464ce2bbSSoby Mathew 		/* Configure the G0 SPIs */
150464ce2bbSSoby Mathew 		gicv2_secure_spis_configure(driver_data->gicd_base,
151464ce2bbSSoby Mathew 				driver_data->g0_interrupt_num,
152464ce2bbSSoby Mathew 				driver_data->g0_interrupt_array);
153dcf01a0aSDan Handley #pragma GCC diagnostic pop
154c639e8ebSJeenu Viswambharan 	}
155c639e8ebSJeenu Viswambharan #endif
156464ce2bbSSoby Mathew 
157464ce2bbSSoby Mathew 	/* Re-enable the secure SPIs now that they have been configured */
158464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT);
159464ce2bbSSoby Mathew }
160464ce2bbSSoby Mathew 
161464ce2bbSSoby Mathew /*******************************************************************************
162464ce2bbSSoby Mathew  * Initialize the ARM GICv2 driver with the provided platform inputs
163464ce2bbSSoby Mathew  ******************************************************************************/
164464ce2bbSSoby Mathew void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data)
165464ce2bbSSoby Mathew {
166464ce2bbSSoby Mathew 	unsigned int gic_version;
167*3fea9c8bSAntonio Nino Diaz 
168*3fea9c8bSAntonio Nino Diaz 	assert(plat_driver_data != NULL);
169*3fea9c8bSAntonio Nino Diaz 	assert(plat_driver_data->gicd_base != 0U);
170*3fea9c8bSAntonio Nino Diaz 	assert(plat_driver_data->gicc_base != 0U);
171464ce2bbSSoby Mathew 
172c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
173c639e8ebSJeenu Viswambharan 	if (plat_driver_data->interrupt_props == NULL) {
174c639e8ebSJeenu Viswambharan 		/* Interrupt properties array size must be 0 */
175c639e8ebSJeenu Viswambharan 		assert(plat_driver_data->interrupt_props_num == 0);
176c639e8ebSJeenu Viswambharan 
177dcf01a0aSDan Handley 		/*
178dcf01a0aSDan Handley 		 * Suppress deprecated declaration warnings in compatibility
179dcf01a0aSDan Handley 		 * function
180dcf01a0aSDan Handley 		 */
181dcf01a0aSDan Handley #pragma GCC diagnostic push
182dcf01a0aSDan Handley #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
183dcf01a0aSDan Handley 
184464ce2bbSSoby Mathew 		/*
185c639e8ebSJeenu Viswambharan 		 * If there are no interrupts of a particular type, then the
186c639e8ebSJeenu Viswambharan 		 * number of interrupts of that type should be 0 and vice-versa.
187464ce2bbSSoby Mathew 		 */
188464ce2bbSSoby Mathew 		assert(plat_driver_data->g0_interrupt_array ?
189464ce2bbSSoby Mathew 				plat_driver_data->g0_interrupt_num :
190464ce2bbSSoby Mathew 				plat_driver_data->g0_interrupt_num == 0);
191dcf01a0aSDan Handley #pragma GCC diagnostic pop
192dcf01a0aSDan Handley 
193dcf01a0aSDan Handley 		WARN("Using deprecated integer interrupt array in "
194dcf01a0aSDan Handley 		     "gicv2_driver_data_t\n");
195dcf01a0aSDan Handley 		WARN("Please migrate to using an interrupt_prop_t array\n");
196c639e8ebSJeenu Viswambharan 	}
197c639e8ebSJeenu Viswambharan #else
1989d6d800dSSamuel Holland 	assert(plat_driver_data->interrupt_props_num > 0 ?
1999d6d800dSSamuel Holland 			plat_driver_data->interrupt_props != NULL : 1);
200c639e8ebSJeenu Viswambharan #endif
201464ce2bbSSoby Mathew 
202464ce2bbSSoby Mathew 	/* Ensure that this is a GICv2 system */
203464ce2bbSSoby Mathew 	gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
204464ce2bbSSoby Mathew 	gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT)
205464ce2bbSSoby Mathew 					& PIDR2_ARCH_REV_MASK;
20664deed19SEtienne Carriere 
20764deed19SEtienne Carriere 	/*
20864deed19SEtienne Carriere 	 * GICv1 with security extension complies with trusted firmware
20964deed19SEtienne Carriere 	 * GICv2 driver as far as virtualization and few tricky power
21064deed19SEtienne Carriere 	 * features are not used. GICv2 features that are not supported
21164deed19SEtienne Carriere 	 * by GICv1 with Security Extensions are:
21264deed19SEtienne Carriere 	 * - virtual interrupt support.
21364deed19SEtienne Carriere 	 * - wake up events.
21464deed19SEtienne Carriere 	 * - writeable GIC state register (for power sequences)
21564deed19SEtienne Carriere 	 * - interrupt priority drop.
21664deed19SEtienne Carriere 	 * - interrupt signal bypass.
21764deed19SEtienne Carriere 	 */
218*3fea9c8bSAntonio Nino Diaz 	assert((gic_version == ARCH_REV_GICV2) ||
219*3fea9c8bSAntonio Nino Diaz 	       (gic_version == ARCH_REV_GICV1));
220464ce2bbSSoby Mathew 
221464ce2bbSSoby Mathew 	driver_data = plat_driver_data;
222464ce2bbSSoby Mathew 
223311b1773SSoby Mathew 	/*
224311b1773SSoby Mathew 	 * The GIC driver data is initialized by the primary CPU with caches
225311b1773SSoby Mathew 	 * enabled. When the secondary CPU boots up, it initializes the
226311b1773SSoby Mathew 	 * GICC/GICR interface with the caches disabled. Hence flush the
227311b1773SSoby Mathew 	 * driver_data to ensure coherency. This is not required if the
2289262eb54SAndrew F. Davis 	 * platform has HW_ASSISTED_COHERENCY or WARMBOOT_ENABLE_DCACHE_EARLY
2299262eb54SAndrew F. Davis 	 * enabled.
230311b1773SSoby Mathew 	 */
2319262eb54SAndrew F. Davis #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
232311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data));
233311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data));
234311b1773SSoby Mathew #endif
235464ce2bbSSoby Mathew 	INFO("ARM GICv2 driver initialized\n");
236464ce2bbSSoby Mathew }
237464ce2bbSSoby Mathew 
238464ce2bbSSoby Mathew /******************************************************************************
239464ce2bbSSoby Mathew  * This function returns whether FIQ is enabled in the GIC CPU interface.
240464ce2bbSSoby Mathew  *****************************************************************************/
241464ce2bbSSoby Mathew unsigned int gicv2_is_fiq_enabled(void)
242464ce2bbSSoby Mathew {
243464ce2bbSSoby Mathew 	unsigned int gicc_ctlr;
244464ce2bbSSoby Mathew 
245*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
246*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
247464ce2bbSSoby Mathew 
248464ce2bbSSoby Mathew 	gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base);
249*3fea9c8bSAntonio Nino Diaz 	return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1U;
250464ce2bbSSoby Mathew }
251464ce2bbSSoby Mathew 
252464ce2bbSSoby Mathew /*******************************************************************************
253464ce2bbSSoby Mathew  * This function returns the type of the highest priority pending interrupt at
254464ce2bbSSoby Mathew  * the GIC cpu interface. The return values can be one of the following :
255464ce2bbSSoby Mathew  *   PENDING_G1_INTID   : The interrupt type is non secure Group 1.
256464ce2bbSSoby Mathew  *   0 - 1019           : The interrupt type is secure Group 0.
257464ce2bbSSoby Mathew  *   GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
258464ce2bbSSoby Mathew  *                            sufficient priority to be signaled
259464ce2bbSSoby Mathew  ******************************************************************************/
260464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_type(void)
261464ce2bbSSoby Mathew {
262*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
263*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
264464ce2bbSSoby Mathew 
265464ce2bbSSoby Mathew 	return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
266464ce2bbSSoby Mathew }
267464ce2bbSSoby Mathew 
268464ce2bbSSoby Mathew /*******************************************************************************
269464ce2bbSSoby Mathew  * This function returns the id of the highest priority pending interrupt at
270464ce2bbSSoby Mathew  * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no
271464ce2bbSSoby Mathew  * interrupt pending.
272464ce2bbSSoby Mathew  ******************************************************************************/
273464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_id(void)
274464ce2bbSSoby Mathew {
275464ce2bbSSoby Mathew 	unsigned int id;
276464ce2bbSSoby Mathew 
277*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
278*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
279464ce2bbSSoby Mathew 
280464ce2bbSSoby Mathew 	id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
281464ce2bbSSoby Mathew 
282464ce2bbSSoby Mathew 	/*
283464ce2bbSSoby Mathew 	 * Find out which non-secure interrupt it is under the assumption that
284464ce2bbSSoby Mathew 	 * the GICC_CTLR.AckCtl bit is 0.
285464ce2bbSSoby Mathew 	 */
286464ce2bbSSoby Mathew 	if (id == PENDING_G1_INTID)
287464ce2bbSSoby Mathew 		id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK;
288464ce2bbSSoby Mathew 
289464ce2bbSSoby Mathew 	return id;
290464ce2bbSSoby Mathew }
291464ce2bbSSoby Mathew 
292464ce2bbSSoby Mathew /*******************************************************************************
293464ce2bbSSoby Mathew  * This functions reads the GIC cpu interface Interrupt Acknowledge register
294464ce2bbSSoby Mathew  * to start handling the pending secure 0 interrupt. It returns the
295464ce2bbSSoby Mathew  * contents of the IAR.
296464ce2bbSSoby Mathew  ******************************************************************************/
297464ce2bbSSoby Mathew unsigned int gicv2_acknowledge_interrupt(void)
298464ce2bbSSoby Mathew {
299*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
300*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
301464ce2bbSSoby Mathew 
302464ce2bbSSoby Mathew 	return gicc_read_IAR(driver_data->gicc_base);
303464ce2bbSSoby Mathew }
304464ce2bbSSoby Mathew 
305464ce2bbSSoby Mathew /*******************************************************************************
306464ce2bbSSoby Mathew  * This functions writes the GIC cpu interface End Of Interrupt register with
307464ce2bbSSoby Mathew  * the passed value to finish handling the active secure group 0 interrupt.
308464ce2bbSSoby Mathew  ******************************************************************************/
309464ce2bbSSoby Mathew void gicv2_end_of_interrupt(unsigned int id)
310464ce2bbSSoby Mathew {
311*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
312*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
313464ce2bbSSoby Mathew 
314464ce2bbSSoby Mathew 	gicc_write_EOIR(driver_data->gicc_base, id);
315464ce2bbSSoby Mathew }
316464ce2bbSSoby Mathew 
317464ce2bbSSoby Mathew /*******************************************************************************
318464ce2bbSSoby Mathew  * This function returns the type of the interrupt id depending upon the group
319464ce2bbSSoby Mathew  * this interrupt has been configured under by the interrupt controller i.e.
320464ce2bbSSoby Mathew  * group0 secure or group1 non secure. It returns zero for Group 0 secure and
321464ce2bbSSoby Mathew  * one for Group 1 non secure interrupt.
322464ce2bbSSoby Mathew  ******************************************************************************/
323464ce2bbSSoby Mathew unsigned int gicv2_get_interrupt_group(unsigned int id)
324464ce2bbSSoby Mathew {
325*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
326*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
327464ce2bbSSoby Mathew 
328464ce2bbSSoby Mathew 	return gicd_get_igroupr(driver_data->gicd_base, id);
329464ce2bbSSoby Mathew }
330eb68ea9bSJeenu Viswambharan 
331eb68ea9bSJeenu Viswambharan /*******************************************************************************
332eb68ea9bSJeenu Viswambharan  * This function returns the priority of the interrupt the processor is
333eb68ea9bSJeenu Viswambharan  * currently servicing.
334eb68ea9bSJeenu Viswambharan  ******************************************************************************/
335eb68ea9bSJeenu Viswambharan unsigned int gicv2_get_running_priority(void)
336eb68ea9bSJeenu Viswambharan {
337*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
338*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
339eb68ea9bSJeenu Viswambharan 
340eb68ea9bSJeenu Viswambharan 	return gicc_read_rpr(driver_data->gicc_base);
341eb68ea9bSJeenu Viswambharan }
342fa9db423SJeenu Viswambharan 
343fa9db423SJeenu Viswambharan /*******************************************************************************
344fa9db423SJeenu Viswambharan  * This function sets the GICv2 target mask pattern for the current PE. The PE
345fa9db423SJeenu Viswambharan  * target mask is used to translate linear PE index (returned by platform core
346fa9db423SJeenu Viswambharan  * position) to a bit mask used when targeting interrupts to a PE, viz. when
347fa9db423SJeenu Viswambharan  * raising SGIs and routing SPIs.
348fa9db423SJeenu Viswambharan  ******************************************************************************/
349fa9db423SJeenu Viswambharan void gicv2_set_pe_target_mask(unsigned int proc_num)
350fa9db423SJeenu Viswambharan {
351*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
352*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
353*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->target_masks != NULL);
354*3fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
355*3fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < driver_data->target_masks_num);
356fa9db423SJeenu Viswambharan 
357fa9db423SJeenu Viswambharan 	/* Return if the target mask is already populated */
358*3fea9c8bSAntonio Nino Diaz 	if (driver_data->target_masks[proc_num] != 0U)
359fa9db423SJeenu Viswambharan 		return;
360fa9db423SJeenu Viswambharan 
361058efeefSJeenu Viswambharan 	/*
362058efeefSJeenu Viswambharan 	 * Update target register corresponding to this CPU and flush for it to
363058efeefSJeenu Viswambharan 	 * be visible to other CPUs.
364058efeefSJeenu Viswambharan 	 */
365*3fea9c8bSAntonio Nino Diaz 	if (driver_data->target_masks[proc_num] == 0U) {
366fa9db423SJeenu Viswambharan 		driver_data->target_masks[proc_num] =
367fa9db423SJeenu Viswambharan 			gicv2_get_cpuif_id(driver_data->gicd_base);
3689262eb54SAndrew F. Davis #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
369058efeefSJeenu Viswambharan 		/*
370058efeefSJeenu Viswambharan 		 * PEs only update their own masks. Primary updates it with
371058efeefSJeenu Viswambharan 		 * caches on. But because secondaries does it with caches off,
372058efeefSJeenu Viswambharan 		 * all updates go to memory directly, and there's no danger of
373058efeefSJeenu Viswambharan 		 * secondaries overwriting each others' mask, despite
374058efeefSJeenu Viswambharan 		 * target_masks[] not being cache line aligned.
375058efeefSJeenu Viswambharan 		 */
376058efeefSJeenu Viswambharan 		flush_dcache_range((uintptr_t)
377058efeefSJeenu Viswambharan 				&driver_data->target_masks[proc_num],
378058efeefSJeenu Viswambharan 				sizeof(driver_data->target_masks[proc_num]));
379058efeefSJeenu Viswambharan #endif
380058efeefSJeenu Viswambharan 	}
381fa9db423SJeenu Viswambharan }
382cbd3f370SJeenu Viswambharan 
383cbd3f370SJeenu Viswambharan /*******************************************************************************
384cbd3f370SJeenu Viswambharan  * This function returns the active status of the interrupt (either because the
385cbd3f370SJeenu Viswambharan  * state is active, or active and pending).
386cbd3f370SJeenu Viswambharan  ******************************************************************************/
387cbd3f370SJeenu Viswambharan unsigned int gicv2_get_interrupt_active(unsigned int id)
388cbd3f370SJeenu Viswambharan {
389*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
390*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
391cbd3f370SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
392cbd3f370SJeenu Viswambharan 
393cbd3f370SJeenu Viswambharan 	return gicd_get_isactiver(driver_data->gicd_base, id);
394cbd3f370SJeenu Viswambharan }
395979225f4SJeenu Viswambharan 
396979225f4SJeenu Viswambharan /*******************************************************************************
397979225f4SJeenu Viswambharan  * This function enables the interrupt identified by id.
398979225f4SJeenu Viswambharan  ******************************************************************************/
399979225f4SJeenu Viswambharan void gicv2_enable_interrupt(unsigned int id)
400979225f4SJeenu Viswambharan {
401*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
402*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
403979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
404979225f4SJeenu Viswambharan 
405979225f4SJeenu Viswambharan 	/*
406979225f4SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
407979225f4SJeenu Viswambharan 	 * interrupt trigger are observed before enabling interrupt.
408979225f4SJeenu Viswambharan 	 */
409979225f4SJeenu Viswambharan 	dsbishst();
410979225f4SJeenu Viswambharan 	gicd_set_isenabler(driver_data->gicd_base, id);
411979225f4SJeenu Viswambharan }
412979225f4SJeenu Viswambharan 
413979225f4SJeenu Viswambharan /*******************************************************************************
414979225f4SJeenu Viswambharan  * This function disables the interrupt identified by id.
415979225f4SJeenu Viswambharan  ******************************************************************************/
416979225f4SJeenu Viswambharan void gicv2_disable_interrupt(unsigned int id)
417979225f4SJeenu Viswambharan {
418*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
419*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
420979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
421979225f4SJeenu Viswambharan 
422979225f4SJeenu Viswambharan 	/*
423979225f4SJeenu Viswambharan 	 * Disable interrupt, and ensure that any shared variable updates
424979225f4SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
425979225f4SJeenu Viswambharan 	 */
426979225f4SJeenu Viswambharan 	gicd_set_icenabler(driver_data->gicd_base, id);
427979225f4SJeenu Viswambharan 	dsbishst();
428979225f4SJeenu Viswambharan }
429f3a86600SJeenu Viswambharan 
430f3a86600SJeenu Viswambharan /*******************************************************************************
431f3a86600SJeenu Viswambharan  * This function sets the interrupt priority as supplied for the given interrupt
432f3a86600SJeenu Viswambharan  * id.
433f3a86600SJeenu Viswambharan  ******************************************************************************/
434f3a86600SJeenu Viswambharan void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority)
435f3a86600SJeenu Viswambharan {
436*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
437*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
438f3a86600SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
439f3a86600SJeenu Viswambharan 
440f3a86600SJeenu Viswambharan 	gicd_set_ipriorityr(driver_data->gicd_base, id, priority);
441f3a86600SJeenu Viswambharan }
44274dce7faSJeenu Viswambharan 
44374dce7faSJeenu Viswambharan /*******************************************************************************
44474dce7faSJeenu Viswambharan  * This function assigns group for the interrupt identified by id. The group can
44574dce7faSJeenu Viswambharan  * be any of GICV2_INTR_GROUP*
44674dce7faSJeenu Viswambharan  ******************************************************************************/
44774dce7faSJeenu Viswambharan void gicv2_set_interrupt_type(unsigned int id, unsigned int type)
44874dce7faSJeenu Viswambharan {
449*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
450*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
45174dce7faSJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
45274dce7faSJeenu Viswambharan 
45374dce7faSJeenu Viswambharan 	/* Serialize read-modify-write to Distributor registers */
45474dce7faSJeenu Viswambharan 	spin_lock(&gic_lock);
45574dce7faSJeenu Viswambharan 	switch (type) {
45674dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP1:
45774dce7faSJeenu Viswambharan 		gicd_set_igroupr(driver_data->gicd_base, id);
45874dce7faSJeenu Viswambharan 		break;
45974dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP0:
46074dce7faSJeenu Viswambharan 		gicd_clr_igroupr(driver_data->gicd_base, id);
46174dce7faSJeenu Viswambharan 		break;
46274dce7faSJeenu Viswambharan 	default:
463*3fea9c8bSAntonio Nino Diaz 		assert(false);
4645aa7498aSJonathan Wright 		break;
46574dce7faSJeenu Viswambharan 	}
46674dce7faSJeenu Viswambharan 	spin_unlock(&gic_lock);
46774dce7faSJeenu Viswambharan }
4688db978b5SJeenu Viswambharan 
4698db978b5SJeenu Viswambharan /*******************************************************************************
4708db978b5SJeenu Viswambharan  * This function raises the specified SGI to requested targets.
4718db978b5SJeenu Viswambharan  *
4728db978b5SJeenu Viswambharan  * The proc_num parameter must be the linear index of the target PE in the
4738db978b5SJeenu Viswambharan  * system.
4748db978b5SJeenu Viswambharan  ******************************************************************************/
4758db978b5SJeenu Viswambharan void gicv2_raise_sgi(int sgi_num, int proc_num)
4768db978b5SJeenu Viswambharan {
4778db978b5SJeenu Viswambharan 	unsigned int sgir_val, target;
4788db978b5SJeenu Viswambharan 
479*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
480*3fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
481*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
4828db978b5SJeenu Viswambharan 
4838db978b5SJeenu Viswambharan 	/*
4848db978b5SJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
4858db978b5SJeenu Viswambharan 	 * should be valid.
4868db978b5SJeenu Viswambharan 	 */
487*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->target_masks != NULL);
488*3fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < driver_data->target_masks_num);
4898db978b5SJeenu Viswambharan 
4908db978b5SJeenu Viswambharan 	/* Don't raise SGI if the mask hasn't been populated */
4918db978b5SJeenu Viswambharan 	target = driver_data->target_masks[proc_num];
492*3fea9c8bSAntonio Nino Diaz 	assert(target != 0U);
4938db978b5SJeenu Viswambharan 
4948db978b5SJeenu Viswambharan 	sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, sgi_num);
4958db978b5SJeenu Viswambharan 
4968db978b5SJeenu Viswambharan 	/*
4978db978b5SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
4988db978b5SJeenu Viswambharan 	 * interrupt trigger are observed before raising SGI.
4998db978b5SJeenu Viswambharan 	 */
5008db978b5SJeenu Viswambharan 	dsbishst();
5018db978b5SJeenu Viswambharan 	gicd_write_sgir(driver_data->gicd_base, sgir_val);
5028db978b5SJeenu Viswambharan }
503fc529feeSJeenu Viswambharan 
504fc529feeSJeenu Viswambharan /*******************************************************************************
505fc529feeSJeenu Viswambharan  * This function sets the interrupt routing for the given SPI interrupt id.
506fc529feeSJeenu Viswambharan  * The interrupt routing is specified in routing mode. The proc_num parameter is
507fc529feeSJeenu Viswambharan  * linear index of the PE to target SPI. When proc_num < 0, the SPI may target
508fc529feeSJeenu Viswambharan  * all PEs.
509fc529feeSJeenu Viswambharan  ******************************************************************************/
510fc529feeSJeenu Viswambharan void gicv2_set_spi_routing(unsigned int id, int proc_num)
511fc529feeSJeenu Viswambharan {
512*3fea9c8bSAntonio Nino Diaz 	unsigned int target;
513fc529feeSJeenu Viswambharan 
514*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
515*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
516fc529feeSJeenu Viswambharan 
517*3fea9c8bSAntonio Nino Diaz 	assert((id >= MIN_SPI_ID) && (id <= MAX_SPI_ID));
518fc529feeSJeenu Viswambharan 
519fc529feeSJeenu Viswambharan 	/*
520fc529feeSJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
521fc529feeSJeenu Viswambharan 	 * should be valid.
522fc529feeSJeenu Viswambharan 	 */
523*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->target_masks != NULL);
524*3fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
525*3fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < driver_data->target_masks_num);
526fc529feeSJeenu Viswambharan 
527fc529feeSJeenu Viswambharan 	if (proc_num < 0) {
528fc529feeSJeenu Viswambharan 		/* Target all PEs */
529fc529feeSJeenu Viswambharan 		target = GIC_TARGET_CPU_MASK;
530fc529feeSJeenu Viswambharan 	} else {
531fc529feeSJeenu Viswambharan 		/* Don't route interrupt if the mask hasn't been populated */
532fc529feeSJeenu Viswambharan 		target = driver_data->target_masks[proc_num];
533*3fea9c8bSAntonio Nino Diaz 		assert(target != 0U);
534fc529feeSJeenu Viswambharan 	}
535fc529feeSJeenu Viswambharan 
536fc529feeSJeenu Viswambharan 	gicd_set_itargetsr(driver_data->gicd_base, id, target);
537fc529feeSJeenu Viswambharan }
538a2816a16SJeenu Viswambharan 
539a2816a16SJeenu Viswambharan /*******************************************************************************
540a2816a16SJeenu Viswambharan  * This function clears the pending status of an interrupt identified by id.
541a2816a16SJeenu Viswambharan  ******************************************************************************/
542a2816a16SJeenu Viswambharan void gicv2_clear_interrupt_pending(unsigned int id)
543a2816a16SJeenu Viswambharan {
544*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
545*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
546a2816a16SJeenu Viswambharan 
547a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
548a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
549a2816a16SJeenu Viswambharan 
550a2816a16SJeenu Viswambharan 	/*
551a2816a16SJeenu Viswambharan 	 * Clear pending interrupt, and ensure that any shared variable updates
552a2816a16SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
553a2816a16SJeenu Viswambharan 	 */
554a2816a16SJeenu Viswambharan 	gicd_set_icpendr(driver_data->gicd_base, id);
555a2816a16SJeenu Viswambharan 	dsbishst();
556a2816a16SJeenu Viswambharan }
557a2816a16SJeenu Viswambharan 
558a2816a16SJeenu Viswambharan /*******************************************************************************
559a2816a16SJeenu Viswambharan  * This function sets the pending status of an interrupt identified by id.
560a2816a16SJeenu Viswambharan  ******************************************************************************/
561a2816a16SJeenu Viswambharan void gicv2_set_interrupt_pending(unsigned int id)
562a2816a16SJeenu Viswambharan {
563*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
564*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
565a2816a16SJeenu Viswambharan 
566a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
567a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
568a2816a16SJeenu Viswambharan 
569a2816a16SJeenu Viswambharan 	/*
570a2816a16SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
571a2816a16SJeenu Viswambharan 	 * interrupt trigger are observed before setting interrupt pending.
572a2816a16SJeenu Viswambharan 	 */
573a2816a16SJeenu Viswambharan 	dsbishst();
574a2816a16SJeenu Viswambharan 	gicd_set_ispendr(driver_data->gicd_base, id);
575a2816a16SJeenu Viswambharan }
576d55a4450SJeenu Viswambharan 
577d55a4450SJeenu Viswambharan /*******************************************************************************
578d55a4450SJeenu Viswambharan  * This function sets the PMR register with the supplied value. Returns the
579d55a4450SJeenu Viswambharan  * original PMR.
580d55a4450SJeenu Viswambharan  ******************************************************************************/
581d55a4450SJeenu Viswambharan unsigned int gicv2_set_pmr(unsigned int mask)
582d55a4450SJeenu Viswambharan {
583d55a4450SJeenu Viswambharan 	unsigned int old_mask;
584d55a4450SJeenu Viswambharan 
585*3fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
586*3fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
587d55a4450SJeenu Viswambharan 
588d55a4450SJeenu Viswambharan 	old_mask = gicc_read_pmr(driver_data->gicc_base);
589d55a4450SJeenu Viswambharan 
590d55a4450SJeenu Viswambharan 	/*
591d55a4450SJeenu Viswambharan 	 * Order memory updates w.r.t. PMR write, and ensure they're visible
592d55a4450SJeenu Viswambharan 	 * before potential out of band interrupt trigger because of PMR update.
593d55a4450SJeenu Viswambharan 	 */
594d55a4450SJeenu Viswambharan 	dmbishst();
595d55a4450SJeenu Viswambharan 	gicc_write_pmr(driver_data->gicc_base, mask);
596d55a4450SJeenu Viswambharan 	dsbishst();
597d55a4450SJeenu Viswambharan 
598d55a4450SJeenu Viswambharan 	return old_mask;
599d55a4450SJeenu Viswambharan }
600