blob: 4b0984d8a9c020de52ab93fabf476b06f8b463bc [file] [log] [blame]
Soby Mathew464ce2b2015-10-07 09:45:27 +01001/*
Soby Mathew311b1772017-02-14 10:11:52 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Soby Mathew464ce2b2015-10-07 09:45:27 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew464ce2b2015-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 Viswambharanc639e8e2017-09-22 08:32:09 +010013#include <interrupt_props.h>
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010014#include <spinlock.h>
Soby Mathewe9ec3ce2016-02-01 17:59:22 +000015#include "../common/gic_common_private.h"
Soby Mathew464ce2b2015-10-07 09:45:27 +010016#include "gicv2_private.h"
17
18static const gicv2_driver_data_t *driver_data;
19
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +010020/*
21 * Spinlock to guard registers needing read-modify-write. APIs protected by this
22 * spinlock are used either at boot time (when only a single CPU is active), or
23 * when the system is fully coherent.
24 */
25spinlock_t gic_lock;
26
Soby Mathew464ce2b2015-10-07 09:45:27 +010027/*******************************************************************************
28 * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
29 * and set the priority mask register to allow all interrupts to trickle in.
30 ******************************************************************************/
31void gicv2_cpuif_enable(void)
32{
33 unsigned int val;
34
35 assert(driver_data);
36 assert(driver_data->gicc_base);
37
38 /*
39 * Enable the Group 0 interrupts, FIQEn and disable Group 0/1
40 * bypass.
41 */
42 val = CTLR_ENABLE_G0_BIT | FIQ_EN_BIT | FIQ_BYP_DIS_GRP0;
43 val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
44
45 /* Program the idle priority in the PMR */
46 gicc_write_pmr(driver_data->gicc_base, GIC_PRI_MASK);
47 gicc_write_ctlr(driver_data->gicc_base, val);
48}
49
50/*******************************************************************************
51 * Place the cpu interface in a state where it can never make a cpu exit wfi as
52 * as result of an asserted interrupt. This is critical for powering down a cpu
53 ******************************************************************************/
54void gicv2_cpuif_disable(void)
55{
56 unsigned int val;
57
58 assert(driver_data);
59 assert(driver_data->gicc_base);
60
61 /* Disable secure, non-secure interrupts and disable their bypass */
62 val = gicc_read_ctlr(driver_data->gicc_base);
63 val &= ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT);
64 val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
65 val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
66 gicc_write_ctlr(driver_data->gicc_base, val);
67}
68
69/*******************************************************************************
70 * Per cpu gic distributor setup which will be done by all cpus after a cold
71 * boot/hotplug. This marks out the secure SPIs and PPIs & enables them.
72 ******************************************************************************/
73void gicv2_pcpu_distif_init(void)
74{
75 assert(driver_data);
76 assert(driver_data->gicd_base);
Soby Mathew464ce2b2015-10-07 09:45:27 +010077
Jeenu Viswambharanc639e8e2017-09-22 08:32:09 +010078#if !ERROR_DEPRECATED
79 if (driver_data->interrupt_props != NULL) {
80#endif
81 gicv2_secure_ppi_sgi_setup_props(driver_data->gicd_base,
82 driver_data->interrupt_props,
83 driver_data->interrupt_props_num);
84#if !ERROR_DEPRECATED
85 } else {
86 assert(driver_data->g0_interrupt_array);
87 gicv2_secure_ppi_sgi_setup(driver_data->gicd_base,
88 driver_data->g0_interrupt_num,
89 driver_data->g0_interrupt_array);
90 }
91#endif
Soby Mathew464ce2b2015-10-07 09:45:27 +010092}
93
94/*******************************************************************************
95 * Global gic distributor init which will be done by the primary cpu after a
96 * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
97 * then enables the secure GIC distributor interface.
98 ******************************************************************************/
99void gicv2_distif_init(void)
100{
101 unsigned int ctlr;
102
103 assert(driver_data);
104 assert(driver_data->gicd_base);
Soby Mathew464ce2b2015-10-07 09:45:27 +0100105
106 /* Disable the distributor before going further */
107 ctlr = gicd_read_ctlr(driver_data->gicd_base);
108 gicd_write_ctlr(driver_data->gicd_base,
109 ctlr & ~(CTLR_ENABLE_G0_BIT | CTLR_ENABLE_G1_BIT));
110
111 /* Set the default attribute of all SPIs */
112 gicv2_spis_configure_defaults(driver_data->gicd_base);
113
Jeenu Viswambharanc639e8e2017-09-22 08:32:09 +0100114#if !ERROR_DEPRECATED
115 if (driver_data->interrupt_props != NULL) {
116#endif
117 gicv2_secure_spis_configure_props(driver_data->gicd_base,
118 driver_data->interrupt_props,
119 driver_data->interrupt_props_num);
120#if !ERROR_DEPRECATED
121 } else {
122 assert(driver_data->g0_interrupt_array);
123
124 /* Configure the G0 SPIs */
125 gicv2_secure_spis_configure(driver_data->gicd_base,
126 driver_data->g0_interrupt_num,
127 driver_data->g0_interrupt_array);
128 }
129#endif
Soby Mathew464ce2b2015-10-07 09:45:27 +0100130
131 /* Re-enable the secure SPIs now that they have been configured */
132 gicd_write_ctlr(driver_data->gicd_base, ctlr | CTLR_ENABLE_G0_BIT);
133}
134
135/*******************************************************************************
136 * Initialize the ARM GICv2 driver with the provided platform inputs
137 ******************************************************************************/
138void gicv2_driver_init(const gicv2_driver_data_t *plat_driver_data)
139{
140 unsigned int gic_version;
141 assert(plat_driver_data);
142 assert(plat_driver_data->gicd_base);
143 assert(plat_driver_data->gicc_base);
144
Jeenu Viswambharanc639e8e2017-09-22 08:32:09 +0100145#if !ERROR_DEPRECATED
146 if (plat_driver_data->interrupt_props == NULL) {
147 /* Interrupt properties array size must be 0 */
148 assert(plat_driver_data->interrupt_props_num == 0);
Soby Mathew464ce2b2015-10-07 09:45:27 +0100149
Jeenu Viswambharanc639e8e2017-09-22 08:32:09 +0100150 /* The platform should provide a list of secure interrupts */
151 assert(plat_driver_data->g0_interrupt_array);
152
153 /*
154 * If there are no interrupts of a particular type, then the
155 * number of interrupts of that type should be 0 and vice-versa.
156 */
157 assert(plat_driver_data->g0_interrupt_array ?
158 plat_driver_data->g0_interrupt_num :
159 plat_driver_data->g0_interrupt_num == 0);
160 }
161#else
162 assert(plat_driver_data->interrupt_props != NULL);
163 assert(plat_driver_data->interrupt_props_num > 0);
164#endif
Soby Mathew464ce2b2015-10-07 09:45:27 +0100165
166 /* Ensure that this is a GICv2 system */
167 gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
168 gic_version = (gic_version >> PIDR2_ARCH_REV_SHIFT)
169 & PIDR2_ARCH_REV_MASK;
170 assert(gic_version == ARCH_REV_GICV2);
171
172 driver_data = plat_driver_data;
173
Soby Mathew311b1772017-02-14 10:11:52 +0000174 /*
175 * The GIC driver data is initialized by the primary CPU with caches
176 * enabled. When the secondary CPU boots up, it initializes the
177 * GICC/GICR interface with the caches disabled. Hence flush the
178 * driver_data to ensure coherency. This is not required if the
179 * platform has HW_ASSISTED_COHERENCY enabled.
180 */
181#if !HW_ASSISTED_COHERENCY
182 flush_dcache_range((uintptr_t) &driver_data, sizeof(driver_data));
183 flush_dcache_range((uintptr_t) driver_data, sizeof(*driver_data));
184#endif
Soby Mathew464ce2b2015-10-07 09:45:27 +0100185 INFO("ARM GICv2 driver initialized\n");
186}
187
188/******************************************************************************
189 * This function returns whether FIQ is enabled in the GIC CPU interface.
190 *****************************************************************************/
191unsigned int gicv2_is_fiq_enabled(void)
192{
193 unsigned int gicc_ctlr;
194
195 assert(driver_data);
196 assert(driver_data->gicc_base);
197
198 gicc_ctlr = gicc_read_ctlr(driver_data->gicc_base);
199 return (gicc_ctlr >> FIQ_EN_SHIFT) & 0x1;
200}
201
202/*******************************************************************************
203 * This function returns the type of the highest priority pending interrupt at
204 * the GIC cpu interface. The return values can be one of the following :
205 * PENDING_G1_INTID : The interrupt type is non secure Group 1.
206 * 0 - 1019 : The interrupt type is secure Group 0.
207 * GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
208 * sufficient priority to be signaled
209 ******************************************************************************/
210unsigned int gicv2_get_pending_interrupt_type(void)
211{
212 assert(driver_data);
213 assert(driver_data->gicc_base);
214
215 return gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
216}
217
218/*******************************************************************************
219 * This function returns the id of the highest priority pending interrupt at
220 * the GIC cpu interface. GIC_SPURIOUS_INTERRUPT is returned when there is no
221 * interrupt pending.
222 ******************************************************************************/
223unsigned int gicv2_get_pending_interrupt_id(void)
224{
225 unsigned int id;
226
227 assert(driver_data);
228 assert(driver_data->gicc_base);
229
230 id = gicc_read_hppir(driver_data->gicc_base) & INT_ID_MASK;
231
232 /*
233 * Find out which non-secure interrupt it is under the assumption that
234 * the GICC_CTLR.AckCtl bit is 0.
235 */
236 if (id == PENDING_G1_INTID)
237 id = gicc_read_ahppir(driver_data->gicc_base) & INT_ID_MASK;
238
239 return id;
240}
241
242/*******************************************************************************
243 * This functions reads the GIC cpu interface Interrupt Acknowledge register
244 * to start handling the pending secure 0 interrupt. It returns the
245 * contents of the IAR.
246 ******************************************************************************/
247unsigned int gicv2_acknowledge_interrupt(void)
248{
249 assert(driver_data);
250 assert(driver_data->gicc_base);
251
252 return gicc_read_IAR(driver_data->gicc_base);
253}
254
255/*******************************************************************************
256 * This functions writes the GIC cpu interface End Of Interrupt register with
257 * the passed value to finish handling the active secure group 0 interrupt.
258 ******************************************************************************/
259void gicv2_end_of_interrupt(unsigned int id)
260{
261 assert(driver_data);
262 assert(driver_data->gicc_base);
263
264 gicc_write_EOIR(driver_data->gicc_base, id);
265}
266
267/*******************************************************************************
268 * This function returns the type of the interrupt id depending upon the group
269 * this interrupt has been configured under by the interrupt controller i.e.
270 * group0 secure or group1 non secure. It returns zero for Group 0 secure and
271 * one for Group 1 non secure interrupt.
272 ******************************************************************************/
273unsigned int gicv2_get_interrupt_group(unsigned int id)
274{
275 assert(driver_data);
276 assert(driver_data->gicd_base);
277
278 return gicd_get_igroupr(driver_data->gicd_base, id);
279}
Jeenu Viswambharaneb68ea92017-09-22 08:32:09 +0100280
281/*******************************************************************************
282 * This function returns the priority of the interrupt the processor is
283 * currently servicing.
284 ******************************************************************************/
285unsigned int gicv2_get_running_priority(void)
286{
287 assert(driver_data);
288 assert(driver_data->gicc_base);
289
290 return gicc_read_rpr(driver_data->gicc_base);
291}
Jeenu Viswambharanfa9db422017-09-22 08:32:09 +0100292
293/*******************************************************************************
294 * This function sets the GICv2 target mask pattern for the current PE. The PE
295 * target mask is used to translate linear PE index (returned by platform core
296 * position) to a bit mask used when targeting interrupts to a PE, viz. when
297 * raising SGIs and routing SPIs.
298 ******************************************************************************/
299void gicv2_set_pe_target_mask(unsigned int proc_num)
300{
301 assert(driver_data);
302 assert(driver_data->gicd_base);
303 assert(driver_data->target_masks);
304 assert(proc_num < GICV2_MAX_TARGET_PE);
305 assert(proc_num < driver_data->target_masks_num);
306
307 /* Return if the target mask is already populated */
308 if (driver_data->target_masks[proc_num])
309 return;
310
Jeenu Viswambharan058efee2017-11-07 16:10:19 +0000311 /*
312 * Update target register corresponding to this CPU and flush for it to
313 * be visible to other CPUs.
314 */
315 if (driver_data->target_masks[proc_num] == 0) {
316 driver_data->target_masks[proc_num] =
317 gicv2_get_cpuif_id(driver_data->gicd_base);
318#if !HW_ASSISTED_COHERENCY
319 /*
320 * PEs only update their own masks. Primary updates it with
321 * caches on. But because secondaries does it with caches off,
322 * all updates go to memory directly, and there's no danger of
323 * secondaries overwriting each others' mask, despite
324 * target_masks[] not being cache line aligned.
325 */
326 flush_dcache_range((uintptr_t)
327 &driver_data->target_masks[proc_num],
328 sizeof(driver_data->target_masks[proc_num]));
329#endif
330 }
Jeenu Viswambharanfa9db422017-09-22 08:32:09 +0100331}
Jeenu Viswambharancbd3f372017-09-22 08:32:09 +0100332
333/*******************************************************************************
334 * This function returns the active status of the interrupt (either because the
335 * state is active, or active and pending).
336 ******************************************************************************/
337unsigned int gicv2_get_interrupt_active(unsigned int id)
338{
339 assert(driver_data);
340 assert(driver_data->gicd_base);
341 assert(id <= MAX_SPI_ID);
342
343 return gicd_get_isactiver(driver_data->gicd_base, id);
344}
Jeenu Viswambharan979225f2017-09-22 08:32:09 +0100345
346/*******************************************************************************
347 * This function enables the interrupt identified by id.
348 ******************************************************************************/
349void gicv2_enable_interrupt(unsigned int id)
350{
351 assert(driver_data);
352 assert(driver_data->gicd_base);
353 assert(id <= MAX_SPI_ID);
354
355 /*
356 * Ensure that any shared variable updates depending on out of band
357 * interrupt trigger are observed before enabling interrupt.
358 */
359 dsbishst();
360 gicd_set_isenabler(driver_data->gicd_base, id);
361}
362
363/*******************************************************************************
364 * This function disables the interrupt identified by id.
365 ******************************************************************************/
366void gicv2_disable_interrupt(unsigned int id)
367{
368 assert(driver_data);
369 assert(driver_data->gicd_base);
370 assert(id <= MAX_SPI_ID);
371
372 /*
373 * Disable interrupt, and ensure that any shared variable updates
374 * depending on out of band interrupt trigger are observed afterwards.
375 */
376 gicd_set_icenabler(driver_data->gicd_base, id);
377 dsbishst();
378}
Jeenu Viswambharanf3a86602017-09-22 08:32:09 +0100379
380/*******************************************************************************
381 * This function sets the interrupt priority as supplied for the given interrupt
382 * id.
383 ******************************************************************************/
384void gicv2_set_interrupt_priority(unsigned int id, unsigned int priority)
385{
386 assert(driver_data);
387 assert(driver_data->gicd_base);
388 assert(id <= MAX_SPI_ID);
389
390 gicd_set_ipriorityr(driver_data->gicd_base, id, priority);
391}
Jeenu Viswambharan74dce7f2017-09-22 08:32:09 +0100392
393/*******************************************************************************
394 * This function assigns group for the interrupt identified by id. The group can
395 * be any of GICV2_INTR_GROUP*
396 ******************************************************************************/
397void gicv2_set_interrupt_type(unsigned int id, unsigned int type)
398{
399 assert(driver_data);
400 assert(driver_data->gicd_base);
401 assert(id <= MAX_SPI_ID);
402
403 /* Serialize read-modify-write to Distributor registers */
404 spin_lock(&gic_lock);
405 switch (type) {
406 case GICV2_INTR_GROUP1:
407 gicd_set_igroupr(driver_data->gicd_base, id);
408 break;
409 case GICV2_INTR_GROUP0:
410 gicd_clr_igroupr(driver_data->gicd_base, id);
411 break;
412 default:
413 assert(0);
414 }
415 spin_unlock(&gic_lock);
416}
Jeenu Viswambharan8db978b2017-09-22 08:32:09 +0100417
418/*******************************************************************************
419 * This function raises the specified SGI to requested targets.
420 *
421 * The proc_num parameter must be the linear index of the target PE in the
422 * system.
423 ******************************************************************************/
424void gicv2_raise_sgi(int sgi_num, int proc_num)
425{
426 unsigned int sgir_val, target;
427
428 assert(driver_data);
429 assert(proc_num < GICV2_MAX_TARGET_PE);
430 assert(driver_data->gicd_base);
431
432 /*
433 * Target masks array must have been supplied, and the core position
434 * should be valid.
435 */
436 assert(driver_data->target_masks);
437 assert(proc_num < driver_data->target_masks_num);
438
439 /* Don't raise SGI if the mask hasn't been populated */
440 target = driver_data->target_masks[proc_num];
441 assert(target != 0);
442
443 sgir_val = GICV2_SGIR_VALUE(SGIR_TGT_SPECIFIC, target, sgi_num);
444
445 /*
446 * Ensure that any shared variable updates depending on out of band
447 * interrupt trigger are observed before raising SGI.
448 */
449 dsbishst();
450 gicd_write_sgir(driver_data->gicd_base, sgir_val);
451}
Jeenu Viswambharanfc529fe2017-09-22 08:32:09 +0100452
453/*******************************************************************************
454 * This function sets the interrupt routing for the given SPI interrupt id.
455 * The interrupt routing is specified in routing mode. The proc_num parameter is
456 * linear index of the PE to target SPI. When proc_num < 0, the SPI may target
457 * all PEs.
458 ******************************************************************************/
459void gicv2_set_spi_routing(unsigned int id, int proc_num)
460{
461 int target;
462
463 assert(driver_data);
464 assert(driver_data->gicd_base);
465
466 assert(id >= MIN_SPI_ID && id <= MAX_SPI_ID);
467
468 /*
469 * Target masks array must have been supplied, and the core position
470 * should be valid.
471 */
472 assert(driver_data->target_masks);
473 assert(proc_num < GICV2_MAX_TARGET_PE);
474 assert(proc_num < driver_data->target_masks_num);
475
476 if (proc_num < 0) {
477 /* Target all PEs */
478 target = GIC_TARGET_CPU_MASK;
479 } else {
480 /* Don't route interrupt if the mask hasn't been populated */
481 target = driver_data->target_masks[proc_num];
482 assert(target != 0);
483 }
484
485 gicd_set_itargetsr(driver_data->gicd_base, id, target);
486}
Jeenu Viswambharana2816a12017-09-22 08:32:09 +0100487
488/*******************************************************************************
489 * This function clears the pending status of an interrupt identified by id.
490 ******************************************************************************/
491void gicv2_clear_interrupt_pending(unsigned int id)
492{
493 assert(driver_data);
494 assert(driver_data->gicd_base);
495
496 /* SGIs can't be cleared pending */
497 assert(id >= MIN_PPI_ID);
498
499 /*
500 * Clear pending interrupt, and ensure that any shared variable updates
501 * depending on out of band interrupt trigger are observed afterwards.
502 */
503 gicd_set_icpendr(driver_data->gicd_base, id);
504 dsbishst();
505}
506
507/*******************************************************************************
508 * This function sets the pending status of an interrupt identified by id.
509 ******************************************************************************/
510void gicv2_set_interrupt_pending(unsigned int id)
511{
512 assert(driver_data);
513 assert(driver_data->gicd_base);
514
515 /* SGIs can't be cleared pending */
516 assert(id >= MIN_PPI_ID);
517
518 /*
519 * Ensure that any shared variable updates depending on out of band
520 * interrupt trigger are observed before setting interrupt pending.
521 */
522 dsbishst();
523 gicd_set_ispendr(driver_data->gicd_base, id);
524}
Jeenu Viswambharand55a4452017-09-22 08:32:09 +0100525
526/*******************************************************************************
527 * This function sets the PMR register with the supplied value. Returns the
528 * original PMR.
529 ******************************************************************************/
530unsigned int gicv2_set_pmr(unsigned int mask)
531{
532 unsigned int old_mask;
533
534 assert(driver_data);
535 assert(driver_data->gicc_base);
536
537 old_mask = gicc_read_pmr(driver_data->gicc_base);
538
539 /*
540 * Order memory updates w.r.t. PMR write, and ensure they're visible
541 * before potential out of band interrupt trigger because of PMR update.
542 */
543 dmbishst();
544 gicc_write_pmr(driver_data->gicc_base, mask);
545 dsbishst();
546
547 return old_mask;
548}