vSMMUv3/pSMMUv3 2 stage VFIO integration
v9 cover vSMMUv3/pSMMUv3 2 stage VFIO integration
Up to now vSMMUv3 has not been integrated with VFIO. VFIO integration requires to program the physical IOMMU consistently with the guest mappings. However, as opposed to VTD, SMMUv3 has no "Caching Mode" which allows easy trapping of guest mappings. This means the vSMMUV3 cannot use the same VFIO integration as VTD.
However SMMUv3 has 2 translation stages. This was devised with virtualization use case in mind where stage 1 is "owned" by the guest whereas the host uses stage 2 for VM isolation.
This series sets up this nested translation stage. It only works if there is one physical SMMUv3 used along with QEMU vSMMUv3 (in other words, it does not work if there is a physical SMMUv2).
- We force the host to use stage 2 instead of stage 1, when we detect a vSMMUV3 is behind a VFIO device. For a VFIO device without any virtual IOMMU, we still use stage 1 as many existing SMMUs expect this behavior.
- We use PCIPASIDOps to propage guest stage1 config changes on STE (Stream Table Entry) changes.
- We implement a specific UNMAP notifier that conveys guest IOTLB invalidations to the host
- We register MSI IOVA/GPA bindings to the host so that this latter can build a nested stage translation
- As the legacy MAP notifier is not called anymore, we must make sure stage 2 mappings are set. This is achieved through another prereg memory listener.
- Physical SMMU stage 1 related faults are reported to the guest via en eventfd mechanism and exposed trhough a dedicated VFIO-PCI region. Then they are reinjected into the guest.
Best Regards
Eric
All the patches can be found at:
https://github.com/eauger/qemu/tree/v6.0.0-rc2-2stage-rfcv9
Previous version:
v8: https://github.com/eauger/qemu/tree/v5.2.0-2stage-rfcv8
Kernel Dependencies:
[1] [PATCH v15 00/12] SMMUv3 Nested Stage Setup (IOMMU part)
[2] [PATCH v13 00/13] SMMUv3 Nested Stage Setup (VFIO part) branch containing both:
https://github.com/eauger/linux/tree/v5.12-rc6-jean-iopf-14-2stage-v15
History:
v8 -> v9:
- added
hw/arm/smmu-common: Allow domain invalidation for NH_ALL/NSNH_ALL following Chenxiang's report
v7 -> v8:
- adapt to changes to the kernel uapi
- Fix unregistration of MSI bindings
- applies on top of range invalidation fixes
- changes in IOTLBEntry (flags)
- addressed all the comments from reviewers/testers I hope. Many thanks to all of you! see individual logs
Eric Auger (28):
hw/vfio/common: trace vfio_connect_container operations
update-linux-headers: Import iommu.h
header update against 5.12-rc6 and IOMMU/VFIO nested stage APIs
memory: Add new fields in IOTLBEntry
hw/arm/smmuv3: Improve stage1 ASID invalidation hw/arm/smmu-common: Allow domain invalidation for NH_ALL/NSNH_ALL
memory: Add IOMMU_ATTR_VFIO_NESTED IOMMU memory region attribute
memory: Add IOMMU_ATTR_MSI_TRANSLATE IOMMU memory region attribute
memory: Introduce IOMMU Memory Region inject_faults API
iommu: Introduce generic header
vfio: Force nested if iommu requires it
vfio: Introduce hostwin_from_range helper
vfio: Introduce helpers to DMA map/unmap a RAM section
vfio: Set up nested stage mappings
vfio: Pass stage 1 MSI bindings to the host
vfio: Helper to get IRQ info including capabilities vfio/pci: Register handler for iommu fault
vfio/pci: Set up the DMA FAULT region
vfio/pci: Implement the DMA fault handler
hw/arm/smmuv3: Advertise MSI_TRANSLATE attribute hw/arm/smmuv3: Store the PASID table GPA in the translation config hw/arm/smmuv3: Fill the IOTLBEntry arch_id on NH_VA invalidation hw/arm/smmuv3: Fill the IOTLBEntry leaf field on NH_VA invalidation hw/arm/smmuv3: Pass stage 1 configurations to the host hw/arm/smmuv3: Implement fault injection
hw/arm/smmuv3: Allow MAP notifiers
pci: Add return_page_response pci ops
vfio/pci: Implement return_page_response page response callback
Liu Yi L (1):
pci: introduce PCIPASIDOps to PCIDevice
hw/arm/smmu-internal.h | 1 + hw/vfio/pci.h | 11 + include/exec/memory.h | 64 +- include/hw/arm/smmu-common.h | 1 + include/hw/iommu/iommu.h | 36 ++ include/hw/pci/pci.h | 15 + include/hw/vfio/vfio-common.h | 19 + include/standard-headers/drm/drm_fourcc.h | 23 +- include/standard-headers/linux/ethtool.h | 54 +- include/standard-headers/linux/fuse.h | 3 +- include/standard-headers/linux/input.h | 2 +- .../standard-headers/rdma/vmw_pvrdma-abi.h | 7 + linux-headers/asm-generic/unistd.h | 4 +- linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/kvm.h | 2 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 1 + linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 1 + linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/kvm.h | 89 +++ linux-headers/linux/vfio.h | 169 +++++- hw/arm/smmu-common.c | 3 +- hw/arm/smmuv3.c | 224 ++++++- hw/i386/intel_iommu.c | 6 +- hw/pci/pci.c | 50 ++ hw/ppc/spapr_iommu.c | 2 +- hw/vfio/common.c | 565 ++++++++++++++---- hw/vfio/pci.c | 415 ++++++++++++- hw/virtio/virtio-iommu.c | 4 +- softmmu/memory.c | 10 + hw/arm/trace-events | 2 + hw/vfio/trace-events | 11 +- scripts/update-linux-headers.sh | 2 +- 39 files changed, 1626 insertions(+), 179 deletions(-) create mode 100644 include/hw/iommu/iommu.h
Signature · 1 line
2.26.3
RFC v9 1/29 hw/vfio/common: trace vfio_connect_container operations
We currently trace vfio_disconnect_container() but we do not trace the container <-> group creation, which can be useful to understand the VFIO topology.
--- hw/vfio/common.c | 3 +++ hw/vfio/trace-events | 2 ++ 2 files changed, 5 insertions(+)
hw/vfio/common.c +3 -0
@@ -1742,6 +1742,8 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, QLIST_FOREACH(container, &space->containers, next) {if (!ioctl (group->fd,VFIO_GROUP_SET_CONTAINER , &container->fd)) { group->container = container; +trace_vfio_connect_existing_container (group->groupid, + container->fd);QLIST_INSERT_HEAD (&container->group_list, group, container_next);vfio_kvm_device_add_group (group);return 0 ;
@@ -1775,6 +1777,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,if (ret) { goto free_container_exit; } +trace_vfio_connect_new_container (group->groupid, container->fd);switch (container->iommu_type) {case VFIO_TYPE1v2_IOMMU:
hw/vfio/trace-events +2 -0
@@ -104,6 +104,8 @@ vfio_listener_region_add_no_dma_map(const char *name, uint64_t iova, uint64_t si vfio_listener_region_del_skip(uint64_t start, uint64_t end) "SKIPPING region_del 0x%"PRIx64" - 0x%"PRIx64 vfio_listener_region_del(uint64_t start, uint64_t end) "region_del 0x%"PRIx64" - 0x%"PRIx64 vfio_disconnect_container(int fd) "close container->fd=%d" +vfio_connect_existing_container(int groupid, int container_fd) "group=%d existing container fd=%d" +vfio_connect_new_container(int groupid, int container_fd) "group=%d new container fd=%d" vfio_put_group(int fd) "close group->fd=%d" vfio_get_device(const char * name, unsigned int flags, unsigned int num_regions, unsigned int num_irqs) "Device %s flags: %u, regions: %u, irqs: %u" vfio_put_base_device(int fd) "close vdev->fd=%d"
Signature · 1 line
2.26.3
RFC v9 2/29 update-linux-headers: Import iommu.h
In reply to Eric AugerUpdate the script to import the new iommu.h uapi header.
--- scripts/update-linux-headers.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/update-linux-headers.sh +1 -1
@@ -142,7 +142,7 @@ done rm -rf "$output/linux-headers/linux" mkdir -p "$output/linux-headers/linux" -for header in kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h \ +for header in kvm.h vfio.h vfio_ccw.h vfio_zdev.h vhost.h iommu.h \ psci.h psp-sev.h userfaultfd.h mman.h; do cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" done
Signature · 1 line
2.26.3
RFC v9 3/29 header update against 5.12-rc6 and IOMMU/VFIO nested stage APIs
In reply to Eric Auger--- include/standard-headers/drm/drm_fourcc.h | 23 ++- include/standard-headers/linux/ethtool.h | 54 +++--- include/standard-headers/linux/fuse.h | 3 +- include/standard-headers/linux/input.h | 2 +- .../standard-headers/rdma/vmw_pvrdma-abi.h | 7 + linux-headers/asm-generic/unistd.h | 4 +- linux-headers/asm-mips/unistd_n32.h | 1 + linux-headers/asm-mips/unistd_n64.h | 1 + linux-headers/asm-mips/unistd_o32.h | 1 + linux-headers/asm-powerpc/kvm.h | 2 + linux-headers/asm-powerpc/unistd_32.h | 1 + linux-headers/asm-powerpc/unistd_64.h | 1 + linux-headers/asm-s390/unistd_32.h | 1 + linux-headers/asm-s390/unistd_64.h | 1 + linux-headers/asm-x86/kvm.h | 1 + linux-headers/asm-x86/unistd_32.h | 1 + linux-headers/asm-x86/unistd_64.h | 1 + linux-headers/asm-x86/unistd_x32.h | 1 + linux-headers/linux/kvm.h | 89 +++++++++ linux-headers/linux/vfio.h | 169 +++++++++++++++++- 20 files changed, 337 insertions(+), 27 deletions(-)
include/standard-headers/drm/drm_fourcc.h +21 -2
@@ -526,6 +526,25 @@ extern "C" {*/ #define I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS fourcc_mod_code(INTEL, 7) +/* +* Intel Color Control Surface with Clear Color (CCS) for Gen-12 render +* compression. +* +* The main surface is Y-tiled and is at plane index 0 whereas CCS is linear +* and at index 1. The clear color is stored at index 2, and the pitch should +* be ignored. The clear color structure is 256 bits. The first 128 bits +* represents Raw Clear Color Red, Green, Blue and Alpha color each represented +* by 32 bits. The raw clear color is consumed by the 3d engine and generates +* the converted clear color of size 64 bits. The first 32 bits store the Lower +* Converted Clear Color value and the next 32 bits store the Higher Converted +* Clear Color value when applicable. The Converted Clear Color values are +* consumed by the DE. The last 64 bits are used to store Color Discard Enable +* and Depth Clear Value Valid which are ignored by the DE. A CCS cache line +* corresponds to an area of 4x1 tiles in the main surface. The main surface +* pitch is required to be a multiple of 4 tile widths. +*/ +#define I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC fourcc_mod_code(INTEL, 8) +/* * Tiled, NV12MT, grouped in 64 (pixels) x 32 (lines) -sized macroblocks *
@@ -1035,9 +1054,9 @@ drm_fourcc_canonicalize_nvidia_format_mod(uint64_t modifier)* Not all combinations are valid, and different SoCs may support different * combinations of layout and options. */ -#define __fourcc_mod_amlogic_layout_mask 0xf +#define __fourcc_mod_amlogic_layout_mask 0xff#define __fourcc_mod_amlogic_options_shift 8 -#define __fourcc_mod_amlogic_options_mask 0xf +#define __fourcc_mod_amlogic_options_mask 0xff#define DRM_FORMAT_MOD_AMLOGIC_FBC (__layout, __options) \ fourcc_mod_code(AMLOGIC, \
include/standard-headers/linux/ethtool.h +33 -21
169 lines not on this page · raw message
include/standard-headers/linux/fuse.h +2 -1
@@ -899,7 +899,8 @@ struct fuse_notify_retrieve_in { };/* Device ioctls: */ -#define FUSE_DEV_IOC_CLONE _IOR(229, 0, uint32_t) +#define FUSE_DEV_IOC_MAGIC 229 +#define FUSE_DEV_IOC_CLONE _IOR(FUSE_DEV_IOC_MAGIC, 0, uint32_t)struct fuse_lseek_in {uint64_t fh;
include/standard-headers/linux/input.h +1 -1
@@ -81,7 +81,7 @@ struct input_id {* in units per radian. * When INPUT_PROP_ACCELEROMETER is set the resolution changes. * The main axes (ABS_X, ABS_Y, ABS_Z) are then reported in -* in units per g (units/g) and in units per degree per second +* units per g (units/g) and in units per degree per second * (units/deg/s) for rotational axes (ABS_RX, ABS_RY, ABS_RZ). */ struct input_absinfo {
include/standard-headers/rdma/vmw_pvrdma-abi.h +7 -0
@@ -133,6 +133,13 @@ enum pvrdma_wc_flags { PVRDMA_WC_FLAGS_MAX = PVRDMA_WC_WITH_NETWORK_HDR_TYPE, }; +enum pvrdma_network_type { +PVRDMA_NETWORK_IB , +PVRDMA_NETWORK_ROCE_V1 =PVRDMA_NETWORK_IB , +PVRDMA_NETWORK_IPV4 , +PVRDMA_NETWORK_IPV6 +}; +struct pvrdma_alloc_ucontext_resp {uint32_t qp_tab_size;uint32_t reserved;
linux-headers/asm-generic/unistd.h +3 -1
@@ -861,9 +861,11 @@ __SYSCALL(__NR_faccessat2, sys_faccessat2) __SYSCALL(__NR_process_madvise, sys_process_madvise)#define __NR_epoll_pwait2 441 __SC_COMP(__NR_epoll_pwait2, sys_epoll_pwait2, compat_sys_epoll_pwait2) +#define __NR_mount_setattr 442 +__SYSCALL(__NR_mount_setattr, sys_mount_setattr)#undef __NR_syscalls -#define __NR_syscalls 442 +#define __NR_syscalls 443/* * 32 bit systems traditionally used different
linux-headers/asm-mips/unistd_n32.h +1 -0
@@ -371,6 +371,7 @@#define __NR_faccessat2 (__NR_Linux + 439)#define __NR_process_madvise (__NR_Linux + 440)#define __NR_epoll_pwait2 (__NR_Linux + 441) +#define __NR_mount_setattr (__NR_Linux + 442)#endif /* _ASM_MIPS_UNISTD_N32_H */
linux-headers/asm-mips/unistd_n64.h +1 -0
@@ -347,6 +347,7 @@#define __NR_faccessat2 (__NR_Linux + 439)#define __NR_process_madvise (__NR_Linux + 440)#define __NR_epoll_pwait2 (__NR_Linux + 441) +#define __NR_mount_setattr (__NR_Linux + 442)#endif /* _ASM_MIPS_UNISTD_N64_H */
linux-headers/asm-mips/unistd_o32.h +1 -0
@@ -417,6 +417,7 @@#define __NR_faccessat2 (__NR_Linux + 439)#define __NR_process_madvise (__NR_Linux + 440)#define __NR_epoll_pwait2 (__NR_Linux + 441) +#define __NR_mount_setattr (__NR_Linux + 442)#endif /* _ASM_MIPS_UNISTD_O32_H */
linux-headers/asm-powerpc/kvm.h +2 -0
@@ -644,6 +644,8 @@ struct kvm_ppc_cpu_char {#define KVM_REG_PPC_MMCR3 (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xc1)#define KVM_REG_PPC_SIER2 (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xc2)#define KVM_REG_PPC_SIER3 (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xc3) +#define KVM_REG_PPC_DAWR1 (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xc4) +#define KVM_REG_PPC_DAWRX1 (KVM_REG_PPC | KVM_REG_SIZE_U64 | 0xc5)/* Transactional Memory checkpointed state: * This is all GPRs, all VSX regs and a subset of SPRs
linux-headers/asm-powerpc/unistd_32.h +1 -0
@@ -424,6 +424,7 @@#define __NR_faccessat2 439#define __NR_process_madvise 440#define __NR_epoll_pwait2 441 +#define __NR_mount_setattr 442#endif /* _ASM_POWERPC_UNISTD_32_H */
linux-headers/asm-powerpc/unistd_64.h +1 -0
@@ -396,6 +396,7 @@#define __NR_faccessat2 439#define __NR_process_madvise 440#define __NR_epoll_pwait2 441 +#define __NR_mount_setattr 442#endif /* _ASM_POWERPC_UNISTD_64_H */
linux-headers/asm-s390/unistd_32.h +1 -0
@@ -414,5 +414,6 @@#define __NR_faccessat2 439#define __NR_process_madvise 440#define __NR_epoll_pwait2 441 +#define __NR_mount_setattr 442#endif /* _ASM_S390_UNISTD_32_H */
linux-headers/asm-s390/unistd_64.h +1 -0
@@ -362,5 +362,6 @@#define __NR_faccessat2 439#define __NR_process_madvise 440#define __NR_epoll_pwait2 441 +#define __NR_mount_setattr 442#endif /* _ASM_S390_UNISTD_64_H */
linux-headers/asm-x86/kvm.h +1 -0
@@ -112,6 +112,7 @@ struct kvm_ioapic_state {#define KVM_NR_IRQCHIPS 3#define KVM_RUN_X86_SMM (1 << 0) +#define KVM_RUN_X86_BUS_LOCK (1 << 1)/* for KVM_GET_REGS and KVM_SET_REGS */ struct kvm_regs {
linux-headers/asm-x86/unistd_32.h +1 -0
@@ -432,6 +432,7 @@#define __NR_faccessat2 439#define __NR_process_madvise 440#define __NR_epoll_pwait2 441 +#define __NR_mount_setattr 442#endif /* _ASM_X86_UNISTD_32_H */
linux-headers/asm-x86/unistd_64.h +1 -0
@@ -354,6 +354,7 @@#define __NR_faccessat2 439#define __NR_process_madvise 440#define __NR_epoll_pwait2 441 +#define __NR_mount_setattr 442#endif /* _ASM_X86_UNISTD_64_H */
linux-headers/asm-x86/unistd_x32.h +1 -0
@@ -307,6 +307,7 @@#define __NR_faccessat2 (__X32_SYSCALL_BIT + 439)#define __NR_process_madvise (__X32_SYSCALL_BIT + 440)#define __NR_epoll_pwait2 (__X32_SYSCALL_BIT + 441) +#define __NR_mount_setattr (__X32_SYSCALL_BIT + 442)#define __NR_rt_sigaction (__X32_SYSCALL_BIT + 512)#define __NR_rt_sigreturn (__X32_SYSCALL_BIT + 513)#define __NR_ioctl (__X32_SYSCALL_BIT + 514)
linux-headers/linux/kvm.h +89 -0
147 lines not on this page · raw message
linux-headers/linux/vfio.h +168 -1
251 lines not on this page · raw message
Signature · 1 line
2.26.3
RFC v9 4/29 memory: Add new fields in IOTLBEntry
In reply to Eric AugerThe current IOTLBEntry becomes too simple to interact with some physical IOMMUs. IOTLBs can be invalidated with different
granularities: domain, pasid, addr. Current IOTLB entry only offers page selective invalidation. Let's add a granularity field that conveys this information.
TLB entries are usually tagged with some ids such as the asid or pasid. When propagating an invalidation command from the guest to the host, we need to pass those IDs.
Also we add a leaf field which indicates, in case of invalidation notification, whether only cache entries for the last level of translation are required to be invalidated.
A flag field is introduced to inform whether those fields are set.
To enforce all existing users do not use those new fields, initialize the IOMMUTLBEvents when needed.
---
v7 -> v8:
- add pasid, granularity and flags
--- include/exec/memory.h | 36 +++++++++++++++++++++++++++++++++++- hw/arm/smmu-common.c | 2 +- hw/arm/smmuv3.c | 2 +- hw/i386/intel_iommu.c | 6 +++--- hw/ppc/spapr_iommu.c | 2 +- hw/virtio/virtio-iommu.c | 4 ++-- 6 files changed, 43 insertions(+), 9 deletions(-)
include/exec/memory.h +35 -1
@@ -75,14 +75,48 @@ typedef enum {IOMMU_RW =3 , } IOMMUAccessFlags; +/* Granularity of the cache invalidation */ +typedef enum { +IOMMU_INV_GRAN_ADDR =0 , +IOMMU_INV_GRAN_PASID , +IOMMU_INV_GRAN_DOMAIN , +}IOMMUInvGranularity ; +#define IOMMU_ACCESS_FLAG (r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0)) +/** +* IOMMUTLBEntry - IOMMU TLB entry +* +* Structure used when performing a translation or when notifying MAP or +* UNMAP (invalidation) events +* +* @target_as: target address space +* @iova: IO virtual address (input) +* @translated_addr: translated address (output) +* @addr_mask: address mask (0xfff means 4K binding), must be multiple of 2 +* @perm: permission flag of the mapping (NONE encodes no mapping or +* invalidation notification) +* @granularity: granularity of the invalidation +* @flags: informs whether the following fields are set +* @arch_id: architecture specific ID tagging the TLB +* @pasid: PASID tagging the TLB +* @leaf: when @perm is NONE, indicates whether only caches for the last +* level of translation need to be invalidated. +*/ struct IOMMUTLBEntry {AddressSpace *target_as;hwaddr iova;hwaddr translated_addr; -hwaddr addr_mask;/* 0xfff = 4k translation */ +hwaddr addr_mask;IOMMUAccessFlags perm; +IOMMUInvGranularity granularity; +#define IOMMU_INV_FLAGS_PASID (1 << 0) +#define IOMMU_INV_FLAGS_ARCHID (1 << 1) +#define IOMMU_INV_FLAGS_LEAF (1 << 2) +uint32_t flags; +uint32_t arch_id; +uint32_t pasid; +bool leaf; };/*
hw/arm/smmu-common.c +1 -1
@@ -471,7 +471,7 @@ IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid)/* Unmap the whole notifier's range */ static void smmu_unmap_notifier_range (IOMMUNotifier *n) { -IOMMUTLBEvent event; +IOMMUTLBEvent event = {}; event.type =IOMMU_NOTIFIER_UNMAP ; event.entry.target_as = &address_space_memory;
hw/arm/smmuv3.c +1 -1
@@ -801,7 +801,7 @@ static void smmuv3_notify_iova(IOMMUMemoryRegion *mr, uint8_t tg, uint64_t num_pages) {SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu); -IOMMUTLBEvent event; +IOMMUTLBEvent event = {};uint8_t granule;if (!tg) {
hw/i386/intel_iommu.c +3 -3
@@ -1195,7 +1195,7 @@ static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,uint32_t offset;uint64_t slpte;uint64_t subpage_size, subpage_mask; -IOMMUTLBEvent event; +IOMMUTLBEvent event = {};uint64_t iova = start;uint64_t iova_next;int ret =0 ;
@@ -2427,7 +2427,7 @@ static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) {VTDAddressSpace *vtd_dev_as; -IOMMUTLBEvent event; +IOMMUTLBEvent event = {};struct VTDBus *vtd_bus;hwaddr addr;uint64_t sz;
@@ -3483,7 +3483,7 @@ static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n) size = remain = end - start +1 ;while (remain >=VTD_PAGE_SIZE ) { -IOMMUTLBEvent event; +IOMMUTLBEvent event = {};uint64_t mask =dma_aligned_pow2_mask (start, end, s->aw_bits);uint64_t size = mask +1 ;
hw/ppc/spapr_iommu.c +1 -1
@@ -450,7 +450,7 @@ static void spapr_tce_reset(DeviceState *dev)static target_ulong put_tce_emu (SpaprTceTable *tcet,target_ulong ioba,target_ulong tce) { -IOMMUTLBEvent event; +IOMMUTLBEvent event = {};hwaddr page_mask =IOMMU_PAGE_MASK (tcet->page_shift);unsigned long index = (ioba - tcet->bus_offset) >> tcet->page_shift;
hw/virtio/virtio-iommu.c +2 -2
@@ -129,7 +129,7 @@ static void virtio_iommu_notify_map(IOMMUMemoryRegion *mr, hwaddr virt_start, hwaddr virt_end, hwaddr paddr, uint32_t flags) { - IOMMUTLBEvent event; + IOMMUTLBEvent event = {};IOMMUAccessFlags perm =IOMMU_ACCESS_FLAG (flags &VIRTIO_IOMMU_MAP_F_READ , flags &VIRTIO_IOMMU_MAP_F_WRITE );
@@ -154,7 +154,7 @@ static void virtio_iommu_notify_map(IOMMUMemoryRegion *mr, hwaddr virt_start,static void virtio_iommu_notify_unmap (IOMMUMemoryRegion *mr,hwaddr virt_start,hwaddr virt_end) { -IOMMUTLBEvent event; +IOMMUTLBEvent event = {};uint64_t delta = virt_end - virt_start;if (!(mr->iommu_notify_flags &IOMMU_NOTIFIER_UNMAP )) {
Signature · 1 line
2.26.3
RFC v9 5/29 hw/arm/smmuv3: Improve stage1 ASID invalidation
In reply to Eric AugerAt the moment ASID invalidation command (CMD_TLBI_NH_ASID) is propagated as a domain invalidation (the whole notifier range is invalidated independently on any ASID information).
The new granularity field now allows to be more precise and restrict the invalidation to a peculiar ASID. Set the corresponding fields and flag.
We still keep the iova and addr_mask settings for consumers that do not support the new fields, like VHOST.
---
v8 -> v9:
- restore the iova and addr_massk settings for consumers that do not support the new fields like VHOST
--- hw/arm/smmuv3.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- hw/arm/trace-events | 1 + 2 files changed, 43 insertions(+), 2 deletions(-)
hw/arm/smmuv3.c +42 -2
@@ -835,6 +835,31 @@ static void smmuv3_notify_iova(IOMMUMemoryRegion *mr,memory_region_notify_iommu_one (n, &event); } +/** +* smmuv3_notify_asid - call the notifier @n for a given asid +* +* @mr: IOMMU mr region handle +* @n: notifier to be called +* @asid: address space ID or negative value if we don't care +*/ +static void smmuv3_notify_asid (IOMMUMemoryRegion *mr, +IOMMUNotifier *n,int asid) +{ +IOMMUTLBEvent event = {}; + + event.type =IOMMU_NOTIFIER_UNMAP ; + event.entry.target_as = &address_space_memory; + event.entry.perm =IOMMU_NONE ; + event.entry.granularity =IOMMU_INV_GRAN_PASID ; + event.entry.flags =IOMMU_INV_FLAGS_ARCHID ; + event.entry.arch_id = asid; + event.entry.iova = n->start; + event.entry.addr_mask = n->end - n->start; + +memory_region_notify_iommu_one (n, &event); +} + +/* invalidate an asid/iova range tuple in all mr's */ static void smmuv3_inv_notifiers_iova (SMMUState *s,int asid,dma_addr_t iova,uint8_t tg,uint64_t num_pages)
@@ -910,6 +935,22 @@ smmuv3_invalidate_ste(gpointer key, gpointer value, gpointer user_data)return true; } +static void smmuv3_s1_asid_inval (SMMUState *s,uint16_t asid) +{ +SMMUDevice *sdev; + +trace_smmuv3_s1_asid_inval (asid); + QLIST_FOREACH(sdev, &s->devices_with_notifiers, next) { +IOMMUMemoryRegion *mr = &sdev->iommu; +IOMMUNotifier *n; + + IOMMU_NOTIFIER_FOREACH(n, mr) { +smmuv3_notify_asid (mr, n, asid); + } + } +smmu_iotlb_inv_asid (s, asid); +} +static int smmuv3_cmdq_consume (SMMUv3State *s) {SMMUState *bs =ARM_SMMU (s);
@@ -1020,8 +1061,7 @@ static int smmuv3_cmdq_consume(SMMUv3State *s)uint16_t asid =CMD_ASID (&cmd);trace_smmuv3_cmdq_tlbi_nh_asid (asid); -smmu_inv_notifiers_all (&s->smmu_state); -smmu_iotlb_inv_asid (bs, asid); +smmuv3_s1_asid_inval (bs, asid);break ; }case SMMU_CMD_TLBI_NH_ALL :
hw/arm/trace-events +1 -0
@@ -46,6 +46,7 @@ smmuv3_cmdq_cfgi_cd(uint32_t sid) "sid=0x%x" smmuv3_config_cache_hit(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache HIT for sid=0x%x (hits=%d, misses=%d, hit rate=%d)" smmuv3_config_cache_miss(uint32_t sid, uint32_t hits, uint32_t misses, uint32_t perc) "Config cache MISS for sid=0x%x (hits=%d, misses=%d, hit rate=%d)" smmuv3_s1_range_inval(int vmid, int asid, uint64_t addr, uint8_t tg, uint64_t num_pages, uint8_t ttl, bool leaf) "vmid=%d asid=%d addr=0x%"PRIx64" tg=%d num_pages=0x%"PRIx64" ttl=%d leaf=%d" +smmuv3_s1_asid_inval(int asid) "asid=%d" smmuv3_cmdq_tlbi_nh(void) "" smmuv3_cmdq_tlbi_nh_asid(uint16_t asid) "asid=%d" smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid=0x%x"
Signature · 1 line
2.26.3
RFC v9 6/29 hw/arm/smmu-common: Allow domain invalidation for NH_ALL/NSNH_ALL
In reply to Eric AugerNH_ALL/NSNH_ALL corresponds to a domain granularity invalidation, ie. all the notifier range gets invalidation, whatever the ASID. So let's set the granularity to IOMMU_INV_GRAN_DOMAIN to allow the consumer to benefit from the info if it can.
--- hw/arm/smmu-common.c | 1 + 1 file changed, 1 insertion(+)
hw/arm/smmu-common.c +1 -0
@@ -478,6 +478,7 @@ static void smmu_unmap_notifier_range(IOMMUNotifier *n) event.entry.iova = n->start; event.entry.perm =IOMMU_NONE ; event.entry.addr_mask = n->end - n->start; + event.entry.granularity =IOMMU_INV_GRAN_DOMAIN ;memory_region_notify_iommu_one (n, &event); }
Signature · 1 line
2.26.3
RFC v9 7/29 memory: Add IOMMU_ATTR_VFIO_NESTED IOMMU memory region attribute
In reply to Eric AugerWe introduce a new IOMMU Memory Region attribute, IOMMU_ATTR_VFIO_NESTED that tells whether the virtual IOMMU requires HW nested paging for VFIO integration.
Current Intel virtual IOMMU device supports "Caching Mode" and does not require 2 stages at physical level to be integrated with VFIO. However SMMUv3 does not implement such "caching mode" and requires to use HW nested paging.
As such SMMUv3 is the first IOMMU device to advertise this attribute.
--- include/exec/memory.h | 3 ++- hw/arm/smmuv3.c | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-)
include/exec/memory.h +2 -1
@@ -273,7 +273,8 @@ typedef struct MemoryRegionClass {enum IOMMUMemoryRegionAttr { -IOMMU_ATTR_SPAPR_TCE_FD +IOMMU_ATTR_SPAPR_TCE_FD , +IOMMU_ATTR_VFIO_NESTED , };/*
hw/arm/smmuv3.c +12 -0
@@ -1582,6 +1582,17 @@ static int smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu,return 0 ; } +static int smmuv3_get_attr (IOMMUMemoryRegion *iommu, +enum IOMMUMemoryRegionAttr attr, +void *data) +{ +if (attr ==IOMMU_ATTR_VFIO_NESTED ) { + *(bool *) data = true; +return 0 ; + } +return -EINVAL ; +} +static void smmuv3_iommu_memory_region_class_init (ObjectClass *klass,void *data) {
@@ -1589,6 +1600,7 @@ static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass, imrc->translate = smmuv3_translate; imrc->notify_flag_changed = smmuv3_notify_flag_changed; + imrc->get_attr = smmuv3_get_attr; }static const TypeInfo smmuv3_type_info = {
Signature · 1 line
2.26.3
RFC v9 8/29 memory: Add IOMMU_ATTR_MSI_TRANSLATE IOMMU memory region attribute
In reply to Eric AugerWe introduce a new IOMMU Memory Region attribute, IOMMU_ATTR_MSI_TRANSLATE which tells whether the virtual IOMMU translates MSIs. ARM SMMU will expose this attribute since, as opposed to Intel DMAR, MSIs are translated as any other DMA requests.
--- include/exec/memory.h | 1 + 1 file changed, 1 insertion(+)
include/exec/memory.h +1 -0
@@ -275,6 +275,7 @@ typedef struct MemoryRegionClass {enum IOMMUMemoryRegionAttr {IOMMU_ATTR_SPAPR_TCE_FD ,IOMMU_ATTR_VFIO_NESTED , +IOMMU_ATTR_MSI_TRANSLATE , };/*
Signature · 1 line
2.26.3
RFC v9 9/29 memory: Introduce IOMMU Memory Region inject_faults API
In reply to Eric AugerThis new API allows to inject @count iommu_faults into the IOMMU memory region.
--- include/exec/memory.h | 24 ++++++++++++++++++++++++ softmmu/memory.c | 10 ++++++++++ 2 files changed, 34 insertions(+)
include/exec/memory.h +24 -0
@@ -65,6 +65,8 @@ struct ReservedRegion {unsigned type; }; +struct iommu_fault ; +typedef struct IOMMUTLBEntry IOMMUTLBEntry ;/* See address_space_translate: bit 0 is read, bit 1 is write. */
@@ -475,6 +477,19 @@ struct IOMMUMemoryRegionClass {int (*iommu_set_page_size_mask)(IOMMUMemoryRegion *iommu,uint64_t page_size_mask,Error **errp); + +/* +* Inject @count faults into the IOMMU memory region +* +* Optional method: if this method is not provided, then +* memory_region_injection_faults() will return -ENOENT +* +* @iommu: the IOMMU memory region to inject the faults in +* @count: number of faults to inject +* @buf: fault buffer +*/ +int (*inject_faults)(IOMMUMemoryRegion *iommu,int count, +struct iommu_fault *buf); };typedef struct CoalescedMemoryRange CoalescedMemoryRange ;
@@ -1520,6 +1535,15 @@ int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);int memory_region_iommu_set_page_size_mask (IOMMUMemoryRegion *iommu_mr,uint64_t page_size_mask,Error **errp); +/** +* memory_region_inject_faults : inject @count faults stored in @buf +* +* @iommu_mr: the IOMMU memory region +* @count: number of faults to be injected +* @buf: buffer containing the faults +*/ +int memory_region_inject_faults (IOMMUMemoryRegion *iommu_mr,int count, +struct iommu_fault *buf);/** * memory_region_name: get a memory region's name
softmmu/memory.c +10 -0
@@ -2030,6 +2030,16 @@ int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr)return imrc->num_indexes (iommu_mr); } +int memory_region_inject_faults (IOMMUMemoryRegion *iommu_mr,int count, +struct iommu_fault *buf) +{ +IOMMUMemoryRegionClass *imrc =IOMMU_MEMORY_REGION_GET_CLASS (iommu_mr); +if (!imrc->inject_faults) { +return -ENOENT ; + } +return imrc->inject_faults (iommu_mr, count, buf); +} +void memory_region_set_log (MemoryRegion *mr,bool log,unsigned client) {uint8_t mask =1 << client;
Signature · 1 line
2.26.3
RFC v9 10/29 iommu: Introduce generic header
In reply to Eric AugerThis header is meant to exposes data types used by several IOMMU devices such as struct for SVA and nested stage configuration.
--- include/hw/iommu/iommu.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 include/hw/iommu/iommu.h
include/hw/iommu/iommu.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* common header for iommu devices +* +* Copyright Red Hat, Inc. 2019 +* +* Authors: +* Eric Auger <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +*/ + +#ifndef QEMU_HW_IOMMU_IOMMU_H +#define QEMU_HW_IOMMU_IOMMU_H +#ifdef __linux__ +#include <linux/iommu.h> +#endif + +typedef struct IOMMUConfig { +union { +#ifdef __linux__ +struct iommu_pasid_table_config pasid_cfg; +#endif + }; +}IOMMUConfig ; + + +#endif /* QEMU_HW_IOMMU_IOMMU_H */
Signature · 1 line
2.26.3
RFC v9 11/29 pci: introduce PCIPASIDOps to PCIDevice
In reply to Eric AugerFrom: Liu Yi L <[email protected]>
This patch introduces PCIPASIDOps for IOMMU related operations.
https://lists.gnu.org/archive/html/qemu-devel/2018-03/msg00078.html
https://lists.gnu.org/archive/html/qemu-devel/2018-03/msg00940.html
So far, to setup virt-SVA for assigned SVA capable device, needs to configure host translation structures for specific pasid. (e.g. bind guest page table to host and enable nested translation in host). Besides, vIOMMU emulator needs to forward guest's cache invalidation to host since host nested translation is enabled. e.g. on VT-d, guest owns 1st level translation table, thus cache invalidation for 1st level should be propagated to host.
This patch adds two functions: alloc_pasid and free_pasid to support guest pasid allocation and free. The implementations of the callbacks would be device passthru modules. Like vfio.
--- include/hw/pci/pci.h | 11 +++++++++++ hw/pci/pci.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)
include/hw/pci/pci.h +11 -0
@@ -9,6 +9,7 @@#include "hw/pci/pcie.h" #include "qom/object.h" +#include "hw/iommu/iommu.h" extern bool pci_available;
@@ -265,6 +266,11 @@ struct PCIReqIDCache { };typedef struct PCIReqIDCache PCIReqIDCache ; +struct PCIPASIDOps { +int (*set_pasid_table)(PCIBus *bus,int32_t devfn,IOMMUConfig *config); +}; +typedef struct PCIPASIDOps PCIPASIDOps ; +struct PCIDevice {DeviceState qdev;bool partially_hotplugged;
@@ -360,6 +366,7 @@ struct PCIDevice {/* ID of standby device in net_failover pair */ char *failover_pair_id;uint32_t acpi_index; +PCIPASIDOps *pasid_ops; }; void pci_register_bar(PCIDevice *pci_dev, int region_num,
@@ -491,6 +498,10 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);AddressSpace *pci_device_iommu_address_space (PCIDevice *dev);void pci_setup_iommu (PCIBus *bus,PCIIOMMUFunc fn,void *opaque); +void pci_setup_pasid_ops (PCIDevice *dev,PCIPASIDOps *ops); +bool pci_device_is_pasid_ops_set (PCIBus *bus,int32_t devfn); +int pci_device_set_pasid_table (PCIBus *bus,int32_t devfn,IOMMUConfig *config); +static inline void pci_set_byte (uint8_t *config,uint8_t val) {
hw/pci/pci.c +34 -0
@@ -2731,6 +2731,40 @@ void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque) bus->iommu_opaque = opaque; } +void pci_setup_pasid_ops (PCIDevice *dev,PCIPASIDOps *ops) +{ +assert (ops && !dev->pasid_ops); + dev->pasid_ops = ops; +} + +bool pci_device_is_pasid_ops_set (PCIBus *bus,int32_t devfn) +{ +PCIDevice *dev; + +if (!bus) { +return false; + } + + dev = bus->devices[devfn]; +return !!(dev && dev->pasid_ops); +} + +int pci_device_set_pasid_table (PCIBus *bus,int32_t devfn, +IOMMUConfig *config) +{ +PCIDevice *dev; + +if (!bus) { +return -EINVAL ; + } + + dev = bus->devices[devfn]; +if (dev && dev->pasid_ops && dev->pasid_ops->set_pasid_table) { +return dev->pasid_ops->set_pasid_table (bus, devfn, config); + } +return -ENOENT ; +} +static void pci_dev_get_w64 (PCIBus *b,PCIDevice *dev,void *opaque) {Range *range = opaque;
Signature · 1 line
2.26.3
RFC v9 12/29 vfio: Force nested if iommu requires it
In reply to Eric AugerIn case we detect the address space is translated by a virtual IOMMU which requires HW nested paging to integrate with VFIO, let's set up the container with the VFIO_TYPE1_NESTING_IOMMU iommu_type.
---
v7 -> v8
- remove as != &address_space_memory as memory_region_is_iommu(as->root) is sufficient [Kunkun]
v4 -> v5:
- fail immediatly if nested is wanted but not supported
v2 -> v3:
- add "nested only is selected if requested by @force_nested" comment in this patch
--- hw/vfio/common.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-)
hw/vfio/common.c +28 -8
@@ -1579,27 +1579,38 @@ static void vfio_put_address_space(VFIOAddressSpace *space)* vfio_get_iommu_type - selects the richest iommu_type (v2 first) */ static int vfio_get_iommu_type (VFIOContainer *container, +bool want_nested,Error **errp) { -int iommu_types[] = { VFIO_TYPE1v2_IOMMU,VFIO_TYPE1_IOMMU , +int iommu_types[] = {VFIO_TYPE1_NESTING_IOMMU , + VFIO_TYPE1v2_IOMMU,VFIO_TYPE1_IOMMU , VFIO_SPAPR_TCE_v2_IOMMU,VFIO_SPAPR_TCE_IOMMU }; -int i; +int i, ret = -EINVAL ;for (i =0 ; i <ARRAY_SIZE (iommu_types); i++) {if (ioctl (container->fd,VFIO_CHECK_EXTENSION , iommu_types[i])) { -return iommu_types[i]; +if (iommu_types[i] ==VFIO_TYPE1_NESTING_IOMMU && !want_nested) { +continue ; + } + ret = iommu_types[i]; +break ; } } -error_setg (errp,"No available IOMMU models" ); -return -EINVAL ; +if (ret <0 ) { +error_setg (errp,"No available IOMMU models" ); + }else if (want_nested && ret !=VFIO_TYPE1_NESTING_IOMMU ) { +error_setg (errp,"Nested mode requested but not supported" ); + ret = -EINVAL ; + } +return ret; }static int vfio_init_container (VFIOContainer *container,int group_fd, -Error **errp) +bool want_nested,Error **errp) {int iommu_type, ret; - iommu_type =vfio_get_iommu_type (container, errp); + iommu_type =vfio_get_iommu_type (container, want_nested, errp);if (iommu_type <0 ) {return iommu_type; }
@@ -1704,6 +1715,14 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,VFIOContainer *container;int ret, fd;VFIOAddressSpace *space; +IOMMUMemoryRegion *iommu_mr; +bool nested = false; + +if (memory_region_is_iommu (as->root)) { + iommu_mr =IOMMU_MEMORY_REGION (as->root); +memory_region_iommu_get_attr (iommu_mr,IOMMU_ATTR_VFIO_NESTED , + (void *)&nested); + } space =vfio_get_address_space (as);
@@ -1773,13 +1792,14 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list); - ret =vfio_init_container (container, group->fd, errp); + ret =vfio_init_container (container, group->fd, nested, errp);if (ret) { goto free_container_exit; }trace_vfio_connect_new_container (group->groupid, container->fd);switch (container->iommu_type) { +case VFIO_TYPE1_NESTING_IOMMU :case VFIO_TYPE1v2_IOMMU:case VFIO_TYPE1_IOMMU : {
Signature · 1 line
2.26.3
RFC v9 13/29 vfio: Introduce hostwin_from_range helper
In reply to Eric AugerLet's introduce a hostwin_from_range() helper that returns the hostwin encapsulating an IOVA range or NULL if none is found.
This improves the readibility of callers and removes the usage of hostwin_found.
--- hw/vfio/common.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-)
hw/vfio/common.c +17 -19
@@ -649,6 +649,19 @@ out:rcu_read_unlock (); } +static VFIOHostDMAWindow * +hostwin_from_range (VFIOContainer *container,hwaddr iova,hwaddr end) +{ +VFIOHostDMAWindow *hostwin; + + QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { +if (hostwin->min_iova <= iova && end <= hostwin->max_iova) { +return hostwin; + } + } +return NULL ; +} +static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {
@@ -658,7 +671,6 @@ static void vfio_listener_region_add(MemoryListener *listener,void *vaddr;int ret;VFIOHostDMAWindow *hostwin; -bool hostwin_found;Error *err =NULL ;if (vfio_listener_skipped_section (section)) {
@@ -744,15 +756,8 @@ static void vfio_listener_region_add(MemoryListener *listener,#endif } - hostwin_found = false; - QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { -if (hostwin->min_iova <= iova && end <= hostwin->max_iova) { - hostwin_found = true; -break ; - } - } - -if (!hostwin_found) { + hostwin =hostwin_from_range (container, iova, end); +if (!hostwin) {error_setg (&err,"Container %p can't map guest IOVA region" " 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); goto fail;
@@ -934,16 +939,9 @@ static void vfio_listener_region_del(MemoryListener *listener,if (memory_region_is_ram_device (section->mr)) {hwaddr pgmask; -VFIOHostDMAWindow *hostwin; -bool hostwin_found = false; +VFIOHostDMAWindow *hostwin =hostwin_from_range (container, iova, end); - QLIST_FOREACH(hostwin, &container->hostwin_list, hostwin_next) { -if (hostwin->min_iova <= iova && end <= hostwin->max_iova) { - hostwin_found = true; -break ; - } - } -assert (hostwin_found);/* or region_add() would have failed */ +assert (hostwin);/* or region_add() would have failed */ pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; try_unmap = !((iova & pgmask) || (int128_get64 (llsize) & pgmask));
Signature · 1 line
2.26.3
RFC v9 14/29 vfio: Introduce helpers to DMA map/unmap a RAM section
In reply to Eric AugerLet's introduce two helpers that allow to DMA map/unmap a RAM section. Those helpers will be called for nested stage setup in another call site. Also the vfio_listener_region_add/del() structure may be clearer.
---
v8 -> v9
- rebase on top of
1eb7f642750c ("vfio: Support host translation granule size")
v5 -> v6:
- add Error **
--- hw/vfio/common.c | 199 +++++++++++++++++++++++++------------------ hw/vfio/trace-events | 4 +- 2 files changed, 119 insertions(+), 84 deletions(-)
hw/vfio/common.c +117 -82
@@ -662,13 +662,126 @@ hostwin_from_range(VFIOContainer *container, hwaddr iova, hwaddr end)return NULL ; } +static int vfio_dma_map_ram_section (VFIOContainer *container, +MemoryRegionSection *section,Error **err) +{ +VFIOHostDMAWindow *hostwin; +Int128 llend, llsize; +hwaddr iova, end; +void *vaddr; +int ret; + +assert (memory_region_is_ram (section->mr)); + + iova =TARGET_PAGE_ALIGN (section->offset_within_address_space); + llend =int128_make64 (section->offset_within_address_space); + llend =int128_add (llend, section->size); + llend =int128_and (llend,int128_exts64 (TARGET_PAGE_MASK )); + end =int128_get64 (int128_sub (llend,int128_one ())); + + vaddr =memory_region_get_ram_ptr (section->mr) + + section->offset_within_region + + (iova - section->offset_within_address_space); + + hostwin =hostwin_from_range (container, iova, end); +if (!hostwin) { +error_setg (err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); +return -EFAULT ; + } + +trace_vfio_dma_map_ram (iova, end, vaddr); + + llsize =int128_sub (llend,int128_make64 (iova)); + +if (memory_region_is_ram_device (section->mr)) { +hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; + +if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) { +trace_vfio_listener_region_add_no_dma_map ( +memory_region_name (section->mr), + section->offset_within_address_space, +int128_getlo (section->size), + pgmask +1 ); +return 0 ; + } + } + + ret =vfio_dma_map (container, iova,int128_get64 (llsize), + vaddr, section->readonly); +if (ret) { +error_setg (err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx", %p) = %d (%m)" , + container, iova,int128_get64 (llsize), vaddr, ret); +if (memory_region_is_ram_device (section->mr)) { +/* Allow unexpected mappings not to be fatal for RAM devices */ +error_report_err (*err); +return 0 ; + } +return ret; + } +return 0 ; +} + +static void vfio_dma_unmap_ram_section (VFIOContainer *container, +MemoryRegionSection *section) +{ +Int128 llend, llsize; +hwaddr iova, end; +bool try_unmap = true; +int ret; + + iova =REAL_HOST_PAGE_ALIGN (section->offset_within_address_space); + llend =int128_make64 (section->offset_within_address_space); + llend =int128_add (llend, section->size); + llend =int128_and (llend,int128_exts64 (qemu_real_host_page_mask)); + +if (int128_ge (int128_make64 (iova), llend)) { +return ; + } + end =int128_get64 (int128_sub (llend,int128_one ())); + + llsize =int128_sub (llend,int128_make64 (iova)); + +trace_vfio_dma_unmap_ram (iova, end); + +if (memory_region_is_ram_device (section->mr)) { +hwaddr pgmask; +VFIOHostDMAWindow *hostwin =hostwin_from_range (container, iova, end); + +assert (hostwin);/* or region_add() would have failed */ + + pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; + try_unmap = !((iova & pgmask) || (int128_get64 (llsize) & pgmask)); + } + +if (try_unmap) { +if (int128_eq (llsize,int128_2_64 ())) { +/* The unmap ioctl doesn't accept a full 64-bit span. */ + llsize =int128_rshift (llsize,1 ); + ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); +if (ret) { +error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx") = %d (%m)" , + container, iova,int128_get64 (llsize), ret); + } + iova +=int128_get64 (llsize); + } + ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); +if (ret) { +error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx") = %d (%m)" , + container, iova,int128_get64 (llsize), ret); + } + } +} +static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener);hwaddr iova, end; -Int128 llend, llsize; -void *vaddr; +Int128 llend;int ret;VFIOHostDMAWindow *hostwin;Error *err =NULL ;
@@ -814,39 +927,7 @@ static void vfio_listener_region_add(MemoryListener *listener, }/* Here we assume that memory_region_is_ram(section->mr)==true */ - - vaddr =memory_region_get_ram_ptr (section->mr) + - section->offset_within_region + - (iova - section->offset_within_address_space); - -trace_vfio_listener_region_add_ram (iova, end, vaddr); - - llsize =int128_sub (llend,int128_make64 (iova)); - -if (memory_region_is_ram_device (section->mr)) { -hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; - -if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) { -trace_vfio_listener_region_add_no_dma_map ( -memory_region_name (section->mr), - section->offset_within_address_space, -int128_getlo (section->size), - pgmask +1 ); -return ; - } - } - - ret =vfio_dma_map (container, iova,int128_get64 (llsize), - vaddr, section->readonly); -if (ret) { -error_setg (&err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx", %p) = %d (%m)" , - container, iova,int128_get64 (llsize), vaddr, ret); -if (memory_region_is_ram_device (section->mr)) { -/* Allow unexpected mappings not to be fatal for RAM devices */ -error_report_err (err); -return ; - } +if (vfio_dma_map_ram_section (container, section, &err)) { goto fail; }
@@ -880,10 +961,6 @@ static void vfio_listener_region_del(MemoryListener *listener, MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener); -hwaddr iova, end; -Int128 llend, llsize; -int ret; -bool try_unmap = true;if (vfio_listener_skipped_section (section)) { trace_vfio_listener_region_del_skip(
@@ -923,49 +1000,7 @@ static void vfio_listener_region_del(MemoryListener *listener,*/ } - iova =REAL_HOST_PAGE_ALIGN (section->offset_within_address_space); - llend =int128_make64 (section->offset_within_address_space); - llend =int128_add (llend, section->size); - llend =int128_and (llend,int128_exts64 (qemu_real_host_page_mask)); - -if (int128_ge (int128_make64 (iova), llend)) { -return ; - } - end =int128_get64 (int128_sub (llend,int128_one ())); - - llsize =int128_sub (llend,int128_make64 (iova)); - -trace_vfio_listener_region_del (iova, end); - -if (memory_region_is_ram_device (section->mr)) { -hwaddr pgmask; -VFIOHostDMAWindow *hostwin =hostwin_from_range (container, iova, end); - -assert (hostwin);/* or region_add() would have failed */ - - pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; - try_unmap = !((iova & pgmask) || (int128_get64 (llsize) & pgmask)); - } - -if (try_unmap) { -if (int128_eq (llsize,int128_2_64 ())) { -/* The unmap ioctl doesn't accept a full 64-bit span. */ - llsize =int128_rshift (llsize,1 ); - ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); -if (ret) { -error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx") = %d (%m)" , - container, iova,int128_get64 (llsize), ret); - } - iova +=int128_get64 (llsize); - } - ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); -if (ret) { -error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx") = %d (%m)" , - container, iova,int128_get64 (llsize), ret); - } - } +vfio_dma_unmap_ram_section (container, section);memory_region_unref (section->mr);
hw/vfio/trace-events +2 -2
@@ -99,10 +99,10 @@ vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "i vfio_listener_region_add_skip(uint64_t start, uint64_t end) "SKIPPING region_add 0x%"PRIx64" - 0x%"PRIx64 vfio_spapr_group_attach(int groupfd, int tablefd) "Attached groupfd %d to liobn fd %d" vfio_listener_region_add_iommu(uint64_t start, uint64_t end) "region_add [iommu] 0x%"PRIx64" - 0x%"PRIx64 -vfio_listener_region_add_ram(uint64_t iova_start, uint64_t iova_end, void *vaddr) "region_add [ram] 0x%"PRIx64" - 0x%"PRIx64" [%p]" +vfio_dma_map_ram(uint64_t iova_start, uint64_t iova_end, void *vaddr) "region_add [ram] 0x%"PRIx64" - 0x%"PRIx64" [%p]" vfio_listener_region_add_no_dma_map(const char *name, uint64_t iova, uint64_t size, uint64_t page_size) "Region \"%s\" 0x%"PRIx64" size=0x%"PRIx64" is not aligned to 0x%"PRIx64" and cannot be mapped for DMA" vfio_listener_region_del_skip(uint64_t start, uint64_t end) "SKIPPING region_del 0x%"PRIx64" - 0x%"PRIx64 -vfio_listener_region_del(uint64_t start, uint64_t end) "region_del 0x%"PRIx64" - 0x%"PRIx64 +vfio_dma_unmap_ram(uint64_t start, uint64_t end) "region_del 0x%"PRIx64" - 0x%"PRIx64 vfio_disconnect_container(int fd) "close container->fd=%d" vfio_connect_existing_container(int groupid, int container_fd) "group=%d existing container fd=%d" vfio_connect_new_container(int groupid, int container_fd) "group=%d new container fd=%d"
Signature · 1 line
2.26.3
Re: [RFC v9 14/29] vfio: Introduce helpers to DMA map/unmap a RAM section
Hi Eric,
Quoted 206 lines · Eric Auger
Let's introduce two helpers that allow to DMA map/unmap a RAM section. Those helpers will be called for nested stage setup in another call site. Also the vfio_listener_region_add/del() structure may be clearer.
Signed-off-by Eric Auger <[email protected]>---
v8 -> v9
- rebase on top of
1eb7f642750c("vfio: Support host translation granule size")v5 -> v6:
- add Error **--- hw/vfio/common.c | 199 +++++++++++++++++++++++++------------------ hw/vfio/trace-events | 4 +- 2 files changed, 119 insertions(+), 84 deletions(-)hw/vfio/common.c +116
-39@@ -662,13 +662,126 @@ hostwin_from_range(VFIOContainer *container, hwaddr iova, hwaddr end)return NULL ; } +static int vfio_dma_map_ram_section (VFIOContainer *container, +MemoryRegionSection *section,Error **err) +{ +VFIOHostDMAWindow *hostwin; +Int128 llend, llsize; +hwaddr iova, end; +void *vaddr; +int ret; + +assert (memory_region_is_ram (section->mr)); + + iova =TARGET_PAGE_ALIGN (section->offset_within_address_space); + llend =int128_make64 (section->offset_within_address_space); + llend =int128_add (llend, section->size); + llend =int128_and (llend,int128_exts64 (TARGET_PAGE_MASK )); + end =int128_get64 (int128_sub (llend,int128_one ())); + + vaddr =memory_region_get_ram_ptr (section->mr) + + section->offset_within_region + + (iova - section->offset_within_address_space); + + hostwin =hostwin_from_range (container, iova, end); +if (!hostwin) { +error_setg (err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); +return -EFAULT ; + } + +trace_vfio_dma_map_ram (iova, end, vaddr); + + llsize =int128_sub (llend,int128_make64 (iova)); + +if (memory_region_is_ram_device (section->mr)) { +hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; + +if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) { +trace_vfio_listener_region_add_no_dma_map ( +memory_region_name (section->mr), + section->offset_within_address_space, +int128_getlo (section->size), + pgmask +1 ); +return 0 ; + } + } + + ret =vfio_dma_map (container, iova,int128_get64 (llsize), + vaddr, section->readonly); +if (ret) { +error_setg (err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx", %p) = %d (%m)" , + container, iova,int128_get64 (llsize), vaddr, ret); +if (memory_region_is_ram_device (section->mr)) { +/* Allow unexpected mappings not to be fatal for RAM devices */ +error_report_err (*err); +return 0 ; + } +return ret; + } +return 0 ; +} + +static void vfio_dma_unmap_ram_section (VFIOContainer *container, +MemoryRegionSection *section) +{ +Int128 llend, llsize; +hwaddr iova, end; +bool try_unmap = true; +int ret; + + iova =REAL_HOST_PAGE_ALIGN (section->offset_within_address_space); + llend =int128_make64 (section->offset_within_address_space); + llend =int128_add (llend, section->size); + llend =int128_and (llend,int128_exts64 (qemu_real_host_page_mask)); + +if (int128_ge (int128_make64 (iova), llend)) { +return ; + } + end =int128_get64 (int128_sub (llend,int128_one ())); + + llsize =int128_sub (llend,int128_make64 (iova)); + +trace_vfio_dma_unmap_ram (iova, end); + +if (memory_region_is_ram_device (section->mr)) { +hwaddr pgmask; +VFIOHostDMAWindow *hostwin =hostwin_from_range (container, iova, end); + +assert (hostwin);/* or region_add() would have failed */ + + pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; + try_unmap = !((iova & pgmask) || (int128_get64 (llsize) & pgmask)); + } + +if (try_unmap) { +if (int128_eq (llsize,int128_2_64 ())) { +/* The unmap ioctl doesn't accept a full 64-bit span. */ + llsize =int128_rshift (llsize,1 ); + ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); +if (ret) { +error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx") = %d (%m)" , + container, iova,int128_get64 (llsize), ret); + } + iova +=int128_get64 (llsize); + } + ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); +if (ret) { +error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx") = %d (%m)" , + container, iova,int128_get64 (llsize), ret); + } + } +} +static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener);hwaddr iova, end; -Int128 llend, llsize; -void *vaddr; +Int128 llend;int ret;VFIOHostDMAWindow *hostwin;Error *err =NULL ;@@ -814,39 +927,7 @@ static void vfio_listener_region_add(MemoryListener *listener, }/* Here we assume that memory_region_is_ram(section->mr)==true */ - - vaddr =memory_region_get_ram_ptr (section->mr) + - section->offset_within_region + - (iova - section->offset_within_address_space); - -trace_vfio_listener_region_add_ram (iova, end, vaddr); - - llsize =int128_sub (llend,int128_make64 (iova)); - -if (memory_region_is_ram_device (section->mr)) { -hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; - -if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) { -trace_vfio_listener_region_add_no_dma_map ( -memory_region_name (section->mr), - section->offset_within_address_space, -int128_getlo (section->size), - pgmask +1 ); -return ; - } - } - - ret =vfio_dma_map (container, iova,int128_get64 (llsize), - vaddr, section->readonly); -if (ret) { -error_setg (&err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx", %p) = %d (%m)" , - container, iova,int128_get64 (llsize), vaddr, ret); -if (memory_region_is_ram_device (section->mr)) { -/* Allow unexpected mappings not to be fatal for RAM devices */ -error_report_err (err); -return ; - } +if (vfio_dma_map_ram_section (container, section, &err)) { goto fail; }@@ -880,10 +961,6 @@ static void vfio_listener_region_del(MemoryListener *listener, MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener); -hwaddr iova, end; -Int128 llend, llsize; -int ret; -bool try_unmap = true;if (vfio_listener_skipped_section (section)) { trace_vfio_listener_region_del_skip(
hw/vfio/common.c static void vfio_listener_region_del(MemoryListener *listener, · line 1001
Quoted 2 lines
hw/vfio/common.c +0
-0@@ -923,49 +1000,7 @@ static void vfio_listener_region_del(MemoryListener *listener,*/ }
There are some codes of vfio_listener_region_del() that just doesn't show up. I post it below.
Quoted 14 lines
hw/vfio/common.c +0
-0if (memory_region_is_iommu (section->mr)) { VFIOGuestIOMMU *giommu; QLIST_FOREACH(giommu, &container->giommu_list, giommu_next) {if (
MEMORY_REGION(giommu->iommu)==section->mr&&giommu->n.start==section->offset_within_region) {memory_region_unregister_iommu_notifier(section->mr, &giommu->n);
QLIST_REMOVE(giommu, giommu_next);g_free(giommu);
break;
}
}
Quoted 8 lines
/*
* FIXME: We assume the one big unmap below is adequate to
* remove any individual page mappings in the IOMMU which
* might have been copied into VFIO. This works for a page table
* based IOMMU where a big unmap flattens a large range of IO-PTEs.
* That may not be true for all IOMMU types.
*/
}
I think we need a check here. If it is nested mode, just return after g_free(giommu). Because in nested mode, stage 2 (gpa->hpa) and the stage 1 (giova->gpa) are set separately.
When hot delete a pci device, we are going to call vfio_listener_region_del() and vfio_prereg_listener_region_del(). So it's not appropriate to unmap RAM section in vfio_listener_region_del(). The RAM section will be unmap in vfio_prereg_listener_region_del().
Thanks,
Kunkun Jiang
Quoted 65 lines
hw/vfio/common.c +1
-43- iova =REAL_HOST_PAGE_ALIGN (section->offset_within_address_space); - llend =int128_make64 (section->offset_within_address_space); - llend =int128_add (llend, section->size); - llend =int128_and (llend,int128_exts64 (qemu_real_host_page_mask)); - -if (int128_ge (int128_make64 (iova), llend)) { -return ; - } - end =int128_get64 (int128_sub (llend,int128_one ())); - - llsize =int128_sub (llend,int128_make64 (iova)); - -trace_vfio_listener_region_del (iova, end); - -if (memory_region_is_ram_device (section->mr)) { -hwaddr pgmask; -VFIOHostDMAWindow *hostwin =hostwin_from_range (container, iova, end); - -assert (hostwin);/* or region_add() would have failed */ - - pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; - try_unmap = !((iova & pgmask) || (int128_get64 (llsize) & pgmask)); - } - -if (try_unmap) { -if (int128_eq (llsize,int128_2_64 ())) { -/* The unmap ioctl doesn't accept a full 64-bit span. */ - llsize =int128_rshift (llsize,1 ); - ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); -if (ret) { -error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx") = %d (%m)" , - container, iova,int128_get64 (llsize), ret); - } - iova +=int128_get64 (llsize); - } - ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); -if (ret) { -error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx") = %d (%m)" , - container, iova,int128_get64 (llsize), ret); - } - } +vfio_dma_unmap_ram_section (container, section);memory_region_unref (section->mr);hw/vfio/trace-events +2
-2@@ -99,10 +99,10 @@ vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "i vfio_listener_region_add_skip(uint64_t start, uint64_t end) "SKIPPING region_add 0x%"PRIx64" - 0x%"PRIx64 vfio_spapr_group_attach(int groupfd, int tablefd) "Attached groupfd %d to liobn fd %d" vfio_listener_region_add_iommu(uint64_t start, uint64_t end) "region_add [iommu] 0x%"PRIx64" - 0x%"PRIx64 -vfio_listener_region_add_ram(uint64_t iova_start, uint64_t iova_end, void *vaddr) "region_add [ram] 0x%"PRIx64" - 0x%"PRIx64" [%p]" +vfio_dma_map_ram(uint64_t iova_start, uint64_t iova_end, void *vaddr) "region_add [ram] 0x%"PRIx64" - 0x%"PRIx64" [%p]" vfio_listener_region_add_no_dma_map(const char *name, uint64_t iova, uint64_t size, uint64_t page_size) "Region \"%s\" 0x%"PRIx64" size=0x%"PRIx64" is not aligned to 0x%"PRIx64" and cannot be mapped for DMA" vfio_listener_region_del_skip(uint64_t start, uint64_t end) "SKIPPING region_del 0x%"PRIx64" - 0x%"PRIx64 -vfio_listener_region_del(uint64_t start, uint64_t end) "region_del 0x%"PRIx64" - 0x%"PRIx64 +vfio_dma_unmap_ram(uint64_t start, uint64_t end) "region_del 0x%"PRIx64" - 0x%"PRIx64 vfio_disconnect_container(int fd) "close container->fd=%d" vfio_connect_existing_container(int groupid, int container_fd) "group=%d existing container fd=%d" vfio_connect_new_container(int groupid, int container_fd) "group=%d new container fd=%d"
Re: [RFC v9 14/29] vfio: Introduce helpers to DMA map/unmap a RAM section
In reply to Eric AugerHi Eric,
Quoted 33 lines · Eric Auger
Let's introduce two helpers that allow to DMA map/unmap a RAM section. Those helpers will be called for nested stage setup in another call site. Also the vfio_listener_region_add/del() structure may be clearer.
Signed-off-by Eric Auger <[email protected]>---
v8 -> v9
- rebase on top of
1eb7f642750c("vfio: Support host translation granule size")v5 -> v6:
- add Error **--- hw/vfio/common.c | 199 +++++++++++++++++++++++++------------------ hw/vfio/trace-events | 4 +- 2 files changed, 119 insertions(+), 84 deletions(-)hw/vfio/common.c +5
-0@@ -662,13 +662,126 @@ hostwin_from_range(VFIOContainer *container, hwaddr iova, hwaddr end)return NULL ; } +static int vfio_dma_map_ram_section (VFIOContainer *container, +MemoryRegionSection *section,Error **err) +{ +VFIOHostDMAWindow *hostwin; +Int128 llend, llsize;
hw/vfio/common.c hostwin_from_range(VFIOContainer *container, hwaddr iova, hwaddr end) · line 679
Quoted 10 lines
hw/vfio/common.c +10
-0+hwaddr iova, end; +void *vaddr; +int ret; + +assert (memory_region_is_ram (section->mr)); + + iova =TARGET_PAGE_ALIGN (section->offset_within_address_space); + llend =int128_make64 (section->offset_within_address_space); + llend =int128_add (llend, section->size); + llend =int128_and (llend,int128_exts64 (TARGET_PAGE_MASK ));
Is there any special meaning for using TRAGET_PAGE_ALIGN here? REAL_HOST_PAGE_ALIGN is used in vfio_listener_region_add.
And I think a check should be added here if using REAL_HOST_PAGE_ALIGN.
hw/vfio/common.c hostwin_from_range(VFIOContainer *container, hwaddr iova, hwaddr end) · line 682
Quoted 3 lines
hw/vfio/common.c +0
-0if (int128_ge (int128_make64 (iova), llend)) {return ; }
It will cause an error log or 'assert(r ==a )' of int128_get64 by calling vfio_prereg_listener_region_add in some scenarios. Some devices' BAR may map MSI-X structures and others in one host page.
By the way, is this set of patch to be updated after "/dev/iommu" is sent out?
Thanks,
Kunkun Jiang
Quoted 230 lines
hw/vfio/common.c +100
-2+ end =int128_get64 (int128_sub (llend,int128_one ())); + + vaddr =memory_region_get_ram_ptr (section->mr) + + section->offset_within_region + + (iova - section->offset_within_address_space); + + hostwin =hostwin_from_range (container, iova, end); +if (!hostwin) { +error_setg (err,"Container %p can't map guest IOVA region" +" 0x%" HWADDR_PRIx"..0x%" HWADDR_PRIx, container, iova, end); +return -EFAULT ; + } + +trace_vfio_dma_map_ram (iova, end, vaddr); + + llsize =int128_sub (llend,int128_make64 (iova)); + +if (memory_region_is_ram_device (section->mr)) { +hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; + +if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) { +trace_vfio_listener_region_add_no_dma_map ( +memory_region_name (section->mr), + section->offset_within_address_space, +int128_getlo (section->size), + pgmask +1 ); +return 0 ; + } + } + + ret =vfio_dma_map (container, iova,int128_get64 (llsize), + vaddr, section->readonly); +if (ret) { +error_setg (err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx", %p) = %d (%m)" , + container, iova,int128_get64 (llsize), vaddr, ret); +if (memory_region_is_ram_device (section->mr)) { +/* Allow unexpected mappings not to be fatal for RAM devices */ +error_report_err (*err); +return 0 ; + } +return ret; + } +return 0 ; +} + +static void vfio_dma_unmap_ram_section (VFIOContainer *container, +MemoryRegionSection *section) +{ +Int128 llend, llsize; +hwaddr iova, end; +bool try_unmap = true; +int ret; + + iova =REAL_HOST_PAGE_ALIGN (section->offset_within_address_space); + llend =int128_make64 (section->offset_within_address_space); + llend =int128_add (llend, section->size); + llend =int128_and (llend,int128_exts64 (qemu_real_host_page_mask)); + +if (int128_ge (int128_make64 (iova), llend)) { +return ; + } + end =int128_get64 (int128_sub (llend,int128_one ())); + + llsize =int128_sub (llend,int128_make64 (iova)); + +trace_vfio_dma_unmap_ram (iova, end); + +if (memory_region_is_ram_device (section->mr)) { +hwaddr pgmask; +VFIOHostDMAWindow *hostwin =hostwin_from_range (container, iova, end); + +assert (hostwin);/* or region_add() would have failed */ + + pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; + try_unmap = !((iova & pgmask) || (int128_get64 (llsize) & pgmask)); + } + +if (try_unmap) { +if (int128_eq (llsize,int128_2_64 ())) { +/* The unmap ioctl doesn't accept a full 64-bit span. */ + llsize =int128_rshift (llsize,1 ); + ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); +if (ret) { +error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx") = %d (%m)" , + container, iova,int128_get64 (llsize), ret); + } + iova +=int128_get64 (llsize); + } + ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); +if (ret) { +error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " +"0x%" HWADDR_PRIx") = %d (%m)" , + container, iova,int128_get64 (llsize), ret); + } + } +} +static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener);hwaddr iova, end; -Int128 llend, llsize; -void *vaddr; +Int128 llend;int ret; VFIOHostDMAWindow *hostwin; Error *err = NULL;hw/vfio/common.c +2
-80@@ -814,39 +927,7 @@ static void vfio_listener_region_add(MemoryListener *listener, }/* Here we assume that memory_region_is_ram(section->mr)==true */ - - vaddr =memory_region_get_ram_ptr (section->mr) + - section->offset_within_region + - (iova - section->offset_within_address_space); - -trace_vfio_listener_region_add_ram (iova, end, vaddr); - - llsize =int128_sub (llend,int128_make64 (iova)); - -if (memory_region_is_ram_device (section->mr)) { -hwaddr pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; - -if ((iova & pgmask) || (int128_get64 (llsize) & pgmask)) { -trace_vfio_listener_region_add_no_dma_map ( -memory_region_name (section->mr), - section->offset_within_address_space, -int128_getlo (section->size), - pgmask +1 ); -return ; - } - } - - ret =vfio_dma_map (container, iova,int128_get64 (llsize), - vaddr, section->readonly); -if (ret) { -error_setg (&err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx", %p) = %d (%m)" , - container, iova,int128_get64 (llsize), vaddr, ret); -if (memory_region_is_ram_device (section->mr)) { -/* Allow unexpected mappings not to be fatal for RAM devices */ -error_report_err (err); -return ; - } +if (vfio_dma_map_ram_section (container, section, &err)) { goto fail; }@@ -880,10 +961,6 @@ static void vfio_listener_region_del(MemoryListener *listener, MemoryRegionSection *section) {VFIOContainer *container =container_of (listener, VFIOContainer, listener); -hwaddr iova, end; -Int128 llend, llsize; -int ret; -bool try_unmap = true;if (vfio_listener_skipped_section (section)) { trace_vfio_listener_region_del_skip(@@ -923,49 +1000,7 @@ static void vfio_listener_region_del(MemoryListener *listener,*/ } - iova =REAL_HOST_PAGE_ALIGN (section->offset_within_address_space); - llend =int128_make64 (section->offset_within_address_space); - llend =int128_add (llend, section->size); - llend =int128_and (llend,int128_exts64 (qemu_real_host_page_mask)); - -if (int128_ge (int128_make64 (iova), llend)) { -return ; - } - end =int128_get64 (int128_sub (llend,int128_one ())); - - llsize =int128_sub (llend,int128_make64 (iova)); - -trace_vfio_listener_region_del (iova, end); - -if (memory_region_is_ram_device (section->mr)) { -hwaddr pgmask; -VFIOHostDMAWindow *hostwin =hostwin_from_range (container, iova, end); - -assert (hostwin);/* or region_add() would have failed */ - - pgmask = (1ULL <<ctz64 (hostwin->iova_pgsizes)) -1 ; - try_unmap = !((iova & pgmask) || (int128_get64 (llsize) & pgmask)); - } - -if (try_unmap) { -if (int128_eq (llsize,int128_2_64 ())) { -/* The unmap ioctl doesn't accept a full 64-bit span. */ - llsize =int128_rshift (llsize,1 ); - ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); -if (ret) { -error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx") = %d (%m)" , - container, iova,int128_get64 (llsize), ret); - } - iova +=int128_get64 (llsize); - } - ret =vfio_dma_unmap (container, iova,int128_get64 (llsize),NULL ); -if (ret) { -error_report ("vfio_dma_unmap(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx") = %d (%m)" , - container, iova,int128_get64 (llsize), ret); - } - } +vfio_dma_unmap_ram_section (container, section);memory_region_unref (section->mr);hw/vfio/trace-events +2
-2@@ -99,10 +99,10 @@ vfio_iommu_map_notify(const char *op, uint64_t iova_start, uint64_t iova_end) "i vfio_listener_region_add_skip(uint64_t start, uint64_t end) "SKIPPING region_add 0x%"PRIx64" - 0x%"PRIx64 vfio_spapr_group_attach(int groupfd, int tablefd) "Attached groupfd %d to liobn fd %d" vfio_listener_region_add_iommu(uint64_t start, uint64_t end) "region_add [iommu] 0x%"PRIx64" - 0x%"PRIx64 -vfio_listener_region_add_ram(uint64_t iova_start, uint64_t iova_end, void *vaddr) "region_add [ram] 0x%"PRIx64" - 0x%"PRIx64" [%p]" +vfio_dma_map_ram(uint64_t iova_start, uint64_t iova_end, void *vaddr) "region_add [ram] 0x%"PRIx64" - 0x%"PRIx64" [%p]" vfio_listener_region_add_no_dma_map(const char *name, uint64_t iova, uint64_t size, uint64_t page_size) "Region \"%s\" 0x%"PRIx64" size=0x%"PRIx64" is not aligned to 0x%"PRIx64" and cannot be mapped for DMA" vfio_listener_region_del_skip(uint64_t start, uint64_t end) "SKIPPING region_del 0x%"PRIx64" - 0x%"PRIx64 -vfio_listener_region_del(uint64_t start, uint64_t end) "region_del 0x%"PRIx64" - 0x%"PRIx64 +vfio_dma_unmap_ram(uint64_t start, uint64_t end) "region_del 0x%"PRIx64" - 0x%"PRIx64 vfio_disconnect_container(int fd) "close container->fd=%d" vfio_connect_existing_container(int groupid, int container_fd) "group=%d existing container fd=%d" vfio_connect_new_container(int groupid, int container_fd) "group=%d new container fd=%d"
RFC v9 15/29 vfio: Set up nested stage mappings
In reply to Eric AugerIn nested mode, legacy vfio_iommu_map_notify cannot be used as there is no "caching" mode and we do not trap on map.
On Intel, vfio_iommu_map_notify was used to DMA map the RAM through the host single stage.
With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a prereg_listener to setup the stage 2 mapping.
The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidations
v6 -> v7:
- remove PASID based invalidation
v5 -> v6:
- add error_report_err()
- remove the abort in case of nested stage case
v4 -> v5:
- use VFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notification
v3 -> v4:
- use iommu_inv_pasid_info for ASID invalidation
v2 -> v3:
- use VFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leaf
v1 -> v2:
- adapt to uapi changes
- pass the asid
- pass IOMMU_NOTIFIER_S1_CFG when initializing the config notifier
--- hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 + 3 files changed, 157 insertions(+), 5 deletions(-)
hw/vfio/common.c +134 -5
@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,return true; } +/* Propagate a guest IOTLB invalidation to the host (nested mode) */ +static void vfio_iommu_unmap_notify (IOMMUNotifier *n,IOMMUTLBEntry *iotlb) +{ +VFIOGuestIOMMU *giommu =container_of (n, VFIOGuestIOMMU, n); +struct vfio_iommu_type1_cache_invalidate ustruct = {}; +VFIOContainer *container = giommu->container; +int ret; + +assert (iotlb->perm ==IOMMU_NONE ); + + ustruct.argsz =sizeof (ustruct); + ustruct.flags =0 ; + ustruct.info.argsz =sizeof (struct iommu_cache_invalidate_info ); + ustruct.info.version =IOMMU_CACHE_INVALIDATE_INFO_VERSION_1 ; + ustruct.info.cache =IOMMU_CACHE_INV_TYPE_IOTLB ; + +switch (iotlb->granularity) { +case IOMMU_INV_GRAN_DOMAIN : + ustruct.info.granularity =IOMMU_INV_GRANU_DOMAIN ; +break ; +case IOMMU_INV_GRAN_PASID : + { +struct iommu_inv_pasid_info *pasid_info; +int archid =-1 ; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity =IOMMU_INV_GRANU_PASID ; +if (iotlb->flags &IOMMU_INV_FLAGS_ARCHID ) { + pasid_info->flags |=IOMMU_INV_ADDR_FLAGS_ARCHID ; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; +trace_vfio_iommu_asid_inv_iotlb (archid); +break ; + } +case IOMMU_INV_GRAN_ADDR : + { +hwaddr start = iotlb->iova + giommu->iommu_offset; +struct iommu_inv_addr_info *addr_info; +size_t size = iotlb->addr_mask +1 ; +int archid =-1 ; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity =IOMMU_INV_GRANU_ADDR ; +if (iotlb->leaf) { + addr_info->flags |=IOMMU_INV_ADDR_FLAGS_LEAF ; + } +if (iotlb->flags &IOMMU_INV_FLAGS_ARCHID ) { + addr_info->flags |=IOMMU_INV_ADDR_FLAGS_ARCHID ; + archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules =1 ; +trace_vfio_iommu_addr_inv_iotlb (archid, start, size, +1 , iotlb->leaf); +break ; + } + } + + ret =ioctl (container->fd,VFIO_IOMMU_CACHE_INVALIDATE , &ustruct); +if (ret) { +error_report ("%p: failed to invalidate CACHE (%d)" , container, ret); + } +} +static void vfio_iommu_map_notify (IOMMUNotifier *n,IOMMUTLBEntry *iotlb) {VFIOGuestIOMMU *giommu =container_of (n, VFIOGuestIOMMU, n);
@@ -776,6 +843,35 @@ static void vfio_dma_unmap_ram_section(VFIOContainer *container, } } +static void vfio_prereg_listener_region_add (MemoryListener *listener, +MemoryRegionSection *section) +{ +VFIOContainer *container = +container_of (listener, VFIOContainer, prereg_listener); +Error *err =NULL ; + +if (!memory_region_is_ram (section->mr)) { +return ; + } + +vfio_dma_map_ram_section (container, section, &err); +if (err) { +error_report_err (err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, +MemoryRegionSection *section) +{ +VFIOContainer *container = +container_of (listener, VFIOContainer, prereg_listener); + +if (!memory_region_is_ram (section->mr)) { +return ; + } + +vfio_dma_unmap_ram_section (container, section); +} +static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {
@@ -879,9 +975,10 @@ static void vfio_listener_region_add(MemoryListener *listener,memory_region_ref (section->mr);if (memory_region_is_iommu (section->mr)) { +IOMMUNotify notify;VFIOGuestIOMMU *giommu;IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (section->mr); -int iommu_idx; +int iommu_idx, flags;trace_vfio_listener_region_add_iommu (iova, end);/*
@@ -900,8 +997,18 @@ static void vfio_listener_region_add(MemoryListener *listener, llend =int128_sub (llend,int128_one ()); iommu_idx =memory_region_iommu_attrs_to_index (iommu_mr,MEMTXATTRS_UNSPECIFIED ); -iommu_notifier_init (&giommu->n, vfio_iommu_map_notify, -IOMMU_NOTIFIER_IOTLB_EVENTS , + +if (container->iommu_type ==VFIO_TYPE1_NESTING_IOMMU ) { +/* IOTLB unmap notifier to propagate guest IOTLB invalidations */ + flags =IOMMU_NOTIFIER_UNMAP ; + notify = vfio_iommu_unmap_notify; + }else { +/* MAP/UNMAP IOTLB notifier */ + flags =IOMMU_NOTIFIER_IOTLB_EVENTS ; + notify = vfio_iommu_map_notify; + } + +iommu_notifier_init (&giommu->n, notify, flags, section->offset_within_region,int128_get64 (llend), iommu_idx);
@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; }QLIST_INSERT_HEAD (&container->giommu_list, giommu, giommu_next); -memory_region_iommu_replay (giommu->iommu, &giommu->n); +if (flags &IOMMU_NOTIFIER_MAP ) { +memory_region_iommu_replay (giommu->iommu, &giommu->n); + }return ; }
@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; +static void vfio_listener_release (VFIOContainer *container) {memory_listener_unregister (&container->listener); -if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { +if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type ==VFIO_TYPE1_NESTING_IOMMU ) {memory_listener_unregister (&container->prereg_listener); } }
@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,vfio_get_iommu_info_migration (container, info); }g_free (info); + +if (container->iommu_type ==VFIO_TYPE1_NESTING_IOMMU ) { + container->prereg_listener = vfio_memory_prereg_listener; +memory_listener_register (&container->prereg_listener, + &address_space_memory); +if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed " +"for container" ); + goto free_container_exit; + } + }break ; }case VFIO_SPAPR_TCE_v2_IOMMU:
hw/vfio/pci.c +21 -0
@@ -2773,6 +2773,25 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table (PCIBus *bus,int32_t devfn, +IOMMUConfig *config) +{ +PCIDevice *pdev = bus->devices[devfn]; +VFIOPCIDevice *vdev =DO_UPCAST (VFIOPCIDevice, pdev, pdev); +VFIOContainer *container = vdev->vbasedev.group->container; +struct vfio_iommu_type1_set_pasid_table info; + + info.argsz =sizeof (info); + info.flags =VFIO_PASID_TABLE_FLAG_SET ; +memcpy (&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + +return ioctl (container->fd,VFIO_IOMMU_SET_PASID_TABLE , &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; +static void vfio_realize (PCIDevice *pdev,Error **errp) {VFIOPCIDevice *vdev =VFIO_PCI (pdev);
@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)vfio_register_req_notifier (vdev);vfio_setup_resetfn_quirk (vdev); +pci_setup_pasid_ops (pdev, &vfio_pci_pasid_ops); +return ; out_deregister:
hw/vfio/trace-events +2 -0
@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name, int index, int nr_areas) "Devic vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end) "sparse entry %d [0x%lx - 0x%lx]" vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype) "%s index %d, %08x/%0x8" vfio_dma_unmap_overflow_workaround(void) "" +vfio_iommu_addr_inv_iotlb(int asid, uint64_t addr, uint64_t size, uint64_t nb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d" +vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d" # platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d"
Signature · 1 line
2.26.3
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Eric,
Quoted 110 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier--- hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 + 3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +49
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,return true; } +/* Propagate a guest IOTLB invalidation to the host (nested mode) */ +static void vfio_iommu_unmap_notify (IOMMUNotifier *n,IOMMUTLBEntry *iotlb) +{ +VFIOGuestIOMMU *giommu =container_of (n, VFIOGuestIOMMU, n); +struct vfio_iommu_type1_cache_invalidate ustruct = {}; +VFIOContainer *container = giommu->container; +int ret; + +assert (iotlb->perm ==IOMMU_NONE ); + + ustruct.argsz =sizeof (ustruct); + ustruct.flags =0 ; + ustruct.info.argsz =sizeof (struct iommu_cache_invalidate_info ); + ustruct.info.version =IOMMU_CACHE_INVALIDATE_INFO_VERSION_1 ; + ustruct.info.cache =IOMMU_CACHE_INV_TYPE_IOTLB ; + +switch (iotlb->granularity) { +case IOMMU_INV_GRAN_DOMAIN : + ustruct.info.granularity =IOMMU_INV_GRANU_DOMAIN ; +break ; +case IOMMU_INV_GRAN_PASID : + { +struct iommu_inv_pasid_info *pasid_info; +int archid =-1 ; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity =IOMMU_INV_GRANU_PASID ; +if (iotlb->flags &IOMMU_INV_FLAGS_ARCHID ) { + pasid_info->flags |=IOMMU_INV_ADDR_FLAGS_ARCHID ; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; +trace_vfio_iommu_asid_inv_iotlb (archid); +break ; + } +case IOMMU_INV_GRAN_ADDR : + { +hwaddr start = iotlb->iova + giommu->iommu_offset; +struct iommu_inv_addr_info *addr_info; +size_t size = iotlb->addr_mask +1 ; +int archid =-1 ; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity =IOMMU_INV_GRANU_ADDR ; +if (iotlb->leaf) { + addr_info->flags |=IOMMU_INV_ADDR_FLAGS_LEAF ; + } +if (iotlb->flags &IOMMU_INV_FLAGS_ARCHID ) { + addr_info->flags |=IOMMU_INV_ADDR_FLAGS_ARCHID ;
hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules =1 ; +trace_vfio_iommu_addr_inv_iotlb (archid, start, size, +1 , iotlb->leaf); +break ; + }
Should we pass a size to host kernel here, even if vSMMU doesn't support RIL or guest kernel doesn't use RIL?
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd with "range" (4K) which can not invalidate the TLB entry of 2M huge page. (pSMMU supports RIL)
Thanks,
Kunkun Jiang
Quoted 182 lines
hw/vfio/common.c +75
-5+ } + + ret =ioctl (container->fd,VFIO_IOMMU_CACHE_INVALIDATE , &ustruct); +if (ret) { +error_report ("%p: failed to invalidate CACHE (%d)" , container, ret); + } +} +static void vfio_iommu_map_notify (IOMMUNotifier *n,IOMMUTLBEntry *iotlb) {VFIOGuestIOMMU *giommu =container_of (n, VFIOGuestIOMMU, n);@@ -776,6 +843,35 @@ static void vfio_dma_unmap_ram_section(VFIOContainer *container, } } +static void vfio_prereg_listener_region_add (MemoryListener *listener, +MemoryRegionSection *section) +{ +VFIOContainer *container = +container_of (listener, VFIOContainer, prereg_listener); +Error *err =NULL ; + +if (!memory_region_is_ram (section->mr)) { +return ; + } + +vfio_dma_map_ram_section (container, section, &err); +if (err) { +error_report_err (err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, +MemoryRegionSection *section) +{ +VFIOContainer *container = +container_of (listener, VFIOContainer, prereg_listener); + +if (!memory_region_is_ram (section->mr)) { +return ; + } + +vfio_dma_unmap_ram_section (container, section); +} +static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {@@ -879,9 +975,10 @@ static void vfio_listener_region_add(MemoryListener *listener,memory_region_ref (section->mr);if (memory_region_is_iommu (section->mr)) { +IOMMUNotify notify;VFIOGuestIOMMU *giommu;IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (section->mr); -int iommu_idx; +int iommu_idx, flags;trace_vfio_listener_region_add_iommu (iova, end);/* @@ -900,8 +997,18 @@ static void vfio_listener_region_add(MemoryListener *listener, llend =int128_sub (llend,int128_one ()); iommu_idx =memory_region_iommu_attrs_to_index (iommu_mr,MEMTXATTRS_UNSPECIFIED ); -iommu_notifier_init (&giommu->n, vfio_iommu_map_notify, -IOMMU_NOTIFIER_IOTLB_EVENTS , + +if (container->iommu_type ==VFIO_TYPE1_NESTING_IOMMU ) { +/* IOTLB unmap notifier to propagate guest IOTLB invalidations */ + flags =IOMMU_NOTIFIER_UNMAP ; + notify = vfio_iommu_unmap_notify; + }else { +/* MAP/UNMAP IOTLB notifier */ + flags =IOMMU_NOTIFIER_IOTLB_EVENTS ; + notify = vfio_iommu_map_notify; + } + +iommu_notifier_init (&giommu->n, notify, flags, section->offset_within_region,int128_get64 (llend), iommu_idx);@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; }QLIST_INSERT_HEAD (&container->giommu_list, giommu, giommu_next); -memory_region_iommu_replay (giommu->iommu, &giommu->n); +if (flags &IOMMU_NOTIFIER_MAP ) { +memory_region_iommu_replay (giommu->iommu, &giommu->n); + }return ; }@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; +static void vfio_listener_release (VFIOContainer *container) {memory_listener_unregister (&container->listener); -if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { +if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type ==VFIO_TYPE1_NESTING_IOMMU ) {memory_listener_unregister (&container->prereg_listener); } }@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as,vfio_get_iommu_info_migration (container, info); }g_free (info); + +if (container->iommu_type ==VFIO_TYPE1_NESTING_IOMMU ) { + container->prereg_listener = vfio_memory_prereg_listener; +memory_listener_register (&container->prereg_listener, + &address_space_memory); +if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret =-1 ; +error_propagate_prepend (errp, container->error, +"RAM memory listener initialization failed " +"for container" ); + goto free_container_exit; + } + }break ; }case VFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +21
-0@@ -2773,6 +2773,25 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table (PCIBus *bus,int32_t devfn, +IOMMUConfig *config) +{ +PCIDevice *pdev = bus->devices[devfn]; +VFIOPCIDevice *vdev =DO_UPCAST (VFIOPCIDevice, pdev, pdev); +VFIOContainer *container = vdev->vbasedev.group->container; +struct vfio_iommu_type1_set_pasid_table info; + + info.argsz =sizeof (info); + info.flags =VFIO_PASID_TABLE_FLAG_SET ; +memcpy (&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + +return ioctl (container->fd,VFIO_IOMMU_SET_PASID_TABLE , &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; +static void vfio_realize (PCIDevice *pdev,Error **errp) {VFIOPCIDevice *vdev =VFIO_PCI (pdev);@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)vfio_register_req_notifier (vdev);vfio_setup_resetfn_quirk (vdev); +pci_setup_pasid_ops (pdev, &vfio_pci_pasid_ops); +return ; out_deregister:hw/vfio/trace-events +2
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name, int index, int nr_areas) "Devic vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end) "sparse entry %d [0x%lx - 0x%lx]" vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype) "%s index %d, %08x/%0x8" vfio_dma_unmap_overflow_workaround(void) "" +vfio_iommu_addr_inv_iotlb(int asid, uint64_t addr, uint64_t size, uint64_t nb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d" +vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d" # platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d"
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Kunkun,
Quoted 127 lines · Kunkun Jiang
Hi Eric,
Quoted 111 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier
---
hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++
hw/vfio/trace-events | 2 +3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; }+/* Propagate a guest IOTLB invalidation to the host (nested mode) */ +static void
vfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support RIL or guest kernel doesn't use RIL?
Quoted 6 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page. (pSMMU supports RIL)
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 201 lines
Thanks,
Kunkun JiangQuoted 195 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct); + if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} +static void
vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +28
-0@@ -776,6 +843,35 @@ static voidvfio_dma_unmap_ram_section (VFIOContainer *container, } } +static void vfio_prereg_listener_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} + static void vfio_listener_region_add(MemoryListener *listener,MemoryRegionSection *section) {
hw/vfio/common.c +2
-1@@ -879,9 +975,10 @@ static void vfio_listener_region_add(MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu; IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); - int iommu_idx; + int iommu_idx, flags; trace_vfio_listener_region_add_iommu(iova, end); /*@@ -900,8 +997,18 @@ static voidvfio_listener_region_add (MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
MEMTXATTRS_UNSPECIFIED);
- iommu_notifier_init(&giommu->n,vfio_iommu_map_notify, -IOMMU_NOTIFIER_IOTLB_EVENTS, +
+ if (container->iommu_type==VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLB invalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }
+
+iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);hw/vfio/common.c +3
-1@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +6
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); }}
hw/vfio/common.c +10
-0@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { + memory_listener_unregister(&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +18
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg, sizeof(config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp) {VFIOPCIDevice *vdev =
VFIO_PCI(pdev);hw/vfio/pci.c +1
-0@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return;
out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devicvfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end)"sparse entry %d [0x%lx - 0x%lx]"vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype)"%s index %d, %08x/%0x8"vfio_dma_unmap_overflow_workaround(void)"" +vfio_iommu_addr_inv_iotlb(int asid,uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d"
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Quoted 136 lines · Auger Eric
Hi Kunkun,
Quoted 127 lines · Kunkun Jiang
Hi Eric,
Quoted 111 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier--- hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 + 3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +48
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; } +/* Propagate a guest IOTLB invalidation to the host (nested mode) */ +static void vfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) +{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry · line 657
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support RIL or guest kernel doesn't use RIL?
Quoted 6 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page. (pSMMU supports RIL)
Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page. The iommu_iotlb_gather->pgsize will be 2M, if a page is 2M huge page. The gather->pgsize will be passed to __arm_smmu_tlb_inv_range as "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_range
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with "iova" (tg = 0).
Thanks,
Kunkun Jiang
Quoted 207 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 189 lines
Thanks,
Kunkun JiangQuoted 187 lines
hw/vfio/common.c +8
-0+ } + + ret = ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct); + if (ret) { + error_report("%p: failed to invalidate CACHE (%d)", container, ret); + } +} + static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry*iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +33
-3@@ -776,6 +843,35 @@ static voidvfio_dma_unmap_ram_section (VFIOContainer *container, } } +static void vfio_prereg_listener_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) {@@ -879,9 +975,10 @@ static void vfio_listener_region_add(MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu; IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); - int iommu_idx; + int iommu_idx, flags; trace_vfio_listener_region_add_iommu(iova, end); /*@@ -900,8 +997,18 @@ static void vfio_listener_region_add(MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, MEMTXATTRS_UNSPECIFIED); - iommu_notifier_init(&giommu->n, vfio_iommu_map_notify, - IOMMU_NOTIFIER_IOTLB_EVENTS, + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLBinvalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }hw/vfio/common.c +5
-1+ + iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +16
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); } }@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { + memory_listener_unregister(&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +19
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg, sizeof(config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev = VFIO_PCI(pdev);@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return; out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devic
vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned
long end) "sparse entry %d [0x%lx - 0x%lx]"
vfio_get_dev_region(const char *name, int index,uint32_ttype,uint32_tsubtype) "%s index %d, %08x/%0x8"
vfio_dma_unmap_overflow_workaround(void)""Quoted 10 lines
Quoted 8 lines
+vfio_iommu_addr_inv_iotlb(int asid,
uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Kunkun,
Quoted 156 lines · Kunkun Jiang
Quoted 141 lines · Auger Eric
Hi Kunkun,
Quoted 130 lines · Kunkun Jiang
Hi Eric,
Quoted 113 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier
---
hw/vfio/common.c | 139
+++++++++++++++++++++++++++++++++++++++++--
hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 +3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; }+/* Propagate a guest IOTLB invalidation to the host (nested mode) */
+static voidvfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 8 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page. The
iommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_range
Quoted 3 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with
"iova" (tg = 0).
OK I get it now. In that case I think I should do a TG=0 invalidation on host even if the host does support RIL. Does that sound correct?
The trouble is the uapi struct does not convey such info explicitly. Maybe I should use nb_granules = 0 to detect that case.
I think for this use case RIL should be supported by the guest. Thoughts?
Thanks
Eric
Quoted 216 lines
Thanks,
Kunkun JiangQuoted 211 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 193 lines
Thanks,
Kunkun JiangQuoted 191 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct); + if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} +static void
vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +0
-0@@ -776,6 +843,35 @@ static void vfio_dma_unmap_ram_section(VFIOContainer *container, } }+static void vfio_prereg_listener_region_add(MemoryListener *listener,
+ MemoryRegionSection *section)hw/vfio/common.c +27
-0+{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} +static void
vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section){hw/vfio/common.c +1
-0@@ -879,9 +975,10 @@ static voidvfio_listener_region_add (MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu;IOMMUMemoryRegion
*iommu_mr=IOMMU_MEMORY_REGION(section->mr);
- intiommu_idx;
+ intiommu_idx, flags;trace_vfio_listener_region_add_iommu(iova, end); /*hw/vfio/common.c +0
-0@@ -900,8 +997,18 @@ static voidvfio_listener_region_add (MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
MEMTXATTRS_UNSPECIFIED);
- iommu_notifier_init(&giommu->n,vfio_iommu_map_notify, -IOMMU_NOTIFIER_IOTLB_EVENTS, +
+ if (container->iommu_type==VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLB invalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }
+
+iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);hw/vfio/common.c +3
-1@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +6
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); }}
hw/vfio/common.c +10
-0@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +18
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp){
VFIOPCIDevice *vdev =VFIO_PCI(pdev);hw/vfio/pci.c +1
-0@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return;
out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devicvfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end)"sparse entry %d [0x%lx - 0x%lx]"vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype)"%s index %d, %08x/%0x8"vfio_dma_unmap_overflow_workaround(void)""Quoted 10 lines
Quoted 8 lines
+vfio_iommu_addr_inv_iotlb(int asid,
uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Eric,
Quoted 159 lines · Auger Eric
Hi Kunkun,
Quoted 156 lines · Kunkun Jiang
Quoted 141 lines · Auger Eric
Hi Kunkun,
Quoted 130 lines · Kunkun Jiang
Hi Eric,
Quoted 113 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier--- hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 + 3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; } +/* Propagate a guest IOTLB invalidation to the host (nestedmode) */
+static voidvfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 8 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page. The
iommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_range
Quoted 5 lines
Quoted 3 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with
"iova" (tg = 0).OK I get it now. In that case I think I should do a TG=0 invalidation on host even if the host does support RIL. Does that sound correct?
Yeah, that's what I thought.
Quoted 2 lines
The trouble is the uapi struct does not convey such info explicitly. Maybe I should use
nb_granules= 0 to detect that case.
It is troublesome to correct this issue. Using nb_granules = 0 may be a good choice.
Quoted 1 line
I think for this use case RIL should be supported by the guest. Thoughts?
Yes. If guest supports RIL, the scenario mentioned above does not exist.
Thanks,
Kunkun Jiang
Quoted 218 lines
Thanks
Eric
Quoted 213 lines
Thanks,
Kunkun JiangQuoted 210 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 193 lines
Thanks,
Kunkun JiangQuoted 191 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct); + if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} + static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry*iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +33
-3@@ -776,6 +843,35 @@ static voidvfio_dma_unmap_ram_section (VFIOContainer *container, } } +static void vfio_prereg_listener_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) {@@ -879,9 +975,10 @@ static void vfio_listener_region_add(MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu; IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); - int iommu_idx; + int iommu_idx, flags; trace_vfio_listener_region_add_iommu(iova, end); /*@@ -900,8 +997,18 @@ static void vfio_listener_region_add(MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, MEMTXATTRS_UNSPECIFIED); - iommu_notifier_init(&giommu->n, vfio_iommu_map_notify, - IOMMU_NOTIFIER_IOTLB_EVENTS, + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLBinvalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }hw/vfio/common.c +5
-1+ + iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +16
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); } }@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +19
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev = VFIO_PCI(pdev);@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return; out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devic
vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned
long end) "sparse entry %d [0x%lx - 0x%lx]"
vfio_get_dev_region(const char *name, int index,uint32_ttype,uint32_tsubtype) "%s index %d, %08x/%0x8"
vfio_dma_unmap_overflow_workaround(void)""Quoted 9 lines
Quoted 8 lines
+vfio_iommu_addr_inv_iotlb(int asid,
uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
.
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Kunkun,
Quoted 170 lines · Kunkun Jiang
Hi Eric,
Quoted 162 lines · Auger Eric
Hi Kunkun,
Quoted 159 lines · Kunkun Jiang
Quoted 142 lines · Auger Eric
Hi Kunkun,
Quoted 130 lines · Kunkun Jiang
Hi Eric,
Quoted 113 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier
---
hw/vfio/common.c | 139
+++++++++++++++++++++++++++++++++++++++++--
hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 +3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; }+/* Propagate a guest IOTLB invalidation to the host (nested mode) */
+static voidvfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 9 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed
the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page.
Theiommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_rangeQuoted 5 lines
Quoted 3 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with
"iova" (tg = 0).OK I get it now. In that case I think I should do a TG=0 invalidation on host even if the host does support RIL. Does that sound correct?
Quoted 7 lines
Yeah, that's what I thought.
Quoted 2 lines
The trouble is the uapi struct does not convey such info explicitly. Maybe I should use
nb_granules= 0 to detect that case.It is troublesome to correct this issue. Using
nb_granules= 0 may be a good choice.Quoted 1 line
I think for this use case RIL should be supported by the guest. Thoughts?
Yes. If guest supports RIL, the scenario mentioned above does not exist.
After further study I really wonder if it is worth supporting the case where the guest does not use range inval. Passing a nb_granules = 0 (~ size) would be OK to perform the TG=0 range invalidation on the host. however host arm_smmu_tlb_inv_range_domain() then calls arm_smmu_atc_inv_domain(smmu_domain, 0, iova, size) which needs a size. We would need to trap guest CMD_ATC_INV and use different code paths on host smmu driver to cascade the various cache invalidations. At the moment, without maintainer giodance, I am a bit reluctant to add this extra complexity.
So I would be inclined to fail in QEMU if we detect TG=0 is being used by the guest. Recent guest kernels would be a prerequisite for this use case. What do you think?
Thanks
Eric
Quoted 231 lines
Thanks,
Kunkun JiangQuoted 226 lines
Thanks
Eric
Quoted 221 lines
Thanks,
Kunkun JiangQuoted 218 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 201 lines
Thanks,
Kunkun JiangQuoted 199 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct);
+ if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} +static void
vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +0
-0@@ -776,6 +843,35 @@ static void vfio_dma_unmap_ram_section(VFIOContainer *container, } }+static void vfio_prereg_listener_region_add(MemoryListener *listener,
+ MemoryRegionSection *section)hw/vfio/common.c +15
-0+{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del(MemoryListener*listener,
+ MemoryRegionSection *section)hw/vfio/common.c +11
-0+{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} +static void
vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section)
{hw/vfio/common.c +1
-0@@ -879,9 +975,10 @@ static voidvfio_listener_region_add (MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu;IOMMUMemoryRegion
*iommu_mr=IOMMU_MEMORY_REGION(section->mr);
- intiommu_idx;
+ intiommu_idx, flags;trace_vfio_listener_region_add_iommu(iova, end); /*hw/vfio/common.c +0
-0@@ -900,8 +997,18 @@ static voidvfio_listener_region_add (MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
MEMTXATTRS_UNSPECIFIED);
- iommu_notifier_init(&giommu->n,vfio_iommu_map_notify, -IOMMU_NOTIFIER_IOTLB_EVENTS, +
+ if (container->iommu_type==VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLB invalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }
+
+iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);hw/vfio/common.c +3
-1@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +6
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); }}
hw/vfio/common.c +10
-0@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +0
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; }+static int vfio_iommu_set_pasid_table(PCIBus *bus,
int32_tdevfn,
+ IOMMUConfig *config)hw/vfio/pci.c +9
-0+{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,
sizeof(config->pasid_cfg));hw/vfio/pci.c +8
-0+ + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; +static void
vfio_realize(PCIDevice *pdev, Error **errp){
VFIOPCIDevice *vdev =VFIO_PCI(pdev);hw/vfio/pci.c +1
-0@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return;
out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devic vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned
long end) "sparse entry %d [0x%lx - 0x%lx]"vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype)"%s index %d, %08x/%0x8"vfio_dma_unmap_overflow_workaround(void)"" +vfio_iommu_addr_inv_iotlb(int asid,uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d,Quoted 9 lines
Quoted 8 lines
addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
.
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Eric,
Quoted 190 lines · Auger Eric
Hi Kunkun,
Quoted 170 lines · Kunkun Jiang
Hi Eric,
Quoted 162 lines · Auger Eric
Hi Kunkun,
Quoted 159 lines · Kunkun Jiang
Quoted 142 lines · Auger Eric
Hi Kunkun,
Quoted 130 lines · Kunkun Jiang
Hi Eric,
Quoted 113 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier--- hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 + 3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; } +/* Propagate a guest IOTLB invalidation to the host (nestedmode) */
+static voidvfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 9 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed
the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page.
Theiommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_rangeQuoted 5 lines
Quoted 3 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with
"iova" (tg = 0).OK I get it now. In that case I think I should do a TG=0 invalidation on host even if the host does support RIL. Does that sound correct?
Quoted 7 lines
Yeah, that's what I thought.
Quoted 2 lines
The trouble is the uapi struct does not convey such info explicitly. Maybe I should use
nb_granules= 0 to detect that case.It is troublesome to correct this issue. Using
nb_granules= 0 may be a good choice.Quoted 1 line
I think for this use case RIL should be supported by the guest. Thoughts?
Yes. If guest supports RIL, the scenario mentioned above does not exist.
After further study I really wonder if it is worth supporting the case where the guest does not use range inval. Passing a
nb_granules= 0 (~ size) would be OK to perform the TG=0 range invalidation on the host. however hostarm_smmu_tlb_inv_range_domain()then callsarm_smmu_atc_inv_domain(smmu_domain, 0, iova, size)which needs a size. We would need to trap guestCMD_ATC_INVand use different code paths on host smmu driver to cascade the various cache invalidations. At the moment, without maintainer giodance, I am a bit reluctant to add this extra complexity.
Quoted 3 lines
So I would be inclined to fail in QEMU if we detect TG=0 is being used by the guest. Recent guest kernels would be a prerequisite for this use case. What do you think?
Sorry for late reply.
How do I determine whether the guest kernel is a recent guest kernel? If I use a modified kernel or an old kernel, this method will cause problems when the vm runs business.
I didn't think of the problem of CMD_ATC_INV. In my opinion, it is the best choice to add processing of CMD_ATC_INV. And some function of smmu driver will issues a separate CMD_ATC_INV, for example arm_smmu_enable_ats(). Is it possible that Qemu needs to support the processing of CMD_ATC_INV in the future?
I also thought of another not-so-good idea. We can use the granule_size to give CMD_ATC_INV a corresponding maximum page size. This will result in poor performance.
For example:
granule_size maximum_page_size
4K 1G
Thanks,
Kunkun Jiang
Quoted 230 lines
Thanks
Eric
Quoted 226 lines
Thanks,
Kunkun JiangQuoted 223 lines
Thanks
Eric
Quoted 219 lines
Thanks,
Kunkun JiangQuoted 217 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 201 lines
Thanks,
Kunkun JiangQuoted 199 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct);
+ if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} + static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry*iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +16
-0@@ -776,6 +843,35 @@ static voidvfio_dma_unmap_ram_section (VFIOContainer *container, } } +static void vfio_prereg_listener_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del(MemoryListener*listener,
+ MemoryRegionSection *section)hw/vfio/common.c +11
-0+{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection*section)
{hw/vfio/common.c +5
-3@@ -879,9 +975,10 @@ static void vfio_listener_region_add(MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu; IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); - int iommu_idx; + int iommu_idx, flags; trace_vfio_listener_region_add_iommu(iova, end); /*@@ -900,8 +997,18 @@ static void vfio_listener_region_add(MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, MEMTXATTRS_UNSPECIFIED); - iommu_notifier_init(&giommu->n, vfio_iommu_map_notify, - IOMMU_NOTIFIER_IOTLB_EVENTS, + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLBinvalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }hw/vfio/common.c +5
-1+ + iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +16
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); } }@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +19
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev = VFIO_PCI(pdev);@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return; out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devic
vfio_region_sparse_mmap_entry(int i, unsigned long start,
unsigned
long end) "sparse entry %d [0x%lx - 0x%lx]"
vfio_get_dev_region(const char *name, int index,uint32_ttype,uint32_tsubtype) "%s index %d, %08x/%0x8"
vfio_dma_unmap_overflow_workaround(void)""
+vfio_iommu_addr_inv_iotlb(int asid,uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d,Quoted 8 lines
Quoted 8 lines
addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
.
.
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Kunkun,
Quoted 203 lines · Kunkun Jiang
Hi Eric,
Quoted 197 lines · Auger Eric
Hi Kunkun,
Quoted 176 lines · Kunkun Jiang
Hi Eric,
Quoted 167 lines · Auger Eric
Hi Kunkun,
Quoted 164 lines · Kunkun Jiang
Quoted 144 lines · Auger Eric
Hi Kunkun,
Quoted 132 lines · Kunkun Jiang
Hi Eric,
Quoted 115 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier
---
hw/vfio/common.c | 139
+++++++++++++++++++++++++++++++++++++++++--
hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 +3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; }+/* Propagate a guest IOTLB invalidation to the host (nested mode) */
+static void vfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry
*iotlb)hw/vfio/common.c +11
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct
iommu_cache_invalidate_info);
+ ustruct.info.version =IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache =IOMMU_CACHE_INV_TYPE_IOTLB;hw/vfio/common.c +34
-0+ + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 9 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed
the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 5 lines
In that case the guest will loop over all 4K images belonging to the 2M
huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, henceThe guest will not loop over all 4K images belonging to the 2M huge page.
Theiommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_rangeQuoted 6 lines
Quoted 4 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only
with
"iova" (tg = 0).OK I get it now. In that case I think I should do a TG=0 invalidation on host even if the host does support RIL. Does that sound correct?
Quoted 8 lines
Yeah, that's what I thought.
Quoted 2 lines
The trouble is the uapi struct does not convey such info explicitly. Maybe I should use
nb_granules= 0 to detect that case.It is troublesome to correct this issue. Using
nb_granules= 0 may be a good choice.Quoted 2 lines
I think for this use case RIL should be supported by the guest. Thoughts?
Yes. If guest supports RIL, the scenario mentioned above does not exist.
After further study I really wonder if it is worth supporting the case where the guest does not use range inval. Passing a
nb_granules= 0 (~ size) would be OK to perform the TG=0 range invalidation on the host. however hostarm_smmu_tlb_inv_range_domain()then callsarm_smmu_atc_inv_domain(smmu_domain, 0, iova, size)which needs a size. We would need to trap guestCMD_ATC_INVand use different code paths on host smmu driver to cascade the various cache invalidations. At the moment, without maintainer giodance, I am a bit reluctant to add this extra complexity.Quoted 3 lines
So I would be inclined to fail in QEMU if we detect TG=0 is being used by the guest. Recent guest kernels would be a prerequisite for this use case. What do you think?
Quoted 6 lines
Sorry for late reply.
How do I determine whether the guest kernel is a recent guest kernel? If I use a modified kernel or an old kernel, this method will cause problems
when the vm runs business.
Yes I aknowledge this detection would happen very late when trapping guest invals.
Quoted 5 lines
I didn't think of the problem of
CMD_ATC_INV. In my opinion, it is the best choice to add processing ofCMD_ATC_INV. And some function of smmu driver will issues a separateCMD_ATC_INV, for examplearm_smmu_enable_ats().
Yes this would mean that in arm_smmu_cache_invalidate() I should not use arm_smmu_tlb_inv_range_domain() and split the actual inv_range from the atc_inv_domain. Or even at the moment, since the vIOMMU does not support ATS I can do without.
Is it possible that Qemu needs to support the
Quoted 1 line
processing of
CMD_ATC_INVin the future?
well that's technically feasible but we would need to make sure the pIOMMU supports it.
Thanks
Eric
Quoted 251 lines
I also thought of another not-so-good idea. We can use the
granule_sizeto giveCMD_ATC_INVa corresponding maximum page size. This will result in poor performance.
For example:
granule_sizemaximum_page_size
4K 1GThanks,
Kunkun JiangQuoted 238 lines
Thanks
Eric
Quoted 234 lines
Thanks,
Kunkun JiangQuoted 231 lines
Thanks
Eric
Quoted 227 lines
Thanks,
Kunkun JiangQuoted 225 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 209 lines
Thanks,
Kunkun JiangQuoted 207 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct);
+ if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} +static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry
*iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +0
-0@@ -776,6 +843,35 @@ static void vfio_dma_unmap_ram_section(VFIOContainer *container, } }+static void vfio_prereg_listener_region_add(MemoryListener *listener,
+ MemoryRegionSection *section)hw/vfio/common.c +15
-0+{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del(MemoryListener*listener,
+ MemoryRegionSection *section)hw/vfio/common.c +11
-0+{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} +static void
vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section)
{hw/vfio/common.c +1
-0@@ -879,9 +975,10 @@ static voidvfio_listener_region_add (MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu;IOMMUMemoryRegion
*iommu_mr=IOMMU_MEMORY_REGION(section->mr);
- intiommu_idx;
+ intiommu_idx, flags;trace_vfio_listener_region_add_iommu(iova, end); /*hw/vfio/common.c +0
-0@@ -900,8 +997,18 @@ static voidvfio_listener_region_add (MemoryListener *listener, llend = int128_sub(llend, int128_one());
iommu_idx= memory_region_iommu_attrs_to_index(iommu_mr,MEMTXATTRS_UNSPECIFIED);
- iommu_notifier_init(&giommu->n,vfio_iommu_map_notify, -IOMMU_NOTIFIER_IOTLB_EVENTS, +
+ if (container->iommu_type==VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLB invalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }
+
+iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);hw/vfio/common.c +3
-1@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +6
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) {
memory_listener_unregister(&container->prereg_listener); }
}hw/vfio/common.c +10
-0@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +0
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; }+static int vfio_iommu_set_pasid_table(PCIBus *bus,
int32_tdevfn,
+ IOMMUConfig *config)hw/vfio/pci.c +9
-0+{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,
sizeof(config->pasid_cfg));
+
+ returnioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info);hw/vfio/pci.c +6
-0+} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; +static void
vfio_realize(PCIDevice *pdev, Error **errp){
VFIOPCIDevice *vdev =VFIO_PCI(pdev);hw/vfio/pci.c +0
-0@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev,Error
**errp)
vfio_register_req_notifier(vdev);vfio_setup_resetfn_quirk(vdev);
+pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); +
return;
out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devic vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned
long end) "sparse entry %d [0x%lx - 0x%lx]" vfio_get_dev_region(const char *name, int index,uint32_ttype,uint32_tsubtype) "%s index %d, %08x/%0x8"vfio_dma_unmap_overflow_workaround(void)"" +vfio_iommu_addr_inv_iotlb(int asid,uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d,Quoted 8 lines
Quoted 8 lines
addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
.
.
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
In reply to Kunkun JiangHi Kunkun,
Quoted 156 lines · Kunkun Jiang
Quoted 141 lines · Auger Eric
Hi Kunkun,
Quoted 130 lines · Kunkun Jiang
Hi Eric,
Quoted 113 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier
---
hw/vfio/common.c | 139
+++++++++++++++++++++++++++++++++++++++++--
hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 +3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; }+/* Propagate a guest IOTLB invalidation to the host (nested mode) */
+static voidvfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 8 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page. The
iommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_range
Quoted 3 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with
"iova" (tg = 0).
I am busy fixing this case. Could you share your guest use case. It is DPDK? In the positive I would be interested in getting your DPDK setup/commands.
Thank you in advance
Eric
Quoted 216 lines
Thanks,
Kunkun JiangQuoted 211 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 193 lines
Thanks,
Kunkun JiangQuoted 191 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct); + if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} +static void
vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +0
-0@@ -776,6 +843,35 @@ static void vfio_dma_unmap_ram_section(VFIOContainer *container, } }+static void vfio_prereg_listener_region_add(MemoryListener *listener,
+ MemoryRegionSection *section)hw/vfio/common.c +27
-0+{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} +static void
vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section){hw/vfio/common.c +1
-0@@ -879,9 +975,10 @@ static voidvfio_listener_region_add (MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu;IOMMUMemoryRegion
*iommu_mr=IOMMU_MEMORY_REGION(section->mr);
- intiommu_idx;
+ intiommu_idx, flags;trace_vfio_listener_region_add_iommu(iova, end); /*hw/vfio/common.c +0
-0@@ -900,8 +997,18 @@ static voidvfio_listener_region_add (MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
MEMTXATTRS_UNSPECIFIED);
- iommu_notifier_init(&giommu->n,vfio_iommu_map_notify, -IOMMU_NOTIFIER_IOTLB_EVENTS, +
+ if (container->iommu_type==VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLB invalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }
+
+iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);hw/vfio/common.c +3
-1@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +6
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); }}
hw/vfio/common.c +10
-0@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +18
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp){
VFIOPCIDevice *vdev =VFIO_PCI(pdev);hw/vfio/pci.c +1
-0@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return;
out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devicvfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end)"sparse entry %d [0x%lx - 0x%lx]"vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype)"%s index %d, %08x/%0x8"vfio_dma_unmap_overflow_workaround(void)""Quoted 10 lines
Quoted 8 lines
+vfio_iommu_addr_inv_iotlb(int asid,
uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Eric,
Quoted 159 lines · Auger Eric
Hi Kunkun,
Quoted 156 lines · Kunkun Jiang
Quoted 141 lines · Auger Eric
Hi Kunkun,
Quoted 130 lines · Kunkun Jiang
Hi Eric,
Quoted 113 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier--- hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 + 3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; } +/* Propagate a guest IOTLB invalidation to the host (nestedmode) */
+static voidvfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 8 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page. The
iommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_range
Quoted 6 lines
Quoted 3 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with
"iova" (tg = 0).I am busy fixing this case. Could you share your guest use case. It is DPDK? In the positive I would be interested in getting your DPDK setup/commands.
I use UADK[1] with the accelerator Hisilicon SEC.
./test_hisi_sec --perf --sync --pktlen 1024 --block 1024 --blknum 1 --times 1 --multi 1 --ctxnum 1
By the way, it needs to modify some code of UADK in vSVA scenario to map 2M huge pages. The modified UADK I used was provided by @Xingang Wang
[1] User Space Accelerator Development Kit
https://github.com/Linaro/uadk
Thanks,
Kunkun Jiang
Quoted 217 lines
Thank you in advance
Eric
Quoted 213 lines
Thanks,
Kunkun JiangQuoted 210 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 193 lines
Thanks,
Kunkun JiangQuoted 191 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct); + if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} + static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry*iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +33
-3@@ -776,6 +843,35 @@ static voidvfio_dma_unmap_ram_section (VFIOContainer *container, } } +static void vfio_prereg_listener_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) {@@ -879,9 +975,10 @@ static void vfio_listener_region_add(MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu; IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); - int iommu_idx; + int iommu_idx, flags; trace_vfio_listener_region_add_iommu(iova, end); /*@@ -900,8 +997,18 @@ static void vfio_listener_region_add(MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, MEMTXATTRS_UNSPECIFIED); - iommu_notifier_init(&giommu->n, vfio_iommu_map_notify, - IOMMU_NOTIFIER_IOTLB_EVENTS, + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLBinvalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }hw/vfio/common.c +5
-1+ + iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +16
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); } }@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +19
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev = VFIO_PCI(pdev);@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return; out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devic
vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned
long end) "sparse entry %d [0x%lx - 0x%lx]"
vfio_get_dev_region(const char *name, int index,uint32_ttype,uint32_tsubtype) "%s index %d, %08x/%0x8"
vfio_dma_unmap_overflow_workaround(void)""Quoted 9 lines
Quoted 8 lines
+vfio_iommu_addr_inv_iotlb(int asid,
uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
.
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
In reply to Kunkun JiangHi Kunkun,
Quoted 156 lines · Kunkun Jiang
Quoted 141 lines · Auger Eric
Hi Kunkun,
Quoted 130 lines · Kunkun Jiang
Hi Eric,
Quoted 113 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier
---
hw/vfio/common.c | 139
+++++++++++++++++++++++++++++++++++++++++--
hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 +3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; }+/* Propagate a guest IOTLB invalidation to the host (nested mode) */
+static voidvfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 8 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page. The
iommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_range
Quoted 3 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with
"iova" (tg = 0).
I am currently respinning the SMMU part (the rest depends on /dev/iommu). While thinking more about this issue you reported a long time ago (sorry), I think a guest not using RIL is problematic wrt vSMMU integration. Your case would not work with vhost either because when you notify vhost you need to pass an invalidation range. In this specific case there is none passed by the guest as the INVAL CMD just contains an iova and that's it. Hope I do not miss anything. And I cannot guess the range size. I only have the granule size from the CD and the IOVA. So how can I properly invalidate in the vhost case beside upgrading the API and pass the info telling the addr range is not set and invalidate the whole vhost cache in that case.
So I would be inclined to support a RIL only guest for nested and globally with vhost. I can't ignore your argument that the detection of non interoperability comes late, with the first inval with TG=0. But I think the SMMU spec without RIL just is *not* virtualization compatible and this was first reported in Aug 2017
(https://lkml.org/lkml/2017/8/11/428), yeah so much time spent on this without outcome :-(. Given the nested feature timeframe, now with this /dev/iommu redesign, can't you consider that using a guest kernel >= 5.7, featuring RIL now is acceptable.
Thanks
Eric
Quoted 217 lines
Thanks,
Kunkun JiangQuoted 212 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 194 lines
Thanks,
Kunkun JiangQuoted 192 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct);
+ if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} +static void
vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +0
-0@@ -776,6 +843,35 @@ static void vfio_dma_unmap_ram_section(VFIOContainer *container, } }+static void vfio_prereg_listener_region_add(MemoryListener *listener,
+ MemoryRegionSection *section)hw/vfio/common.c +27
-0+{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} +static void
vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section){hw/vfio/common.c +1
-0@@ -879,9 +975,10 @@ static voidvfio_listener_region_add (MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu;IOMMUMemoryRegion
*iommu_mr=IOMMU_MEMORY_REGION(section->mr);
- intiommu_idx;
+ intiommu_idx, flags;trace_vfio_listener_region_add_iommu(iova, end); /*hw/vfio/common.c +0
-0@@ -900,8 +997,18 @@ static voidvfio_listener_region_add (MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
MEMTXATTRS_UNSPECIFIED);
- iommu_notifier_init(&giommu->n,vfio_iommu_map_notify, -IOMMU_NOTIFIER_IOTLB_EVENTS, +
+ if (container->iommu_type==VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLB invalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }
+
+iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);hw/vfio/common.c +3
-1@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +6
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); }}
hw/vfio/common.c +10
-0@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +18
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp){
VFIOPCIDevice *vdev =VFIO_PCI(pdev);hw/vfio/pci.c +1
-0@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return;
out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devicvfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end)"sparse entry %d [0x%lx - 0x%lx]"vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype)"%s index %d, %08x/%0x8"vfio_dma_unmap_overflow_workaround(void)""Quoted 10 lines
Quoted 8 lines
+vfio_iommu_addr_inv_iotlb(int asid,
uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
Re: [RFC v9 15/29] vfio: Set up nested stage mappings
Hi Eric,
Quoted 174 lines · Eric Auger
Hi Kunkun,
Quoted 156 lines · Kunkun Jiang
Quoted 141 lines · Auger Eric
Hi Kunkun,
Quoted 130 lines · Kunkun Jiang
Hi Eric,
Quoted 113 lines · Eric Auger
In nested mode, legacy
vfio_iommu_map_notifycannot be used as there is no "caching" mode and we do not trap on map.On Intel,
vfio_iommu_map_notifywas used to DMA map the RAM through the host single stage.With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a
prereg_listenerto setup the stage 2 mapping.The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidationsv6 -> v7:
- remove PASID based invalidationv5 -> v6:
- adderror_report_err()
- remove the abort in case of nested stage casev4 -> v5:
- useVFIO_IOMMU_SET_PASID_TABLE
- use PCIPASIDOps for config notificationv3 -> v4:
- useiommu_inv_pasid_infofor ASID invalidationv2 -> v3:
- useVFIO_IOMMU_ATTACH_PASID_TABLE
- new user API
- handle leafv1 -> v2:
- adapt to uapi changes
- pass the asid
- passIOMMU_NOTIFIER_S1_CFGwhen initializing the config notifier--- hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 + 3 files changed, 157 insertions(+), 5 deletions(-)hw/vfio/common.c +0
-0@@ -595,6 +595,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, return true; } +/* Propagate a guest IOTLB invalidation to the host (nestedmode) */
+static voidvfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)hw/vfio/common.c +47
-0+{ + VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct = {}; + VFIOContainer *container = giommu->container; + int ret; + + assert(iotlb->perm == IOMMU_NONE); + + ustruct.argsz = sizeof(ustruct); + ustruct.flags = 0; + ustruct.info.argsz = sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version = IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache = IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity = IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid = -1; + + pasid_info = &ustruct.info.granu.pasid_info; + ustruct.info.granularity = IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID; + archid = iotlb->arch_id; + } + pasid_info->archid = archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start = iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size = iotlb->addr_mask + 1; + int archid = -1; + + addr_info = &ustruct.info.granu.addr_info; + ustruct.info.granularity = IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |= IOMMU_INV_ADDR_FLAGS_ARCHID;hw/vfio/common.c static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, · line 656
Quoted 10 lines
hw/vfio/common.c +10
-0+ archid = iotlb->arch_id; + } + addr_info->archid = archid; + addr_info->addr = start; + addr_info->granule_size = size; + addr_info->nb_granules = 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + }Should we pass a size to host kernel here, even if vSMMU doesn't support
RIL or guest kernel doesn't use RIL?Quoted 8 lines
It will cause TLBI issue in this scenario: Guest kernel issues a TLBI cmd
without "range" (tg = 0) to invalidate a 2M huge page. Then qemu passed the iova and size (4K) to host kernel. Finally, host kernel issues a TLBI cmd
with "range" (4K) which can not invalidate the TLB entry of 2M huge page.
(pSMMU supports RIL)Quoted 3 lines
In that case the guest will loop over all 4K images belonging to the 2M huge page and invalidate each of them. This should turn into qemu notifications for each 4kB page, no? This is totally inefficient, hence
The guest will not loop over all 4K images belonging to the 2M huge page. The
iommu_iotlb_gather->pgsizewill be 2M, if a page is 2M huge page. Thegather->pgsizewill be passed to__arm_smmu_tlb_inv_rangeas "granule":
iommu_iotlb_gather_add_page
iommu_iotlb_sync
domain->ops->iotlb_sync
arm_smmu_iotlb_sync
arm_smmu_tlb_inv_range_domain
__arm_smmu_tlb_inv_rangeQuoted 3 lines
In the above mentioned scenario, guest kernel will issue a TLBI cmd only with
"iova" (tg = 0).I am currently respinning the SMMU part (the rest depends on
/dev/iommu). While thinking more about this issue you reported a long time ago (sorry), I think a guest not using RIL is problematic wrt vSMMU integration. Your case would not work with vhost either because when you notify vhost you need to pass an invalidation range. In this specific case there is none passed by the guest as the INVAL CMD just contains an iova and that's it. Hope I do not miss anything. And I cannot guess the range size. I only have the granule size from the CD and the IOVA. So how can I properly invalidate in the vhost case beside upgrading the API and pass the info telling the addr range is not set and invalidate the whole vhost cache in that case.
Quoted 9 lines
So I would be inclined to support a RIL only guest for nested and globally with vhost. I can't ignore your argument that the detection of non interoperability comes late, with the first inval with TG=0. But I think the SMMU spec without RIL just is *not* virtualization compatible and this was first reported in Aug 2017
(https://lkml.org/lkml/2017/8/11/428), yeah so much time spent on this without outcome :-(. Given the nested feature timeframe, now with this/dev/iommuredesign, can't you consider that using a guest kernel >= 5.7, featuring RIL now is acceptable.
Ok, I see. Thanks for your reply.
Looking forward to your new patch set.
Thanks,
Kunkun Jiang
Quoted 218 lines
Thanks
Eric
Quoted 214 lines
Thanks,
Kunkun JiangQuoted 211 lines
the support of RIL on guest side and QEMU device.
What do I miss?
Thanks
Eric
Quoted 194 lines
Thanks,
Kunkun JiangQuoted 192 lines
+ }
+
+ ret =ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct);
+ if (ret) {
+error_report("%p: failed to invalidate CACHE (%d)", container, ret);
+ }hw/vfio/common.c +2
-0+} + static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry*iotlb)
{
VFIOGuestIOMMU *giommu =container_of(n, VFIOGuestIOMMU, n);hw/vfio/common.c +33
-3@@ -776,6 +843,35 @@ static voidvfio_dma_unmap_ram_section (VFIOContainer *container, } } +static void vfio_prereg_listener_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + Error *err = NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del (MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container = + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) {@@ -879,9 +975,10 @@ static void vfio_listener_region_add(MemoryListener *listener, memory_region_ref(section->mr); if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu; IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr); - int iommu_idx; + int iommu_idx, flags; trace_vfio_listener_region_add_iommu(iova, end); /*@@ -900,8 +997,18 @@ static void vfio_listener_region_add(MemoryListener *listener, llend = int128_sub(llend, int128_one()); iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, MEMTXATTRS_UNSPECIFIED); - iommu_notifier_init(&giommu->n, vfio_iommu_map_notify, - IOMMU_NOTIFIER_IOTLB_EVENTS, + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLBinvalidations */
+ flags =IOMMU_NOTIFIER_UNMAP;
+ notify =vfio_iommu_unmap_notify;
+ } else {
+ /* MAP/UNMAP IOTLB notifier */
+ flags =IOMMU_NOTIFIER_IOTLB_EVENTS;
+ notify =vfio_iommu_map_notify;
+ }hw/vfio/common.c +5
-1+ + iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx);@@ -921,7 +1028,9 @@ static void vfio_listener_region_add(MemoryListener *listener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } return;}
hw/vfio/common.c +16
-1@@ -1205,10 +1314,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listener_log_sync, }; +static MemoryListener vfio_memory_prereg_listener = { + .region_add = vfio_prereg_listener_region_add, + .region_del = vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type == VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); } }@@ -1858,6 +1973,20 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type == VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener = vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { +memory_listener_unregister (&container->prereg_listener); + ret = -1; + error_propagate_prepend(errp, container->error, + "RAM memory listenerinitialization failed "
+ "for container"); + gotofree_container_exit;
+ }
+ }
break;
}
caseVFIO_SPAPR_TCE_v2_IOMMU:hw/vfio/pci.c +19
-0@@ -2773,6 +2773,25 @@ static voidvfio_unregister_req_notifier (VFIOPCIDevice *vdev) vdev->req_enabled = false; } +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev = bus->devices[devfn]; + VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container = vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz = sizeof(info); + info.flags = VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg,sizeof (config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops = { + .set_pasid_table = vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev = VFIO_PCI(pdev);@@ -3084,6 +3103,8 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return; out_deregister:hw/vfio/trace-events +0
-0@@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name,int index, int
nr_areas) "Devic
vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned
long end) "sparse entry %d [0x%lx - 0x%lx]"
vfio_get_dev_region(const char *name, int index,uint32_ttype,uint32_tsubtype) "%s index %d, %08x/%0x8"
vfio_dma_unmap_overflow_workaround(void)""Quoted 9 lines
Quoted 8 lines
+vfio_iommu_addr_inv_iotlb(int asid,
uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
+vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d".
.
RFC v9 16/29 vfio: Pass stage 1 MSI bindings to the host
In reply to Eric AugerWe register the stage1 MSI bindings when enabling the vectors and we unregister them on msi disable.
---
v7 -> v8:
- add unregistration on msix_diable
- remove vfio_container_unbind_msis()
v4 -> v5:
- use VFIO_IOMMU_SET_MSI_BINDING
v2 -> v3:
- only register the notifier if the IOMMU translates MSIs
- record the msi bindings in a container list and unregister on container release
--- include/hw/vfio/vfio-common.h | 12 ++++++ hw/vfio/common.c | 59 +++++++++++++++++++++++++++ hw/vfio/pci.c | 76 ++++++++++++++++++++++++++++++++++- hw/vfio/trace-events | 2 + 4 files changed, 147 insertions(+), 2 deletions(-)
include/hw/vfio/vfio-common.h +12 -0
@@ -74,6 +74,14 @@ typedef struct VFIOAddressSpace {QLIST_ENTRY (VFIOAddressSpace) list; } VFIOAddressSpace; +typedef struct VFIOMSIBinding { +int index; +hwaddr iova; +hwaddr gpa; +hwaddr size; +QLIST_ENTRY (VFIOMSIBinding) next; +}VFIOMSIBinding ; +struct VFIOGroup ; typedef struct VFIOContainer {
@@ -91,6 +99,7 @@ typedef struct VFIOContainer { QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; + QLIST_HEAD(, VFIOMSIBinding) msibinding_list;QLIST_ENTRY (VFIOContainer) next; } VFIOContainer;
@@ -200,6 +209,9 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);void vfio_put_group (VFIOGroup *group);int vfio_get_device (VFIOGroup *group,const char *name,VFIODevice *vbasedev,Error **errp); +int vfio_iommu_set_msi_binding (VFIOContainer *container,int n, +IOMMUTLBEntry *entry); +int vfio_iommu_unset_msi_binding (VFIOContainer *container,int n);extern const MemoryRegionOps vfio_region_ops; typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
hw/vfio/common.c +59 -0
65 lines not on this page · raw message
hw/vfio/pci.c +74 -2
112 lines not on this page · raw message
hw/vfio/trace-events +2 -0
@@ -122,6 +122,8 @@ vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype vfio_dma_unmap_overflow_workaround(void) "" vfio_iommu_addr_inv_iotlb(int asid, uint64_t addr, uint64_t size, uint64_t nb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d" vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d" +vfio_register_msi_binding(const char *name, int vector, uint64_t giova, uint64_t gdb) "%s: register vector %d gIOVA=0x%"PRIx64 "-> gDB=0x%"PRIx64" stage 1 mapping" +vfio_unregister_msi_binding(const char *name, int vector) "%s: unregister vector %d stage 1 mapping" # platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d"
Signature · 1 line
2.26.3
RE: [RFC v9 16/29] vfio: Pass stage 1 MSI bindings to the host
Hi Eric,
Quoted 239 lines
-----Original Message-----
From: Eric Auger [mailto:[email protected]]
Sent: 11 April 2021 13:09
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Shameerali Kolothum Thodi <[email protected]>; [email protected]; [email protected]; yuzenghui <[email protected]>; [email protected]; [email protected]; jiangkunkun <[email protected]>; [email protected]; chenxiang (M) <[email protected]>; zhukeqian <[email protected]>
Subject: [RFC v9 16/29] vfio: Pass stage 1 MSI bindings to the hostWe register the stage1 MSI bindings when enabling the vectors and we unregister them on msi disable.
Signed-off-by Eric Auger <[email protected]>---
v7 -> v8:
- add unregistration onmsix_diable
- removevfio_container_unbind_msis()v4 -> v5:
- useVFIO_IOMMU_SET_MSI_BINDINGv2 -> v3:
- only register the notifier if the IOMMU translates MSIs
- record the msi bindings in a container list and unregister on container release--- include/hw/vfio/vfio-common.h | 12 ++++++ hw/vfio/common.c | 59 +++++++++++++++++++++++++++ hw/vfio/pci.c | 76 ++++++++++++++++++++++++++++++++++- hw/vfio/trace-events | 2 + 4 files changed, 147 insertions(+), 2 deletions(-)include/hw/vfio/vfio-common.h +12
-0@@ -74,6 +74,14 @@ typedef struct VFIOAddressSpace {QLIST_ENTRY (VFIOAddressSpace) list; } VFIOAddressSpace; +typedef struct VFIOMSIBinding { +int index; +hwaddr iova; +hwaddr gpa; +hwaddr size; +QLIST_ENTRY (VFIOMSIBinding) next; +}VFIOMSIBinding ; +struct VFIOGroup ; typedef struct VFIOContainer {@@ -91,6 +99,7 @@ typedef struct VFIOContainer { QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; + QLIST_HEAD(, VFIOMSIBinding) msibinding_list;QLIST_ENTRY (VFIOContainer) next; } VFIOContainer;@@ -200,6 +209,9 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);void vfio_put_group (VFIOGroup *group);int vfio_get_device (VFIOGroup *group,const char *name,VFIODevice *vbasedev,Error **errp); +int vfio_iommu_set_msi_binding (VFIOContainer *container,int n, +IOMMUTLBEntry *entry); +int vfio_iommu_unset_msi_binding (VFIOContainer *container,int n);extern const MemoryRegionOps vfio_region_ops;typedef
QLIST_HEAD(VFIOGroupList, VFIOGroup)VFIOGroupList;hw/vfio/common.c +59
-0@@ -662,6 +662,65 @@ static void vfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) } } +int vfio_iommu_set_msi_binding (VFIOContainer *container,int n, +IOMMUTLBEntry *iotlb) +{ +struct vfio_iommu_type1_set_msi_binding ustruct; +VFIOMSIBinding *binding; +int ret; + + QLIST_FOREACH(binding, &container->msibinding_list, next) { +if (binding->index == n) { +return 0 ; + } + } + + ustruct.argsz =sizeof (struct vfio_iommu_type1_set_msi_binding ); + ustruct.iova = iotlb->iova; + ustruct.flags =VFIO_IOMMU_BIND_MSI ; + ustruct.gpa = iotlb->translated_addr; + ustruct.size = iotlb->addr_mask +1 ; + ret =ioctl (container->fd,VFIO_IOMMU_SET_MSI_BINDING , &ustruct); +if (ret) { +error_report ("%s: failed to register the stage1 MSI binding (%m)" , + __func__); +return ret; + } + binding =g_new0 (VFIOMSIBinding,1 ); + binding->iova = ustruct.iova; + binding->gpa = ustruct.gpa; + binding->size = ustruct.size; + binding->index = n; + +QLIST_INSERT_HEAD (&container->msibinding_list, binding, next); +return 0 ; +} + +int vfio_iommu_unset_msi_binding (VFIOContainer *container,int n) +{ +struct vfio_iommu_type1_set_msi_binding ustruct; +VFIOMSIBinding *binding, *tmp; +int ret; + + ustruct.argsz =sizeof (struct vfio_iommu_type1_set_msi_binding ); + QLIST_FOREACH_SAFE(binding, &container->msibinding_list, next, tmp) { +if (binding->index != n) { +continue ; + } + ustruct.flags =VFIO_IOMMU_UNBIND_MSI ; + ustruct.iova = binding->iova; + ret =ioctl (container->fd,VFIO_IOMMU_SET_MSI_BINDING , &ustruct); +if (ret) { +error_report ("Failed to unregister the stage1 MSI binding " +"for iova=0x%" PRIx64" (%m)" , binding->iova); + } +QLIST_REMOVE (binding, next); +g_free (binding); +return ret; + } +return 0 ; +} + static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry*iotlb)
{ VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);hw/vfio/pci.c +38
-0@@ -366,6 +366,65 @@ static void vfio_msi_interrupt(void *opaque)notify (&vdev->pdev, nr); } +static bool vfio_iommu_require_msi_binding (IOMMUMemoryRegion *iommu_mr) +{ +bool msi_translate = false, nested = false; + +memory_region_iommu_get_attr (iommu_mr,IOMMU_ATTR_MSI_TRANSLATE , + (void *)&msi_translate); +memory_region_iommu_get_attr (iommu_mr,IOMMU_ATTR_VFIO_NESTED , + (void *)&nested); +if (!nested || !msi_translate) { +return false; + } +return true; +} + +static int vfio_register_msi_binding (VFIOPCIDevice *vdev, +int vector_n,bool set) +{ +VFIOContainer *container = vdev->vbasedev.group->container; +PCIDevice *dev = &vdev->pdev; +AddressSpace *as =pci_device_iommu_address_space (dev); +IOMMUMemoryRegionClass *imrc; +IOMMUMemoryRegion *iommu_mr; +IOMMUTLBEntry entry; +MSIMessage msg; + +if (as == &address_space_memory) { +return 0 ; + } + + iommu_mr =IOMMU_MEMORY_REGION (as->root); +if (!vfio_iommu_require_msi_binding (iommu_mr)) { +return 0 ; + } + +/* MSI doorbell address is translated by an IOMMU */ + +if (!set) {/* unregister */ + trace_vfio_unregister_msi_binding(vdev->vbasedev.name,
vector_n);hw/vfio/pci.c +17
-0+ +return vfio_iommu_unset_msi_binding (container, vector_n); + } + + msg =pci_get_msi_message (dev, vector_n); + imrc =memory_region_get_iommu_class_nocheck (iommu_mr); + +rcu_read_lock (); + entry = imrc->translate (iommu_mr, msg.address,IOMMU_WO ,0 ); +rcu_read_unlock (); + +if (entry.perm ==IOMMU_NONE ) { +return -ENOENT ; + } + + trace_vfio_register_msi_binding(vdev->vbasedev.name, vector_n, + msg.address,entry.translated_addr);
hw/vfio/pci.c +5
-1+ +return vfio_iommu_set_msi_binding (container, vector_n, &entry); +} +static int vfio_enable_vectors (VFIOPCIDevice *vdev,bool msix) {struct vfio_irq_set *irq_set;@@ -383,7 +442,7 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev, bool msix) fds = (int32_t *)&irq_set->data;for (i =0 ; i < vdev->nr_vectors; i++) { -int fd =-1 ; +int ret, fd =-1 ;/* * MSI vs MSI-X - The guest has direct access to MSI mask and
pending
hw/vfio/pci.c static int vfio_enable_vectors(VFIOPCIDevice *vdev, · line 455
Quoted 5 lines
hw/vfio/pci.c +1
-0@@ -392,6 +451,12 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev,bool msix) * KVM signaling path only when configured and unmasked. */ if (vdev->msi_vectors[i].use) { + ret =vfio_register_msi_binding (vdev, i, true);
Just a heads up if you have plans to re-spin this series soon.
With the recent kernel commit 438553958ba1("PCI/MSI: Enable and mask MSI-X early"), MSI-X is enabled early but vectors remained masked.
Looks like it creates a problem here when you try to call pci_get_msi_message() above as it returns msg.address as 0. Hence the S1 MSI bind fails for devices with MSI-X cap.
I have a temp fix here[2] for the issue where we effectively wait till MASKALL is cleared to enable MSI-X. Or else we need to delay this binding for MSI-X for a later stage.
Please take a look and let me know.
Thanks,
Shameer
[1] https://lore.kernel.org/all/[email protected]/
[2] https://github.com/Linaro/qemu/commit/568820e409417473eb6f16dfdf8e9075f5a5feaf
Quoted 65 lines
hw/vfio/pci.c +5
-0+if (ret) { +error_report ("%s failed to register S1 MSI binding " +"for vector %d(%d)" , vdev->vbasedev.name, i, ret); + goto out; + } if (vdev->msi_vectors[i].virq < 0 ||(msix && msix_is_masked(&vdev->pdev, i))) { fd =
event_notifier_get_fd(&vdev->msi_vectors[i].interrupt);hw/vfio/pci.c +1
-0@@ -405,6 +470,7 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev, bool msix) ret =ioctl (vdev->vbasedev.fd,VFIO_DEVICE_SET_IRQS , irq_set); +out:g_free (irq_set);return ret;hw/vfio/pci.c +7
-1@@ -719,7 +785,8 @@ static void vfio_msi_disable_common(VFIOPCIDevice *vdev)static void vfio_msix_disable (VFIOPCIDevice *vdev) { -int i; +int ret, i; +msix_unset_vector_notifiers (&vdev->pdev);@@ -731,6 +798,11 @@ static void vfio_msix_disable(VFIOPCIDevice *vdev)if (vdev->msi_vectors[i].use) {vfio_msix_vector_release (&vdev->pdev, i);msix_vector_unuse (&vdev->pdev, i); + ret =vfio_register_msi_binding (vdev, i, false); +if (ret) { +error_report ("%s: failed to unregister S1 MSI binding " +"for vector %d(%d)" , vdev->vbasedev.name, i, ret); + } } }hw/vfio/trace-events +0
-0@@ -122,6 +122,8 @@ vfio_get_dev_region(const char *name, int index,
uint32_ttype,uint32_tsubtype
vfio_dma_unmap_overflow_workaround(void)"" vfio_iommu_addr_inv_iotlb(int asid,uint64_taddr,uint64_tsize,uint64_tnb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d"
vfio_iommu_asid_inv_iotlb(int asid)"nested IOTLB invalidate asid=%d"
+vfio_register_msi_binding(const char *name, int vector,uint64_tgiova,uint64_tgdb) "%s: register vector %d gIOVA=0x%"PRIx64 "-> gDB=0x%"PRIx64" stage 1 mapping"
+vfio_unregister_msi_binding(const char *name, int vector) "%s: unregister vector %d stage 1 mapping"# platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to group #%d" -- 2.26.3
RFC v9 17/29 vfio: Helper to get IRQ info including capabilities
In reply to Eric AugerAs done for vfio regions, add helpers to retrieve irq info including their optional capabilities.
--- include/hw/vfio/vfio-common.h | 7 +++ hw/vfio/common.c | 97 +++++++++++++++++++++++++++++++++++ hw/vfio/trace-events | 1 + 3 files changed, 105 insertions(+)
include/hw/vfio/vfio-common.h +7 -0
@@ -232,6 +232,13 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, unsigned int *avail);struct vfio_info_cap_header *vfio_get_device_info_cap (struct vfio_device_info *info,uint16_t id); +int vfio_get_irq_info (VFIODevice *vbasedev,int index, +struct vfio_irq_info **info); +int vfio_get_dev_irq_info (VFIODevice *vbasedev,uint32_t type, +uint32_t subtype,struct vfio_irq_info **info); +bool vfio_has_irq_cap (VFIODevice *vbasedev,int irq,uint16_t cap_type); +struct vfio_info_cap_header * +vfio_get_irq_info_cap (struct vfio_irq_info *info,uint16_t id);#endif extern const MemoryListener vfio_prereg_listener;
hw/vfio/common.c +97 -0
121 lines not on this page · raw message
hw/vfio/trace-events +1 -0
@@ -119,6 +119,7 @@ vfio_region_unmap(const char *name, unsigned long offset, unsigned long end) "Re vfio_region_sparse_mmap_header(const char *name, int index, int nr_areas) "Device %s region %d: %d sparse mmap entries" vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long end) "sparse entry %d [0x%lx - 0x%lx]" vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t subtype) "%s index %d, %08x/%0x8" +vfio_get_dev_irq(const char *name, int index, uint32_t type, uint32_t subtype) "%s index %d, %08x/%0x8" vfio_dma_unmap_overflow_workaround(void) "" vfio_iommu_addr_inv_iotlb(int asid, uint64_t addr, uint64_t size, uint64_t nb_granules, bool leaf) "nested IOTLB invalidate asid=%d, addr=0x%"PRIx64" granule_size=0x%"PRIx64" nb_granules=0x%"PRIx64" leaf=%d" vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=%d"
Signature · 1 line
2.26.3
RFC v9 18/29 vfio/pci: Register handler for iommu fault
In reply to Eric AugerWe use the new extended IRQ VFIO_IRQ_TYPE_NESTED type and VFIO_IRQ_SUBTYPE_DMA_FAULT subtype to set/unset
a notifier for physical DMA faults. The associated eventfd is triggered, in nested mode, whenever a fault is detected at IOMMU physical level.
The actual handler will be implemented in subsequent patches.
---
v4 -> v5:
- index_to_str now returns the index name, ie. DMA_FAULT
- use the extended IRQ
v3 -> v4:
- check VFIO_PCI_DMA_FAULT_IRQ_INDEX is supported at kernel level before attempting to set signaling for it.
--- hw/vfio/pci.h | 7 +++++ hw/vfio/pci.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-)
hw/vfio/pci.h +7 -0
@@ -114,6 +114,12 @@ typedef struct VFIOMSIXInfo {unsigned long *pending; } VFIOMSIXInfo; +typedef struct VFIOPCIExtIRQ { +struct VFIOPCIDevice *vdev; +EventNotifier notifier; +uint32_t index; +}VFIOPCIExtIRQ ; +#define TYPE_VFIO_PCI "vfio-pci"OBJECT_DECLARE_SIMPLE_TYPE (VFIOPCIDevice,VFIO_PCI )
@@ -138,6 +144,7 @@ struct VFIOPCIDevice {PCIHostDeviceAddress host;EventNotifier err_notifier;EventNotifier req_notifier; +VFIOPCIExtIRQ *ext_irqs;int (*resetfn)(struct VFIOPCIDevice *);uint32_t vendor_id;uint32_t device_id;
hw/vfio/pci.c +80 -1
111 lines not on this page · raw message
Signature · 1 line
2.26.3
RFC v9 19/29 vfio/pci: Set up the DMA FAULT region
In reply to Eric AugerSet up the fault region which is composed of the actual fault queue (mmappable) and a header used to handle it. The fault queue is mmapped.
---
v4 -> v5:
- use a single DMA FAULT region. No version selection anymore
--- hw/vfio/pci.h | 1 + hw/vfio/pci.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+)
hw/vfio/pci.h +1 -0
@@ -145,6 +145,7 @@ struct VFIOPCIDevice {EventNotifier err_notifier;EventNotifier req_notifier;VFIOPCIExtIRQ *ext_irqs; +VFIORegion dma_fault_region;int (*resetfn)(struct VFIOPCIDevice *);uint32_t vendor_id;uint32_t device_id;
hw/vfio/pci.c +64 -0
93 lines not on this page · raw message
Signature · 1 line
2.26.3
RFC v9 20/29 vfio/pci: Implement the DMA fault handler
In reply to Eric AugerWhenever the eventfd is triggered, we retrieve the DMA fault(s) from the mmapped fault region and inject them in the iommu memory region.
--- hw/vfio/pci.h | 1 + hw/vfio/pci.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+)
hw/vfio/pci.h +1 -0
@@ -146,6 +146,7 @@ struct VFIOPCIDevice {EventNotifier req_notifier;VFIOPCIExtIRQ *ext_irqs;VFIORegion dma_fault_region; +uint32_t fault_tail_index;int (*resetfn)(struct VFIOPCIDevice *);uint32_t vendor_id;uint32_t device_id;
hw/vfio/pci.c +50 -0
60 lines not on this page · raw message
Signature · 1 line
2.26.3
RFC v9 21/29 hw/arm/smmuv3: Advertise MSI_TRANSLATE attribute
In reply to Eric AugerThe SMMUv3 has the peculiarity to translate MSI
transactionss. let's advertise the corresponding
attribute.
---
--- hw/arm/smmuv3.c | 3 +++ 1 file changed, 3 insertions(+)
hw/arm/smmuv3.c +3 -0
@@ -1589,6 +1589,9 @@ static int smmuv3_get_attr(IOMMUMemoryRegion *iommu,if (attr ==IOMMU_ATTR_VFIO_NESTED ) { *(bool *) data = true;return 0 ; + }else if (attr ==IOMMU_ATTR_MSI_TRANSLATE ) { + *(bool *) data = true; +return 0 ; }return -EINVAL ; }
Signature · 1 line
2.26.3
RFC v9 22/29 hw/arm/smmuv3: Store the PASID table GPA in the translation config
In reply to Eric AugerFor VFIO integration we will need to pass the Context Descriptor (CD) table GPA to the host. The CD table is also referred to as the PASID table. Its GPA corresponds to the s1ctrptr field of the Stream Table Entry. So let's decode and store it in the configuration structure.
--- include/hw/arm/smmu-common.h | 1 + hw/arm/smmuv3.c | 1 + 2 files changed, 2 insertions(+)
include/hw/arm/smmu-common.h +1 -0
@@ -76,6 +76,7 @@ typedef struct SMMUTransCfg {uint8_t tbi;/* Top Byte Ignore */ uint16_t asid;SMMUTransTableInfo tt[2 ]; +dma_addr_t s1ctxptr;uint32_t iotlb_hits;/* counts IOTLB hits for this asid */ uint32_t iotlb_misses;/* counts IOTLB misses for this asid */ } SMMUTransCfg;
hw/arm/smmuv3.c +1 -0
@@ -358,6 +358,7 @@ static int decode_ste(SMMUv3State *s, SMMUTransCfg *cfg, "SMMUv3 S1 stalling fault model not allowed yet\n"); goto bad_ste; } + cfg->s1ctxptr =STE_CTXPTR (ste);return 0 ; bad_ste:
Signature · 1 line
2.26.3
RFC v9 23/29 hw/arm/smmuv3: Fill the IOTLBEntry arch_id on NH_VA invalidation
In reply to Eric AugerWhen the guest invalidates one S1 entry, it passes the asid. When propagating this invalidation downto the host, the asid information also must be passed. So let's fill the arch_id field introduced for that purpose and accordingly set the flags to indicate its presence.
---
v7 -> v8:
- set flags
--- hw/arm/smmuv3.c | 2 ++ 1 file changed, 2 insertions(+)
hw/arm/smmuv3.c +2 -0
@@ -832,6 +832,8 @@ static void smmuv3_notify_iova(IOMMUMemoryRegion *mr, event.entry.iova = iova; event.entry.addr_mask = num_pages * (1 << granule) -1 ; event.entry.perm =IOMMU_NONE ; + event.entry.flags =IOMMU_INV_FLAGS_ARCHID ; + event.entry.arch_id = asid;memory_region_notify_iommu_one (n, &event); }
Signature · 1 line
2.26.3
RFC v9 24/29 hw/arm/smmuv3: Fill the IOTLBEntry leaf field on NH_VA invalidation
In reply to Eric AugerLet's propagate the leaf attribute throughout the invalidation path. This hint is used to reduce the scope of the invalidations to the last level of translation. Not enforcing it induces large performance penalties in nested mode.
--- hw/arm/smmuv3.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
hw/arm/smmuv3.c +5 -4
@@ -799,7 +799,7 @@ epilogue:static void smmuv3_notify_iova (IOMMUMemoryRegion *mr,IOMMUNotifier *n,int asid,dma_addr_t iova, -uint8_t tg,uint64_t num_pages) +uint8_t tg,uint64_t num_pages,bool leaf) {SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu);IOMMUTLBEvent event = {};
@@ -834,6 +834,7 @@ static void smmuv3_notify_iova(IOMMUMemoryRegion *mr, event.entry.perm =IOMMU_NONE ; event.entry.flags =IOMMU_INV_FLAGS_ARCHID ; event.entry.arch_id = asid; + event.entry.leaf = leaf;memory_region_notify_iommu_one (n, &event); }
@@ -865,7 +866,7 @@ static void smmuv3_notify_asid(IOMMUMemoryRegion *mr,/* invalidate an asid/iova range tuple in all mr's */ static void smmuv3_inv_notifiers_iova (SMMUState *s,int asid,dma_addr_t iova, -uint8_t tg,uint64_t num_pages) +uint8_t tg,uint64_t num_pages,bool leaf) {SMMUDevice *sdev;
@@ -877,7 +878,7 @@ static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, dma_addr_t iova, tg, num_pages); IOMMU_NOTIFIER_FOREACH(n, mr) { -smmuv3_notify_iova (mr, n, asid, iova, tg, num_pages); +smmuv3_notify_iova (mr, n, asid, iova, tg, num_pages, leaf); } } }
@@ -915,7 +916,7 @@ static void smmuv3_s1_range_inval(SMMUState *s, Cmd *cmd) count = mask +1 ;trace_smmuv3_s1_range_inval (vmid, asid, addr, tg, count, ttl, leaf); -smmuv3_inv_notifiers_iova (s, asid, addr, tg, count); +smmuv3_inv_notifiers_iova (s, asid, addr, tg, count, leaf);smmu_iotlb_inv_iova (s, asid, addr, tg, count, ttl); num_pages -= count;
Signature · 1 line
2.26.3
Re: [RFC v9 24/29] hw/arm/smmuv3: Fill the IOTLBEntry leaf field on NH_VA invalidation
Hi Eric,
Quoted 32 lines · Eric Auger
Let's propagate the leaf attribute throughout the invalidation path. This hint is used to reduce the scope of the invalidations to the last level of translation. Not enforcing it induces large performance penalties in nested mode.
Signed-off-by Eric Auger <[email protected]>--- hw/arm/smmuv3.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)hw/arm/smmuv3.c +2
-1@@ -799,7 +799,7 @@ epilogue:static void smmuv3_notify_iova (IOMMUMemoryRegion *mr,IOMMUNotifier *n,int asid,dma_addr_t iova, -uint8_t tg,uint64_t num_pages) +uint8_t tg,uint64_t num_pages,bool leaf) {SMMUDevice *sdev =container_of (mr, SMMUDevice, iommu);IOMMUTLBEvent event = {};@@ -834,6 +834,7 @@ static void smmuv3_notify_iova(IOMMUMemoryRegion *mr, event.entry.perm =IOMMU_NONE ; event.entry.flags =IOMMU_INV_FLAGS_ARCHID ; event.entry.arch_id = asid; + event.entry.leaf = leaf;memory_region_notify_iommu_one (n, &event); }
hw/arm/smmuv3.c static void smmuv3_notify_asid(IOMMUMemoryRegion *mr, · line 871
Quoted 7 lines
hw/arm/smmuv3.c +1
-1@@ -865,7 +866,7 @@ static void smmuv3_notify_asid(IOMMUMemoryRegion *mr,/* invalidate an asid/iova range tuple in all mr's */ static void smmuv3_inv_notifiers_iova (SMMUState *s,int asid,dma_addr_t iova, -uint8_t tg,uint64_t num_pages) +uint8_t tg,uint64_t num_pages,bool leaf) {SMMUDevice *sdev;
Does the parameter 'leaf' need to be added to the trace?
Quoted 2 lines
hw/arm/smmuv3.c +0
-0trace_smmuv3_inv_notifiers_iova(mr->parent_obj.name, asid, iova,tg,
num_pages);
Thanks,
Kunkun Jiang
Quoted 19 lines
hw/arm/smmuv3.c +2
-2@@ -877,7 +878,7 @@ static void smmuv3_inv_notifiers_iova(SMMUState *s, int asid, dma_addr_t iova, tg, num_pages); IOMMU_NOTIFIER_FOREACH(n, mr) { -smmuv3_notify_iova (mr, n, asid, iova, tg, num_pages); +smmuv3_notify_iova (mr, n, asid, iova, tg, num_pages, leaf); } } }@@ -915,7 +916,7 @@ static void smmuv3_s1_range_inval(SMMUState *s, Cmd *cmd) count = mask +1 ;trace_smmuv3_s1_range_inval (vmid, asid, addr, tg, count, ttl, leaf); -smmuv3_inv_notifiers_iova (s, asid, addr, tg, count); +smmuv3_inv_notifiers_iova (s, asid, addr, tg, count, leaf);smmu_iotlb_inv_iova (s, asid, addr, tg, count, ttl); num_pages -= count;
RFC v9 25/29 hw/arm/smmuv3: Pass stage 1 configurations to the host
In reply to Eric AugerIn case PASID PciOps are set for the device we call the set_pasid_table() callback on each STE update.
This allows to pass the guest stage 1 configuration to the host and apply it at physical level.
---
v4 -> v5:
- Use PciOps instead of config notifiers
v3 -> v4:
- fix compile issue with mingw
v2 -> v3:
- adapt to pasid_cfg field changes. Use local variable
- add trace event
- set version fields
- use CONFIG_PASID
v1 -> v2:
- do not notify anymore on CD change. Anyway the smmuv3 linux driver is not sending any CD invalidation commands. If we were to propagate CD invalidation commands, we would use the CACHE_INVALIDATE VFIO ioctl.
- notify a precise config flags to prepare for addition of new flags
--- hw/arm/smmu-internal.h | 1 + hw/arm/smmuv3.c | 72 ++++++++++++++++++++++++++++++++++++------ hw/arm/trace-events | 1 + 3 files changed, 64 insertions(+), 10 deletions(-)
hw/arm/smmu-internal.h +1 -0
@@ -105,6 +105,7 @@ typedef struct SMMUIOTLBPageInvInfo { } SMMUIOTLBPageInvInfo;typedef struct SMMUSIDRange { +SMMUState *state;uint32_t start;uint32_t end; }SMMUSIDRange ;
hw/arm/smmuv3.c +62 -10
109 lines not on this page · raw message
hw/arm/trace-events +1 -0
@@ -53,4 +53,5 @@ smmuv3_config_cache_inv(uint32_t sid) "Config cache INV for sid=0x%x" smmuv3_notify_flag_add(const char *iommu) "ADD SMMUNotifier node for iommu mr=%s" smmuv3_notify_flag_del(const char *iommu) "DEL SMMUNotifier node for iommu mr=%s" smmuv3_inv_notifiers_iova(const char *name, uint16_t asid, uint64_t iova, uint8_t tg, uint64_t num_pages) "iommu mr=%s asid=%d iova=0x%"PRIx64" tg=%d num_pages=0x%"PRIx64 +smmuv3_notify_config_change(const char *name, uint8_t config, uint64_t s1ctxptr) "iommu mr=%s config=%d s1ctxptr=0x%"PRIx64
Signature · 1 line
2.26.3
RFC v9 26/29 hw/arm/smmuv3: Implement fault injection
In reply to Eric AugerWe convert iommu_fault structs received from the kernel into the data struct used by the emulation code and record the evnts into the virtual event queue.
---
v3 -> v4:
- fix compil issue on mingw
Exhaustive mapping remains to be done
--- hw/arm/smmuv3.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+)
hw/arm/smmuv3.c +71 -0
@@ -1652,6 +1652,76 @@ static int smmuv3_get_attr(IOMMUMemoryRegion *iommu,return -EINVAL ; } +struct iommu_fault ; + +static inline int +smmuv3_inject_faults (IOMMUMemoryRegion *iommu_mr,int count, +struct iommu_fault *buf) +{ +#ifdef __linux__ +SMMUDevice *sdev =container_of (iommu_mr, SMMUDevice, iommu); +SMMUv3State *s3 = sdev->smmu; +uint32_t sid =smmu_get_sid (sdev); +int i; + +for (i =0 ; i < count; i++) { +SMMUEventInfo info = {}; +struct iommu_fault_unrecoverable *record; + +if (buf[i].type !=IOMMU_FAULT_DMA_UNRECOV ) { +continue ; + } + + info.sid = sid; + record = &buf[i].event; + +switch (record->reason) { +case IOMMU_FAULT_REASON_PASID_INVALID : + info.type =SMMU_EVT_C_BAD_SUBSTREAMID ; +/* TODO further fill info.u.c_bad_substream */ +break ; +case IOMMU_FAULT_REASON_PASID_FETCH : + info.type =SMMU_EVT_F_CD_FETCH ; +break ; +case IOMMU_FAULT_REASON_BAD_PASID_ENTRY : + info.type =SMMU_EVT_C_BAD_CD ; +/* TODO further fill info.u.c_bad_cd */ +break ; +case IOMMU_FAULT_REASON_WALK_EABT : + info.type =SMMU_EVT_F_WALK_EABT ; + info.u.f_walk_eabt.addr = record->addr; + info.u.f_walk_eabt.addr2 = record->fetch_addr; +break ; +case IOMMU_FAULT_REASON_PTE_FETCH : + info.type =SMMU_EVT_F_TRANSLATION ; + info.u.f_translation.addr = record->addr; +break ; +case IOMMU_FAULT_REASON_OOR_ADDRESS : + info.type =SMMU_EVT_F_ADDR_SIZE ; + info.u.f_addr_size.addr = record->addr; +break ; +case IOMMU_FAULT_REASON_ACCESS : + info.type =SMMU_EVT_F_ACCESS ; + info.u.f_access.addr = record->addr; +break ; +case IOMMU_FAULT_REASON_PERMISSION : + info.type =SMMU_EVT_F_PERMISSION ; + info.u.f_permission.addr = record->addr; +break ; +default : +warn_report ("%s Unexpected fault reason received from host: %d" , + __func__, record->reason); +continue ; + } + +smmuv3_record_event (s3, &info); + } +return 0 ; +#else +return -1 ; +#endif +} +static void smmuv3_iommu_memory_region_class_init (ObjectClass *klass,void *data) {
@@ -1660,6 +1730,7 @@ static void smmuv3_iommu_memory_region_class_init(ObjectClass *klass, imrc->translate = smmuv3_translate; imrc->notify_flag_changed = smmuv3_notify_flag_changed; imrc->get_attr = smmuv3_get_attr; + imrc->inject_faults = smmuv3_inject_faults; }static const TypeInfo smmuv3_type_info = {
Signature · 1 line
2.26.3
RFC v9 27/29 hw/arm/smmuv3: Allow MAP notifiers
In reply to Eric AugerWe now have all bricks to support nested paging. This uses MAP notifiers to map the MSIs. So let's allow MAP notifiers to be registered.
--- hw/arm/smmuv3.c | 8 -------- 1 file changed, 8 deletions(-)
hw/arm/smmuv3.c +0 -8
@@ -1620,14 +1620,6 @@ static int smmuv3_notify_flag_changed(IOMMUMemoryRegion *iommu,return -EINVAL ; } -if (new &IOMMU_NOTIFIER_MAP ) { -error_setg (errp, -"device %02x.%02x.%x requires iommu MAP notifier which is " -"not currently supported" ,pci_bus_num (sdev->bus), -PCI_SLOT (sdev->devfn),PCI_FUNC (sdev->devfn)); -return -EINVAL ; - } -if (old ==IOMMU_NOTIFIER_NONE ) {trace_smmuv3_notify_flag_add (iommu->parent_obj.name);QLIST_INSERT_HEAD (&s->devices_with_notifiers, sdev, next);
Signature · 1 line
2.26.3
RFC v9 28/29 pci: Add return_page_response pci ops
In reply to Eric AugerAdd a new PCI operation that allows to return page responses to registered VFIO devices
--- include/hw/iommu/iommu.h | 8 ++++++++ include/hw/pci/pci.h | 4 ++++ hw/pci/pci.c | 16 ++++++++++++++++ 3 files changed, 28 insertions(+)
include/hw/iommu/iommu.h +8 -0
@@ -24,5 +24,13 @@ typedef struct IOMMUConfig { }; } IOMMUConfig; +typedef struct IOMMUPageResponse { +union { +#ifdef __linux__ +struct iommu_page_response resp; +#endif + }; +}IOMMUPageResponse ; +#endif /* QEMU_HW_IOMMU_IOMMU_H */
include/hw/pci/pci.h +4 -0
@@ -268,6 +268,8 @@ typedef struct PCIReqIDCache PCIReqIDCache;struct PCIPASIDOps {int (*set_pasid_table)(PCIBus *bus,int32_t devfn,IOMMUConfig *config); +int (*return_page_response)(PCIBus *bus,int32_t devfn, +IOMMUPageResponse *resp); };typedef struct PCIPASIDOps PCIPASIDOps ;
@@ -501,6 +503,8 @@ void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque);void pci_setup_pasid_ops (PCIDevice *dev,PCIPASIDOps *ops);bool pci_device_is_pasid_ops_set (PCIBus *bus,int32_t devfn);int pci_device_set_pasid_table (PCIBus *bus,int32_t devfn,IOMMUConfig *config); +int pci_device_return_page_response (PCIBus *bus,int32_t devfn, +IOMMUPageResponse *resp);static inline void pci_set_byte (uint8_t *config,uint8_t val)
hw/pci/pci.c +16 -0
@@ -2765,6 +2765,22 @@ int pci_device_set_pasid_table(PCIBus *bus, int32_t devfn,return -ENOENT ; } +int pci_device_return_page_response (PCIBus *bus,int32_t devfn, +IOMMUPageResponse *resp) +{ +PCIDevice *dev; + +if (!bus) { +return -EINVAL ; + } + + dev = bus->devices[devfn]; +if (dev && dev->pasid_ops && dev->pasid_ops->return_page_response) { +return dev->pasid_ops->return_page_response (bus, devfn, resp); + } +return -ENOENT ; +} +static void pci_dev_get_w64 (PCIBus *b,PCIDevice *dev,void *opaque) {Range *range = opaque;
Signature · 1 line
2.26.3
RFC v9 29/29 vfio/pci: Implement return_page_response page response callback
In reply to Eric AugerThis patch implements the page response path. The response is written into the page response ring buffer and then update header's head index is updated. This path is not used by this series. It is introduced here as a POC for vSVA/ARM integration.
---
v11 -> v12:
- use VFIO_REGION_INFO_CAP_DMA_FAULT_RESPONSE [Shameer]
- fix hot del regression reported and fixed by Shameer
--- hw/vfio/pci.h | 2 + hw/vfio/pci.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+)
hw/vfio/pci.h +2 -0
@@ -147,6 +147,8 @@ struct VFIOPCIDevice {VFIOPCIExtIRQ *ext_irqs;VFIORegion dma_fault_region;uint32_t fault_tail_index; +VFIORegion dma_fault_response_region; +uint32_t fault_response_head_index;int (*resetfn)(struct VFIOPCIDevice *);uint32_t vendor_id;uint32_t device_id;
hw/vfio/pci.c +123 -0
155 lines not on this page · raw message
Signature · 1 line
2.26.3