Skip to contentSkip to conversation

Introduce SPAPR_IRQ_NR_IPIS and fix max-cpus

PATCH Superseded Harsh Prateek Bora 3 participants 11 messages Nov 23, 2023 – Jan 3, 2024

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Introduce SPAPR_IRQ_NR_IPIS and fix max-cpus

On spapr, the max number of CPU IPIs are 4096 which is accounted during spapr_irq_init but currently existing macro SPAPR_XIRQ_BASE is being used to refer to that. Introducing SPAPR_IRQ_NR_IPIS to refer to the range of CPU IPIS which is being further used to initialize mc->max_cpus during spapr_machine_class_init().

v3:
- addressed further review comments from Cedric
v2:
- addressed review comments from Cedric

Harsh Prateek Bora (2):
ppc/spapr: Introduce SPAPR_IRQ_NR_IPIS to refer IRQ range for CPU
IPIs.
ppc/spapr: Initialize max_cpus limit to SPAPR_IRQ_NR_IPIS.

 include/hw/ppc/spapr_irq.h | 14 +++++++++++++-
 hw/ppc/spapr.c             |  9 +++------
 hw/ppc/spapr_irq.c         |  6 ++++--
 3 files changed, 20 insertions(+), 9 deletions(-)
Signature · 1 line
2.39.3
#8 lore raw

PATCH v3 2/2 ppc/spapr: Initialize max_cpus limit to SPAPR_IRQ_NR_IPIS.

Initialize the machine specific max_cpus limit as per the maximum range of CPU IPIs available. Keeping between 4096 to 8192 will throw IRQ not free error due to XIVE/XICS limitation and keeping beyond 8192 will hit assert in tcg_region_init or spapr_xive_claim_irq.

Logs:

Without patch fix:

[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=4097
qemu-system-ppc64: IRQ 4096 is not free
[root@host build]#

On LPAR:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=8193 **
ERROR:../tcg/region.c:774:tcg_region_init: assertion failed:
(region_size >= 2 * page_size)
Bail out! ERROR:../tcg/region.c:774:tcg_region_init: assertion failed:
(region_size >= 2 * page_size)
Aborted (core dumped)
[root@host build]#

On x86:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=8193
qemu-system-ppc64: ../hw/intc/spapr_xive.c:596: spapr_xive_claim_irq:
Assertion `lisn < xive->nr_irqs' failed.
Aborted (core dumped)
[root@host build]#

With patch fix:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=4097
qemu-system-ppc64: Invalid SMP CPUs 4097. The max CPUs supported by machine 'pseries-8.2' is 4096
[root@host build]#

---
 hw/ppc/spapr.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +3 -6
index df09aa9d6a..222d926f46 100644
@@ -4647,13 +4647,10 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
     mc->block_default_type = IF_SCSI;
 
     /*
-     * Setting max_cpus to INT32_MAX. Both KVM and TCG max_cpus values
-     * should be limited by the host capability instead of hardcoded.
-     * max_cpus for KVM guests will be checked in kvm_init(), and TCG
-     * guests are welcome to have as many CPUs as the host are capable
-     * of emulate.
+     * While KVM determines max cpus in kvm_init() using kvm_max_vcpus(),
+     * In TCG the limit is restricted by the range of CPU IPIs available.
      */
-    mc->max_cpus = INT32_MAX;
+    mc->max_cpus = SPAPR_IRQ_NR_IPIS;
 
     mc->no_parallel = 1;
     mc->default_boot_order = "";
Signature · 1 line
2.39.3
#1 lore raw

PATCH v3 1/2 ppc/spapr: Introduce SPAPR_IRQ_NR_IPIS to refer IRQ range for CPU IPIs.

In reply to Harsh Prateek Bora

spapr_irq_init currently uses existing macro SPAPR_XIRQ_BASE to refer to the range of CPU IPIs during initialization of nr-irqs property. It is more appropriate to have its own define which can be further reused as appropriate for correct interpretation.

---
 include/hw/ppc/spapr_irq.h | 14 +++++++++++++-
 hw/ppc/spapr_irq.c         |  6 ++++--
 2 files changed, 17 insertions(+), 3 deletions(-)
include/hw/ppc/spapr_irq.h +13 -1
index c22a72c9e2..4fd2d5853d 100644
@@ -14,9 +14,21 @@
 #include "qom/object.h"
 
 /*
- * IRQ range offsets per device type
+ * The XIVE IRQ backend uses the same layout as the XICS backend but
+ * covers the full range of the IRQ number space. The IRQ numbers for
+ * the CPU IPIs are allocated at the bottom of this space, below 4K,
+ * to preserve compatibility with XICS which does not use that range.
+ */
+
+/*
+ * CPU IPI range (XIVE only)
  */
 #define SPAPR_IRQ_IPI        0x0
+#define SPAPR_IRQ_NR_IPIS    0x1000
+
+/*
+ * IRQ range offsets per device type
+ */
 
 #define SPAPR_XIRQ_BASE      XICS_IRQ_BASE /* 0x1000 */
 #define SPAPR_IRQ_EPOW       (SPAPR_XIRQ_BASE + 0x0000)
hw/ppc/spapr_irq.c +4 -2
index a0d1e1298e..97b2fc42ab 100644
@@ -23,6 +23,8 @@
 
 #include "trace.h"
 
+QEMU_BUILD_BUG_ON(SPAPR_IRQ_NR_IPIS > SPAPR_XIRQ_BASE);
+
 static const TypeInfo spapr_intc_info = {
     .name = TYPE_SPAPR_INTC,
     .parent = TYPE_INTERFACE,
@@ -329,7 +331,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
         int i;
 
         dev = qdev_new(TYPE_SPAPR_XIVE);
-        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_XIRQ_BASE);
+        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_IRQ_NR_IPIS);
         /*
          * 8 XIVE END structures per CPU. One for each available
          * priority
@@ -356,7 +358,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
     }
 
     spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
-                                      smc->nr_xirqs + SPAPR_XIRQ_BASE);
+                                      smc->nr_xirqs + SPAPR_IRQ_NR_IPIS);
 
     /*
      * Mostly we don't actually need this until reset, except that not
Signature · 1 line
2.39.3
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] ppc/spapr: Introduce SPAPR_IRQ_NR_IPIS to refer IRQ range for CPU IPIs.

Quoted 7 lines · Harsh Prateek Bora

spapr_irq_init currently uses existing macro SPAPR_XIRQ_BASE to refer to the range of CPU IPIs during initialization of nr-irqs property. It is more appropriate to have its own define which can be further reused as appropriate for correct interpretation.

One comment below

Quoted 37 lines
---
  include/hw/ppc/spapr_irq.h | 14 +++++++++++++-
  hw/ppc/spapr_irq.c         |  6 ++++--
  2 files changed, 17 insertions(+), 3 deletions(-)
include/hw/ppc/spapr_irq.h +13 -1
index c22a72c9e2..4fd2d5853d 100644
@@ -14,9 +14,21 @@
  #include "qom/object.h"
 
  /*
- * IRQ range offsets per device type
+ * The XIVE IRQ backend uses the same layout as the XICS backend but
+ * covers the full range of the IRQ number space. The IRQ numbers for
+ * the CPU IPIs are allocated at the bottom of this space, below 4K,
+ * to preserve compatibility with XICS which does not use that range.
+ */
+
+/*
+ * CPU IPI range (XIVE only)
   */
  #define SPAPR_IRQ_IPI        0x0
+#define SPAPR_IRQ_NR_IPIS    0x1000
+
+/*
+ * IRQ range offsets per device type
+ */
 
  #define SPAPR_XIRQ_BASE      XICS_IRQ_BASE /* 0x1000 */
  #define SPAPR_IRQ_EPOW       (SPAPR_XIRQ_BASE + 0x0000)
hw/ppc/spapr_irq.c +0 -0
index a0d1e1298e..97b2fc42ab 100644

hw/ppc/spapr_irq.c line 27

Quoted 5 lines
hw/ppc/spapr_irq.c +2 -0
@@ -23,6 +23,8 @@
 
  #include "trace.h"
 
+QEMU_BUILD_BUG_ON(SPAPR_IRQ_NR_IPIS > SPAPR_XIRQ_BASE);
+

I would have put the check in include/hw/ppc/spapr_irq.h but since SPAPR_XIRQ_BASE is only used in hw/ppc/spapr_irq.c which is always compiled, this is fine. You might want to change that in case a respin is asked for.

Thanks,

C.

Quoted 21 lines
hw/ppc/spapr_irq.c +2 -2
  static const TypeInfo spapr_intc_info = {
      .name = TYPE_SPAPR_INTC,
      .parent = TYPE_INTERFACE,
@@ -329,7 +331,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
          int i;
 
          dev = qdev_new(TYPE_SPAPR_XIVE);
-        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_XIRQ_BASE);
+        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_IRQ_NR_IPIS);
          /*
           * 8 XIVE END structures per CPU. One for each available
           * priority
@@ -356,7 +358,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
      }
 
      spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
-                                      smc->nr_xirqs + SPAPR_XIRQ_BASE);
+                                      smc->nr_xirqs + SPAPR_IRQ_NR_IPIS);
 
      /*
       * Mostly we don't actually need this until reset, except that not
Reviewed-by on v3 2/2
#9 lore raw

Re: [PATCH v3 2/2] ppc/spapr: Initialize max_cpus limit to SPAPR_IRQ_NR_IPIS.

In reply to Harsh Prateek Bora
Quoted 30 lines · Harsh Prateek Bora

Initialize the machine specific max_cpus limit as per the maximum range of CPU IPIs available. Keeping between 4096 to 8192 will throw IRQ not free error due to XIVE/XICS limitation and keeping beyond 8192 will hit assert in tcg_region_init or spapr_xive_claim_irq.

Logs:

Without patch fix:

[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=4097
qemu-system-ppc64: IRQ 4096 is not free
[root@host build]#

On LPAR:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=8193 **
ERROR:../tcg/region.c:774:tcg_region_init: assertion failed:
(region_size >= 2 * page_size)
Bail out! ERROR:../tcg/region.c:774:tcg_region_init: assertion failed:
(region_size >= 2 * page_size)
Aborted (core dumped)
[root@host build]#

On x86:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=8193
qemu-system-ppc64: ../hw/intc/spapr_xive.c:596: spapr_xive_claim_irq:
Assertion `lisn < xive->nr_irqs' failed.
Aborted (core dumped)
[root@host build]#

Quoted 7 lines

With patch fix:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=4097
qemu-system-ppc64: Invalid SMP CPUs 4097. The max CPUs supported by machine 'pseries-8.2' is 4096
[root@host build]#

Thanks,

C.

Quoted 25 lines
---
  hw/ppc/spapr.c | 9 +++------
  1 file changed, 3 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +3 -6
index df09aa9d6a..222d926f46 100644
@@ -4647,13 +4647,10 @@ static void spapr_machine_class_init(ObjectClass *oc, void *data)
      mc->block_default_type = IF_SCSI;
 
      /*
-     * Setting max_cpus to INT32_MAX. Both KVM and TCG max_cpus values
-     * should be limited by the host capability instead of hardcoded.
-     * max_cpus for KVM guests will be checked in kvm_init(), and TCG
-     * guests are welcome to have as many CPUs as the host are capable
-     * of emulate.
+     * While KVM determines max cpus in kvm_init() using kvm_max_vcpus(),
+     * In TCG the limit is restricted by the range of CPU IPIs available.
       */
-    mc->max_cpus = INT32_MAX;
+    mc->max_cpus = SPAPR_IRQ_NR_IPIS;
 
      mc->no_parallel = 1;
      mc->default_boot_order = "";
#3 lore raw

Re: [PATCH v3 1/2] ppc/spapr: Introduce SPAPR_IRQ_NR_IPIS to refer IRQ range for CPU IPIs.

In reply to Cédric Le Goater
Quoted 6 lines · Cédric Le Goater
Quoted 5 lines · Harsh Prateek Bora

spapr_irq_init currently uses existing macro SPAPR_XIRQ_BASE to refer to the range of CPU IPIs during initialization of nr-irqs property. It is more appropriate to have its own define which can be further reused as appropriate for correct interpretation.

Quoted 7 lines
Quoted 2 lines

One comment below

Thanks, responding below ..

Quoted 35 lines
Quoted 35 lines

---
  include/hw/ppc/spapr_irq.h | 14 +++++++++++++-
  hw/ppc/spapr_irq.c         |  6 ++++--

  2 files changed, 17 insertions(+), 3 deletions(-)
include/hw/ppc/spapr_irq.h +13 -1
index c22a72c9e2..4fd2d5853d 100644
@@ -14,9 +14,21 @@
   #include "qom/object.h"
   /*
- * IRQ range offsets per device type
+ * The XIVE IRQ backend uses the same layout as the XICS backend but
+ * covers the full range of the IRQ number space. The IRQ numbers for
+ * the CPU IPIs are allocated at the bottom of this space, below 4K,
+ * to preserve compatibility with XICS which does not use that range.
+ */
+
+/*
+ * CPU IPI range (XIVE only)
    */
   #define SPAPR_IRQ_IPI        0x0
+#define SPAPR_IRQ_NR_IPIS    0x1000
+
+/*
+ * IRQ range offsets per device type
+ */
   #define SPAPR_XIRQ_BASE      XICS_IRQ_BASE /* 0x1000 */
   #define SPAPR_IRQ_EPOW       (SPAPR_XIRQ_BASE + 0x0000)
hw/ppc/spapr_irq.c +0 -0
index a0d1e1298e..97b2fc42ab 100644
@@ -23,6 +23,8 @@
Quoted 9 lines

hw/ppc/spapr_irq.c line 27

Quoted 3 lines
hw/ppc/spapr_irq.c +2 -0
   #include "trace.h"
+QEMU_BUILD_BUG_ON(SPAPR_IRQ_NR_IPIS > SPAPR_XIRQ_BASE);
+

I would have put the check in include/hw/ppc/spapr_irq.h but since SPAPR_XIRQ_BASE is only used in hw/ppc/spapr_irq.c which is always compiled, this is fine. You might want to change that in case a respin is asked for.

I had initially tried keeping it in spapr_irq.h , but that would give a build break for XICS_IRQ_BASE not defined since that gets defined in spapr_xics.h and is included later in some files, however, the QEMU_BUILD_BUG_ON expects it to be defined before it reaches here.

regards,
Harsh

Quoted 29 lines

Thanks,

C.

Quoted 23 lines
hw/ppc/spapr_irq.c +1 -1
   static const TypeInfo spapr_intc_info = {
       .name = TYPE_SPAPR_INTC,
       .parent = TYPE_INTERFACE,
@@ -329,7 +331,7 @@ void spapr_irq_init(SpaprMachineState *spapr, 
 Error **errp)
           int i;
           dev = qdev_new(TYPE_SPAPR_XIVE);
-        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + 
 SPAPR_XIRQ_BASE);
+        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + 
 SPAPR_IRQ_NR_IPIS);
           /*

           * 8 XIVE END structures per CPU. One for each available
           * priority

hw/ppc/spapr_irq.c +1 -1
@@ -356,7 +358,7 @@ void spapr_irq_init(SpaprMachineState *spapr, 
 Error **errp)
       }
       spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
-                                      smc->nr_xirqs + SPAPR_XIRQ_BASE);
+                                      smc->nr_xirqs + 
 SPAPR_IRQ_NR_IPIS);
       /*
        * Mostly we don't actually need this until reset, except that not
#4 lore raw

Re: [PATCH v3 1/2] ppc/spapr: Introduce SPAPR_IRQ_NR_IPIS to refer IRQ range for CPU IPIs.

Quoted 64 lines · Harsh Prateek Bora
Quoted 6 lines · Cédric Le Goater
Quoted 5 lines · Harsh Prateek Bora

spapr_irq_init currently uses existing macro SPAPR_XIRQ_BASE to refer to the range of CPU IPIs during initialization of nr-irqs property. It is more appropriate to have its own define which can be further reused as appropriate for correct interpretation.

Quoted 7 lines
Quoted 2 lines

One comment below

Thanks, responding below ..

Quoted 35 lines
Quoted 35 lines

---
  include/hw/ppc/spapr_irq.h | 14 +++++++++++++-
  hw/ppc/spapr_irq.c         |  6 ++++--

  2 files changed, 17 insertions(+), 3 deletions(-)
include/hw/ppc/spapr_irq.h +13 -1
index c22a72c9e2..4fd2d5853d 100644
@@ -14,9 +14,21 @@
   #include "qom/object.h"
   /*
- * IRQ range offsets per device type
+ * The XIVE IRQ backend uses the same layout as the XICS backend but
+ * covers the full range of the IRQ number space. The IRQ numbers for
+ * the CPU IPIs are allocated at the bottom of this space, below 4K,
+ * to preserve compatibility with XICS which does not use that range.
+ */
+
+/*
+ * CPU IPI range (XIVE only)
    */
   #define SPAPR_IRQ_IPI        0x0
+#define SPAPR_IRQ_NR_IPIS    0x1000
+
+/*
+ * IRQ range offsets per device type
+ */
   #define SPAPR_XIRQ_BASE      XICS_IRQ_BASE /* 0x1000 */
   #define SPAPR_IRQ_EPOW       (SPAPR_XIRQ_BASE + 0x0000)
hw/ppc/spapr_irq.c +0 -0
index a0d1e1298e..97b2fc42ab 100644
@@ -23,6 +23,8 @@
Quoted 9 lines

hw/ppc/spapr_irq.c line 27

Quoted 3 lines
hw/ppc/spapr_irq.c +2 -0
   #include "trace.h"
+QEMU_BUILD_BUG_ON(SPAPR_IRQ_NR_IPIS > SPAPR_XIRQ_BASE);
+

I would have put the check in include/hw/ppc/spapr_irq.h but since SPAPR_XIRQ_BASE is only used in hw/ppc/spapr_irq.c which is always compiled, this is fine. You might want to change that in case a respin is asked for.

Quoted 1 line

I had initially tried keeping it in spapr_irq.h , but that would give a build break for XICS_IRQ_BASE not defined since that gets defined in spapr_xics.h and is included later in some files, however, the QEMU_BUILD_BUG_ON expects it to be defined before it reaches here.

ah. good catch. this went unnoticed and is a bit ugly. We should fix in some ways. May with a define SPAPR_XIRQ_BASE to 0x1000 simply ?

Also, we could probably define the ICS offset to SPAPR_XIRQ_BASE directly under spapr_irq_init() and get rid of ics_instance_init(). The HW IRQ Number offset in the PNV ICS instances is assigned dynamically by the OS (see pnv_phb3). So it should befine to do the same for spapr. In which case we can get rid of XICS_IRQ_BASE.

Thanks,

C.

Quoted 28 lines

regards,
Harsh

Quoted 24 lines

Thanks,

C.

Quoted 18 lines
hw/ppc/spapr_irq.c +2 -2
   static const TypeInfo spapr_intc_info = {
       .name = TYPE_SPAPR_INTC,
       .parent = TYPE_INTERFACE,
@@ -329,7 +331,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
           int i;
           dev = qdev_new(TYPE_SPAPR_XIVE);
-        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_XIRQ_BASE);
+        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_IRQ_NR_IPIS);
           /*
            * 8 XIVE END structures per CPU. One for each available
            * priority
@@ -356,7 +358,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
       }
       spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
-                                      smc->nr_xirqs + SPAPR_XIRQ_BASE);
+                                      smc->nr_xirqs + SPAPR_IRQ_NR_IPIS);
       /*
        * Mostly we don't actually need this until reset, except that not
#5 lore raw

Re: [PATCH v3 1/2] ppc/spapr: Introduce SPAPR_IRQ_NR_IPIS to refer IRQ range for CPU IPIs.

Quoted 65 lines · Cédric Le Goater
Quoted 64 lines · Harsh Prateek Bora
Quoted 6 lines · Cédric Le Goater
Quoted 5 lines · Harsh Prateek Bora

spapr_irq_init currently uses existing macro SPAPR_XIRQ_BASE to refer to the range of CPU IPIs during initialization of nr-irqs property. It is more appropriate to have its own define which can be further reused as appropriate for correct interpretation.

Quoted 7 lines
Quoted 2 lines

One comment below

Thanks, responding below ..

Quoted 44 lines
Quoted 35 lines

---
  include/hw/ppc/spapr_irq.h | 14 +++++++++++++-
  hw/ppc/spapr_irq.c         |  6 ++++--

  2 files changed, 17 insertions(+), 3 deletions(-)
include/hw/ppc/spapr_irq.h +13 -1
index c22a72c9e2..4fd2d5853d 100644
@@ -14,9 +14,21 @@
   #include "qom/object.h"
   /*
- * IRQ range offsets per device type
+ * The XIVE IRQ backend uses the same layout as the XICS backend but
+ * covers the full range of the IRQ number space. The IRQ numbers for
+ * the CPU IPIs are allocated at the bottom of this space, below 4K,
+ * to preserve compatibility with XICS which does not use that range.
+ */
+
+/*
+ * CPU IPI range (XIVE only)
    */
   #define SPAPR_IRQ_IPI        0x0
+#define SPAPR_IRQ_NR_IPIS    0x1000
+
+/*
+ * IRQ range offsets per device type
+ */
   #define SPAPR_XIRQ_BASE      XICS_IRQ_BASE /* 0x1000 */
   #define SPAPR_IRQ_EPOW       (SPAPR_XIRQ_BASE + 0x0000)
hw/ppc/spapr_irq.c +0 -0
index a0d1e1298e..97b2fc42ab 100644

hw/ppc/spapr_irq.c line 25

Quoted 3 lines
hw/ppc/spapr_irq.c +2 -0
@@ -23,6 +23,8 @@
   #include "trace.h"
+QEMU_BUILD_BUG_ON(SPAPR_IRQ_NR_IPIS > SPAPR_XIRQ_BASE);
+

I would have put the check in include/hw/ppc/spapr_irq.h but since SPAPR_XIRQ_BASE is only used in hw/ppc/spapr_irq.c which is always compiled, this is fine. You might want to change that in case a respin is asked for.

Quoted 5 lines
Quoted 1 line

I had initially tried keeping it in spapr_irq.h , but that would give a build break for XICS_IRQ_BASE not defined since that gets defined in spapr_xics.h and is included later in some files, however, the QEMU_BUILD_BUG_ON expects it to be defined before it reaches here.

ah. good catch. this went unnoticed and is a bit ugly. We should fix in some ways. May with a define SPAPR_XIRQ_BASE to 0x1000 simply ?

Hmm, I can do that if a re-spin is reqd, or can be done as a separate patch later also along with other improvements.

Quoted 6 lines

Also, we could probably define the ICS offset to SPAPR_XIRQ_BASE directly under spapr_irq_init() and get rid of ics_instance_init(). The HW IRQ Number offset in the PNV ICS instances is assigned dynamically by the OS (see pnv_phb3). So it should befine to do the same for spapr. In which case we can get rid of XICS_IRQ_BASE.

Hmm, I am not so familiar with XICS yet, so not sure if we really need to do that, but it can be done along with other improvements if needed.

regards,
Harsh

Quoted 41 lines

Thanks,

C.

Quoted 34 lines

regards,
Harsh

Quoted 30 lines

Thanks,

C.

Quoted 24 lines
hw/ppc/spapr_irq.c +1 -1
   static const TypeInfo spapr_intc_info = {
       .name = TYPE_SPAPR_INTC,
       .parent = TYPE_INTERFACE,
@@ -329,7 +331,7 @@ void spapr_irq_init(SpaprMachineState *spapr, 
 Error **errp)
           int i;
           dev = qdev_new(TYPE_SPAPR_XIVE);
-        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + 
 SPAPR_XIRQ_BASE);
+        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + 
 SPAPR_IRQ_NR_IPIS);
           /*

           * 8 XIVE END structures per CPU. One for each available
           * priority

hw/ppc/spapr_irq.c +1 -1
@@ -356,7 +358,7 @@ void spapr_irq_init(SpaprMachineState *spapr, 
 Error **errp)
       }
       spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
-                                      smc->nr_xirqs + 
 SPAPR_XIRQ_BASE);
+                                      smc->nr_xirqs + 
 SPAPR_IRQ_NR_IPIS);
       /*

       * Mostly we don't actually need this until reset, except that not

#6 lore raw

Re: [PATCH v3 1/2] ppc/spapr: Introduce SPAPR_IRQ_NR_IPIS to refer IRQ range for CPU IPIs.

Quoted 68 lines · Harsh Prateek Bora
Quoted 65 lines · Cédric Le Goater
Quoted 64 lines · Harsh Prateek Bora
Quoted 6 lines · Cédric Le Goater
Quoted 5 lines · Harsh Prateek Bora

spapr_irq_init currently uses existing macro SPAPR_XIRQ_BASE to refer to the range of CPU IPIs during initialization of nr-irqs property. It is more appropriate to have its own define which can be further reused as appropriate for correct interpretation.

Quoted 7 lines
Quoted 2 lines

One comment below

Thanks, responding below ..

Quoted 44 lines
Quoted 35 lines

---
  include/hw/ppc/spapr_irq.h | 14 +++++++++++++-
  hw/ppc/spapr_irq.c         |  6 ++++--

  2 files changed, 17 insertions(+), 3 deletions(-)
include/hw/ppc/spapr_irq.h +13 -1
index c22a72c9e2..4fd2d5853d 100644
@@ -14,9 +14,21 @@
   #include "qom/object.h"
   /*
- * IRQ range offsets per device type
+ * The XIVE IRQ backend uses the same layout as the XICS backend but
+ * covers the full range of the IRQ number space. The IRQ numbers for
+ * the CPU IPIs are allocated at the bottom of this space, below 4K,
+ * to preserve compatibility with XICS which does not use that range.
+ */
+
+/*
+ * CPU IPI range (XIVE only)
    */
   #define SPAPR_IRQ_IPI        0x0
+#define SPAPR_IRQ_NR_IPIS    0x1000
+
+/*
+ * IRQ range offsets per device type
+ */
   #define SPAPR_XIRQ_BASE      XICS_IRQ_BASE /* 0x1000 */
   #define SPAPR_IRQ_EPOW       (SPAPR_XIRQ_BASE + 0x0000)
hw/ppc/spapr_irq.c +0 -0
index a0d1e1298e..97b2fc42ab 100644

hw/ppc/spapr_irq.c line 25

Quoted 3 lines
hw/ppc/spapr_irq.c +2 -0
@@ -23,6 +23,8 @@
   #include "trace.h"
+QEMU_BUILD_BUG_ON(SPAPR_IRQ_NR_IPIS > SPAPR_XIRQ_BASE);
+

I would have put the check in include/hw/ppc/spapr_irq.h but since SPAPR_XIRQ_BASE is only used in hw/ppc/spapr_irq.c which is always compiled, this is fine. You might want to change that in case a respin is asked for.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

I had initially tried keeping it in spapr_irq.h , but that would give a build break for XICS_IRQ_BASE not defined since that gets defined in spapr_xics.h and is included later in some files, however, the QEMU_BUILD_BUG_ON expects it to be defined before it reaches here.

ah. good catch. this went unnoticed and is a bit ugly. We should fix in some ways. May with a define SPAPR_XIRQ_BASE to 0x1000 simply ?

Hmm, I can do that if a re-spin is reqd, or can be done as a separate patch later also along with other improvements.

yes. This is food for thoughts for further improvements.

Thanks,

C.

Quoted 49 lines
Quoted 7 lines

Also, we could probably define the ICS offset to SPAPR_XIRQ_BASE directly under spapr_irq_init() and get rid of ics_instance_init(). The HW IRQ Number offset in the PNV ICS instances is assigned dynamically by the OS (see pnv_phb3). So it should befine to do the same for spapr. In which case we can get rid of XICS_IRQ_BASE.

Hmm, I am not so familiar with XICS yet, so not sure if we really need to do that, but it can be done along with other improvements if needed.

regards,
Harsh

Quoted 35 lines

Thanks,

C.

Quoted 28 lines

regards,
Harsh

Quoted 24 lines

Thanks,

C.

Quoted 18 lines
hw/ppc/spapr_irq.c +2 -2
   static const TypeInfo spapr_intc_info = {
       .name = TYPE_SPAPR_INTC,
       .parent = TYPE_INTERFACE,
@@ -329,7 +331,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
           int i;
           dev = qdev_new(TYPE_SPAPR_XIVE);
-        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_XIRQ_BASE);
+        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_IRQ_NR_IPIS);
           /*
            * 8 XIVE END structures per CPU. One for each available
            * priority
@@ -356,7 +358,7 @@ void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
       }
       spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
-                                      smc->nr_xirqs + SPAPR_XIRQ_BASE);
+                                      smc->nr_xirqs + SPAPR_IRQ_NR_IPIS);
       /*
        * Mostly we don't actually need this until reset, except that not
#7 lore raw

Re: [PATCH v3 1/2] ppc/spapr: Introduce SPAPR_IRQ_NR_IPIS to refer IRQ range for CPU IPIs.

Quoted 151 lines · Cédric Le Goater
Quoted 74 lines · Harsh Prateek Bora
Quoted 71 lines · Cédric Le Goater
Quoted 70 lines · Harsh Prateek Bora

On 11/23/23 14:20, CI've applied these patches and verified on the latest upstream qemu. The code is working as expected.

Quoted 6 lines · Harsh Prateek Bora
Quoted 5 lines

spapr_irq_init currently uses existing macro SPAPR_XIRQ_BASE to refer to the range of CPU IPIs during initialization of nr-irqs property. It is more appropriate to have its own define which can be further reused as appropriate for correct interpretation.

Quoted 7 lines
Quoted 2 lines

One comment below

Thanks, responding below ..

Quoted 48 lines
Quoted 39 lines

---
  include/hw/ppc/spapr_irq.h | 14 +++++++++++++-
  hw/ppc/spapr_irq.c         |  6 ++++--

  2 files changed, 17 insertions(+), 3 deletions(-)
include/hw/ppc/spapr_irq.h +1 -1
index c22a72c9e2..4fd2d5853d 100644
@@ -14,9 +14,21 @@
   #include "qom/object.h"
   /*
- * IRQ range offsets per device type
+ * The XIVE IRQ backend uses the same layout as the XICS backend 

but
+ * covers the full range of the IRQ number space. The IRQ numbers for
+ * the CPU IPIs are allocated at the bottom of this space, below 4K,
+ * to preserve compatibility with XICS which does not use that range.
+ */

include/hw/ppc/spapr_irq.h +3 -0
+
+/*
+ * CPU IPI range (XIVE only)

   */
  #define SPAPR_IRQ_IPI        0x0

include/hw/ppc/spapr_irq.h +5 -0
+#define SPAPR_IRQ_NR_IPIS    0x1000
+
+/*
+ * IRQ range offsets per device type
+ */

  #define SPAPR_XIRQ_BASE      XICS_IRQ_BASE /* 0x1000 */ #define SPAPR_IRQ_EPOW       (SPAPR_XIRQ_BASE + 0x0000)

hw/ppc/spapr_irq.c +0 -0
index a0d1e1298e..97b2fc42ab 100644

hw/ppc/spapr_irq.c line 25

Quoted 3 lines
hw/ppc/spapr_irq.c +2 -0
@@ -23,6 +23,8 @@
   #include "trace.h"
+QEMU_BUILD_BUG_ON(SPAPR_IRQ_NR_IPIS > SPAPR_XIRQ_BASE);
+

I would have put the check in include/hw/ppc/spapr_irq.h but since SPAPR_XIRQ_BASE is only used in hw/ppc/spapr_irq.c which is always compiled, this is fine. You might want to change that in case a respin is asked for.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

I had initially tried keeping it in spapr_irq.h , but that would give a build break for XICS_IRQ_BASE not defined since that gets defined in spapr_xics.h and is included later in some files, however, the QEMU_BUILD_BUG_ON expects it to be defined before it reaches here.

ah. good catch. this went unnoticed and is a bit ugly. We should fix in some ways. May with a define SPAPR_XIRQ_BASE to 0x1000 simply ?

Hmm, I can do that if a re-spin is reqd, or can be done as a separate patch later also along with other improvements.

yes. This is food for thoughts for further improvements.

Thanks,

C.

Quoted 8 lines
Quoted 7 lines

Also, we could probably define the ICS offset to SPAPR_XIRQ_BASE directly under spapr_irq_init() and get rid of ics_instance_init(). The HW IRQ Number offset in the PNV ICS instances is assigned dynamically by the OS (see pnv_phb3). So it should befine to do the same for spapr. In which case we can get rid of XICS_IRQ_BASE.

Quoted 47 lines

Hmm, I am not so familiar with XICS yet, so not sure if we really need to do that, but it can be done along with other improvements if needed.

regards,
Harsh

Quoted 41 lines

Thanks,

C.

Quoted 34 lines

regards,
Harsh

Quoted 30 lines

Thanks,

C.

Quoted 24 lines
hw/ppc/spapr_irq.c +1 -1
   static const TypeInfo spapr_intc_info = {
       .name = TYPE_SPAPR_INTC,
       .parent = TYPE_INTERFACE,
@@ -329,7 +331,7 @@ void spapr_irq_init(SpaprMachineState *spapr, 
 Error **errp)
           int i;
           dev = qdev_new(TYPE_SPAPR_XIVE);
-        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + 
 SPAPR_XIRQ_BASE);
+        qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + 
 SPAPR_IRQ_NR_IPIS);
           /*

           * 8 XIVE END structures per CPU. One for each available
           * priority

hw/ppc/spapr_irq.c +1 -1
@@ -356,7 +358,7 @@ void spapr_irq_init(SpaprMachineState *spapr, 
 Error **errp)
       }
       spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
-                                      smc->nr_xirqs + 
 SPAPR_XIRQ_BASE);
+                                      smc->nr_xirqs + 
 SPAPR_IRQ_NR_IPIS);
       /*

       * Mostly we don't actually need this until reset, except that not

I've applied these patches and verified on the latest upstream qemu. The code is working as expected.

#10 lore raw

Re: [PATCH v3 2/2] ppc/spapr: Initialize max_cpus limit to SPAPR_IRQ_NR_IPIS.

In reply to Cédric Le Goater
Quoted 76 lines · Cédric Le Goater
Quoted 30 lines · Harsh Prateek Bora

Initialize the machine specific max_cpus limit as per the maximum range of CPU IPIs available. Keeping between 4096 to 8192 will throw IRQ not free error due to XIVE/XICS limitation and keeping beyond 8192 will hit assert in tcg_region_init or spapr_xive_claim_irq.

Logs:

Without patch fix:

[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=4097
qemu-system-ppc64: IRQ 4096 is not free
[root@host build]#

On LPAR:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=8193 **
ERROR:../tcg/region.c:774:tcg_region_init: assertion failed:
(region_size >= 2 * page_size)
Bail out! ERROR:../tcg/region.c:774:tcg_region_init: assertion failed:
(region_size >= 2 * page_size)
Aborted (core dumped)
[root@host build]#

On x86:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=8193
qemu-system-ppc64: ../hw/intc/spapr_xive.c:596: spapr_xive_claim_irq:
Assertion `lisn < xive->nr_irqs' failed.
Aborted (core dumped)
[root@host build]#

Quoted 7 lines

With patch fix:
[root@host build]# qemu-system-ppc64 -accel tcg -smp 10,maxcpus=4097
qemu-system-ppc64: Invalid SMP CPUs 4097. The max CPUs supported by machine 'pseries-8.2' is 4096
[root@host build]#

Thanks,

C.

Quoted 20 lines

---
  hw/ppc/spapr.c | 9 +++------

  1 file changed, 3 insertions(+), 6 deletions(-)
hw/ppc/spapr.c +2 -5
index df09aa9d6a..222d926f46 100644
@@ -4647,13 +4647,10 @@ static void 
 spapr_machine_class_init(ObjectClass *oc, void *data)
       mc->block_default_type = IF_SCSI;
         /*
-     * Setting max_cpus to INT32_MAX. Both KVM and TCG max_cpus values
-     * should be limited by the host capability instead of hardcoded.
-     * max_cpus for KVM guests will be checked in kvm_init(), and TCG
-     * guests are welcome to have as many CPUs as the host are capable
-     * of emulate.
+     * While KVM determines max cpus in kvm_init() using 
 kvm_max_vcpus(),
+     * In TCG the limit is restricted by the range of CPU IPIs 
Quoted 6 lines

available.
       */
-    mc->max_cpus = INT32_MAX;
+    mc->max_cpus = SPAPR_IRQ_NR_IPIS;
        mc->no_parallel = 1;
      mc->default_boot_order = "";

I've applied these patches and verified on the latest upstream qemu. The code is working as expected. Tested-by: Kowshik Jois<[email protected]>