xref: /rk3399_ARM-atf/drivers/arm/gic/v2/gicv2_main.c (revision a2816a16440d9eb1223ba505bc30faf6cd31b0ee)
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>
1374dce7faSJeenu Viswambharan #include <spinlock.h>
14e9ec3cecSSoby Mathew #include "../common/gic_common_private.h"
15464ce2bbSSoby Mathew #include "gicv2_private.h"
16464ce2bbSSoby Mathew 
17464ce2bbSSoby Mathew static const gicv2_driver_data_t *driver_data;
18464ce2bbSSoby Mathew 
1974dce7faSJeenu Viswambharan /*
2074dce7faSJeenu Viswambharan  * Spinlock to guard registers needing read-modify-write. APIs protected by this
2174dce7faSJeenu Viswambharan  * spinlock are used either at boot time (when only a single CPU is active), or
2274dce7faSJeenu Viswambharan  * when the system is fully coherent.
2374dce7faSJeenu Viswambharan  */
2474dce7faSJeenu Viswambharan spinlock_t gic_lock;
2574dce7faSJeenu Viswambharan 
26464ce2bbSSoby Mathew /*******************************************************************************
27464ce2bbSSoby Mathew  * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
28464ce2bbSSoby Mathew  * and set the priority mask register to allow all interrupts to trickle in.
29464ce2bbSSoby Mathew  ******************************************************************************/
30464ce2bbSSoby Mathew void gicv2_cpuif_enable(void)
31464ce2bbSSoby Mathew {
32464ce2bbSSoby Mathew 	unsigned int val;
33464ce2bbSSoby Mathew 
34464ce2bbSSoby Mathew 	assert(driver_data);
35464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
36464ce2bbSSoby Mathew 
37464ce2bbSSoby Mathew 	/*
38464ce2bbSSoby Mathew 	 * Enable the Group 0 interrupts, FIQEn and disable Group 0/1
39464ce2bbSSoby Mathew 	 * bypass.
40464ce2bbSSoby Mathew 	 */
41464ce2bbSSoby Mathew 	val = CTLR_ENABLE_G0_BIT | FIQ_EN_BIT | FIQ_BYP_DIS_GRP0;
42464ce2bbSSoby Mathew 	val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
43464ce2bbSSoby Mathew 
44464ce2bbSSoby Mathew 	/* Program the idle priority in the PMR */
45464ce2bbSSoby Mathew 	gicc_write_pmr(driver_data->gicc_base, GIC_PRI_MASK);
46464ce2bbSSoby Mathew 	gicc_write_ctlr(driver_data->gicc_base, val);
47464ce2bbSSoby Mathew }
48464ce2bbSSoby Mathew 
49464ce2bbSSoby Mathew /*******************************************************************************
50464ce2bbSSoby Mathew  * Place the cpu interface in a state where it can never make a cpu exit wfi as
51464ce2bbSSoby Mathew  * as result of an asserted interrupt. This is critical for powering down a cpu
52464ce2bbSSoby Mathew  ******************************************************************************/
53464ce2bbSSoby Mathew void gicv2_cpuif_disable(void)
54464ce2bbSSoby Mathew {
55464ce2bbSSoby Mathew 	unsigned int val;
56464ce2bbSSoby Mathew 
57464ce2bbSSoby Mathew 	assert(driver_data);
58464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
59464ce2bbSSoby Mathew 
60464ce2bbSSoby Mathew 	/* Disable secure, non-secure interrupts and disable their bypass */
61464ce2bbSSoby Mathew 	val = gicc_read_ctlr(driver_data->gicc_base);
62464ce2bbSSoby Mathew 	val &= ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT);
63464ce2bbSSoby Mathew 	val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
64464ce2bbSSoby Mathew 	val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
65464ce2bbSSoby Mathew 	gicc_write_ctlr(driver_data->gicc_base, val);
66464ce2bbSSoby Mathew }
67464ce2bbSSoby Mathew 
68464ce2bbSSoby Mathew /*******************************************************************************
69464ce2bbSSoby Mathew  * Per cpu gic distributor setup which will be done by all cpus after a cold
70464ce2bbSSoby Mathew  * boot/hotplug. This marks out the secure SPIs and PPIs & enables them.
71464ce2bbSSoby Mathew  ******************************************************************************/
72464ce2bbSSoby Mathew void gicv2_pcpu_distif_init(void)
73464ce2bbSSoby Mathew {
74464ce2bbSSoby Mathew 	assert(driver_data);
75464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
76464ce2bbSSoby Mathew 	assert(driver_data->g0_interrupt_array);
77464ce2bbSSoby Mathew 
78464ce2bbSSoby Mathew 	gicv2_secure_ppi_sgi_setup(driver_data->gicd_base,
79464ce2bbSSoby Mathew 					driver_data->g0_interrupt_num,
80464ce2bbSSoby Mathew 					driver_data->g0_interrupt_array);
81464ce2bbSSoby Mathew }
82464ce2bbSSoby Mathew 
83464ce2bbSSoby Mathew /*******************************************************************************
84464ce2bbSSoby Mathew  * Global gic distributor init which will be done by the primary cpu after a
85464ce2bbSSoby Mathew  * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
86464ce2bbSSoby Mathew  * then enables the secure GIC distributor interface.
87464ce2bbSSoby Mathew  ******************************************************************************/
88464ce2bbSSoby Mathew void gicv2_distif_init(void)
89464ce2bbSSoby Mathew {
90464ce2bbSSoby Mathew 	unsigned int ctlr;
91464ce2bbSSoby Mathew 
92464ce2bbSSoby Mathew 	assert(driver_data);
93464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
94464ce2bbSSoby Mathew 	assert(driver_data->g0_interrupt_array);
95464ce2bbSSoby Mathew 
96464ce2bbSSoby Mathew 	/* Disable the distributor before going further */
97464ce2bbSSoby Mathew 	ctlr = gicd_read_ctlr(driver_data->gicd_base);
98464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base,
99464ce2bbSSoby Mathew 			ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT));
100464ce2bbSSoby Mathew 
101464ce2bbSSoby Mathew 	/* Set the default attribute of all SPIs */
102464ce2bbSSoby Mathew 	gicv2_spis_configure_defaults(driver_data->gicd_base);
103464ce2bbSSoby Mathew 
104464ce2bbSSoby Mathew 	/* Configure the G0 SPIs */
105464ce2bbSSoby Mathew 	gicv2_secure_spis_configure(driver_data->gicd_base,
106464ce2bbSSoby Mathew 					driver_data->g0_interrupt_num,
107464ce2bbSSoby Mathew 					driver_data->g0_interrupt_array);
108464ce2bbSSoby Mathew 
109464ce2bbSSoby Mathew 	/* Re-enable the secure SPIs now that they have been configured */
110464ce2bbSSoby Mathew 	gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT);
111464ce2bbSSoby Mathew }
112464ce2bbSSoby Mathew 
113464ce2bbSSoby Mathew /*******************************************************************************
114464ce2bbSSoby Mathew  * Initialize the ARM GICv2 driver with the provided platform inputs
115464ce2bbSSoby Mathew  ******************************************************************************/
116464ce2bbSSoby Mathew void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data)
117464ce2bbSSoby Mathew {
118464ce2bbSSoby Mathew 	unsigned int gic_version;
119464ce2bbSSoby Mathew 	assert(plat_driver_data);
120464ce2bbSSoby Mathew 	assert(plat_driver_data->gicd_base);
121464ce2bbSSoby Mathew 	assert(plat_driver_data->gicc_base);
122464ce2bbSSoby Mathew 
123464ce2bbSSoby Mathew 	/*
124464ce2bbSSoby Mathew 	 * The platform should provide a list of atleast one type of
125464ce2bbSSoby Mathew 	 * interrupts
126464ce2bbSSoby Mathew 	 */
127464ce2bbSSoby Mathew 	assert(plat_driver_data->g0_interrupt_array);
128464ce2bbSSoby Mathew 
129464ce2bbSSoby Mathew 	/*
130464ce2bbSSoby Mathew 	 * If there are no interrupts of a particular type, then the number of
131464ce2bbSSoby Mathew 	 * interrupts of that type should be 0 and vice-versa.
132464ce2bbSSoby Mathew 	 */
133464ce2bbSSoby Mathew 	assert(plat_driver_data->g0_interrupt_array ?
134464ce2bbSSoby Mathew 	       plat_driver_data->g0_interrupt_num :
135464ce2bbSSoby Mathew 	       plat_driver_data->g0_interrupt_num == 0);
136464ce2bbSSoby Mathew 
137464ce2bbSSoby Mathew 	/* Ensure that this is a GICv2 system */
138464ce2bbSSoby Mathew 	gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
139464ce2bbSSoby Mathew 	gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT)
140464ce2bbSSoby Mathew 					& PIDR2_ARCH_REV_MASK;
141464ce2bbSSoby Mathew 	assert(gic_version == ARCH_REV_GICV2);
142464ce2bbSSoby Mathew 
143464ce2bbSSoby Mathew 	driver_data = plat_driver_data;
144464ce2bbSSoby Mathew 
145311b1773SSoby Mathew 	/*
146311b1773SSoby Mathew 	 * The GIC driver data is initialized by the primary CPU with caches
147311b1773SSoby Mathew 	 * enabled. When the secondary CPU boots up, it initializes the
148311b1773SSoby Mathew 	 * GICC/GICR interface with the caches disabled. Hence flush the
149311b1773SSoby Mathew 	 * driver_data to ensure coherency. This is not required if the
150311b1773SSoby Mathew 	 * platform has HW_ASSISTED_COHERENCY enabled.
151311b1773SSoby Mathew 	 */
152311b1773SSoby Mathew #if !HW_ASSISTED_COHERENCY
153311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data));
154311b1773SSoby Mathew 	flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data));
155311b1773SSoby Mathew #endif
156464ce2bbSSoby Mathew 	INFO("ARM GICv2 driver initialized\n");
157464ce2bbSSoby Mathew }
158464ce2bbSSoby Mathew 
159464ce2bbSSoby Mathew /******************************************************************************
160464ce2bbSSoby Mathew  * This function returns whether FIQ is enabled in the GIC CPU interface.
161464ce2bbSSoby Mathew  *****************************************************************************/
162464ce2bbSSoby Mathew unsigned int gicv2_is_fiq_enabled(void)
163464ce2bbSSoby Mathew {
164464ce2bbSSoby Mathew 	unsigned int gicc_ctlr;
165464ce2bbSSoby Mathew 
166464ce2bbSSoby Mathew 	assert(driver_data);
167464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
168464ce2bbSSoby Mathew 
169464ce2bbSSoby Mathew 	gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base);
170464ce2bbSSoby Mathew 	return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1;
171464ce2bbSSoby Mathew }
172464ce2bbSSoby Mathew 
173464ce2bbSSoby Mathew /*******************************************************************************
174464ce2bbSSoby Mathew  * This function returns the type of the highest priority pending interrupt at
175464ce2bbSSoby Mathew  * the GIC cpu interface. The return values can be one of the following :
176464ce2bbSSoby Mathew  *   PENDING_G1_INTID   : The interrupt type is non secure Group 1.
177464ce2bbSSoby Mathew  *   0 - 1019           : The interrupt type is secure Group 0.
178464ce2bbSSoby Mathew  *   GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
179464ce2bbSSoby Mathew  *                            sufficient priority to be signaled
180464ce2bbSSoby Mathew  ******************************************************************************/
181464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_type(void)
182464ce2bbSSoby Mathew {
183464ce2bbSSoby Mathew 	assert(driver_data);
184464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
185464ce2bbSSoby Mathew 
186464ce2bbSSoby Mathew 	return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
187464ce2bbSSoby Mathew }
188464ce2bbSSoby Mathew 
189464ce2bbSSoby Mathew /*******************************************************************************
190464ce2bbSSoby Mathew  * This function returns the id of the highest priority pending interrupt at
191464ce2bbSSoby Mathew  * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no
192464ce2bbSSoby Mathew  * interrupt pending.
193464ce2bbSSoby Mathew  ******************************************************************************/
194464ce2bbSSoby Mathew unsigned int gicv2_get_pending_interrupt_id(void)
195464ce2bbSSoby Mathew {
196464ce2bbSSoby Mathew 	unsigned int id;
197464ce2bbSSoby Mathew 
198464ce2bbSSoby Mathew 	assert(driver_data);
199464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
200464ce2bbSSoby Mathew 
201464ce2bbSSoby Mathew 	id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
202464ce2bbSSoby Mathew 
203464ce2bbSSoby Mathew 	/*
204464ce2bbSSoby Mathew 	 * Find out which non-secure interrupt it is under the assumption that
205464ce2bbSSoby Mathew 	 * the GICC_CTLR.AckCtl bit is 0.
206464ce2bbSSoby Mathew 	 */
207464ce2bbSSoby Mathew 	if (id == PENDING_G1_INTID)
208464ce2bbSSoby Mathew 		id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK;
209464ce2bbSSoby Mathew 
210464ce2bbSSoby Mathew 	return id;
211464ce2bbSSoby Mathew }
212464ce2bbSSoby Mathew 
213464ce2bbSSoby Mathew /*******************************************************************************
214464ce2bbSSoby Mathew  * This functions reads the GIC cpu interface Interrupt Acknowledge register
215464ce2bbSSoby Mathew  * to start handling the pending secure 0 interrupt. It returns the
216464ce2bbSSoby Mathew  * contents of the IAR.
217464ce2bbSSoby Mathew  ******************************************************************************/
218464ce2bbSSoby Mathew unsigned int gicv2_acknowledge_interrupt(void)
219464ce2bbSSoby Mathew {
220464ce2bbSSoby Mathew 	assert(driver_data);
221464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
222464ce2bbSSoby Mathew 
223464ce2bbSSoby Mathew 	return gicc_read_IAR(driver_data->gicc_base);
224464ce2bbSSoby Mathew }
225464ce2bbSSoby Mathew 
226464ce2bbSSoby Mathew /*******************************************************************************
227464ce2bbSSoby Mathew  * This functions writes the GIC cpu interface End Of Interrupt register with
228464ce2bbSSoby Mathew  * the passed value to finish handling the active secure group 0 interrupt.
229464ce2bbSSoby Mathew  ******************************************************************************/
230464ce2bbSSoby Mathew void gicv2_end_of_interrupt(unsigned int id)
231464ce2bbSSoby Mathew {
232464ce2bbSSoby Mathew 	assert(driver_data);
233464ce2bbSSoby Mathew 	assert(driver_data->gicc_base);
234464ce2bbSSoby Mathew 
235464ce2bbSSoby Mathew 	gicc_write_EOIR(driver_data->gicc_base, id);
236464ce2bbSSoby Mathew }
237464ce2bbSSoby Mathew 
238464ce2bbSSoby Mathew /*******************************************************************************
239464ce2bbSSoby Mathew  * This function returns the type of the interrupt id depending upon the group
240464ce2bbSSoby Mathew  * this interrupt has been configured under by the interrupt controller i.e.
241464ce2bbSSoby Mathew  * group0 secure or group1 non secure. It returns zero for Group 0 secure and
242464ce2bbSSoby Mathew  * one for Group 1 non secure interrupt.
243464ce2bbSSoby Mathew  ******************************************************************************/
244464ce2bbSSoby Mathew unsigned int gicv2_get_interrupt_group(unsigned int id)
245464ce2bbSSoby Mathew {
246464ce2bbSSoby Mathew 	assert(driver_data);
247464ce2bbSSoby Mathew 	assert(driver_data->gicd_base);
248464ce2bbSSoby Mathew 
249464ce2bbSSoby Mathew 	return gicd_get_igroupr(driver_data->gicd_base, id);
250464ce2bbSSoby Mathew }
251eb68ea9bSJeenu Viswambharan 
252eb68ea9bSJeenu Viswambharan /*******************************************************************************
253eb68ea9bSJeenu Viswambharan  * This function returns the priority of the interrupt the processor is
254eb68ea9bSJeenu Viswambharan  * currently servicing.
255eb68ea9bSJeenu Viswambharan  ******************************************************************************/
256eb68ea9bSJeenu Viswambharan unsigned int gicv2_get_running_priority(void)
257eb68ea9bSJeenu Viswambharan {
258eb68ea9bSJeenu Viswambharan 	assert(driver_data);
259eb68ea9bSJeenu Viswambharan 	assert(driver_data->gicc_base);
260eb68ea9bSJeenu Viswambharan 
261eb68ea9bSJeenu Viswambharan 	return gicc_read_rpr(driver_data->gicc_base);
262eb68ea9bSJeenu Viswambharan }
263fa9db423SJeenu Viswambharan 
264fa9db423SJeenu Viswambharan /*******************************************************************************
265fa9db423SJeenu Viswambharan  * This function sets the GICv2 target mask pattern for the current PE. The PE
266fa9db423SJeenu Viswambharan  * target mask is used to translate linear PE index (returned by platform core
267fa9db423SJeenu Viswambharan  * position) to a bit mask used when targeting interrupts to a PE, viz. when
268fa9db423SJeenu Viswambharan  * raising SGIs and routing SPIs.
269fa9db423SJeenu Viswambharan  ******************************************************************************/
270fa9db423SJeenu Viswambharan void gicv2_set_pe_target_mask(unsigned int proc_num)
271fa9db423SJeenu Viswambharan {
272fa9db423SJeenu Viswambharan 	assert(driver_data);
273fa9db423SJeenu Viswambharan 	assert(driver_data->gicd_base);
274fa9db423SJeenu Viswambharan 	assert(driver_data->target_masks);
275fa9db423SJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
276fa9db423SJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
277fa9db423SJeenu Viswambharan 
278fa9db423SJeenu Viswambharan 	/* Return if the target mask is already populated */
279fa9db423SJeenu Viswambharan 	if (driver_data->target_masks[proc_num])
280fa9db423SJeenu Viswambharan 		return;
281fa9db423SJeenu Viswambharan 
282fa9db423SJeenu Viswambharan 	/* Read target register corresponding to this CPU */
283fa9db423SJeenu Viswambharan 	driver_data->target_masks[proc_num] =
284fa9db423SJeenu Viswambharan 		gicv2_get_cpuif_id(driver_data->gicd_base);
285fa9db423SJeenu Viswambharan }
286cbd3f370SJeenu Viswambharan 
287cbd3f370SJeenu Viswambharan /*******************************************************************************
288cbd3f370SJeenu Viswambharan  * This function returns the active status of the interrupt (either because the
289cbd3f370SJeenu Viswambharan  * state is active, or active and pending).
290cbd3f370SJeenu Viswambharan  ******************************************************************************/
291cbd3f370SJeenu Viswambharan unsigned int gicv2_get_interrupt_active(unsigned int id)
292cbd3f370SJeenu Viswambharan {
293cbd3f370SJeenu Viswambharan 	assert(driver_data);
294cbd3f370SJeenu Viswambharan 	assert(driver_data->gicd_base);
295cbd3f370SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
296cbd3f370SJeenu Viswambharan 
297cbd3f370SJeenu Viswambharan 	return gicd_get_isactiver(driver_data->gicd_base, id);
298cbd3f370SJeenu Viswambharan }
299979225f4SJeenu Viswambharan 
300979225f4SJeenu Viswambharan /*******************************************************************************
301979225f4SJeenu Viswambharan  * This function enables the interrupt identified by id.
302979225f4SJeenu Viswambharan  ******************************************************************************/
303979225f4SJeenu Viswambharan void gicv2_enable_interrupt(unsigned int id)
304979225f4SJeenu Viswambharan {
305979225f4SJeenu Viswambharan 	assert(driver_data);
306979225f4SJeenu Viswambharan 	assert(driver_data->gicd_base);
307979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
308979225f4SJeenu Viswambharan 
309979225f4SJeenu Viswambharan 	/*
310979225f4SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
311979225f4SJeenu Viswambharan 	 * interrupt trigger are observed before enabling interrupt.
312979225f4SJeenu Viswambharan 	 */
313979225f4SJeenu Viswambharan 	dsbishst();
314979225f4SJeenu Viswambharan 	gicd_set_isenabler(driver_data->gicd_base, id);
315979225f4SJeenu Viswambharan }
316979225f4SJeenu Viswambharan 
317979225f4SJeenu Viswambharan /*******************************************************************************
318979225f4SJeenu Viswambharan  * This function disables the interrupt identified by id.
319979225f4SJeenu Viswambharan  ******************************************************************************/
320979225f4SJeenu Viswambharan void gicv2_disable_interrupt(unsigned int id)
321979225f4SJeenu Viswambharan {
322979225f4SJeenu Viswambharan 	assert(driver_data);
323979225f4SJeenu Viswambharan 	assert(driver_data->gicd_base);
324979225f4SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
325979225f4SJeenu Viswambharan 
326979225f4SJeenu Viswambharan 	/*
327979225f4SJeenu Viswambharan 	 * Disable interrupt, and ensure that any shared variable updates
328979225f4SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
329979225f4SJeenu Viswambharan 	 */
330979225f4SJeenu Viswambharan 	gicd_set_icenabler(driver_data->gicd_base, id);
331979225f4SJeenu Viswambharan 	dsbishst();
332979225f4SJeenu Viswambharan }
333f3a86600SJeenu Viswambharan 
334f3a86600SJeenu Viswambharan /*******************************************************************************
335f3a86600SJeenu Viswambharan  * This function sets the interrupt priority as supplied for the given interrupt
336f3a86600SJeenu Viswambharan  * id.
337f3a86600SJeenu Viswambharan  ******************************************************************************/
338f3a86600SJeenu Viswambharan void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority)
339f3a86600SJeenu Viswambharan {
340f3a86600SJeenu Viswambharan 	assert(driver_data);
341f3a86600SJeenu Viswambharan 	assert(driver_data->gicd_base);
342f3a86600SJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
343f3a86600SJeenu Viswambharan 
344f3a86600SJeenu Viswambharan 	gicd_set_ipriorityr(driver_data->gicd_base, id, priority);
345f3a86600SJeenu Viswambharan }
34674dce7faSJeenu Viswambharan 
34774dce7faSJeenu Viswambharan /*******************************************************************************
34874dce7faSJeenu Viswambharan  * This function assigns group for the interrupt identified by id. The group can
34974dce7faSJeenu Viswambharan  * be any of GICV2_INTR_GROUP*
35074dce7faSJeenu Viswambharan  ******************************************************************************/
35174dce7faSJeenu Viswambharan void gicv2_set_interrupt_type(unsigned int id, unsigned int type)
35274dce7faSJeenu Viswambharan {
35374dce7faSJeenu Viswambharan 	assert(driver_data);
35474dce7faSJeenu Viswambharan 	assert(driver_data->gicd_base);
35574dce7faSJeenu Viswambharan 	assert(id <= MAX_SPI_ID);
35674dce7faSJeenu Viswambharan 
35774dce7faSJeenu Viswambharan 	/* Serialize read-modify-write to Distributor registers */
35874dce7faSJeenu Viswambharan 	spin_lock(&gic_lock);
35974dce7faSJeenu Viswambharan 	switch (type) {
36074dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP1:
36174dce7faSJeenu Viswambharan 		gicd_set_igroupr(driver_data->gicd_base, id);
36274dce7faSJeenu Viswambharan 		break;
36374dce7faSJeenu Viswambharan 	case GICV2_INTR_GROUP0:
36474dce7faSJeenu Viswambharan 		gicd_clr_igroupr(driver_data->gicd_base, id);
36574dce7faSJeenu Viswambharan 		break;
36674dce7faSJeenu Viswambharan 	default:
36774dce7faSJeenu Viswambharan 		assert(0);
36874dce7faSJeenu Viswambharan 	}
36974dce7faSJeenu Viswambharan 	spin_unlock(&gic_lock);
37074dce7faSJeenu Viswambharan }
3718db978b5SJeenu Viswambharan 
3728db978b5SJeenu Viswambharan /*******************************************************************************
3738db978b5SJeenu Viswambharan  * This function raises the specified SGI to requested targets.
3748db978b5SJeenu Viswambharan  *
3758db978b5SJeenu Viswambharan  * The proc_num parameter must be the linear index of the target PE in the
3768db978b5SJeenu Viswambharan  * system.
3778db978b5SJeenu Viswambharan  ******************************************************************************/
3788db978b5SJeenu Viswambharan void gicv2_raise_sgi(int sgi_num, int proc_num)
3798db978b5SJeenu Viswambharan {
3808db978b5SJeenu Viswambharan 	unsigned int sgir_val, target;
3818db978b5SJeenu Viswambharan 
3828db978b5SJeenu Viswambharan 	assert(driver_data);
3838db978b5SJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
3848db978b5SJeenu Viswambharan 	assert(driver_data->gicd_base);
3858db978b5SJeenu Viswambharan 
3868db978b5SJeenu Viswambharan 	/*
3878db978b5SJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
3888db978b5SJeenu Viswambharan 	 * should be valid.
3898db978b5SJeenu Viswambharan 	 */
3908db978b5SJeenu Viswambharan 	assert(driver_data->target_masks);
3918db978b5SJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
3928db978b5SJeenu Viswambharan 
3938db978b5SJeenu Viswambharan 	/* Don't raise SGI if the mask hasn't been populated */
3948db978b5SJeenu Viswambharan 	target = driver_data->target_masks[proc_num];
3958db978b5SJeenu Viswambharan 	assert(target != 0);
3968db978b5SJeenu Viswambharan 
3978db978b5SJeenu Viswambharan 	sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, sgi_num);
3988db978b5SJeenu Viswambharan 
3998db978b5SJeenu Viswambharan 	/*
4008db978b5SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
4018db978b5SJeenu Viswambharan 	 * interrupt trigger are observed before raising SGI.
4028db978b5SJeenu Viswambharan 	 */
4038db978b5SJeenu Viswambharan 	dsbishst();
4048db978b5SJeenu Viswambharan 	gicd_write_sgir(driver_data->gicd_base, sgir_val);
4058db978b5SJeenu Viswambharan }
406fc529feeSJeenu Viswambharan 
407fc529feeSJeenu Viswambharan /*******************************************************************************
408fc529feeSJeenu Viswambharan  * This function sets the interrupt routing for the given SPI interrupt id.
409fc529feeSJeenu Viswambharan  * The interrupt routing is specified in routing mode. The proc_num parameter is
410fc529feeSJeenu Viswambharan  * linear index of the PE to target SPI. When proc_num < 0, the SPI may target
411fc529feeSJeenu Viswambharan  * all PEs.
412fc529feeSJeenu Viswambharan  ******************************************************************************/
413fc529feeSJeenu Viswambharan void gicv2_set_spi_routing(unsigned int id, int proc_num)
414fc529feeSJeenu Viswambharan {
415fc529feeSJeenu Viswambharan 	int target;
416fc529feeSJeenu Viswambharan 
417fc529feeSJeenu Viswambharan 	assert(driver_data);
418fc529feeSJeenu Viswambharan 	assert(driver_data->gicd_base);
419fc529feeSJeenu Viswambharan 
420fc529feeSJeenu Viswambharan 	assert(id >= MIN_SPI_ID && id <= MAX_SPI_ID);
421fc529feeSJeenu Viswambharan 
422fc529feeSJeenu Viswambharan 	/*
423fc529feeSJeenu Viswambharan 	 * Target masks array must have been supplied, and the core position
424fc529feeSJeenu Viswambharan 	 * should be valid.
425fc529feeSJeenu Viswambharan 	 */
426fc529feeSJeenu Viswambharan 	assert(driver_data->target_masks);
427fc529feeSJeenu Viswambharan 	assert(proc_num < GICV2_MAX_TARGET_PE);
428fc529feeSJeenu Viswambharan 	assert(proc_num < driver_data->target_masks_num);
429fc529feeSJeenu Viswambharan 
430fc529feeSJeenu Viswambharan 	if (proc_num < 0) {
431fc529feeSJeenu Viswambharan 		/* Target all PEs */
432fc529feeSJeenu Viswambharan 		target = GIC_TARGET_CPU_MASK;
433fc529feeSJeenu Viswambharan 	} else {
434fc529feeSJeenu Viswambharan 		/* Don't route interrupt if the mask hasn't been populated */
435fc529feeSJeenu Viswambharan 		target = driver_data->target_masks[proc_num];
436fc529feeSJeenu Viswambharan 		assert(target != 0);
437fc529feeSJeenu Viswambharan 	}
438fc529feeSJeenu Viswambharan 
439fc529feeSJeenu Viswambharan 	gicd_set_itargetsr(driver_data->gicd_base, id, target);
440fc529feeSJeenu Viswambharan }
441*a2816a16SJeenu Viswambharan 
442*a2816a16SJeenu Viswambharan /*******************************************************************************
443*a2816a16SJeenu Viswambharan  * This function clears the pending status of an interrupt identified by id.
444*a2816a16SJeenu Viswambharan  ******************************************************************************/
445*a2816a16SJeenu Viswambharan void gicv2_clear_interrupt_pending(unsigned int id)
446*a2816a16SJeenu Viswambharan {
447*a2816a16SJeenu Viswambharan 	assert(driver_data);
448*a2816a16SJeenu Viswambharan 	assert(driver_data->gicd_base);
449*a2816a16SJeenu Viswambharan 
450*a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
451*a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
452*a2816a16SJeenu Viswambharan 
453*a2816a16SJeenu Viswambharan 	/*
454*a2816a16SJeenu Viswambharan 	 * Clear pending interrupt, and ensure that any shared variable updates
455*a2816a16SJeenu Viswambharan 	 * depending on out of band interrupt trigger are observed afterwards.
456*a2816a16SJeenu Viswambharan 	 */
457*a2816a16SJeenu Viswambharan 	gicd_set_icpendr(driver_data->gicd_base, id);
458*a2816a16SJeenu Viswambharan 	dsbishst();
459*a2816a16SJeenu Viswambharan }
460*a2816a16SJeenu Viswambharan 
461*a2816a16SJeenu Viswambharan /*******************************************************************************
462*a2816a16SJeenu Viswambharan  * This function sets the pending status of an interrupt identified by id.
463*a2816a16SJeenu Viswambharan  ******************************************************************************/
464*a2816a16SJeenu Viswambharan void gicv2_set_interrupt_pending(unsigned int id)
465*a2816a16SJeenu Viswambharan {
466*a2816a16SJeenu Viswambharan 	assert(driver_data);
467*a2816a16SJeenu Viswambharan 	assert(driver_data->gicd_base);
468*a2816a16SJeenu Viswambharan 
469*a2816a16SJeenu Viswambharan 	/* SGIs can't be cleared pending */
470*a2816a16SJeenu Viswambharan 	assert(id >= MIN_PPI_ID);
471*a2816a16SJeenu Viswambharan 
472*a2816a16SJeenu Viswambharan 	/*
473*a2816a16SJeenu Viswambharan 	 * Ensure that any shared variable updates depending on out of band
474*a2816a16SJeenu Viswambharan 	 * interrupt trigger are observed before setting interrupt pending.
475*a2816a16SJeenu Viswambharan 	 */
476*a2816a16SJeenu Viswambharan 	dsbishst();
477*a2816a16SJeenu Viswambharan 	gicd_set_ispendr(driver_data->gicd_base, id);
478*a2816a16SJeenu Viswambharan }
479