This patch extend the ATTACH/DETACH command handling to call iommu-notifier to map/unmap the memory region in IOMMU using vfio. This replay existing address space mappings on attach command and remove existing address space mappings on detach command.
Signed-off-by: Bharat Bhushan <bharat.bhus...@nxp.com> Signed-off-by: Eric Auger <eric.au...@redhat.com> --- v4->v5: - Rebase to v9 version from Eric - PCIe device hotplug fix hw/virtio/virtio-iommu.c | 47 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 613a77521d..7e8149e719 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -131,8 +131,44 @@ static void virtio_iommu_notify_unmap(IOMMUMemoryRegion *mr, hwaddr iova, memory_region_notify_iommu(mr, 0, entry); } +static gboolean virtio_iommu_mapping_unmap(gpointer key, gpointer value, + gpointer data) +{ + viommu_mapping *mapping = (viommu_mapping *) value; + IOMMUMemoryRegion *mr = (IOMMUMemoryRegion *) data; + + virtio_iommu_notify_unmap(mr, mapping->virt_addr, mapping->size); + + return false; +} + +static gboolean virtio_iommu_mapping_map(gpointer key, gpointer value, + gpointer data) +{ + viommu_mapping *mapping = (viommu_mapping *) value; + IOMMUMemoryRegion *mr = (IOMMUMemoryRegion *) data; + + virtio_iommu_notify_map(mr, mapping->virt_addr, mapping->phys_addr, + mapping->size); + + return false; +} + static void virtio_iommu_detach_endpoint_from_domain(viommu_endpoint *ep) { + VirtioIOMMUNotifierNode *node; + VirtIOIOMMU *s = ep->viommu; + viommu_domain *domain = ep->domain; + uint32_t sid; + + QLIST_FOREACH(node, &s->notifiers_list, next) { + sid = virtio_iommu_get_sid(node->iommu_dev); + if (ep->id == sid) { + g_tree_foreach(domain->mappings, virtio_iommu_mapping_unmap, + &node->iommu_dev->iommu_mr); + } + } + QLIST_REMOVE(ep, next); ep->domain = NULL; } @@ -280,8 +316,10 @@ static int virtio_iommu_attach(VirtIOIOMMU *s, { uint32_t domain_id = le32_to_cpu(req->domain); uint32_t ep_id = le32_to_cpu(req->endpoint); + VirtioIOMMUNotifierNode *node; viommu_domain *domain; viommu_endpoint *ep; + uint32_t sid; trace_virtio_iommu_attach(domain_id, ep_id); @@ -300,6 +338,15 @@ static int virtio_iommu_attach(VirtIOIOMMU *s, ep->domain = domain; g_tree_ref(domain->mappings); + /* replay existing address space mappings on the associated mr */ + QLIST_FOREACH(node, &s->notifiers_list, next) { + sid = virtio_iommu_get_sid(node->iommu_dev); + if (ep->id == sid) { + g_tree_foreach(domain->mappings, virtio_iommu_mapping_map, + &node->iommu_dev->iommu_mr); + } + } + return VIRTIO_IOMMU_S_OK; } -- 2.19.1