gicv3_helpers.c 12.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2021, 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
#include <common/debug.h>
#include <common/interrupt_props.h>
#include <drivers/arm/gic_common.h>

15
#include "../common/gic_common_private.h"
16
17
18
19
20
21
22
23
24
25
26
27
#include "gicv3_private.h"

/******************************************************************************
 * This function marks the core as awake in the re-distributor and
 * ensures that the interface is active.
 *****************************************************************************/
void gicv3_rdistif_mark_core_awake(uintptr_t gicr_base)
{
	/*
	 * The WAKER_PS_BIT should be changed to 0
	 * only when WAKER_CA_BIT is 1.
	 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
28
	assert((gicr_read_waker(gicr_base) & WAKER_CA_BIT) != 0U);
29
30
31
32
33

	/* Mark the connected core as awake */
	gicr_write_waker(gicr_base, gicr_read_waker(gicr_base) & ~WAKER_PS_BIT);

	/* Wait till the WAKER_CA_BIT changes to 0 */
34
35
	while ((gicr_read_waker(gicr_base) & WAKER_CA_BIT) != 0U) {
	}
36
37
38
39
40
41
42
43
44
45
46
47
}

/******************************************************************************
 * This function marks the core as asleep in the re-distributor and ensures
 * that the interface is quiescent.
 *****************************************************************************/
void gicv3_rdistif_mark_core_asleep(uintptr_t gicr_base)
{
	/* Mark the connected core as asleep */
	gicr_write_waker(gicr_base, gicr_read_waker(gicr_base) | WAKER_PS_BIT);

	/* Wait till the WAKER_CA_BIT changes to 1 */
48
49
	while ((gicr_read_waker(gicr_base) & WAKER_CA_BIT) == 0U) {
	}
50
51
52
53
54
55
56
57
58
59
60
61
}

/*******************************************************************************
 * This function probes the Redistributor frames when the driver is initialised
 * and saves their base addresses. These base addresses are used later to
 * initialise each Redistributor interface.
 ******************************************************************************/
void gicv3_rdistif_base_addrs_probe(uintptr_t *rdistif_base_addrs,
					unsigned int rdistif_num,
					uintptr_t gicr_base,
					mpidr_hash_fn mpidr_to_core_pos)
{
62
	u_register_t mpidr;
63
	unsigned int proc_num;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
64
	uint64_t typer_val;
65
66
	uintptr_t rdistif_base = gicr_base;

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
67
	assert(rdistif_base_addrs != NULL);
68
69
70
71
72
73
74
75
76
77

	/*
	 * Iterate over the Redistributor frames. Store the base address of each
	 * frame in the platform provided array. Use the "Processor Number"
	 * field to index into the array if the platform has not provided a hash
	 * function to convert an MPIDR (obtained from the "Affinity Value"
	 * field into a linear index.
	 */
	do {
		typer_val = gicr_read_typer(rdistif_base);
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
78
		if (mpidr_to_core_pos != NULL) {
79
80
81
82
83
84
			mpidr = mpidr_from_gicr_typer(typer_val);
			proc_num = mpidr_to_core_pos(mpidr);
		} else {
			proc_num = (typer_val >> TYPER_PROC_NUM_SHIFT) &
				TYPER_PROC_NUM_MASK;
		}
85

86
		if (proc_num < rdistif_num) {
87
			rdistif_base_addrs[proc_num] = rdistif_base;
88
		}
89

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
90
91
		rdistif_base += (1U << GICR_PCPUBASE_SHIFT);
	} while ((typer_val & TYPER_LAST_BIT) == 0U);
92
93
}

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * Helper function to get the maximum SPI INTID + 1.
 ******************************************************************************/
unsigned int gicv3_get_spi_limit(uintptr_t gicd_base)
{
	unsigned int spi_limit;
	unsigned int typer_reg = gicd_read_typer(gicd_base);

	/* (maximum SPI INTID + 1) is equal to 32 * (GICD_TYPER.ITLinesNumber+1) */
	spi_limit = ((typer_reg & TYPER_IT_LINES_NO_MASK) + 1U) << 5;

	/* Filter out special INTIDs 1020-1023 */
	if (spi_limit > (MAX_SPI_ID + 1U)) {
		return MAX_SPI_ID + 1U;
	}

	return spi_limit;
}

113
/*******************************************************************************
114
 * Helper function to configure the default attributes of (E)SPIs.
115
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
116
void gicv3_spis_config_defaults(uintptr_t gicd_base)
117
{
118
119
120
121
122
123
124
125
126
	unsigned int i, num_ints;
#if GIC_EXT_INTID
	unsigned int num_eints;
#endif
	unsigned int 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;

127
128
129
130
131
132
133
134
	/*
	 * The GICv3 architecture allows GICD_TYPER.ITLinesNumber to be 31, so
	 * the maximum possible value for num_ints is 1024. Limit the value to
	 * MAX_SPI_ID + 1 to avoid getting wrong address in GICD_OFFSET() macro.
	 */
	if (num_ints > MAX_SPI_ID + 1U) {
		num_ints = MAX_SPI_ID + 1U;
	}
135
	INFO("Maximum SPI INTID supported: %u\n", num_ints - 1);
136

137
138
139
140
	/* Treat all (E)SPIs as G1NS by default. We do 32 at a time. */
	for (i = MIN_SPI_ID; i < num_ints; i += (1U << IGROUPR_SHIFT)) {
		gicd_write_igroupr(gicd_base, i, ~0U);
	}
141

142
143
144
145
146
147
148
#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) &
149
			TYPER_ESPI_RANGE_MASK) + 1U) << 5) + MIN_ESPI_ID;
150
		INFO("Maximum ESPI INTID supported: %u\n", num_eints - 1);
151

152
153
154
155
156
157
		for (i = MIN_ESPI_ID; i < num_eints;
					i += (1U << IGROUPR_SHIFT)) {
			gicd_write_igroupr(gicd_base, i, ~0U);
		}
	} else {
		num_eints = 0U;
158
		INFO("ESPI range is not implemented.\n");
159
160
	}
#endif
161

162
163
164
165
	/* Setup the default (E)SPI priorities doing four at a time */
	for (i = MIN_SPI_ID; i < num_ints; i += (1U << IPRIORITYR_SHIFT)) {
		gicd_write_ipriorityr(gicd_base, i, GICD_IPRIORITYR_DEF_VAL);
	}
166

167
168
169
170
171
172
#if GIC_EXT_INTID
	for (i = MIN_ESPI_ID; i < num_eints;
					i += (1U << IPRIORITYR_SHIFT)) {
		gicd_write_ipriorityr(gicd_base, i, GICD_IPRIORITYR_DEF_VAL);
	}
#endif
173
	/*
174
	 * Treat all (E)SPIs as level triggered by default, write 16 at a time
175
	 */
176
177
178
179
180
181
182
183
184
	for (i = MIN_SPI_ID; i < num_ints; i += (1U << ICFGR_SHIFT)) {
		gicd_write_icfgr(gicd_base, i, 0U);
	}

#if GIC_EXT_INTID
	for (i = MIN_ESPI_ID; i < num_eints; i += (1U << ICFGR_SHIFT)) {
		gicd_write_icfgr(gicd_base, i, 0U);
	}
#endif
185
186
}

187
/*******************************************************************************
188
 * Helper function to configure properties of secure (E)SPIs
189
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
190
unsigned int gicv3_secure_spis_config_props(uintptr_t gicd_base,
191
192
193
194
195
196
		const interrupt_prop_t *interrupt_props,
		unsigned int interrupt_props_num)
{
	unsigned int i;
	const interrupt_prop_t *current_prop;
	unsigned long long gic_affinity_val;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
197
	unsigned int ctlr_enable = 0U;
198
199

	/* Make sure there's a valid property array */
200
	if (interrupt_props_num > 0U) {
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
201
		assert(interrupt_props != NULL);
202
	}
203

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
204
	for (i = 0U; i < interrupt_props_num; i++) {
205
206
		current_prop = &interrupt_props[i];

207
208
209
210
		unsigned int intr_num = current_prop->intr_num;

		/* Skip SGI, (E)PPI and LPI interrupts */
		if (!IS_SPI(intr_num)) {
211
			continue;
212
		}
213
214

		/* Configure this interrupt as a secure interrupt */
215
		gicd_clr_igroupr(gicd_base, intr_num);
216
217
218
219

		/* Configure this interrupt as G0 or a G1S interrupt */
		assert((current_prop->intr_grp == INTR_GROUP0) ||
				(current_prop->intr_grp == INTR_GROUP1S));
220

221
		if (current_prop->intr_grp == INTR_GROUP1S) {
222
			gicd_set_igrpmodr(gicd_base, intr_num);
223
224
			ctlr_enable |= CTLR_ENABLE_G1S_BIT;
		} else {
225
			gicd_clr_igrpmodr(gicd_base, intr_num);
226
227
228
229
			ctlr_enable |= CTLR_ENABLE_G0_BIT;
		}

		/* Set interrupt configuration */
230
		gicd_set_icfgr(gicd_base, intr_num, current_prop->intr_cfg);
231
232

		/* Set the priority of this interrupt */
233
234
		gicd_set_ipriorityr(gicd_base, intr_num,
					current_prop->intr_pri);
235

236
		/* Target (E)SPIs to the primary CPU */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
237
238
		gic_affinity_val =
			gicd_irouter_val_from_mpidr(read_mpidr(), 0U);
239
240
		gicd_write_irouter(gicd_base, intr_num,
					gic_affinity_val);
241
242

		/* Enable this interrupt */
243
		gicd_set_isenabler(gicd_base, intr_num);
244
245
246
247
	}

	return ctlr_enable;
}
248
249

/*******************************************************************************
250
 * Helper function to configure the default attributes of (E)SPIs
251
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
252
void gicv3_ppi_sgi_config_defaults(uintptr_t gicr_base)
253
{
254
255
256
257
258
259
260
261
262
263
264
265
266
	unsigned int i, ppi_regs_num, regs_num;

#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
267
	/*
268
269
270
	 * 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.
271
	 */
272
273
274
275
276
	for (i = 0U; i < ppi_regs_num; ++i) {
		gicr_write_icenabler(gicr_base, i, ~0U);
	}

	/* Wait for pending writes to GICR_ICENABLER */
277
278
	gicr_wait_for_pending_write(gicr_base);

279
280
281
282
283
	/* 32 interrupt IDs per GICR_IGROUPR register */
	for (i = 0U; i < ppi_regs_num; ++i) {
		/* Treat all SGIs/(E)PPIs as G1NS by default */
		gicr_write_igroupr(gicr_base, i, ~0U);
	}
284

285
286
287
288
289
290
	/* 4 interrupt IDs per GICR_IPRIORITYR register */
	regs_num = ppi_regs_num << 3;
	for (i = 0U; i < regs_num; ++i) {
		/* Setup the default (E)PPI/SGI priorities doing 4 at a time */
		gicr_write_ipriorityr(gicr_base, i, GICD_IPRIORITYR_DEF_VAL);
	}
291

292
293
294
295
296
297
	/* 16 interrupt IDs per GICR_ICFGR register */
	regs_num = ppi_regs_num << 1;
	for (i = (MIN_PPI_ID >> ICFGR_SHIFT); i < regs_num; ++i) {
		/* Configure all (E)PPIs as level triggered by default */
		gicr_write_icfgr(gicr_base, i, 0U);
	}
298
299
}

300
/*******************************************************************************
301
 * Helper function to configure properties of secure G0 and G1S (E)PPIs and SGIs
302
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
303
unsigned int gicv3_secure_ppi_sgi_config_props(uintptr_t gicr_base,
304
305
306
307
308
		const interrupt_prop_t *interrupt_props,
		unsigned int interrupt_props_num)
{
	unsigned int i;
	const interrupt_prop_t *current_prop;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
309
	unsigned int ctlr_enable = 0U;
310
311

	/* Make sure there's a valid property array */
312
	if (interrupt_props_num > 0U) {
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
313
		assert(interrupt_props != NULL);
314
	}
315

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
316
	for (i = 0U; i < interrupt_props_num; i++) {
317
318
		current_prop = &interrupt_props[i];

319
320
321
322
		unsigned int intr_num = current_prop->intr_num;

		/* Skip (E)SPI interrupt */
		if (!IS_SGI_PPI(intr_num)) {
323
			continue;
324
		}
325
326

		/* Configure this interrupt as a secure interrupt */
327
		gicr_clr_igroupr(gicr_base, intr_num);
328
329
330

		/* Configure this interrupt as G0 or a G1S interrupt */
		assert((current_prop->intr_grp == INTR_GROUP0) ||
331
332
			(current_prop->intr_grp == INTR_GROUP1S));

Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
333
		if (current_prop->intr_grp == INTR_GROUP1S) {
334
			gicr_set_igrpmodr(gicr_base, intr_num);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
335
336
			ctlr_enable |= CTLR_ENABLE_G1S_BIT;
		} else {
337
			gicr_clr_igrpmodr(gicr_base, intr_num);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
338
339
			ctlr_enable |= CTLR_ENABLE_G0_BIT;
		}
340
341

		/* Set the priority of this interrupt */
342
343
		gicr_set_ipriorityr(gicr_base, intr_num,
					current_prop->intr_pri);
344
345

		/*
346
347
		 * Set interrupt configuration for (E)PPIs.
		 * Configurations for SGIs 0-15 are ignored.
348
		 */
349
350
		if (intr_num >= MIN_PPI_ID) {
			gicr_set_icfgr(gicr_base, intr_num,
351
352
353
354
					current_prop->intr_cfg);
		}

		/* Enable this interrupt */
355
		gicr_set_isenabler(gicr_base, intr_num);
356
	}
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
357
358

	return ctlr_enable;
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

/**
 * gicv3_rdistif_get_number_frames() - determine size of GICv3 GICR region
 * @gicr_frame: base address of the GICR region to check
 *
 * This iterates over the GICR_TYPER registers of multiple GICR frames in
 * a GICR region, to find the instance which has the LAST bit set. For most
 * systems this corresponds to the number of cores handled by a redistributor,
 * but there could be disabled cores among them.
 * It assumes that each GICR region is fully accessible (till the LAST bit
 * marks the end of the region).
 * If a platform has multiple GICR regions, this function would need to be
 * called multiple times, providing the respective GICR base address each time.
 *
 * Return: number of valid GICR frames (at least 1, up to PLATFORM_CORE_COUNT)
 ******************************************************************************/
unsigned int gicv3_rdistif_get_number_frames(const uintptr_t gicr_frame)
{
	uintptr_t rdistif_base = gicr_frame;
	unsigned int count;

	for (count = 1; count < PLATFORM_CORE_COUNT; count++) {
		if ((gicr_read_typer(rdistif_base) & TYPER_LAST_BIT) != 0U) {
			break;
		}
		rdistif_base += (1U << GICR_PCPUBASE_SHIFT);
	}

	return count;
}