xref: /rk3399_ARM-atf/drivers/arm/gic/v2/gicv2_main.c (revision 5eb16c4717ac08de7c627cb1ee47c3269f0ec363)
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 
709d40e0eSAntonio Nino Diaz #include <assert.h>
809d40e0eSAntonio Nino Diaz #include <stdbool.h>
909d40e0eSAntonio Nino Diaz 
10464ce2bbSSoby Mathew #include <arch.h>
11464ce2bbSSoby Mathew #include <arch_helpers.h>
1209d40e0eSAntonio Nino Diaz #include <common/debug.h>
1309d40e0eSAntonio Nino Diaz #include <common/interrupt_props.h>
1409d40e0eSAntonio Nino Diaz #include <drivers/arm/gic_common.h>
1509d40e0eSAntonio Nino Diaz #include <drivers/arm/gicv2.h>
1609d40e0eSAntonio Nino Diaz #include <lib/spinlock.h>
173fea9c8bSAntonio Nino Diaz 
18e9ec3cecSSoby Mathew #include "../common/gic_common_private.h"
19464ce2bbSSoby Mathew #include "gicv2_private.h"
20464ce2bbSSoby Mathew 
21464ce2bbSSoby Mathew static const gicv2_driver_data_t *driver_data;
22464ce2bbSSoby Mathew 
2374dce7faSJeenu Viswambharan /*
2474dce7faSJeenu Viswambharan  * Spinlock to guard registers needing read-modify-write. APIs protected by this
2574dce7faSJeenu Viswambharan  * spinlock are used either at boot time (when only a single CPU is active), or
2674dce7faSJeenu Viswambharan  * when the system is fully coherent.
2774dce7faSJeenu Viswambharan  */
287fabe1a8SRoberto Vargas static spinlock_t gic_lock;
2974dce7faSJeenu Viswambharan 
30464ce2bbSSoby Mathew /*******************************************************************************
31464ce2bbSSoby Mathew  * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
32464ce2bbSSoby Mathew  * and set the priority mask register to allow all interrupts to trickle in.
33464ce2bbSSoby Mathew  ******************************************************************************/
34464ce2bbSSoby Mathew void gicv2_cpuif_enable(void)
35464ce2bbSSoby Mathew {
36464ce2bbSSoby Mathew 	unsigned int val;
37464ce2bbSSoby Mathew 
383fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
393fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
40464ce2bbSSoby Mathew 
41464ce2bbSSoby Mathew 	/*
42464ce2bbSSoby Mathew 	 * Enable the Group 0 interrupts, FIQEn and disable Group 0/1
43464ce2bbSSoby Mathew 	 * bypass.
44464ce2bbSSoby Mathew 	 */
45464ce2bbSSoby Mathew 	val = CTLR_ENABLE_G0_BIT | FIQ_EN_BIT | FIQ_BYP_DIS_GRP0;
46464ce2bbSSoby Mathew 	val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
47464ce2bbSSoby Mathew 
48464ce2bbSSoby Mathew 	/* Program the idle priority in the PMR */
49464ce2bbSSoby Mathew 	gicc_write_pmr(driver_data->gicc_base, GIC_PRI_MASK);
50464ce2bbSSoby Mathew 	gicc_write_ctlr(driver_data->gicc_base, val);
51464ce2bbSSoby Mathew }
52464ce2bbSSoby Mathew 
53464ce2bbSSoby Mathew /*******************************************************************************
54464ce2bbSSoby Mathew  * Place the cpu interface in a state where it can never make a cpu exit wfi as
55464ce2bbSSoby Mathew  * as result of an asserted interrupt. This is critical for powering down a cpu
56464ce2bbSSoby Mathew  ******************************************************************************/
57464ce2bbSSoby Mathew void gicv2_cpuif_disable(void)
58464ce2bbSSoby Mathew {
59464ce2bbSSoby Mathew 	unsigned int val;
60464ce2bbSSoby Mathew 
613fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
623fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
63464ce2bbSSoby Mathew 
64464ce2bbSSoby Mathew 	/* Disable secure, non-secure interrupts and disable their bypass */
65464ce2bbSSoby Mathew 	val = gicc_read_ctlr(driver_data->gicc_base);
66464ce2bbSSoby Mathew 	val &= ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT);
67464ce2bbSSoby Mathew 	val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
68464ce2bbSSoby Mathew 	val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
69464ce2bbSSoby Mathew 	gicc_write_ctlr(driver_data->gicc_base, val);
70464ce2bbSSoby Mathew }
71464ce2bbSSoby Mathew 
72464ce2bbSSoby Mathew /*******************************************************************************
73464ce2bbSSoby Mathew  * Per cpu gic distributor setup which will be done by all cpus after a cold
74464ce2bbSSoby Mathew  * boot/hotplug. This marks out the secure SPIs and PPIs & enables them.
75464ce2bbSSoby Mathew  ******************************************************************************/
76464ce2bbSSoby Mathew void gicv2_pcpu_distif_init(void)
77464ce2bbSSoby Mathew {
78385f1dbbSJeenu Viswambharan 	unsigned int ctlr;
79385f1dbbSJeenu Viswambharan 
803fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
813fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
82464ce2bbSSoby Mathew 
83c639e8ebSJeenu Viswambharan 	gicv2_secure_ppi_sgi_setup_props(driver_data->gicd_base,
84c639e8ebSJeenu Viswambharan 			driver_data->interrupt_props,
85c639e8ebSJeenu Viswambharan 			driver_data->interrupt_props_num);
86385f1dbbSJeenu Viswambharan 
87385f1dbbSJeenu Viswambharan 	/* Enable G0 interrupts if not already */
88385f1dbbSJeenu Viswambharan 	ctlr = gicd_read_ctlr(driver_data->gicd_base);
893fea9c8bSAntonio Nino Diaz 	if ((ctlr & CTLR_ENABLE_G0_BIT) == 0U) {
90385f1dbbSJeenu Viswambharan 		gicd_write_ctlr(driver_data->gicd_base,
91385f1dbbSJeenu Viswambharan 				ctlr | CTLR_ENABLE_G0_BIT);
92385f1dbbSJeenu Viswambharan 	}
93c639e8ebSJeenu Viswambharan }
94464ce2bbSSoby Mathew 
95464ce2bbSSoby Mathew /*******************************************************************************
96464ce2bbSSoby Mathew  * Global gic distributor init which will be done by the primary cpu after a
97464ce2bbSSoby Mathew  * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
98464ce2bbSSoby Mathew  * then enables the secure GIC distributor interface.
99464ce2bbSSoby Mathew  ******************************************************************************/
100464ce2bbSSoby Mathew void gicv2_distif_init(void)
101464ce2bbSSoby Mathew {
102464ce2bbSSoby Mathew 	unsigned int ctlr;
103464ce2bbSSoby Mathew 
1043fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
1053fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
106464ce2bbSSoby Mathew 
107464ce2bbSSoby Mathew 	/* Disable the distributor before going further */
108464ce2bbSSoby Mathew 	ctlr = gicd_read_ctlr(driver_data->gicd_base);
109464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base,
110464ce2bbSSoby Mathew 			ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT));
111464ce2bbSSoby Mathew 
112464ce2bbSSoby Mathew 	/* Set the default attribute of all SPIs */
113464ce2bbSSoby Mathew 	gicv2_spis_configure_defaults(driver_data->gicd_base);
114464ce2bbSSoby Mathew 
115c639e8ebSJeenu Viswambharan 	gicv2_secure_spis_configure_props(driver_data->gicd_base,
116c639e8ebSJeenu Viswambharan 			driver_data->interrupt_props,
117c639e8ebSJeenu Viswambharan 			driver_data->interrupt_props_num);
118dcf01a0aSDan Handley 
119464ce2bbSSoby Mathew 
120464ce2bbSSoby Mathew 	/* Re-enable the secure SPIs now that they have been configured */
121464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT);
122464ce2bbSSoby Mathew }
123464ce2bbSSoby Mathew 
124464ce2bbSSoby Mathew /*******************************************************************************
125464ce2bbSSoby Mathew  * Initialize the ARM GICv2 driver with the provided platform inputs
126464ce2bbSSoby Mathew  ******************************************************************************/
127464ce2bbSSoby Mathew void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data)
128464ce2bbSSoby Mathew {
129464ce2bbSSoby Mathew 	unsigned int gic_version;
1303fea9c8bSAntonio Nino Diaz 
1313fea9c8bSAntonio Nino Diaz 	assert(plat_driver_data != NULL);
1323fea9c8bSAntonio Nino Diaz 	assert(plat_driver_data->gicd_base != 0U);
1333fea9c8bSAntonio Nino Diaz 	assert(plat_driver_data->gicc_base != 0U);
134464ce2bbSSoby Mathew 
1359d6d800dSSamuel Holland 	assert(plat_driver_data->interrupt_props_num > 0 ?
1369d6d800dSSamuel Holland 			plat_driver_data->interrupt_props != NULL : 1);
137464ce2bbSSoby Mathew 
138464ce2bbSSoby Mathew 	/* Ensure that this is a GICv2 system */
139464ce2bbSSoby Mathew 	gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
140464ce2bbSSoby Mathew 	gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT)
141464ce2bbSSoby Mathew 					& PIDR2_ARCH_REV_MASK;
14264deed19SEtienne Carriere 
14364deed19SEtienne Carriere 	/*
14464deed19SEtienne Carriere 	 * GICv1 with security extension complies with trusted firmware
14564deed19SEtienne Carriere 	 * GICv2 driver as far as virtualization and few tricky power
14664deed19SEtienne Carriere 	 * features are not used. GICv2 features that are not supported
14764deed19SEtienne Carriere 	 * by GICv1 with Security Extensions are:
14864deed19SEtienne Carriere 	 * - virtual interrupt support.
14964deed19SEtienne Carriere 	 * - wake up events.
15064deed19SEtienne Carriere 	 * - writeable GIC state register (for power sequences)
15164deed19SEtienne Carriere 	 * - interrupt priority drop.
15264deed19SEtienne Carriere 	 * - interrupt signal bypass.
15364deed19SEtienne Carriere 	 */
1543fea9c8bSAntonio Nino Diaz 	assert((gic_version == ARCH_REV_GICV2) ||
1553fea9c8bSAntonio Nino Diaz 	       (gic_version == ARCH_REV_GICV1));
156464ce2bbSSoby Mathew 
157464ce2bbSSoby Mathew 	driver_data = plat_driver_data;
158464ce2bbSSoby Mathew 
159311b1773SSoby Mathew 	/*
160311b1773SSoby Mathew 	 * The GIC driver data is initialized by the primary CPU with caches
161311b1773SSoby Mathew 	 * enabled. When the secondary CPU boots up, it initializes the
162311b1773SSoby Mathew 	 * GICC/GICR interface with the caches disabled. Hence flush the
163311b1773SSoby Mathew 	 * driver_data to ensure coherency. This is not required if the
1649262eb54SAndrew F. Davis 	 * platform has HW_ASSISTED_COHERENCY or WARMBOOT_ENABLE_DCACHE_EARLY
1659262eb54SAndrew F. Davis 	 * enabled.
166311b1773SSoby Mathew 	 */
1679262eb54SAndrew F. Davis #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
168311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data));
169311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data));
170311b1773SSoby Mathew #endif
171464ce2bbSSoby Mathew 	INFO("ARM GICv2 driver initialized\n");
172464ce2bbSSoby Mathew }
173464ce2bbSSoby Mathew 
174464ce2bbSSoby Mathew /******************************************************************************
175464ce2bbSSoby Mathew  * This function returns whether FIQ is enabled in the GIC CPU interface.
176464ce2bbSSoby Mathew  *****************************************************************************/
177464ce2bbSSoby Mathew unsigned int gicv2_is_fiq_enabled(void)
178464ce2bbSSoby Mathew {
179464ce2bbSSoby Mathew 	unsigned int gicc_ctlr;
180464ce2bbSSoby Mathew 
1813fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
1823fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
183464ce2bbSSoby Mathew 
184464ce2bbSSoby Mathew 	gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base);
1853fea9c8bSAntonio Nino Diaz 	return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1U;
186464ce2bbSSoby Mathew }
187464ce2bbSSoby Mathew 
188464ce2bbSSoby Mathew /*******************************************************************************
189464ce2bbSSoby Mathew  * This function returns the type of the highest priority pending interrupt at
190464ce2bbSSoby Mathew  * the GIC cpu interface. The return values can be one of the following :
191464ce2bbSSoby Mathew  *   PENDING_G1_INTID   : The interrupt type is non secure Group 1.
192464ce2bbSSoby Mathew  *   0 - 1019           : The interrupt type is secure Group 0.
193464ce2bbSSoby Mathew  *   GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
194464ce2bbSSoby Mathew  *                            sufficient priority to be signaled
195464ce2bbSSoby Mathew  ******************************************************************************/
196464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_type(void)
197464ce2bbSSoby Mathew {
1983fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
1993fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
200464ce2bbSSoby Mathew 
201464ce2bbSSoby Mathew 	return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
202464ce2bbSSoby Mathew }
203464ce2bbSSoby Mathew 
204464ce2bbSSoby Mathew /*******************************************************************************
205464ce2bbSSoby Mathew  * This function returns the id of the highest priority pending interrupt at
206464ce2bbSSoby Mathew  * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no
207464ce2bbSSoby Mathew  * interrupt pending.
208464ce2bbSSoby Mathew  ******************************************************************************/
209464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_id(void)
210464ce2bbSSoby Mathew {
211464ce2bbSSoby Mathew 	unsigned int id;
212464ce2bbSSoby Mathew 
2133fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
2143fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
215464ce2bbSSoby Mathew 
216464ce2bbSSoby Mathew 	id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
217464ce2bbSSoby Mathew 
218464ce2bbSSoby Mathew 	/*
219464ce2bbSSoby Mathew 	 * Find out which non-secure interrupt it is under the assumption that
220464ce2bbSSoby Mathew 	 * the GICC_CTLR.AckCtl bit is 0.
221464ce2bbSSoby Mathew 	 */
222464ce2bbSSoby Mathew 	if (id == PENDING_G1_INTID)
223464ce2bbSSoby Mathew 		id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK;
224464ce2bbSSoby Mathew 
225464ce2bbSSoby Mathew 	return id;
226464ce2bbSSoby Mathew }
227464ce2bbSSoby Mathew 
228464ce2bbSSoby Mathew /*******************************************************************************
229464ce2bbSSoby Mathew  * This functions reads the GIC cpu interface Interrupt Acknowledge register
230464ce2bbSSoby Mathew  * to start handling the pending secure 0 interrupt. It returns the
231464ce2bbSSoby Mathew  * contents of the IAR.
232464ce2bbSSoby Mathew  ******************************************************************************/
233464ce2bbSSoby Mathew unsigned int gicv2_acknowledge_interrupt(void)
234464ce2bbSSoby Mathew {
2353fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
2363fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
237464ce2bbSSoby Mathew 
238464ce2bbSSoby Mathew 	return gicc_read_IAR(driver_data->gicc_base);
239464ce2bbSSoby Mathew }
240464ce2bbSSoby Mathew 
241464ce2bbSSoby Mathew /*******************************************************************************
242464ce2bbSSoby Mathew  * This functions writes the GIC cpu interface End Of Interrupt register with
243464ce2bbSSoby Mathew  * the passed value to finish handling the active secure group 0 interrupt.
244464ce2bbSSoby Mathew  ******************************************************************************/
245464ce2bbSSoby Mathew void gicv2_end_of_interrupt(unsigned int id)
246464ce2bbSSoby Mathew {
2473fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
2483fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
249464ce2bbSSoby Mathew 
250*5eb16c47SSandeep Tripathy 	/*
251*5eb16c47SSandeep Tripathy 	 * Ensure the write to peripheral registers are *complete* before the write
252*5eb16c47SSandeep Tripathy 	 * to GIC_EOIR.
253*5eb16c47SSandeep Tripathy 	 *
254*5eb16c47SSandeep Tripathy 	 * Note: The completion gurantee depends on various factors of system design
255*5eb16c47SSandeep Tripathy 	 * and the barrier is the best core can do by which execution of further
256*5eb16c47SSandeep Tripathy 	 * instructions waits till the barrier is alive.
257*5eb16c47SSandeep Tripathy 	 */
258*5eb16c47SSandeep Tripathy 	dsbishst();
259464ce2bbSSoby Mathew 	gicc_write_EOIR(driver_data->gicc_base, id);
260464ce2bbSSoby Mathew }
261464ce2bbSSoby Mathew 
262464ce2bbSSoby Mathew /*******************************************************************************
263464ce2bbSSoby Mathew  * This function returns the type of the interrupt id depending upon the group
264464ce2bbSSoby Mathew  * this interrupt has been configured under by the interrupt controller i.e.
265464ce2bbSSoby Mathew  * group0 secure or group1 non secure. It returns zero for Group 0 secure and
266464ce2bbSSoby Mathew  * one for Group 1 non secure interrupt.
267464ce2bbSSoby Mathew  ******************************************************************************/
268464ce2bbSSoby Mathew unsigned int gicv2_get_interrupt_group(unsigned int id)
269464ce2bbSSoby Mathew {
2703fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
2713fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
272464ce2bbSSoby Mathew 
273464ce2bbSSoby Mathew 	return gicd_get_igroupr(driver_data->gicd_base, id);
274464ce2bbSSoby Mathew }
275eb68ea9bSJeenu Viswambharan 
276eb68ea9bSJeenu Viswambharan /*******************************************************************************
277eb68ea9bSJeenu Viswambharan  * This function returns the priority of the interrupt the processor is
278eb68ea9bSJeenu Viswambharan  * currently servicing.
279eb68ea9bSJeenu Viswambharan  ******************************************************************************/
280eb68ea9bSJeenu Viswambharan unsigned int gicv2_get_running_priority(void)
281eb68ea9bSJeenu Viswambharan {
2823fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
2833fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
284eb68ea9bSJeenu Viswambharan 
285eb68ea9bSJeenu Viswambharan 	return gicc_read_rpr(driver_data->gicc_base);
286eb68ea9bSJeenu Viswambharan }
287fa9db423SJeenu Viswambharan 
288fa9db423SJeenu Viswambharan /*******************************************************************************
289fa9db423SJeenu Viswambharan  * This function sets the GICv2 target mask pattern for the current PE. The PE
290fa9db423SJeenu Viswambharan  * target mask is used to translate linear PE index (returned by platform core
29173308618SAntonio Nino Diaz  * position) to a bit mask used when targeting interrupts to a PE (for example
29273308618SAntonio Nino Diaz  * when raising SGIs and routing SPIs).
293fa9db423SJeenu Viswambharan  ******************************************************************************/
294fa9db423SJeenu Viswambharan void gicv2_set_pe_target_mask(unsigned int proc_num)
295fa9db423SJeenu Viswambharan {
2963fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
2973fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
2983fea9c8bSAntonio Nino Diaz 	assert(driver_data->target_masks != NULL);
2993fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
3003fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < driver_data->target_masks_num);
301fa9db423SJeenu Viswambharan 
302fa9db423SJeenu Viswambharan 	/* Return if the target mask is already populated */
3033fea9c8bSAntonio Nino Diaz 	if (driver_data->target_masks[proc_num] != 0U)
304fa9db423SJeenu Viswambharan 		return;
305fa9db423SJeenu Viswambharan 
306058efeefSJeenu Viswambharan 	/*
307058efeefSJeenu Viswambharan 	 * Update target register corresponding to this CPU and flush for it to
308058efeefSJeenu Viswambharan 	 * be visible to other CPUs.
309058efeefSJeenu Viswambharan 	 */
3103fea9c8bSAntonio Nino Diaz 	if (driver_data->target_masks[proc_num] == 0U) {
311fa9db423SJeenu Viswambharan 		driver_data->target_masks[proc_num] =
312fa9db423SJeenu Viswambharan 			gicv2_get_cpuif_id(driver_data->gicd_base);
3139262eb54SAndrew F. Davis #if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
314058efeefSJeenu Viswambharan 		/*
315058efeefSJeenu Viswambharan 		 * PEs only update their own masks. Primary updates it with
316058efeefSJeenu Viswambharan 		 * caches on. But because secondaries does it with caches off,
317058efeefSJeenu Viswambharan 		 * all updates go to memory directly, and there's no danger of
318058efeefSJeenu Viswambharan 		 * secondaries overwriting each others' mask, despite
319058efeefSJeenu Viswambharan 		 * target_masks[] not being cache line aligned.
320058efeefSJeenu Viswambharan 		 */
321058efeefSJeenu Viswambharan 		flush_dcache_range((uintptr_t)
322058efeefSJeenu Viswambharan 				&driver_data->target_masks[proc_num],
323058efeefSJeenu Viswambharan 				sizeof(driver_data->target_masks[proc_num]));
324058efeefSJeenu Viswambharan #endif
325058efeefSJeenu Viswambharan 	}
326fa9db423SJeenu Viswambharan }
327cbd3f370SJeenu Viswambharan 
328cbd3f370SJeenu Viswambharan /*******************************************************************************
329cbd3f370SJeenu Viswambharan  * This function returns the active status of the interrupt (either because the
330cbd3f370SJeenu Viswambharan  * state is active, or active and pending).
331cbd3f370SJeenu Viswambharan  ******************************************************************************/
332cbd3f370SJeenu Viswambharan unsigned int gicv2_get_interrupt_active(unsigned int id)
333cbd3f370SJeenu Viswambharan {
3343fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
3353fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
336cbd3f370SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
337cbd3f370SJeenu Viswambharan 
338cbd3f370SJeenu Viswambharan 	return gicd_get_isactiver(driver_data->gicd_base, id);
339cbd3f370SJeenu Viswambharan }
340979225f4SJeenu Viswambharan 
341979225f4SJeenu Viswambharan /*******************************************************************************
342979225f4SJeenu Viswambharan  * This function enables the interrupt identified by id.
343979225f4SJeenu Viswambharan  ******************************************************************************/
344979225f4SJeenu Viswambharan void gicv2_enable_interrupt(unsigned int id)
345979225f4SJeenu Viswambharan {
3463fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
3473fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
348979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
349979225f4SJeenu Viswambharan 
350979225f4SJeenu Viswambharan 	/*
351979225f4SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
352979225f4SJeenu Viswambharan 	 * interrupt trigger are observed before enabling interrupt.
353979225f4SJeenu Viswambharan 	 */
354979225f4SJeenu Viswambharan 	dsbishst();
355979225f4SJeenu Viswambharan 	gicd_set_isenabler(driver_data->gicd_base, id);
356979225f4SJeenu Viswambharan }
357979225f4SJeenu Viswambharan 
358979225f4SJeenu Viswambharan /*******************************************************************************
359979225f4SJeenu Viswambharan  * This function disables the interrupt identified by id.
360979225f4SJeenu Viswambharan  ******************************************************************************/
361979225f4SJeenu Viswambharan void gicv2_disable_interrupt(unsigned int id)
362979225f4SJeenu Viswambharan {
3633fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
3643fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
365979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
366979225f4SJeenu Viswambharan 
367979225f4SJeenu Viswambharan 	/*
368979225f4SJeenu Viswambharan 	 * Disable interrupt, and ensure that any shared variable updates
369979225f4SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
370979225f4SJeenu Viswambharan 	 */
371979225f4SJeenu Viswambharan 	gicd_set_icenabler(driver_data->gicd_base, id);
372979225f4SJeenu Viswambharan 	dsbishst();
373979225f4SJeenu Viswambharan }
374f3a86600SJeenu Viswambharan 
375f3a86600SJeenu Viswambharan /*******************************************************************************
376f3a86600SJeenu Viswambharan  * This function sets the interrupt priority as supplied for the given interrupt
377f3a86600SJeenu Viswambharan  * id.
378f3a86600SJeenu Viswambharan  ******************************************************************************/
379f3a86600SJeenu Viswambharan void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority)
380f3a86600SJeenu Viswambharan {
3813fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
3823fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
383f3a86600SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
384f3a86600SJeenu Viswambharan 
385f3a86600SJeenu Viswambharan 	gicd_set_ipriorityr(driver_data->gicd_base, id, priority);
386f3a86600SJeenu Viswambharan }
38774dce7faSJeenu Viswambharan 
38874dce7faSJeenu Viswambharan /*******************************************************************************
38974dce7faSJeenu Viswambharan  * This function assigns group for the interrupt identified by id. The group can
39074dce7faSJeenu Viswambharan  * be any of GICV2_INTR_GROUP*
39174dce7faSJeenu Viswambharan  ******************************************************************************/
39274dce7faSJeenu Viswambharan void gicv2_set_interrupt_type(unsigned int id, unsigned int type)
39374dce7faSJeenu Viswambharan {
3943fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
3953fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
39674dce7faSJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
39774dce7faSJeenu Viswambharan 
39874dce7faSJeenu Viswambharan 	/* Serialize read-modify-write to Distributor registers */
39974dce7faSJeenu Viswambharan 	spin_lock(&gic_lock);
40074dce7faSJeenu Viswambharan 	switch (type) {
40174dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP1:
40274dce7faSJeenu Viswambharan 		gicd_set_igroupr(driver_data->gicd_base, id);
40374dce7faSJeenu Viswambharan 		break;
40474dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP0:
40574dce7faSJeenu Viswambharan 		gicd_clr_igroupr(driver_data->gicd_base, id);
40674dce7faSJeenu Viswambharan 		break;
40774dce7faSJeenu Viswambharan 	default:
4083fea9c8bSAntonio Nino Diaz 		assert(false);
4095aa7498aSJonathan Wright 		break;
41074dce7faSJeenu Viswambharan 	}
41174dce7faSJeenu Viswambharan 	spin_unlock(&gic_lock);
41274dce7faSJeenu Viswambharan }
4138db978b5SJeenu Viswambharan 
4148db978b5SJeenu Viswambharan /*******************************************************************************
4158db978b5SJeenu Viswambharan  * This function raises the specified SGI to requested targets.
4168db978b5SJeenu Viswambharan  *
4178db978b5SJeenu Viswambharan  * The proc_num parameter must be the linear index of the target PE in the
4188db978b5SJeenu Viswambharan  * system.
4198db978b5SJeenu Viswambharan  ******************************************************************************/
4208db978b5SJeenu Viswambharan void gicv2_raise_sgi(int sgi_num, int proc_num)
4218db978b5SJeenu Viswambharan {
4228db978b5SJeenu Viswambharan 	unsigned int sgir_val, target;
4238db978b5SJeenu Viswambharan 
4243fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
4253fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
4263fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
4278db978b5SJeenu Viswambharan 
4288db978b5SJeenu Viswambharan 	/*
4298db978b5SJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
4308db978b5SJeenu Viswambharan 	 * should be valid.
4318db978b5SJeenu Viswambharan 	 */
4323fea9c8bSAntonio Nino Diaz 	assert(driver_data->target_masks != NULL);
4333fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < driver_data->target_masks_num);
4348db978b5SJeenu Viswambharan 
4358db978b5SJeenu Viswambharan 	/* Don't raise SGI if the mask hasn't been populated */
4368db978b5SJeenu Viswambharan 	target = driver_data->target_masks[proc_num];
4373fea9c8bSAntonio Nino Diaz 	assert(target != 0U);
4388db978b5SJeenu Viswambharan 
4398db978b5SJeenu Viswambharan 	sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, sgi_num);
4408db978b5SJeenu Viswambharan 
4418db978b5SJeenu Viswambharan 	/*
4428db978b5SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
4438db978b5SJeenu Viswambharan 	 * interrupt trigger are observed before raising SGI.
4448db978b5SJeenu Viswambharan 	 */
4458db978b5SJeenu Viswambharan 	dsbishst();
4468db978b5SJeenu Viswambharan 	gicd_write_sgir(driver_data->gicd_base, sgir_val);
4478db978b5SJeenu Viswambharan }
448fc529feeSJeenu Viswambharan 
449fc529feeSJeenu Viswambharan /*******************************************************************************
450fc529feeSJeenu Viswambharan  * This function sets the interrupt routing for the given SPI interrupt id.
451fc529feeSJeenu Viswambharan  * The interrupt routing is specified in routing mode. The proc_num parameter is
452fc529feeSJeenu Viswambharan  * linear index of the PE to target SPI. When proc_num < 0, the SPI may target
453fc529feeSJeenu Viswambharan  * all PEs.
454fc529feeSJeenu Viswambharan  ******************************************************************************/
455fc529feeSJeenu Viswambharan void gicv2_set_spi_routing(unsigned int id, int proc_num)
456fc529feeSJeenu Viswambharan {
4573fea9c8bSAntonio Nino Diaz 	unsigned int target;
458fc529feeSJeenu Viswambharan 
4593fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
4603fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
461fc529feeSJeenu Viswambharan 
4623fea9c8bSAntonio Nino Diaz 	assert((id >= MIN_SPI_ID) && (id <= MAX_SPI_ID));
463fc529feeSJeenu Viswambharan 
464fc529feeSJeenu Viswambharan 	/*
465fc529feeSJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
466fc529feeSJeenu Viswambharan 	 * should be valid.
467fc529feeSJeenu Viswambharan 	 */
4683fea9c8bSAntonio Nino Diaz 	assert(driver_data->target_masks != NULL);
4693fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
4703fea9c8bSAntonio Nino Diaz 	assert((unsigned int)proc_num < driver_data->target_masks_num);
471fc529feeSJeenu Viswambharan 
472fc529feeSJeenu Viswambharan 	if (proc_num < 0) {
473fc529feeSJeenu Viswambharan 		/* Target all PEs */
474fc529feeSJeenu Viswambharan 		target = GIC_TARGET_CPU_MASK;
475fc529feeSJeenu Viswambharan 	} else {
476fc529feeSJeenu Viswambharan 		/* Don't route interrupt if the mask hasn't been populated */
477fc529feeSJeenu Viswambharan 		target = driver_data->target_masks[proc_num];
4783fea9c8bSAntonio Nino Diaz 		assert(target != 0U);
479fc529feeSJeenu Viswambharan 	}
480fc529feeSJeenu Viswambharan 
481fc529feeSJeenu Viswambharan 	gicd_set_itargetsr(driver_data->gicd_base, id, target);
482fc529feeSJeenu Viswambharan }
483a2816a16SJeenu Viswambharan 
484a2816a16SJeenu Viswambharan /*******************************************************************************
485a2816a16SJeenu Viswambharan  * This function clears the pending status of an interrupt identified by id.
486a2816a16SJeenu Viswambharan  ******************************************************************************/
487a2816a16SJeenu Viswambharan void gicv2_clear_interrupt_pending(unsigned int id)
488a2816a16SJeenu Viswambharan {
4893fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
4903fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
491a2816a16SJeenu Viswambharan 
492a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
493a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
494a2816a16SJeenu Viswambharan 
495a2816a16SJeenu Viswambharan 	/*
496a2816a16SJeenu Viswambharan 	 * Clear pending interrupt, and ensure that any shared variable updates
497a2816a16SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
498a2816a16SJeenu Viswambharan 	 */
499a2816a16SJeenu Viswambharan 	gicd_set_icpendr(driver_data->gicd_base, id);
500a2816a16SJeenu Viswambharan 	dsbishst();
501a2816a16SJeenu Viswambharan }
502a2816a16SJeenu Viswambharan 
503a2816a16SJeenu Viswambharan /*******************************************************************************
504a2816a16SJeenu Viswambharan  * This function sets the pending status of an interrupt identified by id.
505a2816a16SJeenu Viswambharan  ******************************************************************************/
506a2816a16SJeenu Viswambharan void gicv2_set_interrupt_pending(unsigned int id)
507a2816a16SJeenu Viswambharan {
5083fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
5093fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicd_base != 0U);
510a2816a16SJeenu Viswambharan 
511a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
512a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
513a2816a16SJeenu Viswambharan 
514a2816a16SJeenu Viswambharan 	/*
515a2816a16SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
516a2816a16SJeenu Viswambharan 	 * interrupt trigger are observed before setting interrupt pending.
517a2816a16SJeenu Viswambharan 	 */
518a2816a16SJeenu Viswambharan 	dsbishst();
519a2816a16SJeenu Viswambharan 	gicd_set_ispendr(driver_data->gicd_base, id);
520a2816a16SJeenu Viswambharan }
521d55a4450SJeenu Viswambharan 
522d55a4450SJeenu Viswambharan /*******************************************************************************
523d55a4450SJeenu Viswambharan  * This function sets the PMR register with the supplied value. Returns the
524d55a4450SJeenu Viswambharan  * original PMR.
525d55a4450SJeenu Viswambharan  ******************************************************************************/
526d55a4450SJeenu Viswambharan unsigned int gicv2_set_pmr(unsigned int mask)
527d55a4450SJeenu Viswambharan {
528d55a4450SJeenu Viswambharan 	unsigned int old_mask;
529d55a4450SJeenu Viswambharan 
5303fea9c8bSAntonio Nino Diaz 	assert(driver_data != NULL);
5313fea9c8bSAntonio Nino Diaz 	assert(driver_data->gicc_base != 0U);
532d55a4450SJeenu Viswambharan 
533d55a4450SJeenu Viswambharan 	old_mask = gicc_read_pmr(driver_data->gicc_base);
534d55a4450SJeenu Viswambharan 
535d55a4450SJeenu Viswambharan 	/*
536d55a4450SJeenu Viswambharan 	 * Order memory updates w.r.t. PMR write, and ensure they're visible
537d55a4450SJeenu Viswambharan 	 * before potential out of band interrupt trigger because of PMR update.
538d55a4450SJeenu Viswambharan 	 */
539d55a4450SJeenu Viswambharan 	dmbishst();
540d55a4450SJeenu Viswambharan 	gicc_write_pmr(driver_data->gicc_base, mask);
541d55a4450SJeenu Viswambharan 	dsbishst();
542d55a4450SJeenu Viswambharan 
543d55a4450SJeenu Viswambharan 	return old_mask;
544d55a4450SJeenu Viswambharan }
5454acd900dSMarcin Wojtas 
5464acd900dSMarcin Wojtas /*******************************************************************************
5474acd900dSMarcin Wojtas  * This function updates single interrupt configuration to be level/edge
5484acd900dSMarcin Wojtas  * triggered
5494acd900dSMarcin Wojtas  ******************************************************************************/
5504acd900dSMarcin Wojtas void gicv2_interrupt_set_cfg(unsigned int id, unsigned int cfg)
5514acd900dSMarcin Wojtas {
5524acd900dSMarcin Wojtas 	gicd_set_icfgr(driver_data->gicd_base, id, cfg);
5534acd900dSMarcin Wojtas }
554