Enable QEMU NVMe userspace driver on s390x
Subsystems (2)
cover Enable QEMU NVMe userspace driver on s390x
Hi,
Recently on s390x we have enabled mmap support for vfio-pci devices [1]. This allows us to take advantage and use userspace drivers on s390x. However, on s390x we have special instructions for MMIO access. Starting with z15 (and newer platforms) we have new PCI Memory I/O (MIO) instructions which operate on virtually mapped PCI memory spaces, and can be used from userspace. On older platforms we would fallback to using existing system calls for MMIO access.
This patch series introduces support the PCI MIO instructions, and enables s390x support for the userspace NVMe driver on s390x. I would appreciate any review/feedback on the patches.
Thanks
Farhan
[1] https://lore.kernel.org/linux-s390/[email protected]/
Farhan Ali (2):
util: Add functions for s390x mmio read/write block/nvme: Enable NVMe userspace driver for s390x
block/nvme.c | 95 ++++++++++++++++++++++++------ include/qemu/s390x_pci_mmio.h | 17 ++++++ util/meson.build | 2 + util/s390x_pci_mmio.c | 105 ++++++++++++++++++++++++++++++++++ 4 files changed, 201 insertions(+), 18 deletions(-) create mode 100644 include/qemu/s390x_pci_mmio.h create mode 100644 util/s390x_pci_mmio.c
Signature · 1 line
2.43.0
PATCH 1/2 util: Add functions for s390x mmio read/write
Starting with z15 (or newer) we can execute mmio
instructions from userspace. On older platforms
where we don't have these instructions available
we can fallback to using system calls to access
the PCI mapped resources.
This patch adds helper functions for mmio reads
and writes for s390x.
--- include/qemu/s390x_pci_mmio.h | 17 ++++++ util/meson.build | 2 + util/s390x_pci_mmio.c | 105 ++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 include/qemu/s390x_pci_mmio.h create mode 100644 util/s390x_pci_mmio.c
include/qemu/s390x_pci_mmio.h new file +17 -0
@@ -0,0 +1,17 @@ +/* +* s390x PCI MMIO definitions +* +* Copyright 2025 IBM Corp. +* Author(s): Farhan Ali <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef S390X_PCI_MMIO_H +#define S390X_PCI_MMIO_H + +uint64_t s390x_pci_mmio_read_64 (const void *ioaddr); +uint32_t s390x_pci_mmio_read_32 (const void *ioaddr); +void s390x_pci_mmio_write_64 (void *ioaddr,uint64_t val); +void s390x_pci_mmio_write_32 (void *ioaddr,uint32_t val); + +#endif
util/meson.build +2 -0
@@ -131,4 +131,6 @@ elif cpu in ['ppc', 'ppc64'] util_ss.add(files('cpuinfo-ppc.c')) elif cpu in ['riscv32', 'riscv64'] util_ss.add(files('cpuinfo-riscv.c')) +elif cpu == 's390x' + util_ss.add(files('s390x_pci_mmio.c')) endif
util/s390x_pci_mmio.c new file +105 -0
105 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 2/2 block/nvme: Enable NVMe userspace driver for s390x
In reply to Farhan AliOn s390x we can now support userspace mmio and mmap from vfio. This patch uses s390x mmio support to enable the NVMe userspace driver for s390x.
--- block/nvme.c | 95 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 18 deletions(-)
block/nvme.c +77 -18
@@ -24,6 +24,9 @@#include "qemu/option.h" #include "qemu/memalign.h" #include "qemu/vfio-helpers.h" +#ifdef __s390x__ +#include "qemu/s390x_pci_mmio.h" +#endif #include "block/block-io.h" #include "block/block_int.h" #include "system/block-backend.h"
@@ -60,7 +63,7 @@ typedef struct {uint8_t *queue;uint64_t iova;/* Hardware MMIO register */ -volatile uint32_t *doorbell; +uint32_t *doorbell; } NVMeQueue; typedef struct {
@@ -100,7 +103,7 @@ struct BDRVNVMeState {QEMUVFIOState *vfio;void *bar0_wo_map;/* Memory mapped registers */ -volatile struct { +struct {uint32_t sq_tail;uint32_t cq_head; } *doorbells;
@@ -164,6 +167,58 @@ static QemuOptsList runtime_opts = { }, }; +static inline uint32_t nvme_mmio_read_32 (const void *addr) +{ +uint32_t ret; + +#ifdef __s390x__ + ret =s390x_pci_mmio_read_32 (addr); +#else +/* Prevent the compiler from optimizing away the load */ + ret = *((volatile uint32_t *)addr); +#endif + +return le32_to_cpu (ret); +} + +static inline uint64_t nvme_mmio_read_64 (const void *addr) +{ +uint64_t ret; + +#ifdef __s390x__ + ret =s390x_pci_mmio_read_64 (addr); +#else +/* Prevent the compiler from optimizing away the load */ + ret = *((volatile uint64_t *)addr); +#endif + +return le64_to_cpu (ret); +} + +static inline void nvme_mmio_write_32 (void *addr,uint32_t val) +{ + val =cpu_to_le32 (val); + +#ifdef __s390x__ +s390x_pci_mmio_write_32 (addr, val); +#else +/* Prevent the compiler from optimizing away the store */ + *((volatile uint32_t *)addr) = val; +#endif +} + +static inline void nvme_mmio_write_64 (void *addr,uint64_t val) +{ + val =cpu_to_le64 (val); + +#ifdef __s390x__ +s390x_pci_mmio_write_64 (addr, val); +#else +/* Prevent the compiler from optimizing away the store */ + *((volatile uint64_t *)addr) = val; +#endif +} +/* Returns true on success, false on failure. */ static bool nvme_init_queue (BDRVNVMeState *s,NVMeQueue *q,unsigned nentries,size_t entry_bytes,Error **errp)
@@ -292,7 +347,7 @@ static void nvme_kick(NVMeQueuePair *q)assert (!(q->sq.tail &0xFF00 ));/* Fence the write to submission queue entry before notifying the device. */ smp_wmb (); - *q->sq.doorbell =cpu_to_le32 (q->sq.tail); +nvme_mmio_write_32 (q->sq.doorbell, q->sq.tail); q->inflight += q->need_kick; q->need_kick =0 ; }
@@ -441,7 +496,7 @@ static bool nvme_process_completion(NVMeQueuePair *q)if (progress) {/* Notify the device so it can post more completions. */ smp_mb_release (); - *q->cq.doorbell =cpu_to_le32 (q->cq.head); +nvme_mmio_write_32 (q->cq.doorbell, q->cq.head);nvme_wake_free_req_locked (q); }
@@ -460,7 +515,7 @@ static void nvme_process_completion_bh(void *opaque)* so notify the device that it has space to fill in more completions now. */ smp_mb_release (); - *q->cq.doorbell =cpu_to_le32 (q->cq.head); +nvme_mmio_write_32 (q->cq.doorbell, q->cq.head);nvme_wake_free_req_locked (q);nvme_process_completion (q);
@@ -749,9 +804,10 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace,int ret;uint64_t cap;uint32_t ver; +uint32_t cc;uint64_t timeout_ms;uint64_t deadline, now; -volatile NvmeBar *regs =NULL ; +NvmeBar *regs =NULL ;qemu_co_mutex_init (&s->dma_map_lock);qemu_co_queue_init (&s->dma_flush_queue);
@@ -779,7 +835,7 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace,/* Perform initialize sequence as described in NVMe spec "7.6.1 * Initialization". */ - cap =le64_to_cpu (regs->cap); + cap =nvme_mmio_read_64 (®s->cap);trace_nvme_controller_capability_raw (cap);trace_nvme_controller_capability ("Maximum Queue Entries Supported" ,1 +NVME_CAP_MQES (cap));
@@ -805,16 +861,17 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace, bs->bl.request_alignment = s->page_size; timeout_ms =MIN (500 *NVME_CAP_TO (cap),30000 ); - ver =le32_to_cpu (regs->vs); + ver =nvme_mmio_read_32 (®s->vs);trace_nvme_controller_spec_version (extract32 (ver,16 ,16 ),extract32 (ver,8 ,8 ),extract32 (ver,0 ,8 ));/* Reset device to get a clean state. */ - regs->cc =cpu_to_le32 (le32_to_cpu (regs->cc) &0xFE ); + cc =nvme_mmio_read_32 (®s->cc); +nvme_mmio_write_32 (®s->cc, (cc &0xFE ));/* Wait for CSTS.RDY = 0. */ deadline =qemu_clock_get_ns (QEMU_CLOCK_REALTIME ) + timeout_ms *SCALE_MS ; -while (NVME_CSTS_RDY (le32_to_cpu (regs->csts))) { +while (NVME_CSTS_RDY (nvme_mmio_read_32 (®s->csts))) {if (qemu_clock_get_ns (QEMU_CLOCK_REALTIME ) > deadline) { error_setg(errp, "Timeout while waiting for device to reset (%" PRId64 " ms)",
@@ -843,19 +900,21 @@ static int nvme_init(BlockDriverState *bs, const char *device, int namespace, s->queues[INDEX_ADMIN ] = q; s->queue_count =1 ;QEMU_BUILD_BUG_ON ((NVME_QUEUE_SIZE -1 ) &0xF000 ); - regs->aqa =cpu_to_le32 (((NVME_QUEUE_SIZE -1 ) <<AQA_ACQS_SHIFT ) | - ((NVME_QUEUE_SIZE -1 ) <<AQA_ASQS_SHIFT )); - regs->asq =cpu_to_le64 (q->sq.iova); - regs->acq =cpu_to_le64 (q->cq.iova); +nvme_mmio_write_32 (®s->aqa, + ((NVME_QUEUE_SIZE -1 ) <<AQA_ACQS_SHIFT ) | + ((NVME_QUEUE_SIZE -1 ) <<AQA_ASQS_SHIFT )); +nvme_mmio_write_64 (®s->asq, q->sq.iova); +nvme_mmio_write_64 (®s->acq, q->cq.iova);/* After setting up all control registers we can enable device now. */ - regs->cc =cpu_to_le32 ((ctz32 (NVME_CQ_ENTRY_BYTES ) <<CC_IOCQES_SHIFT ) | - (ctz32 (NVME_SQ_ENTRY_BYTES ) <<CC_IOSQES_SHIFT ) | -CC_EN_MASK ); +nvme_mmio_write_32 (®s->cc, + ((ctz32 (NVME_CQ_ENTRY_BYTES ) <<CC_IOCQES_SHIFT ) | + (ctz32 (NVME_SQ_ENTRY_BYTES ) <<CC_IOSQES_SHIFT ) | +CC_EN_MASK ));/* Wait for CSTS.RDY = 1. */ now =qemu_clock_get_ns (QEMU_CLOCK_REALTIME ); deadline = now + timeout_ms *SCALE_MS ; -while (!NVME_CSTS_RDY (le32_to_cpu (regs->csts))) { +while (!NVME_CSTS_RDY (nvme_mmio_read_32 (®s->csts))) {if (qemu_clock_get_ns (QEMU_CLOCK_REALTIME ) > deadline) { error_setg(errp, "Timeout while waiting for device to start (%" PRId64 " ms)",
Signature · 1 line
2.43.0
Re: [PATCH v1 1/2] util: Add functions for s390x mmio read/write
In reply to Farhan AliQuoted 60 lines · Farhan Ali
Starting with z15 (or newer) we can execute mmio
instructions from userspace. On older platforms
where we don't have these instructions available
we can fallback to using system calls to access
the PCI mapped resources.This patch adds helper functions for mmio reads
and writes for s390x.Signed-off-by Farhan Ali <[email protected]>--- include/qemu/s390x_pci_mmio.h | 17 ++++++ util/meson.build | 2 + util/s390x_pci_mmio.c | 105 ++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 include/qemu/s390x_pci_mmio.h create mode 100644 util/s390x_pci_mmio.cinclude/qemu/s390x_pci_mmio.h new file +17
-0@@ -0,0 +1,17 @@ +/* +* s390x PCI MMIO definitions +* +* Copyright 2025 IBM Corp. +* Author(s): Farhan Ali <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef S390X_PCI_MMIO_H +#define S390X_PCI_MMIO_H + +uint64_t s390x_pci_mmio_read_64 (const void *ioaddr); +uint32_t s390x_pci_mmio_read_32 (const void *ioaddr); +void s390x_pci_mmio_write_64 (void *ioaddr,uint64_t val); +void s390x_pci_mmio_write_32 (void *ioaddr,uint32_t val); + +#endif util/meson.build +2
-0@@ -131,4 +131,6 @@ elif cpu in ['ppc', 'ppc64'] util_ss.add(files('cpuinfo-ppc.c')) elif cpu in ['riscv32', 'riscv64'] util_ss.add(files('cpuinfo-riscv.c')) +elif cpu == 's390x' + util_ss.add(files('s390x_pci_mmio.c')) endifutil/s390x_pci_mmio.c new file +2
-0@@ -0,0 +1,105 @@ +/* +* s390x PCI MMIO definitions
util/s390x_pci_mmio.c line 12
Quoted 10 lines
util/s390x_pci_mmio.c new file +10
-0+* +* Copyright 2025 IBM Corp. +* Author(s): Farhan Ali <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <unistd.h> +#include <sys/syscall.h> +#include "qemu/osdep.h"
This should be the first #include in the file. From docs/devel/style.rst:
Include directives
------------------
Order include directives as follows:
.. code-block:: c
#include "qemu/osdep.h" /* Always first... */
#include <...> /* then system headers... */
#include "..." /* and finally QEMU headers. */Otherwise:
Re: [PATCH v1 2/2] block/nvme: Enable NVMe userspace driver for s390x
In reply to Farhan Aliblock/nvme.c static QemuOptsList runtime_opts = { · line 179
Quoted 10 lines · Farhan Ali
block/nvme.c +10
-0+static inline uint32_t nvme_mmio_read_32 (const void *addr) +{ +uint32_t ret; + +#ifdef __s390x__ + ret =s390x_pci_mmio_read_32 (addr); +#else +/* Prevent the compiler from optimizing away the load */ + ret = *((volatile uint32_t *)addr); +#endif
The NVMe driver should not need to worry about s390 PCI MMIO specifics. It would be nice to add a QEMU PCI MMIO load/store API that hides this.
block/nvme.c would use the API to access MMIO registers instead of accessing them directly. The functions could be static inline in the header file to minimize overhead on platforms that can directly access PCI MMIO registers.
Re: [PATCH v1 2/2] block/nvme: Enable NVMe userspace driver for s390x
Quoted 14 lines · Stefan Hajnoczi
block/nvme.c static QemuOptsList runtime_opts = { · line 179
Quoted 10 lines · Farhan Ali
block/nvme.c +10
-0+static inline uint32_t nvme_mmio_read_32 (const void *addr) +{ +uint32_t ret; + +#ifdef __s390x__ + ret =s390x_pci_mmio_read_32 (addr); +#else +/* Prevent the compiler from optimizing away the load */ + ret = *((volatile uint32_t *)addr); +#endif The NVMe driver should not need to worry about s390 PCI MMIO specifics. It would be nice to add a QEMU PCI MMIO load/store API that hides this.
Quoted 4 lines
block/nvme.cwould use the API to access MMIO registers instead of accessing them directly. The functions could be static inline in the header file to minimize overhead on platforms that can directly access PCI MMIO registers.
Hi Stefan,
Thanks for reviewing. I do agree, having a generic QEMU PCI MMIO load/store API for this would be good. And we can hide architecture specifics there. I didn't add that to begin with was NVMe driver would be the only place to consume it so thought might be unnecessary. I will refactor this in v2.
Thanks
Farhan
Re: [PATCH v1 1/2] util: Add functions for s390x mmio read/write
In reply to Stefan HajnocziQuoted 85 lines · Stefan Hajnoczi
Quoted 60 lines · Farhan Ali
Starting with z15 (or newer) we can execute mmio
instructions from userspace. On older platforms
where we don't have these instructions available
we can fallback to using system calls to access
the PCI mapped resources.This patch adds helper functions for mmio reads
and writes for s390x.Signed-off-by Farhan Ali <[email protected]>--- include/qemu/s390x_pci_mmio.h | 17 ++++++ util/meson.build | 2 + util/s390x_pci_mmio.c | 105 ++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+)create mode 100644
include/qemu/s390x_pci_mmio.hcreate mode 100644util/s390x_pci_mmio.cinclude/qemu/s390x_pci_mmio.h new file +17
-0@@ -0,0 +1,17 @@ +/* +* s390x PCI MMIO definitions +* +* Copyright 2025 IBM Corp. +* Author(s): Farhan Ali <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +#ifndef S390X_PCI_MMIO_H +#define S390X_PCI_MMIO_H + +uint64_t s390x_pci_mmio_read_64 (const void *ioaddr); +uint32_t s390x_pci_mmio_read_32 (const void *ioaddr); +void s390x_pci_mmio_write_64 (void *ioaddr,uint64_t val); +void s390x_pci_mmio_write_32 (void *ioaddr,uint32_t val); + +#endif util/meson.build +2
-0@@ -131,4 +131,6 @@ elif cpu in ['ppc', 'ppc64'] util_ss.add(files('cpuinfo-ppc.c')) elif cpu in ['riscv32', 'riscv64'] util_ss.add(files('cpuinfo-riscv.c')) +elif cpu == 's390x' + util_ss.add(files('s390x_pci_mmio.c')) endifutil/s390x_pci_mmio.c new file +2
-0@@ -0,0 +1,105 @@ +/* +* s390x PCI MMIO definitions util/s390x_pci_mmio.c line 12
Quoted 10 lines
util/s390x_pci_mmio.c new file +10
-0+* +* Copyright 2025 IBM Corp. +* Author(s): Farhan Ali <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <unistd.h> +#include <sys/syscall.h> +#include "qemu/osdep.h" This should be the first #include in the file. From
docs/devel/style.rst:Include directives ------------------ Order include directives as follows: .. code-block:: c #include "qemu/osdep.h" /* Always first... */ #include <...> /* then system headers... */ #include "..." /* and finally QEMU headers. */
Quoted 3 lines
Otherwise:
Reviewed-by Stefan Hajnoczi <[email protected]>
Will fix in next revision. Thanks!