gicv3_main.c 45.6 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2020, 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
#include <assert.h>

9
10
#include <arch.h>
#include <arch_helpers.h>
11
12
13
14
15
#include <common/debug.h>
#include <common/interrupt_props.h>
#include <drivers/arm/gicv3.h>
#include <lib/spinlock.h>

16
17
#include "gicv3_private.h"

18
const gicv3_driver_data_t *gicv3_driver_data;
19

20
21
22
23
24
/*
 * 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
25
static spinlock_t gic_lock;
26

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

34
35
36
37
38
39
40
41
42
43
44
45
/* Check interrupt ID for SGI/(E)PPI and (E)SPIs */
static bool is_sgi_ppi(unsigned int id);

/*
 * Helper macros to save and restore GICR and GICD registers
 * corresponding to their numbers to and from the context
 */
#define RESTORE_GICR_REG(base, ctx, name, i)	\
	gicr_write_##name((base), (i), (ctx)->gicr_##name[(i)])

#define SAVE_GICR_REG(base, ctx, name, i)	\
	(ctx)->gicr_##name[(i)] = gicr_read_##name((base), (i))
46
47
48
49

/* Helper macros to save and restore GICD registers to and from the context */
#define RESTORE_GICD_REGS(base, ctx, intr_num, reg, REG)		\
	do {								\
50
51
52
53
54
		for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num);\
				int_id += (1U << REG##R_SHIFT)) {	\
			gicd_write_##reg((base), int_id,		\
				(ctx)->gicd_##reg[(int_id - MIN_SPI_ID) >> \
							REG##R_SHIFT]);	\
55
		}							\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
56
	} while (false)
57
58
59

#define SAVE_GICD_REGS(base, ctx, intr_num, reg, REG)			\
	do {								\
60
61
62
63
		for (unsigned int int_id = MIN_SPI_ID; int_id < (intr_num);\
				int_id += (1U << REG##R_SHIFT)) {	\
			(ctx)->gicd_##reg[(int_id - MIN_SPI_ID) >>	\
			REG##R_SHIFT] = gicd_read_##reg((base), int_id); \
64
		}							\
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
65
	} while (false)
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#if GIC_EXT_INTID
#define RESTORE_GICD_EREGS(base, ctx, intr_num, reg, REG)		\
	do {								\
		for (unsigned int int_id = MIN_ESPI_ID; int_id < (intr_num);\
				int_id += (1U << REG##R_SHIFT)) {	\
			gicd_write_##reg((base), int_id,		\
			(ctx)->gicd_##reg[(int_id - (MIN_ESPI_ID - MIN_SPI_ID))\
						>> REG##R_SHIFT]);	\
		}							\
	} while (false)

#define SAVE_GICD_EREGS(base, ctx, intr_num, reg, REG)			\
	do {								\
		for (unsigned int int_id = MIN_ESPI_ID; int_id < (intr_num);\
				int_id += (1U << REG##R_SHIFT)) {	\
			(ctx)->gicd_##reg[(int_id - (MIN_ESPI_ID - MIN_SPI_ID))\
			>> REG##R_SHIFT] = gicd_read_##reg((base), int_id);\
		}							\
	} while (false)
#else
#define SAVE_GICD_EREGS(base, ctx, intr_num, reg, REG)
#define RESTORE_GICD_EREGS(base, ctx, intr_num, reg, REG)
#endif /* GIC_EXT_INTID */
90

91
92
93
94
/*******************************************************************************
 * This function initialises the ARM GICv3 driver in EL3 with provided platform
 * inputs.
 ******************************************************************************/
95
void __init gicv3_driver_init(const gicv3_driver_data_t *plat_driver_data)
96
97
{
	unsigned int gic_version;
98
	unsigned int gicv2_compat;
99

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
100
101
102
103
	assert(plat_driver_data != NULL);
	assert(plat_driver_data->gicd_base != 0U);
	assert(plat_driver_data->rdistif_num != 0U);
	assert(plat_driver_data->rdistif_base_addrs != NULL);
104
105
106

	assert(IS_IN_EL3());

107
108
	assert((plat_driver_data->interrupt_props_num != 0U) ?
	       (plat_driver_data->interrupt_props != NULL) : 1);
109
110

	/* Check for system register support */
111
112
113
114
#ifndef __aarch64__
	assert((read_id_pfr1() &
			(ID_PFR1_GIC_MASK << ID_PFR1_GIC_SHIFT)) != 0U);
#else
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
115
116
	assert((read_id_aa64pfr0_el1() &
			(ID_AA64PFR0_GIC_MASK << ID_AA64PFR0_GIC_SHIFT)) != 0U);
117
#endif /* !__aarch64__ */
118
119

	gic_version = gicd_read_pidr2(plat_driver_data->gicd_base);
120
	gic_version >>= PIDR2_ARCH_REV_SHIFT;
121
122
	gic_version &= PIDR2_ARCH_REV_MASK;

123
124
125
126
127
128
129
130
131
132
	/* Check GIC version */
#if GIC_ENABLE_V4_EXTN
	assert(gic_version == ARCH_REV_GICV4);

	/* GICv4 supports Direct Virtual LPI injection */
	assert((gicd_read_typer(plat_driver_data->gicd_base)
					& TYPER_DVIS) != 0);
#else
	assert(gic_version == ARCH_REV_GICV3);
#endif
133
	/*
134
135
	 * Find out whether the GIC supports the GICv2 compatibility mode.
	 * The ARE_S bit resets to 0 if supported
136
137
138
	 */
	gicv2_compat = gicd_read_ctlr(plat_driver_data->gicd_base);
	gicv2_compat >>= CTLR_ARE_S_SHIFT;
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	gicv2_compat = gicv2_compat & CTLR_ARE_S_MASK;

	if (plat_driver_data->gicr_base != 0U) {
		/*
		 * 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);
#if !HW_ASSISTED_COHERENCY
		/*
		 * Flush the rdistif_base_addrs[] contents linked to the GICv3 driver.
		 */
		flush_dcache_range((uintptr_t)(plat_driver_data->rdistif_base_addrs),
			plat_driver_data->rdistif_num *
			sizeof(*(plat_driver_data->rdistif_base_addrs)));
#endif
	}
161
	gicv3_driver_data = plat_driver_data;
162

163
164
165
166
	/*
	 * 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
167
	 * driver data to ensure coherency. This is not required if the
168
	 * platform has HW_ASSISTED_COHERENCY enabled.
169
	 */
170
171
172
173
174
#if !HW_ASSISTED_COHERENCY
	flush_dcache_range((uintptr_t)&gicv3_driver_data,
		sizeof(gicv3_driver_data));
	flush_dcache_range((uintptr_t)gicv3_driver_data,
		sizeof(*gicv3_driver_data));
175
#endif
176
177
178
	INFO("GICv%u with%s legacy support detected.\n", gic_version,
				(gicv2_compat == 0U) ? "" : "out");
	INFO("ARM GICv%u driver initialized in EL3\n", gic_version);
179
180
181
182
183
184
}

/*******************************************************************************
 * This function initialises the GIC distributor interface based upon the data
 * provided by the platform while initialising the driver.
 ******************************************************************************/
185
void __init gicv3_distif_init(void)
186
{
187
	unsigned int bitmap;
188

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
189
190
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
191
192
193
194
195
196
197
198

	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.
	 */
199
	gicd_clr_ctlr(gicv3_driver_data->gicd_base,
200
201
202
203
204
205
		      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 */
206
	gicd_set_ctlr(gicv3_driver_data->gicd_base,
207
208
			CTLR_ARE_S_BIT | CTLR_ARE_NS_BIT, RWP_TRUE);

209
	/* Set the default attribute of all (E)SPIs */
Daniel Boulby's avatar
Daniel Boulby committed
210
	gicv3_spis_config_defaults(gicv3_driver_data->gicd_base);
211

212
213
214
215
	bitmap = gicv3_secure_spis_config_props(
			gicv3_driver_data->gicd_base,
			gicv3_driver_data->interrupt_props,
			gicv3_driver_data->interrupt_props_num);
216

217
	/* Enable the secure (E)SPIs now that they have been configured */
218
	gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
219
220
221
222
223
224
225
226
227
228
}

/*******************************************************************************
 * 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;
229
	unsigned int bitmap;
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
230
	uint32_t ctlr;
231

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
232
	assert(gicv3_driver_data != NULL);
233
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
234
235
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
236
237

	ctlr = gicd_read_ctlr(gicv3_driver_data->gicd_base);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
238
	assert((ctlr & CTLR_ARE_S_BIT) != 0U);
239
240
241

	assert(IS_IN_EL3());

242
243
244
	/* Power on redistributor */
	gicv3_rdistif_on(proc_num);

245
	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
246
	assert(gicr_base != 0U);
247

248
	/* Set the default attribute of all SGIs and (E)PPIs */
Daniel Boulby's avatar
Daniel Boulby committed
249
	gicv3_ppi_sgi_config_defaults(gicr_base);
250

251
252
253
	bitmap = gicv3_secure_ppi_sgi_config_props(gicr_base,
			gicv3_driver_data->interrupt_props,
			gicv3_driver_data->interrupt_props_num);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
254
255

	/* Enable interrupt groups as required, if not already */
256
	if ((ctlr & bitmap) != bitmap) {
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
257
		gicd_set_ctlr(gicv3_driver_data->gicd_base, bitmap, RWP_TRUE);
258
	}
259
260
}

261
262
263
264
265
266
267
268
269
270
271
/*******************************************************************************
 * Functions to perform power operations on GIC Redistributor
 ******************************************************************************/
void gicv3_rdistif_off(unsigned int proc_num)
{
}

void gicv3_rdistif_on(unsigned int proc_num)
{
}

272
273
274
275
276
277
278
/*******************************************************************************
 * 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;
279
	u_register_t scr_el3;
280
281
	unsigned int icc_sre_el3;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
282
	assert(gicv3_driver_data != NULL);
283
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
284
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
285
286
287
	assert(IS_IN_EL3());

	/* Mark the connected core as awake */
288
	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
289
290
291
292
293
294
295
296
297
298
299
300
301
	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);

302
	scr_el3 = read_scr_el3();
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318

	/*
	 * 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();

319
320
321
322
	/* Write the secure ICC_SRE_EL1 register */
	write_icc_sre_el1(ICC_SRE_SRE_BIT);
	isb();

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
	/* 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);
	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
343
	assert(gicv3_driver_data != NULL);
344
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
345
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
346
347
348
349
350
351
352
353
354
355
356

	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);

357
	/* Disable Group1 Secure and Non-Secure interrupts */
358
	write_icc_igrpen1_el3(read_icc_igrpen1_el3() &
359
360
			      ~(IGRPEN1_EL3_ENABLE_G1NS_BIT |
			      IGRPEN1_EL3_ENABLE_G1S_BIT));
361
362
363
364
365

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

	/* Mark the connected core as asleep */
366
	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
367
	assert(gicr_base != 0U);
368
369
370
371
372
373
374
375
376
377
378
379
	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
380
	id = (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
381
382
383
384
385

	/*
	 * If the ID is special identifier corresponding to G1S or G1NS
	 * interrupt, then read the highest pending group 1 interrupt.
	 */
386
	if ((id == PENDING_G1S_INTID) || (id == PENDING_G1NS_INTID)) {
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
387
		return (uint32_t)read_icc_hppir1_el1() & HPPIR1_EL1_INTID_MASK;
388
	}
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

	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
405
	return (uint32_t)read_icc_hppir0_el1() & HPPIR0_EL1_INTID_MASK;
406
407
408
409
410
411
412
}

/*******************************************************************************
 * 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 :
413
414
415
 *    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
416
417
 *                   interrupt.
 ******************************************************************************/
418
unsigned int gicv3_get_interrupt_type(unsigned int id, unsigned int proc_num)
419
420
421
422
423
{
	unsigned int igroup, grpmodr;
	uintptr_t gicr_base;

	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
424
	assert(gicv3_driver_data != NULL);
425
426

	/* Ensure the parameters are valid */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
427
	assert((id < PENDING_G1S_INTID) || (id >= MIN_LPI_ID));
428
	assert(proc_num < gicv3_driver_data->rdistif_num);
429
430

	/* All LPI interrupts are Group 1 non secure */
431
	if (id >= MIN_LPI_ID) {
432
		return INTR_GROUP1NS;
433
	}
434

435
436
437
	/* Check interrupt ID */
	if (is_sgi_ppi(id)) {
		/* SGIs: 0-15, PPIs: 16-31, EPPIs: 1056-1119 */
Andrew F. Davis's avatar
Andrew F. Davis committed
438
		assert(gicv3_driver_data->rdistif_base_addrs != NULL);
439
		gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
440
441
		igroup = gicr_get_igroupr(gicr_base, id);
		grpmodr = gicr_get_igrpmodr(gicr_base, id);
442
	} else {
443
		/* SPIs: 32-1019, ESPIs: 4096-5119 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
444
		assert(gicv3_driver_data->gicd_base != 0U);
445
446
		igroup = gicd_get_igroupr(gicv3_driver_data->gicd_base, id);
		grpmodr = gicd_get_igrpmodr(gicv3_driver_data->gicd_base, id);
447
448
449
450
451
452
	}

	/*
	 * If the IGROUP bit is set, then it is a Group 1 Non secure
	 * interrupt
	 */
453
	if (igroup != 0U) {
454
		return INTR_GROUP1NS;
455
	}
456
457

	/* If the GRPMOD bit is set, then it is a Group 1 Secure interrupt */
458
	if (grpmodr != 0U) {
459
		return INTR_GROUP1S;
460
	}
461
462

	/* Else it is a Group 0 Secure interrupt */
463
	return INTR_GROUP0;
464
}
465

466
467
468
469
470
471
472
473
474
475
476
/*****************************************************************************
 * 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.
 *****************************************************************************/
477
478
void gicv3_its_save_disable(uintptr_t gits_base,
				gicv3_its_ctx_t * const its_ctx)
479
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
480
	unsigned int i;
481

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
482
	assert(gicv3_driver_data != NULL);
483
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
484
485
	assert(its_ctx != NULL);
	assert(gits_base != 0U);
486
487
488
489

	its_ctx->gits_ctlr = gits_read_ctlr(gits_base);

	/* Disable the ITS */
490
	gits_write_ctlr(gits_base, its_ctx->gits_ctlr & ~GITS_CTLR_ENABLED_BIT);
491
492
493
494
495
496
497

	/* 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);

498
	for (i = 0U; i < ARRAY_SIZE(its_ctx->gits_baser); i++) {
499
		its_ctx->gits_baser[i] = gits_read_baser(gits_base, i);
500
	}
501
502
503
504
505
506
507
508
509
510
}

/*****************************************************************************
 * 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.
 *****************************************************************************/
511
512
void gicv3_its_restore(uintptr_t gits_base,
			const gicv3_its_ctx_t * const its_ctx)
513
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
514
	unsigned int i;
515

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
516
	assert(gicv3_driver_data != NULL);
517
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
518
519
	assert(its_ctx != NULL);
	assert(gits_base != 0U);
520
521

	/* Assert that the GITS is disabled and quiescent */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
522
523
	assert((gits_read_ctlr(gits_base) & GITS_CTLR_ENABLED_BIT) == 0U);
	assert((gits_read_ctlr(gits_base) & GITS_CTLR_QUIESCENT_BIT) != 0U);
524
525
526
527

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

528
	for (i = 0U; i < ARRAY_SIZE(its_ctx->gits_baser); i++) {
529
		gits_write_baser(gits_base, i, its_ctx->gits_baser[i]);
530
	}
531
532

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

536
537
538
539
/*****************************************************************************
 * Function to save the GIC Redistributor register context. This function
 * must be invoked after CPU interface disable and prior to Distributor save.
 *****************************************************************************/
540
541
void gicv3_rdistif_save(unsigned int proc_num,
			gicv3_redist_ctx_t * const rdist_ctx)
542
543
{
	uintptr_t gicr_base;
544
	unsigned int i, ppi_regs_num, regs_num;
545

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
546
	assert(gicv3_driver_data != NULL);
547
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
548
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
549
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
550
	assert(rdist_ctx != NULL);
551
552
553

	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];

554
555
556
557
558
559
560
561
562
563
564
#if GIC_EXT_INTID
	/* Calculate number of PPI registers */
	ppi_regs_num = (unsigned int)((gicr_read_typer(gicr_base) >>
			TYPER_PPI_NUM_SHIFT) & TYPER_PPI_NUM_MASK) + 1;
	/* All other values except PPInum [0-2] are reserved */
	if (ppi_regs_num > 3U) {
		ppi_regs_num = 1U;
	}
#else
	ppi_regs_num = 1U;
#endif
565
566
567
568
569
570
571
572
573
574
575
	/*
	 * 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);

576
577
578
579
580
581
582
	/* 32 interrupt IDs per register */
	for (i = 0U; i < ppi_regs_num; ++i) {
		SAVE_GICR_REG(gicr_base, rdist_ctx, igroupr, i);
		SAVE_GICR_REG(gicr_base, rdist_ctx, isenabler, i);
		SAVE_GICR_REG(gicr_base, rdist_ctx, ispendr, i);
		SAVE_GICR_REG(gicr_base, rdist_ctx, isactiver, i);
		SAVE_GICR_REG(gicr_base, rdist_ctx, igrpmodr, i);
583
584
	}

585
586
587
588
589
590
591
592
593
594
595
596
597
	/* 16 interrupt IDs per GICR_ICFGR register */
	regs_num = ppi_regs_num << 1;
	for (i = 0U; i < regs_num; ++i) {
		SAVE_GICR_REG(gicr_base, rdist_ctx, icfgr, i);
	}

	rdist_ctx->gicr_nsacr = gicr_read_nsacr(gicr_base);

	/* 4 interrupt IDs per GICR_IPRIORITYR register */
	regs_num = ppi_regs_num << 3;
	for (i = 0U; i < regs_num; ++i) {
		SAVE_GICR_REG(gicr_base, rdist_ctx, ipriorityr, i);
	}
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617

	/*
	 * 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;
618
	unsigned int i, ppi_regs_num, regs_num;
619

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
620
	assert(gicv3_driver_data != NULL);
621
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
622
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
623
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
624
	assert(rdist_ctx != NULL);
625
626
627

	gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];

628
629
630
631
632
633
634
635
636
637
638
#if GIC_EXT_INTID
	/* Calculate number of PPI registers */
	ppi_regs_num = (unsigned int)((gicr_read_typer(gicr_base) >>
			TYPER_PPI_NUM_SHIFT) & TYPER_PPI_NUM_MASK) + 1;
	/* All other values except PPInum [0-2] are reserved */
	if (ppi_regs_num > 3U) {
		ppi_regs_num = 1U;
	}
#else
	ppi_regs_num = 1U;
#endif
639
640
641
642
643
644
645
646
647
648
649
	/* 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);

	/*
650
651
652
	 * Disable all SGIs (imp. def.)/(E)PPIs before configuring them.
	 * This is a more scalable approach as it avoids clearing the enable
	 * bits in the GICD_CTLR.
653
	 */
654
655
656
657
	for (i = 0U; i < ppi_regs_num; ++i) {
		gicr_write_icenabler(gicr_base, i, ~0U);
	}

658
659
660
661
662
663
664
665
666
667
668
669
670
671
	/* 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);

672
673
674
675
676
677
678
679
680
681
682
	/* 32 interrupt IDs per register */
	for (i = 0U; i < ppi_regs_num; ++i) {
		RESTORE_GICR_REG(gicr_base, rdist_ctx, igroupr, i);
		RESTORE_GICR_REG(gicr_base, rdist_ctx, igrpmodr, i);
	}

	/* 4 interrupt IDs per GICR_IPRIORITYR register */
	regs_num = ppi_regs_num << 3;
	for (i = 0U; i < regs_num; ++i) {
		RESTORE_GICR_REG(gicr_base, rdist_ctx, ipriorityr, i);
	}
683

684
685
686
687
	/* 16 interrupt IDs per GICR_ICFGR register */
	regs_num = ppi_regs_num << 1;
	for (i = 0U; i < regs_num; ++i) {
		RESTORE_GICR_REG(gicr_base, rdist_ctx, icfgr, i);
688
689
690
691
	}

	gicr_write_nsacr(gicr_base, rdist_ctx->gicr_nsacr);

692
693
694
695
696
697
698
	/* Restore after group and priorities are set.
	 * 32 interrupt IDs per register
	 */
	for (i = 0U; i < ppi_regs_num; ++i) {
		RESTORE_GICR_REG(gicr_base, rdist_ctx, ispendr, i);
		RESTORE_GICR_REG(gicr_base, rdist_ctx, isactiver, i);
	}
699
700
701

	/*
	 * Wait for all writes to the Distributor to complete before enabling
702
	 * the SGI and (E)PPIs.
703
704
	 */
	gicr_wait_for_upstream_pending_write(gicr_base);
705
706
707
708
709

	/* 32 interrupt IDs per GICR_ISENABLER register */
	for (i = 0U; i < ppi_regs_num; ++i) {
		RESTORE_GICR_REG(gicr_base, rdist_ctx, isenabler, i);
	}
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726

	/*
	 * 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)
{
727
728
729
730
	unsigned int typer_reg, num_ints;
#if GIC_EXT_INTID
	unsigned int num_eints;
#endif
731

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
732
733
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
734
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
735
	assert(dist_ctx != NULL);
736
737
738

	uintptr_t gicd_base = gicv3_driver_data->gicd_base;

739
740
741
742
	typer_reg = gicd_read_typer(gicd_base);

	/* Maximum SPI INTID is 32 * (GICD_TYPER.ITLinesNumber + 1) - 1 */
	num_ints = ((typer_reg & TYPER_IT_LINES_NO_MASK) + 1U) << 5;
743

744
	/* Filter out special INTIDs 1020-1023 */
745
	if (num_ints > (MAX_SPI_ID + 1U)) {
746
		num_ints = MAX_SPI_ID + 1U;
747
	}
748

749
750
751
752
753
754
755
756
757
758
759
760
#if GIC_EXT_INTID
	/* Check if extended SPI range is implemented */
	if ((typer_reg & TYPER_ESPI) != 0U) {
		/*
		 * Maximum ESPI INTID is 32 * (GICD_TYPER.ESPI_range + 1) + 4095
		 */
		num_eints = ((((typer_reg >> TYPER_ESPI_RANGE_SHIFT) &
			TYPER_ESPI_RANGE_MASK) + 1U) << 5) + MIN_ESPI_ID - 1;
	} else {
		num_eints = 0U;
	}
#endif
761
762
763
764
765
766
	/* 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);

767
	/* Save GICD_IGROUPR for INTIDs 32 - 1019 */
768
769
770
771
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUP);

	/* Save GICD_IGROUPRE for INTIDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igroupr, IGROUP);
772

773
	/* Save GICD_ISENABLER for INT_IDs 32 - 1019 */
774
775
776
777
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLE);

	/* Save GICD_ISENABLERE for INT_IDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isenabler, ISENABLE);
778

779
	/* Save GICD_ISPENDR for INTIDs 32 - 1019 */
780
781
782
783
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPEND);

	/* Save GICD_ISPENDRE for INTIDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints,	ispendr, ISPEND);
784

785
	/* Save GICD_ISACTIVER for INTIDs 32 - 1019 */
786
787
788
789
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVE);

	/* Save GICD_ISACTIVERE for INTIDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isactiver, ISACTIVE);
790

791
	/* Save GICD_IPRIORITYR for INTIDs 32 - 1019 */
792
793
794
795
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITY);

	/* Save GICD_IPRIORITYRE for INTIDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ipriorityr, IPRIORITY);
796

797
	/* Save GICD_ICFGR for INTIDs 32 - 1019 */
798
799
800
801
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFG);

	/* Save GICD_ICFGRE for INTIDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, icfgr, ICFG);
802

803
	/* Save GICD_IGRPMODR for INTIDs 32 - 1019 */
804
805
806
807
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMOD);

	/* Save GICD_IGRPMODRE for INTIDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igrpmodr, IGRPMOD);
808

809
	/* Save GICD_NSACR for INTIDs 32 - 1019 */
810
811
812
813
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSAC);

	/* Save GICD_NSACRE for INTIDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, nsacr, NSAC);
814

815
	/* Save GICD_IROUTER for INTIDs 32 - 1019 */
816
817
818
819
	SAVE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTE);

	/* Save GICD_IROUTERE for INTIDs 4096 - 5119 */
	SAVE_GICD_EREGS(gicd_base, dist_ctx, num_eints, irouter, IROUTE);
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836

	/*
	 * 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)
{
837
838
839
840
	unsigned int typer_reg, num_ints;
#if GIC_EXT_INTID
	unsigned int num_eints;
#endif
841

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
842
843
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
844
	assert(IS_IN_EL3());
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
845
	assert(dist_ctx != NULL);
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862

	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);

863
864
865
866
	typer_reg = gicd_read_typer(gicd_base);

	/* Maximum SPI INTID is 32 * (GICD_TYPER.ITLinesNumber + 1) - 1 */
	num_ints = ((typer_reg & TYPER_IT_LINES_NO_MASK) + 1U) << 5;
867

868
	/* Filter out special INTIDs 1020-1023 */
869
	if (num_ints > (MAX_SPI_ID + 1U)) {
870
		num_ints = MAX_SPI_ID + 1U;
871
	}
872

873
874
875
876
877
878
879
880
881
882
883
884
#if GIC_EXT_INTID
	/* Check if extended SPI range is implemented */
	if ((typer_reg & TYPER_ESPI) != 0U) {
		/*
		 * Maximum ESPI INTID is 32 * (GICD_TYPER.ESPI_range + 1) + 4095
		 */
		num_eints = ((((typer_reg >> TYPER_ESPI_RANGE_SHIFT) &
			TYPER_ESPI_RANGE_MASK) + 1U) << 5) + MIN_ESPI_ID - 1;
	} else {
		num_eints = 0U;
	}
#endif
885
	/* Restore GICD_IGROUPR for INTIDs 32 - 1019 */
886
887
888
889
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igroupr, IGROUP);

	/* Restore GICD_IGROUPRE for INTIDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igroupr, IGROUP);
890

891
	/* Restore GICD_IPRIORITYR for INTIDs 32 - 1019 */
892
893
894
895
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ipriorityr, IPRIORITY);

	/* Restore GICD_IPRIORITYRE for INTIDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ipriorityr, IPRIORITY);
896

897
	/* Restore GICD_ICFGR for INTIDs 32 - 1019 */
898
899
900
901
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, icfgr, ICFG);

	/* Restore GICD_ICFGRE for INTIDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, icfgr, ICFG);
902

903
	/* Restore GICD_IGRPMODR for INTIDs 32 - 1019 */
904
905
906
907
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, igrpmodr, IGRPMOD);

	/* Restore GICD_IGRPMODRE for INTIDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, igrpmodr, IGRPMOD);
908

909
	/* Restore GICD_NSACR for INTIDs 32 - 1019 */
910
911
912
913
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, nsacr, NSAC);

	/* Restore GICD_NSACRE for INTIDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, nsacr, NSAC);
914

915
	/* Restore GICD_IROUTER for INTIDs 32 - 1019 */
916
917
918
919
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, irouter, IROUTE);

	/* Restore GICD_IROUTERE for INTIDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, irouter, IROUTE);
920
921

	/*
922
923
	 * Restore ISENABLER(E), ISPENDR(E) and ISACTIVER(E) after
	 * the interrupts are configured.
924
925
	 */

926
	/* Restore GICD_ISENABLER for INT_IDs 32 - 1019 */
927
928
929
930
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isenabler, ISENABLE);

	/* Restore GICD_ISENABLERE for INT_IDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isenabler, ISENABLE);
931

932
	/* Restore GICD_ISPENDR for INTIDs 32 - 1019 */
933
934
935
936
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, ispendr, ISPEND);

	/* Restore GICD_ISPENDRE for INTIDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, ispendr, ISPEND);
937

938
	/* Restore GICD_ISACTIVER for INTIDs 32 - 1019 */
939
940
941
942
	RESTORE_GICD_REGS(gicd_base, dist_ctx, num_ints, isactiver, ISACTIVE);

	/* Restore GICD_ISACTIVERE for INTIDs 4096 - 5119 */
	RESTORE_GICD_EREGS(gicd_base, dist_ctx, num_eints, isactiver, ISACTIVE);
943
944
945
946
947

	/* Restore the GICD_CTLR */
	gicd_write_ctlr(gicd_base, dist_ctx->gicd_ctlr);
	gicd_wait_for_pending_write(gicd_base);
}
948
949
950
951
952
953
954

/*******************************************************************************
 * 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
955
	return (unsigned int)read_icc_rpr_el1();
956
}
957
958
959
960

/*******************************************************************************
 * 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
961
 * interrupt is SGI or (E)PPI and programs the corresponding Redistributor
962
963
964
965
 * interface.
 ******************************************************************************/
unsigned int gicv3_get_interrupt_active(unsigned int id, unsigned int proc_num)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
966
967
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
968
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
969
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
970

971
972
973
974
975
	/* Check interrupt ID */
	if (is_sgi_ppi(id)) {
		/* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
		return gicr_get_isactiver(
			gicv3_driver_data->rdistif_base_addrs[proc_num], id);
976
977
	}

978
979
	/* For SPIs: 32-1019 and ESPIs: 4096-5119 */
	return gicd_get_isactiver(gicv3_driver_data->gicd_base, id);
980
}
981
982
983
984
985
986
987
988

/*******************************************************************************
 * 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
989
990
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
991
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
992
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
993
994
995
996
997
998

	/*
	 * Ensure that any shared variable updates depending on out of band
	 * interrupt trigger are observed before enabling interrupt.
	 */
	dsbishst();
999
1000
1001
1002
1003
1004

	/* Check interrupt ID */
	if (is_sgi_ppi(id)) {
		/* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
		gicr_set_isenabler(
			gicv3_driver_data->rdistif_base_addrs[proc_num], id);
1005
	} else {
1006
		/* For SPIs: 32-1019 and ESPIs: 4096-5119 */
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
		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
1018
1019
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1020
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1021
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
1022
1023
1024
1025
1026

	/*
	 * Disable interrupt, and ensure that any shared variable updates
	 * depending on out of band interrupt trigger are observed afterwards.
	 */
1027
1028
1029
1030
1031
1032

	/* Check interrupt ID */
	if (is_sgi_ppi(id)) {
		/* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
		gicr_set_icenabler(
			gicv3_driver_data->rdistif_base_addrs[proc_num], id);
1033
1034
1035

		/* Write to clear enable requires waiting for pending writes */
		gicr_wait_for_pending_write(
1036
			gicv3_driver_data->rdistif_base_addrs[proc_num]);
1037
	} else {
1038
		/* For SPIs: 32-1019 and ESPIs: 4096-5119 */
1039
1040
1041
1042
1043
1044
1045
1046
		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();
}
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056

/*******************************************************************************
 * 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
1057
1058
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1059
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1060
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
1061

1062
1063
1064
	/* Check interrupt ID */
	if (is_sgi_ppi(id)) {
		/* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
1065
1066
1067
		gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
		gicr_set_ipriorityr(gicr_base, id, priority);
	} else {
1068
		/* For SPIs: 32-1019 and ESPIs: 4096-5119 */
1069
1070
1071
		gicd_set_ipriorityr(gicv3_driver_data->gicd_base, id, priority);
	}
}
1072
1073
1074

/*******************************************************************************
 * This function assigns group for the interrupt identified by id. The proc_num
1075
 * is used if the interrupt is SGI or (E)PPI, and programs the corresponding
1076
1077
1078
1079
1080
 * 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
1081
	bool igroup = false, grpmod = false;
1082
1083
	uintptr_t gicr_base;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1084
1085
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1086
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1087
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
1088
1089
1090

	switch (type) {
	case INTR_GROUP1S:
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1091
1092
		igroup = false;
		grpmod = true;
1093
1094
		break;
	case INTR_GROUP0:
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1095
1096
		igroup = false;
		grpmod = false;
1097
1098
		break;
	case INTR_GROUP1NS:
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1099
1100
		igroup = true;
		grpmod = false;
1101
1102
		break;
	default:
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1103
		assert(false);
1104
		break;
1105
1106
	}

1107
1108
1109
	/* Check interrupt ID */
	if (is_sgi_ppi(id)) {
		/* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
1110
		gicr_base = gicv3_driver_data->rdistif_base_addrs[proc_num];
1111
1112
1113
1114
1115

		igroup ? gicr_set_igroupr(gicr_base, id) :
			 gicr_clr_igroupr(gicr_base, id);
		grpmod ? gicr_set_igrpmodr(gicr_base, id) :
			 gicr_clr_igrpmodr(gicr_base, id);
1116
	} else {
1117
1118
		/* For SPIs: 32-1019 and ESPIs: 4096-5119 */

1119
1120
		/* Serialize read-modify-write to Distributor registers */
		spin_lock(&gic_lock);
1121
1122
1123
1124
1125
1126

		igroup ? gicd_set_igroupr(gicv3_driver_data->gicd_base, id) :
			 gicd_clr_igroupr(gicv3_driver_data->gicd_base, id);
		grpmod ? gicd_set_igrpmodr(gicv3_driver_data->gicd_base, id) :
			 gicd_clr_igrpmodr(gicv3_driver_data->gicd_base, id);

1127
1128
1129
		spin_unlock(&gic_lock);
	}
}
1130
1131
1132
1133
1134
1135

/*******************************************************************************
 * 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
1136
void gicv3_raise_secure_g0_sgi(unsigned int sgi_num, u_register_t target)
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
{
	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
1155
	tgt = BIT_32(aff0);
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168

	/* 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();
}
1169
1170

/*******************************************************************************
1171
 * This function sets the interrupt routing for the given (E)SPI interrupt id.
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
 * 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
1186
1187
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1188
1189

	assert((irm == GICV3_IRM_ANY) || (irm == GICV3_IRM_PE));
1190
1191

	assert(IS_SPI(id));
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201

	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
1202
		if (((router >> IROUTER_IRM_SHIFT) & IROUTER_IRM_MASK) == 0U) {
1203
1204
1205
1206
1207
			ERROR("GICv3 implementation doesn't support routing ANY\n");
			panic();
		}
	}
}
1208
1209
1210

/*******************************************************************************
 * This function clears the pending status of an interrupt identified by id.
1211
 * The proc_num is used if the interrupt is SGI or (E)PPI, and programs the
1212
1213
1214
1215
 * corresponding Redistributor interface.
 ******************************************************************************/
void gicv3_clear_interrupt_pending(unsigned int id, unsigned int proc_num)
{
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1216
1217
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1218
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1219
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
1220
1221
1222
1223
1224

	/*
	 * Clear pending interrupt, and ensure that any shared variable updates
	 * depending on out of band interrupt trigger are observed afterwards.
	 */
1225
1226
1227
1228
1229
1230

	/* Check interrupt ID */
	if (is_sgi_ppi(id)) {
		/* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
		gicr_set_icpendr(
			gicv3_driver_data->rdistif_base_addrs[proc_num], id);
1231
	} else {
1232
		/* For SPIs: 32-1019 and ESPIs: 4096-5119 */
1233
1234
		gicd_set_icpendr(gicv3_driver_data->gicd_base, id);
	}
1235

1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
	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
1246
1247
	assert(gicv3_driver_data != NULL);
	assert(gicv3_driver_data->gicd_base != 0U);
1248
	assert(proc_num < gicv3_driver_data->rdistif_num);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
1249
	assert(gicv3_driver_data->rdistif_base_addrs != NULL);
1250
1251
1252
1253
1254
1255

	/*
	 * Ensure that any shared variable updates depending on out of band
	 * interrupt trigger are observed before setting interrupt pending.
	 */
	dsbishst();
1256
1257
1258
1259
1260
1261

	/* Check interrupt ID */
	if (is_sgi_ppi(id)) {
		/* For SGIs: 0-15, PPIs: 16-31 and EPPIs: 1056-1119 */
		gicr_set_ispendr(
			gicv3_driver_data->rdistif_base_addrs[proc_num], id);
1262
	} else {
1263
		/* For SPIs: 32-1019 and ESPIs: 4096-5119 */
1264
1265
1266
		gicd_set_ispendr(gicv3_driver_data->gicd_base, id);
	}
}
1267
1268
1269
1270
1271
1272
1273
1274
1275

/*******************************************************************************
 * 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;

1276
	old_mask = (unsigned int)read_icc_pmr_el1();
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288

	/*
	 * 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;
}
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322

/*******************************************************************************
 * This function delegates the responsibility of discovering the corresponding
 * Redistributor frames to each CPU itself. It is a modified version of
 * gicv3_rdistif_base_addrs_probe() and is executed by each CPU in the platform
 * unlike the previous way in which only the Primary CPU did the discovery of
 * all the Redistributor frames for every CPU. It also handles the scenario in
 * which the frames of various CPUs are not contiguous in physical memory.
 ******************************************************************************/
int gicv3_rdistif_probe(const uintptr_t gicr_frame)
{
	u_register_t mpidr;
	unsigned int proc_num, proc_self;
	uint64_t typer_val;
	uintptr_t rdistif_base;
	bool gicr_frame_found = false;

	assert(gicv3_driver_data->gicr_base == 0U);

	/* Ensure this function is called with Data Cache enabled */
#ifndef __aarch64__
	assert((read_sctlr() & SCTLR_C_BIT) != 0U);
#else
	assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U);
#endif /* !__aarch64__ */

	proc_self = gicv3_driver_data->mpidr_to_core_pos(read_mpidr_el1());
	rdistif_base = gicr_frame;
	do {
		typer_val = gicr_read_typer(rdistif_base);
		if (gicv3_driver_data->mpidr_to_core_pos != NULL) {
			mpidr = mpidr_from_gicr_typer(typer_val);
			proc_num = gicv3_driver_data->mpidr_to_core_pos(mpidr);
		} else {
1323
1324
			proc_num = (unsigned int)(typer_val >>
				TYPER_PROC_NUM_SHIFT) & TYPER_PROC_NUM_MASK;
1325
1326
1327
1328
1329
		}
		if (proc_num == proc_self) {
			/* The base address doesn't need to be initialized on
			 * every warm boot.
			 */
1330
1331
			if (gicv3_driver_data->rdistif_base_addrs[proc_num]
								!= 0U) {
1332
				return 0;
1333
			}
1334
1335
1336
1337
1338
1339
1340
1341
			gicv3_driver_data->rdistif_base_addrs[proc_num] =
			rdistif_base;
			gicr_frame_found = true;
			break;
		}
		rdistif_base += (uintptr_t)(ULL(1) << GICR_PCPUBASE_SHIFT);
	} while ((typer_val & TYPER_LAST_BIT) == 0U);

1342
	if (!gicr_frame_found) {
1343
		return -1;
1344
	}
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359

	/*
	 * Flush the driver data to ensure coherency. This is
	 * not required if platform has HW_ASSISTED_COHERENCY
	 * enabled.
	 */
#if !HW_ASSISTED_COHERENCY
	/*
	 * Flush the rdistif_base_addrs[] contents linked to the GICv3 driver.
	 */
	flush_dcache_range((uintptr_t)&(gicv3_driver_data->rdistif_base_addrs[proc_num]),
		sizeof(*(gicv3_driver_data->rdistif_base_addrs)));
#endif
	return 0; /* Found matching GICR frame */
}
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379

/******************************************************************************
 * This function checks the interrupt ID and returns true for SGIs and (E)PPIs
 * and false for (E)SPIs IDs.
 *****************************************************************************/
static bool is_sgi_ppi(unsigned int id)
{
	/* SGIs: 0-15, PPIs: 16-31, EPPIs: 1056-1119 */
	if (IS_SGI_PPI(id)) {
		return true;
	}

	/* SPIs: 32-1019, ESPIs: 4096-5119 */
	if (IS_SPI(id)) {
		return false;
	}

	assert(false);
	panic();
}