Skip to contentSkip to conversation

ppc: support for the XIVE interrupt controller (POWER9)

PATCH Superseded Cédric Le Goater 6 participants 184 messages Nov 16, 2018 – Dec 6, 2018 Page 1 of 4

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v5 cover ppc: support for the XIVE interrupt controller (POWER9)

Hello,

Here is the version 5 of the QEMU models adding support for the XIVE interrupt controller to the sPAPR machine, under TCG and KVM, and to the PowerNV POWER9 machine.

The most important changes for sPAPR are the introduction of a new 'dual' pseries machine supporting both interrupt mode: XICS and XIVE, under TCG and KVM, and fixes for the virtual LSIs.

The QEMU PowerNV POWER9 machine now has support for PHB4 bringing it to the level of a "real" machine. It validates the model of the barematel XIVE IC controller proposed in this patchset. The other PowerNV models will be part of another patchset.

Thanks,

C.

Changes in v5 :

Common XIVE models :

- renamed the XIVE structures to fit the changes of the XIVE architecture documents: IVE, EQD, VPD -> EAS, END, NVT
- reworked the monitor ouput to print the EQ contents

sPAPR models :

- introduced a XIVE Router 'reset' method for the Xive Thread Context to set the OS CAM line of the VCPU
- introduced a spapr_irq_init() routine to the sPAPR IRQ backend and reworked the XIVE-only machine to fit mainline QEMU
- introduced a reset() method to the sPAPR IRQ backend to handle changes in the interrupt mode after machine reset
- introduced a 'dual' machine supporting both interrupt mode

KVM :

- introduced some more sPAPR NVT and END indexing helpers for KVM support
- fixed the virtual LSIs in KVM by using the H_INT_ESB source flag
- improved the KVM support with better common classes and cleaner QEMU<->KVM interfaces
- improved KVM migration with a better control on the capture sequence. Still some issues with 'ceded' VCPUs
- introduced KVM support for the 'dual' machine

PowerNV:

- introduced address spaces for the IPI and END set translation tables

Changes in v4 :

See https://lists.gnu.org/archive/html/qemu-devel/2018-06/msg01672.html

= XIVE =================================================================

The POWER9 processor comes with a new interrupt controller, called XIVE as "eXternal Interrupt Virtualization Engine".

* Overall architecture

              XIVE Interrupt Controller
              +-------------------------------------+       IPIs
              | +---------+ +---------+ +---------+ |    +--------+
              | |VC       | |CQ       | |PC       |----> | CORES  |
              | |     esb | |         | |         |----> |        |
              | |     eas | |  Bridge | |         |----> |        |
              | |SC   end | |         | |     nvt | |    |        |
+------+      | +---------+ +----+----+ +---------+ |    +--+-+-+-+
| RAM  |      +------------------|------------------+       | | |
|      |                         |                          | | |
|      |                         |                          | | |
|      |   +---------------------v--------------------------v-v-v---+      other
|      <---+                       Power Bus                        +----> chips
|  esb |   +-----------+-----------------------+--------------------+
|  eas |               |                       |
|  end |               |                       |
|  nvt |           +---+----+              +---+----+
+------+           |SC      |              |SC      |
                   |        |              |        |
                   | 2-bits |              | 2-bits |
                   | local  |              |   VC   |
                   +--------+              +--------+
                     PCIe                  NX,NPU,CAPI

SC: Source Controller (aka. IVSE)
VC: Virtualization Controller (aka. IVRE)
CQ: Common Queue (Bridge)
PC: Presentation Controller (aka. IVPE)

2-bits: source state machine (PQ bits)
esb: Event State Buffer (Array of PQ bits in an IVSE)
eas: Event Assignment Structure
end: Event Notification Descriptor
nvt: Notification Virtual Target

It is composed of three sub-engines :

- Interrupt Virtualization Source Engine (IVSE), or Source Controller (SC). These are found in PCI PHBs, in the PSI host bridge controller, but also inside the main controller for the core IPIs and other sub-chips (NX, CAP, NPU) of the chip/processor. They are configured to feed the IVRE with events.

- Interrupt Virtualization Routing Engine (IVRE) or Virtualization Controller (VC). Its job is to match an event source with an Event Notification Descriptor (END).

- Interrupt Virtualization Presentation Engine (IVPE) or Presentation Controller (PC). It maintains the interrupt context state of each thread and handles the delivery of the external exception to the thread.

* XIVE internal tables

Each of the sub-engines uses a set of tables to redirect exceptions from event sources to CPU threads.

                                             +-------+
   User or OS                                |  EQ   |
       or                            +------>|entries|
   Hypervisor                        |       |  ..   |
     Memory                          |       +-------+
                                     |           ^
                                     |           |
               +--------------------------------------------------+
                                     |           |
   Hypervisor        +------+    +---+--+    +---+--+   +------+
     Memory          | ESB  |    | EAT  |    | ENDT |   | NVTT |
    (skiboot)        +----+-+    +----+-+    +----+-+   +------+
                       ^  |        ^  |        ^  |       ^
                       |  |        |  |        |  |       |
               +--------------------------------------------------+
                       |  |        |  |        |  |       |
                       |  |        |  |        |  |       |
                 +-----|--|--------|--|--------|--|-+   +-|-----+    +------+
                 |     |  |        |  |        |  | |   | | tctx|    |Thread|
    IPI or   ----+     +  v        +  v        +  v |---| +  .. |----->     |
   HW events     |                                  |   |       |    |      |
                 |              IVRE                |   | IVPE  |    +------+
                 +----------------------------------+   +-------+

The IVSE have a 2-bits, P for pending and Q for queued, state machine for each source that allows events to be triggered. They are stored in an array, the Event State Buffer (ESB) and controlled by MMIOs.

If the event is let through, the IVRE looks up in the Event Assignment Structure (EAS) table for an Event Notification Descriptor (END) configured for the source. Each Event Notification Descriptor defines a notification path to a CPU and an in-memory Event Queue, in which will be pushed an EQ data for the OS to pull.

The IVPE determines if a Notification Virtual Target (NVT) can handle the event by scanning the thread contexts of the VPs dispatched on the processor HW threads. It maintains the interrupt context state of each thread in a NVT table.

* Overview of the QEMU models for the XIVE sub-engines

The XiveSource models the IVSE in general, internal and external. It handles the source ESBs and the MMIO interface to control them.

The XiveFabric is a small helper interface interconnecting the XiveSource to the XiveRouter.

The XiveRouter is an abstract model acting as a combined IVRE and IVPE. It routes event notifications using the IVE and EQD tables to the IVPE sub-engine which does a CAM scan to find a CPU to deliver the exception. Storage should be provided by the inheriting classes.

XiveEQSource is a special source object. It exposes the EQ ESB MMIOs of the Event Queues which are used for coalescing event notifications and for escalation. Not used on the field, only to sync the EQ cache in OPAL.

Finally, the XiveTCTX contains the interrupt state context of a thread, four sets of registers, one for each exception that can be delivered to a CPU. These contexts are scanned by the IVPE to find a matching VP when a notification is triggered. It also models the Thread Interrupt Management Area (TIMA), which exposes the thread context registers to the CPU for interrupt management.

* XIVE for sPAPR

sPAPRXive models the XIVE interrupt controller of a sPAPR machine. It inherits from the XiveRouter and provisions storage for the IVE and END tables. The NVT table does not need a backend in sPAPR. It owns a XiveSource object for the IPIs and the virtual device interrupts, a memory region for the TIMA and a XiveENDSource to manage the END ESBs. (not used by Linux).

These choices were made to have a sPAPR interrupt controller consistent with the one found on baremetal and to facilitate KVM support, the main difficulty being the host memory regions exposed to the guest.

The NVT and tbe END indexing needs some care and a set of helpers are defined to ease the conversion between the CPU id as seen by the guest and the XIVE identifiers manipulated by the models.

* Integration in the sPAPR machine, xive only and dual

A new sPAPR IRQ backend is defined for XIVE. It introduces a couple of new operations to handle the differences in the creation of the device tree and in the allocation of the CPU interrupt controller. A new 'xive' only pseries machine is defined using this XIVE backend.

Being able to support both interrupt mode in the same machine requires some more changes. As the machine chooses the interrupt mode at CAS time, it is activated after a reconfiguration done in a reset. This is handled by a new 'dual' sPAPR IRQ backend which is built on top of the XICS and XIVE backend. A new 'dual' pseries machine is defined using this backend.

* KVM support

Support for KVM introduces a set of specific XIVE models, very much like XICS does, which self-connect to their KVM counterparts in the Linux kernel. Two host memory regions are exposed to the guest and need special care at initialization :

- ESB mmios
- Thread Interrupt Management Area (TIMA)

The models uses KVM accessors to synchronize the QEMU state with KVM. The states are :

- the source configuration (EAT)
- the END configuration (ENDT)
- the OS EQ state (toggle bit and index)
- the thread interrupt context registers.

Hybrid guest using KVM and an emulated irqchip (kernel_irqchip=off) is supported.

Migration under KVM is supported but still has some issues with the pages of the OS event notification queues when the VCPUs are 'ceded'. They are mapped to the ZERO_PAGE on the receiving side. Work in progress.

KVM support for the 'dual' machine required some more changes. Both interrupt mode need to be initialized at the QEMU level to keep the IRQ number space in sync and to allow switching from one mode to another. At the KVM level, the whole initialization of the KVM device, sources and presenters, needs to be done in the reset handler when the interrupt mode is chosen. This is a major change in the KVM models.

KVM being initialized at reset, we loose the possiblity to fallback to the QEMU emulated mode in case of failure and failures become fatal to the machine.

* PowerNV models

The PnvXIVE model uses the XiveRouter abstract model just like sPAPRXive. It provides accessors to the EAS, END and NVT tables which are stored in the QEMU PowerNV machine and not in QEMU anymore. It owns a set of memory regions for the IC registers, the ESBs, the END ESBs, the TIMA, the notification MMIO.

Multichip is supported and the available IVSEs are the internal one for the IPIS, the PSI host bridge controller and PHB4.

The next interesting step would be to add escalation events and model the VCPU dispatching to support emulated KVM guests.

* GitHub trees

QEMU sPAPR:

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

QEMU PowerNV:

https://github.com/legoater/qemu/commits/powernv-3.1

Linux/KVM:

https://github.com/legoater/linux/commits/xive-4.20

OPAL:

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

Cédric Le Goater (36):
ppc/xive: introduce a XIVE interrupt source model ppc/xive: add support for the LSI interrupt sources ppc/xive: introduce the XiveFabric interface
ppc/xive: introduce the XiveRouter model
ppc/xive: introduce the XIVE Event Notification Descriptors ppc/xive: add support for the END Event State buffers ppc/xive: introduce the XIVE interrupt thread context ppc/xive: introduce a simplified XIVE presenter ppc/xive: notify the CPU when the interrupt priority is more
privileged
spapr/xive: introduce a XIVE interrupt controller spapr/xive: use the VCPU id as a NVT identifier
spapr: initialize VSMT before initializing the IRQ backend
spapr: introduce a spapr_irq_init() routine
spapr: modify the irq backend 'init' method
spapr: introdude a new machine IRQ backend for XIVE
spapr: add hcalls support for the XIVE exploitation interrupt mode
spapr: add device tree support for the XIVE exploitation mode
spapr: allocate the interrupt thread context under the CPU core
spapr: add a 'pseries-3.1-xive' machine type
spapr: add classes for the XIVE models
spapr: extend the sPAPR IRQ backend for XICS migration spapr/xive: add models for KVM support
spapr/xive: add migration support for KVM
spapr: add a 'reset' method to the sPAPR IRQ backend
spapr: set the interrupt presenter at reset
spapr: add a 'pseries-3.1-dual' machine type
sysbus: add a sysbus_mmio_unmap() helper
ppc/xics: introduce a icp_kvm_init() routine
ppc/xics: remove abort() in icp_kvm_init()
spapr: check for KVM IRQ device activation
spapr/xive: export the spapr_xive_kvm_init() routine spapr/rtas: modify spapr_rtas_register() to remove RTAS handlers
spapr: introduce routines to delete the KVM IRQ device
spapr: add KVM support to the 'dual' machine
ppc: externalize ppc_get_vcpu_by_pir()
ppc/pnv: add XIVE support

 default-configs/ppc64-softmmu.mak |    3 +
 hw/intc/pnv_xive_regs.h           |  314 +++++
 include/hw/ppc/pnv.h              |   22 +-
 include/hw/ppc/pnv_xive.h         |  100 ++
 include/hw/ppc/pnv_xscom.h        |    3 +
 include/hw/ppc/ppc.h              |    1 +
 include/hw/ppc/spapr.h            |   18 +-
 include/hw/ppc/spapr_cpu_core.h   |    2 +
 include/hw/ppc/spapr_irq.h        |   16 +-
 include/hw/ppc/spapr_xive.h       |  113 ++
 include/hw/ppc/xics.h             |    1 +
 include/hw/ppc/xive.h             |  332 ++++++
 include/hw/ppc/xive_regs.h        |  183 +++
 include/hw/sysbus.h               |    1 +
 include/migration/vmstate.h       |    1 +
 linux-headers/asm-powerpc/kvm.h   |   45 +
 linux-headers/linux/kvm.h         |    6 +
 target/ppc/kvm_ppc.h              |    6 +
 hw/core/sysbus.c                  |   10 +
 hw/intc/pnv_xive.c                | 1612 +++++++++++++++++++++++++
 hw/intc/spapr_xive.c              |  523 +++++++++
 hw/intc/spapr_xive_hcall.c        |  954 +++++++++++++++
 hw/intc/spapr_xive_kvm.c          |  983 ++++++++++++++++
 hw/intc/xics_kvm.c                |  113 +-
 hw/intc/xive.c                    | 1811 +++++++++++++++++++++++++++++
 hw/ppc/pnv.c                      |   74 +-
 hw/ppc/ppc.c                      |   16 +
 hw/ppc/spapr.c                    |   84 +-
 hw/ppc/spapr_cpu_core.c           |   31 +-
 hw/ppc/spapr_hcall.c              |   16 +
 hw/ppc/spapr_irq.c                |  455 +++++++-
 hw/ppc/spapr_rtas.c               |    2 +-
 target/ppc/kvm.c                  |    7 +
 hw/intc/Makefile.objs             |    5 +-
 34 files changed, 7780 insertions(+), 83 deletions(-)
 create mode 100644 hw/intc/pnv_xive_regs.h
 create mode 100644 include/hw/ppc/pnv_xive.h
 create mode 100644 include/hw/ppc/spapr_xive.h
 create mode 100644 include/hw/ppc/xive.h
 create mode 100644 include/hw/ppc/xive_regs.h
 create mode 100644 hw/intc/pnv_xive.c
 create mode 100644 hw/intc/spapr_xive.c
 create mode 100644 hw/intc/spapr_xive_hcall.c
 create mode 100644 hw/intc/spapr_xive_kvm.c
 create mode 100644 hw/intc/xive.c
Signature · 1 line
2.17.2
#1 lore raw

PATCH v5 1/36 ppc/xive: introduce a XIVE interrupt source model

The first sub-engine of the overall XIVE architecture is the Interrupt Virtualization Source Engine (IVSE). An IVSE can be integrated into another logic, like in a PCI PHB or in the main interrupt controller to manage IPIs.

Each IVSE instance is associated with an Event State Buffer (ESB) that contains a two bit state entry for each possible event source. When an event is signaled to the IVSE, by MMIO or some other means, the associated interrupt state bits are fetched from the ESB and modified. Depending on the resulting ESB state, the event is forwarded to the IVRE sub-engine of the controller doing the routing.

Each supported ESB entry is associated with either a single or a even/odd pair of pages which provides commands to manage the source:
to EOI, to turn off the source for instance.

On a sPAPR machine, the O/S will obtain the page address of the ESB entry associated with a source and its characteristic using the H_INT_GET_SOURCE_INFO hcall. On PowerNV, a similar OPAL call is used.

The xive_source_notify() routine is in charge forwarding the source event notification to the routing engine. It will be filled later on.

---
 default-configs/ppc64-softmmu.mak |   1 +
 include/hw/ppc/xive.h             | 130 ++++++++++
 hw/intc/xive.c                    | 379 ++++++++++++++++++++++++++++++
 hw/intc/Makefile.objs             |   1 +
 4 files changed, 511 insertions(+)
 create mode 100644 include/hw/ppc/xive.h
 create mode 100644 hw/intc/xive.c
default-configs/ppc64-softmmu.mak +1 -0
index aec2855750d6..2d1e7c5c4668 100644
@@ -16,6 +16,7 @@ CONFIG_VIRTIO_VGA=y
 CONFIG_XICS=$(CONFIG_PSERIES)
 CONFIG_XICS_SPAPR=$(CONFIG_PSERIES)
 CONFIG_XICS_KVM=$(call land,$(CONFIG_PSERIES),$(CONFIG_KVM))
+CONFIG_XIVE=$(CONFIG_PSERIES)
 CONFIG_MEM_DEVICE=y
 CONFIG_DIMM=y
 CONFIG_SPAPR_RNG=y
include/hw/ppc/xive.h new file +130 -0

130 lines not on this page · raw message

hw/intc/xive.c new file +379 -0

379 lines not on this page · raw message

hw/intc/Makefile.objs +1 -0
index 0e9963f5eecc..72a46ed91c31 100644
@@ -37,6 +37,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) += xive.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
Signature · 1 line
2.17.2
#2 lore raw

Re: [Qemu-devel] [PATCH v5 01/36] ppc/xive: introduce a XIVE interrupt source model

Quoted 16 lines · Cédric Le Goater

The first sub-engine of the overall XIVE architecture is the Interrupt Virtualization Source Engine (IVSE). An IVSE can be integrated into another logic, like in a PCI PHB or in the main interrupt controller to manage IPIs.

Each IVSE instance is associated with an Event State Buffer (ESB) that contains a two bit state entry for each possible event source. When an event is signaled to the IVSE, by MMIO or some other means, the associated interrupt state bits are fetched from the ESB and modified. Depending on the resulting ESB state, the event is forwarded to the IVRE sub-engine of the controller doing the routing.

Each supported ESB entry is associated with either a single or a even/odd pair of pages which provides commands to manage the source:
to EOI, to turn off the source for instance.

Quoted 8 lines

On a sPAPR machine, the O/S will obtain the page address of the ESB entry associated with a source and its characteristic using the H_INT_GET_SOURCE_INFO hcall. On PowerNV, a similar OPAL call is used.

The xive_source_notify() routine is in charge forwarding the source event notification to the routing engine. It will be filled later on.

Ok, this is looking basically pretty good. Few details to query below.

Quoted 27 lines
---
 default-configs/ppc64-softmmu.mak |   1 +
 include/hw/ppc/xive.h             | 130 ++++++++++
 hw/intc/xive.c                    | 379 ++++++++++++++++++++++++++++++
 hw/intc/Makefile.objs             |   1 +
 4 files changed, 511 insertions(+)
 create mode 100644 include/hw/ppc/xive.h
 create mode 100644 hw/intc/xive.c
default-configs/ppc64-softmmu.mak +1 -0
index aec2855750d6..2d1e7c5c4668 100644
@@ -16,6 +16,7 @@ CONFIG_VIRTIO_VGA=y
 CONFIG_XICS=$(CONFIG_PSERIES)
 CONFIG_XICS_SPAPR=$(CONFIG_PSERIES)
 CONFIG_XICS_KVM=$(call land,$(CONFIG_PSERIES),$(CONFIG_KVM))
+CONFIG_XIVE=$(CONFIG_PSERIES)
 CONFIG_MEM_DEVICE=y
 CONFIG_DIMM=y
 CONFIG_SPAPR_RNG=y
include/hw/ppc/xive.h new file +0 -0
new file mode 100644
index 000000000000..5fec4b08705d

include/hw/ppc/xive.h line 7

Quoted 7 lines
include/hw/ppc/xive.h new file +7 -0
@@ -0,0 +1,130 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2017-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.

A cheat sheet in the top of this header with the old and new XIVE terms would quite nice to have.

include/hw/ppc/xive.h line 13

Quoted 6 lines
include/hw/ppc/xive.h new file +6 -0
+ */
+
+#ifndef PPC_XIVE_H
+#define PPC_XIVE_H
+
+#include "hw/sysbus.h"

So, I'm a bit dubious about making the XiveSource a SysBus device - I'm concerned it won't play well with tying it into the other devices like PHB that "own" it in real hardware.

I think we'd be better off making it a direct descendent of TYPE_DEVICE which constructs the MMIO region, but doesn't map it. Then we can havea SysBusDevice (and/or other) wrapper which instantiates the XiveSource core and maps it into somewhere accessible.

Quoted 39 lines
include/hw/ppc/xive.h new file +39 -0
+
+/*
+ * XIVE Interrupt Source
+ */
+
+#define TYPE_XIVE_SOURCE "xive-source"
+#define XIVE_SOURCE(obj) OBJECT_CHECK(XiveSource, (obj), TYPE_XIVE_SOURCE)
+
+/*
+ * XIVE Interrupt Source characteristics, which define how the ESB are
+ * controlled.
+ */
+#define XIVE_SRC_H_INT_ESB     0x1 /* ESB managed with hcall H_INT_ESB */
+#define XIVE_SRC_STORE_EOI     0x2 /* Store EOI supported */
+
+typedef struct XiveSource {
+    SysBusDevice parent;
+
+    /* IRQs */
+    uint32_t        nr_irqs;
+    qemu_irq        *qirqs;
+
+    /* PQ bits */
+    uint8_t         *status;
+
+    /* ESB memory region */
+    uint64_t        esb_flags;
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+} XiveSource;
+
+/*
+ * ESB MMIO setting. Can be one page, for both source triggering and
+ * source management, or two different pages. See below for magic
+ * values.
+ */
+#define XIVE_ESB_4K          12 /* PSI HB only */
+#define XIVE_ESB_4K_2PAGE    13
+#define XIVE_ESB_64K         16

include/hw/ppc/xive.h line 62

Quoted 10 lines
include/hw/ppc/xive.h new file +10 -0
+#define XIVE_ESB_64K_2PAGE   17
+
+static inline bool xive_source_esb_has_2page(XiveSource *xsrc)
+{
+    return xsrc->esb_shift == XIVE_ESB_64K_2PAGE ||
+        xsrc->esb_shift == XIVE_ESB_4K_2PAGE;
+}
+
+/* The trigger page is always the first/even page */
+static inline hwaddr xive_source_esb_page(XiveSource *xsrc, uint32_t srcno)

This function doesn't appear to be used anywhere except..

include/hw/ppc/xive.h line 69

Quoted 7 lines
include/hw/ppc/xive.h new file +7 -0
+{
+    assert(srcno < xsrc->nr_irqs);
+    return (1ull << xsrc->esb_shift) * srcno;
+}
+
+/* In a two pages ESB MMIO setting, the odd page is for management */
+static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)

..here, and this function doesn't appear to be used anywhere.

Quoted 203 lines
include/hw/ppc/xive.h new file +61 -0
+{
+    hwaddr addr = xive_source_esb_page(xsrc, srcno);
+
+    if (xive_source_esb_has_2page(xsrc)) {
+        addr += (1 << (xsrc->esb_shift - 1));
+    }
+
+    return addr;
+}
+
+/*
+ * Each interrupt source has a 2-bit state machine which can be
+ * controlled by MMIO. P indicates that an interrupt is pending (has
+ * been sent to a queue and is waiting for an EOI). Q indicates that
+ * the interrupt has been triggered while pending.
+ *
+ * This acts as a coalescing mechanism in order to guarantee that a
+ * given interrupt only occurs at most once in a queue.
+ *
+ * When doing an EOI, the Q bit will indicate if the interrupt
+ * needs to be re-triggered.
+ */
+#define XIVE_ESB_VAL_P        0x2
+#define XIVE_ESB_VAL_Q        0x1
+
+#define XIVE_ESB_RESET        0x0
+#define XIVE_ESB_PENDING      XIVE_ESB_VAL_P
+#define XIVE_ESB_QUEUED       (XIVE_ESB_VAL_P | XIVE_ESB_VAL_Q)
+#define XIVE_ESB_OFF          XIVE_ESB_VAL_Q
+
+/*
+ * "magic" Event State Buffer (ESB) MMIO offsets.
+ *
+ * The following offsets into the ESB MMIO allow to read or manipulate
+ * the PQ bits. They must be used with an 8-byte load instruction.
+ * They all return the previous state of the interrupt (atomically).
+ *
+ * Additionally, some ESB pages support doing an EOI via a store and
+ * some ESBs support doing a trigger via a separate trigger page.
+ */
+#define XIVE_ESB_STORE_EOI      0x400 /* Store */
+#define XIVE_ESB_LOAD_EOI       0x000 /* Load */
+#define XIVE_ESB_GET            0x800 /* Load */
+#define XIVE_ESB_SET_PQ_00      0xc00 /* Load */
+#define XIVE_ESB_SET_PQ_01      0xd00 /* Load */
+#define XIVE_ESB_SET_PQ_10      0xe00 /* Load */
+#define XIVE_ESB_SET_PQ_11      0xf00 /* Load */
+
+uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno);
+uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq);
+
+void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset,
+                                Monitor *mon);
+
+static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return xsrc->qirqs[srcno];
+}
+
+#endif /* PPC_XIVE_H */
hw/intc/xive.c new file +136 -0
new file mode 100644
index 000000000000..f7621f84828c
@@ -0,0 +1,379 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2017-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "target/ppc/cpu.h"
+#include "sysemu/cpus.h"
+#include "sysemu/dma.h"
+#include "monitor/monitor.h"
+#include "hw/ppc/xive.h"
+
+/*
+ * XIVE ESB helpers
+ */
+
+static uint8_t xive_esb_set(uint8_t *pq, uint8_t value)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    *pq &= ~0x3;
+    *pq |= value & 0x3;
+
+    return old_pq;
+}
+
+static bool xive_esb_trigger(uint8_t *pq)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+        xive_esb_set(pq, XIVE_ESB_PENDING);
+        return true;
+    case XIVE_ESB_PENDING:
+    case XIVE_ESB_QUEUED:
+        xive_esb_set(pq, XIVE_ESB_QUEUED);
+        return false;
+    case XIVE_ESB_OFF:
+        xive_esb_set(pq, XIVE_ESB_OFF);
+        return false;
+    default:
+         g_assert_not_reached();
+    }
+}
+
+static bool xive_esb_eoi(uint8_t *pq)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+    case XIVE_ESB_PENDING:
+        xive_esb_set(pq, XIVE_ESB_RESET);
+        return false;
+    case XIVE_ESB_QUEUED:
+        xive_esb_set(pq, XIVE_ESB_PENDING);
+        return true;
+    case XIVE_ESB_OFF:
+        xive_esb_set(pq, XIVE_ESB_OFF);
+        return false;
+    default:
+         g_assert_not_reached();
+    }
+}
+
+/*
+ * XIVE Interrupt Source (or IVSE)
+ */
+
+uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xsrc->status[srcno] & 0x3;
+}
+
+uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_set(&xsrc->status[srcno], pq);
+}
+
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_trigger(&xsrc->status[srcno]);
+}
+
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_eoi(&xsrc->status[srcno]);
+}
+
+/*
+ * Forward the source event notification to the Router
+ */
+static void xive_source_notify(XiveSource *xsrc, int srcno)
+{
+
+}
+
+/*
+ * In a two pages ESB MMIO setting, even page is the trigger page, odd
+ * page is for management
+ */
+static inline bool addr_is_even(hwaddr addr, uint32_t shift)
+{
+    return !((addr >> shift) & 1);
+}
+
+static inline bool xive_source_is_trigger_page(XiveSource *xsrc, hwaddr addr)
+{
+    return xive_source_esb_has_2page(xsrc) &&
+        addr_is_even(addr, xsrc->esb_shift - 1);
+}
+
+/*
+ * ESB MMIO loads
+ *                      Trigger page    Management/EOI page

hw/intc/xive.c line 146

Quoted 10 lines
hw/intc/xive.c new file +10 -0
+ * 2 pages setting      even            odd
+ *
+ * 0x000 .. 0x3FF       -1              EOI and return 0|1
+ * 0x400 .. 0x7FF       -1              EOI and return 0|1
+ * 0x800 .. 0xBFF       -1              return PQ
+ * 0xC00 .. 0xCFF       -1              return PQ and atomically PQ=0
+ * 0xD00 .. 0xDFF       -1              return PQ and atomically PQ=0
+ * 0xE00 .. 0xDFF       -1              return PQ and atomically PQ=1
+ * 0xF00 .. 0xDFF       -1              return PQ and atomically PQ=1
+ */

I can't quite make sense of this table. What do the -1s represent, and how does it relate to the non-2page case?

Quoted 37 lines
hw/intc/xive.c new file +37 -0
+static uint64_t xive_source_esb_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    uint32_t offset = addr & 0xFFF;
+    uint32_t srcno = addr >> xsrc->esb_shift;
+    uint64_t ret = -1;
+
+    /* In a two pages ESB MMIO setting, trigger page should not be read */
+    if (xive_source_is_trigger_page(xsrc, addr)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: invalid load on IRQ %d trigger page at "
+                      "0x%"HWADDR_PRIx"\n", srcno, addr);
+        return -1;
+    }
+
+    switch (offset) {
+    case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
+        ret = xive_source_esb_eoi(xsrc, srcno);
+
+        /* Forward the source event notification for routing */
+        if (ret) {
+            xive_source_notify(xsrc, srcno);
+        }
+        break;
+
+    case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
+        ret = xive_source_esb_get(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        ret = xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB load addr %x\n",

hw/intc/xive.c line 193

Quoted 10 lines
hw/intc/xive.c new file +10 -0
+                      offset);
+    }
+
+    return ret;
+}
+
+/*
+ * ESB MMIO stores
+ *                      Trigger page    Management/EOI page
+ * 2 pages setting      even            odd

As with the previous table, I don't quite understand what the headings above mean.

Quoted 99 lines
hw/intc/xive.c new file +99 -0
+ * 0x000 .. 0x3FF       Trigger         Trigger
+ * 0x400 .. 0x7FF       Trigger         EOI
+ * 0x800 .. 0xBFF       Trigger         undefined
+ * 0xC00 .. 0xCFF       Trigger         PQ=00
+ * 0xD00 .. 0xDFF       Trigger         PQ=01
+ * 0xE00 .. 0xDFF       Trigger         PQ=10
+ * 0xF00 .. 0xDFF       Trigger         PQ=11
+ */
+static void xive_source_esb_write(void *opaque, hwaddr addr,
+                                  uint64_t value, unsigned size)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    uint32_t offset = addr & 0xFFF;
+    uint32_t srcno = addr >> xsrc->esb_shift;
+    bool notify = false;
+
+    /* In a two pages ESB MMIO setting, trigger page only triggers */
+    if (xive_source_is_trigger_page(xsrc, addr)) {
+        notify = xive_source_esb_trigger(xsrc, srcno);
+        goto out;
+    }
+
+    switch (offset) {
+    case 0 ... 0x3FF:
+        notify = xive_source_esb_trigger(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
+        if (!(xsrc->esb_flags & XIVE_SRC_STORE_EOI)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "XIVE: invalid Store EOI for IRQ %d\n", srcno);
+            return;
+        }
+
+        notify = xive_source_esb_eoi(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr %x\n",
+                      offset);
+        return;
+    }
+
+out:
+    /* Forward the source event notification for routing */
+    if (notify) {
+        xive_source_notify(xsrc, srcno);
+    }
+}
+
+static const MemoryRegionOps xive_source_esb_ops = {
+    .read = xive_source_esb_read,
+    .write = xive_source_esb_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static void xive_source_set_irq(void *opaque, int srcno, int val)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    bool notify = false;
+
+    if (val) {
+        notify = xive_source_esb_trigger(xsrc, srcno);
+    }
+
+    /* Forward the source event notification for routing */
+    if (notify) {
+        xive_source_notify(xsrc, srcno);
+    }
+}
+
+void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
+{
+    int i;
+
+    for (i = 0; i < xsrc->nr_irqs; i++) {
+        uint8_t pq = xive_source_esb_get(xsrc, i);
+
+        if (pq == XIVE_ESB_OFF) {
+            continue;
+        }
+
+        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+                       pq & XIVE_ESB_VAL_P ? 'P' : '-',

hw/intc/xive.c line 303

Quoted 10 lines
hw/intc/xive.c new file +10 -0
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+    }
+}
+
+static void xive_source_reset(DeviceState *dev)
+{
+    XiveSource *xsrc = XIVE_SOURCE(dev);
+
+    /* PQs are initialized to 0b01 which corresponds to "ints off" */
+    memset(xsrc->status, 0x1, xsrc->nr_irqs);

You've already got XIVE_ESB_OFF defined to make this a little clearer.

Quoted 88 lines
hw/intc/xive.c new file +76 -0
+}
+
+static void xive_source_realize(DeviceState *dev, Error **errp)
+{
+    XiveSource *xsrc = XIVE_SOURCE(dev);
+
+    if (!xsrc->nr_irqs) {
+        error_setg(errp, "Number of interrupt needs to be greater than 0");
+        return;
+    }
+
+    if (xsrc->esb_shift != XIVE_ESB_4K &&
+        xsrc->esb_shift != XIVE_ESB_4K_2PAGE &&
+        xsrc->esb_shift != XIVE_ESB_64K &&
+        xsrc->esb_shift != XIVE_ESB_64K_2PAGE) {
+        error_setg(errp, "Invalid ESB shift setting");
+        return;
+    }
+
+    xsrc->qirqs = qemu_allocate_irqs(xive_source_set_irq, xsrc,
+                                     xsrc->nr_irqs);
+
+    xsrc->status = g_malloc0(xsrc->nr_irqs);
+
+    memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
+                          &xive_source_esb_ops, xsrc, "xive.esb",
+                          (1ull << xsrc->esb_shift) * xsrc->nr_irqs);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &xsrc->esb_mmio);
+}
+
+static const VMStateDescription vmstate_xive_source = {
+    .name = TYPE_XIVE_SOURCE,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
+        VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+/*
+ * The default XIVE interrupt source setting for the ESB MMIOs is two
+ * 64k pages without Store EOI, to be in sync with KVM.
+ */
+static Property xive_source_properties[] = {
+    DEFINE_PROP_UINT64("flags", XiveSource, esb_flags, 0),
+    DEFINE_PROP_UINT32("nr-irqs", XiveSource, nr_irqs, 0),
+    DEFINE_PROP_UINT32("shift", XiveSource, esb_shift, XIVE_ESB_64K_2PAGE),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_source_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc    = "XIVE Interrupt Source";
+    dc->props   = xive_source_properties;
+    dc->realize = xive_source_realize;
+    dc->reset   = xive_source_reset;
+    dc->vmsd    = &vmstate_xive_source;
+}
+
+static const TypeInfo xive_source_info = {
+    .name          = TYPE_XIVE_SOURCE,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XiveSource),
+    .class_init    = xive_source_class_init,
+};
+
+static void xive_register_types(void)
+{
+    type_register_static(&xive_source_info);
+}
+
+type_init(xive_register_types)
hw/intc/Makefile.objs +1 -0
index 0e9963f5eecc..72a46ed91c31 100644
@@ -37,6 +37,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) += xive.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
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
#3 lore raw

Re: [Qemu-devel] [PATCH v5 01/36] ppc/xive: introduce a XIVE interrupt source model

Quoted 59 lines · David Gibson
Quoted 16 lines · Cédric Le Goater

The first sub-engine of the overall XIVE architecture is the Interrupt Virtualization Source Engine (IVSE). An IVSE can be integrated into another logic, like in a PCI PHB or in the main interrupt controller to manage IPIs.

Each IVSE instance is associated with an Event State Buffer (ESB) that contains a two bit state entry for each possible event source. When an event is signaled to the IVSE, by MMIO or some other means, the associated interrupt state bits are fetched from the ESB and modified. Depending on the resulting ESB state, the event is forwarded to the IVRE sub-engine of the controller doing the routing.

Each supported ESB entry is associated with either a single or a even/odd pair of pages which provides commands to manage the source:
to EOI, to turn off the source for instance.

Quoted 8 lines

On a sPAPR machine, the O/S will obtain the page address of the ESB entry associated with a source and its characteristic using the H_INT_GET_SOURCE_INFO hcall. On PowerNV, a similar OPAL call is used.

The xive_source_notify() routine is in charge forwarding the source event notification to the routing engine. It will be filled later on.

Ok, this is looking basically pretty good. Few details to query below.

Quoted 29 lines
---
 default-configs/ppc64-softmmu.mak |   1 +
 include/hw/ppc/xive.h             | 130 ++++++++++
 hw/intc/xive.c                    | 379 ++++++++++++++++++++++++++++++
 hw/intc/Makefile.objs             |   1 +
 4 files changed, 511 insertions(+)
 create mode 100644 include/hw/ppc/xive.h
 create mode 100644 hw/intc/xive.c
default-configs/ppc64-softmmu.mak +1 -0
index aec2855750d6..2d1e7c5c4668 100644
@@ -16,6 +16,7 @@ CONFIG_VIRTIO_VGA=y
 CONFIG_XICS=$(CONFIG_PSERIES)
 CONFIG_XICS_SPAPR=$(CONFIG_PSERIES)
 CONFIG_XICS_KVM=$(call land,$(CONFIG_PSERIES),$(CONFIG_KVM))
+CONFIG_XIVE=$(CONFIG_PSERIES)
 CONFIG_MEM_DEVICE=y
 CONFIG_DIMM=y
 CONFIG_SPAPR_RNG=y
include/hw/ppc/xive.h new file +2 -0
new file mode 100644
index 000000000000..5fec4b08705d
@@ -0,0 +1,130 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
Quoted 8 lines

include/hw/ppc/xive.h line 7

Quoted 5 lines
include/hw/ppc/xive.h new file +5 -0
+ *
+ * Copyright (c) 2017-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.

A cheat sheet in the top of this header with the old and new XIVE terms would quite nice to have.

Yes. It's a good place. I will put the XIVE acronyms here :

EA Event Assignment
EISN Effective Interrupt Source Number
END Event Notification Descriptor
ESB Event State Buffer
EQ Event Queue
LISN Logical Interrupt Source Number
NVT Notification Virtual Target
TIMA Thread Interrupt Management Area
...

Quoted 10 lines

include/hw/ppc/xive.h line 13

Quoted 6 lines
include/hw/ppc/xive.h new file +6 -0
+ */
+
+#ifndef PPC_XIVE_H
+#define PPC_XIVE_H
+
+#include "hw/sysbus.h"

So, I'm a bit dubious about making the XiveSource a SysBus device - I'm concerned it won't play well with tying it into the other devices like PHB that "own" it in real hardware.

It does but I can take a look at changing it to a DeviceState. The reset handlers might be a concern.

Quoted 2 lines

I think we'd be better off making it a direct descendent of TYPE_DEVICE which constructs the MMIO region, but doesn't map it.

At a moment, I started working on a XiveESB object doing what I think you are suggesting and I removed it. I am reluctant adding more complexity now, the patchset is just growing and growing ...

But I agree there are fundamentals to get right for KVM. Let's talk about it after you have looked at the overall patchset, at least up to KVM initial support.

Quoted 3 lines

Then we can havea SysBusDevice (and/or other) wrapper which instantiates the XiveSource core and maps it into somewhere accessible.

The XIVE controller model does the mapping of the source currently. In the case of sPAPR, the controller model controls the TIMA and for PowerNV, there are quite few others MMIO regions to handle.

Quoted 44 lines
Quoted 44 lines
include/hw/ppc/xive.h new file +43 -0
+
+/*
+ * XIVE Interrupt Source
+ */
+
+#define TYPE_XIVE_SOURCE "xive-source"
+#define XIVE_SOURCE(obj) OBJECT_CHECK(XiveSource, (obj), TYPE_XIVE_SOURCE)
+
+/*
+ * XIVE Interrupt Source characteristics, which define how the ESB are
+ * controlled.
+ */
+#define XIVE_SRC_H_INT_ESB     0x1 /* ESB managed with hcall H_INT_ESB */
+#define XIVE_SRC_STORE_EOI     0x2 /* Store EOI supported */
+
+typedef struct XiveSource {
+    SysBusDevice parent;
+
+    /* IRQs */
+    uint32_t        nr_irqs;
+    qemu_irq        *qirqs;
+
+    /* PQ bits */
+    uint8_t         *status;
+
+    /* ESB memory region */
+    uint64_t        esb_flags;
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+} XiveSource;
+
+/*
+ * ESB MMIO setting. Can be one page, for both source triggering and
+ * source management, or two different pages. See below for magic
+ * values.
+ */
+#define XIVE_ESB_4K          12 /* PSI HB only */
+#define XIVE_ESB_4K_2PAGE    13
+#define XIVE_ESB_64K         16
+#define XIVE_ESB_64K_2PAGE   17
+
+static inline bool xive_source_esb_has_2page(XiveSource *xsrc)
+{
Quoted 8 lines

include/hw/ppc/xive.h line 62

Quoted 6 lines
include/hw/ppc/xive.h new file +6 -0
+    return xsrc->esb_shift == XIVE_ESB_64K_2PAGE ||
+        xsrc->esb_shift == XIVE_ESB_4K_2PAGE;
+}
+
+/* The trigger page is always the first/even page */
+static inline hwaddr xive_source_esb_page(XiveSource *xsrc, uint32_t srcno)

This function doesn't appear to be used anywhere except..

It's used in patch 16 adding the hcalls also.

Quoted 10 lines

include/hw/ppc/xive.h line 69

Quoted 7 lines
include/hw/ppc/xive.h new file +7 -0
+{
+    assert(srcno < xsrc->nr_irqs);
+    return (1ull << xsrc->esb_shift) * srcno;
+}
+
+/* In a two pages ESB MMIO setting, the odd page is for management */
+static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)

..here, and this function doesn't appear to be used anywhere.

It's used in patch 16 adding the hcalls and patch 23 for KVM.

This is basic ESB support which I thought belong to the patch on sources.

Quoted 208 lines
Quoted 208 lines
include/hw/ppc/xive.h new file +61 -0
+{
+    hwaddr addr = xive_source_esb_page(xsrc, srcno);
+
+    if (xive_source_esb_has_2page(xsrc)) {
+        addr += (1 << (xsrc->esb_shift - 1));
+    }
+
+    return addr;
+}
+
+/*
+ * Each interrupt source has a 2-bit state machine which can be
+ * controlled by MMIO. P indicates that an interrupt is pending (has
+ * been sent to a queue and is waiting for an EOI). Q indicates that
+ * the interrupt has been triggered while pending.
+ *
+ * This acts as a coalescing mechanism in order to guarantee that a
+ * given interrupt only occurs at most once in a queue.
+ *
+ * When doing an EOI, the Q bit will indicate if the interrupt
+ * needs to be re-triggered.
+ */
+#define XIVE_ESB_VAL_P        0x2
+#define XIVE_ESB_VAL_Q        0x1
+
+#define XIVE_ESB_RESET        0x0
+#define XIVE_ESB_PENDING      XIVE_ESB_VAL_P
+#define XIVE_ESB_QUEUED       (XIVE_ESB_VAL_P | XIVE_ESB_VAL_Q)
+#define XIVE_ESB_OFF          XIVE_ESB_VAL_Q
+
+/*
+ * "magic" Event State Buffer (ESB) MMIO offsets.
+ *
+ * The following offsets into the ESB MMIO allow to read or manipulate
+ * the PQ bits. They must be used with an 8-byte load instruction.
+ * They all return the previous state of the interrupt (atomically).
+ *
+ * Additionally, some ESB pages support doing an EOI via a store and
+ * some ESBs support doing a trigger via a separate trigger page.
+ */
+#define XIVE_ESB_STORE_EOI      0x400 /* Store */
+#define XIVE_ESB_LOAD_EOI       0x000 /* Load */
+#define XIVE_ESB_GET            0x800 /* Load */
+#define XIVE_ESB_SET_PQ_00      0xc00 /* Load */
+#define XIVE_ESB_SET_PQ_01      0xd00 /* Load */
+#define XIVE_ESB_SET_PQ_10      0xe00 /* Load */
+#define XIVE_ESB_SET_PQ_11      0xf00 /* Load */
+
+uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno);
+uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq);
+
+void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset,
+                                Monitor *mon);
+
+static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return xsrc->qirqs[srcno];
+}
+
+#endif /* PPC_XIVE_H */
hw/intc/xive.c new file +140 -0
new file mode 100644
index 000000000000..f7621f84828c
@@ -0,0 +1,379 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2017-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "target/ppc/cpu.h"
+#include "sysemu/cpus.h"
+#include "sysemu/dma.h"
+#include "monitor/monitor.h"
+#include "hw/ppc/xive.h"
+
+/*
+ * XIVE ESB helpers
+ */
+
+static uint8_t xive_esb_set(uint8_t *pq, uint8_t value)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    *pq &= ~0x3;
+    *pq |= value & 0x3;
+
+    return old_pq;
+}
+
+static bool xive_esb_trigger(uint8_t *pq)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+        xive_esb_set(pq, XIVE_ESB_PENDING);
+        return true;
+    case XIVE_ESB_PENDING:
+    case XIVE_ESB_QUEUED:
+        xive_esb_set(pq, XIVE_ESB_QUEUED);
+        return false;
+    case XIVE_ESB_OFF:
+        xive_esb_set(pq, XIVE_ESB_OFF);
+        return false;
+    default:
+         g_assert_not_reached();
+    }
+}
+
+static bool xive_esb_eoi(uint8_t *pq)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+    case XIVE_ESB_PENDING:
+        xive_esb_set(pq, XIVE_ESB_RESET);
+        return false;
+    case XIVE_ESB_QUEUED:
+        xive_esb_set(pq, XIVE_ESB_PENDING);
+        return true;
+    case XIVE_ESB_OFF:
+        xive_esb_set(pq, XIVE_ESB_OFF);
+        return false;
+    default:
+         g_assert_not_reached();
+    }
+}
+
+/*
+ * XIVE Interrupt Source (or IVSE)
+ */
+
+uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xsrc->status[srcno] & 0x3;
+}
+
+uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_set(&xsrc->status[srcno], pq);
+}
+
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_trigger(&xsrc->status[srcno]);
+}
+
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_eoi(&xsrc->status[srcno]);
+}
+
+/*
+ * Forward the source event notification to the Router
+ */
+static void xive_source_notify(XiveSource *xsrc, int srcno)
+{
+
+}
+
+/*
+ * In a two pages ESB MMIO setting, even page is the trigger page, odd
+ * page is for management
+ */
+static inline bool addr_is_even(hwaddr addr, uint32_t shift)
+{
+    return !((addr >> shift) & 1);
+}
+
+static inline bool xive_source_is_trigger_page(XiveSource *xsrc, hwaddr addr)
+{
+    return xive_source_esb_has_2page(xsrc) &&
+        addr_is_even(addr, xsrc->esb_shift - 1);
+}
+
+/*
+ * ESB MMIO loads
+ *                      Trigger page    Management/EOI page
+ * 2 pages setting      even            odd
+ *
+ * 0x000 .. 0x3FF       -1              EOI and return 0|1
+ * 0x400 .. 0x7FF       -1              EOI and return 0|1
Quoted 8 lines

hw/intc/xive.c line 146

Quoted 6 lines
hw/intc/xive.c new file +6 -0
+ * 0x800 .. 0xBFF       -1              return PQ
+ * 0xC00 .. 0xCFF       -1              return PQ and atomically PQ=0
+ * 0xD00 .. 0xDFF       -1              return PQ and atomically PQ=0
+ * 0xE00 .. 0xDFF       -1              return PQ and atomically PQ=1
+ * 0xF00 .. 0xDFF       -1              return PQ and atomically PQ=1
+ */

I can't quite make sense of this table. What do the -1s represent,

the value returned by the load.

Quoted 1 line

and how does it relate to the non-2page case?

one page ESB support trigger and management on the same page. So for loads, the odd page behavior applies.

Quoted 42 lines
Quoted 42 lines
hw/intc/xive.c new file +42 -0
+static uint64_t xive_source_esb_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    uint32_t offset = addr & 0xFFF;
+    uint32_t srcno = addr >> xsrc->esb_shift;
+    uint64_t ret = -1;
+
+    /* In a two pages ESB MMIO setting, trigger page should not be read */
+    if (xive_source_is_trigger_page(xsrc, addr)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: invalid load on IRQ %d trigger page at "
+                      "0x%"HWADDR_PRIx"\n", srcno, addr);
+        return -1;
+    }
+
+    switch (offset) {
+    case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
+        ret = xive_source_esb_eoi(xsrc, srcno);
+
+        /* Forward the source event notification for routing */
+        if (ret) {
+            xive_source_notify(xsrc, srcno);
+        }
+        break;
+
+    case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
+        ret = xive_source_esb_get(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        ret = xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB load addr %x\n",
+                      offset);
+    }
+
+    return ret;
+}
Quoted 8 lines

hw/intc/xive.c line 193

Quoted 5 lines
hw/intc/xive.c new file +5 -0
+
+/*
+ * ESB MMIO stores
+ *                      Trigger page    Management/EOI page
+ * 2 pages setting      even            odd

As with the previous table, I don't quite understand what the headings above mean.

one page ESB support trigger and management on the same page. So for stores, the odd page behavior applies.

The headings can be improved. I will think of something.

Quoted 103 lines
Quoted 103 lines
hw/intc/xive.c new file +103 -0
+ * 0x000 .. 0x3FF       Trigger         Trigger
+ * 0x400 .. 0x7FF       Trigger         EOI
+ * 0x800 .. 0xBFF       Trigger         undefined
+ * 0xC00 .. 0xCFF       Trigger         PQ=00
+ * 0xD00 .. 0xDFF       Trigger         PQ=01
+ * 0xE00 .. 0xDFF       Trigger         PQ=10
+ * 0xF00 .. 0xDFF       Trigger         PQ=11
+ */
+static void xive_source_esb_write(void *opaque, hwaddr addr,
+                                  uint64_t value, unsigned size)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    uint32_t offset = addr & 0xFFF;
+    uint32_t srcno = addr >> xsrc->esb_shift;
+    bool notify = false;
+
+    /* In a two pages ESB MMIO setting, trigger page only triggers */
+    if (xive_source_is_trigger_page(xsrc, addr)) {
+        notify = xive_source_esb_trigger(xsrc, srcno);
+        goto out;
+    }
+
+    switch (offset) {
+    case 0 ... 0x3FF:
+        notify = xive_source_esb_trigger(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
+        if (!(xsrc->esb_flags & XIVE_SRC_STORE_EOI)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "XIVE: invalid Store EOI for IRQ %d\n", srcno);
+            return;
+        }
+
+        notify = xive_source_esb_eoi(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr %x\n",
+                      offset);
+        return;
+    }
+
+out:
+    /* Forward the source event notification for routing */
+    if (notify) {
+        xive_source_notify(xsrc, srcno);
+    }
+}
+
+static const MemoryRegionOps xive_source_esb_ops = {
+    .read = xive_source_esb_read,
+    .write = xive_source_esb_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static void xive_source_set_irq(void *opaque, int srcno, int val)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    bool notify = false;
+
+    if (val) {
+        notify = xive_source_esb_trigger(xsrc, srcno);
+    }
+
+    /* Forward the source event notification for routing */
+    if (notify) {
+        xive_source_notify(xsrc, srcno);
+    }
+}
+
+void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
+{
+    int i;
+
+    for (i = 0; i < xsrc->nr_irqs; i++) {
+        uint8_t pq = xive_source_esb_get(xsrc, i);
+
+        if (pq == XIVE_ESB_OFF) {
+            continue;
+        }
+
+        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+                       pq & XIVE_ESB_VAL_P ? 'P' : '-',
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+    }
+}
+
Quoted 8 lines

hw/intc/xive.c line 303

Quoted 6 lines
hw/intc/xive.c new file +6 -0
+static void xive_source_reset(DeviceState *dev)
+{
+    XiveSource *xsrc = XIVE_SOURCE(dev);
+
+    /* PQs are initialized to 0b01 which corresponds to "ints off" */
+    memset(xsrc->status, 0x1, xsrc->nr_irqs);

You've already got XIVE_ESB_OFF defined to make this a little clearer.

Sure.

Thanks,

C.

Quoted 90 lines
Quoted 89 lines
hw/intc/xive.c new file +76 -0
+}
+
+static void xive_source_realize(DeviceState *dev, Error **errp)
+{
+    XiveSource *xsrc = XIVE_SOURCE(dev);
+
+    if (!xsrc->nr_irqs) {
+        error_setg(errp, "Number of interrupt needs to be greater than 0");
+        return;
+    }
+
+    if (xsrc->esb_shift != XIVE_ESB_4K &&
+        xsrc->esb_shift != XIVE_ESB_4K_2PAGE &&
+        xsrc->esb_shift != XIVE_ESB_64K &&
+        xsrc->esb_shift != XIVE_ESB_64K_2PAGE) {
+        error_setg(errp, "Invalid ESB shift setting");
+        return;
+    }
+
+    xsrc->qirqs = qemu_allocate_irqs(xive_source_set_irq, xsrc,
+                                     xsrc->nr_irqs);
+
+    xsrc->status = g_malloc0(xsrc->nr_irqs);
+
+    memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
+                          &xive_source_esb_ops, xsrc, "xive.esb",
+                          (1ull << xsrc->esb_shift) * xsrc->nr_irqs);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &xsrc->esb_mmio);
+}
+
+static const VMStateDescription vmstate_xive_source = {
+    .name = TYPE_XIVE_SOURCE,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
+        VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+/*
+ * The default XIVE interrupt source setting for the ESB MMIOs is two
+ * 64k pages without Store EOI, to be in sync with KVM.
+ */
+static Property xive_source_properties[] = {
+    DEFINE_PROP_UINT64("flags", XiveSource, esb_flags, 0),
+    DEFINE_PROP_UINT32("nr-irqs", XiveSource, nr_irqs, 0),
+    DEFINE_PROP_UINT32("shift", XiveSource, esb_shift, XIVE_ESB_64K_2PAGE),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_source_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc    = "XIVE Interrupt Source";
+    dc->props   = xive_source_properties;
+    dc->realize = xive_source_realize;
+    dc->reset   = xive_source_reset;
+    dc->vmsd    = &vmstate_xive_source;
+}
+
+static const TypeInfo xive_source_info = {
+    .name          = TYPE_XIVE_SOURCE,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XiveSource),
+    .class_init    = xive_source_class_init,
+};
+
+static void xive_register_types(void)
+{
+    type_register_static(&xive_source_info);
+}
+
+type_init(xive_register_types)
hw/intc/Makefile.objs +1 -0
index 0e9963f5eecc..72a46ed91c31 100644
@@ -37,6 +37,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) += xive.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
#4 lore raw

Re: [Qemu-devel] [PATCH v5 01/36] ppc/xive: introduce a XIVE interrupt source model

Quoted 71 lines · Cédric Le Goater
Quoted 59 lines · David Gibson
Quoted 16 lines · Cédric Le Goater

The first sub-engine of the overall XIVE architecture is the Interrupt Virtualization Source Engine (IVSE). An IVSE can be integrated into another logic, like in a PCI PHB or in the main interrupt controller to manage IPIs.

Each IVSE instance is associated with an Event State Buffer (ESB) that contains a two bit state entry for each possible event source. When an event is signaled to the IVSE, by MMIO or some other means, the associated interrupt state bits are fetched from the ESB and modified. Depending on the resulting ESB state, the event is forwarded to the IVRE sub-engine of the controller doing the routing.

Each supported ESB entry is associated with either a single or a even/odd pair of pages which provides commands to manage the source:
to EOI, to turn off the source for instance.

Quoted 8 lines

On a sPAPR machine, the O/S will obtain the page address of the ESB entry associated with a source and its characteristic using the H_INT_GET_SOURCE_INFO hcall. On PowerNV, a similar OPAL call is used.

The xive_source_notify() routine is in charge forwarding the source event notification to the routing engine. It will be filled later on.

Ok, this is looking basically pretty good. Few details to query below.

Quoted 29 lines
---
 default-configs/ppc64-softmmu.mak |   1 +
 include/hw/ppc/xive.h             | 130 ++++++++++
 hw/intc/xive.c                    | 379 ++++++++++++++++++++++++++++++
 hw/intc/Makefile.objs             |   1 +
 4 files changed, 511 insertions(+)
 create mode 100644 include/hw/ppc/xive.h
 create mode 100644 hw/intc/xive.c
default-configs/ppc64-softmmu.mak +1 -0
index aec2855750d6..2d1e7c5c4668 100644
@@ -16,6 +16,7 @@ CONFIG_VIRTIO_VGA=y
 CONFIG_XICS=$(CONFIG_PSERIES)
 CONFIG_XICS_SPAPR=$(CONFIG_PSERIES)
 CONFIG_XICS_KVM=$(call land,$(CONFIG_PSERIES),$(CONFIG_KVM))
+CONFIG_XIVE=$(CONFIG_PSERIES)
 CONFIG_MEM_DEVICE=y
 CONFIG_DIMM=y
 CONFIG_SPAPR_RNG=y
include/hw/ppc/xive.h new file +2 -0
new file mode 100644
index 000000000000..5fec4b08705d
@@ -0,0 +1,130 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
Quoted 8 lines

include/hw/ppc/xive.h line 7

Quoted 5 lines
include/hw/ppc/xive.h new file +5 -0
+ *
+ * Copyright (c) 2017-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.

A cheat sheet in the top of this header with the old and new XIVE terms would quite nice to have.

Yes. It's a good place. I will put the XIVE acronyms here :

Quoted 9 lines

EA Event Assignment
EISN Effective Interrupt Source Number
END Event Notification Descriptor
ESB Event State Buffer
EQ Event Queue
LISN Logical Interrupt Source Number
NVT Notification Virtual Target
TIMA Thread Interrupt Management Area
...

That sounds good, but what I'd also like is showing that NVT == VP and EAS == IVT and so forth.

Quoted 11 lines
Quoted 10 lines

include/hw/ppc/xive.h line 13

Quoted 6 lines
include/hw/ppc/xive.h new file +6 -0
+ */
+
+#ifndef PPC_XIVE_H
+#define PPC_XIVE_H
+
+#include "hw/sysbus.h"

So, I'm a bit dubious about making the XiveSource a SysBus device - I'm concerned it won't play well with tying it into the other devices like PHB that "own" it in real hardware.

Quoted 2 lines

It does but I can take a look at changing it to a DeviceState. The reset handlers might be a concern.

As "non bus" device I think you'd need to register your own reset handler rather than just setting dc->reset. Otherwise, I think that should work.

Quoted 10 lines
Quoted 2 lines

I think we'd be better off making it a direct descendent of TYPE_DEVICE which constructs the MMIO region, but doesn't map it.

At a moment, I started working on a XiveESB object doing what I think you are suggesting and I removed it. I am reluctant adding more complexity now, the patchset is just growing and growing ...

But I agree there are fundamentals to get right for KVM. Let's talk about it after you have looked at the overall patchset, at least up to KVM initial support.

Hm, ok.

Quoted 5 lines
Quoted 3 lines

Then we can havea SysBusDevice (and/or other) wrapper which instantiates the XiveSource core and maps it into somewhere accessible.

The XIVE controller model does the mapping of the source currently.

I'm.. I'm not sure what you mean by that. We have a
sysbus_init_mmio() right here which effectively maps in the MMIO region AFAICT.

Quoted 563 lines

In the case of sPAPR, the controller model controls the TIMA and for PowerNV, there are quite few others MMIO regions to handle.

Quoted 44 lines
Quoted 44 lines
include/hw/ppc/xive.h new file +43 -0
+
+/*
+ * XIVE Interrupt Source
+ */
+
+#define TYPE_XIVE_SOURCE "xive-source"
+#define XIVE_SOURCE(obj) OBJECT_CHECK(XiveSource, (obj), TYPE_XIVE_SOURCE)
+
+/*
+ * XIVE Interrupt Source characteristics, which define how the ESB are
+ * controlled.
+ */
+#define XIVE_SRC_H_INT_ESB     0x1 /* ESB managed with hcall H_INT_ESB */
+#define XIVE_SRC_STORE_EOI     0x2 /* Store EOI supported */
+
+typedef struct XiveSource {
+    SysBusDevice parent;
+
+    /* IRQs */
+    uint32_t        nr_irqs;
+    qemu_irq        *qirqs;
+
+    /* PQ bits */
+    uint8_t         *status;
+
+    /* ESB memory region */
+    uint64_t        esb_flags;
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+} XiveSource;
+
+/*
+ * ESB MMIO setting. Can be one page, for both source triggering and
+ * source management, or two different pages. See below for magic
+ * values.
+ */
+#define XIVE_ESB_4K          12 /* PSI HB only */
+#define XIVE_ESB_4K_2PAGE    13
+#define XIVE_ESB_64K         16
+#define XIVE_ESB_64K_2PAGE   17
+
+static inline bool xive_source_esb_has_2page(XiveSource *xsrc)
+{
Quoted 8 lines

include/hw/ppc/xive.h line 62

Quoted 6 lines
include/hw/ppc/xive.h new file +6 -0
+    return xsrc->esb_shift == XIVE_ESB_64K_2PAGE ||
+        xsrc->esb_shift == XIVE_ESB_4K_2PAGE;
+}
+
+/* The trigger page is always the first/even page */
+static inline hwaddr xive_source_esb_page(XiveSource *xsrc, uint32_t srcno)

This function doesn't appear to be used anywhere except..

It's used in patch 16 adding the hcalls also.

Quoted 10 lines

include/hw/ppc/xive.h line 69

Quoted 7 lines
include/hw/ppc/xive.h new file +7 -0
+{
+    assert(srcno < xsrc->nr_irqs);
+    return (1ull << xsrc->esb_shift) * srcno;
+}
+
+/* In a two pages ESB MMIO setting, the odd page is for management */
+static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)

..here, and this function doesn't appear to be used anywhere.

It's used in patch 16 adding the hcalls and patch 23 for KVM.

This is basic ESB support which I thought belong to the patch on sources.

Quoted 208 lines
Quoted 208 lines
include/hw/ppc/xive.h new file +61 -0
+{
+    hwaddr addr = xive_source_esb_page(xsrc, srcno);
+
+    if (xive_source_esb_has_2page(xsrc)) {
+        addr += (1 << (xsrc->esb_shift - 1));
+    }
+
+    return addr;
+}
+
+/*
+ * Each interrupt source has a 2-bit state machine which can be
+ * controlled by MMIO. P indicates that an interrupt is pending (has
+ * been sent to a queue and is waiting for an EOI). Q indicates that
+ * the interrupt has been triggered while pending.
+ *
+ * This acts as a coalescing mechanism in order to guarantee that a
+ * given interrupt only occurs at most once in a queue.
+ *
+ * When doing an EOI, the Q bit will indicate if the interrupt
+ * needs to be re-triggered.
+ */
+#define XIVE_ESB_VAL_P        0x2
+#define XIVE_ESB_VAL_Q        0x1
+
+#define XIVE_ESB_RESET        0x0
+#define XIVE_ESB_PENDING      XIVE_ESB_VAL_P
+#define XIVE_ESB_QUEUED       (XIVE_ESB_VAL_P | XIVE_ESB_VAL_Q)
+#define XIVE_ESB_OFF          XIVE_ESB_VAL_Q
+
+/*
+ * "magic" Event State Buffer (ESB) MMIO offsets.
+ *
+ * The following offsets into the ESB MMIO allow to read or manipulate
+ * the PQ bits. They must be used with an 8-byte load instruction.
+ * They all return the previous state of the interrupt (atomically).
+ *
+ * Additionally, some ESB pages support doing an EOI via a store and
+ * some ESBs support doing a trigger via a separate trigger page.
+ */
+#define XIVE_ESB_STORE_EOI      0x400 /* Store */
+#define XIVE_ESB_LOAD_EOI       0x000 /* Load */
+#define XIVE_ESB_GET            0x800 /* Load */
+#define XIVE_ESB_SET_PQ_00      0xc00 /* Load */
+#define XIVE_ESB_SET_PQ_01      0xd00 /* Load */
+#define XIVE_ESB_SET_PQ_10      0xe00 /* Load */
+#define XIVE_ESB_SET_PQ_11      0xf00 /* Load */
+
+uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno);
+uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq);
+
+void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset,
+                                Monitor *mon);
+
+static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return xsrc->qirqs[srcno];
+}
+
+#endif /* PPC_XIVE_H */
hw/intc/xive.c new file +140 -0
new file mode 100644
index 000000000000..f7621f84828c
@@ -0,0 +1,379 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2017-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "target/ppc/cpu.h"
+#include "sysemu/cpus.h"
+#include "sysemu/dma.h"
+#include "monitor/monitor.h"
+#include "hw/ppc/xive.h"
+
+/*
+ * XIVE ESB helpers
+ */
+
+static uint8_t xive_esb_set(uint8_t *pq, uint8_t value)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    *pq &= ~0x3;
+    *pq |= value & 0x3;
+
+    return old_pq;
+}
+
+static bool xive_esb_trigger(uint8_t *pq)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+        xive_esb_set(pq, XIVE_ESB_PENDING);
+        return true;
+    case XIVE_ESB_PENDING:
+    case XIVE_ESB_QUEUED:
+        xive_esb_set(pq, XIVE_ESB_QUEUED);
+        return false;
+    case XIVE_ESB_OFF:
+        xive_esb_set(pq, XIVE_ESB_OFF);
+        return false;
+    default:
+         g_assert_not_reached();
+    }
+}
+
+static bool xive_esb_eoi(uint8_t *pq)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+    case XIVE_ESB_PENDING:
+        xive_esb_set(pq, XIVE_ESB_RESET);
+        return false;
+    case XIVE_ESB_QUEUED:
+        xive_esb_set(pq, XIVE_ESB_PENDING);
+        return true;
+    case XIVE_ESB_OFF:
+        xive_esb_set(pq, XIVE_ESB_OFF);
+        return false;
+    default:
+         g_assert_not_reached();
+    }
+}
+
+/*
+ * XIVE Interrupt Source (or IVSE)
+ */
+
+uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xsrc->status[srcno] & 0x3;
+}
+
+uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_set(&xsrc->status[srcno], pq);
+}
+
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_trigger(&xsrc->status[srcno]);
+}
+
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_eoi(&xsrc->status[srcno]);
+}
+
+/*
+ * Forward the source event notification to the Router
+ */
+static void xive_source_notify(XiveSource *xsrc, int srcno)
+{
+
+}
+
+/*
+ * In a two pages ESB MMIO setting, even page is the trigger page, odd
+ * page is for management
+ */
+static inline bool addr_is_even(hwaddr addr, uint32_t shift)
+{
+    return !((addr >> shift) & 1);
+}
+
+static inline bool xive_source_is_trigger_page(XiveSource *xsrc, hwaddr addr)
+{
+    return xive_source_esb_has_2page(xsrc) &&
+        addr_is_even(addr, xsrc->esb_shift - 1);
+}
+
+/*
+ * ESB MMIO loads
+ *                      Trigger page    Management/EOI page
+ * 2 pages setting      even            odd
+ *
+ * 0x000 .. 0x3FF       -1              EOI and return 0|1
+ * 0x400 .. 0x7FF       -1              EOI and return 0|1
Quoted 8 lines

hw/intc/xive.c line 146

Quoted 6 lines
hw/intc/xive.c new file +6 -0
+ * 0x800 .. 0xBFF       -1              return PQ
+ * 0xC00 .. 0xCFF       -1              return PQ and atomically PQ=0
+ * 0xD00 .. 0xDFF       -1              return PQ and atomically PQ=0
+ * 0xE00 .. 0xDFF       -1              return PQ and atomically PQ=1
+ * 0xF00 .. 0xDFF       -1              return PQ and atomically PQ=1
+ */

I can't quite make sense of this table. What do the -1s represent,

the value returned by the load.

Quoted 1 line

and how does it relate to the non-2page case?

one page ESB support trigger and management on the same page. So for loads, the odd page behavior applies.

Quoted 42 lines
Quoted 42 lines
hw/intc/xive.c new file +42 -0
+static uint64_t xive_source_esb_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    uint32_t offset = addr & 0xFFF;
+    uint32_t srcno = addr >> xsrc->esb_shift;
+    uint64_t ret = -1;
+
+    /* In a two pages ESB MMIO setting, trigger page should not be read */
+    if (xive_source_is_trigger_page(xsrc, addr)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: invalid load on IRQ %d trigger page at "
+                      "0x%"HWADDR_PRIx"\n", srcno, addr);
+        return -1;
+    }
+
+    switch (offset) {
+    case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
+        ret = xive_source_esb_eoi(xsrc, srcno);
+
+        /* Forward the source event notification for routing */
+        if (ret) {
+            xive_source_notify(xsrc, srcno);
+        }
+        break;
+
+    case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
+        ret = xive_source_esb_get(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        ret = xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB load addr %x\n",
+                      offset);
+    }
+
+    return ret;
+}
Quoted 8 lines

hw/intc/xive.c line 193

Quoted 5 lines
hw/intc/xive.c new file +5 -0
+
+/*
+ * ESB MMIO stores
+ *                      Trigger page    Management/EOI page
+ * 2 pages setting      even            odd

As with the previous table, I don't quite understand what the headings above mean.

one page ESB support trigger and management on the same page. So for stores, the odd page behavior applies.

The headings can be improved. I will think of something.

Quoted 103 lines
Quoted 103 lines
hw/intc/xive.c new file +103 -0
+ * 0x000 .. 0x3FF       Trigger         Trigger
+ * 0x400 .. 0x7FF       Trigger         EOI
+ * 0x800 .. 0xBFF       Trigger         undefined
+ * 0xC00 .. 0xCFF       Trigger         PQ=00
+ * 0xD00 .. 0xDFF       Trigger         PQ=01
+ * 0xE00 .. 0xDFF       Trigger         PQ=10
+ * 0xF00 .. 0xDFF       Trigger         PQ=11
+ */
+static void xive_source_esb_write(void *opaque, hwaddr addr,
+                                  uint64_t value, unsigned size)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    uint32_t offset = addr & 0xFFF;
+    uint32_t srcno = addr >> xsrc->esb_shift;
+    bool notify = false;
+
+    /* In a two pages ESB MMIO setting, trigger page only triggers */
+    if (xive_source_is_trigger_page(xsrc, addr)) {
+        notify = xive_source_esb_trigger(xsrc, srcno);
+        goto out;
+    }
+
+    switch (offset) {
+    case 0 ... 0x3FF:
+        notify = xive_source_esb_trigger(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
+        if (!(xsrc->esb_flags & XIVE_SRC_STORE_EOI)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "XIVE: invalid Store EOI for IRQ %d\n", srcno);
+            return;
+        }
+
+        notify = xive_source_esb_eoi(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr %x\n",
+                      offset);
+        return;
+    }
+
+out:
+    /* Forward the source event notification for routing */
+    if (notify) {
+        xive_source_notify(xsrc, srcno);
+    }
+}
+
+static const MemoryRegionOps xive_source_esb_ops = {
+    .read = xive_source_esb_read,
+    .write = xive_source_esb_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static void xive_source_set_irq(void *opaque, int srcno, int val)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    bool notify = false;
+
+    if (val) {
+        notify = xive_source_esb_trigger(xsrc, srcno);
+    }
+
+    /* Forward the source event notification for routing */
+    if (notify) {
+        xive_source_notify(xsrc, srcno);
+    }
+}
+
+void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
+{
+    int i;
+
+    for (i = 0; i < xsrc->nr_irqs; i++) {
+        uint8_t pq = xive_source_esb_get(xsrc, i);
+
+        if (pq == XIVE_ESB_OFF) {
+            continue;
+        }
+
+        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+                       pq & XIVE_ESB_VAL_P ? 'P' : '-',
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+    }
+}
+
Quoted 8 lines

hw/intc/xive.c line 303

Quoted 6 lines
hw/intc/xive.c new file +6 -0
+static void xive_source_reset(DeviceState *dev)
+{
+    XiveSource *xsrc = XIVE_SOURCE(dev);
+
+    /* PQs are initialized to 0b01 which corresponds to "ints off" */
+    memset(xsrc->status, 0x1, xsrc->nr_irqs);

You've already got XIVE_ESB_OFF defined to make this a little clearer.

Sure.

Thanks,

C.

Quoted 90 lines
Quoted 89 lines
hw/intc/xive.c new file +76 -0
+}
+
+static void xive_source_realize(DeviceState *dev, Error **errp)
+{
+    XiveSource *xsrc = XIVE_SOURCE(dev);
+
+    if (!xsrc->nr_irqs) {
+        error_setg(errp, "Number of interrupt needs to be greater than 0");
+        return;
+    }
+
+    if (xsrc->esb_shift != XIVE_ESB_4K &&
+        xsrc->esb_shift != XIVE_ESB_4K_2PAGE &&
+        xsrc->esb_shift != XIVE_ESB_64K &&
+        xsrc->esb_shift != XIVE_ESB_64K_2PAGE) {
+        error_setg(errp, "Invalid ESB shift setting");
+        return;
+    }
+
+    xsrc->qirqs = qemu_allocate_irqs(xive_source_set_irq, xsrc,
+                                     xsrc->nr_irqs);
+
+    xsrc->status = g_malloc0(xsrc->nr_irqs);
+
+    memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
+                          &xive_source_esb_ops, xsrc, "xive.esb",
+                          (1ull << xsrc->esb_shift) * xsrc->nr_irqs);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &xsrc->esb_mmio);
+}
+
+static const VMStateDescription vmstate_xive_source = {
+    .name = TYPE_XIVE_SOURCE,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
+        VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+/*
+ * The default XIVE interrupt source setting for the ESB MMIOs is two
+ * 64k pages without Store EOI, to be in sync with KVM.
+ */
+static Property xive_source_properties[] = {
+    DEFINE_PROP_UINT64("flags", XiveSource, esb_flags, 0),
+    DEFINE_PROP_UINT32("nr-irqs", XiveSource, nr_irqs, 0),
+    DEFINE_PROP_UINT32("shift", XiveSource, esb_shift, XIVE_ESB_64K_2PAGE),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_source_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc    = "XIVE Interrupt Source";
+    dc->props   = xive_source_properties;
+    dc->realize = xive_source_realize;
+    dc->reset   = xive_source_reset;
+    dc->vmsd    = &vmstate_xive_source;
+}
+
+static const TypeInfo xive_source_info = {
+    .name          = TYPE_XIVE_SOURCE,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XiveSource),
+    .class_init    = xive_source_class_init,
+};
+
+static void xive_register_types(void)
+{
+    type_register_static(&xive_source_info);
+}
+
+type_init(xive_register_types)
hw/intc/Makefile.objs +1 -0
index 0e9963f5eecc..72a46ed91c31 100644
@@ -37,6 +37,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) += xive.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
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
#5 lore raw

Re: [Qemu-devel] [PATCH v5 01/36] ppc/xive: introduce a XIVE interrupt source model

Quoted 82 lines · David Gibson
Quoted 71 lines · Cédric Le Goater
Quoted 59 lines · David Gibson
Quoted 16 lines · Cédric Le Goater

The first sub-engine of the overall XIVE architecture is the Interrupt Virtualization Source Engine (IVSE). An IVSE can be integrated into another logic, like in a PCI PHB or in the main interrupt controller to manage IPIs.

Each IVSE instance is associated with an Event State Buffer (ESB) that contains a two bit state entry for each possible event source. When an event is signaled to the IVSE, by MMIO or some other means, the associated interrupt state bits are fetched from the ESB and modified. Depending on the resulting ESB state, the event is forwarded to the IVRE sub-engine of the controller doing the routing.

Each supported ESB entry is associated with either a single or a even/odd pair of pages which provides commands to manage the source:
to EOI, to turn off the source for instance.

Quoted 8 lines

On a sPAPR machine, the O/S will obtain the page address of the ESB entry associated with a source and its characteristic using the H_INT_GET_SOURCE_INFO hcall. On PowerNV, a similar OPAL call is used.

The xive_source_notify() routine is in charge forwarding the source event notification to the routing engine. It will be filled later on.

Ok, this is looking basically pretty good. Few details to query below.

Quoted 29 lines
---
 default-configs/ppc64-softmmu.mak |   1 +
 include/hw/ppc/xive.h             | 130 ++++++++++
 hw/intc/xive.c                    | 379 ++++++++++++++++++++++++++++++
 hw/intc/Makefile.objs             |   1 +
 4 files changed, 511 insertions(+)
 create mode 100644 include/hw/ppc/xive.h
 create mode 100644 hw/intc/xive.c
default-configs/ppc64-softmmu.mak +1 -0
index aec2855750d6..2d1e7c5c4668 100644
@@ -16,6 +16,7 @@ CONFIG_VIRTIO_VGA=y
 CONFIG_XICS=$(CONFIG_PSERIES)
 CONFIG_XICS_SPAPR=$(CONFIG_PSERIES)
 CONFIG_XICS_KVM=$(call land,$(CONFIG_PSERIES),$(CONFIG_KVM))
+CONFIG_XIVE=$(CONFIG_PSERIES)
 CONFIG_MEM_DEVICE=y
 CONFIG_DIMM=y
 CONFIG_SPAPR_RNG=y
include/hw/ppc/xive.h new file +2 -0
new file mode 100644
index 000000000000..5fec4b08705d
@@ -0,0 +1,130 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
Quoted 8 lines

include/hw/ppc/xive.h line 7

Quoted 5 lines
include/hw/ppc/xive.h new file +5 -0
+ *
+ * Copyright (c) 2017-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.

A cheat sheet in the top of this header with the old and new XIVE terms would quite nice to have.

Yes. It's a good place. I will put the XIVE acronyms here :

Quoted 9 lines

EA Event Assignment
EISN Effective Interrupt Source Number
END Event Notification Descriptor
ESB Event State Buffer
EQ Event Queue
LISN Logical Interrupt Source Number
NVT Notification Virtual Target
TIMA Thread Interrupt Management Area
...

Quoted 2 lines

That sounds good, but what I'd also like is showing that NVT == VP and EAS == IVT and so forth.

sure. I will add that.

skiboot and PowerNV Linux are using a mixed version and I wonder if I should not clarify that also, skiboot at least.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

include/hw/ppc/xive.h line 13

Quoted 6 lines
include/hw/ppc/xive.h new file +6 -0
+ */
+
+#ifndef PPC_XIVE_H
+#define PPC_XIVE_H
+
+#include "hw/sysbus.h"

So, I'm a bit dubious about making the XiveSource a SysBus device - I'm concerned it won't play well with tying it into the other devices like PHB that "own" it in real hardware.

Quoted 6 lines
Quoted 2 lines

It does but I can take a look at changing it to a DeviceState. The reset handlers might be a concern.

As "non bus" device I think you'd need to register your own reset handler rather than just setting dc->reset. Otherwise, I think that should work.

yes. I will give a look/try, it might not be such a problem.

Quoted 17 lines
Quoted 10 lines
Quoted 2 lines

I think we'd be better off making it a direct descendent of TYPE_DEVICE which constructs the MMIO region, but doesn't map it.

At a moment, I started working on a XiveESB object doing what I think you are suggesting and I removed it. I am reluctant adding more complexity now, the patchset is just growing and growing ...

But I agree there are fundamentals to get right for KVM. Let's talk about it after you have looked at the overall patchset, at least up to KVM initial support.

Hm, ok.

Quoted 4 lines
Quoted 3 lines

Then we can havea SysBusDevice (and/or other) wrapper which instantiates the XiveSource core and maps it into somewhere accessible.

Quoted 5 lines
Quoted 1 line

The XIVE controller model does the mapping of the source currently.

I'm.. I'm not sure what you mean by that. We have a
sysbus_init_mmio() right here which effectively maps in the MMIO region AFAICT.

yes. what I meant is that the XIVE controller model does all the mapping, for the TIMA and for the ESB pages of the XiveSource.

This is a 'critical' part of the XIVE model because the region have a different nature under KVM, which requires the KVM device to be created before the region are, and to be destroyed when the device is.

It took me a while to get all in place to support all aspects of the model: KVM and not KVM, switch of interrupt controller, machine reset, post_load, PowerNV devices, etc. So expect some resistant from me on that topic.

Thanks,

C.

Quoted 564 lines
Quoted 563 lines

In the case of sPAPR, the controller model controls the TIMA and for PowerNV, there are quite few others MMIO regions to handle.

Quoted 44 lines
Quoted 44 lines
include/hw/ppc/xive.h new file +43 -0
+
+/*
+ * XIVE Interrupt Source
+ */
+
+#define TYPE_XIVE_SOURCE "xive-source"
+#define XIVE_SOURCE(obj) OBJECT_CHECK(XiveSource, (obj), TYPE_XIVE_SOURCE)
+
+/*
+ * XIVE Interrupt Source characteristics, which define how the ESB are
+ * controlled.
+ */
+#define XIVE_SRC_H_INT_ESB     0x1 /* ESB managed with hcall H_INT_ESB */
+#define XIVE_SRC_STORE_EOI     0x2 /* Store EOI supported */
+
+typedef struct XiveSource {
+    SysBusDevice parent;
+
+    /* IRQs */
+    uint32_t        nr_irqs;
+    qemu_irq        *qirqs;
+
+    /* PQ bits */
+    uint8_t         *status;
+
+    /* ESB memory region */
+    uint64_t        esb_flags;
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+} XiveSource;
+
+/*
+ * ESB MMIO setting. Can be one page, for both source triggering and
+ * source management, or two different pages. See below for magic
+ * values.
+ */
+#define XIVE_ESB_4K          12 /* PSI HB only */
+#define XIVE_ESB_4K_2PAGE    13
+#define XIVE_ESB_64K         16
+#define XIVE_ESB_64K_2PAGE   17
+
+static inline bool xive_source_esb_has_2page(XiveSource *xsrc)
+{
Quoted 8 lines

include/hw/ppc/xive.h line 62

Quoted 6 lines
include/hw/ppc/xive.h new file +6 -0
+    return xsrc->esb_shift == XIVE_ESB_64K_2PAGE ||
+        xsrc->esb_shift == XIVE_ESB_4K_2PAGE;
+}
+
+/* The trigger page is always the first/even page */
+static inline hwaddr xive_source_esb_page(XiveSource *xsrc, uint32_t srcno)

This function doesn't appear to be used anywhere except..

It's used in patch 16 adding the hcalls also.

Quoted 10 lines

include/hw/ppc/xive.h line 69

Quoted 7 lines
include/hw/ppc/xive.h new file +7 -0
+{
+    assert(srcno < xsrc->nr_irqs);
+    return (1ull << xsrc->esb_shift) * srcno;
+}
+
+/* In a two pages ESB MMIO setting, the odd page is for management */
+static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)

..here, and this function doesn't appear to be used anywhere.

It's used in patch 16 adding the hcalls and patch 23 for KVM.

This is basic ESB support which I thought belong to the patch on sources.

Quoted 208 lines
Quoted 208 lines
include/hw/ppc/xive.h new file +61 -0
+{
+    hwaddr addr = xive_source_esb_page(xsrc, srcno);
+
+    if (xive_source_esb_has_2page(xsrc)) {
+        addr += (1 << (xsrc->esb_shift - 1));
+    }
+
+    return addr;
+}
+
+/*
+ * Each interrupt source has a 2-bit state machine which can be
+ * controlled by MMIO. P indicates that an interrupt is pending (has
+ * been sent to a queue and is waiting for an EOI). Q indicates that
+ * the interrupt has been triggered while pending.
+ *
+ * This acts as a coalescing mechanism in order to guarantee that a
+ * given interrupt only occurs at most once in a queue.
+ *
+ * When doing an EOI, the Q bit will indicate if the interrupt
+ * needs to be re-triggered.
+ */
+#define XIVE_ESB_VAL_P        0x2
+#define XIVE_ESB_VAL_Q        0x1
+
+#define XIVE_ESB_RESET        0x0
+#define XIVE_ESB_PENDING      XIVE_ESB_VAL_P
+#define XIVE_ESB_QUEUED       (XIVE_ESB_VAL_P | XIVE_ESB_VAL_Q)
+#define XIVE_ESB_OFF          XIVE_ESB_VAL_Q
+
+/*
+ * "magic" Event State Buffer (ESB) MMIO offsets.
+ *
+ * The following offsets into the ESB MMIO allow to read or manipulate
+ * the PQ bits. They must be used with an 8-byte load instruction.
+ * They all return the previous state of the interrupt (atomically).
+ *
+ * Additionally, some ESB pages support doing an EOI via a store and
+ * some ESBs support doing a trigger via a separate trigger page.
+ */
+#define XIVE_ESB_STORE_EOI      0x400 /* Store */
+#define XIVE_ESB_LOAD_EOI       0x000 /* Load */
+#define XIVE_ESB_GET            0x800 /* Load */
+#define XIVE_ESB_SET_PQ_00      0xc00 /* Load */
+#define XIVE_ESB_SET_PQ_01      0xd00 /* Load */
+#define XIVE_ESB_SET_PQ_10      0xe00 /* Load */
+#define XIVE_ESB_SET_PQ_11      0xf00 /* Load */
+
+uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno);
+uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq);
+
+void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset,
+                                Monitor *mon);
+
+static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return xsrc->qirqs[srcno];
+}
+
+#endif /* PPC_XIVE_H */
hw/intc/xive.c new file +140 -0
new file mode 100644
index 000000000000..f7621f84828c
@@ -0,0 +1,379 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2017-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "target/ppc/cpu.h"
+#include "sysemu/cpus.h"
+#include "sysemu/dma.h"
+#include "monitor/monitor.h"
+#include "hw/ppc/xive.h"
+
+/*
+ * XIVE ESB helpers
+ */
+
+static uint8_t xive_esb_set(uint8_t *pq, uint8_t value)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    *pq &= ~0x3;
+    *pq |= value & 0x3;
+
+    return old_pq;
+}
+
+static bool xive_esb_trigger(uint8_t *pq)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+        xive_esb_set(pq, XIVE_ESB_PENDING);
+        return true;
+    case XIVE_ESB_PENDING:
+    case XIVE_ESB_QUEUED:
+        xive_esb_set(pq, XIVE_ESB_QUEUED);
+        return false;
+    case XIVE_ESB_OFF:
+        xive_esb_set(pq, XIVE_ESB_OFF);
+        return false;
+    default:
+         g_assert_not_reached();
+    }
+}
+
+static bool xive_esb_eoi(uint8_t *pq)
+{
+    uint8_t old_pq = *pq & 0x3;
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+    case XIVE_ESB_PENDING:
+        xive_esb_set(pq, XIVE_ESB_RESET);
+        return false;
+    case XIVE_ESB_QUEUED:
+        xive_esb_set(pq, XIVE_ESB_PENDING);
+        return true;
+    case XIVE_ESB_OFF:
+        xive_esb_set(pq, XIVE_ESB_OFF);
+        return false;
+    default:
+         g_assert_not_reached();
+    }
+}
+
+/*
+ * XIVE Interrupt Source (or IVSE)
+ */
+
+uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xsrc->status[srcno] & 0x3;
+}
+
+uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_set(&xsrc->status[srcno], pq);
+}
+
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_trigger(&xsrc->status[srcno]);
+}
+
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+
+    return xive_esb_eoi(&xsrc->status[srcno]);
+}
+
+/*
+ * Forward the source event notification to the Router
+ */
+static void xive_source_notify(XiveSource *xsrc, int srcno)
+{
+
+}
+
+/*
+ * In a two pages ESB MMIO setting, even page is the trigger page, odd
+ * page is for management
+ */
+static inline bool addr_is_even(hwaddr addr, uint32_t shift)
+{
+    return !((addr >> shift) & 1);
+}
+
+static inline bool xive_source_is_trigger_page(XiveSource *xsrc, hwaddr addr)
+{
+    return xive_source_esb_has_2page(xsrc) &&
+        addr_is_even(addr, xsrc->esb_shift - 1);
+}
+
+/*
+ * ESB MMIO loads
+ *                      Trigger page    Management/EOI page
+ * 2 pages setting      even            odd
+ *
+ * 0x000 .. 0x3FF       -1              EOI and return 0|1
+ * 0x400 .. 0x7FF       -1              EOI and return 0|1
Quoted 8 lines

hw/intc/xive.c line 146

Quoted 6 lines
hw/intc/xive.c new file +6 -0
+ * 0x800 .. 0xBFF       -1              return PQ
+ * 0xC00 .. 0xCFF       -1              return PQ and atomically PQ=0
+ * 0xD00 .. 0xDFF       -1              return PQ and atomically PQ=0
+ * 0xE00 .. 0xDFF       -1              return PQ and atomically PQ=1
+ * 0xF00 .. 0xDFF       -1              return PQ and atomically PQ=1
+ */

I can't quite make sense of this table. What do the -1s represent,

the value returned by the load.

Quoted 1 line

and how does it relate to the non-2page case?

one page ESB support trigger and management on the same page. So for loads, the odd page behavior applies.

Quoted 42 lines
Quoted 42 lines
hw/intc/xive.c new file +42 -0
+static uint64_t xive_source_esb_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    uint32_t offset = addr & 0xFFF;
+    uint32_t srcno = addr >> xsrc->esb_shift;
+    uint64_t ret = -1;
+
+    /* In a two pages ESB MMIO setting, trigger page should not be read */
+    if (xive_source_is_trigger_page(xsrc, addr)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: invalid load on IRQ %d trigger page at "
+                      "0x%"HWADDR_PRIx"\n", srcno, addr);
+        return -1;
+    }
+
+    switch (offset) {
+    case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
+        ret = xive_source_esb_eoi(xsrc, srcno);
+
+        /* Forward the source event notification for routing */
+        if (ret) {
+            xive_source_notify(xsrc, srcno);
+        }
+        break;
+
+    case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
+        ret = xive_source_esb_get(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        ret = xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB load addr %x\n",
+                      offset);
+    }
+
+    return ret;
+}
Quoted 8 lines

hw/intc/xive.c line 193

Quoted 5 lines
hw/intc/xive.c new file +5 -0
+
+/*
+ * ESB MMIO stores
+ *                      Trigger page    Management/EOI page
+ * 2 pages setting      even            odd

As with the previous table, I don't quite understand what the headings above mean.

one page ESB support trigger and management on the same page. So for stores, the odd page behavior applies.

The headings can be improved. I will think of something.

Quoted 103 lines
Quoted 103 lines
hw/intc/xive.c new file +103 -0
+ * 0x000 .. 0x3FF       Trigger         Trigger
+ * 0x400 .. 0x7FF       Trigger         EOI
+ * 0x800 .. 0xBFF       Trigger         undefined
+ * 0xC00 .. 0xCFF       Trigger         PQ=00
+ * 0xD00 .. 0xDFF       Trigger         PQ=01
+ * 0xE00 .. 0xDFF       Trigger         PQ=10
+ * 0xF00 .. 0xDFF       Trigger         PQ=11
+ */
+static void xive_source_esb_write(void *opaque, hwaddr addr,
+                                  uint64_t value, unsigned size)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    uint32_t offset = addr & 0xFFF;
+    uint32_t srcno = addr >> xsrc->esb_shift;
+    bool notify = false;
+
+    /* In a two pages ESB MMIO setting, trigger page only triggers */
+    if (xive_source_is_trigger_page(xsrc, addr)) {
+        notify = xive_source_esb_trigger(xsrc, srcno);
+        goto out;
+    }
+
+    switch (offset) {
+    case 0 ... 0x3FF:
+        notify = xive_source_esb_trigger(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
+        if (!(xsrc->esb_flags & XIVE_SRC_STORE_EOI)) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "XIVE: invalid Store EOI for IRQ %d\n", srcno);
+            return;
+        }
+
+        notify = xive_source_esb_eoi(xsrc, srcno);
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
+        break;
+
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr %x\n",
+                      offset);
+        return;
+    }
+
+out:
+    /* Forward the source event notification for routing */
+    if (notify) {
+        xive_source_notify(xsrc, srcno);
+    }
+}
+
+static const MemoryRegionOps xive_source_esb_ops = {
+    .read = xive_source_esb_read,
+    .write = xive_source_esb_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static void xive_source_set_irq(void *opaque, int srcno, int val)
+{
+    XiveSource *xsrc = XIVE_SOURCE(opaque);
+    bool notify = false;
+
+    if (val) {
+        notify = xive_source_esb_trigger(xsrc, srcno);
+    }
+
+    /* Forward the source event notification for routing */
+    if (notify) {
+        xive_source_notify(xsrc, srcno);
+    }
+}
+
+void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
+{
+    int i;
+
+    for (i = 0; i < xsrc->nr_irqs; i++) {
+        uint8_t pq = xive_source_esb_get(xsrc, i);
+
+        if (pq == XIVE_ESB_OFF) {
+            continue;
+        }
+
+        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+                       pq & XIVE_ESB_VAL_P ? 'P' : '-',
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+    }
+}
+
Quoted 8 lines

hw/intc/xive.c line 303

Quoted 6 lines
hw/intc/xive.c new file +6 -0
+static void xive_source_reset(DeviceState *dev)
+{
+    XiveSource *xsrc = XIVE_SOURCE(dev);
+
+    /* PQs are initialized to 0b01 which corresponds to "ints off" */
+    memset(xsrc->status, 0x1, xsrc->nr_irqs);

You've already got XIVE_ESB_OFF defined to make this a little clearer.

Sure.

Thanks,

C.

Quoted 90 lines
Quoted 89 lines
hw/intc/xive.c new file +76 -0
+}
+
+static void xive_source_realize(DeviceState *dev, Error **errp)
+{
+    XiveSource *xsrc = XIVE_SOURCE(dev);
+
+    if (!xsrc->nr_irqs) {
+        error_setg(errp, "Number of interrupt needs to be greater than 0");
+        return;
+    }
+
+    if (xsrc->esb_shift != XIVE_ESB_4K &&
+        xsrc->esb_shift != XIVE_ESB_4K_2PAGE &&
+        xsrc->esb_shift != XIVE_ESB_64K &&
+        xsrc->esb_shift != XIVE_ESB_64K_2PAGE) {
+        error_setg(errp, "Invalid ESB shift setting");
+        return;
+    }
+
+    xsrc->qirqs = qemu_allocate_irqs(xive_source_set_irq, xsrc,
+                                     xsrc->nr_irqs);
+
+    xsrc->status = g_malloc0(xsrc->nr_irqs);
+
+    memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
+                          &xive_source_esb_ops, xsrc, "xive.esb",
+                          (1ull << xsrc->esb_shift) * xsrc->nr_irqs);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &xsrc->esb_mmio);
+}
+
+static const VMStateDescription vmstate_xive_source = {
+    .name = TYPE_XIVE_SOURCE,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
+        VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+/*
+ * The default XIVE interrupt source setting for the ESB MMIOs is two
+ * 64k pages without Store EOI, to be in sync with KVM.
+ */
+static Property xive_source_properties[] = {
+    DEFINE_PROP_UINT64("flags", XiveSource, esb_flags, 0),
+    DEFINE_PROP_UINT32("nr-irqs", XiveSource, nr_irqs, 0),
+    DEFINE_PROP_UINT32("shift", XiveSource, esb_shift, XIVE_ESB_64K_2PAGE),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_source_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc    = "XIVE Interrupt Source";
+    dc->props   = xive_source_properties;
+    dc->realize = xive_source_realize;
+    dc->reset   = xive_source_reset;
+    dc->vmsd    = &vmstate_xive_source;
+}
+
+static const TypeInfo xive_source_info = {
+    .name          = TYPE_XIVE_SOURCE,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XiveSource),
+    .class_init    = xive_source_class_init,
+};
+
+static void xive_register_types(void)
+{
+    type_register_static(&xive_source_info);
+}
+
+type_init(xive_register_types)
hw/intc/Makefile.objs +1 -0
index 0e9963f5eecc..72a46ed91c31 100644
@@ -37,6 +37,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) += xive.o
 obj-$(CONFIG_POWERNV) += xics_pnv.o
 obj-$(CONFIG_ALLWINNER_A10_PIC) += allwinner-a10-pic.o
 obj-$(CONFIG_S390_FLIC) += s390_flic.o
#6 lore raw

Re: [Qemu-devel] [PATCH v5 01/36] ppc/xive: introduce a XIVE interrupt source model

In reply to David Gibson
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

include/hw/ppc/xive.h line 13

Quoted 6 lines

+ */

include/hw/ppc/xive.h new file +5 -0
+
+#ifndef PPC_XIVE_H
+#define PPC_XIVE_H
+
+#include "hw/sysbus.h"

So, I'm a bit dubious about making the XiveSource a SysBus device - I'm concerned it won't play well with tying it into the other devices like PHB that "own" it in real hardware.

Quoted 6 lines
Quoted 2 lines

It does but I can take a look at changing it to a DeviceState. The reset handlers might be a concern.

As "non bus" device I think you'd need to register your own reset handler rather than just setting dc->reset. Otherwise, I think that should work.

I removed from XIVE the SysBus dependencies and indeed it's better not to rely on the default reset and mapping behavior of sysbus.

I am addressing your comments in a WIP v6 branch on github.

C.

#7 lore raw

PATCH v5 2/36 ppc/xive: add support for the LSI interrupt sources

In reply to Cédric Le Goater

The 'sent' status of the LSI interrupt source is modeled with the 'P' bit of the ESB and the assertion status of the source is maintained in an array under the main sPAPRXive object. The type of the source is stored in the same array for practical reasons.

---
 include/hw/ppc/xive.h | 20 ++++++++++++-
 hw/intc/xive.c        | 68 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 81 insertions(+), 7 deletions(-)
include/hw/ppc/xive.h +19 -1
index 5fec4b08705d..e118acd59f1e 100644
@@ -32,8 +32,10 @@ typedef struct XiveSource {
     /* IRQs */
     uint32_t        nr_irqs;
     qemu_irq        *qirqs;
+    unsigned long   *lsi_map;
+    int32_t         lsi_map_size; /* for VMSTATE_BITMAP */
 
-    /* PQ bits */
+    /* PQ bits and LSI assertion bit */
     uint8_t         *status;
 
     /* ESB memory region */
@@ -89,6 +91,7 @@ static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)
  * When doing an EOI, the Q bit will indicate if the interrupt
  * needs to be re-triggered.
  */
+#define XIVE_STATUS_ASSERTED  0x4  /* Extra bit for LSI */
 #define XIVE_ESB_VAL_P        0x2
 #define XIVE_ESB_VAL_Q        0x1
 
@@ -127,4 +130,19 @@ static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno)
     return xsrc->qirqs[srcno];
 }
 
+static inline bool xive_source_irq_is_lsi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return test_bit(srcno, xsrc->lsi_map);
+}
+
+static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
+                                       bool lsi)
+{
+    assert(srcno < xsrc->nr_irqs);
+    if (lsi) {
+        bitmap_set(xsrc->lsi_map, srcno, 1);
+    }
+}
+
 #endif /* PPC_XIVE_H */
hw/intc/xive.c +62 -6

120 lines not on this page · raw message

Signature · 1 line
2.17.2
#8 lore raw

Re: [Qemu-devel] [PATCH v5 02/36] ppc/xive: add support for the LSI interrupt sources

Quoted 6 lines · Cédric Le Goater

The 'sent' status of the LSI interrupt source is modeled with the 'P' bit of the ESB and the assertion status of the source is maintained in an array under the main sPAPRXive object. The type of the source is stored in the same array for practical reasons.

Looks good except for some minor details.

Quoted 10 lines
---
 include/hw/ppc/xive.h | 20 ++++++++++++-
 hw/intc/xive.c        | 68 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 81 insertions(+), 7 deletions(-)
include/hw/ppc/xive.h +0 -0
index 5fec4b08705d..e118acd59f1e 100644

include/hw/ppc/xive.h typedef struct XiveSource { · line 36

Quoted 5 lines
include/hw/ppc/xive.h +2 -0
@@ -32,8 +32,10 @@ typedef struct XiveSource {
     /* IRQs */
     uint32_t        nr_irqs;
     qemu_irq        *qirqs;
+    unsigned long   *lsi_map;
+    int32_t         lsi_map_size; /* for VMSTATE_BITMAP */

At some point it's possible we'll want XiveSource subclasses that just know which irqs are LSI and which aren't without an explicit map. But this detail isn't exposed in the migration stream or the user interface, so we can tweak it later as ncessary.

Quoted 15 lines
include/hw/ppc/xive.h +2 -1
-    /* PQ bits */
+    /* PQ bits and LSI assertion bit */
     uint8_t         *status;
 
     /* ESB memory region */
@@ -89,6 +91,7 @@ static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)
  * When doing an EOI, the Q bit will indicate if the interrupt
  * needs to be re-triggered.
  */
+#define XIVE_STATUS_ASSERTED  0x4  /* Extra bit for LSI */
 #define XIVE_ESB_VAL_P        0x2
 #define XIVE_ESB_VAL_Q        0x1
 
@@ -127,4 +130,19 @@ static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno)
     return xsrc->qirqs[srcno];

include/hw/ppc/xive.h static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno) · line 140

Quoted 10 lines
include/hw/ppc/xive.h +8 -0
 }
 
+static inline bool xive_source_irq_is_lsi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return test_bit(srcno, xsrc->lsi_map);
+}
+
+static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
+                                       bool lsi)

The function name isn't obvious about this being controlling LSI configuration. '..._irq_set_lsi' maybe?

Quoted 13 lines
include/hw/ppc/xive.h +7 -0
+{
+    assert(srcno < xsrc->nr_irqs);
+    if (lsi) {
+        bitmap_set(xsrc->lsi_map, srcno, 1);
+    }
+}
+
 #endif /* PPC_XIVE_H */
hw/intc/xive.c +0 -0
index f7621f84828c..ac4605fee8b7 100644

hw/intc/xive.c uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq) · line 95

Quoted 8 lines
hw/intc/xive.c +4 -0
@@ -88,14 +88,40 @@ uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
     return xive_esb_set(&xsrc->status[srcno], pq);
 }
 
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t
 srcno)

What exactly "trigger" means isn't entirely obvious for an LSI. Might be clearer to have "lsi_assert" and "lsi_deassert" helpers instead.

Quoted 112 lines
hw/intc/xive.c +23 -1
+{
+    uint8_t old_pq = xive_source_esb_get(xsrc, srcno);
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+        xive_source_esb_set(xsrc, srcno, XIVE_ESB_PENDING);
+        return true;
+    default:
+        return false;
+    }
+}
+
 /*
  * Returns whether the event notification should be forwarded.
  */
 static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
 {
+    bool ret;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_trigger(&xsrc->status[srcno]);
+    ret = xive_esb_trigger(&xsrc->status[srcno]);
+
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&
+        xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: queued an event on LSI IRQ %d\n", srcno);
+    }
+
+    return ret;
 }
 

/*

hw/intc/xive.c +34 -5
@@ -103,9 +129,22 @@ static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
  */
 static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
 {
+    bool ret;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_eoi(&xsrc->status[srcno]);
+    ret = xive_esb_eoi(&xsrc->status[srcno]);
+
+    /* LSI sources do not set the Q bit but they can still be
+     * asserted, in which case we should forward a new event
+     * notification
+     */
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&
+        xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
+        ret = xive_source_lsi_trigger(xsrc, srcno);
+    }
+
+    return ret;
 }
 
 /*
@@ -268,8 +307,17 @@ static void xive_source_set_irq(void *opaque, int srcno, int val)
     XiveSource *xsrc = XIVE_SOURCE(opaque);
     bool notify = false;
 
-    if (val) {
-        notify = xive_source_esb_trigger(xsrc, srcno);
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        if (val) {
+            xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
+            notify = xive_source_lsi_trigger(xsrc, srcno);
+        } else {
+            xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
+        }
+    } else {
+        if (val) {
+            notify = xive_source_esb_trigger(xsrc, srcno);
+        }
     }
 
     /* Forward the source event notification for routing */
@@ -289,9 +337,11 @@ void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
             continue;
         }
 
-        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+        monitor_printf(mon, "  %08x %s %c%c%c\n", i + offset,
+                       xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
-                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
+                       xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
     }
 }
 
@@ -299,6 +349,8 @@ static void xive_source_reset(DeviceState *dev)
 {
     XiveSource *xsrc = XIVE_SOURCE(dev);
 
+    /* Do not clear the LSI bitmap */
+
     /* PQs are initialized to 0b01 which corresponds to "ints off" */
     memset(xsrc->status, 0x1, xsrc->nr_irqs);
 }
@@ -325,6 +377,9 @@ static void xive_source_realize(DeviceState *dev, Error **errp)
 
     xsrc->status = g_malloc0(xsrc->nr_irqs);
 
+    xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
+    xsrc->lsi_map_size = xsrc->nr_irqs;
+
     memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
                           &xive_source_esb_ops, xsrc, "xive.esb",
                           (1ull << xsrc->esb_shift) * xsrc->nr_irqs);

hw/intc/xive.c static const VMStateDescription vmstate_xive_source = { · line 396

Quoted 4 lines
hw/intc/xive.c +1 -0
@@ -338,6 +393,7 @@ static const VMStateDescription vmstate_xive_source = {
     .fields = (VMStateField[]) {
         VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
         VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
+        VMSTATE_BITMAP(lsi_map, XiveSource, 1, lsi_map_size),

This shouldn't be here. The lsi_map is all set up at machine configuration time and then static, so it doesn't need to be migrated.

Quoted 3 lines
hw/intc/xive.c +0 -0
         VMSTATE_END_OF_LIST()
     },
 };
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
#9 lore raw

Re: [Qemu-devel] [PATCH v5 02/36] ppc/xive: add support for the LSI interrupt sources

Quoted 48 lines · David Gibson
Quoted 6 lines · Cédric Le Goater

The 'sent' status of the LSI interrupt source is modeled with the 'P' bit of the ESB and the assertion status of the source is maintained in an array under the main sPAPRXive object. The type of the source is stored in the same array for practical reasons.

Looks good except for some minor details.

Quoted 10 lines
---
 include/hw/ppc/xive.h | 20 ++++++++++++-
 hw/intc/xive.c        | 68 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 81 insertions(+), 7 deletions(-)
include/hw/ppc/xive.h +0 -0
index 5fec4b08705d..e118acd59f1e 100644

include/hw/ppc/xive.h typedef struct XiveSource { · line 36

Quoted 5 lines
include/hw/ppc/xive.h +2 -0
@@ -32,8 +32,10 @@ typedef struct XiveSource {
     /* IRQs */
     uint32_t        nr_irqs;
     qemu_irq        *qirqs;
+    unsigned long   *lsi_map;
+    int32_t         lsi_map_size; /* for VMSTATE_BITMAP */

At some point it's possible we'll want XiveSource subclasses that just know which irqs are LSI and which aren't without an explicit map. But this detail isn't exposed in the migration stream or the user interface, so we can tweak it later as ncessary.

Quoted 17 lines
include/hw/ppc/xive.h +2 -1
-    /* PQ bits */
+    /* PQ bits and LSI assertion bit */
     uint8_t         *status;
 
     /* ESB memory region */
@@ -89,6 +91,7 @@ static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)
  * When doing an EOI, the Q bit will indicate if the interrupt
  * needs to be re-triggered.
  */
+#define XIVE_STATUS_ASSERTED  0x4  /* Extra bit for LSI */
 #define XIVE_ESB_VAL_P        0x2
 #define XIVE_ESB_VAL_Q        0x1
 
@@ -127,4 +130,19 @@ static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno)
     return xsrc->qirqs[srcno];
 }
 
Quoted 11 lines

include/hw/ppc/xive.h static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno) · line 140

Quoted 8 lines
include/hw/ppc/xive.h +8 -0
+static inline bool xive_source_irq_is_lsi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return test_bit(srcno, xsrc->lsi_map);
+}
+
+static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
+                                       bool lsi)

The function name isn't obvious about this being controlling LSI configuration. '..._irq_set_lsi' maybe?

yes.

Quoted 16 lines
Quoted 16 lines
include/hw/ppc/xive.h +7 -0
+{
+    assert(srcno < xsrc->nr_irqs);
+    if (lsi) {
+        bitmap_set(xsrc->lsi_map, srcno, 1);
+    }
+}
+
 #endif /* PPC_XIVE_H */
hw/intc/xive.c +0 -0
index f7621f84828c..ac4605fee8b7 100644
@@ -88,14 +88,40 @@ uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
     return xive_esb_set(&xsrc->status[srcno], pq);
 }
 
Quoted 8 lines

hw/intc/xive.c uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)

Quoted 5 lines
hw/intc/xive.c +4 -0
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t
 srcno)

What exactly "trigger" means isn't entirely obvious for an LSI. Might be clearer to have "lsi_assert" and "lsi_deassert" helpers instead.

This is called only when the interrupt is asserted. So it is a simplified LSI trigger depending only on the 'P' bit.

Quoted 106 lines
Quoted 106 lines
hw/intc/xive.c +23 -1
+{
+    uint8_t old_pq = xive_source_esb_get(xsrc, srcno);
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+        xive_source_esb_set(xsrc, srcno, XIVE_ESB_PENDING);
+        return true;
+    default:
+        return false;
+    }
+}
+
 /*
  * Returns whether the event notification should be forwarded.
  */
 static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
 {
+    bool ret;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_trigger(&xsrc->status[srcno]);
+    ret = xive_esb_trigger(&xsrc->status[srcno]);
+
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&
+        xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: queued an event on LSI IRQ %d\n", srcno);
+    }
+
+    return ret;
 }
 

/*

hw/intc/xive.c +31 -5
@@ -103,9 +129,22 @@ static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
  */
 static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
 {
+    bool ret;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_eoi(&xsrc->status[srcno]);
+    ret = xive_esb_eoi(&xsrc->status[srcno]);
+
+    /* LSI sources do not set the Q bit but they can still be
+     * asserted, in which case we should forward a new event
+     * notification
+     */
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&
+        xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
+        ret = xive_source_lsi_trigger(xsrc, srcno);
+    }
+
+    return ret;
 }
 
 /*
@@ -268,8 +307,17 @@ static void xive_source_set_irq(void *opaque, int srcno, int val)
     XiveSource *xsrc = XIVE_SOURCE(opaque);
     bool notify = false;
 
-    if (val) {
-        notify = xive_source_esb_trigger(xsrc, srcno);
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        if (val) {
+            xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
+            notify = xive_source_lsi_trigger(xsrc, srcno);
+        } else {
+            xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
+        }
+    } else {
+        if (val) {
+            notify = xive_source_esb_trigger(xsrc, srcno);
+        }
     }
 
     /* Forward the source event notification for routing */
@@ -289,9 +337,11 @@ void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
             continue;
         }
 
-        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+        monitor_printf(mon, "  %08x %s %c%c%c\n", i + offset,
+                       xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
-                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
+                       xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
     }
 }
 
@@ -299,6 +349,8 @@ static void xive_source_reset(DeviceState *dev)
 {
     XiveSource *xsrc = XIVE_SOURCE(dev);
 
+    /* Do not clear the LSI bitmap */
+
     /* PQs are initialized to 0b01 which corresponds to "ints off" */
     memset(xsrc->status, 0x1, xsrc->nr_irqs);
 }
@@ -325,6 +377,9 @@ static void xive_source_realize(DeviceState *dev, Error **errp)
 
     xsrc->status = g_malloc0(xsrc->nr_irqs);
 
Quoted 14 lines

hw/intc/xive.c static const VMStateDescription vmstate_xive_source = { · line 396

Quoted 11 lines
hw/intc/xive.c +4 -0
+    xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
+    xsrc->lsi_map_size = xsrc->nr_irqs;
+
     memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
                           &xive_source_esb_ops, xsrc, "xive.esb",
                           (1ull << xsrc->esb_shift) * xsrc->nr_irqs);
@@ -338,6 +393,7 @@ static const VMStateDescription vmstate_xive_source = {
     .fields = (VMStateField[]) {
         VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
         VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
+        VMSTATE_BITMAP(lsi_map, XiveSource, 1, lsi_map_size),

This shouldn't be here. The lsi_map is all set up at machine configuration time and then static, so it doesn't need to be migrated.

yes. of course ... I will get rid of it.

Thanks,

C.

Quoted 5 lines
Quoted 4 lines
hw/intc/xive.c +0 -0
         VMSTATE_END_OF_LIST()
     },
 };
#10 lore raw

Re: [Qemu-devel] [PATCH v5 02/36] ppc/xive: add support for the LSI interrupt sources

Quoted 89 lines · Cédric Le Goater
Quoted 48 lines · David Gibson
Quoted 6 lines · Cédric Le Goater

The 'sent' status of the LSI interrupt source is modeled with the 'P' bit of the ESB and the assertion status of the source is maintained in an array under the main sPAPRXive object. The type of the source is stored in the same array for practical reasons.

Looks good except for some minor details.

Quoted 10 lines
---
 include/hw/ppc/xive.h | 20 ++++++++++++-
 hw/intc/xive.c        | 68 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 81 insertions(+), 7 deletions(-)
include/hw/ppc/xive.h +0 -0
index 5fec4b08705d..e118acd59f1e 100644

include/hw/ppc/xive.h typedef struct XiveSource { · line 36

Quoted 5 lines
include/hw/ppc/xive.h +2 -0
@@ -32,8 +32,10 @@ typedef struct XiveSource {
     /* IRQs */
     uint32_t        nr_irqs;
     qemu_irq        *qirqs;
+    unsigned long   *lsi_map;
+    int32_t         lsi_map_size; /* for VMSTATE_BITMAP */

At some point it's possible we'll want XiveSource subclasses that just know which irqs are LSI and which aren't without an explicit map. But this detail isn't exposed in the migration stream or the user interface, so we can tweak it later as ncessary.

Quoted 17 lines
include/hw/ppc/xive.h +2 -1
-    /* PQ bits */
+    /* PQ bits and LSI assertion bit */
     uint8_t         *status;
 
     /* ESB memory region */
@@ -89,6 +91,7 @@ static inline hwaddr xive_source_esb_mgmt(XiveSource *xsrc, int srcno)
  * When doing an EOI, the Q bit will indicate if the interrupt
  * needs to be re-triggered.
  */
+#define XIVE_STATUS_ASSERTED  0x4  /* Extra bit for LSI */
 #define XIVE_ESB_VAL_P        0x2
 #define XIVE_ESB_VAL_Q        0x1
 
@@ -127,4 +130,19 @@ static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno)
     return xsrc->qirqs[srcno];
 }
 
Quoted 11 lines

include/hw/ppc/xive.h static inline qemu_irq xive_source_qirq(XiveSource *xsrc, uint32_t srcno) · line 140

Quoted 8 lines
include/hw/ppc/xive.h +8 -0
+static inline bool xive_source_irq_is_lsi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return test_bit(srcno, xsrc->lsi_map);
+}
+
+static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
+                                       bool lsi)

The function name isn't obvious about this being controlling LSI configuration. '..._irq_set_lsi' maybe?

yes.

Quoted 16 lines
Quoted 16 lines
include/hw/ppc/xive.h +7 -0
+{
+    assert(srcno < xsrc->nr_irqs);
+    if (lsi) {
+        bitmap_set(xsrc->lsi_map, srcno, 1);
+    }
+}
+
 #endif /* PPC_XIVE_H */
hw/intc/xive.c +0 -0
index f7621f84828c..ac4605fee8b7 100644
@@ -88,14 +88,40 @@ uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
     return xive_esb_set(&xsrc->status[srcno], pq);
 }
 
Quoted 8 lines

hw/intc/xive.c uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)

Quoted 5 lines
hw/intc/xive.c +4 -0
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t
 srcno)

What exactly "trigger" means isn't entirely obvious for an LSI. Might be clearer to have "lsi_assert" and "lsi_deassert" helpers instead.

Quoted 2 lines

This is called only when the interrupt is asserted. So it is a simplified LSI trigger depending only on the 'P' bit.

Yes, I see that. But the result is that while the MSI logic is encapsulated in the MSI trigger function, this leaves the LSI logic split across the trigger function and set_irq() itself. I think it would be better to have assert and deassert helpers instead, which handle both the trigger/notification and also the updating of the ASSERTED bit.

Quoted 132 lines
Quoted 106 lines
Quoted 106 lines
hw/intc/xive.c +23 -1
+{
+    uint8_t old_pq = xive_source_esb_get(xsrc, srcno);
+
+    switch (old_pq) {
+    case XIVE_ESB_RESET:
+        xive_source_esb_set(xsrc, srcno, XIVE_ESB_PENDING);
+        return true;
+    default:
+        return false;
+    }
+}
+
 /*
  * Returns whether the event notification should be forwarded.
  */
 static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
 {
+    bool ret;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_trigger(&xsrc->status[srcno]);
+    ret = xive_esb_trigger(&xsrc->status[srcno]);
+
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&
+        xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: queued an event on LSI IRQ %d\n", srcno);
+    }
+
+    return ret;
 }
 

/*

hw/intc/xive.c +31 -5
@@ -103,9 +129,22 @@ static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
  */
 static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
 {
+    bool ret;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_eoi(&xsrc->status[srcno]);
+    ret = xive_esb_eoi(&xsrc->status[srcno]);
+
+    /* LSI sources do not set the Q bit but they can still be
+     * asserted, in which case we should forward a new event
+     * notification
+     */
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&
+        xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
+        ret = xive_source_lsi_trigger(xsrc, srcno);
+    }
+
+    return ret;
 }
 
 /*
@@ -268,8 +307,17 @@ static void xive_source_set_irq(void *opaque, int srcno, int val)
     XiveSource *xsrc = XIVE_SOURCE(opaque);
     bool notify = false;
 
-    if (val) {
-        notify = xive_source_esb_trigger(xsrc, srcno);
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        if (val) {
+            xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
+            notify = xive_source_lsi_trigger(xsrc, srcno);
+        } else {
+            xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
+        }
+    } else {
+        if (val) {
+            notify = xive_source_esb_trigger(xsrc, srcno);
+        }
     }
 
     /* Forward the source event notification for routing */
@@ -289,9 +337,11 @@ void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
             continue;
         }
 
-        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+        monitor_printf(mon, "  %08x %s %c%c%c\n", i + offset,
+                       xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
-                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
+                       xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
     }
 }
 
@@ -299,6 +349,8 @@ static void xive_source_reset(DeviceState *dev)
 {
     XiveSource *xsrc = XIVE_SOURCE(dev);
 
+    /* Do not clear the LSI bitmap */
+
     /* PQs are initialized to 0b01 which corresponds to "ints off" */
     memset(xsrc->status, 0x1, xsrc->nr_irqs);
 }
@@ -325,6 +377,9 @@ static void xive_source_realize(DeviceState *dev, Error **errp)
 
     xsrc->status = g_malloc0(xsrc->nr_irqs);
 
Quoted 14 lines

hw/intc/xive.c static const VMStateDescription vmstate_xive_source = { · line 396

Quoted 11 lines
hw/intc/xive.c +4 -0
+    xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
+    xsrc->lsi_map_size = xsrc->nr_irqs;
+
     memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
                           &xive_source_esb_ops, xsrc, "xive.esb",
                           (1ull << xsrc->esb_shift) * xsrc->nr_irqs);
@@ -338,6 +393,7 @@ static const VMStateDescription vmstate_xive_source = {
     .fields = (VMStateField[]) {
         VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
         VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
+        VMSTATE_BITMAP(lsi_map, XiveSource, 1, lsi_map_size),

This shouldn't be here. The lsi_map is all set up at machine configuration time and then static, so it doesn't need to be migrated.

yes. of course ... I will get rid of it.

Thanks,

C.

Quoted 5 lines
Quoted 4 lines
hw/intc/xive.c +0 -0
         VMSTATE_END_OF_LIST()
     },
 };
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
#11 lore raw

Re: [Qemu-devel] [PATCH v5 02/36] ppc/xive: add support for the LSI interrupt sources

Quoted 13 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
(quoted diff) +4 -0
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t

srcno)

What exactly "trigger" means isn't entirely obvious for an LSI. Might be clearer to have "lsi_assert" and "lsi_deassert" helpers instead.

Quoted 2 lines

This is called only when the interrupt is asserted. So it is a simplified LSI trigger depending only on the 'P' bit.

Quoted 6 lines

Yes, I see that. But the result is that while the MSI logic is encapsulated in the MSI trigger function, this leaves the LSI logic split across the trigger function and set_irq() itself. I think it would be better to have assert and deassert helpers instead, which handle both the trigger/notification and also the updating of the ASSERTED bit.

Something like the xive_source_set_irq_lsi() below ?

Thanks,

C.

---
 hw/intc/xive.c        |   58 ++++++++++++++++++++++++++++++++++++++++++++------
 include/hw/ppc/xive.h |   19 +++++++++++++++-
 2 files changed, 70 insertions(+), 7 deletions(-)
include/hw/ppc/xive.h +18 -1
Index: qemu.git/include/hw/ppc/xive.h
===================================================================
@@ -32,8 +32,9 @@ typedef struct XiveSource {
     /* IRQs */
     uint32_t        nr_irqs;
     qemu_irq        *qirqs;
+    unsigned long   *lsi_map;
 
-    /* PQ bits */
+    /* PQ bits and LSI assertion bit */
     uint8_t         *status;
 
     /* ESB memory region */
@@ -89,6 +90,7 @@ static inline hwaddr xive_source_esb_mgm
  * When doing an EOI, the Q bit will indicate if the interrupt
  * needs to be re-triggered.
  */
+#define XIVE_STATUS_ASSERTED  0x4  /* Extra bit for LSI */
 #define XIVE_ESB_VAL_P        0x2
 #define XIVE_ESB_VAL_Q        0x1
 
@@ -127,4 +129,19 @@ static inline qemu_irq xive_source_qirq(
     return xsrc->qirqs[srcno];
 }
 
+static inline bool xive_source_irq_is_lsi(XiveSource *xsrc, uint32_t srcno)
+{
+    assert(srcno < xsrc->nr_irqs);
+    return test_bit(srcno, xsrc->lsi_map);
+}
+
+static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
+                                       bool lsi)
+{
+    assert(srcno < xsrc->nr_irqs);
+    if (lsi) {
+        bitmap_set(xsrc->lsi_map, srcno, 1);
+    }
+}
+
 #endif /* PPC_XIVE_H */
hw/intc/xive.c +52 -6

101 lines not on this page · raw message

#12 lore raw

Re: [Qemu-devel] [PATCH v5 02/36] ppc/xive: add support for the LSI interrupt sources

Quoted 13 lines · Cédric Le Goater
Quoted 13 lines
Quoted 12 lines
Quoted 9 lines
Quoted 6 lines
hw/intc/xive.c +4 -0
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t

srcno)

What exactly "trigger" means isn't entirely obvious for an LSI. Might be clearer to have "lsi_assert" and "lsi_deassert" helpers instead.

This is called only when the interrupt is asserted. So it is a simplified LSI trigger depending only on the 'P' bit.

Quoted 8 lines
Quoted 6 lines

Yes, I see that. But the result is that while the MSI logic is encapsulated in the MSI trigger function, this leaves the LSI logic split across the trigger function and set_irq() itself. I think it would be better to have assert and deassert helpers instead, which handle both the trigger/notification and also the updating of the ASSERTED bit.

Something like the xive_source_set_irq_lsi() below ?

Uh.. not exactly what I had in mind, but close enough.

[snip]
+/*

hw/intc/xive.c uint8_t xive_source_esb_set(XiveSource * · line 116

Quoted 12 lines

+ * Returns whether the event notification should be forwarded.
+ */
static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno) {

hw/intc/xive.c +5 -1
+    bool notify;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_trigger(&xsrc->status[srcno]);
+    notify = xive_esb_trigger(&xsrc->status[srcno]);
+
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&

Except that this block can go, since this function is no longer called for LSIs.

Quoted 80 lines
hw/intc/xive.c +33 -5
+        xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: queued an event on LSI IRQ %d\n", srcno);
+    }
+
+    return notify;
 }
 
 /*
@@ -103,9 +127,22 @@ static bool xive_source_esb_trigger(Xive
  */
 static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
 {
+    bool notify;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_eoi(&xsrc->status[srcno]);
+    notify = xive_esb_eoi(&xsrc->status[srcno]);
+
+    /* LSI sources do not set the Q bit but they can still be
+     * asserted, in which case we should forward a new event
+     * notification
+     */
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        bool level = xsrc->status[srcno] & XIVE_STATUS_ASSERTED;
+        notify = xive_source_set_irq_lsi(xsrc, srcno, level);
+    }
+
+    return notify;
 }
 
 /*
@@ -268,8 +305,12 @@ static void xive_source_set_irq(void *op
     XiveSource *xsrc = XIVE_SOURCE(opaque);
     bool notify = false;
 
-    if (val) {
-        notify = xive_source_esb_trigger(xsrc, srcno);
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        notify = xive_source_set_irq_lsi(xsrc, srcno, val);
+    } else {
+        if (val) {
+            notify = xive_source_esb_trigger(xsrc, srcno);
+        }
     }
 
     /* Forward the source event notification for routing */
@@ -289,9 +330,11 @@ void xive_source_pic_print_info(XiveSour
             continue;
         }
 
-        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+        monitor_printf(mon, "  %08x %s %c%c%c\n", i + offset,
+                       xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
-                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
+                       xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
     }
 }
 
@@ -299,6 +342,8 @@ static void xive_source_reset(DeviceStat
 {
     XiveSource *xsrc = XIVE_SOURCE(dev);
 
+    /* Do not clear the LSI bitmap */
+
     /* PQs are initialized to 0b01 which corresponds to "ints off" */
     memset(xsrc->status, 0x1, xsrc->nr_irqs);
 }
@@ -324,6 +369,7 @@ static void xive_source_realize(DeviceSt
                                      xsrc->nr_irqs);
 
     xsrc->status = g_malloc0(xsrc->nr_irqs);
+    xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
 
     memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
                           &xive_source_esb_ops, xsrc, "xive.esb",
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
#13 lore raw

Re: [Qemu-devel] [PATCH v5 02/36] ppc/xive: add support for the LSI interrupt sources

Quoted 35 lines · David Gibson
Quoted 13 lines · Cédric Le Goater
Quoted 13 lines
Quoted 12 lines
Quoted 9 lines
Quoted 6 lines
hw/intc/xive.c +4 -0
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t

srcno)

What exactly "trigger" means isn't entirely obvious for an LSI. Might be clearer to have "lsi_assert" and "lsi_deassert" helpers instead.

This is called only when the interrupt is asserted. So it is a simplified LSI trigger depending only on the 'P' bit.

Quoted 8 lines
Quoted 6 lines

Yes, I see that. But the result is that while the MSI logic is encapsulated in the MSI trigger function, this leaves the LSI logic split across the trigger function and set_irq() itself. I think it would be better to have assert and deassert helpers instead, which handle both the trigger/notification and also the updating of the ASSERTED bit.

Something like the xive_source_set_irq_lsi() below ?

Uh.. not exactly what I had in mind, but close enough.

[snip]
+/*

Quoted 8 lines

+ * Returns whether the event notification should be forwarded.
+ */
static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno) {

hw/intc/xive.c +2 -0
+    bool notify;
+
     assert(srcno < xsrc->nr_irqs);
Quoted 7 lines

hw/intc/xive.c uint8_t xive_source_esb_set(XiveSource * · line 116

Quoted 4 lines
hw/intc/xive.c +3 -1
-    return xive_esb_trigger(&xsrc->status[srcno]);
+    notify = xive_esb_trigger(&xsrc->status[srcno]);
+
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&

Except that this block can go, since this function is no longer called for LSIs.

It still can be through the ESB MMIOs, if the guest does a load on the trigger page.

C.

Quoted 82 lines
Quoted 81 lines
hw/intc/xive.c +33 -5
+        xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: queued an event on LSI IRQ %d\n", srcno);
+    }
+
+    return notify;
 }
 
 /*
@@ -103,9 +127,22 @@ static bool xive_source_esb_trigger(Xive
  */
 static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
 {
+    bool notify;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_eoi(&xsrc->status[srcno]);
+    notify = xive_esb_eoi(&xsrc->status[srcno]);
+
+    /* LSI sources do not set the Q bit but they can still be
+     * asserted, in which case we should forward a new event
+     * notification
+     */
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        bool level = xsrc->status[srcno] & XIVE_STATUS_ASSERTED;
+        notify = xive_source_set_irq_lsi(xsrc, srcno, level);
+    }
+
+    return notify;
 }
 
 /*
@@ -268,8 +305,12 @@ static void xive_source_set_irq(void *op
     XiveSource *xsrc = XIVE_SOURCE(opaque);
     bool notify = false;
 
-    if (val) {
-        notify = xive_source_esb_trigger(xsrc, srcno);
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        notify = xive_source_set_irq_lsi(xsrc, srcno, val);
+    } else {
+        if (val) {
+            notify = xive_source_esb_trigger(xsrc, srcno);
+        }
     }
 
     /* Forward the source event notification for routing */
@@ -289,9 +330,11 @@ void xive_source_pic_print_info(XiveSour
             continue;
         }
 
-        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+        monitor_printf(mon, "  %08x %s %c%c%c\n", i + offset,
+                       xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
-                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
+                       xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
     }
 }
 
@@ -299,6 +342,8 @@ static void xive_source_reset(DeviceStat
 {
     XiveSource *xsrc = XIVE_SOURCE(dev);
 
+    /* Do not clear the LSI bitmap */
+
     /* PQs are initialized to 0b01 which corresponds to "ints off" */
     memset(xsrc->status, 0x1, xsrc->nr_irqs);
 }
@@ -324,6 +369,7 @@ static void xive_source_realize(DeviceSt
                                      xsrc->nr_irqs);
 
     xsrc->status = g_malloc0(xsrc->nr_irqs);
+    xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
 
     memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
                           &xive_source_esb_ops, xsrc, "xive.esb",
#14 lore raw

Re: [Qemu-devel] [PATCH v5 02/36] ppc/xive: add support for the LSI interrupt sources

Quoted 44 lines · Cédric Le Goater
Quoted 35 lines · David Gibson
Quoted 13 lines · Cédric Le Goater
Quoted 13 lines
Quoted 12 lines
Quoted 9 lines
Quoted 6 lines
hw/intc/xive.c +4 -0
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t

srcno)

What exactly "trigger" means isn't entirely obvious for an LSI. Might be clearer to have "lsi_assert" and "lsi_deassert" helpers instead.

This is called only when the interrupt is asserted. So it is a simplified LSI trigger depending only on the 'P' bit.

Quoted 8 lines
Quoted 6 lines

Yes, I see that. But the result is that while the MSI logic is encapsulated in the MSI trigger function, this leaves the LSI logic split across the trigger function and set_irq() itself. I think it would be better to have assert and deassert helpers instead, which handle both the trigger/notification and also the updating of the ASSERTED bit.

Something like the xive_source_set_irq_lsi() below ?

Uh.. not exactly what I had in mind, but close enough.

[snip]
+/*

Quoted 8 lines

+ * Returns whether the event notification should be forwarded.
+ */
static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno) {

hw/intc/xive.c +2 -0
+    bool notify;
+
     assert(srcno < xsrc->nr_irqs);
Quoted 7 lines

hw/intc/xive.c uint8_t xive_source_esb_set(XiveSource * · line 116

Quoted 4 lines
hw/intc/xive.c +3 -1
-    return xive_esb_trigger(&xsrc->status[srcno]);
+    notify = xive_esb_trigger(&xsrc->status[srcno]);
+
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&

Except that this block can go, since this function is no longer called for LSIs.

Quoted 2 lines

It still can be through the ESB MMIOs, if the guest does a load on the trigger page.

Oh, good point. That makes me rethink all my comments on this matter.

In that case I think your original code was fine, except that I'd prefer to see the setting of the ASSERTED bit inside the trigger function, instead of in the set_irq() caller.

Quoted 87 lines

C.

Quoted 82 lines
Quoted 81 lines
hw/intc/xive.c +33 -5
+        xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: queued an event on LSI IRQ %d\n", srcno);
+    }
+
+    return notify;
 }
 
 /*
@@ -103,9 +127,22 @@ static bool xive_source_esb_trigger(Xive
  */
 static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
 {
+    bool notify;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_eoi(&xsrc->status[srcno]);
+    notify = xive_esb_eoi(&xsrc->status[srcno]);
+
+    /* LSI sources do not set the Q bit but they can still be
+     * asserted, in which case we should forward a new event
+     * notification
+     */
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        bool level = xsrc->status[srcno] & XIVE_STATUS_ASSERTED;
+        notify = xive_source_set_irq_lsi(xsrc, srcno, level);
+    }
+
+    return notify;
 }
 
 /*
@@ -268,8 +305,12 @@ static void xive_source_set_irq(void *op
     XiveSource *xsrc = XIVE_SOURCE(opaque);
     bool notify = false;
 
-    if (val) {
-        notify = xive_source_esb_trigger(xsrc, srcno);
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        notify = xive_source_set_irq_lsi(xsrc, srcno, val);
+    } else {
+        if (val) {
+            notify = xive_source_esb_trigger(xsrc, srcno);
+        }
     }
 
     /* Forward the source event notification for routing */
@@ -289,9 +330,11 @@ void xive_source_pic_print_info(XiveSour
             continue;
         }
 
-        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+        monitor_printf(mon, "  %08x %s %c%c%c\n", i + offset,
+                       xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
-                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
+                       xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
     }
 }
 
@@ -299,6 +342,8 @@ static void xive_source_reset(DeviceStat
 {
     XiveSource *xsrc = XIVE_SOURCE(dev);
 
+    /* Do not clear the LSI bitmap */
+
     /* PQs are initialized to 0b01 which corresponds to "ints off" */
     memset(xsrc->status, 0x1, xsrc->nr_irqs);
 }
@@ -324,6 +369,7 @@ static void xive_source_realize(DeviceSt
                                      xsrc->nr_irqs);
 
     xsrc->status = g_malloc0(xsrc->nr_irqs);
+    xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
 
     memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
                           &xive_source_esb_ops, xsrc, "xive.esb",
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
#15 lore raw

Re: [Qemu-devel] [PATCH v5 02/36] ppc/xive: add support for the LSI interrupt sources

Quoted 45 lines · David Gibson
Quoted 44 lines · Cédric Le Goater
Quoted 42 lines · David Gibson
Quoted 13 lines · Cédric Le Goater
Quoted 13 lines
Quoted 12 lines
Quoted 9 lines
Quoted 6 lines
hw/intc/xive.c +4 -0
+/*
+ * Returns whether the event notification should be forwarded.
+ */
+static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t

srcno)

What exactly "trigger" means isn't entirely obvious for an LSI. Might be clearer to have "lsi_assert" and "lsi_deassert" helpers instead.

This is called only when the interrupt is asserted. So it is a simplified LSI trigger depending only on the 'P' bit.

Quoted 8 lines
Quoted 6 lines

Yes, I see that. But the result is that while the MSI logic is encapsulated in the MSI trigger function, this leaves the LSI logic split across the trigger function and set_irq() itself. I think it would be better to have assert and deassert helpers instead, which handle both the trigger/notification and also the updating of the ASSERTED bit.

Something like the xive_source_set_irq_lsi() below ?

Uh.. not exactly what I had in mind, but close enough.

[snip]
+/*

hw/intc/xive.c uint8_t xive_source_esb_set(XiveSource * · line 116

Quoted 12 lines

+ * Returns whether the event notification should be forwarded.
+ */
static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno) {

hw/intc/xive.c +5 -1
+    bool notify;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_trigger(&xsrc->status[srcno]);
+    notify = xive_esb_trigger(&xsrc->status[srcno]);
+
+    if (xive_source_irq_is_lsi(xsrc, srcno) &&

Except that this block can go, since this function is no longer called for LSIs.

Quoted 8 lines
Quoted 2 lines

It still can be through the ESB MMIOs, if the guest does a load on the trigger page.

Oh, good point. That makes me rethink all my comments on this matter.

In that case I think your original code was fine, except that I'd prefer to see the setting of the ASSERTED bit inside the trigger function, instead of in the set_irq() caller.

ok. I will change that.

Thanks,

C.

Quoted 90 lines
Quoted 89 lines

C.

Quoted 82 lines
Quoted 81 lines
hw/intc/xive.c +33 -5
+        xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "XIVE: queued an event on LSI IRQ %d\n", srcno);
+    }
+
+    return notify;
 }
 
 /*
@@ -103,9 +127,22 @@ static bool xive_source_esb_trigger(Xive
  */
 static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
 {
+    bool notify;
+
     assert(srcno < xsrc->nr_irqs);
 
-    return xive_esb_eoi(&xsrc->status[srcno]);
+    notify = xive_esb_eoi(&xsrc->status[srcno]);
+
+    /* LSI sources do not set the Q bit but they can still be
+     * asserted, in which case we should forward a new event
+     * notification
+     */
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        bool level = xsrc->status[srcno] & XIVE_STATUS_ASSERTED;
+        notify = xive_source_set_irq_lsi(xsrc, srcno, level);
+    }
+
+    return notify;
 }
 
 /*
@@ -268,8 +305,12 @@ static void xive_source_set_irq(void *op
     XiveSource *xsrc = XIVE_SOURCE(opaque);
     bool notify = false;
 
-    if (val) {
-        notify = xive_source_esb_trigger(xsrc, srcno);
+    if (xive_source_irq_is_lsi(xsrc, srcno)) {
+        notify = xive_source_set_irq_lsi(xsrc, srcno, val);
+    } else {
+        if (val) {
+            notify = xive_source_esb_trigger(xsrc, srcno);
+        }
     }
 
     /* Forward the source event notification for routing */
@@ -289,9 +330,11 @@ void xive_source_pic_print_info(XiveSour
             continue;
         }
 
-        monitor_printf(mon, "  %08x %c%c\n", i + offset,
+        monitor_printf(mon, "  %08x %s %c%c%c\n", i + offset,
+                       xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
                        pq & XIVE_ESB_VAL_P ? 'P' : '-',
-                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-');
+                       pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
+                       xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
     }
 }
 
@@ -299,6 +342,8 @@ static void xive_source_reset(DeviceStat
 {
     XiveSource *xsrc = XIVE_SOURCE(dev);
 
+    /* Do not clear the LSI bitmap */
+
     /* PQs are initialized to 0b01 which corresponds to "ints off" */
     memset(xsrc->status, 0x1, xsrc->nr_irqs);
 }
@@ -324,6 +369,7 @@ static void xive_source_realize(DeviceSt
                                      xsrc->nr_irqs);
 
     xsrc->status = g_malloc0(xsrc->nr_irqs);
+    xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
 
     memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
                           &xive_source_esb_ops, xsrc, "xive.esb",
#16 lore raw

PATCH v5 3/36 ppc/xive: introduce the XiveFabric interface

In reply to Cédric Le Goater

The XiveFabric offers a simple interface, between the XiveSource object and the main interrupt controller of the machine. It will forward event notifications to the XIVE Interrupt Virtualization Routing Engine (IVRE).

---
 include/hw/ppc/xive.h | 23 +++++++++++++++++++++++
 hw/intc/xive.c        | 25 +++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
include/hw/ppc/xive.h +23 -0
index e118acd59f1e..be93fae6317b 100644
@@ -12,6 +12,27 @@
 
 #include "hw/sysbus.h"
 
+/*
+ * XIVE Fabric (Interface between Source and Router)
+ */
+
+typedef struct XiveFabric {
+    Object parent;
+} XiveFabric;
+
+#define TYPE_XIVE_FABRIC "xive-fabric"
+#define XIVE_FABRIC(obj)                                     \
+    OBJECT_CHECK(XiveFabric, (obj), TYPE_XIVE_FABRIC)
+#define XIVE_FABRIC_CLASS(klass)                                     \
+    OBJECT_CLASS_CHECK(XiveFabricClass, (klass), TYPE_XIVE_FABRIC)
+#define XIVE_FABRIC_GET_CLASS(obj)                                   \
+    OBJECT_GET_CLASS(XiveFabricClass, (obj), TYPE_XIVE_FABRIC)
+
+typedef struct XiveFabricClass {
+    InterfaceClass parent;
+    void (*notify)(XiveFabric *xf, uint32_t lisn);
+} XiveFabricClass;
+
 /*
  * XIVE Interrupt Source
  */
@@ -42,6 +63,8 @@ typedef struct XiveSource {
     uint64_t        esb_flags;
     uint32_t        esb_shift;
     MemoryRegion    esb_mmio;
+
+    XiveFabric      *xive;
 } XiveSource;
 
 /*
hw/intc/xive.c +25 -0
index ac4605fee8b7..014a2e41f71f 100644
@@ -152,7 +152,11 @@ static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
  */
 static void xive_source_notify(XiveSource *xsrc, int srcno)
 {
+    XiveFabricClass *xfc = XIVE_FABRIC_GET_CLASS(xsrc->xive);
 
+    if (xfc->notify) {
+        xfc->notify(xsrc->xive, srcno);
+    }
 }
 
 /*
@@ -358,6 +362,17 @@ static void xive_source_reset(DeviceState *dev)
 static void xive_source_realize(DeviceState *dev, Error **errp)
 {
     XiveSource *xsrc = XIVE_SOURCE(dev);
+    Object *obj;
+    Error *local_err = NULL;
+
+    obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'xive' not found: ");
+        return;
+    }
+
+    xsrc->xive = XIVE_FABRIC(obj);
 
     if (!xsrc->nr_irqs) {
         error_setg(errp, "Number of interrupt needs to be greater than 0");
@@ -427,9 +442,19 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 
+/*
+ * XIVE Fabric
+ */
+static const TypeInfo xive_fabric_info = {
+    .name = TYPE_XIVE_FABRIC,
+    .parent = TYPE_INTERFACE,
+    .class_size = sizeof(XiveFabricClass),
+};
+
 static void xive_register_types(void)
 {
     type_register_static(&xive_source_info);
+    type_register_static(&xive_fabric_info);
 }
 
 type_init(xive_register_types)
Signature · 1 line
2.17.2
#17 lore raw

PATCH v5 4/36 ppc/xive: introduce the XiveRouter model

In reply to Cédric Le Goater

The XiveRouter models the second sub-engine of the overall XIVE architecture : the Interrupt Virtualization Routing Engine (IVRE).

The IVRE handles event notifications of the IVSE through MMIO stores and performs the interrupt routing process. For this purpose, it uses a set of table stored in system memory, the first of which being the Event Assignment Structure (EAS) table.

The EAT associates an interrupt source number with an Event Notification Descriptor (END) which will be used in a second phase of the routing process to identify a Notification Virtual Target.

The XiveRouter is an abstract class which needs to be inherited from to define a storage for the EAT, and other upcoming tables. The 'chip-id' atttribute is not strictly necessary for the sPAPR and PowerNV machines but it's a good way to test the routing algorithm. Without this atttribute, the XiveRouter could be a simple QOM interface.

---
 include/hw/ppc/xive.h      | 32 ++++++++++++++
 include/hw/ppc/xive_regs.h | 31 ++++++++++++++
 hw/intc/xive.c             | 86 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 include/hw/ppc/xive_regs.h
include/hw/ppc/xive.h +32 -0
index be93fae6317b..5a0696366577 100644
@@ -11,6 +11,7 @@
 #define PPC_XIVE_H
 
 #include "hw/sysbus.h"
+#include "hw/ppc/xive_regs.h"
 
 /*
  * XIVE Fabric (Interface between Source and Router)
@@ -168,4 +169,35 @@ static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
     }
 }
 
+/*
+ * XIVE Router
+ */
+
+typedef struct XiveRouter {
+    SysBusDevice    parent;
+
+    uint32_t        chip_id;
+} XiveRouter;
+
+#define TYPE_XIVE_ROUTER "xive-router"
+#define XIVE_ROUTER(obj)                                \
+    OBJECT_CHECK(XiveRouter, (obj), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_CLASS(klass)                                        \
+    OBJECT_CLASS_CHECK(XiveRouterClass, (klass), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_GET_CLASS(obj)                              \
+    OBJECT_GET_CLASS(XiveRouterClass, (obj), TYPE_XIVE_ROUTER)
+
+typedef struct XiveRouterClass {
+    SysBusDeviceClass parent;
+
+    /* XIVE table accessors */
+    int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+} XiveRouterClass;
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h new file +31 -0
new file mode 100644
index 000000000000..12499b33614c
@@ -0,0 +1,31 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2016-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#ifndef PPC_XIVE_REGS_H
+#define PPC_XIVE_REGS_H
+
+/* EAS (Event Assignment Structure)
+ *
+ * One per interrupt source. Targets an interrupt to a given Event
+ * Notification Descriptor (END) and provides the corresponding
+ * logical interrupt number (END data)
+ */
+typedef struct XiveEAS {
+        /* Use a single 64-bit definition to make it easier to
+         * perform atomic updates
+         */
+        uint64_t        w;
+#define EAS_VALID       PPC_BIT(0)
+#define EAS_END_BLOCK   PPC_BITMASK(4, 7)        /* Destination END block# */
+#define EAS_END_INDEX   PPC_BITMASK(8, 31)       /* Destination END index */
+#define EAS_MASKED      PPC_BIT(32)              /* Masked */
+#define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
+} XiveEAS;
+
+#endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +86 -0

98 lines not on this page · raw message

Signature · 1 line
2.17.2
#18 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 57 lines · Cédric Le Goater

The XiveRouter models the second sub-engine of the overall XIVE architecture : the Interrupt Virtualization Routing Engine (IVRE).

The IVRE handles event notifications of the IVSE through MMIO stores and performs the interrupt routing process. For this purpose, it uses a set of table stored in system memory, the first of which being the Event Assignment Structure (EAS) table.

The EAT associates an interrupt source number with an Event Notification Descriptor (END) which will be used in a second phase of the routing process to identify a Notification Virtual Target.

The XiveRouter is an abstract class which needs to be inherited from to define a storage for the EAT, and other upcoming tables. The 'chip-id' atttribute is not strictly necessary for the sPAPR and PowerNV machines but it's a good way to test the routing algorithm. Without this atttribute, the XiveRouter could be a simple QOM interface.

---
 include/hw/ppc/xive.h      | 32 ++++++++++++++
 include/hw/ppc/xive_regs.h | 31 ++++++++++++++
 hw/intc/xive.c             | 86 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 include/hw/ppc/xive_regs.h
include/hw/ppc/xive.h +15 -0
index be93fae6317b..5a0696366577 100644
@@ -11,6 +11,7 @@
 #define PPC_XIVE_H
 
 #include "hw/sysbus.h"
+#include "hw/ppc/xive_regs.h"
 
 /*
  * XIVE Fabric (Interface between Source and Router)
@@ -168,4 +169,35 @@ static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
     }
 }
 
+/*
+ * XIVE Router
+ */
+
+typedef struct XiveRouter {
+    SysBusDevice    parent;
+
+    uint32_t        chip_id;
+} XiveRouter;
+
+#define TYPE_XIVE_ROUTER "xive-router"
+#define XIVE_ROUTER(obj)                                \
+    OBJECT_CHECK(XiveRouter, (obj), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_CLASS(klass)                                        \

include/hw/ppc/xive.h static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno, · line 195

Quoted 10 lines
include/hw/ppc/xive.h +10 -0
+    OBJECT_CLASS_CHECK(XiveRouterClass, (klass), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_GET_CLASS(obj)                              \
+    OBJECT_GET_CLASS(XiveRouterClass, (obj), TYPE_XIVE_ROUTER)
+
+typedef struct XiveRouterClass {
+    SysBusDeviceClass parent;
+
+    /* XIVE table accessors */
+    int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

2) The signatures are a bit odd here. For the setter, a value would make sense than a (XiveEAS *), since it's just a word. For the getter you could return the EAS value directly rather than using a pointer - there's already a valid bit in the EAS so you can construct a value with that cleared if the lisn is out of bounds.

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
#19 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 6 lines · David Gibson

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Nope, though the PAPR model will via hcalls

Quoted 6 lines

2) The signatures are a bit odd here. For the setter, a value would make sense than a (XiveEAS *), since it's just a word. For the getter you could return the EAS value directly rather than using a pointer - there's already a valid bit in the EAS so you can construct a value with that cleared if the lisn is out of bounds.

#20 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

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

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Nope, though the PAPR model will via hcalls

Indeed. The H_INT_SET_SOURCE_CONFIG hcall updates the EAT.

Quoted 5 lines
Quoted 5 lines

2) The signatures are a bit odd here. For the setter, a value would make sense than a (XiveEAS *), since it's just a word. For the getter you could return the EAS value directly rather than using a pointer - there's already a valid bit in the EAS so you can construct a value with that cleared if the lisn is out of bounds.

Yes we could. I think I made it that way to be consistent with the other XIVE internal structures which are bigger : END, NVT

There might be other reasons in Pnv. One was to use generic accessors to the guest RAM but I didn't do it finally. Take a look at the Pnv model and we might decide to change the prototype then. I don't think it's a major change.

Thanks,

C.

#21 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 13 lines · Cédric Le Goater
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Nope, though the PAPR model will via hcalls

Indeed. The H_INT_SET_SOURCE_CONFIG hcall updates the EAT.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

2) The signatures are a bit odd here. For the setter, a value would make sense than a (XiveEAS *), since it's just a word. For the getter you could return the EAS value directly rather than using a pointer - there's already a valid bit in the EAS so you can construct a value with that cleared if the lisn is out of bounds.

Yes we could. I think I made it that way to be consistent with the other XIVE internal structures which are bigger : END, NVT

Yeah, but as noted elsewhere I don't really like the get/set model for the bigger-than-word-size structures. It gives the impression that they're atomic updates when they can't be, as well as unnecessarily copying a bunch of stuff, sometimes on hot paths

Quoted 4 lines

There might be other reasons in Pnv. One was to use generic accessors to the guest RAM but I didn't do it finally. Take a look at the Pnv model and we might decide to change the prototype then. I don't think it's a major change.

Hmmm.

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
#22 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

In reply to Benjamin Herrenschmidt
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Nope, though the PAPR model will via hcalls

Right, bit AIUI the set_eas hook is about abstracting PAPR vs bare metal details. Since the hcall knows it's PAPR it can just update the backing information for the EAS directly, and no need for an abstracted hook.

Quoted 8 lines
Quoted 7 lines

2) The signatures are a bit odd here. For the setter, a value would make sense than a (XiveEAS *), since it's just a word. For the getter you could return the EAS value directly rather than using a pointer - there's already a valid bit in the EAS so you can construct a value with that cleared if the lisn is out of bounds.

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
#23 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

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

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Quoted 6 lines
Quoted 1 line

Nope, though the PAPR model will via hcalls

Right, bit AIUI the set_eas hook is about abstracting PAPR vs bare metal details. Since the hcall knows it's PAPR it can just update the backing information for the EAS directly, and no need for an abstracted hook.

Indeed, the first versions of the XIVE patchset did not use such hooks, but when discussed we said we wanted abstract methods for the router to validate the overall XIVE model, which is useful for PowerNV.

We can change again and have the hcalls get/set directly in the EAT and ENDT. It would certainly simplify the sPAPR model.

C.

Quoted 10 lines
Quoted 9 lines
Quoted 8 lines

2) The signatures are a bit odd here. For the setter, a value would make sense than a (XiveEAS *), since it's just a word. For the getter you could return the EAS value directly rather than using a pointer - there's already a valid bit in the EAS so you can construct a value with that cleared if the lisn is out of bounds.

#24 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 17 lines · Cédric Le Goater
Quoted 9 lines · David Gibson
Quoted 8 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Quoted 6 lines
Quoted 1 line

Nope, though the PAPR model will via hcalls

Right, bit AIUI the set_eas hook is about abstracting PAPR vs bare metal details. Since the hcall knows it's PAPR it can just update the backing information for the EAS directly, and no need for an abstracted hook.

Quoted 6 lines

Indeed, the first versions of the XIVE patchset did not use such hooks, but when discussed we said we wanted abstract methods for the router to validate the overall XIVE model, which is useful for PowerNV.

We can change again and have the hcalls get/set directly in the EAT and ENDT. It would certainly simplify the sPAPR model.

I think that's the better approach.

Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

2) The signatures are a bit odd here. For the setter, a value would make sense than a (XiveEAS *), since it's just a word. For the getter you could return the EAS value directly rather than using a pointer - there's already a valid bit in the EAS so you can construct a value with that cleared if the lisn is out of bounds.

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
#25 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 18 lines · David Gibson
Quoted 17 lines · Cédric Le Goater
Quoted 15 lines · David Gibson
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Nope, though the PAPR model will via hcalls

Right, bit AIUI the set_eas hook is about abstracting PAPR vs bare metal details. Since the hcall knows it's PAPR it can just update the backing information for the EAS directly, and no need for an abstracted hook.

Quoted 8 lines
Quoted 6 lines

Indeed, the first versions of the XIVE patchset did not use such hooks, but when discussed we said we wanted abstract methods for the router to validate the overall XIVE model, which is useful for PowerNV.

We can change again and have the hcalls get/set directly in the EAT and ENDT. It would certainly simplify the sPAPR model.

I think that's the better approach.

ok. let's keep that in mind for :

[PATCH v5 11/36] spapr/xive: use the VCPU id as a NVT identifier [PATCH v5 16/36] spapr: add hcalls support for the XIVE exploitation

which are using the XiveRouter methods to access the controller EAT and ENDT. I thought that was good practice to validate the model but we can use direct sPAPR table accessors or none at all.

I think these prereq patches could be merged now :

[PATCH v5 12/36] spapr: initialize VSMT before initializing the IRQ [PATCH v5 13/36] spapr: introduce a spapr_irq_init() routine [PATCH v5 14/36] spapr: modify the irq backend 'init' method

This one also :

[PATCH v5 21/36] spapr: extend the sPAPR IRQ backend for XICS

Thanks,

C.

#26 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 28 lines · Cédric Le Goater
Quoted 18 lines · David Gibson
Quoted 17 lines · Cédric Le Goater
Quoted 15 lines · David Gibson
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Nope, though the PAPR model will via hcalls

Right, bit AIUI the set_eas hook is about abstracting PAPR vs bare metal details. Since the hcall knows it's PAPR it can just update the backing information for the EAS directly, and no need for an abstracted hook.

Quoted 8 lines
Quoted 6 lines

Indeed, the first versions of the XIVE patchset did not use such hooks, but when discussed we said we wanted abstract methods for the router to validate the overall XIVE model, which is useful for PowerNV.

We can change again and have the hcalls get/set directly in the EAT and ENDT. It would certainly simplify the sPAPR model.

I think that's the better approach.

Quoted 8 lines

ok. let's keep that in mind for :

[PATCH v5 11/36] spapr/xive: use the VCPU id as a NVT identifier [PATCH v5 16/36] spapr: add hcalls support for the XIVE exploitation

which are using the XiveRouter methods to access the controller EAT and ENDT. I thought that was good practice to validate the model but we can use direct sPAPR table accessors or none at all.

Ok. Consistency is good as a general rule, but I don't think it makes sense to force the EAT and the ENDT into the same model. The EAT is pure configuration, whereas the the ENDT has both configuration and status. Or to look at it another way, the EAT is purely software controlled, whereas the ENDT is at least partially hardware controlled.

(I realize that gets a bit fuzzy when considering PAPR, but I think from the point of view of the XIVE model it makes sense to treat the PAPR hypervisor logic as "software", even though it's "hardware" from the guest point of view).

Quoted 16 lines

I think these prereq patches could be merged now :

[PATCH v5 12/36] spapr: initialize VSMT before initializing the IRQ [PATCH v5 13/36] spapr: introduce a spapr_irq_init() routine [PATCH v5 14/36] spapr: modify the irq backend 'init' method

This one also :

[PATCH v5 21/36] spapr: extend the sPAPR IRQ backend for XICS

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
#27 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 34 lines · David Gibson
Quoted 33 lines · Cédric Le Goater
Quoted 18 lines · David Gibson
Quoted 17 lines · Cédric Le Goater
Quoted 15 lines · David Gibson
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Nope, though the PAPR model will via hcalls

Right, bit AIUI the set_eas hook is about abstracting PAPR vs bare metal details. Since the hcall knows it's PAPR it can just update the backing information for the EAS directly, and no need for an abstracted hook.

Quoted 8 lines
Quoted 6 lines

Indeed, the first versions of the XIVE patchset did not use such hooks, but when discussed we said we wanted abstract methods for the router to validate the overall XIVE model, which is useful for PowerNV.

We can change again and have the hcalls get/set directly in the EAT and ENDT. It would certainly simplify the sPAPR model.

I think that's the better approach.

ok. let's keep that in mind for :

[PATCH v5 11/36] spapr/xive: use the VCPU id as a NVT identifier [PATCH v5 16/36] spapr: add hcalls support for the XIVE exploitation

Quoted 6 lines
Quoted 3 lines

which are using the XiveRouter methods to access the controller EAT and ENDT. I thought that was good practice to validate the model but we can use direct sPAPR table accessors or none at all.

Ok. Consistency is good as a general rule, but I don't think it makes sense to force the EAT and the ENDT into the same model.

What do you mean by model ? the QEMU machine IC model ?

Quoted 5 lines

The EAT is
pure configuration, whereas the the ENDT has both configuration and status. Or to look at it another way, the EAT is purely software controlled, whereas the ENDT is at least partially hardware controlled.

yes but the EAT and the ENDT are XIVE internal tables of the same XIVE sub-engine, the IVRE, Interrupt Virtualization Routing Engine, formely known as VC, for Virtualization Controller. the EAS is just an entry point to the ENDT. I don't see why we would use different models for them.

Quoted 4 lines

(I realize that gets a bit fuzzy when considering PAPR, but I think from the point of view of the XIVE model it makes sense to treat the PAPR hypervisor logic as "software", even though it's "hardware" from the guest point of view).

That I agree but the resulting code is too ugly in the hcalls. Tell me when you reach patch 11, which links the machine IC model sPAPRXive to the generic XiveRouter and also check patch 16 introducing the hcalls, which update the XIVE internal tables.

Thanks,

C.

Quoted 17 lines
Quoted 16 lines

I think these prereq patches could be merged now :

[PATCH v5 12/36] spapr: initialize VSMT before initializing the IRQ [PATCH v5 13/36] spapr: introduce a spapr_irq_init() routine [PATCH v5 14/36] spapr: modify the irq backend 'init' method

This one also :

[PATCH v5 21/36] spapr: extend the sPAPR IRQ backend for XICS

Thanks,

C.

#28 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 35 lines · Cédric Le Goater
Quoted 34 lines · David Gibson
Quoted 33 lines · Cédric Le Goater
Quoted 18 lines · David Gibson
Quoted 17 lines · Cédric Le Goater
Quoted 15 lines · David Gibson
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 6 lines · David Gibson

Sorry, didn't think of this in my first reply.

1) Does the hardware ever actually write back to the EAS? I know it does for the END, but it's not clear why it would need to for the EAS. If not, we don't need the setter.

Nope, though the PAPR model will via hcalls

Right, bit AIUI the set_eas hook is about abstracting PAPR vs bare metal details. Since the hcall knows it's PAPR it can just update the backing information for the EAS directly, and no need for an abstracted hook.

Quoted 8 lines
Quoted 6 lines

Indeed, the first versions of the XIVE patchset did not use such hooks, but when discussed we said we wanted abstract methods for the router to validate the overall XIVE model, which is useful for PowerNV.

We can change again and have the hcalls get/set directly in the EAT and ENDT. It would certainly simplify the sPAPR model.

I think that's the better approach.

ok. let's keep that in mind for :

[PATCH v5 11/36] spapr/xive: use the VCPU id as a NVT identifier [PATCH v5 16/36] spapr: add hcalls support for the XIVE exploitation

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

which are using the XiveRouter methods to access the controller EAT and ENDT. I thought that was good practice to validate the model but we can use direct sPAPR table accessors or none at all.

Ok. Consistency is good as a general rule, but I don't think it makes sense to force the EAT and the ENDT into the same model.

What do you mean by model ? the QEMU machine IC model ?

Oh, sorry, nothing that formal. By "model" I just meant the same pattern of accessor hooks. They certainly do belong in the same qemu object.

Quoted 45 lines
Quoted 5 lines

The EAT is
pure configuration, whereas the the ENDT has both configuration and status. Or to look at it another way, the EAT is purely software controlled, whereas the ENDT is at least partially hardware controlled.

yes but the EAT and the ENDT are XIVE internal tables of the same XIVE sub-engine, the IVRE, Interrupt Virtualization Routing Engine, formely known as VC, for Virtualization Controller. the EAS is just an entry point to the ENDT. I don't see why we would use different models for them.

Quoted 4 lines

(I realize that gets a bit fuzzy when considering PAPR, but I think from the point of view of the XIVE model it makes sense to treat the PAPR hypervisor logic as "software", even though it's "hardware" from the guest point of view).

That I agree but the resulting code is too ugly in the hcalls. Tell me when you reach patch 11, which links the machine IC model sPAPRXive to the generic XiveRouter and also check patch 16 introducing the hcalls, which update the XIVE internal tables.

Thanks,

C.

Quoted 17 lines
Quoted 16 lines

I think these prereq patches could be merged now :

[PATCH v5 12/36] spapr: initialize VSMT before initializing the IRQ [PATCH v5 13/36] spapr: introduce a spapr_irq_init() routine [PATCH v5 14/36] spapr: modify the irq backend 'init' method

This one also :

[PATCH v5 21/36] spapr: extend the sPAPR IRQ backend for XICS

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
#29 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

In reply to Cédric Le Goater
Quoted 32 lines · Cédric Le Goater

The XiveRouter models the second sub-engine of the overall XIVE architecture : the Interrupt Virtualization Routing Engine (IVRE).

The IVRE handles event notifications of the IVSE through MMIO stores and performs the interrupt routing process. For this purpose, it uses a set of table stored in system memory, the first of which being the Event Assignment Structure (EAS) table.

The EAT associates an interrupt source number with an Event Notification Descriptor (END) which will be used in a second phase of the routing process to identify a Notification Virtual Target.

The XiveRouter is an abstract class which needs to be inherited from to define a storage for the EAT, and other upcoming tables. The 'chip-id' atttribute is not strictly necessary for the sPAPR and PowerNV machines but it's a good way to test the routing algorithm. Without this atttribute, the XiveRouter could be a simple QOM interface.

---
 include/hw/ppc/xive.h      | 32 ++++++++++++++
 include/hw/ppc/xive_regs.h | 31 ++++++++++++++
 hw/intc/xive.c             | 86 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 include/hw/ppc/xive_regs.h
include/hw/ppc/xive.h +0 -0
index be93fae6317b..5a0696366577 100644

include/hw/ppc/xive.h line 13

Quoted 3 lines
include/hw/ppc/xive.h +0 -0
@@ -11,6 +11,7 @@
 #define PPC_XIVE_H
 
 #include "hw/sysbus.h"

Again, I don't think making this a SysBusDevice is quite right. Even more so for the router than the source, because at least for PAPR it might not have any MMIO presence at all.

Quoted 6 lines
include/hw/ppc/xive.h +1 -0
+#include "hw/ppc/xive_regs.h"
 
 /*
  * XIVE Fabric (Interface between Source and Router)
@@ -168,4 +169,35 @@ static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
     }

include/hw/ppc/xive.h static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno, · line 179

Quoted 10 lines
include/hw/ppc/xive.h +8 -0
 }
 
+/*
+ * XIVE Router
+ */
+
+typedef struct XiveRouter {
+    SysBusDevice    parent;
+
+    uint32_t        chip_id;

I don't think this belongs in the base class. The PowerNV specific variants will need it, but it doesn't make sense for the PAPR version.

Quoted 87 lines
include/hw/ppc/xive.h +23 -0
+} XiveRouter;
+
+#define TYPE_XIVE_ROUTER "xive-router"
+#define XIVE_ROUTER(obj)                                \
+    OBJECT_CHECK(XiveRouter, (obj), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_CLASS(klass)                                        \
+    OBJECT_CLASS_CHECK(XiveRouterClass, (klass), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_GET_CLASS(obj)                              \
+    OBJECT_GET_CLASS(XiveRouterClass, (obj), TYPE_XIVE_ROUTER)
+
+typedef struct XiveRouterClass {
+    SysBusDeviceClass parent;
+
+    /* XIVE table accessors */
+    int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+} XiveRouterClass;
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h new file +31 -0
new file mode 100644
index 000000000000..12499b33614c
@@ -0,0 +1,31 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2016-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#ifndef PPC_XIVE_REGS_H
+#define PPC_XIVE_REGS_H
+
+/* EAS (Event Assignment Structure)
+ *
+ * One per interrupt source. Targets an interrupt to a given Event
+ * Notification Descriptor (END) and provides the corresponding
+ * logical interrupt number (END data)
+ */
+typedef struct XiveEAS {
+        /* Use a single 64-bit definition to make it easier to
+         * perform atomic updates
+         */
+        uint64_t        w;
+#define EAS_VALID       PPC_BIT(0)
+#define EAS_END_BLOCK   PPC_BITMASK(4, 7)        /* Destination END block# */
+#define EAS_END_INDEX   PPC_BITMASK(8, 31)       /* Destination END index */
+#define EAS_MASKED      PPC_BIT(32)              /* Masked */
+#define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
+} XiveEAS;
+
+#endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +18 -0
index 014a2e41f71f..c4c90a25758e 100644
@@ -442,6 +442,91 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 
+/*
+ * XIVE Router (aka. Virtualization Controller or IVRE)
+ */
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
+{
+    XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+    return xrc->get_eas(xrtr, lisn, eas);
+}
+
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
+{
+    XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+    return xrc->set_eas(xrtr, lisn, eas);
+}
+

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 472

Quoted 10 lines
hw/intc/xive.c +10 -0
+static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
+{
+    XiveRouter *xrtr = XIVE_ROUTER(xf);
+    XiveEAS eas;
+
+    /* EAS cache lookup */
+    if (xive_router_get_eas(xrtr, lisn, &eas)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
+        return;
+    }

AFAICT a bad LISN here means a qemu error (in the source, probably), not a user or guest error, so an assert() would be more appropriate.

Quoted 30 lines
hw/intc/xive.c +30 -0
+
+    /* The IVRE has a State Bit Cache for its internal sources which
+     * is also involed at this point. We skip the SBC lookup because
+     * the state bits of the sources are modeled internally in QEMU.
+     */
+
+    if (!(eas.w & EAS_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid LISN %x\n", lisn);
+        return;
+    }
+
+    if (eas.w & EAS_MASKED) {
+        /* Notification completed */
+        return;
+    }
+}
+
+static Property xive_router_properties[] = {
+    DEFINE_PROP_UINT32("chip-id", XiveRouter, chip_id, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_router_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    XiveFabricClass *xfc = XIVE_FABRIC_CLASS(klass);
+
+    dc->desc    = "XIVE Router Engine";
+    dc->props   = xive_router_properties;
+    xfc->notify = xive_router_notify;

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 512

Quoted 10 lines
hw/intc/xive.c +10 -0
+}
+
+static const TypeInfo xive_router_info = {
+    .name          = TYPE_XIVE_ROUTER,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .abstract      = true,
+    .class_size    = sizeof(XiveRouterClass),
+    .class_init    = xive_router_class_init,
+    .interfaces    = (InterfaceInfo[]) {
+        { TYPE_XIVE_FABRIC },

So as far as I can see so far, the XiveFabric interface will essentially have to be implemented on the router object, so I'm not seeing much point to having the interface rather than just a direct call on the router object. But I haven't read the whole series yet, so maybe I'm missing something.

Quoted 28 lines
hw/intc/xive.c +18 -0
+        { }
+    }
+};
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
+{
+    if (!(eas->w & EAS_VALID)) {
+        return;
+    }
+
+    monitor_printf(mon, "  %08x %s end:%02x/%04x data:%08x\n",
+                   lisn, eas->w & EAS_MASKED ? "M" : " ",
+                   (uint8_t)  GETFIELD(EAS_END_BLOCK, eas->w),
+                   (uint32_t) GETFIELD(EAS_END_INDEX, eas->w),
+                   (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
+}
+
 /*
  * XIVE Fabric
  */
@@ -455,6 +540,7 @@ static void xive_register_types(void)
 {
     type_register_static(&xive_source_info);
     type_register_static(&xive_fabric_info);
+    type_register_static(&xive_router_info);
 }
 
 type_init(xive_register_types)
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
#30 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 35 lines · David Gibson
Quoted 34 lines · Cédric Le Goater

The XiveRouter models the second sub-engine of the overall XIVE architecture : the Interrupt Virtualization Routing Engine (IVRE).

The IVRE handles event notifications of the IVSE through MMIO stores and performs the interrupt routing process. For this purpose, it uses a set of table stored in system memory, the first of which being the Event Assignment Structure (EAS) table.

The EAT associates an interrupt source number with an Event Notification Descriptor (END) which will be used in a second phase of the routing process to identify a Notification Virtual Target.

The XiveRouter is an abstract class which needs to be inherited from to define a storage for the EAT, and other upcoming tables. The 'chip-id' atttribute is not strictly necessary for the sPAPR and PowerNV machines but it's a good way to test the routing algorithm. Without this atttribute, the XiveRouter could be a simple QOM interface.

---
 include/hw/ppc/xive.h      | 32 ++++++++++++++
 include/hw/ppc/xive_regs.h | 31 ++++++++++++++
 hw/intc/xive.c             | 86 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 include/hw/ppc/xive_regs.h
include/hw/ppc/xive.h +0 -0
index be93fae6317b..5a0696366577 100644
@@ -11,6 +11,7 @@
 #define PPC_XIVE_H
 
Quoted 5 lines

include/hw/ppc/xive.h line 13

Quoted 1 line
include/hw/ppc/xive.h +0 -0
 #include "hw/sysbus.h"

Again, I don't think making this a SysBusDevice is quite right. Even more so for the router than the source, because at least for PAPR it might not have any MMIO presence at all.

The controller model inherits from the XiveRouter and manages the TIMA.

Quoted 8 lines
Quoted 8 lines
include/hw/ppc/xive.h +1 -0
+#include "hw/ppc/xive_regs.h"
 
 /*
  * XIVE Fabric (Interface between Source and Router)
@@ -168,4 +169,35 @@ static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
     }
 }
 
Quoted 11 lines

include/hw/ppc/xive.h static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno, · line 179

Quoted 8 lines
include/hw/ppc/xive.h +8 -0
+/*
+ * XIVE Router
+ */
+
+typedef struct XiveRouter {
+    SysBusDevice    parent;
+
+    uint32_t        chip_id;

I don't think this belongs in the base class. The PowerNV specific variants will need it, but it doesn't make sense for the PAPR version.

yeah. I am using it as a END and NVT block identifier but it's not required for sPAPR, it could just be zero.

It was good to test the routing algo which should not assume that the block id is zero.

Quoted 93 lines
Quoted 93 lines
include/hw/ppc/xive.h +23 -0
+} XiveRouter;
+
+#define TYPE_XIVE_ROUTER "xive-router"
+#define XIVE_ROUTER(obj)                                \
+    OBJECT_CHECK(XiveRouter, (obj), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_CLASS(klass)                                        \
+    OBJECT_CLASS_CHECK(XiveRouterClass, (klass), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_GET_CLASS(obj)                              \
+    OBJECT_GET_CLASS(XiveRouterClass, (obj), TYPE_XIVE_ROUTER)
+
+typedef struct XiveRouterClass {
+    SysBusDeviceClass parent;
+
+    /* XIVE table accessors */
+    int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+} XiveRouterClass;
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h new file +31 -0
new file mode 100644
index 000000000000..12499b33614c
@@ -0,0 +1,31 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2016-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#ifndef PPC_XIVE_REGS_H
+#define PPC_XIVE_REGS_H
+
+/* EAS (Event Assignment Structure)
+ *
+ * One per interrupt source. Targets an interrupt to a given Event
+ * Notification Descriptor (END) and provides the corresponding
+ * logical interrupt number (END data)
+ */
+typedef struct XiveEAS {
+        /* Use a single 64-bit definition to make it easier to
+         * perform atomic updates
+         */
+        uint64_t        w;
+#define EAS_VALID       PPC_BIT(0)
+#define EAS_END_BLOCK   PPC_BITMASK(4, 7)        /* Destination END block# */
+#define EAS_END_INDEX   PPC_BITMASK(8, 31)       /* Destination END index */
+#define EAS_MASKED      PPC_BIT(32)              /* Masked */
+#define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
+} XiveEAS;
+
+#endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +23 -0
index 014a2e41f71f..c4c90a25758e 100644
@@ -442,6 +442,91 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 
+/*
+ * XIVE Router (aka. Virtualization Controller or IVRE)
+ */
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
+{
+    XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+    return xrc->get_eas(xrtr, lisn, eas);
+}
+
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
+{
+    XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+    return xrc->set_eas(xrtr, lisn, eas);
+}
+
+static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
+{
+    XiveRouter *xrtr = XIVE_ROUTER(xf);
+    XiveEAS eas;
+
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 472

Quoted 5 lines
hw/intc/xive.c +5 -0
+    /* EAS cache lookup */
+    if (xive_router_get_eas(xrtr, lisn, &eas)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
+        return;
+    }

AFAICT a bad LISN here means a qemu error (in the source, probably), not a user or guest error, so an assert() would be more appropriate.

hmm, I would say no because in the case of PowerNV, the firmware could have badly configured the ISN offset of a source which would notify the router with a bad notification event data.

Quoted 38 lines
Quoted 38 lines
hw/intc/xive.c +38 -0
+
+    /* The IVRE has a State Bit Cache for its internal sources which
+     * is also involed at this point. We skip the SBC lookup because
+     * the state bits of the sources are modeled internally in QEMU.
+     */
+
+    if (!(eas.w & EAS_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid LISN %x\n", lisn);
+        return;
+    }
+
+    if (eas.w & EAS_MASKED) {
+        /* Notification completed */
+        return;
+    }
+}
+
+static Property xive_router_properties[] = {
+    DEFINE_PROP_UINT32("chip-id", XiveRouter, chip_id, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_router_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    XiveFabricClass *xfc = XIVE_FABRIC_CLASS(klass);
+
+    dc->desc    = "XIVE Router Engine";
+    dc->props   = xive_router_properties;
+    xfc->notify = xive_router_notify;
+}
+
+static const TypeInfo xive_router_info = {
+    .name          = TYPE_XIVE_ROUTER,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .abstract      = true,
+    .class_size    = sizeof(XiveRouterClass),
+    .class_init    = xive_router_class_init,
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 512

Quoted 2 lines
hw/intc/xive.c +2 -0
+    .interfaces    = (InterfaceInfo[]) {
+        { TYPE_XIVE_FABRIC },

So as far as I can see so far, the XiveFabric interface will essentially have to be implemented on the router object, so I'm not seeing much point to having the interface rather than just a direct call on the router object. But I haven't read the whole series yet, so maybe I'm missing something.

The PSIHB and PHB4 models are using it but there are not in the series.

I can send the PSIHB patch in the next version if you like, it's the patch right after PnvXive. It's attached below for the moment. Look at pnv_psi_notify().

Thanks,

C.

Quoted 27 lines
Quoted 27 lines
hw/intc/xive.c +18 -0
+        { }
+    }
+};
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
+{
+    if (!(eas->w & EAS_VALID)) {
+        return;
+    }
+
+    monitor_printf(mon, "  %08x %s end:%02x/%04x data:%08x\n",
+                   lisn, eas->w & EAS_MASKED ? "M" : " ",
+                   (uint8_t)  GETFIELD(EAS_END_BLOCK, eas->w),
+                   (uint32_t) GETFIELD(EAS_END_INDEX, eas->w),
+                   (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
+}
+
 /*
  * XIVE Fabric
  */
@@ -455,6 +540,7 @@ static void xive_register_types(void)
 {
     type_register_static(&xive_source_info);
     type_register_static(&xive_fabric_info);
+    type_register_static(&xive_router_info);
 }
 
Quoted 4 lines

hw/intc/xive.c static void xive_register_types(void) · line 546

Quoted 1 line
hw/intc/xive.c +0 -0
 type_init(xive_register_types)

From 680fd6ff7c99e669708fbc5cfdbfcd95e83e7c07 Mon Sep 17 00:00:00 2001

From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= <[email protected]>
Date: Wed, 21 Nov 2018 10:29:45 +0100
Subject: [PATCH] ppc/pnv: add a PSI bridge model for POWER9 processor
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The PSI bridge on POWER9 is very similar to POWER8. The BAR is still set through XSCOM but the controls are now entirely done with MMIOs. More interrupts are defined and the interrupt controller interface has changed to XIVE. The POWER9 model is a first example of the usage of the notify() handler of the XiveFabric interface, linking the PSI XiveSource to its owning device model.

---
 include/hw/ppc/pnv.h       |   6 +
 include/hw/ppc/pnv_psi.h   |  50 ++++-
 include/hw/ppc/pnv_xscom.h |   3 +
 hw/ppc/pnv.c               |  20 +-
 hw/ppc/pnv_psi.c           | 390 ++++++++++++++++++++++++++++++++++---
 5 files changed, 444 insertions(+), 25 deletions(-)
include/hw/ppc/pnv.h +6 -0
index c402e5d5844b..8be1147481f9 100644
@@ -88,6 +88,7 @@ typedef struct Pnv9Chip {
 
     /*< public >*/
     PnvXive      xive;
+    PnvPsi       psi;
 } Pnv9Chip;
 
 typedef struct PnvChipClass {
@@ -250,11 +251,16 @@ void pnv_bmc_powerdown(IPMIBmc *bmc);
 #define PNV9_XIVE_PC_SIZE            0x0000001000000000ull
 #define PNV9_XIVE_PC_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006018000000000ull)
 
+#define PNV9_PSIHB_SIZE              0x0000000000100000ull
+#define PNV9_PSIHB_BASE(chip)        PNV9_CHIP_BASE(chip, 0x0006030203000000ull)
+
 #define PNV9_XIVE_IC_SIZE            0x0000000000080000ull
 #define PNV9_XIVE_IC_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006030203100000ull)
 
 #define PNV9_XIVE_TM_SIZE            0x0000000000040000ull
 #define PNV9_XIVE_TM_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006030203180000ull)
 
+#define PNV9_PSIHB_ESB_SIZE          0x0000000000010000ull
+#define PNV9_PSIHB_ESB_BASE(chip)    PNV9_CHIP_BASE(chip, 0x00060302031c0000ull)
 
 #endif /* _PPC_PNV_H */
include/hw/ppc/pnv_psi.h +48 -2

73 lines not on this page · raw message

include/hw/ppc/pnv_xscom.h +3 -0
index 5bd43467a1ab..019b45bf9189 100644
@@ -82,6 +82,9 @@ typedef struct PnvXScomInterfaceClass {
 #define PNV_XSCOM_PBCQ_SPCI_BASE  0x9013c00
 #define PNV_XSCOM_PBCQ_SPCI_SIZE  0x5
 
+#define PNV9_XSCOM_PSIHB_BASE     0x5012900
+#define PNV9_XSCOM_PSIHB_SIZE     0x100
+
 #define PNV9_XSCOM_XIVE_BASE      0x5013000
 #define PNV9_XSCOM_XIVE_SIZE      0x300
 
hw/ppc/pnv.c +19 -1
index b6af896c30e4..e67c9d7d3995 100644
@@ -743,7 +743,7 @@ static void pnv_chip_power8_instance_init(Object *obj)
     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(obj);
     int i;
 
-    object_initialize(&chip8->psi, sizeof(chip8->psi), TYPE_PNV_PSI);
+    object_initialize(&chip8->psi, sizeof(chip8->psi), TYPE_PNV_PSI_POWER8);
     object_property_add_child(obj, "psi", OBJECT(&chip8->psi), NULL);
     object_property_add_const_link(OBJECT(&chip8->psi), "xics",
                                    OBJECT(qdev_get_machine()), &error_abort);
@@ -923,6 +923,11 @@ static void pnv_chip_power9_instance_init(Object *obj)
     object_property_add_child(obj, "xive", OBJECT(&chip9->xive), NULL);
     object_property_add_const_link(OBJECT(&chip9->xive), "chip", obj,
                                    &error_abort);
+
+    object_initialize(&chip9->psi, sizeof(chip9->psi), TYPE_PNV_PSI_POWER9);
+    object_property_add_child(obj, "psi", OBJECT(&chip9->psi), NULL);
+    object_property_add_const_link(OBJECT(&chip9->psi), "chip", obj,
+                                   &error_abort);
 }
 
 static void pnv_chip_power9_realize(DeviceState *dev, Error **errp)
@@ -955,6 +960,18 @@ static void pnv_chip_power9_realize(DeviceState *dev, Error **errp)
     qdev_set_parent_bus(DEVICE(&chip9->xive), sysbus_get_default());
     pnv_xscom_add_subregion(chip, PNV9_XSCOM_XIVE_BASE,
                             &chip9->xive.xscom_regs);
+
+    /* Processor Service Interface (PSI) Host Bridge */
+    object_property_set_int(OBJECT(&chip9->psi), PNV9_PSIHB_BASE(chip),
+                            "bar", &error_fatal);
+    object_property_set_bool(OBJECT(&chip9->psi), true, "realized", &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+    qdev_set_parent_bus(DEVICE(&chip9->psi), sysbus_get_default());
+    pnv_xscom_add_subregion(chip, PNV9_XSCOM_PSIHB_BASE,
+                            &chip9->psi.xscom_regs);
 }
 
 static void pnv_chip_power9_class_init(ObjectClass *klass, void *data)
@@ -1188,6 +1205,7 @@ static void pnv_pic_print_info(InterruptStatsProvider *obj,
             Pnv9Chip *chip9 = PNV9_CHIP(chip);
 
              pnv_xive_pic_print_info(&chip9->xive, mon);
+             pnv_psi_pic_print_info(&chip9->psi, mon);
         } else {
             Pnv8Chip *chip8 = PNV8_CHIP(chip);
 
hw/ppc/pnv_psi.c +368 -22

497 lines not on this page · raw message

Signature · 1 line
2.17.2
#31 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 36 lines · Cédric Le Goater
Quoted 35 lines · David Gibson
Quoted 34 lines · Cédric Le Goater

The XiveRouter models the second sub-engine of the overall XIVE architecture : the Interrupt Virtualization Routing Engine (IVRE).

The IVRE handles event notifications of the IVSE through MMIO stores and performs the interrupt routing process. For this purpose, it uses a set of table stored in system memory, the first of which being the Event Assignment Structure (EAS) table.

The EAT associates an interrupt source number with an Event Notification Descriptor (END) which will be used in a second phase of the routing process to identify a Notification Virtual Target.

The XiveRouter is an abstract class which needs to be inherited from to define a storage for the EAT, and other upcoming tables. The 'chip-id' atttribute is not strictly necessary for the sPAPR and PowerNV machines but it's a good way to test the routing algorithm. Without this atttribute, the XiveRouter could be a simple QOM interface.

---
 include/hw/ppc/xive.h      | 32 ++++++++++++++
 include/hw/ppc/xive_regs.h | 31 ++++++++++++++
 hw/intc/xive.c             | 86 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 include/hw/ppc/xive_regs.h
include/hw/ppc/xive.h +0 -0
index be93fae6317b..5a0696366577 100644
@@ -11,6 +11,7 @@
 #define PPC_XIVE_H
 
Quoted 8 lines
Quoted 5 lines

include/hw/ppc/xive.h line 13

Quoted 1 line
include/hw/ppc/xive.h +0 -0
 #include "hw/sysbus.h"

Again, I don't think making this a SysBusDevice is quite right. Even more so for the router than the source, because at least for PAPR it might not have any MMIO presence at all.

The controller model inherits from the XiveRouter and manages the TIMA.

Um.. I'm not sure what you mean by the "controller model". Surely the presenter should own the TIMA, not the router?

Quoted 129 lines
Quoted 9 lines
Quoted 9 lines
include/hw/ppc/xive.h +1 -0
+#include "hw/ppc/xive_regs.h"
 
 /*
  * XIVE Fabric (Interface between Source and Router)
@@ -168,4 +169,35 @@ static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
     }
 }
 
Quoted 11 lines

include/hw/ppc/xive.h static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno, · line 179

Quoted 8 lines
include/hw/ppc/xive.h +8 -0
+/*
+ * XIVE Router
+ */
+
+typedef struct XiveRouter {
+    SysBusDevice    parent;
+
+    uint32_t        chip_id;

I don't think this belongs in the base class. The PowerNV specific variants will need it, but it doesn't make sense for the PAPR version.

yeah. I am using it as a END and NVT block identifier but it's not required for sPAPR, it could just be zero.

It was good to test the routing algo which should not assume that the block id is zero.

Quoted 93 lines
Quoted 93 lines
include/hw/ppc/xive.h +23 -0
+} XiveRouter;
+
+#define TYPE_XIVE_ROUTER "xive-router"
+#define XIVE_ROUTER(obj)                                \
+    OBJECT_CHECK(XiveRouter, (obj), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_CLASS(klass)                                        \
+    OBJECT_CLASS_CHECK(XiveRouterClass, (klass), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_GET_CLASS(obj)                              \
+    OBJECT_GET_CLASS(XiveRouterClass, (obj), TYPE_XIVE_ROUTER)
+
+typedef struct XiveRouterClass {
+    SysBusDeviceClass parent;
+
+    /* XIVE table accessors */
+    int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+} XiveRouterClass;
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h new file +31 -0
new file mode 100644
index 000000000000..12499b33614c
@@ -0,0 +1,31 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2016-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#ifndef PPC_XIVE_REGS_H
+#define PPC_XIVE_REGS_H
+
+/* EAS (Event Assignment Structure)
+ *
+ * One per interrupt source. Targets an interrupt to a given Event
+ * Notification Descriptor (END) and provides the corresponding
+ * logical interrupt number (END data)
+ */
+typedef struct XiveEAS {
+        /* Use a single 64-bit definition to make it easier to
+         * perform atomic updates
+         */
+        uint64_t        w;
+#define EAS_VALID       PPC_BIT(0)
+#define EAS_END_BLOCK   PPC_BITMASK(4, 7)        /* Destination END block# */
+#define EAS_END_INDEX   PPC_BITMASK(8, 31)       /* Destination END index */
+#define EAS_MASKED      PPC_BIT(32)              /* Masked */
+#define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
+} XiveEAS;
+
+#endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +23 -0
index 014a2e41f71f..c4c90a25758e 100644
@@ -442,6 +442,91 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 
+/*
+ * XIVE Router (aka. Virtualization Controller or IVRE)
+ */
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
+{
+    XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+    return xrc->get_eas(xrtr, lisn, eas);
+}
+
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
+{
+    XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+    return xrc->set_eas(xrtr, lisn, eas);
+}
+
+static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
+{
+    XiveRouter *xrtr = XIVE_ROUTER(xf);
+    XiveEAS eas;
+
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 472

Quoted 5 lines
hw/intc/xive.c +5 -0
+    /* EAS cache lookup */
+    if (xive_router_get_eas(xrtr, lisn, &eas)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
+        return;
+    }

AFAICT a bad LISN here means a qemu error (in the source, probably), not a user or guest error, so an assert() would be more appropriate.

Quoted 3 lines

hmm, I would say no because in the case of PowerNV, the firmware could have badly configured the ISN offset of a source which would notify the router with a bad notification event data.

Ah, good point. That's fine as it is then.

Quoted 47 lines
Quoted 38 lines
Quoted 38 lines
hw/intc/xive.c +38 -0
+
+    /* The IVRE has a State Bit Cache for its internal sources which
+     * is also involed at this point. We skip the SBC lookup because
+     * the state bits of the sources are modeled internally in QEMU.
+     */
+
+    if (!(eas.w & EAS_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid LISN %x\n", lisn);
+        return;
+    }
+
+    if (eas.w & EAS_MASKED) {
+        /* Notification completed */
+        return;
+    }
+}
+
+static Property xive_router_properties[] = {
+    DEFINE_PROP_UINT32("chip-id", XiveRouter, chip_id, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_router_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    XiveFabricClass *xfc = XIVE_FABRIC_CLASS(klass);
+
+    dc->desc    = "XIVE Router Engine";
+    dc->props   = xive_router_properties;
+    xfc->notify = xive_router_notify;
+}
+
+static const TypeInfo xive_router_info = {
+    .name          = TYPE_XIVE_ROUTER,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .abstract      = true,
+    .class_size    = sizeof(XiveRouterClass),
+    .class_init    = xive_router_class_init,
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 512

Quoted 2 lines
hw/intc/xive.c +2 -0
+    .interfaces    = (InterfaceInfo[]) {
+        { TYPE_XIVE_FABRIC },

So as far as I can see so far, the XiveFabric interface will essentially have to be implemented on the router object, so I'm not seeing much point to having the interface rather than just a direct call on the router object. But I haven't read the whole series yet, so maybe I'm missing something.

Quoted 5 lines

The PSIHB and PHB4 models are using it but there are not in the series.

I can send the PSIHB patch in the next version if you like, it's the patch right after PnvXive. It's attached below for the moment. Look at pnv_psi_notify().

Hrm, I see. This seems like a really convoluted way of achieving what you need here. We want to abstract exactly how the source delivers notifies, but doing it with an interface on some object that's not necessarily either the source or the router seems odd. At the very least the names need to change (of both interface and property for the target object).

Quoted 749 lines

Thanks,

C.

Quoted 27 lines
Quoted 27 lines
hw/intc/xive.c +18 -0
+        { }
+    }
+};
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
+{
+    if (!(eas->w & EAS_VALID)) {
+        return;
+    }
+
+    monitor_printf(mon, "  %08x %s end:%02x/%04x data:%08x\n",
+                   lisn, eas->w & EAS_MASKED ? "M" : " ",
+                   (uint8_t)  GETFIELD(EAS_END_BLOCK, eas->w),
+                   (uint32_t) GETFIELD(EAS_END_INDEX, eas->w),
+                   (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
+}
+
 /*
  * XIVE Fabric
  */
@@ -455,6 +540,7 @@ static void xive_register_types(void)
 {
     type_register_static(&xive_source_info);
     type_register_static(&xive_fabric_info);
+    type_register_static(&xive_router_info);
 }
 
Quoted 5 lines

hw/intc/xive.c static void xive_register_types(void) · line 546

Quoted 1 line
hw/intc/xive.c +0 -0
 type_init(xive_register_types)

From 680fd6ff7c99e669708fbc5cfdbfcd95e83e7c07 Mon Sep 17 00:00:00 2001

From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= <[email protected]>
Date: Wed, 21 Nov 2018 10:29:45 +0100
Subject: [PATCH] ppc/pnv: add a PSI bridge model for POWER9 processor
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The PSI bridge on POWER9 is very similar to POWER8. The BAR is still set through XSCOM but the controls are now entirely done with MMIOs. More interrupts are defined and the interrupt controller interface has changed to XIVE. The POWER9 model is a first example of the usage of the notify() handler of the XiveFabric interface, linking the PSI XiveSource to its owning device model.

---
 include/hw/ppc/pnv.h       |   6 +
 include/hw/ppc/pnv_psi.h   |  50 ++++-
 include/hw/ppc/pnv_xscom.h |   3 +
 hw/ppc/pnv.c               |  20 +-
 hw/ppc/pnv_psi.c           | 390 ++++++++++++++++++++++++++++++++++---
 5 files changed, 444 insertions(+), 25 deletions(-)
include/hw/ppc/pnv.h +6 -0
index c402e5d5844b..8be1147481f9 100644
@@ -88,6 +88,7 @@ typedef struct Pnv9Chip {
 
     /*< public >*/
     PnvXive      xive;
+    PnvPsi       psi;
 } Pnv9Chip;
 
 typedef struct PnvChipClass {
@@ -250,11 +251,16 @@ void pnv_bmc_powerdown(IPMIBmc *bmc);
 #define PNV9_XIVE_PC_SIZE            0x0000001000000000ull
 #define PNV9_XIVE_PC_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006018000000000ull)
 
+#define PNV9_PSIHB_SIZE              0x0000000000100000ull
+#define PNV9_PSIHB_BASE(chip)        PNV9_CHIP_BASE(chip, 0x0006030203000000ull)
+
 #define PNV9_XIVE_IC_SIZE            0x0000000000080000ull
 #define PNV9_XIVE_IC_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006030203100000ull)
 
 #define PNV9_XIVE_TM_SIZE            0x0000000000040000ull
 #define PNV9_XIVE_TM_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006030203180000ull)
 
+#define PNV9_PSIHB_ESB_SIZE          0x0000000000010000ull
+#define PNV9_PSIHB_ESB_BASE(chip)    PNV9_CHIP_BASE(chip, 0x00060302031c0000ull)
 
 #endif /* _PPC_PNV_H */
include/hw/ppc/pnv_psi.h +48 -2
index f6af5eae1fa8..b8f8d082bcf9 100644
@@ -21,10 +21,35 @@
 
 #include "hw/sysbus.h"
 #include "hw/ppc/xics.h"
+#include "hw/ppc/xive.h"
 
 #define TYPE_PNV_PSI "pnv-psi"
 #define PNV_PSI(obj) \
      OBJECT_CHECK(PnvPsi, (obj), TYPE_PNV_PSI)
+#define PNV_PSI_CLASS(klass) \
+     OBJECT_CLASS_CHECK(PnvPsiClass, (klass), TYPE_PNV_PSI)
+#define PNV_PSI_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(PnvPsiClass, (obj), TYPE_PNV_PSI)
+
+typedef struct PnvPsi PnvPsi;
+typedef struct PnvChip PnvChip;
+typedef struct PnvPsiClass {
+    SysBusDeviceClass parent_class;
+
+    int chip_type;
+    uint32_t xscom_pcba;
+    uint32_t xscom_size;
+
+    void (*irq_set)(PnvPsi *psi, int, bool state);
+} PnvPsiClass;
+
+#define TYPE_PNV_PSI_POWER8 TYPE_PNV_PSI "-POWER8"
+#define PNV_PSI_POWER8(obj) \
+    OBJECT_CHECK(PnvPsi, (obj), TYPE_PNV_PSI_POWER8)
+
+#define TYPE_PNV_PSI_POWER9 TYPE_PNV_PSI "-POWER9"
+#define PNV_PSI_POWER9(obj) \
+    OBJECT_CHECK(PnvPsi, (obj), TYPE_PNV_PSI_POWER9)
 
 #define PSIHB_XSCOM_MAX         0x20
 
@@ -38,9 +63,12 @@ typedef struct PnvPsi {
     /* MemoryRegion fsp_mr; */
     uint64_t fsp_bar;
 
-    /* Interrupt generation */
+    /* P8 Interrupt generation */
     ICSState ics;
 
+    /* P9 Interrupt generation */
+    XiveSource source;
+
     /* Registers */
     uint64_t regs[PSIHB_XSCOM_MAX];
 
@@ -60,6 +88,24 @@ typedef enum PnvPsiIrq {
 
 #define PSI_NUM_INTERRUPTS 6
 
-extern void pnv_psi_irq_set(PnvPsi *psi, PnvPsiIrq irq, bool state);
+/* P9 PSI Interrupts */
+#define PSIHB9_IRQ_PSI          0
+#define PSIHB9_IRQ_OCC          1
+#define PSIHB9_IRQ_FSI          2
+#define PSIHB9_IRQ_LPCHC        3
+#define PSIHB9_IRQ_LOCAL_ERR    4
+#define PSIHB9_IRQ_GLOBAL_ERR   5
+#define PSIHB9_IRQ_TPM          6
+#define PSIHB9_IRQ_LPC_SIRQ0    7
+#define PSIHB9_IRQ_LPC_SIRQ1    8
+#define PSIHB9_IRQ_LPC_SIRQ2    9
+#define PSIHB9_IRQ_LPC_SIRQ3    10
+#define PSIHB9_IRQ_SBE_I2C      11
+#define PSIHB9_IRQ_DIO          12
+#define PSIHB9_IRQ_PSU          13
+#define PSIHB9_NUM_IRQS         14
+
+void pnv_psi_irq_set(PnvPsi *psi, int irq, bool state);
+void pnv_psi_pic_print_info(PnvPsi *psi, Monitor *mon);
 
 #endif /* _PPC_PNV_PSI_H */
include/hw/ppc/pnv_xscom.h +3 -0
index 5bd43467a1ab..019b45bf9189 100644
@@ -82,6 +82,9 @@ typedef struct PnvXScomInterfaceClass {
 #define PNV_XSCOM_PBCQ_SPCI_BASE  0x9013c00
 #define PNV_XSCOM_PBCQ_SPCI_SIZE  0x5
 
+#define PNV9_XSCOM_PSIHB_BASE     0x5012900
+#define PNV9_XSCOM_PSIHB_SIZE     0x100
+
 #define PNV9_XSCOM_XIVE_BASE      0x5013000
 #define PNV9_XSCOM_XIVE_SIZE      0x300
 
hw/ppc/pnv.c +19 -1
index b6af896c30e4..e67c9d7d3995 100644
@@ -743,7 +743,7 @@ static void pnv_chip_power8_instance_init(Object *obj)
     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(obj);
     int i;
 
-    object_initialize(&chip8->psi, sizeof(chip8->psi), TYPE_PNV_PSI);
+    object_initialize(&chip8->psi, sizeof(chip8->psi), TYPE_PNV_PSI_POWER8);
     object_property_add_child(obj, "psi", OBJECT(&chip8->psi), NULL);
     object_property_add_const_link(OBJECT(&chip8->psi), "xics",
                                    OBJECT(qdev_get_machine()), &error_abort);
@@ -923,6 +923,11 @@ static void pnv_chip_power9_instance_init(Object *obj)
     object_property_add_child(obj, "xive", OBJECT(&chip9->xive), NULL);
     object_property_add_const_link(OBJECT(&chip9->xive), "chip", obj,
                                    &error_abort);
+
+    object_initialize(&chip9->psi, sizeof(chip9->psi), TYPE_PNV_PSI_POWER9);
+    object_property_add_child(obj, "psi", OBJECT(&chip9->psi), NULL);
+    object_property_add_const_link(OBJECT(&chip9->psi), "chip", obj,
+                                   &error_abort);
 }
 
 static void pnv_chip_power9_realize(DeviceState *dev, Error **errp)
@@ -955,6 +960,18 @@ static void pnv_chip_power9_realize(DeviceState *dev, Error **errp)
     qdev_set_parent_bus(DEVICE(&chip9->xive), sysbus_get_default());
     pnv_xscom_add_subregion(chip, PNV9_XSCOM_XIVE_BASE,
                             &chip9->xive.xscom_regs);
+
+    /* Processor Service Interface (PSI) Host Bridge */
+    object_property_set_int(OBJECT(&chip9->psi), PNV9_PSIHB_BASE(chip),
+                            "bar", &error_fatal);
+    object_property_set_bool(OBJECT(&chip9->psi), true, "realized", &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+    qdev_set_parent_bus(DEVICE(&chip9->psi), sysbus_get_default());
+    pnv_xscom_add_subregion(chip, PNV9_XSCOM_PSIHB_BASE,
+                            &chip9->psi.xscom_regs);
 }
 
 static void pnv_chip_power9_class_init(ObjectClass *klass, void *data)
@@ -1188,6 +1205,7 @@ static void pnv_pic_print_info(InterruptStatsProvider *obj,
             Pnv9Chip *chip9 = PNV9_CHIP(chip);
 
              pnv_xive_pic_print_info(&chip9->xive, mon);
+             pnv_psi_pic_print_info(&chip9->psi, mon);
         } else {
             Pnv8Chip *chip8 = PNV8_CHIP(chip);
 
hw/ppc/pnv_psi.c +368 -22
index 5b969127c303..8b85dd9555e8 100644
@@ -22,6 +22,7 @@
 #include "target/ppc/cpu.h"
 #include "qemu/log.h"
 #include "qapi/error.h"
+#include "monitor/monitor.h"
 
 #include "exec/address-spaces.h"
 
@@ -114,12 +115,14 @@
 #define PSIHB_BAR_MASK                  0x0003fffffff00000ull
 #define PSIHB_FSPBAR_MASK               0x0003ffff00000000ull
 
+#define PSIHB_REG(addr) (((addr) >> 3) + PSIHB_XSCOM_BAR)
+
 static void pnv_psi_set_bar(PnvPsi *psi, uint64_t bar)
 {
     MemoryRegion *sysmem = get_system_memory();
     uint64_t old = psi->regs[PSIHB_XSCOM_BAR];
 
-    psi->regs[PSIHB_XSCOM_BAR] = bar & (PSIHB_BAR_MASK | PSIHB_BAR_EN);
+    psi->regs[PSIHB_XSCOM_BAR] = bar;
 
     /* Update MR, always remove it first */
     if (old & PSIHB_BAR_EN) {
@@ -128,7 +131,7 @@ static void pnv_psi_set_bar(PnvPsi *psi, uint64_t bar)
 
     /* Then add it back if needed */
     if (bar & PSIHB_BAR_EN) {
-        uint64_t addr = bar & PSIHB_BAR_MASK;
+        uint64_t addr = bar & ~PSIHB_BAR_EN;
         memory_region_add_subregion(sysmem, addr, &psi->regs_mr);
     }
 }
@@ -205,7 +208,12 @@ static const uint64_t stat_bits[] = {
     [PSIHB_IRQ_EXTERNAL]  = PSIHB_IRQ_STAT_EXT,
 };
 
-void pnv_psi_irq_set(PnvPsi *psi, PnvPsiIrq irq, bool state)
+void pnv_psi_irq_set(PnvPsi *psi, int irq, bool state)
+{
+    PNV_PSI_GET_CLASS(psi)->irq_set(psi, irq, state);
+}
+
+static void pnv_psi_power8_irq_set(PnvPsi *psi, int irq, bool state)
 {
     ICSState *ics = &psi->ics;
     uint32_t xivr_reg;
@@ -324,7 +332,7 @@ static uint64_t pnv_psi_reg_read(PnvPsi *psi, uint32_t offset, bool mmio)
         val = psi->regs[offset];
         break;
     default:
-        qemu_log_mask(LOG_UNIMP, "PSI: read at Ox%" PRIx32 "\n", offset);
+        qemu_log_mask(LOG_UNIMP, "PSI: read at 0x%" PRIx32 "\n", offset);
     }
     return val;
 }
@@ -383,7 +391,7 @@ static void pnv_psi_reg_write(PnvPsi *psi, uint32_t offset, uint64_t val,
         pnv_psi_set_irsn(psi, val);
         break;
     default:
-        qemu_log_mask(LOG_UNIMP, "PSI: write at Ox%" PRIx32 "\n", offset);
+        qemu_log_mask(LOG_UNIMP, "PSI: write at 0x%" PRIx32 "\n", offset);
     }
 }
 
@@ -393,13 +401,13 @@ static void pnv_psi_reg_write(PnvPsi *psi, uint32_t offset, uint64_t val,
  */
 static uint64_t pnv_psi_mmio_read(void *opaque, hwaddr addr, unsigned size)
 {
-    return pnv_psi_reg_read(opaque, (addr >> 3) + PSIHB_XSCOM_BAR, true);
+    return pnv_psi_reg_read(opaque, PSIHB_REG(addr), true);
 }
 
 static void pnv_psi_mmio_write(void *opaque, hwaddr addr,
                               uint64_t val, unsigned size)
 {
-    pnv_psi_reg_write(opaque, (addr >> 3) + PSIHB_XSCOM_BAR, val, true);
+    pnv_psi_reg_write(opaque, PSIHB_REG(addr), val, true);
 }
 
 static const MemoryRegionOps psi_mmio_ops = {
@@ -441,7 +449,7 @@ static const MemoryRegionOps pnv_psi_xscom_ops = {
     }
 };
 
-static void pnv_psi_init(Object *obj)
+static void pnv_psi_power8_instance_init(Object *obj)
 {
     PnvPsi *psi = PNV_PSI(obj);
 
@@ -458,7 +466,7 @@ static const uint8_t irq_to_xivr[] = {
     PSIHB_XSCOM_XIVR_EXT,
 };
 
-static void pnv_psi_realize(DeviceState *dev, Error **errp)
+static void pnv_psi_power8_realize(DeviceState *dev, Error **errp)
 {
     PnvPsi *psi = PNV_PSI(dev);
     ICSState *ics = &psi->ics;
@@ -510,28 +518,34 @@ static void pnv_psi_realize(DeviceState *dev, Error **errp)
     }
 }
 
+static const char compat_p8[] = "ibm,power8-psihb-x\0ibm,psihb-x";
+static const char compat_p9[] = "ibm,power9-psihb-x\0ibm,psihb-x";
+
 static int pnv_psi_dt_xscom(PnvXScomInterface *dev, void *fdt, int xscom_offset)
 {
-    const char compat[] = "ibm,power8-psihb-x\0ibm,psihb-x";
+    PnvPsiClass *ppc = PNV_PSI_GET_CLASS(dev);
     char *name;
     int offset;
-    uint32_t lpc_pcba = PNV_XSCOM_PSIHB_BASE;
     uint32_t reg[] = {
-        cpu_to_be32(lpc_pcba),
-        cpu_to_be32(PNV_XSCOM_PSIHB_SIZE)
+        cpu_to_be32(ppc->xscom_pcba),
+        cpu_to_be32(ppc->xscom_size)
     };
 
-    name = g_strdup_printf("psihb@%x", lpc_pcba);
+    name = g_strdup_printf("psihb@%x", ppc->xscom_pcba);
     offset = fdt_add_subnode(fdt, xscom_offset, name);
     _FDT(offset);
     g_free(name);
 
-    _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
-
-    _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 2)));
-    _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 1)));
-    _FDT((fdt_setprop(fdt, offset, "compatible", compat,
-                      sizeof(compat))));
+    _FDT(fdt_setprop(fdt, offset, "reg", reg, sizeof(reg)));
+    _FDT(fdt_setprop_cell(fdt, offset, "#address-cells", 2));
+    _FDT(fdt_setprop_cell(fdt, offset, "#size-cells", 1));
+    if (ppc->chip_type == PNV_CHIP_POWER9) {
+        _FDT(fdt_setprop(fdt, offset, "compatible", compat_p9,
+                         sizeof(compat_p9)));
+    } else {
+        _FDT(fdt_setprop(fdt, offset, "compatible", compat_p8,
+                         sizeof(compat_p8)));
+    }
     return 0;
 }
 
@@ -541,6 +555,324 @@ static Property pnv_psi_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void pnv_psi_power8_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PnvPsiClass *ppc = PNV_PSI_CLASS(klass);
+
+    dc->desc    = "PowerNV PSI Controller POWER8";
+    dc->realize = pnv_psi_power8_realize;
+
+    ppc->chip_type =  PNV_CHIP_POWER8;
+    ppc->xscom_pcba = PNV_XSCOM_PSIHB_BASE;
+    ppc->xscom_size = PNV_XSCOM_PSIHB_SIZE;
+    ppc->irq_set    = pnv_psi_power8_irq_set;
+}
+
+static const TypeInfo pnv_psi_power8_info = {
+    .name          = TYPE_PNV_PSI_POWER8,
+    .parent        = TYPE_PNV_PSI,
+    .instance_init = pnv_psi_power8_instance_init,
+    .class_init    = pnv_psi_power8_class_init,
+};
+
+/* Common registers */
+
+#define PSIHB9_CR                       0x20
+#define PSIHB9_SEMR                     0x28
+
+/* P9 registers */
+
+#define PSIHB9_INTERRUPT_CONTROL        0x58
+#define   PSIHB9_IRQ_METHOD             PPC_BIT(0)
+#define   PSIHB9_IRQ_RESET              PPC_BIT(1)
+#define PSIHB9_ESB_CI_BASE              0x60
+#define   PSIHB9_ESB_CI_VALID           1
+#define PSIHB9_ESB_NOTIF_ADDR           0x68
+#define   PSIHB9_ESB_NOTIF_VALID        1
+#define PSIHB9_IVT_OFFSET               0x70
+#define   PSIHB9_IVT_OFF_SHIFT          32
+
+#define PSIHB9_IRQ_LEVEL                0x78 /* assertion */
+#define   PSIHB9_IRQ_LEVEL_PSI          PPC_BIT(0)
+#define   PSIHB9_IRQ_LEVEL_OCC          PPC_BIT(1)
+#define   PSIHB9_IRQ_LEVEL_FSI          PPC_BIT(2)
+#define   PSIHB9_IRQ_LEVEL_LPCHC        PPC_BIT(3)
+#define   PSIHB9_IRQ_LEVEL_LOCAL_ERR    PPC_BIT(4)
+#define   PSIHB9_IRQ_LEVEL_GLOBAL_ERR   PPC_BIT(5)
+#define   PSIHB9_IRQ_LEVEL_TPM          PPC_BIT(6)
+#define   PSIHB9_IRQ_LEVEL_LPC_SIRQ1    PPC_BIT(7)
+#define   PSIHB9_IRQ_LEVEL_LPC_SIRQ2    PPC_BIT(8)
+#define   PSIHB9_IRQ_LEVEL_LPC_SIRQ3    PPC_BIT(9)
+#define   PSIHB9_IRQ_LEVEL_LPC_SIRQ4    PPC_BIT(10)
+#define   PSIHB9_IRQ_LEVEL_SBE_I2C      PPC_BIT(11)
+#define   PSIHB9_IRQ_LEVEL_DIO          PPC_BIT(12)
+#define   PSIHB9_IRQ_LEVEL_PSU          PPC_BIT(13)
+#define   PSIHB9_IRQ_LEVEL_I2C_C        PPC_BIT(14)
+#define   PSIHB9_IRQ_LEVEL_I2C_D        PPC_BIT(15)
+#define   PSIHB9_IRQ_LEVEL_I2C_E        PPC_BIT(16)
+#define   PSIHB9_IRQ_LEVEL_SBE          PPC_BIT(19)
+
+#define PSIHB9_IRQ_STAT                 0x80 /* P bit */
+#define   PSIHB9_IRQ_STAT_PSI           PPC_BIT(0)
+#define   PSIHB9_IRQ_STAT_OCC           PPC_BIT(1)
+#define   PSIHB9_IRQ_STAT_FSI           PPC_BIT(2)
+#define   PSIHB9_IRQ_STAT_LPCHC         PPC_BIT(3)
+#define   PSIHB9_IRQ_STAT_LOCAL_ERR     PPC_BIT(4)
+#define   PSIHB9_IRQ_STAT_GLOBAL_ERR    PPC_BIT(5)
+#define   PSIHB9_IRQ_STAT_TPM           PPC_BIT(6)
+#define   PSIHB9_IRQ_STAT_LPC_SIRQ1     PPC_BIT(7)
+#define   PSIHB9_IRQ_STAT_LPC_SIRQ2     PPC_BIT(8)
+#define   PSIHB9_IRQ_STAT_LPC_SIRQ3     PPC_BIT(9)
+#define   PSIHB9_IRQ_STAT_LPC_SIRQ4     PPC_BIT(10)
+#define   PSIHB9_IRQ_STAT_SBE_I2C       PPC_BIT(11)
+#define   PSIHB9_IRQ_STAT_DIO           PPC_BIT(12)
+#define   PSIHB9_IRQ_STAT_PSU           PPC_BIT(13)
+
+static void pnv_psi_notify(XiveFabric *xf, uint32_t srcno)
+{
+    PnvPsi *psi = PNV_PSI(xf);
+    uint64_t notif_port = psi->regs[PSIHB_REG(PSIHB9_ESB_NOTIF_ADDR)];
+    bool valid = notif_port & PSIHB9_ESB_NOTIF_VALID;
+    uint64_t notify_addr = notif_port & ~PSIHB9_ESB_NOTIF_VALID;
+
+    uint32_t offset =
+        (psi->regs[PSIHB_REG(PSIHB9_IVT_OFFSET)] >> PSIHB9_IVT_OFF_SHIFT);
+    uint64_t lisn = cpu_to_be64(offset + srcno);
+
+    if (valid) {
+        cpu_physical_memory_write(notify_addr, &lisn, sizeof(lisn));
+    }
+}
+
+/*
+ * TODO : move to parent class
+ */
+static void pnv_psi_reset(DeviceState *dev)
+{
+    PnvPsi *psi = PNV_PSI(dev);
+
+    memset(psi->regs, 0x0, sizeof(psi->regs));
+
+    psi->regs[PSIHB_XSCOM_BAR] = psi->bar | PSIHB_BAR_EN;
+}
+
+static uint64_t pnv_psi_p9_mmio_read(void *opaque, hwaddr addr, unsigned size)
+{
+    PnvPsi *psi = PNV_PSI(opaque);
+    uint32_t reg = PSIHB_REG(addr);
+    uint64_t val = -1;
+
+    switch (addr) {
+    case PSIHB9_CR:
+    case PSIHB9_SEMR:
+        /* FSP stuff */
+    case PSIHB9_INTERRUPT_CONTROL:
+    case PSIHB9_ESB_CI_BASE:
+    case PSIHB9_ESB_NOTIF_ADDR:
+    case PSIHB9_IVT_OFFSET:
+        val = psi->regs[reg];
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: read at 0x%" PRIx64 "\n", addr);
+    }
+
+    return val;
+}
+
+static void pnv_psi_p9_mmio_write(void *opaque, hwaddr addr,
+                                  uint64_t val, unsigned size)
+{
+    PnvPsi *psi = PNV_PSI(opaque);
+    uint32_t reg = PSIHB_REG(addr);
+    MemoryRegion *sysmem = get_system_memory();
+
+    switch (addr) {
+    case PSIHB9_CR:
+    case PSIHB9_SEMR:
+        /* FSP stuff */
+        break;
+    case PSIHB9_INTERRUPT_CONTROL:
+        if (val & PSIHB9_IRQ_RESET) {
+            device_reset(DEVICE(&psi->source));
+        }
+        psi->regs[reg] = val;
+        break;
+
+    case PSIHB9_ESB_CI_BASE:
+        if (!(val & PSIHB9_ESB_CI_VALID)) {
+            if (psi->regs[reg] & PSIHB9_ESB_CI_VALID) {
+                memory_region_del_subregion(sysmem, &psi->source.esb_mmio);
+            }
+        } else {
+            if (!(psi->regs[reg] & PSIHB9_ESB_CI_VALID)) {
+                memory_region_add_subregion(sysmem,
+                                        val & ~PSIHB9_ESB_CI_VALID,
+                                        &psi->source.esb_mmio);
+            }
+        }
+        psi->regs[reg] = val;
+        break;
+
+    case PSIHB9_ESB_NOTIF_ADDR:
+        psi->regs[reg] = val;
+        break;
+    case PSIHB9_IVT_OFFSET:
+        psi->regs[reg] = val;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: write at 0x%" PRIx64 "\n", addr);
+    }
+}
+
+static const MemoryRegionOps pnv_psi_p9_mmio_ops = {
+    .read = pnv_psi_p9_mmio_read,
+    .write = pnv_psi_p9_mmio_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static uint64_t pnv_psi_p9_xscom_read(void *opaque, hwaddr addr, unsigned size)
+{
+    /* No read are expected */
+    qemu_log_mask(LOG_GUEST_ERROR, "PSI: xscom read at 0x%" PRIx64 "\n", addr);
+    return -1;
+}
+
+static void pnv_psi_p9_xscom_write(void *opaque, hwaddr addr,
+                                uint64_t val, unsigned size)
+{
+    PnvPsi *psi = PNV_PSI(opaque);
+
+    /* XSCOM is only used to set the PSIHB MMIO region */
+    switch (addr >> 3) {
+    case PSIHB_XSCOM_BAR:
+        pnv_psi_set_bar(psi, val);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: xscom write at 0x%" PRIx64 "\n",
+                      addr);
+    }
+}
+
+static const MemoryRegionOps pnv_psi_p9_xscom_ops = {
+    .read = pnv_psi_p9_xscom_read,
+    .write = pnv_psi_p9_xscom_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    }
+};
+
+static void pnv_psi_power9_irq_set(PnvPsi *psi, int irq, bool state)
+{
+    uint32_t irq_method = psi->regs[PSIHB_REG(PSIHB9_INTERRUPT_CONTROL)];
+
+    if (irq > PSIHB9_NUM_IRQS) {
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: Unsupported irq %d\n", irq);
+        return;
+    }
+
+    if (irq_method & PSIHB9_IRQ_METHOD) {
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: LSI IRQ method no supported\n");
+        return;
+    }
+
+    /* Update LSI levels */
+    if (state) {
+        psi->regs[PSIHB_REG(PSIHB9_IRQ_LEVEL)] |= PPC_BIT(irq);
+    } else {
+        psi->regs[PSIHB_REG(PSIHB9_IRQ_LEVEL)] &= ~PPC_BIT(irq);
+    }
+
+    qemu_set_irq(xive_source_qirq(&psi->source, irq), state);
+}
+
+static void pnv_psi_power9_instance_init(Object *obj)
+{
+    PnvPsi *psi = PNV_PSI(obj);
+
+    object_initialize(&psi->source, sizeof(psi->source), TYPE_XIVE_SOURCE);
+    object_property_add_child(obj, "source", OBJECT(&psi->source), NULL);
+}
+
+static void pnv_psi_power9_realize(DeviceState *dev, Error **errp)
+{
+    PnvPsi *psi = PNV_PSI(dev);
+    XiveSource *xsrc = &psi->source;
+    Error *local_err = NULL;
+    int i;
+
+    /* This is the only device with 4k ESB pages */
+    object_property_set_int(OBJECT(xsrc), XIVE_ESB_4K, "shift",
+                            &error_fatal);
+    object_property_set_int(OBJECT(xsrc), PSIHB9_NUM_IRQS, "nr-irqs",
+                            &error_fatal);
+    object_property_add_const_link(OBJECT(xsrc), "xive", OBJECT(psi),
+                                   &error_fatal);
+    object_property_set_bool(OBJECT(xsrc), true, "realized", &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+    qdev_set_parent_bus(DEVICE(xsrc), sysbus_get_default());
+
+    for (i = 0; i < xsrc->nr_irqs; i++) {
+        xive_source_irq_set(xsrc, i, true);
+    }
+
+    /* XSCOM region for PSI registers */
+    pnv_xscom_region_init(&psi->xscom_regs, OBJECT(dev), &pnv_psi_p9_xscom_ops,
+                psi, "xscom-psi", PNV9_XSCOM_PSIHB_SIZE);
+
+    /* Initialize MMIO region */
+    memory_region_init_io(&psi->regs_mr, OBJECT(dev), &pnv_psi_p9_mmio_ops, psi,
+                          "psihb", PNV9_PSIHB_SIZE);
+
+    /* Default BAR for MMIO region */
+    pnv_psi_set_bar(psi, psi->bar | PSIHB_BAR_EN);
+}
+
+static void pnv_psi_power9_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PnvPsiClass *ppc = PNV_PSI_CLASS(klass);
+    XiveFabricClass *xfc = XIVE_FABRIC_CLASS(klass);
+
+    dc->desc    = "PowerNV PSI Controller POWER9";
+    dc->realize = pnv_psi_power9_realize;
+
+    ppc->chip_type  = PNV_CHIP_POWER9;
+    ppc->xscom_pcba = PNV9_XSCOM_PSIHB_BASE;
+    ppc->xscom_size = PNV9_XSCOM_PSIHB_SIZE;
+    ppc->irq_set    = pnv_psi_power9_irq_set;
+
+    xfc->notify      = pnv_psi_notify;
+}
+
+static const TypeInfo pnv_psi_power9_info = {
+    .name          = TYPE_PNV_PSI_POWER9,
+    .parent        = TYPE_PNV_PSI,
+    .instance_init = pnv_psi_power9_instance_init,
+    .class_init    = pnv_psi_power9_class_init,
+    .interfaces = (InterfaceInfo[]) {
+            { TYPE_XIVE_FABRIC },
+            { },
+    },
+};
+
 static void pnv_psi_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
@@ -548,16 +880,18 @@ static void pnv_psi_class_init(ObjectClass *klass, void *data)
 
     xdc->dt_xscom = pnv_psi_dt_xscom;
 
-    dc->realize = pnv_psi_realize;
+    dc->desc = "PowerNV PSI Controller";
     dc->props = pnv_psi_properties;
+    dc->reset  = pnv_psi_reset;
 }
 
 static const TypeInfo pnv_psi_info = {
     .name          = TYPE_PNV_PSI,
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(PnvPsi),
-    .instance_init = pnv_psi_init,
     .class_init    = pnv_psi_class_init,
+    .class_size    = sizeof(PnvPsiClass),
+    .abstract      = true,
     .interfaces    = (InterfaceInfo[]) {
         { TYPE_PNV_XSCOM_INTERFACE },
         { }
@@ -567,6 +901,18 @@ static const TypeInfo pnv_psi_info = {
 static void pnv_psi_register_types(void)
 {
     type_register_static(&pnv_psi_info);
+    type_register_static(&pnv_psi_power8_info);
+    type_register_static(&pnv_psi_power9_info);
 }
 
 type_init(pnv_psi_register_types)
+
+void pnv_psi_pic_print_info(PnvPsi *psi, Monitor *mon)
+{
+    uint32_t offset =
+        (psi->regs[PSIHB_REG(PSIHB9_IVT_OFFSET)] >> PSIHB9_IVT_OFF_SHIFT);
+
+    monitor_printf(mon, "PSIHB Source %08x .. %08x\n",
+                  offset, offset + psi->source.nr_irqs - 1);
+    xive_source_pic_print_info(&psi->source, offset, mon);
+}
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
#32 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

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

The XiveRouter models the second sub-engine of the overall XIVE architecture : the Interrupt Virtualization Routing Engine (IVRE).

The IVRE handles event notifications of the IVSE through MMIO stores and performs the interrupt routing process. For this purpose, it uses a set of table stored in system memory, the first of which being the Event Assignment Structure (EAS) table.

The EAT associates an interrupt source number with an Event Notification Descriptor (END) which will be used in a second phase of the routing process to identify a Notification Virtual Target.

The XiveRouter is an abstract class which needs to be inherited from to define a storage for the EAT, and other upcoming tables. The 'chip-id' atttribute is not strictly necessary for the sPAPR and PowerNV machines but it's a good way to test the routing algorithm. Without this atttribute, the XiveRouter could be a simple QOM interface.

---
 include/hw/ppc/xive.h      | 32 ++++++++++++++
 include/hw/ppc/xive_regs.h | 31 ++++++++++++++
 hw/intc/xive.c             | 86 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 149 insertions(+)
 create mode 100644 include/hw/ppc/xive_regs.h
include/hw/ppc/xive.h +0 -0
index be93fae6317b..5a0696366577 100644

include/hw/ppc/xive.h line 13

Quoted 3 lines
include/hw/ppc/xive.h +0 -0
@@ -11,6 +11,7 @@
 #define PPC_XIVE_H
 
 #include "hw/sysbus.h"

Again, I don't think making this a SysBusDevice is quite right. Even more so for the router than the source, because at least for PAPR it might not have any MMIO presence at all.

Quoted 5 lines
Quoted 2 lines

The controller model inherits from the XiveRouter and manages the TIMA.

Um.. I'm not sure what you mean by the "controller model". Surely the presenter should own the TIMA, not the router?

The XIVE VC (routing) and PC (presente) subengines are merged under the controller model sPAPRXIVE and the PC matching algo looking for a target is reduced to one function. We don't want to model the exchanges on the PowerBUS.

The TIMA MMIO region exposing the thread interrupt context is managed by the controller sPAPRXIVE (easier for KVM) and the interrupt context registers are under model XiveTCXT. This is our XICS/ICP equivalent in XIVE.

Quoted 183 lines
Quoted 128 lines
Quoted 8 lines
Quoted 8 lines
include/hw/ppc/xive.h +1 -0
+#include "hw/ppc/xive_regs.h"
 
 /*
  * XIVE Fabric (Interface between Source and Router)
@@ -168,4 +169,35 @@ static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno,
     }
 }
 
Quoted 11 lines

include/hw/ppc/xive.h static inline void xive_source_irq_set(XiveSource *xsrc, uint32_t srcno, · line 179

Quoted 8 lines
include/hw/ppc/xive.h +8 -0
+/*
+ * XIVE Router
+ */
+
+typedef struct XiveRouter {
+    SysBusDevice    parent;
+
+    uint32_t        chip_id;

I don't think this belongs in the base class. The PowerNV specific variants will need it, but it doesn't make sense for the PAPR version.

yeah. I am using it as a END and NVT block identifier but it's not required for sPAPR, it could just be zero.

It was good to test the routing algo which should not assume that the block id is zero.

Quoted 93 lines
Quoted 93 lines
include/hw/ppc/xive.h +23 -0
+} XiveRouter;
+
+#define TYPE_XIVE_ROUTER "xive-router"
+#define XIVE_ROUTER(obj)                                \
+    OBJECT_CHECK(XiveRouter, (obj), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_CLASS(klass)                                        \
+    OBJECT_CLASS_CHECK(XiveRouterClass, (klass), TYPE_XIVE_ROUTER)
+#define XIVE_ROUTER_GET_CLASS(obj)                              \
+    OBJECT_GET_CLASS(XiveRouterClass, (obj), TYPE_XIVE_ROUTER)
+
+typedef struct XiveRouterClass {
+    SysBusDeviceClass parent;
+
+    /* XIVE table accessors */
+    int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+} XiveRouterClass;
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h new file +31 -0
new file mode 100644
index 000000000000..12499b33614c
@@ -0,0 +1,31 @@
+/*
+ * QEMU PowerPC XIVE interrupt controller model
+ *
+ * Copyright (c) 2016-2018, IBM Corporation.
+ *
+ * This code is licensed under the GPL version 2 or later. See the
+ * COPYING file in the top-level directory.
+ */
+
+#ifndef PPC_XIVE_REGS_H
+#define PPC_XIVE_REGS_H
+
+/* EAS (Event Assignment Structure)
+ *
+ * One per interrupt source. Targets an interrupt to a given Event
+ * Notification Descriptor (END) and provides the corresponding
+ * logical interrupt number (END data)
+ */
+typedef struct XiveEAS {
+        /* Use a single 64-bit definition to make it easier to
+         * perform atomic updates
+         */
+        uint64_t        w;
+#define EAS_VALID       PPC_BIT(0)
+#define EAS_END_BLOCK   PPC_BITMASK(4, 7)        /* Destination END block# */
+#define EAS_END_INDEX   PPC_BITMASK(8, 31)       /* Destination END index */
+#define EAS_MASKED      PPC_BIT(32)              /* Masked */
+#define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
+} XiveEAS;
+
+#endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +23 -0
index 014a2e41f71f..c4c90a25758e 100644
@@ -442,6 +442,91 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 
+/*
+ * XIVE Router (aka. Virtualization Controller or IVRE)
+ */
+
+int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
+{
+    XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+    return xrc->get_eas(xrtr, lisn, eas);
+}
+
+int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
+{
+    XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+    return xrc->set_eas(xrtr, lisn, eas);
+}
+
+static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
+{
+    XiveRouter *xrtr = XIVE_ROUTER(xf);
+    XiveEAS eas;
+
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 472

Quoted 5 lines
hw/intc/xive.c +5 -0
+    /* EAS cache lookup */
+    if (xive_router_get_eas(xrtr, lisn, &eas)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
+        return;
+    }

AFAICT a bad LISN here means a qemu error (in the source, probably), not a user or guest error, so an assert() would be more appropriate.

Quoted 3 lines

hmm, I would say no because in the case of PowerNV, the firmware could have badly configured the ISN offset of a source which would notify the router with a bad notification event data.

Ah, good point. That's fine as it is then.

Quoted 49 lines
Quoted 38 lines
Quoted 38 lines
hw/intc/xive.c +38 -0
+
+    /* The IVRE has a State Bit Cache for its internal sources which
+     * is also involed at this point. We skip the SBC lookup because
+     * the state bits of the sources are modeled internally in QEMU.
+     */
+
+    if (!(eas.w & EAS_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid LISN %x\n", lisn);
+        return;
+    }
+
+    if (eas.w & EAS_MASKED) {
+        /* Notification completed */
+        return;
+    }
+}
+
+static Property xive_router_properties[] = {
+    DEFINE_PROP_UINT32("chip-id", XiveRouter, chip_id, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_router_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    XiveFabricClass *xfc = XIVE_FABRIC_CLASS(klass);
+
+    dc->desc    = "XIVE Router Engine";
+    dc->props   = xive_router_properties;
+    xfc->notify = xive_router_notify;
+}
+
+static const TypeInfo xive_router_info = {
+    .name          = TYPE_XIVE_ROUTER,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .abstract      = true,
+    .class_size    = sizeof(XiveRouterClass),
+    .class_init    = xive_router_class_init,
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 512

Quoted 2 lines
hw/intc/xive.c +2 -0
+    .interfaces    = (InterfaceInfo[]) {
+        { TYPE_XIVE_FABRIC },

So as far as I can see so far, the XiveFabric interface will essentially have to be implemented on the router object, so I'm not seeing much point to having the interface rather than just a direct call on the router object. But I haven't read the whole series yet, so maybe I'm missing something.

The PSIHB and PHB4 models are using it but there are not in the series.

Quoted 7 lines
Quoted 3 lines

I can send the PSIHB patch in the next version if you like, it's the patch right after PnvXive. It's attached below for the moment. Look at pnv_psi_notify().

Hrm, I see. This seems like a really convoluted way of achieving what you need here. We want to abstract exactly how the source delivers notifies,

on sPAPR, I agree that the forwarding of event notification could be a simple XiveRouter call but the XiveRouter covers both machines :/

On PowerNV, HW uses MMIOs to forward events and only the device knows about the IRQ number offset in the global IRQ number space and the notification port to use for the MMIO store. A PowerNV XIVE source would forward the event notification to a piece of logic which sends a PowerBUS event notification message. How it reaches the XIVE IC is beyong QEMU as it would means modeling the PowerBUS.

Quoted 2 lines

but doing it with an interface on some object that's not necessarily either the source or the router seems odd.

There is no direct link between the device owing the source and the XIVE controller, they could be on the same Power chip but the routing could be done by some other chips. This scenario is covered btw.

See it as a connector object.

Quoted 1 line

At the very least the names need to change (of both interface and > property for the target object).

I am fine with renaming it. With the above explanations, if they are clear enough, how do see them ?

Thanks,

C.

Quoted 752 lines
Quoted 750 lines

Thanks,

C.

Quoted 27 lines
Quoted 27 lines
hw/intc/xive.c +18 -0
+        { }
+    }
+};
+
+void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
+{
+    if (!(eas->w & EAS_VALID)) {
+        return;
+    }
+
+    monitor_printf(mon, "  %08x %s end:%02x/%04x data:%08x\n",
+                   lisn, eas->w & EAS_MASKED ? "M" : " ",
+                   (uint8_t)  GETFIELD(EAS_END_BLOCK, eas->w),
+                   (uint32_t) GETFIELD(EAS_END_INDEX, eas->w),
+                   (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
+}
+
 /*
  * XIVE Fabric
  */
@@ -455,6 +540,7 @@ static void xive_register_types(void)
 {
     type_register_static(&xive_source_info);
     type_register_static(&xive_fabric_info);
+    type_register_static(&xive_router_info);
 }
 
Quoted 5 lines

hw/intc/xive.c static void xive_register_types(void) · line 546

Quoted 1 line
hw/intc/xive.c +0 -0
 type_init(xive_register_types)

From 680fd6ff7c99e669708fbc5cfdbfcd95e83e7c07 Mon Sep 17 00:00:00 2001

From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= <[email protected]>
Date: Wed, 21 Nov 2018 10:29:45 +0100
Subject: [PATCH] ppc/pnv: add a PSI bridge model for POWER9 processor
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The PSI bridge on POWER9 is very similar to POWER8. The BAR is still set through XSCOM but the controls are now entirely done with MMIOs. More interrupts are defined and the interrupt controller interface has changed to XIVE. The POWER9 model is a first example of the usage of the notify() handler of the XiveFabric interface, linking the PSI XiveSource to its owning device model.

---
 include/hw/ppc/pnv.h       |   6 +
 include/hw/ppc/pnv_psi.h   |  50 ++++-
 include/hw/ppc/pnv_xscom.h |   3 +
 hw/ppc/pnv.c               |  20 +-
 hw/ppc/pnv_psi.c           | 390 ++++++++++++++++++++++++++++++++++---
 5 files changed, 444 insertions(+), 25 deletions(-)
include/hw/ppc/pnv.h +6 -0
index c402e5d5844b..8be1147481f9 100644
@@ -88,6 +88,7 @@ typedef struct Pnv9Chip {
 
     /*< public >*/
     PnvXive      xive;
+    PnvPsi       psi;
 } Pnv9Chip;
 
 typedef struct PnvChipClass {
@@ -250,11 +251,16 @@ void pnv_bmc_powerdown(IPMIBmc *bmc);
 #define PNV9_XIVE_PC_SIZE            0x0000001000000000ull
 #define PNV9_XIVE_PC_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006018000000000ull)
 
+#define PNV9_PSIHB_SIZE              0x0000000000100000ull
+#define PNV9_PSIHB_BASE(chip)        PNV9_CHIP_BASE(chip, 0x0006030203000000ull)
+
 #define PNV9_XIVE_IC_SIZE            0x0000000000080000ull
 #define PNV9_XIVE_IC_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006030203100000ull)
 
 #define PNV9_XIVE_TM_SIZE            0x0000000000040000ull
 #define PNV9_XIVE_TM_BASE(chip)      PNV9_CHIP_BASE(chip, 0x0006030203180000ull)
 
+#define PNV9_PSIHB_ESB_SIZE          0x0000000000010000ull
+#define PNV9_PSIHB_ESB_BASE(chip)    PNV9_CHIP_BASE(chip, 0x00060302031c0000ull)
 
 #endif /* _PPC_PNV_H */
include/hw/ppc/pnv_psi.h +48 -2
index f6af5eae1fa8..b8f8d082bcf9 100644
@@ -21,10 +21,35 @@
 
 #include "hw/sysbus.h"
 #include "hw/ppc/xics.h"
+#include "hw/ppc/xive.h"
 
 #define TYPE_PNV_PSI "pnv-psi"
 #define PNV_PSI(obj) \
      OBJECT_CHECK(PnvPsi, (obj), TYPE_PNV_PSI)
+#define PNV_PSI_CLASS(klass) \
+     OBJECT_CLASS_CHECK(PnvPsiClass, (klass), TYPE_PNV_PSI)
+#define PNV_PSI_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(PnvPsiClass, (obj), TYPE_PNV_PSI)
+
+typedef struct PnvPsi PnvPsi;
+typedef struct PnvChip PnvChip;
+typedef struct PnvPsiClass {
+    SysBusDeviceClass parent_class;
+
+    int chip_type;
+    uint32_t xscom_pcba;
+    uint32_t xscom_size;
+
+    void (*irq_set)(PnvPsi *psi, int, bool state);
+} PnvPsiClass;
+
+#define TYPE_PNV_PSI_POWER8 TYPE_PNV_PSI "-POWER8"
+#define PNV_PSI_POWER8(obj) \
+    OBJECT_CHECK(PnvPsi, (obj), TYPE_PNV_PSI_POWER8)
+
+#define TYPE_PNV_PSI_POWER9 TYPE_PNV_PSI "-POWER9"
+#define PNV_PSI_POWER9(obj) \
+    OBJECT_CHECK(PnvPsi, (obj), TYPE_PNV_PSI_POWER9)
 
 #define PSIHB_XSCOM_MAX         0x20
 
@@ -38,9 +63,12 @@ typedef struct PnvPsi {
     /* MemoryRegion fsp_mr; */
     uint64_t fsp_bar;
 
-    /* Interrupt generation */
+    /* P8 Interrupt generation */
     ICSState ics;
 
+    /* P9 Interrupt generation */
+    XiveSource source;
+
     /* Registers */
     uint64_t regs[PSIHB_XSCOM_MAX];
 
@@ -60,6 +88,24 @@ typedef enum PnvPsiIrq {
 
 #define PSI_NUM_INTERRUPTS 6
 
-extern void pnv_psi_irq_set(PnvPsi *psi, PnvPsiIrq irq, bool state);
+/* P9 PSI Interrupts */
+#define PSIHB9_IRQ_PSI          0
+#define PSIHB9_IRQ_OCC          1
+#define PSIHB9_IRQ_FSI          2
+#define PSIHB9_IRQ_LPCHC        3
+#define PSIHB9_IRQ_LOCAL_ERR    4
+#define PSIHB9_IRQ_GLOBAL_ERR   5
+#define PSIHB9_IRQ_TPM          6
+#define PSIHB9_IRQ_LPC_SIRQ0    7
+#define PSIHB9_IRQ_LPC_SIRQ1    8
+#define PSIHB9_IRQ_LPC_SIRQ2    9
+#define PSIHB9_IRQ_LPC_SIRQ3    10
+#define PSIHB9_IRQ_SBE_I2C      11
+#define PSIHB9_IRQ_DIO          12
+#define PSIHB9_IRQ_PSU          13
+#define PSIHB9_NUM_IRQS         14
+
+void pnv_psi_irq_set(PnvPsi *psi, int irq, bool state);
+void pnv_psi_pic_print_info(PnvPsi *psi, Monitor *mon);
 
 #endif /* _PPC_PNV_PSI_H */
include/hw/ppc/pnv_xscom.h +3 -0
index 5bd43467a1ab..019b45bf9189 100644
@@ -82,6 +82,9 @@ typedef struct PnvXScomInterfaceClass {
 #define PNV_XSCOM_PBCQ_SPCI_BASE  0x9013c00
 #define PNV_XSCOM_PBCQ_SPCI_SIZE  0x5
 
+#define PNV9_XSCOM_PSIHB_BASE     0x5012900
+#define PNV9_XSCOM_PSIHB_SIZE     0x100
+
 #define PNV9_XSCOM_XIVE_BASE      0x5013000
 #define PNV9_XSCOM_XIVE_SIZE      0x300
 
hw/ppc/pnv.c +19 -1
index b6af896c30e4..e67c9d7d3995 100644
@@ -743,7 +743,7 @@ static void pnv_chip_power8_instance_init(Object *obj)
     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(obj);
     int i;
 
-    object_initialize(&chip8->psi, sizeof(chip8->psi), TYPE_PNV_PSI);
+    object_initialize(&chip8->psi, sizeof(chip8->psi), TYPE_PNV_PSI_POWER8);
     object_property_add_child(obj, "psi", OBJECT(&chip8->psi), NULL);
     object_property_add_const_link(OBJECT(&chip8->psi), "xics",
                                    OBJECT(qdev_get_machine()), &error_abort);
@@ -923,6 +923,11 @@ static void pnv_chip_power9_instance_init(Object *obj)
     object_property_add_child(obj, "xive", OBJECT(&chip9->xive), NULL);
     object_property_add_const_link(OBJECT(&chip9->xive), "chip", obj,
                                    &error_abort);
+
+    object_initialize(&chip9->psi, sizeof(chip9->psi), TYPE_PNV_PSI_POWER9);
+    object_property_add_child(obj, "psi", OBJECT(&chip9->psi), NULL);
+    object_property_add_const_link(OBJECT(&chip9->psi), "chip", obj,
+                                   &error_abort);
 }
 
 static void pnv_chip_power9_realize(DeviceState *dev, Error **errp)
@@ -955,6 +960,18 @@ static void pnv_chip_power9_realize(DeviceState *dev, Error **errp)
     qdev_set_parent_bus(DEVICE(&chip9->xive), sysbus_get_default());
     pnv_xscom_add_subregion(chip, PNV9_XSCOM_XIVE_BASE,
                             &chip9->xive.xscom_regs);
+
+    /* Processor Service Interface (PSI) Host Bridge */
+    object_property_set_int(OBJECT(&chip9->psi), PNV9_PSIHB_BASE(chip),
+                            "bar", &error_fatal);
+    object_property_set_bool(OBJECT(&chip9->psi), true, "realized", &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+    qdev_set_parent_bus(DEVICE(&chip9->psi), sysbus_get_default());
+    pnv_xscom_add_subregion(chip, PNV9_XSCOM_PSIHB_BASE,
+                            &chip9->psi.xscom_regs);
 }
 
 static void pnv_chip_power9_class_init(ObjectClass *klass, void *data)
@@ -1188,6 +1205,7 @@ static void pnv_pic_print_info(InterruptStatsProvider *obj,
             Pnv9Chip *chip9 = PNV9_CHIP(chip);
 
              pnv_xive_pic_print_info(&chip9->xive, mon);
+             pnv_psi_pic_print_info(&chip9->psi, mon);
         } else {
             Pnv8Chip *chip8 = PNV8_CHIP(chip);
 
hw/ppc/pnv_psi.c +368 -22
index 5b969127c303..8b85dd9555e8 100644
@@ -22,6 +22,7 @@
 #include "target/ppc/cpu.h"
 #include "qemu/log.h"
 #include "qapi/error.h"
+#include "monitor/monitor.h"
 
 #include "exec/address-spaces.h"
 
@@ -114,12 +115,14 @@
 #define PSIHB_BAR_MASK                  0x0003fffffff00000ull
 #define PSIHB_FSPBAR_MASK               0x0003ffff00000000ull
 
+#define PSIHB_REG(addr) (((addr) >> 3) + PSIHB_XSCOM_BAR)
+
 static void pnv_psi_set_bar(PnvPsi *psi, uint64_t bar)
 {
     MemoryRegion *sysmem = get_system_memory();
     uint64_t old = psi->regs[PSIHB_XSCOM_BAR];
 
-    psi->regs[PSIHB_XSCOM_BAR] = bar & (PSIHB_BAR_MASK | PSIHB_BAR_EN);
+    psi->regs[PSIHB_XSCOM_BAR] = bar;
 
     /* Update MR, always remove it first */
     if (old & PSIHB_BAR_EN) {
@@ -128,7 +131,7 @@ static void pnv_psi_set_bar(PnvPsi *psi, uint64_t bar)
 
     /* Then add it back if needed */
     if (bar & PSIHB_BAR_EN) {
-        uint64_t addr = bar & PSIHB_BAR_MASK;
+        uint64_t addr = bar & ~PSIHB_BAR_EN;
         memory_region_add_subregion(sysmem, addr, &psi->regs_mr);
     }
 }
@@ -205,7 +208,12 @@ static const uint64_t stat_bits[] = {
     [PSIHB_IRQ_EXTERNAL]  = PSIHB_IRQ_STAT_EXT,
 };
 
-void pnv_psi_irq_set(PnvPsi *psi, PnvPsiIrq irq, bool state)
+void pnv_psi_irq_set(PnvPsi *psi, int irq, bool state)
+{
+    PNV_PSI_GET_CLASS(psi)->irq_set(psi, irq, state);
+}
+
+static void pnv_psi_power8_irq_set(PnvPsi *psi, int irq, bool state)
 {
     ICSState *ics = &psi->ics;
     uint32_t xivr_reg;
@@ -324,7 +332,7 @@ static uint64_t pnv_psi_reg_read(PnvPsi *psi, uint32_t offset, bool mmio)
         val = psi->regs[offset];
         break;
     default:
-        qemu_log_mask(LOG_UNIMP, "PSI: read at Ox%" PRIx32 "\n", offset);
+        qemu_log_mask(LOG_UNIMP, "PSI: read at 0x%" PRIx32 "\n", offset);
     }
     return val;
 }
@@ -383,7 +391,7 @@ static void pnv_psi_reg_write(PnvPsi *psi, uint32_t offset, uint64_t val,
         pnv_psi_set_irsn(psi, val);
         break;
     default:
-        qemu_log_mask(LOG_UNIMP, "PSI: write at Ox%" PRIx32 "\n", offset);
+        qemu_log_mask(LOG_UNIMP, "PSI: write at 0x%" PRIx32 "\n", offset);
     }
 }
 
@@ -393,13 +401,13 @@ static void pnv_psi_reg_write(PnvPsi *psi, uint32_t offset, uint64_t val,
  */
 static uint64_t pnv_psi_mmio_read(void *opaque, hwaddr addr, unsigned size)
 {
-    return pnv_psi_reg_read(opaque, (addr >> 3) + PSIHB_XSCOM_BAR, true);
+    return pnv_psi_reg_read(opaque, PSIHB_REG(addr), true);
 }
 
 static void pnv_psi_mmio_write(void *opaque, hwaddr addr,
                               uint64_t val, unsigned size)
 {
-    pnv_psi_reg_write(opaque, (addr >> 3) + PSIHB_XSCOM_BAR, val, true);
+    pnv_psi_reg_write(opaque, PSIHB_REG(addr), val, true);
 }
 
 static const MemoryRegionOps psi_mmio_ops = {
@@ -441,7 +449,7 @@ static const MemoryRegionOps pnv_psi_xscom_ops = {
     }
 };
 
-static void pnv_psi_init(Object *obj)
+static void pnv_psi_power8_instance_init(Object *obj)
 {
     PnvPsi *psi = PNV_PSI(obj);
 
@@ -458,7 +466,7 @@ static const uint8_t irq_to_xivr[] = {
     PSIHB_XSCOM_XIVR_EXT,
 };
 
-static void pnv_psi_realize(DeviceState *dev, Error **errp)
+static void pnv_psi_power8_realize(DeviceState *dev, Error **errp)
 {
     PnvPsi *psi = PNV_PSI(dev);
     ICSState *ics = &psi->ics;
@@ -510,28 +518,34 @@ static void pnv_psi_realize(DeviceState *dev, Error **errp)
     }
 }
 
+static const char compat_p8[] = "ibm,power8-psihb-x\0ibm,psihb-x";
+static const char compat_p9[] = "ibm,power9-psihb-x\0ibm,psihb-x";
+
 static int pnv_psi_dt_xscom(PnvXScomInterface *dev, void *fdt, int xscom_offset)
 {
-    const char compat[] = "ibm,power8-psihb-x\0ibm,psihb-x";
+    PnvPsiClass *ppc = PNV_PSI_GET_CLASS(dev);
     char *name;
     int offset;
-    uint32_t lpc_pcba = PNV_XSCOM_PSIHB_BASE;
     uint32_t reg[] = {
-        cpu_to_be32(lpc_pcba),
-        cpu_to_be32(PNV_XSCOM_PSIHB_SIZE)
+        cpu_to_be32(ppc->xscom_pcba),
+        cpu_to_be32(ppc->xscom_size)
     };
 
-    name = g_strdup_printf("psihb@%x", lpc_pcba);
+    name = g_strdup_printf("psihb@%x", ppc->xscom_pcba);
     offset = fdt_add_subnode(fdt, xscom_offset, name);
     _FDT(offset);
     g_free(name);
 
-    _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg))));
-
-    _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 2)));
-    _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 1)));
-    _FDT((fdt_setprop(fdt, offset, "compatible", compat,
-                      sizeof(compat))));
+    _FDT(fdt_setprop(fdt, offset, "reg", reg, sizeof(reg)));
+    _FDT(fdt_setprop_cell(fdt, offset, "#address-cells", 2));
+    _FDT(fdt_setprop_cell(fdt, offset, "#size-cells", 1));
+    if (ppc->chip_type == PNV_CHIP_POWER9) {
+        _FDT(fdt_setprop(fdt, offset, "compatible", compat_p9,
+                         sizeof(compat_p9)));
+    } else {
+        _FDT(fdt_setprop(fdt, offset, "compatible", compat_p8,
+                         sizeof(compat_p8)));
+    }
     return 0;
 }
 
@@ -541,6 +555,324 @@ static Property pnv_psi_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static void pnv_psi_power8_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PnvPsiClass *ppc = PNV_PSI_CLASS(klass);
+
+    dc->desc    = "PowerNV PSI Controller POWER8";
+    dc->realize = pnv_psi_power8_realize;
+
+    ppc->chip_type =  PNV_CHIP_POWER8;
+    ppc->xscom_pcba = PNV_XSCOM_PSIHB_BASE;
+    ppc->xscom_size = PNV_XSCOM_PSIHB_SIZE;
+    ppc->irq_set    = pnv_psi_power8_irq_set;
+}
+
+static const TypeInfo pnv_psi_power8_info = {
+    .name          = TYPE_PNV_PSI_POWER8,
+    .parent        = TYPE_PNV_PSI,
+    .instance_init = pnv_psi_power8_instance_init,
+    .class_init    = pnv_psi_power8_class_init,
+};
+
+/* Common registers */
+
+#define PSIHB9_CR                       0x20
+#define PSIHB9_SEMR                     0x28
+
+/* P9 registers */
+
+#define PSIHB9_INTERRUPT_CONTROL        0x58
+#define   PSIHB9_IRQ_METHOD             PPC_BIT(0)
+#define   PSIHB9_IRQ_RESET              PPC_BIT(1)
+#define PSIHB9_ESB_CI_BASE              0x60
+#define   PSIHB9_ESB_CI_VALID           1
+#define PSIHB9_ESB_NOTIF_ADDR           0x68
+#define   PSIHB9_ESB_NOTIF_VALID        1
+#define PSIHB9_IVT_OFFSET               0x70
+#define   PSIHB9_IVT_OFF_SHIFT          32
+
+#define PSIHB9_IRQ_LEVEL                0x78 /* assertion */
+#define   PSIHB9_IRQ_LEVEL_PSI          PPC_BIT(0)
+#define   PSIHB9_IRQ_LEVEL_OCC          PPC_BIT(1)
+#define   PSIHB9_IRQ_LEVEL_FSI          PPC_BIT(2)
+#define   PSIHB9_IRQ_LEVEL_LPCHC        PPC_BIT(3)
+#define   PSIHB9_IRQ_LEVEL_LOCAL_ERR    PPC_BIT(4)
+#define   PSIHB9_IRQ_LEVEL_GLOBAL_ERR   PPC_BIT(5)
+#define   PSIHB9_IRQ_LEVEL_TPM          PPC_BIT(6)
+#define   PSIHB9_IRQ_LEVEL_LPC_SIRQ1    PPC_BIT(7)
+#define   PSIHB9_IRQ_LEVEL_LPC_SIRQ2    PPC_BIT(8)
+#define   PSIHB9_IRQ_LEVEL_LPC_SIRQ3    PPC_BIT(9)
+#define   PSIHB9_IRQ_LEVEL_LPC_SIRQ4    PPC_BIT(10)
+#define   PSIHB9_IRQ_LEVEL_SBE_I2C      PPC_BIT(11)
+#define   PSIHB9_IRQ_LEVEL_DIO          PPC_BIT(12)
+#define   PSIHB9_IRQ_LEVEL_PSU          PPC_BIT(13)
+#define   PSIHB9_IRQ_LEVEL_I2C_C        PPC_BIT(14)
+#define   PSIHB9_IRQ_LEVEL_I2C_D        PPC_BIT(15)
+#define   PSIHB9_IRQ_LEVEL_I2C_E        PPC_BIT(16)
+#define   PSIHB9_IRQ_LEVEL_SBE          PPC_BIT(19)
+
+#define PSIHB9_IRQ_STAT                 0x80 /* P bit */
+#define   PSIHB9_IRQ_STAT_PSI           PPC_BIT(0)
+#define   PSIHB9_IRQ_STAT_OCC           PPC_BIT(1)
+#define   PSIHB9_IRQ_STAT_FSI           PPC_BIT(2)
+#define   PSIHB9_IRQ_STAT_LPCHC         PPC_BIT(3)
+#define   PSIHB9_IRQ_STAT_LOCAL_ERR     PPC_BIT(4)
+#define   PSIHB9_IRQ_STAT_GLOBAL_ERR    PPC_BIT(5)
+#define   PSIHB9_IRQ_STAT_TPM           PPC_BIT(6)
+#define   PSIHB9_IRQ_STAT_LPC_SIRQ1     PPC_BIT(7)
+#define   PSIHB9_IRQ_STAT_LPC_SIRQ2     PPC_BIT(8)
+#define   PSIHB9_IRQ_STAT_LPC_SIRQ3     PPC_BIT(9)
+#define   PSIHB9_IRQ_STAT_LPC_SIRQ4     PPC_BIT(10)
+#define   PSIHB9_IRQ_STAT_SBE_I2C       PPC_BIT(11)
+#define   PSIHB9_IRQ_STAT_DIO           PPC_BIT(12)
+#define   PSIHB9_IRQ_STAT_PSU           PPC_BIT(13)
+
+static void pnv_psi_notify(XiveFabric *xf, uint32_t srcno)
+{
+    PnvPsi *psi = PNV_PSI(xf);
+    uint64_t notif_port = psi->regs[PSIHB_REG(PSIHB9_ESB_NOTIF_ADDR)];
+    bool valid = notif_port & PSIHB9_ESB_NOTIF_VALID;
+    uint64_t notify_addr = notif_port & ~PSIHB9_ESB_NOTIF_VALID;
+
+    uint32_t offset =
+        (psi->regs[PSIHB_REG(PSIHB9_IVT_OFFSET)] >> PSIHB9_IVT_OFF_SHIFT);
+    uint64_t lisn = cpu_to_be64(offset + srcno);
+
+    if (valid) {
+        cpu_physical_memory_write(notify_addr, &lisn, sizeof(lisn));
+    }
+}
+
+/*
+ * TODO : move to parent class
+ */
+static void pnv_psi_reset(DeviceState *dev)
+{
+    PnvPsi *psi = PNV_PSI(dev);
+
+    memset(psi->regs, 0x0, sizeof(psi->regs));
+
+    psi->regs[PSIHB_XSCOM_BAR] = psi->bar | PSIHB_BAR_EN;
+}
+
+static uint64_t pnv_psi_p9_mmio_read(void *opaque, hwaddr addr, unsigned size)
+{
+    PnvPsi *psi = PNV_PSI(opaque);
+    uint32_t reg = PSIHB_REG(addr);
+    uint64_t val = -1;
+
+    switch (addr) {
+    case PSIHB9_CR:
+    case PSIHB9_SEMR:
+        /* FSP stuff */
+    case PSIHB9_INTERRUPT_CONTROL:
+    case PSIHB9_ESB_CI_BASE:
+    case PSIHB9_ESB_NOTIF_ADDR:
+    case PSIHB9_IVT_OFFSET:
+        val = psi->regs[reg];
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: read at 0x%" PRIx64 "\n", addr);
+    }
+
+    return val;
+}
+
+static void pnv_psi_p9_mmio_write(void *opaque, hwaddr addr,
+                                  uint64_t val, unsigned size)
+{
+    PnvPsi *psi = PNV_PSI(opaque);
+    uint32_t reg = PSIHB_REG(addr);
+    MemoryRegion *sysmem = get_system_memory();
+
+    switch (addr) {
+    case PSIHB9_CR:
+    case PSIHB9_SEMR:
+        /* FSP stuff */
+        break;
+    case PSIHB9_INTERRUPT_CONTROL:
+        if (val & PSIHB9_IRQ_RESET) {
+            device_reset(DEVICE(&psi->source));
+        }
+        psi->regs[reg] = val;
+        break;
+
+    case PSIHB9_ESB_CI_BASE:
+        if (!(val & PSIHB9_ESB_CI_VALID)) {
+            if (psi->regs[reg] & PSIHB9_ESB_CI_VALID) {
+                memory_region_del_subregion(sysmem, &psi->source.esb_mmio);
+            }
+        } else {
+            if (!(psi->regs[reg] & PSIHB9_ESB_CI_VALID)) {
+                memory_region_add_subregion(sysmem,
+                                        val & ~PSIHB9_ESB_CI_VALID,
+                                        &psi->source.esb_mmio);
+            }
+        }
+        psi->regs[reg] = val;
+        break;
+
+    case PSIHB9_ESB_NOTIF_ADDR:
+        psi->regs[reg] = val;
+        break;
+    case PSIHB9_IVT_OFFSET:
+        psi->regs[reg] = val;
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: write at 0x%" PRIx64 "\n", addr);
+    }
+}
+
+static const MemoryRegionOps pnv_psi_p9_mmio_ops = {
+    .read = pnv_psi_p9_mmio_read,
+    .write = pnv_psi_p9_mmio_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static uint64_t pnv_psi_p9_xscom_read(void *opaque, hwaddr addr, unsigned size)
+{
+    /* No read are expected */
+    qemu_log_mask(LOG_GUEST_ERROR, "PSI: xscom read at 0x%" PRIx64 "\n", addr);
+    return -1;
+}
+
+static void pnv_psi_p9_xscom_write(void *opaque, hwaddr addr,
+                                uint64_t val, unsigned size)
+{
+    PnvPsi *psi = PNV_PSI(opaque);
+
+    /* XSCOM is only used to set the PSIHB MMIO region */
+    switch (addr >> 3) {
+    case PSIHB_XSCOM_BAR:
+        pnv_psi_set_bar(psi, val);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: xscom write at 0x%" PRIx64 "\n",
+                      addr);
+    }
+}
+
+static const MemoryRegionOps pnv_psi_p9_xscom_ops = {
+    .read = pnv_psi_p9_xscom_read,
+    .write = pnv_psi_p9_xscom_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    }
+};
+
+static void pnv_psi_power9_irq_set(PnvPsi *psi, int irq, bool state)
+{
+    uint32_t irq_method = psi->regs[PSIHB_REG(PSIHB9_INTERRUPT_CONTROL)];
+
+    if (irq > PSIHB9_NUM_IRQS) {
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: Unsupported irq %d\n", irq);
+        return;
+    }
+
+    if (irq_method & PSIHB9_IRQ_METHOD) {
+        qemu_log_mask(LOG_GUEST_ERROR, "PSI: LSI IRQ method no supported\n");
+        return;
+    }
+
+    /* Update LSI levels */
+    if (state) {
+        psi->regs[PSIHB_REG(PSIHB9_IRQ_LEVEL)] |= PPC_BIT(irq);
+    } else {
+        psi->regs[PSIHB_REG(PSIHB9_IRQ_LEVEL)] &= ~PPC_BIT(irq);
+    }
+
+    qemu_set_irq(xive_source_qirq(&psi->source, irq), state);
+}
+
+static void pnv_psi_power9_instance_init(Object *obj)
+{
+    PnvPsi *psi = PNV_PSI(obj);
+
+    object_initialize(&psi->source, sizeof(psi->source), TYPE_XIVE_SOURCE);
+    object_property_add_child(obj, "source", OBJECT(&psi->source), NULL);
+}
+
+static void pnv_psi_power9_realize(DeviceState *dev, Error **errp)
+{
+    PnvPsi *psi = PNV_PSI(dev);
+    XiveSource *xsrc = &psi->source;
+    Error *local_err = NULL;
+    int i;
+
+    /* This is the only device with 4k ESB pages */
+    object_property_set_int(OBJECT(xsrc), XIVE_ESB_4K, "shift",
+                            &error_fatal);
+    object_property_set_int(OBJECT(xsrc), PSIHB9_NUM_IRQS, "nr-irqs",
+                            &error_fatal);
+    object_property_add_const_link(OBJECT(xsrc), "xive", OBJECT(psi),
+                                   &error_fatal);
+    object_property_set_bool(OBJECT(xsrc), true, "realized", &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+    qdev_set_parent_bus(DEVICE(xsrc), sysbus_get_default());
+
+    for (i = 0; i < xsrc->nr_irqs; i++) {
+        xive_source_irq_set(xsrc, i, true);
+    }
+
+    /* XSCOM region for PSI registers */
+    pnv_xscom_region_init(&psi->xscom_regs, OBJECT(dev), &pnv_psi_p9_xscom_ops,
+                psi, "xscom-psi", PNV9_XSCOM_PSIHB_SIZE);
+
+    /* Initialize MMIO region */
+    memory_region_init_io(&psi->regs_mr, OBJECT(dev), &pnv_psi_p9_mmio_ops, psi,
+                          "psihb", PNV9_PSIHB_SIZE);
+
+    /* Default BAR for MMIO region */
+    pnv_psi_set_bar(psi, psi->bar | PSIHB_BAR_EN);
+}
+
+static void pnv_psi_power9_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    PnvPsiClass *ppc = PNV_PSI_CLASS(klass);
+    XiveFabricClass *xfc = XIVE_FABRIC_CLASS(klass);
+
+    dc->desc    = "PowerNV PSI Controller POWER9";
+    dc->realize = pnv_psi_power9_realize;
+
+    ppc->chip_type  = PNV_CHIP_POWER9;
+    ppc->xscom_pcba = PNV9_XSCOM_PSIHB_BASE;
+    ppc->xscom_size = PNV9_XSCOM_PSIHB_SIZE;
+    ppc->irq_set    = pnv_psi_power9_irq_set;
+
+    xfc->notify      = pnv_psi_notify;
+}
+
+static const TypeInfo pnv_psi_power9_info = {
+    .name          = TYPE_PNV_PSI_POWER9,
+    .parent        = TYPE_PNV_PSI,
+    .instance_init = pnv_psi_power9_instance_init,
+    .class_init    = pnv_psi_power9_class_init,
+    .interfaces = (InterfaceInfo[]) {
+            { TYPE_XIVE_FABRIC },
+            { },
+    },
+};
+
 static void pnv_psi_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
@@ -548,16 +880,18 @@ static void pnv_psi_class_init(ObjectClass *klass, void *data)
 
     xdc->dt_xscom = pnv_psi_dt_xscom;
 
-    dc->realize = pnv_psi_realize;
+    dc->desc = "PowerNV PSI Controller";
     dc->props = pnv_psi_properties;
+    dc->reset  = pnv_psi_reset;
 }
 
 static const TypeInfo pnv_psi_info = {
     .name          = TYPE_PNV_PSI,
     .parent        = TYPE_SYS_BUS_DEVICE,
     .instance_size = sizeof(PnvPsi),
-    .instance_init = pnv_psi_init,
     .class_init    = pnv_psi_class_init,
+    .class_size    = sizeof(PnvPsiClass),
+    .abstract      = true,
     .interfaces    = (InterfaceInfo[]) {
         { TYPE_PNV_XSCOM_INTERFACE },
         { }
@@ -567,6 +901,18 @@ static const TypeInfo pnv_psi_info = {
 static void pnv_psi_register_types(void)
 {
     type_register_static(&pnv_psi_info);
+    type_register_static(&pnv_psi_power8_info);
+    type_register_static(&pnv_psi_power9_info);
 }
 
 type_init(pnv_psi_register_types)
+
+void pnv_psi_pic_print_info(PnvPsi *psi, Monitor *mon)
+{
+    uint32_t offset =
+        (psi->regs[PSIHB_REG(PSIHB9_IVT_OFFSET)] >> PSIHB9_IVT_OFF_SHIFT);
+
+    monitor_printf(mon, "PSIHB Source %08x .. %08x\n",
+                  offset, offset + psi->source.nr_irqs - 1);
+    xive_source_pic_print_info(&psi->source, offset, mon);
+}
#33 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 5 lines · Cédric Le Goater
Quoted 3 lines · David Gibson
Quoted 2 lines · Cédric Le Goater
Quoted 1 line · David Gibson

On Fri, Nov 16, 2018 at 11:56:57AM +0100, Cédric Le Goater

wrote:

[snip]

Quoted 32 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

So as far as I can see so far, the XiveFabric interface will essentially have to be implemented on the router object, so I'm not seeing much point to having the interface rather than just a direct call on the router object. But I haven't read the whole series yet, so maybe I'm missing something.

The PSIHB and PHB4 models are using it but there are not in the series.

Quoted 7 lines
Quoted 3 lines

I can send the PSIHB patch in the next version if you like, it's the patch right after PnvXive. It's attached below for the moment. Look at pnv_psi_notify().

Hrm, I see. This seems like a really convoluted way of achieving what you need here. We want to abstract exactly how the source delivers notifies,

on sPAPR, I agree that the forwarding of event notification could be a simple XiveRouter call but the XiveRouter covers both machines :/

On PowerNV, HW uses MMIOs to forward events and only the device knows about the IRQ number offset in the global IRQ number space and the notification port to use for the MMIO store. A PowerNV XIVE source would forward the event notification to a piece of logic which sends a PowerBUS event notification message. How it reaches the XIVE IC is beyong QEMU as it would means modeling the PowerBUS.

Quoted 2 lines

but doing it with an interface on some object that's not necessarily either the source or the router seems odd.

There is no direct link between the device owing the source and the XIVE controller, they could be on the same Power chip but the routing could be done by some other chips. This scenario is covered btw.

Quoted 6 lines

See it as a connector object.

Quoted 1 line

At the very least the names need to change (of both interface and > property for the target object).

I am fine with renaming it. With the above explanations, if they are clear enough, how do see them ?

TBH, I didn't find the info above particularly illuminating. However, I think perusing the code has finally gotten my head around the model (sorry it's taken so long). I think two things were confusing me.

1) The name had be thinking in terms of the XicsFabric, but the function here is totally different.

2) I was thinking of the XiveSource as handling all source-side irq related logic, but I guess it's real function is a bit more limited. As I now understand it, it's only really handling the ESB and immediately surrounding logic - the "owning" device (e.g. PHB or PSI) is responsible for the connection "up the stack" as it were.

So, I'm ok with the model. Just to verify that my understanding is correct, can you confirm my reasoning below:

* For PowerNV, we'd generally expect the notify function to be implemented by the "owning" device. For the XIVE internal source, that would be the XiveRouter itself, immediately triggering the right EAS. For the PHB and PSI irq sources, that will code in the PHB/PSI which performs the MMIO to poke a router.

* For PAPR, for simplicity, we'd expect to wire all sources direct to a single system-wide router object.

I definitely think it needs a name change though. "XiveNotify" perhaps? And the property to configure it on the XiveSource, maybe "notify" or "notify_via".

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
#34 lore raw

Re: [Qemu-devel] [PATCH v5 04/36] ppc/xive: introduce the XiveRouter model

Quoted 39 lines · David Gibson
Quoted 5 lines · Cédric Le Goater
Quoted 3 lines · David Gibson
Quoted 2 lines · Cédric Le Goater
Quoted 1 line · David Gibson

On Fri, Nov 16, 2018 at 11:56:57AM +0100, Cédric Le Goater

wrote:

[snip]

Quoted 32 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

So as far as I can see so far, the XiveFabric interface will essentially have to be implemented on the router object, so I'm not seeing much point to having the interface rather than just a direct call on the router object. But I haven't read the whole series yet, so maybe I'm missing something.

The PSIHB and PHB4 models are using it but there are not in the series.

Quoted 7 lines
Quoted 3 lines

I can send the PSIHB patch in the next version if you like, it's the patch right after PnvXive. It's attached below for the moment. Look at pnv_psi_notify().

Hrm, I see. This seems like a really convoluted way of achieving what you need here. We want to abstract exactly how the source delivers notifies,

on sPAPR, I agree that the forwarding of event notification could be a simple XiveRouter call but the XiveRouter covers both machines :/

On PowerNV, HW uses MMIOs to forward events and only the device knows about the IRQ number offset in the global IRQ number space and the notification port to use for the MMIO store. A PowerNV XIVE source would forward the event notification to a piece of logic which sends a PowerBUS event notification message. How it reaches the XIVE IC is beyong QEMU as it would means modeling the PowerBUS.

Quoted 2 lines

but doing it with an interface on some object that's not necessarily either the source or the router seems odd.

There is no direct link between the device owing the source and the XIVE controller, they could be on the same Power chip but the routing could be done by some other chips. This scenario is covered btw.

Quoted 8 lines
Quoted 6 lines

See it as a connector object.

Quoted 1 line

At the very least the names need to change (of both interface and > property for the target object).

I am fine with renaming it. With the above explanations, if they are clear enough, how do see them ?

TBH, I didn't find the info above particularly illuminating.

This is really a PowerNV need. So, I can reshuffle the code and make a direct link between the XiveSource and the XiveRouter models for sPAPR. It's a small change. and reintroduce XiveFabric (or whatever name we choose) later before the PowerNV Xive and PSIHB model.

Quoted 6 lines

However,
I think perusing the code has finally gotten my head around the model (sorry it's taken so long). I think two things were confusing me.

1) The name had be thinking in terms of the XicsFabric, but the function here is totally different.

Yes. I agree. It's not the same thing at all.

Quoted 5 lines

2) I was thinking of the XiveSource as handling all source-side irq related logic, but I guess it's real function is a bit more limited. As I now understand it, it's only really handling the ESB and immediately surrounding logic - the "owning" device (e.g. PHB or PSI) is responsible for the connection "up the stack" as it were.

yes.

Quoted 8 lines

So, I'm ok with the model. Just to verify that my understanding is correct, can you confirm my reasoning below:

* For PowerNV, we'd generally expect the notify function to be implemented by the "owning" device. For the XIVE internal source, that would be the XiveRouter itself, immediately triggering the right EAS. For the PHB and PSI irq sources, that will code in the PHB/PSI which performs the MMIO to poke a router.

exactly.

Quoted 2 lines

* For PAPR, for simplicity, we'd expect to wire all sources direct to a single system-wide router object.

yes.

Quoted 3 lines

I definitely think it needs a name change though. "XiveNotify" perhaps?

Yes. XiveNotifier may be ? to use a noun and not a verb.

Quoted 2 lines

And the property to configure it on the XiveSource, maybe "notify" or "notify_via".

What the XIVE engines are doing is forwarding a trigger event to the next engine that can possibly do the routing to the final target.

In the specs, the verbs 'trigger', 'forward', 'notify', 'route' are commonly used. I think 'notify' is the most frequent.

ok for 'notify'.

Thanks,

C.

#35 lore raw

PATCH v5 5/36 ppc/xive: introduce the XIVE Event Notification Descriptors

In reply to Cédric Le Goater

To complete the event routing, the IVRE sub-engine uses an internal table containing Event Notification Descriptor (END) structures.

An END specifies on which Event Queue (EQ) the event notification data, defined in the associated EAS, should be posted when an exception occurs. It also defines which Notification Virtual Target (NVT) should be notified.

The Event Queue is a memory page provided by the O/S defining a circular buffer, one per server and priority couple, containing Event Queue entries. These are 4 bytes long, the first bit being a 'generation' bit and the 31 following bits the END Data field. They are pulled by the O/S when the exception occurs.

The END Data field is a way to set an invariant logical event source number for an IRQ. It is set with the H_INT_SET_SOURCE_CONFIG hcall when the EISN flag is used.

---
 include/hw/ppc/xive.h      |  18 ++++
 include/hw/ppc/xive_regs.h |  48 ++++++++++
 hw/intc/xive.c             | 185 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 248 insertions(+), 3 deletions(-)
include/hw/ppc/xive.h +18 -0
index 5a0696366577..ce62aaf28343 100644
@@ -193,11 +193,29 @@ typedef struct XiveRouterClass {
     /* XIVE table accessors */
     int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
     int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*get_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);
+    int (*set_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);
 } XiveRouterClass;
 
 void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
 
 int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
 int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+
+/*
+ * For legacy compatibility, the exceptions define up to 256 different
+ * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
+ * and the least favored level 0xFF.
+ */
+#define XIVE_PRIORITY_MAX  7
+
+void xive_end_reset(XiveEND *end);
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +48 -0

52 lines not on this page · raw message

hw/intc/xive.c +182 -3

209 lines not on this page · raw message

Signature · 1 line
2.17.2
#36 lore raw

Re: [Qemu-devel] [PATCH v5 05/36] ppc/xive: introduce the XIVE Event Notification Descriptors

Quoted 30 lines · Cédric Le Goater

To complete the event routing, the IVRE sub-engine uses an internal table containing Event Notification Descriptor (END) structures.

An END specifies on which Event Queue (EQ) the event notification data, defined in the associated EAS, should be posted when an exception occurs. It also defines which Notification Virtual Target (NVT) should be notified.

The Event Queue is a memory page provided by the O/S defining a circular buffer, one per server and priority couple, containing Event Queue entries. These are 4 bytes long, the first bit being a 'generation' bit and the 31 following bits the END Data field. They are pulled by the O/S when the exception occurs.

The END Data field is a way to set an invariant logical event source number for an IRQ. It is set with the H_INT_SET_SOURCE_CONFIG hcall when the EISN flag is used.

---
 include/hw/ppc/xive.h      |  18 ++++
 include/hw/ppc/xive_regs.h |  48 ++++++++++
 hw/intc/xive.c             | 185 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 248 insertions(+), 3 deletions(-)
include/hw/ppc/xive.h +0 -0
index 5a0696366577..ce62aaf28343 100644

include/hw/ppc/xive.h typedef struct XiveRouterClass { · line 199

Quoted 7 lines
include/hw/ppc/xive.h +4 -0
@@ -193,11 +193,29 @@ typedef struct XiveRouterClass {
     /* XIVE table accessors */
     int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
     int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*get_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);
+    int (*set_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);

Hrm. So unlike the EAS, which is basically just a word, the END is a pretty large structure. It's unclear here if get/set are expected to copy the whole thing out and in, or if get get give you a pointer into a "live" structure and set just does any necessary barriers after an update.

Really, for a non-atomic value like this, I'm not sure get/set is the right model.

Also as I understand it nearly all the indices in XIVE are broken into block/index. Is there a reason those are folded together into lisn for the EAS, but not for the END?

Quoted 87 lines
include/hw/ppc/xive.h +14 -0
 } XiveRouterClass;
 
 void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
 
 int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
 int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+
+/*
+ * For legacy compatibility, the exceptions define up to 256 different
+ * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
+ * and the least favored level 0xFF.
+ */
+#define XIVE_PRIORITY_MAX  7
+
+void xive_end_reset(XiveEND *end);
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +48 -0
index 12499b33614c..f97fb2b90bee 100644
@@ -28,4 +28,52 @@ typedef struct XiveEAS {
 #define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
 } XiveEAS;
 
+/* Event Notification Descriptor (END) */
+typedef struct XiveEND {
+        uint32_t        w0;
+#define END_W0_VALID             PPC_BIT32(0) /* "v" bit */
+#define END_W0_ENQUEUE           PPC_BIT32(1) /* "q" bit */
+#define END_W0_UCOND_NOTIFY      PPC_BIT32(2) /* "n" bit */
+#define END_W0_BACKLOG           PPC_BIT32(3) /* "b" bit */
+#define END_W0_PRECL_ESC_CTL     PPC_BIT32(4) /* "p" bit */
+#define END_W0_ESCALATE_CTL      PPC_BIT32(5) /* "e" bit */
+#define END_W0_UNCOND_ESCALATE   PPC_BIT32(6) /* "u" bit - DD2.0 */
+#define END_W0_SILENT_ESCALATE   PPC_BIT32(7) /* "s" bit - DD2.0 */
+#define END_W0_QSIZE             PPC_BITMASK32(12, 15)
+#define END_W0_SW0               PPC_BIT32(16)
+#define END_W0_FIRMWARE          END_W0_SW0 /* Owned by FW */
+#define END_QSIZE_4K             0
+#define END_QSIZE_64K            4
+#define END_W0_HWDEP             PPC_BITMASK32(24, 31)
+        uint32_t        w1;
+#define END_W1_ESn               PPC_BITMASK32(0, 1)
+#define END_W1_ESn_P             PPC_BIT32(0)
+#define END_W1_ESn_Q             PPC_BIT32(1)
+#define END_W1_ESe               PPC_BITMASK32(2, 3)
+#define END_W1_ESe_P             PPC_BIT32(2)
+#define END_W1_ESe_Q             PPC_BIT32(3)
+#define END_W1_GENERATION        PPC_BIT32(9)
+#define END_W1_PAGE_OFF          PPC_BITMASK32(10, 31)
+        uint32_t        w2;
+#define END_W2_MIGRATION_REG     PPC_BITMASK32(0, 3)
+#define END_W2_OP_DESC_HI        PPC_BITMASK32(4, 31)
+        uint32_t        w3;
+#define END_W3_OP_DESC_LO        PPC_BITMASK32(0, 31)
+        uint32_t        w4;
+#define END_W4_ESC_END_BLOCK     PPC_BITMASK32(4, 7)
+#define END_W4_ESC_END_INDEX     PPC_BITMASK32(8, 31)
+        uint32_t        w5;
+#define END_W5_ESC_END_DATA      PPC_BITMASK32(1, 31)
+        uint32_t        w6;
+#define END_W6_FORMAT_BIT        PPC_BIT32(8)
+#define END_W6_NVT_BLOCK         PPC_BITMASK32(9, 12)
+#define END_W6_NVT_INDEX         PPC_BITMASK32(13, 31)
+        uint32_t        w7;
+#define END_W7_F0_IGNORE         PPC_BIT32(0)
+#define END_W7_F0_BLK_GROUPING   PPC_BIT32(1)
+#define END_W7_F0_PRIORITY       PPC_BITMASK32(8, 15)
+#define END_W7_F1_WAKEZ          PPC_BIT32(0)
+#define END_W7_F1_LOG_SERVER_ID  PPC_BITMASK32(1, 31)
+} XiveEND;
+
 #endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +0 -0
index c4c90a25758e..9cb001e7b540 100644
@@ -442,6 +442,101 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 454

Quoted 10 lines
hw/intc/xive.c +10 -0
+/*
+ * XiveEND helpers
+ */
+
+void xive_end_reset(XiveEND *end)
+{
+    memset(end, 0, sizeof(*end));
+
+    /* switch off the escalation and notification ESBs */
+    end->w1 = END_W1_ESe_Q | END_W1_ESn_Q;

It's not obvious to me what circumstances this would be called under. Since the ENDs are in system memory, a memset() seems like an odd thing for (virtual) hardware to be doing to it.

Quoted 51 lines
hw/intc/xive.c +51 -0
+}
+
+static void xive_end_queue_pic_print_info(XiveEND *end, uint32_t width,
+                                          Monitor *mon)
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qentries = 1 << (qsize + 10);
+    int i;
+
+    /*
+     * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
+     */
+    monitor_printf(mon, " [ ");
+    qindex = (qindex - (width - 1)) & (qentries - 1);
+    for (i = 0; i < width; i++) {
+        uint64_t qaddr = qaddr_base + (qindex << 2);
+        uint32_t qdata = -1;
+
+        if (dma_memory_read(&address_space_memory, qaddr, &qdata,
+                            sizeof(qdata))) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
+                          HWADDR_PRIx "\n", qaddr);
+            return;
+        }
+        monitor_printf(mon, "%s%08x ", i == width - 1 ? "^" : "",
+                       be32_to_cpu(qdata));
+        qindex = (qindex + 1) & (qentries - 1);
+    }
+    monitor_printf(mon, "]\n");
+}
+
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon)
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qgen = GETFIELD(END_W1_GENERATION, end->w1);
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qentries = 1 << (qsize + 10);
+
+    uint32_t nvt = GETFIELD(END_W6_NVT_INDEX, end->w6);
+    uint8_t priority = GETFIELD(END_W7_F0_PRIORITY, end->w7);
+
+    if (!(end->w0 & END_W0_VALID)) {
+        return;
+    }
+
+    monitor_printf(mon, "  %08x %c%c%c%c%c prio:%d nvt:%04x eq:@%08"PRIx64
+                   "% 6d/%5d ^%d", end_idx,
+                   end->w0 & END_W0_VALID ? 'v' : '-',

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 515

Quoted 10 lines
hw/intc/xive.c +10 -0
+                   end->w0 & END_W0_ENQUEUE ? 'q' : '-',
+                   end->w0 & END_W0_UCOND_NOTIFY ? 'n' : '-',
+                   end->w0 & END_W0_BACKLOG ? 'b' : '-',
+                   end->w0 & END_W0_ESCALATE_CTL ? 'e' : '-',
+                   priority, nvt, qaddr_base, qindex, qentries, qgen);
+
+    xive_end_queue_pic_print_info(end, 6, mon);
+}
+
+static void xive_end_push(XiveEND *end, uint32_t data)

s/push/enqueue/ please, "push" suggests a stack. (Not to mention that "push" and "pull" are used as terms elsewhere in XIVE).

Quoted 111 lines
hw/intc/xive.c +100 -0
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qgen = GETFIELD(END_W1_GENERATION, end->w1);
+
+    uint64_t qaddr = qaddr_base + (qindex << 2);
+    uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
+    uint32_t qentries = 1 << (qsize + 10);
+
+    if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata))) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
+                      HWADDR_PRIx "\n", qaddr);
+        return;
+    }
+
+    qindex = (qindex + 1) & (qentries - 1);
+    if (qindex == 0) {
+        qgen ^= 1;
+        end->w1 = SETFIELD(END_W1_GENERATION, end->w1, qgen);
+    }
+    end->w1 = SETFIELD(END_W1_PAGE_OFF, end->w1, qindex);
+}
+
 /*
  * XIVE Router (aka. Virtualization Controller or IVRE)
  */
@@ -460,6 +555,82 @@ int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
     return xrc->set_eas(xrtr, lisn, eas);
 }
 
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->get_end(xrtr, end_blk, end_idx, end);
+}
+
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->set_end(xrtr, end_blk, end_idx, end);
+}
+
+/*
+ * An END trigger can come from an event trigger (IPI or HW) or from
+ * another chip. We don't model the PowerBus but the END trigger
+ * message has the same parameters than in the function below.
+ */
+static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
+                                   uint32_t end_idx, uint32_t end_data)
+{
+    XiveEND end;
+    uint8_t priority;
+    uint8_t format;
+
+    /* END cache lookup */
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
+                      end_idx);
+        return;
+    }
+
+    if (!(end.w0 & END_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
+                      end_blk, end_idx);
+        return;
+    }
+
+    if (end.w0 & END_W0_ENQUEUE) {
+        xive_end_push(&end, end_data);
+        xive_router_set_end(xrtr, end_blk, end_idx, &end);
+    }
+
+    /*
+     * The W7 format depends on the F bit in W6. It defines the type
+     * of the notification :
+     *
+     *   F=0 : single or multiple NVT notification
+     *   F=1 : User level Event-Based Branch (EBB) notification, no
+     *         priority
+     */
+    format = GETFIELD(END_W6_FORMAT_BIT, end.w6);
+    priority = GETFIELD(END_W7_F0_PRIORITY, end.w7);
+
+    /* The END is masked */
+    if (format == 0 && priority == 0xff) {
+        return;
+    }
+
+    /*
+     * Check the END ESn (Event State Buffer for notification) for
+     * even futher coalescing in the Router
+     */
+    if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
+        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
+        return;
+    }
+
+    /*
+     * Follows IVPE notification
+     */
+}
+
 static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
 {
     XiveRouter *xrtr = XIVE_ROUTER(xf);

hw/intc/xive.c static void xive_router_notify(XiveFabric *xf, uint32_t lisn) · line 647

Quoted 9 lines
hw/intc/xive.c +3 -3
@@ -471,9 +642,9 @@ static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
         return;
     }
 
-    /* The IVRE has a State Bit Cache for its internal sources which
-     * is also involed at this point. We skip the SBC lookup because
-     * the state bits of the sources are modeled internally in QEMU.
+    /* The IVRE checks the State Bit Cache at this point. We skip the
+     * SBC lookup because the state bits of the sources are modeled
+     * internally in QEMU.

Replacing a comment about something we're not doing with a different comment about something we're not doing doesn't seem very useful. Maybe fold these together into one patch or the other.

Quoted 18 lines
hw/intc/xive.c +8 -0
      */
 
     if (!(eas.w & EAS_VALID)) {
@@ -485,6 +656,14 @@ static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
         /* Notification completed */
         return;
     }
+
+    /*
+     * The event trigger becomes an END trigger
+     */
+    xive_router_end_notify(xrtr,
+                           GETFIELD(EAS_END_BLOCK, eas.w),
+                           GETFIELD(EAS_END_INDEX, eas.w),
+                           GETFIELD(EAS_END_DATA,  eas.w));
 }
 
 static Property xive_router_properties[] = {
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
#37 lore raw

Re: [Qemu-devel] [PATCH v5 05/36] ppc/xive: introduce the XIVE Event Notification Descriptors

Quoted 13 lines · David Gibson

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 454

Quoted 7 lines
hw/intc/xive.c +6 -0
+void xive_end_reset(XiveEND *end)
+{
+    memset(end, 0, sizeof(*end));
+
+    /* switch off the escalation and notification ESBs */
+    end->w1 = END_W1_ESe_Q | END_W1_ESn_Q;

It's not obvious to me what circumstances this would be called under. Since the ENDs are in system memory, a memset() seems like an odd thing for (virtual) hardware to be doing to it.

Quoted 1 line

+}

Not on PAPR ...

Ben.

#38 lore raw

Re: [Qemu-devel] [PATCH v5 05/36] ppc/xive: introduce the XIVE Event Notification Descriptors

Quoted 13 lines · Benjamin Herrenschmidt
Quoted 12 lines · David Gibson

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 454

Quoted 7 lines
hw/intc/xive.c +6 -0
+void xive_end_reset(XiveEND *end)
+{
+    memset(end, 0, sizeof(*end));
+
+    /* switch off the escalation and notification ESBs */
+    end->w1 = END_W1_ESe_Q | END_W1_ESn_Q;

It's not obvious to me what circumstances this would be called under. Since the ENDs are in system memory, a memset() seems like an odd thing for (virtual) hardware to be doing to it.

Quoted 3 lines
Quoted 1 line
Quoted 1 line

+}

Not on PAPR ...

Right, so the memset() can go in PAPR specific code.

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
#39 lore raw

Re: [Qemu-devel] [PATCH v5 05/36] ppc/xive: introduce the XIVE Event Notification Descriptors

In reply to David Gibson
Quoted 26 lines · David Gibson
Quoted 25 lines · Cédric Le Goater

To complete the event routing, the IVRE sub-engine uses an internal table containing Event Notification Descriptor (END) structures.

An END specifies on which Event Queue (EQ) the event notification data, defined in the associated EAS, should be posted when an exception occurs. It also defines which Notification Virtual Target (NVT) should be notified.

The Event Queue is a memory page provided by the O/S defining a circular buffer, one per server and priority couple, containing Event Queue entries. These are 4 bytes long, the first bit being a 'generation' bit and the 31 following bits the END Data field. They are pulled by the O/S when the exception occurs.

The END Data field is a way to set an invariant logical event source number for an IRQ. It is set with the H_INT_SET_SOURCE_CONFIG hcall when the EISN flag is used.

---
 include/hw/ppc/xive.h      |  18 ++++
 include/hw/ppc/xive_regs.h |  48 ++++++++++
 hw/intc/xive.c             | 185 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 248 insertions(+), 3 deletions(-)
Quoted 15 lines

include/hw/ppc/xive.h typedef struct XiveRouterClass { · line 199

Quoted 12 lines
include/hw/ppc/xive.h +4 -0
index 5a0696366577..ce62aaf28343 100644
@@ -193,11 +193,29 @@ typedef struct XiveRouterClass {
     /* XIVE table accessors */
     int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
     int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*get_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);
+    int (*set_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);

Hrm. So unlike the EAS, which is basically just a word, the END is a pretty large structure.

yes. and so will be the NVT.

Quoted 2 lines

It's unclear here if get/set are expected to copy the whole thing out and in,

That's the plan.

What I had in mind are memory accessors to the XIVE structures, which are local to QEMU for sPAPR and in the guest RAM for PowerNV (Please take a look at the XIVE PowerNV model).

Quoted 1 line

or if get give you a pointer into a "live" structure

no

Quoted 1 line

and set just does any necessary barriers after an update.

that would be too complex for the PowerNV model I think. There is a cache in between the software running on the (QEMU) machine and the XIVE HW but it would be hard to handle.

Quoted 2 lines

Really, for a non-atomic value like this, I'm not sure get/set is the right model.

ok. we need something to get them out and in.

Quoted 3 lines

Also as I understand it nearly all the indices in XIVE are broken into block/index. Is there a reason those are folded together into lisn for the EAS, but not for the END?

The indexing of the EAT is global to the sytem and the index defines which blk to use. The IRQ source numbers on the powerbus are architected to be :

    #define XIVE_SRCNO(blk, idx)      ((uint32_t)(blk) << 28 | (idx))

and XIVE can use different strategies to identify the XIVE IC in charge of routing. It can be a one-to-one chip to block relation as skiboot does. Using a block scope table is possible also. Our model only supports one block per chip and some shortcuts are taken but not that much in fact.

Remote access to the XIVE structures of another chip are done through MMIO (not modeled in PowerNV) and the blkid is used to partition the MMIO regions. Being local is better for performance because the END and NVT tables have a strong relation with the XIVE subengines using them (VC and PC).

May be, Ben can clarified it this is badly explained.

Quoted 87 lines
Quoted 87 lines
include/hw/ppc/xive.h +14 -0
 } XiveRouterClass;
 
 void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
 
 int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
 int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+
+/*
+ * For legacy compatibility, the exceptions define up to 256 different
+ * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
+ * and the least favored level 0xFF.
+ */
+#define XIVE_PRIORITY_MAX  7
+
+void xive_end_reset(XiveEND *end);
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +48 -0
index 12499b33614c..f97fb2b90bee 100644
@@ -28,4 +28,52 @@ typedef struct XiveEAS {
 #define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
 } XiveEAS;
 
+/* Event Notification Descriptor (END) */
+typedef struct XiveEND {
+        uint32_t        w0;
+#define END_W0_VALID             PPC_BIT32(0) /* "v" bit */
+#define END_W0_ENQUEUE           PPC_BIT32(1) /* "q" bit */
+#define END_W0_UCOND_NOTIFY      PPC_BIT32(2) /* "n" bit */
+#define END_W0_BACKLOG           PPC_BIT32(3) /* "b" bit */
+#define END_W0_PRECL_ESC_CTL     PPC_BIT32(4) /* "p" bit */
+#define END_W0_ESCALATE_CTL      PPC_BIT32(5) /* "e" bit */
+#define END_W0_UNCOND_ESCALATE   PPC_BIT32(6) /* "u" bit - DD2.0 */
+#define END_W0_SILENT_ESCALATE   PPC_BIT32(7) /* "s" bit - DD2.0 */
+#define END_W0_QSIZE             PPC_BITMASK32(12, 15)
+#define END_W0_SW0               PPC_BIT32(16)
+#define END_W0_FIRMWARE          END_W0_SW0 /* Owned by FW */
+#define END_QSIZE_4K             0
+#define END_QSIZE_64K            4
+#define END_W0_HWDEP             PPC_BITMASK32(24, 31)
+        uint32_t        w1;
+#define END_W1_ESn               PPC_BITMASK32(0, 1)
+#define END_W1_ESn_P             PPC_BIT32(0)
+#define END_W1_ESn_Q             PPC_BIT32(1)
+#define END_W1_ESe               PPC_BITMASK32(2, 3)
+#define END_W1_ESe_P             PPC_BIT32(2)
+#define END_W1_ESe_Q             PPC_BIT32(3)
+#define END_W1_GENERATION        PPC_BIT32(9)
+#define END_W1_PAGE_OFF          PPC_BITMASK32(10, 31)
+        uint32_t        w2;
+#define END_W2_MIGRATION_REG     PPC_BITMASK32(0, 3)
+#define END_W2_OP_DESC_HI        PPC_BITMASK32(4, 31)
+        uint32_t        w3;
+#define END_W3_OP_DESC_LO        PPC_BITMASK32(0, 31)
+        uint32_t        w4;
+#define END_W4_ESC_END_BLOCK     PPC_BITMASK32(4, 7)
+#define END_W4_ESC_END_INDEX     PPC_BITMASK32(8, 31)
+        uint32_t        w5;
+#define END_W5_ESC_END_DATA      PPC_BITMASK32(1, 31)
+        uint32_t        w6;
+#define END_W6_FORMAT_BIT        PPC_BIT32(8)
+#define END_W6_NVT_BLOCK         PPC_BITMASK32(9, 12)
+#define END_W6_NVT_INDEX         PPC_BITMASK32(13, 31)
+        uint32_t        w7;
+#define END_W7_F0_IGNORE         PPC_BIT32(0)
+#define END_W7_F0_BLK_GROUPING   PPC_BIT32(1)
+#define END_W7_F0_PRIORITY       PPC_BITMASK32(8, 15)
+#define END_W7_F1_WAKEZ          PPC_BIT32(0)
+#define END_W7_F1_LOG_SERVER_ID  PPC_BITMASK32(1, 31)
+} XiveEND;
+
 #endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +0 -0
index c4c90a25758e..9cb001e7b540 100644
@@ -442,6 +442,101 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 
Quoted 14 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 454

Quoted 10 lines
hw/intc/xive.c +10 -0
+/*
+ * XiveEND helpers
+ */
+
+void xive_end_reset(XiveEND *end)
+{
+    memset(end, 0, sizeof(*end));
+
+    /* switch off the escalation and notification ESBs */
+    end->w1 = END_W1_ESe_Q | END_W1_ESn_Q;

It's not obvious to me what circumstances this would be called under. Since the ENDs are in system memory, a memset() seems like an odd thing for (virtual) hardware to be doing to it.

It makes sense on sPAPR if one day some OS starts using the END ESBs for further coalescing of the events. None does for now but I have added the model though.

Quoted 56 lines
Quoted 56 lines
hw/intc/xive.c +56 -0
+}
+
+static void xive_end_queue_pic_print_info(XiveEND *end, uint32_t width,
+                                          Monitor *mon)
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qentries = 1 << (qsize + 10);
+    int i;
+
+    /*
+     * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
+     */
+    monitor_printf(mon, " [ ");
+    qindex = (qindex - (width - 1)) & (qentries - 1);
+    for (i = 0; i < width; i++) {
+        uint64_t qaddr = qaddr_base + (qindex << 2);
+        uint32_t qdata = -1;
+
+        if (dma_memory_read(&address_space_memory, qaddr, &qdata,
+                            sizeof(qdata))) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
+                          HWADDR_PRIx "\n", qaddr);
+            return;
+        }
+        monitor_printf(mon, "%s%08x ", i == width - 1 ? "^" : "",
+                       be32_to_cpu(qdata));
+        qindex = (qindex + 1) & (qentries - 1);
+    }
+    monitor_printf(mon, "]\n");
+}
+
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon)
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qgen = GETFIELD(END_W1_GENERATION, end->w1);
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qentries = 1 << (qsize + 10);
+
+    uint32_t nvt = GETFIELD(END_W6_NVT_INDEX, end->w6);
+    uint8_t priority = GETFIELD(END_W7_F0_PRIORITY, end->w7);
+
+    if (!(end->w0 & END_W0_VALID)) {
+        return;
+    }
+
+    monitor_printf(mon, "  %08x %c%c%c%c%c prio:%d nvt:%04x eq:@%08"PRIx64
+                   "% 6d/%5d ^%d", end_idx,
+                   end->w0 & END_W0_VALID ? 'v' : '-',
+                   end->w0 & END_W0_ENQUEUE ? 'q' : '-',
+                   end->w0 & END_W0_UCOND_NOTIFY ? 'n' : '-',
+                   end->w0 & END_W0_BACKLOG ? 'b' : '-',
+                   end->w0 & END_W0_ESCALATE_CTL ? 'e' : '-',
+                   priority, nvt, qaddr_base, qindex, qentries, qgen);
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 515

Quoted 5 lines
hw/intc/xive.c +5 -0
+
+    xive_end_queue_pic_print_info(end, 6, mon);
+}
+
+static void xive_end_push(XiveEND *end, uint32_t data)

s/push/enqueue/ please, "push" suggests a stack. (Not to mention that "push" and "pull" are used as terms elsewhere in XIVE).

yes. you are right. I will change.

Quoted 114 lines
Quoted 114 lines
hw/intc/xive.c +100 -0
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qgen = GETFIELD(END_W1_GENERATION, end->w1);
+
+    uint64_t qaddr = qaddr_base + (qindex << 2);
+    uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
+    uint32_t qentries = 1 << (qsize + 10);
+
+    if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata))) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
+                      HWADDR_PRIx "\n", qaddr);
+        return;
+    }
+
+    qindex = (qindex + 1) & (qentries - 1);
+    if (qindex == 0) {
+        qgen ^= 1;
+        end->w1 = SETFIELD(END_W1_GENERATION, end->w1, qgen);
+    }
+    end->w1 = SETFIELD(END_W1_PAGE_OFF, end->w1, qindex);
+}
+
 /*
  * XIVE Router (aka. Virtualization Controller or IVRE)
  */
@@ -460,6 +555,82 @@ int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
     return xrc->set_eas(xrtr, lisn, eas);
 }
 
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->get_end(xrtr, end_blk, end_idx, end);
+}
+
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->set_end(xrtr, end_blk, end_idx, end);
+}
+
+/*
+ * An END trigger can come from an event trigger (IPI or HW) or from
+ * another chip. We don't model the PowerBus but the END trigger
+ * message has the same parameters than in the function below.
+ */
+static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
+                                   uint32_t end_idx, uint32_t end_data)
+{
+    XiveEND end;
+    uint8_t priority;
+    uint8_t format;
+
+    /* END cache lookup */
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
+                      end_idx);
+        return;
+    }
+
+    if (!(end.w0 & END_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
+                      end_blk, end_idx);
+        return;
+    }
+
+    if (end.w0 & END_W0_ENQUEUE) {
+        xive_end_push(&end, end_data);
+        xive_router_set_end(xrtr, end_blk, end_idx, &end);
+    }
+
+    /*
+     * The W7 format depends on the F bit in W6. It defines the type
+     * of the notification :
+     *
+     *   F=0 : single or multiple NVT notification
+     *   F=1 : User level Event-Based Branch (EBB) notification, no
+     *         priority
+     */
+    format = GETFIELD(END_W6_FORMAT_BIT, end.w6);
+    priority = GETFIELD(END_W7_F0_PRIORITY, end.w7);
+
+    /* The END is masked */
+    if (format == 0 && priority == 0xff) {
+        return;
+    }
+
+    /*
+     * Check the END ESn (Event State Buffer for notification) for
+     * even futher coalescing in the Router
+     */
+    if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
+        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
+        return;
+    }
+
+    /*
+     * Follows IVPE notification
+     */
+}
+
 static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
 {
     XiveRouter *xrtr = XIVE_ROUTER(xf);
@@ -471,9 +642,9 @@ static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
         return;
     }
 
Quoted 10 lines

hw/intc/xive.c static void xive_router_notify(XiveFabric *xf, uint32_t lisn) · line 647

Quoted 6 lines
hw/intc/xive.c +3 -3
-    /* The IVRE has a State Bit Cache for its internal sources which
-     * is also involed at this point. We skip the SBC lookup because
-     * the state bits of the sources are modeled internally in QEMU.
+    /* The IVRE checks the State Bit Cache at this point. We skip the
+     * SBC lookup because the state bits of the sources are modeled
+     * internally in QEMU.

Replacing a comment about something we're not doing with a different comment about something we're not doing doesn't seem very useful. Maybe fold these together into one patch or the other.

That's me rephrasing. it should be indeed in the previous patch

Thanks,

C.

Quoted 19 lines
Quoted 18 lines
hw/intc/xive.c +8 -0
      */
 
     if (!(eas.w & EAS_VALID)) {
@@ -485,6 +656,14 @@ static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
         /* Notification completed */
         return;
     }
+
+    /*
+     * The event trigger becomes an END trigger
+     */
+    xive_router_end_notify(xrtr,
+                           GETFIELD(EAS_END_BLOCK, eas.w),
+                           GETFIELD(EAS_END_INDEX, eas.w),
+                           GETFIELD(EAS_END_DATA,  eas.w));
 }
 
 static Property xive_router_properties[] = {
#40 lore raw

Re: [Qemu-devel] [PATCH v5 05/36] ppc/xive: introduce the XIVE Event Notification Descriptors

Quoted 43 lines · Cédric Le Goater
Quoted 26 lines · David Gibson
Quoted 25 lines · Cédric Le Goater

To complete the event routing, the IVRE sub-engine uses an internal table containing Event Notification Descriptor (END) structures.

An END specifies on which Event Queue (EQ) the event notification data, defined in the associated EAS, should be posted when an exception occurs. It also defines which Notification Virtual Target (NVT) should be notified.

The Event Queue is a memory page provided by the O/S defining a circular buffer, one per server and priority couple, containing Event Queue entries. These are 4 bytes long, the first bit being a 'generation' bit and the 31 following bits the END Data field. They are pulled by the O/S when the exception occurs.

The END Data field is a way to set an invariant logical event source number for an IRQ. It is set with the H_INT_SET_SOURCE_CONFIG hcall when the EISN flag is used.

---
 include/hw/ppc/xive.h      |  18 ++++
 include/hw/ppc/xive_regs.h |  48 ++++++++++
 hw/intc/xive.c             | 185 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 248 insertions(+), 3 deletions(-)
Quoted 15 lines

include/hw/ppc/xive.h typedef struct XiveRouterClass { · line 199

Quoted 12 lines
include/hw/ppc/xive.h +4 -0
index 5a0696366577..ce62aaf28343 100644
@@ -193,11 +193,29 @@ typedef struct XiveRouterClass {
     /* XIVE table accessors */
     int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
     int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*get_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);
+    int (*set_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);

Hrm. So unlike the EAS, which is basically just a word, the END is a pretty large structure.

Quoted 6 lines

yes. and so will be the NVT.

Quoted 2 lines

It's unclear here if get/set are expected to copy the whole thing out and in,

That's the plan.

Yeah, I don't think that's a good idea. In some cases the updates are on hot paths, so the extra copy isn't good, and more importantly it makes it look like an atomic update, but it's not really.

Well... I guess it probably is because of the BQL, but I'd prefer not to rely on that excessively.

Quoted 9 lines

What I had in mind are memory accessors to the XIVE structures, which are local to QEMU for sPAPR and in the guest RAM for PowerNV (Please take a look at the XIVE PowerNV model).

Quoted 1 line

or if get give you a pointer into a "live" structure

no

Quoted 1 line

and set just does any necessary barriers after an update.

Quoted 8 lines

that would be too complex for the PowerNV model I think. There is a cache in between the software running on the (QEMU) machine and the XIVE HW but it would be hard to handle.

Quoted 2 lines

Really, for a non-atomic value like this, I'm not sure get/set is the right model.

ok. we need something to get them out and in.

I've thought about this a bit more. What I think might work is "end_read" and "end_write" callbacks, which take a word number in addition to the parameters you have already

Quoted 15 lines
Quoted 3 lines

Also as I understand it nearly all the indices in XIVE are broken into block/index. Is there a reason those are folded together into lisn for the EAS, but not for the END?

The indexing of the EAT is global to the sytem and the index defines which blk to use. The IRQ source numbers on the powerbus are architected to be :

    #define XIVE_SRCNO(blk, idx)      ((uint32_t)(blk) << 28 | (idx))

and XIVE can use different strategies to identify the XIVE IC in charge of routing. It can be a one-to-one chip to block relation as skiboot does. Using a block scope table is possible also. Our model only supports one block per chip and some shortcuts are taken but not that much in fact.

Quoted 7 lines

Remote access to the XIVE structures of another chip are done through MMIO (not modeled in PowerNV) and the blkid is used to partition the MMIO regions. Being local is better for performance because the END and NVT tables have a strong relation with the XIVE subengines using them (VC and PC).

May be, Ben can clarified it this is badly explained.

Right.. I think I understand what the blocks are all about.

But my question is, why encode the block and index together for the EAS, but separately for the END?

Quoted 103 lines
Quoted 88 lines
Quoted 88 lines
include/hw/ppc/xive.h +14 -0
 } XiveRouterClass;
 
 void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
 
 int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
 int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+
+/*
+ * For legacy compatibility, the exceptions define up to 256 different
+ * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
+ * and the least favored level 0xFF.
+ */
+#define XIVE_PRIORITY_MAX  7
+
+void xive_end_reset(XiveEND *end);
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +48 -0
index 12499b33614c..f97fb2b90bee 100644
@@ -28,4 +28,52 @@ typedef struct XiveEAS {
 #define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
 } XiveEAS;
 
+/* Event Notification Descriptor (END) */
+typedef struct XiveEND {
+        uint32_t        w0;
+#define END_W0_VALID             PPC_BIT32(0) /* "v" bit */
+#define END_W0_ENQUEUE           PPC_BIT32(1) /* "q" bit */
+#define END_W0_UCOND_NOTIFY      PPC_BIT32(2) /* "n" bit */
+#define END_W0_BACKLOG           PPC_BIT32(3) /* "b" bit */
+#define END_W0_PRECL_ESC_CTL     PPC_BIT32(4) /* "p" bit */
+#define END_W0_ESCALATE_CTL      PPC_BIT32(5) /* "e" bit */
+#define END_W0_UNCOND_ESCALATE   PPC_BIT32(6) /* "u" bit - DD2.0 */
+#define END_W0_SILENT_ESCALATE   PPC_BIT32(7) /* "s" bit - DD2.0 */
+#define END_W0_QSIZE             PPC_BITMASK32(12, 15)
+#define END_W0_SW0               PPC_BIT32(16)
+#define END_W0_FIRMWARE          END_W0_SW0 /* Owned by FW */
+#define END_QSIZE_4K             0
+#define END_QSIZE_64K            4
+#define END_W0_HWDEP             PPC_BITMASK32(24, 31)
+        uint32_t        w1;
+#define END_W1_ESn               PPC_BITMASK32(0, 1)
+#define END_W1_ESn_P             PPC_BIT32(0)
+#define END_W1_ESn_Q             PPC_BIT32(1)
+#define END_W1_ESe               PPC_BITMASK32(2, 3)
+#define END_W1_ESe_P             PPC_BIT32(2)
+#define END_W1_ESe_Q             PPC_BIT32(3)
+#define END_W1_GENERATION        PPC_BIT32(9)
+#define END_W1_PAGE_OFF          PPC_BITMASK32(10, 31)
+        uint32_t        w2;
+#define END_W2_MIGRATION_REG     PPC_BITMASK32(0, 3)
+#define END_W2_OP_DESC_HI        PPC_BITMASK32(4, 31)
+        uint32_t        w3;
+#define END_W3_OP_DESC_LO        PPC_BITMASK32(0, 31)
+        uint32_t        w4;
+#define END_W4_ESC_END_BLOCK     PPC_BITMASK32(4, 7)
+#define END_W4_ESC_END_INDEX     PPC_BITMASK32(8, 31)
+        uint32_t        w5;
+#define END_W5_ESC_END_DATA      PPC_BITMASK32(1, 31)
+        uint32_t        w6;
+#define END_W6_FORMAT_BIT        PPC_BIT32(8)
+#define END_W6_NVT_BLOCK         PPC_BITMASK32(9, 12)
+#define END_W6_NVT_INDEX         PPC_BITMASK32(13, 31)
+        uint32_t        w7;
+#define END_W7_F0_IGNORE         PPC_BIT32(0)
+#define END_W7_F0_BLK_GROUPING   PPC_BIT32(1)
+#define END_W7_F0_PRIORITY       PPC_BITMASK32(8, 15)
+#define END_W7_F1_WAKEZ          PPC_BIT32(0)
+#define END_W7_F1_LOG_SERVER_ID  PPC_BITMASK32(1, 31)
+} XiveEND;
+
 #endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +0 -0
index c4c90a25758e..9cb001e7b540 100644
@@ -442,6 +442,101 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 
Quoted 14 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 454

Quoted 10 lines
hw/intc/xive.c +10 -0
+/*
+ * XiveEND helpers
+ */
+
+void xive_end_reset(XiveEND *end)
+{
+    memset(end, 0, sizeof(*end));
+
+    /* switch off the escalation and notification ESBs */
+    end->w1 = END_W1_ESe_Q | END_W1_ESn_Q;

It's not obvious to me what circumstances this would be called under. Since the ENDs are in system memory, a memset() seems like an odd thing for (virtual) hardware to be doing to it.

Quoted 3 lines

It makes sense on sPAPR if one day some OS starts using the END ESBs for further coalescing of the events. None does for now but I have added the model though.

Hrm, I think that belongs in PAPR specific code. It's not really part of the router model - it's the PAPR stuff configuring the router at reset time (much as firmware would configure it at reset time for bare metal).

Quoted 219 lines
Quoted 57 lines
Quoted 57 lines
hw/intc/xive.c +56 -0
+}
+
+static void xive_end_queue_pic_print_info(XiveEND *end, uint32_t width,
+                                          Monitor *mon)
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qentries = 1 << (qsize + 10);
+    int i;
+
+    /*
+     * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
+     */
+    monitor_printf(mon, " [ ");
+    qindex = (qindex - (width - 1)) & (qentries - 1);
+    for (i = 0; i < width; i++) {
+        uint64_t qaddr = qaddr_base + (qindex << 2);
+        uint32_t qdata = -1;
+
+        if (dma_memory_read(&address_space_memory, qaddr, &qdata,
+                            sizeof(qdata))) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
+                          HWADDR_PRIx "\n", qaddr);
+            return;
+        }
+        monitor_printf(mon, "%s%08x ", i == width - 1 ? "^" : "",
+                       be32_to_cpu(qdata));
+        qindex = (qindex + 1) & (qentries - 1);
+    }
+    monitor_printf(mon, "]\n");
+}
+
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon)
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qgen = GETFIELD(END_W1_GENERATION, end->w1);
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qentries = 1 << (qsize + 10);
+
+    uint32_t nvt = GETFIELD(END_W6_NVT_INDEX, end->w6);
+    uint8_t priority = GETFIELD(END_W7_F0_PRIORITY, end->w7);
+
+    if (!(end->w0 & END_W0_VALID)) {
+        return;
+    }
+
+    monitor_printf(mon, "  %08x %c%c%c%c%c prio:%d nvt:%04x eq:@%08"PRIx64
+                   "% 6d/%5d ^%d", end_idx,
+                   end->w0 & END_W0_VALID ? 'v' : '-',
+                   end->w0 & END_W0_ENQUEUE ? 'q' : '-',
+                   end->w0 & END_W0_UCOND_NOTIFY ? 'n' : '-',
+                   end->w0 & END_W0_BACKLOG ? 'b' : '-',
+                   end->w0 & END_W0_ESCALATE_CTL ? 'e' : '-',
+                   priority, nvt, qaddr_base, qindex, qentries, qgen);
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 515

Quoted 5 lines
hw/intc/xive.c +5 -0
+
+    xive_end_queue_pic_print_info(end, 6, mon);
+}
+
+static void xive_end_push(XiveEND *end, uint32_t data)

s/push/enqueue/ please, "push" suggests a stack. (Not to mention that "push" and "pull" are used as terms elsewhere in XIVE).

yes. you are right. I will change.

Quoted 114 lines
Quoted 114 lines
hw/intc/xive.c +100 -0
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qgen = GETFIELD(END_W1_GENERATION, end->w1);
+
+    uint64_t qaddr = qaddr_base + (qindex << 2);
+    uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
+    uint32_t qentries = 1 << (qsize + 10);
+
+    if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata))) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
+                      HWADDR_PRIx "\n", qaddr);
+        return;
+    }
+
+    qindex = (qindex + 1) & (qentries - 1);
+    if (qindex == 0) {
+        qgen ^= 1;
+        end->w1 = SETFIELD(END_W1_GENERATION, end->w1, qgen);
+    }
+    end->w1 = SETFIELD(END_W1_PAGE_OFF, end->w1, qindex);
+}
+
 /*
  * XIVE Router (aka. Virtualization Controller or IVRE)
  */
@@ -460,6 +555,82 @@ int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
     return xrc->set_eas(xrtr, lisn, eas);
 }
 
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->get_end(xrtr, end_blk, end_idx, end);
+}
+
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->set_end(xrtr, end_blk, end_idx, end);
+}
+
+/*
+ * An END trigger can come from an event trigger (IPI or HW) or from
+ * another chip. We don't model the PowerBus but the END trigger
+ * message has the same parameters than in the function below.
+ */
+static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
+                                   uint32_t end_idx, uint32_t end_data)
+{
+    XiveEND end;
+    uint8_t priority;
+    uint8_t format;
+
+    /* END cache lookup */
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
+                      end_idx);
+        return;
+    }
+
+    if (!(end.w0 & END_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
+                      end_blk, end_idx);
+        return;
+    }
+
+    if (end.w0 & END_W0_ENQUEUE) {
+        xive_end_push(&end, end_data);
+        xive_router_set_end(xrtr, end_blk, end_idx, &end);
+    }
+
+    /*
+     * The W7 format depends on the F bit in W6. It defines the type
+     * of the notification :
+     *
+     *   F=0 : single or multiple NVT notification
+     *   F=1 : User level Event-Based Branch (EBB) notification, no
+     *         priority
+     */
+    format = GETFIELD(END_W6_FORMAT_BIT, end.w6);
+    priority = GETFIELD(END_W7_F0_PRIORITY, end.w7);
+
+    /* The END is masked */
+    if (format == 0 && priority == 0xff) {
+        return;
+    }
+
+    /*
+     * Check the END ESn (Event State Buffer for notification) for
+     * even futher coalescing in the Router
+     */
+    if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
+        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
+        return;
+    }
+
+    /*
+     * Follows IVPE notification
+     */
+}
+
 static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
 {
     XiveRouter *xrtr = XIVE_ROUTER(xf);
@@ -471,9 +642,9 @@ static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
         return;
     }
 
Quoted 10 lines

hw/intc/xive.c static void xive_router_notify(XiveFabric *xf, uint32_t lisn) · line 647

Quoted 6 lines
hw/intc/xive.c +3 -3
-    /* The IVRE has a State Bit Cache for its internal sources which
-     * is also involed at this point. We skip the SBC lookup because
-     * the state bits of the sources are modeled internally in QEMU.
+    /* The IVRE checks the State Bit Cache at this point. We skip the
+     * SBC lookup because the state bits of the sources are modeled
+     * internally in QEMU.

Replacing a comment about something we're not doing with a different comment about something we're not doing doesn't seem very useful. Maybe fold these together into one patch or the other.

That's me rephrasing. it should be indeed in the previous patch

Thanks,

C.

Quoted 19 lines
Quoted 18 lines
hw/intc/xive.c +8 -0
      */
 
     if (!(eas.w & EAS_VALID)) {
@@ -485,6 +656,14 @@ static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
         /* Notification completed */
         return;
     }
+
+    /*
+     * The event trigger becomes an END trigger
+     */
+    xive_router_end_notify(xrtr,
+                           GETFIELD(EAS_END_BLOCK, eas.w),
+                           GETFIELD(EAS_END_INDEX, eas.w),
+                           GETFIELD(EAS_END_DATA,  eas.w));
 }
 
 static Property xive_router_properties[] = {
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
#41 lore raw

Re: [Qemu-devel] [PATCH v5 05/36] ppc/xive: introduce the XIVE Event Notification Descriptors

Quoted 71 lines · David Gibson
Quoted 43 lines · Cédric Le Goater
Quoted 26 lines · David Gibson
Quoted 25 lines · Cédric Le Goater

To complete the event routing, the IVRE sub-engine uses an internal table containing Event Notification Descriptor (END) structures.

An END specifies on which Event Queue (EQ) the event notification data, defined in the associated EAS, should be posted when an exception occurs. It also defines which Notification Virtual Target (NVT) should be notified.

The Event Queue is a memory page provided by the O/S defining a circular buffer, one per server and priority couple, containing Event Queue entries. These are 4 bytes long, the first bit being a 'generation' bit and the 31 following bits the END Data field. They are pulled by the O/S when the exception occurs.

The END Data field is a way to set an invariant logical event source number for an IRQ. It is set with the H_INT_SET_SOURCE_CONFIG hcall when the EISN flag is used.

---
 include/hw/ppc/xive.h      |  18 ++++
 include/hw/ppc/xive_regs.h |  48 ++++++++++
 hw/intc/xive.c             | 185 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 248 insertions(+), 3 deletions(-)
Quoted 15 lines

include/hw/ppc/xive.h typedef struct XiveRouterClass { · line 199

Quoted 12 lines
include/hw/ppc/xive.h +4 -0
index 5a0696366577..ce62aaf28343 100644
@@ -193,11 +193,29 @@ typedef struct XiveRouterClass {
     /* XIVE table accessors */
     int (*get_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
     int (*set_eas)(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+    int (*get_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);
+    int (*set_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                   XiveEND *end);

Hrm. So unlike the EAS, which is basically just a word, the END is a pretty large structure.

Quoted 6 lines

yes. and so will be the NVT.

Quoted 2 lines

It's unclear here if get/set are expected to copy the whole thing out and in,

That's the plan.

Yeah, I don't think that's a good idea. In some cases the updates are on hot paths, so the extra copy isn't good, and more importantly it makes it look like an atomic update, but it's not really.

Well... I guess it probably is because of the BQL, but I'd prefer not to rely on that excessively.

Quoted 13 lines

What I had in mind are memory accessors to the XIVE structures, which are local to QEMU for sPAPR and in the guest RAM for PowerNV (Please take a look at the XIVE PowerNV model).

Quoted 1 line

or if get give you a pointer into a "live" structure

no

Quoted 1 line

and set just does any necessary barriers after an update.

that would be too complex for the PowerNV model I think. There is a cache in between the software running on the (QEMU) machine and the XIVE HW but it would be hard to handle.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Really, for a non-atomic value like this, I'm not sure get/set is the right model.

ok. we need something to get them out and in.

I've thought about this a bit more. What I think might work is "end_read" and "end_write" callbacks, which take a word number in addition to the parameters you have already

ouch. This is not going to simplify the routing algo where all the get and set are done. The whole END is needed in the END trigger. So we will need routines to get and set the whole END.

The NVT is not used too much for the moment.

Quoted 21 lines
Quoted 21 lines
Quoted 3 lines

Also as I understand it nearly all the indices in XIVE are broken into block/index. Is there a reason those are folded together into lisn for the EAS, but not for the END?

The indexing of the EAT is global to the sytem and the index defines which blk to use. The IRQ source numbers on the powerbus are architected to be :

    #define XIVE_SRCNO(blk, idx)      ((uint32_t)(blk) << 28 | (idx))

and XIVE can use different strategies to identify the XIVE IC in charge of routing. It can be a one-to-one chip to block relation as skiboot does. Using a block scope table is possible also. Our model only supports one block per chip and some shortcuts are taken but not that much in fact.

Remote access to the XIVE structures of another chip are done through MMIO (not modeled in PowerNV) and the blkid is used to partition the MMIO regions. Being local is better for performance because the END and NVT tables have a strong relation with the XIVE subengines using them (VC and PC).

Quoted 6 lines
Quoted 1 line

May be, Ben can clarified it this is badly explained.

Right.. I think I understand what the blocks are all about.

But my question is, why encode the block and index together for the EAS, but separately for the END?

mostly because when the PowerNV devices forward an notification event, they do so by writing the full IRQ number on a MMIO notify port and this number reaches the XiveRouter routing algo without being modified. On sPAPR it's even simpler.

But we could decode the full IRQ number in the router before querying the associated EAS. The EAS accessor would then use the same interface. I will look into it. Not a big change. I think.

Quoted 104 lines
Quoted 104 lines
Quoted 103 lines
Quoted 89 lines
include/hw/ppc/xive.h +14 -0
 } XiveRouterClass;
 
 void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
 
 int xive_router_get_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
 int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas);
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end);
+
+/*
+ * For legacy compatibility, the exceptions define up to 256 different
+ * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
+ * and the least favored level 0xFF.
+ */
+#define XIVE_PRIORITY_MAX  7
+
+void xive_end_reset(XiveEND *end);
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +48 -0
index 12499b33614c..f97fb2b90bee 100644
@@ -28,4 +28,52 @@ typedef struct XiveEAS {
 #define EAS_END_DATA    PPC_BITMASK(33, 63)      /* Data written to the END */
 } XiveEAS;
 
+/* Event Notification Descriptor (END) */
+typedef struct XiveEND {
+        uint32_t        w0;
+#define END_W0_VALID             PPC_BIT32(0) /* "v" bit */
+#define END_W0_ENQUEUE           PPC_BIT32(1) /* "q" bit */
+#define END_W0_UCOND_NOTIFY      PPC_BIT32(2) /* "n" bit */
+#define END_W0_BACKLOG           PPC_BIT32(3) /* "b" bit */
+#define END_W0_PRECL_ESC_CTL     PPC_BIT32(4) /* "p" bit */
+#define END_W0_ESCALATE_CTL      PPC_BIT32(5) /* "e" bit */
+#define END_W0_UNCOND_ESCALATE   PPC_BIT32(6) /* "u" bit - DD2.0 */
+#define END_W0_SILENT_ESCALATE   PPC_BIT32(7) /* "s" bit - DD2.0 */
+#define END_W0_QSIZE             PPC_BITMASK32(12, 15)
+#define END_W0_SW0               PPC_BIT32(16)
+#define END_W0_FIRMWARE          END_W0_SW0 /* Owned by FW */
+#define END_QSIZE_4K             0
+#define END_QSIZE_64K            4
+#define END_W0_HWDEP             PPC_BITMASK32(24, 31)
+        uint32_t        w1;
+#define END_W1_ESn               PPC_BITMASK32(0, 1)
+#define END_W1_ESn_P             PPC_BIT32(0)
+#define END_W1_ESn_Q             PPC_BIT32(1)
+#define END_W1_ESe               PPC_BITMASK32(2, 3)
+#define END_W1_ESe_P             PPC_BIT32(2)
+#define END_W1_ESe_Q             PPC_BIT32(3)
+#define END_W1_GENERATION        PPC_BIT32(9)
+#define END_W1_PAGE_OFF          PPC_BITMASK32(10, 31)
+        uint32_t        w2;
+#define END_W2_MIGRATION_REG     PPC_BITMASK32(0, 3)
+#define END_W2_OP_DESC_HI        PPC_BITMASK32(4, 31)
+        uint32_t        w3;
+#define END_W3_OP_DESC_LO        PPC_BITMASK32(0, 31)
+        uint32_t        w4;
+#define END_W4_ESC_END_BLOCK     PPC_BITMASK32(4, 7)
+#define END_W4_ESC_END_INDEX     PPC_BITMASK32(8, 31)
+        uint32_t        w5;
+#define END_W5_ESC_END_DATA      PPC_BITMASK32(1, 31)
+        uint32_t        w6;
+#define END_W6_FORMAT_BIT        PPC_BIT32(8)
+#define END_W6_NVT_BLOCK         PPC_BITMASK32(9, 12)
+#define END_W6_NVT_INDEX         PPC_BITMASK32(13, 31)
+        uint32_t        w7;
+#define END_W7_F0_IGNORE         PPC_BIT32(0)
+#define END_W7_F0_BLK_GROUPING   PPC_BIT32(1)
+#define END_W7_F0_PRIORITY       PPC_BITMASK32(8, 15)
+#define END_W7_F1_WAKEZ          PPC_BIT32(0)
+#define END_W7_F1_LOG_SERVER_ID  PPC_BITMASK32(1, 31)
+} XiveEND;
+
 #endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +0 -0
index c4c90a25758e..9cb001e7b540 100644
@@ -442,6 +442,101 @@ static const TypeInfo xive_source_info = {
     .class_init    = xive_source_class_init,
 };
 

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 454

Quoted 10 lines
hw/intc/xive.c +10 -0
+/*
+ * XiveEND helpers
+ */
+
+void xive_end_reset(XiveEND *end)
+{
+    memset(end, 0, sizeof(*end));
+
+    /* switch off the escalation and notification ESBs */
+    end->w1 = END_W1_ESe_Q | END_W1_ESn_Q;

It's not obvious to me what circumstances this would be called under. Since the ENDs are in system memory, a memset() seems like an odd thing for (virtual) hardware to be doing to it.

Quoted 8 lines
Quoted 3 lines

It makes sense on sPAPR if one day some OS starts using the END ESBs for further coalescing of the events. None does for now but I have added the model though.

Hrm, I think that belongs in PAPR specific code. It's not really part of the router model - it's the PAPR stuff configuring the router at reset time (much as firmware would configure it at reset time for bare metal).

This is true this routine is only used by the H_INT_RESET hcall and by the reset handler of the sPAPR controller model. But it made sense to put this END helper routine with the other END routines. Don't you think so ?

C.

Quoted 221 lines
Quoted 220 lines
Quoted 58 lines
Quoted 58 lines
hw/intc/xive.c +56 -0
+}
+
+static void xive_end_queue_pic_print_info(XiveEND *end, uint32_t width,
+                                          Monitor *mon)
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qentries = 1 << (qsize + 10);
+    int i;
+
+    /*
+     * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
+     */
+    monitor_printf(mon, " [ ");
+    qindex = (qindex - (width - 1)) & (qentries - 1);
+    for (i = 0; i < width; i++) {
+        uint64_t qaddr = qaddr_base + (qindex << 2);
+        uint32_t qdata = -1;
+
+        if (dma_memory_read(&address_space_memory, qaddr, &qdata,
+                            sizeof(qdata))) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
+                          HWADDR_PRIx "\n", qaddr);
+            return;
+        }
+        monitor_printf(mon, "%s%08x ", i == width - 1 ? "^" : "",
+                       be32_to_cpu(qdata));
+        qindex = (qindex + 1) & (qentries - 1);
+    }
+    monitor_printf(mon, "]\n");
+}
+
+void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon)
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qgen = GETFIELD(END_W1_GENERATION, end->w1);
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qentries = 1 << (qsize + 10);
+
+    uint32_t nvt = GETFIELD(END_W6_NVT_INDEX, end->w6);
+    uint8_t priority = GETFIELD(END_W7_F0_PRIORITY, end->w7);
+
+    if (!(end->w0 & END_W0_VALID)) {
+        return;
+    }
+
+    monitor_printf(mon, "  %08x %c%c%c%c%c prio:%d nvt:%04x eq:@%08"PRIx64
+                   "% 6d/%5d ^%d", end_idx,
+                   end->w0 & END_W0_VALID ? 'v' : '-',
+                   end->w0 & END_W0_ENQUEUE ? 'q' : '-',
+                   end->w0 & END_W0_UCOND_NOTIFY ? 'n' : '-',
+                   end->w0 & END_W0_BACKLOG ? 'b' : '-',
+                   end->w0 & END_W0_ESCALATE_CTL ? 'e' : '-',
+                   priority, nvt, qaddr_base, qindex, qentries, qgen);
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 515

Quoted 5 lines
hw/intc/xive.c +5 -0
+
+    xive_end_queue_pic_print_info(end, 6, mon);
+}
+
+static void xive_end_push(XiveEND *end, uint32_t data)

s/push/enqueue/ please, "push" suggests a stack. (Not to mention that "push" and "pull" are used as terms elsewhere in XIVE).

yes. you are right. I will change.

Quoted 114 lines
Quoted 114 lines
hw/intc/xive.c +100 -0
+{
+    uint64_t qaddr_base = (((uint64_t)(end->w2 & 0x0fffffff)) << 32) | end->w3;
+    uint32_t qsize = GETFIELD(END_W0_QSIZE, end->w0);
+    uint32_t qindex = GETFIELD(END_W1_PAGE_OFF, end->w1);
+    uint32_t qgen = GETFIELD(END_W1_GENERATION, end->w1);
+
+    uint64_t qaddr = qaddr_base + (qindex << 2);
+    uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
+    uint32_t qentries = 1 << (qsize + 10);
+
+    if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata))) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
+                      HWADDR_PRIx "\n", qaddr);
+        return;
+    }
+
+    qindex = (qindex + 1) & (qentries - 1);
+    if (qindex == 0) {
+        qgen ^= 1;
+        end->w1 = SETFIELD(END_W1_GENERATION, end->w1, qgen);
+    }
+    end->w1 = SETFIELD(END_W1_PAGE_OFF, end->w1, qindex);
+}
+
 /*
  * XIVE Router (aka. Virtualization Controller or IVRE)
  */
@@ -460,6 +555,82 @@ int xive_router_set_eas(XiveRouter *xrtr, uint32_t lisn, XiveEAS *eas)
     return xrc->set_eas(xrtr, lisn, eas);
 }
 
+int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->get_end(xrtr, end_blk, end_idx, end);
+}
+
+int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
+                        XiveEND *end)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->set_end(xrtr, end_blk, end_idx, end);
+}
+
+/*
+ * An END trigger can come from an event trigger (IPI or HW) or from
+ * another chip. We don't model the PowerBus but the END trigger
+ * message has the same parameters than in the function below.
+ */
+static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
+                                   uint32_t end_idx, uint32_t end_data)
+{
+    XiveEND end;
+    uint8_t priority;
+    uint8_t format;
+
+    /* END cache lookup */
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
+                      end_idx);
+        return;
+    }
+
+    if (!(end.w0 & END_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
+                      end_blk, end_idx);
+        return;
+    }
+
+    if (end.w0 & END_W0_ENQUEUE) {
+        xive_end_push(&end, end_data);
+        xive_router_set_end(xrtr, end_blk, end_idx, &end);
+    }
+
+    /*
+     * The W7 format depends on the F bit in W6. It defines the type
+     * of the notification :
+     *
+     *   F=0 : single or multiple NVT notification
+     *   F=1 : User level Event-Based Branch (EBB) notification, no
+     *         priority
+     */
+    format = GETFIELD(END_W6_FORMAT_BIT, end.w6);
+    priority = GETFIELD(END_W7_F0_PRIORITY, end.w7);
+
+    /* The END is masked */
+    if (format == 0 && priority == 0xff) {
+        return;
+    }
+
+    /*
+     * Check the END ESn (Event State Buffer for notification) for
+     * even futher coalescing in the Router
+     */
+    if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
+        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
+        return;
+    }
+
+    /*
+     * Follows IVPE notification
+     */
+}
+
 static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
 {
     XiveRouter *xrtr = XIVE_ROUTER(xf);
@@ -471,9 +642,9 @@ static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
         return;
     }
 
Quoted 10 lines

hw/intc/xive.c static void xive_router_notify(XiveFabric *xf, uint32_t lisn) · line 647

Quoted 6 lines
hw/intc/xive.c +3 -3
-    /* The IVRE has a State Bit Cache for its internal sources which
-     * is also involed at this point. We skip the SBC lookup because
-     * the state bits of the sources are modeled internally in QEMU.
+    /* The IVRE checks the State Bit Cache at this point. We skip the
+     * SBC lookup because the state bits of the sources are modeled
+     * internally in QEMU.

Replacing a comment about something we're not doing with a different comment about something we're not doing doesn't seem very useful. Maybe fold these together into one patch or the other.

That's me rephrasing. it should be indeed in the previous patch

Thanks,

C.

Quoted 19 lines
Quoted 18 lines
hw/intc/xive.c +8 -0
      */
 
     if (!(eas.w & EAS_VALID)) {
@@ -485,6 +656,14 @@ static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
         /* Notification completed */
         return;
     }
+
+    /*
+     * The event trigger becomes an END trigger
+     */
+    xive_router_end_notify(xrtr,
+                           GETFIELD(EAS_END_BLOCK, eas.w),
+                           GETFIELD(EAS_END_INDEX, eas.w),
+                           GETFIELD(EAS_END_DATA,  eas.w));
 }
 
 static Property xive_router_properties[] = {
#42 lore raw

Re: [Qemu-devel] [PATCH v5 05/36] ppc/xive: introduce the XIVE Event Notification Descriptors

Quoted 5 lines · Cédric Le Goater
Quoted 3 lines · David Gibson
Quoted 2 lines · Cédric Le Goater
Quoted 1 line · David Gibson

On Fri, Nov 16, 2018 at 11:56:58AM +0100, Cédric Le Goater

wrote:

[snip]

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
hw/intc/xive.c +6 -0
+/*
+ * XiveEND helpers
+ */
+
+void xive_end_reset(XiveEND *end)
+{
Quoted 8 lines

hw/intc/xive.c static const TypeInfo xive_source_info = { · line 454

Quoted 4 lines
hw/intc/xive.c +4 -0
+    memset(end, 0, sizeof(*end));
+
+    /* switch off the escalation and notification ESBs */
+    end->w1 = END_W1_ESe_Q | END_W1_ESn_Q;

It's not obvious to me what circumstances this would be called under. Since the ENDs are in system memory, a memset() seems like an odd thing for (virtual) hardware to be doing to it.

It makes sense on sPAPR if one day some OS starts using the END ESBs for further coalescing of the events. None does for now but I have added the model though.

Quoted 8 lines
Quoted 4 lines

Hrm, I think that belongs in PAPR specific code. It's not really part of the router model - it's the PAPR stuff configuring the router at reset time (much as firmware would configure it at reset time for bare metal).

This is true this routine is only used by the H_INT_RESET hcall and by the reset handler of the sPAPR controller model. But it made sense to put this END helper routine with the other END routines. Don't you think so ?

Actually, no. In real hardware this would be handled by a different component - the system firmware would do this setup of the ENDs as it configures the XIVE. So it makes sense to do this in a separate component for PAPR as well. In this case that's another piece of qemu (the spapr stuff) rather than being within the VM, but the difference isn't important to the END handling itself.

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
#43 lore raw

PATCH v5 6/36 ppc/xive: add support for the END Event State buffers

In reply to Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +20 -0
index ce62aaf28343..24301bf2076d 100644
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)
+
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +158 -2

178 lines not on this page · raw message

Signature · 1 line
2.17.2
#44 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 26 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 10 lines
include/hw/ppc/xive.h +7 -0
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Quoted 93 lines
include/hw/ppc/xive.h +12 -0
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +61 -2
index 9cb001e7b540..5a8882d47a98 100644
@@ -622,8 +622,18 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
      * even futher coalescing in the Router
      */
     if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
-        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
-        return;
+        uint8_t pq = GETFIELD(END_W1_ESn, end.w1);
+        bool notify = xive_esb_trigger(&pq);
+
+        if (pq != GETFIELD(END_W1_ESn, end.w1)) {
+            end.w1 = SETFIELD(END_W1_ESn, end.w1, pq);
+            xive_router_set_end(xrtr, end_blk, end_idx, &end);
+        }
+
+        /* ESn[Q]=1 : end of notification */
+        if (!notify) {
+            return;
+        }
     }
 
     /*
@@ -706,6 +716,151 @@ void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
                    (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
 }
 
+/*
+ * END ESB MMIO loads
+ */
+static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
+    XiveRouter *xrtr = xsrc->xrtr;
+    uint32_t offset = addr & 0xFFF;
+    uint8_t end_blk;
+    uint32_t end_idx;
+    XiveEND end;
+    uint32_t end_esmask;
+    uint8_t pq;
+    uint64_t ret = -1;
+
+    end_blk = xrtr->chip_id;
+    end_idx = addr >> (xsrc->esb_shift + 1);
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
+                      end_idx);
+        return -1;
+    }
+
+    if (!(end.w0 & END_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
+                      end_blk, end_idx);
+        return -1;
+    }
+
+    end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END_W1_ESn : END_W1_ESe;
+    pq = GETFIELD(end_esmask, end.w1);
+
+    switch (offset) {
+    case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
+        ret = xive_esb_eoi(&pq);
+
+        /* Forward the source event notification for routing ?? */
+        break;
+
+    case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
+        ret = pq;
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
+        break;

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 777

Quoted 10 lines
hw/intc/xive.c +10 -0
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
+                      offset);
+        return -1;
+    }
+
+    if (pq != GETFIELD(end_esmask, end.w1)) {
+        end.w1 = SETFIELD(end_esmask, end.w1, pq);
+        xive_router_set_end(xrtr, end_blk, end_idx, &end);
+    }

We can probably share some more code with XiveSource here, but that's something that can be refined later.

Quoted 97 lines
hw/intc/xive.c +87 -0
+
+    return ret;
+}
+
+/*
+ * END ESB MMIO stores are invalid
+ */
+static void xive_end_source_write(void *opaque, hwaddr addr,
+                                  uint64_t value, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr 0x%"
+                  HWADDR_PRIx"\n", addr);
+}
+
+static const MemoryRegionOps xive_end_source_ops = {
+    .read = xive_end_source_read,
+    .write = xive_end_source_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static void xive_end_source_realize(DeviceState *dev, Error **errp)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(dev);
+    Object *obj;
+    Error *local_err = NULL;
+
+    obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'xive' not found: ");
+        return;
+    }
+
+    xsrc->xrtr = XIVE_ROUTER(obj);
+
+    if (!xsrc->nr_ends) {
+        error_setg(errp, "Number of interrupt needs to be greater than 0");
+        return;
+    }
+
+    if (xsrc->esb_shift != XIVE_ESB_4K &&
+        xsrc->esb_shift != XIVE_ESB_64K) {
+        error_setg(errp, "Invalid ESB shift setting");
+        return;
+    }
+
+    /*
+     * Each END is assigned an even/odd pair of MMIO pages, the even page
+     * manages the ESn field while the odd page manages the ESe field.
+     */
+    memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
+                          &xive_end_source_ops, xsrc, "xive.end",
+                          (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &xsrc->esb_mmio);
+}
+
+static Property xive_end_source_properties[] = {
+    DEFINE_PROP_UINT32("nr-ends", XiveENDSource, nr_ends, 0),
+    DEFINE_PROP_UINT32("shift", XiveENDSource, esb_shift, XIVE_ESB_64K),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_end_source_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc    = "XIVE END Source";
+    dc->props   = xive_end_source_properties;
+    dc->realize = xive_end_source_realize;
+}
+
+static const TypeInfo xive_end_source_info = {
+    .name          = TYPE_XIVE_END_SOURCE,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XiveENDSource),
+    .class_init    = xive_end_source_class_init,
+};
+
 /*
  * XIVE Fabric
  */
@@ -720,6 +875,7 @@ static void xive_register_types(void)
     type_register_static(&xive_source_info);
     type_register_static(&xive_fabric_info);
     type_register_static(&xive_router_info);
+    type_register_static(&xive_end_source_info);
 }
 
 type_init(xive_register_types)
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
#45 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 30 lines · David Gibson
Quoted 29 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
Quoted 10 lines

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 7 lines
include/hw/ppc/xive.h +7 -0
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

yes, it should probably be under the XiveRouter you are right because there is a direct link with the ENDT which is in the XiverRouter.

But if I remove the chip_id field from the XiveRouter, it becomes a QOM interface. something to ponder.

Quoted 98 lines
Quoted 98 lines
include/hw/ppc/xive.h +12 -0
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +66 -2
index 9cb001e7b540..5a8882d47a98 100644
@@ -622,8 +622,18 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
      * even futher coalescing in the Router
      */
     if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
-        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
-        return;
+        uint8_t pq = GETFIELD(END_W1_ESn, end.w1);
+        bool notify = xive_esb_trigger(&pq);
+
+        if (pq != GETFIELD(END_W1_ESn, end.w1)) {
+            end.w1 = SETFIELD(END_W1_ESn, end.w1, pq);
+            xive_router_set_end(xrtr, end_blk, end_idx, &end);
+        }
+
+        /* ESn[Q]=1 : end of notification */
+        if (!notify) {
+            return;
+        }
     }
 
     /*
@@ -706,6 +716,151 @@ void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
                    (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
 }
 
+/*
+ * END ESB MMIO loads
+ */
+static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
+    XiveRouter *xrtr = xsrc->xrtr;
+    uint32_t offset = addr & 0xFFF;
+    uint8_t end_blk;
+    uint32_t end_idx;
+    XiveEND end;
+    uint32_t end_esmask;
+    uint8_t pq;
+    uint64_t ret = -1;
+
+    end_blk = xrtr->chip_id;
+    end_idx = addr >> (xsrc->esb_shift + 1);
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
+                      end_idx);
+        return -1;
+    }
+
+    if (!(end.w0 & END_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
+                      end_blk, end_idx);
+        return -1;
+    }
+
+    end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END_W1_ESn : END_W1_ESe;
+    pq = GETFIELD(end_esmask, end.w1);
+
+    switch (offset) {
+    case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
+        ret = xive_esb_eoi(&pq);
+
+        /* Forward the source event notification for routing ?? */
+        break;
+
+    case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
+        ret = pq;
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
+                      offset);
+        return -1;
+    }
Quoted 8 lines

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 777

Quoted 5 lines
hw/intc/xive.c +5 -0
+
+    if (pq != GETFIELD(end_esmask, end.w1)) {
+        end.w1 = SETFIELD(end_esmask, end.w1, pq);
+        xive_router_set_end(xrtr, end_blk, end_idx, &end);
+    }

We can probably share some more code with XiveSource here, but that's something that can be refined later.

yes clearly. The idea was to introduce a XiveESB model handling only the MMIO aspects and rely on an interface to query/modify the underlying PQ bits. These state bits are related to a device and the ESB pages are the XIVE way to expose them.

I left that for later. I didn't want to complexify more the XiveSource with a feature not used today.

Thanks,

C.

Quoted 99 lines
Quoted 98 lines
hw/intc/xive.c +87 -0
+
+    return ret;
+}
+
+/*
+ * END ESB MMIO stores are invalid
+ */
+static void xive_end_source_write(void *opaque, hwaddr addr,
+                                  uint64_t value, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr 0x%"
+                  HWADDR_PRIx"\n", addr);
+}
+
+static const MemoryRegionOps xive_end_source_ops = {
+    .read = xive_end_source_read,
+    .write = xive_end_source_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static void xive_end_source_realize(DeviceState *dev, Error **errp)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(dev);
+    Object *obj;
+    Error *local_err = NULL;
+
+    obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'xive' not found: ");
+        return;
+    }
+
+    xsrc->xrtr = XIVE_ROUTER(obj);
+
+    if (!xsrc->nr_ends) {
+        error_setg(errp, "Number of interrupt needs to be greater than 0");
+        return;
+    }
+
+    if (xsrc->esb_shift != XIVE_ESB_4K &&
+        xsrc->esb_shift != XIVE_ESB_64K) {
+        error_setg(errp, "Invalid ESB shift setting");
+        return;
+    }
+
+    /*
+     * Each END is assigned an even/odd pair of MMIO pages, the even page
+     * manages the ESn field while the odd page manages the ESe field.
+     */
+    memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
+                          &xive_end_source_ops, xsrc, "xive.end",
+                          (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &xsrc->esb_mmio);
+}
+
+static Property xive_end_source_properties[] = {
+    DEFINE_PROP_UINT32("nr-ends", XiveENDSource, nr_ends, 0),
+    DEFINE_PROP_UINT32("shift", XiveENDSource, esb_shift, XIVE_ESB_64K),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_end_source_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc    = "XIVE END Source";
+    dc->props   = xive_end_source_properties;
+    dc->realize = xive_end_source_realize;
+}
+
+static const TypeInfo xive_end_source_info = {
+    .name          = TYPE_XIVE_END_SOURCE,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XiveENDSource),
+    .class_init    = xive_end_source_class_init,
+};
+
 /*
  * XIVE Fabric
  */
@@ -720,6 +875,7 @@ static void xive_register_types(void)
     type_register_static(&xive_source_info);
     type_register_static(&xive_fabric_info);
     type_register_static(&xive_router_info);
+    type_register_static(&xive_end_source_info);
 }
 
 type_init(xive_register_types)
#46 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 42 lines · Cédric Le Goater
Quoted 30 lines · David Gibson
Quoted 29 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
Quoted 10 lines

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 7 lines
include/hw/ppc/xive.h +7 -0
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Quoted 5 lines

yes, it should probably be under the XiveRouter you are right because there is a direct link with the ENDT which is in the XiverRouter.

But if I remove the chip_id field from the XiveRouter, it becomes a QOM interface. something to ponder.

Huh? I really don't understand what you're saying here. What does chip_id have to do with anything?

Quoted 220 lines
Quoted 99 lines
Quoted 99 lines
include/hw/ppc/xive.h +12 -0
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +66 -2
index 9cb001e7b540..5a8882d47a98 100644
@@ -622,8 +622,18 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
      * even futher coalescing in the Router
      */
     if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
-        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
-        return;
+        uint8_t pq = GETFIELD(END_W1_ESn, end.w1);
+        bool notify = xive_esb_trigger(&pq);
+
+        if (pq != GETFIELD(END_W1_ESn, end.w1)) {
+            end.w1 = SETFIELD(END_W1_ESn, end.w1, pq);
+            xive_router_set_end(xrtr, end_blk, end_idx, &end);
+        }
+
+        /* ESn[Q]=1 : end of notification */
+        if (!notify) {
+            return;
+        }
     }
 
     /*
@@ -706,6 +716,151 @@ void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
                    (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
 }
 
+/*
+ * END ESB MMIO loads
+ */
+static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
+    XiveRouter *xrtr = xsrc->xrtr;
+    uint32_t offset = addr & 0xFFF;
+    uint8_t end_blk;
+    uint32_t end_idx;
+    XiveEND end;
+    uint32_t end_esmask;
+    uint8_t pq;
+    uint64_t ret = -1;
+
+    end_blk = xrtr->chip_id;
+    end_idx = addr >> (xsrc->esb_shift + 1);
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
+                      end_idx);
+        return -1;
+    }
+
+    if (!(end.w0 & END_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
+                      end_blk, end_idx);
+        return -1;
+    }
+
+    end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END_W1_ESn : END_W1_ESe;
+    pq = GETFIELD(end_esmask, end.w1);
+
+    switch (offset) {
+    case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
+        ret = xive_esb_eoi(&pq);
+
+        /* Forward the source event notification for routing ?? */
+        break;
+
+    case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
+        ret = pq;
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
+                      offset);
+        return -1;
+    }
Quoted 8 lines

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 777

Quoted 5 lines
hw/intc/xive.c +5 -0
+
+    if (pq != GETFIELD(end_esmask, end.w1)) {
+        end.w1 = SETFIELD(end_esmask, end.w1, pq);
+        xive_router_set_end(xrtr, end_blk, end_idx, &end);
+    }

We can probably share some more code with XiveSource here, but that's something that can be refined later.

yes clearly. The idea was to introduce a XiveESB model handling only the MMIO aspects and rely on an interface to query/modify the underlying PQ bits. These state bits are related to a device and the ESB pages are the XIVE way to expose them.

I left that for later. I didn't want to complexify more the XiveSource with a feature not used today.

Thanks,

C.

Quoted 99 lines
Quoted 98 lines
hw/intc/xive.c +87 -0
+
+    return ret;
+}
+
+/*
+ * END ESB MMIO stores are invalid
+ */
+static void xive_end_source_write(void *opaque, hwaddr addr,
+                                  uint64_t value, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr 0x%"
+                  HWADDR_PRIx"\n", addr);
+}
+
+static const MemoryRegionOps xive_end_source_ops = {
+    .read = xive_end_source_read,
+    .write = xive_end_source_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static void xive_end_source_realize(DeviceState *dev, Error **errp)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(dev);
+    Object *obj;
+    Error *local_err = NULL;
+
+    obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'xive' not found: ");
+        return;
+    }
+
+    xsrc->xrtr = XIVE_ROUTER(obj);
+
+    if (!xsrc->nr_ends) {
+        error_setg(errp, "Number of interrupt needs to be greater than 0");
+        return;
+    }
+
+    if (xsrc->esb_shift != XIVE_ESB_4K &&
+        xsrc->esb_shift != XIVE_ESB_64K) {
+        error_setg(errp, "Invalid ESB shift setting");
+        return;
+    }
+
+    /*
+     * Each END is assigned an even/odd pair of MMIO pages, the even page
+     * manages the ESn field while the odd page manages the ESe field.
+     */
+    memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
+                          &xive_end_source_ops, xsrc, "xive.end",
+                          (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &xsrc->esb_mmio);
+}
+
+static Property xive_end_source_properties[] = {
+    DEFINE_PROP_UINT32("nr-ends", XiveENDSource, nr_ends, 0),
+    DEFINE_PROP_UINT32("shift", XiveENDSource, esb_shift, XIVE_ESB_64K),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_end_source_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc    = "XIVE END Source";
+    dc->props   = xive_end_source_properties;
+    dc->realize = xive_end_source_realize;
+}
+
+static const TypeInfo xive_end_source_info = {
+    .name          = TYPE_XIVE_END_SOURCE,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XiveENDSource),
+    .class_init    = xive_end_source_class_init,
+};
+
 /*
  * XIVE Fabric
  */
@@ -720,6 +875,7 @@ static void xive_register_types(void)
     type_register_static(&xive_source_info);
     type_register_static(&xive_fabric_info);
     type_register_static(&xive_router_info);
+    type_register_static(&xive_end_source_info);
 }
 
 type_init(xive_register_types)
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
#47 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 43 lines · David Gibson
Quoted 42 lines · Cédric Le Goater
Quoted 40 lines · David Gibson
Quoted 26 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 10 lines
include/hw/ppc/xive.h +7 -0
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Quoted 8 lines
Quoted 5 lines

yes, it should probably be under the XiveRouter you are right because there is a direct link with the ENDT which is in the XiverRouter.

But if I remove the chip_id field from the XiveRouter, it becomes a QOM interface. something to ponder.

Huh? I really don't understand what you're saying here. What does chip_id have to do with anything?

I am quoting a comment of yours :

	> +/*
	> + * XIVE Router
	> + */
	> +
	> +typedef struct XiveRouter {
	> +    SysBusDevice    parent;
	> +
	> +    uint32_t        chip_id;

	I don't think this belongs in the base class.  The PowerNV specific
	variants will need it, but it doesn't make sense for the PAPR version.

If we remove 'chip_id' from XiveRouter, it can become a QOM interface without state, like the XiveFabric is.

C.

#48 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 68 lines · Cédric Le Goater
Quoted 43 lines · David Gibson
Quoted 42 lines · Cédric Le Goater
Quoted 40 lines · David Gibson
Quoted 26 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 10 lines
include/hw/ppc/xive.h +7 -0
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Quoted 8 lines
Quoted 5 lines

yes, it should probably be under the XiveRouter you are right because there is a direct link with the ENDT which is in the XiverRouter.

But if I remove the chip_id field from the XiveRouter, it becomes a QOM interface. something to ponder.

Huh? I really don't understand what you're saying here. What does chip_id have to do with anything?

I am quoting a comment of yours :

	> +/*
	> + * XIVE Router
	> + */
	> +
	> +typedef struct XiveRouter {
	> +    SysBusDevice    parent;
	> +
	> +    uint32_t        chip_id;

	I don't think this belongs in the base class.  The PowerNV specific
	variants will need it, but it doesn't make sense for the PAPR version.
Quoted 2 lines

If we remove 'chip_id' from XiveRouter, it can become a QOM interface without state, like the XiveFabric is.

Hm, not really. At this stage the object does't have any state, but at least the pnv variants will have state for the registers which point it to the EAST and ENDT, so a "real" object still makes more sense than an interface. If it were an interface it's not clear what real object it would be attached to.

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
#49 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

In reply to David Gibson
Quoted 30 lines · David Gibson
Quoted 29 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
Quoted 10 lines

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 7 lines
include/hw/ppc/xive.h +7 -0
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Coming back on this question because removing the chip_id from the router is a problem for the END triggering. At least with the current design. See below for the comment.

Quoted 54 lines
Quoted 54 lines
include/hw/ppc/xive.h +12 -0
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +22 -2
index 9cb001e7b540..5a8882d47a98 100644
@@ -622,8 +622,18 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
      * even futher coalescing in the Router
      */
     if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
-        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
-        return;
+        uint8_t pq = GETFIELD(END_W1_ESn, end.w1);
+        bool notify = xive_esb_trigger(&pq);
+
+        if (pq != GETFIELD(END_W1_ESn, end.w1)) {
+            end.w1 = SETFIELD(END_W1_ESn, end.w1, pq);
+            xive_router_set_end(xrtr, end_blk, end_idx, &end);
+        }
+
+        /* ESn[Q]=1 : end of notification */
+        if (!notify) {
+            return;
+        }
     }
 
     /*
@@ -706,6 +716,151 @@ void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
                    (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
 }
 
+/*
+ * END ESB MMIO loads
+ */
+static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
+    XiveRouter *xrtr = xsrc->xrtr;
+    uint32_t offset = addr & 0xFFF;
+    uint8_t end_blk;
+    uint32_t end_idx;
Quoted 8 lines

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 736

Quoted 8 lines
hw/intc/xive.c +8 -0
+    XiveEND end;
+    uint32_t end_esmask;
+    uint8_t pq;
+    uint64_t ret = -1;
+
+    end_blk = xrtr->chip_id;
+    end_idx = addr >> (xsrc->esb_shift + 1);
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {

The current END accessors require a block identifier, hence xrtr->chip_id, but in this case, we don't really need it because we are using the ENDT local to the router/chip.

I don't know how to handle simply this case without keeping chip_id :/

C.

Quoted 143 lines
Quoted 31 lines
hw/intc/xive.c +31 -0
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
+                      end_idx);
+        return -1;
+    }
+
+    if (!(end.w0 & END_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
+                      end_blk, end_idx);
+        return -1;
+    }
+
+    end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END_W1_ESn : END_W1_ESe;
+    pq = GETFIELD(end_esmask, end.w1);
+
+    switch (offset) {
+    case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
+        ret = xive_esb_eoi(&pq);
+
+        /* Forward the source event notification for routing ?? */
+        break;
+
+    case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
+        ret = pq;
+        break;
+
+    case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
+    case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
+    case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
+    case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
+        ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
+        break;

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 777

Quoted 10 lines
hw/intc/xive.c +10 -0
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
+                      offset);
+        return -1;
+    }
+
+    if (pq != GETFIELD(end_esmask, end.w1)) {
+        end.w1 = SETFIELD(end_esmask, end.w1, pq);
+        xive_router_set_end(xrtr, end_blk, end_idx, &end);
+    }

We can probably share some more code with XiveSource here, but that's something that can be refined later.

Quoted 97 lines
hw/intc/xive.c +87 -0
+
+    return ret;
+}
+
+/*
+ * END ESB MMIO stores are invalid
+ */
+static void xive_end_source_write(void *opaque, hwaddr addr,
+                                  uint64_t value, unsigned size)
+{
+    qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr 0x%"
+                  HWADDR_PRIx"\n", addr);
+}
+
+static const MemoryRegionOps xive_end_source_ops = {
+    .read = xive_end_source_read,
+    .write = xive_end_source_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 8,
+        .max_access_size = 8,
+    },
+};
+
+static void xive_end_source_realize(DeviceState *dev, Error **errp)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(dev);
+    Object *obj;
+    Error *local_err = NULL;
+
+    obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'xive' not found: ");
+        return;
+    }
+
+    xsrc->xrtr = XIVE_ROUTER(obj);
+
+    if (!xsrc->nr_ends) {
+        error_setg(errp, "Number of interrupt needs to be greater than 0");
+        return;
+    }
+
+    if (xsrc->esb_shift != XIVE_ESB_4K &&
+        xsrc->esb_shift != XIVE_ESB_64K) {
+        error_setg(errp, "Invalid ESB shift setting");
+        return;
+    }
+
+    /*
+     * Each END is assigned an even/odd pair of MMIO pages, the even page
+     * manages the ESn field while the odd page manages the ESe field.
+     */
+    memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
+                          &xive_end_source_ops, xsrc, "xive.end",
+                          (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
+    sysbus_init_mmio(SYS_BUS_DEVICE(dev), &xsrc->esb_mmio);
+}
+
+static Property xive_end_source_properties[] = {
+    DEFINE_PROP_UINT32("nr-ends", XiveENDSource, nr_ends, 0),
+    DEFINE_PROP_UINT32("shift", XiveENDSource, esb_shift, XIVE_ESB_64K),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void xive_end_source_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->desc    = "XIVE END Source";
+    dc->props   = xive_end_source_properties;
+    dc->realize = xive_end_source_realize;
+}
+
+static const TypeInfo xive_end_source_info = {
+    .name          = TYPE_XIVE_END_SOURCE,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(XiveENDSource),
+    .class_init    = xive_end_source_class_init,
+};
+
 /*
  * XIVE Fabric
  */
@@ -720,6 +875,7 @@ static void xive_register_types(void)
     type_register_static(&xive_source_info);
     type_register_static(&xive_fabric_info);
     type_register_static(&xive_router_info);
+    type_register_static(&xive_end_source_info);
 }
 
 type_init(xive_register_types)
#50 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 113 lines · Cédric Le Goater
Quoted 30 lines · David Gibson
Quoted 29 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
Quoted 10 lines

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 7 lines
include/hw/ppc/xive.h +7 -0
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Coming back on this question because removing the chip_id from the router is a problem for the END triggering. At least with the current design. See below for the comment.

Quoted 54 lines
Quoted 54 lines
include/hw/ppc/xive.h +12 -0
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +22 -2
index 9cb001e7b540..5a8882d47a98 100644
@@ -622,8 +622,18 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
      * even futher coalescing in the Router
      */
     if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
-        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
-        return;
+        uint8_t pq = GETFIELD(END_W1_ESn, end.w1);
+        bool notify = xive_esb_trigger(&pq);
+
+        if (pq != GETFIELD(END_W1_ESn, end.w1)) {
+            end.w1 = SETFIELD(END_W1_ESn, end.w1, pq);
+            xive_router_set_end(xrtr, end_blk, end_idx, &end);
+        }
+
+        /* ESn[Q]=1 : end of notification */
+        if (!notify) {
+            return;
+        }
     }
 
     /*
@@ -706,6 +716,151 @@ void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
                    (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
 }
 
+/*
+ * END ESB MMIO loads
+ */
+static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
+    XiveRouter *xrtr = xsrc->xrtr;
+    uint32_t offset = addr & 0xFFF;
+    uint8_t end_blk;
+    uint32_t end_idx;
Quoted 8 lines

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 736

Quoted 8 lines
hw/intc/xive.c +8 -0
+    XiveEND end;
+    uint32_t end_esmask;
+    uint8_t pq;
+    uint64_t ret = -1;
+
+    end_blk = xrtr->chip_id;
+    end_idx = addr >> (xsrc->esb_shift + 1);
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {

The current END accessors require a block identifier, hence xrtr->chip_id, but in this case, we don't really need it because we are using the ENDT local to the router/chip.

Quoted 1 line

I don't know how to handle simply this case without keeping chip_id :/

I don't really follow how chip_id is relevant here. AFAICT the END accessors take a block id and the back end is responsible for interpreting them. The ponwernv one will map it to chip id, but the PAPR one can just ignore it or only use block 0.

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
#51 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 114 lines · David Gibson
Quoted 112 lines · Cédric Le Goater
Quoted 30 lines · David Gibson
Quoted 29 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
Quoted 10 lines

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 7 lines
include/hw/ppc/xive.h +7 -0
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Coming back on this question because removing the chip_id from the router is a problem for the END triggering. At least with the current design. See below for the comment.

Quoted 54 lines
Quoted 54 lines
include/hw/ppc/xive.h +12 -0
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +22 -2
index 9cb001e7b540..5a8882d47a98 100644
@@ -622,8 +622,18 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
      * even futher coalescing in the Router
      */
     if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
-        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
-        return;
+        uint8_t pq = GETFIELD(END_W1_ESn, end.w1);
+        bool notify = xive_esb_trigger(&pq);
+
+        if (pq != GETFIELD(END_W1_ESn, end.w1)) {
+            end.w1 = SETFIELD(END_W1_ESn, end.w1, pq);
+            xive_router_set_end(xrtr, end_blk, end_idx, &end);
+        }
+
+        /* ESn[Q]=1 : end of notification */
+        if (!notify) {
+            return;
+        }
     }
 
     /*
@@ -706,6 +716,151 @@ void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
                    (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
 }
 
+/*
+ * END ESB MMIO loads
+ */
+static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
+    XiveRouter *xrtr = xsrc->xrtr;
+    uint32_t offset = addr & 0xFFF;
+    uint8_t end_blk;
+    uint32_t end_idx;
Quoted 8 lines

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 736

Quoted 8 lines
hw/intc/xive.c +8 -0
+    XiveEND end;
+    uint32_t end_esmask;
+    uint8_t pq;
+    uint64_t ret = -1;
+
+    end_blk = xrtr->chip_id;
+    end_idx = addr >> (xsrc->esb_shift + 1);
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {

The current END accessors require a block identifier, hence xrtr->chip_id, but in this case, we don't really need it because we are using the ENDT local to the router/chip.

Quoted 6 lines
Quoted 1 line

I don't know how to handle simply this case without keeping chip_id :/

I don't really follow how chip_id is relevant here. AFAICT the END accessors take a block id and the back end is responsible for interpreting them. The ponwernv one will map it to chip id, but the PAPR one can just ignore it or only use block 0.

Yes. But the block value comes from the xrtr->chip_id today, on PAPR and PowerNV, even if it's block 0.

What I could do is add a "chip-id" property to XiveENDSource possibly.

C.

#52 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 122 lines · Cédric Le Goater
Quoted 114 lines · David Gibson
Quoted 112 lines · Cédric Le Goater
Quoted 30 lines · David Gibson
Quoted 29 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
Quoted 10 lines

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 7 lines
include/hw/ppc/xive.h +7 -0
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Coming back on this question because removing the chip_id from the router is a problem for the END triggering. At least with the current design. See below for the comment.

Quoted 54 lines
Quoted 54 lines
include/hw/ppc/xive.h +12 -0
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +22 -2
index 9cb001e7b540..5a8882d47a98 100644
@@ -622,8 +622,18 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
      * even futher coalescing in the Router
      */
     if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
-        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
-        return;
+        uint8_t pq = GETFIELD(END_W1_ESn, end.w1);
+        bool notify = xive_esb_trigger(&pq);
+
+        if (pq != GETFIELD(END_W1_ESn, end.w1)) {
+            end.w1 = SETFIELD(END_W1_ESn, end.w1, pq);
+            xive_router_set_end(xrtr, end_blk, end_idx, &end);
+        }
+
+        /* ESn[Q]=1 : end of notification */
+        if (!notify) {
+            return;
+        }
     }
 
     /*
@@ -706,6 +716,151 @@ void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
                    (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
 }
 
+/*
+ * END ESB MMIO loads
+ */
+static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
+    XiveRouter *xrtr = xsrc->xrtr;
+    uint32_t offset = addr & 0xFFF;
+    uint8_t end_blk;
+    uint32_t end_idx;
Quoted 8 lines

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 736

Quoted 8 lines
hw/intc/xive.c +8 -0
+    XiveEND end;
+    uint32_t end_esmask;
+    uint8_t pq;
+    uint64_t ret = -1;
+
+    end_blk = xrtr->chip_id;
+    end_idx = addr >> (xsrc->esb_shift + 1);
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {

The current END accessors require a block identifier, hence xrtr->chip_id, but in this case, we don't really need it because we are using the ENDT local to the router/chip.

Quoted 6 lines
Quoted 1 line

I don't know how to handle simply this case without keeping chip_id :/

I don't really follow how chip_id is relevant here. AFAICT the END accessors take a block id and the back end is responsible for interpreting them. The ponwernv one will map it to chip id, but the PAPR one can just ignore it or only use block 0.

Quoted 4 lines

Yes. But the block value comes from the xrtr->chip_id today, on PAPR and PowerNV, even if it's block 0.

What I could do is add a "chip-id" property to XiveENDSource possibly.

This still seems wrong for the PAPR model. Why can't you configure the end_block value directly in the Xive components, then just set it equal to the chip_id when you build the powernv machine?

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
#53 lore raw

Re: [Qemu-devel] [PATCH v5 06/36] ppc/xive: add support for the END Event State buffers

Quoted 123 lines · David Gibson
Quoted 122 lines · Cédric Le Goater
Quoted 120 lines · David Gibson
Quoted 113 lines · Cédric Le Goater
Quoted 30 lines · David Gibson
Quoted 29 lines · Cédric Le Goater

The Event Notification Descriptor also contains two Event State Buffers providing further coalescing of interrupts, one for the notification event (ESn) and one for the escalation events (ESe). A MMIO page is assigned for each to control the EOI through loads only. Stores are not allowed.

The END ESBs are modeled through an object resembling the 'XiveSource' It is stateless as the END state bits are backed into the XiveEND structure under the XiveRouter and the MMIO accesses follow the same rules as for the standard source ESBs.

END ESBs are not supported by the Linux drivers neither on OPAL nor on sPAPR. Nevetherless, it provides a mean to study the question in the future and validates a bit more the XIVE model.

---
 include/hw/ppc/xive.h |  20 ++++++
 hw/intc/xive.c        | 160 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 178 insertions(+), 2 deletions(-)
include/hw/ppc/xive.h +0 -0
index ce62aaf28343..24301bf2076d 100644
@@ -208,6 +208,26 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 
Quoted 10 lines

include/hw/ppc/xive.h int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 217

Quoted 7 lines
include/hw/ppc/xive.h +7 -0
+/*
+ * XIVE END ESBs
+ */
+
+#define TYPE_XIVE_END_SOURCE "xive-end-source"
+#define XIVE_END_SOURCE(obj) \
+    OBJECT_CHECK(XiveENDSource, (obj), TYPE_XIVE_END_SOURCE)

Is there a particular reason to make this a full QOM object, rather than just embedding it in the XiveRouter?

Coming back on this question because removing the chip_id from the router is a problem for the END triggering. At least with the current design. See below for the comment.

Quoted 54 lines
Quoted 54 lines
include/hw/ppc/xive.h +12 -0
+typedef struct XiveENDSource {
+    SysBusDevice parent;
+
+    uint32_t        nr_ends;
+
+    /* ESB memory region */
+    uint32_t        esb_shift;
+    MemoryRegion    esb_mmio;
+
+    XiveRouter      *xrtr;
+} XiveENDSource;
+
 /*
  * For legacy compatibility, the exceptions define up to 256 different
  * priorities. P9 implements only 9 levels : 8 active levels [0 - 7]
hw/intc/xive.c +22 -2
index 9cb001e7b540..5a8882d47a98 100644
@@ -622,8 +622,18 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
      * even futher coalescing in the Router
      */
     if (!(end.w0 & END_W0_UCOND_NOTIFY)) {
-        qemu_log_mask(LOG_UNIMP, "XIVE: !UCOND_NOTIFY not implemented\n");
-        return;
+        uint8_t pq = GETFIELD(END_W1_ESn, end.w1);
+        bool notify = xive_esb_trigger(&pq);
+
+        if (pq != GETFIELD(END_W1_ESn, end.w1)) {
+            end.w1 = SETFIELD(END_W1_ESn, end.w1, pq);
+            xive_router_set_end(xrtr, end_blk, end_idx, &end);
+        }
+
+        /* ESn[Q]=1 : end of notification */
+        if (!notify) {
+            return;
+        }
     }
 
     /*
@@ -706,6 +716,151 @@ void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
                    (uint32_t) GETFIELD(EAS_END_DATA, eas->w));
 }
 
+/*
+ * END ESB MMIO loads
+ */
+static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
+{
+    XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
+    XiveRouter *xrtr = xsrc->xrtr;
+    uint32_t offset = addr & 0xFFF;
+    uint8_t end_blk;
+    uint32_t end_idx;
Quoted 8 lines

hw/intc/xive.c void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon) · line 736

Quoted 8 lines
hw/intc/xive.c +8 -0
+    XiveEND end;
+    uint32_t end_esmask;
+    uint8_t pq;
+    uint64_t ret = -1;
+
+    end_blk = xrtr->chip_id;
+    end_idx = addr >> (xsrc->esb_shift + 1);
+    if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {

The current END accessors require a block identifier, hence xrtr->chip_id, but in this case, we don't really need it because we are using the ENDT local to the router/chip.

Quoted 1 line

I don't know how to handle simply this case without keeping chip_id :/

I don't really follow how chip_id is relevant here. AFAICT the END accessors take a block id and the back end is responsible for interpreting them. The ponwernv one will map it to chip id, but the PAPR one can just ignore it or only use block 0.

Quoted 6 lines
Quoted 4 lines

Yes. But the block value comes from the xrtr->chip_id today, on PAPR and PowerNV, even if it's block 0.

What I could do is add a "chip-id" property to XiveENDSource possibly.

This still seems wrong for the PAPR model.

We don't really care for PAPR. We can use 0 but the model is common with PowerNV.

Quoted 2 lines

Why can't you configure the end_block value directly in the Xive components,

That is what I am proposing to add a "chip-id" property to XiveENDSource

Quoted 2 lines

then just set it equal to the chip_id when you build the powernv machine?

yes. that's how it's more or less built. It's a little more complex on PowerNV because there are low level settings on how the chip_id is used in the PC and VC, but the modeling is minimal.

C.

#54 lore raw

PATCH v5 7/36 ppc/xive: introduce the XIVE interrupt thread context

In reply to Cédric Le Goater

Each POWER9 processor chip has a XIVE presenter that can generate four different exceptions to its threads:

- hypervisor exception,
- O/S exception
- Event-Based Branch (EBB)
- msgsnd (doorbell).

Each exception has a state independent from the others called a Thread Interrupt Management context. This context is a set of registers which lets the thread handle priority management and interrupt acknowledgment among other things. The most important ones being :

  - Interrupt Priority Register  (PIPR)
  - Interrupt Pending Buffer     (IPB)
  - Current Processor Priority   (CPPR)

- Notification Source Register (NSR)

These registers are accessible through a specific MMIO region, called the Thread Interrupt Management Area (TIMA), four aligned pages, each exposing a different view of the registers. First page (page address ending in 0b00) gives access to the entire context and is reserved for the ring 0 security monitor. The second (page address ending in 0b01) is for the hypervisor, ring 1. The third (page address ending in 0b10) is for the operating system, ring 2. The fourth (page address ending in 0b11) is for user level, ring 3.

The thread interrupt context is modeled with a XiveTCTX object containing the values of the different exception registers. The TIMA region is mapped at the same address for each CPU.

---
 include/hw/ppc/xive.h      |  36 +++
 include/hw/ppc/xive_regs.h |  82 +++++++
 hw/intc/xive.c             | 443 +++++++++++++++++++++++++++++++++++++
 3 files changed, 561 insertions(+)
include/hw/ppc/xive.h +36 -0
index 24301bf2076d..5987f26ddb98 100644
@@ -238,4 +238,40 @@ typedef struct XiveENDSource {
 void xive_end_reset(XiveEND *end);
 void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
+/*
+ * XIVE Thread interrupt Management (TM) context
+ */
+
+#define TYPE_XIVE_TCTX "xive-tctx"
+#define XIVE_TCTX(obj) OBJECT_CHECK(XiveTCTX, (obj), TYPE_XIVE_TCTX)
+
+/*
+ * XIVE Thread interrupt Management register rings :
+ *
+ *   QW-0  User       event-based exception state
+ *   QW-1  O/S        OS context for priority management, interrupt acks
+ *   QW-2  Pool       hypervisor context for virtual processor being dispatched
+ *   QW-3  Physical   for the security monitor to manage the entire context
+ */
+#define TM_RING_COUNT           4
+#define TM_RING_SIZE            0x10
+
+typedef struct XiveTCTX {
+    DeviceState parent_obj;
+
+    CPUState    *cs;
+    qemu_irq    output;
+
+    uint8_t     regs[TM_RING_COUNT * TM_RING_SIZE];
+
+    XiveRouter  *xrtr;
+} XiveTCTX;
+
+/*
+ * XIVE Thread Interrupt Management Aera (TIMA)
+ */
+extern const MemoryRegionOps xive_tm_ops;
+
+void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon);
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +82 -0

88 lines not on this page · raw message

hw/intc/xive.c +443 -0

455 lines not on this page · raw message

Signature · 1 line
2.17.2
#55 lore raw

Re: [Qemu-devel] [PATCH v5 07/36] ppc/xive: introduce the XIVE interrupt thread context

Quoted 50 lines · Cédric Le Goater

Each POWER9 processor chip has a XIVE presenter that can generate four different exceptions to its threads:

- hypervisor exception,
- O/S exception
- Event-Based Branch (EBB)
- msgsnd (doorbell).

Each exception has a state independent from the others called a Thread Interrupt Management context. This context is a set of registers which lets the thread handle priority management and interrupt acknowledgment among other things. The most important ones being :

  - Interrupt Priority Register  (PIPR)
  - Interrupt Pending Buffer     (IPB)
  - Current Processor Priority   (CPPR)

- Notification Source Register (NSR)

These registers are accessible through a specific MMIO region, called the Thread Interrupt Management Area (TIMA), four aligned pages, each exposing a different view of the registers. First page (page address ending in 0b00) gives access to the entire context and is reserved for the ring 0 security monitor. The second (page address ending in 0b01) is for the hypervisor, ring 1. The third (page address ending in 0b10) is for the operating system, ring 2. The fourth (page address ending in 0b11) is for user level, ring 3.

The thread interrupt context is modeled with a XiveTCTX object containing the values of the different exception registers. The TIMA region is mapped at the same address for each CPU.

---
 include/hw/ppc/xive.h      |  36 +++
 include/hw/ppc/xive_regs.h |  82 +++++++
 hw/intc/xive.c             | 443 +++++++++++++++++++++++++++++++++++++
 3 files changed, 561 insertions(+)
include/hw/ppc/xive.h +4 -0
index 24301bf2076d..5987f26ddb98 100644
@@ -238,4 +238,40 @@ typedef struct XiveENDSource {
 void xive_end_reset(XiveEND *end);
 void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
+/*
+ * XIVE Thread interrupt Management (TM) context
+ */
+

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 254

Quoted 10 lines
include/hw/ppc/xive.h +10 -0
+#define TYPE_XIVE_TCTX "xive-tctx"
+#define XIVE_TCTX(obj) OBJECT_CHECK(XiveTCTX, (obj), TYPE_XIVE_TCTX)
+
+/*
+ * XIVE Thread interrupt Management register rings :
+ *
+ *   QW-0  User       event-based exception state
+ *   QW-1  O/S        OS context for priority management, interrupt acks
+ *   QW-2  Pool       hypervisor context for virtual processor being dispatched
+ *   QW-3  Physical   for the security monitor to manage the entire context

That last description is misleading, AIUI the hypervisor can and does make use of the physical ring as well as the pool ring.

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 265

Quoted 11 lines
include/hw/ppc/xive.h +11 -0
+ */
+#define TM_RING_COUNT           4
+#define TM_RING_SIZE            0x10
+
+typedef struct XiveTCTX {
+    DeviceState parent_obj;
+
+    CPUState    *cs;
+    qemu_irq    output;
+
+    uint8_t     regs[TM_RING_COUNT * TM_RING_SIZE];

I'm a bit dubious about representing the state with a full buffer like this. Isn't a fair bit of this space reserved or derived values which aren't backed by real state?

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 267

Quoted 2 lines
include/hw/ppc/xive.h +2 -0
+
+    XiveRouter  *xrtr;

What's this for? AFAIK a TCTX isn't associated with a particular routing unit.

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 271

Quoted 4 lines
include/hw/ppc/xive.h +4 -0
+} XiveTCTX;
+
+/*
+ * XIVE Thread Interrupt Management Aera (TIMA)

Typo s/Aera/Area/

Quoted 149 lines
include/hw/ppc/xive.h +5 -0
+ */
+extern const MemoryRegionOps xive_tm_ops;
+
+void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon);
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +82 -0
index f97fb2b90bee..2e3d6cb507da 100644
@@ -10,6 +10,88 @@
 #ifndef PPC_XIVE_REGS_H
 #define PPC_XIVE_REGS_H
 
+#define TM_SHIFT                16
+
+/* TM register offsets */
+#define TM_QW0_USER             0x000 /* All rings */
+#define TM_QW1_OS               0x010 /* Ring 0..2 */
+#define TM_QW2_HV_POOL          0x020 /* Ring 0..1 */
+#define TM_QW3_HV_PHYS          0x030 /* Ring 0..1 */
+
+/* Byte offsets inside a QW             QW0 QW1 QW2 QW3 */
+#define TM_NSR                  0x0  /*  +   +   -   +  */
+#define TM_CPPR                 0x1  /*  -   +   -   +  */
+#define TM_IPB                  0x2  /*  -   +   +   +  */
+#define TM_LSMFB                0x3  /*  -   +   +   +  */
+#define TM_ACK_CNT              0x4  /*  -   +   -   -  */
+#define TM_INC                  0x5  /*  -   +   -   +  */
+#define TM_AGE                  0x6  /*  -   +   -   +  */
+#define TM_PIPR                 0x7  /*  -   +   -   +  */
+
+#define TM_WORD0                0x0
+#define TM_WORD1                0x4
+
+/*
+ * QW word 2 contains the valid bit at the top and other fields
+ * depending on the QW.
+ */
+#define TM_WORD2                0x8
+#define   TM_QW0W2_VU           PPC_BIT32(0)
+#define   TM_QW0W2_LOGIC_SERV   PPC_BITMASK32(1, 31) /* XX 2,31 ? */
+#define   TM_QW1W2_VO           PPC_BIT32(0)
+#define   TM_QW1W2_OS_CAM       PPC_BITMASK32(8, 31)
+#define   TM_QW2W2_VP           PPC_BIT32(0)
+#define   TM_QW2W2_POOL_CAM     PPC_BITMASK32(8, 31)
+#define   TM_QW3W2_VT           PPC_BIT32(0)
+#define   TM_QW3W2_LP           PPC_BIT32(6)
+#define   TM_QW3W2_LE           PPC_BIT32(7)
+#define   TM_QW3W2_T            PPC_BIT32(31)
+
+/*
+ * In addition to normal loads to "peek" and writes (only when invalid)
+ * using 4 and 8 bytes accesses, the above registers support these
+ * "special" byte operations:
+ *
+ *   - Byte load from QW0[NSR] - User level NSR (EBB)
+ *   - Byte store to QW0[NSR] - User level NSR (EBB)
+ *   - Byte load/store to QW1[CPPR] and QW3[CPPR] - CPPR access
+ *   - Byte load from QW3[TM_WORD2] - Read VT||00000||LP||LE on thrd 0
+ *                                    otherwise VT||0000000
+ *   - Byte store to QW3[TM_WORD2] - Set VT bit (and LP/LE if present)
+ *
+ * Then we have all these "special" CI ops at these offset that trigger
+ * all sorts of side effects:
+ */
+#define TM_SPC_ACK_EBB          0x800   /* Load8 ack EBB to reg*/
+#define TM_SPC_ACK_OS_REG       0x810   /* Load16 ack OS irq to reg */
+#define TM_SPC_PUSH_USR_CTX     0x808   /* Store32 Push/Validate user context */
+#define TM_SPC_PULL_USR_CTX     0x808   /* Load32 Pull/Invalidate user
+                                         * context */
+#define TM_SPC_SET_OS_PENDING   0x812   /* Store8 Set OS irq pending bit */
+#define TM_SPC_PULL_OS_CTX      0x818   /* Load32/Load64 Pull/Invalidate OS
+                                         * context to reg */
+#define TM_SPC_PULL_POOL_CTX    0x828   /* Load32/Load64 Pull/Invalidate Pool
+                                         * context to reg*/
+#define TM_SPC_ACK_HV_REG       0x830   /* Load16 ack HV irq to reg */
+#define TM_SPC_PULL_USR_CTX_OL  0xc08   /* Store8 Pull/Inval usr ctx to odd
+                                         * line */
+#define TM_SPC_ACK_OS_EL        0xc10   /* Store8 ack OS irq to even line */
+#define TM_SPC_ACK_HV_POOL_EL   0xc20   /* Store8 ack HV evt pool to even
+                                         * line */
+#define TM_SPC_ACK_HV_EL        0xc30   /* Store8 ack HV irq to even line */
+/* XXX more... */
+
+/* NSR fields for the various QW ack types */
+#define TM_QW0_NSR_EB           PPC_BIT8(0)
+#define TM_QW1_NSR_EO           PPC_BIT8(0)
+#define TM_QW3_NSR_HE           PPC_BITMASK8(0, 1)
+#define  TM_QW3_NSR_HE_NONE     0
+#define  TM_QW3_NSR_HE_POOL     1
+#define  TM_QW3_NSR_HE_PHYS     2
+#define  TM_QW3_NSR_HE_LSI      3
+#define TM_QW3_NSR_I            PPC_BIT8(2)
+#define TM_QW3_NSR_GRP_LVL      PPC_BIT8(3, 7)
+
 /* EAS (Event Assignment Structure)
  *
  * One per interrupt source. Targets an interrupt to a given Event
hw/intc/xive.c +42 -0
index 5a8882d47a98..4c6cb5d52975 100644
@@ -15,6 +15,448 @@
 #include "sysemu/dma.h"
 #include "monitor/monitor.h"
 #include "hw/ppc/xive.h"
+#include "hw/ppc/xive_regs.h"
+
+/*
+ * XIVE Thread Interrupt Management context
+ */
+
+static uint64_t xive_tctx_accept(XiveTCTX *tctx, uint8_t ring)
+{
+    return 0;
+}
+
+static void xive_tctx_set_cppr(XiveTCTX *tctx, uint8_t ring, uint8_t cppr)
+{
+    if (cppr > XIVE_PRIORITY_MAX) {
+        cppr = 0xff;
+    }
+
+    tctx->regs[ring + TM_CPPR] = cppr;
+}
+
+/*
+ * XIVE Thread Interrupt Management Area (TIMA)
+ *
+ * This region gives access to the registers of the thread interrupt
+ * management context. It is four page wide, each page providing a
+ * different view of the registers. The page with the lower offset is
+ * the most privileged and gives access to the entire context.
+ */
+
+#define XIVE_TM_HW_PAGE   0x0
+#define XIVE_TM_HV_PAGE   0x1
+#define XIVE_TM_OS_PAGE   0x2
+#define XIVE_TM_USER_PAGE 0x3
+
+/*
+ * Define an access map for each page of the TIMA that we will use in
+ * the memory region ops to filter values when doing loads and stores
+ * of raw registers values
+ *
+ * Registers accessibility bits :
+ *
+ *    0x0 - no access

hw/intc/xive.c line 69

Quoted 10 lines
hw/intc/xive.c +10 -0
+ *    0x1 - write only
+ *    0x2 - read only
+ *    0x3 - read/write
+ */
+
+static const uint8_t xive_tm_hw_view[] = {
+    /* QW-0 User */   3, 0, 0, 0,   0, 0, 0, 0,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-1 OS   */   3, 3, 3, 3,   3, 3, 0, 3,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-2 HV   */   0, 0, 3, 3,   0, 0, 0, 0,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-3 HW   */   3, 3, 3, 3,   0, 3, 0, 3,   3, 0, 0, 3,   3, 3, 3, 0,

Can we stick to the "Pool" / "Phys" names rather than inventing HV and HW. XIVE already has too many names for things. To clarify that's for the naming of the QWs, the view names are fine.

Quoted 4 lines
hw/intc/xive.c +4 -0
+};
+
+static const uint8_t xive_tm_hv_view[] = {
+    /* QW-0 User */   3, 0, 0, 0,   0, 0, 0, 0,   3, 3, 3, 3,   0, 0, 0, 0,

hw/intc/xive.c line 83

Quoted 10 lines
hw/intc/xive.c +10 -0
+    /* QW-1 OS   */   3, 3, 3, 3,   3, 3, 0, 3,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-2 HV   */   0, 0, 3, 3,   0, 0, 0, 0,   0, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-3 HW   */   3, 3, 3, 3,   0, 3, 0, 3,   3, 0, 0, 3,   0, 0, 0, 0,
+};
+
+static const uint8_t xive_tm_os_view[] = {
+    /* QW-0 User */   3, 0, 0, 0,   0, 0, 0, 0,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-1 OS   */   2, 3, 2, 2,   2, 2, 0, 2,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-2 HV   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-3 HW   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 3, 3, 0,

Are those bytes near the end of QW-3 really accessible in OS but not hypervisor view?

Quoted 264 lines
hw/intc/xive.c +264 -0
+};
+
+static const uint8_t xive_tm_user_view[] = {
+    /* QW-0 User */   3, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-1 OS   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-2 HV   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-3 HW   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+};
+
+/*
+ * Overall TIMA access map for the thread interrupt management context
+ * registers
+ */
+static const uint8_t *xive_tm_views[] = {
+    [XIVE_TM_HW_PAGE]   = xive_tm_hw_view,
+    [XIVE_TM_HV_PAGE]   = xive_tm_hv_view,
+    [XIVE_TM_OS_PAGE]   = xive_tm_os_view,
+    [XIVE_TM_USER_PAGE] = xive_tm_user_view,
+};
+
+/*
+ * Computes a register access mask for a given offset in the TIMA
+ */
+static uint64_t xive_tm_mask(hwaddr offset, unsigned size, bool write)
+{
+    uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
+    uint8_t reg_offset = offset & 0x3F;
+    uint8_t reg_mask = write ? 0x1 : 0x2;
+    uint64_t mask = 0x0;
+    int i;
+
+    for (i = 0; i < size; i++) {
+        if (xive_tm_views[page_offset][reg_offset + i] & reg_mask) {
+            mask |= (uint64_t) 0xff << (8 * (size - i - 1));
+        }
+    }
+
+    return mask;
+}
+
+static void xive_tm_raw_write(XiveTCTX *tctx, hwaddr offset, uint64_t value,
+                              unsigned size)
+{
+    uint8_t ring_offset = offset & 0x30;
+    uint8_t reg_offset = offset & 0x3F;
+    uint64_t mask = xive_tm_mask(offset, size, true);
+    int i;
+
+    /*
+     * Only 4 or 8 bytes stores are allowed and the User ring is
+     * excluded
+     */
+    if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA @%"
+                      HWADDR_PRIx"\n", offset);
+        return;
+    }
+
+    /*
+     * Use the register offset for the raw values and filter out
+     * reserved values
+     */
+    for (i = 0; i < size; i++) {
+        uint8_t byte_mask = (mask >> (8 * (size - i - 1)));
+        if (byte_mask) {
+            tctx->regs[reg_offset + i] = (value >> (8 * (size - i - 1))) &
+                byte_mask;
+        }
+    }
+}
+
+static uint64_t xive_tm_raw_read(XiveTCTX *tctx, hwaddr offset, unsigned size)
+{
+    uint8_t ring_offset = offset & 0x30;
+    uint8_t reg_offset = offset & 0x3F;
+    uint64_t mask = xive_tm_mask(offset, size, false);
+    uint64_t ret;
+    int i;
+
+    /*
+     * Only 4 or 8 bytes loads are allowed and the User ring is
+     * excluded
+     */
+    if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access at TIMA @%"
+                      HWADDR_PRIx"\n", offset);
+        return -1;
+    }
+
+    /* Use the register offset for the raw values */
+    ret = 0;
+    for (i = 0; i < size; i++) {
+        ret |= (uint64_t) tctx->regs[reg_offset + i] << (8 * (size - i - 1));
+    }
+
+    /* filter out reserved values */
+    return ret & mask;
+}
+
+/*
+ * The TM context is mapped twice within each page. Stores and loads
+ * to the first mapping below 2K write and read the specified values
+ * without modification. The second mapping above 2K performs specific
+ * state changes (side effects) in addition to setting/returning the
+ * interrupt management area context of the processor thread.
+ */
+static uint64_t xive_tm_ack_os_reg(XiveTCTX *tctx, hwaddr offset, unsigned size)
+{
+    return xive_tctx_accept(tctx, TM_QW1_OS);
+}
+
+static void xive_tm_set_os_cppr(XiveTCTX *tctx, hwaddr offset,
+                                uint64_t value, unsigned size)
+{
+    xive_tctx_set_cppr(tctx, TM_QW1_OS, value & 0xff);
+}
+
+/*
+ * Define a mapping of "special" operations depending on the TIMA page
+ * offset and the size of the operation.
+ */
+typedef struct XiveTmOp {
+    uint8_t  page_offset;
+    uint32_t op_offset;
+    unsigned size;
+    void     (*write_handler)(XiveTCTX *tctx, hwaddr offset, uint64_t value,
+                              unsigned size);
+    uint64_t (*read_handler)(XiveTCTX *tctx, hwaddr offset, unsigned size);
+} XiveTmOp;
+
+static const XiveTmOp xive_tm_operations[] = {
+    /*
+     * MMIOs below 2K : raw values and special operations without side
+     * effects
+     */
+    { XIVE_TM_OS_PAGE, TM_QW1_OS + TM_CPPR,   1, xive_tm_set_os_cppr, NULL },
+
+    /* MMIOs above 2K : special operations with side effects */
+    { XIVE_TM_OS_PAGE, TM_SPC_ACK_OS_REG,     2, NULL, xive_tm_ack_os_reg },
+};
+
+static const XiveTmOp *xive_tm_find_op(hwaddr offset, unsigned size, bool write)
+{
+    uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
+    uint32_t op_offset = offset & 0xFFF;
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(xive_tm_operations); i++) {
+        const XiveTmOp *xto = &xive_tm_operations[i];
+
+        /* Accesses done from a more privileged TIMA page is allowed */
+        if (xto->page_offset >= page_offset &&
+            xto->op_offset == op_offset &&
+            xto->size == size &&
+            ((write && xto->write_handler) || (!write && xto->read_handler))) {
+            return xto;
+        }
+    }
+    return NULL;
+}
+
+/*
+ * TIMA MMIO handlers
+ */
+static void xive_tm_write(void *opaque, hwaddr offset,
+                          uint64_t value, unsigned size)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
+    XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
+    const XiveTmOp *xto;
+
+    /*
+     * TODO: check V bit in Q[0-3]W2, check PTER bit associated with CPU
+     */
+
+    /*
+     * First, check for special operations in the 2K region
+     */
+    if (offset & 0x800) {
+        xto = xive_tm_find_op(offset, size, true);
+        if (!xto) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA"
+                          "@%"HWADDR_PRIx"\n", offset);
+        } else {
+            xto->write_handler(tctx, offset, value, size);
+        }
+        return;
+    }
+
+    /*
+     * Then, for special operations in the region below 2K.
+     */
+    xto = xive_tm_find_op(offset, size, true);
+    if (xto) {
+        xto->write_handler(tctx, offset, value, size);
+        return;
+    }
+
+    /*
+     * Finish with raw access to the register values
+     */
+    xive_tm_raw_write(tctx, offset, value, size);
+}
+
+static uint64_t xive_tm_read(void *opaque, hwaddr offset, unsigned size)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
+    XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
+    const XiveTmOp *xto;
+
+    /*
+     * TODO: check V bit in Q[0-3]W2, check PTER bit associated with CPU
+     */
+
+    /*
+     * First, check for special operations in the 2K region
+     */
+    if (offset & 0x800) {
+        xto = xive_tm_find_op(offset, size, false);
+        if (!xto) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access to TIMA"
+                          "@%"HWADDR_PRIx"\n", offset);
+            return -1;
+        }
+        return xto->read_handler(tctx, offset, size);
+    }
+
+    /*
+     * Then, for special operations in the region below 2K.
+     */
+    xto = xive_tm_find_op(offset, size, false);
+    if (xto) {
+        return xto->read_handler(tctx, offset, size);
+    }
+
+    /*
+     * Finish with raw access to the register values
+     */
+    return xive_tm_raw_read(tctx, offset, size);
+}
+
+const MemoryRegionOps xive_tm_ops = {
+    .read = xive_tm_read,
+    .write = xive_tm_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 1,
+        .max_access_size = 8,
+    },
+};
+
+static char *xive_tctx_ring_print(uint8_t *ring)
+{
+    uint32_t w2 = be32_to_cpu(*((uint32_t *) &ring[TM_WORD2]));
+
+    return g_strdup_printf("%02x   %02x  %02x    %02x   %02x  "
+                   "%02x  %02x   %02x  %08x",
+                   ring[TM_NSR], ring[TM_CPPR], ring[TM_IPB], ring[TM_LSMFB],
+                   ring[TM_ACK_CNT], ring[TM_INC], ring[TM_AGE], ring[TM_PIPR],
+                   w2);

hw/intc/xive.c line 357

Quoted 10 lines
hw/intc/xive.c +10 -0
+}
+
+static const struct {
+    uint8_t    qw;
+    const char *name;
+} xive_tctx_ring_infos[TM_RING_COUNT] = {
+    { TM_QW3_HV_PHYS, "HW"   },
+    { TM_QW2_HV_POOL, "HV"   },
+    { TM_QW1_OS,      "OS"   },
+    { TM_QW0_USER,    "USER" },

Likewise here if we can stick to PHYS and POOL rather than HW and HV.

Also, the qw field takes exactly the values 0..3, why not just an array of names indexed by the ring number.

Quoted 113 lines
hw/intc/xive.c +103 -0
+};
+
+void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon)
+{
+    int cpu_index = tctx->cs ? tctx->cs->cpu_index : -1;
+    int i;
+
+    monitor_printf(mon, "CPU[%04x]:   QW   NSR CPPR IPB LSMFB ACK# INC AGE PIPR"
+                   "  W2\n", cpu_index);
+
+    for (i = 0; i < TM_RING_COUNT; i++) {
+        char *s = xive_tctx_ring_print(&tctx->regs[xive_tctx_ring_infos[i].qw]);
+        monitor_printf(mon, "CPU[%04x]: %4s    %s\n", cpu_index,
+                       xive_tctx_ring_infos[i].name, s);
+        g_free(s);
+    }
+}
+
+static void xive_tctx_reset(void *dev)
+{
+    XiveTCTX *tctx = XIVE_TCTX(dev);
+
+    memset(tctx->regs, 0, sizeof(tctx->regs));
+
+    /* Set some defaults */
+    tctx->regs[TM_QW1_OS + TM_LSMFB] = 0xFF;
+    tctx->regs[TM_QW1_OS + TM_ACK_CNT] = 0xFF;
+    tctx->regs[TM_QW1_OS + TM_AGE] = 0xFF;
+}
+
+static void xive_tctx_realize(DeviceState *dev, Error **errp)
+{
+    XiveTCTX *tctx = XIVE_TCTX(dev);
+    PowerPCCPU *cpu;
+    CPUPPCState *env;
+    Object *obj;
+    Error *local_err = NULL;
+
+    obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'xive' not found: ");
+        return;
+    }
+    tctx->xrtr = XIVE_ROUTER(obj);
+
+    obj = object_property_get_link(OBJECT(dev), "cpu", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'cpu' not found: ");
+        return;
+    }
+
+    cpu = POWERPC_CPU(obj);
+    tctx->cs = CPU(obj);
+
+    env = &cpu->env;
+    switch (PPC_INPUT(env)) {
+    case PPC_FLAGS_INPUT_POWER7:
+        tctx->output = env->irq_inputs[POWER7_INPUT_INT];
+        break;
+
+    default:
+        error_setg(errp, "XIVE interrupt controller does not support "
+                   "this CPU bus model");
+        return;
+    }
+
+    qemu_register_reset(xive_tctx_reset, dev);
+}
+
+static void xive_tctx_unrealize(DeviceState *dev, Error **errp)
+{
+    qemu_unregister_reset(xive_tctx_reset, dev);
+}
+
+static const VMStateDescription vmstate_xive_tctx = {
+    .name = TYPE_XIVE_TCTX,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_BUFFER(regs, XiveTCTX),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static void xive_tctx_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = xive_tctx_realize;
+    dc->unrealize = xive_tctx_unrealize;
+    dc->desc = "XIVE Interrupt Thread Context";
+    dc->vmsd = &vmstate_xive_tctx;
+}
+
+static const TypeInfo xive_tctx_info = {
+    .name          = TYPE_XIVE_TCTX,
+    .parent        = TYPE_DEVICE,
+    .instance_size = sizeof(XiveTCTX),
+    .class_init    = xive_tctx_class_init,
+};
 
 /*
  * XIVE ESB helpers
@@ -876,6 +1318,7 @@ static void xive_register_types(void)
     type_register_static(&xive_fabric_info);
     type_register_static(&xive_router_info);
     type_register_static(&xive_end_source_info);
+    type_register_static(&xive_tctx_info);
 }
 
 type_init(xive_register_types)
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
#56 lore raw

Re: [Qemu-devel] [PATCH v5 07/36] ppc/xive: introduce the XIVE interrupt thread context

Quoted 56 lines · David Gibson
Quoted 55 lines · Cédric Le Goater

Each POWER9 processor chip has a XIVE presenter that can generate four different exceptions to its threads:

- hypervisor exception,
- O/S exception
- Event-Based Branch (EBB)
- msgsnd (doorbell).

Each exception has a state independent from the others called a Thread Interrupt Management context. This context is a set of registers which lets the thread handle priority management and interrupt acknowledgment among other things. The most important ones being :

  - Interrupt Priority Register  (PIPR)
  - Interrupt Pending Buffer     (IPB)
  - Current Processor Priority   (CPPR)

- Notification Source Register (NSR)

These registers are accessible through a specific MMIO region, called the Thread Interrupt Management Area (TIMA), four aligned pages, each exposing a different view of the registers. First page (page address ending in 0b00) gives access to the entire context and is reserved for the ring 0 security monitor. The second (page address ending in 0b01) is for the hypervisor, ring 1. The third (page address ending in 0b10) is for the operating system, ring 2. The fourth (page address ending in 0b11) is for user level, ring 3.

The thread interrupt context is modeled with a XiveTCTX object containing the values of the different exception registers. The TIMA region is mapped at the same address for each CPU.

---
 include/hw/ppc/xive.h      |  36 +++
 include/hw/ppc/xive_regs.h |  82 +++++++
 hw/intc/xive.c             | 443 +++++++++++++++++++++++++++++++++++++
 3 files changed, 561 insertions(+)
include/hw/ppc/xive.h +9 -0
index 24301bf2076d..5987f26ddb98 100644
@@ -238,4 +238,40 @@ typedef struct XiveENDSource {
 void xive_end_reset(XiveEND *end);
 void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
+/*
+ * XIVE Thread interrupt Management (TM) context
+ */
+
+#define TYPE_XIVE_TCTX "xive-tctx"
+#define XIVE_TCTX(obj) OBJECT_CHECK(XiveTCTX, (obj), TYPE_XIVE_TCTX)
+
+/*
+ * XIVE Thread interrupt Management register rings :
Quoted 8 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 254

Quoted 5 lines
include/hw/ppc/xive.h +5 -0
+ *
+ *   QW-0  User       event-based exception state
+ *   QW-1  O/S        OS context for priority management, interrupt acks
+ *   QW-2  Pool       hypervisor context for virtual processor being dispatched
+ *   QW-3  Physical   for the security monitor to manage the entire context

That last description is misleading, AIUI the hypervisor can and does make use of the physical ring as well as the pool ring.

yes. The description is from the spec. I will rephrase.

Quoted 16 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 265

Quoted 12 lines
include/hw/ppc/xive.h +11 -0
+ */
+#define TM_RING_COUNT           4
+#define TM_RING_SIZE            0x10
+
+typedef struct XiveTCTX {
+    DeviceState parent_obj;
+
+    CPUState    *cs;
+    qemu_irq    output;
+
+    uint8_t     regs[TM_RING_COUNT * TM_RING_SIZE];

I'm a bit dubious about representing the state with a full buffer like this. Isn't a fair bit of this space reserved or derived values which aren't backed by real state?

Under sPAPR only the TM_QW1_OS ring is accessed but the TM_QW0_USER will also be when we support EBB.

When running under the PowerNV machine, all rings could be accessed. Today only 2 and 3 are.

It seemed correct to expose all registers under the thread interrupt context model and filter the accesses with the TIMA. It fits the HW well.

Quoted 6 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 267

Quoted 3 lines
include/hw/ppc/xive.h +2 -0
+
+    XiveRouter  *xrtr;

What's this for? AFAIK a TCTX isn't associated with a particular routing unit.

I should have added the pointer in patch 11 where it is used. This is to let the sPAPR XIVE controller model reset the OS CAM line with the VP identifier.

The TCTX belong to the IVPE XIVE subengine and as the IVRE and IVPE are modeled under the XiveRouter, it's not suprising to see this '*xrtr' back pointer. But I agree we might not need. Let's talk about it when you reach patch 11.

Quoted 7 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 271

Quoted 5 lines
include/hw/ppc/xive.h +4 -0
+} XiveTCTX;
+
+/*
+ * XIVE Thread Interrupt Management Aera (TIMA)

Typo s/Aera/Area/

yep.

Quoted 156 lines
Quoted 156 lines
include/hw/ppc/xive.h +5 -0
+ */
+extern const MemoryRegionOps xive_tm_ops;
+
+void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon);
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +82 -0
index f97fb2b90bee..2e3d6cb507da 100644
@@ -10,6 +10,88 @@
 #ifndef PPC_XIVE_REGS_H
 #define PPC_XIVE_REGS_H
 
+#define TM_SHIFT                16
+
+/* TM register offsets */
+#define TM_QW0_USER             0x000 /* All rings */
+#define TM_QW1_OS               0x010 /* Ring 0..2 */
+#define TM_QW2_HV_POOL          0x020 /* Ring 0..1 */
+#define TM_QW3_HV_PHYS          0x030 /* Ring 0..1 */
+
+/* Byte offsets inside a QW             QW0 QW1 QW2 QW3 */
+#define TM_NSR                  0x0  /*  +   +   -   +  */
+#define TM_CPPR                 0x1  /*  -   +   -   +  */
+#define TM_IPB                  0x2  /*  -   +   +   +  */
+#define TM_LSMFB                0x3  /*  -   +   +   +  */
+#define TM_ACK_CNT              0x4  /*  -   +   -   -  */
+#define TM_INC                  0x5  /*  -   +   -   +  */
+#define TM_AGE                  0x6  /*  -   +   -   +  */
+#define TM_PIPR                 0x7  /*  -   +   -   +  */
+
+#define TM_WORD0                0x0
+#define TM_WORD1                0x4
+
+/*
+ * QW word 2 contains the valid bit at the top and other fields
+ * depending on the QW.
+ */
+#define TM_WORD2                0x8
+#define   TM_QW0W2_VU           PPC_BIT32(0)
+#define   TM_QW0W2_LOGIC_SERV   PPC_BITMASK32(1, 31) /* XX 2,31 ? */
+#define   TM_QW1W2_VO           PPC_BIT32(0)
+#define   TM_QW1W2_OS_CAM       PPC_BITMASK32(8, 31)
+#define   TM_QW2W2_VP           PPC_BIT32(0)
+#define   TM_QW2W2_POOL_CAM     PPC_BITMASK32(8, 31)
+#define   TM_QW3W2_VT           PPC_BIT32(0)
+#define   TM_QW3W2_LP           PPC_BIT32(6)
+#define   TM_QW3W2_LE           PPC_BIT32(7)
+#define   TM_QW3W2_T            PPC_BIT32(31)
+
+/*
+ * In addition to normal loads to "peek" and writes (only when invalid)
+ * using 4 and 8 bytes accesses, the above registers support these
+ * "special" byte operations:
+ *
+ *   - Byte load from QW0[NSR] - User level NSR (EBB)
+ *   - Byte store to QW0[NSR] - User level NSR (EBB)
+ *   - Byte load/store to QW1[CPPR] and QW3[CPPR] - CPPR access
+ *   - Byte load from QW3[TM_WORD2] - Read VT||00000||LP||LE on thrd 0
+ *                                    otherwise VT||0000000
+ *   - Byte store to QW3[TM_WORD2] - Set VT bit (and LP/LE if present)
+ *
+ * Then we have all these "special" CI ops at these offset that trigger
+ * all sorts of side effects:
+ */
+#define TM_SPC_ACK_EBB          0x800   /* Load8 ack EBB to reg*/
+#define TM_SPC_ACK_OS_REG       0x810   /* Load16 ack OS irq to reg */
+#define TM_SPC_PUSH_USR_CTX     0x808   /* Store32 Push/Validate user context */
+#define TM_SPC_PULL_USR_CTX     0x808   /* Load32 Pull/Invalidate user
+                                         * context */
+#define TM_SPC_SET_OS_PENDING   0x812   /* Store8 Set OS irq pending bit */
+#define TM_SPC_PULL_OS_CTX      0x818   /* Load32/Load64 Pull/Invalidate OS
+                                         * context to reg */
+#define TM_SPC_PULL_POOL_CTX    0x828   /* Load32/Load64 Pull/Invalidate Pool
+                                         * context to reg*/
+#define TM_SPC_ACK_HV_REG       0x830   /* Load16 ack HV irq to reg */
+#define TM_SPC_PULL_USR_CTX_OL  0xc08   /* Store8 Pull/Inval usr ctx to odd
+                                         * line */
+#define TM_SPC_ACK_OS_EL        0xc10   /* Store8 ack OS irq to even line */
+#define TM_SPC_ACK_HV_POOL_EL   0xc20   /* Store8 ack HV evt pool to even
+                                         * line */
+#define TM_SPC_ACK_HV_EL        0xc30   /* Store8 ack HV irq to even line */
+/* XXX more... */
+
+/* NSR fields for the various QW ack types */
+#define TM_QW0_NSR_EB           PPC_BIT8(0)
+#define TM_QW1_NSR_EO           PPC_BIT8(0)
+#define TM_QW3_NSR_HE           PPC_BITMASK8(0, 1)
+#define  TM_QW3_NSR_HE_NONE     0
+#define  TM_QW3_NSR_HE_POOL     1
+#define  TM_QW3_NSR_HE_PHYS     2
+#define  TM_QW3_NSR_HE_LSI      3
+#define TM_QW3_NSR_I            PPC_BIT8(2)
+#define TM_QW3_NSR_GRP_LVL      PPC_BIT8(3, 7)
+
 /* EAS (Event Assignment Structure)
  *
  * One per interrupt source. Targets an interrupt to a given Event
hw/intc/xive.c +48 -0
index 5a8882d47a98..4c6cb5d52975 100644
@@ -15,6 +15,448 @@
 #include "sysemu/dma.h"
 #include "monitor/monitor.h"
 #include "hw/ppc/xive.h"
+#include "hw/ppc/xive_regs.h"
+
+/*
+ * XIVE Thread Interrupt Management context
+ */
+
+static uint64_t xive_tctx_accept(XiveTCTX *tctx, uint8_t ring)
+{
+    return 0;
+}
+
+static void xive_tctx_set_cppr(XiveTCTX *tctx, uint8_t ring, uint8_t cppr)
+{
+    if (cppr > XIVE_PRIORITY_MAX) {
+        cppr = 0xff;
+    }
+
+    tctx->regs[ring + TM_CPPR] = cppr;
+}
+
+/*
+ * XIVE Thread Interrupt Management Area (TIMA)
+ *
+ * This region gives access to the registers of the thread interrupt
+ * management context. It is four page wide, each page providing a
+ * different view of the registers. The page with the lower offset is
+ * the most privileged and gives access to the entire context.
+ */
+
+#define XIVE_TM_HW_PAGE   0x0
+#define XIVE_TM_HV_PAGE   0x1
+#define XIVE_TM_OS_PAGE   0x2
+#define XIVE_TM_USER_PAGE 0x3
+
+/*
+ * Define an access map for each page of the TIMA that we will use in
+ * the memory region ops to filter values when doing loads and stores
+ * of raw registers values
+ *
+ * Registers accessibility bits :
+ *
+ *    0x0 - no access
+ *    0x1 - write only
+ *    0x2 - read only
+ *    0x3 - read/write
+ */
+
+static const uint8_t xive_tm_hw_view[] = {
Quoted 8 lines

hw/intc/xive.c line 69

Quoted 4 lines
hw/intc/xive.c +4 -0
+    /* QW-0 User */   3, 0, 0, 0,   0, 0, 0, 0,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-1 OS   */   3, 3, 3, 3,   3, 3, 0, 3,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-2 HV   */   0, 0, 3, 3,   0, 0, 0, 0,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-3 HW   */   3, 3, 3, 3,   0, 3, 0, 3,   3, 0, 0, 3,   3, 3, 3, 0,

Can we stick to the "Pool" / "Phys" names rather than inventing HV and HW. XIVE already has too many names for things. To clarify that's for the naming of the QWs, the view names are fine.

sure. I will fix the names.

Quoted 9 lines
Quoted 9 lines
hw/intc/xive.c +9 -0
+};
+
+static const uint8_t xive_tm_hv_view[] = {
+    /* QW-0 User */   3, 0, 0, 0,   0, 0, 0, 0,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-1 OS   */   3, 3, 3, 3,   3, 3, 0, 3,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-2 HV   */   0, 0, 3, 3,   0, 0, 0, 0,   0, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-3 HW   */   3, 3, 3, 3,   0, 3, 0, 3,   3, 0, 0, 3,   0, 0, 0, 0,
+};
+
Quoted 8 lines

hw/intc/xive.c line 83

Quoted 5 lines
hw/intc/xive.c +5 -0
+static const uint8_t xive_tm_os_view[] = {
+    /* QW-0 User */   3, 0, 0, 0,   0, 0, 0, 0,   3, 3, 3, 3,   0, 0, 0, 0,
+    /* QW-1 OS   */   2, 3, 2, 2,   2, 2, 0, 2,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-2 HV   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-3 HW   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 3, 3, 0,

Are those bytes near the end of QW-3 really accessible in OS but not hypervisor view?

No. It's copy/paste error. Thx.

Quoted 268 lines
Quoted 268 lines
hw/intc/xive.c +268 -0
+};
+
+static const uint8_t xive_tm_user_view[] = {
+    /* QW-0 User */   3, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-1 OS   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-2 HV   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+    /* QW-3 HW   */   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,   0, 0, 0, 0,
+};
+
+/*
+ * Overall TIMA access map for the thread interrupt management context
+ * registers
+ */
+static const uint8_t *xive_tm_views[] = {
+    [XIVE_TM_HW_PAGE]   = xive_tm_hw_view,
+    [XIVE_TM_HV_PAGE]   = xive_tm_hv_view,
+    [XIVE_TM_OS_PAGE]   = xive_tm_os_view,
+    [XIVE_TM_USER_PAGE] = xive_tm_user_view,
+};
+
+/*
+ * Computes a register access mask for a given offset in the TIMA
+ */
+static uint64_t xive_tm_mask(hwaddr offset, unsigned size, bool write)
+{
+    uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
+    uint8_t reg_offset = offset & 0x3F;
+    uint8_t reg_mask = write ? 0x1 : 0x2;
+    uint64_t mask = 0x0;
+    int i;
+
+    for (i = 0; i < size; i++) {
+        if (xive_tm_views[page_offset][reg_offset + i] & reg_mask) {
+            mask |= (uint64_t) 0xff << (8 * (size - i - 1));
+        }
+    }
+
+    return mask;
+}
+
+static void xive_tm_raw_write(XiveTCTX *tctx, hwaddr offset, uint64_t value,
+                              unsigned size)
+{
+    uint8_t ring_offset = offset & 0x30;
+    uint8_t reg_offset = offset & 0x3F;
+    uint64_t mask = xive_tm_mask(offset, size, true);
+    int i;
+
+    /*
+     * Only 4 or 8 bytes stores are allowed and the User ring is
+     * excluded
+     */
+    if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA @%"
+                      HWADDR_PRIx"\n", offset);
+        return;
+    }
+
+    /*
+     * Use the register offset for the raw values and filter out
+     * reserved values
+     */
+    for (i = 0; i < size; i++) {
+        uint8_t byte_mask = (mask >> (8 * (size - i - 1)));
+        if (byte_mask) {
+            tctx->regs[reg_offset + i] = (value >> (8 * (size - i - 1))) &
+                byte_mask;
+        }
+    }
+}
+
+static uint64_t xive_tm_raw_read(XiveTCTX *tctx, hwaddr offset, unsigned size)
+{
+    uint8_t ring_offset = offset & 0x30;
+    uint8_t reg_offset = offset & 0x3F;
+    uint64_t mask = xive_tm_mask(offset, size, false);
+    uint64_t ret;
+    int i;
+
+    /*
+     * Only 4 or 8 bytes loads are allowed and the User ring is
+     * excluded
+     */
+    if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access at TIMA @%"
+                      HWADDR_PRIx"\n", offset);
+        return -1;
+    }
+
+    /* Use the register offset for the raw values */
+    ret = 0;
+    for (i = 0; i < size; i++) {
+        ret |= (uint64_t) tctx->regs[reg_offset + i] << (8 * (size - i - 1));
+    }
+
+    /* filter out reserved values */
+    return ret & mask;
+}
+
+/*
+ * The TM context is mapped twice within each page. Stores and loads
+ * to the first mapping below 2K write and read the specified values
+ * without modification. The second mapping above 2K performs specific
+ * state changes (side effects) in addition to setting/returning the
+ * interrupt management area context of the processor thread.
+ */
+static uint64_t xive_tm_ack_os_reg(XiveTCTX *tctx, hwaddr offset, unsigned size)
+{
+    return xive_tctx_accept(tctx, TM_QW1_OS);
+}
+
+static void xive_tm_set_os_cppr(XiveTCTX *tctx, hwaddr offset,
+                                uint64_t value, unsigned size)
+{
+    xive_tctx_set_cppr(tctx, TM_QW1_OS, value & 0xff);
+}
+
+/*
+ * Define a mapping of "special" operations depending on the TIMA page
+ * offset and the size of the operation.
+ */
+typedef struct XiveTmOp {
+    uint8_t  page_offset;
+    uint32_t op_offset;
+    unsigned size;
+    void     (*write_handler)(XiveTCTX *tctx, hwaddr offset, uint64_t value,
+                              unsigned size);
+    uint64_t (*read_handler)(XiveTCTX *tctx, hwaddr offset, unsigned size);
+} XiveTmOp;
+
+static const XiveTmOp xive_tm_operations[] = {
+    /*
+     * MMIOs below 2K : raw values and special operations without side
+     * effects
+     */
+    { XIVE_TM_OS_PAGE, TM_QW1_OS + TM_CPPR,   1, xive_tm_set_os_cppr, NULL },
+
+    /* MMIOs above 2K : special operations with side effects */
+    { XIVE_TM_OS_PAGE, TM_SPC_ACK_OS_REG,     2, NULL, xive_tm_ack_os_reg },
+};
+
+static const XiveTmOp *xive_tm_find_op(hwaddr offset, unsigned size, bool write)
+{
+    uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
+    uint32_t op_offset = offset & 0xFFF;
+    int i;
+
+    for (i = 0; i < ARRAY_SIZE(xive_tm_operations); i++) {
+        const XiveTmOp *xto = &xive_tm_operations[i];
+
+        /* Accesses done from a more privileged TIMA page is allowed */
+        if (xto->page_offset >= page_offset &&
+            xto->op_offset == op_offset &&
+            xto->size == size &&
+            ((write && xto->write_handler) || (!write && xto->read_handler))) {
+            return xto;
+        }
+    }
+    return NULL;
+}
+
+/*
+ * TIMA MMIO handlers
+ */
+static void xive_tm_write(void *opaque, hwaddr offset,
+                          uint64_t value, unsigned size)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
+    XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
+    const XiveTmOp *xto;
+
+    /*
+     * TODO: check V bit in Q[0-3]W2, check PTER bit associated with CPU
+     */
+
+    /*
+     * First, check for special operations in the 2K region
+     */
+    if (offset & 0x800) {
+        xto = xive_tm_find_op(offset, size, true);
+        if (!xto) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA"
+                          "@%"HWADDR_PRIx"\n", offset);
+        } else {
+            xto->write_handler(tctx, offset, value, size);
+        }
+        return;
+    }
+
+    /*
+     * Then, for special operations in the region below 2K.
+     */
+    xto = xive_tm_find_op(offset, size, true);
+    if (xto) {
+        xto->write_handler(tctx, offset, value, size);
+        return;
+    }
+
+    /*
+     * Finish with raw access to the register values
+     */
+    xive_tm_raw_write(tctx, offset, value, size);
+}
+
+static uint64_t xive_tm_read(void *opaque, hwaddr offset, unsigned size)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(current_cpu);
+    XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
+    const XiveTmOp *xto;
+
+    /*
+     * TODO: check V bit in Q[0-3]W2, check PTER bit associated with CPU
+     */
+
+    /*
+     * First, check for special operations in the 2K region
+     */
+    if (offset & 0x800) {
+        xto = xive_tm_find_op(offset, size, false);
+        if (!xto) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access to TIMA"
+                          "@%"HWADDR_PRIx"\n", offset);
+            return -1;
+        }
+        return xto->read_handler(tctx, offset, size);
+    }
+
+    /*
+     * Then, for special operations in the region below 2K.
+     */
+    xto = xive_tm_find_op(offset, size, false);
+    if (xto) {
+        return xto->read_handler(tctx, offset, size);
+    }
+
+    /*
+     * Finish with raw access to the register values
+     */
+    return xive_tm_raw_read(tctx, offset, size);
+}
+
+const MemoryRegionOps xive_tm_ops = {
+    .read = xive_tm_read,
+    .write = xive_tm_write,
+    .endianness = DEVICE_BIG_ENDIAN,
+    .valid = {
+        .min_access_size = 1,
+        .max_access_size = 8,
+    },
+    .impl = {
+        .min_access_size = 1,
+        .max_access_size = 8,
+    },
+};
+
+static char *xive_tctx_ring_print(uint8_t *ring)
+{
+    uint32_t w2 = be32_to_cpu(*((uint32_t *) &ring[TM_WORD2]));
+
+    return g_strdup_printf("%02x   %02x  %02x    %02x   %02x  "
+                   "%02x  %02x   %02x  %08x",
+                   ring[TM_NSR], ring[TM_CPPR], ring[TM_IPB], ring[TM_LSMFB],
+                   ring[TM_ACK_CNT], ring[TM_INC], ring[TM_AGE], ring[TM_PIPR],
+                   w2);
+}
+
+static const struct {
+    uint8_t    qw;
Quoted 8 lines

hw/intc/xive.c line 357

Quoted 6 lines
hw/intc/xive.c +6 -0
+    const char *name;
+} xive_tctx_ring_infos[TM_RING_COUNT] = {
+    { TM_QW3_HV_PHYS, "HW"   },
+    { TM_QW2_HV_POOL, "HV"   },
+    { TM_QW1_OS,      "OS"   },
+    { TM_QW0_USER,    "USER" },

Likewise here if we can stick to PHYS and POOL rather than HW and HV.

I don't remember why I changed the names :/ I will fix.

Quoted 2 lines

Also, the qw field takes exactly the values 0..3, why not just an array of names indexed by the ring number.

yes. There should be room for simplification.

Thanks,

C.

Quoted 114 lines
Quoted 113 lines
hw/intc/xive.c +103 -0
+};
+
+void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon)
+{
+    int cpu_index = tctx->cs ? tctx->cs->cpu_index : -1;
+    int i;
+
+    monitor_printf(mon, "CPU[%04x]:   QW   NSR CPPR IPB LSMFB ACK# INC AGE PIPR"
+                   "  W2\n", cpu_index);
+
+    for (i = 0; i < TM_RING_COUNT; i++) {
+        char *s = xive_tctx_ring_print(&tctx->regs[xive_tctx_ring_infos[i].qw]);
+        monitor_printf(mon, "CPU[%04x]: %4s    %s\n", cpu_index,
+                       xive_tctx_ring_infos[i].name, s);
+        g_free(s);
+    }
+}
+
+static void xive_tctx_reset(void *dev)
+{
+    XiveTCTX *tctx = XIVE_TCTX(dev);
+
+    memset(tctx->regs, 0, sizeof(tctx->regs));
+
+    /* Set some defaults */
+    tctx->regs[TM_QW1_OS + TM_LSMFB] = 0xFF;
+    tctx->regs[TM_QW1_OS + TM_ACK_CNT] = 0xFF;
+    tctx->regs[TM_QW1_OS + TM_AGE] = 0xFF;
+}
+
+static void xive_tctx_realize(DeviceState *dev, Error **errp)
+{
+    XiveTCTX *tctx = XIVE_TCTX(dev);
+    PowerPCCPU *cpu;
+    CPUPPCState *env;
+    Object *obj;
+    Error *local_err = NULL;
+
+    obj = object_property_get_link(OBJECT(dev), "xive", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'xive' not found: ");
+        return;
+    }
+    tctx->xrtr = XIVE_ROUTER(obj);
+
+    obj = object_property_get_link(OBJECT(dev), "cpu", &local_err);
+    if (!obj) {
+        error_propagate(errp, local_err);
+        error_prepend(errp, "required link 'cpu' not found: ");
+        return;
+    }
+
+    cpu = POWERPC_CPU(obj);
+    tctx->cs = CPU(obj);
+
+    env = &cpu->env;
+    switch (PPC_INPUT(env)) {
+    case PPC_FLAGS_INPUT_POWER7:
+        tctx->output = env->irq_inputs[POWER7_INPUT_INT];
+        break;
+
+    default:
+        error_setg(errp, "XIVE interrupt controller does not support "
+                   "this CPU bus model");
+        return;
+    }
+
+    qemu_register_reset(xive_tctx_reset, dev);
+}
+
+static void xive_tctx_unrealize(DeviceState *dev, Error **errp)
+{
+    qemu_unregister_reset(xive_tctx_reset, dev);
+}
+
+static const VMStateDescription vmstate_xive_tctx = {
+    .name = TYPE_XIVE_TCTX,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_BUFFER(regs, XiveTCTX),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
+static void xive_tctx_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = xive_tctx_realize;
+    dc->unrealize = xive_tctx_unrealize;
+    dc->desc = "XIVE Interrupt Thread Context";
+    dc->vmsd = &vmstate_xive_tctx;
+}
+
+static const TypeInfo xive_tctx_info = {
+    .name          = TYPE_XIVE_TCTX,
+    .parent        = TYPE_DEVICE,
+    .instance_size = sizeof(XiveTCTX),
+    .class_init    = xive_tctx_class_init,
+};
 
 /*
  * XIVE ESB helpers
@@ -876,6 +1318,7 @@ static void xive_register_types(void)
     type_register_static(&xive_fabric_info);
     type_register_static(&xive_router_info);
     type_register_static(&xive_end_source_info);
+    type_register_static(&xive_tctx_info);
 }
 
 type_init(xive_register_types)
#57 lore raw

Re: [Qemu-devel] [PATCH v5 07/36] ppc/xive: introduce the XIVE interrupt thread context

Quoted 85 lines · Cédric Le Goater
Quoted 56 lines · David Gibson
Quoted 55 lines · Cédric Le Goater

Each POWER9 processor chip has a XIVE presenter that can generate four different exceptions to its threads:

- hypervisor exception,
- O/S exception
- Event-Based Branch (EBB)
- msgsnd (doorbell).

Each exception has a state independent from the others called a Thread Interrupt Management context. This context is a set of registers which lets the thread handle priority management and interrupt acknowledgment among other things. The most important ones being :

  - Interrupt Priority Register  (PIPR)
  - Interrupt Pending Buffer     (IPB)
  - Current Processor Priority   (CPPR)

- Notification Source Register (NSR)

These registers are accessible through a specific MMIO region, called the Thread Interrupt Management Area (TIMA), four aligned pages, each exposing a different view of the registers. First page (page address ending in 0b00) gives access to the entire context and is reserved for the ring 0 security monitor. The second (page address ending in 0b01) is for the hypervisor, ring 1. The third (page address ending in 0b10) is for the operating system, ring 2. The fourth (page address ending in 0b11) is for user level, ring 3.

The thread interrupt context is modeled with a XiveTCTX object containing the values of the different exception registers. The TIMA region is mapped at the same address for each CPU.

---
 include/hw/ppc/xive.h      |  36 +++
 include/hw/ppc/xive_regs.h |  82 +++++++
 hw/intc/xive.c             | 443 +++++++++++++++++++++++++++++++++++++
 3 files changed, 561 insertions(+)
include/hw/ppc/xive.h +9 -0
index 24301bf2076d..5987f26ddb98 100644
@@ -238,4 +238,40 @@ typedef struct XiveENDSource {
 void xive_end_reset(XiveEND *end);
 void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
+/*
+ * XIVE Thread interrupt Management (TM) context
+ */
+
+#define TYPE_XIVE_TCTX "xive-tctx"
+#define XIVE_TCTX(obj) OBJECT_CHECK(XiveTCTX, (obj), TYPE_XIVE_TCTX)
+
+/*
+ * XIVE Thread interrupt Management register rings :
Quoted 8 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 254

Quoted 5 lines
include/hw/ppc/xive.h +5 -0
+ *
+ *   QW-0  User       event-based exception state
+ *   QW-1  O/S        OS context for priority management, interrupt acks
+ *   QW-2  Pool       hypervisor context for virtual processor being dispatched
+ *   QW-3  Physical   for the security monitor to manage the entire context

That last description is misleading, AIUI the hypervisor can and does make use of the physical ring as well as the pool ring.

yes. The description is from the spec. I will rephrase.

Quoted 16 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 265

Quoted 12 lines
include/hw/ppc/xive.h +11 -0
+ */
+#define TM_RING_COUNT           4
+#define TM_RING_SIZE            0x10
+
+typedef struct XiveTCTX {
+    DeviceState parent_obj;
+
+    CPUState    *cs;
+    qemu_irq    output;
+
+    uint8_t     regs[TM_RING_COUNT * TM_RING_SIZE];

I'm a bit dubious about representing the state with a full buffer like this. Isn't a fair bit of this space reserved or derived values which aren't backed by real state?

Quoted 5 lines

Under sPAPR only the TM_QW1_OS ring is accessed but the TM_QW0_USER will also be when we support EBB.

When running under the PowerNV machine, all rings could be accessed. Today only 2 and 3 are.

It's not the fact that all rings are exposed that I'm talking about. What I mean is that some of the logical register address space isn't actually by actually modifiable registers. For example the HW CAM bits are hardwired to a value which can be derived from elsewhere. Likewise the "highest pending priority" register or whatever it's called is AFAICT calculated on read from the CPPR and IPB.

So the thing I'm questioning is having actual storage bytes associated with logical registers which are actually derived from other state.

Quoted 10 lines

It seemed correct to expose all registers under the thread interrupt context model and filter the accesses with the TIMA. It fits the HW well.

Quoted 5 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 267

Quoted 2 lines
include/hw/ppc/xive.h +2 -0
+
+    XiveRouter  *xrtr;

What's this for? AFAIK a TCTX isn't associated with a particular routing unit.

Quoted 8 lines

I should have added the pointer in patch 11 where it is used. This is to let the sPAPR XIVE controller model reset the OS CAM line with the VP identifier.

The TCTX belong to the IVPE XIVE subengine and as the IVRE and IVPE are modeled under the XiveRouter, it's not suprising to see this '*xrtr' back pointer. But I agree we might not need. Let's talk about it when you reach patch 11.

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
#58 lore raw

Re: [Qemu-devel] [PATCH v5 07/36] ppc/xive: introduce the XIVE interrupt thread context

Quoted 89 lines · David Gibson
Quoted 88 lines · Cédric Le Goater
Quoted 56 lines · David Gibson
Quoted 55 lines · Cédric Le Goater

Each POWER9 processor chip has a XIVE presenter that can generate four different exceptions to its threads:

- hypervisor exception,
- O/S exception
- Event-Based Branch (EBB)
- msgsnd (doorbell).

Each exception has a state independent from the others called a Thread Interrupt Management context. This context is a set of registers which lets the thread handle priority management and interrupt acknowledgment among other things. The most important ones being :

  - Interrupt Priority Register  (PIPR)
  - Interrupt Pending Buffer     (IPB)
  - Current Processor Priority   (CPPR)

- Notification Source Register (NSR)

These registers are accessible through a specific MMIO region, called the Thread Interrupt Management Area (TIMA), four aligned pages, each exposing a different view of the registers. First page (page address ending in 0b00) gives access to the entire context and is reserved for the ring 0 security monitor. The second (page address ending in 0b01) is for the hypervisor, ring 1. The third (page address ending in 0b10) is for the operating system, ring 2. The fourth (page address ending in 0b11) is for user level, ring 3.

The thread interrupt context is modeled with a XiveTCTX object containing the values of the different exception registers. The TIMA region is mapped at the same address for each CPU.

---
 include/hw/ppc/xive.h      |  36 +++
 include/hw/ppc/xive_regs.h |  82 +++++++
 hw/intc/xive.c             | 443 +++++++++++++++++++++++++++++++++++++
 3 files changed, 561 insertions(+)
include/hw/ppc/xive.h +9 -0
index 24301bf2076d..5987f26ddb98 100644
@@ -238,4 +238,40 @@ typedef struct XiveENDSource {
 void xive_end_reset(XiveEND *end);
 void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon);
 
+/*
+ * XIVE Thread interrupt Management (TM) context
+ */
+
+#define TYPE_XIVE_TCTX "xive-tctx"
+#define XIVE_TCTX(obj) OBJECT_CHECK(XiveTCTX, (obj), TYPE_XIVE_TCTX)
+
+/*
+ * XIVE Thread interrupt Management register rings :
Quoted 8 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 254

Quoted 5 lines
include/hw/ppc/xive.h +5 -0
+ *
+ *   QW-0  User       event-based exception state
+ *   QW-1  O/S        OS context for priority management, interrupt acks
+ *   QW-2  Pool       hypervisor context for virtual processor being dispatched
+ *   QW-3  Physical   for the security monitor to manage the entire context

That last description is misleading, AIUI the hypervisor can and does make use of the physical ring as well as the pool ring.

yes. The description is from the spec. I will rephrase.

Quoted 16 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 265

Quoted 12 lines
include/hw/ppc/xive.h +11 -0
+ */
+#define TM_RING_COUNT           4
+#define TM_RING_SIZE            0x10
+
+typedef struct XiveTCTX {
+    DeviceState parent_obj;
+
+    CPUState    *cs;
+    qemu_irq    output;
+
+    uint8_t     regs[TM_RING_COUNT * TM_RING_SIZE];

I'm a bit dubious about representing the state with a full buffer like this. Isn't a fair bit of this space reserved or derived values which aren't backed by real state?

Under sPAPR only the TM_QW1_OS ring is accessed but the TM_QW0_USER will also be when we support EBB.

Quoted 7 lines
Quoted 2 lines

When running under the PowerNV machine, all rings could be accessed. Today only 2 and 3 are.

It's not the fact that all rings are exposed that I'm talking about. What I mean is that some of the logical register address space isn't actually by actually modifiable registers. For example the HW CAM bits are hardwired to a value which can be derived from elsewhere.

yes indeed.

Quoted 2 lines

Likewise the "highest pending priority" register or whatever it's called is AFAICT calculated on read from the CPPR and IPB.

yes. the PIPR is computed from the IPB. (patch 10)

Quoted 2 lines

So the thing I'm questioning is having actual storage bytes associated with logical registers which are actually derived from other state.

having storage for word2 and word3 on all rings is questionable I agree, but I think the eight (NSR CPPR IPB LSMFB ACK# INC AGE PIPR) registers are good to have explicitly for the model.

It fits well the HW and all the TIMA accessors which would be much more complex if we only represented the interesting bits we need to transfer.

We also use the word0 and word1 to store the KVM state (os ring, maybe user one day) directly pulled from the KVM VCPU structure. word2 is also collected to print out the KVM VP identifier in the QEMU monitor.

So, I think these four quad-words are useful and their storage is relatively small compared to the EAT (8bytes * nr_irqs) and ENDT (32bytes * 8 * nr_cpus).

Thanks,

C.

Quoted 21 lines
Quoted 10 lines

It seemed correct to expose all registers under the thread interrupt context model and filter the accesses with the TIMA. It fits the HW well.

Quoted 5 lines

include/hw/ppc/xive.h typedef struct XiveENDSource { · line 267

Quoted 2 lines
include/hw/ppc/xive.h +2 -0
+
+    XiveRouter  *xrtr;

What's this for? AFAIK a TCTX isn't associated with a particular routing unit.

Quoted 8 lines

I should have added the pointer in patch 11 where it is used. This is to let the sPAPR XIVE controller model reset the OS CAM line with the VP identifier.

The TCTX belong to the IVPE XIVE subengine and as the IVRE and IVPE are modeled under the XiveRouter, it's not suprising to see this '*xrtr' back pointer. But I agree we might not need. Let's talk about it when you reach patch 11.

Ok.

#59 lore raw

PATCH v5 8/36 ppc/xive: introduce a simplified XIVE presenter

In reply to Cédric Le Goater

The last sub-engine of the XIVE architecture is the Interrupt Virtualization Presentation Engine (IVPE). On HW, they share elements, the Power Bus interface (CQ), the routing table descriptors, and they can be combined in the same HW logic. We do the same in QEMU and combine both engines in the XiveRouter for simplicity.

When the IVRE has completed its job of matching an event source with a Notification Virtual Target (NVT) to notify, it forwards the event notification to the IVPE sub-engine. The IVPE scans the thread interrupt contexts of the Notification Virtual Targets (NVT) dispatched on the HW processor threads and if a match is found, it signals the thread. If not, the IVPE escalates the notification to some other targets and records the notification in a backlog queue.

The IVPE maintains the thread interrupt context state for each of its NVTs not dispatched on HW processor threads in the Notification Virtual Target table (NVTT).

The model currently only supports single NVT notifications.

---
 include/hw/ppc/xive.h      |  13 +++
 include/hw/ppc/xive_regs.h |  22 ++++
 hw/intc/xive.c             | 223 +++++++++++++++++++++++++++++++++++++
 3 files changed, 258 insertions(+)
include/hw/ppc/xive.h +13 -0
index 5987f26ddb98..e715a6c6923d 100644
@@ -197,6 +197,10 @@ typedef struct XiveRouterClass {
                    XiveEND *end);
     int (*set_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                    XiveEND *end);
+    int (*get_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                   XiveNVT *nvt);
+    int (*set_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                   XiveNVT *nvt);
 } XiveRouterClass;
 
 void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
@@ -207,6 +211,10 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
+int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt);
+int xive_router_set_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt);
 
 /*
  * XIVE END ESBs
@@ -274,4 +282,9 @@ extern const MemoryRegionOps xive_tm_ops;
 
 void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon);
 
+static inline uint32_t xive_tctx_cam_line(uint8_t nvt_blk, uint32_t nvt_idx)
+{
+    return (nvt_blk << 19) | nvt_idx;
+}
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +22 -0
index 2e3d6cb507da..05cb992d2815 100644
@@ -158,4 +158,26 @@ typedef struct XiveEND {
 #define END_W7_F1_LOG_SERVER_ID  PPC_BITMASK32(1, 31)
 } XiveEND;
 
+/* Notification Virtual Target (NVT) */
+typedef struct XiveNVT {
+        uint32_t        w0;
+#define NVT_W0_VALID             PPC_BIT32(0)
+        uint32_t        w1;
+        uint32_t        w2;
+        uint32_t        w3;
+        uint32_t        w4;
+        uint32_t        w5;
+        uint32_t        w6;
+        uint32_t        w7;
+        uint32_t        w8;
+#define NVT_W8_GRP_VALID         PPC_BIT32(0)
+        uint32_t        w9;
+        uint32_t        wa;
+        uint32_t        wb;
+        uint32_t        wc;
+        uint32_t        wd;
+        uint32_t        we;
+        uint32_t        wf;
+} XiveNVT;
+
 #endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +223 -0

241 lines not on this page · raw message

Signature · 1 line
2.17.2
#60 lore raw

Re: [Qemu-devel] [PATCH v5 08/36] ppc/xive: introduce a simplified XIVE presenter

Quoted 5 lines · Cédric Le Goater

The last sub-engine of the XIVE architecture is the Interrupt Virtualization Presentation Engine (IVPE). On HW, they share elements, the Power Bus interface (CQ), the routing table descriptors, and they can be combined in the same HW logic. We do the same in QEMU and combine both engines in the XiveRouter for simplicity.

Ok, I'm not entirely convinced combining the IVPE and IVRE into a single object is a good idea, but we can probably discuss that once I've read further.

Quoted 26 lines

When the IVRE has completed its job of matching an event source with a Notification Virtual Target (NVT) to notify, it forwards the event notification to the IVPE sub-engine. The IVPE scans the thread interrupt contexts of the Notification Virtual Targets (NVT) dispatched on the HW processor threads and if a match is found, it signals the thread. If not, the IVPE escalates the notification to some other targets and records the notification in a backlog queue.

The IVPE maintains the thread interrupt context state for each of its NVTs not dispatched on HW processor threads in the Notification Virtual Target table (NVTT).

The model currently only supports single NVT notifications.

---
 include/hw/ppc/xive.h      |  13 +++
 include/hw/ppc/xive_regs.h |  22 ++++
 hw/intc/xive.c             | 223 +++++++++++++++++++++++++++++++++++++
 3 files changed, 258 insertions(+)
include/hw/ppc/xive.h +0 -0
index 5987f26ddb98..e715a6c6923d 100644

include/hw/ppc/xive.h typedef struct XiveRouterClass { · line 203

Quoted 7 lines
include/hw/ppc/xive.h +4 -0
@@ -197,6 +197,10 @@ typedef struct XiveRouterClass {
                    XiveEND *end);
     int (*set_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                    XiveEND *end);
+    int (*get_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                   XiveNVT *nvt);
+    int (*set_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                   XiveNVT *nvt);

As with the ENDs, I don't think get/set is a good interface for a bigger-than-word-size object.

Quoted 15 lines
include/hw/ppc/xive.h +4 -0
 } XiveRouterClass;
 
 void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
@@ -207,6 +211,10 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
+int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt);
+int xive_router_set_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt);
 
 /*
  * XIVE END ESBs

include/hw/ppc/xive.h extern const MemoryRegionOps xive_tm_ops; · line 287

Quoted 6 lines
include/hw/ppc/xive.h +3 -0
@@ -274,4 +282,9 @@ extern const MemoryRegionOps xive_tm_ops;
 
 void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon);
 
+static inline uint32_t xive_tctx_cam_line(uint8_t nvt_blk, uint32_t nvt_idx)
+{
+    return (nvt_blk << 19) | nvt_idx;

I'm guessing this formula is the standard way of combining the NVT block and index into a single word? If so, I think we should standardize on passing a single word "nvt_id" around and only splitting it when we need to use the block separately. Same goes for the end_id, assuming there's a standard way of putting that into a single word. That will address the point I raised earlier about lisn being passed around as a single word, but these later stage ids being split.

We'll probably want some inlines or macros to build an nvt/end/lisn/whatever id from block and index as well.

Quoted 54 lines
include/hw/ppc/xive.h +2 -0
+}
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +22 -0
index 2e3d6cb507da..05cb992d2815 100644
@@ -158,4 +158,26 @@ typedef struct XiveEND {
 #define END_W7_F1_LOG_SERVER_ID  PPC_BITMASK32(1, 31)
 } XiveEND;
 
+/* Notification Virtual Target (NVT) */
+typedef struct XiveNVT {
+        uint32_t        w0;
+#define NVT_W0_VALID             PPC_BIT32(0)
+        uint32_t        w1;
+        uint32_t        w2;
+        uint32_t        w3;
+        uint32_t        w4;
+        uint32_t        w5;
+        uint32_t        w6;
+        uint32_t        w7;
+        uint32_t        w8;
+#define NVT_W8_GRP_VALID         PPC_BIT32(0)
+        uint32_t        w9;
+        uint32_t        wa;
+        uint32_t        wb;
+        uint32_t        wc;
+        uint32_t        wd;
+        uint32_t        we;
+        uint32_t        wf;
+} XiveNVT;
+
 #endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +12 -0
index 4c6cb5d52975..5ba3b06e6e25 100644
@@ -373,6 +373,32 @@ void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon)
     }
 }
 
+/* The HW CAM (23bits) is hardwired to :
+ *
+ *   0x000||0b1||4Bit chip number||7Bit Thread number.
+ *
+ * and when the block grouping extension is enabled :
+ *
+ *   4Bit chip number||0x001||7Bit Thread number.
+ */
+static uint32_t tctx_hw_cam_line(bool block_group, uint8_t chip_id, uint8_t tid)
+{
+    if (block_group) {
+        return 1 << 11 | (chip_id & 0xf) << 7 | (tid & 0x7f);

hw/intc/xive.c void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon) · line 397

Quoted 10 lines
hw/intc/xive.c +10 -0
+    } else {
+        return (chip_id & 0xf) << 11 | 1 << 7 | (tid & 0x7f);
+    }
+}
+
+static uint32_t xive_tctx_hw_cam_line(XiveTCTX *tctx, bool block_group)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(tctx->cs);
+    CPUPPCState *env = &cpu->env;
+    uint32_t pir = env->spr_cb[SPR_PIR].default_value;

I don't much like reaching into the cpu state itself. I think a better idea would be to have the TCTX have its HW CAM id set during initialization (via a property) and then use that. This will mean less mucking about if future cpu revisions don't split the PIR into chip and tid ids in the same way.

Quoted 31 lines
hw/intc/xive.c +24 -0
+    return tctx_hw_cam_line(block_group, (pir >> 8) & 0xf, pir & 0x7f);
+}
+
 static void xive_tctx_reset(void *dev)
 {
     XiveTCTX *tctx = XIVE_TCTX(dev);
@@ -1013,6 +1039,195 @@ int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
    return xrc->set_end(xrtr, end_blk, end_idx, end);
 }
 
+int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->get_nvt(xrtr, nvt_blk, nvt_idx, nvt);
+}
+
+int xive_router_set_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->set_nvt(xrtr, nvt_blk, nvt_idx, nvt);
+}
+
+static bool xive_tctx_ring_match(XiveTCTX *tctx, uint8_t ring,
+                                 uint8_t nvt_blk, uint32_t nvt_idx,
+                                 bool cam_ignore, uint32_t logic_serv)
+{
+    uint8_t *regs = &tctx->regs[ring];

hw/intc/xive.c int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 1072

Quoted 10 lines
hw/intc/xive.c +10 -0
+    uint32_t w2 = be32_to_cpu(*((uint32_t *) &regs[TM_WORD2]));
+    uint32_t cam = xive_tctx_cam_line(nvt_blk, nvt_idx);
+    bool block_group = false; /* TODO (PowerNV) */
+
+    /* TODO (PowerNV): ignore low order bits of nvt id */
+
+    switch (ring) {
+    case TM_QW3_HV_PHYS:
+        return (w2 & TM_QW3W2_VT) && xive_tctx_hw_cam_line(tctx, block_group) ==
+            tctx_hw_cam_line(block_group, nvt_blk, nvt_idx);

The difference between "xive_tctx_hw_cam_line" and "tctx_hw_cam_line" here is far from obvious. Remember that namespacing prefixes aren't necessary for static functions, which can let you give more descriptive names without getting excessively long.

Quoted 31 lines
hw/intc/xive.c +31 -0
+    case TM_QW2_HV_POOL:
+        return (w2 & TM_QW2W2_VP) && (cam == GETFIELD(TM_QW2W2_POOL_CAM, w2));
+
+    case TM_QW1_OS:
+        return (w2 & TM_QW1W2_VO) && (cam == GETFIELD(TM_QW1W2_OS_CAM, w2));
+
+    case TM_QW0_USER:
+        return ((w2 & TM_QW1W2_VO) && (cam == GETFIELD(TM_QW1W2_OS_CAM, w2)) &&
+                (w2 & TM_QW0W2_VU) &&
+                (logic_serv == GETFIELD(TM_QW0W2_LOGIC_SERV, w2)));
+
+    default:
+        g_assert_not_reached();
+    }
+}
+
+static int xive_presenter_tctx_match(XiveTCTX *tctx, uint8_t format,
+                                     uint8_t nvt_blk, uint32_t nvt_idx,
+                                     bool cam_ignore, uint32_t logic_serv)
+{
+    if (format == 0) {
+        /* F=0 & i=1: Logical server notification */
+        if (cam_ignore == true) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no support for LS "
+                          "NVT %x/%x\n", nvt_blk, nvt_idx);
+             return -1;
+        }
+
+        /* F=0 & i=0: Specific NVT notification */
+        if (xive_tctx_ring_match(tctx, TM_QW3_HV_PHYS,
+                                nvt_blk, nvt_idx, false, 0)) {

hw/intc/xive.c int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 1114

Quoted 10 lines
hw/intc/xive.c +10 -0
+            return TM_QW3_HV_PHYS;
+        }
+        if (xive_tctx_ring_match(tctx, TM_QW2_HV_POOL,
+                                nvt_blk, nvt_idx, false, 0)) {
+            return TM_QW2_HV_POOL;
+        }
+        if (xive_tctx_ring_match(tctx, TM_QW1_OS,
+                                nvt_blk, nvt_idx, false, 0)) {
+            return TM_QW1_OS;
+        }

Hm. It's a bit pointless to iterate through each ring calling a common function, when that "common" function consists entirely of a switch which makes it not really common at all.

So I think you want separate helper functions for each ring's match, or even just fold the previous function into this one.

Quoted 51 lines
hw/intc/xive.c +51 -0
+    } else {
+        /* F=1 : User level Event-Based Branch (EBB) notification */
+        if (xive_tctx_ring_match(tctx, TM_QW0_USER,
+                                nvt_blk, nvt_idx, false, logic_serv)) {
+            return TM_QW0_USER;
+        }
+    }
+    return -1;
+}
+
+typedef struct XiveTCTXMatch {
+    XiveTCTX *tctx;
+    uint8_t ring;
+} XiveTCTXMatch;
+
+static bool xive_presenter_match(XiveRouter *xrtr, uint8_t format,
+                                 uint8_t nvt_blk, uint32_t nvt_idx,
+                                 bool cam_ignore, uint8_t priority,
+                                 uint32_t logic_serv, XiveTCTXMatch *match)
+{
+    CPUState *cs;
+
+    /* TODO (PowerNV): handle chip_id overwrite of block field for
+     * hardwired CAM compares */
+
+    CPU_FOREACH(cs) {
+        PowerPCCPU *cpu = POWERPC_CPU(cs);
+        XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
+        int ring;
+
+        /*
+         * HW checks that the CPU is enabled in the Physical Thread
+         * Enable Register (PTER).
+         */
+
+        /*
+         * Check the thread context CAM lines and record matches. We
+         * will handle CPU exception delivery later
+         */
+        ring = xive_presenter_tctx_match(tctx, format, nvt_blk, nvt_idx,
+                                         cam_ignore, logic_serv);
+        /*
+         * Save the context and follow on to catch duplicates, that we
+         * don't support yet.
+         */
+        if (ring != -1) {
+            if (match->tctx) {
+                qemu_log_mask(LOG_GUEST_ERROR, "XIVE: already found a thread "
+                              "context NVT %x/%x\n", nvt_blk, nvt_idx);
+                return false;
+            }

hw/intc/xive.c int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 1175

Quoted 10 lines
hw/intc/xive.c +10 -0
+
+            match->ring = ring;
+            match->tctx = tctx;
+        }
+    }
+
+    if (!match->tctx) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is not dispatched\n",
+                      nvt_blk, nvt_idx);
+        return false;

Hmm.. this isn't actually an error isn't it? At least not for powernv
- that just means the NVT isn't currently dispatched, so we'll need to trigger the escalation interrupt. Does this get changed later in the series?

Quoted 73 lines
hw/intc/xive.c +63 -0
+    }
+
+    return true;
+}
+
+/*
+ * This is our simple Xive Presenter Engine model. It is merged in the
+ * Router as it does not require an extra object.
+ *
+ * It receives notification requests sent by the IVRE to find one
+ * matching NVT (or more) dispatched on the processor threads. In case
+ * of a single NVT notification, the process is abreviated and the
+ * thread is signaled if a match is found. In case of a logical server
+ * notification (bits ignored at the end of the NVT identifier), the
+ * IVPE and IVRE select a winning thread using different filters. This
+ * involves 2 or 3 exchanges on the PowerBus that the model does not
+ * support.
+ *
+ * The parameters represent what is sent on the PowerBus
+ */
+static void xive_presenter_notify(XiveRouter *xrtr, uint8_t format,
+                                  uint8_t nvt_blk, uint32_t nvt_idx,
+                                  bool cam_ignore, uint8_t priority,
+                                  uint32_t logic_serv)
+{
+    XiveNVT nvt;
+    XiveTCTXMatch match = { 0 };
+    bool found;
+
+    /* NVT cache lookup */
+    if (xive_router_get_nvt(xrtr, nvt_blk, nvt_idx, &nvt)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no NVT %x/%x\n",
+                      nvt_blk, nvt_idx);
+        return;
+    }
+
+    if (!(nvt.w0 & NVT_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is invalid\n",
+                      nvt_blk, nvt_idx);
+        return;
+    }
+
+    found = xive_presenter_match(xrtr, format, nvt_blk, nvt_idx, cam_ignore,
+                                 priority, logic_serv, &match);
+    if (found) {
+        return;
+    }
+
+    /* If no matching NVT is dispatched on a HW thread :
+     * - update the NVT structure if backlog is activated
+     * - escalate (ESe PQ bits and EAS in w4-5) if escalation is
+     *   activated
+     */
+}
+
 /*
  * An END trigger can come from an event trigger (IPI or HW) or from
  * another chip. We don't model the PowerBus but the END trigger
@@ -1081,6 +1296,14 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
     /*
      * Follows IVPE notification
      */
+    xive_presenter_notify(xrtr, format,
+                          GETFIELD(END_W6_NVT_BLOCK, end.w6),
+                          GETFIELD(END_W6_NVT_INDEX, end.w6),
+                          GETFIELD(END_W7_F0_IGNORE, end.w7),
+                          priority,
+                          GETFIELD(END_W7_F1_LOG_SERVER_ID, end.w7));
+
+    /* TODO: Auto EOI. */
 }
 
 static void xive_router_notify(XiveFabric *xf, uint32_t lisn)
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
#61 lore raw

Re: [Qemu-devel] [PATCH v5 08/36] ppc/xive: introduce a simplified XIVE presenter

Quoted 10 lines · David Gibson
Quoted 5 lines · Cédric Le Goater

The last sub-engine of the XIVE architecture is the Interrupt Virtualization Presentation Engine (IVPE). On HW, they share elements, the Power Bus interface (CQ), the routing table descriptors, and they can be combined in the same HW logic. We do the same in QEMU and combine both engines in the XiveRouter for simplicity.

Ok, I'm not entirely convinced combining the IVPE and IVRE into a single object is a good idea, but we can probably discuss that once I've read further.

Keep in mind that the communication between the two is a bit more hairy than simple notifications, though. Especially once we start implementing escalation interrupts or worse, groups...

Quoted 378 lines
Quoted 26 lines

When the IVRE has completed its job of matching an event source with a Notification Virtual Target (NVT) to notify, it forwards the event notification to the IVPE sub-engine. The IVPE scans the thread interrupt contexts of the Notification Virtual Targets (NVT) dispatched on the HW processor threads and if a match is found, it signals the thread. If not, the IVPE escalates the notification to some other targets and records the notification in a backlog queue.

The IVPE maintains the thread interrupt context state for each of its NVTs not dispatched on HW processor threads in the Notification Virtual Target table (NVTT).

The model currently only supports single NVT notifications.

---
 include/hw/ppc/xive.h      |  13 +++
 include/hw/ppc/xive_regs.h |  22 ++++
 hw/intc/xive.c             | 223 +++++++++++++++++++++++++++++++++++++
 3 files changed, 258 insertions(+)
include/hw/ppc/xive.h +0 -0
index 5987f26ddb98..e715a6c6923d 100644

include/hw/ppc/xive.h typedef struct XiveRouterClass { · line 203

Quoted 7 lines
include/hw/ppc/xive.h +4 -0
@@ -197,6 +197,10 @@ typedef struct XiveRouterClass {
                    XiveEND *end);
     int (*set_end)(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                    XiveEND *end);
+    int (*get_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                   XiveNVT *nvt);
+    int (*set_nvt)(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                   XiveNVT *nvt);

As with the ENDs, I don't think get/set is a good interface for a bigger-than-word-size object.

Quoted 15 lines
include/hw/ppc/xive.h +4 -0
 } XiveRouterClass;
 
 void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon);
@@ -207,6 +211,10 @@ int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
 int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
                         XiveEND *end);
+int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt);
+int xive_router_set_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt);
 
 /*
  * XIVE END ESBs

include/hw/ppc/xive.h extern const MemoryRegionOps xive_tm_ops; · line 287

Quoted 6 lines
include/hw/ppc/xive.h +3 -0
@@ -274,4 +282,9 @@ extern const MemoryRegionOps xive_tm_ops;
 
 void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon);
 
+static inline uint32_t xive_tctx_cam_line(uint8_t nvt_blk, uint32_t nvt_idx)
+{
+    return (nvt_blk << 19) | nvt_idx;

I'm guessing this formula is the standard way of combining the NVT block and index into a single word? If so, I think we should standardize on passing a single word "nvt_id" around and only splitting it when we need to use the block separately. Same goes for the end_id, assuming there's a standard way of putting that into a single word. That will address the point I raised earlier about lisn being passed around as a single word, but these later stage ids being split.

We'll probably want some inlines or macros to build an nvt/end/lisn/whatever id from block and index as well.

Quoted 54 lines
include/hw/ppc/xive.h +2 -0
+}
+
 #endif /* PPC_XIVE_H */
include/hw/ppc/xive_regs.h +22 -0
index 2e3d6cb507da..05cb992d2815 100644
@@ -158,4 +158,26 @@ typedef struct XiveEND {
 #define END_W7_F1_LOG_SERVER_ID  PPC_BITMASK32(1, 31)
 } XiveEND;
 
+/* Notification Virtual Target (NVT) */
+typedef struct XiveNVT {
+        uint32_t        w0;
+#define NVT_W0_VALID             PPC_BIT32(0)
+        uint32_t        w1;
+        uint32_t        w2;
+        uint32_t        w3;
+        uint32_t        w4;
+        uint32_t        w5;
+        uint32_t        w6;
+        uint32_t        w7;
+        uint32_t        w8;
+#define NVT_W8_GRP_VALID         PPC_BIT32(0)
+        uint32_t        w9;
+        uint32_t        wa;
+        uint32_t        wb;
+        uint32_t        wc;
+        uint32_t        wd;
+        uint32_t        we;
+        uint32_t        wf;
+} XiveNVT;
+
 #endif /* PPC_XIVE_REGS_H */
hw/intc/xive.c +12 -0
index 4c6cb5d52975..5ba3b06e6e25 100644
@@ -373,6 +373,32 @@ void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon)
     }
 }
 
+/* The HW CAM (23bits) is hardwired to :
+ *
+ *   0x000||0b1||4Bit chip number||7Bit Thread number.
+ *
+ * and when the block grouping extension is enabled :
+ *
+ *   4Bit chip number||0x001||7Bit Thread number.
+ */
+static uint32_t tctx_hw_cam_line(bool block_group, uint8_t chip_id, uint8_t tid)
+{
+    if (block_group) {
+        return 1 << 11 | (chip_id & 0xf) << 7 | (tid & 0x7f);

hw/intc/xive.c void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon) · line 397

Quoted 10 lines
hw/intc/xive.c +10 -0
+    } else {
+        return (chip_id & 0xf) << 11 | 1 << 7 | (tid & 0x7f);
+    }
+}
+
+static uint32_t xive_tctx_hw_cam_line(XiveTCTX *tctx, bool block_group)
+{
+    PowerPCCPU *cpu = POWERPC_CPU(tctx->cs);
+    CPUPPCState *env = &cpu->env;
+    uint32_t pir = env->spr_cb[SPR_PIR].default_value;

I don't much like reaching into the cpu state itself. I think a better idea would be to have the TCTX have its HW CAM id set during initialization (via a property) and then use that. This will mean less mucking about if future cpu revisions don't split the PIR into chip and tid ids in the same way.

Quoted 31 lines
hw/intc/xive.c +24 -0
+    return tctx_hw_cam_line(block_group, (pir >> 8) & 0xf, pir & 0x7f);
+}
+
 static void xive_tctx_reset(void *dev)
 {
     XiveTCTX *tctx = XIVE_TCTX(dev);
@@ -1013,6 +1039,195 @@ int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
    return xrc->set_end(xrtr, end_blk, end_idx, end);
 }
 
+int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->get_nvt(xrtr, nvt_blk, nvt_idx, nvt);
+}
+
+int xive_router_set_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
+                        XiveNVT *nvt)
+{
+   XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
+
+   return xrc->set_nvt(xrtr, nvt_blk, nvt_idx, nvt);
+}
+
+static bool xive_tctx_ring_match(XiveTCTX *tctx, uint8_t ring,
+                                 uint8_t nvt_blk, uint32_t nvt_idx,
+                                 bool cam_ignore, uint32_t logic_serv)
+{
+    uint8_t *regs = &tctx->regs[ring];

hw/intc/xive.c int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 1072

Quoted 10 lines
hw/intc/xive.c +10 -0
+    uint32_t w2 = be32_to_cpu(*((uint32_t *) &regs[TM_WORD2]));
+    uint32_t cam = xive_tctx_cam_line(nvt_blk, nvt_idx);
+    bool block_group = false; /* TODO (PowerNV) */
+
+    /* TODO (PowerNV): ignore low order bits of nvt id */
+
+    switch (ring) {
+    case TM_QW3_HV_PHYS:
+        return (w2 & TM_QW3W2_VT) && xive_tctx_hw_cam_line(tctx, block_group) ==
+            tctx_hw_cam_line(block_group, nvt_blk, nvt_idx);

The difference between "xive_tctx_hw_cam_line" and "tctx_hw_cam_line" here is far from obvious. Remember that namespacing prefixes aren't necessary for static functions, which can let you give more descriptive names without getting excessively long.

Quoted 31 lines
hw/intc/xive.c +31 -0
+    case TM_QW2_HV_POOL:
+        return (w2 & TM_QW2W2_VP) && (cam == GETFIELD(TM_QW2W2_POOL_CAM, w2));
+
+    case TM_QW1_OS:
+        return (w2 & TM_QW1W2_VO) && (cam == GETFIELD(TM_QW1W2_OS_CAM, w2));
+
+    case TM_QW0_USER:
+        return ((w2 & TM_QW1W2_VO) && (cam == GETFIELD(TM_QW1W2_OS_CAM, w2)) &&
+                (w2 & TM_QW0W2_VU) &&
+                (logic_serv == GETFIELD(TM_QW0W2_LOGIC_SERV, w2)));
+
+    default:
+        g_assert_not_reached();
+    }
+}
+
+static int xive_presenter_tctx_match(XiveTCTX *tctx, uint8_t format,
+                                     uint8_t nvt_blk, uint32_t nvt_idx,
+                                     bool cam_ignore, uint32_t logic_serv)
+{
+    if (format == 0) {
+        /* F=0 & i=1: Logical server notification */
+        if (cam_ignore == true) {
+            qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no support for LS "
+                          "NVT %x/%x\n", nvt_blk, nvt_idx);
+             return -1;
+        }
+
+        /* F=0 & i=0: Specific NVT notification */
+        if (xive_tctx_ring_match(tctx, TM_QW3_HV_PHYS,
+                                nvt_blk, nvt_idx, false, 0)) {

hw/intc/xive.c int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 1114

Quoted 10 lines
hw/intc/xive.c +10 -0
+            return TM_QW3_HV_PHYS;
+        }
+        if (xive_tctx_ring_match(tctx, TM_QW2_HV_POOL,
+                                nvt_blk, nvt_idx, false, 0)) {
+            return TM_QW2_HV_POOL;
+        }
+        if (xive_tctx_ring_match(tctx, TM_QW1_OS,
+                                nvt_blk, nvt_idx, false, 0)) {
+            return TM_QW1_OS;
+        }

Hm. It's a bit pointless to iterate through each ring calling a common function, when that "common" function consists entirely of a switch which makes it not really common at all.

So I think you want separate helper functions for each ring's match, or even just fold the previous function into this one.

Quoted 51 lines
hw/intc/xive.c +51 -0
+    } else {
+        /* F=1 : User level Event-Based Branch (EBB) notification */
+        if (xive_tctx_ring_match(tctx, TM_QW0_USER,
+                                nvt_blk, nvt_idx, false, logic_serv)) {
+            return TM_QW0_USER;
+        }
+    }
+    return -1;
+}
+
+typedef struct XiveTCTXMatch {
+    XiveTCTX *tctx;
+    uint8_t ring;
+} XiveTCTXMatch;
+
+static bool xive_presenter_match(XiveRouter *xrtr, uint8_t format,
+                                 uint8_t nvt_blk, uint32_t nvt_idx,
+                                 bool cam_ignore, uint8_t priority,
+                                 uint32_t logic_serv, XiveTCTXMatch *match)
+{
+    CPUState *cs;
+
+    /* TODO (PowerNV): handle chip_id overwrite of block field for
+     * hardwired CAM compares */
+
+    CPU_FOREACH(cs) {
+        PowerPCCPU *cpu = POWERPC_CPU(cs);
+        XiveTCTX *tctx = XIVE_TCTX(cpu->intc);
+        int ring;
+
+        /*
+         * HW checks that the CPU is enabled in the Physical Thread
+         * Enable Register (PTER).
+         */
+
+        /*
+         * Check the thread context CAM lines and record matches. We
+         * will handle CPU exception delivery later
+         */
+        ring = xive_presenter_tctx_match(tctx, format, nvt_blk, nvt_idx,
+                                         cam_ignore, logic_serv);
+        /*
+         * Save the context and follow on to catch duplicates, that we
+         * don't support yet.
+         */
+        if (ring != -1) {
+            if (match->tctx) {
+                qemu_log_mask(LOG_GUEST_ERROR, "XIVE: already found a thread "
+                              "context NVT %x/%x\n", nvt_blk, nvt_idx);
+                return false;
+            }

hw/intc/xive.c int xive_router_set_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx, · line 1175

Quoted 10 lines
hw/intc/xive.c +10 -0
+
+            match->ring = ring;
+            match->tctx = tctx;
+        }
+    }
+
+    if (!match->tctx) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is not dispatched\n",
+                      nvt_blk, nvt_idx);
+        return false;

Hmm.. this isn't actually an error isn't it? At least not for powernv
- that just means the NVT isn't currently dispatched, so we'll need to trigger the escalation interrupt. Does this get changed later in the series?

Quoted 73 lines
hw/intc/xive.c +63 -0
+    }
+
+    return true;
+}
+
+/*
+ * This is our simple Xive Presenter Engine model. It is merged in the
+ * Router as it does not require an extra object.
+ *
+ * It receives notification requests sent by the IVRE to find one
+ * matching NVT (or more) dispatched on the processor threads. In case
+ * of a single NVT notification, the process is abreviated and the
+ * thread is signaled if a match is found. In case of a logical server
+ * notification (bits ignored at the end of the NVT identifier), the
+ * IVPE and IVRE select a winning thread using different filters. This
+ * involves 2 or 3 exchanges on the PowerBus that the model does not
+ * support.
+ *
+ * The parameters represent what is sent on the PowerBus
+ */
+static void xive_presenter_notify(XiveRouter *xrtr, uint8_t format,
+                                  uint8_t nvt_blk, uint32_t nvt_idx,
+                                  bool cam_ignore, uint8_t priority,
+                                  uint32_t logic_serv)
+{
+    XiveNVT nvt;
+    XiveTCTXMatch match = { 0 };
+    bool found;
+
+    /* NVT cache lookup */
+    if (xive_router_get_nvt(xrtr, nvt_blk, nvt_idx, &nvt)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no NVT %x/%x\n",
+                      nvt_blk, nvt_idx);
+        return;
+    }
+
+    if (!(nvt.w0 & NVT_W0_VALID)) {
+        qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is invalid\n",
+                      nvt_blk, nvt_idx);
+        return;
+    }
+
+    found = xive_presenter_match(xrtr, format, nvt_blk, nvt_idx, cam_ignore,
+                                 priority, logic_serv, &match);
+    if (found) {
+        return;
+    }
+
+    /* If no matching NVT is dispatched on a HW thread :
+     * - update the NVT structure if backlog is activated
+     * - escalate (ESe PQ bits and EAS in w4-5) if escalation is
+     *   activated
+     */
+}
+
 /*
  * An END trigger can come from an event trigger (IPI or HW) or from
  * another chip. We don't model the PowerBus but the END trigger
@@ -1081,6 +1296,14 @@ static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
     /*
      * Follows IVPE notification
      */
+    xive_presenter_notify(xrtr, format,
+                          GETFIELD(END_W6_NVT_BLOCK, end.w6),
+                          GETFIELD(END_W6_NVT_INDEX, end.w6),
+                          GETFIELD(END_W7_F0_IGNORE, end.w7),
+                          priority,
+                          GETFIELD(END_W7_F1_LOG_SERVER_ID, end.w7));
+
+    /* TODO: Auto EOI. */
 }
 
 static void xive_router_notify(XiveFabric *xf, uint32_t lisn)