gicv3_helpers.c 8.46 KB
Newer Older
1
/*
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
#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 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
34
	while ((gicr_read_waker(gicr_base) & WAKER_CA_BIT) != 0U)
35
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 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
48
	while ((gicr_read_waker(gicr_base) & WAKER_CA_BIT) == 0U)
49
50
51
52
53
54
55
56
57
58
59
60
61
62
		;
}


/*******************************************************************************
 * 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)
{
63
	u_register_t mpidr;
64
	unsigned int proc_num;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
65
	uint64_t typer_val;
66
67
	uintptr_t rdistif_base = gicr_base;

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

	/*
	 * 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
79
		if (mpidr_to_core_pos != NULL) {
80
81
82
83
84
85
			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;
		}
86
87
88
89

		if (proc_num < rdistif_num)
			rdistif_base_addrs[proc_num] = rdistif_base;

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
}

/*******************************************************************************
 * Helper function to configure the default attributes of SPIs.
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
97
void gicv3_spis_config_defaults(uintptr_t gicd_base)
98
99
100
101
102
{
	unsigned int index, num_ints;

	num_ints = gicd_read_typer(gicd_base);
	num_ints &= TYPER_IT_LINES_NO_MASK;
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
103
	num_ints = (num_ints + 1U) << 5;
104
105
106
107
108

	/*
	 * Treat all SPIs as G1NS by default. The number of interrupts is
	 * calculated as 32 * (IT_LINES + 1). We do 32 at a time.
	 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
109
	for (index = MIN_SPI_ID; index < num_ints; index += 32U)
110
111
112
		gicd_write_igroupr(gicd_base, index, ~0U);

	/* Setup the default SPI priorities doing four at a time */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
113
	for (index = MIN_SPI_ID; index < num_ints; index += 4U)
114
115
116
117
118
119
120
121
		gicd_write_ipriorityr(gicd_base,
				      index,
				      GICD_IPRIORITYR_DEF_VAL);

	/*
	 * Treat all SPIs as level triggered by default, write 16 at
	 * a time
	 */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
122
123
	for (index = MIN_SPI_ID; index < num_ints; index += 16U)
		gicd_write_icfgr(gicd_base, index, 0U);
124
125
}

126
127
128
/*******************************************************************************
 * Helper function to configure properties of secure SPIs
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
129
unsigned int gicv3_secure_spis_config_props(uintptr_t gicd_base,
130
131
132
133
134
135
		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
136
	unsigned int ctlr_enable = 0U;
137
138

	/* Make sure there's a valid property array */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
139
140
	if (interrupt_props_num > 0U)
		assert(interrupt_props != NULL);
141

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
142
	for (i = 0U; i < interrupt_props_num; i++) {
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
		current_prop = &interrupt_props[i];

		if (current_prop->intr_num < MIN_SPI_ID)
			continue;

		/* Configure this interrupt as a secure interrupt */
		gicd_clr_igroupr(gicd_base, current_prop->intr_num);

		/* Configure this interrupt as G0 or a G1S interrupt */
		assert((current_prop->intr_grp == INTR_GROUP0) ||
				(current_prop->intr_grp == INTR_GROUP1S));
		if (current_prop->intr_grp == INTR_GROUP1S) {
			gicd_set_igrpmodr(gicd_base, current_prop->intr_num);
			ctlr_enable |= CTLR_ENABLE_G1S_BIT;
		} else {
			gicd_clr_igrpmodr(gicd_base, current_prop->intr_num);
			ctlr_enable |= CTLR_ENABLE_G0_BIT;
		}

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

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

		/* Target SPIs to the primary CPU */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
171
172
		gic_affinity_val =
			gicd_irouter_val_from_mpidr(read_mpidr(), 0U);
173
174
175
176
177
178
179
180
181
		gicd_write_irouter(gicd_base, current_prop->intr_num,
				gic_affinity_val);

		/* Enable this interrupt */
		gicd_set_isenabler(gicd_base, current_prop->intr_num);
	}

	return ctlr_enable;
}
182
183
184
185

/*******************************************************************************
 * Helper function to configure the default attributes of SPIs.
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
186
void gicv3_ppi_sgi_config_defaults(uintptr_t gicr_base)
187
188
189
190
191
192
193
194
{
	unsigned int index;

	/*
	 * 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
195
	gicr_write_icenabler0(gicr_base, ~0U);
196
197
198
199
200
201
	gicr_wait_for_pending_write(gicr_base);

	/* Treat all SGIs/PPIs as G1NS by default. */
	gicr_write_igroupr0(gicr_base, ~0U);

	/* Setup the default PPI/SGI priorities doing four at a time */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
202
	for (index = 0U; index < MIN_SPI_ID; index += 4U)
203
204
205
206
207
		gicr_write_ipriorityr(gicr_base,
				      index,
				      GICD_IPRIORITYR_DEF_VAL);

	/* Configure all PPIs as level triggered by default */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
208
	gicr_write_icfgr1(gicr_base, 0U);
209
210
}

211
212
213
/*******************************************************************************
 * Helper function to configure properties of secure G0 and G1S PPIs and SGIs.
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
214
unsigned int gicv3_secure_ppi_sgi_config_props(uintptr_t gicr_base,
215
216
217
218
219
		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
220
	unsigned int ctlr_enable = 0U;
221
222

	/* Make sure there's a valid property array */
Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
223
224
	if (interrupt_props_num > 0U)
		assert(interrupt_props != NULL);
225

Antonio Nino Diaz's avatar
Antonio Nino Diaz committed
226
	for (i = 0U; i < interrupt_props_num; i++) {
227
228
229
230
231
232
233
234
235
236
237
		current_prop = &interrupt_props[i];

		if (current_prop->intr_num >= MIN_SPI_ID)
			continue;

		/* Configure this interrupt as a secure interrupt */
		gicr_clr_igroupr0(gicr_base, current_prop->intr_num);

		/* Configure this interrupt as G0 or a G1S interrupt */
		assert((current_prop->intr_grp == INTR_GROUP0) ||
				(current_prop->intr_grp == INTR_GROUP1S));
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
238
		if (current_prop->intr_grp == INTR_GROUP1S) {
239
			gicr_set_igrpmodr0(gicr_base, current_prop->intr_num);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
240
241
			ctlr_enable |= CTLR_ENABLE_G1S_BIT;
		} else {
242
			gicr_clr_igrpmodr0(gicr_base, current_prop->intr_num);
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
243
244
			ctlr_enable |= CTLR_ENABLE_G0_BIT;
		}
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

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

		/*
		 * Set interrupt configuration for PPIs. Configuration for SGIs
		 * are ignored.
		 */
		if ((current_prop->intr_num >= MIN_PPI_ID) &&
				(current_prop->intr_num < MIN_SPI_ID)) {
			gicr_set_icfgr1(gicr_base, current_prop->intr_num,
					current_prop->intr_cfg);
		}

		/* Enable this interrupt */
		gicr_set_isenabler0(gicr_base, current_prop->intr_num);
	}
Jeenu Viswambharan's avatar
Jeenu Viswambharan committed
263
264

	return ctlr_enable;
265
}