xref: /rk3399_ARM-atf/drivers/arm/gic/v2/gicv2_main.c (revision 7fabe1a89903cab4919304faa085ee6eaaec5c9d)
1464ce2bbSSoby Mathew /*
2*7fabe1a8SRoberto 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>
15e9ec3cecSSoby Mathew #include "../common/gic_common_private.h"
16464ce2bbSSoby Mathew #include "gicv2_private.h"
17464ce2bbSSoby Mathew 
18464ce2bbSSoby Mathew static const gicv2_driver_data_t *driver_data;
19464ce2bbSSoby Mathew 
2074dce7faSJeenu Viswambharan /*
2174dce7faSJeenu Viswambharan  * Spinlock to guard registers needing read-modify-write. APIs protected by this
2274dce7faSJeenu Viswambharan  * spinlock are used either at boot time (when only a single CPU is active), or
2374dce7faSJeenu Viswambharan  * when the system is fully coherent.
2474dce7faSJeenu Viswambharan  */
25*7fabe1a8SRoberto Vargas static spinlock_t gic_lock;
2674dce7faSJeenu Viswambharan 
27464ce2bbSSoby Mathew /*******************************************************************************
28464ce2bbSSoby Mathew  * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
29464ce2bbSSoby Mathew  * and set the priority mask register to allow all interrupts to trickle in.
30464ce2bbSSoby Mathew  ******************************************************************************/
31464ce2bbSSoby Mathew void gicv2_cpuif_enable(void)
32464ce2bbSSoby Mathew {
33464ce2bbSSoby Mathew 	unsigned int val;
34464ce2bbSSoby Mathew 
35464ce2bbSSoby Mathew 	assert(driver_data);
36464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
37464ce2bbSSoby Mathew 
38464ce2bbSSoby Mathew 	/*
39464ce2bbSSoby Mathew 	 * Enable the Group 0 interrupts, FIQEn and disable Group 0/1
40464ce2bbSSoby Mathew 	 * bypass.
41464ce2bbSSoby Mathew 	 */
42464ce2bbSSoby Mathew 	val = CTLR_ENABLE_G0_BIT | FIQ_EN_BIT | FIQ_BYP_DIS_GRP0;
43464ce2bbSSoby Mathew 	val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
44464ce2bbSSoby Mathew 
45464ce2bbSSoby Mathew 	/* Program the idle priority in the PMR */
46464ce2bbSSoby Mathew 	gicc_write_pmr(driver_data->gicc_base, GIC_PRI_MASK);
47464ce2bbSSoby Mathew 	gicc_write_ctlr(driver_data->gicc_base, val);
48464ce2bbSSoby Mathew }
49464ce2bbSSoby Mathew 
50464ce2bbSSoby Mathew /*******************************************************************************
51464ce2bbSSoby Mathew  * Place the cpu interface in a state where it can never make a cpu exit wfi as
52464ce2bbSSoby Mathew  * as result of an asserted interrupt. This is critical for powering down a cpu
53464ce2bbSSoby Mathew  ******************************************************************************/
54464ce2bbSSoby Mathew void gicv2_cpuif_disable(void)
55464ce2bbSSoby Mathew {
56464ce2bbSSoby Mathew 	unsigned int val;
57464ce2bbSSoby Mathew 
58464ce2bbSSoby Mathew 	assert(driver_data);
59464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
60464ce2bbSSoby Mathew 
61464ce2bbSSoby Mathew 	/* Disable secure, non-secure interrupts and disable their bypass */
62464ce2bbSSoby Mathew 	val = gicc_read_ctlr(driver_data->gicc_base);
63464ce2bbSSoby Mathew 	val &= ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT);
64464ce2bbSSoby Mathew 	val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
65464ce2bbSSoby Mathew 	val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
66464ce2bbSSoby Mathew 	gicc_write_ctlr(driver_data->gicc_base, val);
67464ce2bbSSoby Mathew }
68464ce2bbSSoby Mathew 
69464ce2bbSSoby Mathew /*******************************************************************************
70464ce2bbSSoby Mathew  * Per cpu gic distributor setup which will be done by all cpus after a cold
71464ce2bbSSoby Mathew  * boot/hotplug. This marks out the secure SPIs and PPIs & enables them.
72464ce2bbSSoby Mathew  ******************************************************************************/
73464ce2bbSSoby Mathew void gicv2_pcpu_distif_init(void)
74464ce2bbSSoby Mathew {
75385f1dbbSJeenu Viswambharan 	unsigned int ctlr;
76385f1dbbSJeenu Viswambharan 
77464ce2bbSSoby Mathew 	assert(driver_data);
78464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
79464ce2bbSSoby Mathew 
80c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
81c639e8ebSJeenu Viswambharan 	if (driver_data->interrupt_props != NULL) {
82c639e8ebSJeenu Viswambharan #endif
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);
86c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
87c639e8ebSJeenu Viswambharan 	} else {
88c639e8ebSJeenu Viswambharan 		assert(driver_data->g0_interrupt_array);
89464ce2bbSSoby Mathew 		gicv2_secure_ppi_sgi_setup(driver_data->gicd_base,
90464ce2bbSSoby Mathew 				driver_data->g0_interrupt_num,
91464ce2bbSSoby Mathew 				driver_data->g0_interrupt_array);
92464ce2bbSSoby Mathew 	}
93c639e8ebSJeenu Viswambharan #endif
94385f1dbbSJeenu Viswambharan 
95385f1dbbSJeenu Viswambharan 	/* Enable G0 interrupts if not already */
96385f1dbbSJeenu Viswambharan 	ctlr = gicd_read_ctlr(driver_data->gicd_base);
97385f1dbbSJeenu Viswambharan 	if ((ctlr & CTLR_ENABLE_G0_BIT) == 0) {
98385f1dbbSJeenu Viswambharan 		gicd_write_ctlr(driver_data->gicd_base,
99385f1dbbSJeenu Viswambharan 				ctlr | CTLR_ENABLE_G0_BIT);
100385f1dbbSJeenu Viswambharan 	}
101c639e8ebSJeenu Viswambharan }
102464ce2bbSSoby Mathew 
103464ce2bbSSoby Mathew /*******************************************************************************
104464ce2bbSSoby Mathew  * Global gic distributor init which will be done by the primary cpu after a
105464ce2bbSSoby Mathew  * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
106464ce2bbSSoby Mathew  * then enables the secure GIC distributor interface.
107464ce2bbSSoby Mathew  ******************************************************************************/
108464ce2bbSSoby Mathew void gicv2_distif_init(void)
109464ce2bbSSoby Mathew {
110464ce2bbSSoby Mathew 	unsigned int ctlr;
111464ce2bbSSoby Mathew 
112464ce2bbSSoby Mathew 	assert(driver_data);
113464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
114464ce2bbSSoby Mathew 
115464ce2bbSSoby Mathew 	/* Disable the distributor before going further */
116464ce2bbSSoby Mathew 	ctlr = gicd_read_ctlr(driver_data->gicd_base);
117464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base,
118464ce2bbSSoby Mathew 			ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT));
119464ce2bbSSoby Mathew 
120464ce2bbSSoby Mathew 	/* Set the default attribute of all SPIs */
121464ce2bbSSoby Mathew 	gicv2_spis_configure_defaults(driver_data->gicd_base);
122464ce2bbSSoby Mathew 
123c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
124c639e8ebSJeenu Viswambharan 	if (driver_data->interrupt_props != NULL) {
125c639e8ebSJeenu Viswambharan #endif
126c639e8ebSJeenu Viswambharan 		gicv2_secure_spis_configure_props(driver_data->gicd_base,
127c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props,
128c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props_num);
129c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
130c639e8ebSJeenu Viswambharan 	} else {
131c639e8ebSJeenu Viswambharan 		assert(driver_data->g0_interrupt_array);
132c639e8ebSJeenu Viswambharan 
133464ce2bbSSoby Mathew 		/* Configure the G0 SPIs */
134464ce2bbSSoby Mathew 		gicv2_secure_spis_configure(driver_data->gicd_base,
135464ce2bbSSoby Mathew 				driver_data->g0_interrupt_num,
136464ce2bbSSoby Mathew 				driver_data->g0_interrupt_array);
137c639e8ebSJeenu Viswambharan 	}
138c639e8ebSJeenu Viswambharan #endif
139464ce2bbSSoby Mathew 
140464ce2bbSSoby Mathew 	/* Re-enable the secure SPIs now that they have been configured */
141464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT);
142464ce2bbSSoby Mathew }
143464ce2bbSSoby Mathew 
144464ce2bbSSoby Mathew /*******************************************************************************
145464ce2bbSSoby Mathew  * Initialize the ARM GICv2 driver with the provided platform inputs
146464ce2bbSSoby Mathew  ******************************************************************************/
147464ce2bbSSoby Mathew void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data)
148464ce2bbSSoby Mathew {
149464ce2bbSSoby Mathew 	unsigned int gic_version;
150464ce2bbSSoby Mathew 	assert(plat_driver_data);
151464ce2bbSSoby Mathew 	assert(plat_driver_data->gicd_base);
152464ce2bbSSoby Mathew 	assert(plat_driver_data->gicc_base);
153464ce2bbSSoby Mathew 
154c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
155c639e8ebSJeenu Viswambharan 	if (plat_driver_data->interrupt_props == NULL) {
156c639e8ebSJeenu Viswambharan 		/* Interrupt properties array size must be 0 */
157c639e8ebSJeenu Viswambharan 		assert(plat_driver_data->interrupt_props_num == 0);
158c639e8ebSJeenu Viswambharan 
159c639e8ebSJeenu Viswambharan 		/* The platform should provide a list of secure interrupts */
160464ce2bbSSoby Mathew 		assert(plat_driver_data->g0_interrupt_array);
161464ce2bbSSoby Mathew 
162464ce2bbSSoby Mathew 		/*
163c639e8ebSJeenu Viswambharan 		 * If there are no interrupts of a particular type, then the
164c639e8ebSJeenu Viswambharan 		 * number of interrupts of that type should be 0 and vice-versa.
165464ce2bbSSoby Mathew 		 */
166464ce2bbSSoby Mathew 		assert(plat_driver_data->g0_interrupt_array ?
167464ce2bbSSoby Mathew 				plat_driver_data->g0_interrupt_num :
168464ce2bbSSoby Mathew 				plat_driver_data->g0_interrupt_num == 0);
169c639e8ebSJeenu Viswambharan 	}
170c639e8ebSJeenu Viswambharan #else
171c639e8ebSJeenu Viswambharan 	assert(plat_driver_data->interrupt_props != NULL);
172c639e8ebSJeenu Viswambharan 	assert(plat_driver_data->interrupt_props_num > 0);
173c639e8ebSJeenu Viswambharan #endif
174464ce2bbSSoby Mathew 
175464ce2bbSSoby Mathew 	/* Ensure that this is a GICv2 system */
176464ce2bbSSoby Mathew 	gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
177464ce2bbSSoby Mathew 	gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT)
178464ce2bbSSoby Mathew 					& PIDR2_ARCH_REV_MASK;
17964deed19SEtienne Carriere 
18064deed19SEtienne Carriere 	/*
18164deed19SEtienne Carriere 	 * GICv1 with security extension complies with trusted firmware
18264deed19SEtienne Carriere 	 * GICv2 driver as far as virtualization and few tricky power
18364deed19SEtienne Carriere 	 * features are not used. GICv2 features that are not supported
18464deed19SEtienne Carriere 	 * by GICv1 with Security Extensions are:
18564deed19SEtienne Carriere 	 * - virtual interrupt support.
18664deed19SEtienne Carriere 	 * - wake up events.
18764deed19SEtienne Carriere 	 * - writeable GIC state register (for power sequences)
18864deed19SEtienne Carriere 	 * - interrupt priority drop.
18964deed19SEtienne Carriere 	 * - interrupt signal bypass.
19064deed19SEtienne Carriere 	 */
19164deed19SEtienne Carriere 	assert(gic_version == ARCH_REV_GICV2 || gic_version == ARCH_REV_GICV1);
192464ce2bbSSoby Mathew 
193464ce2bbSSoby Mathew 	driver_data = plat_driver_data;
194464ce2bbSSoby Mathew 
195311b1773SSoby Mathew 	/*
196311b1773SSoby Mathew 	 * The GIC driver data is initialized by the primary CPU with caches
197311b1773SSoby Mathew 	 * enabled. When the secondary CPU boots up, it initializes the
198311b1773SSoby Mathew 	 * GICC/GICR interface with the caches disabled. Hence flush the
199311b1773SSoby Mathew 	 * driver_data to ensure coherency. This is not required if the
200311b1773SSoby Mathew 	 * platform has HW_ASSISTED_COHERENCY enabled.
201311b1773SSoby Mathew 	 */
202311b1773SSoby Mathew #if !HW_ASSISTED_COHERENCY
203311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data));
204311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data));
205311b1773SSoby Mathew #endif
206464ce2bbSSoby Mathew 	INFO("ARM GICv2 driver initialized\n");
207464ce2bbSSoby Mathew }
208464ce2bbSSoby Mathew 
209464ce2bbSSoby Mathew /******************************************************************************
210464ce2bbSSoby Mathew  * This function returns whether FIQ is enabled in the GIC CPU interface.
211464ce2bbSSoby Mathew  *****************************************************************************/
212464ce2bbSSoby Mathew unsigned int gicv2_is_fiq_enabled(void)
213464ce2bbSSoby Mathew {
214464ce2bbSSoby Mathew 	unsigned int gicc_ctlr;
215464ce2bbSSoby Mathew 
216464ce2bbSSoby Mathew 	assert(driver_data);
217464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
218464ce2bbSSoby Mathew 
219464ce2bbSSoby Mathew 	gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base);
220464ce2bbSSoby Mathew 	return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1;
221464ce2bbSSoby Mathew }
222464ce2bbSSoby Mathew 
223464ce2bbSSoby Mathew /*******************************************************************************
224464ce2bbSSoby Mathew  * This function returns the type of the highest priority pending interrupt at
225464ce2bbSSoby Mathew  * the GIC cpu interface. The return values can be one of the following :
226464ce2bbSSoby Mathew  *   PENDING_G1_INTID   : The interrupt type is non secure Group 1.
227464ce2bbSSoby Mathew  *   0 - 1019           : The interrupt type is secure Group 0.
228464ce2bbSSoby Mathew  *   GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
229464ce2bbSSoby Mathew  *                            sufficient priority to be signaled
230464ce2bbSSoby Mathew  ******************************************************************************/
231464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_type(void)
232464ce2bbSSoby Mathew {
233464ce2bbSSoby Mathew 	assert(driver_data);
234464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
235464ce2bbSSoby Mathew 
236464ce2bbSSoby Mathew 	return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
237464ce2bbSSoby Mathew }
238464ce2bbSSoby Mathew 
239464ce2bbSSoby Mathew /*******************************************************************************
240464ce2bbSSoby Mathew  * This function returns the id of the highest priority pending interrupt at
241464ce2bbSSoby Mathew  * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no
242464ce2bbSSoby Mathew  * interrupt pending.
243464ce2bbSSoby Mathew  ******************************************************************************/
244464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_id(void)
245464ce2bbSSoby Mathew {
246464ce2bbSSoby Mathew 	unsigned int id;
247464ce2bbSSoby Mathew 
248464ce2bbSSoby Mathew 	assert(driver_data);
249464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
250464ce2bbSSoby Mathew 
251464ce2bbSSoby Mathew 	id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
252464ce2bbSSoby Mathew 
253464ce2bbSSoby Mathew 	/*
254464ce2bbSSoby Mathew 	 * Find out which non-secure interrupt it is under the assumption that
255464ce2bbSSoby Mathew 	 * the GICC_CTLR.AckCtl bit is 0.
256464ce2bbSSoby Mathew 	 */
257464ce2bbSSoby Mathew 	if (id == PENDING_G1_INTID)
258464ce2bbSSoby Mathew 		id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK;
259464ce2bbSSoby Mathew 
260464ce2bbSSoby Mathew 	return id;
261464ce2bbSSoby Mathew }
262464ce2bbSSoby Mathew 
263464ce2bbSSoby Mathew /*******************************************************************************
264464ce2bbSSoby Mathew  * This functions reads the GIC cpu interface Interrupt Acknowledge register
265464ce2bbSSoby Mathew  * to start handling the pending secure 0 interrupt. It returns the
266464ce2bbSSoby Mathew  * contents of the IAR.
267464ce2bbSSoby Mathew  ******************************************************************************/
268464ce2bbSSoby Mathew unsigned int gicv2_acknowledge_interrupt(void)
269464ce2bbSSoby Mathew {
270464ce2bbSSoby Mathew 	assert(driver_data);
271464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
272464ce2bbSSoby Mathew 
273464ce2bbSSoby Mathew 	return gicc_read_IAR(driver_data->gicc_base);
274464ce2bbSSoby Mathew }
275464ce2bbSSoby Mathew 
276464ce2bbSSoby Mathew /*******************************************************************************
277464ce2bbSSoby Mathew  * This functions writes the GIC cpu interface End Of Interrupt register with
278464ce2bbSSoby Mathew  * the passed value to finish handling the active secure group 0 interrupt.
279464ce2bbSSoby Mathew  ******************************************************************************/
280464ce2bbSSoby Mathew void gicv2_end_of_interrupt(unsigned int id)
281464ce2bbSSoby Mathew {
282464ce2bbSSoby Mathew 	assert(driver_data);
283464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
284464ce2bbSSoby Mathew 
285464ce2bbSSoby Mathew 	gicc_write_EOIR(driver_data->gicc_base, id);
286464ce2bbSSoby Mathew }
287464ce2bbSSoby Mathew 
288464ce2bbSSoby Mathew /*******************************************************************************
289464ce2bbSSoby Mathew  * This function returns the type of the interrupt id depending upon the group
290464ce2bbSSoby Mathew  * this interrupt has been configured under by the interrupt controller i.e.
291464ce2bbSSoby Mathew  * group0 secure or group1 non secure. It returns zero for Group 0 secure and
292464ce2bbSSoby Mathew  * one for Group 1 non secure interrupt.
293464ce2bbSSoby Mathew  ******************************************************************************/
294464ce2bbSSoby Mathew unsigned int gicv2_get_interrupt_group(unsigned int id)
295464ce2bbSSoby Mathew {
296464ce2bbSSoby Mathew 	assert(driver_data);
297464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
298464ce2bbSSoby Mathew 
299464ce2bbSSoby Mathew 	return gicd_get_igroupr(driver_data->gicd_base, id);
300464ce2bbSSoby Mathew }
301eb68ea9bSJeenu Viswambharan 
302eb68ea9bSJeenu Viswambharan /*******************************************************************************
303eb68ea9bSJeenu Viswambharan  * This function returns the priority of the interrupt the processor is
304eb68ea9bSJeenu Viswambharan  * currently servicing.
305eb68ea9bSJeenu Viswambharan  ******************************************************************************/
306eb68ea9bSJeenu Viswambharan unsigned int gicv2_get_running_priority(void)
307eb68ea9bSJeenu Viswambharan {
308eb68ea9bSJeenu Viswambharan 	assert(driver_data);
309eb68ea9bSJeenu Viswambharan 	assert(driver_data->gicc_base);
310eb68ea9bSJeenu Viswambharan 
311eb68ea9bSJeenu Viswambharan 	return gicc_read_rpr(driver_data->gicc_base);
312eb68ea9bSJeenu Viswambharan }
313fa9db423SJeenu Viswambharan 
314fa9db423SJeenu Viswambharan /*******************************************************************************
315fa9db423SJeenu Viswambharan  * This function sets the GICv2 target mask pattern for the current PE. The PE
316fa9db423SJeenu Viswambharan  * target mask is used to translate linear PE index (returned by platform core
317fa9db423SJeenu Viswambharan  * position) to a bit mask used when targeting interrupts to a PE, viz. when
318fa9db423SJeenu Viswambharan  * raising SGIs and routing SPIs.
319fa9db423SJeenu Viswambharan  ******************************************************************************/
320fa9db423SJeenu Viswambharan void gicv2_set_pe_target_mask(unsigned int proc_num)
321fa9db423SJeenu Viswambharan {
322fa9db423SJeenu Viswambharan 	assert(driver_data);
323fa9db423SJeenu Viswambharan 	assert(driver_data->gicd_base);
324fa9db423SJeenu Viswambharan 	assert(driver_data->target_masks);
325fa9db423SJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
326fa9db423SJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
327fa9db423SJeenu Viswambharan 
328fa9db423SJeenu Viswambharan 	/* Return if the target mask is already populated */
329fa9db423SJeenu Viswambharan 	if (driver_data->target_masks[proc_num])
330fa9db423SJeenu Viswambharan 		return;
331fa9db423SJeenu Viswambharan 
332058efeefSJeenu Viswambharan 	/*
333058efeefSJeenu Viswambharan 	 * Update target register corresponding to this CPU and flush for it to
334058efeefSJeenu Viswambharan 	 * be visible to other CPUs.
335058efeefSJeenu Viswambharan 	 */
336058efeefSJeenu Viswambharan 	if (driver_data->target_masks[proc_num] == 0) {
337fa9db423SJeenu Viswambharan 		driver_data->target_masks[proc_num] =
338fa9db423SJeenu Viswambharan 			gicv2_get_cpuif_id(driver_data->gicd_base);
339058efeefSJeenu Viswambharan #if !HW_ASSISTED_COHERENCY
340058efeefSJeenu Viswambharan 		/*
341058efeefSJeenu Viswambharan 		 * PEs only update their own masks. Primary updates it with
342058efeefSJeenu Viswambharan 		 * caches on. But because secondaries does it with caches off,
343058efeefSJeenu Viswambharan 		 * all updates go to memory directly, and there's no danger of
344058efeefSJeenu Viswambharan 		 * secondaries overwriting each others' mask, despite
345058efeefSJeenu Viswambharan 		 * target_masks[] not being cache line aligned.
346058efeefSJeenu Viswambharan 		 */
347058efeefSJeenu Viswambharan 		flush_dcache_range((uintptr_t)
348058efeefSJeenu Viswambharan 				&driver_data->target_masks[proc_num],
349058efeefSJeenu Viswambharan 				sizeof(driver_data->target_masks[proc_num]));
350058efeefSJeenu Viswambharan #endif
351058efeefSJeenu Viswambharan 	}
352fa9db423SJeenu Viswambharan }
353cbd3f370SJeenu Viswambharan 
354cbd3f370SJeenu Viswambharan /*******************************************************************************
355cbd3f370SJeenu Viswambharan  * This function returns the active status of the interrupt (either because the
356cbd3f370SJeenu Viswambharan  * state is active, or active and pending).
357cbd3f370SJeenu Viswambharan  ******************************************************************************/
358cbd3f370SJeenu Viswambharan unsigned int gicv2_get_interrupt_active(unsigned int id)
359cbd3f370SJeenu Viswambharan {
360cbd3f370SJeenu Viswambharan 	assert(driver_data);
361cbd3f370SJeenu Viswambharan 	assert(driver_data->gicd_base);
362cbd3f370SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
363cbd3f370SJeenu Viswambharan 
364cbd3f370SJeenu Viswambharan 	return gicd_get_isactiver(driver_data->gicd_base, id);
365cbd3f370SJeenu Viswambharan }
366979225f4SJeenu Viswambharan 
367979225f4SJeenu Viswambharan /*******************************************************************************
368979225f4SJeenu Viswambharan  * This function enables the interrupt identified by id.
369979225f4SJeenu Viswambharan  ******************************************************************************/
370979225f4SJeenu Viswambharan void gicv2_enable_interrupt(unsigned int id)
371979225f4SJeenu Viswambharan {
372979225f4SJeenu Viswambharan 	assert(driver_data);
373979225f4SJeenu Viswambharan 	assert(driver_data->gicd_base);
374979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
375979225f4SJeenu Viswambharan 
376979225f4SJeenu Viswambharan 	/*
377979225f4SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
378979225f4SJeenu Viswambharan 	 * interrupt trigger are observed before enabling interrupt.
379979225f4SJeenu Viswambharan 	 */
380979225f4SJeenu Viswambharan 	dsbishst();
381979225f4SJeenu Viswambharan 	gicd_set_isenabler(driver_data->gicd_base, id);
382979225f4SJeenu Viswambharan }
383979225f4SJeenu Viswambharan 
384979225f4SJeenu Viswambharan /*******************************************************************************
385979225f4SJeenu Viswambharan  * This function disables the interrupt identified by id.
386979225f4SJeenu Viswambharan  ******************************************************************************/
387979225f4SJeenu Viswambharan void gicv2_disable_interrupt(unsigned int id)
388979225f4SJeenu Viswambharan {
389979225f4SJeenu Viswambharan 	assert(driver_data);
390979225f4SJeenu Viswambharan 	assert(driver_data->gicd_base);
391979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
392979225f4SJeenu Viswambharan 
393979225f4SJeenu Viswambharan 	/*
394979225f4SJeenu Viswambharan 	 * Disable interrupt, and ensure that any shared variable updates
395979225f4SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
396979225f4SJeenu Viswambharan 	 */
397979225f4SJeenu Viswambharan 	gicd_set_icenabler(driver_data->gicd_base, id);
398979225f4SJeenu Viswambharan 	dsbishst();
399979225f4SJeenu Viswambharan }
400f3a86600SJeenu Viswambharan 
401f3a86600SJeenu Viswambharan /*******************************************************************************
402f3a86600SJeenu Viswambharan  * This function sets the interrupt priority as supplied for the given interrupt
403f3a86600SJeenu Viswambharan  * id.
404f3a86600SJeenu Viswambharan  ******************************************************************************/
405f3a86600SJeenu Viswambharan void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority)
406f3a86600SJeenu Viswambharan {
407f3a86600SJeenu Viswambharan 	assert(driver_data);
408f3a86600SJeenu Viswambharan 	assert(driver_data->gicd_base);
409f3a86600SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
410f3a86600SJeenu Viswambharan 
411f3a86600SJeenu Viswambharan 	gicd_set_ipriorityr(driver_data->gicd_base, id, priority);
412f3a86600SJeenu Viswambharan }
41374dce7faSJeenu Viswambharan 
41474dce7faSJeenu Viswambharan /*******************************************************************************
41574dce7faSJeenu Viswambharan  * This function assigns group for the interrupt identified by id. The group can
41674dce7faSJeenu Viswambharan  * be any of GICV2_INTR_GROUP*
41774dce7faSJeenu Viswambharan  ******************************************************************************/
41874dce7faSJeenu Viswambharan void gicv2_set_interrupt_type(unsigned int id, unsigned int type)
41974dce7faSJeenu Viswambharan {
42074dce7faSJeenu Viswambharan 	assert(driver_data);
42174dce7faSJeenu Viswambharan 	assert(driver_data->gicd_base);
42274dce7faSJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
42374dce7faSJeenu Viswambharan 
42474dce7faSJeenu Viswambharan 	/* Serialize read-modify-write to Distributor registers */
42574dce7faSJeenu Viswambharan 	spin_lock(&gic_lock);
42674dce7faSJeenu Viswambharan 	switch (type) {
42774dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP1:
42874dce7faSJeenu Viswambharan 		gicd_set_igroupr(driver_data->gicd_base, id);
42974dce7faSJeenu Viswambharan 		break;
43074dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP0:
43174dce7faSJeenu Viswambharan 		gicd_clr_igroupr(driver_data->gicd_base, id);
43274dce7faSJeenu Viswambharan 		break;
43374dce7faSJeenu Viswambharan 	default:
43474dce7faSJeenu Viswambharan 		assert(0);
43574dce7faSJeenu Viswambharan 	}
43674dce7faSJeenu Viswambharan 	spin_unlock(&gic_lock);
43774dce7faSJeenu Viswambharan }
4388db978b5SJeenu Viswambharan 
4398db978b5SJeenu Viswambharan /*******************************************************************************
4408db978b5SJeenu Viswambharan  * This function raises the specified SGI to requested targets.
4418db978b5SJeenu Viswambharan  *
4428db978b5SJeenu Viswambharan  * The proc_num parameter must be the linear index of the target PE in the
4438db978b5SJeenu Viswambharan  * system.
4448db978b5SJeenu Viswambharan  ******************************************************************************/
4458db978b5SJeenu Viswambharan void gicv2_raise_sgi(int sgi_num, int proc_num)
4468db978b5SJeenu Viswambharan {
4478db978b5SJeenu Viswambharan 	unsigned int sgir_val, target;
4488db978b5SJeenu Viswambharan 
4498db978b5SJeenu Viswambharan 	assert(driver_data);
4508db978b5SJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
4518db978b5SJeenu Viswambharan 	assert(driver_data->gicd_base);
4528db978b5SJeenu Viswambharan 
4538db978b5SJeenu Viswambharan 	/*
4548db978b5SJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
4558db978b5SJeenu Viswambharan 	 * should be valid.
4568db978b5SJeenu Viswambharan 	 */
4578db978b5SJeenu Viswambharan 	assert(driver_data->target_masks);
4588db978b5SJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
4598db978b5SJeenu Viswambharan 
4608db978b5SJeenu Viswambharan 	/* Don't raise SGI if the mask hasn't been populated */
4618db978b5SJeenu Viswambharan 	target = driver_data->target_masks[proc_num];
4628db978b5SJeenu Viswambharan 	assert(target != 0);
4638db978b5SJeenu Viswambharan 
4648db978b5SJeenu Viswambharan 	sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, sgi_num);
4658db978b5SJeenu Viswambharan 
4668db978b5SJeenu Viswambharan 	/*
4678db978b5SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
4688db978b5SJeenu Viswambharan 	 * interrupt trigger are observed before raising SGI.
4698db978b5SJeenu Viswambharan 	 */
4708db978b5SJeenu Viswambharan 	dsbishst();
4718db978b5SJeenu Viswambharan 	gicd_write_sgir(driver_data->gicd_base, sgir_val);
4728db978b5SJeenu Viswambharan }
473fc529feeSJeenu Viswambharan 
474fc529feeSJeenu Viswambharan /*******************************************************************************
475fc529feeSJeenu Viswambharan  * This function sets the interrupt routing for the given SPI interrupt id.
476fc529feeSJeenu Viswambharan  * The interrupt routing is specified in routing mode. The proc_num parameter is
477fc529feeSJeenu Viswambharan  * linear index of the PE to target SPI. When proc_num < 0, the SPI may target
478fc529feeSJeenu Viswambharan  * all PEs.
479fc529feeSJeenu Viswambharan  ******************************************************************************/
480fc529feeSJeenu Viswambharan void gicv2_set_spi_routing(unsigned int id, int proc_num)
481fc529feeSJeenu Viswambharan {
482fc529feeSJeenu Viswambharan 	int target;
483fc529feeSJeenu Viswambharan 
484fc529feeSJeenu Viswambharan 	assert(driver_data);
485fc529feeSJeenu Viswambharan 	assert(driver_data->gicd_base);
486fc529feeSJeenu Viswambharan 
487fc529feeSJeenu Viswambharan 	assert(id >= MIN_SPI_ID && id <= MAX_SPI_ID);
488fc529feeSJeenu Viswambharan 
489fc529feeSJeenu Viswambharan 	/*
490fc529feeSJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
491fc529feeSJeenu Viswambharan 	 * should be valid.
492fc529feeSJeenu Viswambharan 	 */
493fc529feeSJeenu Viswambharan 	assert(driver_data->target_masks);
494fc529feeSJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
495fc529feeSJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
496fc529feeSJeenu Viswambharan 
497fc529feeSJeenu Viswambharan 	if (proc_num < 0) {
498fc529feeSJeenu Viswambharan 		/* Target all PEs */
499fc529feeSJeenu Viswambharan 		target = GIC_TARGET_CPU_MASK;
500fc529feeSJeenu Viswambharan 	} else {
501fc529feeSJeenu Viswambharan 		/* Don't route interrupt if the mask hasn't been populated */
502fc529feeSJeenu Viswambharan 		target = driver_data->target_masks[proc_num];
503fc529feeSJeenu Viswambharan 		assert(target != 0);
504fc529feeSJeenu Viswambharan 	}
505fc529feeSJeenu Viswambharan 
506fc529feeSJeenu Viswambharan 	gicd_set_itargetsr(driver_data->gicd_base, id, target);
507fc529feeSJeenu Viswambharan }
508a2816a16SJeenu Viswambharan 
509a2816a16SJeenu Viswambharan /*******************************************************************************
510a2816a16SJeenu Viswambharan  * This function clears the pending status of an interrupt identified by id.
511a2816a16SJeenu Viswambharan  ******************************************************************************/
512a2816a16SJeenu Viswambharan void gicv2_clear_interrupt_pending(unsigned int id)
513a2816a16SJeenu Viswambharan {
514a2816a16SJeenu Viswambharan 	assert(driver_data);
515a2816a16SJeenu Viswambharan 	assert(driver_data->gicd_base);
516a2816a16SJeenu Viswambharan 
517a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
518a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
519a2816a16SJeenu Viswambharan 
520a2816a16SJeenu Viswambharan 	/*
521a2816a16SJeenu Viswambharan 	 * Clear pending interrupt, and ensure that any shared variable updates
522a2816a16SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
523a2816a16SJeenu Viswambharan 	 */
524a2816a16SJeenu Viswambharan 	gicd_set_icpendr(driver_data->gicd_base, id);
525a2816a16SJeenu Viswambharan 	dsbishst();
526a2816a16SJeenu Viswambharan }
527a2816a16SJeenu Viswambharan 
528a2816a16SJeenu Viswambharan /*******************************************************************************
529a2816a16SJeenu Viswambharan  * This function sets the pending status of an interrupt identified by id.
530a2816a16SJeenu Viswambharan  ******************************************************************************/
531a2816a16SJeenu Viswambharan void gicv2_set_interrupt_pending(unsigned int id)
532a2816a16SJeenu Viswambharan {
533a2816a16SJeenu Viswambharan 	assert(driver_data);
534a2816a16SJeenu Viswambharan 	assert(driver_data->gicd_base);
535a2816a16SJeenu Viswambharan 
536a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
537a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
538a2816a16SJeenu Viswambharan 
539a2816a16SJeenu Viswambharan 	/*
540a2816a16SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
541a2816a16SJeenu Viswambharan 	 * interrupt trigger are observed before setting interrupt pending.
542a2816a16SJeenu Viswambharan 	 */
543a2816a16SJeenu Viswambharan 	dsbishst();
544a2816a16SJeenu Viswambharan 	gicd_set_ispendr(driver_data->gicd_base, id);
545a2816a16SJeenu Viswambharan }
546d55a4450SJeenu Viswambharan 
547d55a4450SJeenu Viswambharan /*******************************************************************************
548d55a4450SJeenu Viswambharan  * This function sets the PMR register with the supplied value. Returns the
549d55a4450SJeenu Viswambharan  * original PMR.
550d55a4450SJeenu Viswambharan  ******************************************************************************/
551d55a4450SJeenu Viswambharan unsigned int gicv2_set_pmr(unsigned int mask)
552d55a4450SJeenu Viswambharan {
553d55a4450SJeenu Viswambharan 	unsigned int old_mask;
554d55a4450SJeenu Viswambharan 
555d55a4450SJeenu Viswambharan 	assert(driver_data);
556d55a4450SJeenu Viswambharan 	assert(driver_data->gicc_base);
557d55a4450SJeenu Viswambharan 
558d55a4450SJeenu Viswambharan 	old_mask = gicc_read_pmr(driver_data->gicc_base);
559d55a4450SJeenu Viswambharan 
560d55a4450SJeenu Viswambharan 	/*
561d55a4450SJeenu Viswambharan 	 * Order memory updates w.r.t. PMR write, and ensure they're visible
562d55a4450SJeenu Viswambharan 	 * before potential out of band interrupt trigger because of PMR update.
563d55a4450SJeenu Viswambharan 	 */
564d55a4450SJeenu Viswambharan 	dmbishst();
565d55a4450SJeenu Viswambharan 	gicc_write_pmr(driver_data->gicc_base, mask);
566d55a4450SJeenu Viswambharan 	dsbishst();
567d55a4450SJeenu Viswambharan 
568d55a4450SJeenu Viswambharan 	return old_mask;
569d55a4450SJeenu Viswambharan }
570