1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "qemu/osdep.h"
19#include "qapi/error.h"
20#include "qemu/module.h"
21#include "hw/intc/arm_gicv3.h"
22#include "gicv3_internal.h"
23
24static bool irqbetter(GICv3CPUState *cs, int irq, uint8_t prio)
25{
26
27
28
29
30
31
32
33 if (prio < cs->hppi.prio) {
34 return true;
35 }
36
37
38
39
40 if (prio == cs->hppi.prio && irq <= cs->hppi.irq) {
41 return true;
42 }
43 return false;
44}
45
46static uint32_t gicd_int_pending(GICv3State *s, int irq)
47{
48
49
50
51
52
53
54
55
56
57
58
59
60 uint32_t pend, grpmask;
61 uint32_t pending = *gic_bmp_ptr32(s->pending, irq);
62 uint32_t edge_trigger = *gic_bmp_ptr32(s->edge_trigger, irq);
63 uint32_t level = *gic_bmp_ptr32(s->level, irq);
64 uint32_t group = *gic_bmp_ptr32(s->group, irq);
65 uint32_t grpmod = *gic_bmp_ptr32(s->grpmod, irq);
66 uint32_t enable = *gic_bmp_ptr32(s->enabled, irq);
67 uint32_t active = *gic_bmp_ptr32(s->active, irq);
68
69 pend = pending | (~edge_trigger & level);
70 pend &= enable;
71 pend &= ~active;
72
73 if (s->gicd_ctlr & GICD_CTLR_DS) {
74 grpmod = 0;
75 }
76
77 grpmask = 0;
78 if (s->gicd_ctlr & GICD_CTLR_EN_GRP1NS) {
79 grpmask |= group;
80 }
81 if (s->gicd_ctlr & GICD_CTLR_EN_GRP1S) {
82 grpmask |= (~group & grpmod);
83 }
84 if (s->gicd_ctlr & GICD_CTLR_EN_GRP0) {
85 grpmask |= (~group & ~grpmod);
86 }
87 pend &= grpmask;
88
89 return pend;
90}
91
92static uint32_t gicr_int_pending(GICv3CPUState *cs)
93{
94
95
96
97
98
99
100
101
102
103
104
105 uint32_t pend, grpmask, grpmod;
106
107 pend = cs->gicr_ipendr0 | (~cs->edge_trigger & cs->level);
108 pend &= cs->gicr_ienabler0;
109 pend &= ~cs->gicr_iactiver0;
110
111 if (cs->gic->gicd_ctlr & GICD_CTLR_DS) {
112 grpmod = 0;
113 } else {
114 grpmod = cs->gicr_igrpmodr0;
115 }
116
117 grpmask = 0;
118 if (cs->gic->gicd_ctlr & GICD_CTLR_EN_GRP1NS) {
119 grpmask |= cs->gicr_igroupr0;
120 }
121 if (cs->gic->gicd_ctlr & GICD_CTLR_EN_GRP1S) {
122 grpmask |= (~cs->gicr_igroupr0 & grpmod);
123 }
124 if (cs->gic->gicd_ctlr & GICD_CTLR_EN_GRP0) {
125 grpmask |= (~cs->gicr_igroupr0 & ~grpmod);
126 }
127 pend &= grpmask;
128
129 return pend;
130}
131
132
133
134
135static void gicv3_redist_update_noirqset(GICv3CPUState *cs)
136{
137
138
139
140 bool seenbetter = false;
141 uint8_t prio;
142 int i;
143 uint32_t pend;
144
145
146
147
148 pend = gicr_int_pending(cs);
149
150 if (pend) {
151 for (i = 0; i < GIC_INTERNAL; i++) {
152 if (!(pend & (1 << i))) {
153 continue;
154 }
155 prio = cs->gicr_ipriorityr[i];
156 if (irqbetter(cs, i, prio)) {
157 cs->hppi.irq = i;
158 cs->hppi.prio = prio;
159 seenbetter = true;
160 }
161 }
162 }
163
164 if (seenbetter) {
165 cs->hppi.grp = gicv3_irq_group(cs->gic, cs, cs->hppi.irq);
166 }
167
168 if ((cs->gicr_ctlr & GICR_CTLR_ENABLE_LPIS) && cs->gic->lpi_enable &&
169 (cs->gic->gicd_ctlr & GICD_CTLR_EN_GRP1NS) &&
170 (cs->hpplpi.prio != 0xff)) {
171 if (irqbetter(cs, cs->hpplpi.irq, cs->hpplpi.prio)) {
172 cs->hppi.irq = cs->hpplpi.irq;
173 cs->hppi.prio = cs->hpplpi.prio;
174 cs->hppi.grp = cs->hpplpi.grp;
175 seenbetter = true;
176 }
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190 if (!seenbetter && cs->hppi.prio != 0xff &&
191 (cs->hppi.irq < GIC_INTERNAL ||
192 cs->hppi.irq >= GICV3_LPI_INTID_START)) {
193 gicv3_full_update_noirqset(cs->gic);
194 }
195}
196
197
198
199
200
201void gicv3_redist_update(GICv3CPUState *cs)
202{
203 gicv3_redist_update_noirqset(cs);
204 gicv3_cpuif_update(cs);
205}
206
207
208
209
210
211static void gicv3_update_noirqset(GICv3State *s, int start, int len)
212{
213 int i;
214 uint8_t prio;
215 uint32_t pend = 0;
216
217 assert(start >= GIC_INTERNAL);
218 assert(len > 0);
219
220 for (i = 0; i < s->num_cpu; i++) {
221 s->cpu[i].seenbetter = false;
222 }
223
224
225 for (i = start; i < start + len; i++) {
226 GICv3CPUState *cs;
227
228 if (i == start || (i & 0x1f) == 0) {
229
230 pend = gicd_int_pending(s, i & ~0x1f);
231 }
232
233 if (!(pend & (1 << (i & 0x1f)))) {
234 continue;
235 }
236 cs = s->gicd_irouter_target[i];
237 if (!cs) {
238
239
240
241 continue;
242 }
243 prio = s->gicd_ipriority[i];
244 if (irqbetter(cs, i, prio)) {
245 cs->hppi.irq = i;
246 cs->hppi.prio = prio;
247 cs->seenbetter = true;
248 }
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262 for (i = 0; i < s->num_cpu; i++) {
263 GICv3CPUState *cs = &s->cpu[i];
264
265 if (cs->seenbetter) {
266 cs->hppi.grp = gicv3_irq_group(cs->gic, cs, cs->hppi.irq);
267 }
268
269 if (!cs->seenbetter && cs->hppi.prio != 0xff &&
270 cs->hppi.irq >= start && cs->hppi.irq < start + len) {
271 gicv3_full_update_noirqset(s);
272 break;
273 }
274 }
275}
276
277void gicv3_update(GICv3State *s, int start, int len)
278{
279 int i;
280
281 gicv3_update_noirqset(s, start, len);
282 for (i = 0; i < s->num_cpu; i++) {
283 gicv3_cpuif_update(&s->cpu[i]);
284 }
285}
286
287void gicv3_full_update_noirqset(GICv3State *s)
288{
289
290
291
292 int i;
293
294 for (i = 0; i < s->num_cpu; i++) {
295 s->cpu[i].hppi.prio = 0xff;
296 }
297
298
299
300
301
302
303 gicv3_update_noirqset(s, GIC_INTERNAL, s->num_irq - GIC_INTERNAL);
304
305 for (i = 0; i < s->num_cpu; i++) {
306 gicv3_redist_update_noirqset(&s->cpu[i]);
307 }
308}
309
310void gicv3_full_update(GICv3State *s)
311{
312
313
314
315 int i;
316
317 gicv3_full_update_noirqset(s);
318 for (i = 0; i < s->num_cpu; i++) {
319 gicv3_cpuif_update(&s->cpu[i]);
320 }
321}
322
323
324static void gicv3_set_irq(void *opaque, int irq, int level)
325{
326
327
328
329
330
331
332 GICv3State *s = opaque;
333
334 if (irq < (s->num_irq - GIC_INTERNAL)) {
335
336 gicv3_dist_set_irq(s, irq + GIC_INTERNAL, level);
337 } else {
338
339 int cpu;
340
341 irq -= (s->num_irq - GIC_INTERNAL);
342 cpu = irq / GIC_INTERNAL;
343 irq %= GIC_INTERNAL;
344 assert(cpu < s->num_cpu);
345
346
347
348 assert(irq >= GIC_NR_SGIS);
349 gicv3_redist_set_irq(&s->cpu[cpu], irq, level);
350 }
351}
352
353static void arm_gicv3_post_load(GICv3State *s)
354{
355 int i;
356
357
358
359 for (i = 0; i < s->num_cpu; i++) {
360 gicv3_redist_update_lpi_only(&s->cpu[i]);
361 }
362 gicv3_full_update_noirqset(s);
363
364 gicv3_cache_all_target_cpustates(s);
365}
366
367static const MemoryRegionOps gic_ops[] = {
368 {
369 .read_with_attrs = gicv3_dist_read,
370 .write_with_attrs = gicv3_dist_write,
371 .endianness = DEVICE_NATIVE_ENDIAN,
372 .valid.min_access_size = 1,
373 .valid.max_access_size = 8,
374 .impl.min_access_size = 1,
375 .impl.max_access_size = 8,
376 },
377 {
378 .read_with_attrs = gicv3_redist_read,
379 .write_with_attrs = gicv3_redist_write,
380 .endianness = DEVICE_NATIVE_ENDIAN,
381 .valid.min_access_size = 1,
382 .valid.max_access_size = 8,
383 .impl.min_access_size = 1,
384 .impl.max_access_size = 8,
385 }
386};
387
388static void arm_gic_realize(DeviceState *dev, Error **errp)
389{
390
391 GICv3State *s = ARM_GICV3(dev);
392 ARMGICv3Class *agc = ARM_GICV3_GET_CLASS(s);
393 Error *local_err = NULL;
394
395 agc->parent_realize(dev, &local_err);
396 if (local_err) {
397 error_propagate(errp, local_err);
398 return;
399 }
400
401 gicv3_init_irqs_and_mmio(s, gicv3_set_irq, gic_ops);
402
403 gicv3_init_cpuif(s);
404}
405
406static void arm_gicv3_class_init(ObjectClass *klass, void *data)
407{
408 DeviceClass *dc = DEVICE_CLASS(klass);
409 ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
410 ARMGICv3Class *agc = ARM_GICV3_CLASS(klass);
411
412 agcc->post_load = arm_gicv3_post_load;
413 device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
414}
415
416static const TypeInfo arm_gicv3_info = {
417 .name = TYPE_ARM_GICV3,
418 .parent = TYPE_ARM_GICV3_COMMON,
419 .instance_size = sizeof(GICv3State),
420 .class_init = arm_gicv3_class_init,
421 .class_size = sizeof(ARMGICv3Class),
422};
423
424static void arm_gicv3_register_types(void)
425{
426 type_register_static(&arm_gicv3_info);
427}
428
429type_init(arm_gicv3_register_types)
430