gicv3_main.c 39.3 KB
Newer Older
1
/*
Roberto Vargas's avatar
Roberto Vargas committed
2
 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
8
9
10
11
 */

#include <arch.h>
#include <arch_helpers.h>
#include <assert.h>
#include <debug.h>
#include <gicv3.h>
12
#include <interrupt_props.h>
13
#include <spinlock.h>
14
15
#include "gicv3_private.h"

16
const gicv3_driver_data_t *gicv3_driver_data;
17
18
static unsigned int gicv2_compat;

19
20
21
22
23
/*
 * Spinlock to guard registers needing read-modify-write. APIs protected by this
 * spinlock are used either at boot time (when only a single CPU is active), or
 * when the system is fully coherent.
 */
Roberto Vargas's avatar
Roberto Vargas committed
24
static spinlock_t gic_lock;
25

26
27
28
29
30
31
32
/*
 * Redistributor power operations are weakly bound so that they can be
 * overridden
 */
#pragma weak gicv3_rdistif_off
#pragma weak gicv3_rdistif_on

33
34
35
36

/* Helper macros to save and restore GICD registers to and from the context */
#define RESTORE_GICD_REGS(base, ctx, intr_num, reg, REG)		\
	do {								\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
37
38
		for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num); \
				int_id += (1U << REG##_SHIFT)) {	\
39
40
41
			gicd_write_##reg(base, int_id,			\
				ctx->gicd_##reg[(int_id - MIN_SPI_ID) >> REG##_SHIFT]); \
		}							\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
42
	} while (false)
43
44
45

#define SAVE_GICD_REGS(base, ctx, intr_num, reg, REG)			\
	do {								\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
46
47
		for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num); \
				int_id += (1U << REG##_SHIFT)) {	\
48
49
50
			ctx->gicd_##reg[(int_id - MIN_SPI_ID) >> REG##_SHIFT] =\
					gicd_read_##reg(base, int_id);	\
		}							\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
51
	} while (false)
52
53


54
55
56
57
58
59
60
61
/*******************************************************************************
 * This function initialises the ARM GICv3 driver in EL3 with provided platform
 * inputs.
 ******************************************************************************/
void gicv3_driver_init(const gicv3_driver_data_t *plat_driver_data)
{
	unsigned int gic_version;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
62
63
64
65
66
	assert(plat_driver_data != NULL);
	assert(plat_driver_data->gicd_base != 0U);
	assert(plat_driver_data->gicr_base != 0U);
	assert(plat_driver_data->rdistif_num != 0U);
	assert(plat_driver_data->rdistif_base_addrs != NULL);
67
68
69

	assert(IS_IN_EL3());

70
71
72
73
74
#if !ERROR_DEPRECATED
	if (plat_driver_data->interrupt_props == NULL) {
		/* Interrupt properties array size must be 0 */
		assert(plat_driver_data->interrupt_props_num == 0);

75
76
77
78
79
80
81
		/*
		 * Suppress deprecated declaration warnings in compatibility
		 * function
		 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
		/*
		 * The platform should provide a list of at least one type of
		 * interrupt.
		 */
		assert(plat_driver_data->g0_interrupt_array ||
				plat_driver_data->g1s_interrupt_array);

		/*
		 * If there are no interrupts of a particular type, then the
		 * number of interrupts of that type should be 0 and vice-versa.
		 */
		assert(plat_driver_data->g0_interrupt_array ?
				plat_driver_data->g0_interrupt_num :
				plat_driver_data->g0_interrupt_num == 0);
		assert(plat_driver_data->g1s_interrupt_array ?
				plat_driver_data->g1s_interrupt_num :
				plat_driver_data->g1s_interrupt_num == 0);
99
100
101
102
103
#pragma GCC diagnostic pop

		WARN("Using deprecated integer interrupt arrays in "
		     "gicv3_driver_data_t\n");
		WARN("Please migrate to using interrupt_prop_t arrays\n");
104
105
	}
#else
106
107
	assert(plat_driver_data->interrupt_props_num > 0 ?
	       plat_driver_data->interrupt_props != NULL : 1);
108
#endif
109
110

	/* Check for system register support */
111
#ifdef AARCH32
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
112
	assert((read_id_pfr1() & (ID_PFR1_GIC_MASK << ID_PFR1_GIC_SHIFT)) != 0U);
113
#else
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
114
115
	assert((read_id_aa64pfr0_el1() &
			(ID_AA64PFR0_GIC_MASK << ID_AA64PFR0_GIC_SHIFT)) != 0U);
116
#endif /* AARCH32 */
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142

	/* The GIC version should be 3.0 */
	gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
	gic_version >>=	PIDR2_ARCH_REV_SHIFT;
	gic_version &= PIDR2_ARCH_REV_MASK;
	assert(gic_version == ARCH_REV_GICV3);

	/*
	 * Find out whether the GIC supports the GICv2 compatibility mode. The
	 * ARE_S bit resets to 0 if supported
	 */
	gicv2_compat = gicd_read_ctlr(plat_driver_data->gicd_base);
	gicv2_compat >>= CTLR_ARE_S_SHIFT;
	gicv2_compat = !(gicv2_compat & CTLR_ARE_S_MASK);

	/*
	 * Find the base address of each implemented Redistributor interface.
	 * The number of interfaces should be equal to the number of CPUs in the
	 * system. The memory for saving these addresses has to be allocated by
	 * the platform port
	 */
	gicv3_rdistif_base_addrs_probe(plat_driver_data->rdistif_base_addrs,
					   plat_driver_data->rdistif_num,
					   plat_driver_data->gicr_base,
					   plat_driver_data->mpidr_to_core_pos);

143
	gicv3_driver_data = plat_driver_data;
144

145
146
147
148
	/*
	 * The GIC driver data is initialized by the primary CPU with caches
	 * enabled. When the secondary CPU boots up, it initializes the
	 * GICC/GICR interface with the caches disabled. Hence flush the
149
	 * driver data to ensure coherency. This is not required if the
150
151
	 * platform has HW_ASSISTED_COHERENCY or WARMBOOT_ENABLE_DCACHE_EARLY
	 * enabled.
152
	 */
153
#if !(HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
154
155
156
157
	flush_dcache_range((uintptr_t) &gicv3_driver_data,
			sizeof(gicv3_driver_data));
	flush_dcache_range((uintptr_t) gicv3_driver_data,
			sizeof(*gicv3_driver_data));
158
159
#endif

160
161
162
163
164
165
166
167
168
169
170
	INFO("GICv3 %s legacy support detected."
			" ARM GICV3 driver initialized in EL3\n",
			gicv2_compat ? "with" : "without");
}

/*******************************************************************************
 * This function initialises the GIC distributor interface based upon the data
 * provided by the platform while initialising the driver.
 ******************************************************************************/
void gicv3_distif_init(void)
{
171
172
	unsigned int bitmap = 0;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
173
174
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
175
176
177
178
179
180
181
182

	assert(IS_IN_EL3());

	/*
	 * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
	 * the ARE_S bit. The Distributor might generate a system error
	 * otherwise.
	 */
183
	gicd_clr_ctlr(gicv3_driver_data->gicd_base,
184
185
186
187
188
189
		      CTLR_ENABLE_G0_BIT |
		      CTLR_ENABLE_G1S_BIT |
		      CTLR_ENABLE_G1NS_BIT,
		      RWP_TRUE);

	/* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
190
	gicd_set_ctlr(gicv3_driver_data->gicd_base,
191
192
193
			CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);

	/* Set the default attribute of all SPIs */
Daniel Boulby's avatar
Daniel Boulby committed
194
	gicv3_spis_config_defaults(gicv3_driver_data->gicd_base);
195

196
197
198
#if !ERROR_DEPRECATED
	if (gicv3_driver_data->interrupt_props != NULL) {
#endif
Daniel Boulby's avatar
Daniel Boulby committed
199
		bitmap = gicv3_secure_spis_config_props(
200
201
202
203
204
				gicv3_driver_data->gicd_base,
				gicv3_driver_data->interrupt_props,
				gicv3_driver_data->interrupt_props_num);
#if !ERROR_DEPRECATED
	} else {
205
206
207
208
209
210
211
		/*
		 * Suppress deprecated declaration warnings in compatibility
		 * function
		 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

212
213
214
215
216
		assert(gicv3_driver_data->g1s_interrupt_array ||
				gicv3_driver_data->g0_interrupt_array);

		/* Configure the G1S SPIs */
		if (gicv3_driver_data->g1s_interrupt_array) {
Daniel Boulby's avatar
Daniel Boulby committed
217
			gicv3_secure_spis_config(gicv3_driver_data->gicd_base,
218
219
					gicv3_driver_data->g1s_interrupt_num,
					gicv3_driver_data->g1s_interrupt_array,
220
					INTR_GROUP1S);
221
222
			bitmap |= CTLR_ENABLE_G1S_BIT;
		}
223

224
225
		/* Configure the G0 SPIs */
		if (gicv3_driver_data->g0_interrupt_array) {
Daniel Boulby's avatar
Daniel Boulby committed
226
			gicv3_secure_spis_config(gicv3_driver_data->gicd_base,
227
228
					gicv3_driver_data->g0_interrupt_num,
					gicv3_driver_data->g0_interrupt_array,
229
					INTR_GROUP0);
230
231
			bitmap |= CTLR_ENABLE_G0_BIT;
		}
232
#pragma GCC diagnostic pop
233
	}
234
#endif
235
236

	/* Enable the secure SPIs now that they have been configured */
237
	gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
238
239
240
241
242
243
244
245
246
247
}

/*******************************************************************************
 * This function initialises the GIC Redistributor interface of the calling CPU
 * (identified by the 'proc_num' parameter) based upon the data provided by the
 * platform while initialising the driver.
 ******************************************************************************/
void gicv3_rdistif_init(unsigned int proc_num)
{
	uintptr_t gicr_base;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
248
	unsigned int bitmap = 0U;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
249
	uint32_t ctlr;
250

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
251
	assert(gicv3_driver_data != NULL);
252
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
253
254
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
255
256

	ctlr = gicd_read_ctlr(gicv3_driver_data->gicd_base);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
257
	assert((ctlr & CTLR_ARE_S_BIT) != 0U);
258
259
260

	assert(IS_IN_EL3());

261
262
263
	/* Power on redistributor */
	gicv3_rdistif_on(proc_num);

264
	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
265
266

	/* Set the default attribute of all SGIs and PPIs */
Daniel Boulby's avatar
Daniel Boulby committed
267
	gicv3_ppi_sgi_config_defaults(gicr_base);
268

269
270
271
#if !ERROR_DEPRECATED
	if (gicv3_driver_data->interrupt_props != NULL) {
#endif
Daniel Boulby's avatar
Daniel Boulby committed
272
		bitmap = gicv3_secure_ppi_sgi_config_props(gicr_base,
273
274
275
276
				gicv3_driver_data->interrupt_props,
				gicv3_driver_data->interrupt_props_num);
#if !ERROR_DEPRECATED
	} else {
277
278
279
280
281
282
283
		/*
		 * Suppress deprecated declaration warnings in compatibility
		 * function
		 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

284
285
286
287
288
		assert(gicv3_driver_data->g1s_interrupt_array ||
		       gicv3_driver_data->g0_interrupt_array);

		/* Configure the G1S SGIs/PPIs */
		if (gicv3_driver_data->g1s_interrupt_array) {
Daniel Boulby's avatar
Daniel Boulby committed
289
			gicv3_secure_ppi_sgi_config(gicr_base,
290
291
					gicv3_driver_data->g1s_interrupt_num,
					gicv3_driver_data->g1s_interrupt_array,
292
					INTR_GROUP1S);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
293
			bitmap |= CTLR_ENABLE_G1S_BIT;
294
		}
295

296
297
		/* Configure the G0 SGIs/PPIs */
		if (gicv3_driver_data->g0_interrupt_array) {
Daniel Boulby's avatar
Daniel Boulby committed
298
			gicv3_secure_ppi_sgi_config(gicr_base,
299
300
					gicv3_driver_data->g0_interrupt_num,
					gicv3_driver_data->g0_interrupt_array,
301
					INTR_GROUP0);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
302
			bitmap |= CTLR_ENABLE_G0_BIT;
303
		}
304
#pragma GCC diagnostic pop
305
	}
306
#endif
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
307
308
309
310

	/* Enable interrupt groups as required, if not already */
	if ((ctlr & bitmap) != bitmap)
		gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
311
312
}

313
314
315
316
317
318
319
320
321
322
323
324
325
/*******************************************************************************
 * Functions to perform power operations on GIC Redistributor
 ******************************************************************************/
void gicv3_rdistif_off(unsigned int proc_num)
{
	return;
}

void gicv3_rdistif_on(unsigned int proc_num)
{
	return;
}

326
327
328
329
330
331
332
333
334
335
/*******************************************************************************
 * This function enables the GIC CPU interface of the calling CPU using only
 * system register accesses.
 ******************************************************************************/
void gicv3_cpuif_enable(unsigned int proc_num)
{
	uintptr_t gicr_base;
	unsigned int scr_el3;
	unsigned int icc_sre_el3;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
336
	assert(gicv3_driver_data != NULL);
337
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
338
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
339
340
341
	assert(IS_IN_EL3());

	/* Mark the connected core as awake */
342
	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
343
344
345
346
347
348
349
350
351
352
353
354
355
	gicv3_rdistif_mark_core_awake(gicr_base);

	/* Disable the legacy interrupt bypass */
	icc_sre_el3 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT;

	/*
	 * Enable system register access for EL3 and allow lower exception
	 * levels to configure the same for themselves. If the legacy mode is
	 * not supported, the SRE bit is RAO/WI
	 */
	icc_sre_el3 |= (ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT);
	write_icc_sre_el3(read_icc_sre_el3() | icc_sre_el3);

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
356
	scr_el3 = (uint32_t) read_scr_el3();
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

	/*
	 * Switch to NS state to write Non secure ICC_SRE_EL1 and
	 * ICC_SRE_EL2 registers.
	 */
	write_scr_el3(scr_el3 | SCR_NS_BIT);
	isb();

	write_icc_sre_el2(read_icc_sre_el2() | icc_sre_el3);
	write_icc_sre_el1(ICC_SRE_SRE_BIT);
	isb();

	/* Switch to secure state. */
	write_scr_el3(scr_el3 & (~SCR_NS_BIT));
	isb();

	/* Program the idle priority in the PMR */
	write_icc_pmr_el1(GIC_PRI_MASK);

	/* Enable Group0 interrupts */
	write_icc_igrpen0_el1(IGRPEN1_EL1_ENABLE_G0_BIT);

	/* Enable Group1 Secure interrupts */
	write_icc_igrpen1_el3(read_icc_igrpen1_el3() |
				IGRPEN1_EL3_ENABLE_G1S_BIT);

	/* Write the secure ICC_SRE_EL1 register */
	write_icc_sre_el1(ICC_SRE_SRE_BIT);
	isb();
}

/*******************************************************************************
 * This function disables the GIC CPU interface of the calling CPU using
 * only system register accesses.
 ******************************************************************************/
void gicv3_cpuif_disable(unsigned int proc_num)
{
	uintptr_t gicr_base;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
396
	assert(gicv3_driver_data != NULL);
397
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
398
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
399
400
401
402
403
404
405
406
407
408
409

	assert(IS_IN_EL3());

	/* Disable legacy interrupt bypass */
	write_icc_sre_el3(read_icc_sre_el3() |
			  (ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT));

	/* Disable Group0 interrupts */
	write_icc_igrpen0_el1(read_icc_igrpen0_el1() &
			      ~IGRPEN1_EL1_ENABLE_G0_BIT);

410
	/* Disable Group1 Secure and Non-Secure interrupts */
411
	write_icc_igrpen1_el3(read_icc_igrpen1_el3() &
412
413
			      ~(IGRPEN1_EL3_ENABLE_G1NS_BIT |
			      IGRPEN1_EL3_ENABLE_G1S_BIT));
414
415
416
417
418

	/* Synchronise accesses to group enable registers */
	isb();

	/* Mark the connected core as asleep */
419
	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
420
421
422
423
424
425
426
427
428
429
430
431
	gicv3_rdistif_mark_core_asleep(gicr_base);
}

/*******************************************************************************
 * This function returns the id of the highest priority pending interrupt at
 * the GIC cpu interface.
 ******************************************************************************/
unsigned int gicv3_get_pending_interrupt_id(void)
{
	unsigned int id;

	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
432
	id = (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
433
434
435
436
437
438

	/*
	 * If the ID is special identifier corresponding to G1S or G1NS
	 * interrupt, then read the highest pending group 1 interrupt.
	 */
	if ((id == PENDING_G1S_INTID) || (id == PENDING_G1NS_INTID))
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
439
		return (uint32_t)read_icc_hppir1_el1() & HPPIR1_EL1_INTID_MASK;
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

	return id;
}

/*******************************************************************************
 * This function returns the type of the highest priority pending interrupt at
 * the GIC cpu interface. The return values can be one of the following :
 *   PENDING_G1S_INTID  : The interrupt type is secure Group 1.
 *   PENDING_G1NS_INTID : The interrupt type is non secure Group 1.
 *   0 - 1019           : The interrupt type is secure Group 0.
 *   GIC_SPURIOUS_INTERRUPT : there is no pending interrupt with
 *                            sufficient priority to be signaled
 ******************************************************************************/
unsigned int gicv3_get_pending_interrupt_type(void)
{
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
456
	return (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
457
458
459
460
461
462
463
}

/*******************************************************************************
 * 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 Secure / Non Secure. The return value can be one of the
 * following :
464
465
466
 *    INTR_GROUP0  : The interrupt type is a Secure Group 0 interrupt
 *    INTR_GROUP1S : The interrupt type is a Secure Group 1 secure interrupt
 *    INTR_GROUP1NS: The interrupt type is a Secure Group 1 non secure
467
468
469
470
471
472
473
474
475
 *                   interrupt.
 ******************************************************************************/
unsigned int gicv3_get_interrupt_type(unsigned int id,
					  unsigned int proc_num)
{
	unsigned int igroup, grpmodr;
	uintptr_t gicr_base;

	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
476
	assert(gicv3_driver_data != NULL);
477
478

	/* Ensure the parameters are valid */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
479
	assert((id < PENDING_G1S_INTID) || (id >= MIN_LPI_ID));
480
	assert(proc_num < gicv3_driver_data->rdistif_num);
481
482
483

	/* All LPI interrupts are Group 1 non secure */
	if (id >= MIN_LPI_ID)
484
		return INTR_GROUP1NS;
485
486

	if (id < MIN_SPI_ID) {
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
487
		assert(gicv3_driver_data->rdistif_base_addrs != 0U);
488
		gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
489
490
491
		igroup = gicr_get_igroupr0(gicr_base, id);
		grpmodr = gicr_get_igrpmodr0(gicr_base, id);
	} else {
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
492
		assert(gicv3_driver_data->gicd_base != 0U);
493
494
		igroup = gicd_get_igroupr(gicv3_driver_data->gicd_base, id);
		grpmodr = gicd_get_igrpmodr(gicv3_driver_data->gicd_base, id);
495
496
497
498
499
500
	}

	/*
	 * If the IGROUP bit is set, then it is a Group 1 Non secure
	 * interrupt
	 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
501
	if (igroup != 0U)
502
		return INTR_GROUP1NS;
503
504

	/* If the GRPMOD bit is set, then it is a Group 1 Secure interrupt */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
505
	if (grpmodr != 0U)
506
		return INTR_GROUP1S;
507
508

	/* Else it is a Group 0 Secure interrupt */
509
	return INTR_GROUP0;
510
}
511

512
513
514
515
516
517
518
519
520
521
522
523
524
/*****************************************************************************
 * Function to save and disable the GIC ITS register context. The power
 * management of GIC ITS is implementation-defined and this function doesn't
 * save any memory structures required to support ITS. As the sequence to save
 * this state is implementation defined, it should be executed in platform
 * specific code. Calling this function alone and then powering down the GIC and
 * ITS without implementing the aforementioned platform specific code will
 * corrupt the ITS state.
 *
 * This function must be invoked after the GIC CPU interface is disabled.
 *****************************************************************************/
void gicv3_its_save_disable(uintptr_t gits_base, gicv3_its_ctx_t * const its_ctx)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
525
	unsigned int i;
526

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
527
	assert(gicv3_driver_data != NULL);
528
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
529
530
	assert(its_ctx != NULL);
	assert(gits_base != 0U);
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557

	its_ctx->gits_ctlr = gits_read_ctlr(gits_base);

	/* Disable the ITS */
	gits_write_ctlr(gits_base, its_ctx->gits_ctlr &
					(~GITS_CTLR_ENABLED_BIT));

	/* Wait for quiescent state */
	gits_wait_for_quiescent_bit(gits_base);

	its_ctx->gits_cbaser = gits_read_cbaser(gits_base);
	its_ctx->gits_cwriter = gits_read_cwriter(gits_base);

	for (i = 0; i < ARRAY_SIZE(its_ctx->gits_baser); i++)
		its_ctx->gits_baser[i] = gits_read_baser(gits_base, i);
}

/*****************************************************************************
 * Function to restore the GIC ITS register context. The power
 * management of GIC ITS is implementation defined and this function doesn't
 * restore any memory structures required to support ITS. The assumption is
 * that these structures are in memory and are retained during system suspend.
 *
 * This must be invoked before the GIC CPU interface is enabled.
 *****************************************************************************/
void gicv3_its_restore(uintptr_t gits_base, const gicv3_its_ctx_t * const its_ctx)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
558
	unsigned int i;
559

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
560
	assert(gicv3_driver_data != NULL);
561
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
562
563
	assert(its_ctx != NULL);
	assert(gits_base != 0U);
564
565

	/* Assert that the GITS is disabled and quiescent */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
566
567
	assert((gits_read_ctlr(gits_base) & GITS_CTLR_ENABLED_BIT) == 0U);
	assert((gits_read_ctlr(gits_base) & GITS_CTLR_QUIESCENT_BIT) != 0U);
568
569
570
571
572
573
574
575
576
577
578
579

	gits_write_cbaser(gits_base, its_ctx->gits_cbaser);
	gits_write_cwriter(gits_base, its_ctx->gits_cwriter);

	for (i = 0; i < ARRAY_SIZE(its_ctx->gits_baser); i++)
		gits_write_baser(gits_base, i, its_ctx->gits_baser[i]);

	/* Restore the ITS CTLR but leave the ITS disabled */
	gits_write_ctlr(gits_base, its_ctx->gits_ctlr &
			(~GITS_CTLR_ENABLED_BIT));
}

580
581
582
583
584
585
586
587
588
/*****************************************************************************
 * Function to save the GIC Redistributor register context. This function
 * must be invoked after CPU interface disable and prior to Distributor save.
 *****************************************************************************/
void gicv3_rdistif_save(unsigned int proc_num, gicv3_redist_ctx_t * const rdist_ctx)
{
	uintptr_t gicr_base;
	unsigned int int_id;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
589
	assert(gicv3_driver_data != NULL);
590
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
591
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
592
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
593
	assert(rdist_ctx != NULL);
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616

	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];

	/*
	 * Wait for any write to GICR_CTLR to complete before trying to save any
	 * state.
	 */
	gicr_wait_for_pending_write(gicr_base);

	rdist_ctx->gicr_ctlr = gicr_read_ctlr(gicr_base);

	rdist_ctx->gicr_propbaser = gicr_read_propbaser(gicr_base);
	rdist_ctx->gicr_pendbaser = gicr_read_pendbaser(gicr_base);

	rdist_ctx->gicr_igroupr0 = gicr_read_igroupr0(gicr_base);
	rdist_ctx->gicr_isenabler0 = gicr_read_isenabler0(gicr_base);
	rdist_ctx->gicr_ispendr0 = gicr_read_ispendr0(gicr_base);
	rdist_ctx->gicr_isactiver0 = gicr_read_isactiver0(gicr_base);
	rdist_ctx->gicr_icfgr0 = gicr_read_icfgr0(gicr_base);
	rdist_ctx->gicr_icfgr1 = gicr_read_icfgr1(gicr_base);
	rdist_ctx->gicr_igrpmodr0 = gicr_read_igrpmodr0(gicr_base);
	rdist_ctx->gicr_nsacr = gicr_read_nsacr(gicr_base);
	for (int_id = MIN_SGI_ID; int_id < TOTAL_PCPU_INTR_NUM;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
617
			int_id += (1U << IPRIORITYR_SHIFT)) {
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
		rdist_ctx->gicr_ipriorityr[(int_id - MIN_SGI_ID) >> IPRIORITYR_SHIFT] =
				gicr_read_ipriorityr(gicr_base, int_id);
	}


	/*
	 * Call the pre-save hook that implements the IMP DEF sequence that may
	 * be required on some GIC implementations. As this may need to access
	 * the Redistributor registers, we pass it proc_num.
	 */
	gicv3_distif_pre_save(proc_num);
}

/*****************************************************************************
 * Function to restore the GIC Redistributor register context. We disable
 * LPI and per-cpu interrupts before we start restore of the Redistributor.
 * This function must be invoked after Distributor restore but prior to
 * CPU interface enable. The pending and active interrupts are restored
 * after the interrupts are fully configured and enabled.
 *****************************************************************************/
void gicv3_rdistif_init_restore(unsigned int proc_num,
				const gicv3_redist_ctx_t * const rdist_ctx)
{
	uintptr_t gicr_base;
	unsigned int int_id;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
644
	assert(gicv3_driver_data != NULL);
645
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
646
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
647
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
648
	assert(rdist_ctx != NULL);
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666

	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];

	/* Power on redistributor */
	gicv3_rdistif_on(proc_num);

	/*
	 * Call the post-restore hook that implements the IMP DEF sequence that
	 * may be required on some GIC implementations. As this may need to
	 * access the Redistributor registers, we pass it proc_num.
	 */
	gicv3_distif_post_restore(proc_num);

	/*
	 * Disable all SGIs (imp. def.)/PPIs before configuring them. This is a
	 * more scalable approach as it avoids clearing the enable bits in the
	 * GICD_CTLR
	 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
667
	gicr_write_icenabler0(gicr_base, ~0U);
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
	/* Wait for pending writes to GICR_ICENABLER */
	gicr_wait_for_pending_write(gicr_base);

	/*
	 * Disable the LPIs to avoid unpredictable behavior when writing to
	 * GICR_PROPBASER and GICR_PENDBASER.
	 */
	gicr_write_ctlr(gicr_base,
			rdist_ctx->gicr_ctlr & ~(GICR_CTLR_EN_LPIS_BIT));

	/* Restore registers' content */
	gicr_write_propbaser(gicr_base, rdist_ctx->gicr_propbaser);
	gicr_write_pendbaser(gicr_base, rdist_ctx->gicr_pendbaser);

	gicr_write_igroupr0(gicr_base, rdist_ctx->gicr_igroupr0);

	for (int_id = MIN_SGI_ID; int_id < TOTAL_PCPU_INTR_NUM;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
685
			int_id += (1U << IPRIORITYR_SHIFT)) {
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
		gicr_write_ipriorityr(gicr_base, int_id,
		rdist_ctx->gicr_ipriorityr[
				(int_id - MIN_SGI_ID) >> IPRIORITYR_SHIFT]);
	}

	gicr_write_icfgr0(gicr_base, rdist_ctx->gicr_icfgr0);
	gicr_write_icfgr1(gicr_base, rdist_ctx->gicr_icfgr1);
	gicr_write_igrpmodr0(gicr_base, rdist_ctx->gicr_igrpmodr0);
	gicr_write_nsacr(gicr_base, rdist_ctx->gicr_nsacr);

	/* Restore after group and priorities are set */
	gicr_write_ispendr0(gicr_base, rdist_ctx->gicr_ispendr0);
	gicr_write_isactiver0(gicr_base, rdist_ctx->gicr_isactiver0);

	/*
	 * Wait for all writes to the Distributor to complete before enabling
	 * the SGI and PPIs.
	 */
	gicr_wait_for_upstream_pending_write(gicr_base);
	gicr_write_isenabler0(gicr_base, rdist_ctx->gicr_isenabler0);

	/*
	 * Restore GICR_CTLR.Enable_LPIs bit and wait for pending writes in case
	 * the first write to GICR_CTLR was still in flight (this write only
	 * restores GICR_CTLR.Enable_LPIs and no waiting is required for this
	 * bit).
	 */
	gicr_write_ctlr(gicr_base, rdist_ctx->gicr_ctlr);
	gicr_wait_for_pending_write(gicr_base);
}

/*****************************************************************************
 * Function to save the GIC Distributor register context. This function
 * must be invoked after CPU interface disable and Redistributor save.
 *****************************************************************************/
void gicv3_distif_save(gicv3_dist_ctx_t * const dist_ctx)
{
	unsigned int num_ints;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
725
726
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
727
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
728
	assert(dist_ctx != NULL);
729
730
731
732
733

	uintptr_t gicd_base = gicv3_driver_data->gicd_base;

	num_ints = gicd_read_typer(gicd_base);
	num_ints &= TYPER_IT_LINES_NO_MASK;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
734
	num_ints = (num_ints + 1U) << 5;
735

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
736
	assert(num_ints <= (MAX_SPI_ID + 1U));
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786

	/* Wait for pending write to complete */
	gicd_wait_for_pending_write(gicd_base);

	/* Save the GICD_CTLR */
	dist_ctx->gicd_ctlr = gicd_read_ctlr(gicd_base);

	/* Save GICD_IGROUPR for INTIDs 32 - 1020 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUPR);

	/* Save GICD_ISENABLER for INT_IDs 32 - 1020 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLER);

	/* Save GICD_ISPENDR for INTIDs 32 - 1020 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPENDR);

	/* Save GICD_ISACTIVER for INTIDs 32 - 1020 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVER);

	/* Save GICD_IPRIORITYR for INTIDs 32 - 1020 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITYR);

	/* Save GICD_ICFGR for INTIDs 32 - 1020 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFGR);

	/* Save GICD_IGRPMODR for INTIDs 32 - 1020 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMODR);

	/* Save GICD_NSACR for INTIDs 32 - 1020 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSACR);

	/* Save GICD_IROUTER for INTIDs 32 - 1024 */
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTER);

	/*
	 * GICD_ITARGETSR<n> and GICD_SPENDSGIR<n> are RAZ/WI when
	 * GICD_CTLR.ARE_(S|NS) bits are set which is the case for our GICv3
	 * driver.
	 */
}

/*****************************************************************************
 * Function to restore the GIC Distributor register context. We disable G0, G1S
 * and G1NS interrupt groups before we start restore of the Distributor. This
 * function must be invoked prior to Redistributor restore and CPU interface
 * enable. The pending and active interrupts are restored after the interrupts
 * are fully configured and enabled.
 *****************************************************************************/
void gicv3_distif_init_restore(const gicv3_dist_ctx_t * const dist_ctx)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
787
	unsigned int num_ints = 0U;
788

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
789
790
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
791
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
792
	assert(dist_ctx != NULL);
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811

	uintptr_t gicd_base = gicv3_driver_data->gicd_base;

	/*
	 * Clear the "enable" bits for G0/G1S/G1NS interrupts before configuring
	 * the ARE_S bit. The Distributor might generate a system error
	 * otherwise.
	 */
	gicd_clr_ctlr(gicd_base,
		      CTLR_ENABLE_G0_BIT |
		      CTLR_ENABLE_G1S_BIT |
		      CTLR_ENABLE_G1NS_BIT,
		      RWP_TRUE);

	/* Set the ARE_S and ARE_NS bit now that interrupts have been disabled */
	gicd_set_ctlr(gicd_base, CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);

	num_ints = gicd_read_typer(gicd_base);
	num_ints &= TYPER_IT_LINES_NO_MASK;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
812
	num_ints = (num_ints + 1U) << 5;
813

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
814
	assert(num_ints <= (MAX_SPI_ID + 1U));
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852

	/* Restore GICD_IGROUPR for INTIDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUPR);

	/* Restore GICD_IPRIORITYR for INTIDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITYR);

	/* Restore GICD_ICFGR for INTIDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFGR);

	/* Restore GICD_IGRPMODR for INTIDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMODR);

	/* Restore GICD_NSACR for INTIDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSACR);

	/* Restore GICD_IROUTER for INTIDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTER);

	/*
	 * Restore ISENABLER, ISPENDR and ISACTIVER after the interrupts are
	 * configured.
	 */

	/* Restore GICD_ISENABLER for INT_IDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLER);

	/* Restore GICD_ISPENDR for INTIDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPENDR);

	/* Restore GICD_ISACTIVER for INTIDs 32 - 1020 */
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVER);

	/* Restore the GICD_CTLR */
	gicd_write_ctlr(gicd_base, dist_ctx->gicd_ctlr);
	gicd_wait_for_pending_write(gicd_base);

}
853
854
855
856
857
858
859

/*******************************************************************************
 * This function gets the priority of the interrupt the processor is currently
 * servicing.
 ******************************************************************************/
unsigned int gicv3_get_running_priority(void)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
860
	return (unsigned int)read_icc_rpr_el1();
861
}
862
863
864
865
866
867
868
869
870
871
872

/*******************************************************************************
 * This function checks if the interrupt identified by id is active (whether the
 * state is either active, or active and pending). The proc_num is used if the
 * interrupt is SGI or PPI and programs the corresponding Redistributor
 * interface.
 ******************************************************************************/
unsigned int gicv3_get_interrupt_active(unsigned int id, unsigned int proc_num)
{
	unsigned int value;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
873
874
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
875
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
876
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
877
878
879
880
881
882
883
884
885
886
887
888
	assert(id <= MAX_SPI_ID);

	if (id < MIN_SPI_ID) {
		/* For SGIs and PPIs */
		value = gicr_get_isactiver0(
				gicv3_driver_data->rdistif_base_addrs[proc_num], id);
	} else {
		value = gicd_get_isactiver(gicv3_driver_data->gicd_base, id);
	}

	return value;
}
889
890
891
892
893
894
895
896

/*******************************************************************************
 * This function enables the interrupt identified by id. The proc_num
 * is used if the interrupt is SGI or PPI, and programs the corresponding
 * Redistributor interface.
 ******************************************************************************/
void gicv3_enable_interrupt(unsigned int id, unsigned int proc_num)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
897
898
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
899
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
900
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
	assert(id <= MAX_SPI_ID);

	/*
	 * Ensure that any shared variable updates depending on out of band
	 * interrupt trigger are observed before enabling interrupt.
	 */
	dsbishst();
	if (id < MIN_SPI_ID) {
		/* For SGIs and PPIs */
		gicr_set_isenabler0(
				gicv3_driver_data->rdistif_base_addrs[proc_num],
				id);
	} else {
		gicd_set_isenabler(gicv3_driver_data->gicd_base, id);
	}
}

/*******************************************************************************
 * This function disables the interrupt identified by id. The proc_num
 * is used if the interrupt is SGI or PPI, and programs the corresponding
 * Redistributor interface.
 ******************************************************************************/
void gicv3_disable_interrupt(unsigned int id, unsigned int proc_num)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
925
926
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
927
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
928
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
	assert(id <= MAX_SPI_ID);

	/*
	 * Disable interrupt, and ensure that any shared variable updates
	 * depending on out of band interrupt trigger are observed afterwards.
	 */
	if (id < MIN_SPI_ID) {
		/* For SGIs and PPIs */
		gicr_set_icenabler0(
				gicv3_driver_data->rdistif_base_addrs[proc_num],
				id);

		/* Write to clear enable requires waiting for pending writes */
		gicr_wait_for_pending_write(
				gicv3_driver_data->rdistif_base_addrs[proc_num]);
	} else {
		gicd_set_icenabler(gicv3_driver_data->gicd_base, id);

		/* Write to clear enable requires waiting for pending writes */
		gicd_wait_for_pending_write(gicv3_driver_data->gicd_base);
	}

	dsbishst();
}
953
954
955
956
957
958
959
960
961
962

/*******************************************************************************
 * This function sets the interrupt priority as supplied for the given interrupt
 * id.
 ******************************************************************************/
void gicv3_set_interrupt_priority(unsigned int id, unsigned int proc_num,
		unsigned int priority)
{
	uintptr_t gicr_base;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
963
964
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
965
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
966
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
967
968
969
970
971
972
973
974
975
	assert(id <= MAX_SPI_ID);

	if (id < MIN_SPI_ID) {
		gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
		gicr_set_ipriorityr(gicr_base, id, priority);
	} else {
		gicd_set_ipriorityr(gicv3_driver_data->gicd_base, id, priority);
	}
}
976
977
978
979
980
981
982
983
984

/*******************************************************************************
 * This function assigns group for the interrupt identified by id. The proc_num
 * is used if the interrupt is SGI or PPI, and programs the corresponding
 * Redistributor interface. The group can be any of GICV3_INTR_GROUP*
 ******************************************************************************/
void gicv3_set_interrupt_type(unsigned int id, unsigned int proc_num,
		unsigned int type)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
985
	bool igroup = false, grpmod = false;
986
987
	uintptr_t gicr_base;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
988
989
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
990
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
991
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
992
993
994

	switch (type) {
	case INTR_GROUP1S:
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
995
996
		igroup = false;
		grpmod = true;
997
998
		break;
	case INTR_GROUP0:
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
999
1000
		igroup = false;
		grpmod = false;
1001
1002
		break;
	case INTR_GROUP1NS:
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1003
1004
		igroup = true;
		grpmod = false;
1005
1006
		break;
	default:
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1007
		assert(false);
1008
		break;
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
	}

	if (id < MIN_SPI_ID) {
		gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
		if (igroup)
			gicr_set_igroupr0(gicr_base, id);
		else
			gicr_clr_igroupr0(gicr_base, id);

		if (grpmod)
			gicr_set_igrpmodr0(gicr_base, id);
		else
			gicr_clr_igrpmodr0(gicr_base, id);
	} else {
		/* Serialize read-modify-write to Distributor registers */
		spin_lock(&gic_lock);
		if (igroup)
			gicd_set_igroupr(gicv3_driver_data->gicd_base, id);
		else
			gicd_clr_igroupr(gicv3_driver_data->gicd_base, id);

		if (grpmod)
			gicd_set_igrpmodr(gicv3_driver_data->gicd_base, id);
		else
			gicd_clr_igrpmodr(gicv3_driver_data->gicd_base, id);
		spin_unlock(&gic_lock);
	}
}
1037
1038
1039
1040
1041
1042

/*******************************************************************************
 * This function raises the specified Secure Group 0 SGI.
 *
 * The target parameter must be a valid MPIDR in the system.
 ******************************************************************************/
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1043
void gicv3_raise_secure_g0_sgi(unsigned int sgi_num, u_register_t target)
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
{
	unsigned int tgt, aff3, aff2, aff1, aff0;
	uint64_t sgi_val;

	/* Verify interrupt number is in the SGI range */
	assert((sgi_num >= MIN_SGI_ID) && (sgi_num < MIN_PPI_ID));

	/* Extract affinity fields from target */
	aff0 = MPIDR_AFFLVL0_VAL(target);
	aff1 = MPIDR_AFFLVL1_VAL(target);
	aff2 = MPIDR_AFFLVL2_VAL(target);
	aff3 = MPIDR_AFFLVL3_VAL(target);

	/*
	 * Make target list from affinity 0, and ensure GICv3 SGI can target
	 * this PE.
	 */
	assert(aff0 < GICV3_MAX_SGI_TARGETS);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1062
	tgt = BIT_32(aff0);
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075

	/* Raise SGI to PE specified by its affinity */
	sgi_val = GICV3_SGIR_VALUE(aff3, aff2, aff1, sgi_num, SGIR_IRM_TO_AFF,
			tgt);

	/*
	 * Ensure that any shared variable updates depending on out of band
	 * interrupt trigger are observed before raising SGI.
	 */
	dsbishst();
	write_icc_sgi0r_el1(sgi_val);
	isb();
}
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092

/*******************************************************************************
 * This function sets the interrupt routing for the given SPI interrupt id.
 * The interrupt routing is specified in routing mode and mpidr.
 *
 * The routing mode can be either of:
 *  - GICV3_IRM_ANY
 *  - GICV3_IRM_PE
 *
 * The mpidr is the affinity of the PE to which the interrupt will be routed,
 * and is ignored for routing mode GICV3_IRM_ANY.
 ******************************************************************************/
void gicv3_set_spi_routing(unsigned int id, unsigned int irm, u_register_t mpidr)
{
	unsigned long long aff;
	uint64_t router;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1093
1094
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1095
1096

	assert((irm == GICV3_IRM_ANY) || (irm == GICV3_IRM_PE));
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1097
	assert((id >= MIN_SPI_ID) && (id <= MAX_SPI_ID));
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107

	aff = gicd_irouter_val_from_mpidr(mpidr, irm);
	gicd_write_irouter(gicv3_driver_data->gicd_base, id, aff);

	/*
	 * In implementations that do not require 1 of N distribution of SPIs,
	 * IRM might be RAZ/WI. Read back and verify IRM bit.
	 */
	if (irm == GICV3_IRM_ANY) {
		router = gicd_read_irouter(gicv3_driver_data->gicd_base, id);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1108
		if (((router >> IROUTER_IRM_SHIFT) & IROUTER_IRM_MASK) == 0U) {
1109
1110
1111
1112
1113
			ERROR("GICv3 implementation doesn't support routing ANY\n");
			panic();
		}
	}
}
1114
1115
1116
1117
1118
1119
1120
1121

/*******************************************************************************
 * This function clears the pending status of an interrupt identified by id.
 * The proc_num is used if the interrupt is SGI or PPI, and programs the
 * corresponding Redistributor interface.
 ******************************************************************************/
void gicv3_clear_interrupt_pending(unsigned int id, unsigned int proc_num)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1122
1123
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1124
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1125
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147

	/*
	 * Clear pending interrupt, and ensure that any shared variable updates
	 * depending on out of band interrupt trigger are observed afterwards.
	 */
	if (id < MIN_SPI_ID) {
		/* For SGIs and PPIs */
		gicr_set_icpendr0(gicv3_driver_data->rdistif_base_addrs[proc_num],
				id);
	} else {
		gicd_set_icpendr(gicv3_driver_data->gicd_base, id);
	}
	dsbishst();
}

/*******************************************************************************
 * This function sets the pending status of an interrupt identified by id.
 * The proc_num is used if the interrupt is SGI or PPI and programs the
 * corresponding Redistributor interface.
 ******************************************************************************/
void gicv3_set_interrupt_pending(unsigned int id, unsigned int proc_num)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1148
1149
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1150
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1151
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165

	/*
	 * Ensure that any shared variable updates depending on out of band
	 * interrupt trigger are observed before setting interrupt pending.
	 */
	dsbishst();
	if (id < MIN_SPI_ID) {
		/* For SGIs and PPIs */
		gicr_set_ispendr0(gicv3_driver_data->rdistif_base_addrs[proc_num],
				id);
	} else {
		gicd_set_ispendr(gicv3_driver_data->gicd_base, id);
	}
}
1166
1167
1168
1169
1170
1171
1172
1173
1174

/*******************************************************************************
 * This function sets the PMR register with the supplied value. Returns the
 * original PMR.
 ******************************************************************************/
unsigned int gicv3_set_pmr(unsigned int mask)
{
	unsigned int old_mask;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1175
	old_mask = (uint32_t) read_icc_pmr_el1();
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187

	/*
	 * Order memory updates w.r.t. PMR write, and ensure they're visible
	 * before potential out of band interrupt trigger because of PMR update.
	 * PMR system register writes are self-synchronizing, so no ISB required
	 * thereafter.
	 */
	dsbishst();
	write_icc_pmr_el1(mask);

	return old_mask;
}