xref: /rk3399_ARM-atf/drivers/arm/gic/v2/gicv2_main.c (revision c639e8ebeeb152fc32f2feff65c84a37825400b3)
1464ce2bbSSoby Mathew /*
2311b1773SSoby Mathew  * Copyright (c) 2015-2017, 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>
13*c639e8ebSJeenu 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  */
2574dce7faSJeenu Viswambharan 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 {
75464ce2bbSSoby Mathew 	assert(driver_data);
76464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
77464ce2bbSSoby Mathew 
78*c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
79*c639e8ebSJeenu Viswambharan 	if (driver_data->interrupt_props != NULL) {
80*c639e8ebSJeenu Viswambharan #endif
81*c639e8ebSJeenu Viswambharan 		gicv2_secure_ppi_sgi_setup_props(driver_data->gicd_base,
82*c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props,
83*c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props_num);
84*c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
85*c639e8ebSJeenu Viswambharan 	} else {
86*c639e8ebSJeenu Viswambharan 		assert(driver_data->g0_interrupt_array);
87464ce2bbSSoby Mathew 		gicv2_secure_ppi_sgi_setup(driver_data->gicd_base,
88464ce2bbSSoby Mathew 				driver_data->g0_interrupt_num,
89464ce2bbSSoby Mathew 				driver_data->g0_interrupt_array);
90464ce2bbSSoby Mathew 	}
91*c639e8ebSJeenu Viswambharan #endif
92*c639e8ebSJeenu Viswambharan }
93464ce2bbSSoby Mathew 
94464ce2bbSSoby Mathew /*******************************************************************************
95464ce2bbSSoby Mathew  * Global gic distributor init which will be done by the primary cpu after a
96464ce2bbSSoby Mathew  * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
97464ce2bbSSoby Mathew  * then enables the secure GIC distributor interface.
98464ce2bbSSoby Mathew  ******************************************************************************/
99464ce2bbSSoby Mathew void gicv2_distif_init(void)
100464ce2bbSSoby Mathew {
101464ce2bbSSoby Mathew 	unsigned int ctlr;
102464ce2bbSSoby Mathew 
103464ce2bbSSoby Mathew 	assert(driver_data);
104464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
105464ce2bbSSoby Mathew 
106464ce2bbSSoby Mathew 	/* Disable the distributor before going further */
107464ce2bbSSoby Mathew 	ctlr = gicd_read_ctlr(driver_data->gicd_base);
108464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base,
109464ce2bbSSoby Mathew 			ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT));
110464ce2bbSSoby Mathew 
111464ce2bbSSoby Mathew 	/* Set the default attribute of all SPIs */
112464ce2bbSSoby Mathew 	gicv2_spis_configure_defaults(driver_data->gicd_base);
113464ce2bbSSoby Mathew 
114*c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
115*c639e8ebSJeenu Viswambharan 	if (driver_data->interrupt_props != NULL) {
116*c639e8ebSJeenu Viswambharan #endif
117*c639e8ebSJeenu Viswambharan 		gicv2_secure_spis_configure_props(driver_data->gicd_base,
118*c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props,
119*c639e8ebSJeenu Viswambharan 				driver_data->interrupt_props_num);
120*c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
121*c639e8ebSJeenu Viswambharan 	} else {
122*c639e8ebSJeenu Viswambharan 		assert(driver_data->g0_interrupt_array);
123*c639e8ebSJeenu Viswambharan 
124464ce2bbSSoby Mathew 		/* Configure the G0 SPIs */
125464ce2bbSSoby Mathew 		gicv2_secure_spis_configure(driver_data->gicd_base,
126464ce2bbSSoby Mathew 				driver_data->g0_interrupt_num,
127464ce2bbSSoby Mathew 				driver_data->g0_interrupt_array);
128*c639e8ebSJeenu Viswambharan 	}
129*c639e8ebSJeenu Viswambharan #endif
130464ce2bbSSoby Mathew 
131464ce2bbSSoby Mathew 	/* Re-enable the secure SPIs now that they have been configured */
132464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT);
133464ce2bbSSoby Mathew }
134464ce2bbSSoby Mathew 
135464ce2bbSSoby Mathew /*******************************************************************************
136464ce2bbSSoby Mathew  * Initialize the ARM GICv2 driver with the provided platform inputs
137464ce2bbSSoby Mathew  ******************************************************************************/
138464ce2bbSSoby Mathew void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data)
139464ce2bbSSoby Mathew {
140464ce2bbSSoby Mathew 	unsigned int gic_version;
141464ce2bbSSoby Mathew 	assert(plat_driver_data);
142464ce2bbSSoby Mathew 	assert(plat_driver_data->gicd_base);
143464ce2bbSSoby Mathew 	assert(plat_driver_data->gicc_base);
144464ce2bbSSoby Mathew 
145*c639e8ebSJeenu Viswambharan #if !ERROR_DEPRECATED
146*c639e8ebSJeenu Viswambharan 	if (plat_driver_data->interrupt_props == NULL) {
147*c639e8ebSJeenu Viswambharan 		/* Interrupt properties array size must be 0 */
148*c639e8ebSJeenu Viswambharan 		assert(plat_driver_data->interrupt_props_num == 0);
149*c639e8ebSJeenu Viswambharan 
150*c639e8ebSJeenu Viswambharan 		/* The platform should provide a list of secure interrupts */
151464ce2bbSSoby Mathew 		assert(plat_driver_data->g0_interrupt_array);
152464ce2bbSSoby Mathew 
153464ce2bbSSoby Mathew 		/*
154*c639e8ebSJeenu Viswambharan 		 * If there are no interrupts of a particular type, then the
155*c639e8ebSJeenu Viswambharan 		 * number of interrupts of that type should be 0 and vice-versa.
156464ce2bbSSoby Mathew 		 */
157464ce2bbSSoby Mathew 		assert(plat_driver_data->g0_interrupt_array ?
158464ce2bbSSoby Mathew 				plat_driver_data->g0_interrupt_num :
159464ce2bbSSoby Mathew 				plat_driver_data->g0_interrupt_num == 0);
160*c639e8ebSJeenu Viswambharan 	}
161*c639e8ebSJeenu Viswambharan #else
162*c639e8ebSJeenu Viswambharan 	assert(plat_driver_data->interrupt_props != NULL);
163*c639e8ebSJeenu Viswambharan 	assert(plat_driver_data->interrupt_props_num > 0);
164*c639e8ebSJeenu Viswambharan #endif
165464ce2bbSSoby Mathew 
166464ce2bbSSoby Mathew 	/* Ensure that this is a GICv2 system */
167464ce2bbSSoby Mathew 	gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
168464ce2bbSSoby Mathew 	gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT)
169464ce2bbSSoby Mathew 					& PIDR2_ARCH_REV_MASK;
170464ce2bbSSoby Mathew 	assert(gic_version == ARCH_REV_GICV2);
171464ce2bbSSoby Mathew 
172464ce2bbSSoby Mathew 	driver_data = plat_driver_data;
173464ce2bbSSoby Mathew 
174311b1773SSoby Mathew 	/*
175311b1773SSoby Mathew 	 * The GIC driver data is initialized by the primary CPU with caches
176311b1773SSoby Mathew 	 * enabled. When the secondary CPU boots up, it initializes the
177311b1773SSoby Mathew 	 * GICC/GICR interface with the caches disabled. Hence flush the
178311b1773SSoby Mathew 	 * driver_data to ensure coherency. This is not required if the
179311b1773SSoby Mathew 	 * platform has HW_ASSISTED_COHERENCY enabled.
180311b1773SSoby Mathew 	 */
181311b1773SSoby Mathew #if !HW_ASSISTED_COHERENCY
182311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data));
183311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data));
184311b1773SSoby Mathew #endif
185464ce2bbSSoby Mathew 	INFO("ARM GICv2 driver initialized\n");
186464ce2bbSSoby Mathew }
187464ce2bbSSoby Mathew 
188464ce2bbSSoby Mathew /******************************************************************************
189464ce2bbSSoby Mathew  * This function returns whether FIQ is enabled in the GIC CPU interface.
190464ce2bbSSoby Mathew  *****************************************************************************/
191464ce2bbSSoby Mathew unsigned int gicv2_is_fiq_enabled(void)
192464ce2bbSSoby Mathew {
193464ce2bbSSoby Mathew 	unsigned int gicc_ctlr;
194464ce2bbSSoby Mathew 
195464ce2bbSSoby Mathew 	assert(driver_data);
196464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
197464ce2bbSSoby Mathew 
198464ce2bbSSoby Mathew 	gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base);
199464ce2bbSSoby Mathew 	return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1;
200464ce2bbSSoby Mathew }
201464ce2bbSSoby Mathew 
202464ce2bbSSoby Mathew /*******************************************************************************
203464ce2bbSSoby Mathew  * This function returns the type of the highest priority pending interrupt at
204464ce2bbSSoby Mathew  * the GIC cpu interface. The return values can be one of the following :
205464ce2bbSSoby Mathew  *   PENDING_G1_INTID   : The interrupt type is non secure Group 1.
206464ce2bbSSoby Mathew  *   0 - 1019           : The interrupt type is secure Group 0.
207464ce2bbSSoby Mathew  *   GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
208464ce2bbSSoby Mathew  *                            sufficient priority to be signaled
209464ce2bbSSoby Mathew  ******************************************************************************/
210464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_type(void)
211464ce2bbSSoby Mathew {
212464ce2bbSSoby Mathew 	assert(driver_data);
213464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
214464ce2bbSSoby Mathew 
215464ce2bbSSoby Mathew 	return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
216464ce2bbSSoby Mathew }
217464ce2bbSSoby Mathew 
218464ce2bbSSoby Mathew /*******************************************************************************
219464ce2bbSSoby Mathew  * This function returns the id of the highest priority pending interrupt at
220464ce2bbSSoby Mathew  * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no
221464ce2bbSSoby Mathew  * interrupt pending.
222464ce2bbSSoby Mathew  ******************************************************************************/
223464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_id(void)
224464ce2bbSSoby Mathew {
225464ce2bbSSoby Mathew 	unsigned int id;
226464ce2bbSSoby Mathew 
227464ce2bbSSoby Mathew 	assert(driver_data);
228464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
229464ce2bbSSoby Mathew 
230464ce2bbSSoby Mathew 	id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
231464ce2bbSSoby Mathew 
232464ce2bbSSoby Mathew 	/*
233464ce2bbSSoby Mathew 	 * Find out which non-secure interrupt it is under the assumption that
234464ce2bbSSoby Mathew 	 * the GICC_CTLR.AckCtl bit is 0.
235464ce2bbSSoby Mathew 	 */
236464ce2bbSSoby Mathew 	if (id == PENDING_G1_INTID)
237464ce2bbSSoby Mathew 		id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK;
238464ce2bbSSoby Mathew 
239464ce2bbSSoby Mathew 	return id;
240464ce2bbSSoby Mathew }
241464ce2bbSSoby Mathew 
242464ce2bbSSoby Mathew /*******************************************************************************
243464ce2bbSSoby Mathew  * This functions reads the GIC cpu interface Interrupt Acknowledge register
244464ce2bbSSoby Mathew  * to start handling the pending secure 0 interrupt. It returns the
245464ce2bbSSoby Mathew  * contents of the IAR.
246464ce2bbSSoby Mathew  ******************************************************************************/
247464ce2bbSSoby Mathew unsigned int gicv2_acknowledge_interrupt(void)
248464ce2bbSSoby Mathew {
249464ce2bbSSoby Mathew 	assert(driver_data);
250464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
251464ce2bbSSoby Mathew 
252464ce2bbSSoby Mathew 	return gicc_read_IAR(driver_data->gicc_base);
253464ce2bbSSoby Mathew }
254464ce2bbSSoby Mathew 
255464ce2bbSSoby Mathew /*******************************************************************************
256464ce2bbSSoby Mathew  * This functions writes the GIC cpu interface End Of Interrupt register with
257464ce2bbSSoby Mathew  * the passed value to finish handling the active secure group 0 interrupt.
258464ce2bbSSoby Mathew  ******************************************************************************/
259464ce2bbSSoby Mathew void gicv2_end_of_interrupt(unsigned int id)
260464ce2bbSSoby Mathew {
261464ce2bbSSoby Mathew 	assert(driver_data);
262464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
263464ce2bbSSoby Mathew 
264464ce2bbSSoby Mathew 	gicc_write_EOIR(driver_data->gicc_base, id);
265464ce2bbSSoby Mathew }
266464ce2bbSSoby Mathew 
267464ce2bbSSoby Mathew /*******************************************************************************
268464ce2bbSSoby Mathew  * This function returns the type of the interrupt id depending upon the group
269464ce2bbSSoby Mathew  * this interrupt has been configured under by the interrupt controller i.e.
270464ce2bbSSoby Mathew  * group0 secure or group1 non secure. It returns zero for Group 0 secure and
271464ce2bbSSoby Mathew  * one for Group 1 non secure interrupt.
272464ce2bbSSoby Mathew  ******************************************************************************/
273464ce2bbSSoby Mathew unsigned int gicv2_get_interrupt_group(unsigned int id)
274464ce2bbSSoby Mathew {
275464ce2bbSSoby Mathew 	assert(driver_data);
276464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
277464ce2bbSSoby Mathew 
278464ce2bbSSoby Mathew 	return gicd_get_igroupr(driver_data->gicd_base, id);
279464ce2bbSSoby Mathew }
280eb68ea9bSJeenu Viswambharan 
281eb68ea9bSJeenu Viswambharan /*******************************************************************************
282eb68ea9bSJeenu Viswambharan  * This function returns the priority of the interrupt the processor is
283eb68ea9bSJeenu Viswambharan  * currently servicing.
284eb68ea9bSJeenu Viswambharan  ******************************************************************************/
285eb68ea9bSJeenu Viswambharan unsigned int gicv2_get_running_priority(void)
286eb68ea9bSJeenu Viswambharan {
287eb68ea9bSJeenu Viswambharan 	assert(driver_data);
288eb68ea9bSJeenu Viswambharan 	assert(driver_data->gicc_base);
289eb68ea9bSJeenu Viswambharan 
290eb68ea9bSJeenu Viswambharan 	return gicc_read_rpr(driver_data->gicc_base);
291eb68ea9bSJeenu Viswambharan }
292fa9db423SJeenu Viswambharan 
293fa9db423SJeenu Viswambharan /*******************************************************************************
294fa9db423SJeenu Viswambharan  * This function sets the GICv2 target mask pattern for the current PE. The PE
295fa9db423SJeenu Viswambharan  * target mask is used to translate linear PE index (returned by platform core
296fa9db423SJeenu Viswambharan  * position) to a bit mask used when targeting interrupts to a PE, viz. when
297fa9db423SJeenu Viswambharan  * raising SGIs and routing SPIs.
298fa9db423SJeenu Viswambharan  ******************************************************************************/
299fa9db423SJeenu Viswambharan void gicv2_set_pe_target_mask(unsigned int proc_num)
300fa9db423SJeenu Viswambharan {
301fa9db423SJeenu Viswambharan 	assert(driver_data);
302fa9db423SJeenu Viswambharan 	assert(driver_data->gicd_base);
303fa9db423SJeenu Viswambharan 	assert(driver_data->target_masks);
304fa9db423SJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
305fa9db423SJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
306fa9db423SJeenu Viswambharan 
307fa9db423SJeenu Viswambharan 	/* Return if the target mask is already populated */
308fa9db423SJeenu Viswambharan 	if (driver_data->target_masks[proc_num])
309fa9db423SJeenu Viswambharan 		return;
310fa9db423SJeenu Viswambharan 
311fa9db423SJeenu Viswambharan 	/* Read target register corresponding to this CPU */
312fa9db423SJeenu Viswambharan 	driver_data->target_masks[proc_num] =
313fa9db423SJeenu Viswambharan 		gicv2_get_cpuif_id(driver_data->gicd_base);
314fa9db423SJeenu Viswambharan }
315cbd3f370SJeenu Viswambharan 
316cbd3f370SJeenu Viswambharan /*******************************************************************************
317cbd3f370SJeenu Viswambharan  * This function returns the active status of the interrupt (either because the
318cbd3f370SJeenu Viswambharan  * state is active, or active and pending).
319cbd3f370SJeenu Viswambharan  ******************************************************************************/
320cbd3f370SJeenu Viswambharan unsigned int gicv2_get_interrupt_active(unsigned int id)
321cbd3f370SJeenu Viswambharan {
322cbd3f370SJeenu Viswambharan 	assert(driver_data);
323cbd3f370SJeenu Viswambharan 	assert(driver_data->gicd_base);
324cbd3f370SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
325cbd3f370SJeenu Viswambharan 
326cbd3f370SJeenu Viswambharan 	return gicd_get_isactiver(driver_data->gicd_base, id);
327cbd3f370SJeenu Viswambharan }
328979225f4SJeenu Viswambharan 
329979225f4SJeenu Viswambharan /*******************************************************************************
330979225f4SJeenu Viswambharan  * This function enables the interrupt identified by id.
331979225f4SJeenu Viswambharan  ******************************************************************************/
332979225f4SJeenu Viswambharan void gicv2_enable_interrupt(unsigned int id)
333979225f4SJeenu Viswambharan {
334979225f4SJeenu Viswambharan 	assert(driver_data);
335979225f4SJeenu Viswambharan 	assert(driver_data->gicd_base);
336979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
337979225f4SJeenu Viswambharan 
338979225f4SJeenu Viswambharan 	/*
339979225f4SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
340979225f4SJeenu Viswambharan 	 * interrupt trigger are observed before enabling interrupt.
341979225f4SJeenu Viswambharan 	 */
342979225f4SJeenu Viswambharan 	dsbishst();
343979225f4SJeenu Viswambharan 	gicd_set_isenabler(driver_data->gicd_base, id);
344979225f4SJeenu Viswambharan }
345979225f4SJeenu Viswambharan 
346979225f4SJeenu Viswambharan /*******************************************************************************
347979225f4SJeenu Viswambharan  * This function disables the interrupt identified by id.
348979225f4SJeenu Viswambharan  ******************************************************************************/
349979225f4SJeenu Viswambharan void gicv2_disable_interrupt(unsigned int id)
350979225f4SJeenu Viswambharan {
351979225f4SJeenu Viswambharan 	assert(driver_data);
352979225f4SJeenu Viswambharan 	assert(driver_data->gicd_base);
353979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
354979225f4SJeenu Viswambharan 
355979225f4SJeenu Viswambharan 	/*
356979225f4SJeenu Viswambharan 	 * Disable interrupt, and ensure that any shared variable updates
357979225f4SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
358979225f4SJeenu Viswambharan 	 */
359979225f4SJeenu Viswambharan 	gicd_set_icenabler(driver_data->gicd_base, id);
360979225f4SJeenu Viswambharan 	dsbishst();
361979225f4SJeenu Viswambharan }
362f3a86600SJeenu Viswambharan 
363f3a86600SJeenu Viswambharan /*******************************************************************************
364f3a86600SJeenu Viswambharan  * This function sets the interrupt priority as supplied for the given interrupt
365f3a86600SJeenu Viswambharan  * id.
366f3a86600SJeenu Viswambharan  ******************************************************************************/
367f3a86600SJeenu Viswambharan void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority)
368f3a86600SJeenu Viswambharan {
369f3a86600SJeenu Viswambharan 	assert(driver_data);
370f3a86600SJeenu Viswambharan 	assert(driver_data->gicd_base);
371f3a86600SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
372f3a86600SJeenu Viswambharan 
373f3a86600SJeenu Viswambharan 	gicd_set_ipriorityr(driver_data->gicd_base, id, priority);
374f3a86600SJeenu Viswambharan }
37574dce7faSJeenu Viswambharan 
37674dce7faSJeenu Viswambharan /*******************************************************************************
37774dce7faSJeenu Viswambharan  * This function assigns group for the interrupt identified by id. The group can
37874dce7faSJeenu Viswambharan  * be any of GICV2_INTR_GROUP*
37974dce7faSJeenu Viswambharan  ******************************************************************************/
38074dce7faSJeenu Viswambharan void gicv2_set_interrupt_type(unsigned int id, unsigned int type)
38174dce7faSJeenu Viswambharan {
38274dce7faSJeenu Viswambharan 	assert(driver_data);
38374dce7faSJeenu Viswambharan 	assert(driver_data->gicd_base);
38474dce7faSJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
38574dce7faSJeenu Viswambharan 
38674dce7faSJeenu Viswambharan 	/* Serialize read-modify-write to Distributor registers */
38774dce7faSJeenu Viswambharan 	spin_lock(&gic_lock);
38874dce7faSJeenu Viswambharan 	switch (type) {
38974dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP1:
39074dce7faSJeenu Viswambharan 		gicd_set_igroupr(driver_data->gicd_base, id);
39174dce7faSJeenu Viswambharan 		break;
39274dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP0:
39374dce7faSJeenu Viswambharan 		gicd_clr_igroupr(driver_data->gicd_base, id);
39474dce7faSJeenu Viswambharan 		break;
39574dce7faSJeenu Viswambharan 	default:
39674dce7faSJeenu Viswambharan 		assert(0);
39774dce7faSJeenu Viswambharan 	}
39874dce7faSJeenu Viswambharan 	spin_unlock(&gic_lock);
39974dce7faSJeenu Viswambharan }
4008db978b5SJeenu Viswambharan 
4018db978b5SJeenu Viswambharan /*******************************************************************************
4028db978b5SJeenu Viswambharan  * This function raises the specified SGI to requested targets.
4038db978b5SJeenu Viswambharan  *
4048db978b5SJeenu Viswambharan  * The proc_num parameter must be the linear index of the target PE in the
4058db978b5SJeenu Viswambharan  * system.
4068db978b5SJeenu Viswambharan  ******************************************************************************/
4078db978b5SJeenu Viswambharan void gicv2_raise_sgi(int sgi_num, int proc_num)
4088db978b5SJeenu Viswambharan {
4098db978b5SJeenu Viswambharan 	unsigned int sgir_val, target;
4108db978b5SJeenu Viswambharan 
4118db978b5SJeenu Viswambharan 	assert(driver_data);
4128db978b5SJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
4138db978b5SJeenu Viswambharan 	assert(driver_data->gicd_base);
4148db978b5SJeenu Viswambharan 
4158db978b5SJeenu Viswambharan 	/*
4168db978b5SJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
4178db978b5SJeenu Viswambharan 	 * should be valid.
4188db978b5SJeenu Viswambharan 	 */
4198db978b5SJeenu Viswambharan 	assert(driver_data->target_masks);
4208db978b5SJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
4218db978b5SJeenu Viswambharan 
4228db978b5SJeenu Viswambharan 	/* Don't raise SGI if the mask hasn't been populated */
4238db978b5SJeenu Viswambharan 	target = driver_data->target_masks[proc_num];
4248db978b5SJeenu Viswambharan 	assert(target != 0);
4258db978b5SJeenu Viswambharan 
4268db978b5SJeenu Viswambharan 	sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, sgi_num);
4278db978b5SJeenu Viswambharan 
4288db978b5SJeenu Viswambharan 	/*
4298db978b5SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
4308db978b5SJeenu Viswambharan 	 * interrupt trigger are observed before raising SGI.
4318db978b5SJeenu Viswambharan 	 */
4328db978b5SJeenu Viswambharan 	dsbishst();
4338db978b5SJeenu Viswambharan 	gicd_write_sgir(driver_data->gicd_base, sgir_val);
4348db978b5SJeenu Viswambharan }
435fc529feeSJeenu Viswambharan 
436fc529feeSJeenu Viswambharan /*******************************************************************************
437fc529feeSJeenu Viswambharan  * This function sets the interrupt routing for the given SPI interrupt id.
438fc529feeSJeenu Viswambharan  * The interrupt routing is specified in routing mode. The proc_num parameter is
439fc529feeSJeenu Viswambharan  * linear index of the PE to target SPI. When proc_num < 0, the SPI may target
440fc529feeSJeenu Viswambharan  * all PEs.
441fc529feeSJeenu Viswambharan  ******************************************************************************/
442fc529feeSJeenu Viswambharan void gicv2_set_spi_routing(unsigned int id, int proc_num)
443fc529feeSJeenu Viswambharan {
444fc529feeSJeenu Viswambharan 	int target;
445fc529feeSJeenu Viswambharan 
446fc529feeSJeenu Viswambharan 	assert(driver_data);
447fc529feeSJeenu Viswambharan 	assert(driver_data->gicd_base);
448fc529feeSJeenu Viswambharan 
449fc529feeSJeenu Viswambharan 	assert(id >= MIN_SPI_ID && id <= MAX_SPI_ID);
450fc529feeSJeenu Viswambharan 
451fc529feeSJeenu Viswambharan 	/*
452fc529feeSJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
453fc529feeSJeenu Viswambharan 	 * should be valid.
454fc529feeSJeenu Viswambharan 	 */
455fc529feeSJeenu Viswambharan 	assert(driver_data->target_masks);
456fc529feeSJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
457fc529feeSJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
458fc529feeSJeenu Viswambharan 
459fc529feeSJeenu Viswambharan 	if (proc_num < 0) {
460fc529feeSJeenu Viswambharan 		/* Target all PEs */
461fc529feeSJeenu Viswambharan 		target = GIC_TARGET_CPU_MASK;
462fc529feeSJeenu Viswambharan 	} else {
463fc529feeSJeenu Viswambharan 		/* Don't route interrupt if the mask hasn't been populated */
464fc529feeSJeenu Viswambharan 		target = driver_data->target_masks[proc_num];
465fc529feeSJeenu Viswambharan 		assert(target != 0);
466fc529feeSJeenu Viswambharan 	}
467fc529feeSJeenu Viswambharan 
468fc529feeSJeenu Viswambharan 	gicd_set_itargetsr(driver_data->gicd_base, id, target);
469fc529feeSJeenu Viswambharan }
470a2816a16SJeenu Viswambharan 
471a2816a16SJeenu Viswambharan /*******************************************************************************
472a2816a16SJeenu Viswambharan  * This function clears the pending status of an interrupt identified by id.
473a2816a16SJeenu Viswambharan  ******************************************************************************/
474a2816a16SJeenu Viswambharan void gicv2_clear_interrupt_pending(unsigned int id)
475a2816a16SJeenu Viswambharan {
476a2816a16SJeenu Viswambharan 	assert(driver_data);
477a2816a16SJeenu Viswambharan 	assert(driver_data->gicd_base);
478a2816a16SJeenu Viswambharan 
479a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
480a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
481a2816a16SJeenu Viswambharan 
482a2816a16SJeenu Viswambharan 	/*
483a2816a16SJeenu Viswambharan 	 * Clear pending interrupt, and ensure that any shared variable updates
484a2816a16SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
485a2816a16SJeenu Viswambharan 	 */
486a2816a16SJeenu Viswambharan 	gicd_set_icpendr(driver_data->gicd_base, id);
487a2816a16SJeenu Viswambharan 	dsbishst();
488a2816a16SJeenu Viswambharan }
489a2816a16SJeenu Viswambharan 
490a2816a16SJeenu Viswambharan /*******************************************************************************
491a2816a16SJeenu Viswambharan  * This function sets the pending status of an interrupt identified by id.
492a2816a16SJeenu Viswambharan  ******************************************************************************/
493a2816a16SJeenu Viswambharan void gicv2_set_interrupt_pending(unsigned int id)
494a2816a16SJeenu Viswambharan {
495a2816a16SJeenu Viswambharan 	assert(driver_data);
496a2816a16SJeenu Viswambharan 	assert(driver_data->gicd_base);
497a2816a16SJeenu Viswambharan 
498a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
499a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
500a2816a16SJeenu Viswambharan 
501a2816a16SJeenu Viswambharan 	/*
502a2816a16SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
503a2816a16SJeenu Viswambharan 	 * interrupt trigger are observed before setting interrupt pending.
504a2816a16SJeenu Viswambharan 	 */
505a2816a16SJeenu Viswambharan 	dsbishst();
506a2816a16SJeenu Viswambharan 	gicd_set_ispendr(driver_data->gicd_base, id);
507a2816a16SJeenu Viswambharan }
508d55a4450SJeenu Viswambharan 
509d55a4450SJeenu Viswambharan /*******************************************************************************
510d55a4450SJeenu Viswambharan  * This function sets the PMR register with the supplied value. Returns the
511d55a4450SJeenu Viswambharan  * original PMR.
512d55a4450SJeenu Viswambharan  ******************************************************************************/
513d55a4450SJeenu Viswambharan unsigned int gicv2_set_pmr(unsigned int mask)
514d55a4450SJeenu Viswambharan {
515d55a4450SJeenu Viswambharan 	unsigned int old_mask;
516d55a4450SJeenu Viswambharan 
517d55a4450SJeenu Viswambharan 	assert(driver_data);
518d55a4450SJeenu Viswambharan 	assert(driver_data->gicc_base);
519d55a4450SJeenu Viswambharan 
520d55a4450SJeenu Viswambharan 	old_mask = gicc_read_pmr(driver_data->gicc_base);
521d55a4450SJeenu Viswambharan 
522d55a4450SJeenu Viswambharan 	/*
523d55a4450SJeenu Viswambharan 	 * Order memory updates w.r.t. PMR write, and ensure they're visible
524d55a4450SJeenu Viswambharan 	 * before potential out of band interrupt trigger because of PMR update.
525d55a4450SJeenu Viswambharan 	 */
526d55a4450SJeenu Viswambharan 	dmbishst();
527d55a4450SJeenu Viswambharan 	gicc_write_pmr(driver_data->gicc_base, mask);
528d55a4450SJeenu Viswambharan 	dsbishst();
529d55a4450SJeenu Viswambharan 
530d55a4450SJeenu Viswambharan 	return old_mask;
531d55a4450SJeenu Viswambharan }
532