From: Bharat Bhushan <bbhushan2@marvell.com>
Extend VIRTIO_IOMMU_T_MAP/UNMAP request to notify memory listeners. It
will call VFIO notifier to map/unmap regions in the physical IOMMU.
Signed-off-by: Bharat Bhushan <bbhushan2@marvell.com>
Signed-off-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
---
v10:
* Use the flags from IOMMUMemoryRegion
* Pass virt_start/virt_end rather than size, to avoid dealing with
overflow and for consistency.
---
hw/virtio/virtio-iommu.c | 53 ++++++++++++++++++++++++++++++++++++++++
hw/virtio/trace-events | 2 ++
2 files changed, 55 insertions(+)
diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c
index 33115e82186..fcdf3a819f8 100644
--- a/hw/virtio/virtio-iommu.c
+++ b/hw/virtio/virtio-iommu.c
@@ -125,6 +125,49 @@ static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
}
}
+static void virtio_iommu_notify_map(IOMMUMemoryRegion *mr, hwaddr virt_start,
+ hwaddr virt_end, hwaddr paddr)
+{
+ IOMMUTLBEntry entry;
+ IOMMUNotifierFlag flags = mr->iommu_notify_flags;
+
+ if (!(flags & IOMMU_NOTIFIER_MAP)) {
+ return;
+ }
+
+ trace_virtio_iommu_notify_map(mr->parent_obj.name, virt_start, virt_end,
+ paddr);
+
+ entry.target_as = &address_space_memory;
+ entry.addr_mask = virt_end - virt_start;
+ entry.iova = virt_start;
+ entry.perm = IOMMU_RW;
+ entry.translated_addr = paddr;
+
+ memory_region_notify_iommu(mr, 0, entry);
+}
+
+static void virtio_iommu_notify_unmap(IOMMUMemoryRegion *mr, hwaddr virt_start,
+ hwaddr virt_end)
+{
+ IOMMUTLBEntry entry;
+ IOMMUNotifierFlag flags = mr->iommu_notify_flags;
+
+ if (!(flags & IOMMU_NOTIFIER_UNMAP)) {
+ return;
+ }
+
+ trace_virtio_iommu_notify_unmap(mr->parent_obj.name, virt_start, virt_end);
+
+ entry.target_as = &address_space_memory;
+ entry.addr_mask = virt_end - virt_start;
+ entry.iova = virt_start;
+ entry.perm = IOMMU_NONE;
+ entry.translated_addr = 0;
+
+ memory_region_notify_iommu(mr, 0, entry);
+}
+
static void virtio_iommu_detach_endpoint_from_domain(VirtIOIOMMUEndpoint *ep)
{
if (!ep->domain) {
@@ -315,6 +358,7 @@ static int virtio_iommu_map(VirtIOIOMMU *s,
VirtIOIOMMUDomain *domain;
VirtIOIOMMUInterval *interval;
VirtIOIOMMUMapping *mapping;
+ VirtIOIOMMUEndpoint *ep;
if (flags & ~VIRTIO_IOMMU_MAP_F_MASK) {
return VIRTIO_IOMMU_S_INVAL;
@@ -344,6 +388,10 @@ static int virtio_iommu_map(VirtIOIOMMU *s,
g_tree_insert(domain->mappings, interval, mapping);
+ QLIST_FOREACH(ep, &domain->endpoint_list, next) {
+ virtio_iommu_notify_map(ep->iommu_mr, virt_start, virt_end, phys_start);
+ }
+
return VIRTIO_IOMMU_S_OK;
}
@@ -356,6 +404,7 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
VirtIOIOMMUMapping *iter_val;
VirtIOIOMMUInterval interval, *iter_key;
VirtIOIOMMUDomain *domain;
+ VirtIOIOMMUEndpoint *ep;
int ret = VIRTIO_IOMMU_S_OK;
trace_virtio_iommu_unmap(domain_id, virt_start, virt_end);
@@ -373,6 +422,10 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
uint64_t current_high = iter_key->high;
if (interval.low <= current_low && interval.high >= current_high) {
+ QLIST_FOREACH(ep, &domain->endpoint_list, next) {
+ virtio_iommu_notify_unmap(ep->iommu_mr, current_low,
+ current_high);
+ }
g_tree_remove(domain->mappings, iter_key);
trace_virtio_iommu_unmap_done(domain_id, current_low, current_high);
} else {
diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events
index cf1e59de302..65a48555c78 100644
--- a/hw/virtio/trace-events
+++ b/hw/virtio/trace-events
@@ -106,6 +106,8 @@ virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d"
virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"
virtio_iommu_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoint, uint64_t addr) "FAULT reason=%d flags=%d endpoint=%d address =0x%"PRIx64
virtio_iommu_fill_resv_property(uint32_t devid, uint8_t subtype, uint64_t start, uint64_t end) "dev= %d, type=%d start=0x%"PRIx64" end=0x%"PRIx64
+virtio_iommu_notify_map(const char *name, uint64_t virt_start, uint64_t virt_end, uint64_t phys_start) "mr=%s virt_start=0x%"PRIx64" virt_end=0x%"PRIx64" phys_start=0x%"PRIx64
+virtio_iommu_notify_unmap(const char *name, uint64_t virt_start, uint64_t virt_end) "mr=%s virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
# virtio-mem.c
virtio_mem_send_response(uint16_t type) "type=%" PRIu16
--
2.28.0
Hi Jean,
On 10/8/20 7:15 PM, Jean-Philippe Brucker wrote:
> From: Bharat Bhushan <bbhushan2@marvell.com>
>
> Extend VIRTIO_IOMMU_T_MAP/UNMAP request to notify memory listeners. It
> will call VFIO notifier to map/unmap regions in the physical IOMMU.
>
> Signed-off-by: Bharat Bhushan <bbhushan2@marvell.com>
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
> ---
> v10:
> * Use the flags from IOMMUMemoryRegion
> * Pass virt_start/virt_end rather than size, to avoid dealing with
> overflow and for consistency.
> ---
> hw/virtio/virtio-iommu.c | 53 ++++++++++++++++++++++++++++++++++++++++
> hw/virtio/trace-events | 2 ++
> 2 files changed, 55 insertions(+)
>
> diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c
> index 33115e82186..fcdf3a819f8 100644
> --- a/hw/virtio/virtio-iommu.c
> +++ b/hw/virtio/virtio-iommu.c
> @@ -125,6 +125,49 @@ static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
> }
> }
>
> +static void virtio_iommu_notify_map(IOMMUMemoryRegion *mr, hwaddr virt_start,
> + hwaddr virt_end, hwaddr paddr)
> +{
> + IOMMUTLBEntry entry;
> + IOMMUNotifierFlag flags = mr->iommu_notify_flags;
> +
> + if (!(flags & IOMMU_NOTIFIER_MAP)) {
> + return;
> + }
> +
> + trace_virtio_iommu_notify_map(mr->parent_obj.name, virt_start, virt_end,
> + paddr);
> +
> + entry.target_as = &address_space_memory;
> + entry.addr_mask = virt_end - virt_start;
> + entry.iova = virt_start;
> + entry.perm = IOMMU_RW;
logically you should be able to cascade the struct virtio_iommu_req_map
*req flags field instead.
> + entry.translated_addr = paddr;
> +
> + memory_region_notify_iommu(mr, 0, entry);
> +}
> +
> +static void virtio_iommu_notify_unmap(IOMMUMemoryRegion *mr, hwaddr virt_start,
> + hwaddr virt_end)
> +{
> + IOMMUTLBEntry entry;
> + IOMMUNotifierFlag flags = mr->iommu_notify_flags;
> +
> + if (!(flags & IOMMU_NOTIFIER_UNMAP)) {
> + return;
> + }
> +
> + trace_virtio_iommu_notify_unmap(mr->parent_obj.name, virt_start, virt_end);
> +
> + entry.target_as = &address_space_memory;
> + entry.addr_mask = virt_end - virt_start;
> + entry.iova = virt_start;
> + entry.perm = IOMMU_NONE;
> + entry.translated_addr = 0;
> +
> + memory_region_notify_iommu(mr, 0, entry);
> +}
> +
> static void virtio_iommu_detach_endpoint_from_domain(VirtIOIOMMUEndpoint *ep)
> {
> if (!ep->domain) {
> @@ -315,6 +358,7 @@ static int virtio_iommu_map(VirtIOIOMMU *s,
> VirtIOIOMMUDomain *domain;
> VirtIOIOMMUInterval *interval;
> VirtIOIOMMUMapping *mapping;
> + VirtIOIOMMUEndpoint *ep;
>
> if (flags & ~VIRTIO_IOMMU_MAP_F_MASK) {
> return VIRTIO_IOMMU_S_INVAL;
> @@ -344,6 +388,10 @@ static int virtio_iommu_map(VirtIOIOMMU *s,
>
> g_tree_insert(domain->mappings, interval, mapping);
>
> + QLIST_FOREACH(ep, &domain->endpoint_list, next) {
> + virtio_iommu_notify_map(ep->iommu_mr, virt_start, virt_end, phys_start);
> + }
> +
> return VIRTIO_IOMMU_S_OK;
> }
>
> @@ -356,6 +404,7 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
> VirtIOIOMMUMapping *iter_val;
> VirtIOIOMMUInterval interval, *iter_key;
> VirtIOIOMMUDomain *domain;
> + VirtIOIOMMUEndpoint *ep;
> int ret = VIRTIO_IOMMU_S_OK;
>
> trace_virtio_iommu_unmap(domain_id, virt_start, virt_end);
> @@ -373,6 +422,10 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
> uint64_t current_high = iter_key->high;
>
> if (interval.low <= current_low && interval.high >= current_high) {
> + QLIST_FOREACH(ep, &domain->endpoint_list, next) {
> + virtio_iommu_notify_unmap(ep->iommu_mr, current_low,
> + current_high);
> + }
> g_tree_remove(domain->mappings, iter_key);
> trace_virtio_iommu_unmap_done(domain_id, current_low, current_high);
> } else {
> diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events
> index cf1e59de302..65a48555c78 100644
> --- a/hw/virtio/trace-events
> +++ b/hw/virtio/trace-events
> @@ -106,6 +106,8 @@ virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d"
> virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"
> virtio_iommu_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoint, uint64_t addr) "FAULT reason=%d flags=%d endpoint=%d address =0x%"PRIx64
> virtio_iommu_fill_resv_property(uint32_t devid, uint8_t subtype, uint64_t start, uint64_t end) "dev= %d, type=%d start=0x%"PRIx64" end=0x%"PRIx64
> +virtio_iommu_notify_map(const char *name, uint64_t virt_start, uint64_t virt_end, uint64_t phys_start) "mr=%s virt_start=0x%"PRIx64" virt_end=0x%"PRIx64" phys_start=0x%"PRIx64
> +virtio_iommu_notify_unmap(const char *name, uint64_t virt_start, uint64_t virt_end) "mr=%s virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
>
> # virtio-mem.c
> virtio_mem_send_response(uint16_t type) "type=%" PRIu16
>
Besides it looks good to me.
Thanks
Eric
On Fri, Oct 16, 2020 at 09:58:28AM +0200, Auger Eric wrote:
> > +static void virtio_iommu_notify_map(IOMMUMemoryRegion *mr, hwaddr virt_start,
> > + hwaddr virt_end, hwaddr paddr)
> > +{
> > + IOMMUTLBEntry entry;
> > + IOMMUNotifierFlag flags = mr->iommu_notify_flags;
> > +
> > + if (!(flags & IOMMU_NOTIFIER_MAP)) {
> > + return;
> > + }
> > +
> > + trace_virtio_iommu_notify_map(mr->parent_obj.name, virt_start, virt_end,
> > + paddr);
> > +
> > + entry.target_as = &address_space_memory;
> > + entry.addr_mask = virt_end - virt_start;
> > + entry.iova = virt_start;
> > + entry.perm = IOMMU_RW;
> logically you should be able to cascade the struct virtio_iommu_req_map
> *req flags field instead.
Agreed.
I'm also thinking of adding a check for VIRTIO_IOMMU_MAP_F_MMIO, to avoid
going further into the notifier and maybe do the same for unmap.
Thanks,
Jean
© 2016 - 2025 Red Hat, Inc.