Skip to contentSkip to conversation

Support arm-gic-kvm save/restore

RFC PATCH Superseded Christoffer Dall 4 participants 24 messages Sep 26, 2013 – Nov 19, 2013

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Support arm-gic-kvm save/restore

Implement support to save/restore the ARM KVM VGIC state from the kernel. The basic appraoch is to transfer state from the in-kernel VGIC to the emulated arm-gic state representation and let the standard QEMU vmstate save/restore handle saving the arm-gic state. Restore works by reversing the process.

The first few patches adds missing features and fixes issues with the arm-gic implementation in qemu in preparation for the actual save/restore logic.

The patches depend on the device control patch series sent out earlier, which can also be found here:
git://git.linaro.org/people/cdall/qemu-arm.git migration/device-ctrl-v2

The whole patch series based on top of the above can be found here:
git://git.linaro.org/people/cdall/qemu-arm.git migration/vgic-v2

Changelog [v2]:
- Changes are described in the individual patches
- VMState additions has been split into a separate patch

Christoffer Dall (6):
hw: arm_gic: Fix gic_set_irq handling
hw: arm_gic: Introduce GIC_SET_PRIORITY macro
hw: arm_gic: Keep track of SGI sources
arm_gic: Support setting/getting binary point reg
vmstate: Add uint32 2D-array support
hw: arm_gic_kvm: Add KVM VGIC save/restore logic

 hw/intc/arm_gic.c           |   73 ++++++--
 hw/intc/arm_gic_common.c    |    8 +-
 hw/intc/arm_gic_kvm.c       |  424 ++++++++++++++++++++++++++++++++++++++++++-
 hw/intc/gic_internal.h      |   19 ++
 include/migration/vmstate.h |    6 +
 5 files changed, 506 insertions(+), 24 deletions(-)
Signature · 1 line
1.7.10.4
#1 lore raw

RFC PATCH v2 1/6 hw: arm_gic: Fix gic_set_irq handling

For some reason only edge-triggered or enabled level-triggered interrupts would set the pending state of a raised IRQ. This is not in compliance with the specs, which indicate that the pending state is separate from the enabled state, which only controls if a pending interrupt is actually forwarded to the CPU interface.

Therefore, simply always set the pending state on a rising edge, but only clear the pending state of falling edge if the interrupt is level triggered.

Changelog [v2]:
- Fix bisection issue, by not using gic_clear_pending yet.

---
 hw/intc/arm_gic.c |    9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
hw/intc/arm_gic.c +5 -4
index d431b7a..c7a24d5 100644
@@ -128,11 +128,12 @@ static void gic_set_irq(void *opaque, int irq, int level)
 
     if (level) {
         GIC_SET_LEVEL(irq, cm);
-        if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
-            DPRINTF("Set %d pending mask %x\n", irq, target);
-            GIC_SET_PENDING(irq, target);
-        }
+        DPRINTF("Set %d pending mask %x\n", irq, target);
+        GIC_SET_PENDING(irq, target);
     } else {
+        if (!GIC_TEST_TRIGGER(irq)) {
+            GIC_CLEAR_PENDING(irq, target);
+        }
         GIC_CLEAR_LEVEL(irq, cm);
     }
     gic_update(s);
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [RFC PATCH v2 1/6] hw: arm_gic: Fix gic_set_irq handling

Quoted 17 lines · Christoffer Dall

For some reason only edge-triggered or enabled level-triggered interrupts would set the pending state of a raised IRQ. This is not in compliance with the specs, which indicate that the pending state is separate from the enabled state, which only controls if a pending interrupt is actually forwarded to the CPU interface.

Therefore, simply always set the pending state on a rising edge, but only clear the pending state of falling edge if the interrupt is level triggered.

hw/intc/arm_gic.c +0 -3
@@ -128,11 +128,12 @@ static void gic_set_irq(void *opaque, int irq, int level)
 
     if (level) {
         GIC_SET_LEVEL(irq, cm);
-        if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
-            DPRINTF("Set %d pending mask %x\n", irq, target);
-            GIC_SET_PENDING(irq, target);

hw/intc/arm_gic.c static void gic_set_irq(void *opaque, int irq, int level) · line 139

Quoted 10 lines
hw/intc/arm_gic.c +5 -1
-        }
+        DPRINTF("Set %d pending mask %x\n", irq, target);
+        GIC_SET_PENDING(irq, target);
     } else {
+        if (!GIC_TEST_TRIGGER(irq)) {
+            GIC_CLEAR_PENDING(irq, target);
+        }
         GIC_CLEAR_LEVEL(irq, cm);
     }
     gic_update(s);

The old logic is definitely wrong here, but this still isn't quite right. See the GIC v2 spec, section 4.3.8 GICD_ICPENDRn and specifically the circuit diagram in Figure 4.10. For a level triggered interrupt we mustn't clear the pending state on a 1->0 transition of the input if it was latched by the guest writing to GICD_ISPENDR.

In other words, the internal state of the GIC (ie the state of the latch) and the value in the ICPENDR/ISPENDR register on read aren't the same thing, and the bug in our current GIC model is that we're trying to use the .pending field for both purposes at the same time.

-- PMM

#3 lore raw

Re: [Qemu-devel] [RFC PATCH v2 1/6] hw: arm_gic: Fix gic_set_irq handling

Quoted 37 lines · Peter Maydell
Quoted 17 lines · Christoffer Dall

For some reason only edge-triggered or enabled level-triggered interrupts would set the pending state of a raised IRQ. This is not in compliance with the specs, which indicate that the pending state is separate from the enabled state, which only controls if a pending interrupt is actually forwarded to the CPU interface.

Therefore, simply always set the pending state on a rising edge, but only clear the pending state of falling edge if the interrupt is level triggered.

hw/intc/arm_gic.c +0 -3
@@ -128,11 +128,12 @@ static void gic_set_irq(void *opaque, int irq, int level)
 
     if (level) {
         GIC_SET_LEVEL(irq, cm);
-        if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
-            DPRINTF("Set %d pending mask %x\n", irq, target);
-            GIC_SET_PENDING(irq, target);

hw/intc/arm_gic.c static void gic_set_irq(void *opaque, int irq, int level) · line 139

Quoted 10 lines
hw/intc/arm_gic.c +5 -1
-        }
+        DPRINTF("Set %d pending mask %x\n", irq, target);
+        GIC_SET_PENDING(irq, target);
     } else {
+        if (!GIC_TEST_TRIGGER(irq)) {
+            GIC_CLEAR_PENDING(irq, target);
+        }
         GIC_CLEAR_LEVEL(irq, cm);
     }
     gic_update(s);

The old logic is definitely wrong here, but this still isn't quite right. See the GIC v2 spec, section 4.3.8 GICD_ICPENDRn and specifically the circuit diagram in Figure 4.10. For a level triggered interrupt we mustn't clear the pending state on a 1->0 transition of the input if it was latched by the guest writing to GICD_ISPENDR.

Quoted 6 lines

In other words, the internal state of the GIC (ie the state of the latch) and the value in the ICPENDR/ISPENDR register on read aren't the same thing, and the bug in our current GIC model is that we're trying to use the .pending field for both purposes at the same time.

So I think that's a comment that belongs more in the category of all the things that are broken with the GICv2 emulation and should be separate fixes. I don't believe Linux uses this and the in-kernel GIC emulation also doesn't keep track of this state, but the following patch should address the issue. Do you want me to fold such two patches into one?

-Christoffer

#4 lore raw

PATCH 1/1 arm_gic: Keep track of GICD_CPENDR and GICD_SPENDR

In reply to Peter Maydell

If software writes to the ISPENDR and sets the pending state of a level-triggered interrupt, the falling edge of the hardware input must not clear the pending state. Conversely, if software writes to the ICPENDR, the pending state of a level-triggered interrupt should only be cleared if the hardware input is not asserted.

This requires an extra state variable to keep track of software writes.

---
 hw/intc/arm_gic.c        | 20 +++++++++++++++++---
 hw/intc/arm_gic_common.c |  5 +++--
 hw/intc/gic_internal.h   |  4 ++++
 3 files changed, 24 insertions(+), 5 deletions(-)
hw/intc/arm_gic.c +17 -3
index d1ddac1..db54061 100644
@@ -101,6 +101,12 @@ static void gic_clear_pending(GICState *s, int irq, int cm, uint8_t src)
 {
     unsigned cpu;
 
+    /* If a level-triggered interrupt has been set to pending through the
+     * GICD_SPENDR, then a falling edge does not clear the pending state.
+     */
+    if (GIC_TEST_SW_PENDING(irq, cm))
+        return;
+
     GIC_CLEAR_PENDING(irq, cm);
     if (irq < GIC_NR_SGIS) {
         cpu = (unsigned)ffs(cm) - 1;
@@ -177,8 +183,9 @@ uint32_t gic_acknowledge_irq(GICState *s, int cpu)
     s->last_active[new_irq][cpu] = s->running_irq[cpu];
     /* Clear pending flags for both level and edge triggered interrupts.
        Level triggered IRQs will be reasserted once they become inactive.  */
-    gic_clear_pending(s, new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm,
-                                  GIC_SGI_SRC(new_irq, cpu));
+    cm = GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm;
+    GIC_CLEAR_SW_PENDING(new_irq, cm);
+    gic_clear_pending(s, new_irq, cm, GIC_SGI_SRC(new_irq, cpu));
     gic_set_running_irq(s, cpu, new_irq);
     DPRINTF("ACK %d\n", new_irq);
     return new_irq;
@@ -445,16 +452,23 @@ static void gic_dist_writeb(void *opaque, hwaddr offset,
         for (i = 0; i < 8; i++) {
             if (value & (1 << i)) {
                 GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
+                if (!GIC_TEST_TRIGGER(irq + i)) {
+                    GIC_SET_SW_PENDING(irq + i, GIC_TARGET(irq + i));
+                }
             }
         }
     } else if (offset < 0x300) {
+        int cm = (1 << cpu);
         /* Interrupt Clear Pending.  */
         irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
         if (irq >= s->num_irq)
             goto bad_reg;
         for (i = 0; i < 8; i++, irq++) {
             if (irq > GIC_NR_SGIS && value & (1 << i)) {
-                gic_clear_pending(s, irq, 1 << cpu, 0);
+                GIC_CLEAR_SW_PENDING(irq, cm);
+                if (GIC_TEST_TRIGGER(irq + i) || !GIC_TEST_LEVEL(irq, cm)) {
+                    GIC_CLEAR_PENDING(irq, cm);
+                }
             }
         }
     } else if (offset < 0x400) {
hw/intc/arm_gic_common.c +3 -2
index 1d3b738..7f0615f 100644
@@ -43,11 +43,12 @@ static int gic_post_load(void *opaque, int version_id)
 
 static const VMStateDescription vmstate_gic_irq_state = {
     .name = "arm_gic_irq_state",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .fields = (VMStateField[]) {
         VMSTATE_UINT8(enabled, gic_irq_state),
         VMSTATE_UINT8(pending, gic_irq_state),
+        VMSTATE_UINT8(sw_pending, gic_irq_state),
         VMSTATE_UINT8(active, gic_irq_state),
         VMSTATE_UINT8(level, gic_irq_state),
         VMSTATE_BOOL(model, gic_irq_state),
hw/intc/gic_internal.h +4 -0
index f9133b9..173c607 100644
@@ -43,6 +43,9 @@
 #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
 #define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
 #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
+#define GIC_SET_SW_PENDING(irq, cm) s->irq_state[irq].sw_pending |= (cm)
+#define GIC_CLEAR_SW_PENDING(irq, cm) s->irq_state[irq].sw_pending &= ~(cm)
+#define GIC_TEST_SW_PENDING(irq, cm) ((s->irq_state[irq].sw_pending & (cm)) != 0)
 #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
 #define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
 #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
@@ -65,6 +68,7 @@ typedef struct gic_irq_state {
     /* The enable bits are only banked for per-cpu interrupts.  */
     uint8_t enabled;
     uint8_t pending;
+    uint8_t sw_pending; /* keep track of GICD_ISPENDR and GICD_ICPENDR writes */
     uint8_t active;
     uint8_t level;
     bool model; /* 0 = N:N, 1 = 1:N */
Signature · 1 line
1.8.1.2
#5 lore raw

Re: [Qemu-devel] [PATCH] arm_gic: Keep track of GICD_CPENDR and GICD_SPENDR

Hi Christoffer,

Not related to the patch, for edge type of interrupt, will setting bit in ICD_SPENDR generate interrupt?

Thanks
-Bharat

Quoted 124 lines

-----Original Message-----
From: Christoffer Dall [mailto:[email protected]]
Sent: Wednesday, October 23, 2013 8:57 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]
Subject: [PATCH] arm_gic: Keep track of GICD_CPENDR and GICD_SPENDR

If software writes to the ISPENDR and sets the pending state of a level- triggered interrupt, the falling edge of the hardware input must not clear the pending state. Conversely, if software writes to the ICPENDR, the pending state of a level-triggered interrupt should only be cleared if the hardware input is not asserted.

This requires an extra state variable to keep track of software writes.

---
 hw/intc/arm_gic.c        | 20 +++++++++++++++++---
 hw/intc/arm_gic_common.c |  5 +++--
 hw/intc/gic_internal.h   |  4 ++++
 3 files changed, 24 insertions(+), 5 deletions(-)
hw/intc/arm_gic.c +17 -3
@@ -101,6 +101,12 @@ static void gic_clear_pending(GICState *s, int irq, int cm,
 uint8_t src)  {
     unsigned cpu;
 
+    /* If a level-triggered interrupt has been set to pending through the
+     * GICD_SPENDR, then a falling edge does not clear the pending state.
+     */
+    if (GIC_TEST_SW_PENDING(irq, cm))
+        return;
+
     GIC_CLEAR_PENDING(irq, cm);
     if (irq < GIC_NR_SGIS) {
         cpu = (unsigned)ffs(cm) - 1;
@@ -177,8 +183,9 @@ uint32_t gic_acknowledge_irq(GICState *s, int cpu)
     s->last_active[new_irq][cpu] = s->running_irq[cpu];
     /* Clear pending flags for both level and edge triggered interrupts.
        Level triggered IRQs will be reasserted once they become inactive.  */
-    gic_clear_pending(s, new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm,
-                                  GIC_SGI_SRC(new_irq, cpu));
+    cm = GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm;
+    GIC_CLEAR_SW_PENDING(new_irq, cm);
+    gic_clear_pending(s, new_irq, cm, GIC_SGI_SRC(new_irq, cpu));
     gic_set_running_irq(s, cpu, new_irq);
     DPRINTF("ACK %d\n", new_irq);
     return new_irq;
@@ -445,16 +452,23 @@ static void gic_dist_writeb(void *opaque, hwaddr offset,
         for (i = 0; i < 8; i++) {
             if (value & (1 << i)) {
                 GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
+                if (!GIC_TEST_TRIGGER(irq + i)) {
+                    GIC_SET_SW_PENDING(irq + i, GIC_TARGET(irq + i));
+                }
             }
         }
     } else if (offset < 0x300) {
+        int cm = (1 << cpu);
         /* Interrupt Clear Pending.  */
         irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
         if (irq >= s->num_irq)
             goto bad_reg;
         for (i = 0; i < 8; i++, irq++) {
             if (irq > GIC_NR_SGIS && value & (1 << i)) {
-                gic_clear_pending(s, irq, 1 << cpu, 0);
+                GIC_CLEAR_SW_PENDING(irq, cm);
+                if (GIC_TEST_TRIGGER(irq + i) || !GIC_TEST_LEVEL(irq, cm)) {
+                    GIC_CLEAR_PENDING(irq, cm);
+                }
             }
         }
     } else if (offset < 0x400) {
hw/intc/arm_gic_common.c index renamed from hw/intc/arm_gic_common.c +0 -0

1d3b738..7f0615f 100644

hw/intc/arm_gic_common.c +3 -2
@@ -43,11 +43,12 @@ static int gic_post_load(void *opaque, int version_id)
 
 static const VMStateDescription vmstate_gic_irq_state = {
     .name = "arm_gic_irq_state",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .fields = (VMStateField[]) {
         VMSTATE_UINT8(enabled, gic_irq_state),
         VMSTATE_UINT8(pending, gic_irq_state),
+        VMSTATE_UINT8(sw_pending, gic_irq_state),
         VMSTATE_UINT8(active, gic_irq_state),
         VMSTATE_UINT8(level, gic_irq_state),
         VMSTATE_BOOL(model, gic_irq_state), diff --git a/hw/intc/gic_internal.h

b/hw/intc/gic_internal.h index f9133b9..173c607 100644

hw/intc/gic_internal.h +0 -0
@@ -43,6 +43,9 @@
 #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)  #define

GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm) #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)

hw/intc/gic_internal.h +4 -0
+#define GIC_SET_SW_PENDING(irq, cm) s->irq_state[irq].sw_pending |=
+(cm) #define GIC_CLEAR_SW_PENDING(irq, cm) s->irq_state[irq].sw_pending
+&= ~(cm) #define GIC_TEST_SW_PENDING(irq, cm)
+((s->irq_state[irq].sw_pending & (cm)) != 0)
 #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)  #define

GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm) #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0) @@ -65,6 +68,7

@@ typedef struct gic_irq_state {
     /* The enable bits are only banked for per-cpu interrupts.  */
     uint8_t enabled;
     uint8_t pending;
+    uint8_t sw_pending; /* keep track of GICD_ISPENDR and GICD_ICPENDR
+ writes */
     uint8_t active;
     uint8_t level;
     bool model; /* 0 = N:N, 1 = 1:N */

--
1.8.1.2

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/cucslists/listinfo/kvmarm

#6 lore raw

Re: [Qemu-devel] [PATCH] arm_gic: Keep track of GICD_CPENDR and GICD_SPENDR

Quoted 4 lines · On Tue, Oct 29, 2013 at 04:10:59PM +0000, Bhushan Bharat-R65777 wrote:

Hi Christoffer,

Not related to the patch, for edge type of interrupt, will setting bit in ICD_SPENDR generate interrupt?

Hi Bharat,

That depends, if the interrupt was not previously pending, setting the bit for the corresponding interrupt will cause it to be pending, if the interrupt is also enabled and has high enough priority, then the interrupt gets forwarded to the CPU interface and if the corresponding CPU is not masking interrupts, it will cause an interrupt on the CPU.

The same is actually valid for level-triggered interrupts too, however, the difference is that if the interrupt was already pending, it has no effect whatsoever on edge-triggered interrupts, but level-triggered interrupts will stay pending even if the external line is deasserted.

-Christoffer

Quoted 127 lines
Quoted 125 lines

-----Original Message-----
From: Christoffer Dall [mailto:[email protected]]
Sent: Wednesday, October 23, 2013 8:57 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]
Subject: [PATCH] arm_gic: Keep track of GICD_CPENDR and GICD_SPENDR

If software writes to the ISPENDR and sets the pending state of a level- triggered interrupt, the falling edge of the hardware input must not clear the pending state. Conversely, if software writes to the ICPENDR, the pending state of a level-triggered interrupt should only be cleared if the hardware input is not asserted.

This requires an extra state variable to keep track of software writes.

---
 hw/intc/arm_gic.c        | 20 +++++++++++++++++---
 hw/intc/arm_gic_common.c |  5 +++--
 hw/intc/gic_internal.h   |  4 ++++
 3 files changed, 24 insertions(+), 5 deletions(-)
hw/intc/arm_gic.c +17 -3
@@ -101,6 +101,12 @@ static void gic_clear_pending(GICState *s, int irq, int cm,
 uint8_t src)  {
     unsigned cpu;
 
+    /* If a level-triggered interrupt has been set to pending through the
+     * GICD_SPENDR, then a falling edge does not clear the pending state.
+     */
+    if (GIC_TEST_SW_PENDING(irq, cm))
+        return;
+
     GIC_CLEAR_PENDING(irq, cm);
     if (irq < GIC_NR_SGIS) {
         cpu = (unsigned)ffs(cm) - 1;
@@ -177,8 +183,9 @@ uint32_t gic_acknowledge_irq(GICState *s, int cpu)
     s->last_active[new_irq][cpu] = s->running_irq[cpu];
     /* Clear pending flags for both level and edge triggered interrupts.
        Level triggered IRQs will be reasserted once they become inactive.  */
-    gic_clear_pending(s, new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm,
-                                  GIC_SGI_SRC(new_irq, cpu));
+    cm = GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm;
+    GIC_CLEAR_SW_PENDING(new_irq, cm);
+    gic_clear_pending(s, new_irq, cm, GIC_SGI_SRC(new_irq, cpu));
     gic_set_running_irq(s, cpu, new_irq);
     DPRINTF("ACK %d\n", new_irq);
     return new_irq;
@@ -445,16 +452,23 @@ static void gic_dist_writeb(void *opaque, hwaddr offset,
         for (i = 0; i < 8; i++) {
             if (value & (1 << i)) {
                 GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
+                if (!GIC_TEST_TRIGGER(irq + i)) {
+                    GIC_SET_SW_PENDING(irq + i, GIC_TARGET(irq + i));
+                }
             }
         }
     } else if (offset < 0x300) {
+        int cm = (1 << cpu);
         /* Interrupt Clear Pending.  */
         irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
         if (irq >= s->num_irq)
             goto bad_reg;
         for (i = 0; i < 8; i++, irq++) {
             if (irq > GIC_NR_SGIS && value & (1 << i)) {
-                gic_clear_pending(s, irq, 1 << cpu, 0);
+                GIC_CLEAR_SW_PENDING(irq, cm);
+                if (GIC_TEST_TRIGGER(irq + i) || !GIC_TEST_LEVEL(irq, cm)) {
+                    GIC_CLEAR_PENDING(irq, cm);
+                }
             }
         }
     } else if (offset < 0x400) {
hw/intc/arm_gic_common.c index renamed from hw/intc/arm_gic_common.c +0 -0

1d3b738..7f0615f 100644

hw/intc/arm_gic_common.c +3 -2
@@ -43,11 +43,12 @@ static int gic_post_load(void *opaque, int version_id)
 
 static const VMStateDescription vmstate_gic_irq_state = {
     .name = "arm_gic_irq_state",
-    .version_id = 1,
-    .minimum_version_id = 1,
+    .version_id = 2,
+    .minimum_version_id = 2,
     .fields = (VMStateField[]) {
         VMSTATE_UINT8(enabled, gic_irq_state),
         VMSTATE_UINT8(pending, gic_irq_state),
+        VMSTATE_UINT8(sw_pending, gic_irq_state),
         VMSTATE_UINT8(active, gic_irq_state),
         VMSTATE_UINT8(level, gic_irq_state),
         VMSTATE_BOOL(model, gic_irq_state), diff --git a/hw/intc/gic_internal.h

b/hw/intc/gic_internal.h index f9133b9..173c607 100644

hw/intc/gic_internal.h +0 -0
@@ -43,6 +43,9 @@
 #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)  #define

GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm) #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)

hw/intc/gic_internal.h +4 -0
+#define GIC_SET_SW_PENDING(irq, cm) s->irq_state[irq].sw_pending |=
+(cm) #define GIC_CLEAR_SW_PENDING(irq, cm) s->irq_state[irq].sw_pending
+&= ~(cm) #define GIC_TEST_SW_PENDING(irq, cm)
+((s->irq_state[irq].sw_pending & (cm)) != 0)
 #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)  #define

GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm) #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0) @@ -65,6 +68,7

@@ typedef struct gic_irq_state {
     /* The enable bits are only banked for per-cpu interrupts.  */
     uint8_t enabled;
     uint8_t pending;
+    uint8_t sw_pending; /* keep track of GICD_ISPENDR and GICD_ICPENDR
+ writes */
     uint8_t active;
     uint8_t level;
     bool model; /* 0 = N:N, 1 = 1:N */

--
1.8.1.2

_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/cucslists/listinfo/kvmarm

Signature · 1 line
Christoffer
#7 lore raw

RFC PATCH v2 2/6 hw: arm_gic: Introduce GIC_SET_PRIORITY macro

In reply to Christoffer Dall

To make the code slightly cleaner to look at and make the save/restore code easier to understand, introduce this macro to set the priority of interrupts.

---
 hw/intc/arm_gic.c      |   15 ++++++++++-----
 hw/intc/gic_internal.h |    1 +
 2 files changed, 11 insertions(+), 5 deletions(-)
hw/intc/arm_gic.c +10 -5
index c7a24d5..7eaa55f 100644
@@ -169,6 +169,15 @@ uint32_t gic_acknowledge_irq(GICState *s, int cpu)
     return new_irq;
 }
 
+void gic_set_priority(GICState *s, int cpu, int irq, uint8_t val)
+{
+    if (irq < GIC_INTERNAL) {
+        s->priority1[irq][cpu] = val;
+    } else {
+        s->priority2[(irq) - GIC_INTERNAL] = val;
+    }
+}
+
 void gic_complete_irq(GICState *s, int cpu, int irq)
 {
     int update = 0;
@@ -444,11 +453,7 @@ static void gic_dist_writeb(void *opaque, hwaddr offset,
         irq = (offset - 0x400) + GIC_BASE_IRQ;
         if (irq >= s->num_irq)
             goto bad_reg;
-        if (irq < GIC_INTERNAL) {
-            s->priority1[irq][cpu] = value;
-        } else {
-            s->priority2[irq - GIC_INTERNAL] = value;
-        }
+        gic_set_priority(s, cpu, irq, value);
     } else if (offset < 0xc00) {
         /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
          * annoying exception of the 11MPCore's GIC.
hw/intc/gic_internal.h +1 -0
index b3788a8..09e7722 100644
@@ -111,6 +111,7 @@ uint32_t gic_acknowledge_irq(GICState *s, int cpu);
 void gic_complete_irq(GICState *s, int cpu, int irq);
 void gic_update(GICState *s);
 void gic_init_irqs_and_distributor(GICState *s, int num_irq);
+void gic_set_priority(GICState *s, int cpu, int irq, uint8_t val);
 
 #define TYPE_ARM_GIC_COMMON "arm_gic_common"
 #define ARM_GIC_COMMON(obj) \
Signature · 1 line
1.7.10.4
Reviewed-by on v2 2/6
#8 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/6] hw: arm_gic: Introduce GIC_SET_PRIORITY macro

Quoted 5 lines · Christoffer Dall

To make the code slightly cleaner to look at and make the save/restore code easier to understand, introduce this macro to set the priority of interrupts.

Commit message and subject need updating since it's not a macro and not GIC_SET_PRIORITY any more, but otherwise

-- PMM

#9 lore raw

RFC PATCH v2 3/6 hw: arm_gic: Keep track of SGI sources

In reply to Christoffer Dall

Right now the arm gic emulation doesn't keep track of the source of an SGI (which apparently Linux guests don't use, or they're fine with assuming CPU 0 always).

Add the necessary matrix on the GICState structure and maintain the data when setting and clearing the pending state of an IRQ.

Note that we always choose to present the source as the lowest-numbered CPU in case multiple cores have signalled the same SGI number to a core on the system.

---

Changelog [v2]:
- Fixed endless loop bug
- Bump version_id and minimum_version_id on vmstate struct

---
 hw/intc/arm_gic.c        |   41 ++++++++++++++++++++++++++++++++---------
 hw/intc/arm_gic_common.c |    5 +++--
 hw/intc/gic_internal.h   |    3 +++
 3 files changed, 38 insertions(+), 11 deletions(-)
hw/intc/arm_gic.c +32 -9
index 7eaa55f..6470d37 100644
@@ -97,6 +97,20 @@ void gic_set_pending_private(GICState *s, int cpu, int irq)
     gic_update(s);
 }
 
+static void gic_clear_pending(GICState *s, int irq, int cm, uint8_t src)
+{
+    unsigned cpu;
+
+    GIC_CLEAR_PENDING(irq, cm);
+    if (irq < GIC_NR_SGIS) {
+        cpu = (unsigned)ffs(cm) - 1;
+        while (cpu < NCPU) {
+            s->sgi_source[irq][cpu] &= ~(1 << src);
+            cpu = (unsigned)ffs(cm) - 1;
+        }
+    }
+}
+
 /* Process a change in an external IRQ input.  */
 static void gic_set_irq(void *opaque, int irq, int level)
 {
@@ -132,7 +146,7 @@ static void gic_set_irq(void *opaque, int irq, int level)
         GIC_SET_PENDING(irq, target);
     } else {
         if (!GIC_TEST_TRIGGER(irq)) {
-            GIC_CLEAR_PENDING(irq, target);
+            gic_clear_pending(s, irq, target, 0);
         }
         GIC_CLEAR_LEVEL(irq, cm);
     }
@@ -163,7 +177,8 @@ uint32_t gic_acknowledge_irq(GICState *s, int cpu)
     s->last_active[new_irq][cpu] = s->running_irq[cpu];
     /* Clear pending flags for both level and edge triggered interrupts.
        Level triggered IRQs will be reasserted once they become inactive.  */
-    GIC_CLEAR_PENDING(new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm);
+    gic_clear_pending(s, new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm,
+                                  GIC_SGI_SRC(new_irq, cpu));
     gic_set_running_irq(s, cpu, new_irq);
     DPRINTF("ACK %d\n", new_irq);
     return new_irq;
@@ -437,12 +452,9 @@ static void gic_dist_writeb(void *opaque, hwaddr offset,
         irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
         if (irq >= s->num_irq)
             goto bad_reg;
-        for (i = 0; i < 8; i++) {
-            /* ??? This currently clears the pending bit for all CPUs, even
-               for per-CPU interrupts.  It's unclear whether this is the
-               corect behavior.  */
-            if (value & (1 << i)) {
-                GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
+        for (i = 0; i < 8; i++, irq++) {
+            if (irq > GIC_NR_SGIS && value & (1 << i)) {
+                gic_clear_pending(s, irq, 1 << cpu, 0);
             }
         }
     } else if (offset < 0x400) {
@@ -515,6 +527,7 @@ static void gic_dist_writel(void *opaque, hwaddr offset,
         int cpu;
         int irq;
         int mask;
+        unsigned target_cpu;
 
         cpu = gic_get_current_cpu(s);
         irq = value & 0x3ff;
@@ -534,6 +547,12 @@ static void gic_dist_writel(void *opaque, hwaddr offset,
             break;
         }
         GIC_SET_PENDING(irq, mask);
+        target_cpu = (unsigned)ffs(mask) - 1;
+        while (target_cpu < NCPU) {
+            s->sgi_source[irq][target_cpu] |= (1 << cpu);
+            mask &= ~(1 << target_cpu);
+            target_cpu = (unsigned)ffs(mask) - 1;
+        }
         gic_update(s);
         return;
     }
@@ -551,6 +570,8 @@ static const MemoryRegionOps gic_dist_ops = {
 
 static uint32_t gic_cpu_read(GICState *s, int cpu, int offset)
 {
+    int value;
+
     switch (offset) {
     case 0x00: /* Control */
         return s->cpu_enabled[cpu];
@@ -560,7 +581,9 @@ static uint32_t gic_cpu_read(GICState *s, int cpu, int offset)
         /* ??? Not implemented.  */
         return 0;
     case 0x0c: /* Acknowledge */
-        return gic_acknowledge_irq(s, cpu);
+        value = gic_acknowledge_irq(s, cpu);
+        value |= (GIC_SGI_SRC(value, cpu) & 0x7) << 10;
+        return value;
     case 0x14: /* Running Priority */
         return s->running_priority[cpu];
     case 0x18: /* Highest Pending Interrupt */
hw/intc/arm_gic_common.c +3 -2
index 709b5c2..0657e8b 100644
@@ -58,8 +58,8 @@ static const VMStateDescription vmstate_gic_irq_state = {
 
 static const VMStateDescription vmstate_gic = {
     .name = "arm_gic",
-    .version_id = 4,
-    .minimum_version_id = 4,
+    .version_id = 5,
+    .minimum_version_id = 5,
     .pre_save = gic_pre_save,
     .post_load = gic_post_load,
     .fields = (VMStateField[]) {
@@ -71,6 +71,7 @@ static const VMStateDescription vmstate_gic = {
         VMSTATE_UINT8_2DARRAY(priority1, GICState, GIC_INTERNAL, NCPU),
         VMSTATE_UINT8_ARRAY(priority2, GICState, GIC_MAXIRQ - GIC_INTERNAL),
         VMSTATE_UINT16_2DARRAY(last_active, GICState, GIC_MAXIRQ, NCPU),
+        VMSTATE_UINT8_2DARRAY(sgi_source, GICState, GIC_NR_SGIS, NCPU),
         VMSTATE_UINT16_ARRAY(priority_mask, GICState, NCPU),
         VMSTATE_UINT16_ARRAY(running_irq, GICState, NCPU),
         VMSTATE_UINT16_ARRAY(running_priority, GICState, NCPU),
hw/intc/gic_internal.h +3 -0
index 09e7722..5b53242 100644
@@ -27,6 +27,7 @@
 #define GIC_MAXIRQ 1020
 /* First 32 are private to each CPU (SGIs and PPIs). */
 #define GIC_INTERNAL 32
+#define GIC_NR_SGIS  16
 /* Maximum number of possible CPU interfaces, determined by GIC architecture */
 #define NCPU 8
 
@@ -58,6 +59,7 @@
                                     s->priority1[irq][cpu] :            \
                                     s->priority2[(irq) - GIC_INTERNAL])
 #define GIC_TARGET(irq) s->irq_target[irq]
+#define GIC_SGI_SRC(irq, cpu) (((irq) < GIC_NR_SGIS) ? ffs(s->sgi_source[irq][cpu]) - 1 : 0)
 
 typedef struct gic_irq_state {
     /* The enable bits are only banked for per-cpu interrupts.  */
@@ -83,6 +85,7 @@ typedef struct GICState {
     uint8_t priority1[GIC_INTERNAL][NCPU];
     uint8_t priority2[GIC_MAXIRQ - GIC_INTERNAL];
     uint16_t last_active[GIC_MAXIRQ][NCPU];
+    uint8_t sgi_source[GIC_NR_SGIS][NCPU];
 
     uint16_t priority_mask[NCPU];
     uint16_t running_irq[NCPU];
Signature · 1 line
1.7.10.4
#10 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/6] hw: arm_gic: Keep track of SGI sources

Quoted 10 lines · Christoffer Dall

Right now the arm gic emulation doesn't keep track of the source of an SGI (which apparently Linux guests don't use, or they're fine with assuming CPU 0 always).

Add the necessary matrix on the GICState structure and maintain the data when setting and clearing the pending state of an IRQ.

Note that we always choose to present the source as the lowest-numbered CPU in case multiple cores have signalled the same SGI number to a core on the system.

Shouldn't the state you have in sgi_source[][] be surfaced as the GICD_CPENDSGIR/GICD_SPENDSGIR registers [for a v2 GIC; they don't exist in v1]? It might then be better to represent the state in our data structures in the same layout as the registers.

Quoted 19 lines

---

Changelog [v2]:
- Fixed endless loop bug
- Bump version_id and minimum_version_id on vmstate struct

---
 hw/intc/arm_gic.c        |   41 ++++++++++++++++++++++++++++++++---------
 hw/intc/arm_gic_common.c |    5 +++--
 hw/intc/gic_internal.h   |    3 +++
 3 files changed, 38 insertions(+), 11 deletions(-)
hw/intc/arm_gic.c +0 -0
index 7eaa55f..6470d37 100644

hw/intc/arm_gic.c void gic_set_pending_private(GICState *s, int cpu, int irq) · line 101

Quoted 5 lines
hw/intc/arm_gic.c +2 -0
@@ -97,6 +97,20 @@ void gic_set_pending_private(GICState *s, int cpu, int irq)
     gic_update(s);
 }
 
+static void gic_clear_pending(GICState *s, int irq, int cm, uint8_t src)
+{

I think that in the cases where we pass in 0 for src that the irq can't be < GIC_NR_SGIS.

hw/intc/arm_gic.c void gic_set_pending_private(GICState *s, int cpu, int irq) · line 106

Quoted 5 lines
hw/intc/arm_gic.c +5 -0
+    unsigned cpu;
+
+    GIC_CLEAR_PENDING(irq, cm);
+    if (irq < GIC_NR_SGIS) {
+        cpu = (unsigned)ffs(cm) - 1;

If you used ctz32() rather than ffs() it would save you having to subtract one all the time. Also, those unsigned casts are pretty
ugly: better to just make 'cpu' an int...

hw/intc/arm_gic.c void gic_set_pending_private(GICState *s, int cpu, int irq) · line 110

Quoted 4 lines
hw/intc/arm_gic.c +4 -0
+        while (cpu < NCPU) {
+            s->sgi_source[irq][cpu] &= ~(1 << src);
+            cpu = (unsigned)ffs(cm) - 1;
+        }

...this still seems to be an infinite loop: cm isn't modified inside the loop so cpu will always have the same value each time.

     610:       eb fe                   jmp    610 <gic_clear_pending+0x50>

hw/intc/arm_gic.c void gic_set_pending_private(GICState *s, int cpu, int irq) · line 111

Quoted 1 line
hw/intc/arm_gic.c +1 -0
+    }

Are you sure the logic in this function is right? (ie that we should only clear the sgi_source[][] bit for this source, and not completely? If nothing else, I think the interrupt should stay pending if some other source cpu still wants it to be pending. That is, I think we need to track the pending status separately for each (irq,target-cpu,source-cpu) separately for SGIs. (I'm not totally sure I have this right though, the spec is quite confusing.)

hw/intc/arm_gic.c line 62

Quoted 11 lines
hw/intc/arm_gic.c +3 -0
+}
+
 
 /* Maximum number of possible CPU interfaces, determined by GIC architecture */
 #define NCPU 8
@@ -58,6 +59,7 @@
                                     s->priority1[irq][cpu] :            \
                                     s->priority2[(irq) - GIC_INTERNAL])
 #define GIC_TARGET(irq) s->irq_target[irq]
+#define GIC_SGI_SRC(irq, cpu) (((irq) < GIC_NR_SGIS) ? ffs(s->sgi_source[irq][cpu]) - 1 : 0)

WARNING: line over 80 characters
#161: FILE: hw/intc/gic_internal.h:62:
+#define GIC_SGI_SRC(irq, cpu) (((irq) < GIC_NR_SGIS) ? ffs(s->sgi_source[irq][cpu]) - 1 : 0)

-- PMM

#11 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/6] hw: arm_gic: Keep track of SGI sources

Quoted 8 lines · Peter Maydell

Are you sure the logic in this function is right? (ie that we should only clear the sgi_source[][] bit for this source, and not completely? If nothing else, I think the interrupt should stay pending if some other source cpu still wants it to be pending. That is, I think we need to track the pending status separately for each (irq,target-cpu,source-cpu) separately for SGIs. (I'm not totally sure I have this right though, the spec is quite confusing.)

Having spoken to Marc I'm now a bit less confused. For SGIs:
* there is effectively a pending bit for each (irq-number, source-cpu, target-cpu) tuple
* for a v2 GIC these are guest-visible in GICD_CPENDSGIRn and GICD_SPENDSGIRn; for a pre-v2 GIC the state still exists, it's just not guest-visible
* the overall "pending state" bit in GICD_ISPENDRn and GICD_ICPENDRn is effectively the logical OR of the pending bits for each source-cpu; writes to this bit are ignored
* when the guest reads from GICC_IAR, if the SGI is pending for multiple source-cpus for this target-cpu then we clear the pending bit for the (int, src, tgt) tuple in question, but it will still be set for the other (int,src,tgt) tuples for the other source-cpus (and so the GICD_ISPENDRn bit will still be set)

Tangentially, I notice that we don't correctly handle the PENDING bit for level triggered interrupts, since we do:

/* Clear pending flags for both level and edge triggered interrupts.
Level triggered IRQs will be reasserted once they become inactive. */
gic_clear_pending(s, new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm,
GIC_SGI_SRC(new_irq, cpu));

in gic_acknowledge_irq(). This is wrong, because section 3.2.4 is clear for a level triggered interrupt that if the interrupt signal remains asserted (which it usually will be) then we go from Pending to Active+Pending (whereas our current implementation goes from Pending to Active and then resets Pending later in gic_complete_irq()).

-- PMM

#12 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/6] hw: arm_gic: Keep track of SGI sources

Quoted 1 line · Peter Maydell

On 14 October 2013 16:36, Peter Maydell <[email protected]> wrote:

[...]

Quoted 10 lines

Tangentially, I notice that we don't correctly handle the PENDING bit for level triggered interrupts, since we do:

/* Clear pending flags for both level and edge triggered interrupts.
Level triggered IRQs will be reasserted once they become inactive. */
gic_clear_pending(s, new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm,
GIC_SGI_SRC(new_irq, cpu));

Quoted 7 lines

in gic_acknowledge_irq(). This is wrong, because section 3.2.4 is clear for a level triggered interrupt that if the interrupt signal remains asserted (which it usually will be) then we go from Pending to Active+Pending (whereas our current implementation goes from Pending to Active and then resets Pending later in gic_complete_irq()).

Yes, I will send this patch to address this as part of the revised series:

hw/intc/arm_gic.c +8 -4
index bf1eb02..fce66c6 100644
@@ -175,10 +180,15 @@ uint32_t gic_acknowledge_irq(GICState *s, int cpu)
         return 1023;
     }
     s->last_active[new_irq][cpu] = s->running_irq[cpu];
-    /* Clear pending flags for both level and edge triggered interrupts.
-       Level triggered IRQs will be reasserted once they become inactive.  */
-    gic_clear_pending(s, new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm,
-                                  GIC_SGI_SRC(new_irq, cpu));
+    /* Clear pending flags for edge-triggered and non-asserted level-triggered
+     * interrupts.
+     */
+    cm = GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm;
+    if (GIC_TEST_TRIGGER(new_irq) || !GIC_TEST_LEVEL(new_irq, cm)) {
+        gic_clear_pending(s, new_irq, cm, GIC_SGI_SRC(new_irq, cpu));
+    }
+
     gic_set_running_irq(s, cpu, new_irq);
     DPRINTF("ACK %d\n", new_irq);
     return new_irq;
Signature · 1 line
Christoffer
#13 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/6] hw: arm_gic: Keep track of SGI sources

In reply to Peter Maydell
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Christoffer Dall

Right now the arm gic emulation doesn't keep track of the source of an SGI (which apparently Linux guests don't use, or they're fine with assuming CPU 0 always).

Add the necessary matrix on the GICState structure and maintain the data when setting and clearing the pending state of an IRQ.

Quoted 9 lines
Quoted 3 lines

Note that we always choose to present the source as the lowest-numbered CPU in case multiple cores have signalled the same SGI number to a core on the system.

Shouldn't the state you have in sgi_source[][] be surfaced as the GICD_CPENDSGIR/GICD_SPENDSGIR registers [for a v2 GIC; they don't exist in v1]? It might then be better to represent the state in our data structures in the same layout as the registers.

Hmm, those registers actually don't represent which CPU is the *source* of a given SGI. I think the array I propose is quite reasonable...

Quoted 22 lines
Quoted 22 lines

---

Changelog [v2]:
- Fixed endless loop bug
- Bump version_id and minimum_version_id on vmstate struct

---
 hw/intc/arm_gic.c        |   41 ++++++++++++++++++++++++++++++++---------
 hw/intc/arm_gic_common.c |    5 +++--
 hw/intc/gic_internal.h   |    3 +++
 3 files changed, 38 insertions(+), 11 deletions(-)
hw/intc/arm_gic.c +0 -0
index 7eaa55f..6470d37 100644
@@ -97,6 +97,20 @@ void gic_set_pending_private(GICState *s, int cpu, int irq)
     gic_update(s);
 }
 
Quoted 6 lines

hw/intc/arm_gic.c void gic_set_pending_private(GICState *s, int cpu, int irq) · line 101

Quoted 2 lines
hw/intc/arm_gic.c +2 -0
+static void gic_clear_pending(GICState *s, int irq, int cm, uint8_t src)
+{

I think that in the cases where we pass in 0 for src that the irq can't be < GIC_NR_SGIS.

not quite sure what you mean by this comment, we pass in 0 for src in two cases: 1) when we are dealing with something else than an SGI (irq >= GIC_NR_SGIS), but we could pass whatever, the value doesn't make sense here 2) when we are dealing with an SGI, the source can be 0 up to s->num_cpu - 1.

Quoted 10 lines

hw/intc/arm_gic.c void gic_set_pending_private(GICState *s, int cpu, int irq) · line 106

Quoted 5 lines
hw/intc/arm_gic.c +5 -0
+    unsigned cpu;
+
+    GIC_CLEAR_PENDING(irq, cm);
+    if (irq < GIC_NR_SGIS) {
+        cpu = (unsigned)ffs(cm) - 1;

If you used ctz32() rather than ffs() it would save you having to subtract one all the time. Also, those unsigned casts are pretty
ugly: better to just make 'cpu' an int...

yeah, that was quite ridiculous.

Quoted 10 lines

hw/intc/arm_gic.c void gic_set_pending_private(GICState *s, int cpu, int irq) · line 110

Quoted 4 lines
hw/intc/arm_gic.c +4 -0
+        while (cpu < NCPU) {
+            s->sgi_source[irq][cpu] &= ~(1 << src);
+            cpu = (unsigned)ffs(cm) - 1;
+        }

...this still seems to be an infinite loop: cm isn't modified inside the loop so cpu will always have the same value each time.

     610:       eb fe                   jmp    610 <gic_clear_pending+0x50>

if only I had for_each_set_bit - does QEMU have something sane for this? Anyway, I tried again to fix this. Thanks for spotting my broken fix for my broken code.

Quoted 11 lines

hw/intc/arm_gic.c void gic_set_pending_private(GICState *s, int cpu, int irq) · line 111

Quoted 1 line
hw/intc/arm_gic.c +1 -0
+    }

Are you sure the logic in this function is right? (ie that we should only clear the sgi_source[][] bit for this source, and not completely? If nothing else, I think the interrupt should stay pending if some other source cpu still wants it to be pending. That is, I think we need to track the pending status separately for each (irq,target-cpu,source-cpu) separately for SGIs. (I'm not totally sure I have this right though, the spec is quite confusing.)

no, you're right, for SGIs we need to loop through the sgi_source array and make sure the irq is not pending on any CPUs from any CPUs.

Quoted 6 lines
Quoted 6 lines
hw/intc/arm_gic.c +2 -0
+}
+
 
 /* Maximum number of possible CPU interfaces, determined by GIC architecture */
 #define NCPU 8
Quoted 11 lines

hw/intc/arm_gic.c line 62

Quoted 5 lines
hw/intc/arm_gic.c +1 -0
@@ -58,6 +59,7 @@
                                     s->priority1[irq][cpu] :            \
                                     s->priority2[(irq) - GIC_INTERNAL])
 #define GIC_TARGET(irq) s->irq_target[irq]
+#define GIC_SGI_SRC(irq, cpu) (((irq) < GIC_NR_SGIS) ? ffs(s->sgi_source[irq][cpu]) - 1 : 0)

WARNING: line over 80 characters
#161: FILE: hw/intc/gic_internal.h:62:
+#define GIC_SGI_SRC(irq, cpu) (((irq) < GIC_NR_SGIS) ? ffs(s->sgi_source[irq][cpu]) - 1 : 0)

Thans,
-Christoffer

#14 lore raw

RFC PATCH v2 4/6 arm_gic: Support setting/getting binary point reg

In reply to Christoffer Dall

Add a binary_point field to the gic emulation structure and support setting/getting this register now when we have it. We don't actually support interrupt grouping yet, oh well.

Changelog [v2]:
- Renamed binary_point to bpr and abpr
- Added GICC_ABPR read-as-write logic for TCG

---
 hw/intc/arm_gic.c        |   10 +++++++---
 hw/intc/arm_gic_common.c |    6 ++++--
 hw/intc/gic_internal.h   |    7 +++++++
 3 files changed, 18 insertions(+), 5 deletions(-)
hw/intc/arm_gic.c +7 -3
index 6470d37..d1ddac1 100644
@@ -578,8 +578,7 @@ static uint32_t gic_cpu_read(GICState *s, int cpu, int offset)
     case 0x04: /* Priority mask */
         return s->priority_mask[cpu];
     case 0x08: /* Binary Point */
-        /* ??? Not implemented.  */
-        return 0;
+        return s->bpr[cpu];
     case 0x0c: /* Acknowledge */
         value = gic_acknowledge_irq(s, cpu);
         value |= (GIC_SGI_SRC(value, cpu) & 0x7) << 10;
@@ -588,6 +587,8 @@ static uint32_t gic_cpu_read(GICState *s, int cpu, int offset)
         return s->running_priority[cpu];
     case 0x18: /* Highest Pending Interrupt */
         return s->current_pending[cpu];
+    case 0x1c: /* Aliased Binary Point */
+        return s->abpr[cpu];
     default:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "gic_cpu_read: Bad offset %x\n", (int)offset);
@@ -606,10 +607,13 @@ static void gic_cpu_write(GICState *s, int cpu, int offset, uint32_t value)
         s->priority_mask[cpu] = (value & 0xff);
         break;
     case 0x08: /* Binary Point */
-        /* ??? Not implemented.  */
+        s->bpr[cpu] = (value & 0x7);
         break;
     case 0x10: /* End Of Interrupt */
         return gic_complete_irq(s, cpu, value & 0x3ff);
+    case 0x1c: /* Aliased Binary Point */
+        s->abpr[cpu] = (value & 0x7);
+        break;
     default:
         qemu_log_mask(LOG_GUEST_ERROR,
                       "gic_cpu_write: Bad offset %x\n", (int)offset);
hw/intc/arm_gic_common.c +4 -2
index 0657e8b..5449d77 100644
@@ -58,8 +58,8 @@ static const VMStateDescription vmstate_gic_irq_state = {
 
 static const VMStateDescription vmstate_gic = {
     .name = "arm_gic",
-    .version_id = 5,
-    .minimum_version_id = 5,
+    .version_id = 6,
+    .minimum_version_id = 6,
     .pre_save = gic_pre_save,
     .post_load = gic_post_load,
     .fields = (VMStateField[]) {
@@ -76,6 +76,8 @@ static const VMStateDescription vmstate_gic = {
         VMSTATE_UINT16_ARRAY(running_irq, GICState, NCPU),
         VMSTATE_UINT16_ARRAY(running_priority, GICState, NCPU),
         VMSTATE_UINT16_ARRAY(current_pending, GICState, NCPU),
+        VMSTATE_UINT8_ARRAY(bpr, GICState, NCPU),
+        VMSTATE_UINT8_ARRAY(abpr, GICState, NCPU),
         VMSTATE_END_OF_LIST()
     }
 };
hw/intc/gic_internal.h +7 -0
index 5b53242..758b85a 100644
@@ -92,6 +92,13 @@ typedef struct GICState {
     uint16_t running_priority[NCPU];
     uint16_t current_pending[NCPU];
 
+    /* We present the GICv2 without security extensions to a guest and
+     * therefore the guest can configure the GICC_CTLR to configure group 1
+     * binary point in the abpr.
+     */
+    uint8_t  bpr[NCPU];
+    uint8_t  abpr[NCPU];
+
     uint32_t num_cpu;
 
     MemoryRegion iomem; /* Distributor */
Signature · 1 line
1.7.10.4
#15 lore raw

Re: [Qemu-devel] [RFC PATCH v2 4/6] arm_gic: Support setting/getting binary point reg

hw/intc/arm_gic.c static void gic_cpu_write(GICState *s, int cpu, int offset, uint32_t value) · line 616

Quoted 12 lines · Christoffer Dall
hw/intc/arm_gic.c +4 -1
@@ -606,10 +607,13 @@ static void gic_cpu_write(GICState *s, int cpu, int offset, uint32_t value)
         s->priority_mask[cpu] = (value & 0xff);
         break;
     case 0x08: /* Binary Point */
-        /* ??? Not implemented.  */
+        s->bpr[cpu] = (value & 0x7);
         break;
     case 0x10: /* End Of Interrupt */
         return gic_complete_irq(s, cpu, value & 0x3ff);
+    case 0x1c: /* Aliased Binary Point */
+        s->abpr[cpu] = (value & 0x7);
+        break;

The ABPR should RAZ/WI for GICs prior to v2, so this needs to be guarded with "if (s->revision >= 2) { ...".

Otherwise looks good.

-- PMM

#16 lore raw

RFC PATCH v2 5/6 vmstate: Add uint32 2D-array support

In reply to Christoffer Dall

Add support for saving VMtate of 2D arrays of uint32 values.

---
 include/migration/vmstate.h |    6 ++++++
 1 file changed, 6 insertions(+)
include/migration/vmstate.h +6 -0
index 1c31b5d..e5538c7 100644
@@ -633,9 +633,15 @@ extern const VMStateInfo vmstate_info_bitmap;
 #define VMSTATE_UINT32_ARRAY_V(_f, _s, _n, _v)                        \
     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint32, uint32_t)
 
+#define VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, _v)                \
+    VMSTATE_2DARRAY(_f, _s, _n1, _n2, _v, vmstate_info_uint32, uint32_t)
+
 #define VMSTATE_UINT32_ARRAY(_f, _s, _n)                              \
     VMSTATE_UINT32_ARRAY_V(_f, _s, _n, 0)
 
+#define VMSTATE_UINT32_2DARRAY(_f, _s, _n1, _n2)                      \
+    VMSTATE_UINT32_2DARRAY_V(_f, _s, _n1, _n2, 0)
+
 #define VMSTATE_UINT64_ARRAY_V(_f, _s, _n, _v)                        \
     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint64, uint64_t)
 
Signature · 1 line
1.7.10.4
Reviewed-by on v2 5/6
#17 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] vmstate: Add uint32 2D-array support

Quoted 1 line · Christoffer Dall

Add support for saving VMtate of 2D arrays of uint32 values.

"VMState". Otherwise:

-- PMM

#18 lore raw

RFC PATCH v2 6/6 hw: arm_gic_kvm: Add KVM VGIC save/restore logic

In reply to Christoffer Dall

Save and restore the ARM KVM VGIC state from the kernel. We rely on QEMU to marshal the GICState data structure and therefore simply synchronize the kernel state with the QEMU emulated state in both directions.

We take some care on the restore path to check the VGIC has been configured with enough IRQs and CPU interfaces that we can properly restore the state, and for separate set/clear registers we first fully clear the registers and then set the required bits.

Changelog [v2]:
- Remove num_irq from GIC VMstate structure
- Increment GIC VMstate version number
- Use extract32/deposit32 for bit-field modifications
- Address other smaller review comments
- Renames kvm_arm_gic_dist_[readr/writer] functions to kvm_dist_[get/put] and shortened other function names
- Use concrete format for APRn

---
 hw/intc/arm_gic_common.c |    5 +-
 hw/intc/arm_gic_kvm.c    |  424 +++++++++++++++++++++++++++++++++++++++++++++-
 hw/intc/gic_internal.h   |    8 +
 3 files changed, 433 insertions(+), 4 deletions(-)
hw/intc/arm_gic_common.c +3 -2
index 5449d77..1d3b738 100644
@@ -58,8 +58,8 @@ static const VMStateDescription vmstate_gic_irq_state = {
 
 static const VMStateDescription vmstate_gic = {
     .name = "arm_gic",
-    .version_id = 6,
-    .minimum_version_id = 6,
+    .version_id = 7,
+    .minimum_version_id = 7,
     .pre_save = gic_pre_save,
     .post_load = gic_post_load,
     .fields = (VMStateField[]) {
@@ -78,6 +78,7 @@ static const VMStateDescription vmstate_gic = {
         VMSTATE_UINT16_ARRAY(current_pending, GICState, NCPU),
         VMSTATE_UINT8_ARRAY(bpr, GICState, NCPU),
         VMSTATE_UINT8_ARRAY(abpr, GICState, NCPU),
+        VMSTATE_UINT32_2DARRAY(apr, GICState, 4, NCPU),
         VMSTATE_END_OF_LIST()
     }
 };
hw/intc/arm_gic_kvm.c +422 -2

448 lines not on this page · raw message

hw/intc/gic_internal.h +8 -0
index 758b85a..f9133b9 100644
@@ -99,6 +99,14 @@ typedef struct GICState {
     uint8_t  bpr[NCPU];
     uint8_t  abpr[NCPU];
 
+    /* The APR is implementation defined, so we choose a layout identical to
+     * the KVM ABI layout for QEMU's implementation of the gic:
+     * If one or more interrupts for preemption level X are active, then
+     *   APRn[X mod 32] == 0b1,  where n = X / 32
+     * otherwise the bit is clear.
+     */
+    uint32_t apr[4][NCPU];
+
     uint32_t num_cpu;
 
     MemoryRegion iomem; /* Distributor */
Signature · 1 line
1.7.10.4
#19 lore raw

Re: [Qemu-devel] [RFC PATCH v2 6/6] hw: arm_gic_kvm: Add KVM VGIC save/restore logic

Quoted 1 line · [email protected] writes:

Save and restore the ARM KVM VGIC state from the kernel. We rely on

<snip>

hw/intc/arm_gic_common.c static const VMStateDescription vmstate_gic_irq_state = { · line 65

Quoted 10 lines
 
 static const VMStateDescription vmstate_gic = {
     .name = "arm_gic",
hw/intc/arm_gic_common.c +2 -2
-    .version_id = 6,
-    .minimum_version_id = 6,
+    .version_id = 7,
+    .minimum_version_id = 7,
     .pre_save = gic_pre_save,
     .post_load = gic_post_load,
     .fields = (VMStateField[]) {

Does this mean QEMU and Kernel need to be kept in lock-step for compatibility?

Quoted 5 lines
hw/intc/arm_gic_kvm.c +4 -0
+//#define DEBUG_GIC_KVM
+
+#ifdef DEBUG_GIC_KVM
+static const int debug_gic_kvm = 1;

hw/intc/arm_gic_kvm.c line 40

Quoted 10 lines
hw/intc/arm_gic_kvm.c +10 -0
+#else
+static const int debug_gic_kvm = 0;
+#endif
+
+#define DPRINTF(fmt, ...) do { \
+        if (debug_gic_kvm) { \
+            printf("arm_gic: " fmt , ## __VA_ARGS__); \
+        } \
+    } while (0)
+

Shouldn't we be using QEMU logging framework for this? Also for the fprintfs later on.

Quoted 109 lines

#define TYPE_KVM_ARM_GIC "kvm-arm-gic" #define KVM_ARM_GIC(obj) \
OBJECT_CHECK(GICState, (obj), TYPE_KVM_ARM_GIC)

hw/intc/arm_gic_kvm.c +102 -0
@@ -72,14 +87,419 @@ static void kvm_arm_gic_set_irq(void *opaque, int irq, int level)
     kvm_set_irq(kvm_state, kvm_irq, !!level);
 }
 
+static bool kvm_arm_gic_can_save_restore(GICState *s)
+{
+    return s->dev_fd >= 0;
+}
+
+static void kvm_gic_access(GICState *s, int group, int offset,
+                                   int cpu, uint32_t *val, bool write)
+{
+    struct kvm_device_attr attr;
+    int type;
+    int err;
+
+    cpu = cpu & 0xff;
+
+    attr.flags = 0;
+    attr.group = group;
+    attr.attr = (((uint64_t)cpu << KVM_DEV_ARM_VGIC_CPUID_SHIFT) &
+                 KVM_DEV_ARM_VGIC_CPUID_MASK) |
+                (((uint64_t)offset << KVM_DEV_ARM_VGIC_OFFSET_SHIFT) &
+                 KVM_DEV_ARM_VGIC_OFFSET_MASK);
+    attr.addr = (uintptr_t)val;
+
+    if (write) {
+        type = KVM_SET_DEVICE_ATTR;
+    } else {
+        type = KVM_GET_DEVICE_ATTR;
+    }
+
+    err = kvm_device_ioctl(s->dev_fd, type, &attr);
+    if (err < 0) {
+            fprintf(stderr, "KVM_{SET/GET}_DEVICE_ATTR failed: %s\n",
+                    strerror(-err));
+            abort();
+    }
+}
+
+static void kvm_gicd_access(GICState *s, int offset, int cpu,
+                            uint32_t *val, bool write)
+{
+    kvm_gic_access(s, KVM_DEV_ARM_VGIC_GRP_DIST_REGS,
+                   offset, cpu, val, write);
+}
+
+static void kvm_gicc_access(GICState *s, int offset, int cpu,
+                            uint32_t *val, bool write)
+{
+    kvm_gic_access(s, KVM_DEV_ARM_VGIC_GRP_CPU_REGS,
+                   offset, cpu, val, write);
+}
+
+#define for_each_irq_reg(_ctr, _max_irq, _field_width) \
+    for (_ctr = 0; _ctr < ((_max_irq) / (32 / (_field_width))); _ctr++)
+
+/*
+ * Translate from the in-kernel field for an IRQ value to/from the qemu
+ * representation.
+ */
+typedef void (*vgic_translate_fn)(GICState *s, int irq, int cpu,
+                                  uint32_t *field, bool to_kernel);
+
+/* synthetic translate function used for clear/set registers to completely
+ * clear a setting using a clear-register before setting the remaing bits
+ * using a set-register */
+static void translate_clear(GICState *s, int irq, int cpu,
+                            uint32_t *field, bool to_kernel)
+{
+    if (to_kernel) {
+        *field = ~0;
+    } else {
+        /* does not make sense: qemu model doesn't use set/clear regs */
+        abort();
+    }
+}
+
+static void translate_enabled(GICState *s, int irq, int cpu,
+                              uint32_t *field, bool to_kernel)
+{
+    int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
+
+    if (to_kernel) {
+        *field = GIC_TEST_ENABLED(irq, cm);
+    } else {
+        if (*field & 1) {
+            GIC_SET_ENABLED(irq, cm);
+        }
+    }
+}
+
+static void translate_pending(GICState *s, int irq, int cpu,
+                              uint32_t *field, bool to_kernel)
+{
+    int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
+
+    if (to_kernel) {
+        *field = GIC_TEST_PENDING(irq, cm);
+    } else {
+        if (*field & 1) {
+            GIC_SET_PENDING(irq, cm);
+            /* TODO: Capture is level-line is held high in the kernel */
+        }
+    }
+}

hw/intc/arm_gic_kvm.c static void kvm_arm_gic_set_irq(void *opaque, int irq, int level) · line 201

Quoted 10 lines
hw/intc/arm_gic_kvm.c +10 -0
+
+static void translate_active(GICState *s, int irq, int cpu,
+                             uint32_t *field, bool to_kernel)
+{
+    int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
+
+    if (to_kernel) {
+        *field = GIC_TEST_ACTIVE(irq, cm);
+    } else {
+        if (*field & 1) {

Should 1, 0x2 etc be #define'd constants?

<snip>

Quoted 60 lines
hw/intc/arm_gic_kvm.c +57 -1
 static void kvm_arm_gic_put(GICState *s)
 {
-    /* TODO: there isn't currently a kernel interface to set the GIC state */
+    uint32_t reg;
+    int i;
+    int cpu;
+    int num_cpu;
+    int num_irq;
+
+    if (!kvm_arm_gic_can_save_restore(s)) {
+            DPRINTF("Cannot put kernel gic state, no kernel interface");
+            return;
+    }
+
+    /* Note: We do the restore in a slightly different order than the save
+     * (where the order doesn't matter and is simply ordered according to the
+     * register offset values */
+
+    /*****************************************************************
+     * Distributor State
+     */
+
+    /* s->enabled -> GICD_CTLR */
+    reg = s->enabled;
+    kvm_gicd_access(s, 0x0, 0, &reg, true);
+
+    /* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */
+    kvm_gicd_access(s, 0x4, 0, &reg, false);
+    num_irq = ((reg & 0x1f) + 1) * 32;
+    num_cpu = ((reg & 0xe0) >> 5) + 1;
+
+    if (num_irq < s->num_irq) {
+            fprintf(stderr, "Restoring %u IRQs, but kernel supports max %d\n",
+                    s->num_irq, num_irq);
+            abort();
+    } else if (num_cpu != s->num_cpu ) {
+            fprintf(stderr, "Restoring %u CPU interfaces, kernel only has %d\n",
+                    s->num_cpu, num_cpu);
+            /* Did we not create the VCPUs in the kernel yet? */
+            abort();
+    }
+
+    /* TODO: Consider checking compatibility with the IIDR ? */
+
+    /* irq_state[n].enabled -> GICD_ISENABLERn */
+    kvm_dist_put(s, 0x180, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x100, 1, s->num_irq, translate_enabled);
+
+    /* s->irq_target[irq] -> GICD_ITARGETSRn
+     * (restore targets before pending to ensure the pending state is set on
+     * the appropriate CPU interfaces in the kernel) */
+    kvm_dist_put(s, 0x800, 8, s->num_irq, translate_targets);
+
+    /* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */
+    kvm_dist_put(s, 0x280, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x200, 1, s->num_irq, translate_pending);
+
+    /* irq_state[n].active -> GICD_ISACTIVERn */
+    kvm_dist_put(s, 0x380, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x300, 1, s->num_irq, translate_active);

hw/intc/arm_gic_kvm.c static void kvm_arm_gic_set_irq(void *opaque, int irq, int level) · line 374

Quoted 10 lines
hw/intc/arm_gic_kvm.c +10 -0
+
+    /* irq_state[n].trigger -> GICD_ICFRn */
+    kvm_dist_put(s, 0xc00, 2, s->num_irq, translate_trigger);
+
+    /* s->priorityX[irq] -> ICD_IPRIORITYRn */
+    kvm_dist_put(s, 0x400, 8, s->num_irq, translate_priority);
+
+    /* s->sgi_source -> ICD_CPENDSGIRn */
+    kvm_dist_put(s, 0xf10, 8, GIC_NR_SGIS, translate_clear);
+    kvm_dist_put(s, 0xf20, 8, GIC_NR_SGIS, translate_sgisource);

Again what are these magic numbers? I assume the comments refer to the actual reg names in the manual? Perhaps putting a reference to the manual if these values aren't to be used else where?

<snip>

Signature · 1 line
Alex Bennée
#20 lore raw

Re: [Qemu-devel] [RFC PATCH v2 6/6] hw: arm_gic_kvm: Add KVM VGIC save/restore logic

Quoted 16 lines · Alex Bennée
Quoted 1 line · [email protected] writes:

Save and restore the ARM KVM VGIC state from the kernel. We rely on

<snip>

hw/intc/arm_gic_common.c static const VMStateDescription vmstate_gic_irq_state = { · line 65

Quoted 10 lines
 static const VMStateDescription vmstate_gic = {
     .name = "arm_gic",
hw/intc/arm_gic_common.c +2 -2
-    .version_id = 6,
-    .minimum_version_id = 6,
+    .version_id = 7,
+    .minimum_version_id = 7,
     .pre_save = gic_pre_save,
     .post_load = gic_post_load,
     .fields = (VMStateField[]) {
Quoted 2 lines

Does this mean QEMU and Kernel need to be kept in lock-step for compatibility?

No. This patch is a little confusing because it's both adding the new fields and also adding the save/restore support, but once we have the data structures and vmstate in QEMU holding all the state the kernel needs, there shouldn't be any need to update the vmstate in a backwards-incompatible way.

Quoted 10 lines
Quoted 10 lines
hw/intc/arm_gic_kvm.c +9 -0
+//#define DEBUG_GIC_KVM
+
+#ifdef DEBUG_GIC_KVM
+static const int debug_gic_kvm = 1;
+#else
+static const int debug_gic_kvm = 0;
+#endif
+
+#define DPRINTF(fmt, ...) do { \
Quoted 8 lines

hw/intc/arm_gic_kvm.c line 40

Quoted 5 lines
hw/intc/arm_gic_kvm.c +5 -0
+        if (debug_gic_kvm) { \
+            printf("arm_gic: " fmt , ## __VA_ARGS__); \
+        } \
+    } while (0)
+

Shouldn't we be using QEMU logging framework for this? Also for the fprintfs later on.

No, these are debug printfs, not things which would be interesting to the average user/person trying to debug a guest. We don't have a particularly clean framework for compile time enabled debug printfs, so 'some random macro in each individual file' is the current approach.

-- PMM

#21 lore raw

Re: [Qemu-devel] [RFC PATCH v2 6/6] hw: arm_gic_kvm: Add KVM VGIC save/restore logic

In reply to Alex Bennée
Quoted 3 lines · Alex Bennée

[email protected] writes:

[...]

Quoted 13 lines

hw/intc/arm_gic_kvm.c static void kvm_arm_gic_set_irq(void *opaque, int irq, int level) · line 201

Quoted 10 lines
hw/intc/arm_gic_kvm.c +10 -0
+
+static void translate_active(GICState *s, int irq, int cpu,
+                             uint32_t *field, bool to_kernel)
+{
+    int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
+
+    if (to_kernel) {
+        *field = GIC_TEST_ACTIVE(irq, cm);
+    } else {
+        if (*field & 1) {

Should 1, 0x2 etc be #define'd constants?

Quoted 1 line

<snip>

It would be a little tedious. The whole bunch of emulation files for the GIC here are sort of predicated on the fact that you know how to find the GIC manual and can see the register encodings and determine the offsets.

There's also very little reuse for these, as this is the only place where we translate the qemu-specific data structure format to the KVM API format.

That being said, I wouldn't mind cleaning up a lot of this code and adding defines for the register offsets would at least be very helpful, but for now I'm sticking with the current style, trying to get this code finished, and then I'll follow up with a cleanup patch set, which takes care of the whole lot - not just the KVM interface specific bits.

Quoted 66 lines
Quoted 66 lines

static void kvm_arm_gic_put(GICState *s)
{

hw/intc/arm_gic_kvm.c +63 -1
-    /* TODO: there isn't currently a kernel interface to set the GIC state */
+    uint32_t reg;
+    int i;
+    int cpu;
+    int num_cpu;
+    int num_irq;
+
+    if (!kvm_arm_gic_can_save_restore(s)) {
+            DPRINTF("Cannot put kernel gic state, no kernel interface");
+            return;
+    }
+
+    /* Note: We do the restore in a slightly different order than the save
+     * (where the order doesn't matter and is simply ordered according to the
+     * register offset values */
+
+    /*****************************************************************
+     * Distributor State
+     */
+
+    /* s->enabled -> GICD_CTLR */
+    reg = s->enabled;
+    kvm_gicd_access(s, 0x0, 0, &reg, true);
+
+    /* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */
+    kvm_gicd_access(s, 0x4, 0, &reg, false);
+    num_irq = ((reg & 0x1f) + 1) * 32;
+    num_cpu = ((reg & 0xe0) >> 5) + 1;
+
+    if (num_irq < s->num_irq) {
+            fprintf(stderr, "Restoring %u IRQs, but kernel supports max %d\n",
+                    s->num_irq, num_irq);
+            abort();
+    } else if (num_cpu != s->num_cpu ) {
+            fprintf(stderr, "Restoring %u CPU interfaces, kernel only has %d\n",
+                    s->num_cpu, num_cpu);
+            /* Did we not create the VCPUs in the kernel yet? */
+            abort();
+    }
+
+    /* TODO: Consider checking compatibility with the IIDR ? */
+
+    /* irq_state[n].enabled -> GICD_ISENABLERn */
+    kvm_dist_put(s, 0x180, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x100, 1, s->num_irq, translate_enabled);
+
+    /* s->irq_target[irq] -> GICD_ITARGETSRn
+     * (restore targets before pending to ensure the pending state is set on
+     * the appropriate CPU interfaces in the kernel) */
+    kvm_dist_put(s, 0x800, 8, s->num_irq, translate_targets);
+
+    /* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */
+    kvm_dist_put(s, 0x280, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x200, 1, s->num_irq, translate_pending);
+
+    /* irq_state[n].active -> GICD_ISACTIVERn */
+    kvm_dist_put(s, 0x380, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x300, 1, s->num_irq, translate_active);
+
+    /* irq_state[n].trigger -> GICD_ICFRn */
+    kvm_dist_put(s, 0xc00, 2, s->num_irq, translate_trigger);
+
+    /* s->priorityX[irq] -> ICD_IPRIORITYRn */
+    kvm_dist_put(s, 0x400, 8, s->num_irq, translate_priority);
Quoted 9 lines

hw/intc/arm_gic_kvm.c static void kvm_arm_gic_set_irq(void *opaque, int irq, int level) · line 374

Quoted 4 lines
hw/intc/arm_gic_kvm.c +4 -0
+
+    /* s->sgi_source -> ICD_CPENDSGIRn */
+    kvm_dist_put(s, 0xf10, 8, GIC_NR_SGIS, translate_clear);
+    kvm_dist_put(s, 0xf20, 8, GIC_NR_SGIS, translate_sgisource);

Again what are these magic numbers? I assume the comments refer to the actual reg names in the manual? Perhaps putting a reference to the manual if these values aren't to be used else where?

These are offsets (as indicated by the parameter name in kvm_dist_put) for the corresponding registers, and yes, the comments are the reg names used in the manual. As I said above, you're lost here if you're not looking at the manual while looking at the code, but I really do want to clean up this part and share the offsets with arm_gic.c later, add nice references to the manual and a whole bunch of other stuff that could be made much nicer, but I'd really like to get this functional piece of code in first.

Thanks,
-Christoffer

#22 lore raw

Re: [Qemu-devel] [RFC PATCH v2 6/6] hw: arm_gic_kvm: Add KVM VGIC save/restore logic

In reply to Christoffer Dall
Quoted 42 lines · Christoffer Dall

Save and restore the ARM KVM VGIC state from the kernel. We rely on QEMU to marshal the GICState data structure and therefore simply synchronize the kernel state with the QEMU emulated state in both directions.

We take some care on the restore path to check the VGIC has been configured with enough IRQs and CPU interfaces that we can properly restore the state, and for separate set/clear registers we first fully clear the registers and then set the required bits.

Changelog [v2]:
- Remove num_irq from GIC VMstate structure
- Increment GIC VMstate version number
- Use extract32/deposit32 for bit-field modifications
- Address other smaller review comments
- Renames kvm_arm_gic_dist_[readr/writer] functions to kvm_dist_[get/put] and shortened other function names
- Use concrete format for APRn

---
 hw/intc/arm_gic_common.c |    5 +-
 hw/intc/arm_gic_kvm.c    |  424 +++++++++++++++++++++++++++++++++++++++++++++-
 hw/intc/gic_internal.h   |    8 +
 3 files changed, 433 insertions(+), 4 deletions(-)
hw/intc/arm_gic_common.c +2 -2
index 5449d77..1d3b738 100644
@@ -58,8 +58,8 @@ static const VMStateDescription vmstate_gic_irq_state = {
 
 static const VMStateDescription vmstate_gic = {
     .name = "arm_gic",
-    .version_id = 6,
-    .minimum_version_id = 6,
+    .version_id = 7,
+    .minimum_version_id = 7,
     .pre_save = gic_pre_save,
     .post_load = gic_post_load,
     .fields = (VMStateField[]) {

hw/intc/arm_gic_common.c static const VMStateDescription vmstate_gic = { · line 81

Quoted 4 lines
hw/intc/arm_gic_common.c +1 -0
@@ -78,6 +78,7 @@ static const VMStateDescription vmstate_gic = {
         VMSTATE_UINT16_ARRAY(current_pending, GICState, NCPU),
         VMSTATE_UINT8_ARRAY(bpr, GICState, NCPU),
         VMSTATE_UINT8_ARRAY(abpr, GICState, NCPU),
+        VMSTATE_UINT32_2DARRAY(apr, GICState, 4, NCPU),

I feel like we should add this new apr state (plus some documentation and at least the TCG read/write interface to the state) in one patch and then put the save/load in its own patch.

hw/intc/arm_gic_common.c static const VMStateDescription vmstate_gic = {

Quoted 5 lines
hw/intc/arm_gic_common.c +5 -0
+    err = kvm_device_ioctl(s->dev_fd, type, &attr);
+    if (err < 0) {
+            fprintf(stderr, "KVM_{SET/GET}_DEVICE_ATTR failed: %s\n",
+                    strerror(-err));
+            abort();

Bad indent.

Quoted 92 lines
hw/intc/arm_gic_common.c +89 -1
+    }
+}
 static void kvm_arm_gic_put(GICState *s)
 {
-    /* TODO: there isn't currently a kernel interface to set the GIC state */
+    uint32_t reg;
+    int i;
+    int cpu;
+    int num_cpu;
+    int num_irq;
+
+    if (!kvm_arm_gic_can_save_restore(s)) {
+            DPRINTF("Cannot put kernel gic state, no kernel interface");
+            return;
+    }
+
+    /* Note: We do the restore in a slightly different order than the save
+     * (where the order doesn't matter and is simply ordered according to the
+     * register offset values */
+
+    /*****************************************************************
+     * Distributor State
+     */
+
+    /* s->enabled -> GICD_CTLR */
+    reg = s->enabled;
+    kvm_gicd_access(s, 0x0, 0, &reg, true);
+
+    /* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */
+    kvm_gicd_access(s, 0x4, 0, &reg, false);
+    num_irq = ((reg & 0x1f) + 1) * 32;
+    num_cpu = ((reg & 0xe0) >> 5) + 1;
+
+    if (num_irq < s->num_irq) {
+            fprintf(stderr, "Restoring %u IRQs, but kernel supports max %d\n",
+                    s->num_irq, num_irq);
+            abort();
+    } else if (num_cpu != s->num_cpu ) {
+            fprintf(stderr, "Restoring %u CPU interfaces, kernel only has %d\n",
+                    s->num_cpu, num_cpu);
+            /* Did we not create the VCPUs in the kernel yet? */
+            abort();
+    }
+
+    /* TODO: Consider checking compatibility with the IIDR ? */
+
+    /* irq_state[n].enabled -> GICD_ISENABLERn */
+    kvm_dist_put(s, 0x180, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x100, 1, s->num_irq, translate_enabled);
+
+    /* s->irq_target[irq] -> GICD_ITARGETSRn
+     * (restore targets before pending to ensure the pending state is set on
+     * the appropriate CPU interfaces in the kernel) */
+    kvm_dist_put(s, 0x800, 8, s->num_irq, translate_targets);
+
+    /* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */
+    kvm_dist_put(s, 0x280, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x200, 1, s->num_irq, translate_pending);
+
+    /* irq_state[n].active -> GICD_ISACTIVERn */
+    kvm_dist_put(s, 0x380, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x300, 1, s->num_irq, translate_active);
+
+    /* irq_state[n].trigger -> GICD_ICFRn */
+    kvm_dist_put(s, 0xc00, 2, s->num_irq, translate_trigger);
+
+    /* s->priorityX[irq] -> ICD_IPRIORITYRn */
+    kvm_dist_put(s, 0x400, 8, s->num_irq, translate_priority);
+
+    /* s->sgi_source -> ICD_CPENDSGIRn */
+    kvm_dist_put(s, 0xf10, 8, GIC_NR_SGIS, translate_clear);
+    kvm_dist_put(s, 0xf20, 8, GIC_NR_SGIS, translate_sgisource);
+
+
+    /*****************************************************************
+     * CPU Interface(s) State
+     */
+
+    for (cpu = 0; cpu < s->num_cpu; cpu++) {
+        /* s->cpu_enabled[cpu] -> GICC_CTLR */
+        reg = s->cpu_enabled[cpu];
+        kvm_gicc_access(s, 0x00, cpu, &reg, true);
+
+        /* s->priority_mask[cpu] -> GICC_PMR */
+        reg = (s->priority_mask[cpu] & 0xff);
+        kvm_gicc_access(s, 0x04, cpu, &reg, true);
+
+        /* s->bpr[cpu] -> GICC_BPR */
+        reg = (s->bpr[cpu] & 0x7);
+        kvm_gicc_access(s, 0x08, cpu, &reg, true);
+
+        /* s->abpr[cpu] -> GICC_ABPR */

hw/intc/arm_gic_common.c

Quoted 10 lines
hw/intc/arm_gic_common.c +9 -0
+        reg = (s->abpr[cpu] & 0x7);
+        kvm_gicc_access(s, 0x1c, cpu, &reg, true);
+
+        /* s->apr[n][cpu] -> GICC_APRn */
+        for (i = 0; i < 4; i++) {
+            reg = s->apr[i][cpu];
+            kvm_gicc_access(s, 0xd0 + i * 4, cpu, &reg, true);
+        }
+    }
 }

So an interesting question here is "is there state we currently save/restore for the TCG GIC which we're not passing to the kernel, and if so why?". I think the fields we don't do anything with are:
gic_irq_state::model
GICState::last_active
GICState::running_irq
GICState::running_priority
GICState::current_pending

I'm guessing these are a mix of "kernel doesn't implement something" and "TCG model is wrong and this state should show up somewhere else"; but which is which?

Quoted 5 lines
hw/intc/gic_internal.h +0 -0
index 758b85a..f9133b9 100644

hw/intc/gic_internal.h typedef struct GICState { · line 108

Quoted 10 lines
hw/intc/gic_internal.h +7 -0
@@ -99,6 +99,14 @@ typedef struct GICState {
     uint8_t  bpr[NCPU];
     uint8_t  abpr[NCPU];
 
+    /* The APR is implementation defined, so we choose a layout identical to
+     * the KVM ABI layout for QEMU's implementation of the gic:
+     * If one or more interrupts for preemption level X are active, then
+     *   APRn[X mod 32] == 0b1,  where n = X / 32
+     * otherwise the bit is clear.
+     */
+    uint32_t apr[4][NCPU];

You can delete the "or more" -- there can onyl be one interrupt active at each group priority (GICv2 spec 3.3.3).

Also, where does the hardcoded 4 come from? Answer: it's the worst-case number of GICC_APRn registers, which happens if you have 7 bits of group priority, which means you have 128 preemption levels, which at one bit per level fits into 4 32 bit APR regs. I think a GIC_NR_APRS #define might be nice.

Quoted 6 lines
hw/intc/gic_internal.h +1 -1
+
     uint32_t num_cpu;
 
     MemoryRegion iomem; /* Distributor */
--

1.7.10.4

thanks
-- PMM

#23 lore raw

Re: [Qemu-devel] [RFC PATCH v2 6/6] hw: arm_gic_kvm: Add KVM VGIC save/restore logic

Quoted 45 lines · Peter Maydell
Quoted 43 lines · Christoffer Dall

Save and restore the ARM KVM VGIC state from the kernel. We rely on QEMU to marshal the GICState data structure and therefore simply synchronize the kernel state with the QEMU emulated state in both directions.

We take some care on the restore path to check the VGIC has been configured with enough IRQs and CPU interfaces that we can properly restore the state, and for separate set/clear registers we first fully clear the registers and then set the required bits.

Changelog [v2]:
- Remove num_irq from GIC VMstate structure
- Increment GIC VMstate version number
- Use extract32/deposit32 for bit-field modifications
- Address other smaller review comments
- Renames kvm_arm_gic_dist_[readr/writer] functions to kvm_dist_[get/put] and shortened other function names
- Use concrete format for APRn

---
 hw/intc/arm_gic_common.c |    5 +-
 hw/intc/arm_gic_kvm.c    |  424 +++++++++++++++++++++++++++++++++++++++++++++-
 hw/intc/gic_internal.h   |    8 +
 3 files changed, 433 insertions(+), 4 deletions(-)
hw/intc/arm_gic_common.c +2 -2
index 5449d77..1d3b738 100644
@@ -58,8 +58,8 @@ static const VMStateDescription vmstate_gic_irq_state = {
 
 static const VMStateDescription vmstate_gic = {
     .name = "arm_gic",
-    .version_id = 6,
-    .minimum_version_id = 6,
+    .version_id = 7,
+    .minimum_version_id = 7,
     .pre_save = gic_pre_save,
     .post_load = gic_post_load,
     .fields = (VMStateField[]) {
@@ -78,6 +78,7 @@ static const VMStateDescription vmstate_gic = {
         VMSTATE_UINT16_ARRAY(current_pending, GICState, NCPU),
Quoted 9 lines

hw/intc/arm_gic_common.c static const VMStateDescription vmstate_gic = { · line 81

Quoted 3 lines
hw/intc/arm_gic_common.c +1 -0
         VMSTATE_UINT8_ARRAY(bpr, GICState, NCPU),
         VMSTATE_UINT8_ARRAY(abpr, GICState, NCPU),
+        VMSTATE_UINT32_2DARRAY(apr, GICState, 4, NCPU),

I feel like we should add this new apr state (plus some documentation and at least the TCG read/write interface to the state) in one patch and then put the save/load in its own patch.

Sounds reasonable, I've added a separate patch for the next series.

Quoted 8 lines

hw/intc/arm_gic_common.c static const VMStateDescription vmstate_gic = {

Quoted 5 lines
hw/intc/arm_gic_common.c +5 -0
+    err = kvm_device_ioctl(s->dev_fd, type, &attr);
+    if (err < 0) {
+            fprintf(stderr, "KVM_{SET/GET}_DEVICE_ATTR failed: %s\n",
+                    strerror(-err));
+            abort();

Bad indent.

indeed, sorry about that.

Quoted 100 lines
Quoted 100 lines
hw/intc/arm_gic_common.c +97 -1
+    }
+}
 static void kvm_arm_gic_put(GICState *s)
 {
-    /* TODO: there isn't currently a kernel interface to set the GIC state */
+    uint32_t reg;
+    int i;
+    int cpu;
+    int num_cpu;
+    int num_irq;
+
+    if (!kvm_arm_gic_can_save_restore(s)) {
+            DPRINTF("Cannot put kernel gic state, no kernel interface");
+            return;
+    }
+
+    /* Note: We do the restore in a slightly different order than the save
+     * (where the order doesn't matter and is simply ordered according to the
+     * register offset values */
+
+    /*****************************************************************
+     * Distributor State
+     */
+
+    /* s->enabled -> GICD_CTLR */
+    reg = s->enabled;
+    kvm_gicd_access(s, 0x0, 0, &reg, true);
+
+    /* Sanity checking on GICD_TYPER and s->num_irq, s->num_cpu */
+    kvm_gicd_access(s, 0x4, 0, &reg, false);
+    num_irq = ((reg & 0x1f) + 1) * 32;
+    num_cpu = ((reg & 0xe0) >> 5) + 1;
+
+    if (num_irq < s->num_irq) {
+            fprintf(stderr, "Restoring %u IRQs, but kernel supports max %d\n",
+                    s->num_irq, num_irq);
+            abort();
+    } else if (num_cpu != s->num_cpu ) {
+            fprintf(stderr, "Restoring %u CPU interfaces, kernel only has %d\n",
+                    s->num_cpu, num_cpu);
+            /* Did we not create the VCPUs in the kernel yet? */
+            abort();
+    }
+
+    /* TODO: Consider checking compatibility with the IIDR ? */
+
+    /* irq_state[n].enabled -> GICD_ISENABLERn */
+    kvm_dist_put(s, 0x180, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x100, 1, s->num_irq, translate_enabled);
+
+    /* s->irq_target[irq] -> GICD_ITARGETSRn
+     * (restore targets before pending to ensure the pending state is set on
+     * the appropriate CPU interfaces in the kernel) */
+    kvm_dist_put(s, 0x800, 8, s->num_irq, translate_targets);
+
+    /* irq_state[n].pending + irq_state[n].level -> GICD_ISPENDRn */
+    kvm_dist_put(s, 0x280, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x200, 1, s->num_irq, translate_pending);
+
+    /* irq_state[n].active -> GICD_ISACTIVERn */
+    kvm_dist_put(s, 0x380, 1, s->num_irq, translate_clear);
+    kvm_dist_put(s, 0x300, 1, s->num_irq, translate_active);
+
+    /* irq_state[n].trigger -> GICD_ICFRn */
+    kvm_dist_put(s, 0xc00, 2, s->num_irq, translate_trigger);
+
+    /* s->priorityX[irq] -> ICD_IPRIORITYRn */
+    kvm_dist_put(s, 0x400, 8, s->num_irq, translate_priority);
+
+    /* s->sgi_source -> ICD_CPENDSGIRn */
+    kvm_dist_put(s, 0xf10, 8, GIC_NR_SGIS, translate_clear);
+    kvm_dist_put(s, 0xf20, 8, GIC_NR_SGIS, translate_sgisource);
+
+
+    /*****************************************************************
+     * CPU Interface(s) State
+     */
+
+    for (cpu = 0; cpu < s->num_cpu; cpu++) {
+        /* s->cpu_enabled[cpu] -> GICC_CTLR */
+        reg = s->cpu_enabled[cpu];
+        kvm_gicc_access(s, 0x00, cpu, &reg, true);
+
+        /* s->priority_mask[cpu] -> GICC_PMR */
+        reg = (s->priority_mask[cpu] & 0xff);
+        kvm_gicc_access(s, 0x04, cpu, &reg, true);
+
+        /* s->bpr[cpu] -> GICC_BPR */
+        reg = (s->bpr[cpu] & 0x7);
+        kvm_gicc_access(s, 0x08, cpu, &reg, true);
+
+        /* s->abpr[cpu] -> GICC_ABPR */
+        reg = (s->abpr[cpu] & 0x7);
+        kvm_gicc_access(s, 0x1c, cpu, &reg, true);
+
+        /* s->apr[n][cpu] -> GICC_APRn */
+        for (i = 0; i < 4; i++) {
+            reg = s->apr[i][cpu];
+            kvm_gicc_access(s, 0xd0 + i * 4, cpu, &reg, true);
+        }
Quoted 8 lines
Quoted 2 lines

+ }
}

So an interesting question here is "is there state we currently save/restore for the TCG GIC which we're not passing to the kernel, and if so why?". I think the fields we don't do anything with are:
gic_irq_state::model

the model field specifies if the interrup uses the 1:N or the N:N model. In reasonably recent versions of the GIC all SGIs are N:N and all peripheral interrupts are 1:N and it is therefore a static property for a GIC v2.0 that we don't need to save and restore. QEMU's model does seem to emulate an older version and a newer version at the same time, and this logic is slightly broken for the ICFGRn handling, but this would require a discussion of exactly what QEMU should be supporting and what we may break if we decide to support one thing vs. another.

Quoted 3 lines

GICState::last_active
GICState::running_irq
GICState::running_priority

As far as I understand the code, these three are used to implement the running priority support on the QEMU emulated CPU interface side. This state is implemented in hardware for the virtual CPU interface for the VGIC based on the information in the list registers and the GICD_ISACTIVER and registers. I think the QEMU code code be changed to look at the active state of the registers and with a coherent CPU affinity model should be able to reproduce this state on the fly without keeping these extra state variables around, but I'm not 100% confident.

Quoted 1 line

GICState::current_pending

Again, the virtual CPU register generates this state based on the LRs present on the interface. The in-kernel distributor emulation needs to choose the right interrupts to queue based on priority if prorities were to be supported in the kernel. The necessary state, however, is preserved through the registers that we do save an restore.

Quoted 4 lines

I'm guessing these are a mix of "kernel doesn't implement something" and "TCG model is wrong and this state should show up somewhere else"; but which is which?

So I think the answer to all of the above is that this is implementation state that QEMU carries because it makes it more efficient/easier or just because that was the way it was written, and the kernel does maintain this state via the current registers being saved and restored.

This of course implies that the QEMU state is somewhat redundant, but this may be on purpose.

Do you feel it is necessary for this sort of documentation to go into the source somewhere, or would you just like to clear it up for the review?

Quoted 19 lines
Quoted 5 lines
hw/intc/gic_internal.h +0 -0
index 758b85a..f9133b9 100644

hw/intc/gic_internal.h typedef struct GICState { · line 108

Quoted 10 lines
hw/intc/gic_internal.h +7 -0
@@ -99,6 +99,14 @@ typedef struct GICState {
     uint8_t  bpr[NCPU];
     uint8_t  abpr[NCPU];
 
+    /* The APR is implementation defined, so we choose a layout identical to
+     * the KVM ABI layout for QEMU's implementation of the gic:
+     * If one or more interrupts for preemption level X are active, then
+     *   APRn[X mod 32] == 0b1,  where n = X / 32
+     * otherwise the bit is clear.
+     */
+    uint32_t apr[4][NCPU];

You can delete the "or more" -- there can onyl be one interrupt active at each group priority (GICv2 spec 3.3.3).

Quoted 6 lines

Also, where does the hardcoded 4 come from? Answer: it's the worst-case number of GICC_APRn registers, which happens if you have 7 bits of group priority, which means you have 128 preemption levels, which at one bit per level fits into 4 32 bit APR regs. I think a GIC_NR_APRS #define might be nice.

Fair enough, I added one in the separate patch based on a new define for max number of preemption levels.

Quoted 6 lines
Quoted 6 lines
hw/intc/gic_internal.h +1 -1
+
     uint32_t num_cpu;
 
     MemoryRegion iomem; /* Distributor */
--

1.7.10.4

Thanks for the review!
-Christoffer