vSMMUv3/pSMMUv3 2 stage VFIO integration
v8 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
This series applies on top of
[PATCH v2 0/7] Some vIOMMU fixes and
PATCH 0/2] Additional vIOMMU fixes related to UNMAP notifiers
All the patches can be found at:
v8: https://github.com/eauger/qemu/tree/v5.2.0-2stage-rfcv8
Previous version:
v7: https://github.com/eauger/qemu/tree/v5.2.0-rc1-2stage-rfcv7
Kernel Dependencies:
[1] [PATCH v14 00/14] SMMUv3 Nested Stage Setup (IOMMU part)
[2] [PATCH v12 00/13] SMMUv3 Nested Stage Setup (VFIO part) branch containing both:
https://github.com/eauger/linux/tree/v5.11-stallv12-2stage-v14
History:
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
v6 -> v7:
- rebase on v5.2.0-rc1
- added:
"pci: Add return_page_response pci ops" and
"vfio/pci: Implement return_page_response page response callback" for vSVA integration (not used in this series).
v5 -> v6:
- just rebase work
v4 -> v5:
- Use PCIPASIDOps for config update notifications
- removal of notification for MSI binding which is not needed anymore
- Use a single fault region
- use the specific interrupt index
v3 -> v4:
- adapt to changes in uapi (asid cache invalidation)
- check VFIO_PCI_DMA_FAULT_IRQ_INDEX is supported at kernel level before attempting to set signaling for it.
- sync on 5.2-rc1 kernel headers + Drew's patch that imports sve_context.h
- fix MSI binding for MSI (not MSIX)
- fix mingw compilation
v2 -> v3:
- rework fault handling
- MSI binding registration done in vfio-pci. MSI binding tear down called on container cleanup path
- leaf parameter propagated
v1 -> v2:
- Fixed dual assignment (asid now correctly propagated on TLB invalidations)
- Integrated fault reporting
Eric Auger (27):
hw/vfio/common: trace vfio_connect_container operations
update-linux-headers: Import iommu.h
header update against 5.11-rc2 and IOMMU/VFIO nested stage APIs
memory: Add new fields in IOTLBEntry
hw/arm/smmuv3: Properly propagate S1 asid invalidation
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-common.c | 2 +- hw/arm/smmu-internal.h | 1 + hw/arm/smmuv3.c | 222 ++++++- hw/arm/trace-events | 2 + 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/vfio/pci.h | 11 + hw/vfio/trace-events | 11 +- hw/virtio/virtio-iommu.c | 4 +- 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 + .../infiniband/hw/vmw_pvrdma/pvrdma_ring.h | 14 +- linux-headers/linux/iommu.h | 395 ++++++++++++ linux-headers/linux/vfio.h | 141 ++++- scripts/update-linux-headers.sh | 2 +- softmmu/memory.c | 10 + 22 files changed, 1828 insertions(+), 160 deletions(-) create mode 100644 include/hw/iommu/iommu.h create mode 100644 linux-headers/linux/iommu.h
Signature · 1 line
2.26.2
RFC v8 2/28 update-linux-headers: Import iommu.h
Update 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.2
RFC v8 4/28 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.
Also 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 user those new fields, initialize the IOMMUTLBEvents when needed.
---
v7 -> v8:
- add pasid, granularity and flags
--- 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 ++-- include/exec/memory.h | 36 +++++++++++++++++++++++++++++++++++- 6 files changed, 43 insertions(+), 9 deletions(-)
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 )) {
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; };/*
Signature · 1 line
2.26.2
RFC v8 5/28 hw/arm/smmuv3: Properly propagate S1 asid invalidation
In reply to Eric AugerAt the moment ASID invalidation command (CMD_TLBI_NH_ASID) is propagated as a domain invalidation, ie. all ASIDs get invalidated, failing to restrict the invalidation to the accurate asid.
Fix that by populating the new fields laterly introduced in the IOTLEntry struct, namely setting the granularity to PASID and setting the arch_id to the invalidated asid.
--- hw/arm/smmuv3.c | 42 ++++++++++++++++++++++++++++++++++++++++-- hw/arm/trace-events | 1 + 2 files changed, 41 insertions(+), 2 deletions(-)
hw/arm/smmuv3.c +40 -2
@@ -835,6 +835,29 @@ 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; + +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 +933,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 +1059,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.2
RFC v8 1/28 hw/vfio/common: trace vfio_connect_container operations
In reply to Eric AugerWe 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
@@ -1709,6 +1709,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 ;
@@ -1742,6 +1744,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.2
RFC v8 9/28 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.2
RFC v8 6/28 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.
--- hw/arm/smmuv3.c | 12 ++++++++++++ include/exec/memory.h | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-)
hw/arm/smmuv3.c +12 -0
@@ -1580,6 +1580,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) {
@@ -1587,6 +1598,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 = {
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 , };/*
Signature · 1 line
2.26.2
RFC v8 12/28 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
@@ -654,6 +654,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) {
@@ -663,7 +676,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)) {
@@ -748,15 +760,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;
@@ -937,16 +942,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.2
RFC v8 10/28 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.
--- hw/pci/pci.c | 34 ++++++++++++++++++++++++++++++++++ include/hw/pci/pci.h | 11 +++++++++++ 2 files changed, 45 insertions(+)
hw/pci/pci.c +34 -0
@@ -2724,6 +2724,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;
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;
@@ -359,6 +365,7 @@ struct PCIDevice {/* ID of standby device in net_failover pair */ char *failover_pair_id; +PCIPASIDOps *pasid_ops; }; void pci_register_bar(PCIDevice *pci_dev, int region_num,
@@ -490,6 +497,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) {
Signature · 1 line
2.26.2
RFC v8 15/28 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
--- hw/vfio/common.c | 59 +++++++++++++++++++++++++++ hw/vfio/pci.c | 76 ++++++++++++++++++++++++++++++++++- hw/vfio/trace-events | 2 + include/hw/vfio/vfio-common.h | 12 ++++++ 4 files changed, 147 insertions(+), 2 deletions(-)
hw/vfio/common.c +59 -0
@@ -667,6 +667,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 +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"
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;
Signature · 1 line
2.26.2
RFC v8 3/28 header update against 5.11-rc2 and IOMMU/VFIO nested stage APIs
In reply to Eric Auger--- .../infiniband/hw/vmw_pvrdma/pvrdma_ring.h | 14 +- linux-headers/linux/iommu.h | 395 ++++++++++++++++++ linux-headers/linux/vfio.h | 141 ++++++- 3 files changed, 542 insertions(+), 8 deletions(-) create mode 100644 linux-headers/linux/iommu.h
include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/pvrdma_ring.h +7 -7
@@ -68,7 +68,7 @@ static inline int pvrdma_idx_valid(uint32_t idx, uint32_t max_elems)static inline int32_t pvrdma_idx (int *var,uint32_t max_elems) { -const unsigned int idx =qatomic_read (var); +const unsigned int idx =atomic_read (var);if (pvrdma_idx_valid (idx, max_elems))return idx & (max_elems -1 );
@@ -77,17 +77,17 @@ static inline int32_t pvrdma_idx(int *var, uint32_t max_elems)static inline void pvrdma_idx_ring_inc (int *var,uint32_t max_elems) { -uint32_t idx =qatomic_read (var) +1 ;/* Increment. */ +uint32_t idx =atomic_read (var) +1 ;/* Increment. */ idx &= (max_elems <<1 ) -1 ;/* Modulo size, flip gen. */ -qatomic_set (var, idx); +atomic_set (var, idx); }static inline int32_t pvrdma_idx_ring_has_space (const struct pvrdma_ring *r,uint32_t max_elems,uint32_t *out_tail) { -const uint32_t tail =qatomic_read (&r->prod_tail); -const uint32_t head =qatomic_read (&r->cons_head); +const uint32_t tail =atomic_read (&r->prod_tail); +const uint32_t head =atomic_read (&r->cons_head);if (pvrdma_idx_valid (tail, max_elems) &&pvrdma_idx_valid (head, max_elems)) {
@@ -100,8 +100,8 @@ static inline int32_t pvrdma_idx_ring_has_space(const struct pvrdma_ring *r,static inline int32_t pvrdma_idx_ring_has_data (const struct pvrdma_ring *r,uint32_t max_elems,uint32_t *out_head) { -const uint32_t tail =qatomic_read (&r->prod_tail); -const uint32_t head =qatomic_read (&r->cons_head); +const uint32_t tail =atomic_read (&r->prod_tail); +const uint32_t head =atomic_read (&r->cons_head);if (pvrdma_idx_valid (tail, max_elems) &&pvrdma_idx_valid (head, max_elems)) {
linux-headers/linux/iommu.h new file +395 -0
395 lines not on this page · raw message
linux-headers/linux/vfio.h +140 -1
188 lines not on this page · raw message
Signature · 1 line
2.26.2
RFC v8 16/28 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.
--- hw/vfio/common.c | 97 +++++++++++++++++++++++++++++++++++ hw/vfio/trace-events | 1 + include/hw/vfio/vfio-common.h | 7 +++ 3 files changed, 105 insertions(+)
hw/vfio/common.c +97 -0
@@ -1419,6 +1419,25 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,return true; } +struct vfio_info_cap_header * +vfio_get_irq_info_cap (struct vfio_irq_info *info,uint16_t id) +{ +struct vfio_info_cap_header *hdr; +void *ptr = info; + +if (!(info->flags &VFIO_IRQ_INFO_FLAG_CAPS )) { +return NULL ; + } + +for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) { +if (hdr->id == id) { +return hdr; + } + } + +return NULL ; +} +static int vfio_setup_region_sparse_mmaps (VFIORegion *region,struct vfio_region_info *info) {
@@ -2352,6 +2371,33 @@ retry:return 0 ; } +int vfio_get_irq_info (VFIODevice *vbasedev,int index, +struct vfio_irq_info **info) +{ +size_t argsz =sizeof (struct vfio_irq_info ); + + *info =g_malloc0 (argsz); + + (*info)->index = index; +retry: + (*info)->argsz = argsz; + +if (ioctl (vbasedev->fd,VFIO_DEVICE_GET_IRQ_INFO , *info)) { +g_free (*info); + *info =NULL ; +return -errno; + } + +if ((*info)->argsz > argsz) { + argsz = (*info)->argsz; + *info =g_realloc (*info, argsz); + + goto retry; + } + +return 0 ; +} +int vfio_get_dev_region_info (VFIODevice *vbasedev,uint32_t type,uint32_t subtype,struct vfio_region_info **info) {
@@ -2387,6 +2433,42 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,return -ENODEV ; } +int vfio_get_dev_irq_info (VFIODevice *vbasedev,uint32_t type, +uint32_t subtype,struct vfio_irq_info **info) +{ +int i; + +for (i =0 ; i < vbasedev->num_irqs; i++) { +struct vfio_info_cap_header *hdr; +struct vfio_irq_info_cap_type *cap_type; + +if (vfio_get_irq_info (vbasedev, i, info)) { +continue ; + } + + hdr =vfio_get_irq_info_cap (*info,VFIO_IRQ_INFO_CAP_TYPE ); +if (!hdr) { +g_free (*info); +continue ; + } + + cap_type = container_of(hdr, struct vfio_irq_info_cap_type, header); + +trace_vfio_get_dev_irq (vbasedev->name, i, + cap_type->type, cap_type->subtype); + +if (cap_type->type == type && cap_type->subtype == subtype) { +return 0 ; + } + +g_free (*info); + } + + *info =NULL ; +return -ENODEV ; +} + +bool vfio_has_region_cap (VFIODevice *vbasedev,int region,uint16_t cap_type) {struct vfio_region_info *info =NULL ;
@@ -2402,6 +2484,21 @@ bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type)return ret; } +bool vfio_has_irq_cap (VFIODevice *vbasedev,int region,uint16_t cap_type) +{ +struct vfio_region_info *info =NULL ; +bool ret = false; + +if (!vfio_get_region_info (vbasedev, region, &info)) { +if (vfio_get_region_info_cap (info, cap_type)) { + ret = true; + } +g_free (info); + } + +return ret; +} +/* * Interfaces for IBM EEH (Enhanced Error Handling) */
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"
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;
Signature · 1 line
2.26.2
RFC v8 7/28 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.2
RFC v8 18/28 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.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 1 + 2 files changed, 65 insertions(+)
hw/vfio/pci.c +64 -0
@@ -2600,11 +2600,67 @@ int vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)return 0 ; } +static void vfio_init_fault_regions (VFIOPCIDevice *vdev,Error **errp) +{ +struct vfio_region_info *fault_region_info =NULL ; +struct vfio_region_info_cap_fault *cap_fault; +VFIODevice *vbasedev = &vdev->vbasedev; +struct vfio_info_cap_header *hdr; +char *fault_region_name; +int ret; + + ret =vfio_get_dev_region_info (&vdev->vbasedev, +VFIO_REGION_TYPE_NESTED , +VFIO_REGION_SUBTYPE_NESTED_DMA_FAULT , + &fault_region_info); +if (ret) { + goto out; + } + + hdr =vfio_get_region_info_cap (fault_region_info, +VFIO_REGION_INFO_CAP_DMA_FAULT ); +if (!hdr) { +error_setg (errp,"failed to retrieve DMA FAULT capability" ); + goto out; + } + cap_fault = container_of(hdr, struct vfio_region_info_cap_fault, + header); +if (cap_fault->version !=1 ) { +error_setg (errp,"Unsupported DMA FAULT API version %d" , + cap_fault->version); + goto out; + } + + fault_region_name =g_strdup_printf ("%s DMA FAULT %d" , + vbasedev->name, + fault_region_info->index); + + ret =vfio_region_setup (OBJECT (vdev), vbasedev, + &vdev->dma_fault_region, + fault_region_info->index, + fault_region_name); +g_free (fault_region_name); +if (ret) { +error_setg_errno (errp, -ret, +"failed to set up the DMA FAULT region %d" , + fault_region_info->index); + goto out; + } + + ret =vfio_region_mmap (&vdev->dma_fault_region); +if (ret) { +error_setg_errno (errp, -ret,"Failed to mmap the DMA FAULT queue" ); + } +out: +g_free (fault_region_info); +} +static void vfio_populate_device (VFIOPCIDevice *vdev,Error **errp) {VFIODevice *vbasedev = &vdev->vbasedev;struct vfio_region_info *reg_info;struct vfio_irq_info irq_info = { .argsz =sizeof (irq_info) }; +Error *err =NULL ;int i, ret =-1 ;/* Sanity check device */
@@ -2668,6 +2724,12 @@ static void vfio_populate_device(VFIOPCIDevice *vdev, Error **errp) } } +vfio_init_fault_regions (vdev, &err); +if (err) { +error_propagate (errp, err); +return ; + } + irq_info.index =VFIO_PCI_ERR_IRQ_INDEX ; ret =ioctl (vdev->vbasedev.fd,VFIO_DEVICE_GET_IRQ_INFO , &irq_info);
@@ -3260,6 +3322,7 @@ static void vfio_instance_finalize(Object *obj)vfio_display_finalize (vdev);vfio_bars_finalize (vdev); +vfio_region_finalize (&vdev->dma_fault_region);g_free (vdev->emulated_config_bits);g_free (vdev->rom);/*
@@ -3280,6 +3343,7 @@ static void vfio_exitfn(PCIDevice *pdev)vfio_unregister_req_notifier (vdev);vfio_unregister_err_notifier (vdev);vfio_unregister_ext_irq_notifiers (vdev); +vfio_region_exit (&vdev->dma_fault_region);pci_device_set_intx_routing_notifier (&vdev->pdev,NULL );if (vdev->irqchip_change_notifier.notify) {kvm_irqchip_remove_change_notifier (&vdev->irqchip_change_notifier);
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;
Signature · 1 line
2.26.2
RFC v8 20/28 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
@@ -1587,6 +1587,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.2
RFC v8 8/28 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 ;
@@ -1496,6 +1511,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
@@ -2029,6 +2029,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.2
RFC v8 19/28 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.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 1 + 2 files changed, 51 insertions(+)
hw/vfio/pci.c +50 -0
@@ -2915,10 +2915,60 @@ static PCIPASIDOps vfio_pci_pasid_ops = {static void vfio_dma_fault_notifier_handler (void *opaque) {VFIOPCIExtIRQ *ext_irq = opaque; +VFIOPCIDevice *vdev = ext_irq->vdev; +PCIDevice *pdev = &vdev->pdev; +AddressSpace *as =pci_device_iommu_address_space (pdev); +IOMMUMemoryRegion *iommu_mr =IOMMU_MEMORY_REGION (as->root); +struct vfio_region_dma_fault header; +struct iommu_fault *queue; +char *queue_buffer =NULL ; +ssize_t bytes;if (!event_notifier_test_and_clear (&ext_irq->notifier)) {return ; } + + bytes =pread (vdev->vbasedev.fd, &header,sizeof (header), + vdev->dma_fault_region.fd_offset); +if (bytes !=sizeof (header)) { +error_report ("%s unable to read the fault region header (0x%lx)" , + __func__, bytes); +return ; + } + +/* Normally the fault queue is mmapped */ + queue = (struct iommu_fault *)vdev->dma_fault_region.mmaps[0 ].mmap; +if (!queue) { +size_t queue_size = header.nb_entries * header.entry_size; + +error_report ("%s: fault queue not mmapped: slower fault handling" , + vdev->vbasedev.name); + + queue_buffer =g_malloc (queue_size); + bytes =pread (vdev->vbasedev.fd, queue_buffer, queue_size, + vdev->dma_fault_region.fd_offset + header.offset); +if (bytes != queue_size) { +error_report ("%s unable to read the fault queue (0x%lx)" , + __func__, bytes); +return ; + } + + queue = (struct iommu_fault *)queue_buffer; + } + +while (vdev->fault_tail_index != header.head) { +memory_region_inject_faults (iommu_mr,1 , + &queue[vdev->fault_tail_index]); + vdev->fault_tail_index = + (vdev->fault_tail_index +1 ) % header.nb_entries; + } + bytes =pwrite (vdev->vbasedev.fd, &vdev->fault_tail_index,4 , + vdev->dma_fault_region.fd_offset); +if (bytes !=4 ) { +error_report ("%s unable to write the fault region tail index (0x%lx)" , + __func__, bytes); + } +g_free (queue_buffer); } static int vfio_register_ext_irq_handler(VFIOPCIDevice *vdev,
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;
Signature · 1 line
2.26.2
RFC v8 23/28 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); }
@@ -863,7 +864,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;
@@ -875,7 +876,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); } } }
@@ -913,7 +914,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.2
RFC v8 24/28 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.2
RFC v8 25/28 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
@@ -1650,6 +1650,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) {
@@ -1658,6 +1728,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.2
RFC v8 13/28 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.
---
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
@@ -667,13 +667,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 =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 )); + +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 ;
@@ -818,39 +931,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; }
@@ -884,10 +965,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(
@@ -926,49 +1003,7 @@ static void vfio_listener_region_del(MemoryListener *listener,*/ } - 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 )); - -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.2
RFC v8 21/28 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.
--- hw/arm/smmuv3.c | 1 + include/hw/arm/smmu-common.h | 1 + 2 files changed, 2 insertions(+)
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:
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;
Signature · 1 line
2.26.2
RFC v8 28/28 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.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 2 + 2 files changed, 125 insertions(+)
hw/vfio/pci.c +123 -0
@@ -2655,6 +2655,61 @@ out:g_free (fault_region_info); } +static void vfio_init_fault_response_regions (VFIOPCIDevice *vdev,Error **errp) +{ +struct vfio_region_info *fault_region_info =NULL ; +struct vfio_region_info_cap_fault *cap_fault; +VFIODevice *vbasedev = &vdev->vbasedev; +struct vfio_info_cap_header *hdr; +char *fault_region_name; +int ret; + + ret =vfio_get_dev_region_info (&vdev->vbasedev, +VFIO_REGION_TYPE_NESTED , +VFIO_REGION_SUBTYPE_NESTED_DMA_FAULT_RESPONSE , + &fault_region_info); +if (ret) { + goto out; + } + + hdr =vfio_get_region_info_cap (fault_region_info, +VFIO_REGION_INFO_CAP_DMA_FAULT_RESPONSE ); +if (!hdr) { +error_setg (errp,"failed to retrieve DMA FAULT RESPONSE capability" ); + goto out; + } + cap_fault = container_of(hdr, struct vfio_region_info_cap_fault, + header); +if (cap_fault->version !=1 ) { +error_setg (errp,"Unsupported DMA FAULT RESPONSE API version %d" , + cap_fault->version); + goto out; + } + + fault_region_name =g_strdup_printf ("%s DMA FAULT RESPONSE %d" , + vbasedev->name, + fault_region_info->index); + + ret =vfio_region_setup (OBJECT (vdev), vbasedev, + &vdev->dma_fault_response_region, + fault_region_info->index, + fault_region_name); +g_free (fault_region_name); +if (ret) { +error_setg_errno (errp, -ret, +"failed to set up the DMA FAULT RESPONSE region %d" , + fault_region_info->index); + goto out; + } + + ret =vfio_region_mmap (&vdev->dma_fault_response_region); +if (ret) { +error_setg_errno (errp, -ret,"Failed to mmap the DMA FAULT RESPONSE queue" ); + } +out: +g_free (fault_region_info); +} +static void vfio_populate_device (VFIOPCIDevice *vdev,Error **errp) {VFIODevice *vbasedev = &vdev->vbasedev;
@@ -2730,6 +2785,12 @@ static void vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)return ; } +vfio_init_fault_response_regions (vdev, &err); +if (err) { +error_propagate (errp, err); +return ; + } + irq_info.index =VFIO_PCI_ERR_IRQ_INDEX ; ret =ioctl (vdev->vbasedev.fd,VFIO_DEVICE_GET_IRQ_INFO , &irq_info);
@@ -2908,8 +2969,68 @@ static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn,return ioctl (container->fd,VFIO_IOMMU_SET_PASID_TABLE , &info); } +static int vfio_iommu_return_page_response (PCIBus *bus,int32_t devfn, +IOMMUPageResponse *resp) +{ +PCIDevice *pdev = bus->devices[devfn]; +VFIOPCIDevice *vdev =DO_UPCAST (VFIOPCIDevice, pdev, pdev); +struct iommu_page_response *response = &resp->resp; +struct vfio_region_dma_fault_response header; +struct iommu_page_response *queue; +char *queue_buffer =NULL ; +ssize_t bytes; + +if (!vdev->dma_fault_response_region.mem) { +return -EINVAL ; + } + +/* read the header */ + bytes =pread (vdev->vbasedev.fd, &header,sizeof (header), + vdev->dma_fault_response_region.fd_offset); +if (bytes !=sizeof (header)) { +error_report ("%s unable to read the fault region header (0x%lx)" , + __func__, bytes); +return -1 ; + } + +/* Normally the fault queue is mmapped */ + queue = (struct iommu_page_response *)vdev->dma_fault_response_region.mmaps[0 ].mmap; +if (!queue) { +size_t queue_size = header.nb_entries * header.entry_size; + +error_report ("%s: fault queue not mmapped: slower fault handling" , + vdev->vbasedev.name); + + queue_buffer =g_malloc (queue_size); + bytes =pread (vdev->vbasedev.fd, queue_buffer, queue_size, + vdev->dma_fault_response_region.fd_offset + header.offset); +if (bytes != queue_size) { +error_report ("%s unable to read the fault queue (0x%lx)" , + __func__, bytes); +return -1 ; + } + + queue = (struct iommu_page_response *)queue_buffer; + } +/* deposit the new response in the queue and increment the head */ +memcpy (queue + header.head, response, header.entry_size); + + vdev->fault_response_head_index = + (vdev->fault_response_head_index +1 ) % header.nb_entries; + bytes =pwrite (vdev->vbasedev.fd, &vdev->fault_response_head_index,4 , + vdev->dma_fault_response_region.fd_offset); +if (bytes !=4 ) { +error_report ("%s unable to write the fault response region head index (0x%lx)" , + __func__, bytes); + } +g_free (queue_buffer); + +return 0 ; +} +static PCIPASIDOps vfio_pci_pasid_ops = { .set_pasid_table = vfio_iommu_set_pasid_table, + .return_page_response = vfio_iommu_return_page_response, };static void vfio_dma_fault_notifier_handler (void *opaque)
@@ -3373,6 +3494,7 @@ static void vfio_instance_finalize(Object *obj)vfio_display_finalize (vdev);vfio_bars_finalize (vdev);vfio_region_finalize (&vdev->dma_fault_region); +vfio_region_finalize (&vdev->dma_fault_response_region);g_free (vdev->emulated_config_bits);g_free (vdev->rom);/*
@@ -3394,6 +3516,7 @@ static void vfio_exitfn(PCIDevice *pdev)vfio_unregister_err_notifier (vdev);vfio_unregister_ext_irq_notifiers (vdev);vfio_region_exit (&vdev->dma_fault_region); +vfio_region_exit (&vdev->dma_fault_response_region);pci_device_set_intx_routing_notifier (&vdev->pdev,NULL );if (vdev->irqchip_change_notifier.notify) {kvm_irqchip_remove_change_notifier (&vdev->irqchip_change_notifier);
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;
Signature · 1 line
2.26.2
RFC v8 22/28 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.2
RFC v8 17/28 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.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++- hw/vfio/pci.h | 7 +++++ 2 files changed, 87 insertions(+), 1 deletion(-)
hw/vfio/pci.c +80 -1
@@ -2850,6 +2850,76 @@ static PCIPASIDOps vfio_pci_pasid_ops = { .set_pasid_table = vfio_iommu_set_pasid_table, }; +static void vfio_dma_fault_notifier_handler (void *opaque) +{ +VFIOPCIExtIRQ *ext_irq = opaque; + +if (!event_notifier_test_and_clear (&ext_irq->notifier)) { +return ; + } +} + +static int vfio_register_ext_irq_handler (VFIOPCIDevice *vdev, +uint32_t type,uint32_t subtype, +IOHandler *handler) +{ +int32_t fd, ext_irq_index, index; +struct vfio_irq_info *irq_info; +Error *err =NULL ; +EventNotifier *n; +int ret; + + ret =vfio_get_dev_irq_info (&vdev->vbasedev, type, subtype, &irq_info); +if (ret) { +return ret; + } + index = irq_info->index; + ext_irq_index = irq_info->index -VFIO_PCI_NUM_IRQS ; +g_free (irq_info); + + vdev->ext_irqs[ext_irq_index].vdev = vdev; + vdev->ext_irqs[ext_irq_index].index = index; + n = &vdev->ext_irqs[ext_irq_index].notifier; + + ret =event_notifier_init (n,0 ); +if (ret) { +error_report ("vfio: Unable to init event notifier for ext irq %d(%d)" , + ext_irq_index, ret); +return ret; + } + + fd =event_notifier_get_fd (n); +qemu_set_fd_handler (fd, vfio_dma_fault_notifier_handler,NULL , + &vdev->ext_irqs[ext_irq_index]); + + ret =vfio_set_irq_signaling (&vdev->vbasedev, index,0 , +VFIO_IRQ_SET_ACTION_TRIGGER , fd, &err); +if (ret) { +error_reportf_err (err,VFIO_MSG_PREFIX , vdev->vbasedev.name); +qemu_set_fd_handler (fd,NULL ,NULL , vdev); +event_notifier_cleanup (n); + } +return ret; +} + +static void vfio_unregister_ext_irq_notifiers (VFIOPCIDevice *vdev) +{ +VFIODevice *vbasedev = &vdev->vbasedev; +Error *err =NULL ; +int i; + +for (i =0 ; i < vbasedev->num_irqs -VFIO_PCI_NUM_IRQS ; i++) { +if (vfio_set_irq_signaling (vbasedev, i +VFIO_PCI_NUM_IRQS ,0 , +VFIO_IRQ_SET_ACTION_TRIGGER ,-1 , &err)) { +error_reportf_err (err,VFIO_MSG_PREFIX , vdev->vbasedev.name); + } +qemu_set_fd_handler (event_notifier_get_fd (&vdev->ext_irqs[i].notifier), +NULL ,NULL , vdev); +event_notifier_cleanup (&vdev->ext_irqs[i].notifier); + } +g_free (vdev->ext_irqs); +} +static void vfio_realize (PCIDevice *pdev,Error **errp) {VFIOPCIDevice *vdev =VFIO_PCI (pdev);
@@ -2860,7 +2930,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)ssize_t len;struct stat st;int groupid; -int i, ret; +int i, ret, nb_ext_irqs;bool is_mdev;if (!vdev->vbasedev.sysfsdev) {
@@ -2948,6 +3018,11 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) goto error; } + nb_ext_irqs = vdev->vbasedev.num_irqs -VFIO_PCI_NUM_IRQS ; +if (nb_ext_irqs >0 ) { + vdev->ext_irqs =g_new0 (VFIOPCIExtIRQ, nb_ext_irqs); + } +vfio_populate_device (vdev, &err);if (err) {error_propagate (errp, err);
@@ -3159,6 +3234,9 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)vfio_register_err_notifier (vdev);vfio_register_req_notifier (vdev); +vfio_register_ext_irq_handler (vdev,VFIO_IRQ_TYPE_NESTED , +VFIO_IRQ_SUBTYPE_DMA_FAULT , + vfio_dma_fault_notifier_handler);vfio_setup_resetfn_quirk (vdev);pci_setup_pasid_ops (pdev, &vfio_pci_pasid_ops);
@@ -3201,6 +3279,7 @@ static void vfio_exitfn(PCIDevice *pdev)vfio_unregister_req_notifier (vdev);vfio_unregister_err_notifier (vdev); +vfio_unregister_ext_irq_notifiers (vdev);pci_device_set_intx_routing_notifier (&vdev->pdev,NULL );if (vdev->irqchip_change_notifier.notify) {kvm_irqchip_remove_change_notifier (&vdev->irqchip_change_notifier);
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;
Signature · 1 line
2.26.2
RFC v8 26/28 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
@@ -1618,14 +1618,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.2
RFC v8 11/28 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
@@ -1546,27 +1546,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; }
@@ -1671,6 +1682,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);
@@ -1740,13 +1759,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.2
RFC v8 14/28 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
@@ -600,6 +600,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);
@@ -781,6 +848,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) {
@@ -883,9 +979,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);/*
@@ -904,8 +1001,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);
@@ -925,7 +1032,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 ; }
@@ -1172,10 +1281,16 @@ static const MemoryListener vfio_memory_listener = { .log_sync = vfio_listerner_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); } }
@@ -1825,6 +1940,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
@@ -2759,6 +2759,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);
@@ -3070,6 +3089,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.2
RFC v8 27/28 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
--- hw/pci/pci.c | 16 ++++++++++++++++ include/hw/iommu/iommu.h | 8 ++++++++ include/hw/pci/pci.h | 4 ++++ 3 files changed, 28 insertions(+)
hw/pci/pci.c +16 -0
@@ -2758,6 +2758,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;
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 ;
@@ -500,6 +502,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)
Signature · 1 line
2.26.2
Re: [RFC v8 03/28] header update against 5.11-rc2 and IOMMU/VFIO nested stage APIs
In reply to Eric AugerQuoted 44 lines · Eric Auger
Signed-off-by Eric Auger <[email protected]>--- .../infiniband/hw/vmw_pvrdma/pvrdma_ring.h | 14 +- linux-headers/linux/iommu.h | 395 ++++++++++++++++++ linux-headers/linux/vfio.h | 141 ++++++- 3 files changed, 542 insertions(+), 8 deletions(-)create mode 100644
linux-headers/linux/iommu.hinclude/standard-headers/drivers/infiniband/hw/vmw_pvrdma/pvrdma_ring.h +5
-5@@ -68,7 +68,7 @@ static inline int pvrdma_idx_valid(uint32_t idx, uint32_t max_elems)static inline int32_t pvrdma_idx (int *var,uint32_t max_elems) { -const unsigned int idx =qatomic_read (var); +const unsigned int idx =atomic_read (var);if (pvrdma_idx_valid (idx, max_elems))return idx & (max_elems -1 );@@ -77,17 +77,17 @@ static inline int32_t pvrdma_idx(int *var, uint32_t max_elems)static inline void pvrdma_idx_ring_inc (int *var,uint32_t max_elems) { -uint32_t idx =qatomic_read (var) +1 ;/* Increment. */ +uint32_t idx =atomic_read (var) +1 ;/* Increment. */ idx &= (max_elems <<1 ) -1 ;/* Modulo size, flip gen. */ -qatomic_set (var, idx); +atomic_set (var, idx); }static inline int32_t pvrdma_idx_ring_has_space (const struct pvrdma_ring *r,uint32_t max_elems,uint32_t *out_tail) { -const uint32_t tail =qatomic_read (&r->prod_tail); -const uint32_t head =qatomic_read (&r->cons_head); +const uint32_t tail =atomic_read (&r->prod_tail); +const uint32_t head =atomic_read (&r->cons_head);if (pvrdma_idx_valid (tail, max_elems) &&pvrdma_idx_valid (head, max_elems)) {
include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/pvrdma_ring.h static inline int32_t pvrdma_idx_ring_has_space(const struct pvrdma_ring *r, · line 107
Quoted 10 lines
include/standard-headers/drivers/infiniband/hw/vmw_pvrdma/pvrdma_ring.h +2
-2@@ -100,8 +100,8 @@ static inline int32_t pvrdma_idx_ring_has_space(const struct pvrdma_ring *r,static inline int32_t pvrdma_idx_ring_has_data (const struct pvrdma_ring *r,uint32_t max_elems,uint32_t *out_head) { -const uint32_t tail =qatomic_read (&r->prod_tail); -const uint32_t head =qatomic_read (&r->cons_head); +const uint32_t tail =atomic_read (&r->prod_tail); +const uint32_t head =atomic_read (&r->cons_head);if (pvrdma_idx_valid (tail, max_elems) &&pvrdma_idx_valid (head, max_elems)) {
The atomic_ has been renamed to qatomic_ in the patch d73415a3154. It seems that the pvrdma_ring.h doesn't need to be updated.
Best Regards.
Kunkun Jiang
Quoted 5 lines
linux-headers/linux/iommu.h new file +0
-0
[...]
Quoted 3 lines
/* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */
/*