Skip to contentSkip to conversation

spapr: Guest exploitation of the XIVE interrupt controller (POWER9)

PATCH Superseded Cédric Le Goater 5 participants 128 messages Nov 23, 2017 – Dec 7, 2017 Page 2 of 2

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#99 lore raw

Re: [Qemu-devel] [PATCH 17/25] spapr: add a sPAPRXive object to the machine

In reply to David Gibson
Quoted 13 lines · David Gibson
Quoted 12 lines · Cédric Le Goater
Quoted 6 lines · David Gibson
Quoted 2 lines · Cédric Le Goater

The XIVE object is designed to be always available, so it is created unconditionally on newer machines.

There doesn't actually seem to be anything dependent on machine version here.

No. I thought that was too early in the patchset. This is handled in the last patch with a 'xive_exploitation' bool which is set to false on older machines.

Quoted 6 lines
Quoted 2 lines

But, nevertheless, the XIVE objects are always created even if not used. Something to discuss.

That'll definitely break backwards migration, since the destination won't understand the (unused but still present) xive state it receives.

no because it's not sent. the vmstate 'needed' op of the sPAPRXive object discards it :

    static bool vmstate_spapr_xive_needed(void *opaque)
    {
        sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
 
        return spapr->xive_exploitation;
    }
Quoted 1 line

So xives can only be created on new machine types.

That would be better I agree. I can probably use the 'xive_exploitation' bool to condition its creation.

Quoted 3 lines

I'm ok
(at least tentatively) with always creating them on the newer machine types, regardless of whether the guest ends up exploiting it or not.

OK.

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Depending on the configuration and
the guest capabilities, the CAS negotiation process will decide which interrupt model to use, legacy or XIVE.

The XIVE model makes use of the full range of the IRQ number space because the IRQ numbers for the CPU IPIs are allocated in the range below XICS_IRQ_BASE, which is unused by XICS.

Quoted 5 lines
Quoted 2 lines

Ok. And I take it 4096 is enough space for the XIVE IPIs for the forseeable future?

The biggest real system I am aware of as 16 sockets, 192 cores, SMT8. That's 1536 cpus. pseries has a max_cpus of 1024.

Ok, so we can go to double the current system size, but not 4x. Not sure if that seems adequate or not. Still it's a relatively minor detail.

#100 lore raw

Re: [Qemu-devel] [PATCH 17/25] spapr: add a sPAPRXive object to the machine

Quoted 24 lines · Cédric Le Goater
Quoted 13 lines · David Gibson
Quoted 12 lines · Cédric Le Goater
Quoted 6 lines · David Gibson
Quoted 2 lines · Cédric Le Goater

The XIVE object is designed to be always available, so it is created unconditionally on newer machines.

There doesn't actually seem to be anything dependent on machine version here.

No. I thought that was too early in the patchset. This is handled in the last patch with a 'xive_exploitation' bool which is set to false on older machines.

Quoted 6 lines
Quoted 2 lines

But, nevertheless, the XIVE objects are always created even if not used. Something to discuss.

That'll definitely break backwards migration, since the destination won't understand the (unused but still present) xive state it receives.

no because it's not sent. the vmstate 'needed' op of the sPAPRXive object discards it :

Quoted 6 lines
    static bool vmstate_spapr_xive_needed(void *opaque)
    {
        sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
 
        return spapr->xive_exploitation;
    }

Ah, sorry, missed that. Once we have negotiation we'll need to make sure the xive_exploitation bit is sent first, of course, but I'm pretty sure the machine state is already sent first.

Quoted 4 lines
Quoted 1 line

So xives can only be created on new machine types.

That would be better I agree. I can probably use the 'xive_exploitation' bool to condition its creation.

Hrm. I'm less sure about that - I'm not sure the lifetimes line up. But I'd like to avoid creating them on earlier machine types, even if xive_exploitation can't do the trick.

Quoted 27 lines
Quoted 4 lines

I'm ok
(at least tentatively) with always creating them on the newer machine types, regardless of whether the guest ends up exploiting it or not.

OK.

Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Depending on the configuration and
the guest capabilities, the CAS negotiation process will decide which interrupt model to use, legacy or XIVE.

The XIVE model makes use of the full range of the IRQ number space because the IRQ numbers for the CPU IPIs are allocated in the range below XICS_IRQ_BASE, which is unused by XICS.

Quoted 5 lines
Quoted 2 lines

Ok. And I take it 4096 is enough space for the XIVE IPIs for the forseeable future?

The biggest real system I am aware of as 16 sockets, 192 cores, SMT8. That's 1536 cpus. pseries has a max_cpus of 1024.

Ok, so we can go to double the current system size, but not 4x. Not sure if that seems adequate or not. Still it's a relatively minor detail.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#101 lore raw

Re: [Qemu-devel] [PATCH 17/25] spapr: add a sPAPRXive object to the machine

Quoted 36 lines · David Gibson
Quoted 24 lines · Cédric Le Goater
Quoted 13 lines · David Gibson
Quoted 12 lines · Cédric Le Goater
Quoted 6 lines · David Gibson
Quoted 2 lines · Cédric Le Goater

The XIVE object is designed to be always available, so it is created unconditionally on newer machines.

There doesn't actually seem to be anything dependent on machine version here.

No. I thought that was too early in the patchset. This is handled in the last patch with a 'xive_exploitation' bool which is set to false on older machines.

Quoted 6 lines
Quoted 2 lines

But, nevertheless, the XIVE objects are always created even if not used. Something to discuss.

That'll definitely break backwards migration, since the destination won't understand the (unused but still present) xive state it receives.

no because it's not sent. the vmstate 'needed' op of the sPAPRXive object discards it :

Quoted 6 lines
    static bool vmstate_spapr_xive_needed(void *opaque)
    {
        sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
 
        return spapr->xive_exploitation;
    }

Ah, sorry, missed that. Once we have negotiation we'll need to make sure the xive_exploitation bit is sent first, of course, but I'm pretty sure the machine state is already sent first.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

So xives can only be created on new machine types.

That would be better I agree. I can probably use the 'xive_exploitation' bool to condition its creation.

Hrm. I'm less sure about that - I'm not sure the lifetimes line up. But I'd like to avoid creating them on earlier machine types, even if xive_exploitation can't do the trick.

Yes. I agree. I think we can work something out without introducing too much complexity. The XIVE object is directly used by the machine only to set/unset IRQ numbers. Otherwise, it is always conditioned by :

    spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)

I think adding a couple of more tests on the 'xive_exploitation' bool should work out for older machines.

Thanks,

C.

#102 lore raw

Re: [Qemu-devel] [PATCH 17/25] spapr: add a sPAPRXive object to the machine

Quoted 51 lines · Cédric Le Goater
Quoted 36 lines · David Gibson
Quoted 24 lines · Cédric Le Goater
Quoted 13 lines · David Gibson
Quoted 12 lines · Cédric Le Goater
Quoted 6 lines · David Gibson
Quoted 2 lines · Cédric Le Goater

The XIVE object is designed to be always available, so it is created unconditionally on newer machines.

There doesn't actually seem to be anything dependent on machine version here.

No. I thought that was too early in the patchset. This is handled in the last patch with a 'xive_exploitation' bool which is set to false on older machines.

Quoted 6 lines
Quoted 2 lines

But, nevertheless, the XIVE objects are always created even if not used. Something to discuss.

That'll definitely break backwards migration, since the destination won't understand the (unused but still present) xive state it receives.

no because it's not sent. the vmstate 'needed' op of the sPAPRXive object discards it :

Quoted 6 lines
    static bool vmstate_spapr_xive_needed(void *opaque)
    {
        sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
 
        return spapr->xive_exploitation;
    }

Ah, sorry, missed that. Once we have negotiation we'll need to make sure the xive_exploitation bit is sent first, of course, but I'm pretty sure the machine state is already sent first.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

So xives can only be created on new machine types.

That would be better I agree. I can probably use the 'xive_exploitation' bool to condition its creation.

Hrm. I'm less sure about that - I'm not sure the lifetimes line up. But I'd like to avoid creating them on earlier machine types, even if xive_exploitation can't do the trick.

Yes. I agree. I think we can work something out without introducing too much complexity. The XIVE object is directly used by the machine only to set/unset IRQ numbers. Otherwise, it is always conditioned by :

Quoted 4 lines
    spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)

I think adding a couple of more tests on the 'xive_exploitation' bool should work out for older machines.

Ok. If not you can always add a "xive_possible" flag to the MachineClass.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#103 lore raw

Re: [Qemu-devel] [PATCH 17/25] spapr: add a sPAPRXive object to the machine

In reply to David Gibson
Quoted 8 lines

hw/ppc/spapr.c static void ppc_spapr_init(MachineState *machine) · line 2394

Quoted 5 lines
hw/ppc/spapr.c +5 -0
+    } else {
+        /* XIVE uses the full range of IRQ numbers. The CPU IPIs will
+         * use the range below XICS_IRQ_BASE, which is unused by XICS. */
+        spapr->xive = spapr_xive_create(spapr, XICS_IRQ_BASE + XICS_IRQS_SPAPR,
+                                        &error_fatal);

XICS_IRQ_BASE == 4096, and XICS_IRQS_SPAPR (which we should rename at some point) == 1024.

BTW, why XICS_IRQ_BASE == 4096 ? I could not find a reason for this offset.

Quoted 2 lines

So we have a total irq space of 5k, which is a bit odd. I'd be ok with rounding it out to 8k for newer machines if that's useful.

ok. and using a machine class value to maintain compatibility. That would be useful if we allocate more PHBs.

Quoted 3 lines

Sparse allocations in there might make life easier for getting consistent irq numbers without an "allocator" per se (because we can use different regions for VIO, PCI intx, MSI, etc. etc.).

So, do you think we should modify the IRQ allocator routines to be able to segment the IRQ number space and let devices specify the range they want to use ?

That would be useful for the PHB LSIs. The starting IRQ for the PHB could be aligned on some value depending on the PHB index, first would come the LSI interrupts and then the MSIs which are allocated later on by the guest. We would have predictable values.

Thanks,

C.

#104 lore raw

Re: [Qemu-devel] [PATCH 17/25] spapr: add a sPAPRXive object to the machine

Quoted 11 lines · Cédric Le Goater
Quoted 8 lines

hw/ppc/spapr.c static void ppc_spapr_init(MachineState *machine) · line 2394

Quoted 5 lines
hw/ppc/spapr.c +5 -0
+    } else {
+        /* XIVE uses the full range of IRQ numbers. The CPU IPIs will
+         * use the range below XICS_IRQ_BASE, which is unused by XICS. */
+        spapr->xive = spapr_xive_create(spapr, XICS_IRQ_BASE + XICS_IRQS_SPAPR,
+                                        &error_fatal);

XICS_IRQ_BASE == 4096, and XICS_IRQS_SPAPR (which we should rename at some point) == 1024.

BTW, why XICS_IRQ_BASE == 4096 ? I could not find a reason for this offset.

It's basically arbitrary. Possible I copied the value used in practice on a PowerVM system of the time, but I don't recall for sure.

Quoted 6 lines
Quoted 2 lines

So we have a total irq space of 5k, which is a bit odd. I'd be ok with rounding it out to 8k for newer machines if that's useful.

ok. and using a machine class value to maintain compatibility. That would be useful if we allocate more PHBs.

Quoted 7 lines
Quoted 3 lines

Sparse allocations in there might make life easier for getting consistent irq numbers without an "allocator" per se (because we can use different regions for VIO, PCI intx, MSI, etc. etc.).

So, do you think we should modify the IRQ allocator routines to be able to segment the IRQ number space and let devices specify the range they want to use ?

No, I'm suggesting *eliminating* the IRQ allocator routines (except for backwards compat) and having devices "just know" their irq numbers based on their own device number and the portion of the overall irq space they're supposed to live in.

PCI MSI is an exception, obviously, it will need some sort of runtime allocation.

Quoted 9 lines

That would be useful for the PHB LSIs. The starting IRQ for the PHB could be aligned on some value depending on the PHB index, first would come the LSI interrupts and then the MSIs which are allocated later on by the guest. We would have predictable values.

Thanks,

C.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#105 lore raw

PATCH 18/25 spapr: allocate IRQ numbers for the XIVE interrupt mode

In reply to Cédric Le Goater

The IRQ numbers for the IPIs are allocated at the bottom of the IRQ number space to preserve compatibility with XICS which only uses IRQ numbers above 4096.

Also make sure that the allocated IRQ numbers are kept in sync between XICS and XIVE.

---
 hw/ppc/spapr.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/ppc/spapr.c +7 -0
index 0e0107c8272c..ca4e72187f60 100644
@@ -2392,6 +2392,11 @@ static void ppc_spapr_init(MachineState *machine)
          * use the range below XICS_IRQ_BASE, which is unused by XICS. */
         spapr->xive = spapr_xive_create(spapr, XICS_IRQ_BASE + XICS_IRQS_SPAPR,
                                         &error_fatal);
+
+        /* Allocate the first IRQ numbers for the XIVE IPIs */
+        for (i = 0; i < xics_max_server_number(); ++i) {
+            spapr_xive_irq_set(spapr->xive, i, false);
+        }
     }
 
     /* Set up containers for ibm,client-architecture-support negotiated options
@@ -3631,6 +3636,7 @@ static int ics_find_free_block(ICSState *ics, int num, int alignnum)
 static void spapr_irq_set(sPAPRMachineState *spapr, int irq, bool lsi)
 {
     ics_set_irq_type(spapr->ics, irq - spapr->ics->offset, lsi);
+    spapr_xive_irq_set(spapr->xive, irq, lsi);
 }
 
 int spapr_irq_alloc(sPAPRMachineState *spapr, int irq_hint, bool lsi,
@@ -3721,6 +3727,7 @@ void spapr_irq_free(sPAPRMachineState *spapr, int irq, int num)
             memset(&ics->irqs[i], 0, sizeof(ICSIRQState));
         }
     }
+    spapr_xive_irq_unset(spapr->xive, irq);
 }
 
 qemu_irq spapr_irq_get_qirq(sPAPRMachineState *spapr, int irq)
Signature · 1 line
2.13.6
#106 lore raw

PATCH 19/25 spapr: add hcalls support for the XIVE interrupt mode

In reply to Cédric Le Goater

A set of Hypervisor's call are used to configure the interrupt sources and the event/notification queues of the guest:

- H_INT_GET_SOURCE_INFO

used to obtain the address of the MMIO page of the Event State Buffer (PQ bits) entry associated with the source.

- H_INT_SET_SOURCE_CONFIG

assigns a source to a "target".

- H_INT_GET_SOURCE_CONFIG

determines to which "target" and "priority" is assigned to a source

- H_INT_GET_QUEUE_INFO

returns the address of the notification management page associated with the specified "target" and "priority".

- H_INT_SET_QUEUE_CONFIG

sets or resets the event queue for a given "target" and "priority". It is also used to set the notification config associated with the queue, only unconditional notification for the moment. Reset is performed with a queue size of 0 and queueing is disabled in that case.

- H_INT_GET_QUEUE_CONFIG

returns the queue settings for a given "target" and "priority".

- H_INT_RESET

resets all of the partition's interrupt exploitation structures to their initial state, losing all configuration set via the hcalls H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.

- H_INT_SYNC

issue a synchronisation on a source to make sure sure all notifications have reached their queue.

Calls that still need to be addressed :

H_INT_SET_OS_REPORTING_LINE
H_INT_GET_OS_REPORTING_LINE

See the code for more documentation on each hcall.

---
 hw/intc/Makefile.objs       |   2 +-
 hw/intc/spapr_xive_hcall.c  | 885 ++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr.c              |   2 +
 include/hw/ppc/spapr.h      |  15 +-
 include/hw/ppc/spapr_xive.h |   4 +
 5 files changed, 906 insertions(+), 2 deletions(-)
 create mode 100644 hw/intc/spapr_xive_hcall.c
hw/intc/Makefile.objs +1 -1
index 49e13e7aeeee..122e2ec77e8d 100644
@@ -35,7 +35,7 @@ obj-$(CONFIG_SH4) += sh_intc.o
 obj-$(CONFIG_XICS) += xics.o
 obj-$(CONFIG_XICS_SPAPR) += xics_spapr.o
 obj-$(CONFIG_XICS_KVM) += xics_kvm.o
-obj-$(CONFIG_XIVE_SPAPR) += spapr_xive.o
+obj-$(CONFIG_XIVE_SPAPR) += spapr_xive.o spapr_xive_hcall.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
hw/intc/spapr_xive_hcall.c new file +885 -0

885 lines not on this page · raw message

hw/ppc/spapr.c +2 -0
index ca4e72187f60..8b15c0b500d0 100644
@@ -222,6 +222,8 @@ static sPAPRXive *spapr_xive_create(sPAPRMachineState *spapr, int nr_irqs,
         goto error;
     }
 
+    spapr_xive_hcall_init(spapr);
+
     return SPAPR_XIVE(obj);
 error:
     error_propagate(errp, local_err);
include/hw/ppc/spapr.h +14 -1
index 90e2b0f6c678..a25e218b34e2 100644
@@ -387,7 +387,20 @@ struct sPAPRMachineState {
 #define H_INVALIDATE_PID        0x378
 #define H_REGISTER_PROC_TBL     0x37C
 #define H_SIGNAL_SYS_RESET      0x380
-#define MAX_HCALL_OPCODE        H_SIGNAL_SYS_RESET
+
+#define H_INT_GET_SOURCE_INFO   0x3A8
+#define H_INT_SET_SOURCE_CONFIG 0x3AC
+#define H_INT_GET_SOURCE_CONFIG 0x3B0
+#define H_INT_GET_QUEUE_INFO    0x3B4
+#define H_INT_SET_QUEUE_CONFIG  0x3B8
+#define H_INT_GET_QUEUE_CONFIG  0x3BC
+#define H_INT_SET_OS_REPORTING_LINE 0x3C0
+#define H_INT_GET_OS_REPORTING_LINE 0x3C4
+#define H_INT_ESB               0x3C8
+#define H_INT_SYNC              0x3CC
+#define H_INT_RESET             0x3D0
+
+#define MAX_HCALL_OPCODE        H_INT_RESET
 
 /* The hcalls above are standardized in PAPR and implemented by pHyp
  * as well.
include/hw/ppc/spapr_xive.h +4 -0
index 6e8a189e723f..3f822220647f 100644
@@ -79,4 +79,8 @@ bool spapr_xive_irq_unset(sPAPRXive *xive, uint32_t lisn);
 void spapr_xive_pic_print_info(sPAPRXive *xive, Monitor *mon);
 void spapr_xive_icp_pic_print_info(sPAPRXiveICP *xicp, Monitor *mon);
 
+typedef struct sPAPRMachineState sPAPRMachineState;
+
+void spapr_xive_hcall_init(sPAPRMachineState *spapr);
+
 #endif /* PPC_SPAPR_XIVE_H */
Signature · 1 line
2.13.6
#107 lore raw

Re: [Qemu-devel] [PATCH 19/25] spapr: add hcalls support for the XIVE interrupt mode

Quoted 123 lines · Cédric Le Goater

A set of Hypervisor's call are used to configure the interrupt sources and the event/notification queues of the guest:

- H_INT_GET_SOURCE_INFO

used to obtain the address of the MMIO page of the Event State Buffer (PQ bits) entry associated with the source.

- H_INT_SET_SOURCE_CONFIG

assigns a source to a "target".

- H_INT_GET_SOURCE_CONFIG

determines to which "target" and "priority" is assigned to a source

- H_INT_GET_QUEUE_INFO

returns the address of the notification management page associated with the specified "target" and "priority".

- H_INT_SET_QUEUE_CONFIG

sets or resets the event queue for a given "target" and "priority". It is also used to set the notification config associated with the queue, only unconditional notification for the moment. Reset is performed with a queue size of 0 and queueing is disabled in that case.

- H_INT_GET_QUEUE_CONFIG

returns the queue settings for a given "target" and "priority".

- H_INT_RESET

resets all of the partition's interrupt exploitation structures to their initial state, losing all configuration set via the hcalls H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.

- H_INT_SYNC

issue a synchronisation on a source to make sure sure all notifications have reached their queue.

Calls that still need to be addressed :

H_INT_SET_OS_REPORTING_LINE
H_INT_GET_OS_REPORTING_LINE

See the code for more documentation on each hcall.

---
 hw/intc/Makefile.objs       |   2 +-
 hw/intc/spapr_xive_hcall.c  | 885 ++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr.c              |   2 +
 include/hw/ppc/spapr.h      |  15 +-
 include/hw/ppc/spapr_xive.h |   4 +
 5 files changed, 906 insertions(+), 2 deletions(-)
 create mode 100644 hw/intc/spapr_xive_hcall.c
hw/intc/Makefile.objs +1 -1
index 49e13e7aeeee..122e2ec77e8d 100644
@@ -35,7 +35,7 @@ obj-$(CONFIG_SH4) += sh_intc.o
 obj-$(CONFIG_XICS) += xics.o
 obj-$(CONFIG_XICS_SPAPR) += xics_spapr.o
 obj-$(CONFIG_XICS_KVM) += xics_kvm.o
-obj-$(CONFIG_XIVE_SPAPR) += spapr_xive.o
+obj-$(CONFIG_XIVE_SPAPR) += spapr_xive.o spapr_xive_hcall.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
hw/intc/spapr_xive_hcall.c new file +43 -0
new file mode 100644
index 000000000000..676fe0e2d5c7
@@ -0,0 +1,885 @@
+/*
+ * QEMU PowerPC sPAPR XIVE model
+ *
+ * Copyright (c) 2017, IBM Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "cpu.h"
+#include "hw/ppc/spapr.h"
+#include "hw/ppc/spapr_xive.h"
+#include "hw/ppc/fdt.h"
+#include "monitor/monitor.h"
+
+#include "xive-internal.h"
+
+/* Priority ranges reserved by the hypervisor. The Linux driver is
+ * expected to choose priority 6.
+ */
+static const uint32_t reserved_priorities[] = {
+    7,    /* start */
+    0xf8, /* count */
+};
+
+static bool priority_is_valid(uint32_t priority)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(reserved_priorities) / 2; i++) {
+        uint32_t base  = reserved_priorities[2 * i];
+        uint32_t count = reserved_priorities[2 * i + 1];

hw/intc/spapr_xive_hcall.c line 53

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+
+        if (priority >= base && priority < base + count) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: priority %d is reserved\n",
+                          __func__, priority);
+            return false;
+        }
+    }
+
+    return true;
+}

This seems like overkill. Aren't there only 0..7 levels supported in hardware, in which case a one byte bitmap will suffice to store the reserved levels.

To check my understanding again, if you're running this with KVM, the host kernel and qemu will need to agree on which are the reserved levels, yes?

Quoted 33 lines
hw/intc/spapr_xive_hcall.c new file +33 -0
+
+/*
+ * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
+ * real address of the MMIO page through which the Event State Buffer
+ * entry associated with the value of the "lisn" parameter is managed.
+ *
+ * Parameters:
+ * Input
+ * - "flags"
+ *       Bits 0-63 reserved
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *       "ibm,xive-lisn-ranges" properties, or as returned by the
+ *       ibm,query-interrupt-source-number RTAS call, or as returned
+ *       by the H_ALLOCATE_VAS_WINDOW hcall
+ *
+ * Output
+ * - R4: "flags"
+ *       Bits 0-59: Reserved
+ *       Bit 60: H_INT_ESB must be used for Event State Buffer
+ *               management
+ *       Bit 61: 1 == LSI  0 == MSI
+ *       Bit 62: the full function page supports trigger
+ *       Bit 63: Store EOI Supported
+ * - R5: Logical Real address of full function Event State Buffer
+ *       management page, -1 if ESB hcall flag is set to 1.
+ * - R6: Logical Real Address of trigger only Event State Buffer
+ *       management page or -1.
+ * - R7: Power of 2 page size for the ESB management pages returned in
+ *       R5 and R6.
+ */
+static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
+                                          sPAPRMachineState *spapr,
+                                          target_ulong opcode,

hw/intc/spapr_xive_hcall.c line 96

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+                                          target_ulong *args)
+{
+    sPAPRXive *xive = spapr->xive;
+    XiveIVE *ive;
+    target_ulong flags  = args[0];
+    target_ulong lisn   = args[1];
+    uint64_t mmio_base;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;

Is H_FUNCTION required by the PAPR ACRs here? Usually we only use H_FUNCTION if the hypercall doesn't exist at all, and if unavailable for other reasons use H_AUTHORITY or something.

Quoted 7 lines
hw/intc/spapr_xive_hcall.c new file +7 -0
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*

hw/intc/spapr_xive_hcall.c line 113

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    mmio_base = (uint64_t)xive->esb_base + (1ull << xive->esb_shift) * lisn;

Hrm.. why was xive->esb_base not already a u64?

Quoted 9 lines
hw/intc/spapr_xive_hcall.c new file +9 -0
+    args[0] = 0;
+    if (spapr_xive_irq_is_lsi(xive, lisn)) {
+        args[0] |= XIVE_SRC_LSI;
+    }
+    if (xive->flags & XIVE_SRC_TRIGGER) {
+        args[0] |= XIVE_SRC_TRIGGER;
+    }
+
+    if (xive->flags & XIVE_SRC_H_INT_ESB) {

hw/intc/spapr_xive_hcall.c line 133

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+        args[1] = -1; /* never used in QEMU  */
+        args[2] = -1;
+    } else {
+        args[1] = mmio_base;
+        if (xive->flags & XIVE_SRC_TRIGGER) {
+            args[2] = -1; /* No specific trigger page */
+        } else {
+            args[2] = -1; /* TODO: support for specific trigger page */
+        }
+    }

What does the availability of SRC_TRIGGER (and INT_ESB) depend on? If it varies with host capabilities, that's going to be real pain for migration.

Quoted 35 lines
hw/intc/spapr_xive_hcall.c new file +35 -0
+
+    args[3] = xive->esb_shift;
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
+ * Interrupt Source to a target. The Logical Interrupt Source is
+ * designated with the "lisn" parameter and the target is designated
+ * with the "target" and "priority" parameters.  Upon return from the
+ * hcall(), no additional interrupts will be directed to the old EQ.
+ *
+ * TODO: The old EQ should be investigated for interrupts that
+ * occurred prior to or during the hcall().
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-61: Reserved
+ *      Bit 62: set the "eisn" in the EA
+ *      Bit 63: masks the interrupt source in the hardware interrupt
+ *      control structure. An interrupt masked by this mechanism will
+ *      be dropped, but it's source state bits will still be
+ *      set. There is no race-free way of unmasking and restoring the
+ *      source. Thus this should only be used in interrupts that are
+ *      also masked at the source, and only in cases where the
+ *      interrupt is not meant to be used for a large amount of time
+ *      because no valid target exists for it for example
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as returned by
+ *      the H_ALLOCATE_VAS_WINDOW hcall
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *      "ibm,ppc-interrupt-gserver#s"

hw/intc/spapr_xive_hcall.c line 178

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+ * - "priority" is a valid priority not in
+ *      "ibm,plat-res-int-priorities"
+ * - "eisn" is the guest EISN associated with the "lisn"
+ *
+ * Output:
+ * - None
+ */
+
+#define XIVE_SRC_SET_EISN (1ull << (63 - 62))
+#define XIVE_SRC_MASK     (1ull << (63 - 63))

Aren't there already a bunch of macros you have for defining things in terms of IBM bit numbers, so you can avoid open coding (63 - whatever).

Quoted 45 lines
hw/intc/spapr_xive_hcall.c new file +45 -0
+
+static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
+                                            sPAPRMachineState *spapr,
+                                            target_ulong opcode,
+                                            target_ulong *args)
+{
+    XiveIVE *ive;
+    uint64_t new_ive;
+    target_ulong flags    = args[0];
+    target_ulong lisn     = args[1];
+    target_ulong target   = args[2];
+    target_ulong priority = args[3];
+    target_ulong eisn     = args[4];
+    uint32_t eq_idx;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~(XIVE_SRC_SET_EISN | XIVE_SRC_MASK)) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    /* priority 0xff is used to reset the IVE */
+    if (priority == 0xff) {
+        new_ive = IVE_VALID | IVE_MASKED;
+        goto out;
+    }
+
+    new_ive = ive->w;
+
+    if (flags & XIVE_SRC_MASK) {
+        new_ive = ive->w | IVE_MASKED;
+    } else {
+        new_ive = ive->w & ~IVE_MASKED;

hw/intc/spapr_xive_hcall.c line 233

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+    }
+
+    if (!priority_is_valid(priority)) {
+        return H_P4;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */

What's this about? I thought the point of XIVE was you could set up target queues for your vcpus regardless of mapping to physical cpus.

Quoted 287 lines
hw/intc/spapr_xive_hcall.c new file +287 -0
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P3;
+    }
+
+    new_ive = SETFIELD(IVE_EQ_BLOCK, new_ive, 0ul);
+    new_ive = SETFIELD(IVE_EQ_INDEX, new_ive, eq_idx);
+
+    if (flags & XIVE_SRC_SET_EISN) {
+        new_ive = SETFIELD(IVE_EQ_DATA, new_ive, eisn);
+    }
+
+out:
+    /* TODO: handle syncs ? */
+
+    /* And update */
+    ive->w = new_ive;
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
+ * target/priority pair is assigned to the specified Logical Interrupt
+ * Source.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63 Reserved
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ *
+ * Output:
+ * - R4: Target to which the specified Logical Interrupt Source is
+ *       assigned
+ * - R5: Priority to which the specified Logical Interrupt Source is
+ *       assigned
+ * - R6: EISN for the specified Logical Interrupt Source (this will be
+ *       equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
+ */
+static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
+                                            sPAPRMachineState *spapr,
+                                            target_ulong opcode,
+                                            target_ulong *args)
+{
+    target_ulong flags = args[0];
+    target_ulong lisn = args[1];
+    XiveIVE *ive;
+    XiveEQ *eq;
+    uint32_t eq_idx;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    eq_idx = GETFIELD(IVE_EQ_INDEX, ive->w);
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq) {
+        return H_HARDWARE;
+    }
+
+    args[0] = GETFIELD(EQ_W6_NVT_INDEX, eq->w6);
+
+    if (ive->w & IVE_MASKED) {
+        args[1] = 0xff;
+    } else {
+        args[1] = GETFIELD(EQ_W7_F0_PRIORITY, eq->w7);
+    }
+
+    args[2] = GETFIELD(IVE_EQ_DATA, ive->w);
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
+ * address of the notification management page associated with the
+ * specified target and priority.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *       Bits 0-63 Reserved
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ *
+ * Output:
+ * - R4: Logical real address of notification page
+ * - R5: Power of 2 page size of the notification page
+ */
+static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
+                                         sPAPRMachineState *spapr,
+                                         target_ulong opcode,
+                                         target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    uint32_t eq_idx;
+    XiveEQ *eq;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq)  {
+        return H_HARDWARE;
+    }
+
+    args[0] = -1; /* TODO: return ESn page */
+    if (eq->w0 & EQ_W0_ENQUEUE) {
+        args[1] = GETFIELD(EQ_W0_QSIZE, eq->w0) + 12;
+    } else {
+        args[1] = 0;
+    }
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
+ * a given "target" and "priority".  It is also used to set the
+ * notification config associated with the EQ.  An EQ size of 0 is
+ * used to reset the EQ config for a given target and priority. If
+ * resetting the EQ config, the END associated with the given "target"
+ * and "priority" will be changed to disable queueing.
+ *
+ * Upon return from the hcall(), no additional interrupts will be
+ * directed to the old EQ (if one was set). The old EQ (if one was
+ * set) should be investigated for interrupts that occurred prior to
+ * or during the hcall().
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      Bit 63: Unconditional Notify (n) per the XIVE spec
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ * - "eventQueue": The logical real address of the start of the EQ
+ * - "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
+ *
+ * Output:
+ * - None
+ */
+
+#define XIVE_EQ_ALWAYS_NOTIFY (1ull << (63 - 63))
+
+static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
+                                           sPAPRMachineState *spapr,
+                                           target_ulong opcode,
+                                           target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    target_ulong qpage    = args[3];
+    target_ulong qsize    = args[4];
+    uint32_t eq_idx;
+    XiveEQ *old_eq;
+    XiveEQ eq;
+    uint32_t qdata;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_EQ_ALWAYS_NOTIFY) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    old_eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!old_eq)  {
+        return H_HARDWARE;
+    }
+
+    eq = *old_eq;
+
+    switch (qsize) {
+    case 12:
+    case 16:
+    case 21:
+    case 24:
+        eq.w3 = ((uint64_t)qpage) & 0xffffffff;
+        eq.w2 = (((uint64_t)qpage)) >> 32 & 0x0fffffff;
+        eq.w0 |= EQ_W0_ENQUEUE;
+        eq.w0 = SETFIELD(EQ_W0_QSIZE, eq.w0, qsize - 12);
+        break;
+    case 0:
+        /* reset queue and disable queueing */
+        eq.w2 = eq.w3 = 0;
+        eq.w0 &= ~EQ_W0_ENQUEUE;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid EQ size %"PRIx64"\n",
+                      __func__, qsize);
+        return H_P5;
+    }
+
+    if (qsize) {
+        /*
+         * Let's validate the EQ address with a read of the first EQ
+         * entry. We could also check that the full queue has been
+         * zeroed by the OS.
+         */
+        if (address_space_read(&address_space_memory, qpage,
+                               MEMTXATTRS_UNSPECIFIED,
+                               (uint8_t *) &qdata, sizeof(qdata))) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to read EQ data @0x%"
+                          HWADDR_PRIx "\n", __func__, qpage);
+            return H_P4;
+        }
+    }
+

hw/intc/spapr_xive_hcall.c line 531

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+    /* Ensure the priority and target are correctly set (they will not
+     * be right after allocation)
+     */
+    eq.w6 = SETFIELD(EQ_W6_NVT_BLOCK, 0ul, 0ul) |
+        SETFIELD(EQ_W6_NVT_INDEX, 0ul, target);
+    eq.w7 = SETFIELD(EQ_W7_F0_PRIORITY, 0ul, priority);
+
+    /* TODO: depends on notitification page (ESn) from H_INT_GET_QUEUE_INFO */
+    if (flags & XIVE_EQ_ALWAYS_NOTIFY) {
+        eq.w0 |= EQ_W0_UCOND_NOTIFY;

Do you need to also clear if the flag is not set? AFAICT eq.w0 is inherited from teh old queue and enver reset from scratch.

hw/intc/spapr_xive_hcall.c line 544

Quoted 13 lines
hw/intc/spapr_xive_hcall.c new file +13 -0
+    }
+
+    /* The generation bit for the EQ starts at 1 and The EQ page
+     * offset counter starts at 0.
+     */
+    eq.w1 = EQ_W1_GENERATION | SETFIELD(EQ_W1_PAGE_OFF, 0ul, 0ul);
+    eq.w0 |= EQ_W0_VALID;
+
+    /* TODO: issue syncs required to ensure all in-flight interrupts
+     * are complete on the old EQ */
+
+    /* Update EQ */
+    *old_eq = eq;

Hrm. The BQL probably saves you, but in general do you need to make sure the ENQUEUE bit is set after updating everything else?

Quoted 82 lines
hw/intc/spapr_xive_hcall.c new file +82 -0
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
+ * target and priority.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      Bit 63: Debug: Return debug data
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ *
+ * Output:
+ * - R4: "flags":
+ *       Bits 0-62: Reserved
+ *       Bit 63: The value of Unconditional Notify (n) per the XIVE spec
+ * - R5: The logical real address of the start of the EQ
+ * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
+ * - R7: The value of Event Queue Offset Counter per XIVE spec
+ *       if "Debug" = 1, else 0
+ *
+ */
+
+#define XIVE_EQ_DEBUG     (1ull << (63 - 63))
+
+static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
+                                           sPAPRMachineState *spapr,
+                                           target_ulong opcode,
+                                           target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    uint32_t eq_idx;
+    XiveEQ *eq;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_EQ_DEBUG) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq)  {
+        return H_HARDWARE;
+    }
+
+    args[0] = 0;
+    if (eq->w0 & EQ_W0_UCOND_NOTIFY) {
+        args[0] |= XIVE_EQ_ALWAYS_NOTIFY;
+    }
+

hw/intc/spapr_xive_hcall.c line 636

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+    if (eq->w0 & EQ_W0_ENQUEUE) {
+        args[1] =
+            (((uint64_t)(eq->w2 & 0x0fffffff)) << 32) | eq->w3;
+        args[2] = GETFIELD(EQ_W0_QSIZE, eq->w0) + 12;
+    } else {
+        args[1] = 0;
+        args[2] = 0;
+    }
+
+    /* TODO: do we need any locking on the EQ ? */

Probably not if you're designating it as protected by the BQL.

Quoted 301 lines
hw/intc/spapr_xive_hcall.c new file +249 -0
+    if (flags & XIVE_EQ_DEBUG) {
+        /* Load the event queue generation number into the return flags */
+        args[0] |= GETFIELD(EQ_W1_GENERATION, eq->w1);
+
+        /* Load R7 with the event queue offset counter */
+        args[3] = GETFIELD(EQ_W1_PAGE_OFF, eq->w1);
+    }
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
+ * reporting cache line pair for the calling thread.  The reporting
+ * cache lines will contain the OS interrupt context when the OS
+ * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
+ * interrupt. The reporting cache lines can be reset by inputting -1
+ * in "reportingLine".  Issuing the CI store byte without reporting
+ * cache lines registered will result in the data not being accessible
+ * to the OS.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "reportingLine": The logical real address of the reporting cache
+ *    line pair
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
+                                                sPAPRMachineState *spapr,
+                                                target_ulong opcode,
+                                                target_ulong *args)
+{
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* TODO: H_INT_SET_OS_REPORTING_LINE */
+    return H_FUNCTION;
+}
+
+/*
+ * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
+ * real address of the reporting cache line pair set for the input
+ * "target".  If no reporting cache line pair has been set, -1 is
+ * returned.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "reportingLine": The logical real address of the reporting cache
+ *   line pair
+ *
+ * Output:
+ * - R4: The logical real address of the reporting line if set, else -1
+ */
+static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
+                                                sPAPRMachineState *spapr,
+                                                target_ulong opcode,
+                                                target_ulong *args)
+{
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* TODO: H_INT_GET_OS_REPORTING_LINE */
+    return H_FUNCTION;
+}
+
+/*
+ * The H_INT_ESB hcall() is used to issue a load or store to the ESB
+ * page for the input "lisn".  This hcall is only supported for LISNs
+ * that have the ESB hcall flag set to 1 when returned from hcall()
+ * H_INT_GET_SOURCE_INFO.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      bit 63: Store: Store=1, store operation, else load operation
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ * - "esbOffset" is the offset into the ESB page for the load or store operation
+ * - "storeData" is the data to write for a store operation
+ *
+ * Output:
+ * - R4: R4: The value of the load if load operation, else -1
+ */
+
+#define XIVE_ESB_STORE (1ull << (63 - 63))
+
+static target_ulong h_int_esb(PowerPCCPU *cpu,
+                              sPAPRMachineState *spapr,
+                              target_ulong opcode,
+                              target_ulong *args)
+{
+    sPAPRXive *xive = spapr->xive;
+    XiveIVE *ive;
+    target_ulong flags   = args[0];
+    target_ulong lisn    = args[1];
+    target_ulong offset  = args[2];
+    target_ulong data    = args[3];
+    uint64_t esb_base;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_ESB_STORE) {
+        return H_PARAMETER;
+    }
+
+    ive = spapr_xive_get_ive(xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    if (offset > (1ull << xive->esb_shift)) {
+        return H_P3;
+    }
+
+    esb_base = (uint64_t)xive->esb_base + (1ull << xive->esb_shift) * lisn;
+    esb_base += offset;
+
+    if (dma_memory_rw(&address_space_memory, esb_base, &data, 8,
+                      (flags & XIVE_ESB_STORE))) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to rw data @0x%"
+                      HWADDR_PRIx "\n", __func__, esb_base);
+        return H_HARDWARE;
+    }
+    args[0] = (flags & XIVE_ESB_STORE) ? -1 : data;
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SYNC hcall() is used to issue hardware syncs that will
+ * ensure any in flight events for the input lisn are in the event
+ * queue.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_sync(PowerPCCPU *cpu,
+                               sPAPRMachineState *spapr,
+                               target_ulong opcode,
+                               target_ulong *args)
+{
+    XiveIVE *ive;
+    target_ulong flags   = args[0];
+    target_ulong lisn    = args[1];
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* This is not real hardware. Nothing to be done */
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_RESET hcall() is used to reset all of the partition's
+ * interrupt exploitation structures to their initial state.  This
+ * means losing all previously set interrupt state set via
+ * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_reset(PowerPCCPU *cpu,
+                                sPAPRMachineState *spapr,
+                                target_ulong opcode,
+                                target_ulong *args)
+{
+    target_ulong flags   = args[0];
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    spapr_xive_reset(spapr->xive);
+    return H_SUCCESS;
+}
+
+void spapr_xive_hcall_init(sPAPRMachineState *spapr)
+{
+    spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
+    spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
+    spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
+    spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
+    spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
+    spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
+    spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
+                             h_int_set_os_reporting_line);
+    spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
+                             h_int_get_os_reporting_line);
+    spapr_register_hypercall(H_INT_ESB, h_int_esb);
+    spapr_register_hypercall(H_INT_SYNC, h_int_sync);
+    spapr_register_hypercall(H_INT_RESET, h_int_reset);
+}
hw/ppc/spapr.c +2 -0
index ca4e72187f60..8b15c0b500d0 100644
@@ -222,6 +222,8 @@ static sPAPRXive *spapr_xive_create(sPAPRMachineState *spapr, int nr_irqs,
         goto error;
     }
 
+    spapr_xive_hcall_init(spapr);
+
     return SPAPR_XIVE(obj);
 error:
     error_propagate(errp, local_err);
include/hw/ppc/spapr.h +14 -1
index 90e2b0f6c678..a25e218b34e2 100644
@@ -387,7 +387,20 @@ struct sPAPRMachineState {
 #define H_INVALIDATE_PID        0x378
 #define H_REGISTER_PROC_TBL     0x37C
 #define H_SIGNAL_SYS_RESET      0x380
-#define MAX_HCALL_OPCODE        H_SIGNAL_SYS_RESET
+
+#define H_INT_GET_SOURCE_INFO   0x3A8
+#define H_INT_SET_SOURCE_CONFIG 0x3AC
+#define H_INT_GET_SOURCE_CONFIG 0x3B0
+#define H_INT_GET_QUEUE_INFO    0x3B4
+#define H_INT_SET_QUEUE_CONFIG  0x3B8
+#define H_INT_GET_QUEUE_CONFIG  0x3BC
+#define H_INT_SET_OS_REPORTING_LINE 0x3C0
+#define H_INT_GET_OS_REPORTING_LINE 0x3C4
+#define H_INT_ESB               0x3C8
+#define H_INT_SYNC              0x3CC
+#define H_INT_RESET             0x3D0
+
+#define MAX_HCALL_OPCODE        H_INT_RESET
 
 /* The hcalls above are standardized in PAPR and implemented by pHyp
  * as well.
include/hw/ppc/spapr_xive.h +4 -0
index 6e8a189e723f..3f822220647f 100644
@@ -79,4 +79,8 @@ bool spapr_xive_irq_unset(sPAPRXive *xive, uint32_t lisn);
 void spapr_xive_pic_print_info(sPAPRXive *xive, Monitor *mon);
 void spapr_xive_icp_pic_print_info(sPAPRXiveICP *xicp, Monitor *mon);
 
+typedef struct sPAPRMachineState sPAPRMachineState;
+
+void spapr_xive_hcall_init(sPAPRMachineState *spapr);
+
 #endif /* PPC_SPAPR_XIVE_H */
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#108 lore raw

Re: [Qemu-devel] [PATCH 19/25] spapr: add hcalls support for the XIVE interrupt mode

Quoted 130 lines · David Gibson
Quoted 129 lines · Cédric Le Goater

A set of Hypervisor's call are used to configure the interrupt sources and the event/notification queues of the guest:

- H_INT_GET_SOURCE_INFO

used to obtain the address of the MMIO page of the Event State Buffer (PQ bits) entry associated with the source.

- H_INT_SET_SOURCE_CONFIG

assigns a source to a "target".

- H_INT_GET_SOURCE_CONFIG

determines to which "target" and "priority" is assigned to a source

- H_INT_GET_QUEUE_INFO

returns the address of the notification management page associated with the specified "target" and "priority".

- H_INT_SET_QUEUE_CONFIG

sets or resets the event queue for a given "target" and "priority". It is also used to set the notification config associated with the queue, only unconditional notification for the moment. Reset is performed with a queue size of 0 and queueing is disabled in that case.

- H_INT_GET_QUEUE_CONFIG

returns the queue settings for a given "target" and "priority".

- H_INT_RESET

resets all of the partition's interrupt exploitation structures to their initial state, losing all configuration set via the hcalls H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.

- H_INT_SYNC

issue a synchronisation on a source to make sure sure all notifications have reached their queue.

Calls that still need to be addressed :

H_INT_SET_OS_REPORTING_LINE
H_INT_GET_OS_REPORTING_LINE

See the code for more documentation on each hcall.

---
 hw/intc/Makefile.objs       |   2 +-
 hw/intc/spapr_xive_hcall.c  | 885 ++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr.c              |   2 +
 include/hw/ppc/spapr.h      |  15 +-
 include/hw/ppc/spapr_xive.h |   4 +
 5 files changed, 906 insertions(+), 2 deletions(-)
 create mode 100644 hw/intc/spapr_xive_hcall.c
hw/intc/Makefile.objs +1 -1
index 49e13e7aeeee..122e2ec77e8d 100644
@@ -35,7 +35,7 @@ obj-$(CONFIG_SH4) += sh_intc.o
 obj-$(CONFIG_XICS) += xics.o
 obj-$(CONFIG_XICS_SPAPR) += xics_spapr.o
 obj-$(CONFIG_XICS_KVM) += xics_kvm.o
-obj-$(CONFIG_XIVE_SPAPR) += spapr_xive.o
+obj-$(CONFIG_XIVE_SPAPR) += spapr_xive.o spapr_xive_hcall.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
hw/intc/spapr_xive_hcall.c new file +49 -0
new file mode 100644
index 000000000000..676fe0e2d5c7
@@ -0,0 +1,885 @@
+/*
+ * QEMU PowerPC sPAPR XIVE model
+ *
+ * Copyright (c) 2017, IBM Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "cpu.h"
+#include "hw/ppc/spapr.h"
+#include "hw/ppc/spapr_xive.h"
+#include "hw/ppc/fdt.h"
+#include "monitor/monitor.h"
+
+#include "xive-internal.h"
+
+/* Priority ranges reserved by the hypervisor. The Linux driver is
+ * expected to choose priority 6.
+ */
+static const uint32_t reserved_priorities[] = {
+    7,    /* start */
+    0xf8, /* count */
+};
+
+static bool priority_is_valid(uint32_t priority)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(reserved_priorities) / 2; i++) {
+        uint32_t base  = reserved_priorities[2 * i];
+        uint32_t count = reserved_priorities[2 * i + 1];
+
+        if (priority >= base && priority < base + count) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: priority %d is reserved\n",
+                          __func__, priority);
+            return false;
+        }
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 53

Quoted 4 lines
hw/intc/spapr_xive_hcall.c new file +4 -0
+    }
+
+    return true;
+}

This seems like overkill. Aren't there only 0..7 levels supported in hardware, in which case a one byte bitmap will suffice to store the reserved levels.

I was trying the use the same array that will be exposed in the device tree in the "ibm,plat-res-int-priorities" property, defined as follow in PAPR:

property name that designates to the client program that the platform has reserved one or more interrupt priorities for its own use.

prop-encoded-value: one or more (interrupt priority, range) pairs, where interrupt priority is a single cell hexidec- imal number between 0x00 and 0xFF, and range is an integer encoded as with encode-int that represents the number of contiguous interrupt priorities that have been reserved by the platform for its internal use.

But I agree, it's a bit overkill to check for 0..7 levels ...

Quoted 3 lines

To check my understanding again, if you're running this with KVM, the host kernel and qemu will need to agree on which are the reserved levels, yes?

Hmm, these values are quite static. So I don't think there will be any sort of exchange between KVM and QEMU to define the range to expose to the guest.

For the moment, Linux only uses one priority, the lowest, and Ben has introduced in OPAL an automatic interrupt escalation feature using queue 7 for all other queues (DD2.0 cpus). So we only expose range 0..6 to the guest for this purpose.

So we agreed orally.

Quoted 38 lines
Quoted 38 lines
hw/intc/spapr_xive_hcall.c new file +37 -0
+
+/*
+ * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
+ * real address of the MMIO page through which the Event State Buffer
+ * entry associated with the value of the "lisn" parameter is managed.
+ *
+ * Parameters:
+ * Input
+ * - "flags"
+ *       Bits 0-63 reserved
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *       "ibm,xive-lisn-ranges" properties, or as returned by the
+ *       ibm,query-interrupt-source-number RTAS call, or as returned
+ *       by the H_ALLOCATE_VAS_WINDOW hcall
+ *
+ * Output
+ * - R4: "flags"
+ *       Bits 0-59: Reserved
+ *       Bit 60: H_INT_ESB must be used for Event State Buffer
+ *               management
+ *       Bit 61: 1 == LSI  0 == MSI
+ *       Bit 62: the full function page supports trigger
+ *       Bit 63: Store EOI Supported
+ * - R5: Logical Real address of full function Event State Buffer
+ *       management page, -1 if ESB hcall flag is set to 1.
+ * - R6: Logical Real Address of trigger only Event State Buffer
+ *       management page or -1.
+ * - R7: Power of 2 page size for the ESB management pages returned in
+ *       R5 and R6.
+ */
+static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
+                                          sPAPRMachineState *spapr,
+                                          target_ulong opcode,
+                                          target_ulong *args)
+{
+    sPAPRXive *xive = spapr->xive;
+    XiveIVE *ive;
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 96

Quoted 6 lines
hw/intc/spapr_xive_hcall.c new file +6 -0
+    target_ulong flags  = args[0];
+    target_ulong lisn   = args[1];
+    uint64_t mmio_base;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;

Is H_FUNCTION required by the PAPR ACRs here?

yes. quoting the specs :

	/* H_Function: The calling OS is not in exploitation mode */

I need to review once more all of the return errors but, last time I checked they looked sane.

Quoted 15 lines

Usually we only use
H_FUNCTION if the hypercall doesn't exist at all, and if unavailable for other reasons use H_AUTHORITY or something.

Quoted 11 lines
hw/intc/spapr_xive_hcall.c new file +11 -0
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 113

Quoted 6 lines
hw/intc/spapr_xive_hcall.c new file +6 -0
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    mmio_base = (uint64_t)xive->esb_base + (1ull << xive->esb_shift) * lisn;

Hrm.. why was xive->esb_base not already a u64?

its an 'hwaddr'. Yes I can remove it.

Quoted 9 lines

hw/intc/spapr_xive_hcall.c line 123

Quoted 9 lines
hw/intc/spapr_xive_hcall.c new file +9 -0
+    args[0] = 0;
+    if (spapr_xive_irq_is_lsi(xive, lisn)) {
+        args[0] |= XIVE_SRC_LSI;
+    }
+    if (xive->flags & XIVE_SRC_TRIGGER) {
+        args[0] |= XIVE_SRC_TRIGGER;
+    }
+
+    if (xive->flags & XIVE_SRC_H_INT_ESB) {

btw, this is why I have the ->flags field. Do you still want me to remove it ? because I would like to keep the logic below. No big deal if not.

Quoted 11 lines

hw/intc/spapr_xive_hcall.c line 133

Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+        args[1] = -1; /* never used in QEMU  */
+        args[2] = -1;
+    } else {
+        args[1] = mmio_base;
+        if (xive->flags & XIVE_SRC_TRIGGER) {
+            args[2] = -1; /* No specific trigger page */
+        } else {
+            args[2] = -1; /* TODO: support for specific trigger page */
+        }
+    }
Quoted 1 line

What does the availability of SRC_TRIGGER (and INT_ESB) depend on?

The CPU revision. But we won't introduce XIVE exploitation mode on anything else than DD2.0 which has full XIVE support. Even STORE_EOI that we should be adding.

Quoted 2 lines

If it varies with host capabilities, that's going to be real pain for migration.

Yes. I am not aware of any future extension but I agree this is something we need to keep an eye on.

Quoted 40 lines
Quoted 40 lines
hw/intc/spapr_xive_hcall.c new file +40 -0
+
+    args[3] = xive->esb_shift;
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
+ * Interrupt Source to a target. The Logical Interrupt Source is
+ * designated with the "lisn" parameter and the target is designated
+ * with the "target" and "priority" parameters.  Upon return from the
+ * hcall(), no additional interrupts will be directed to the old EQ.
+ *
+ * TODO: The old EQ should be investigated for interrupts that
+ * occurred prior to or during the hcall().
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-61: Reserved
+ *      Bit 62: set the "eisn" in the EA
+ *      Bit 63: masks the interrupt source in the hardware interrupt
+ *      control structure. An interrupt masked by this mechanism will
+ *      be dropped, but it's source state bits will still be
+ *      set. There is no race-free way of unmasking and restoring the
+ *      source. Thus this should only be used in interrupts that are
+ *      also masked at the source, and only in cases where the
+ *      interrupt is not meant to be used for a large amount of time
+ *      because no valid target exists for it for example
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as returned by
+ *      the H_ALLOCATE_VAS_WINDOW hcall
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *      "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *      "ibm,plat-res-int-priorities"
+ * - "eisn" is the guest EISN associated with the "lisn"
+ *
+ * Output:
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 178

Quoted 5 lines
hw/intc/spapr_xive_hcall.c new file +5 -0
+ * - None
+ */
+
+#define XIVE_SRC_SET_EISN (1ull << (63 - 62))
+#define XIVE_SRC_MASK     (1ull << (63 - 63))

Aren't there already a bunch of macros you have for defining things in terms of IBM bit numbers, so you can avoid open coding (63 - whatever).

Yes.

On that topic, could we include the PPC_BIT* macros somewhere under ppc ?

Quoted 49 lines
Quoted 49 lines
hw/intc/spapr_xive_hcall.c new file +49 -0
+
+static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
+                                            sPAPRMachineState *spapr,
+                                            target_ulong opcode,
+                                            target_ulong *args)
+{
+    XiveIVE *ive;
+    uint64_t new_ive;
+    target_ulong flags    = args[0];
+    target_ulong lisn     = args[1];
+    target_ulong target   = args[2];
+    target_ulong priority = args[3];
+    target_ulong eisn     = args[4];
+    uint32_t eq_idx;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~(XIVE_SRC_SET_EISN | XIVE_SRC_MASK)) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    /* priority 0xff is used to reset the IVE */
+    if (priority == 0xff) {
+        new_ive = IVE_VALID | IVE_MASKED;
+        goto out;
+    }
+
+    new_ive = ive->w;
+
+    if (flags & XIVE_SRC_MASK) {
+        new_ive = ive->w | IVE_MASKED;
+    } else {
+        new_ive = ive->w & ~IVE_MASKED;
+    }
+
+    if (!priority_is_valid(priority)) {
+        return H_P4;
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 233

Quoted 6 lines
hw/intc/spapr_xive_hcall.c new file +6 -0
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */

What's this about?

That is from the specs and I haven't quite figured out what it meant. I need to ask.

Quoted 2 lines

I thought the point of XIVE was you could set up
target queues for your vcpus regardless of mapping to physical cpus.

yes.

Quoted 292 lines
Quoted 292 lines
hw/intc/spapr_xive_hcall.c new file +292 -0
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P3;
+    }
+
+    new_ive = SETFIELD(IVE_EQ_BLOCK, new_ive, 0ul);
+    new_ive = SETFIELD(IVE_EQ_INDEX, new_ive, eq_idx);
+
+    if (flags & XIVE_SRC_SET_EISN) {
+        new_ive = SETFIELD(IVE_EQ_DATA, new_ive, eisn);
+    }
+
+out:
+    /* TODO: handle syncs ? */
+
+    /* And update */
+    ive->w = new_ive;
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
+ * target/priority pair is assigned to the specified Logical Interrupt
+ * Source.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63 Reserved
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ *
+ * Output:
+ * - R4: Target to which the specified Logical Interrupt Source is
+ *       assigned
+ * - R5: Priority to which the specified Logical Interrupt Source is
+ *       assigned
+ * - R6: EISN for the specified Logical Interrupt Source (this will be
+ *       equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
+ */
+static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
+                                            sPAPRMachineState *spapr,
+                                            target_ulong opcode,
+                                            target_ulong *args)
+{
+    target_ulong flags = args[0];
+    target_ulong lisn = args[1];
+    XiveIVE *ive;
+    XiveEQ *eq;
+    uint32_t eq_idx;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    eq_idx = GETFIELD(IVE_EQ_INDEX, ive->w);
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq) {
+        return H_HARDWARE;
+    }
+
+    args[0] = GETFIELD(EQ_W6_NVT_INDEX, eq->w6);
+
+    if (ive->w & IVE_MASKED) {
+        args[1] = 0xff;
+    } else {
+        args[1] = GETFIELD(EQ_W7_F0_PRIORITY, eq->w7);
+    }
+
+    args[2] = GETFIELD(IVE_EQ_DATA, ive->w);
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
+ * address of the notification management page associated with the
+ * specified target and priority.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *       Bits 0-63 Reserved
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ *
+ * Output:
+ * - R4: Logical real address of notification page
+ * - R5: Power of 2 page size of the notification page
+ */
+static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
+                                         sPAPRMachineState *spapr,
+                                         target_ulong opcode,
+                                         target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    uint32_t eq_idx;
+    XiveEQ *eq;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq)  {
+        return H_HARDWARE;
+    }
+
+    args[0] = -1; /* TODO: return ESn page */
+    if (eq->w0 & EQ_W0_ENQUEUE) {
+        args[1] = GETFIELD(EQ_W0_QSIZE, eq->w0) + 12;
+    } else {
+        args[1] = 0;
+    }
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
+ * a given "target" and "priority".  It is also used to set the
+ * notification config associated with the EQ.  An EQ size of 0 is
+ * used to reset the EQ config for a given target and priority. If
+ * resetting the EQ config, the END associated with the given "target"
+ * and "priority" will be changed to disable queueing.
+ *
+ * Upon return from the hcall(), no additional interrupts will be
+ * directed to the old EQ (if one was set). The old EQ (if one was
+ * set) should be investigated for interrupts that occurred prior to
+ * or during the hcall().
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      Bit 63: Unconditional Notify (n) per the XIVE spec
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ * - "eventQueue": The logical real address of the start of the EQ
+ * - "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
+ *
+ * Output:
+ * - None
+ */
+
+#define XIVE_EQ_ALWAYS_NOTIFY (1ull << (63 - 63))
+
+static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
+                                           sPAPRMachineState *spapr,
+                                           target_ulong opcode,
+                                           target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    target_ulong qpage    = args[3];
+    target_ulong qsize    = args[4];
+    uint32_t eq_idx;
+    XiveEQ *old_eq;
+    XiveEQ eq;
+    uint32_t qdata;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_EQ_ALWAYS_NOTIFY) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    old_eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!old_eq)  {
+        return H_HARDWARE;
+    }
+
+    eq = *old_eq;
+
+    switch (qsize) {
+    case 12:
+    case 16:
+    case 21:
+    case 24:
+        eq.w3 = ((uint64_t)qpage) & 0xffffffff;
+        eq.w2 = (((uint64_t)qpage)) >> 32 & 0x0fffffff;
+        eq.w0 |= EQ_W0_ENQUEUE;
+        eq.w0 = SETFIELD(EQ_W0_QSIZE, eq.w0, qsize - 12);
+        break;
+    case 0:
+        /* reset queue and disable queueing */
+        eq.w2 = eq.w3 = 0;
+        eq.w0 &= ~EQ_W0_ENQUEUE;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid EQ size %"PRIx64"\n",
+                      __func__, qsize);
+        return H_P5;
+    }
+
+    if (qsize) {
+        /*
+         * Let's validate the EQ address with a read of the first EQ
+         * entry. We could also check that the full queue has been
+         * zeroed by the OS.
+         */
+        if (address_space_read(&address_space_memory, qpage,
+                               MEMTXATTRS_UNSPECIFIED,
+                               (uint8_t *) &qdata, sizeof(qdata))) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to read EQ data @0x%"
+                          HWADDR_PRIx "\n", __func__, qpage);
+            return H_P4;
+        }
+    }
+
+    /* Ensure the priority and target are correctly set (they will not
+     * be right after allocation)
+     */
+    eq.w6 = SETFIELD(EQ_W6_NVT_BLOCK, 0ul, 0ul) |
+        SETFIELD(EQ_W6_NVT_INDEX, 0ul, target);
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 531

Quoted 5 lines
hw/intc/spapr_xive_hcall.c new file +5 -0
+    eq.w7 = SETFIELD(EQ_W7_F0_PRIORITY, 0ul, priority);
+
+    /* TODO: depends on notitification page (ESn) from H_INT_GET_QUEUE_INFO */
+    if (flags & XIVE_EQ_ALWAYS_NOTIFY) {
+        eq.w0 |= EQ_W0_UCOND_NOTIFY;

Do you need to also clear if the flag is not set? AFAICT eq.w0 is inherited from teh old queue and enver reset from scratch.

True. It is always on if the EQ is not reseted. I also need to be more precise in spapr_xive_irq() when dealing with the reseted EQs. The model has not fallen in to that trap yet.

Quoted 8 lines
Quoted 8 lines
hw/intc/spapr_xive_hcall.c new file +8 -0
+    }
+
+    /* The generation bit for the EQ starts at 1 and The EQ page
+     * offset counter starts at 0.
+     */
+    eq.w1 = EQ_W1_GENERATION | SETFIELD(EQ_W1_PAGE_OFF, 0ul, 0ul);
+    eq.w0 |= EQ_W0_VALID;
+
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 544

Quoted 5 lines
hw/intc/spapr_xive_hcall.c new file +5 -0
+    /* TODO: issue syncs required to ensure all in-flight interrupts
+     * are complete on the old EQ */
+
+    /* Update EQ */
+    *old_eq = eq;

Hrm. The BQL probably saves you, but in general do you need to make sure the ENQUEUE bit is set after updating everything else?

There is a rather complex procedure to update the HW, cache and memory. See xive_eqc_cache_update() in OPAL. I will need to dig in for the PowerNV support ...

Quoted 86 lines
Quoted 86 lines
hw/intc/spapr_xive_hcall.c new file +86 -0
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
+ * target and priority.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      Bit 63: Debug: Return debug data
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ *
+ * Output:
+ * - R4: "flags":
+ *       Bits 0-62: Reserved
+ *       Bit 63: The value of Unconditional Notify (n) per the XIVE spec
+ * - R5: The logical real address of the start of the EQ
+ * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
+ * - R7: The value of Event Queue Offset Counter per XIVE spec
+ *       if "Debug" = 1, else 0
+ *
+ */
+
+#define XIVE_EQ_DEBUG     (1ull << (63 - 63))
+
+static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
+                                           sPAPRMachineState *spapr,
+                                           target_ulong opcode,
+                                           target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    uint32_t eq_idx;
+    XiveEQ *eq;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_EQ_DEBUG) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq)  {
+        return H_HARDWARE;
+    }
+
+    args[0] = 0;
+    if (eq->w0 & EQ_W0_UCOND_NOTIFY) {
+        args[0] |= XIVE_EQ_ALWAYS_NOTIFY;
+    }
+
+    if (eq->w0 & EQ_W0_ENQUEUE) {
+        args[1] =
+            (((uint64_t)(eq->w2 & 0x0fffffff)) << 32) | eq->w3;
+        args[2] = GETFIELD(EQ_W0_QSIZE, eq->w0) + 12;
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 636

Quoted 6 lines
hw/intc/spapr_xive_hcall.c new file +6 -0
+    } else {
+        args[1] = 0;
+        args[2] = 0;
+    }
+
+    /* TODO: do we need any locking on the EQ ? */

Probably not if you're designating it as protected by the BQL.

OK.

Thanks,

C.

Quoted 302 lines
Quoted 301 lines
hw/intc/spapr_xive_hcall.c new file +249 -0
+    if (flags & XIVE_EQ_DEBUG) {
+        /* Load the event queue generation number into the return flags */
+        args[0] |= GETFIELD(EQ_W1_GENERATION, eq->w1);
+
+        /* Load R7 with the event queue offset counter */
+        args[3] = GETFIELD(EQ_W1_PAGE_OFF, eq->w1);
+    }
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
+ * reporting cache line pair for the calling thread.  The reporting
+ * cache lines will contain the OS interrupt context when the OS
+ * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
+ * interrupt. The reporting cache lines can be reset by inputting -1
+ * in "reportingLine".  Issuing the CI store byte without reporting
+ * cache lines registered will result in the data not being accessible
+ * to the OS.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "reportingLine": The logical real address of the reporting cache
+ *    line pair
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
+                                                sPAPRMachineState *spapr,
+                                                target_ulong opcode,
+                                                target_ulong *args)
+{
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* TODO: H_INT_SET_OS_REPORTING_LINE */
+    return H_FUNCTION;
+}
+
+/*
+ * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
+ * real address of the reporting cache line pair set for the input
+ * "target".  If no reporting cache line pair has been set, -1 is
+ * returned.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "reportingLine": The logical real address of the reporting cache
+ *   line pair
+ *
+ * Output:
+ * - R4: The logical real address of the reporting line if set, else -1
+ */
+static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
+                                                sPAPRMachineState *spapr,
+                                                target_ulong opcode,
+                                                target_ulong *args)
+{
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* TODO: H_INT_GET_OS_REPORTING_LINE */
+    return H_FUNCTION;
+}
+
+/*
+ * The H_INT_ESB hcall() is used to issue a load or store to the ESB
+ * page for the input "lisn".  This hcall is only supported for LISNs
+ * that have the ESB hcall flag set to 1 when returned from hcall()
+ * H_INT_GET_SOURCE_INFO.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      bit 63: Store: Store=1, store operation, else load operation
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ * - "esbOffset" is the offset into the ESB page for the load or store operation
+ * - "storeData" is the data to write for a store operation
+ *
+ * Output:
+ * - R4: R4: The value of the load if load operation, else -1
+ */
+
+#define XIVE_ESB_STORE (1ull << (63 - 63))
+
+static target_ulong h_int_esb(PowerPCCPU *cpu,
+                              sPAPRMachineState *spapr,
+                              target_ulong opcode,
+                              target_ulong *args)
+{
+    sPAPRXive *xive = spapr->xive;
+    XiveIVE *ive;
+    target_ulong flags   = args[0];
+    target_ulong lisn    = args[1];
+    target_ulong offset  = args[2];
+    target_ulong data    = args[3];
+    uint64_t esb_base;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_ESB_STORE) {
+        return H_PARAMETER;
+    }
+
+    ive = spapr_xive_get_ive(xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    if (offset > (1ull << xive->esb_shift)) {
+        return H_P3;
+    }
+
+    esb_base = (uint64_t)xive->esb_base + (1ull << xive->esb_shift) * lisn;
+    esb_base += offset;
+
+    if (dma_memory_rw(&address_space_memory, esb_base, &data, 8,
+                      (flags & XIVE_ESB_STORE))) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to rw data @0x%"
+                      HWADDR_PRIx "\n", __func__, esb_base);
+        return H_HARDWARE;
+    }
+    args[0] = (flags & XIVE_ESB_STORE) ? -1 : data;
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SYNC hcall() is used to issue hardware syncs that will
+ * ensure any in flight events for the input lisn are in the event
+ * queue.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_sync(PowerPCCPU *cpu,
+                               sPAPRMachineState *spapr,
+                               target_ulong opcode,
+                               target_ulong *args)
+{
+    XiveIVE *ive;
+    target_ulong flags   = args[0];
+    target_ulong lisn    = args[1];
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* This is not real hardware. Nothing to be done */
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_RESET hcall() is used to reset all of the partition's
+ * interrupt exploitation structures to their initial state.  This
+ * means losing all previously set interrupt state set via
+ * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_reset(PowerPCCPU *cpu,
+                                sPAPRMachineState *spapr,
+                                target_ulong opcode,
+                                target_ulong *args)
+{
+    target_ulong flags   = args[0];
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    spapr_xive_reset(spapr->xive);
+    return H_SUCCESS;
+}
+
+void spapr_xive_hcall_init(sPAPRMachineState *spapr)
+{
+    spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
+    spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
+    spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
+    spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
+    spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
+    spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
+    spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
+                             h_int_set_os_reporting_line);
+    spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
+                             h_int_get_os_reporting_line);
+    spapr_register_hypercall(H_INT_ESB, h_int_esb);
+    spapr_register_hypercall(H_INT_SYNC, h_int_sync);
+    spapr_register_hypercall(H_INT_RESET, h_int_reset);
+}
hw/ppc/spapr.c +2 -0
index ca4e72187f60..8b15c0b500d0 100644
@@ -222,6 +222,8 @@ static sPAPRXive *spapr_xive_create(sPAPRMachineState *spapr, int nr_irqs,
         goto error;
     }
 
+    spapr_xive_hcall_init(spapr);
+
     return SPAPR_XIVE(obj);
 error:
     error_propagate(errp, local_err);
include/hw/ppc/spapr.h +14 -1
index 90e2b0f6c678..a25e218b34e2 100644
@@ -387,7 +387,20 @@ struct sPAPRMachineState {
 #define H_INVALIDATE_PID        0x378
 #define H_REGISTER_PROC_TBL     0x37C
 #define H_SIGNAL_SYS_RESET      0x380
-#define MAX_HCALL_OPCODE        H_SIGNAL_SYS_RESET
+
+#define H_INT_GET_SOURCE_INFO   0x3A8
+#define H_INT_SET_SOURCE_CONFIG 0x3AC
+#define H_INT_GET_SOURCE_CONFIG 0x3B0
+#define H_INT_GET_QUEUE_INFO    0x3B4
+#define H_INT_SET_QUEUE_CONFIG  0x3B8
+#define H_INT_GET_QUEUE_CONFIG  0x3BC
+#define H_INT_SET_OS_REPORTING_LINE 0x3C0
+#define H_INT_GET_OS_REPORTING_LINE 0x3C4
+#define H_INT_ESB               0x3C8
+#define H_INT_SYNC              0x3CC
+#define H_INT_RESET             0x3D0
+
+#define MAX_HCALL_OPCODE        H_INT_RESET
 
 /* The hcalls above are standardized in PAPR and implemented by pHyp
  * as well.
include/hw/ppc/spapr_xive.h +4 -0
index 6e8a189e723f..3f822220647f 100644
@@ -79,4 +79,8 @@ bool spapr_xive_irq_unset(sPAPRXive *xive, uint32_t lisn);
 void spapr_xive_pic_print_info(sPAPRXive *xive, Monitor *mon);
 void spapr_xive_icp_pic_print_info(sPAPRXiveICP *xicp, Monitor *mon);
 
+typedef struct sPAPRMachineState sPAPRMachineState;
+
+void spapr_xive_hcall_init(sPAPRMachineState *spapr);
+
 #endif /* PPC_SPAPR_XIVE_H */
#109 lore raw

Re: [Qemu-devel] [PATCH 19/25] spapr: add hcalls support for the XIVE interrupt mode

Quoted 156 lines · Cédric Le Goater
Quoted 130 lines · David Gibson
Quoted 129 lines · Cédric Le Goater

A set of Hypervisor's call are used to configure the interrupt sources and the event/notification queues of the guest:

- H_INT_GET_SOURCE_INFO

used to obtain the address of the MMIO page of the Event State Buffer (PQ bits) entry associated with the source.

- H_INT_SET_SOURCE_CONFIG

assigns a source to a "target".

- H_INT_GET_SOURCE_CONFIG

determines to which "target" and "priority" is assigned to a source

- H_INT_GET_QUEUE_INFO

returns the address of the notification management page associated with the specified "target" and "priority".

- H_INT_SET_QUEUE_CONFIG

sets or resets the event queue for a given "target" and "priority". It is also used to set the notification config associated with the queue, only unconditional notification for the moment. Reset is performed with a queue size of 0 and queueing is disabled in that case.

- H_INT_GET_QUEUE_CONFIG

returns the queue settings for a given "target" and "priority".

- H_INT_RESET

resets all of the partition's interrupt exploitation structures to their initial state, losing all configuration set via the hcalls H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.

- H_INT_SYNC

issue a synchronisation on a source to make sure sure all notifications have reached their queue.

Calls that still need to be addressed :

H_INT_SET_OS_REPORTING_LINE
H_INT_GET_OS_REPORTING_LINE

See the code for more documentation on each hcall.

---
 hw/intc/Makefile.objs       |   2 +-
 hw/intc/spapr_xive_hcall.c  | 885 ++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr.c              |   2 +
 include/hw/ppc/spapr.h      |  15 +-
 include/hw/ppc/spapr_xive.h |   4 +
 5 files changed, 906 insertions(+), 2 deletions(-)
 create mode 100644 hw/intc/spapr_xive_hcall.c
hw/intc/Makefile.objs +1 -1
index 49e13e7aeeee..122e2ec77e8d 100644
@@ -35,7 +35,7 @@ obj-$(CONFIG_SH4) += sh_intc.o
 obj-$(CONFIG_XICS) += xics.o
 obj-$(CONFIG_XICS_SPAPR) += xics_spapr.o
 obj-$(CONFIG_XICS_KVM) += xics_kvm.o
-obj-$(CONFIG_XIVE_SPAPR) += spapr_xive.o
+obj-$(CONFIG_XIVE_SPAPR) += spapr_xive.o spapr_xive_hcall.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
hw/intc/spapr_xive_hcall.c new file +49 -0
new file mode 100644
index 000000000000..676fe0e2d5c7
@@ -0,0 +1,885 @@
+/*
+ * QEMU PowerPC sPAPR XIVE model
+ *
+ * Copyright (c) 2017, IBM Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "cpu.h"
+#include "hw/ppc/spapr.h"
+#include "hw/ppc/spapr_xive.h"
+#include "hw/ppc/fdt.h"
+#include "monitor/monitor.h"
+
+#include "xive-internal.h"
+
+/* Priority ranges reserved by the hypervisor. The Linux driver is
+ * expected to choose priority 6.
+ */
+static const uint32_t reserved_priorities[] = {
+    7,    /* start */
+    0xf8, /* count */
+};
+
+static bool priority_is_valid(uint32_t priority)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(reserved_priorities) / 2; i++) {
+        uint32_t base  = reserved_priorities[2 * i];
+        uint32_t count = reserved_priorities[2 * i + 1];
+
+        if (priority >= base && priority < base + count) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: priority %d is reserved\n",
+                          __func__, priority);
+            return false;
+        }
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 53

Quoted 4 lines
hw/intc/spapr_xive_hcall.c new file +4 -0
+    }
+
+    return true;
+}

This seems like overkill. Aren't there only 0..7 levels supported in hardware, in which case a one byte bitmap will suffice to store the reserved levels.

I was trying the use the same array that will be exposed in the device tree in the "ibm,plat-res-int-priorities" property, defined as follow in PAPR:

property name that designates to the client program that the platform has reserved one or more interrupt priorities for its own use.

prop-encoded-value: one or more (interrupt priority, range) pairs, where interrupt priority is a single cell hexidec- imal number between 0x00 and 0xFF, and range is an integer encoded as with encode-int that represents the number of contiguous interrupt priorities that have been reserved by the platform for its internal use.

Quoted 1 line

But I agree, it's a bit overkill to check for 0..7 levels ...

Ok, I do see the point here. Hmm.. not sure where best to go with this. One source of data is always good, and this is probably less complex than deriving the DT list from a bitmap.

On the other hand I am wary these days of over-generalizing, since it can lead to nightmares for migration consistency.

Quoted 8 lines
Quoted 3 lines

To check my understanding again, if you're running this with KVM, the host kernel and qemu will need to agree on which are the reserved levels, yes?

Hmm, these values are quite static. So I don't think there will be any sort of exchange between KVM and QEMU to define the range to expose to the guest.

Quoted 6 lines

For the moment, Linux only uses one priority, the lowest, and Ben has introduced in OPAL an automatic interrupt escalation feature using queue 7 for all other queues (DD2.0 cpus). So we only expose range 0..6 to the guest for this purpose.

So we agreed orally.

Ok, that's fine, just making sure I understand the situation.

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
hw/intc/spapr_xive_hcall.c new file +7 -0
+static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
+                                          sPAPRMachineState *spapr,
+                                          target_ulong opcode,
+                                          target_ulong *args)
+{
+    sPAPRXive *xive = spapr->xive;
+    XiveIVE *ive;
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 96

Quoted 6 lines
hw/intc/spapr_xive_hcall.c new file +6 -0
+    target_ulong flags  = args[0];
+    target_ulong lisn   = args[1];
+    uint64_t mmio_base;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;

Is H_FUNCTION required by the PAPR ACRs here?

Quoted 6 lines

yes. quoting the specs :

	/* H_Function: The calling OS is not in exploitation mode */

I need to review once more all of the return errors but, last time I checked they looked sane.

Ok.

Quoted 24 lines
Quoted 15 lines

Usually we only use
H_FUNCTION if the hypercall doesn't exist at all, and if unavailable for other reasons use H_AUTHORITY or something.

Quoted 11 lines
hw/intc/spapr_xive_hcall.c new file +11 -0
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 113

Quoted 6 lines
hw/intc/spapr_xive_hcall.c new file +6 -0
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    mmio_base = (uint64_t)xive->esb_base + (1ull << xive->esb_shift) * lisn;

Hrm.. why was xive->esb_base not already a u64?

Quoted 1 line

its an 'hwaddr'. Yes I can remove it.

Right. mmio_base should be a hwaddr too, so you shouldn't need a cast.

Quoted 25 lines
Quoted 9 lines

hw/intc/spapr_xive_hcall.c line 123

Quoted 9 lines
hw/intc/spapr_xive_hcall.c new file +9 -0
+    args[0] = 0;
+    if (spapr_xive_irq_is_lsi(xive, lisn)) {
+        args[0] |= XIVE_SRC_LSI;
+    }
+    if (xive->flags & XIVE_SRC_TRIGGER) {
+        args[0] |= XIVE_SRC_TRIGGER;
+    }
+
+    if (xive->flags & XIVE_SRC_H_INT_ESB) {

btw, this is why I have the ->flags field. Do you still want me to remove it ? because I would like to keep the logic below. No big deal if not.

Quoted 11 lines
Quoted 10 lines
hw/intc/spapr_xive_hcall.c new file +10 -0
+        args[1] = -1; /* never used in QEMU  */
+        args[2] = -1;
+    } else {
+        args[1] = mmio_base;
+        if (xive->flags & XIVE_SRC_TRIGGER) {
+            args[2] = -1; /* No specific trigger page */
+        } else {
+            args[2] = -1; /* TODO: support for specific trigger page */
+        }
+    }
Quoted 5 lines
Quoted 1 line

What does the availability of SRC_TRIGGER (and INT_ESB) depend on?

The CPU revision. But we won't introduce XIVE exploitation mode on anything else than DD2.0 which has full XIVE support. Even STORE_EOI that we should be adding.

Hrm. Host CPU? That's a problem - if guest visible properties like this vary with the host CPU, migration breaks.

Quoted 6 lines
Quoted 3 lines

If it varies with host capabilities, that's going to be real pain for migration.

Yes. I am not aware of any future extension but I agree this is something we need to keep an eye on.

I'm not talking about future extension, I'm meaning right now.

Quoted 50 lines
Quoted 41 lines
Quoted 41 lines
hw/intc/spapr_xive_hcall.c new file +40 -0
+
+    args[3] = xive->esb_shift;
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
+ * Interrupt Source to a target. The Logical Interrupt Source is
+ * designated with the "lisn" parameter and the target is designated
+ * with the "target" and "priority" parameters.  Upon return from the
+ * hcall(), no additional interrupts will be directed to the old EQ.
+ *
+ * TODO: The old EQ should be investigated for interrupts that
+ * occurred prior to or during the hcall().
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-61: Reserved
+ *      Bit 62: set the "eisn" in the EA
+ *      Bit 63: masks the interrupt source in the hardware interrupt
+ *      control structure. An interrupt masked by this mechanism will
+ *      be dropped, but it's source state bits will still be
+ *      set. There is no race-free way of unmasking and restoring the
+ *      source. Thus this should only be used in interrupts that are
+ *      also masked at the source, and only in cases where the
+ *      interrupt is not meant to be used for a large amount of time
+ *      because no valid target exists for it for example
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as returned by
+ *      the H_ALLOCATE_VAS_WINDOW hcall
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *      "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *      "ibm,plat-res-int-priorities"
+ * - "eisn" is the guest EISN associated with the "lisn"
+ *
+ * Output:
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 178

Quoted 5 lines
hw/intc/spapr_xive_hcall.c new file +5 -0
+ * - None
+ */
+
+#define XIVE_SRC_SET_EISN (1ull << (63 - 62))
+#define XIVE_SRC_MASK     (1ull << (63 - 63))

Aren't there already a bunch of macros you have for defining things in terms of IBM bit numbers, so you can avoid open coding (63 - whatever).

Quoted 3 lines

Yes.

On that topic, could we include the PPC_BIT* macros somewhere under ppc ?

Uh, sure, why not. target/ppc/cpu.h seems the logical place.

Quoted 796 lines
Quoted 49 lines
Quoted 49 lines
hw/intc/spapr_xive_hcall.c new file +49 -0
+
+static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
+                                            sPAPRMachineState *spapr,
+                                            target_ulong opcode,
+                                            target_ulong *args)
+{
+    XiveIVE *ive;
+    uint64_t new_ive;
+    target_ulong flags    = args[0];
+    target_ulong lisn     = args[1];
+    target_ulong target   = args[2];
+    target_ulong priority = args[3];
+    target_ulong eisn     = args[4];
+    uint32_t eq_idx;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~(XIVE_SRC_SET_EISN | XIVE_SRC_MASK)) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    /* priority 0xff is used to reset the IVE */
+    if (priority == 0xff) {
+        new_ive = IVE_VALID | IVE_MASKED;
+        goto out;
+    }
+
+    new_ive = ive->w;
+
+    if (flags & XIVE_SRC_MASK) {
+        new_ive = ive->w | IVE_MASKED;
+    } else {
+        new_ive = ive->w & ~IVE_MASKED;
+    }
+
+    if (!priority_is_valid(priority)) {
+        return H_P4;
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 233

Quoted 6 lines
hw/intc/spapr_xive_hcall.c new file +6 -0
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */

What's this about?

That is from the specs and I haven't quite figured out what it meant. I need to ask.

Quoted 2 lines

I thought the point of XIVE was you could set up
target queues for your vcpus regardless of mapping to physical cpus.

yes.

Quoted 292 lines
Quoted 292 lines
hw/intc/spapr_xive_hcall.c new file +292 -0
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P3;
+    }
+
+    new_ive = SETFIELD(IVE_EQ_BLOCK, new_ive, 0ul);
+    new_ive = SETFIELD(IVE_EQ_INDEX, new_ive, eq_idx);
+
+    if (flags & XIVE_SRC_SET_EISN) {
+        new_ive = SETFIELD(IVE_EQ_DATA, new_ive, eisn);
+    }
+
+out:
+    /* TODO: handle syncs ? */
+
+    /* And update */
+    ive->w = new_ive;
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
+ * target/priority pair is assigned to the specified Logical Interrupt
+ * Source.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63 Reserved
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ *
+ * Output:
+ * - R4: Target to which the specified Logical Interrupt Source is
+ *       assigned
+ * - R5: Priority to which the specified Logical Interrupt Source is
+ *       assigned
+ * - R6: EISN for the specified Logical Interrupt Source (this will be
+ *       equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
+ */
+static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
+                                            sPAPRMachineState *spapr,
+                                            target_ulong opcode,
+                                            target_ulong *args)
+{
+    target_ulong flags = args[0];
+    target_ulong lisn = args[1];
+    XiveIVE *ive;
+    XiveEQ *eq;
+    uint32_t eq_idx;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    eq_idx = GETFIELD(IVE_EQ_INDEX, ive->w);
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq) {
+        return H_HARDWARE;
+    }
+
+    args[0] = GETFIELD(EQ_W6_NVT_INDEX, eq->w6);
+
+    if (ive->w & IVE_MASKED) {
+        args[1] = 0xff;
+    } else {
+        args[1] = GETFIELD(EQ_W7_F0_PRIORITY, eq->w7);
+    }
+
+    args[2] = GETFIELD(IVE_EQ_DATA, ive->w);
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
+ * address of the notification management page associated with the
+ * specified target and priority.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *       Bits 0-63 Reserved
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ *
+ * Output:
+ * - R4: Logical real address of notification page
+ * - R5: Power of 2 page size of the notification page
+ */
+static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
+                                         sPAPRMachineState *spapr,
+                                         target_ulong opcode,
+                                         target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    uint32_t eq_idx;
+    XiveEQ *eq;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq)  {
+        return H_HARDWARE;
+    }
+
+    args[0] = -1; /* TODO: return ESn page */
+    if (eq->w0 & EQ_W0_ENQUEUE) {
+        args[1] = GETFIELD(EQ_W0_QSIZE, eq->w0) + 12;
+    } else {
+        args[1] = 0;
+    }
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
+ * a given "target" and "priority".  It is also used to set the
+ * notification config associated with the EQ.  An EQ size of 0 is
+ * used to reset the EQ config for a given target and priority. If
+ * resetting the EQ config, the END associated with the given "target"
+ * and "priority" will be changed to disable queueing.
+ *
+ * Upon return from the hcall(), no additional interrupts will be
+ * directed to the old EQ (if one was set). The old EQ (if one was
+ * set) should be investigated for interrupts that occurred prior to
+ * or during the hcall().
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      Bit 63: Unconditional Notify (n) per the XIVE spec
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ * - "eventQueue": The logical real address of the start of the EQ
+ * - "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
+ *
+ * Output:
+ * - None
+ */
+
+#define XIVE_EQ_ALWAYS_NOTIFY (1ull << (63 - 63))
+
+static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
+                                           sPAPRMachineState *spapr,
+                                           target_ulong opcode,
+                                           target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    target_ulong qpage    = args[3];
+    target_ulong qsize    = args[4];
+    uint32_t eq_idx;
+    XiveEQ *old_eq;
+    XiveEQ eq;
+    uint32_t qdata;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_EQ_ALWAYS_NOTIFY) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    old_eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!old_eq)  {
+        return H_HARDWARE;
+    }
+
+    eq = *old_eq;
+
+    switch (qsize) {
+    case 12:
+    case 16:
+    case 21:
+    case 24:
+        eq.w3 = ((uint64_t)qpage) & 0xffffffff;
+        eq.w2 = (((uint64_t)qpage)) >> 32 & 0x0fffffff;
+        eq.w0 |= EQ_W0_ENQUEUE;
+        eq.w0 = SETFIELD(EQ_W0_QSIZE, eq.w0, qsize - 12);
+        break;
+    case 0:
+        /* reset queue and disable queueing */
+        eq.w2 = eq.w3 = 0;
+        eq.w0 &= ~EQ_W0_ENQUEUE;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid EQ size %"PRIx64"\n",
+                      __func__, qsize);
+        return H_P5;
+    }
+
+    if (qsize) {
+        /*
+         * Let's validate the EQ address with a read of the first EQ
+         * entry. We could also check that the full queue has been
+         * zeroed by the OS.
+         */
+        if (address_space_read(&address_space_memory, qpage,
+                               MEMTXATTRS_UNSPECIFIED,
+                               (uint8_t *) &qdata, sizeof(qdata))) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to read EQ data @0x%"
+                          HWADDR_PRIx "\n", __func__, qpage);
+            return H_P4;
+        }
+    }
+
+    /* Ensure the priority and target are correctly set (they will not
+     * be right after allocation)
+     */
+    eq.w6 = SETFIELD(EQ_W6_NVT_BLOCK, 0ul, 0ul) |
+        SETFIELD(EQ_W6_NVT_INDEX, 0ul, target);
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 531

Quoted 5 lines
hw/intc/spapr_xive_hcall.c new file +5 -0
+    eq.w7 = SETFIELD(EQ_W7_F0_PRIORITY, 0ul, priority);
+
+    /* TODO: depends on notitification page (ESn) from H_INT_GET_QUEUE_INFO */
+    if (flags & XIVE_EQ_ALWAYS_NOTIFY) {
+        eq.w0 |= EQ_W0_UCOND_NOTIFY;

Do you need to also clear if the flag is not set? AFAICT eq.w0 is inherited from teh old queue and enver reset from scratch.

True. It is always on if the EQ is not reseted. I also need to be more precise in spapr_xive_irq() when dealing with the reseted EQs. The model has not fallen in to that trap yet.

Quoted 8 lines
Quoted 8 lines
hw/intc/spapr_xive_hcall.c new file +8 -0
+    }
+
+    /* The generation bit for the EQ starts at 1 and The EQ page
+     * offset counter starts at 0.
+     */
+    eq.w1 = EQ_W1_GENERATION | SETFIELD(EQ_W1_PAGE_OFF, 0ul, 0ul);
+    eq.w0 |= EQ_W0_VALID;
+
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 544

Quoted 5 lines
hw/intc/spapr_xive_hcall.c new file +5 -0
+    /* TODO: issue syncs required to ensure all in-flight interrupts
+     * are complete on the old EQ */
+
+    /* Update EQ */
+    *old_eq = eq;

Hrm. The BQL probably saves you, but in general do you need to make sure the ENQUEUE bit is set after updating everything else?

There is a rather complex procedure to update the HW, cache and memory. See xive_eqc_cache_update() in OPAL. I will need to dig in for the PowerNV support ...

Quoted 86 lines
Quoted 86 lines
hw/intc/spapr_xive_hcall.c new file +86 -0
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
+ * target and priority.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      Bit 63: Debug: Return debug data
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "priority" is a valid priority not in
+ *       "ibm,plat-res-int-priorities"
+ *
+ * Output:
+ * - R4: "flags":
+ *       Bits 0-62: Reserved
+ *       Bit 63: The value of Unconditional Notify (n) per the XIVE spec
+ * - R5: The logical real address of the start of the EQ
+ * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
+ * - R7: The value of Event Queue Offset Counter per XIVE spec
+ *       if "Debug" = 1, else 0
+ *
+ */
+
+#define XIVE_EQ_DEBUG     (1ull << (63 - 63))
+
+static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
+                                           sPAPRMachineState *spapr,
+                                           target_ulong opcode,
+                                           target_ulong *args)
+{
+    target_ulong flags    = args[0];
+    target_ulong target   = args[1];
+    target_ulong priority = args[2];
+    uint32_t eq_idx;
+    XiveEQ *eq;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_EQ_DEBUG) {
+        return H_PARAMETER;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    if (!priority_is_valid(priority)) {
+        return H_P3;
+    }
+
+    /* Validate that "target" is part of the list of threads allocated
+     * to the partition. For that, find the EQ corresponding to the
+     * target.
+     */
+    if (!spapr_xive_eq_for_server(spapr->xive, target, priority, &eq_idx)) {
+        return H_P2;
+    }
+
+    /* TODO: If the partition thread count is greater than the
+     * hardware thread count, validate the "target" has a
+     * corresponding hardware thread else return H_NOT_AVAILABLE.
+     */
+
+    eq = spapr_xive_get_eq(spapr->xive, eq_idx);
+    if (!eq)  {
+        return H_HARDWARE;
+    }
+
+    args[0] = 0;
+    if (eq->w0 & EQ_W0_UCOND_NOTIFY) {
+        args[0] |= XIVE_EQ_ALWAYS_NOTIFY;
+    }
+
+    if (eq->w0 & EQ_W0_ENQUEUE) {
+        args[1] =
+            (((uint64_t)(eq->w2 & 0x0fffffff)) << 32) | eq->w3;
+        args[2] = GETFIELD(EQ_W0_QSIZE, eq->w0) + 12;
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 636

Quoted 6 lines
hw/intc/spapr_xive_hcall.c new file +6 -0
+    } else {
+        args[1] = 0;
+        args[2] = 0;
+    }
+
+    /* TODO: do we need any locking on the EQ ? */

Probably not if you're designating it as protected by the BQL.

OK.

Thanks,

C.

Quoted 302 lines
Quoted 301 lines
hw/intc/spapr_xive_hcall.c new file +249 -0
+    if (flags & XIVE_EQ_DEBUG) {
+        /* Load the event queue generation number into the return flags */
+        args[0] |= GETFIELD(EQ_W1_GENERATION, eq->w1);
+
+        /* Load R7 with the event queue offset counter */
+        args[3] = GETFIELD(EQ_W1_PAGE_OFF, eq->w1);
+    }
+
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
+ * reporting cache line pair for the calling thread.  The reporting
+ * cache lines will contain the OS interrupt context when the OS
+ * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
+ * interrupt. The reporting cache lines can be reset by inputting -1
+ * in "reportingLine".  Issuing the CI store byte without reporting
+ * cache lines registered will result in the data not being accessible
+ * to the OS.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "reportingLine": The logical real address of the reporting cache
+ *    line pair
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
+                                                sPAPRMachineState *spapr,
+                                                target_ulong opcode,
+                                                target_ulong *args)
+{
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* TODO: H_INT_SET_OS_REPORTING_LINE */
+    return H_FUNCTION;
+}
+
+/*
+ * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
+ * real address of the reporting cache line pair set for the input
+ * "target".  If no reporting cache line pair has been set, -1 is
+ * returned.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "target" is per "ibm,ppc-interrupt-server#s" or
+ *       "ibm,ppc-interrupt-gserver#s"
+ * - "reportingLine": The logical real address of the reporting cache
+ *   line pair
+ *
+ * Output:
+ * - R4: The logical real address of the reporting line if set, else -1
+ */
+static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
+                                                sPAPRMachineState *spapr,
+                                                target_ulong opcode,
+                                                target_ulong *args)
+{
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* TODO: H_INT_GET_OS_REPORTING_LINE */
+    return H_FUNCTION;
+}
+
+/*
+ * The H_INT_ESB hcall() is used to issue a load or store to the ESB
+ * page for the input "lisn".  This hcall is only supported for LISNs
+ * that have the ESB hcall flag set to 1 when returned from hcall()
+ * H_INT_GET_SOURCE_INFO.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-62: Reserved
+ *      bit 63: Store: Store=1, store operation, else load operation
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ * - "esbOffset" is the offset into the ESB page for the load or store operation
+ * - "storeData" is the data to write for a store operation
+ *
+ * Output:
+ * - R4: R4: The value of the load if load operation, else -1
+ */
+
+#define XIVE_ESB_STORE (1ull << (63 - 63))
+
+static target_ulong h_int_esb(PowerPCCPU *cpu,
+                              sPAPRMachineState *spapr,
+                              target_ulong opcode,
+                              target_ulong *args)
+{
+    sPAPRXive *xive = spapr->xive;
+    XiveIVE *ive;
+    target_ulong flags   = args[0];
+    target_ulong lisn    = args[1];
+    target_ulong offset  = args[2];
+    target_ulong data    = args[3];
+    uint64_t esb_base;
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags & ~XIVE_ESB_STORE) {
+        return H_PARAMETER;
+    }
+
+    ive = spapr_xive_get_ive(xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    if (offset > (1ull << xive->esb_shift)) {
+        return H_P3;
+    }
+
+    esb_base = (uint64_t)xive->esb_base + (1ull << xive->esb_shift) * lisn;
+    esb_base += offset;
+
+    if (dma_memory_rw(&address_space_memory, esb_base, &data, 8,
+                      (flags & XIVE_ESB_STORE))) {
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to rw data @0x%"
+                      HWADDR_PRIx "\n", __func__, esb_base);
+        return H_HARDWARE;
+    }
+    args[0] = (flags & XIVE_ESB_STORE) ? -1 : data;
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_SYNC hcall() is used to issue hardware syncs that will
+ * ensure any in flight events for the input lisn are in the event
+ * queue.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ * - "lisn" is per "interrupts", "interrupt-map", or
+ *      "ibm,xive-lisn-ranges" properties, or as returned by the
+ *      ibm,query-interrupt-source-number RTAS call, or as
+ *      returned by the H_ALLOCATE_VAS_WINDOW hcall
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_sync(PowerPCCPU *cpu,
+                               sPAPRMachineState *spapr,
+                               target_ulong opcode,
+                               target_ulong *args)
+{
+    XiveIVE *ive;
+    target_ulong flags   = args[0];
+    target_ulong lisn    = args[1];
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    ive = spapr_xive_get_ive(spapr->xive, lisn);
+    if (!ive || !(ive->w & IVE_VALID)) {
+        return H_P2;
+    }
+
+    /*
+     * H_STATE should be returned if a H_INT_RESET is in progress.
+     * This is not needed when running the emulation under QEMU
+     */
+
+    /* This is not real hardware. Nothing to be done */
+    return H_SUCCESS;
+}
+
+/*
+ * The H_INT_RESET hcall() is used to reset all of the partition's
+ * interrupt exploitation structures to their initial state.  This
+ * means losing all previously set interrupt state set via
+ * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
+ *
+ * Parameters:
+ * Input:
+ * - "flags"
+ *      Bits 0-63: Reserved
+ *
+ * Output:
+ * - None
+ */
+static target_ulong h_int_reset(PowerPCCPU *cpu,
+                                sPAPRMachineState *spapr,
+                                target_ulong opcode,
+                                target_ulong *args)
+{
+    target_ulong flags   = args[0];
+
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return H_FUNCTION;
+    }
+
+    if (flags) {
+        return H_PARAMETER;
+    }
+
+    spapr_xive_reset(spapr->xive);
+    return H_SUCCESS;
+}
+
+void spapr_xive_hcall_init(sPAPRMachineState *spapr)
+{
+    spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
+    spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
+    spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
+    spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
+    spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
+    spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
+    spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
+                             h_int_set_os_reporting_line);
+    spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
+                             h_int_get_os_reporting_line);
+    spapr_register_hypercall(H_INT_ESB, h_int_esb);
+    spapr_register_hypercall(H_INT_SYNC, h_int_sync);
+    spapr_register_hypercall(H_INT_RESET, h_int_reset);
+}
hw/ppc/spapr.c +2 -0
index ca4e72187f60..8b15c0b500d0 100644
@@ -222,6 +222,8 @@ static sPAPRXive *spapr_xive_create(sPAPRMachineState *spapr, int nr_irqs,
         goto error;
     }
 
+    spapr_xive_hcall_init(spapr);
+
     return SPAPR_XIVE(obj);
 error:
     error_propagate(errp, local_err);
include/hw/ppc/spapr.h +14 -1
index 90e2b0f6c678..a25e218b34e2 100644
@@ -387,7 +387,20 @@ struct sPAPRMachineState {
 #define H_INVALIDATE_PID        0x378
 #define H_REGISTER_PROC_TBL     0x37C
 #define H_SIGNAL_SYS_RESET      0x380
-#define MAX_HCALL_OPCODE        H_SIGNAL_SYS_RESET
+
+#define H_INT_GET_SOURCE_INFO   0x3A8
+#define H_INT_SET_SOURCE_CONFIG 0x3AC
+#define H_INT_GET_SOURCE_CONFIG 0x3B0
+#define H_INT_GET_QUEUE_INFO    0x3B4
+#define H_INT_SET_QUEUE_CONFIG  0x3B8
+#define H_INT_GET_QUEUE_CONFIG  0x3BC
+#define H_INT_SET_OS_REPORTING_LINE 0x3C0
+#define H_INT_GET_OS_REPORTING_LINE 0x3C4
+#define H_INT_ESB               0x3C8
+#define H_INT_SYNC              0x3CC
+#define H_INT_RESET             0x3D0
+
+#define MAX_HCALL_OPCODE        H_INT_RESET
 
 /* The hcalls above are standardized in PAPR and implemented by pHyp
  * as well.
include/hw/ppc/spapr_xive.h +4 -0
index 6e8a189e723f..3f822220647f 100644
@@ -79,4 +79,8 @@ bool spapr_xive_irq_unset(sPAPRXive *xive, uint32_t lisn);
 void spapr_xive_pic_print_info(sPAPRXive *xive, Monitor *mon);
 void spapr_xive_icp_pic_print_info(sPAPRXiveICP *xicp, Monitor *mon);
 
+typedef struct sPAPRMachineState sPAPRMachineState;
+
+void spapr_xive_hcall_init(sPAPRMachineState *spapr);
+
 #endif /* PPC_SPAPR_XIVE_H */
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#110 lore raw

Re: [Qemu-devel] [PATCH 19/25] spapr: add hcalls support for the XIVE interrupt mode

Quoted 6 lines · David Gibson
Quoted 3 lines

The CPU revision. But we won't introduce XIVE exploitation mode on anything else than DD2.0 which has full XIVE support. Even STORE_EOI that we should be adding.

Hrm. Host CPU? That's a problem - if guest visible properties like this vary with the host CPU, migration breaks.

I don't think this is going to be a problem in practice. The availability of trigger comes from OPAL but in practice, all virtual interrupts are going to support it always, only some of the HW originated ones (PCIe MSIs for example and LSIs) won't. And we don't migrate with PCIe devices passed through.

So the guest need the info, but we should be ok with migration from P9 DD2.0 onwards. Nobody sane cares about P9 DD1.0.

Any future chip will have to ensure that we don't lose that property in HW least we lost migration, but that would break AIX too so I'm reasonably confident the HW guys will get that right ;-)

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

If it varies with host capabilities, that's going to be real pain for migration.

Yes. I am not aware of any future extension but I agree this is something we need to keep an eye on.

I'm not talking about future extension, I'm meaning right now.

No, no issue right now.

Cheers,
Ben.

#111 lore raw

Re: [Qemu-devel] [PATCH 19/25] spapr: add hcalls support for the XIVE interrupt mode

Quoted 11 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson
Quoted 3 lines

The CPU revision. But we won't introduce XIVE exploitation mode on anything else than DD2.0 which has full XIVE support. Even STORE_EOI that we should be adding.

Hrm. Host CPU? That's a problem - if guest visible properties like this vary with the host CPU, migration breaks.

I don't think this is going to be a problem in practice. The availability of trigger comes from OPAL but in practice, all virtual interrupts are going to support it always,

Ok. It still makes me nervous to derive guest visible features from the host. I'd prefer to just hardwire the XIVE model to always/never advertise it and simply fail if that isn't workable for the host kernel.

Quoted 3 lines

only some of the HW
originated ones (PCIe MSIs for example and LSIs) won't. And we don't migrate with PCIe devices passed through.

.. and cross that bridge when we come to it.

Quoted 18 lines

So the guest need the info, but we should be ok with migration from P9 DD2.0 onwards. Nobody sane cares about P9 DD1.0.

Any future chip will have to ensure that we don't lose that property in HW least we lost migration, but that would break AIX too so I'm reasonably confident the HW guys will get that right ;-)

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

If it varies with host capabilities, that's going to be real pain for migration.

Yes. I am not aware of any future extension but I agree this is something we need to keep an eye on.

I'm not talking about future extension, I'm meaning right now.

No, no issue right now.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#112 lore raw

Re: [Qemu-devel] [PATCH 19/25] spapr: add hcalls support for the XIVE interrupt mode

Quoted 9 lines · David Gibson
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson
Quoted 3 lines

The CPU revision. But we won't introduce XIVE exploitation mode on anything else than DD2.0 which has full XIVE support. Even STORE_EOI that we should be adding.

Hrm. Host CPU? That's a problem - if guest visible properties like this vary with the host CPU, migration breaks.

Quoted 7 lines
Quoted 3 lines

I don't think this is going to be a problem in practice. The availability of trigger comes from OPAL but in practice, all virtual interrupts are going to support it always,

Ok. It still makes me nervous to derive guest visible features from the host. I'd prefer to just hardwire the XIVE model to always/never advertise it and simply fail if that isn't workable for the host kernel.

We could fail loudly if we see an migratable interrupt that doesn't have the flag.

Cheers,
Ben.

#113 lore raw

Re: [Qemu-devel] [PATCH 19/25] spapr: add hcalls support for the XIVE interrupt mode

In reply to David Gibson

[ ... ]

Quoted 38 lines
Quoted 38 lines
Quoted 13 lines
Quoted 13 lines
hw/intc/spapr_xive_hcall.c new file +13 -0
+static bool priority_is_valid(uint32_t priority)
+{
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(reserved_priorities) / 2; i++) {
+        uint32_t base  = reserved_priorities[2 * i];
+        uint32_t count = reserved_priorities[2 * i + 1];
+
+        if (priority >= base && priority < base + count) {
+            qemu_log_mask(LOG_GUEST_ERROR, "%s: priority %d is reserved\n",
+                          __func__, priority);
+            return false;
+        }
Quoted 8 lines

hw/intc/spapr_xive_hcall.c line 53

Quoted 4 lines
hw/intc/spapr_xive_hcall.c new file +4 -0
+    }
+
+    return true;
+}

This seems like overkill. Aren't there only 0..7 levels supported in hardware, in which case a one byte bitmap will suffice to store the reserved levels.

I was trying the use the same array that will be exposed in the device tree in the "ibm,plat-res-int-priorities" property, defined as follow in PAPR:

property name that designates to the client program that the platform has reserved one or more interrupt priorities for its own use.

prop-encoded-value: one or more (interrupt priority, range) pairs, where interrupt priority is a single cell hexidec- imal number between 0x00 and 0xFF, and range is an integer encoded as with encode-int that represents the number of contiguous interrupt priorities that have been reserved by the platform for its internal use.

Quoted 8 lines
Quoted 1 line

But I agree, it's a bit overkill to check for 0..7 levels ...

Ok, I do see the point here. Hmm.. not sure where best to go with this. One source of data is always good, and this is probably less complex than deriving the DT list from a bitmap.

On the other hand I am wary these days of over-generalizing, since it can lead to nightmares for migration consistency.

I should be able to simplify by using one array of valid priorities for the guest, and from that build the array of reserved priorities for the platform.

[ ... ]

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ mmio_base = (uint64_t)xive->esb_base + (1ull << xive->esb_shift) * lisn;

Hrm.. why was xive->esb_base not already a u64?

its an 'hwaddr'. Yes I can remove it.

Right. mmio_base should be a hwaddr too, so you shouldn't need a cast.

yes.

[ ... ]

Quoted 10 lines
Quoted 5 lines
Quoted 1 line

What does the availability of SRC_TRIGGER (and INT_ESB) depend on?

The CPU revision. But we won't introduce XIVE exploitation mode on anything else than DD2.0 which has full XIVE support. Even STORE_EOI that we should be adding.

Hrm. Host CPU? That's a problem - if guest visible properties like this vary with the host CPU, migration breaks.

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

If it varies with host capabilities, that's going to be real pain for migration.

Yes. I am not aware of any future extension but I agree this is something we need to keep an eye on.

I'm not talking about future extension, I'm meaning right now.

I think Ben has answered these questions.

[ ... ]

Quoted 3 lines
Quoted 1 line

On that topic, could we include the PPC_BIT* macros somewhere under ppc ?

Uh, sure, why not. target/ppc/cpu.h seems the logical place.

I will send a preliminary patch for 2.12 then.

Thanks,

C.

#114 lore raw

PATCH 20/25 spapr: add device tree support for the XIVE interrupt mode

In reply to Cédric Le Goater

The XIVE interface for the guest is described in the device tree under the "interrupt-controller" node. A couple of new properties are specific to XIVE :

- "reg"

contains the base address and size of the thread interrupt managnement areas (TIMA), also called rings, for the User level and for the Guest OS level. Only the Guest OS level is taken into account today.

- "ibm,xive-eq-sizes"

the size of the event queues. One cell per size supported, contains log2 of size, in ascending order.

- "ibm,xive-lisn-ranges"

the interrupt numbers ranges assigned to the guest. These are allocated using a simple bitmap.

and also under the root node :

- "ibm,plat-res-int-priorities"

contains a list of priorities that the hypervisor has reserved for its own use. Simulate ranges as defined by the PowerVM Hypervisor.

When the XIVE interrupt mode is activated after the CAS negotiation, the machine will perform a reboot to rebuild the device tree.

---
 hw/intc/spapr_xive_hcall.c  | 50 +++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr.c              |  7 ++++++-
 hw/ppc/spapr_hcall.c        |  6 ++++++
 include/hw/ppc/spapr_xive.h |  2 ++
 4 files changed, 64 insertions(+), 1 deletion(-)
hw/intc/spapr_xive_hcall.c +50 -0
index 676fe0e2d5c7..60c6c9f4be8f 100644
@@ -883,3 +883,53 @@ void spapr_xive_hcall_init(sPAPRMachineState *spapr)
     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
     spapr_register_hypercall(H_INT_RESET, h_int_reset);
 }
+
+void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers,
+                         void *fdt, uint32_t phandle)
+{
+    sPAPRXive *xive = spapr->xive;
+    int node;
+    uint64_t timas[2 * 2];
+    uint32_t lisn_ranges[] = {
+        cpu_to_be32(0),
+        cpu_to_be32(nr_servers),
+    };
+    uint32_t eq_sizes[] = {
+        cpu_to_be32(12), /* 4K */
+        cpu_to_be32(16), /* 64K */
+        cpu_to_be32(21), /* 2M */
+        cpu_to_be32(24), /* 16M */
+    };
+    uint32_t plat_res_int_priorities[ARRAY_SIZE(reserved_priorities)];
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(plat_res_int_priorities); i++) {
+        plat_res_int_priorities[i] = cpu_to_be32(reserved_priorities[i]);
+    }
+
+    /* Thread Interrupt Management Areas : User and OS */
+    for (i = 0; i < 2; i++) {
+        timas[i * 2] = cpu_to_be64(xive->tm_base + i * (1 << xive->tm_shift));
+        timas[i * 2 + 1] = cpu_to_be64(1 << xive->tm_shift);
+    }
+
+    _FDT(node = fdt_add_subnode(fdt, 0, "interrupt-controller"));
+
+    _FDT(fdt_setprop_string(fdt, node, "name", "interrupt-controller"));
+    _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
+    _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
+
+    _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
+    _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
+                     sizeof(eq_sizes)));
+    _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
+                     sizeof(lisn_ranges)));
+
+    /* For SLOF */
+    _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
+    _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
+
+    /* top properties */
+    _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
+                     plat_res_int_priorities, sizeof(plat_res_int_priorities)));
+}
hw/ppc/spapr.c +6 -1
index 8b15c0b500d0..3a62369883cc 100644
@@ -1127,7 +1127,12 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
 
     /* /interrupt controller */
-    spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
+    } else {
+        /* Populate device tree for XIVE */
+        spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
+    }
 
     ret = spapr_populate_memory(spapr, fdt);
     if (ret < 0) {
hw/ppc/spapr_hcall.c +6 -0
index be22a6b2895f..e2a1665beee9 100644
@@ -1646,6 +1646,12 @@ static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
             (spapr_h_cas_compose_response(spapr, args[1], args[2],
                                           ov5_updates) != 0);
     }
+
+    /* We need to rebuild the device tree for XIVE, generate a reset */
+    if (!spapr->cas_reboot) {
+        spapr->cas_reboot = spapr_ovec_test(ov5_updates, OV5_XIVE_EXPLOIT);
+    }
+
     spapr_ovec_cleanup(ov5_updates);
 
     if (spapr->cas_reboot) {
include/hw/ppc/spapr_xive.h +2 -0
index 3f822220647f..f6d4bf26e06a 100644
@@ -82,5 +82,7 @@ void spapr_xive_icp_pic_print_info(sPAPRXiveICP *xicp, Monitor *mon);
 typedef struct sPAPRMachineState sPAPRMachineState;
 
 void spapr_xive_hcall_init(sPAPRMachineState *spapr);
+void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, void *fdt,
+                         uint32_t phandle);
 
 #endif /* PPC_SPAPR_XIVE_H */
Signature · 1 line
2.13.6
#115 lore raw

Re: [Qemu-devel] [PATCH 20/25] spapr: add device tree support for the XIVE interrupt mode

Quoted 44 lines · Cédric Le Goater

The XIVE interface for the guest is described in the device tree under the "interrupt-controller" node. A couple of new properties are specific to XIVE :

- "reg"

contains the base address and size of the thread interrupt managnement areas (TIMA), also called rings, for the User level and for the Guest OS level. Only the Guest OS level is taken into account today.

- "ibm,xive-eq-sizes"

the size of the event queues. One cell per size supported, contains log2 of size, in ascending order.

- "ibm,xive-lisn-ranges"

the interrupt numbers ranges assigned to the guest. These are allocated using a simple bitmap.

and also under the root node :

- "ibm,plat-res-int-priorities"

contains a list of priorities that the hypervisor has reserved for its own use. Simulate ranges as defined by the PowerVM Hypervisor.

When the XIVE interrupt mode is activated after the CAS negotiation, the machine will perform a reboot to rebuild the device tree.

---
 hw/intc/spapr_xive_hcall.c  | 50 +++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr.c              |  7 ++++++-
 hw/ppc/spapr_hcall.c        |  6 ++++++
 include/hw/ppc/spapr_xive.h |  2 ++
 4 files changed, 64 insertions(+), 1 deletion(-)
hw/intc/spapr_xive_hcall.c +0 -0
index 676fe0e2d5c7..60c6c9f4be8f 100644

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 888

Quoted 6 lines
hw/intc/spapr_xive_hcall.c +3 -0
@@ -883,3 +883,53 @@ void spapr_xive_hcall_init(sPAPRMachineState *spapr)
     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
     spapr_register_hypercall(H_INT_RESET, h_int_reset);
 }
+
+void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers,
+                         void *fdt, uint32_t phandle)

Call it spapr_dt_xive() please, I'm trying to standardize on that pattern for functions creating DT pieces.

Quoted 18 lines
hw/intc/spapr_xive_hcall.c +18 -0
+{
+    sPAPRXive *xive = spapr->xive;
+    int node;
+    uint64_t timas[2 * 2];
+    uint32_t lisn_ranges[] = {
+        cpu_to_be32(0),
+        cpu_to_be32(nr_servers),
+    };
+    uint32_t eq_sizes[] = {
+        cpu_to_be32(12), /* 4K */
+        cpu_to_be32(16), /* 64K */
+        cpu_to_be32(21), /* 2M */
+        cpu_to_be32(24), /* 16M */
+    };
+    uint32_t plat_res_int_priorities[ARRAY_SIZE(reserved_priorities)];
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(plat_res_int_priorities); i++) {

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 916

Quoted 10 lines
hw/intc/spapr_xive_hcall.c +10 -0
+        plat_res_int_priorities[i] = cpu_to_be32(reserved_priorities[i]);
+    }
+
+    /* Thread Interrupt Management Areas : User and OS */
+    for (i = 0; i < 2; i++) {
+        timas[i * 2] = cpu_to_be64(xive->tm_base + i * (1 << xive->tm_shift));
+        timas[i * 2 + 1] = cpu_to_be64(1 << xive->tm_shift);
+    }
+
+    _FDT(node = fdt_add_subnode(fdt, 0, "interrupt-controller"));

You need a unit address here matching the reg property.

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 918

Quoted 2 lines
hw/intc/spapr_xive_hcall.c +2 -0
+
+    _FDT(fdt_setprop_string(fdt, node, "name", "interrupt-controller"));

You don't need to set name properties explicitly for flattened trees.

Quoted 64 lines
hw/intc/spapr_xive_hcall.c +17 -0
+    _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
+    _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
+
+    _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
+    _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
+                     sizeof(eq_sizes)));
+    _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
+                     sizeof(lisn_ranges)));
+
+    /* For SLOF */
+    _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
+    _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
+
+    /* top properties */
+    _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
+                     plat_res_int_priorities, sizeof(plat_res_int_priorities)));
+}
hw/ppc/spapr.c +6 -1
index 8b15c0b500d0..3a62369883cc 100644
@@ -1127,7 +1127,12 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
 
     /* /interrupt controller */
-    spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
+    } else {
+        /* Populate device tree for XIVE */
+        spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
+    }
 
     ret = spapr_populate_memory(spapr, fdt);
     if (ret < 0) {
hw/ppc/spapr_hcall.c +6 -0
index be22a6b2895f..e2a1665beee9 100644
@@ -1646,6 +1646,12 @@ static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
             (spapr_h_cas_compose_response(spapr, args[1], args[2],
                                           ov5_updates) != 0);
     }
+
+    /* We need to rebuild the device tree for XIVE, generate a reset */
+    if (!spapr->cas_reboot) {
+        spapr->cas_reboot = spapr_ovec_test(ov5_updates, OV5_XIVE_EXPLOIT);
+    }
+
     spapr_ovec_cleanup(ov5_updates);
 
     if (spapr->cas_reboot) {
include/hw/ppc/spapr_xive.h +2 -0
index 3f822220647f..f6d4bf26e06a 100644
@@ -82,5 +82,7 @@ void spapr_xive_icp_pic_print_info(sPAPRXiveICP *xicp, Monitor *mon);
 typedef struct sPAPRMachineState sPAPRMachineState;
 
 void spapr_xive_hcall_init(sPAPRMachineState *spapr);
+void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, void *fdt,
+                         uint32_t phandle);
 
 #endif /* PPC_SPAPR_XIVE_H */
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#116 lore raw

Re: [Qemu-devel] [PATCH 20/25] spapr: add device tree support for the XIVE interrupt mode

Quoted 40 lines · David Gibson
Quoted 39 lines · Cédric Le Goater

The XIVE interface for the guest is described in the device tree under the "interrupt-controller" node. A couple of new properties are specific to XIVE :

- "reg"

contains the base address and size of the thread interrupt managnement areas (TIMA), also called rings, for the User level and for the Guest OS level. Only the Guest OS level is taken into account today.

- "ibm,xive-eq-sizes"

the size of the event queues. One cell per size supported, contains log2 of size, in ascending order.

- "ibm,xive-lisn-ranges"

the interrupt numbers ranges assigned to the guest. These are allocated using a simple bitmap.

and also under the root node :

- "ibm,plat-res-int-priorities"

contains a list of priorities that the hypervisor has reserved for its own use. Simulate ranges as defined by the PowerVM Hypervisor.

When the XIVE interrupt mode is activated after the CAS negotiation, the machine will perform a reboot to rebuild the device tree.

---
 hw/intc/spapr_xive_hcall.c  | 50 +++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr.c              |  7 ++++++-
 hw/ppc/spapr_hcall.c        |  6 ++++++
 include/hw/ppc/spapr_xive.h |  2 ++
 4 files changed, 64 insertions(+), 1 deletion(-)
Quoted 14 lines

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 888

Quoted 11 lines
hw/intc/spapr_xive_hcall.c +3 -0
index 676fe0e2d5c7..60c6c9f4be8f 100644
@@ -883,3 +883,53 @@ void spapr_xive_hcall_init(sPAPRMachineState *spapr)
     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
     spapr_register_hypercall(H_INT_RESET, h_int_reset);
 }
+
+void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers,
+                         void *fdt, uint32_t phandle)

Call it spapr_dt_xive() please, I'm trying to standardize on that pattern for functions creating DT pieces.

OK. And what about the first argument : sPAPRMachineState *spapr or sPAPRXive *xive ? I tend to prefer the first option because it's related to the interface with the guest, like the hcalls.

Quoted 23 lines
Quoted 23 lines
hw/intc/spapr_xive_hcall.c +22 -0
+{
+    sPAPRXive *xive = spapr->xive;
+    int node;
+    uint64_t timas[2 * 2];
+    uint32_t lisn_ranges[] = {
+        cpu_to_be32(0),
+        cpu_to_be32(nr_servers),
+    };
+    uint32_t eq_sizes[] = {
+        cpu_to_be32(12), /* 4K */
+        cpu_to_be32(16), /* 64K */
+        cpu_to_be32(21), /* 2M */
+        cpu_to_be32(24), /* 16M */
+    };
+    uint32_t plat_res_int_priorities[ARRAY_SIZE(reserved_priorities)];
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(plat_res_int_priorities); i++) {
+        plat_res_int_priorities[i] = cpu_to_be32(reserved_priorities[i]);
+    }
+
+    /* Thread Interrupt Management Areas : User and OS */
Quoted 8 lines

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 916

Quoted 6 lines
hw/intc/spapr_xive_hcall.c +6 -0
+    for (i = 0; i < 2; i++) {
+        timas[i * 2] = cpu_to_be64(xive->tm_base + i * (1 << xive->tm_shift));
+        timas[i * 2 + 1] = cpu_to_be64(1 << xive->tm_shift);
+    }
+
+    _FDT(node = fdt_add_subnode(fdt, 0, "interrupt-controller"));

You need a unit address here matching the reg property.

Indeed. I didn't notice. Curiously it was taking the first address specified in the reg property of the node.

Quoted 4 lines

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 918

Quoted 2 lines
hw/intc/spapr_xive_hcall.c +2 -0
+
+    _FDT(fdt_setprop_string(fdt, node, "name", "interrupt-controller"));

You don't need to set name properties explicitly for flattened trees.

OK.

Thanks,

C.

Quoted 65 lines
Quoted 64 lines
hw/intc/spapr_xive_hcall.c +17 -0
+    _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
+    _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
+
+    _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
+    _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
+                     sizeof(eq_sizes)));
+    _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
+                     sizeof(lisn_ranges)));
+
+    /* For SLOF */
+    _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
+    _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
+
+    /* top properties */
+    _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
+                     plat_res_int_priorities, sizeof(plat_res_int_priorities)));
+}
hw/ppc/spapr.c +6 -1
index 8b15c0b500d0..3a62369883cc 100644
@@ -1127,7 +1127,12 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
 
     /* /interrupt controller */
-    spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
+    } else {
+        /* Populate device tree for XIVE */
+        spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
+    }
 
     ret = spapr_populate_memory(spapr, fdt);
     if (ret < 0) {
hw/ppc/spapr_hcall.c +6 -0
index be22a6b2895f..e2a1665beee9 100644
@@ -1646,6 +1646,12 @@ static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
             (spapr_h_cas_compose_response(spapr, args[1], args[2],
                                           ov5_updates) != 0);
     }
+
+    /* We need to rebuild the device tree for XIVE, generate a reset */
+    if (!spapr->cas_reboot) {
+        spapr->cas_reboot = spapr_ovec_test(ov5_updates, OV5_XIVE_EXPLOIT);
+    }
+
     spapr_ovec_cleanup(ov5_updates);
 
     if (spapr->cas_reboot) {
include/hw/ppc/spapr_xive.h +2 -0
index 3f822220647f..f6d4bf26e06a 100644
@@ -82,5 +82,7 @@ void spapr_xive_icp_pic_print_info(sPAPRXiveICP *xicp, Monitor *mon);
 typedef struct sPAPRMachineState sPAPRMachineState;
 
 void spapr_xive_hcall_init(sPAPRMachineState *spapr);
+void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, void *fdt,
+                         uint32_t phandle);
 
 #endif /* PPC_SPAPR_XIVE_H */
#117 lore raw

Re: [Qemu-devel] [PATCH 20/25] spapr: add device tree support for the XIVE interrupt mode

Quoted 56 lines · Cédric Le Goater
Quoted 40 lines · David Gibson
Quoted 39 lines · Cédric Le Goater

The XIVE interface for the guest is described in the device tree under the "interrupt-controller" node. A couple of new properties are specific to XIVE :

- "reg"

contains the base address and size of the thread interrupt managnement areas (TIMA), also called rings, for the User level and for the Guest OS level. Only the Guest OS level is taken into account today.

- "ibm,xive-eq-sizes"

the size of the event queues. One cell per size supported, contains log2 of size, in ascending order.

- "ibm,xive-lisn-ranges"

the interrupt numbers ranges assigned to the guest. These are allocated using a simple bitmap.

and also under the root node :

- "ibm,plat-res-int-priorities"

contains a list of priorities that the hypervisor has reserved for its own use. Simulate ranges as defined by the PowerVM Hypervisor.

When the XIVE interrupt mode is activated after the CAS negotiation, the machine will perform a reboot to rebuild the device tree.

---
 hw/intc/spapr_xive_hcall.c  | 50 +++++++++++++++++++++++++++++++++++++++++++++
 hw/ppc/spapr.c              |  7 ++++++-
 hw/ppc/spapr_hcall.c        |  6 ++++++
 include/hw/ppc/spapr_xive.h |  2 ++
 4 files changed, 64 insertions(+), 1 deletion(-)
Quoted 14 lines

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 888

Quoted 11 lines
hw/intc/spapr_xive_hcall.c +3 -0
index 676fe0e2d5c7..60c6c9f4be8f 100644
@@ -883,3 +883,53 @@ void spapr_xive_hcall_init(sPAPRMachineState *spapr)
     spapr_register_hypercall(H_INT_SYNC, h_int_sync);
     spapr_register_hypercall(H_INT_RESET, h_int_reset);
 }
+
+void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers,
+                         void *fdt, uint32_t phandle)

Call it spapr_dt_xive() please, I'm trying to standardize on that pattern for functions creating DT pieces.

Quoted 3 lines

OK. And what about the first argument : sPAPRMachineState *spapr or sPAPRXive *xive ? I tend to prefer the first option because it's related to the interface with the guest, like the hcalls.

Yes, using the MachineState as the first parameter is fine.

Quoted 33 lines
Quoted 24 lines
Quoted 24 lines
hw/intc/spapr_xive_hcall.c +22 -0
+{
+    sPAPRXive *xive = spapr->xive;
+    int node;
+    uint64_t timas[2 * 2];
+    uint32_t lisn_ranges[] = {
+        cpu_to_be32(0),
+        cpu_to_be32(nr_servers),
+    };
+    uint32_t eq_sizes[] = {
+        cpu_to_be32(12), /* 4K */
+        cpu_to_be32(16), /* 64K */
+        cpu_to_be32(21), /* 2M */
+        cpu_to_be32(24), /* 16M */
+    };
+    uint32_t plat_res_int_priorities[ARRAY_SIZE(reserved_priorities)];
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(plat_res_int_priorities); i++) {
+        plat_res_int_priorities[i] = cpu_to_be32(reserved_priorities[i]);
+    }
+
+    /* Thread Interrupt Management Areas : User and OS */
Quoted 8 lines

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 916

Quoted 6 lines
hw/intc/spapr_xive_hcall.c +6 -0
+    for (i = 0; i < 2; i++) {
+        timas[i * 2] = cpu_to_be64(xive->tm_base + i * (1 << xive->tm_shift));
+        timas[i * 2 + 1] = cpu_to_be64(1 << xive->tm_shift);
+    }
+
+    _FDT(node = fdt_add_subnode(fdt, 0, "interrupt-controller"));

You need a unit address here matching the reg property.

Quoted 2 lines

Indeed. I didn't notice. Curiously it was taking the first address specified in the reg property of the node.

I'm guessing that's SLOF's intervention.

Quoted 80 lines
Quoted 5 lines

hw/intc/spapr_xive_hcall.c void spapr_xive_hcall_init(sPAPRMachineState *spapr) · line 918

Quoted 3 lines
hw/intc/spapr_xive_hcall.c +2 -0
+
+    _FDT(fdt_setprop_string(fdt, node, "name", "interrupt-controller"));

You don't need to set name properties explicitly for flattened trees.

OK.

Thanks,

C.

Quoted 65 lines
Quoted 64 lines
hw/intc/spapr_xive_hcall.c +17 -0
+    _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
+    _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
+
+    _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
+    _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
+                     sizeof(eq_sizes)));
+    _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
+                     sizeof(lisn_ranges)));
+
+    /* For SLOF */
+    _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
+    _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
+
+    /* top properties */
+    _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
+                     plat_res_int_priorities, sizeof(plat_res_int_priorities)));
+}
hw/ppc/spapr.c +6 -1
index 8b15c0b500d0..3a62369883cc 100644
@@ -1127,7 +1127,12 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
     _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
 
     /* /interrupt controller */
-    spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
+    if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
+    } else {
+        /* Populate device tree for XIVE */
+        spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
+    }
 
     ret = spapr_populate_memory(spapr, fdt);
     if (ret < 0) {
hw/ppc/spapr_hcall.c +6 -0
index be22a6b2895f..e2a1665beee9 100644
@@ -1646,6 +1646,12 @@ static target_ulong h_client_architecture_support(PowerPCCPU *cpu,
             (spapr_h_cas_compose_response(spapr, args[1], args[2],
                                           ov5_updates) != 0);
     }
+
+    /* We need to rebuild the device tree for XIVE, generate a reset */
+    if (!spapr->cas_reboot) {
+        spapr->cas_reboot = spapr_ovec_test(ov5_updates, OV5_XIVE_EXPLOIT);
+    }
+
     spapr_ovec_cleanup(ov5_updates);
 
     if (spapr->cas_reboot) {
include/hw/ppc/spapr_xive.h +2 -0
index 3f822220647f..f6d4bf26e06a 100644
@@ -82,5 +82,7 @@ void spapr_xive_icp_pic_print_info(sPAPRXiveICP *xicp, Monitor *mon);
 typedef struct sPAPRMachineState sPAPRMachineState;
 
 void spapr_xive_hcall_init(sPAPRMachineState *spapr);
+void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, void *fdt,
+                         uint32_t phandle);
 
 #endif /* PPC_SPAPR_XIVE_H */
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#118 lore raw

PATCH 21/25 spapr: introduce a helper to map the XIVE memory regions

In reply to Cédric Le Goater

When the XIVE interrupt mode is activated, the machine needs to expose to the guest the MMIO regions use by the controller :

- Event State Buffer (ESB)
- Thread Interrupt Management Area (TIMA)

Migration will also need to reflect the current interrupt mode in use.

---
 hw/intc/spapr_xive_hcall.c  | 14 ++++++++++++++
 hw/ppc/spapr.c              |  5 +++++
 include/hw/ppc/spapr_xive.h |  1 +
 3 files changed, 20 insertions(+)
hw/intc/spapr_xive_hcall.c +14 -0
index 60c6c9f4be8f..ba217144878e 100644
@@ -933,3 +933,17 @@ void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers,
     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
 }
+
+void spapr_xive_mmio_map(sPAPRMachineState *spapr)
+{
+    sPAPRXive *xive = spapr->xive;
+
+    /* ESBs */
+    sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->esb_base);
+
+    /* Thread Management Interrupt Areas */
+    /* TODO: Only map the OS TIMA for the moment. Mapping the whole
+     * region needs some rework in the handlers */
+    sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1,
+                    xive->tm_base + (1 << xive->tm_shift));
+}
hw/ppc/spapr.c +5 -0
index 3a62369883cc..734706c18cb3 100644
@@ -1132,6 +1132,7 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
     } else {
         /* Populate device tree for XIVE */
         spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
+        spapr_xive_mmio_map(spapr);
     }
 
     ret = spapr_populate_memory(spapr, fdt);
@@ -1613,6 +1614,10 @@ static int spapr_post_load(void *opaque, int version_id)
         }
     }
 
+    if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_xive_mmio_map(spapr);
+    }
+
     return err;
 }
 
include/hw/ppc/spapr_xive.h +1 -0
index f6d4bf26e06a..88355f7eb643 100644
@@ -84,5 +84,6 @@ typedef struct sPAPRMachineState sPAPRMachineState;
 void spapr_xive_hcall_init(sPAPRMachineState *spapr);
 void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, void *fdt,
                          uint32_t phandle);
+void spapr_xive_mmio_map(sPAPRMachineState *spapr);
 
 #endif /* PPC_SPAPR_XIVE_H */
Signature · 1 line
2.13.6
#119 lore raw

Re: [Qemu-devel] [PATCH 21/25] spapr: introduce a helper to map the XIVE memory regions

Quoted 26 lines · Cédric Le Goater

When the XIVE interrupt mode is activated, the machine needs to expose to the guest the MMIO regions use by the controller :

- Event State Buffer (ESB)
- Thread Interrupt Management Area (TIMA)

Migration will also need to reflect the current interrupt mode in use.

---
 hw/intc/spapr_xive_hcall.c  | 14 ++++++++++++++
 hw/ppc/spapr.c              |  5 +++++
 include/hw/ppc/spapr_xive.h |  1 +
 3 files changed, 20 insertions(+)
hw/intc/spapr_xive_hcall.c +3 -0
index 60c6c9f4be8f..ba217144878e 100644
@@ -933,3 +933,17 @@ void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers,
     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
 }
+
+void spapr_xive_mmio_map(sPAPRMachineState *spapr)
+{

hw/intc/spapr_xive_hcall.c void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, · line 948

Quoted 10 lines
hw/intc/spapr_xive_hcall.c +10 -0
+    sPAPRXive *xive = spapr->xive;
+
+    /* ESBs */
+    sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->esb_base);
+
+    /* Thread Management Interrupt Areas */
+    /* TODO: Only map the OS TIMA for the moment. Mapping the whole
+     * region needs some rework in the handlers */
+    sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1,
+                    xive->tm_base + (1 << xive->tm_shift));

You probably shouldn't be exposing the user TIMA in the DT if you're only allowing the OS TIME to be mapped.

hw/ppc/spapr.c static void *spapr_build_fdt(sPAPRMachineState *spapr, · line 1135

Quoted 10 lines
hw/intc/spapr_xive_hcall.c +1 -0
+}
hw/ppc/spapr.c +1 -0
index 3a62369883cc..734706c18cb3 100644
@@ -1132,6 +1132,7 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
     } else {
         /* Populate device tree for XIVE */
         spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
+        spapr_xive_mmio_map(spapr);

This doesn't belong here, spapr_build_fdt() should _just_ build the fdt, not have side effects on the actual device state.

Quoted 25 lines
hw/ppc/spapr.c +4 -0
     }
 
     ret = spapr_populate_memory(spapr, fdt);
@@ -1613,6 +1614,10 @@ static int spapr_post_load(void *opaque, int version_id)
         }
     }
 
+    if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_xive_mmio_map(spapr);
+    }
+
     return err;
 }
 
include/hw/ppc/spapr_xive.h +1 -0
index f6d4bf26e06a..88355f7eb643 100644
@@ -84,5 +84,6 @@ typedef struct sPAPRMachineState sPAPRMachineState;
 void spapr_xive_hcall_init(sPAPRMachineState *spapr);
 void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, void *fdt,
                          uint32_t phandle);
+void spapr_xive_mmio_map(sPAPRMachineState *spapr);
 
 #endif /* PPC_SPAPR_XIVE_H */
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#120 lore raw

Re: [Qemu-devel] [PATCH 21/25] spapr: introduce a helper to map the XIVE memory regions

Quoted 32 lines · David Gibson
Quoted 31 lines · Cédric Le Goater

When the XIVE interrupt mode is activated, the machine needs to expose to the guest the MMIO regions use by the controller :

- Event State Buffer (ESB)
- Thread Interrupt Management Area (TIMA)

Migration will also need to reflect the current interrupt mode in use.

---
 hw/intc/spapr_xive_hcall.c  | 14 ++++++++++++++
 hw/ppc/spapr.c              |  5 +++++
 include/hw/ppc/spapr_xive.h |  1 +
 3 files changed, 20 insertions(+)
hw/intc/spapr_xive_hcall.c +8 -0
index 60c6c9f4be8f..ba217144878e 100644
@@ -933,3 +933,17 @@ void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers,
     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
 }
+
+void spapr_xive_mmio_map(sPAPRMachineState *spapr)
+{
+    sPAPRXive *xive = spapr->xive;
+
+    /* ESBs */
+    sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->esb_base);
+
Quoted 8 lines

hw/intc/spapr_xive_hcall.c void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, · line 948

Quoted 5 lines
hw/intc/spapr_xive_hcall.c +5 -0
+    /* Thread Management Interrupt Areas */
+    /* TODO: Only map the OS TIMA for the moment. Mapping the whole
+     * region needs some rework in the handlers */
+    sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1,
+                    xive->tm_base + (1 << xive->tm_shift));

You probably shouldn't be exposing the user TIMA in the DT if you're only allowing the OS TIME to be mapped.

The specs requires to map both Uset and OS TIMA.

Quoted 14 lines

hw/ppc/spapr.c static void *spapr_build_fdt(sPAPRMachineState *spapr, · line 1135

Quoted 11 lines
hw/intc/spapr_xive_hcall.c +1 -0
+}
hw/ppc/spapr.c +1 -0
index 3a62369883cc..734706c18cb3 100644
@@ -1132,6 +1132,7 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
     } else {
         /* Populate device tree for XIVE */
         spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
+        spapr_xive_mmio_map(spapr);

This doesn't belong here, spapr_build_fdt() should _just_ build the fdt, not have side effects on the actual device state.

Yes. I will move the rest of the XIVE setup in the reset handler before the device tree is built.

Thanks,

C.

Quoted 26 lines
Quoted 25 lines
hw/ppc/spapr.c +4 -0
     }
 
     ret = spapr_populate_memory(spapr, fdt);
@@ -1613,6 +1614,10 @@ static int spapr_post_load(void *opaque, int version_id)
         }
     }
 
+    if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_xive_mmio_map(spapr);
+    }
+
     return err;
 }
 
include/hw/ppc/spapr_xive.h +1 -0
index f6d4bf26e06a..88355f7eb643 100644
@@ -84,5 +84,6 @@ typedef struct sPAPRMachineState sPAPRMachineState;
 void spapr_xive_hcall_init(sPAPRMachineState *spapr);
 void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, void *fdt,
                          uint32_t phandle);
+void spapr_xive_mmio_map(sPAPRMachineState *spapr);
 
 #endif /* PPC_SPAPR_XIVE_H */
#121 lore raw

Re: [Qemu-devel] [PATCH 21/25] spapr: introduce a helper to map the XIVE memory regions

Quoted 42 lines · Cédric Le Goater
Quoted 32 lines · David Gibson
Quoted 31 lines · Cédric Le Goater

When the XIVE interrupt mode is activated, the machine needs to expose to the guest the MMIO regions use by the controller :

- Event State Buffer (ESB)
- Thread Interrupt Management Area (TIMA)

Migration will also need to reflect the current interrupt mode in use.

---
 hw/intc/spapr_xive_hcall.c  | 14 ++++++++++++++
 hw/ppc/spapr.c              |  5 +++++
 include/hw/ppc/spapr_xive.h |  1 +
 3 files changed, 20 insertions(+)
hw/intc/spapr_xive_hcall.c +8 -0
index 60c6c9f4be8f..ba217144878e 100644
@@ -933,3 +933,17 @@ void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers,
     _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
                      plat_res_int_priorities, sizeof(plat_res_int_priorities)));
 }
+
+void spapr_xive_mmio_map(sPAPRMachineState *spapr)
+{
+    sPAPRXive *xive = spapr->xive;
+
+    /* ESBs */
+    sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->esb_base);
+
Quoted 8 lines

hw/intc/spapr_xive_hcall.c void spapr_xive_populate(sPAPRMachineState *spapr, int nr_servers, · line 948

Quoted 5 lines
hw/intc/spapr_xive_hcall.c +5 -0
+    /* Thread Management Interrupt Areas */
+    /* TODO: Only map the OS TIMA for the moment. Mapping the whole
+     * region needs some rework in the handlers */
+    sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1,
+                    xive->tm_base + (1 << xive->tm_shift));

You probably shouldn't be exposing the user TIMA in the DT if you're only allowing the OS TIME to be mapped.

Quoted 1 line

The specs requires to map both Uset and OS TIMA.

Ok.

Quoted 15 lines
Quoted 14 lines

hw/ppc/spapr.c static void *spapr_build_fdt(sPAPRMachineState *spapr, · line 1135

Quoted 11 lines
hw/intc/spapr_xive_hcall.c +1 -0
+}
hw/ppc/spapr.c +1 -0
index 3a62369883cc..734706c18cb3 100644
@@ -1132,6 +1132,7 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
     } else {
         /* Populate device tree for XIVE */
         spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
+        spapr_xive_mmio_map(spapr);

This doesn't belong here, spapr_build_fdt() should _just_ build the fdt, not have side effects on the actual device state.

Quoted 2 lines

Yes. I will move the rest of the XIVE setup in the reset handler before the device tree is built.

Ok.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#122 lore raw

PATCH 22/25 spapr: add XIVE support to spapr_irq_get_qirq()

In reply to Cédric Le Goater

The XIVE object has its own set of qirqs which is to be used when the XIVE interrupt mode is activated.

---
 hw/ppc/spapr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/spapr.c +6 -2
index 734706c18cb3..a91ec1c0751a 100644
@@ -3746,8 +3746,12 @@ qemu_irq spapr_irq_get_qirq(sPAPRMachineState *spapr, int irq)
 {
     ICSState *ics = spapr->ics;
 
-    if (ics_valid_irq(ics, irq)) {
-        return ics->qirqs[irq - ics->offset];
+    if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return spapr->xive->qirqs[irq];
+    } else {
+        if (ics_valid_irq(ics, irq)) {
+            return ics->qirqs[irq - ics->offset];
+        }
     }
 
     return NULL;
Signature · 1 line
2.13.6
#123 lore raw

Re: [Qemu-devel] [PATCH 22/25] spapr: add XIVE support to spapr_irq_get_qirq()

Quoted 13 lines · Cédric Le Goater

The XIVE object has its own set of qirqs which is to be used when the XIVE interrupt mode is activated.

---
 hw/ppc/spapr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
hw/ppc/spapr.c +0 -0
index 734706c18cb3..a91ec1c0751a 100644

hw/ppc/spapr.c qemu_irq spapr_irq_get_qirq(sPAPRMachineState *spapr, int irq) · line 3750

Quoted 7 lines
hw/ppc/spapr.c +2 -2
@@ -3746,8 +3746,12 @@ qemu_irq spapr_irq_get_qirq(sPAPRMachineState *spapr, int irq)
 {
     ICSState *ics = spapr->ics;
 
-    if (ics_valid_irq(ics, irq)) {
-        return ics->qirqs[irq - ics->offset];
+    if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return spapr->xive->qirqs[irq];

You should have a xive helper function for this - spapr code shouldn't be reaching into the internal XIVE structure.

Quoted 7 lines
hw/ppc/spapr.c +4 -0
+    } else {
+        if (ics_valid_irq(ics, irq)) {
+            return ics->qirqs[irq - ics->offset];
+        }
     }
 
     return NULL;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#124 lore raw

PATCH 23/25 spapr: toggle the ICP depending on the selected interrupt mode

In reply to Cédric Le Goater

Each interrupt mode has its own specific interrupt presenter object, that we store under the CPU object, one for XICS and one for XIVE. The active presenter, corresponding to the current interrupt mode, is simply selected with a lookup on the children of the CPU.

Migration and CPU hotplug also need to reflect the current interrupt mode in use.

---
 hw/ppc/spapr.c                  | 21 ++++++++++++++++++++-
 hw/ppc/spapr_cpu_core.c         | 31 +++++++++++++++++++++++++++++++
 include/hw/ppc/spapr_cpu_core.h |  1 +
 3 files changed, 52 insertions(+), 1 deletion(-)
hw/ppc/spapr.c +20 -1
index a91ec1c0751a..b7389dbdf5ca 100644
@@ -1128,8 +1128,10 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
 
     /* /interrupt controller */
     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_cpu_core_set_icp(spapr->icp_type);
         spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
     } else {
+        spapr_cpu_core_set_icp(TYPE_SPAPR_XIVE_ICP);
         /* Populate device tree for XIVE */
         spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
         spapr_xive_mmio_map(spapr);
@@ -1615,6 +1617,7 @@ static int spapr_post_load(void *opaque, int version_id)
     }
 
     if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_cpu_core_set_icp(TYPE_SPAPR_XIVE_ICP);
         spapr_xive_mmio_map(spapr);
     }
 
@@ -3610,7 +3613,7 @@ static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
 Object *spapr_icp_create(sPAPRMachineState *spapr, CPUState *cs, Error **errp)
 {
     Error *local_err = NULL;
-    Object *obj;
+    Object *obj, *obj_xive;
 
     obj = icp_create(cs, spapr->icp_type, XICS_FABRIC(spapr), &local_err);
     if (local_err) {
@@ -3618,6 +3621,22 @@ Object *spapr_icp_create(sPAPRMachineState *spapr, CPUState *cs, Error **errp)
         return NULL;
     }
 
+    /* Add a XIVE interrupt presenter. The machine will switch the CPU
+     * ICP depending on the interrupt model negotiated at CAS time.
+     */
+    obj_xive = icp_create(cs, TYPE_SPAPR_XIVE_ICP, XICS_FABRIC(spapr),
+                          &local_err);
+    if (local_err) {
+        object_unparent(obj);
+        error_propagate(errp, local_err);
+        return NULL;
+    }
+
+    /* when hotplugged, the CPU should have the correct ICP */
+    if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return obj_xive;
+    }
+
     return obj;
 }
 
hw/ppc/spapr_cpu_core.c +31 -0
index 61a9850e688b..b0e39270f262 100644
@@ -257,3 +257,34 @@ static const TypeInfo spapr_cpu_core_type_infos[] = {
 };
 
 DEFINE_TYPES(spapr_cpu_core_type_infos)
+
+typedef struct ForeachFindICPArgs {
+    const char *icp_type;
+    Object *icp;
+} ForeachFindICPArgs;
+
+static int spapr_cpu_core_find_icp(Object *child, void *opaque)
+{
+    ForeachFindICPArgs *args = opaque;
+
+    if (object_dynamic_cast(child, args->icp_type)) {
+        args->icp = child;
+    }
+
+    return args->icp != NULL;
+}
+
+void spapr_cpu_core_set_icp(const char *icp_type)
+{
+    CPUState *cs;
+
+    CPU_FOREACH(cs) {
+        ForeachFindICPArgs args = { icp_type, NULL };
+        PowerPCCPU *cpu = POWERPC_CPU(cs);
+
+        object_child_foreach(OBJECT(cs), spapr_cpu_core_find_icp, &args);
+        g_assert(args.icp);
+
+        cpu->intc = args.icp;
+    }
+}
include/hw/ppc/spapr_cpu_core.h +1 -0
index f2d48d6a6786..a657dfb8863c 100644
@@ -38,4 +38,5 @@ typedef struct sPAPRCPUCoreClass {
 } sPAPRCPUCoreClass;
 
 const char *spapr_get_cpu_core_type(const char *cpu_type);
+void spapr_cpu_core_set_icp(const char *icp_type);
 #endif
Signature · 1 line
2.13.6
#125 lore raw

Re: [Qemu-devel] [PATCH 23/25] spapr: toggle the ICP depending on the selected interrupt mode

Quoted 20 lines · Cédric Le Goater

Each interrupt mode has its own specific interrupt presenter object, that we store under the CPU object, one for XICS and one for XIVE. The active presenter, corresponding to the current interrupt mode, is simply selected with a lookup on the children of the CPU.

Migration and CPU hotplug also need to reflect the current interrupt mode in use.

---
 hw/ppc/spapr.c                  | 21 ++++++++++++++++++++-
 hw/ppc/spapr_cpu_core.c         | 31 +++++++++++++++++++++++++++++++
 include/hw/ppc/spapr_cpu_core.h |  1 +
 3 files changed, 52 insertions(+), 1 deletion(-)
hw/ppc/spapr.c +0 -0
index a91ec1c0751a..b7389dbdf5ca 100644

hw/ppc/spapr.c static void *spapr_build_fdt(sPAPRMachineState *spapr, · line 1134

Quoted 7 lines
hw/ppc/spapr.c +2 -0
@@ -1128,8 +1128,10 @@ static void *spapr_build_fdt(sPAPRMachineState *spapr,
 
     /* /interrupt controller */
     if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_cpu_core_set_icp(spapr->icp_type);
         spapr_dt_xics(xics_max_server_number(), fdt, PHANDLE_XICP);
     } else {
+        spapr_cpu_core_set_icp(TYPE_SPAPR_XIVE_ICP);

Again you shouldn't have non-DT side-effects from spapr_build_fdt().

Quoted 21 lines
hw/ppc/spapr.c +2 -1
         /* Populate device tree for XIVE */
         spapr_xive_populate(spapr, xics_max_server_number(), fdt, PHANDLE_XICP);
         spapr_xive_mmio_map(spapr);
@@ -1615,6 +1617,7 @@ static int spapr_post_load(void *opaque, int version_id)
     }
 
     if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_cpu_core_set_icp(TYPE_SPAPR_XIVE_ICP);
         spapr_xive_mmio_map(spapr);
     }
 
@@ -3610,7 +3613,7 @@ static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
 Object *spapr_icp_create(sPAPRMachineState *spapr, CPUState *cs, Error **errp)
 {
     Error *local_err = NULL;
-    Object *obj;
+    Object *obj, *obj_xive;
 
     obj = icp_create(cs, spapr->icp_type, XICS_FABRIC(spapr), &local_err);
     if (local_err) {

hw/ppc/spapr.c Object *spapr_icp_create(sPAPRMachineState *spapr, CPUState *cs, Error **errp) · line 3628

Quoted 8 lines
hw/ppc/spapr.c +5 -0
@@ -3618,6 +3621,22 @@ Object *spapr_icp_create(sPAPRMachineState *spapr, CPUState *cs, Error **errp)
         return NULL;
     }
 
+    /* Add a XIVE interrupt presenter. The machine will switch the CPU
+     * ICP depending on the interrupt model negotiated at CAS time.
+     */
+    obj_xive = icp_create(cs, TYPE_SPAPR_XIVE_ICP, XICS_FABRIC(spapr),
+                          &local_err);

You shouldn't be using icp_create() a xics function, for xive.

Quoted 63 lines
hw/ppc/spapr.c +11 -0
+    if (local_err) {
+        object_unparent(obj);
+        error_propagate(errp, local_err);
+        return NULL;
+    }
+
+    /* when hotplugged, the CPU should have the correct ICP */
+    if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        return obj_xive;
+    }
+
     return obj;
 }
 
hw/ppc/spapr_cpu_core.c +31 -0
index 61a9850e688b..b0e39270f262 100644
@@ -257,3 +257,34 @@ static const TypeInfo spapr_cpu_core_type_infos[] = {
 };
 
 DEFINE_TYPES(spapr_cpu_core_type_infos)
+
+typedef struct ForeachFindICPArgs {
+    const char *icp_type;
+    Object *icp;
+} ForeachFindICPArgs;
+
+static int spapr_cpu_core_find_icp(Object *child, void *opaque)
+{
+    ForeachFindICPArgs *args = opaque;
+
+    if (object_dynamic_cast(child, args->icp_type)) {
+        args->icp = child;
+    }
+
+    return args->icp != NULL;
+}
+
+void spapr_cpu_core_set_icp(const char *icp_type)
+{
+    CPUState *cs;
+
+    CPU_FOREACH(cs) {
+        ForeachFindICPArgs args = { icp_type, NULL };
+        PowerPCCPU *cpu = POWERPC_CPU(cs);
+
+        object_child_foreach(OBJECT(cs), spapr_cpu_core_find_icp, &args);
+        g_assert(args.icp);
+
+        cpu->intc = args.icp;
+    }
+}
include/hw/ppc/spapr_cpu_core.h +1 -0
index f2d48d6a6786..a657dfb8863c 100644
@@ -38,4 +38,5 @@ typedef struct sPAPRCPUCoreClass {
 } sPAPRCPUCoreClass;
 
 const char *spapr_get_cpu_core_type(const char *cpu_type);
+void spapr_cpu_core_set_icp(const char *icp_type);
 #endif
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#126 lore raw

PATCH 24/25 spapr: add support to dump XIVE information

In reply to Cédric Le Goater

Modify the InterruptStatsProvider output to reflect the interrupt mode currently in use by the machine.

---
 hw/ppc/spapr.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
hw/ppc/spapr.c +10 -2
index b7389dbdf5ca..9fe3a9966b12 100644
@@ -3785,10 +3785,18 @@ static void spapr_pic_print_info(InterruptStatsProvider *obj,
     CPU_FOREACH(cs) {
         PowerPCCPU *cpu = POWERPC_CPU(cs);
 
-        icp_pic_print_info(ICP(cpu->intc), mon);
+        if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+            spapr_xive_icp_pic_print_info(SPAPR_XIVE_ICP(cpu->intc), mon);
+        } else {
+            icp_pic_print_info(ICP(cpu->intc), mon);
+        }
     }
 
-    ics_pic_print_info(spapr->ics, mon);
+    if (spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
+        spapr_xive_pic_print_info(spapr->xive, mon);
+    } else {
+        ics_pic_print_info(spapr->ics, mon);
+    }
 }
 
 int spapr_vcpu_id(PowerPCCPU *cpu)
Signature · 1 line
2.13.6
#127 lore raw

PATCH 25/25 spapr: advertise XIVE exploitation mode in CAS

In reply to Cédric Le Goater

We introduce a 'xive_exploitation' boolean at the machine level to enable the XIVE interrupt mode for newer machines and to disable it on older ones.

The XIVE interrupt mode can still be forced on the command line with a machine option. That might be a bit dangerous. To be discussed.

---
 hw/intc/spapr_xive.c   | 10 ++++++----
 hw/ppc/spapr.c         | 52 +++++++++++++++++++++++++++++++++++++++++++++-----
 include/hw/ppc/spapr.h |  1 +
 3 files changed, 54 insertions(+), 9 deletions(-)
hw/intc/spapr_xive.c +6 -4
index b732aaf4f8ba..f7fab70cb8bb 100644
@@ -736,8 +736,9 @@ static const VMStateDescription vmstate_spapr_xive_ive = {
 
 static bool vmstate_spapr_xive_needed(void *opaque)
 {
-    /* TODO check machine XIVE support */
-    return true;
+    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+
+    return spapr->xive_exploitation;
 }
 
 static const VMStateDescription vmstate_spapr_xive = {
@@ -863,8 +864,9 @@ static const VMStateDescription vmstate_spapr_xive_icp_eq = {
 
 static bool vmstate_spapr_xive_icp_needed(void *opaque)
 {
-    /* TODO check machine XIVE support */
-    return true;
+    sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
+
+    return spapr->xive_exploitation;
 }
 
 static const VMStateDescription vmstate_spapr_xive_icp = {
hw/ppc/spapr.c +47 -5

96 lines not on this page · raw message

include/hw/ppc/spapr.h +1 -0
index a25e218b34e2..c4f051f974fe 100644
@@ -129,6 +129,7 @@ struct sPAPRMachineState {
 
     const char *icp_type;
     sPAPRXive  *xive;
+    bool xive_exploitation;
 };
 
 #define H_SUCCESS         0
Signature · 1 line
2.13.6