vhost device IOTLB support
v4 cover vhost device IOTLB support
Hi all:
As the userspace vitio driver became popular, more and more request were received for secure DMA environemt (DMAR). So this series tries to make DMAR works for virtio/vhost. The idea is let virtio/vhost co-work with userspace iommu implememtation. This is done through:
- for virtio, when platform supports IOMMU (VIRTIO_F_IOMMU_PLATFORM), virtio will not assume address_space_memory, instead a transport specific method were introduced for querying the dma address space and dma helpers were used in device emulation codes.
- for vhost, implement a device IOTLB by using device IOTLB API supported by recent kernel. With this API, vhost kernel can query IOTLB entry for a specified iova from qemu, qemu can invalidate an arbitrary range of iova in vhost kernel.
The device IOTLB API is totaly architecture independent, an example implementation was done with intel iommu by:
- implement basic ATS (Address Translation Service) for virtio-pci, this will make device IOTLB visible for iommu driver in guest.
- implement device IOTLB descriptor processing in intel iommu (enabled through device-iotlb=on), and trigger the device IOTLB invalidation in vhost through iommu notifier.
It could be easily ported to other IOMMU or architecture even if it doesn't support device IOTLB. (e.g just invalidate the vhost IOTLB during IOMMU IOTLB invalidation). But this will be slow since several devics were contending userspace IOTLB entries.
AMD IOMMU suppot for device IOTLB is ready, but it depends on other fixes to work correctly.
Test was done by:
- intel_iommu=on/strict in guest.
- vfio l2fwd in guest.
This main use case is the programs that use fixed mapping in guest (e.g dpdk). If 1G hugepage were used in guest, thanks to the SLLPS support, we can get 100% TLB hit rate for l2fwd in guest.
For the normal kernel driver which uses lots of dynamic mapping and unmapping, we may see performance penalty, this could be optimized in the future.
TODO:
- more platforms and IOMMU support (done but block by other bugs of AMD IOMMU)
- performance optimizations (e.g merging adjacent mappings)
- non ATS support (userspace IOTLB snooping)
- using ATSCtl to disable Device IOTLB
Changes from V3:
- rebase to HEAD
- fill all entry of IOMMUTLBEntry before notify
Changes from V2:
- rebase to HEAD
- avoid querying dma_as each time by using vdev->dma_as directly
- fix and improve address_space_get_iotlb_entry()
- drop patch 1 (which has been posted as an independent fix)
- fix ECAP when device-iotlb=off
- fix centos6 build
Changes from V1:
- rebase to HEAD
- avoid calling transport specific dma as fetching method each time by caching it in vdev
- convert to use new IOMMU notifier API
- silent checkpatch warnings and fix 32bit build
- use "device-iotlb" instead of "device_iotlb"
- rename virtio_memory_map() to vhost_memory_map() and move it to vhost.c
- use memory_region_is_iommu() instead of inventing new one
Changes from RFC:
- rebase to HEAD
- switch to use new vhost device IOTLB API
- use the new feature bit VIRITO_F_IOMMU_PLATFORM
- finalize basic ATS implementation
- add ATSR for Root port ATS transaction
- fix the iommu notifier handling during unregistering
- use snprintf() in patch 3
- correc the loop in address_space_get_iotlb_entry()
- small tweak on the address calculation during device iotlb descriptor processing.
Jason Wang (10):
virtio: convert to use DMA api
intel_iommu: name vtd address space with devfn
intel_iommu: allocate new key when creating new address space
exec: introduce address_space_get_iotlb_entry()
intel_iommu: support device iotlb descriptor
virtio-pci: address space translation service (ATS) support
acpi: add ATSR for q35
memory: handle alias for iommu notifier
memory: handle alias in memory_region_is_iommu()
vhost_net: device IOTLB support
exec.c | 33 ++++++ hw/block/virtio-blk.c | 2 +- hw/char/virtio-serial-bus.c | 3 +- hw/i386/acpi-build.c | 9 ++ hw/i386/intel_iommu.c | 92 +++++++++++++-- hw/i386/intel_iommu_internal.h | 13 ++- hw/i386/x86-iommu.c | 17 +++ hw/pci/pcie.c | 16 +++ hw/scsi/virtio-scsi.c | 4 +- hw/virtio/vhost-backend.c | 99 +++++++++++++++++ hw/virtio/vhost.c | 178 ++++++++++++++++++++++++++---- hw/virtio/virtio-bus.c | 8 ++ hw/virtio/virtio-pci.c | 21 ++++ hw/virtio/virtio-pci.h | 4 + hw/virtio/virtio.c | 57 ++++++---- include/exec/memory.h | 8 ++ include/hw/acpi/acpi-defs.h | 12 ++ include/hw/i386/x86-iommu.h | 1 + include/hw/pci/pcie.h | 4 + include/hw/virtio/vhost-backend.h | 13 +++ include/hw/virtio/vhost.h | 4 + include/hw/virtio/virtio-access.h | 31 ++++-- include/hw/virtio/virtio-bus.h | 1 + include/hw/virtio/virtio.h | 9 +- include/standard-headers/linux/pci_regs.h | 1 + memory.c | 9 ++ net/tap.c | 1 + 27 files changed, 582 insertions(+), 68 deletions(-)
Signature · 1 line
2.7.4
PATCH v4 1/10 virtio: convert to use DMA api
Currently, all virtio devices bypass IOMMU completely. This is because address_space_memory is assumed and used during DMA emulation. This patch converts the virtio core API to use DMA API. This idea is
- introducing a new transport specific helper to query the dma address space. (only pci version is implemented).
- query and use this address space during virtio device guest memory accessing when iommu platform (VIRTIO_F_IOMMU_PLATFORM) was enabled for this device.
--- hw/block/virtio-blk.c | 2 +- hw/char/virtio-serial-bus.c | 3 ++- hw/scsi/virtio-scsi.c | 4 ++- hw/virtio/virtio-bus.c | 8 ++++++ hw/virtio/virtio-pci.c | 14 ++++++++++ hw/virtio/virtio.c | 57 +++++++++++++++++++++++++-------------- include/hw/virtio/virtio-access.h | 31 ++++++++++++++------- include/hw/virtio/virtio-bus.h | 1 + include/hw/virtio/virtio.h | 9 ++++--- 9 files changed, 93 insertions(+), 36 deletions(-)
hw/block/virtio-blk.c +1 -1
@@ -857,7 +857,7 @@ static int virtio_blk_load_device(VirtIODevice *vdev, QEMUFile *f, } } - req =qemu_get_virtqueue_element (f,sizeof (VirtIOBlockReq)); + req =qemu_get_virtqueue_element (vdev, f,sizeof (VirtIOBlockReq));virtio_blk_init_request (s,virtio_get_queue (vdev, vq_idx), req); req->next = s->rq; s->rq = req;
hw/char/virtio-serial-bus.c +2 -1
@@ -732,6 +732,7 @@ static void virtio_serial_post_load_timer_cb(void *opaque)static int fetch_active_ports_list (QEMUFile *f,VirtIOSerial *s,uint32_t nr_active_ports) { +VirtIODevice *vdev =VIRTIO_DEVICE (s);uint32_t i; s->post_load =g_malloc0 (sizeof (*s->post_load));
@@ -765,7 +766,7 @@ static int fetch_active_ports_list(QEMUFile *f,qemu_get_be64s (f, &port->iov_offset); port->elem = -qemu_get_virtqueue_element (f,sizeof (VirtQueueElement)); +qemu_get_virtqueue_element (vdev, f,sizeof (VirtQueueElement));/* * Port was throttled on source machine. Let's
hw/scsi/virtio-scsi.c +3 -1
@@ -198,12 +198,14 @@ static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)SCSIBus *bus = sreq->bus;VirtIOSCSI *s =container_of (bus, VirtIOSCSI, bus);VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (s); +VirtIODevice *vdev =VIRTIO_DEVICE (s);VirtIOSCSIReq *req;uint32_t n;qemu_get_be32s (f, &n);assert (n < vs->conf.num_queues); - req =qemu_get_virtqueue_element (f,sizeof (VirtIOSCSIReq) + vs->cdb_size); + req =qemu_get_virtqueue_element (vdev, f, +sizeof (VirtIOSCSIReq) + vs->cdb_size);virtio_scsi_init_req (s, vs->cmd_vqs[n], req); if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
hw/virtio/virtio-bus.c +8 -0
@@ -28,6 +28,7 @@#include "hw/qdev.h" #include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio.h" +#include "exec/address-spaces.h" /* #define DEBUG_VIRTIO_BUS */
@@ -61,6 +62,13 @@ void virtio_bus_device_plugged(VirtIODevice *vdev, Error **errp)if (klass->device_plugged !=NULL ) { klass->device_plugged (qbus->parent, errp); } + +if (klass->get_dma_as !=NULL && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM )) { + vdev->dma_as = klass->get_dma_as (qbus->parent); + }else { + vdev->dma_as = &address_space_memory; + } }/* Reset the virtio_bus */
hw/virtio/virtio-pci.c +14 -0
@@ -1144,6 +1144,14 @@ static int virtio_pci_query_nvectors(DeviceState *d)return proxy->nvectors; } +static AddressSpace *virtio_pci_get_dma_as (DeviceState *d) +{ +VirtIOPCIProxy *proxy =VIRTIO_PCI (d); +PCIDevice *dev = &proxy->pci_dev; + +return pci_get_address_space (dev); +} +static int virtio_pci_add_mem_cap (VirtIOPCIProxy *proxy,struct virtio_pci_cap *cap) {
@@ -1601,6 +1609,11 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp) }if (legacy) { +if (virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM )) { +error_setg (errp,"VIRTIO_F_IOMMU_PLATFORM was supported by" +"neither legacy nor transitional device." ); +return ; + }/* legacy and transitional */ pci_set_word (config +PCI_SUBSYSTEM_VENDOR_ID ,pci_get_word (config +PCI_VENDOR_ID ));
@@ -2520,6 +2533,7 @@ static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) k->query_nvectors = virtio_pci_query_nvectors; k->ioeventfd_enabled = virtio_pci_ioeventfd_enabled; k->ioeventfd_assign = virtio_pci_ioeventfd_assign; + k->get_dma_as = virtio_pci_get_dma_as; }static const TypeInfo virtio_pci_bus_info = {
hw/virtio/virtio.c +37 -20
135 lines not on this page · raw message
include/hw/virtio/virtio-access.h +21 -10
@@ -17,6 +17,7 @@#define QEMU_VIRTIO_ACCESS_H #include "hw/virtio/virtio.h" +#include "hw/virtio/virtio-bus.h" #include "exec/address-spaces.h" #if defined(TARGET_PPC64 ) || defined(TARGET_ARM )
@@ -40,45 +41,55 @@ static inline bool virtio_access_is_big_endian(VirtIODevice *vdev)static inline uint16_t virtio_lduw_phys (VirtIODevice *vdev,hwaddr pa) { +AddressSpace *dma_as = vdev->dma_as; +if (virtio_access_is_big_endian (vdev)) { -return lduw_be_phys (&address_space_memory, pa); +return lduw_be_phys (dma_as, pa); } -return lduw_le_phys (&address_space_memory, pa); +return lduw_le_phys (dma_as, pa); }static inline uint32_t virtio_ldl_phys (VirtIODevice *vdev,hwaddr pa) { +AddressSpace *dma_as = vdev->dma_as; +if (virtio_access_is_big_endian (vdev)) { -return ldl_be_phys (&address_space_memory, pa); +return ldl_be_phys (dma_as, pa); } -return ldl_le_phys (&address_space_memory, pa); +return ldl_le_phys (dma_as, pa); }static inline uint64_t virtio_ldq_phys (VirtIODevice *vdev,hwaddr pa) { +AddressSpace *dma_as = vdev->dma_as; +if (virtio_access_is_big_endian (vdev)) { -return ldq_be_phys (&address_space_memory, pa); +return ldq_be_phys (dma_as, pa); } -return ldq_le_phys (&address_space_memory, pa); +return ldq_le_phys (dma_as, pa); }static inline void virtio_stw_phys (VirtIODevice *vdev,hwaddr pa,uint16_t value) { +AddressSpace *dma_as = vdev->dma_as; +if (virtio_access_is_big_endian (vdev)) { -stw_be_phys (&address_space_memory, pa, value); +stw_be_phys (dma_as, pa, value); }else { -stw_le_phys (&address_space_memory, pa, value); +stw_le_phys (dma_as, pa, value); } }static inline void virtio_stl_phys (VirtIODevice *vdev,hwaddr pa,uint32_t value) { +AddressSpace *dma_as = vdev->dma_as; +if (virtio_access_is_big_endian (vdev)) { -stl_be_phys (&address_space_memory, pa, value); +stl_be_phys (dma_as, pa, value); }else { -stl_le_phys (&address_space_memory, pa, value); +stl_le_phys (dma_as, pa, value); } }
include/hw/virtio/virtio-bus.h +1 -0
@@ -88,6 +88,7 @@ typedef struct VirtioBusClass {* Note that changing this will break migration for this transport. */ bool has_variable_vring_alignment; + AddressSpace *(*get_dma_as)(DeviceState *d); } VirtioBusClass;struct VirtioBusState {
include/hw/virtio/virtio.h +6 -3
@@ -92,6 +92,7 @@ struct VirtIODevicechar *bus_name;uint8_t device_endian;bool use_guest_notifier_mask; +AddressSpace *dma_as; QLIST_HEAD(, VirtQueue) *vector_queues; };
@@ -170,9 +171,9 @@ bool virtqueue_rewind(VirtQueue *vq, unsigned int num);void virtqueue_fill (VirtQueue *vq,const VirtQueueElement *elem,unsigned int len,unsigned int idx); -void virtqueue_map (VirtQueueElement *elem); +void virtqueue_map (VirtIODevice *vdev,VirtQueueElement *elem);void *virtqueue_pop (VirtQueue *vq,size_t sz); -void *qemu_get_virtqueue_element (QEMUFile *f,size_t sz); +void *qemu_get_virtqueue_element (VirtIODevice *vdev,QEMUFile *f,size_t sz);void qemu_put_virtqueue_element (QEMUFile *f,VirtQueueElement *elem);int virtqueue_avail_bytes (VirtQueue *vq,unsigned int in_bytes,unsigned int out_bytes);
@@ -255,7 +256,9 @@ typedef struct VirtIORNGConf VirtIORNGConf;DEFINE_PROP_BIT64 ("notify_on_empty" , _state, _field, \VIRTIO_F_NOTIFY_ON_EMPTY , true), \DEFINE_PROP_BIT64 ("any_layout" , _state, _field, \ - VIRTIO_F_ANY_LAYOUT, true) +VIRTIO_F_ANY_LAYOUT , true), \ +DEFINE_PROP_BIT64 ("iommu_platform" , _state, _field, \ + VIRTIO_F_IOMMU_PLATFORM, false)hwaddr virtio_queue_get_desc_addr (VirtIODevice *vdev,int n);hwaddr virtio_queue_get_avail_addr (VirtIODevice *vdev,int n);
Signature · 1 line
2.7.4
PATCH v4 2/10 intel_iommu: name vtd address space with devfn
In reply to Jason WangTo avoid duplicated name and ease debugging.
--- hw/i386/intel_iommu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/i386/intel_iommu.c +3 -1
@@ -2324,6 +2324,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)uintptr_t key = (uintptr_t )bus;VTDBus *vtd_bus =g_hash_table_lookup (s->vtd_as_by_busptr, &key);VTDAddressSpace *vtd_dev_as; +char name[128 ];if (!vtd_bus) {/* No corresponding free() */
@@ -2337,6 +2338,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn) vtd_dev_as = vtd_bus->dev_as[devfn];if (!vtd_dev_as) { +snprintf (name,sizeof (name),"intel_iommu_devfn_%d" , devfn); vtd_bus->dev_as[devfn] = vtd_dev_as =g_malloc0 (sizeof (VTDAddressSpace)); vtd_dev_as->bus = bus;
@@ -2351,7 +2353,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)memory_region_add_subregion (&vtd_dev_as->iommu,VTD_INTERRUPT_ADDR_FIRST , &vtd_dev_as->iommu_ir);address_space_init (&vtd_dev_as->as, - &vtd_dev_as->iommu,"intel_iommu" ); + &vtd_dev_as->iommu, name); }return vtd_dev_as; }
Signature · 1 line
2.7.4
PATCH v4 3/10 intel_iommu: allocate new key when creating new address space
In reply to Jason WangWe use the pointer to stack for key for new address space, this will break hash table searching, fixing by g_malloc() a new key instead.
--- hw/i386/intel_iommu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +3 -2
@@ -2327,12 +2327,13 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)char name[128 ];if (!vtd_bus) { +uintptr_t *new_key =g_malloc (sizeof (*new_key)); + *new_key = (uintptr_t )bus;/* No corresponding free() */ vtd_bus =g_malloc0 (sizeof (VTDBus) +sizeof (VTDAddressSpace *) * \X86_IOMMU_PCI_DEVFN_MAX ); vtd_bus->bus = bus; - key = (uintptr_t )bus; -g_hash_table_insert (s->vtd_as_by_busptr, &key, vtd_bus); +g_hash_table_insert (s->vtd_as_by_busptr, new_key, vtd_bus); } vtd_dev_as = vtd_bus->dev_as[devfn];
Signature · 1 line
2.7.4
PATCH v4 4/10 exec: introduce address_space_get_iotlb_entry()
In reply to Jason WangThis patch introduces a helper to query the iotlb entry for a possible iova. This will be used by later device IOTLB API to enable the capability for a dataplane (e.g vhost) to query the IOTLB.
--- exec.c | 33 +++++++++++++++++++++++++++++++++ include/exec/memory.h | 5 +++++ 2 files changed, 38 insertions(+)
exec.c +33 -0
@@ -449,6 +449,39 @@ address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *x }/* Called from RCU critical section */ +IOMMUTLBEntry address_space_get_iotlb_entry (AddressSpace *as,hwaddr addr, +bool is_write) +{ +IOMMUTLBEntry iotlb = {0 }; +MemoryRegionSection *section; +MemoryRegion *mr; + +for (;;) { +AddressSpaceDispatch *d =atomic_rcu_read (&as->dispatch); + section =address_space_lookup_region (d, addr, false); + addr = addr - section->offset_within_address_space + + section->offset_within_region; + mr = section->mr; + +if (!mr->iommu_ops) { +break ; + } + + iotlb = mr->iommu_ops->translate (mr, addr, is_write); +if (!(iotlb.perm & (1 << is_write))) { + iotlb.target_as =NULL ; +break ; + } + + addr = ((iotlb.translated_addr & ~iotlb.addr_mask) + | (addr & iotlb.addr_mask)); + as = iotlb.target_as; + } + +return iotlb; +} + +/* Called from RCU critical section */ MemoryRegion *address_space_translate (AddressSpace *as,hwaddr addr,hwaddr *xlat,hwaddr *plen,bool is_write)
include/exec/memory.h +5 -0
@@ -1537,6 +1537,11 @@ void stl_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val);void stl_be_phys_cached (MemoryRegionCache *cache,hwaddr addr,uint32_t val);void stq_le_phys_cached (MemoryRegionCache *cache,hwaddr addr,uint64_t val);void stq_be_phys_cached (MemoryRegionCache *cache,hwaddr addr,uint64_t val); +/* address_space_get_iotlb_entry: translate an address into an IOTLB +* entry. Should be called from an RCU critical section. +*/ +IOMMUTLBEntry address_space_get_iotlb_entry (AddressSpace *as,hwaddr addr, +bool is_write);/* address_space_translate: translate an address range into an address space * into a MemoryRegion and an address range into that section. Should be
Signature · 1 line
2.7.4
PATCH v4 5/10 intel_iommu: support device iotlb descriptor
In reply to Jason WangThis patch enables device IOTLB support for intel iommu. The major work is to implement QI device IOTLB descriptor processing and notify the device through iommu notifier.
--- hw/i386/intel_iommu.c | 83 +++++++++++++++++++++++++++++++++++++++--- hw/i386/intel_iommu_internal.h | 13 ++++++- hw/i386/x86-iommu.c | 17 +++++++++ include/hw/i386/x86-iommu.h | 1 + 4 files changed, 107 insertions(+), 7 deletions(-)
hw/i386/intel_iommu.c +78 -5
@@ -738,11 +738,18 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,"context-entry hi 0x%" PRIx64" lo 0x%" PRIx64, ce->hi, ce->lo);return -VTD_FR_CONTEXT_ENTRY_INV ; - }else if (ce->lo &VTD_CONTEXT_ENTRY_TT ) { -VTD_DPRINTF (GENERAL ,"error: unsupported Translation Type in " -"context-entry hi 0x%" PRIx64" lo 0x%" PRIx64, - ce->hi, ce->lo); -return -VTD_FR_CONTEXT_ENTRY_INV ; + }else { +switch (ce->lo &VTD_CONTEXT_ENTRY_TT ) { +case VTD_CONTEXT_TT_MULTI_LEVEL : +/* fall through */ +case VTD_CONTEXT_TT_DEV_IOTLB : +break ; +default : +VTD_DPRINTF (GENERAL ,"error: unsupported Translation Type in " +"context-entry hi 0x%" PRIx64" lo 0x%" PRIx64, + ce->hi, ce->lo); +return -VTD_FR_CONTEXT_ENTRY_INV ; + } }return 0 ; }
@@ -1438,7 +1445,61 @@ static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,vtd_iec_notify_all (s, !inv_desc->iec.granularity, inv_desc->iec.index, inv_desc->iec.index_mask); +return true; +} + +static bool vtd_process_device_iotlb_desc (IntelIOMMUState *s, +VTDInvDesc *inv_desc) +{ +VTDAddressSpace *vtd_dev_as; +IOMMUTLBEntry entry; +struct VTDBus *vtd_bus; +hwaddr addr; +uint64_t sz; +uint16_t sid; +uint8_t devfn; +bool size; +uint8_t bus_num; + + addr =VTD_INV_DESC_DEVICE_IOTLB_ADDR (inv_desc->hi); + sid =VTD_INV_DESC_DEVICE_IOTLB_SID (inv_desc->lo); + devfn = sid &0xff ; + bus_num = sid >>8 ; + size =VTD_INV_DESC_DEVICE_IOTLB_SIZE (inv_desc->hi); + +if ((inv_desc->lo &VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO ) || + (inv_desc->hi &VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI )) { +VTD_DPRINTF (GENERAL ,"error: non-zero reserved field in Device " +"IOTLB Invalidate Descriptor hi 0x%" PRIx64" lo 0x%" PRIx64, + inv_desc->hi, inv_desc->lo); +return false; + } + + vtd_bus =vtd_find_as_from_bus_num (s, bus_num); +if (!vtd_bus) { + goto done; + } + + vtd_dev_as = vtd_bus->dev_as[devfn]; +if (!vtd_dev_as) { + goto done; + } + +if (size) { + sz =1 << (ctz64 (~(addr | (VTD_PAGE_MASK_4K -1 ))) +1 ); + addr &= ~(sz -1 ); + }else { + sz =VTD_PAGE_SIZE ; + } + entry.target_as = &vtd_dev_as->as; + entry.addr_mask = sz -1 ; + entry.iova = addr; + entry.perm =IOMMU_NONE ; + entry.translated_addr =0 ; +memory_region_notify_iommu (entry.target_as->root, entry); + +done:return true; }
@@ -1490,6 +1551,14 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s) }break ; +case VTD_INV_DESC_DEVICE : +VTD_DPRINTF (INV ,"Device IOTLB Invalidation Descriptor hi 0x%" PRIx64 +" lo 0x%" PRIx64, inv_desc.hi, inv_desc.lo); +if (!vtd_process_device_iotlb_desc (s, &inv_desc)) { +return false; + } +break ; +default : VTD_DPRINTF(GENERAL, "error: unkonw Invalidation Descriptor type " "hi 0x%"PRIx64 " lo 0x%"PRIx64 " type %"PRIu8,
@@ -2395,6 +2464,10 @@ static void vtd_init(IntelIOMMUState *s)assert (s->intr_eim !=ON_OFF_AUTO_AUTO ); } +if (x86_iommu->dt_supported) { + s->ecap |=VTD_ECAP_DT ; + } +vtd_reset_context_cache (s);vtd_reset_iotlb (s);
hw/i386/intel_iommu_internal.h +11 -2
@@ -183,6 +183,7 @@/* (offset >> 4) << 8 */ #define VTD_ECAP_IRO (DMAR_IOTLB_REG_OFFSET << 4)#define VTD_ECAP_QI (1ULL << 1) +#define VTD_ECAP_DT (1ULL << 2)/* Interrupt Remapping support */ #define VTD_ECAP_IR (1ULL << 3)#define VTD_ECAP_EIM (1ULL << 4)
@@ -326,6 +327,7 @@ typedef union VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_TYPE 0xf#define VTD_INV_DESC_CC 0x1/* Context-cache Invalidate Desc */ #define VTD_INV_DESC_IOTLB 0x2 +#define VTD_INV_DESC_DEVICE 0x3#define VTD_INV_DESC_IEC 0x4/* Interrupt Entry Cache Invalidate Descriptor */ #define VTD_INV_DESC_WAIT 0x5/* Invalidation Wait Descriptor */
@@ -361,6 +363,13 @@ typedef union VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_IOTLB_RSVD_LO 0xffffffff0000ff00ULL#define VTD_INV_DESC_IOTLB_RSVD_HI 0xf80ULL +/* Mask for Device IOTLB Invalidate Descriptor */ +#define VTD_INV_DESC_DEVICE_IOTLB_ADDR (val) ((val) & 0xfffffffffffff000ULL) +#define VTD_INV_DESC_DEVICE_IOTLB_SIZE (val) ((val) & 0x1) +#define VTD_INV_DESC_DEVICE_IOTLB_SID (val) (((val) >> 32) & 0xFFFFULL) +#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL +#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8 +/* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo {uint16_t domain_id;
@@ -399,8 +408,8 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_CONTEXT_ENTRY_FPD (1ULL << 1)/* Fault Processing Disable */ #define VTD_CONTEXT_ENTRY_TT (3ULL << 2)/* Translation Type */ #define VTD_CONTEXT_TT_MULTI_LEVEL 0 -#define VTD_CONTEXT_TT_DEV_IOTLB 1 -#define VTD_CONTEXT_TT_PASS_THROUGH 2 +#define VTD_CONTEXT_TT_DEV_IOTLB (1ULL << 2) +#define VTD_CONTEXT_TT_PASS_THROUGH (2ULL << 2)/* Second Level Page Translation Pointer*/ #define VTD_CONTEXT_ENTRY_SLPTPTR (~0xfffULL)#define VTD_CONTEXT_ENTRY_RSVD_LO (0xff0ULL | ~VTD_HAW_MASK)
hw/i386/x86-iommu.c +17 -0
@@ -106,6 +106,18 @@ static void x86_iommu_intremap_prop_set(Object *o, bool value, Error **errp) s->intr_supported = value; } +static bool x86_iommu_device_iotlb_prop_get (Object *o,Error **errp) +{ +X86IOMMUState *s =X86_IOMMU_DEVICE (o); +return s->dt_supported; +} + +static void x86_iommu_device_iotlb_prop_set (Object *o,bool value,Error **errp) +{ +X86IOMMUState *s =X86_IOMMU_DEVICE (o); + s->dt_supported = value; +} +static void x86_iommu_instance_init (Object *o) {X86IOMMUState *s =X86_IOMMU_DEVICE (o);
@@ -114,6 +126,11 @@ static void x86_iommu_instance_init(Object *o) s->intr_supported = false;object_property_add_bool (o,"intremap" , x86_iommu_intremap_prop_get, x86_iommu_intremap_prop_set,NULL ); + s->dt_supported = false; +object_property_add_bool (o,"device-iotlb" , + x86_iommu_device_iotlb_prop_get, + x86_iommu_device_iotlb_prop_set, +NULL ); }static const TypeInfo x86_iommu_info = {
include/hw/i386/x86-iommu.h +1 -0
@@ -73,6 +73,7 @@ typedef struct IEC_Notifier IEC_Notifier;struct X86IOMMUState {SysBusDevice busdev;bool intr_supported;/* Whether vIOMMU supports IR */ +bool dt_supported;/* Whether vIOMMU supports DT */ IommuType type;/* IOMMU type - AMD/Intel */ QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */ };
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH V4 05/10] intel_iommu: support device iotlb descriptor
Quoted 9 lines · Jason Wang
This patch enables device IOTLB support for intel iommu. The major work is to implement QI device IOTLB descriptor processing and notify the device through iommu notifier.
Cc Paolo Bonzini <[email protected]>Cc Richard Henderson <[email protected]>Cc Eduardo Habkost <[email protected]>Cc Michael S. Tsirkin <[email protected]>Signed-off-by Jason Wang <[email protected]>
Quoted 218 lines
--- hw/i386/intel_iommu.c | 83 +++++++++++++++++++++++++++++++++++++++--- hw/i386/intel_iommu_internal.h | 13 ++++++- hw/i386/x86-iommu.c | 17 +++++++++ include/hw/i386/x86-iommu.h | 1 + 4 files changed, 107 insertions(+), 7 deletions(-)hw/i386/intel_iommu.c +78
-5@@ -738,11 +738,18 @@ static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,"context-entry hi 0x%" PRIx64" lo 0x%" PRIx64, ce->hi, ce->lo);return -VTD_FR_CONTEXT_ENTRY_INV ; - }else if (ce->lo &VTD_CONTEXT_ENTRY_TT ) { -VTD_DPRINTF (GENERAL ,"error: unsupported Translation Type in " -"context-entry hi 0x%" PRIx64" lo 0x%" PRIx64, - ce->hi, ce->lo); -return -VTD_FR_CONTEXT_ENTRY_INV ; + }else { +switch (ce->lo &VTD_CONTEXT_ENTRY_TT ) { +case VTD_CONTEXT_TT_MULTI_LEVEL : +/* fall through */ +case VTD_CONTEXT_TT_DEV_IOTLB : +break ; +default : +VTD_DPRINTF (GENERAL ,"error: unsupported Translation Type in " +"context-entry hi 0x%" PRIx64" lo 0x%" PRIx64, + ce->hi, ce->lo); +return -VTD_FR_CONTEXT_ENTRY_INV ; + } }return 0 ; }@@ -1438,7 +1445,61 @@ static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,vtd_iec_notify_all (s, !inv_desc->iec.granularity, inv_desc->iec.index, inv_desc->iec.index_mask); +return true; +} + +static bool vtd_process_device_iotlb_desc (IntelIOMMUState *s, +VTDInvDesc *inv_desc) +{ +VTDAddressSpace *vtd_dev_as; +IOMMUTLBEntry entry; +struct VTDBus *vtd_bus; +hwaddr addr; +uint64_t sz; +uint16_t sid; +uint8_t devfn; +bool size; +uint8_t bus_num; + + addr =VTD_INV_DESC_DEVICE_IOTLB_ADDR (inv_desc->hi); + sid =VTD_INV_DESC_DEVICE_IOTLB_SID (inv_desc->lo); + devfn = sid &0xff ; + bus_num = sid >>8 ; + size =VTD_INV_DESC_DEVICE_IOTLB_SIZE (inv_desc->hi); + +if ((inv_desc->lo &VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO ) || + (inv_desc->hi &VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI )) { +VTD_DPRINTF (GENERAL ,"error: non-zero reserved field in Device " +"IOTLB Invalidate Descriptor hi 0x%" PRIx64" lo 0x%" PRIx64, + inv_desc->hi, inv_desc->lo); +return false; + } + + vtd_bus =vtd_find_as_from_bus_num (s, bus_num); +if (!vtd_bus) { + goto done; + } + + vtd_dev_as = vtd_bus->dev_as[devfn]; +if (!vtd_dev_as) { + goto done; + } + +if (size) { + sz =1 << (ctz64 (~(addr | (VTD_PAGE_MASK_4K -1 ))) +1 ); + addr &= ~(sz -1 ); + }else { + sz =VTD_PAGE_SIZE ; + } + entry.target_as = &vtd_dev_as->as; + entry.addr_mask = sz -1 ; + entry.iova = addr; + entry.perm =IOMMU_NONE ; + entry.translated_addr =0 ; +memory_region_notify_iommu (entry.target_as->root, entry); + +done:return true; }@@ -1490,6 +1551,14 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s) }break ; +case VTD_INV_DESC_DEVICE : +VTD_DPRINTF (INV ,"Device IOTLB Invalidation Descriptor hi 0x%" PRIx64 +" lo 0x%" PRIx64, inv_desc.hi, inv_desc.lo); +if (!vtd_process_device_iotlb_desc (s, &inv_desc)) { +return false; + } +break ; +default : VTD_DPRINTF(GENERAL, "error: unkonw Invalidation Descriptor type " "hi 0x%"PRIx64 " lo 0x%"PRIx64 " type %"PRIu8,@@ -2395,6 +2464,10 @@ static void vtd_init(IntelIOMMUState *s)assert (s->intr_eim !=ON_OFF_AUTO_AUTO ); } +if (x86_iommu->dt_supported) { + s->ecap |=VTD_ECAP_DT ; + } +vtd_reset_context_cache (s);vtd_reset_iotlb (s);hw/i386/intel_iommu_internal.h +11
-2@@ -183,6 +183,7 @@/* (offset >> 4) << 8 */ #define VTD_ECAP_IRO (DMAR_IOTLB_REG_OFFSET << 4)#define VTD_ECAP_QI (1ULL << 1) +#define VTD_ECAP_DT (1ULL << 2)/* Interrupt Remapping support */ #define VTD_ECAP_IR (1ULL << 3)#define VTD_ECAP_EIM (1ULL << 4)@@ -326,6 +327,7 @@ typedef union VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_TYPE 0xf#define VTD_INV_DESC_CC 0x1/* Context-cache Invalidate Desc */ #define VTD_INV_DESC_IOTLB 0x2 +#define VTD_INV_DESC_DEVICE 0x3#define VTD_INV_DESC_IEC 0x4/* Interrupt Entry Cache Invalidate Descriptor */ #define VTD_INV_DESC_WAIT 0x5/* Invalidation Wait Descriptor */ @@ -361,6 +363,13 @@ typedef union VTDInvDesc VTDInvDesc;#define VTD_INV_DESC_IOTLB_RSVD_LO 0xffffffff0000ff00ULL#define VTD_INV_DESC_IOTLB_RSVD_HI 0xf80ULL +/* Mask for Device IOTLB Invalidate Descriptor */ +#define VTD_INV_DESC_DEVICE_IOTLB_ADDR (val) ((val) & 0xfffffffffffff000ULL) +#define VTD_INV_DESC_DEVICE_IOTLB_SIZE (val) ((val) & 0x1) +#define VTD_INV_DESC_DEVICE_IOTLB_SID (val) (((val) >> 32) & 0xFFFFULL) +#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL +#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8 +/* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo {uint16_t domain_id;@@ -399,8 +408,8 @@ typedef struct VTDRootEntry VTDRootEntry;#define VTD_CONTEXT_ENTRY_FPD (1ULL << 1)/* Fault Processing Disable */ #define VTD_CONTEXT_ENTRY_TT (3ULL << 2)/* Translation Type */ #define VTD_CONTEXT_TT_MULTI_LEVEL 0 -#define VTD_CONTEXT_TT_DEV_IOTLB 1 -#define VTD_CONTEXT_TT_PASS_THROUGH 2 +#define VTD_CONTEXT_TT_DEV_IOTLB (1ULL << 2) +#define VTD_CONTEXT_TT_PASS_THROUGH (2ULL << 2)/* Second Level Page Translation Pointer*/ #define VTD_CONTEXT_ENTRY_SLPTPTR (~0xfffULL)#define VTD_CONTEXT_ENTRY_RSVD_LO (0xff0ULL | ~VTD_HAW_MASK)hw/i386/x86-iommu.c +17
-0@@ -106,6 +106,18 @@ static void x86_iommu_intremap_prop_set(Object *o, bool value, Error **errp) s->intr_supported = value; } +static bool x86_iommu_device_iotlb_prop_get (Object *o,Error **errp) +{ +X86IOMMUState *s =X86_IOMMU_DEVICE (o); +return s->dt_supported; +} + +static void x86_iommu_device_iotlb_prop_set (Object *o,bool value,Error **errp) +{ +X86IOMMUState *s =X86_IOMMU_DEVICE (o); + s->dt_supported = value; +} +static void x86_iommu_instance_init (Object *o) {X86IOMMUState *s =X86_IOMMU_DEVICE (o);@@ -114,6 +126,11 @@ static void x86_iommu_instance_init(Object *o) s->intr_supported = false;object_property_add_bool (o,"intremap" , x86_iommu_intremap_prop_get, x86_iommu_intremap_prop_set,NULL ); + s->dt_supported = false; +object_property_add_bool (o,"device-iotlb" , + x86_iommu_device_iotlb_prop_get, + x86_iommu_device_iotlb_prop_set, +NULL ); }static const TypeInfo x86_iommu_info = {include/hw/i386/x86-iommu.h +1
-0@@ -73,6 +73,7 @@ typedef struct IEC_Notifier IEC_Notifier;struct X86IOMMUState {SysBusDevice busdev;bool intr_supported;/* Whether vIOMMU supports IR */ +bool dt_supported;/* Whether vIOMMU supports DT */ IommuType type;/* IOMMU type - AMD/Intel */ QLIST_HEAD(, IEC_Notifier) iec_notifiers; /* IEC notify list */ };Signature · 1 line
2.7.4
-- peterx
PATCH v4 6/10 virtio-pci: address space translation service (ATS) support
In reply to Jason WangThis patches enable the Address Translation Service support for virtio pci devices. This is needed for a guest visible Device IOTLB implementation and will be required by vhost device IOTLB API implementation for intel IOMMU.
--- hw/pci/pcie.c | 16 ++++++++++++++++ hw/virtio/virtio-pci.c | 7 +++++++ hw/virtio/virtio-pci.h | 4 ++++ include/hw/pci/pcie.h | 4 ++++ include/standard-headers/linux/pci_regs.h | 1 + 5 files changed, 32 insertions(+)
hw/pci/pcie.c +16 -0
@@ -717,3 +717,19 @@ void pcie_dev_ser_num_init(PCIDevice *dev, uint16_t offset, uint64_t ser_num) PCI_EXT_CAP_DSN_SIZEOF);pci_set_quad (dev->config + offset + pci_dsn_cap, ser_num); } + +void pcie_ats_init (PCIDevice *dev,uint16_t offset) +{ +pcie_add_capability (dev,PCI_EXT_CAP_ID_ATS ,0x1 , + offset,PCI_EXT_CAP_ATS_SIZEOF ); + + dev->exp.ats_cap = offset; + +/* Invalidate Queue Depth 0, Page Aligned Request 0 */ +pci_set_word (dev->config + offset +PCI_ATS_CAP ,0 ); +/* STU 0, Disabled by default */ +pci_set_word (dev->config + offset +PCI_ATS_CTRL ,0 ); + +pci_set_word (dev->wmask + dev->exp.ats_cap +PCI_ATS_CTRL ,0x800f ); +} +
hw/virtio/virtio-pci.c +7 -0
@@ -1815,6 +1815,11 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)* PCI Power Management Interface Specification. */ pci_set_word (pci_dev->config + pos +PCI_PM_PMC ,0x3 ); + +if (proxy->flags &VIRTIO_PCI_FLAG_ATS ) { +pcie_ats_init (pci_dev,256 ); + } + }else {/* * make future invocations of pci_is_express() return false
@@ -1868,6 +1873,8 @@ static Property virtio_pci_properties[] = { VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT, false),DEFINE_PROP_BOOL ("x-ignore-backend-features" , VirtIOPCIProxy, ignore_backend_features, false), +DEFINE_PROP_BIT ("ats" , VirtIOPCIProxy, flags, +VIRTIO_PCI_FLAG_ATS_BIT , false), DEFINE_PROP_END_OF_LIST(), };
hw/virtio/virtio-pci.h +4 -0
@@ -72,6 +72,7 @@ enum {VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY_BIT ,VIRTIO_PCI_FLAG_DISABLE_PCIE_BIT ,VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT , + VIRTIO_PCI_FLAG_ATS_BIT, };/* Need to activate work-arounds for buggy guests at vmstate load. */
@@ -96,6 +97,9 @@ enum {#define VIRTIO_PCI_FLAG_PAGE_PER_VQ \ (1 << VIRTIO_PCI_FLAG_PAGE_PER_VQ_BIT) +/* address space translation service */ +#define VIRTIO_PCI_FLAG_ATS (1 << VIRTIO_PCI_FLAG_ATS_BIT) + typedef struct { MSIMessage msg; int virq;
include/hw/pci/pcie.h +4 -0
@@ -74,6 +74,9 @@ struct PCIExpressDevice {/* AER */ uint16_t aer_cap;PCIEAERLog aer_log; + +/* Offset of ATS capability in config space */ +uint16_t ats_cap; };#define COMPAT_PROP_PCP "power_controller_present"
@@ -120,6 +123,7 @@ void pcie_add_capability(PCIDevice *dev,void pcie_ari_init (PCIDevice *dev,uint16_t offset,uint16_t nextfn);void pcie_dev_ser_num_init (PCIDevice *dev,uint16_t offset,uint64_t ser_num); +void pcie_ats_init (PCIDevice *dev,uint16_t offset);extern const VMStateDescription vmstate_pcie_device;
include/standard-headers/linux/pci_regs.h +1 -0
@@ -678,6 +678,7 @@#define PCI_EXT_CAP_ID_MAX PCI_EXT_CAP_ID_PTM#define PCI_EXT_CAP_DSN_SIZEOF 12 +#define PCI_EXT_CAP_ATS_SIZEOF 8#define PCI_EXT_CAP_MCAST_ENDPOINT_SIZEOF 40/* Advanced Error Reporting */
Signature · 1 line
2.7.4
PATCH v4 7/10 acpi: add ATSR for q35
In reply to Jason WangThis patch provides ATSR which was a requirement for software that wants to enable ATS on endpoint devices behind a Root Port. This is done simply by setting ALL_PORTS which indicates all PCI-Express Root Ports support ATS transactions.
--- hw/i386/acpi-build.c | 9 +++++++++ include/hw/acpi/acpi-defs.h | 12 ++++++++++++ 2 files changed, 21 insertions(+)
hw/i386/acpi-build.c +9 -0
@@ -2575,6 +2575,7 @@ build_dmar_q35(GArray *table_data, BIOSLinker *linker)AcpiTableDmar *dmar;AcpiDmarHardwareUnit *drhd; +AcpiDmarRootPortATS *atsr;uint8_t dmar_flags =0 ;X86IOMMUState *iommu =x86_iommu_get_default ();AcpiDmarDeviceScope *scope =NULL ;
@@ -2608,6 +2609,14 @@ build_dmar_q35(GArray *table_data, BIOSLinker *linker) scope->path[0 ].device =PCI_SLOT (Q35_PSEUDO_DEVFN_IOAPIC ); scope->path[0 ].function =PCI_FUNC (Q35_PSEUDO_DEVFN_IOAPIC ); +if (iommu->dt_supported) { + atsr =acpi_data_push (table_data,sizeof (*atsr)); + atsr->type =cpu_to_le16 (ACPI_DMAR_TYPE_ATSR ); + atsr->length =cpu_to_le16 (sizeof (*atsr)); + atsr->flags =ACPI_DMAR_ATSR_ALL_PORTS ; + atsr->pci_segment =cpu_to_le16 (0 ); + } +build_header (linker, table_data, (void *)(table_data->data + dmar_start),"DMAR" , table_data->len - dmar_start,1 ,NULL ,NULL ); }
include/hw/acpi/acpi-defs.h +12 -0
@@ -638,8 +638,20 @@ struct AcpiDmarHardwareUnit { }QEMU_PACKED ;typedef struct AcpiDmarHardwareUnit AcpiDmarHardwareUnit ; +/* Type 2: Root Port ATS Capability Reporting Structure */ +struct AcpiDmarRootPortATS { +uint16_t type; +uint16_t length; +uint8_t flags; +uint8_t reserved; +uint16_t pci_segment; +AcpiDmarDeviceScope scope[0 ]; +}QEMU_PACKED ; +typedef struct AcpiDmarRootPortATS AcpiDmarRootPortATS ; +/* Masks for Flags field above */ #define ACPI_DMAR_INCLUDE_PCI_ALL 1 +#define ACPI_DMAR_ATSR_ALL_PORTS 1/* * Input Output Remapping Table (IORT)
Signature · 1 line
2.7.4
PATCH v4 8/10 memory: handle alias for iommu notifier
In reply to Jason Wang--- memory.c | 9 +++++++++ 1 file changed, 9 insertions(+)
memory.c +9 -0
@@ -1603,6 +1603,11 @@ static void memory_region_update_iommu_notify_flags(MemoryRegion *mr)void memory_region_register_iommu_notifier (MemoryRegion *mr,IOMMUNotifier *n) { +if (mr->alias) { +memory_region_register_iommu_notifier (mr->alias, n); +return ; + } +/* We need to register for at least one bitfield */ assert (n->notifier_flags !=IOMMU_NOTIFIER_NONE );QLIST_INSERT_HEAD (&mr->iommu_notify, n, node);
@@ -1643,6 +1648,10 @@ void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n,void memory_region_unregister_iommu_notifier (MemoryRegion *mr,IOMMUNotifier *n) { +if (mr->alias) { +memory_region_unregister_iommu_notifier (mr->alias, n); +return ; + }QLIST_REMOVE (n, node);memory_region_update_iommu_notify_flags (mr); }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH V4 08/10] memory: handle alias for iommu notifier
Quoted 3 lines · Jason Wang
Cc Paolo Bonzini <[email protected]>Acked-by Paolo Bonzini <[email protected]>Signed-off-by Jason Wang <[email protected]>
Quoted 34 lines
--- memory.c | 9 +++++++++ 1 file changed, 9 insertions(+)memory.c +9
-0@@ -1603,6 +1603,11 @@ static void memory_region_update_iommu_notify_flags(MemoryRegion *mr)void memory_region_register_iommu_notifier (MemoryRegion *mr,IOMMUNotifier *n) { +if (mr->alias) { +memory_region_register_iommu_notifier (mr->alias, n); +return ; + } +/* We need to register for at least one bitfield */ assert (n->notifier_flags !=IOMMU_NOTIFIER_NONE );QLIST_INSERT_HEAD (&mr->iommu_notify, n, node);@@ -1643,6 +1648,10 @@ void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n,void memory_region_unregister_iommu_notifier (MemoryRegion *mr,IOMMUNotifier *n) { +if (mr->alias) { +memory_region_unregister_iommu_notifier (mr->alias, n); +return ; + }QLIST_REMOVE (n, node);memory_region_update_iommu_notify_flags (mr); }Signature · 1 line
2.7.4
-- peterx
PATCH v4 9/10 memory: handle alias in memory_region_is_iommu()
In reply to Jason Wang--- include/exec/memory.h | 3 +++ 1 file changed, 3 insertions(+)
include/exec/memory.h +3 -0
@@ -628,6 +628,9 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)*/ static inline bool memory_region_is_iommu (MemoryRegion *mr) { +if (mr->alias) { +return memory_region_is_iommu (mr->alias); + }return mr->iommu_ops; }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH V4 09/10] memory: handle alias in memory_region_is_iommu()
Quoted 3 lines · Jason Wang
Cc Paolo Bonzini <[email protected]>Acked-by Paolo Bonzini <[email protected]>Signed-off-by Jason Wang <[email protected]>
Quoted 21 lines
--- include/exec/memory.h | 3 +++ 1 file changed, 3 insertions(+)include/exec/memory.h +3
-0@@ -628,6 +628,9 @@ static inline bool memory_region_is_romd(MemoryRegion *mr)*/ static inline bool memory_region_is_iommu (MemoryRegion *mr) { +if (mr->alias) { +return memory_region_is_iommu (mr->alias); + }return mr->iommu_ops; }Signature · 1 line
2.7.4
-- peterx
PATCH v4 10/10 vhost_net: device IOTLB support
In reply to Jason WangThis patches implements Device IOTLB support for vhost kernel. This is done through:
1) switch to use dma helpers when map/unmap vrings from vhost codes
2) introduce a set of VhostOps to:
- setting up device IOTLB request callback
- processing device IOTLB request
- processing device IOTLB invalidation
2) kernel support for Device IOTLB API:
- allow vhost-net to query the IOMMU IOTLB entry through eventfd
- enable the ability for qemu to update a specified mapping of vhost
- through ioctl.
- enable the ability to invalidate a specified range of iova for the device IOTLB of vhost through ioctl. In x86/intel_iommu case this is triggered through iommu memory region notifier from device IOTLB invalidation descriptor processing routine.
With all the above, kernel vhost_net can co-operate with userspace IOMMU. For vhost-user, the support could be easily done on top by implementing the VhostOps.
--- hw/virtio/vhost-backend.c | 99 +++++++++++++++++++++ hw/virtio/vhost.c | 178 +++++++++++++++++++++++++++++++++----- include/hw/virtio/vhost-backend.h | 13 +++ include/hw/virtio/vhost.h | 4 + net/tap.c | 1 + 5 files changed, 273 insertions(+), 22 deletions(-)
hw/virtio/vhost-backend.c +99 -0
@@ -185,6 +185,102 @@ static int vhost_kernel_vsock_set_running(struct vhost_dev *dev, int start) }#endif /* CONFIG_VHOST_VSOCK */ +static void vhost_kernel_iotlb_read (void *opaque) +{ +struct vhost_dev *dev = opaque; +struct vhost_msg msg; +ssize_t len; + +while ((len =read ((uintptr_t )dev->opaque, &msg,sizeof msg)) >0 ) { +struct vhost_iotlb_msg *imsg = &msg.iotlb; +if (len <sizeof msg) { +error_report ("Wrong vhost message len: %d" , (int )len); +break ; + } +if (msg.type !=VHOST_IOTLB_MSG ) { +error_report ("Unknown vhost iotlb message type" ); +break ; + } +switch (imsg->type) { +case VHOST_IOTLB_MISS : +vhost_device_iotlb_miss (dev, imsg->iova, + imsg->perm !=VHOST_ACCESS_RO ); +break ; +case VHOST_IOTLB_UPDATE : +case VHOST_IOTLB_INVALIDATE : +error_report ("Unexpected IOTLB message type" ); +break ; +case VHOST_IOTLB_ACCESS_FAIL : +/* FIXME: report device iotlb error */ +break ; +default : +break ; + } + } +} + +static int vhost_kernel_update_device_iotlb (struct vhost_dev *dev, +uint64_t iova,uint64_t uaddr, +uint64_t len, +IOMMUAccessFlags perm) +{ +struct vhost_msg msg; + msg.type =VHOST_IOTLB_MSG ; + msg.iotlb.iova = iova; + msg.iotlb.uaddr = uaddr; + msg.iotlb.size = len; + msg.iotlb.type =VHOST_IOTLB_UPDATE ; + +switch (perm) { +case IOMMU_RO : + msg.iotlb.perm =VHOST_ACCESS_RO ; +break ; +case IOMMU_WO : + msg.iotlb.perm =VHOST_ACCESS_WO ; +break ; +case IOMMU_RW : + msg.iotlb.perm =VHOST_ACCESS_RW ; +break ; +default : +g_assert_not_reached (); + } + +if (write ((uintptr_t )dev->opaque, &msg,sizeof msg) !=sizeof msg) { +error_report ("Fail to update device iotlb" ); +return -EFAULT ; + } + +return 0 ; +} + +static int vhost_kernel_invalidate_device_iotlb (struct vhost_dev *dev, +uint64_t iova,uint64_t len) +{ +struct vhost_msg msg; + + msg.type =VHOST_IOTLB_MSG ; + msg.iotlb.iova = iova; + msg.iotlb.size = len; + msg.iotlb.type =VHOST_IOTLB_INVALIDATE ; + +if (write ((uintptr_t )dev->opaque, &msg,sizeof msg) !=sizeof msg) { +error_report ("Fail to invalidate device iotlb" ); +return -EFAULT ; + } + +return 0 ; +} + +static void vhost_kernel_set_iotlb_callback (struct vhost_dev *dev, +int enabled) +{ +if (enabled) +qemu_set_fd_handler ((uintptr_t )dev->opaque, + vhost_kernel_iotlb_read,NULL , dev); +else +qemu_set_fd_handler ((uintptr_t )dev->opaque,NULL ,NULL ,NULL ); +} +static const VhostOps kernel_ops = { .backend_type =VHOST_BACKEND_TYPE_KERNEL , .vhost_backend_init = vhost_kernel_init,
@@ -214,6 +310,9 @@ static const VhostOps kernel_ops = { .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid, .vhost_vsock_set_running = vhost_kernel_vsock_set_running,#endif /* CONFIG_VHOST_VSOCK */ + .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback, + .vhost_update_device_iotlb = vhost_kernel_update_device_iotlb, + .vhost_invalidate_device_iotlb = vhost_kernel_invalidate_device_iotlb, };int vhost_set_backend_type (struct vhost_dev *dev,VhostBackendType backend_type)
hw/virtio/vhost.c +156 -22
332 lines not on this page · raw message
include/hw/virtio/vhost-backend.h +13 -0
@@ -11,6 +11,8 @@ #ifndef VHOST_BACKEND_H #define VHOST_BACKEND_H +#include "exec/memory.h" + typedef enum VhostBackendType { VHOST_BACKEND_TYPE_NONE = 0, VHOST_BACKEND_TYPE_KERNEL = 1,
@@ -76,6 +78,14 @@ typedef bool (*vhost_backend_can_merge_op)(struct vhost_dev *dev,typedef int (*vhost_vsock_set_guest_cid_op )(struct vhost_dev *dev,uint64_t guest_cid);typedef int (*vhost_vsock_set_running_op )(struct vhost_dev *dev,int start); +typedef void (*vhost_set_iotlb_callback_op )(struct vhost_dev *dev, +int enabled); +typedef int (*vhost_update_device_iotlb_op )(struct vhost_dev *dev, +uint64_t iova,uint64_t uaddr, +uint64_t len, +IOMMUAccessFlags perm); +typedef int (*vhost_invalidate_device_iotlb_op )(struct vhost_dev *dev, +uint64_t iova,uint64_t len); typedef struct VhostOps { VhostBackendType backend_type;
@@ -107,6 +117,9 @@ typedef struct VhostOps {vhost_backend_can_merge_op vhost_backend_can_merge;vhost_vsock_set_guest_cid_op vhost_vsock_set_guest_cid;vhost_vsock_set_running_op vhost_vsock_set_running; +vhost_set_iotlb_callback_op vhost_set_iotlb_callback; +vhost_update_device_iotlb_op vhost_update_device_iotlb; +vhost_invalidate_device_iotlb_op vhost_invalidate_device_iotlb; } VhostOps;extern const VhostOps user_ops;
include/hw/virtio/vhost.h +4 -0
@@ -21,6 +21,7 @@ struct vhost_virtqueue {unsigned long long used_phys;unsigned used_size;EventNotifier masked_notifier; +struct vhost_dev *dev; };typedef unsigned long vhost_log_chunk_t ;
@@ -38,6 +39,7 @@ struct vhost_log {struct vhost_memory ;struct vhost_dev { +VirtIODevice *vdev;MemoryListener memory_listener;struct vhost_memory *mem;int n_mem_sections;
@@ -62,6 +64,7 @@ struct vhost_dev {void *opaque;struct vhost_log *log;QLIST_ENTRY (vhost_dev) entry; +IOMMUNotifier n; }; int vhost_dev_init(struct vhost_dev *hdev, void *opaque,
@@ -91,4 +94,5 @@ bool vhost_has_free_slot(void);int vhost_net_set_backend (struct vhost_dev *hdev,struct vhost_vring_file *file); +void vhost_device_iotlb_miss (struct vhost_dev *dev,uint64_t iova,int write);#endif
net/tap.c +1 -0
@@ -696,6 +696,7 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, "tap: open vhost char device failed");return ; } +fcntl (vhostfd,F_SETFL ,O_NONBLOCK ); } options.opaque = (void *)(uintptr_t )vhostfd;
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH V4 10/10] vhost_net: device IOTLB support
Quoted 18 lines · Jason Wang
This patches implements Device IOTLB support for vhost kernel. This is done through:
1) switch to use dma helpers when map/unmap vrings from vhost codes
2) introduce a set of VhostOps to:
- setting up device IOTLB request callback
- processing device IOTLB request
- processing device IOTLB invalidation
2) kernel support for Device IOTLB API:- allow vhost-net to query the IOMMU IOTLB entry through eventfd
- enable the ability for qemu to update a specified mapping of vhost
- through ioctl.
- enable the ability to invalidate a specified range of iova for the device IOTLB of vhost through ioctl. In x86/intel_iommu case this is triggered through iommu memory region notifier from device IOTLB invalidation descriptor processing routine.
Quoted 6 lines
With all the above, kernel
vhost_netcan co-operate with userspace IOMMU. For vhost-user, the support could be easily done on top by implementing the VhostOps.Cc Michael S. Tsirkin <[email protected]>Signed-off-by Jason Wang <[email protected]>
Specifically this patch is the one causing issues.
Quoted 564 lines
--- hw/virtio/vhost-backend.c | 99 +++++++++++++++++++++ hw/virtio/vhost.c | 178 +++++++++++++++++++++++++++++++++----- include/hw/virtio/vhost-backend.h | 13 +++ include/hw/virtio/vhost.h | 4 + net/tap.c | 1 + 5 files changed, 273 insertions(+), 22 deletions(-)hw/virtio/vhost-backend.c +99
-0@@ -185,6 +185,102 @@ static int vhost_kernel_vsock_set_running(struct vhost_dev *dev, int start) }#endif /* CONFIG_VHOST_VSOCK */ +static void vhost_kernel_iotlb_read (void *opaque) +{ +struct vhost_dev *dev = opaque; +struct vhost_msg msg; +ssize_t len; + +while ((len =read ((uintptr_t )dev->opaque, &msg,sizeof msg)) >0 ) { +struct vhost_iotlb_msg *imsg = &msg.iotlb; +if (len <sizeof msg) { +error_report ("Wrong vhost message len: %d" , (int )len); +break ; + } +if (msg.type !=VHOST_IOTLB_MSG ) { +error_report ("Unknown vhost iotlb message type" ); +break ; + } +switch (imsg->type) { +case VHOST_IOTLB_MISS : +vhost_device_iotlb_miss (dev, imsg->iova, + imsg->perm !=VHOST_ACCESS_RO ); +break ; +case VHOST_IOTLB_UPDATE : +case VHOST_IOTLB_INVALIDATE : +error_report ("Unexpected IOTLB message type" ); +break ; +case VHOST_IOTLB_ACCESS_FAIL : +/* FIXME: report device iotlb error */ +break ; +default : +break ; + } + } +} + +static int vhost_kernel_update_device_iotlb (struct vhost_dev *dev, +uint64_t iova,uint64_t uaddr, +uint64_t len, +IOMMUAccessFlags perm) +{ +struct vhost_msg msg; + msg.type =VHOST_IOTLB_MSG ; + msg.iotlb.iova = iova; + msg.iotlb.uaddr = uaddr; + msg.iotlb.size = len; + msg.iotlb.type =VHOST_IOTLB_UPDATE ; + +switch (perm) { +case IOMMU_RO : + msg.iotlb.perm =VHOST_ACCESS_RO ; +break ; +case IOMMU_WO : + msg.iotlb.perm =VHOST_ACCESS_WO ; +break ; +case IOMMU_RW : + msg.iotlb.perm =VHOST_ACCESS_RW ; +break ; +default : +g_assert_not_reached (); + } + +if (write ((uintptr_t )dev->opaque, &msg,sizeof msg) !=sizeof msg) { +error_report ("Fail to update device iotlb" ); +return -EFAULT ; + } + +return 0 ; +} + +static int vhost_kernel_invalidate_device_iotlb (struct vhost_dev *dev, +uint64_t iova,uint64_t len) +{ +struct vhost_msg msg; + + msg.type =VHOST_IOTLB_MSG ; + msg.iotlb.iova = iova; + msg.iotlb.size = len; + msg.iotlb.type =VHOST_IOTLB_INVALIDATE ; + +if (write ((uintptr_t )dev->opaque, &msg,sizeof msg) !=sizeof msg) { +error_report ("Fail to invalidate device iotlb" ); +return -EFAULT ; + } + +return 0 ; +} + +static void vhost_kernel_set_iotlb_callback (struct vhost_dev *dev, +int enabled) +{ +if (enabled) +qemu_set_fd_handler ((uintptr_t )dev->opaque, + vhost_kernel_iotlb_read,NULL , dev); +else +qemu_set_fd_handler ((uintptr_t )dev->opaque,NULL ,NULL ,NULL ); +} +static const VhostOps kernel_ops = { .backend_type =VHOST_BACKEND_TYPE_KERNEL , .vhost_backend_init = vhost_kernel_init,@@ -214,6 +310,9 @@ static const VhostOps kernel_ops = { .vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid, .vhost_vsock_set_running = vhost_kernel_vsock_set_running,#endif /* CONFIG_VHOST_VSOCK */ + .vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback, + .vhost_update_device_iotlb = vhost_kernel_update_device_iotlb, + .vhost_invalidate_device_iotlb = vhost_kernel_invalidate_device_iotlb, };int vhost_set_backend_type (struct vhost_dev *dev,VhostBackendType backend_type)hw/virtio/vhost.c +156
-22@@ -26,6 +26,7 @@#include "hw/virtio/virtio-bus.h" #include "hw/virtio/virtio-access.h" #include "migration/migration.h" +#include "sysemu/dma.h" /* enabled until disconnected backend stabilizes */ #define _VHOST_DEBUG 1@@ -421,8 +422,35 @@ static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) dev->log_size = size; } +static void *vhost_memory_map (VirtIODevice *vdev,hwaddr addr, +hwaddr *plen,int is_write) +{ +AddressSpace *dma_as = vdev->dma_as; + +if (!memory_region_is_iommu (dma_as->root)) { +return dma_memory_map (dma_as, addr, plen, is_write ? +DMA_DIRECTION_FROM_DEVICE : +DMA_DIRECTION_TO_DEVICE ); + }else { +return (void *)(uintptr_t )addr; + } +} + +static void vhost_memory_unmap (VirtIODevice *vdev,void *buffer, +hwaddr len,int is_write, +hwaddr access_len) +{ +AddressSpace *dma_as = vdev->dma_as; -static int vhost_verify_ring_part_mapping(void *part, +if (!memory_region_is_iommu (dma_as->root)) { +dma_memory_unmap (dma_as, buffer, len, is_write ? +DMA_DIRECTION_FROM_DEVICE :DMA_DIRECTION_TO_DEVICE , + access_len); + } +} + +static int vhost_verify_ring_part_mapping(struct vhost_dev *dev, + void *part, uint64_t part_addr, uint64_t part_size, uint64_t start_addr,@@ -436,14 +464,14 @@ static int vhost_verify_ring_part_mapping(void *part,return 0 ; } l = part_size; - p =cpu_physical_memory_map (part_addr, &l,1 ); + p =vhost_memory_map (dev->vdev, part_addr, &l,1 );if (!p || l != part_size) { r = -ENOMEM ; }if (p != part) { r = -EBUSY ; } -cpu_physical_memory_unmap (p, l,0 ,0 ); +vhost_memory_unmap (dev->vdev, p, l,0 ,0 );return r; }@@ -463,21 +491,21 @@ static int vhost_verify_ring_mappings(struct vhost_dev *dev,struct vhost_virtqueue *vq = dev->vqs + i; j =0 ; - r =vhost_verify_ring_part_mapping (vq->desc, vq->desc_phys, + r =vhost_verify_ring_part_mapping (dev, vq->desc, vq->desc_phys, vq->desc_size, start_addr, size);if (!r) {break ; } j++; - r =vhost_verify_ring_part_mapping (vq->avail, vq->avail_phys, + r =vhost_verify_ring_part_mapping (dev, vq->avail, vq->avail_phys, vq->avail_size, start_addr, size);if (!r) {break ; } j++; - r =vhost_verify_ring_part_mapping (vq->used, vq->used_phys, + r =vhost_verify_ring_part_mapping (dev, vq->used, vq->used_phys, vq->used_size, start_addr, size);if (!r) {break ;@@ -715,13 +743,27 @@ static int vhost_virtqueue_set_addr(struct vhost_dev *dev,return 0 ; } -static int vhost_dev_set_features (struct vhost_dev *dev,bool enable_log) +static int vhost_dev_has_iommu (struct vhost_dev *dev) +{ +VirtIODevice *vdev = dev->vdev; +AddressSpace *dma_as = vdev->dma_as; + +return memory_region_is_iommu (dma_as->root) && +virtio_host_has_feature (vdev,VIRTIO_F_IOMMU_PLATFORM ); +} + +static int vhost_dev_set_features (struct vhost_dev *dev, +bool enable_log) {uint64_t features = dev->acked_features; +bool has_iommu =vhost_dev_has_iommu (dev);int r;if (enable_log) { features |=0x1ULL <<VHOST_F_LOG_ALL ; } +if (has_iommu) { + features |=0x1ULL <<VIRTIO_F_IOMMU_PLATFORM ; + } r = dev->vhost_ops->vhost_set_features (dev, features);if (r <0 ) {VHOST_OPS_DEBUG ("vhost_set_features failed" );@@ -858,6 +900,56 @@ static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,return -errno; } +static int vhost_memory_region_lookup (struct vhost_dev *hdev, +uint64_t gpa,uint64_t *uaddr, +uint64_t *len) +{ +int i; + +for (i =0 ; i < hdev->mem->nregions; i++) { +struct vhost_memory_region *reg = hdev->mem->regions + i; + +if (gpa >= reg->guest_phys_addr && + reg->guest_phys_addr + reg->memory_size > gpa) { + *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr; + *len = reg->guest_phys_addr + reg->memory_size - gpa; +return 0 ; + } + } + +return -EFAULT ; +} + +void vhost_device_iotlb_miss (struct vhost_dev *dev,uint64_t iova,int write) +{ +IOMMUTLBEntry iotlb; +uint64_t uaddr, len; + +rcu_read_lock (); + + iotlb =address_space_get_iotlb_entry (dev->vdev->dma_as, + iova, write); +if (iotlb.target_as !=NULL ) { +if (vhost_memory_region_lookup (dev, iotlb.translated_addr, + &uaddr, &len)) { +error_report ("Fail to lookup the translated address " +"%" PRIx64, iotlb.translated_addr); + goto out; + } + + len =MIN (iotlb.addr_mask +1 , len); + iova = iova & ~iotlb.addr_mask; + +if (dev->vhost_ops->vhost_update_device_iotlb (dev, iova, uaddr, + len, iotlb.perm)) { +error_report ("Fail to update device iotlb" ); + goto out; + } + } +out: +rcu_read_unlock (); +} + static int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, struct vhost_virtqueue *vq,@@ -903,21 +995,21 @@ static int vhost_virtqueue_start(struct vhost_dev *dev, vq->desc_size = s = l =virtio_queue_get_desc_size (vdev, idx); vq->desc_phys = a =virtio_queue_get_desc_addr (vdev, idx); - vq->desc =cpu_physical_memory_map (a, &l,0 ); + vq->desc =vhost_memory_map (vdev, a, &l,0 );if (!vq->desc || l != s) { r = -ENOMEM ; goto fail_alloc_desc; } vq->avail_size = s = l =virtio_queue_get_avail_size (vdev, idx); vq->avail_phys = a =virtio_queue_get_avail_addr (vdev, idx); - vq->avail =cpu_physical_memory_map (a, &l,0 ); + vq->avail =vhost_memory_map (vdev, a, &l,0 );if (!vq->avail || l != s) { r = -ENOMEM ; goto fail_alloc_avail; } vq->used_size = s = l =virtio_queue_get_used_size (vdev, idx); vq->used_phys = a =virtio_queue_get_used_addr (vdev, idx); - vq->used =cpu_physical_memory_map (a, &l,1 ); + vq->used =vhost_memory_map (vdev, a, &l,1 );if (!vq->used || l != s) { r = -ENOMEM ; goto fail_alloc_used;@@ -963,14 +1055,14 @@ static int vhost_virtqueue_start(struct vhost_dev *dev, fail_vector: fail_kick: fail_alloc: -cpu_physical_memory_unmap (vq->used,virtio_queue_get_used_size (vdev, idx), -0 ,0 ); +vhost_memory_unmap (vdev, vq->used,virtio_queue_get_used_size (vdev, idx), +0 ,0 ); fail_alloc_used: -cpu_physical_memory_unmap (vq->avail,virtio_queue_get_avail_size (vdev, idx), -0 ,0 ); +vhost_memory_unmap (vdev, vq->avail,virtio_queue_get_avail_size (vdev, idx), +0 ,0 ); fail_alloc_avail: -cpu_physical_memory_unmap (vq->desc,virtio_queue_get_desc_size (vdev, idx), -0 ,0 ); +vhost_memory_unmap (vdev, vq->desc,virtio_queue_get_desc_size (vdev, idx), +0 ,0 ); fail_alloc_desc:return r; }@@ -1003,12 +1095,12 @@ static void vhost_virtqueue_stop(struct vhost_dev *dev, vhost_vq_index); } -cpu_physical_memory_unmap (vq->used,virtio_queue_get_used_size (vdev, idx), -1 ,virtio_queue_get_used_size (vdev, idx)); -cpu_physical_memory_unmap (vq->avail,virtio_queue_get_avail_size (vdev, idx), -0 ,virtio_queue_get_avail_size (vdev, idx)); -cpu_physical_memory_unmap (vq->desc,virtio_queue_get_desc_size (vdev, idx), -0 ,virtio_queue_get_desc_size (vdev, idx)); +vhost_memory_unmap (vdev, vq->used,virtio_queue_get_used_size (vdev, idx), +1 ,virtio_queue_get_used_size (vdev, idx)); +vhost_memory_unmap (vdev, vq->avail,virtio_queue_get_avail_size (vdev, idx), +0 ,virtio_queue_get_avail_size (vdev, idx)); +vhost_memory_unmap (vdev, vq->desc,virtio_queue_get_desc_size (vdev, idx), +0 ,virtio_queue_get_desc_size (vdev, idx)); } static void vhost_eventfd_add(MemoryListener *listener,@@ -1065,6 +1157,9 @@ static int vhost_virtqueue_init(struct vhost_dev *dev, r = -errno; goto fail_call; } + + vq->dev = dev; +return 0 ; fail_call:event_notifier_cleanup (&vq->masked_notifier);@@ -1076,12 +1171,24 @@ static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq)event_notifier_cleanup (&vq->masked_notifier); } +static void vhost_iommu_unmap_notify (IOMMUNotifier *n,IOMMUTLBEntry *iotlb) +{ + struct vhost_dev *hdev = container_of(n, struct vhost_dev, n); + +if (hdev->vhost_ops->vhost_invalidate_device_iotlb (hdev, + iotlb->iova, + iotlb->addr_mask +1 )) { +error_report ("Fail to invalidate device iotlb" ); + } +} +int vhost_dev_init (struct vhost_dev *hdev,void *opaque,VhostBackendType backend_type,uint32_t busyloop_timeout) {uint64_t features;int i, r, n_initialized_vqs =0 ; + hdev->vdev =NULL ; hdev->migration_blocker =NULL ; r =vhost_set_backend_type (hdev, backend_type);@@ -1146,6 +1253,9 @@ int vhost_dev_init(struct vhost_dev *hdev, void *opaque, .priority = 10 }; + hdev->n.notify = vhost_iommu_unmap_notify; + hdev->n.notifier_flags =IOMMU_NOTIFIER_UNMAP ; +if (hdev->migration_blocker ==NULL ) {if (!(hdev->features & (0x1ULL <<VHOST_F_LOG_ALL ))) { error_setg(&hdev->migration_blocker,@@ -1341,11 +1451,18 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev)assert (hdev->vhost_ops); hdev->started = true; + hdev->vdev = vdev; r =vhost_dev_set_features (hdev, hdev->log_enabled);if (r <0 ) { goto fail_features; } + +if (vhost_dev_has_iommu (hdev)) { +memory_region_register_iommu_notifier (vdev->dma_as->root, + &hdev->n); + } + r = hdev->vhost_ops->vhost_set_mem_table (hdev, hdev->mem);if (r <0 ) {VHOST_OPS_DEBUG ("vhost_set_mem_table failed" );@@ -1379,6 +1496,16 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev) } } + hdev->vhost_ops->vhost_set_iotlb_callback (hdev, true); + +if (vhost_dev_has_iommu (hdev)) { +/* Update used ring information for IOTLB to work correctly, +* vhost-kernel code requires for this.*/ +for (i =0 ; i < hdev->nvqs; ++i) { +struct vhost_virtqueue *vq = hdev->vqs + i; +vhost_device_iotlb_miss (hdev, vq->used_phys, true); + } + }return 0 ; fail_log:vhost_log_put (hdev, false);@@ -1390,6 +1517,7 @@ fail_vq: hdev->vq_index + i); } i = hdev->nvqs; + fail_mem: fail_features:@@ -1404,6 +1532,7 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev)/* should only be called after backend is connected */ assert (hdev->vhost_ops); + hdev->vhost_ops->vhost_set_iotlb_callback (hdev, false);for (i =0 ; i < hdev->nvqs; ++i) { vhost_virtqueue_stop(hdev,@@ -1412,8 +1541,13 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev) hdev->vq_index + i); } +if (vhost_dev_has_iommu (hdev)) { +memory_region_unregister_iommu_notifier (vdev->dma_as->root, + &hdev->n); + }vhost_log_put (hdev, true); hdev->started = false; + hdev->vdev =NULL ; } int vhost_net_set_backend(struct vhost_dev *hdev,include/hw/virtio/vhost-backend.h +13
-0@@ -11,6 +11,8 @@ #ifndef VHOST_BACKEND_H #define VHOST_BACKEND_H +#include "exec/memory.h" + typedef enum VhostBackendType { VHOST_BACKEND_TYPE_NONE = 0, VHOST_BACKEND_TYPE_KERNEL = 1,@@ -76,6 +78,14 @@ typedef bool (*vhost_backend_can_merge_op)(struct vhost_dev *dev,typedef int (*vhost_vsock_set_guest_cid_op )(struct vhost_dev *dev,uint64_t guest_cid);typedef int (*vhost_vsock_set_running_op )(struct vhost_dev *dev,int start); +typedef void (*vhost_set_iotlb_callback_op )(struct vhost_dev *dev, +int enabled); +typedef int (*vhost_update_device_iotlb_op )(struct vhost_dev *dev, +uint64_t iova,uint64_t uaddr, +uint64_t len, +IOMMUAccessFlags perm); +typedef int (*vhost_invalidate_device_iotlb_op )(struct vhost_dev *dev, +uint64_t iova,uint64_t len); typedef struct VhostOps { VhostBackendType backend_type;@@ -107,6 +117,9 @@ typedef struct VhostOps {vhost_backend_can_merge_op vhost_backend_can_merge;vhost_vsock_set_guest_cid_op vhost_vsock_set_guest_cid;vhost_vsock_set_running_op vhost_vsock_set_running; +vhost_set_iotlb_callback_op vhost_set_iotlb_callback; +vhost_update_device_iotlb_op vhost_update_device_iotlb; +vhost_invalidate_device_iotlb_op vhost_invalidate_device_iotlb; } VhostOps;extern const VhostOps user_ops;include/hw/virtio/vhost.h +4
-0@@ -21,6 +21,7 @@ struct vhost_virtqueue {unsigned long long used_phys;unsigned used_size;EventNotifier masked_notifier; +struct vhost_dev *dev; };typedef unsigned long vhost_log_chunk_t ;@@ -38,6 +39,7 @@ struct vhost_log {struct vhost_memory ;struct vhost_dev { +VirtIODevice *vdev;MemoryListener memory_listener;struct vhost_memory *mem;int n_mem_sections;@@ -62,6 +64,7 @@ struct vhost_dev {void *opaque;struct vhost_log *log;QLIST_ENTRY (vhost_dev) entry; +IOMMUNotifier n; }; int vhost_dev_init(struct vhost_dev *hdev, void *opaque,@@ -91,4 +94,5 @@ bool vhost_has_free_slot(void);int vhost_net_set_backend (struct vhost_dev *hdev,struct vhost_vring_file *file); +void vhost_device_iotlb_miss (struct vhost_dev *dev,uint64_t iova,int write);#endif net/tap.c +1
-0@@ -696,6 +696,7 @@ static void net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, "tap: open vhost char device failed");return ; } +fcntl (vhostfd,F_SETFL ,O_NONBLOCK ); } options.opaque = (void *)(uintptr_t )vhostfd;Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH V4 10/10] vhost_net: device IOTLB support
Quoted 19 lines · Michael S. Tsirkin
Quoted 18 lines · Jason Wang
This patches implements Device IOTLB support for vhost kernel. This is done through:
1) switch to use dma helpers when map/unmap vrings from vhost codes
2) introduce a set of VhostOps to:
- setting up device IOTLB request callback
- processing device IOTLB request
- processing device IOTLB invalidation
2) kernel support for Device IOTLB API:- allow vhost-net to query the IOMMU IOTLB entry through eventfd
- enable the ability for qemu to update a specified mapping of vhost
- through ioctl.
- enable the ability to invalidate a specified range of iova for the device IOTLB of vhost through ioctl. In x86/intel_iommu case this is triggered through iommu memory region notifier from device IOTLB invalidation descriptor processing routine.
Quoted 9 lines
Quoted 6 lines
With all the above, kernel
vhost_netcan co-operate with userspace IOMMU. For vhost-user, the support could be easily done on top by implementing the VhostOps.Cc Michael S. Tsirkin<[email protected]>Signed-off-by Jason Wang<[email protected]>Specifically this patch is the one causing issues.
Yes, this is because it tries to enable device IOTLB for vhost-user. Will post a new version for this patch.
Thanks
Quoted 38 lines · Jason Wang
Hi all:
As the userspace vitio driver became popular, more and more request were received for secure DMA environemt (DMAR). So this series tries to make DMAR works for virtio/vhost. The idea is let virtio/vhost co-work with userspace iommu implememtation. This is done through:
- for virtio, when platform supports IOMMU (
VIRTIO_F_IOMMU_PLATFORM), virtio will not assumeaddress_space_memory, instead a transport specific method were introduced for querying the dma address space and dma helpers were used in device emulation codes.
- for vhost, implement a device IOTLB by using device IOTLB API supported by recent kernel. With this API, vhost kernel can query IOTLB entry for a specified iova from qemu, qemu can invalidate an arbitrary range of iova in vhost kernel.The device IOTLB API is totaly architecture independent, an example implementation was done with intel iommu by:
- implement basic ATS (Address Translation Service) for virtio-pci, this will make device IOTLB visible for iommu driver in guest.
- implement device IOTLB descriptor processing in intel iommu (enabled through device-iotlb=on), and trigger the device IOTLB invalidation in vhost through iommu notifier.It could be easily ported to other IOMMU or architecture even if it doesn't support device IOTLB. (e.g just invalidate the vhost IOTLB during IOMMU IOTLB invalidation). But this will be slow since several devics were contending userspace IOTLB entries.
AMD IOMMU suppot for device IOTLB is ready, but it depends on other fixes to work correctly.
Test was done by:
- intel_iommu=on/strict in guest.
- vfio l2fwd in guest.
Quoted 7 lines
This main use case is the programs that use fixed mapping in guest (e.g dpdk). If 1G hugepage were used in guest, thanks to the SLLPS support, we can get 100% TLB hit rate for l2fwd in guest.
For the normal kernel driver which uses lots of dynamic mapping and unmapping, we may see performance penalty, this could be optimized in the future.
This causes make check failure with Broken pipe message. Probably an unexpected interaction with vhost-user.
Quoted 84 lines
TODO:
- more platforms and IOMMU support (done but block by other bugs of AMD IOMMU)
- performance optimizations (e.g merging adjacent mappings)
- non ATS support (userspace IOTLB snooping)
- using ATSCtl to disable Device IOTLBChanges from V3:
- rebase to HEAD
- fill all entry of IOMMUTLBEntry before notifyChanges from V2:
- rebase to HEAD
- avoid queryingdma_aseach time by usingvdev->dma_asdirectly
- fix and improveaddress_space_get_iotlb_entry()
- drop patch 1 (which has been posted as an independent fix)
- fix ECAP when device-iotlb=off
- fix centos6 buildChanges from V1:
- rebase to HEAD
- avoid calling transport specific dma as fetching method each time by caching it in vdev
- convert to use new IOMMU notifier API
- silent checkpatch warnings and fix 32bit build
- use "device-iotlb" instead of "device_iotlb"
- renamevirtio_memory_map()tovhost_memory_map()and move it tovhost.c
- usememory_region_is_iommu()instead of inventing new oneChanges from RFC:
- rebase to HEAD
- switch to use new vhost device IOTLB API
- use the new feature bitVIRITO_F_IOMMU_PLATFORM
- finalize basic ATS implementation
- add ATSR for Root port ATS transaction
- fix the iommu notifier handling during unregistering
- usesnprintf()in patch 3
- correc the loop inaddress_space_get_iotlb_entry()
- small tweak on the address calculation during device iotlb descriptor processing.Jason Wang (10):
virtio: convert to use DMA api
intel_iommu: name vtd address space with devfn
intel_iommu: allocate new key when creating new address space
exec: introduceaddress_space_get_iotlb_entry()
intel_iommu: support device iotlb descriptor
virtio-pci: address space translation service (ATS) support
acpi: add ATSR for q35
memory: handle alias for iommu notifier
memory: handle alias inmemory_region_is_iommu()
vhost_net: device IOTLB supportexec.c | 33 ++++++ hw/block/virtio-blk.c | 2 +- hw/char/virtio-serial-bus.c | 3 +- hw/i386/acpi-build.c | 9 ++ hw/i386/intel_iommu.c | 92 +++++++++++++-- hw/i386/intel_iommu_internal.h | 13 ++- hw/i386/x86-iommu.c | 17 +++ hw/pci/pcie.c | 16 +++ hw/scsi/virtio-scsi.c | 4 +- hw/virtio/vhost-backend.c | 99 +++++++++++++++++ hw/virtio/vhost.c | 178 ++++++++++++++++++++++++++---- hw/virtio/virtio-bus.c | 8 ++ hw/virtio/virtio-pci.c | 21 ++++ hw/virtio/virtio-pci.h | 4 + hw/virtio/virtio.c | 57 ++++++---- include/exec/memory.h | 8 ++ include/hw/acpi/acpi-defs.h | 12 ++ include/hw/i386/x86-iommu.h | 1 + include/hw/pci/pcie.h | 4 + include/hw/virtio/vhost-backend.h | 13 +++ include/hw/virtio/vhost.h | 4 + include/hw/virtio/virtio-access.h | 31 ++++-- include/hw/virtio/virtio-bus.h | 1 + include/hw/virtio/virtio.h | 9 +- include/standard-headers/linux/pci_regs.h | 1 + memory.c | 9 ++ net/tap.c | 1 + 27 files changed, 582 insertions(+), 68 deletions(-)Signature · 1 line
2.7.4