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