Skip to contentSkip to conversation

spapr: introduce an IRQ allocator at the machine level

PATCH Superseded Cédric Le Goater 1 participant 14 messages Nov 9, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover spapr: introduce an IRQ allocator at the machine level

Hello,

Currently, the ICSState 'ics' object of the sPAPR machine acts as the global interrupt source handler and also as the IRQ number allocator for the machine. Some IRQ numbers are allocated very early in the machine initialization sequence to populate the device tree, and this is a problem to introduce the new POWER XIVE interrupt model, as it needs to share the IRQ numbers with the older model.

To prepare ground for XIVE, here is a proposal adding a set of new XICSFabric operations to let the machine handle directly the IRQ number allocation and to decorrelate the allocation from the interrupt source object :

    bool (*irq_test)(XICSFabric *xi, int irq);
    int (*irq_alloc_block)(XICSFabric *xi, int count, int align);
    void (*irq_free_block)(XICSFabric *xi, int irq, int num);
    bool (*irq_is_lsi)(XICSFabric *xi, int irq);

In these prototypes, the 'irq' parameter refers to a number in the global IRQ number space.

On the sPAPR platform, these operations are simply backed by a bitmap and to handle migration compatibility, we introduce a machine class flag 'has_irq_bitmap', which is set to false for older machines.

To completely remove the use of the ICSState object (required to introduce XIVE), we also need to change how the nature of an interrupt, MSI or LSI, is stored. Today, this is done using the flag attribute of the ICSIRQState array. We change that by splitting the IRQ number space of the machine in two: first the LSIs and then the MSIs. This has the benefit to keep the LSI IRQ numbers in a well known range which is useful for PHB hotplug.

The git repo for this pachset can be found here along with the latest XIVE model:

https://github.com/legoater/qemu/commits/xive

Thanks,

C.

Tests :

- make check on each patch
- migration :

     qemu-2.12 (pseries-2.12) <->  qemu-2.12 (pseries-2.12)
     qemu-2.12 (pseries-2.10) <->  qemu-2.12 (pseries-2.10)
     qemu-2.10 (pseries-2.10) <->  qemu-2.12 (pseries-2.10)

Changes since v1 :

- reorganised patchset to introduce the XICSFabric operations before the major changes: bitmap and IRQ number space split
- introduced a reference bitmap to save some state in migration

Cédric Le Goater (12):
spapr: add pseries 2.12 machine type
ppc/xics: remove useless if condition
spapr: introduce new XICSFabric operations for an IRQ allocator
spapr: move current IRQ allocation under the machine
spapr: introduce an IRQ allocator using a bitmap
spapr: store a reference IRQ bitmap
spapr: introduce an 'irq_base' number
spapr: remove the use of ics_valid_irq()
spapr: introduce a XICSFabric is_lsi() operation
spapr: split the IRQ number space for LSI interrupts
sparp: merge ics_set_irq_type() in irq_alloc_block() operation
spapr: use sPAPRMachineState in spapr_ics_ prototypes

 hw/intc/trace-events   |   2 -
 hw/intc/xics.c         |  37 ++++++-----
 hw/intc/xics_kvm.c     |   4 +-
 hw/intc/xics_spapr.c   |  76 ++++-----------------
 hw/ppc/pnv.c           |  34 ++++++++++
 hw/ppc/pnv_psi.c       |   4 --
 hw/ppc/spapr.c         | 174 ++++++++++++++++++++++++++++++++++++++++++++++++-
 hw/ppc/spapr_events.c  |   4 +-
 hw/ppc/spapr_pci.c     |   8 +--
 hw/ppc/spapr_vio.c     |   2 +-
 hw/ppc/trace-events    |   2 +
 include/hw/ppc/spapr.h |   6 ++
 include/hw/ppc/xics.h  |  20 ++++--
 13 files changed, 267 insertions(+), 106 deletions(-)
Signature · 1 line
2.13.6
#1 lore raw

PATCH v2 1/12 spapr: add pseries 2.12 machine type

---
 hw/ppc/spapr.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
hw/ppc/spapr.c +15 -1
index d682f013d422..a2dcbee07214 100644
@@ -3687,6 +3687,20 @@ static const TypeInfo spapr_machine_info = {
     type_init(spapr_machine_register_##suffix)
 
 /*
+ * pseries-2.12
+ */
+static void spapr_machine_2_12_instance_options(MachineState *machine)
+{
+}
+
+static void spapr_machine_2_12_class_options(MachineClass *mc)
+{
+    /* Defaults for the latest behaviour inherited from the base class */
+}
+
+DEFINE_SPAPR_MACHINE(2_12, "2.12", true);
+
+/*
  * pseries-2.11
  */
 static void spapr_machine_2_11_instance_options(MachineState *machine)
@@ -3698,7 +3712,7 @@ static void spapr_machine_2_11_class_options(MachineClass *mc)
     /* Defaults for the latest behaviour inherited from the base class */
 }
 
-DEFINE_SPAPR_MACHINE(2_11, "2.11", true);
+DEFINE_SPAPR_MACHINE(2_11, "2.11", false);
 
 /*
  * pseries-2.10
Signature · 1 line
2.13.6
#2 lore raw

PATCH v2 2/12 ppc/xics: remove useless if condition

In reply to Cédric Le Goater

The previous code section uses a 'first < 0' test and returns. Therefore, there is no need to test the 'first' variable against '>= 0' afterwards.

---
 hw/intc/xics_spapr.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
hw/intc/xics_spapr.c +2 -4
index d98ea8b13068..e8c0a1b3e903 100644
@@ -329,10 +329,8 @@ int spapr_ics_alloc_block(ICSState *ics, int num, bool lsi,
         return -1;
     }
 
-    if (first >= 0) {
-        for (i = first; i < first + num; ++i) {
-            ics_set_irq_type(ics, i, lsi);
-        }
+    for (i = first; i < first + num; ++i) {
+        ics_set_irq_type(ics, i, lsi);
     }
     first += ics->offset;
 
Signature · 1 line
2.13.6
#3 lore raw

PATCH v2 3/12 spapr: introduce new XICSFabric operations for an IRQ allocator

In reply to Cédric Le Goater

Currently, the ICSState 'ics' object of the sPAPR machine acts as the global interrupt source handler and also as the IRQ number allocator for the machine. Some IRQ numbers are allocated very early in the machine initialization sequence to populate the device tree, and this is a problem to introduce the new POWER XIVE interrupt model, as it needs to share the IRQ numbers with the older model.

To prepare ground for XIVE, here is a set of new XICSFabric operations to let the machine handle directly the IRQ number allocation and to decorrelate the allocation from the interrupt source object :

    bool (*irq_test)(XICSFabric *xi, int irq);
    int (*irq_alloc_block)(XICSFabric *xi, int count, int align);
    void (*irq_free_block)(XICSFabric *xi, int irq, int num);

In these prototypes, the 'irq' parameter refers to a number in the global IRQ number space. Indexes for arrays storing different state informations on the interrupts, like the ICSIRQState, are usually named 'srcno'.

---
 hw/ppc/spapr.c        | 19 +++++++++++++++++++
 include/hw/ppc/xics.h |  4 ++++
 2 files changed, 23 insertions(+)
hw/ppc/spapr.c +19 -0
index a2dcbee07214..84d68f2fdbae 100644
@@ -3536,6 +3536,21 @@ static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
     return cpu ? ICP(cpu->intc) : NULL;
 }
 
+static bool spapr_irq_test(XICSFabric *xi, int irq)
+{
+    return false;
+}
+
+static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align)
+{
+    return -1;
+}
+
+static void spapr_irq_free_block(XICSFabric *xi, int irq, int num)
+{
+    ;
+}
+
 static void spapr_pic_print_info(InterruptStatsProvider *obj,
                                  Monitor *mon)
 {
@@ -3630,6 +3645,10 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
     xic->ics_get = spapr_ics_get;
     xic->ics_resend = spapr_ics_resend;
     xic->icp_get = spapr_icp_get;
+    xic->irq_test = spapr_irq_test;
+    xic->irq_alloc_block = spapr_irq_alloc_block;
+    xic->irq_free_block = spapr_irq_free_block;
+
     ispc->print_info = spapr_pic_print_info;
     /* Force NUMA node memory size to be a multiple of
      * SPAPR_MEMORY_BLOCK_SIZE (256M) since that's the granularity
include/hw/ppc/xics.h +4 -0
index 28d248abad61..30e7f2e0a7dd 100644
@@ -175,6 +175,10 @@ typedef struct XICSFabricClass {
     ICSState *(*ics_get)(XICSFabric *xi, int irq);
     void (*ics_resend)(XICSFabric *xi);
     ICPState *(*icp_get)(XICSFabric *xi, int server);
+    /* IRQ allocator helpers */
+    bool (*irq_test)(XICSFabric *xi, int irq);
+    int (*irq_alloc_block)(XICSFabric *xi, int count, int align);
+    void (*irq_free_block)(XICSFabric *xi, int irq, int num);
 } XICSFabricClass;
 
 #define XICS_IRQS_SPAPR               1024
Signature · 1 line
2.13.6
#4 lore raw

PATCH v2 4/12 spapr: move current IRQ allocation under the machine

In reply to Cédric Le Goater

Use the new XICSFabric operations to handle the IRQ number allocation directly under the machine. These changes only move code and adapt it to take into account the new API which uses IRQ numbers.

On PowerNV, only provide a basic irq_test() operation. For the moment, there is no need for more.

---
 hw/intc/trace-events |  2 --
 hw/intc/xics.c       |  3 ++-
 hw/intc/xics_spapr.c | 57 +++++++++-------------------------------------------
 hw/ppc/pnv.c         | 18 +++++++++++++++++
 hw/ppc/spapr.c       | 56 ++++++++++++++++++++++++++++++++++++++++++++++++---
 hw/ppc/trace-events  |  2 ++
 6 files changed, 85 insertions(+), 53 deletions(-)
hw/intc/trace-events +0 -2
index b86f242b0fcf..e34ecf7a16e5 100644
@@ -65,8 +65,6 @@ xics_ics_simple_reject(int nr, int srcno) "reject irq 0x%x [src %d]"
 xics_ics_simple_eoi(int nr) "ics_eoi: irq 0x%x"
 xics_alloc(int irq) "irq %d"
 xics_alloc_block(int first, int num, bool lsi, int align) "first irq %d, %d irqs, lsi=%d, alignnum %d"
-xics_ics_free(int src, int irq, int num) "Source#%d, first irq %d, %d irqs"
-xics_ics_free_warn(int src, int irq) "Source#%d, irq %d is already free"
 
 # hw/intc/s390_flic_kvm.c
 flic_create_device(int err) "flic: create device failed %d"
hw/intc/xics.c +2 -1
index cc9816e7f204..2c4899f278e2 100644
@@ -53,6 +53,7 @@ void icp_pic_print_info(ICPState *icp, Monitor *mon)
 void ics_pic_print_info(ICSState *ics, Monitor *mon)
 {
     uint32_t i;
+    XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(ics->xics);
 
     monitor_printf(mon, "ICS %4x..%4x %p\n",
                    ics->offset, ics->offset + ics->nr_irqs - 1, ics);
@@ -64,7 +65,7 @@ void ics_pic_print_info(ICSState *ics, Monitor *mon)
     for (i = 0; i < ics->nr_irqs; i++) {
         ICSIRQState *irq = ics->irqs + i;
 
-        if (!(irq->flags & XICS_FLAGS_IRQ_MASK)) {
+        if (!xic->irq_test(ics->xics, i + ics->offset)) {
             continue;
         }
         monitor_printf(mon, "  %4x %s %02x %02x\n",
hw/intc/xics_spapr.c +10 -47

108 lines not on this page · raw message

hw/ppc/pnv.c +18 -0
index c35c439d816b..8288940ef9d7 100644
@@ -1018,6 +1018,23 @@ static ICPState *pnv_icp_get(XICSFabric *xi, int pir)
     return cpu ? ICP(cpu->intc) : NULL;
 }
 
+static bool pnv_irq_test(XICSFabric *xi, int irq)
+{
+    PnvMachineState *pnv = POWERNV_MACHINE(xi);
+    int i;
+
+    /* We don't have a IRQ allocator for the PowerNV machine yet, so
+     * just check that the IRQ number is valid for the PSI source
+     */
+    for (i = 0; i < pnv->num_chips; i++) {
+        ICSState *ics = &pnv->chips[i]->psi.ics;
+        if (ics_valid_irq(ics, irq)) {
+            return true;
+        }
+    }
+    return false;
+}
+
 static void pnv_pic_print_info(InterruptStatsProvider *obj,
                                Monitor *mon)
 {
@@ -1102,6 +1119,7 @@ static void powernv_machine_class_init(ObjectClass *oc, void *data)
     xic->icp_get = pnv_icp_get;
     xic->ics_get = pnv_ics_get;
     xic->ics_resend = pnv_ics_resend;
+    xic->irq_test = pnv_irq_test;
     ispc->print_info = pnv_pic_print_info;
 
     powernv_machine_class_props_init(oc);
hw/ppc/spapr.c +53 -3

72 lines not on this page · raw message

hw/ppc/trace-events +2 -0
index 4a6a6490fa78..dc9ab4c4deb3 100644
@@ -12,6 +12,8 @@ spapr_pci_msi_retry(unsigned config_addr, unsigned req_num, unsigned max_irqs) "
 # hw/ppc/spapr.c
 spapr_cas_failed(unsigned long n) "DT diff buffer is too small: %ld bytes"
 spapr_cas_continue(unsigned long n) "Copy changes to the guest: %ld bytes"
+spapr_irq_free(int src, int irq, int num) "Source#%d, first irq %d, %d irqs"
+spapr_irq_free_warn(int src, int irq) "Source#%d, irq %d is already free"
 
 # hw/ppc/spapr_hcall.c
 spapr_cas_pvr_try(uint32_t pvr) "0x%x"
Signature · 1 line
2.13.6
#5 lore raw

PATCH v2 5/12 spapr: introduce an IRQ allocator using a bitmap

In reply to Cédric Le Goater

On the sPAPR platform, these IRQ operations are simply backed by a bitmap 'irq_map' in the machine.

Older pseries machines do not have a bitmap acting as a IRQ number allocator but use the ICSIRQState array for this purpose. This difference between machine versions needs to be taken into account when loading the state.

To keep in sync the bitmap with the ICSIRQState array flags, we introduce a machine class flag 'has_irq_bitmap' set to false for older versions only.

---
 hw/ppc/spapr.c         | 97 ++++++++++++++++++++++++++++++++++----------------
 include/hw/ppc/spapr.h |  4 +++
 2 files changed, 70 insertions(+), 31 deletions(-)
hw/ppc/spapr.c +66 -31
index 481bfb0277df..70f6a96ec909 100644
@@ -1532,6 +1532,7 @@ static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
 static int spapr_post_load(void *opaque, int version_id)
 {
     sPAPRMachineState *spapr = (sPAPRMachineState *)opaque;
+    sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
     int err = 0;
 
     if (!object_dynamic_cast(OBJECT(spapr->ics), TYPE_ICS_KVM)) {
@@ -1562,6 +1563,20 @@ static int spapr_post_load(void *opaque, int version_id)
         }
     }
 
+    /*
+     * Synchronize the IRQ number bitmap with the ICSIRQState array
+     * coming from an older pseries machine
+     */
+    if (!smc->has_irq_bitmap) {
+        int srcno;
+
+        for (srcno = 0; srcno < spapr->ics->nr_irqs; srcno++) {
+            if (spapr->ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MASK &&
+                !test_bit(srcno, spapr->irq_map)) {
+                bitmap_set(spapr->irq_map, srcno, 1);
+            }
+        }
+    }
     return err;
 }
 
@@ -1681,6 +1696,30 @@ static const VMStateDescription vmstate_spapr_patb_entry = {
     },
 };
 
+static bool spapr_irq_map_needed(void *opaque)
+{
+    sPAPRMachineState *spapr = opaque;
+    sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
+
+    /* Never transfer the bitmap on older machines, it doesn't exist */
+    if (!smc->has_irq_bitmap) {
+        return false;
+    }
+
+    return true;
+}
+
+static const VMStateDescription vmstate_spapr_irq_map = {
+    .name = "spapr_irq_map",
+    .version_id = 0,
+    .minimum_version_id = 0,
+    .needed = spapr_irq_map_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_BITMAP(irq_map, sPAPRMachineState, 0, nr_irqs),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 static const VMStateDescription vmstate_spapr = {
     .name = "spapr",
     .version_id = 3,
@@ -1700,6 +1739,7 @@ static const VMStateDescription vmstate_spapr = {
         &vmstate_spapr_ov5_cas,
         &vmstate_spapr_patb_entry,
         &vmstate_spapr_pending_events,
+        &vmstate_spapr_irq_map,
         NULL
     }
 };
@@ -2337,8 +2377,12 @@ static void ppc_spapr_init(MachineState *machine)
     /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
     load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
 
+    /* Initialize the IRQ allocator */
+    spapr->nr_irqs  = XICS_IRQS_SPAPR;
+    spapr->irq_map  = bitmap_new(spapr->nr_irqs);
+
     /* Set up Interrupt Controller before we create the VCPUs */
-    xics_system_init(machine, XICS_IRQS_SPAPR, &error_fatal);
+    xics_system_init(machine, spapr->nr_irqs, &error_fatal);
 
     /* Set up containers for ibm,client-architecture-support negotiated options
      */
@@ -3536,50 +3580,37 @@ static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
     return cpu ? ICP(cpu->intc) : NULL;
 }
 
-#define ICS_IRQ_FREE(ics, srcno)   \
-    (!((ics)->irqs[(srcno)].flags & (XICS_FLAGS_IRQ_MASK)))
-
-static int ics_find_free_block(ICSState *ics, int num, int alignnum)
-{
-    int first, i;
-
-    for (first = 0; first < ics->nr_irqs; first += alignnum) {
-        if (num > (ics->nr_irqs - first)) {
-            return -1;
-        }
-        for (i = first; i < first + num; ++i) {
-            if (!ICS_IRQ_FREE(ics, i)) {
-                break;
-            }
-        }
-        if (i == (first + num)) {
-            return first;
-        }
-    }
-
-    return -1;
-}
-
 static bool spapr_irq_test(XICSFabric *xi, int irq)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
     ICSState *ics = spapr->ics;
     int srcno = irq - ics->offset;
 
-    return ICS_IRQ_FREE(ics, srcno);
+    return test_bit(srcno, spapr->irq_map);
 }
 
 static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
     ICSState *ics = spapr->ics;
+    int start = 0;
     int srcno;
 
-    srcno = ics_find_free_block(ics, count, align);
-    if (srcno == -1) {
+    /*
+     * The 'align_mask' parameter of bitmap_find_next_zero_area()
+     * should be one less than a power of 2; 0 means no
+     * alignment. Adapt the 'align' value of the former allocator to
+     * fit the requirements of bitmap_find_next_zero_area()
+     */
+    align -= 1;
+
+    srcno = bitmap_find_next_zero_area(spapr->irq_map, spapr->nr_irqs, start,
+                                       count, align);
+    if (srcno == spapr->nr_irqs) {
         return -1;
     }
 
+    bitmap_set(spapr->irq_map, srcno, count);
     return srcno + ics->offset;
 }
 
@@ -3593,11 +3624,11 @@ static void spapr_irq_free_block(XICSFabric *xi, int irq, int num)
     if (ics_valid_irq(ics, irq)) {
         trace_spapr_irq_free(0, irq, num);
         for (i = srcno; i < srcno + num; ++i) {
-            if (ICS_IRQ_FREE(ics, i)) {
+            if (!test_bit(i, spapr->irq_map)) {
                 trace_spapr_irq_free_warn(0, i + ics->offset);
             }
-            memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
         }
+        bitmap_clear(spapr->irq_map, srcno, num);
     }
 }
 
@@ -3679,6 +3710,7 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
     hc->unplug_request = spapr_machine_device_unplug_request;
 
     smc->dr_lmb_enabled = true;
+    smc->has_irq_bitmap = true;
     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power8_v2.0");
     mc->has_hotpluggable_cpus = true;
     smc->resize_hpt_default = SPAPR_RESIZE_HPT_ENABLED;
@@ -3778,7 +3810,10 @@ static void spapr_machine_2_11_instance_options(MachineState *machine)
 
 static void spapr_machine_2_11_class_options(MachineClass *mc)
 {
-    /* Defaults for the latest behaviour inherited from the base class */
+    sPAPRMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
+
+    spapr_machine_2_12_class_options(mc);
+    smc->has_irq_bitmap = false;
 }
 
 DEFINE_SPAPR_MACHINE(2_11, "2.11", false);
include/hw/ppc/spapr.h +4 -0
index 9d21ca9bde3a..71faeff17a40 100644
@@ -7,6 +7,7 @@
 #include "hw/ppc/spapr_drc.h"
 #include "hw/mem/pc-dimm.h"
 #include "hw/ppc/spapr_ovec.h"
+#include "qemu/bitmap.h"
 
 struct VIOsPAPRBus;
 struct sPAPRPHBState;
@@ -61,6 +62,7 @@ struct sPAPRMachineClass {
     bool dr_lmb_enabled;       /* enable dynamic-reconfig/hotplug of LMBs */
     bool use_ohci_by_default;  /* use USB-OHCI instead of XHCI */
     bool pre_2_10_has_unused_icps;
+    bool has_irq_bitmap;
     void (*phb_placement)(sPAPRMachineState *spapr, uint32_t index,
                           uint64_t *buid, hwaddr *pio, 
                           hwaddr *mmio32, hwaddr *mmio64,
@@ -78,6 +80,8 @@ struct sPAPRMachineState {
     struct VIOsPAPRBus *vio_bus;
     QLIST_HEAD(, sPAPRPHBState) phbs;
     struct sPAPRNVRAM *nvram;
+    int32_t nr_irqs;
+    unsigned long *irq_map;
     ICSState *ics;
     sPAPRRTCState rtc;
 
Signature · 1 line
2.13.6
#6 lore raw

PATCH v2 6/12 spapr: store a reference IRQ bitmap

In reply to Cédric Le Goater

To save some state when the guest is migrated, we capture the IRQ bitmap after all devices have been reseted and store it as a reference for the machine.

---
 hw/ppc/spapr.c         | 5 ++++-
 include/hw/ppc/spapr.h | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)
hw/ppc/spapr.c +4 -1
index 70f6a96ec909..d4d0a36cc5bf 100644
@@ -1437,6 +1437,9 @@ static void ppc_spapr_reset(void)
     qemu_devices_reset();
     spapr_clear_pending_events(spapr);
 
+    spapr->irq_map_ref = bitmap_new(spapr->nr_irqs);
+    bitmap_copy(spapr->irq_map_ref, spapr->irq_map, spapr->nr_irqs);
+
     /*
      * We place the device tree and RTAS just below either the top of the RMA,
      * or just below 2GB, whichever is lowere, so that it can be
@@ -1706,7 +1709,7 @@ static bool spapr_irq_map_needed(void *opaque)
         return false;
     }
 
-    return true;
+    return !bitmap_equal(spapr->irq_map, spapr->irq_map_ref, spapr->nr_irqs);
 }
 
 static const VMStateDescription vmstate_spapr_irq_map = {
include/hw/ppc/spapr.h +1 -0
index 71faeff17a40..090eda962ccb 100644
@@ -82,6 +82,7 @@ struct sPAPRMachineState {
     struct sPAPRNVRAM *nvram;
     int32_t nr_irqs;
     unsigned long *irq_map;
+    unsigned long *irq_map_ref;
     ICSState *ics;
     sPAPRRTCState rtc;
 
Signature · 1 line
2.13.6
#7 lore raw

PATCH v2 7/12 spapr: introduce an 'irq_base' number

In reply to Cédric Le Goater

'irq_base' is a base IRQ number which lets us allocate only the subset of the IRQ numbers used on the sPAPR platform. It is sync with the ICSState 'offset' attribute and this is slightly redundant. We could also choose to waste some extra bytes (512) and allocate the whole number space. To be discussed.

But more important, it removes a dependency on the ICSState object of the sPAPR machine which is required for XIVE.

---
 hw/ppc/spapr.c         | 11 +++++------
 include/hw/ppc/spapr.h |  1 +
 2 files changed, 6 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +5 -6
index d4d0a36cc5bf..6c3fc12ed8e1 100644
@@ -2383,6 +2383,7 @@ static void ppc_spapr_init(MachineState *machine)
     /* Initialize the IRQ allocator */
     spapr->nr_irqs  = XICS_IRQS_SPAPR;
     spapr->irq_map  = bitmap_new(spapr->nr_irqs);
+    spapr->irq_base = XICS_IRQ_BASE;
 
     /* Set up Interrupt Controller before we create the VCPUs */
     xics_system_init(machine, spapr->nr_irqs, &error_fatal);
@@ -3586,8 +3587,7 @@ static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
 static bool spapr_irq_test(XICSFabric *xi, int irq)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
-    ICSState *ics = spapr->ics;
-    int srcno = irq - ics->offset;
+    int srcno = irq - spapr->irq_base;
 
     return test_bit(srcno, spapr->irq_map);
 }
@@ -3595,7 +3595,6 @@ static bool spapr_irq_test(XICSFabric *xi, int irq)
 static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
-    ICSState *ics = spapr->ics;
     int start = 0;
     int srcno;
 
@@ -3614,21 +3613,21 @@ static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align)
     }
 
     bitmap_set(spapr->irq_map, srcno, count);
-    return srcno + ics->offset;
+    return srcno + spapr->irq_base;
 }
 
 static void spapr_irq_free_block(XICSFabric *xi, int irq, int num)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
     ICSState *ics = spapr->ics;
-    int srcno = irq - ics->offset;
+    int srcno = irq - spapr->irq_base;
     int i;
 
     if (ics_valid_irq(ics, irq)) {
         trace_spapr_irq_free(0, irq, num);
         for (i = srcno; i < srcno + num; ++i) {
             if (!test_bit(i, spapr->irq_map)) {
-                trace_spapr_irq_free_warn(0, i + ics->offset);
+                trace_spapr_irq_free_warn(0, i + spapr->irq_base);
             }
         }
         bitmap_clear(spapr->irq_map, srcno, num);
include/hw/ppc/spapr.h +1 -0
index 090eda962ccb..8d227fe6f56c 100644
@@ -83,6 +83,7 @@ struct sPAPRMachineState {
     int32_t nr_irqs;
     unsigned long *irq_map;
     unsigned long *irq_map_ref;
+    uint32_t irq_base;
     ICSState *ics;
     sPAPRRTCState rtc;
 
Signature · 1 line
2.13.6
#8 lore raw

PATCH v2 8/12 spapr: remove the use of ics_valid_irq()

In reply to Cédric Le Goater

ics_valid_irq() can be replaced by a simple check on the 'srcno' number. It also removes a dependency on the ICSState object of the sPAPR machine.

---
 hw/ppc/spapr.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/ppc/spapr.c +1 -2
index 6c3fc12ed8e1..0c6a4c8bffc0 100644
@@ -3619,11 +3619,10 @@ static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align)
 static void spapr_irq_free_block(XICSFabric *xi, int irq, int num)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
-    ICSState *ics = spapr->ics;
     int srcno = irq - spapr->irq_base;
     int i;
 
-    if (ics_valid_irq(ics, irq)) {
+    if (srcno >= 0 && srcno < spapr->nr_irqs) {
         trace_spapr_irq_free(0, irq, num);
         for (i = srcno; i < srcno + num; ++i) {
             if (!test_bit(i, spapr->irq_map)) {
Signature · 1 line
2.13.6
#9 lore raw

PATCH v2 9/12 spapr: introduce a XICSFabric irq_is_lsi() operation

In reply to Cédric Le Goater

It will be used later on to distinguish the allocation of an LSI interrupt from an MSI and also to reduce the use of the ICSIRQState array of the ICSState object, which is on our way to introduce XIVE.

The 'irq' parameter continues to refer to the global IRQ number space.

On PowerNV, only the PSI controller interrupts are handled and they are all LSIs.

---
 hw/intc/xics.c        | 26 +++++++++++++++++---------
 hw/intc/xics_kvm.c    |  4 ++--
 hw/ppc/pnv.c          | 16 ++++++++++++++++
 hw/ppc/spapr.c        |  9 +++++++++
 include/hw/ppc/xics.h |  2 ++
 5 files changed, 46 insertions(+), 11 deletions(-)
hw/intc/xics.c +17 -9
index 2c4899f278e2..42880e736697 100644
@@ -33,6 +33,7 @@
 #include "trace.h"
 #include "qemu/timer.h"
 #include "hw/ppc/xics.h"
+#include "hw/ppc/spapr.h"
 #include "qemu/error-report.h"
 #include "qapi/visitor.h"
 #include "monitor/monitor.h"
@@ -70,8 +71,7 @@ void ics_pic_print_info(ICSState *ics, Monitor *mon)
         }
         monitor_printf(mon, "  %4x %s %02x %02x\n",
                        ics->offset + i,
-                       (irq->flags & XICS_FLAGS_IRQ_LSI) ?
-                       "LSI" : "MSI",
+                       ics_is_lsi(ics, i) ? "LSI" : "MSI",
                        irq->priority, irq->status);
     }
 }
@@ -377,6 +377,14 @@ static const TypeInfo icp_info = {
 /*
  * ICS: Source layer
  */
+bool ics_is_lsi(ICSState *ics, int srcno)
+{
+    XICSFabric *xi = ics->xics;
+    XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(xi);
+
+    return xic->irq_is_lsi(xi, srcno + ics->offset);
+}
+
 static void ics_simple_resend_msi(ICSState *ics, int srcno)
 {
     ICSIRQState *irq = ics->irqs + srcno;
@@ -435,7 +443,7 @@ static void ics_simple_set_irq(void *opaque, int srcno, int val)
 {
     ICSState *ics = (ICSState *)opaque;
 
-    if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
+    if (ics_is_lsi(ics, srcno)) {
         ics_simple_set_irq_lsi(ics, srcno, val);
     } else {
         ics_simple_set_irq_msi(ics, srcno, val);
@@ -472,7 +480,7 @@ void ics_simple_write_xive(ICSState *ics, int srcno, int server,
     trace_xics_ics_simple_write_xive(ics->offset + srcno, srcno, server,
                                      priority);
 
-    if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
+    if (ics_is_lsi(ics, srcno)) {
         ics_simple_write_xive_lsi(ics, srcno);
     } else {
         ics_simple_write_xive_msi(ics, srcno);
@@ -484,10 +492,10 @@ static void ics_simple_reject(ICSState *ics, uint32_t nr)
     ICSIRQState *irq = ics->irqs + nr - ics->offset;
 
     trace_xics_ics_simple_reject(nr, nr - ics->offset);
-    if (irq->flags & XICS_FLAGS_IRQ_MSI) {
-        irq->status |= XICS_STATUS_REJECTED;
-    } else if (irq->flags & XICS_FLAGS_IRQ_LSI) {
+    if (ics_is_lsi(ics, nr - ics->offset)) {
         irq->status &= ~XICS_STATUS_SENT;
+    } else {
+        irq->status |= XICS_STATUS_REJECTED;
     }
 }
 
@@ -497,7 +505,7 @@ static void ics_simple_resend(ICSState *ics)
 
     for (i = 0; i < ics->nr_irqs; i++) {
         /* FIXME: filter by server#? */
-        if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
+        if (ics_is_lsi(ics, i)) {
             ics_simple_resend_lsi(ics, i);
         } else {
             ics_simple_resend_msi(ics, i);
@@ -512,7 +520,7 @@ static void ics_simple_eoi(ICSState *ics, uint32_t nr)
 
     trace_xics_ics_simple_eoi(nr);
 
-    if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI) {
+    if (ics_is_lsi(ics, srcno)) {
         irq->status &= ~XICS_STATUS_SENT;
     }
 }
hw/intc/xics_kvm.c +2 -2
index 3091ad3ac2c8..2f10637c9f7c 100644
@@ -258,7 +258,7 @@ static int ics_set_kvm_state(ICSState *ics, int version_id)
             state |= KVM_XICS_MASKED;
         }
 
-        if (ics->irqs[i].flags & XICS_FLAGS_IRQ_LSI) {
+        if (ics_is_lsi(ics, i)) {
             state |= KVM_XICS_LEVEL_SENSITIVE;
             if (irq->status & XICS_STATUS_ASSERTED) {
                 state |= KVM_XICS_PENDING;
@@ -293,7 +293,7 @@ static void ics_kvm_set_irq(void *opaque, int srcno, int val)
     int rc;
 
     args.irq = srcno + ics->offset;
-    if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
+    if (!ics_is_lsi(ics, srcno)) {
         if (!val) {
             return;
         }
hw/ppc/pnv.c +16 -0
index 8288940ef9d7..958223376b4c 100644
@@ -1035,6 +1035,21 @@ static bool pnv_irq_test(XICSFabric *xi, int irq)
     return false;
 }
 
+static bool pnv_irq_is_lsi(XICSFabric *xi, int irq)
+{
+    PnvMachineState *pnv = POWERNV_MACHINE(xi);
+    int i;
+
+    /* PowerNV machine only has PSI interrupts which are all LSIs */
+    for (i = 0; i < pnv->num_chips; i++) {
+        ICSState *ics = &pnv->chips[i]->psi.ics;
+        if (ics_valid_irq(ics, irq)) {
+            return true;
+        }
+    }
+    return false;
+}
+
 static void pnv_pic_print_info(InterruptStatsProvider *obj,
                                Monitor *mon)
 {
@@ -1120,6 +1135,7 @@ static void powernv_machine_class_init(ObjectClass *oc, void *data)
     xic->ics_get = pnv_ics_get;
     xic->ics_resend = pnv_ics_resend;
     xic->irq_test = pnv_irq_test;
+    xic->irq_is_lsi = pnv_irq_is_lsi;
     ispc->print_info = pnv_pic_print_info;
 
     powernv_machine_class_props_init(oc);
hw/ppc/spapr.c +9 -0
index 0c6a4c8bffc0..86d5e310e85f 100644
@@ -3633,6 +3633,14 @@ static void spapr_irq_free_block(XICSFabric *xi, int irq, int num)
     }
 }
 
+static bool spapr_irq_is_lsi(XICSFabric *xi, int irq)
+{
+    sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
+    int srcno = irq - spapr->irq_base;
+
+    return spapr->ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI;
+}
+
 static void spapr_pic_print_info(InterruptStatsProvider *obj,
                                  Monitor *mon)
 {
@@ -3731,6 +3739,7 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
     xic->irq_test = spapr_irq_test;
     xic->irq_alloc_block = spapr_irq_alloc_block;
     xic->irq_free_block = spapr_irq_free_block;
+    xic->irq_is_lsi = spapr_irq_is_lsi;
 
     ispc->print_info = spapr_pic_print_info;
     /* Force NUMA node memory size to be a multiple of
include/hw/ppc/xics.h +2 -0
index 30e7f2e0a7dd..478f8e510179 100644
@@ -179,6 +179,7 @@ typedef struct XICSFabricClass {
     bool (*irq_test)(XICSFabric *xi, int irq);
     int (*irq_alloc_block)(XICSFabric *xi, int count, int align);
     void (*irq_free_block)(XICSFabric *xi, int irq, int num);
+    bool (*irq_is_lsi)(XICSFabric *xi, int irq);
 } XICSFabricClass;
 
 #define XICS_IRQS_SPAPR               1024
@@ -205,6 +206,7 @@ void ics_simple_write_xive(ICSState *ics, int nr, int server,
 void ics_set_irq_type(ICSState *ics, int srcno, bool lsi);
 void icp_pic_print_info(ICPState *icp, Monitor *mon);
 void ics_pic_print_info(ICSState *ics, Monitor *mon);
+bool ics_is_lsi(ICSState *ics, int srno);
 
 void ics_resend(ICSState *ics);
 void icp_resend(ICPState *ss);
Signature · 1 line
2.13.6
#10 lore raw

PATCH v2 10/12 spapr: split the IRQ number space for LSI interrupts

In reply to Cédric Le Goater

The nature of an interrupt, MSI or LSI, is stored under the flag attribute of the ICSIRQState array. To reduce the use of this array and consequently of the ICSState object (needed to introduce for the new XIVE model), we choose to split the IRQ number space of the machine in two: first the LSIs and then the MSIs.

This also has the benefit to keep the LSI IRQ numbers in a well known range which will be useful for PHB hotplug.

For compatibility with older machines, we use the machine class flag 'has_irq_bitmap'.

---
 hw/intc/xics_spapr.c  |  6 +++---
 hw/ppc/spapr.c        | 27 ++++++++++++++++++++++++++-
 include/hw/ppc/xics.h |  2 +-
 3 files changed, 30 insertions(+), 5 deletions(-)
hw/intc/xics_spapr.c +3 -3
index de9e65d35247..b8e91aaf52bd 100644
@@ -260,7 +260,7 @@ int spapr_ics_alloc(ICSState *ics, int irq_hint, bool lsi, Error **errp)
         }
         irq = irq_hint;
     } else {
-        irq = xic->irq_alloc_block(ics->xics, 1, 1);
+        irq = xic->irq_alloc_block(ics->xics, 1, 1, lsi);
         if (irq < 0) {
             error_setg(errp, "can't allocate IRQ: no IRQ left");
             return -1;
@@ -297,9 +297,9 @@ int spapr_ics_alloc_block(ICSState *ics, int num, bool lsi,
     if (align) {
         assert((num == 1) || (num == 2) || (num == 4) ||
                (num == 8) || (num == 16) || (num == 32));
-        first = xic->irq_alloc_block(ics->xics, num, num);
+        first = xic->irq_alloc_block(ics->xics, num, num, lsi);
     } else {
-        first = xic->irq_alloc_block(ics->xics, num, 1);
+        first = xic->irq_alloc_block(ics->xics, num, 1, lsi);
     }
     if (first < 0) {
         error_setg(errp, "can't find a free %d-IRQ block", num);
hw/ppc/spapr.c +26 -1
index 86d5e310e85f..d576e235d226 100644
@@ -3592,9 +3592,21 @@ static bool spapr_irq_test(XICSFabric *xi, int irq)
     return test_bit(srcno, spapr->irq_map);
 }
 
-static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align)
+
+/*
+ * Let's provision 4 LSIs per PHBs
+ */
+#define SPAPR_MAX_LSI (SPAPR_MAX_PHBS * 4)
+
+/*
+ * Split the IRQ number space of the machine in two: first the LSIs
+ * and then the MSIs. This allows us to keep the LSI IRQ numbers in a
+ * well known range which is useful for PHB hotplug.
+ */
+static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align, bool lsi)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
+    sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
     int start = 0;
     int srcno;
 
@@ -3606,12 +3618,20 @@ static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align)
      */
     align -= 1;
 
+    if (!lsi && smc->has_irq_bitmap) {
+        start = SPAPR_MAX_LSI;
+    }
+
     srcno = bitmap_find_next_zero_area(spapr->irq_map, spapr->nr_irqs, start,
                                        count, align);
     if (srcno == spapr->nr_irqs) {
         return -1;
     }
 
+    if (lsi && smc->has_irq_bitmap && srcno >= SPAPR_MAX_LSI) {
+        return -1;
+    }
+
     bitmap_set(spapr->irq_map, srcno, count);
     return srcno + spapr->irq_base;
 }
@@ -3636,8 +3656,13 @@ static void spapr_irq_free_block(XICSFabric *xi, int irq, int num)
 static bool spapr_irq_is_lsi(XICSFabric *xi, int irq)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
+    sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
     int srcno = irq - spapr->irq_base;
 
+    if (smc->has_irq_bitmap) {
+        return (srcno >= 0) && (srcno < SPAPR_MAX_LSI);
+    }
+
     return spapr->ics->irqs[srcno].flags & XICS_FLAGS_IRQ_LSI;
 }
 
include/hw/ppc/xics.h +1 -1
index 478f8e510179..292b929e88eb 100644
@@ -177,7 +177,7 @@ typedef struct XICSFabricClass {
     ICPState *(*icp_get)(XICSFabric *xi, int server);
     /* IRQ allocator helpers */
     bool (*irq_test)(XICSFabric *xi, int irq);
-    int (*irq_alloc_block)(XICSFabric *xi, int count, int align);
+    int (*irq_alloc_block)(XICSFabric *xi, int count, int align, bool lsi);
     void (*irq_free_block)(XICSFabric *xi, int irq, int num);
     bool (*irq_is_lsi)(XICSFabric *xi, int irq);
 } XICSFabricClass;
Signature · 1 line
2.13.6
#11 lore raw

PATCH v2 11/12 sparp: merge ics_set_irq_type() in irq_alloc_block() operation

In reply to Cédric Le Goater

Setting the XICS_FLAGS_IRQ_LSI (or XICS_FLAGS_IRQ_MSI) for older pseries machines can now be done directly under the irq_alloc_block() operation.

For compatibility with older machines, we use the machine class flag 'has_irq_bitmap'.

---
 hw/intc/xics.c        |  8 --------
 hw/intc/xics_spapr.c  |  7 +------
 hw/ppc/pnv_psi.c      |  4 ----
 hw/ppc/spapr.c        | 14 ++++++++++++++
 include/hw/ppc/xics.h |  1 -
 5 files changed, 15 insertions(+), 19 deletions(-)
hw/intc/xics.c +0 -8
index 42880e736697..237eed3c11f8 100644
@@ -710,14 +710,6 @@ ICPState *xics_icp_get(XICSFabric *xi, int server)
     return xic->icp_get(xi, server);
 }
 
-void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
-{
-    assert(!(ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MASK));
-
-    ics->irqs[srcno].flags |=
-        lsi ? XICS_FLAGS_IRQ_LSI : XICS_FLAGS_IRQ_MSI;
-}
-
 static void xics_register_types(void)
 {
     type_register_static(&ics_simple_info);
hw/intc/xics_spapr.c +1 -6
index b8e91aaf52bd..f28e9136f2f6 100644
@@ -267,7 +267,6 @@ int spapr_ics_alloc(ICSState *ics, int irq_hint, bool lsi, Error **errp)
         }
     }
 
-    ics_set_irq_type(ics, irq - ics->offset, lsi);
     trace_xics_alloc(irq);
 
     return irq;
@@ -280,7 +279,7 @@ int spapr_ics_alloc(ICSState *ics, int irq_hint, bool lsi, Error **errp)
 int spapr_ics_alloc_block(ICSState *ics, int num, bool lsi,
                           bool align, Error **errp)
 {
-    int i, first = -1;
+    int first = -1;
     XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(ics->xics);
 
     if (!ics) {
@@ -306,10 +305,6 @@ int spapr_ics_alloc_block(ICSState *ics, int num, bool lsi,
         return -1;
     }
 
-    for (i = first; i < first + num; ++i) {
-        ics_set_irq_type(ics, i - ics->offset, lsi);
-    }
-
     trace_xics_alloc_block(first, num, lsi, align);
 
     return first;
hw/ppc/pnv_psi.c +0 -4
index 9876c266223d..ee7fca311cbf 100644
@@ -487,10 +487,6 @@ static void pnv_psi_realize(DeviceState *dev, Error **errp)
         return;
     }
 
-    for (i = 0; i < ics->nr_irqs; i++) {
-        ics_set_irq_type(ics, i, true);
-    }
-
     /* XSCOM region for PSI registers */
     pnv_xscom_region_init(&psi->xscom_regs, OBJECT(dev), &pnv_psi_xscom_ops,
                 psi, "xscom-psi", PNV_XSCOM_PSIHB_SIZE);
hw/ppc/spapr.c +14 -0
index d576e235d226..0cf1e02fa2b7 100644
@@ -3592,6 +3592,13 @@ static bool spapr_irq_test(XICSFabric *xi, int irq)
     return test_bit(srcno, spapr->irq_map);
 }
 
+static void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
+{
+    assert(!(ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MASK));
+
+    ics->irqs[srcno].flags |=
+        lsi ? XICS_FLAGS_IRQ_LSI : XICS_FLAGS_IRQ_MSI;
+}
 
 /*
  * Let's provision 4 LSIs per PHBs
@@ -3632,6 +3639,13 @@ static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align, bool lsi)
         return -1;
     }
 
+    if (!smc->has_irq_bitmap) {
+        int i;
+        for (i = srcno; i < srcno + count; ++i) {
+            ics_set_irq_type(spapr->ics, i, lsi);
+        }
+    }
+
     bitmap_set(spapr->irq_map, srcno, count);
     return srcno + spapr->irq_base;
 }
include/hw/ppc/xics.h +0 -1
index 292b929e88eb..056cf37bc68f 100644
@@ -203,7 +203,6 @@ void icp_eoi(ICPState *icp, uint32_t xirr);
 void ics_simple_write_xive(ICSState *ics, int nr, int server,
                            uint8_t priority, uint8_t saved_priority);
 
-void ics_set_irq_type(ICSState *ics, int srcno, bool lsi);
 void icp_pic_print_info(ICPState *icp, Monitor *mon);
 void ics_pic_print_info(ICSState *ics, Monitor *mon);
 bool ics_is_lsi(ICSState *ics, int srno);
Signature · 1 line
2.13.6
#12 lore raw

PATCH v2 12/12 spapr: use sPAPRMachineState in spapr_ics_ prototypes

In reply to Cédric Le Goater

The routines manipulating the IRQ numbers for the sPAPR machine do not have any relation with the ICSState anymore. So use a sPAPRMachineState parameter in their prototype and prefix them with spapr_irq_.

---
 hw/intc/xics_spapr.c  | 30 ++++++++++++------------------
 hw/ppc/spapr.c        |  5 +++--
 hw/ppc/spapr_events.c |  4 ++--
 hw/ppc/spapr_pci.c    |  8 ++++----
 hw/ppc/spapr_vio.c    |  2 +-
 include/hw/ppc/xics.h | 13 +++++++------
 6 files changed, 29 insertions(+), 33 deletions(-)
hw/intc/xics_spapr.c +12 -18
index f28e9136f2f6..b5c8b8fa0e89 100644
@@ -245,22 +245,20 @@ void xics_spapr_init(sPAPRMachineState *spapr)
     spapr_register_hypercall(H_IPOLL, h_ipoll);
 }
 
-int spapr_ics_alloc(ICSState *ics, int irq_hint, bool lsi, Error **errp)
+int spapr_irq_alloc(sPAPRMachineState *spapr, int irq_hint, bool lsi,
+                    Error **errp)
 {
     int irq;
-    XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(ics->xics);
+    XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(spapr);
 
-    if (!ics) {
-        return -1;
-    }
     if (irq_hint) {
-        if (xic->irq_test(ics->xics, irq_hint)) {
+        if (xic->irq_test(XICS_FABRIC(spapr), irq_hint)) {
             error_setg(errp, "can't allocate IRQ %d: already in use", irq_hint);
             return -1;
         }
         irq = irq_hint;
     } else {
-        irq = xic->irq_alloc_block(ics->xics, 1, 1, lsi);
+        irq = xic->irq_alloc_block(XICS_FABRIC(spapr), 1, 1, lsi);
         if (irq < 0) {
             error_setg(errp, "can't allocate IRQ: no IRQ left");
             return -1;
@@ -276,15 +274,11 @@ int spapr_ics_alloc(ICSState *ics, int irq_hint, bool lsi, Error **errp)
  * Allocate block of consecutive IRQs, and return the number of the first IRQ in
  * the block. If align==true, aligns the first IRQ number to num.
  */
-int spapr_ics_alloc_block(ICSState *ics, int num, bool lsi,
+int spapr_irq_alloc_block(sPAPRMachineState *spapr, int num, bool lsi,
                           bool align, Error **errp)
 {
     int first = -1;
-    XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(ics->xics);
-
-    if (!ics) {
-        return -1;
-    }
+    XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(spapr);
 
     /*
      * MSIMesage::data is used for storing VIRQ so
@@ -296,9 +290,9 @@ int spapr_ics_alloc_block(ICSState *ics, int num, bool lsi,
     if (align) {
         assert((num == 1) || (num == 2) || (num == 4) ||
                (num == 8) || (num == 16) || (num == 32));
-        first = xic->irq_alloc_block(ics->xics, num, num, lsi);
+        first = xic->irq_alloc_block(XICS_FABRIC(spapr), num, num, lsi);
     } else {
-        first = xic->irq_alloc_block(ics->xics, num, 1, lsi);
+        first = xic->irq_alloc_block(XICS_FABRIC(spapr), num, 1, lsi);
     }
     if (first < 0) {
         error_setg(errp, "can't find a free %d-IRQ block", num);
@@ -310,11 +304,11 @@ int spapr_ics_alloc_block(ICSState *ics, int num, bool lsi,
     return first;
 }
 
-void spapr_ics_free(ICSState *ics, int irq, int num)
+void spapr_irq_free(sPAPRMachineState *spapr, int irq, int num)
 {
-    XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(ics->xics);
+    XICSFabricClass *xic = XICS_FABRIC_GET_CLASS(spapr);
 
-    xic->irq_free_block(ics->xics, irq, num);
+    xic->irq_free_block(XICS_FABRIC(spapr), irq, num);
 }
 
 void spapr_dt_xics(int nr_servers, void *fdt, uint32_t phandle)
hw/ppc/spapr.c +3 -2
index 0cf1e02fa2b7..dcde3f4da659 100644
@@ -3610,7 +3610,8 @@ static void ics_set_irq_type(ICSState *ics, int srcno, bool lsi)
  * and then the MSIs. This allows us to keep the LSI IRQ numbers in a
  * well known range which is useful for PHB hotplug.
  */
-static int spapr_irq_alloc_block(XICSFabric *xi, int count, int align, bool lsi)
+static int spapr_irq_alloc_block_xi(XICSFabric *xi, int count, int align,
+                                    bool lsi)
 {
     sPAPRMachineState *spapr = SPAPR_MACHINE(xi);
     sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
@@ -3776,7 +3777,7 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
     xic->ics_resend = spapr_ics_resend;
     xic->icp_get = spapr_icp_get;
     xic->irq_test = spapr_irq_test;
-    xic->irq_alloc_block = spapr_irq_alloc_block;
+    xic->irq_alloc_block = spapr_irq_alloc_block_xi;
     xic->irq_free_block = spapr_irq_free_block;
     xic->irq_is_lsi = spapr_irq_is_lsi;
 
hw/ppc/spapr_events.c +2 -2
index e377fc7ddea2..cead596f3e7a 100644
@@ -718,7 +718,7 @@ void spapr_events_init(sPAPRMachineState *spapr)
     spapr->event_sources = spapr_event_sources_new();
 
     spapr_event_sources_register(spapr->event_sources, EVENT_CLASS_EPOW,
-                                 spapr_ics_alloc(spapr->ics, 0, false,
+                                 spapr_irq_alloc(spapr, 0, false,
                                                   &error_fatal));
 
     /* NOTE: if machine supports modern/dedicated hotplug event source,
@@ -731,7 +731,7 @@ void spapr_events_init(sPAPRMachineState *spapr)
      */
     if (spapr->use_hotplug_event_source) {
         spapr_event_sources_register(spapr->event_sources, EVENT_CLASS_HOT_PLUG,
-                                     spapr_ics_alloc(spapr->ics, 0, false,
+                                     spapr_irq_alloc(spapr, 0, false,
                                                       &error_fatal));
     }
 
hw/ppc/spapr_pci.c +4 -4
index 5a3122a9f9f9..e0ef77a480e5 100644
@@ -314,7 +314,7 @@ static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
             return;
         }
 
-        spapr_ics_free(spapr->ics, msi->first_irq, msi->num);
+        spapr_irq_free(spapr, msi->first_irq, msi->num);
         if (msi_present(pdev)) {
             spapr_msi_setmsg(pdev, 0, false, 0, 0);
         }
@@ -352,7 +352,7 @@ static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
     }
 
     /* Allocate MSIs */
-    irq = spapr_ics_alloc_block(spapr->ics, req_num, false,
+    irq = spapr_irq_alloc_block(spapr, req_num, false,
                            ret_intr_type == RTAS_TYPE_MSI, &err);
     if (err) {
         error_reportf_err(err, "Can't allocate MSIs for device %x: ",
@@ -363,7 +363,7 @@ static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
 
     /* Release previous MSIs */
     if (msi) {
-        spapr_ics_free(spapr->ics, msi->first_irq, msi->num);
+        spapr_irq_free(spapr, msi->first_irq, msi->num);
         g_hash_table_remove(phb->msi, &config_addr);
     }
 
@@ -1675,7 +1675,7 @@ static void spapr_phb_realize(DeviceState *dev, Error **errp)
         uint32_t irq;
         Error *local_err = NULL;
 
-        irq = spapr_ics_alloc_block(spapr->ics, 1, true, false, &local_err);
+        irq = spapr_irq_alloc_block(spapr, 1, true, false, &local_err);
         if (local_err) {
             error_propagate(errp, local_err);
             error_prepend(errp, "can't allocate LSIs: ");
hw/ppc/spapr_vio.c +1 -1
index ea3bc8bd9e21..bb7ed2c537b0 100644
@@ -454,7 +454,7 @@ static void spapr_vio_busdev_realize(DeviceState *qdev, Error **errp)
         dev->qdev.id = id;
     }
 
-    dev->irq = spapr_ics_alloc(spapr->ics, dev->irq, false, &local_err);
+    dev->irq = spapr_irq_alloc(spapr, dev->irq, false, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
         return;
include/hw/ppc/xics.h +7 -6
index 056cf37bc68f..dd3e2eacedb2 100644
@@ -184,10 +184,13 @@ typedef struct XICSFabricClass {
 
 #define XICS_IRQS_SPAPR               1024
 
-int spapr_ics_alloc(ICSState *ics, int irq_hint, bool lsi, Error **errp);
-int spapr_ics_alloc_block(ICSState *ics, int num, bool lsi, bool align,
-                           Error **errp);
-void spapr_ics_free(ICSState *ics, int irq, int num);
+typedef struct sPAPRMachineState sPAPRMachineState;
+
+int spapr_irq_alloc(sPAPRMachineState *spapr, int irq_hint, bool lsi,
+                    Error **errp);
+int spapr_irq_alloc_block(sPAPRMachineState *spapr, int num, bool lsi,
+                          bool align, Error **errp);
+void spapr_irq_free(sPAPRMachineState *spapr, int irq, int num);
 void spapr_dt_xics(int nr_servers, void *fdt, uint32_t phandle);
 
 qemu_irq xics_get_qirq(XICSFabric *xi, int irq);
@@ -210,8 +213,6 @@ bool ics_is_lsi(ICSState *ics, int srno);
 void ics_resend(ICSState *ics);
 void icp_resend(ICPState *ss);
 
-typedef struct sPAPRMachineState sPAPRMachineState;
-
 int xics_kvm_init(sPAPRMachineState *spapr, Error **errp);
 void xics_spapr_init(sPAPRMachineState *spapr);
 
Signature · 1 line
2.13.6
#13 lore raw
In reply to Cédric Le Goater

David,

Quoted 19 lines · Cédric Le Goater

Hello,

Currently, the ICSState 'ics' object of the sPAPR machine acts as the global interrupt source handler and also as the IRQ number allocator for the machine. Some IRQ numbers are allocated very early in the machine initialization sequence to populate the device tree, and this is a problem to introduce the new POWER XIVE interrupt model, as it needs to share the IRQ numbers with the older model.

To prepare ground for XIVE, here is a proposal adding a set of new XICSFabric operations to let the machine handle directly the IRQ number allocation and to decorrelate the allocation from the interrupt source object :

    bool (*irq_test)(XICSFabric *xi, int irq);
    int (*irq_alloc_block)(XICSFabric *xi, int count, int align);
    void (*irq_free_block)(XICSFabric *xi, int irq, int num);
    bool (*irq_is_lsi)(XICSFabric *xi, int irq);
Quoted 6 lines

In these prototypes, the 'irq' parameter refers to a number in the global IRQ number space.

On the sPAPR platform, these operations are simply backed by a bitmap and to handle migration compatibility, we introduce a machine class flag 'has_irq_bitmap', which is set to false for older machines.

Talking with Greg, I have found a better solution to handle migration compatibility which is to do like the phb_placement. That is to use two sets of XICS IRQ operations : one for the latest pseries machine using the bitmap and another one for the older pseries machine using the ICSIRQState. It impacts mostly patch 5.

This solution is cleaner and I will send a v3 in my morning unless you start reviewing and have some obvious comments in the v2 series.

Thanks,

C.

Quoted 60 lines

To completely remove the use of the ICSState object (required to introduce XIVE), we also need to change how the nature of an interrupt, MSI or LSI, is stored. Today, this is done using the flag attribute of the ICSIRQState array. We change that by splitting the IRQ number space of the machine in two: first the LSIs and then the MSIs. This has the benefit to keep the LSI IRQ numbers in a well known range which is useful for PHB hotplug.

The git repo for this pachset can be found here along with the latest XIVE model:

https://github.com/legoater/qemu/commits/xive

Thanks,

C.

Tests :

- make check on each patch
- migration :

     qemu-2.12 (pseries-2.12) <->  qemu-2.12 (pseries-2.12)
     qemu-2.12 (pseries-2.10) <->  qemu-2.12 (pseries-2.10)
     qemu-2.10 (pseries-2.10) <->  qemu-2.12 (pseries-2.10)

Changes since v1 :

- reorganised patchset to introduce the XICSFabric operations before the major changes: bitmap and IRQ number space split
- introduced a reference bitmap to save some state in migration

Cédric Le Goater (12):
spapr: add pseries 2.12 machine type
ppc/xics: remove useless if condition
spapr: introduce new XICSFabric operations for an IRQ allocator
spapr: move current IRQ allocation under the machine
spapr: introduce an IRQ allocator using a bitmap
spapr: store a reference IRQ bitmap
spapr: introduce an 'irq_base' number
spapr: remove the use of ics_valid_irq()
spapr: introduce a XICSFabric is_lsi() operation
spapr: split the IRQ number space for LSI interrupts
sparp: merge ics_set_irq_type() in irq_alloc_block() operation
spapr: use sPAPRMachineState in spapr_ics_ prototypes

 hw/intc/trace-events   |   2 -
 hw/intc/xics.c         |  37 ++++++-----
 hw/intc/xics_kvm.c     |   4 +-
 hw/intc/xics_spapr.c   |  76 ++++-----------------
 hw/ppc/pnv.c           |  34 ++++++++++
 hw/ppc/pnv_psi.c       |   4 --
 hw/ppc/spapr.c         | 174 ++++++++++++++++++++++++++++++++++++++++++++++++-
 hw/ppc/spapr_events.c  |   4 +-
 hw/ppc/spapr_pci.c     |   8 +--
 hw/ppc/spapr_vio.c     |   2 +-
 hw/ppc/trace-events    |   2 +
 include/hw/ppc/spapr.h |   6 ++
 include/hw/ppc/xics.h  |  20 ++++--
 13 files changed, 267 insertions(+), 106 deletions(-)