s390x/pci: Fixing s390 vfio-pci ISM support
cover s390x/pci: Fixing s390 vfio-pci ISM support
Today, ISM devices are completely disallowed for vfio-pci passthrough as QEMU rejects the device due to an (inappropriate) MSI-X check. Removing this fence, however, reveals additional deficiencies in the s390x PCI interception layer that prevent ISM devices from working correctly. Namely, ISM block write operations have particular requirements in regards to the alignment, size and order of writes performed that cannot be guaranteed when breaking up write operations through the typical vfio_pci_bar_rw paths. Furthermore, ISM requires that legacy/non-MIO s390 PCI instructions are used, which is also not guaranteed when the I/O is passed through the typical userspace channels.
This patchset provides a set of fixes related to enabling ISM device passthrough and includes patches to enable use of a new vfio region that will allow s390x PCI pass-through devices to perform s390 PCI instructions in such a way that the same instruction issued on the guest is re-issued on the host.
The region is intended for use with ISM devices specifically, as they do not implement MSI-X and thus transferring I/O in this manner will not interfere with vfio-pci MSI-X masking. If someone can think of a good way to also make this MSI-X compatible (and thus usable by other device types besides ISM), I'm open to suggestions...
Associated kernel patchset:
https://lkml.org/lkml/2020/12/9/1059
Matthew Rosato (8):
linux-headers: update against 5.10-rc7 s390x/pci: MSI-X isn't strictly required for passthrough s390x/pci: fix pcistb length
s390x/pci: Introduce the ZpciOps structure s390x/pci: Fix memory_region_access_valid call s390x/pci: Handle devices that support relaxed alignment s390x/pci: PCISTB via the vfio zPCI I/O region s390x/pci: PCILG via the vfio zPCI I/O region
hw/s390x/s390-pci-bus.c | 32 ++- hw/s390x/s390-pci-inst.c | 289 ++++++++++++++------- hw/s390x/s390-pci-vfio.c | 164 ++++++++++++ include/hw/s390x/s390-pci-bus.h | 24 ++ include/hw/s390x/s390-pci-clp.h | 1 + include/hw/s390x/s390-pci-inst.h | 3 + include/hw/s390x/s390-pci-vfio.h | 23 ++ include/standard-headers/asm-x86/kvm_para.h | 1 + .../infiniband/hw/vmw_pvrdma/pvrdma_verbs.h | 2 +- include/standard-headers/linux/vhost_types.h | 9 + linux-headers/linux/vfio.h | 4 + linux-headers/linux/vfio_zdev.h | 33 +++ linux-headers/linux/vhost.h | 4 + 13 files changed, 486 insertions(+), 103 deletions(-)
Signature · 1 line
1.8.3.1
RFC 1/8 linux-headers: update against 5.10-rc7
Placeholder commit to pull in changes from "vfio-pci/zdev: Pass the relaxed alignment flag" and "vfio-pci/zdev: Introduce the PCISTB vfio region"
--- include/standard-headers/asm-x86/kvm_para.h | 1 + .../infiniband/hw/vmw_pvrdma/pvrdma_verbs.h | 2 +- include/standard-headers/linux/vhost_types.h | 9 ++++++ linux-headers/linux/vfio.h | 4 +++ linux-headers/linux/vfio_zdev.h | 33 ++++++++++++++++++++++ linux-headers/linux/vhost.h | 4 +++ 6 files changed, 52 insertions(+), 1 deletion(-)
include/standard-headers/asm-x86/kvm_para.h +1 -0
@@ -32,6 +32,7 @@#define KVM_FEATURE_POLL_CONTROL 12#define KVM_FEATURE_PV_SCHED_YIELD 13#define KVM_FEATURE_ASYNC_PF_INT 14 +#define KVM_FEATURE_MSI_EXT_DEST_ID 15#define KVM_HINTS_REALTIME 0
include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/pvrdma_verbs.h +1 -1
@@ -176,7 +176,7 @@ struct pvrdma_port_attr {uint8_t subnet_timeout;uint8_t init_type_reply;uint8_t active_width; -uint16_t active_speed; +uint8_t active_speed;uint8_t phys_state;uint8_t reserved[2 ]; };
include/standard-headers/linux/vhost_types.h +9 -0
@@ -138,6 +138,15 @@ struct vhost_vdpa_config {uint8_t buf[0 ]; }; +/* vhost vdpa IOVA range +* @first: First address that can be mapped by vhost-vDPA +* @last: Last address that can be mapped by vhost-vDPA +*/ +struct vhost_vdpa_iova_range { +uint64_t first; +uint64_t last; +}; +/* Feature bits */ /* Log all write descriptors. Can be changed while device is active. */ #define VHOST_F_LOG_ALL 26
linux-headers/linux/vfio.h +4 -0
@@ -338,6 +338,10 @@ struct vfio_region_info_cap_type {* to do TLB invalidation on a GPU. */ #define VFIO_REGION_SUBTYPE_IBM_NVLINK2_ATSD (1) +/* +* IBM zPCI I/O region +*/ +#define VFIO_REGION_SUBTYPE_IBM_ZPCI_IO (2)/* sub-types for VFIO_REGION_TYPE_GFX */ #define VFIO_REGION_SUBTYPE_GFX_EDID (1)
linux-headers/linux/vfio_zdev.h +33 -0
@@ -43,6 +43,7 @@ struct vfio_device_info_cap_zpci_group {__u64 msi_addr;/* MSI address */ __u64 flags;#define VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH 1/* Program-specified TLB refresh */ +#define VFIO_DEVICE_INFO_ZPCI_FLAG_RELAXED 2/* Relaxed Length and Alignment */ __u16 mui;/* Measurement Block Update Interval */ __u16 noi;/* Maximum number of MSIs */ __u16 maxstbl;/* Maximum Store Block Length */
@@ -75,4 +76,36 @@ struct vfio_device_info_cap_zpci_pfip { __u8 pfip[]; }; +/** +* VFIO_REGION_SUBTYPE_IBM_ZPCI_IO - VFIO zPCI PCI Direct I/O Region +* +* This region is used to transfer I/O operations from the guest directly +* to the host zPCI I/O layer. +* +* The _hdr area is user-readable and is used to provide setup information. +* The _req area is user-writable and is used to provide the I/O operation. +*/ +struct vfio_zpci_io_hdr { +__u64 flags; +__u16 max;/* Max block operation size allowed */ +__u16 reserved; +__u32 reserved2; +}; + +struct vfio_zpci_io_req { +__u64 flags; +#define VFIO_ZPCI_IO_FLAG_READ (1 << 0)/* Read Operation Specified */ +#define VFIO_ZPCI_IO_FLAG_BLOCKW (1 << 1)/* Block Write Operation Specified */ +__u64 gaddr;/* Address of guest data */ +__u64 offset;/* Offset into target PCI Address Space */ +__u32 reserved; +__u16 len;/* Length of guest operation */ +__u8 pcias;/* Target PCI Address Space */ +__u8 reserved2; +}; + +struct vfio_region_zpci_io { +struct vfio_zpci_io_hdr hdr; +struct vfio_zpci_io_req req; +};#endif
linux-headers/linux/vhost.h +4 -0
@@ -146,4 +146,8 @@/* Set event fd for config interrupt*/ #define VHOST_VDPA_SET_CONFIG_CALL _IOW(VHOST_VIRTIO, 0x77, int) + +/* Get the valid iova range */ +#define VHOST_VDPA_GET_IOVA_RANGE _IOR(VHOST_VIRTIO, 0x78, \ + struct vhost_vdpa_iova_range)#endif
Signature · 1 line
1.8.3.1
RFC 2/8 s390x/pci: MSI-X isn't strictly required for passthrough
In reply to Matthew Rosatos390 PCI currently disallows PCI devices without the MSI-X capability. However, this fence doesn't make sense for passthrough devices. Move the check to only fence emulated devices (e.g., virtio).
--- hw/s390x/s390-pci-bus.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
hw/s390x/s390-pci-bus.c +8 -6
@@ -1028,12 +1028,12 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,s390_pci_get_clp_info (pbdev); }else { pbdev->fh |=FH_SHM_EMUL ; - } -if (s390_pci_msix_init (pbdev)) { -error_setg (errp,"MSI-X support is mandatory " -"in the S390 architecture" ); -return ; +if (s390_pci_msix_init (pbdev)) { +error_setg (errp,"MSI-X support is mandatory " +"in the S390 architecture" ); +return ; + } }if (dev->hotplugged) {
@@ -1073,7 +1073,9 @@ static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, devfn = pci_dev->devfn;qdev_unrealize (dev); -s390_pci_msix_free (pbdev); +if (pbdev->fh &FH_SHM_EMUL ) { +s390_pci_msix_free (pbdev); + }s390_pci_iommu_free (s, bus, devfn); pbdev->pdev =NULL ; pbdev->state =ZPCI_FS_RESERVED ;
Signature · 1 line
1.8.3.1
Re: [RFC 2/8] s390x/pci: MSI-X isn't strictly required for passthrough
Quoted 33 lines · On Wed, 9 Dec 2020 15:34:20 -0500 Matthew Rosato <[email protected]>…
s390 PCI currently disallows PCI devices without the MSI-X capability. However, this fence doesn't make sense for passthrough devices. Move the check to only fence emulated devices (e.g., virtio).
Signed-off-by Matthew Rosato <[email protected]>Reviewed-by Pierre Morel <[email protected]>--- hw/s390x/s390-pci-bus.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)hw/s390x/s390-pci-bus.c +5
-5@@ -1028,12 +1028,12 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,s390_pci_get_clp_info (pbdev); }else { pbdev->fh |=FH_SHM_EMUL ; - } -if (s390_pci_msix_init (pbdev)) { -error_setg (errp,"MSI-X support is mandatory " -"in the S390 architecture" ); -return ; +if (s390_pci_msix_init (pbdev)) { +error_setg (errp,"MSI-X support is mandatory " +"in the S390 architecture" ); +return ; + } }if (dev->hotplugged) {
hw/s390x/s390-pci-bus.c static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1081
Quoted 10 lines
hw/s390x/s390-pci-bus.c +3
-1@@ -1073,7 +1073,9 @@ static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, devfn = pci_dev->devfn;qdev_unrealize (dev); -s390_pci_msix_free (pbdev); +if (pbdev->fh &FH_SHM_EMUL ) { +s390_pci_msix_free (pbdev); + }s390_pci_iommu_free (s, bus, devfn); pbdev->pdev =NULL ; pbdev->state =ZPCI_FS_RESERVED ;
Remind me: Wasn't it only msi that was strictly required (i.e., not msi-x?)
Can we generally relax this requirement, possibly with some changes in the adapter interrupt mapping? I might misremember, though.
Re: [RFC 2/8] s390x/pci: MSI-X isn't strictly required for passthrough
Quoted 49 lines · Cornelia Huck
Quoted 33 lines · On Wed, 9 Dec 2020 15:34:20 -0500 Matthew Rosato <[email protected]>…
s390 PCI currently disallows PCI devices without the MSI-X capability. However, this fence doesn't make sense for passthrough devices. Move the check to only fence emulated devices (e.g., virtio).
Signed-off-by Matthew Rosato <[email protected]>Reviewed-by Pierre Morel <[email protected]>--- hw/s390x/s390-pci-bus.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)hw/s390x/s390-pci-bus.c +5
-5@@ -1028,12 +1028,12 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,s390_pci_get_clp_info (pbdev); }else { pbdev->fh |=FH_SHM_EMUL ; - } -if (s390_pci_msix_init (pbdev)) { -error_setg (errp,"MSI-X support is mandatory " -"in the S390 architecture" ); -return ; +if (s390_pci_msix_init (pbdev)) { +error_setg (errp,"MSI-X support is mandatory " +"in the S390 architecture" ); +return ; + } }if (dev->hotplugged) {hw/s390x/s390-pci-bus.c static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1081
Quoted 10 lines
hw/s390x/s390-pci-bus.c +3
-1@@ -1073,7 +1073,9 @@ static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, devfn = pci_dev->devfn;qdev_unrealize (dev); -s390_pci_msix_free (pbdev); +if (pbdev->fh &FH_SHM_EMUL ) { +s390_pci_msix_free (pbdev); + }s390_pci_iommu_free (s, bus, devfn); pbdev->pdev =NULL ; pbdev->state =ZPCI_FS_RESERVED ;Remind me: Wasn't it only msi that was strictly required (i.e., not msi-x?)
Quoted 3 lines
Can we generally relax this requirement, possibly with some changes in the adapter interrupt mapping? I might misremember, though.
Yes, but even so our current emulation support only sets up for MSI-X, it does not have an msi_init() equivalent. I do believe that this requirement can be relaxed at some point for the emulation support as well, but the focus on this set was to at least stop fencing passthrough for no reason.
Re: [RFC 2/8] s390x/pci: MSI-X isn't strictly required for passthrough
Quoted 54 lines · On Thu, 10 Dec 2020 10:13:29 -0500 Matthew Rosato <[email protected]>…
Quoted 49 lines · Cornelia Huck
Quoted 33 lines · On Wed, 9 Dec 2020 15:34:20 -0500 Matthew Rosato <[email protected]>…
s390 PCI currently disallows PCI devices without the MSI-X capability. However, this fence doesn't make sense for passthrough devices. Move the check to only fence emulated devices (e.g., virtio).
Signed-off-by Matthew Rosato <[email protected]>Reviewed-by Pierre Morel <[email protected]>--- hw/s390x/s390-pci-bus.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)hw/s390x/s390-pci-bus.c +5
-5@@ -1028,12 +1028,12 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,s390_pci_get_clp_info (pbdev); }else { pbdev->fh |=FH_SHM_EMUL ; - } -if (s390_pci_msix_init (pbdev)) { -error_setg (errp,"MSI-X support is mandatory " -"in the S390 architecture" ); -return ; +if (s390_pci_msix_init (pbdev)) { +error_setg (errp,"MSI-X support is mandatory " +"in the S390 architecture" ); +return ; + } }if (dev->hotplugged) {hw/s390x/s390-pci-bus.c static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1081
Quoted 10 lines
hw/s390x/s390-pci-bus.c +3
-1@@ -1073,7 +1073,9 @@ static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, devfn = pci_dev->devfn;qdev_unrealize (dev); -s390_pci_msix_free (pbdev); +if (pbdev->fh &FH_SHM_EMUL ) { +s390_pci_msix_free (pbdev); + }s390_pci_iommu_free (s, bus, devfn); pbdev->pdev =NULL ; pbdev->state =ZPCI_FS_RESERVED ;Remind me: Wasn't it only msi that was strictly required (i.e., not msi-x?)
Quoted 3 lines
Can we generally relax this requirement, possibly with some changes in the adapter interrupt mapping? I might misremember, though.
Quoted 5 lines
Yes, but even so our current emulation support only sets up for MSI-X, it does not have an
msi_init()equivalent. I do believe that this requirement can be relaxed at some point for the emulation support as well, but the focus on this set was to at least stop fencing passthrough for no reason.
Looking back to when this was introduced, I see that 857cc71985dc ("s390x/pci: merge msix init functions") actually makes this mandatory and states that nothing changes for passthrough. Has anything changed regarding msi-x in the architecture in the meantime?
Re: [RFC 2/8] s390x/pci: MSI-X isn't strictly required for passthrough
Quoted 63 lines · Cornelia Huck
Quoted 54 lines · On Thu, 10 Dec 2020 10:13:29 -0500 Matthew Rosato <[email protected]>…
Quoted 49 lines · Cornelia Huck
Quoted 33 lines · On Wed, 9 Dec 2020 15:34:20 -0500 Matthew Rosato <[email protected]>…
s390 PCI currently disallows PCI devices without the MSI-X capability. However, this fence doesn't make sense for passthrough devices. Move the check to only fence emulated devices (e.g., virtio).
Signed-off-by Matthew Rosato <[email protected]>Reviewed-by Pierre Morel <[email protected]>--- hw/s390x/s390-pci-bus.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)hw/s390x/s390-pci-bus.c +5
-5@@ -1028,12 +1028,12 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,s390_pci_get_clp_info (pbdev); }else { pbdev->fh |=FH_SHM_EMUL ; - } -if (s390_pci_msix_init (pbdev)) { -error_setg (errp,"MSI-X support is mandatory " -"in the S390 architecture" ); -return ; +if (s390_pci_msix_init (pbdev)) { +error_setg (errp,"MSI-X support is mandatory " +"in the S390 architecture" ); +return ; + } }if (dev->hotplugged) {hw/s390x/s390-pci-bus.c static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, · line 1081
Quoted 10 lines
hw/s390x/s390-pci-bus.c +3
-1@@ -1073,7 +1073,9 @@ static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev, devfn = pci_dev->devfn;qdev_unrealize (dev); -s390_pci_msix_free (pbdev); +if (pbdev->fh &FH_SHM_EMUL ) { +s390_pci_msix_free (pbdev); + }s390_pci_iommu_free (s, bus, devfn); pbdev->pdev =NULL ; pbdev->state =ZPCI_FS_RESERVED ;Remind me: Wasn't it only msi that was strictly required (i.e., not msi-x?)
Quoted 3 lines
Can we generally relax this requirement, possibly with some changes in the adapter interrupt mapping? I might misremember, though.
Quoted 5 lines
Yes, but even so our current emulation support only sets up for MSI-X, it does not have an
msi_init()equivalent. I do believe that this requirement can be relaxed at some point for the emulation support as well, but the focus on this set was to at least stop fencing passthrough for no reason.
Quoted 5 lines
Looking back to when this was introduced, I see that
857cc71985dc("s390x/pci: merge msix init functions") actually makes this mandatory and states that nothing changes for passthrough. Has anything changed regarding msi-x in the architecture in the meantime?
I don't believe anything has changed from the architecture or kernel support perspective (Maybe I'm wrong about the latter; Niklas feel free to correct me in that case) -- Otherwise, as far as I can, tell the statement from 857cc71985dc was just incorrect and the restriction was placed unnecessarily for passthrough. vfio-pci sets up for both, and our base s390 kernel support allows for both (and I can drive ISM traffic and see interrupts being routed to the guest with these patches applied of course); it's just our qemu emulation support that only sets up for MSI-X.
RFC 3/8 s390x/pci: fix pcistb length
In reply to Matthew RosatoIn pcistb_service_call, we are grabbing 8 bits from a guest register to indicate the length of the store operation -- but per the architecture the length is actually defined by 13 bits of the guest register.
--- hw/s390x/s390-pci-inst.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
hw/s390x/s390-pci-inst.c +2 -2
@@ -750,7 +750,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,int i;uint32_t fh;uint8_t pcias; -uint8_t len; +uint16_t len;uint8_t buffer[128 ];if (env->psw.mask &PSW_MASK_PSTATE ) {
@@ -760,7 +760,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, fh = env->regs[r1] >>32 ; pcias = (env->regs[r1] >>16 ) &0xf ; - len = env->regs[r1] &0xff ; + len = env->regs[r1] &0x1fff ; offset = env->regs[r3];if (!(fh &FH_MASK_ENABLE )) {
Signature · 1 line
1.8.3.1
Re: [RFC 3/8] s390x/pci: fix pcistb length
Quoted 24 lines · On Wed, 9 Dec 2020 15:34:21 -0500 Matthew Rosato <[email protected]>…
In
pcistb_service_call, we are grabbing 8 bits from a guest register to indicate the length of the store operation -- but per the architecture the length is actually defined by 13 bits of the guest register.Signed-off-by Matthew Rosato <[email protected]>Reviewed-by Pierre Morel <[email protected]>--- hw/s390x/s390-pci-inst.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/s390x/s390-pci-inst.c +1
-1@@ -750,7 +750,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,int i;uint32_t fh;uint8_t pcias; -uint8_t len; +uint16_t len;uint8_t buffer[128 ];if (env->psw.mask &PSW_MASK_PSTATE ) {
hw/s390x/s390-pci-inst.c int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, · line 766
Quoted 8 lines
hw/s390x/s390-pci-inst.c +1
-1@@ -760,7 +760,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, fh = env->regs[r1] >>32 ; pcias = (env->regs[r1] >>16 ) &0xf ; - len = env->regs[r1] &0xff ; + len = env->regs[r1] &0x1fff ; offset = env->regs[r3];if (!(fh &FH_MASK_ENABLE )) {
Is that a general problem that we just did not notice before?
If yes, this probably deserves a Fixes: tag and can be queued independently of the rest of the series.
Re: [RFC 3/8] s390x/pci: fix pcistb length
Quoted 34 lines · Cornelia Huck
Quoted 31 lines · On Wed, 9 Dec 2020 15:34:21 -0500 Matthew Rosato <[email protected]>…
In
pcistb_service_call, we are grabbing 8 bits from a guest register to indicate the length of the store operation -- but per the architecture the length is actually defined by 13 bits of the guest register.Signed-off-by Matthew Rosato <[email protected]>Reviewed-by Pierre Morel <[email protected]>--- hw/s390x/s390-pci-inst.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/s390x/s390-pci-inst.c +2
-2@@ -750,7 +750,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,int i;uint32_t fh;uint8_t pcias; -uint8_t len; +uint16_t len;uint8_t buffer[128 ];if (env->psw.mask &PSW_MASK_PSTATE ) {@@ -760,7 +760,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, fh = env->regs[r1] >>32 ; pcias = (env->regs[r1] >>16 ) &0xf ; - len = env->regs[r1] &0xff ; + len = env->regs[r1] &0x1fff ; offset = env->regs[r3];
Quoted 7 lines
hw/s390x/s390-pci-inst.c int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, · line 766
Quoted 1 line
hw/s390x/s390-pci-inst.c +0
-0if (!(fh &FH_MASK_ENABLE )) {Is that a general problem that we just did not notice before?
If yes, this probably deserves a Fixes: tag and can be queued independently of the rest of the series.
Good point. I can split this out, and same for "s390x/pci: Fix memory_region_access_valid call"
Re: [RFC 3/8] s390x/pci: fix pcistb length
Quoted 39 lines · On Thu, 10 Dec 2020 10:15:06 -0500 Matthew Rosato <[email protected]>…
Quoted 38 lines · Cornelia Huck
Quoted 24 lines · On Wed, 9 Dec 2020 15:34:21 -0500 Matthew Rosato <[email protected]>…
In
pcistb_service_call, we are grabbing 8 bits from a guest register to indicate the length of the store operation -- but per the architecture the length is actually defined by 13 bits of the guest register.Signed-off-by Matthew Rosato <[email protected]>Reviewed-by Pierre Morel <[email protected]>--- hw/s390x/s390-pci-inst.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)hw/s390x/s390-pci-inst.c +1
-1@@ -750,7 +750,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,int i;uint32_t fh;uint8_t pcias; -uint8_t len; +uint16_t len;uint8_t buffer[128 ];if (env->psw.mask &PSW_MASK_PSTATE ) {hw/s390x/s390-pci-inst.c int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, · line 766
Quoted 8 lines
hw/s390x/s390-pci-inst.c +1
-1@@ -760,7 +760,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, fh = env->regs[r1] >>32 ; pcias = (env->regs[r1] >>16 ) &0xf ; - len = env->regs[r1] &0xff ; + len = env->regs[r1] &0x1fff ; offset = env->regs[r3];if (!(fh &FH_MASK_ENABLE )) {Is that a general problem that we just did not notice before?
Quoted 7 lines
Quoted 3 lines
If yes, this probably deserves a Fixes: tag and can be queued independently of the rest of the series.
Good point. I can split this out, and same for "s390x/pci: Fix
memory_region_access_validcall"
Any plans for sending this? I'm currently collecting patches for a last pull request before the holidays; but I'm sure there will be more pull requests next year :)
RFC 4/8 s390x/pci: Introduce the ZpciOps structure
In reply to Matthew RosatoAs inftrastructure to introduce different PCI instruction handlers, introduce the ZpciOps structure to contain function pointers for the handlers. Add default handlers for the PCISTG, PCILG and PCISTB instructions.
--- hw/s390x/s390-pci-bus.c | 3 + hw/s390x/s390-pci-inst.c | 238 ++++++++++++++++++++++++++------------- include/hw/s390x/s390-pci-bus.h | 22 ++++ include/hw/s390x/s390-pci-inst.h | 1 + 4 files changed, 185 insertions(+), 79 deletions(-)
hw/s390x/s390-pci-bus.c +3 -0
@@ -1020,6 +1020,7 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev, pbdev->iommu->pbdev = pbdev; pbdev->state =ZPCI_FS_DISABLED ;set_pbdev_info (pbdev); +zpci_assign_default_ops (pbdev);if (object_dynamic_cast (OBJECT (dev),"vfio-pci" )) { pbdev->fh |=FH_SHM_VFIO ;
@@ -1079,6 +1080,8 @@ static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,s390_pci_iommu_free (s, bus, devfn); pbdev->pdev =NULL ; pbdev->state =ZPCI_FS_RESERVED ; +if (pbdev->pcistb_buf) +qemu_vfree (pbdev->pcistb_buf); }else if (object_dynamic_cast (OBJECT (dev),TYPE_S390_PCI_DEVICE )) { pbdev =S390_PCI_DEVICE (dev); pbdev->fid =0 ;
hw/s390x/s390-pci-inst.c +159 -79
316 lines not on this page · raw message
include/hw/s390x/s390-pci-bus.h +22 -0
@@ -325,6 +325,26 @@ typedef struct S390PCIGroup {S390PCIGroup *s390_group_create (int id);S390PCIGroup *s390_group_find (int id); +typedef struct ZpciOps { +int (*pcistg)(S390PCIBusDevice *pbdev, +uint64_t data, +uint8_t pcias, +uint16_t len, +uint64_t offset); +int (*pcilg)(S390PCIBusDevice *pbdev, +uint64_t *data, +uint8_t pcias, +uint16_t len, +uint64_t offset); +int (*pcistb)(S390PCIBusDevice *pbdev, +S390CPU *cpu, +uint64_t gaddr, +uint8_t ar, +uint8_t pcias, +uint16_t len, +uint64_t offset); +}ZpciOps ; +struct S390PCIBusDevice {DeviceState qdev;PCIDevice *pdev;
@@ -350,6 +370,8 @@ struct S390PCIBusDevice {MemoryRegion msix_notify_mr;IndAddr *summary_ind;IndAddr *indicator; +ZpciOps ops; +uint8_t *pcistb_buf;bool pci_unplug_request_processed;bool unplug_requested;QTAILQ_ENTRY (S390PCIBusDevice) link;
include/hw/s390x/s390-pci-inst.h +1 -0
@@ -111,6 +111,7 @@ int mpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar,int stpcifc_service_call (S390CPU *cpu,uint8_t r1,uint64_t fiba,uint8_t ar,uintptr_t ra);void fmb_timer_free (S390PCIBusDevice *pbdev); +void zpci_assign_default_ops (S390PCIBusDevice *pbdev);#define ZPCI_IO_BAR_MIN 0#define ZPCI_IO_BAR_MAX 5
Signature · 1 line
1.8.3.1
RFC 5/8 s390x/pci: Fix memory_region_access_valid call
In reply to Matthew RosatoIn pcistb_service_handler, a call is made to validate that the memory region can be accessed. However, the call is made using the entire length of the pcistb operation, which can be larger than the allowed memory access size (8). Since we already know that the provided buffer is a multiple of 8, fix the call to memory_region_access_valid to iterate over the memory region in the same way as the subsequent call to memory_region_dispatch_write.
--- hw/s390x/s390-pci-inst.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
hw/s390x/s390-pci-inst.c +5 -3
@@ -795,9 +795,11 @@ static int pcistb_default(S390PCIBusDevice *pbdev, S390CPU *cpu, mr =s390_get_subregion (mr, offset, len); offset -= mr->addr; -if (!memory_region_access_valid (mr, offset, len, true, -MEMTXATTRS_UNSPECIFIED )) { -return -EINVAL ; +for (i =0 ; i < len /8 ; i++) { +if (!memory_region_access_valid (mr, offset + i *8 ,8 , true, +MEMTXATTRS_UNSPECIFIED )) { +return -EINVAL ; + } }if (s390_cpu_virt_mem_read (cpu, gaddr, ar, pbdev->pcistb_buf, len)) {
Signature · 1 line
1.8.3.1
Re: [RFC 5/8] s390x/pci: Fix memory_region_access_valid call
Quoted 22 lines · On Wed, 9 Dec 2020 15:34:23 -0500 Matthew Rosato <[email protected]>…
In
pcistb_service_handler, a call is made to validate that the memory region can be accessed. However, the call is made using the entire length of the pcistb operation, which can be larger than the allowed memory access size (8). Since we already know that the provided buffer is a multiple of 8, fix the call tomemory_region_access_validto iterate over the memory region in the same way as the subsequent call tomemory_region_dispatch_write.Signed-off-by Matthew Rosato <[email protected]>--- hw/s390x/s390-pci-inst.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)hw/s390x/s390-pci-inst.c +0
-1@@ -795,9 +795,11 @@ static int pcistb_default(S390PCIBusDevice *pbdev, S390CPU *cpu, mr =s390_get_subregion (mr, offset, len); offset -= mr->addr; - if (!memory_region_access_valid(mr, offset, len, true,
hw/s390x/s390-pci-inst.c static int pcistb_default(S390PCIBusDevice *pbdev, S390CPU *cpu, · line 805
Quoted 10 lines
hw/s390x/s390-pci-inst.c +5
-2- MEMTXATTRS_UNSPECIFIED)) { -return -EINVAL ; +for (i =0 ; i < len /8 ; i++) { +if (!memory_region_access_valid (mr, offset + i *8 ,8 , true, +MEMTXATTRS_UNSPECIFIED )) { +return -EINVAL ; + } }if (s390_cpu_virt_mem_read (cpu, gaddr, ar, pbdev->pcistb_buf, len)) {
Hm, that looks like a fix that's applicable for the current code base as well... do you want to split this out?
RFC 6/8 s390x/pci: Handle devices that support relaxed alignment
In reply to Matthew RosatoCertain zPCI device types (e.g. ISM) allow for a different set of address alignment rules for PCISTB instructions. Recognize this distinction and perform only a subset of alignment checks for intercepted PCISTB instructions.
--- hw/s390x/s390-pci-inst.c | 34 ++++++++++++++++++++-------------- hw/s390x/s390-pci-vfio.c | 3 +++ include/hw/s390x/s390-pci-clp.h | 1 + 3 files changed, 24 insertions(+), 14 deletions(-)
hw/s390x/s390-pci-inst.c +20 -14
@@ -868,25 +868,31 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,return 0 ; } -/* Verify the address, offset and length */ -/* offset must be a multiple of 8 */ -if (offset %8 ) { - goto specification_error; - } -/* Length must be greater than 8, a multiple of 8 */ -/* and not greater than maxstbl */ -if ((len <=8 ) || (len %8 ) || - (len > pbdev->pci_group->zpci_group.maxstbl)) { - goto specification_error; +/* +* If the specified device supports relaxed alignment, some checks can +* be skipped. +*/ +if (!(pbdev->pci_group->zpci_group.fr &CLP_RSP_QPCIG_MASK_RELAXED )) { +/* Verify the address, offset and length */ +/* offset must be a multiple of 8 */ +if (offset %8 ) { + goto specification_error; + } +/* Length must be greater than 8, a multiple of 8 */ +/* and not greater than maxstbl */ +if ((len <=8 ) || (len %8 ) || + (len > pbdev->pci_group->zpci_group.maxstbl)) { + goto specification_error; + } +/* Guest address must be double word aligned */ +if (gaddr &0x07UL ) { + goto specification_error; + } }/* Do not cross a 4K-byte boundary */ if (((offset &0xfff ) + len) >0x1000 ) { goto specification_error; } -/* Guest address must be double word aligned */ -if (gaddr &0x07UL ) { - goto specification_error; - } ret = pbdev->ops.pcistb (pbdev, cpu, gaddr, ar, pcias, len, offset);
hw/s390x/s390-pci-vfio.c +3 -0
@@ -156,6 +156,9 @@ static void s390_pci_read_group(S390PCIBusDevice *pbdev,if (cap->flags &VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH ) { resgrp->fr =1 ; } +if (cap->flags &VFIO_DEVICE_INFO_ZPCI_FLAG_RELAXED ) { + resgrp->fr |=CLP_RSP_QPCIG_MASK_RELAXED ; + } resgrp->dasm = cap->dasm; resgrp->msia = cap->msi_addr; resgrp->mui = cap->mui;
include/hw/s390x/s390-pci-clp.h +1 -0
@@ -158,6 +158,7 @@ typedef struct ClpRspQueryPciGrp {#define CLP_RSP_QPCIG_MASK_NOI 0xfffuint16_t i;uint8_t version; +#define CLP_RSP_QPCIG_MASK_RELAXED 0x8#define CLP_RSP_QPCIG_MASK_FRAME 0x2#define CLP_RSP_QPCIG_MASK_REFRESH 0x1uint8_t fr;
Signature · 1 line
1.8.3.1
RFC 7/8 s390x/pci: PCISTB via the vfio zPCI I/O region
In reply to Matthew RosatoFor ISM devices, use the vfio region to handle intercepted PCISTB instructions. This region will allow large block I/O instructions intercepted from the guest to be performed as a single I/O instruction on the host. This ensure that proper write patterns that are expected by the underlying device are respected and ensures that a non-MIO instruction is used to perform the operation (as ISM devices do not support the MIO instruction set).
Furthermore, add a requirement that the I/O region must be available in order to pass the device through to the guest.
--- hw/s390x/s390-pci-bus.c | 15 ++++++ hw/s390x/s390-pci-inst.c | 8 +++ hw/s390x/s390-pci-vfio.c | 108 +++++++++++++++++++++++++++++++++++++++ include/hw/s390x/s390-pci-bus.h | 2 + include/hw/s390x/s390-pci-inst.h | 1 + include/hw/s390x/s390-pci-vfio.h | 15 ++++++ 6 files changed, 149 insertions(+)
hw/s390x/s390-pci-bus.c +15 -0
@@ -977,6 +977,7 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,S390pciState *s =S390_PCI_HOST_BRIDGE (hotplug_dev);PCIDevice *pdev =NULL ;S390PCIBusDevice *pbdev =NULL ; +int ret;if (object_dynamic_cast (OBJECT (dev),TYPE_PCI_BRIDGE )) {PCIBridge *pb =PCI_BRIDGE (dev);
@@ -1027,6 +1028,20 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev, pbdev->iommu->dma_limit =s390_pci_start_dma_count (s, pbdev);/* Fill in CLP information passed via the vfio region */ s390_pci_get_clp_info (pbdev); + +/* +* For a relaxed-alignment device, setup the special I/O region +* if available. Otherwise, the device cannot be passed through. +*/ + ret =0 ; +if (pbdev->pci_group->zpci_group.fr &CLP_RSP_QPCIG_MASK_RELAXED ) { + ret =s390_pci_get_zpci_io_region (pbdev); + } +if (ret) { +error_setg (errp,"vfio zPCI I/O region support is mandatory " +"for %02x:%02x.%01x" ,pci_dev_bus_num (pdev), +PCI_SLOT (pdev->devfn),PCI_FUNC (pdev->devfn)); + } }else { pbdev->fh |=FH_SHM_EMUL ;
hw/s390x/s390-pci-inst.c +8 -0
@@ -19,6 +19,7 @@#include "sysemu/hw_accel.h" #include "hw/s390x/s390-pci-inst.h" #include "hw/s390x/s390-pci-bus.h" +#include "hw/s390x/s390-pci-vfio.h" #include "hw/s390x/tod.h" #ifndef DEBUG_S390PCI_INST
@@ -897,6 +898,8 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr, ret = pbdev->ops.pcistb (pbdev, cpu, gaddr, ar, pcias, len, offset);switch (ret) { +case -EIO : +/* fall through */ case -EINVAL :s390_program_interrupt (env,PGM_OPERAND , ra);return 0 ;
@@ -1386,3 +1389,8 @@ void zpci_assign_default_ops(S390PCIBusDevice *pbdev) pbdev->ops.pcilg = pcilg_default; pbdev->ops.pcistb = pcistb_default; } + +void zpci_assign_ops_vfio_io_region (S390PCIBusDevice *pbdev) +{ + pbdev->ops.pcistb = s390_pci_vfio_pcistb; +}
hw/s390x/s390-pci-vfio.c +108 -0
117 lines not on this page · raw message
include/hw/s390x/s390-pci-bus.h +2 -0
@@ -355,6 +355,8 @@ struct S390PCIBusDevice {uint32_t fh;uint32_t fid;bool fid_defined; +uint64_t io_region_op_offset; +struct vfio_region_zpci_io *io_region;uint64_t fmb_addr;ZpciFmb fmb;QEMUTimer *fmb_timer;
include/hw/s390x/s390-pci-inst.h +1 -0
@@ -112,6 +112,7 @@ int stpcifc_service_call(S390CPU *cpu, uint8_t r1, uint64_t fiba, uint8_t ar, uintptr_t ra);void fmb_timer_free (S390PCIBusDevice *pbdev);void zpci_assign_default_ops (S390PCIBusDevice *pbdev); +void zpci_assign_ops_vfio_io_region (S390PCIBusDevice *pbdev);#define ZPCI_IO_BAR_MIN 0#define ZPCI_IO_BAR_MAX 5
include/hw/s390x/s390-pci-vfio.h +15 -0
@@ -21,6 +21,10 @@ S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s, S390PCIBusDevice *pbdev);void s390_pci_end_dma_count (S390pciState *s,S390PCIDMACount *cnt);void s390_pci_get_clp_info (S390PCIBusDevice *pbdev); +int s390_pci_get_zpci_io_region (S390PCIBusDevice *pbdev); +int s390_pci_vfio_pcistb (S390PCIBusDevice *pbdev,S390CPU *cpu,uint64_t gaddr, +uint8_t ar,uint8_t pcias,uint16_t len, +uint64_t offset);#else static inline bool s390_pci_update_dma_avail (int fd,unsigned int *avail) {
@@ -34,6 +38,17 @@ static inline S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,static inline void s390_pci_end_dma_count (S390pciState *s,S390PCIDMACount *cnt) { }static inline void s390_pci_get_clp_info (S390PCIBusDevice *pbdev) { } +static inline int s390_pci_get_zpci_io_region (S390PCIBusDevice *pbdev) +{ +return -EINVAL ; +} +static inline int s390_pci_vfio_pcistb (S390PCIBusDevice *pbdev,S390CPU *cpu, +uint64_t gaddr,uint8_t ar, +uint8_t pcias,uint16_t len, +uint64_t offset) +{ +return -EIO ; +}#endif #endif
Signature · 1 line
1.8.3.1
RFC 8/8 s390x/pci: PCILG via the vfio zPCI I/O region
In reply to Matthew RosatoFor ISM devices, use the vfio region to handle intercepted PCILG instructions. This will allow read I/Os intercepted from the guest to be performed as single operations that ensure the same non-MIO PCI instruction is used on the host as specified in the guest.
--- hw/s390x/s390-pci-inst.c | 3 ++- hw/s390x/s390-pci-vfio.c | 53 ++++++++++++++++++++++++++++++++++++++++ include/hw/s390x/s390-pci-inst.h | 1 + include/hw/s390x/s390-pci-vfio.h | 8 ++++++ 4 files changed, 64 insertions(+), 1 deletion(-)
hw/s390x/s390-pci-inst.c +2 -1
@@ -352,7 +352,7 @@ out:* @ptr: a pointer to a uint64_t data field * @len: the length of the valid data, must be 1,2,4 or 8 */ -static int zpci_endian_swap (uint64_t *ptr,uint8_t len) +int zpci_endian_swap (uint64_t *ptr,uint8_t len) {uint64_t data = *ptr;
@@ -1392,5 +1392,6 @@ void zpci_assign_default_ops(S390PCIBusDevice *pbdev)void zpci_assign_ops_vfio_io_region (S390PCIBusDevice *pbdev) { + pbdev->ops.pcilg = s390_pci_vfio_pcilg; pbdev->ops.pcistb = s390_pci_vfio_pcistb; }
hw/s390x/s390-pci-vfio.c +53 -0
59 lines not on this page · raw message
include/hw/s390x/s390-pci-inst.h +1 -0
@@ -101,6 +101,7 @@ typedef struct ZpciFib {int pci_dereg_irqs (S390PCIBusDevice *pbdev);void pci_dereg_ioat (S390PCIIOMMU *iommu);int clp_service_call (S390CPU *cpu,uint8_t r2,uintptr_t ra); +int zpci_endian_swap (uint64_t *ptr,uint8_t len);int pcilg_service_call (S390CPU *cpu,uint8_t r1,uint8_t r2,uintptr_t ra);int pcistg_service_call (S390CPU *cpu,uint8_t r1,uint8_t r2,uintptr_t ra);int rpcit_service_call (S390CPU *cpu,uint8_t r1,uint8_t r2,uintptr_t ra);
include/hw/s390x/s390-pci-vfio.h +8 -0
@@ -22,6 +22,8 @@ S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s,void s390_pci_end_dma_count (S390pciState *s,S390PCIDMACount *cnt);void s390_pci_get_clp_info (S390PCIBusDevice *pbdev);int s390_pci_get_zpci_io_region (S390PCIBusDevice *pbdev); +int s390_pci_vfio_pcilg (S390PCIBusDevice *pbdev,uint64_t *data,uint8_t pcias, +uint16_t len,uint64_t offset);int s390_pci_vfio_pcistb (S390PCIBusDevice *pbdev,S390CPU *cpu,uint64_t gaddr,uint8_t ar,uint8_t pcias,uint16_t len,uint64_t offset);
@@ -42,6 +44,12 @@ static inline int s390_pci_get_zpci_io_region(S390PCIBusDevice *pbdev) {return -EINVAL ; } +static inline int s390_pci_vfio_pcilg (S390PCIBusDevice *pbdev,uint64_t *data, +uint8_t pcias,uint16_t len, +uint64_t offset) +{ +return -EIO ; +} static inline int s390_pci_vfio_pcistb(S390PCIBusDevice *pbdev, S390CPU *cpu, uint64_t gaddr, uint8_t ar, uint8_t pcias, uint16_t len,
Signature · 1 line
1.8.3.1