gic_v2.c 8.24 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2016, 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
#include <arch.h>
8
#include <assert.h>
Dan Handley's avatar
Dan Handley committed
9
#include <gic_v2.h>
10
#include <interrupt_mgmt.h>
11
12
13
#include <mmio.h>

/*******************************************************************************
14
 * GIC Distributor interface accessors for reading entire registers
15
16
 ******************************************************************************/

17
unsigned int gicd_read_igroupr(uintptr_t base, unsigned int id)
18
19
20
21
22
{
	unsigned n = id >> IGROUPR_SHIFT;
	return mmio_read_32(base + GICD_IGROUPR + (n << 2));
}

23
unsigned int gicd_read_isenabler(uintptr_t base, unsigned int id)
24
25
26
27
28
{
	unsigned n = id >> ISENABLER_SHIFT;
	return mmio_read_32(base + GICD_ISENABLER + (n << 2));
}

29
unsigned int gicd_read_icenabler(uintptr_t base, unsigned int id)
30
31
32
33
34
{
	unsigned n = id >> ICENABLER_SHIFT;
	return mmio_read_32(base + GICD_ICENABLER + (n << 2));
}

35
unsigned int gicd_read_ispendr(uintptr_t base, unsigned int id)
36
37
38
39
40
{
	unsigned n = id >> ISPENDR_SHIFT;
	return mmio_read_32(base + GICD_ISPENDR + (n << 2));
}

41
unsigned int gicd_read_icpendr(uintptr_t base, unsigned int id)
42
43
44
45
46
{
	unsigned n = id >> ICPENDR_SHIFT;
	return mmio_read_32(base + GICD_ICPENDR + (n << 2));
}

47
unsigned int gicd_read_isactiver(uintptr_t base, unsigned int id)
48
49
50
51
52
{
	unsigned n = id >> ISACTIVER_SHIFT;
	return mmio_read_32(base + GICD_ISACTIVER + (n << 2));
}

53
unsigned int gicd_read_icactiver(uintptr_t base, unsigned int id)
54
55
56
57
58
{
	unsigned n = id >> ICACTIVER_SHIFT;
	return mmio_read_32(base + GICD_ICACTIVER + (n << 2));
}

59
unsigned int gicd_read_ipriorityr(uintptr_t base, unsigned int id)
60
61
62
63
64
{
	unsigned n = id >> IPRIORITYR_SHIFT;
	return mmio_read_32(base + GICD_IPRIORITYR + (n << 2));
}

65
unsigned int gicd_read_itargetsr(uintptr_t base, unsigned int id)
66
67
68
69
70
{
	unsigned n = id >> ITARGETSR_SHIFT;
	return mmio_read_32(base + GICD_ITARGETSR + (n << 2));
}

71
unsigned int gicd_read_icfgr(uintptr_t base, unsigned int id)
72
73
74
75
76
{
	unsigned n = id >> ICFGR_SHIFT;
	return mmio_read_32(base + GICD_ICFGR + (n << 2));
}

77
unsigned int gicd_read_cpendsgir(uintptr_t base, unsigned int id)
78
79
80
81
82
{
	unsigned n = id >> CPENDSGIR_SHIFT;
	return mmio_read_32(base + GICD_CPENDSGIR + (n << 2));
}

83
unsigned int gicd_read_spendsgir(uintptr_t base, unsigned int id)
84
85
86
87
88
89
{
	unsigned n = id >> SPENDSGIR_SHIFT;
	return mmio_read_32(base + GICD_SPENDSGIR + (n << 2));
}

/*******************************************************************************
90
 * GIC Distributor interface accessors for writing entire registers
91
92
 ******************************************************************************/

93
void gicd_write_igroupr(uintptr_t base, unsigned int id, unsigned int val)
94
95
96
97
98
{
	unsigned n = id >> IGROUPR_SHIFT;
	mmio_write_32(base + GICD_IGROUPR + (n << 2), val);
}

99
void gicd_write_isenabler(uintptr_t base, unsigned int id, unsigned int val)
100
101
102
103
104
{
	unsigned n = id >> ISENABLER_SHIFT;
	mmio_write_32(base + GICD_ISENABLER + (n << 2), val);
}

105
void gicd_write_icenabler(uintptr_t base, unsigned int id, unsigned int val)
106
107
108
109
110
{
	unsigned n = id >> ICENABLER_SHIFT;
	mmio_write_32(base + GICD_ICENABLER + (n << 2), val);
}

111
void gicd_write_ispendr(uintptr_t base, unsigned int id, unsigned int val)
112
113
114
115
116
{
	unsigned n = id >> ISPENDR_SHIFT;
	mmio_write_32(base + GICD_ISPENDR + (n << 2), val);
}

117
void gicd_write_icpendr(uintptr_t base, unsigned int id, unsigned int val)
118
119
120
121
122
{
	unsigned n = id >> ICPENDR_SHIFT;
	mmio_write_32(base + GICD_ICPENDR + (n << 2), val);
}

123
void gicd_write_isactiver(uintptr_t base, unsigned int id, unsigned int val)
124
125
126
127
128
{
	unsigned n = id >> ISACTIVER_SHIFT;
	mmio_write_32(base + GICD_ISACTIVER + (n << 2), val);
}

129
void gicd_write_icactiver(uintptr_t base, unsigned int id, unsigned int val)
130
131
132
133
134
{
	unsigned n = id >> ICACTIVER_SHIFT;
	mmio_write_32(base + GICD_ICACTIVER + (n << 2), val);
}

135
void gicd_write_ipriorityr(uintptr_t base, unsigned int id, unsigned int val)
136
137
138
139
140
{
	unsigned n = id >> IPRIORITYR_SHIFT;
	mmio_write_32(base + GICD_IPRIORITYR + (n << 2), val);
}

141
void gicd_write_itargetsr(uintptr_t base, unsigned int id, unsigned int val)
142
143
144
145
146
{
	unsigned n = id >> ITARGETSR_SHIFT;
	mmio_write_32(base + GICD_ITARGETSR + (n << 2), val);
}

147
void gicd_write_icfgr(uintptr_t base, unsigned int id, unsigned int val)
148
149
150
151
152
{
	unsigned n = id >> ICFGR_SHIFT;
	mmio_write_32(base + GICD_ICFGR + (n << 2), val);
}

153
void gicd_write_cpendsgir(uintptr_t base, unsigned int id, unsigned int val)
154
155
156
157
158
{
	unsigned n = id >> CPENDSGIR_SHIFT;
	mmio_write_32(base + GICD_CPENDSGIR + (n << 2), val);
}

159
void gicd_write_spendsgir(uintptr_t base, unsigned int id, unsigned int val)
160
161
162
163
164
165
{
	unsigned n = id >> SPENDSGIR_SHIFT;
	mmio_write_32(base + GICD_SPENDSGIR + (n << 2), val);
}

/*******************************************************************************
166
 * GIC Distributor interface accessors for individual interrupt manipulation
167
 ******************************************************************************/
168
unsigned int gicd_get_igroupr(uintptr_t base, unsigned int id)
169
170
171
172
173
174
175
{
	unsigned bit_num = id & ((1 << IGROUPR_SHIFT) - 1);
	unsigned int reg_val = gicd_read_igroupr(base, id);

	return (reg_val >> bit_num) & 0x1;
}

176
void gicd_set_igroupr(uintptr_t base, unsigned int id)
177
178
179
180
181
182
183
{
	unsigned bit_num = id & ((1 << IGROUPR_SHIFT) - 1);
	unsigned int reg_val = gicd_read_igroupr(base, id);

	gicd_write_igroupr(base, id, reg_val | (1 << bit_num));
}

184
void gicd_clr_igroupr(uintptr_t base, unsigned int id)
185
186
187
188
189
190
191
{
	unsigned bit_num = id & ((1 << IGROUPR_SHIFT) - 1);
	unsigned int reg_val = gicd_read_igroupr(base, id);

	gicd_write_igroupr(base, id, reg_val & ~(1 << bit_num));
}

192
void gicd_set_isenabler(uintptr_t base, unsigned int id)
193
194
195
{
	unsigned bit_num = id & ((1 << ISENABLER_SHIFT) - 1);

196
	gicd_write_isenabler(base, id, (1 << bit_num));
197
198
}

199
void gicd_set_icenabler(uintptr_t base, unsigned int id)
200
201
202
{
	unsigned bit_num = id & ((1 << ICENABLER_SHIFT) - 1);

203
	gicd_write_icenabler(base, id, (1 << bit_num));
204
205
}

206
void gicd_set_ispendr(uintptr_t base, unsigned int id)
207
208
209
{
	unsigned bit_num = id & ((1 << ISPENDR_SHIFT) - 1);

210
	gicd_write_ispendr(base, id, (1 << bit_num));
211
212
}

213
void gicd_set_icpendr(uintptr_t base, unsigned int id)
214
215
216
{
	unsigned bit_num = id & ((1 << ICPENDR_SHIFT) - 1);

217
	gicd_write_icpendr(base, id, (1 << bit_num));
218
219
}

220
void gicd_set_isactiver(uintptr_t base, unsigned int id)
221
222
223
{
	unsigned bit_num = id & ((1 << ISACTIVER_SHIFT) - 1);

224
	gicd_write_isactiver(base, id, (1 << bit_num));
225
226
}

227
void gicd_set_icactiver(uintptr_t base, unsigned int id)
228
229
230
{
	unsigned bit_num = id & ((1 << ICACTIVER_SHIFT) - 1);

231
	gicd_write_icactiver(base, id, (1 << bit_num));
232
233
234
235
236
237
}

/*
 * Make sure that the interrupt's group is set before expecting
 * this function to do its job correctly.
 */
238
void gicd_set_ipriorityr(uintptr_t base, unsigned int id, unsigned int pri)
239
240
241
242
{
	/*
	 * Enforce ARM recommendation to manage priority values such
	 * that group1 interrupts always have a lower priority than
243
244
245
	 * group0 interrupts.
	 * Note, lower numerical values are higher priorities so the comparison
	 * checks below are reversed from what might be expected.
246
	 */
247
248
249
250
251
252
	assert(gicd_get_igroupr(base, id) == GRP1 ?
		pri >= GIC_HIGHEST_NS_PRIORITY &&
			pri <= GIC_LOWEST_NS_PRIORITY :
		pri >= GIC_HIGHEST_SEC_PRIORITY &&
			pri <= GIC_LOWEST_SEC_PRIORITY);

253
	mmio_write_8(base + GICD_IPRIORITYR + id, pri & GIC_PRI_MASK);
254
255
}

256
void gicd_set_itargetsr(uintptr_t base, unsigned int id, unsigned int target)
257
{
258
	mmio_write_8(base + GICD_ITARGETSR + id, target & GIC_TARGET_CPU_MASK);
259
260
}

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*******************************************************************************
 * This function allows the interrupt management framework to determine (through
 * the platform) which interrupt line (IRQ/FIQ) to use for an interrupt type to
 * route it to EL3. The interrupt line is represented as the bit position of the
 * IRQ or FIQ bit in the SCR_EL3.
 ******************************************************************************/
uint32_t gicv2_interrupt_type_to_line(uint32_t cpuif_base, uint32_t type)
{
	uint32_t gicc_ctlr;

	/* Non-secure interrupts are signalled on the IRQ line always */
	if (type == INTR_TYPE_NS)
		return __builtin_ctz(SCR_IRQ_BIT);

	/*
	 * Secure interrupts are signalled using the IRQ line if the FIQ_EN
	 * bit is not set else they are signalled using the FIQ line.
	 */
	gicc_ctlr = gicc_read_ctlr(cpuif_base);
	if (gicc_ctlr & FIQ_EN)
		return __builtin_ctz(SCR_FIQ_BIT);
	else
		return __builtin_ctz(SCR_IRQ_BIT);
}