xref: /openbmc/qemu/hw/intc/arm_gicv3.c (revision 6016b7b4)
1 /*
2  * ARM Generic Interrupt Controller v3 (emulation)
3  *
4  * Copyright (c) 2015 Huawei.
5  * Copyright (c) 2016 Linaro Limited
6  * Written by Shlomo Pongratz, Peter Maydell
7  *
8  * This code is licensed under the GPL, version 2 or (at your option)
9  * any later version.
10  */
11 
12 /* This file contains implementation code for an interrupt controller
13  * which implements the GICv3 architecture. Specifically this is where
14  * the device class itself and the functions for handling interrupts
15  * coming in and going out live.
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 
24 static bool irqbetter(GICv3CPUState *cs, int irq, uint8_t prio)
25 {
26     /* Return true if this IRQ at this priority should take
27      * precedence over the current recorded highest priority
28      * pending interrupt for this CPU. We also return true if
29      * the current recorded highest priority pending interrupt
30      * is the same as this one (a property which the calling code
31      * relies on).
32      */
33     if (prio < cs->hppi.prio) {
34         return true;
35     }
36     /* If multiple pending interrupts have the same priority then it is an
37      * IMPDEF choice which of them to signal to the CPU. We choose to
38      * signal the one with the lowest interrupt number.
39      */
40     if (prio == cs->hppi.prio && irq <= cs->hppi.irq) {
41         return true;
42     }
43     return false;
44 }
45 
46 static uint32_t gicd_int_pending(GICv3State *s, int irq)
47 {
48     /* Recalculate which distributor interrupts are actually pending
49      * in the group of 32 interrupts starting at irq (which should be a multiple
50      * of 32), and return a 32-bit integer which has a bit set for each
51      * interrupt that is eligible to be signaled to the CPU interface.
52      *
53      * An interrupt is pending if:
54      *  + the PENDING latch is set OR it is level triggered and the input is 1
55      *  + its ENABLE bit is set
56      *  + the GICD enable bit for its group is set
57      *  + its ACTIVE bit is not set (otherwise it would be Active+Pending)
58      * Conveniently we can bulk-calculate this with bitwise operations.
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 
92 static uint32_t gicr_int_pending(GICv3CPUState *cs)
93 {
94     /* Recalculate which redistributor interrupts are actually pending,
95      * and return a 32-bit integer which has a bit set for each interrupt
96      * that is eligible to be signaled to the CPU interface.
97      *
98      * An interrupt is pending if:
99      *  + the PENDING latch is set OR it is level triggered and the input is 1
100      *  + its ENABLE bit is set
101      *  + the GICD enable bit for its group is set
102      *  + its ACTIVE bit is not set (otherwise it would be Active+Pending)
103      * Conveniently we can bulk-calculate this with bitwise operations.
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 /* Update the interrupt status after state in a redistributor
133  * or CPU interface has changed, but don't tell the CPU i/f.
134  */
135 static void gicv3_redist_update_noirqset(GICv3CPUState *cs)
136 {
137     /* Find the highest priority pending interrupt among the
138      * redistributor interrupts (SGIs and PPIs).
139      */
140     bool seenbetter = false;
141     uint8_t prio;
142     int i;
143     uint32_t pend;
144 
145     /* Find out which redistributor interrupts are eligible to be
146      * signaled to the CPU interface.
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->hpplpi.prio != 0xff)) {
170         if (irqbetter(cs, cs->hpplpi.irq, cs->hpplpi.prio)) {
171             cs->hppi.irq = cs->hpplpi.irq;
172             cs->hppi.prio = cs->hpplpi.prio;
173             cs->hppi.grp = cs->hpplpi.grp;
174             seenbetter = true;
175         }
176     }
177 
178     /* If the best interrupt we just found would preempt whatever
179      * was the previous best interrupt before this update, then
180      * we know it's definitely the best one now.
181      * If we didn't find an interrupt that would preempt the previous
182      * best, and the previous best is outside our range (or there was no
183      * previous pending interrupt at all), then that is still valid, and
184      * we leave it as the best.
185      * Otherwise, we need to do a full update (because the previous best
186      * interrupt has reduced in priority and any other interrupt could
187      * now be the new best one).
188      */
189     if (!seenbetter && cs->hppi.prio != 0xff &&
190         (cs->hppi.irq < GIC_INTERNAL ||
191          cs->hppi.irq >= GICV3_LPI_INTID_START)) {
192         gicv3_full_update_noirqset(cs->gic);
193     }
194 }
195 
196 /* Update the GIC status after state in a redistributor or
197  * CPU interface has changed, and inform the CPU i/f of
198  * its new highest priority pending interrupt.
199  */
200 void gicv3_redist_update(GICv3CPUState *cs)
201 {
202     gicv3_redist_update_noirqset(cs);
203     gicv3_cpuif_update(cs);
204 }
205 
206 /* Update the GIC status after state in the distributor has
207  * changed affecting @len interrupts starting at @start,
208  * but don't tell the CPU i/f.
209  */
210 static void gicv3_update_noirqset(GICv3State *s, int start, int len)
211 {
212     int i;
213     uint8_t prio;
214     uint32_t pend = 0;
215 
216     assert(start >= GIC_INTERNAL);
217     assert(len > 0);
218 
219     for (i = 0; i < s->num_cpu; i++) {
220         s->cpu[i].seenbetter = false;
221     }
222 
223     /* Find the highest priority pending interrupt in this range. */
224     for (i = start; i < start + len; i++) {
225         GICv3CPUState *cs;
226 
227         if (i == start || (i & 0x1f) == 0) {
228             /* Calculate the next 32 bits worth of pending status */
229             pend = gicd_int_pending(s, i & ~0x1f);
230         }
231 
232         if (!(pend & (1 << (i & 0x1f)))) {
233             continue;
234         }
235         cs = s->gicd_irouter_target[i];
236         if (!cs) {
237             /* Interrupts targeting no implemented CPU should remain pending
238              * and not be forwarded to any CPU.
239              */
240             continue;
241         }
242         prio = s->gicd_ipriority[i];
243         if (irqbetter(cs, i, prio)) {
244             cs->hppi.irq = i;
245             cs->hppi.prio = prio;
246             cs->seenbetter = true;
247         }
248     }
249 
250     /* If the best interrupt we just found would preempt whatever
251      * was the previous best interrupt before this update, then
252      * we know it's definitely the best one now.
253      * If we didn't find an interrupt that would preempt the previous
254      * best, and the previous best is outside our range (or there was
255      * no previous pending interrupt at all), then that
256      * is still valid, and we leave it as the best.
257      * Otherwise, we need to do a full update (because the previous best
258      * interrupt has reduced in priority and any other interrupt could
259      * now be the new best one).
260      */
261     for (i = 0; i < s->num_cpu; i++) {
262         GICv3CPUState *cs = &s->cpu[i];
263 
264         if (cs->seenbetter) {
265             cs->hppi.grp = gicv3_irq_group(cs->gic, cs, cs->hppi.irq);
266         }
267 
268         if (!cs->seenbetter && cs->hppi.prio != 0xff &&
269             cs->hppi.irq >= start && cs->hppi.irq < start + len) {
270             gicv3_full_update_noirqset(s);
271             break;
272         }
273     }
274 }
275 
276 void gicv3_update(GICv3State *s, int start, int len)
277 {
278     int i;
279 
280     gicv3_update_noirqset(s, start, len);
281     for (i = 0; i < s->num_cpu; i++) {
282         gicv3_cpuif_update(&s->cpu[i]);
283     }
284 }
285 
286 void gicv3_full_update_noirqset(GICv3State *s)
287 {
288     /* Completely recalculate the GIC status from scratch, but
289      * don't update any outbound IRQ lines.
290      */
291     int i;
292 
293     for (i = 0; i < s->num_cpu; i++) {
294         s->cpu[i].hppi.prio = 0xff;
295     }
296 
297     /* Note that we can guarantee that these functions will not
298      * recursively call back into gicv3_full_update(), because
299      * at each point the "previous best" is always outside the
300      * range we ask them to update.
301      */
302     gicv3_update_noirqset(s, GIC_INTERNAL, s->num_irq - GIC_INTERNAL);
303 
304     for (i = 0; i < s->num_cpu; i++) {
305         gicv3_redist_update_noirqset(&s->cpu[i]);
306     }
307 }
308 
309 void gicv3_full_update(GICv3State *s)
310 {
311     /* Completely recalculate the GIC status from scratch, including
312      * updating outbound IRQ lines.
313      */
314     int i;
315 
316     gicv3_full_update_noirqset(s);
317     for (i = 0; i < s->num_cpu; i++) {
318         gicv3_cpuif_update(&s->cpu[i]);
319     }
320 }
321 
322 /* Process a change in an external IRQ input. */
323 static void gicv3_set_irq(void *opaque, int irq, int level)
324 {
325     /* Meaning of the 'irq' parameter:
326      *  [0..N-1] : external interrupts
327      *  [N..N+31] : PPI (internal) interrupts for CPU 0
328      *  [N+32..N+63] : PPI (internal interrupts for CPU 1
329      *  ...
330      */
331     GICv3State *s = opaque;
332 
333     if (irq < (s->num_irq - GIC_INTERNAL)) {
334         /* external interrupt (SPI) */
335         gicv3_dist_set_irq(s, irq + GIC_INTERNAL, level);
336     } else {
337         /* per-cpu interrupt (PPI) */
338         int cpu;
339 
340         irq -= (s->num_irq - GIC_INTERNAL);
341         cpu = irq / GIC_INTERNAL;
342         irq %= GIC_INTERNAL;
343         assert(cpu < s->num_cpu);
344         /* Raising SGIs via this function would be a bug in how the board
345          * model wires up interrupts.
346          */
347         assert(irq >= GIC_NR_SGIS);
348         gicv3_redist_set_irq(&s->cpu[cpu], irq, level);
349     }
350 }
351 
352 static void arm_gicv3_post_load(GICv3State *s)
353 {
354     int i;
355     /* Recalculate our cached idea of the current highest priority
356      * pending interrupt, but don't set IRQ or FIQ lines.
357      */
358     for (i = 0; i < s->num_cpu; i++) {
359         gicv3_redist_update_lpi_only(&s->cpu[i]);
360     }
361     gicv3_full_update_noirqset(s);
362     /* Repopulate the cache of GICv3CPUState pointers for target CPUs */
363     gicv3_cache_all_target_cpustates(s);
364 }
365 
366 static const MemoryRegionOps gic_ops[] = {
367     {
368         .read_with_attrs = gicv3_dist_read,
369         .write_with_attrs = gicv3_dist_write,
370         .endianness = DEVICE_NATIVE_ENDIAN,
371     },
372     {
373         .read_with_attrs = gicv3_redist_read,
374         .write_with_attrs = gicv3_redist_write,
375         .endianness = DEVICE_NATIVE_ENDIAN,
376     }
377 };
378 
379 static void arm_gic_realize(DeviceState *dev, Error **errp)
380 {
381     /* Device instance realize function for the GIC sysbus device */
382     GICv3State *s = ARM_GICV3(dev);
383     ARMGICv3Class *agc = ARM_GICV3_GET_CLASS(s);
384     Error *local_err = NULL;
385 
386     agc->parent_realize(dev, &local_err);
387     if (local_err) {
388         error_propagate(errp, local_err);
389         return;
390     }
391 
392     gicv3_init_irqs_and_mmio(s, gicv3_set_irq, gic_ops);
393 
394     gicv3_init_cpuif(s);
395 }
396 
397 static void arm_gicv3_class_init(ObjectClass *klass, void *data)
398 {
399     DeviceClass *dc = DEVICE_CLASS(klass);
400     ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
401     ARMGICv3Class *agc = ARM_GICV3_CLASS(klass);
402 
403     agcc->post_load = arm_gicv3_post_load;
404     device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
405 }
406 
407 static const TypeInfo arm_gicv3_info = {
408     .name = TYPE_ARM_GICV3,
409     .parent = TYPE_ARM_GICV3_COMMON,
410     .instance_size = sizeof(GICv3State),
411     .class_init = arm_gicv3_class_init,
412     .class_size = sizeof(ARMGICv3Class),
413 };
414 
415 static void arm_gicv3_register_types(void)
416 {
417     type_register_static(&arm_gicv3_info);
418 }
419 
420 type_init(arm_gicv3_register_types)
421