Skip to contentSkip to conversation

hw/intc/arm_gicv5: Minor fixes and cleanups

PATCH Superseded Peter Maydell 2 participants 13 messages Aug 24 – Sep 14

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover hw/intc/arm_gicv5: Minor fixes and cleanups

This patchset is a compilation of minor fixes and cleanups to the GICv5 code that I found in the course of implementing virtualization support:

* Various in-memory data structures are defined to be aligned to their size, even if the guest provides a base address that is less aligned
* We had some extra blank lines that didn't match the style in the rest of the file
* We were unnecessarily extracting a bit from a raw register value when we already had it cached as a boolean in a struct
* We weren't tidying up our allocated memory on finalize
* We can re-use arm_cpu_set_irq() rather than implementing the set/clear IRQ/FIQ/NMI locally. (This doesn't matter much for these, but VIRQ/VFIQ/VINMI have more complex logic in arm_cpu_set_irq() that we don't want to have to duplicate.)

thanks
-- PMM

Peter Maydell (6):
hw/intc/arm_gicv5: Ignore low bits in IRS_IST_BASER.ADDR hw/intc/arm_gicv5: Ignore low bits in L1_ISTE.L2_ADDR hw/intc/arm_gicv5: Regularize spacing in config_readl() switch hw/intc/arm_gicv5: Use cached IRS_IST_BASER.VALID state hw/intc/arm_gicv5: Free memory in finalize
target/arm: GICv5 cpuif: Use arm_cpu_set_irq() to set IRQ/FIQ status

 hw/intc/arm_gicv5.c          | 67 ++++++++++++++++++++++++++----------
 hw/intc/arm_gicv5_common.c   |  3 ++
 include/hw/intc/arm_gicv5.h  |  1 +
 target/arm/cpu.c             | 14 ++++++--
 target/arm/internals.h       | 14 ++++++++
 target/arm/tcg/gicv5-cpuif.c | 35 +++----------------
 6 files changed, 81 insertions(+), 53 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/6 hw/intc/arm_gicv5: Ignore low bits in IRS_IST_BASER.ADDR

The IRS_IST_BASER.ADDR documentation says:

For IRS_IST_CFGR.STRUCTURE = 0:
Bits[N:0] of the resulting address are 0 where N depends on the ISTSZ and LPI_ID_BITS fields in IRS_IST_CFGR as follows:

    N = Max(5, (ISTSZ + 1) + LPI_ID_BITS)

For IRS_IST_CFGR.STRUCTURE = 1:

Bits[N:0] of the resulting address are 0 where N depends on L2SZ, ISTSZ, and LPI_ID_BITS in IRS_IST_CFGR as follows:

    N = Max(5, LPI_ID_BITS - ((10 - ISTSZ) + (2 * L2SZ)) + 2)

This enforces that the IST is aligned to the size of the IST table, or to a 64-byte boundary if the table is smaller than 64 bytes, even if the guest programs the low bits in the ADDR field to specify an insufficiently aligned address.

We were missing this check, and instead trusted the guest-provided address to be sufficiently aligned already; add the alignment enforcement.

The GICv5 spec defines the masking to be used with the expressions quoted above, which work with the raw field values from the IRS_IST_CFGR register; these simplify down to some straightforward expressions based on the sanitized values we've already extracted form those fields. You can get to the expressions we use here either by working through the simplification of the GIC spec expressions, or from first principles (the number of bits needed for the size of the table in bytes is the number of bits that index it plus the number of bits specifying the size of an individual table entry).

---
 hw/intc/arm_gicv5.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
hw/intc/arm_gicv5.c +28 -0
index 08878193dfc..cdc6c9d3466 100644
@@ -1313,6 +1313,7 @@ static void irs_ist_baser_write(GICv5 *s, GICv5Domain domain, uint64_t value)
          */
         GICv5ISTConfig *cfg = &s->phys_lpi_config[domain];
         uint8_t istbits, l2bits, l2_idx_bits;
+        int tableszbits;
         uint8_t id_bits = FIELD_EX64(cs->irs_ist_cfgr[domain],
                                      IRS_IST_CFGR, LPI_ID_BITS);
         id_bits = MIN(MAX(id_bits, QEMU_GICV5_MIN_LPI_ID_BITS), QEMU_GICV5_ID_BITS);
@@ -1359,6 +1360,33 @@ static void irs_ist_baser_write(GICv5 *s, GICv5Domain domain, uint64_t value)
         cfg->l2_idx_bits = l2_idx_bits;
         cfg->structure = FIELD_EX64(cs->irs_ist_cfgr[domain],
                                     IRS_IST_CFGR, STRUCTURE);
+
+        /*
+         * Bits [N:0] of the base addr are ignored, to enforce that in a
+         * 1-level IST it is aligned to the size of the ISTE array,
+         * and in a 2-level IST it is aligned to the size of the L1 table.
+         * The spec defines N in terms of the raw L2SZ, ISTSZ and LPI_ID_BITS
+         * fields, but we have already sanitized these to
+         *  istbits = ISTSZ + 2
+         *  l2bits = (L2SZ * 2) + 12
+         *  id_bits = LPI_ID_BITS
+         * We have also already enforced the "bottom 6 bits are 0" via
+         * R_IRS_IST_BASER_ADDR_MASK, so can skip the Max() operation.
+         * We want tableszbits == the number of bits to zero, which is N + 1.
+         */
+        if (cfg->structure) {
+            /*
+             * L1 table is indexed by the (id_bits - l2_idx_bits) bits in
+             * the ID, so has 2^(id_bits - l2_idx_bits) entries, and each
+             * entry is 8 bytes == 2^3.
+             */
+            tableszbits = id_bits - l2_idx_bits + 3;
+        } else {
+            /* table has 2^id_bits entries of 2^istbits size each */
+            tableszbits = id_bits + istbits;
+        }
+        cfg->base &= MAKE_64BIT_MASK(0, tableszbits);
+
         if (!cfg->lpi_cache) {
             /*
              * Keys are GINT_TO_POINTER(intid), so we want the g_direct_hash
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/6] hw/intc/arm_gicv5: Ignore low bits in IRS_IST_BASER.ADDR

Quoted 6 lines · Peter Maydell

The IRS_IST_BASER.ADDR documentation says:

For IRS_IST_CFGR.STRUCTURE = 0:
Bits[N:0] of the resulting address are 0 where N depends on the ISTSZ and LPI_ID_BITS fields in IRS_IST_CFGR as follows:

Quoted 8 lines
    N = Max(5, (ISTSZ + 1) + LPI_ID_BITS)

For IRS_IST_CFGR.STRUCTURE = 1:

Bits[N:0] of the resulting address are 0 where N depends on L2SZ, ISTSZ, and LPI_ID_BITS in IRS_IST_CFGR as follows:

    N = Max(5, LPI_ID_BITS - ((10 - ISTSZ) + (2 * L2SZ)) + 2)

+ cfg->base &= MAKE_64BIT_MASK(0, tableszbits);

No sooner had I sent this than I found a bug: this should be
&= ~MAKE_64BIT_MASK(...)

-- PMM

#3 lore raw

PATCH 2/6 hw/intc/arm_gicv5: Ignore low bits in L1_ISTE.L2_ADDR

In reply to Peter Maydell

The GICv5 spec defines that an L2 IST is always aligned to its size, by stating that bits [N:0] of the address are 0 where

    N = 11 + (2 * L2SZ)

We were missing this check, and only aligning by applying R_L1_ISTE_L2_ADDR_MASK, so we would only enforce 4K alignment even if the L2 IST was 16K or 64K.

Add the missing masking of low bits. We already needed to calculate the number of bits needed to index each byte in the L2 table, so add it to the GICv5ISTConfig to save having to re-calculate it later.

---
 hw/intc/arm_gicv5.c         | 6 ++++++
 include/hw/intc/arm_gicv5.h | 1 +
 2 files changed, 7 insertions(+)
hw/intc/arm_gicv5.c +6 -0
index cdc6c9d3466..20680a5c225 100644
@@ -595,6 +595,11 @@ static bool get_l2_iste_addr(GICv5Common *cs, const GICv5ISTConfig *cfg,
             return false;
         }
         l2_base = l1_iste & R_L1_ISTE_L2_ADDR_MASK;
+        /*
+         * The effective base address of the L2 table is always
+         * aligned to the size of the L2 table.
+         */
+        l2_base &= MAKE_64BIT_MASK(0, cfg->l2bits);
         id = extract32(id, 0, cfg->l2_idx_bits);
     } else {
         /* 1-level table */
@@ -1358,6 +1363,7 @@ static void irs_ist_baser_write(GICv5 *s, GICv5Domain domain, uint64_t value)
         cfg->id_bits = id_bits;
         cfg->istsz = 1 << istbits;
         cfg->l2_idx_bits = l2_idx_bits;
+        cfg->l2bits = l2bits;
         cfg->structure = FIELD_EX64(cs->irs_ist_cfgr[domain],
                                     IRS_IST_CFGR, STRUCTURE);
 
include/hw/intc/arm_gicv5.h +1 -0
index b8baf003ad6..d763a7939c7 100644
@@ -23,6 +23,7 @@ typedef struct GICv5ISTConfig {
     uint8_t id_bits; /* number of bits in an ID for this table */
     uint8_t l2_idx_bits; /* number of ID bits that index into L2 table */
     uint8_t istsz; /* L2 ISTE size in bytes */
+    uint8_t l2bits; /* bits needed to index L2 table in bytes */
     bool structure; /* true if using 2-level table */
     bool valid; /* true if this table is valid and usable */
     /* This caches IST information about pending LPIs */
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/6] hw/intc/arm_gicv5: Ignore low bits in L1_ISTE.L2_ADDR

Quoted 24 lines · Peter Maydell

The GICv5 spec defines that an L2 IST is always aligned to its size, by stating that bits [N:0] of the address are 0 where

    N = 11 + (2 * L2SZ)

We were missing this check, and only aligning by applying R_L1_ISTE_L2_ADDR_MASK, so we would only enforce 4K alignment even if the L2 IST was 16K or 64K.

Add the missing masking of low bits. We already needed to calculate the number of bits needed to index each byte in the L2 table, so add it to the GICv5ISTConfig to save having to re-calculate it later.

---
 hw/intc/arm_gicv5.c         | 6 ++++++
 include/hw/intc/arm_gicv5.h | 1 +
 2 files changed, 7 insertions(+)
hw/intc/arm_gicv5.c +0 -0
index cdc6c9d3466..20680a5c225 100644

hw/intc/arm_gicv5.c static bool get_l2_iste_addr(GICv5Common *cs, const GICv5ISTConfig *cfg, · line 602

Quoted 8 lines
hw/intc/arm_gicv5.c +5 -0
@@ -595,6 +595,11 @@ static bool get_l2_iste_addr(GICv5Common *cs, const GICv5ISTConfig *cfg,
             return false;
         }
         l2_base = l1_iste & R_L1_ISTE_L2_ADDR_MASK;
+        /*
+         * The effective base address of the L2 table is always
+         * aligned to the size of the L2 table.
+         */
+        l2_base &= MAKE_64BIT_MASK(0, cfg->l2bits);

Here too we are missing a ~ .

-- PMM

#5 lore raw

PATCH 3/6 hw/intc/arm_gicv5: Regularize spacing in config_readl() switch

In reply to Peter Maydell

In this file we don't put blank lines between cases in switches, except that for the switch in config_readl() we've written it half with blank lines and half without. Delete the blank lines to make it consistent with itself and the rest of the file.

---
I figured probably better to do this before adding a bunch more cases to the switch, which virt support will do.

---
 hw/intc/arm_gicv5.c | 15 ---------------
 1 file changed, 15 deletions(-)
hw/intc/arm_gicv5.c +0 -15
index 20680a5c225..7480c12413b 100644
@@ -1468,53 +1468,41 @@ static bool config_readl(GICv5 *s, GICv5Domain domain, hwaddr offset,
             v &= ~R_IRS_IDR0_VIRT_ONE_N_MASK;
         }
         return true;
-
     case A_IRS_IDR1:
         *data = cs->irs_idr1;
         return true;
-
     case A_IRS_IDR2:
         *data = cs->irs_idr2;
         return true;
-
     case A_IRS_IDR3:
         /* In EL3 IDR0.VIRT is 0 so this is RES0 */
         *data = domain == GICV5_ID_EL3 ? 0 : cs->irs_idr3;
         return true;
-
     case A_IRS_IDR4:
         /* In EL3 IDR0.VIRT is 0 so this is RES0 */
         *data = domain == GICV5_ID_EL3 ? 0 : cs->irs_idr4;
         return true;
-
     case A_IRS_IDR5:
         *data = cs->irs_idr5;
         return true;
-
     case A_IRS_IDR6:
         *data = cs->irs_idr6;
         return true;
-
     case A_IRS_IDR7:
         *data = cs->irs_idr7;
         return true;
-
     case A_IRS_IIDR:
         *data = cs->irs_iidr;
         return true;
-
     case A_IRS_AIDR:
         *data = cs->irs_aidr;
         return true;
-
     case A_IRS_IST_BASER:
         *data = extract64(cs->irs_ist_baser[domain], 0, 32);
         return true;
-
     case A_IRS_IST_BASER + 4:
         *data = extract64(cs->irs_ist_baser[domain], 32, 32);
         return true;
-
     case A_IRS_IST_STATUSR:
         /*
          * For QEMU writes to IRS_IST_BASER and IRS_MAP_L2_ISTR take effect
@@ -1522,11 +1510,9 @@ static bool config_readl(GICv5 *s, GICv5Domain domain, hwaddr offset,
          */
         *data = R_IRS_IST_STATUSR_IDLE_MASK;
         return true;
-
     case A_IRS_IST_CFGR:
         *data = cs->irs_ist_cfgr[domain];
         return true;
-
     case A_IRS_SPI_STATUSR:
         /*
          * QEMU writes to IRS_SPI_{CFGR,DOMAINR,SELR,VMR} take effect
@@ -1537,7 +1523,6 @@ static bool config_readl(GICv5 *s, GICv5Domain domain, hwaddr offset,
         v = FIELD_DP32(v, IRS_SPI_STATUSR, IDLE, 1);
         *data = v;
         return true;
-
     case A_IRS_SPI_CFGR:
     {
         GICv5SPIState *spi = spi_for_selr(cs, domain);
Signature · 1 line
2.43.0
Reviewed-by on v1 3/6
#6 lore raw

Re: [PATCH 3/6] hw/intc/arm_gicv5: Regularize spacing in config_readl() switch

Quoted 5 lines · Peter Maydell

In this file we don't put blank lines between cases in switches, except that for the switch in config_readl() we've written it half with blank lines and half without. Delete the blank lines to make it consistent with itself and the rest of the file.

Quoted 7 lines

---
I figured probably better to do this before adding a bunch more cases to the switch, which virt support will do.

---
  hw/intc/arm_gicv5.c | 15 ---------------
  1 file changed, 15 deletions(-)
#7 lore raw

PATCH 4/6 hw/intc/arm_gicv5: Use cached IRS_IST_BASER.VALID state

In reply to Peter Maydell

When the guest writes to IRS_IST_BASER we effectively cache the value of the VALID bit in the GICv5ISTConfig valid field entry. In a few places we check whether the IST is valid by doing a bit extract on the register value; switch those to look at cfg->structure instead.

---
 hw/intc/arm_gicv5.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
hw/intc/arm_gicv5.c +3 -4
index 7480c12413b..8ffb2a49020 100644
@@ -1223,8 +1223,7 @@ static void irs_map_l2_istr_write(GICv5 *s, GICv5Domain domain, uint64_t value)
     uint64_t l1_iste;
     MemTxResult res;
 
-    if (!FIELD_EX64(cs->irs_ist_baser[domain], IRS_IST_BASER, VALID) ||
-        !cfg->structure) {
+    if (!cfg->valid || !cfg->structure) {
         /* WI if no IST set up or it is not 2-level */
         return;
     }
@@ -1294,7 +1293,7 @@ static void irs_ist_baser_write(GICv5 *s, GICv5Domain domain, uint64_t value)
 {
     GICv5Common *cs = ARM_GICV5_COMMON(s);
 
-    if (FIELD_EX64(cs->irs_ist_baser[domain], IRS_IST_BASER, VALID)) {
+    if (s->phys_lpi_config[domain].valid) {
         /* If VALID is set, ADDR is RO and we can only update VALID */
         bool valid = FIELD_EX64(value, IRS_IST_BASER, VALID);
         if (valid) {
@@ -1604,7 +1603,7 @@ static bool config_writel(GICv5 *s, GICv5Domain domain, hwaddr offset,
                             deposit64(cs->irs_ist_baser[domain], 32, 32, data));
         return true;
     case A_IRS_IST_CFGR:
-        if (FIELD_EX64(cs->irs_ist_baser[domain], IRS_IST_BASER, VALID)) {
+        if (s->phys_lpi_config[domain].valid) {
             qemu_log_mask(LOG_GUEST_ERROR,
                           "guest tried to write IRS_IST_CFGR for %s config frame "
                           "while IST_BASER.VALID set\n", domain_name[domain]);
Signature · 1 line
2.43.0
#8 lore raw

PATCH 5/6 hw/intc/arm_gicv5: Free memory in finalize

In reply to Peter Maydell

The GICv5 allocates memory and creates hashtables during realize and as part of its operation. Free those resources in finalize.

Since the GICv5 is created at QEMU startup and remains in existence for the lifetime of the process, this isn't an in-practice leak, only a theoretical one.

---
 hw/intc/arm_gicv5.c        | 11 +++++++++++
 hw/intc/arm_gicv5_common.c |  3 +++
 2 files changed, 14 insertions(+)
hw/intc/arm_gicv5.c +11 -0
index 8ffb2a49020..de0044df9c6 100644
@@ -1970,6 +1970,17 @@ static void gicv5_init(Object *obj)
 
 static void gicv5_finalize(Object *obj)
 {
+    GICv5 *s = ARM_GICV5(obj);
+
+    for (int i = 0; i < NUM_GICV5_DOMAINS; i++) {
+        GHashTable *ht = s->phys_lpi_config[i].lpi_cache;
+
+        if (ht) {
+            g_hash_table_destroy(ht);
+        }
+
+        g_free(s->hppi[i]);
+    }
 }
 
 static void gicv5_class_init(ObjectClass *oc, const void *data)
hw/intc/arm_gicv5_common.c +3 -0
index b155486af65..cc67246686e 100644
@@ -112,6 +112,9 @@ static void gicv5_common_init(Object *obj)
 
 static void gicv5_common_finalize(Object *obj)
 {
+    GICv5Common *cs = ARM_GICV5_COMMON(obj);
+
+    g_free(cs->spi);
 }
 
 static void gicv5_common_realize(DeviceState *dev, Error **errp)
Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH 5/6] hw/intc/arm_gicv5: Free memory in finalize

Hi Peter,

Quoted 2 lines · Peter Maydell

The GICv5 allocates memory and creates hashtables during realize and as part of its operation. Free those resources in finalize.

Why not in unrealize() path?

Quoted 9 lines

Since the GICv5 is created at QEMU startup and remains in existence for the lifetime of the process, this isn't an in-practice leak, only a theoretical one.

---
  hw/intc/arm_gicv5.c        | 11 +++++++++++
  hw/intc/arm_gicv5_common.c |  3 +++
  2 files changed, 14 insertions(+)
#10 lore raw

Re: [PATCH 5/6] hw/intc/arm_gicv5: Free memory in finalize

Quoted 8 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 2 lines · Peter Maydell

The GICv5 allocates memory and creates hashtables during realize and as part of its operation. Free those resources in finalize.

Why not in unrealize() path?

No particular strong reason. Do we have some life-cycle/style guidance on when we should put things in unrealize vs finalize ?

-- PMM

#11 lore raw

Re: [PATCH 5/6] hw/intc/arm_gicv5: Free memory in finalize

Quoted 6 lines · Peter Maydell
Quoted 3 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 8 lines
Quoted 5 lines · Peter Maydell
Quoted 2 lines

The GICv5 allocates memory and creates hashtables during realize and as part of its operation. Free those resources in finalize.

Why not in unrealize() path?

No particular strong reason. Do we have some life-cycle/style guidance on when we should put things in unrealize vs finalize ?

Looking at this again, I noticed that to put these in an unrealize we would need to register an unrealize method we don't have now, using device_class_set_parent_unrealize() for the child classes, and have the child classes chain up to the parent unrealize. On the other hand, we have (empty) finalize methods in these classes already and you get the chaining up to parent classes for free.

So unless we have a reason or concrete style guidance saying why we ought to prefer to do cleanup in unrealize rather than finalize, I'm going to leave this as is for v2 of this series.

thanks
-- PMM

#12 lore raw

PATCH 6/6 target/arm: GICv5 cpuif: Use arm_cpu_set_irq() to set IRQ/FIQ status

In reply to Peter Maydell

Currently the GICv5 cpu interface directly calls cpu_interrupt() and cpu_reset_interrupt() to raise and lower IRQ and FIQ. This is almost but not exactly what we do for GICv2/GICv3 external GPIO IRQ/FIQ line changes. Those are handled by arm_cpu_set_irq(), and as well as calling cpu_interrupt() or cpu_reset_interrupt() they also update env->irq_line_state.

For IRQ and FIQ it doesn't matter whether we set and clear the bits in irq_line_state, because nothing looks at them. But for VIRQ and VFIQ it will matter, because VIRQ and VFIQ are the logical OR of the line state and some bits in HCR_EL2. We're going to need to call arm_cpu_set_irq() for VIRQ and VFIQ; for consistency, handle IRQ and FIQ that way too.

Since arm_cpu_set_irq() is now a function called from other parts of the CPU code, we make its first argument a properly typed one, with a wrapper taking void* for when we need to use it as a qemu_irq_handler.

---
 target/arm/cpu.c             | 14 +++++++++++---
 target/arm/internals.h       | 14 ++++++++++++++
 target/arm/tcg/gicv5-cpuif.c | 35 ++++-------------------------------
 3 files changed, 29 insertions(+), 34 deletions(-)
target/arm/cpu.c +11 -3
index 77aa78f00e2..2b1b87b0ed4 100644
@@ -806,9 +806,8 @@ void arm_emulate_firmware_reset(CPUState *cpustate, int target_el)
 
 
 #ifndef CONFIG_USER_ONLY
-static void arm_cpu_set_irq(void *opaque, int irq, int level)
+void arm_cpu_set_irq(ARMCPU *cpu, int irq, int level)
 {
-    ARMCPU *cpu = opaque;
     CPUARMState *env = &cpu->env;
     CPUState *cs = CPU(cpu);
     static const int mask[] = {
@@ -860,6 +859,15 @@ static void arm_cpu_set_irq(void *opaque, int irq, int level)
     }
 }
 
+/*
+ * Wrapper of arm_cpu_set_irq with the right argument types to be a
+ * qemu_irq_handler function for our inbound GPIO lines.
+ */
+static void arm_cpu_gpio_set_irq(void *opaque, int irq, int level)
+{
+    arm_cpu_set_irq(opaque, irq, level);
+}
+
 static bool arm_cpu_internal_is_big_endian(CPUState *cs)
 {
     ARMCPU *cpu = ARM_CPU(cs);
@@ -1263,7 +1271,7 @@ static void arm_cpu_initfn(Object *obj)
          */
         qdev_init_gpio_in(DEVICE(cpu), arm_cpu_kvm_set_irq, 6);
     } else {
-        qdev_init_gpio_in(DEVICE(cpu), arm_cpu_set_irq, 6);
+        qdev_init_gpio_in(DEVICE(cpu), arm_cpu_gpio_set_irq, 6);
     }
 
     qdev_init_gpio_out(DEVICE(cpu), cpu->gt_timer_outputs,
target/arm/internals.h +14 -0
index f6b4e173a54..f6c9f984952 100644
@@ -1882,6 +1882,20 @@ void define_gcs_cpregs(ARMCPU *cpu);
 /* Add the cpreg definitions for OMAP CP15 regs */
 void define_omap_cp_regs(ARMCPU *cpu);
 
+#ifndef CONFIG_USER_ONLY
+/**
+ * arm_cpu_set_irq: Assert or clear an IRQ/FIQ/etc interrupt line
+ * @cpu: CPU to set interrupt on
+ * @irq: one of the ARM_CPU_IRQ/ARM_CPU_FIQ/ARM_CPU_VIRQ/etc values
+ * @level: 0 to deassert, non-zero to assert
+ *
+ * This function is called by the qemu_irq_handler function
+ * when IRQ etc are real inbound GPIO lines; it is also called
+ * directly by the GICv5 CPU interface.
+ */
+void arm_cpu_set_irq(ARMCPU *cpu, int irq, int level);
+#endif
+
 /* Add the cpreg definitions for the GICv5 CPU interface */
 void define_gicv5_cpuif_regs(ARMCPU *cpu);
 
target/arm/tcg/gicv5-cpuif.c +4 -31
index a6e6c440849..517c780d1f7 100644
@@ -173,33 +173,6 @@ static GICv5PendingIrq gic_hppi(CPUARMState *env, GICv5Domain domain)
     return best;
 }
 
-static void cpu_interrupt_update(CPUARMState *env, int irqtype, bool new_state)
-{
-    CPUState *cs = env_cpu(env);
-
-    /*
-     * OPT: calling cpu_interrupt() and cpu_reset_interrupt() has the
-     * correct behaviour, but is not optimal for the case where we're
-     * setting the interrupt line to the same level it already has.
-     *
-     * Clearing an already clear interrupt is free (it's just doing an
-     * atomic AND operation). Signalling an already set interrupt is a
-     * bit less ideal (it might unnecessarily kick the CPU).
-     *
-     * We could potentially use cpu_test_interrupt(), like
-     * arm_cpu_update_{virq,vfiq,vinmi,vserr}, since we always hold
-     * the BQL here; or perhaps there is an abstraction we could
-     * provide in the core code that all these places could call.
-     *
-     * For now, this is simple and definitely correct.
-     */
-    if (new_state) {
-        cpu_interrupt(cs, irqtype);
-    } else {
-        cpu_reset_interrupt(cs, irqtype);
-    }
-}
-
 static void gicv5_update_irq_fiq(CPUARMState *env)
 {
     /*
@@ -243,12 +216,12 @@ static void gicv5_update_irq_fiq(CPUARMState *env)
     /*
      * Unlike a GICv3 or GICv2, there is no external IRQ or FIQ line
      * to the CPU. Instead we directly signal the interrupt via
-     * cpu_interrupt()/cpu_reset_interrupt().
+     * arm_cpu_set_irq().
      */
     trace_gicv5_update_irq_fiq(irq, fiq, superpriority);
-    cpu_interrupt_update(env, CPU_INTERRUPT_HARD, irq);
-    cpu_interrupt_update(env, CPU_INTERRUPT_FIQ, fiq);
-    cpu_interrupt_update(env, CPU_INTERRUPT_NMI, superpriority);
+    arm_cpu_set_irq(env_archcpu(env), ARM_CPU_IRQ, irq);
+    arm_cpu_set_irq(env_archcpu(env), ARM_CPU_FIQ, fiq);
+    arm_cpu_set_irq(env_archcpu(env), ARM_CPU_NMI, superpriority);
 }
 
 static void gic_recalc_ppi_hppi(CPUARMState *env)
Signature · 1 line
2.43.0