blob: 311e77e98d4251838e123f84d7707f8d51d8ec06 [file] [log] [blame]
Soby Mathewe063d3c2015-10-07 09:45:27 +01001/*
Roberto Vargas05712702018-02-12 12:36:17 +00002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Soby Mathewe063d3c2015-10-07 09:45:27 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathewe063d3c2015-10-07 09:45:27 +01005 */
6
7#include <arch.h>
8#include <arch_helpers.h>
9#include <assert.h>
10#include <debug.h>
11#include <gic_common.h>
12#include <gicv2.h>
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +010013#include <interrupt_props.h>
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +010014#include <spinlock.h>
Antonio Nino Diazca994e72018-08-21 10:02:33 +010015#include <stdbool.h>
16
Soby Mathew50f6fe42016-02-01 17:59:22 +000017#include "../common/gic_common_private.h"
Soby Mathewe063d3c2015-10-07 09:45:27 +010018#include "gicv2_private.h"
19
20static const gicv2_driver_data_t *driver_data;
21
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +010022/*
23 * Spinlock to guard registers needing read-modify-write. APIs protected by this
24 * spinlock are used either at boot time (when only a single CPU is active), or
25 * when the system is fully coherent.
26 */
Roberto Vargas05712702018-02-12 12:36:17 +000027static spinlock_t gic_lock;
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +010028
Soby Mathewe063d3c2015-10-07 09:45:27 +010029/*******************************************************************************
30 * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
31 * and set the priority mask register to allow all interrupts to trickle in.
32 ******************************************************************************/
33void gicv2_cpuif_enable(void)
34{
35 unsigned int val;
36
Antonio Nino Diazca994e72018-08-21 10:02:33 +010037 assert(driver_data != NULL);
38 assert(driver_data->gicc_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +010039
40 /*
41 * Enable the Group 0 interrupts, FIQEn and disable Group 0/1
42 * bypass.
43 */
44 val = CTLR_ENABLE_G0_BIT | FIQ_EN_BIT | FIQ_BYP_DIS_GRP0;
45 val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
46
47 /* Program the idle priority in the PMR */
48 gicc_write_pmr(driver_data->gicc_base, GIC_PRI_MASK);
49 gicc_write_ctlr(driver_data->gicc_base, val);
50}
51
52/*******************************************************************************
53 * Place the cpu interface in a state where it can never make a cpu exit wfi as
54 * as result of an asserted interrupt. This is critical for powering down a cpu
55 ******************************************************************************/
56void gicv2_cpuif_disable(void)
57{
58 unsigned int val;
59
Antonio Nino Diazca994e72018-08-21 10:02:33 +010060 assert(driver_data != NULL);
61 assert(driver_data->gicc_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +010062
63 /* Disable secure, non-secure interrupts and disable their bypass */
64 val = gicc_read_ctlr(driver_data->gicc_base);
65 val &= ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT);
66 val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
67 val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
68 gicc_write_ctlr(driver_data->gicc_base, val);
69}
70
71/*******************************************************************************
72 * Per cpu gic distributor setup which will be done by all cpus after a cold
73 * boot/hotplug. This marks out the secure SPIs and PPIs & enables them.
74 ******************************************************************************/
75void gicv2_pcpu_distif_init(void)
76{
Jeenu Viswambharan88d8f452017-11-07 08:38:23 +000077 unsigned int ctlr;
78
Antonio Nino Diazca994e72018-08-21 10:02:33 +010079 assert(driver_data != NULL);
80 assert(driver_data->gicd_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +010081
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +010082#if !ERROR_DEPRECATED
83 if (driver_data->interrupt_props != NULL) {
84#endif
85 gicv2_secure_ppi_sgi_setup_props(driver_data->gicd_base,
86 driver_data->interrupt_props,
87 driver_data->interrupt_props_num);
88#if !ERROR_DEPRECATED
89 } else {
Dan Handley4d408b52018-03-01 16:00:15 +000090 /*
91 * Suppress deprecated declaration warnings in compatibility
92 * function
93 */
94#pragma GCC diagnostic push
95#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +010096 assert(driver_data->g0_interrupt_array);
97 gicv2_secure_ppi_sgi_setup(driver_data->gicd_base,
98 driver_data->g0_interrupt_num,
99 driver_data->g0_interrupt_array);
Dan Handley4d408b52018-03-01 16:00:15 +0000100#pragma GCC diagnostic pop
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100101 }
102#endif
Jeenu Viswambharan88d8f452017-11-07 08:38:23 +0000103
104 /* Enable G0 interrupts if not already */
105 ctlr = gicd_read_ctlr(driver_data->gicd_base);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100106 if ((ctlr & CTLR_ENABLE_G0_BIT) == 0U) {
Jeenu Viswambharan88d8f452017-11-07 08:38:23 +0000107 gicd_write_ctlr(driver_data->gicd_base,
108 ctlr | CTLR_ENABLE_G0_BIT);
109 }
Soby Mathewe063d3c2015-10-07 09:45:27 +0100110}
111
112/*******************************************************************************
113 * Global gic distributor init which will be done by the primary cpu after a
114 * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
115 * then enables the secure GIC distributor interface.
116 ******************************************************************************/
117void gicv2_distif_init(void)
118{
119 unsigned int ctlr;
120
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100121 assert(driver_data != NULL);
122 assert(driver_data->gicd_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100123
124 /* Disable the distributor before going further */
125 ctlr = gicd_read_ctlr(driver_data->gicd_base);
126 gicd_write_ctlr(driver_data->gicd_base,
127 ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT));
128
129 /* Set the default attribute of all SPIs */
130 gicv2_spis_configure_defaults(driver_data->gicd_base);
131
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100132#if !ERROR_DEPRECATED
133 if (driver_data->interrupt_props != NULL) {
134#endif
135 gicv2_secure_spis_configure_props(driver_data->gicd_base,
136 driver_data->interrupt_props,
137 driver_data->interrupt_props_num);
138#if !ERROR_DEPRECATED
139 } else {
Dan Handley4d408b52018-03-01 16:00:15 +0000140 /*
141 * Suppress deprecated declaration warnings in compatibility
142 * function
143 */
144#pragma GCC diagnostic push
145#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
146
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100147 assert(driver_data->g0_interrupt_array);
148
149 /* Configure the G0 SPIs */
150 gicv2_secure_spis_configure(driver_data->gicd_base,
151 driver_data->g0_interrupt_num,
152 driver_data->g0_interrupt_array);
Dan Handley4d408b52018-03-01 16:00:15 +0000153#pragma GCC diagnostic pop
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100154 }
155#endif
Soby Mathewe063d3c2015-10-07 09:45:27 +0100156
157 /* Re-enable the secure SPIs now that they have been configured */
158 gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT);
159}
160
161/*******************************************************************************
162 * Initialize the ARM GICv2 driver with the provided platform inputs
163 ******************************************************************************/
164void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data)
165{
166 unsigned int gic_version;
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100167
168 assert(plat_driver_data != NULL);
169 assert(plat_driver_data->gicd_base != 0U);
170 assert(plat_driver_data->gicc_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100171
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100172#if !ERROR_DEPRECATED
173 if (plat_driver_data->interrupt_props == NULL) {
174 /* Interrupt properties array size must be 0 */
175 assert(plat_driver_data->interrupt_props_num == 0);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100176
Dan Handley4d408b52018-03-01 16:00:15 +0000177 /*
178 * Suppress deprecated declaration warnings in compatibility
179 * function
180 */
181#pragma GCC diagnostic push
182#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
183
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100184 /*
185 * If there are no interrupts of a particular type, then the
186 * number of interrupts of that type should be 0 and vice-versa.
187 */
188 assert(plat_driver_data->g0_interrupt_array ?
189 plat_driver_data->g0_interrupt_num :
190 plat_driver_data->g0_interrupt_num == 0);
Dan Handley4d408b52018-03-01 16:00:15 +0000191#pragma GCC diagnostic pop
192
193 WARN("Using deprecated integer interrupt array in "
194 "gicv2_driver_data_t\n");
195 WARN("Please migrate to using an interrupt_prop_t array\n");
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100196 }
197#else
Samuel Holland694f81f2017-11-09 12:07:53 -0600198 assert(plat_driver_data->interrupt_props_num > 0 ?
199 plat_driver_data->interrupt_props != NULL : 1);
Jeenu Viswambharanaeb267c2017-09-22 08:32:09 +0100200#endif
Soby Mathewe063d3c2015-10-07 09:45:27 +0100201
202 /* Ensure that this is a GICv2 system */
203 gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
204 gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT)
205 & PIDR2_ARCH_REV_MASK;
Etienne Carriere0a8c3532017-11-05 22:57:38 +0100206
207 /*
208 * GICv1 with security extension complies with trusted firmware
209 * GICv2 driver as far as virtualization and few tricky power
210 * features are not used. GICv2 features that are not supported
211 * by GICv1 with Security Extensions are:
212 * - virtual interrupt support.
213 * - wake up events.
214 * - writeable GIC state register (for power sequences)
215 * - interrupt priority drop.
216 * - interrupt signal bypass.
217 */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100218 assert((gic_version == ARCH_REV_GICV2) ||
219 (gic_version == ARCH_REV_GICV1));
Soby Mathewe063d3c2015-10-07 09:45:27 +0100220
221 driver_data = plat_driver_data;
222
Soby Mathew72645132017-02-14 10:11:52 +0000223 /*
224 * The GIC driver data is initialized by the primary CPU with caches
225 * enabled. When the secondary CPU boots up, it initializes the
226 * GICC/GICR interface with the caches disabled. Hence flush the
227 * driver_data to ensure coherency. This is not required if the
Andrew F. Davis4d23a642018-07-26 13:50:14 -0500228 * platform has HW_ASSISTED_COHERENCY or WARMBOOT_ENABLE_DCACHE_EARLY
229 * enabled.
Soby Mathew72645132017-02-14 10:11:52 +0000230 */
Andrew F. Davis4d23a642018-07-26 13:50:14 -0500231#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
Soby Mathew72645132017-02-14 10:11:52 +0000232 flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data));
233 flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data));
234#endif
Soby Mathewe063d3c2015-10-07 09:45:27 +0100235 INFO("ARM GICv2 driver initialized\n");
236}
237
238/******************************************************************************
239 * This function returns whether FIQ is enabled in the GIC CPU interface.
240 *****************************************************************************/
241unsigned int gicv2_is_fiq_enabled(void)
242{
243 unsigned int gicc_ctlr;
244
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100245 assert(driver_data != NULL);
246 assert(driver_data->gicc_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100247
248 gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base);
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100249 return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1U;
Soby Mathewe063d3c2015-10-07 09:45:27 +0100250}
251
252/*******************************************************************************
253 * This function returns the type of the highest priority pending interrupt at
254 * the GIC cpu interface. The return values can be one of the following :
255 * PENDING_G1_INTID : The interrupt type is non secure Group 1.
256 * 0 - 1019 : The interrupt type is secure Group 0.
257 * GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
258 * sufficient priority to be signaled
259 ******************************************************************************/
260unsigned int gicv2_get_pending_interrupt_type(void)
261{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100262 assert(driver_data != NULL);
263 assert(driver_data->gicc_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100264
265 return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
266}
267
268/*******************************************************************************
269 * This function returns the id of the highest priority pending interrupt at
270 * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no
271 * interrupt pending.
272 ******************************************************************************/
273unsigned int gicv2_get_pending_interrupt_id(void)
274{
275 unsigned int id;
276
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100277 assert(driver_data != NULL);
278 assert(driver_data->gicc_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100279
280 id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
281
282 /*
283 * Find out which non-secure interrupt it is under the assumption that
284 * the GICC_CTLR.AckCtl bit is 0.
285 */
286 if (id == PENDING_G1_INTID)
287 id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK;
288
289 return id;
290}
291
292/*******************************************************************************
293 * This functions reads the GIC cpu interface Interrupt Acknowledge register
294 * to start handling the pending secure 0 interrupt. It returns the
295 * contents of the IAR.
296 ******************************************************************************/
297unsigned int gicv2_acknowledge_interrupt(void)
298{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100299 assert(driver_data != NULL);
300 assert(driver_data->gicc_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100301
302 return gicc_read_IAR(driver_data->gicc_base);
303}
304
305/*******************************************************************************
306 * This functions writes the GIC cpu interface End Of Interrupt register with
307 * the passed value to finish handling the active secure group 0 interrupt.
308 ******************************************************************************/
309void gicv2_end_of_interrupt(unsigned int id)
310{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100311 assert(driver_data != NULL);
312 assert(driver_data->gicc_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100313
314 gicc_write_EOIR(driver_data->gicc_base, id);
315}
316
317/*******************************************************************************
318 * This function returns the type of the interrupt id depending upon the group
319 * this interrupt has been configured under by the interrupt controller i.e.
320 * group0 secure or group1 non secure. It returns zero for Group 0 secure and
321 * one for Group 1 non secure interrupt.
322 ******************************************************************************/
323unsigned int gicv2_get_interrupt_group(unsigned int id)
324{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100325 assert(driver_data != NULL);
326 assert(driver_data->gicd_base != 0U);
Soby Mathewe063d3c2015-10-07 09:45:27 +0100327
328 return gicd_get_igroupr(driver_data->gicd_base, id);
329}
Jeenu Viswambharanb1e957e2017-09-22 08:32:09 +0100330
331/*******************************************************************************
332 * This function returns the priority of the interrupt the processor is
333 * currently servicing.
334 ******************************************************************************/
335unsigned int gicv2_get_running_priority(void)
336{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100337 assert(driver_data != NULL);
338 assert(driver_data->gicc_base != 0U);
Jeenu Viswambharanb1e957e2017-09-22 08:32:09 +0100339
340 return gicc_read_rpr(driver_data->gicc_base);
341}
Jeenu Viswambharan393fdd92017-09-22 08:32:09 +0100342
343/*******************************************************************************
344 * This function sets the GICv2 target mask pattern for the current PE. The PE
345 * target mask is used to translate linear PE index (returned by platform core
346 * position) to a bit mask used when targeting interrupts to a PE, viz. when
347 * raising SGIs and routing SPIs.
348 ******************************************************************************/
349void gicv2_set_pe_target_mask(unsigned int proc_num)
350{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100351 assert(driver_data != NULL);
352 assert(driver_data->gicd_base != 0U);
353 assert(driver_data->target_masks != NULL);
354 assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
355 assert((unsigned int)proc_num < driver_data->target_masks_num);
Jeenu Viswambharan393fdd92017-09-22 08:32:09 +0100356
357 /* Return if the target mask is already populated */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100358 if (driver_data->target_masks[proc_num] != 0U)
Jeenu Viswambharan393fdd92017-09-22 08:32:09 +0100359 return;
360
Jeenu Viswambharanfbf5bda2017-11-07 16:10:19 +0000361 /*
362 * Update target register corresponding to this CPU and flush for it to
363 * be visible to other CPUs.
364 */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100365 if (driver_data->target_masks[proc_num] == 0U) {
Jeenu Viswambharanfbf5bda2017-11-07 16:10:19 +0000366 driver_data->target_masks[proc_num] =
367 gicv2_get_cpuif_id(driver_data->gicd_base);
Andrew F. Davis4d23a642018-07-26 13:50:14 -0500368#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
Jeenu Viswambharanfbf5bda2017-11-07 16:10:19 +0000369 /*
370 * PEs only update their own masks. Primary updates it with
371 * caches on. But because secondaries does it with caches off,
372 * all updates go to memory directly, and there's no danger of
373 * secondaries overwriting each others' mask, despite
374 * target_masks[] not being cache line aligned.
375 */
376 flush_dcache_range((uintptr_t)
377 &driver_data->target_masks[proc_num],
378 sizeof(driver_data->target_masks[proc_num]));
379#endif
380 }
Jeenu Viswambharan393fdd92017-09-22 08:32:09 +0100381}
Jeenu Viswambharan24e70292017-09-22 08:32:09 +0100382
383/*******************************************************************************
384 * This function returns the active status of the interrupt (either because the
385 * state is active, or active and pending).
386 ******************************************************************************/
387unsigned int gicv2_get_interrupt_active(unsigned int id)
388{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100389 assert(driver_data != NULL);
390 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharan24e70292017-09-22 08:32:09 +0100391 assert(id <= MAX_SPI_ID);
392
393 return gicd_get_isactiver(driver_data->gicd_base, id);
394}
Jeenu Viswambharan0fcdfff2017-09-22 08:32:09 +0100395
396/*******************************************************************************
397 * This function enables the interrupt identified by id.
398 ******************************************************************************/
399void gicv2_enable_interrupt(unsigned int id)
400{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100401 assert(driver_data != NULL);
402 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharan0fcdfff2017-09-22 08:32:09 +0100403 assert(id <= MAX_SPI_ID);
404
405 /*
406 * Ensure that any shared variable updates depending on out of band
407 * interrupt trigger are observed before enabling interrupt.
408 */
409 dsbishst();
410 gicd_set_isenabler(driver_data->gicd_base, id);
411}
412
413/*******************************************************************************
414 * This function disables the interrupt identified by id.
415 ******************************************************************************/
416void gicv2_disable_interrupt(unsigned int id)
417{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100418 assert(driver_data != NULL);
419 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharan0fcdfff2017-09-22 08:32:09 +0100420 assert(id <= MAX_SPI_ID);
421
422 /*
423 * Disable interrupt, and ensure that any shared variable updates
424 * depending on out of band interrupt trigger are observed afterwards.
425 */
426 gicd_set_icenabler(driver_data->gicd_base, id);
427 dsbishst();
428}
Jeenu Viswambharan447b89d2017-09-22 08:32:09 +0100429
430/*******************************************************************************
431 * This function sets the interrupt priority as supplied for the given interrupt
432 * id.
433 ******************************************************************************/
434void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority)
435{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100436 assert(driver_data != NULL);
437 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharan447b89d2017-09-22 08:32:09 +0100438 assert(id <= MAX_SPI_ID);
439
440 gicd_set_ipriorityr(driver_data->gicd_base, id, priority);
441}
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +0100442
443/*******************************************************************************
444 * This function assigns group for the interrupt identified by id. The group can
445 * be any of GICV2_INTR_GROUP*
446 ******************************************************************************/
447void gicv2_set_interrupt_type(unsigned int id, unsigned int type)
448{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100449 assert(driver_data != NULL);
450 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +0100451 assert(id <= MAX_SPI_ID);
452
453 /* Serialize read-modify-write to Distributor registers */
454 spin_lock(&gic_lock);
455 switch (type) {
456 case GICV2_INTR_GROUP1:
457 gicd_set_igroupr(driver_data->gicd_base, id);
458 break;
459 case GICV2_INTR_GROUP0:
460 gicd_clr_igroupr(driver_data->gicd_base, id);
461 break;
462 default:
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100463 assert(false);
Jonathan Wright39b42212018-03-13 15:24:29 +0000464 break;
Jeenu Viswambharanc06f05c2017-09-22 08:32:09 +0100465 }
466 spin_unlock(&gic_lock);
467}
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100468
469/*******************************************************************************
470 * This function raises the specified SGI to requested targets.
471 *
472 * The proc_num parameter must be the linear index of the target PE in the
473 * system.
474 ******************************************************************************/
475void gicv2_raise_sgi(int sgi_num, int proc_num)
476{
477 unsigned int sgir_val, target;
478
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100479 assert(driver_data != NULL);
480 assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
481 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100482
483 /*
484 * Target masks array must have been supplied, and the core position
485 * should be valid.
486 */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100487 assert(driver_data->target_masks != NULL);
488 assert((unsigned int)proc_num < driver_data->target_masks_num);
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100489
490 /* Don't raise SGI if the mask hasn't been populated */
491 target = driver_data->target_masks[proc_num];
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100492 assert(target != 0U);
Jeenu Viswambharanab14e9b2017-09-22 08:32:09 +0100493
494 sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, sgi_num);
495
496 /*
497 * Ensure that any shared variable updates depending on out of band
498 * interrupt trigger are observed before raising SGI.
499 */
500 dsbishst();
501 gicd_write_sgir(driver_data->gicd_base, sgir_val);
502}
Jeenu Viswambharandce70b32017-09-22 08:32:09 +0100503
504/*******************************************************************************
505 * This function sets the interrupt routing for the given SPI interrupt id.
506 * The interrupt routing is specified in routing mode. The proc_num parameter is
507 * linear index of the PE to target SPI. When proc_num < 0, the SPI may target
508 * all PEs.
509 ******************************************************************************/
510void gicv2_set_spi_routing(unsigned int id, int proc_num)
511{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100512 unsigned int target;
Jeenu Viswambharandce70b32017-09-22 08:32:09 +0100513
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100514 assert(driver_data != NULL);
515 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharandce70b32017-09-22 08:32:09 +0100516
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100517 assert((id >= MIN_SPI_ID) && (id <= MAX_SPI_ID));
Jeenu Viswambharandce70b32017-09-22 08:32:09 +0100518
519 /*
520 * Target masks array must have been supplied, and the core position
521 * should be valid.
522 */
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100523 assert(driver_data->target_masks != NULL);
524 assert((unsigned int)proc_num < GICV2_MAX_TARGET_PE);
525 assert((unsigned int)proc_num < driver_data->target_masks_num);
Jeenu Viswambharandce70b32017-09-22 08:32:09 +0100526
527 if (proc_num < 0) {
528 /* Target all PEs */
529 target = GIC_TARGET_CPU_MASK;
530 } else {
531 /* Don't route interrupt if the mask hasn't been populated */
532 target = driver_data->target_masks[proc_num];
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100533 assert(target != 0U);
Jeenu Viswambharandce70b32017-09-22 08:32:09 +0100534 }
535
536 gicd_set_itargetsr(driver_data->gicd_base, id, target);
537}
Jeenu Viswambharaneb1c12c2017-09-22 08:32:09 +0100538
539/*******************************************************************************
540 * This function clears the pending status of an interrupt identified by id.
541 ******************************************************************************/
542void gicv2_clear_interrupt_pending(unsigned int id)
543{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100544 assert(driver_data != NULL);
545 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharaneb1c12c2017-09-22 08:32:09 +0100546
547 /* SGIs can't be cleared pending */
548 assert(id >= MIN_PPI_ID);
549
550 /*
551 * Clear pending interrupt, and ensure that any shared variable updates
552 * depending on out of band interrupt trigger are observed afterwards.
553 */
554 gicd_set_icpendr(driver_data->gicd_base, id);
555 dsbishst();
556}
557
558/*******************************************************************************
559 * This function sets the pending status of an interrupt identified by id.
560 ******************************************************************************/
561void gicv2_set_interrupt_pending(unsigned int id)
562{
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100563 assert(driver_data != NULL);
564 assert(driver_data->gicd_base != 0U);
Jeenu Viswambharaneb1c12c2017-09-22 08:32:09 +0100565
566 /* SGIs can't be cleared pending */
567 assert(id >= MIN_PPI_ID);
568
569 /*
570 * Ensure that any shared variable updates depending on out of band
571 * interrupt trigger are observed before setting interrupt pending.
572 */
573 dsbishst();
574 gicd_set_ispendr(driver_data->gicd_base, id);
575}
Jeenu Viswambharan62505072017-09-22 08:32:09 +0100576
577/*******************************************************************************
578 * This function sets the PMR register with the supplied value. Returns the
579 * original PMR.
580 ******************************************************************************/
581unsigned int gicv2_set_pmr(unsigned int mask)
582{
583 unsigned int old_mask;
584
Antonio Nino Diazca994e72018-08-21 10:02:33 +0100585 assert(driver_data != NULL);
586 assert(driver_data->gicc_base != 0U);
Jeenu Viswambharan62505072017-09-22 08:32:09 +0100587
588 old_mask = gicc_read_pmr(driver_data->gicc_base);
589
590 /*
591 * Order memory updates w.r.t. PMR write, and ensure they're visible
592 * before potential out of band interrupt trigger because of PMR update.
593 */
594 dmbishst();
595 gicc_write_pmr(driver_data->gicc_base, mask);
596 dsbishst();
597
598 return old_mask;
599}