arm_gic.c 13.7 KB
Newer Older
Ian Spray's avatar
Ian Spray committed
1
/*
2
 * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved.
Ian Spray's avatar
Ian Spray committed
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
Ian Spray's avatar
Ian Spray committed
5
6
 */

7
#include <arch.h>
Ian Spray's avatar
Ian Spray committed
8
#include <arch_helpers.h>
9
#include <arm_gic.h>
10
11
#include <assert.h>
#include <bl_common.h>
12
#include <debug.h>
Dan Handley's avatar
Dan Handley committed
13
14
#include <gic_v2.h>
#include <gic_v3.h>
15
#include <interrupt_mgmt.h>
16
17
#include <platform.h>
#include <stdint.h>
18

Juan Castillo's avatar
Juan Castillo committed
19
20
21
22
23
24
/* Value used to initialize Non-Secure IRQ priorities four at a time */
#define GICD_IPRIORITYR_DEF_VAL \
	(GIC_HIGHEST_NS_PRIORITY | \
	(GIC_HIGHEST_NS_PRIORITY << 8) | \
	(GIC_HIGHEST_NS_PRIORITY << 16) | \
	(GIC_HIGHEST_NS_PRIORITY << 24))
25

26
27
28
static uintptr_t g_gicc_base;
static uintptr_t g_gicd_base;
static uintptr_t g_gicr_base;
29
30
31
static const unsigned int *g_irq_sec_ptr;
static unsigned int g_num_irqs;

Ian Spray's avatar
Ian Spray committed
32
33
34
35
36
37
38

/*******************************************************************************
 * This function does some minimal GICv3 configuration. The Firmware itself does
 * not fully support GICv3 at this time and relies on GICv2 emulation as
 * provided by GICv3. This function allows software (like Linux) in later stages
 * to use full GICv3 features.
 ******************************************************************************/
39
static void gicv3_cpuif_setup(void)
Ian Spray's avatar
Ian Spray committed
40
{
41
	unsigned int val;
42
	uintptr_t base;
Ian Spray's avatar
Ian Spray committed
43
44
45
46
47
48
49
50
51

	/*
	 * When CPUs come out of reset they have their GICR_WAKER.ProcessorSleep
	 * bit set. In order to allow interrupts to get routed to the CPU we
	 * need to clear this bit if set and wait for GICR_WAKER.ChildrenAsleep
	 * to clear (GICv3 Architecture specification 5.4.23).
	 * GICR_WAKER is NOT banked per CPU, compute the correct base address
	 * per CPU.
	 */
52
53
	assert(g_gicr_base);
	base = gicv3_get_rdist(g_gicr_base, read_mpidr());
54
55
56
57
58
59
60
	if (base == (uintptr_t)NULL) {
		/* No re-distributor base address. This interface cannot be
		 * configured.
		 */
		panic();
	}

Ian Spray's avatar
Ian Spray committed
61
62
63
64
65
66
67
68
	val = gicr_read_waker(base);

	val &= ~WAKER_PS;
	gicr_write_waker(base, val);
	dsb();

	/* We need to wait for ChildrenAsleep to clear. */
	val = gicr_read_waker(base);
69
	while (val & WAKER_CA)
Ian Spray's avatar
Ian Spray committed
70
71
72
73
		val = gicr_read_waker(base);

	val = read_icc_sre_el3();
	write_icc_sre_el3(val | ICC_SRE_EN | ICC_SRE_SRE);
74
	isb();
Ian Spray's avatar
Ian Spray committed
75
76
77
78
79
80
}

/*******************************************************************************
 * This function does some minimal GICv3 configuration when cores go
 * down.
 ******************************************************************************/
81
static void gicv3_cpuif_deactivate(void)
Ian Spray's avatar
Ian Spray committed
82
{
83
84
	unsigned int val;
	uintptr_t base;
Ian Spray's avatar
Ian Spray committed
85
86
87
88
89
90
91
92

	/*
	 * When taking CPUs down we need to set GICR_WAKER.ProcessorSleep and
	 * wait for GICR_WAKER.ChildrenAsleep to get set.
	 * (GICv3 Architecture specification 5.4.23).
	 * GICR_WAKER is NOT banked per CPU, compute the correct base address
	 * per CPU.
	 */
93
94
	assert(g_gicr_base);
	base = gicv3_get_rdist(g_gicr_base, read_mpidr());
95
96
97
98
99
100
101
	if (base == (uintptr_t)NULL) {
		/* No re-distributor base address. This interface cannot be
		 * configured.
		 */
		panic();
	}

Ian Spray's avatar
Ian Spray committed
102
103
104
105
106
107
108
	val = gicr_read_waker(base);
	val |= WAKER_PS;
	gicr_write_waker(base, val);
	dsb();

	/* We need to wait for ChildrenAsleep to set. */
	val = gicr_read_waker(base);
109
	while ((val & WAKER_CA) == 0)
Ian Spray's avatar
Ian Spray committed
110
111
112
113
114
115
116
117
		val = gicr_read_waker(base);
}


/*******************************************************************************
 * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
 * and set the priority mask register to allow all interrupts to trickle in.
 ******************************************************************************/
118
void arm_gic_cpuif_setup(void)
Ian Spray's avatar
Ian Spray committed
119
120
121
{
	unsigned int val;

122
123
	assert(g_gicc_base);
	val = gicc_read_iidr(g_gicc_base);
Ian Spray's avatar
Ian Spray committed
124
125
126
127
128
129

	/*
	 * If GICv3 we need to do a bit of additional setup. We want to
	 * allow default GICv2 behaviour but allow the next stage to
	 * enable full gicv3 features.
	 */
130
	if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3)
Ian Spray's avatar
Ian Spray committed
131
132
133
134
135
		gicv3_cpuif_setup();

	val = ENABLE_GRP0 | FIQ_EN | FIQ_BYP_DIS_GRP0;
	val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;

136
137
	gicc_write_pmr(g_gicc_base, GIC_PRI_MASK);
	gicc_write_ctlr(g_gicc_base, val);
Ian Spray's avatar
Ian Spray committed
138
139
140
141
142
143
}

/*******************************************************************************
 * Place the cpu interface in a state where it can never make a cpu exit wfi as
 * as result of an asserted interrupt. This is critical for powering down a cpu
 ******************************************************************************/
144
void arm_gic_cpuif_deactivate(void)
Ian Spray's avatar
Ian Spray committed
145
146
147
148
{
	unsigned int val;

	/* Disable secure, non-secure interrupts and disable their bypass */
149
150
	assert(g_gicc_base);
	val = gicc_read_ctlr(g_gicc_base);
Ian Spray's avatar
Ian Spray committed
151
152
153
	val &= ~(ENABLE_GRP0 | ENABLE_GRP1);
	val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
	val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
154
	gicc_write_ctlr(g_gicc_base, val);
Ian Spray's avatar
Ian Spray committed
155

156
	val = gicc_read_iidr(g_gicc_base);
Ian Spray's avatar
Ian Spray committed
157
158
159
160
161

	/*
	 * If GICv3 we need to do a bit of additional setup. Make sure the
	 * RDIST is put to sleep.
	 */
162
	if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3)
Ian Spray's avatar
Ian Spray committed
163
164
165
166
167
168
169
		gicv3_cpuif_deactivate();
}

/*******************************************************************************
 * Per cpu gic distributor setup which will be done by all cpus after a cold
 * boot/hotplug. This marks out the secure interrupts & enables them.
 ******************************************************************************/
170
void arm_gic_pcpu_distif_setup(void)
Ian Spray's avatar
Ian Spray committed
171
{
172
	unsigned int index, irq_num, sec_ppi_sgi_mask;
173
174

	assert(g_gicd_base);
Juan Castillo's avatar
Juan Castillo committed
175
176
177
178
179
180
181

	/* Setup PPI priorities doing four at a time */
	for (index = 0; index < 32; index += 4) {
		gicd_write_ipriorityr(g_gicd_base, index,
				GICD_IPRIORITYR_DEF_VAL);
	}

182
	assert(g_irq_sec_ptr);
183
	sec_ppi_sgi_mask = 0;
184
185
186
187

	/* Ensure all SGIs and PPIs are Group0 to begin with */
	gicd_write_igroupr(g_gicd_base, 0, 0);

188
189
190
	for (index = 0; index < g_num_irqs; index++) {
		irq_num = g_irq_sec_ptr[index];
		if (irq_num < MIN_SPI_ID) {
191
			/* We have an SGI or a PPI */
192
			sec_ppi_sgi_mask |= 1U << irq_num;
193
194
195
196
197
			gicd_set_ipriorityr(g_gicd_base, irq_num,
				GIC_HIGHEST_SEC_PRIORITY);
			gicd_set_isenabler(g_gicd_base, irq_num);
		}
	}
198
199
200
201
202
203
204
205

	/*
	 * Invert the bitmask to create a mask for non-secure PPIs and
	 * SGIs. Program the GICD_IGROUPR0 with this bit mask. This write will
	 * update the GICR_IGROUPR0 as well in case we are running on a GICv3
	 * system. This is critical if GICD_CTLR.ARE_NS=1.
	 */
	gicd_write_igroupr(g_gicd_base, 0, ~sec_ppi_sgi_mask);
Ian Spray's avatar
Ian Spray committed
206
207
}

Juan Castillo's avatar
Juan Castillo committed
208
209
210
211
212
213
214
215
216
217
218
/*******************************************************************************
 * Get the current CPU bit mask from GICD_ITARGETSR0
 ******************************************************************************/
static unsigned int arm_gic_get_cpuif_id(void)
{
	unsigned int val;

	val = gicd_read_itargetsr(g_gicd_base, 0);
	return val & GIC_TARGET_CPU_MASK;
}

Ian Spray's avatar
Ian Spray committed
219
220
221
222
223
/*******************************************************************************
 * Global gic distributor setup which will be done by the primary cpu after a
 * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
 * then enables the secure GIC distributor interface.
 ******************************************************************************/
224
static void arm_gic_distif_setup(void)
Ian Spray's avatar
Ian Spray committed
225
{
226
	unsigned int num_ints, ctlr, index, irq_num;
Juan Castillo's avatar
Juan Castillo committed
227
	uint8_t target_cpu;
Ian Spray's avatar
Ian Spray committed
228
229

	/* Disable the distributor before going further */
230
231
	assert(g_gicd_base);
	ctlr = gicd_read_ctlr(g_gicd_base);
Ian Spray's avatar
Ian Spray committed
232
	ctlr &= ~(ENABLE_GRP0 | ENABLE_GRP1);
233
	gicd_write_ctlr(g_gicd_base, ctlr);
Ian Spray's avatar
Ian Spray committed
234
235

	/*
Juan Castillo's avatar
Juan Castillo committed
236
237
	 * Mark out non-secure SPI interrupts. The number of interrupts is
	 * calculated as 32 * (IT_LINES + 1). We do 32 at a time.
Ian Spray's avatar
Ian Spray committed
238
	 */
239
	num_ints = gicd_read_typer(g_gicd_base) & IT_LINES_NO_MASK;
Juan Castillo's avatar
Juan Castillo committed
240
241
242
243
244
245
246
247
248
249
250
251
	num_ints = (num_ints + 1) << 5;
	for (index = MIN_SPI_ID; index < num_ints; index += 32)
		gicd_write_igroupr(g_gicd_base, index, ~0);

	/* Setup SPI priorities doing four at a time */
	for (index = MIN_SPI_ID; index < num_ints; index += 4) {
		gicd_write_ipriorityr(g_gicd_base, index,
				GICD_IPRIORITYR_DEF_VAL);
	}

	/* Read the target CPU mask */
	target_cpu = arm_gic_get_cpuif_id();
Ian Spray's avatar
Ian Spray committed
252

Juan Castillo's avatar
Juan Castillo committed
253
	/* Configure SPI secure interrupts now */
254
255
256
257
258
259
260
261
	assert(g_irq_sec_ptr);
	for (index = 0; index < g_num_irqs; index++) {
		irq_num = g_irq_sec_ptr[index];
		if (irq_num >= MIN_SPI_ID) {
			/* We have an SPI */
			gicd_clr_igroupr(g_gicd_base, irq_num);
			gicd_set_ipriorityr(g_gicd_base, irq_num,
				GIC_HIGHEST_SEC_PRIORITY);
Juan Castillo's avatar
Juan Castillo committed
262
			gicd_set_itargetsr(g_gicd_base, irq_num, target_cpu);
263
264
265
			gicd_set_isenabler(g_gicd_base, irq_num);
		}
	}
Juan Castillo's avatar
Juan Castillo committed
266
267
268
269
270
271

	/*
	 * Configure the SGI and PPI. This is done in a separated function
	 * because each CPU is responsible for initializing its own private
	 * interrupts.
	 */
272
273
274
275
276
277
278
279
	arm_gic_pcpu_distif_setup();

	gicd_write_ctlr(g_gicd_base, ctlr | ENABLE_GRP0);
}

/*******************************************************************************
 * Initialize the ARM GIC driver with the provided platform inputs
******************************************************************************/
280
281
282
283
284
void arm_gic_init(uintptr_t gicc_base,
		  uintptr_t gicd_base,
		  uintptr_t gicr_base,
		  const unsigned int *irq_sec_ptr,
		  unsigned int num_irqs)
285
{
Juan Castillo's avatar
Juan Castillo committed
286
287
	unsigned int val;

288
289
290
	assert(gicc_base);
	assert(gicd_base);
	assert(irq_sec_ptr);
Juan Castillo's avatar
Juan Castillo committed
291

292
293
	g_gicc_base = gicc_base;
	g_gicd_base = gicd_base;
Juan Castillo's avatar
Juan Castillo committed
294
295
296
297
298
299
300
301

	val = gicc_read_iidr(g_gicc_base);

	if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
		assert(gicr_base);
		g_gicr_base = gicr_base;
	}

302
303
	g_irq_sec_ptr = irq_sec_ptr;
	g_num_irqs = num_irqs;
Ian Spray's avatar
Ian Spray committed
304
305
}

306
307
308
309
/*******************************************************************************
 * Setup the ARM GIC CPU and distributor interfaces.
******************************************************************************/
void arm_gic_setup(void)
Ian Spray's avatar
Ian Spray committed
310
{
311
312
	arm_gic_cpuif_setup();
	arm_gic_distif_setup();
Ian Spray's avatar
Ian Spray committed
313
}
314
315
316
317

/*******************************************************************************
 * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
 * The interrupt controller knows which pin/line it uses to signal a type of
318
319
320
321
322
323
 * interrupt. This function provides a common implementation of
 * plat_interrupt_type_to_line() in an ARM GIC environment for optional re-use
 * across platforms. It lets the interrupt management framework determine
 * for a type of interrupt and security state, which line should be used in the
 * SCR_EL3 to control its routing to EL3. The interrupt line is represented as
 * the bit position of the IRQ or FIQ bit in the SCR_EL3.
324
 ******************************************************************************/
325
326
uint32_t arm_gic_interrupt_type_to_line(uint32_t type,
				uint32_t security_state)
327
328
329
330
331
{
	assert(type == INTR_TYPE_S_EL1 ||
	       type == INTR_TYPE_EL3 ||
	       type == INTR_TYPE_NS);

332
	assert(sec_state_is_valid(security_state));
333
334
335
336
337
338

	/*
	 * We ignore the security state parameter under the assumption that
	 * both normal and secure worlds are using ARM GICv2. This parameter
	 * will be used when the secure world starts using GICv3.
	 */
339
340
#if ARM_GIC_ARCH == 2
	return gicv2_interrupt_type_to_line(g_gicc_base, type);
341
#else
342
343
#error "Invalid ARM GIC architecture version specified for platform port"
#endif /* ARM_GIC_ARCH */
344
345
}

346
#if ARM_GIC_ARCH == 2
347
348
349
350
351
/*******************************************************************************
 * This function returns the type of the highest priority pending interrupt at
 * the GIC cpu interface. INTR_TYPE_INVAL is returned when there is no
 * interrupt pending.
 ******************************************************************************/
352
uint32_t arm_gic_get_pending_interrupt_type(void)
353
{
354
	uint32_t id;
355

356
	assert(g_gicc_base);
357
	id = gicc_read_hppir(g_gicc_base) & INT_ID_MASK;
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

	/* Assume that all secure interrupts are S-EL1 interrupts */
	if (id < 1022)
		return INTR_TYPE_S_EL1;

	if (id == GIC_SPURIOUS_INTERRUPT)
		return INTR_TYPE_INVAL;

	return INTR_TYPE_NS;
}

/*******************************************************************************
 * This function returns the id of the highest priority pending interrupt at
 * the GIC cpu interface. INTR_ID_UNAVAILABLE is returned when there is no
 * interrupt pending.
 ******************************************************************************/
374
uint32_t arm_gic_get_pending_interrupt_id(void)
375
{
376
	uint32_t id;
377

378
	assert(g_gicc_base);
379
	id = gicc_read_hppir(g_gicc_base) & INT_ID_MASK;
380
381
382
383
384
385
386
387
388
389
390

	if (id < 1022)
		return id;

	if (id == 1023)
		return INTR_ID_UNAVAILABLE;

	/*
	 * Find out which non-secure interrupt it is under the assumption that
	 * the GICC_CTLR.AckCtl bit is 0.
	 */
391
	return gicc_read_ahppir(g_gicc_base) & INT_ID_MASK;
392
393
394
395
396
397
}

/*******************************************************************************
 * This functions reads the GIC cpu interface Interrupt Acknowledge register
 * to start handling the pending interrupt. It returns the contents of the IAR.
 ******************************************************************************/
398
uint32_t arm_gic_acknowledge_interrupt(void)
399
{
400
401
	assert(g_gicc_base);
	return gicc_read_IAR(g_gicc_base);
402
403
404
405
406
407
}

/*******************************************************************************
 * This functions writes the GIC cpu interface End Of Interrupt register with
 * the passed value to finish handling the active interrupt
 ******************************************************************************/
408
void arm_gic_end_of_interrupt(uint32_t id)
409
{
410
411
	assert(g_gicc_base);
	gicc_write_EOIR(g_gicc_base, id);
412
413
414
415
416
417
418
}

/*******************************************************************************
 * This function returns the type of the interrupt id depending upon the group
 * this interrupt has been configured under by the interrupt controller i.e.
 * group0 or group1.
 ******************************************************************************/
419
uint32_t arm_gic_get_interrupt_type(uint32_t id)
420
421
422
{
	uint32_t group;

423
424
	assert(g_gicd_base);
	group = gicd_get_igroupr(g_gicd_base, id);
425
426
427
428
429
430
431
432
433

	/* Assume that all secure interrupts are S-EL1 interrupts */
	if (group == GRP0)
		return INTR_TYPE_S_EL1;
	else
		return INTR_TYPE_NS;
}

#else
434
435
#error "Invalid ARM GIC architecture version specified for platform port"
#endif /* ARM_GIC_ARCH */