Skip to contentSkip to conversation

virtio-iommu: Support VIRTIO_IOMMU_F_BYPASS_CONFIG

PATCH Superseded Jean-Philippe Brucker 3 participants 12 messages Sep 30, 2021 – Jan 11, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover virtio-iommu: Support VIRTIO_IOMMU_F_BYPASS_CONFIG

Replace the VIRTIO_IOMMU_F_BYPASS feature with VIRTIO_IOMMU_F_BYPASS_CONFIG, which enables a config space bit to switch global bypass on and off.

Add a boot-bypass option, which defaults to 'on' to be in line with other vIOMMUs and to allow running firmware/bootloader that are unaware of the IOMMU.

See the spec change for more rationale
https://lists.oasis-open.org/archives/virtio-dev/202109/msg00137.html

Jean-Philippe Brucker (3):
NOMERGE: virtio-iommu: Add definitions for
VIRTIO_IOMMU_F_BYPASS_CONFIG
virtio-iommu: Default to bypass during boot
virtio-iommu: Support bypass domain

 include/hw/virtio/virtio-iommu.h              |  1 +
 include/standard-headers/linux/virtio_iommu.h | 10 +++-
 hw/virtio/virtio-iommu.c                      | 60 ++++++++++++++++---
 hw/virtio/trace-events                        |  4 +-
 4 files changed, 64 insertions(+), 11 deletions(-)
Signature · 1 line
2.33.0
#1 lore raw

PATCH 1/3 NOMERGE: virtio-iommu: Add definitions for VIRTIO_IOMMU_F_BYPASS_CONFIG

Pull VIRTIO_IOMMU_F_BYPASS_CONFIG changes from Linux (not upstream yet).

---
 include/standard-headers/linux/virtio_iommu.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
include/standard-headers/linux/virtio_iommu.h +8 -2
index b9443b83a1..d14808e3fb 100644
@@ -13,9 +13,10 @@
 #define VIRTIO_IOMMU_F_INPUT_RANGE		0
 #define VIRTIO_IOMMU_F_DOMAIN_RANGE		1
 #define VIRTIO_IOMMU_F_MAP_UNMAP		2
-#define VIRTIO_IOMMU_F_BYPASS			3
+#define VIRTIO_IOMMU_F_BYPASS			3 /* Deprecated */
 #define VIRTIO_IOMMU_F_PROBE			4
 #define VIRTIO_IOMMU_F_MMIO			5
+#define VIRTIO_IOMMU_F_BYPASS_CONFIG		6
 
 struct virtio_iommu_range_64 {
 	uint64_t					start;
@@ -36,6 +37,8 @@ struct virtio_iommu_config {
 	struct virtio_iommu_range_32		domain_range;
 	/* Probe buffer size */
 	uint32_t					probe_size;
+	uint8_t					bypass;
+	uint8_t					reserved[7];
 };
 
 /* Request types */
@@ -66,11 +69,14 @@ struct virtio_iommu_req_tail {
 	uint8_t					reserved[3];
 };
 
+#define VIRTIO_IOMMU_ATTACH_F_BYPASS		(1 << 0)
+
 struct virtio_iommu_req_attach {
 	struct virtio_iommu_req_head		head;
 	uint32_t					domain;
 	uint32_t					endpoint;
-	uint8_t					reserved[8];
+	uint32_t					flags;
+	uint8_t					reserved[4];
 	struct virtio_iommu_req_tail		tail;
 };
 
Signature · 1 line
2.33.0
#2 lore raw

PATCH 2/3 virtio-iommu: Default to bypass during boot

In reply to Jean-Philippe Brucker

Currently the virtio-iommu device must be programmed before it allows DMA from any PCI device. This can make the VM entirely unusable when a virtio-iommu driver isn't present, for example in a bootloader that loads the OS from storage.

Similarly to the other vIOMMU implementations, default to DMA bypassing the IOMMU during boot. Add a "boot-bypass" option that lets users change this behavior.

---
 include/hw/virtio/virtio-iommu.h |  1 +
 hw/virtio/virtio-iommu.c         | 28 +++++++++++++++++++++++-----
 hw/virtio/trace-events           |  4 ++--
 3 files changed, 26 insertions(+), 7 deletions(-)
include/hw/virtio/virtio-iommu.h +1 -0
index 273e35c04b..4c66989ca4 100644
@@ -58,6 +58,7 @@ struct VirtIOIOMMU {
     GTree *domains;
     QemuMutex mutex;
     GTree *endpoints;
+    bool boot_bypass;
 };
 
 #endif
hw/virtio/virtio-iommu.c +23 -5
index 1b23e8e18c..82edeaa101 100644
@@ -728,8 +728,7 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
         .perm = IOMMU_NONE,
     };
 
-    bypass_allowed = virtio_vdev_has_feature(&s->parent_obj,
-                                             VIRTIO_IOMMU_F_BYPASS);
+    bypass_allowed = s->config.bypass;
 
     sid = virtio_iommu_get_bdf(sdev);
 
@@ -828,7 +827,8 @@ static void virtio_iommu_get_config(VirtIODevice *vdev, uint8_t *config_data)
                                   config->input_range.start,
                                   config->input_range.end,
                                   config->domain_range.end,
-                                  config->probe_size);
+                                  config->probe_size,
+                                  config->bypass);
     memcpy(config_data, &dev->config, sizeof(struct virtio_iommu_config));
 }
 
@@ -836,13 +836,29 @@ static void virtio_iommu_set_config(VirtIODevice *vdev,
                                       const uint8_t *config_data)
 {
     struct virtio_iommu_config config;
+    VirtIOIOMMU *dev = VIRTIO_IOMMU(vdev);
 
     memcpy(&config, config_data, sizeof(struct virtio_iommu_config));
+
+    if (config.bypass != dev->config.bypass) {
+        if (!virtio_vdev_has_feature(vdev, VIRTIO_IOMMU_F_BYPASS_CONFIG)) {
+            virtio_error(vdev, "cannot set config.bypass");
+            return;
+        }
+        if (config.bypass != 0 && config.bypass != 1) {
+            warn_report("invalid config.bypass value '%d'", config.bypass);
+            dev->config.bypass = 0;
+            return;
+        }
+        dev->config.bypass = config.bypass;
+    }
+
     trace_virtio_iommu_set_config(config.page_size_mask,
                                   config.input_range.start,
                                   config.input_range.end,
                                   config.domain_range.end,
-                                  config.probe_size);
+                                  config.probe_size,
+                                  config.bypass);
 }
 
 static uint64_t virtio_iommu_get_features(VirtIODevice *vdev, uint64_t f,
@@ -986,6 +1002,7 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
     s->config.input_range.end = -1UL;
     s->config.domain_range.end = 32;
     s->config.probe_size = VIOMMU_PROBE_SIZE;
+    s->config.bypass = s->boot_bypass;
 
     virtio_add_feature(&s->features, VIRTIO_RING_F_EVENT_IDX);
     virtio_add_feature(&s->features, VIRTIO_RING_F_INDIRECT_DESC);
@@ -993,9 +1010,9 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_INPUT_RANGE);
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_DOMAIN_RANGE);
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MAP_UNMAP);
-    virtio_add_feature(&s->features, VIRTIO_IOMMU_F_BYPASS);
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MMIO);
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_PROBE);
+    virtio_add_feature(&s->features, VIRTIO_IOMMU_F_BYPASS_CONFIG);
 
     qemu_mutex_init(&s->mutex);
 
@@ -1169,6 +1186,7 @@ static const VMStateDescription vmstate_virtio_iommu = {
 
 static Property virtio_iommu_properties[] = {
     DEFINE_PROP_LINK("primary-bus", VirtIOIOMMU, primary_bus, "PCI", PCIBus *),
+    DEFINE_PROP_BOOL("boot-bypass", VirtIOIOMMU, boot_bypass, true),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/virtio/trace-events +2 -2
index 8ed19e9d0c..6bc3821ba3 100644
@@ -90,8 +90,8 @@ virtio_mmio_setting_irq(int level) "virtio_mmio setting IRQ %d"
 virtio_iommu_device_reset(void) "reset!"
 virtio_iommu_get_features(uint64_t features) "device supports features=0x%"PRIx64
 virtio_iommu_device_status(uint8_t status) "driver status = %d"
-virtio_iommu_get_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_range=%d probe_size=0x%x"
-virtio_iommu_set_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_bits=%d probe_size=0x%x"
+virtio_iommu_get_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size, uint8_t bypass) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_range=%d probe_size=0x%x bypass=0x%x"
+virtio_iommu_set_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size, uint8_t bypass) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_bits=%d probe_size=0x%x bypass=0x%x"
 virtio_iommu_attach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
 virtio_iommu_detach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
 virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end, uint64_t phys_start, uint32_t flags) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64 " phys_start=0x%"PRIx64" flags=%d"
Signature · 1 line
2.33.0
#3 lore raw

Re: [PATCH 2/3] virtio-iommu: Default to bypass during boot

Hi jean,

Quoted 8 lines · Jean-Philippe Brucker

Currently the virtio-iommu device must be programmed before it allows DMA from any PCI device. This can make the VM entirely unusable when a virtio-iommu driver isn't present, for example in a bootloader that loads the OS from storage.

Similarly to the other vIOMMU implementations, default to DMA bypassing the IOMMU during boot. Add a "boot-bypass" option that lets users change this behavior.

s/option/property
set to true by default.

Also mention this deprecates the legacy VIRTIO_IOMMU_F_BYPASS feature

Quoted 50 lines
---
 include/hw/virtio/virtio-iommu.h |  1 +
 hw/virtio/virtio-iommu.c         | 28 +++++++++++++++++++++++-----
 hw/virtio/trace-events           |  4 ++--
 3 files changed, 26 insertions(+), 7 deletions(-)
include/hw/virtio/virtio-iommu.h +1 -0
index 273e35c04b..4c66989ca4 100644
@@ -58,6 +58,7 @@ struct VirtIOIOMMU {
     GTree *domains;
     QemuMutex mutex;
     GTree *endpoints;
+    bool boot_bypass;
 };
 
 #endif
hw/virtio/virtio-iommu.c +4 -3
index 1b23e8e18c..82edeaa101 100644
@@ -728,8 +728,7 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
         .perm = IOMMU_NONE,
     };
 
-    bypass_allowed = virtio_vdev_has_feature(&s->parent_obj,
-                                             VIRTIO_IOMMU_F_BYPASS);
+    bypass_allowed = s->config.bypass;
 
     sid = virtio_iommu_get_bdf(sdev);
 
@@ -828,7 +827,8 @@ static void virtio_iommu_get_config(VirtIODevice *vdev, uint8_t *config_data)
                                   config->input_range.start,
                                   config->input_range.end,
                                   config->domain_range.end,
-                                  config->probe_size);
+                                  config->probe_size,
+                                  config->bypass);
     memcpy(config_data, &dev->config, sizeof(struct virtio_iommu_config));
 }
 
@@ -836,13 +836,29 @@ static void virtio_iommu_set_config(VirtIODevice *vdev,
                                       const uint8_t *config_data)
 {
     struct virtio_iommu_config config;
+    VirtIOIOMMU *dev = VIRTIO_IOMMU(vdev);
 

hw/virtio/virtio-iommu.c static void virtio_iommu_set_config(VirtIODevice *vdev, · line 850

Quoted 10 lines
hw/virtio/virtio-iommu.c +9 -0
     memcpy(&config, config_data, sizeof(struct virtio_iommu_config));
+
+    if (config.bypass != dev->config.bypass) {
+        if (!virtio_vdev_has_feature(vdev, VIRTIO_IOMMU_F_BYPASS_CONFIG)) {
+            virtio_error(vdev, "cannot set config.bypass");
+            return;
+        }
+        if (config.bypass != 0 && config.bypass != 1) {
+            warn_report("invalid config.bypass value '%d'", config.bypass);
+            dev->config.bypass = 0;

Shouldn't we leave the old value instead. The spec proposal says it takes 0 though.

Quoted 47 lines
hw/virtio/virtio-iommu.c +10 -2
+            return;
+        }
+        dev->config.bypass = config.bypass;
+    }
+
     trace_virtio_iommu_set_config(config.page_size_mask,
                                   config.input_range.start,
                                   config.input_range.end,
                                   config.domain_range.end,
-                                  config.probe_size);
+                                  config.probe_size,
+                                  config.bypass);
 }
 
 static uint64_t virtio_iommu_get_features(VirtIODevice *vdev, uint64_t f,
@@ -986,6 +1002,7 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
     s->config.input_range.end = -1UL;
     s->config.domain_range.end = 32;
     s->config.probe_size = VIOMMU_PROBE_SIZE;
+    s->config.bypass = s->boot_bypass;
 
     virtio_add_feature(&s->features, VIRTIO_RING_F_EVENT_IDX);
     virtio_add_feature(&s->features, VIRTIO_RING_F_INDIRECT_DESC);
@@ -993,9 +1010,9 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_INPUT_RANGE);
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_DOMAIN_RANGE);
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MAP_UNMAP);
-    virtio_add_feature(&s->features, VIRTIO_IOMMU_F_BYPASS);
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_MMIO);
     virtio_add_feature(&s->features, VIRTIO_IOMMU_F_PROBE);
+    virtio_add_feature(&s->features, VIRTIO_IOMMU_F_BYPASS_CONFIG);
 
     qemu_mutex_init(&s->mutex);
 
@@ -1169,6 +1186,7 @@ static const VMStateDescription vmstate_virtio_iommu = {
 
 static Property virtio_iommu_properties[] = {
     DEFINE_PROP_LINK("primary-bus", VirtIOIOMMU, primary_bus, "PCI", PCIBus *),
+    DEFINE_PROP_BOOL("boot-bypass", VirtIOIOMMU, boot_bypass, true),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/virtio/trace-events +0 -0
index 8ed19e9d0c..6bc3821ba3 100644

hw/virtio/trace-events virtio_mmio_setting_irq(int level) "virtio_mmio setting IRQ %d" · line 97

Quoted 10 lines
hw/virtio/trace-events +2 -2
@@ -90,8 +90,8 @@ virtio_mmio_setting_irq(int level) "virtio_mmio setting IRQ %d"
 virtio_iommu_device_reset(void) "reset!"
 virtio_iommu_get_features(uint64_t features) "device supports features=0x%"PRIx64
 virtio_iommu_device_status(uint8_t status) "driver status = %d"
-virtio_iommu_get_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_range=%d probe_size=0x%x"
-virtio_iommu_set_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_bits=%d probe_size=0x%x"
+virtio_iommu_get_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size, uint8_t bypass) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_range=%d probe_size=0x%x bypass=0x%x"
+virtio_iommu_set_config(uint64_t page_size_mask, uint64_t start, uint64_t end, uint32_t domain_range, uint32_t probe_size, uint8_t bypass) "page_size_mask=0x%"PRIx64" start=0x%"PRIx64" end=0x%"PRIx64" domain_bits=%d probe_size=0x%x bypass=0x%x"
 virtio_iommu_attach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
 virtio_iommu_detach(uint32_t domain_id, uint32_t ep_id) "domain=%d endpoint=%d"
 virtio_iommu_map(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end, uint64_t phys_start, uint32_t flags) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64 " phys_start=0x%"PRIx64" flags=%d"

Thanks

Eric

#4 lore raw

Re: [PATCH 2/3] virtio-iommu: Default to bypass during boot

Quoted 10 lines · Eric Auger

hw/virtio/virtio-iommu.c static void virtio_iommu_set_config(VirtIODevice *vdev, · line 850

Quoted 8 lines
hw/virtio/virtio-iommu.c +8 -0
+    if (config.bypass != dev->config.bypass) {
+        if (!virtio_vdev_has_feature(vdev, VIRTIO_IOMMU_F_BYPASS_CONFIG)) {
+            virtio_error(vdev, "cannot set config.bypass");
+            return;
+        }
+        if (config.bypass != 0 && config.bypass != 1) {
+            warn_report("invalid config.bypass value '%d'", config.bypass);
+            dev->config.bypass = 0;

Shouldn't we leave the old value instead. The spec proposal says it takes 0 though.

Yes keeping the old value is better, I'll change the spec. Writing a different value is a driver bug since new values for the bypass field will be keyed off a new feature bit.

Thanks,
Jean

#5 lore raw

PATCH 3/3 virtio-iommu: Support bypass domain

In reply to Jean-Philippe Brucker

The driver can create a bypass domain by passing the VIRTIO_IOMMU_ATTACH_F_BYPASS flag on the ATTACH request. Bypass domains perform slightly better than domains with identity mappings since they skip translation.

---
 hw/virtio/virtio-iommu.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
hw/virtio/virtio-iommu.c +30 -2
index 82edeaa101..4f0207a3eb 100644
@@ -42,6 +42,7 @@
 
 typedef struct VirtIOIOMMUDomain {
     uint32_t id;
+    bool bypass;
     GTree *mappings;
     QLIST_HEAD(, VirtIOIOMMUEndpoint) endpoint_list;
 } VirtIOIOMMUDomain;
@@ -257,12 +258,16 @@ static void virtio_iommu_put_endpoint(gpointer data)
 }
 
 static VirtIOIOMMUDomain *virtio_iommu_get_domain(VirtIOIOMMU *s,
-                                                  uint32_t domain_id)
+                                                  uint32_t domain_id,
+                                                  bool bypass)
 {
     VirtIOIOMMUDomain *domain;
 
     domain = g_tree_lookup(s->domains, GUINT_TO_POINTER(domain_id));
     if (domain) {
+        if (domain->bypass != bypass) {
+            return NULL;
+        }
         return domain;
     }
     domain = g_malloc0(sizeof(*domain));
@@ -270,6 +275,7 @@ static VirtIOIOMMUDomain *virtio_iommu_get_domain(VirtIOIOMMU *s,
     domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
                                    NULL, (GDestroyNotify)g_free,
                                    (GDestroyNotify)g_free);
+    domain->bypass = bypass;
     g_tree_insert(s->domains, GUINT_TO_POINTER(domain_id), domain);
     QLIST_INIT(&domain->endpoint_list);
     trace_virtio_iommu_get_domain(domain_id);
@@ -333,11 +339,16 @@ 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);
+    uint32_t flags = le32_to_cpu(req->flags);
     VirtIOIOMMUDomain *domain;
     VirtIOIOMMUEndpoint *ep;
 
     trace_virtio_iommu_attach(domain_id, ep_id);
 
+    if (flags & ~VIRTIO_IOMMU_ATTACH_F_BYPASS) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
     ep = virtio_iommu_get_endpoint(s, ep_id);
     if (!ep) {
         return VIRTIO_IOMMU_S_NOENT;
@@ -355,7 +366,12 @@ static int virtio_iommu_attach(VirtIOIOMMU *s,
         }
     }
 
-    domain = virtio_iommu_get_domain(s, domain_id);
+    domain = virtio_iommu_get_domain(s, domain_id,
+                                     flags & VIRTIO_IOMMU_ATTACH_F_BYPASS);
+    if (!domain) {
+        /* Incompatible flags */
+        return VIRTIO_IOMMU_S_INVAL;
+    }
     QLIST_INSERT_HEAD(&domain->endpoint_list, ep, next);
 
     ep->domain = domain;
@@ -418,6 +434,10 @@ static int virtio_iommu_map(VirtIOIOMMU *s,
         return VIRTIO_IOMMU_S_NOENT;
     }
 
+    if (domain->bypass) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
     interval = g_malloc0(sizeof(*interval));
 
     interval->low = virt_start;
@@ -463,6 +483,11 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
     if (!domain) {
         return VIRTIO_IOMMU_S_NOENT;
     }
+
+    if (domain->bypass) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
     interval.low = virt_start;
     interval.high = virt_end;
 
@@ -779,6 +804,9 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
             entry.perm = flag;
         }
         goto unlock;
+    } else if (ep->domain->bypass) {
+        entry.perm = flag;
+        goto unlock;
     }
 
     found = g_tree_lookup_extended(ep->domain->mappings, (gpointer)(&interval),
Signature · 1 line
2.33.0
#6 lore raw

Re: [PATCH 3/3] virtio-iommu: Support bypass domain

Hi jean,

Quoted 66 lines · Jean-Philippe Brucker

The driver can create a bypass domain by passing the VIRTIO_IOMMU_ATTACH_F_BYPASS flag on the ATTACH request. Bypass domains perform slightly better than domains with identity mappings since they skip translation.

---
 hw/virtio/virtio-iommu.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
hw/virtio/virtio-iommu.c +12 -1
index 82edeaa101..4f0207a3eb 100644
@@ -42,6 +42,7 @@
 
 typedef struct VirtIOIOMMUDomain {
     uint32_t id;
+    bool bypass;
     GTree *mappings;
     QLIST_HEAD(, VirtIOIOMMUEndpoint) endpoint_list;
 } VirtIOIOMMUDomain;
@@ -257,12 +258,16 @@ static void virtio_iommu_put_endpoint(gpointer data)
 }
 
 static VirtIOIOMMUDomain *virtio_iommu_get_domain(VirtIOIOMMU *s,
-                                                  uint32_t domain_id)
+                                                  uint32_t domain_id,
+                                                  bool bypass)
 {
     VirtIOIOMMUDomain *domain;
 
     domain = g_tree_lookup(s->domains, GUINT_TO_POINTER(domain_id));
     if (domain) {
+        if (domain->bypass != bypass) {
+            return NULL;
+        }
         return domain;
     }
     domain = g_malloc0(sizeof(*domain));
@@ -270,6 +275,7 @@ static VirtIOIOMMUDomain *virtio_iommu_get_domain(VirtIOIOMMU *s,
     domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
                                    NULL, (GDestroyNotify)g_free,
                                    (GDestroyNotify)g_free);
+    domain->bypass = bypass;
     g_tree_insert(s->domains, GUINT_TO_POINTER(domain_id), domain);
     QLIST_INIT(&domain->endpoint_list);
     trace_virtio_iommu_get_domain(domain_id);
@@ -333,11 +339,16 @@ 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);
+    uint32_t flags = le32_to_cpu(req->flags);
     VirtIOIOMMUDomain *domain;
     VirtIOIOMMUEndpoint *ep;
 
     trace_virtio_iommu_attach(domain_id, ep_id);
 
+    if (flags & ~VIRTIO_IOMMU_ATTACH_F_BYPASS) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
     ep = virtio_iommu_get_endpoint(s, ep_id);
     if (!ep) {
         return VIRTIO_IOMMU_S_NOENT;

hw/virtio/virtio-iommu.c static int virtio_iommu_attach(VirtIOIOMMU *s, · line 372

Quoted 8 lines
hw/virtio/virtio-iommu.c +4 -1
@@ -355,7 +366,12 @@ static int virtio_iommu_attach(VirtIOIOMMU *s,
         }
     }
 
-    domain = virtio_iommu_get_domain(s, domain_id);
+    domain = virtio_iommu_get_domain(s, domain_id,
+                                     flags & VIRTIO_IOMMU_ATTACH_F_BYPASS);
+    if (!domain) {
+        /* Incompatible flags */

Incompatible bypass flag

Quoted 29 lines
hw/virtio/virtio-iommu.c +11 -0
+        return VIRTIO_IOMMU_S_INVAL;
+    }
     QLIST_INSERT_HEAD(&domain->endpoint_list, ep, next);
 
     ep->domain = domain;
@@ -418,6 +434,10 @@ static int virtio_iommu_map(VirtIOIOMMU *s,
         return VIRTIO_IOMMU_S_NOENT;
     }
 
+    if (domain->bypass) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
     interval = g_malloc0(sizeof(*interval));
 
     interval->low = virt_start;
@@ -463,6 +483,11 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s,
     if (!domain) {
         return VIRTIO_IOMMU_S_NOENT;
     }
+
+    if (domain->bypass) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
     interval.low = virt_start;
     interval.high = virt_end;
 

hw/virtio/virtio-iommu.c static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr, · line 812

Quoted 9 lines
hw/virtio/virtio-iommu.c +3 -0
@@ -779,6 +804,9 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,
             entry.perm = flag;
         }
         goto unlock;
+    } else if (ep->domain->bypass) {
+        entry.perm = flag;
+        goto unlock;
     }
 
     found = g_tree_lookup_extended(ep->domain->mappings, (gpointer)(&interval),

Otherwise looks good to me.

Eric

#7 lore raw
In reply to Jean-Philippe Brucker

Hi Jean, Michael,

Quoted 10 lines · Jean-Philippe Brucker

Replace the VIRTIO_IOMMU_F_BYPASS feature with VIRTIO_IOMMU_F_BYPASS_CONFIG, which enables a config space bit to switch global bypass on and off.

Add a boot-bypass option, which defaults to 'on' to be in line with other vIOMMUs and to allow running firmware/bootloader that are unaware of the IOMMU.

See the spec change for more rationale
https://lists.oasis-open.org/archives/virtio-dev/202109/msg00137.html

I guess the kernel bits should be merged in 5.17?

Thanks

Eric

Quoted 13 lines

Jean-Philippe Brucker (3):
NOMERGE: virtio-iommu: Add definitions for
VIRTIO_IOMMU_F_BYPASS_CONFIG
virtio-iommu: Default to bypass during boot
virtio-iommu: Support bypass domain

 include/hw/virtio/virtio-iommu.h              |  1 +
 include/standard-headers/linux/virtio_iommu.h | 10 +++-
 hw/virtio/virtio-iommu.c                      | 60 ++++++++++++++++---
 hw/virtio/trace-events                        |  4 +-
 4 files changed, 64 insertions(+), 11 deletions(-)
#8 lore raw

Hi Eric,

Quoted 7 lines · Eric Auger

Hi Jean, Michael,

Quoted 4 lines · Jean-Philippe Brucker

Replace the VIRTIO_IOMMU_F_BYPASS feature with VIRTIO_IOMMU_F_BYPASS_CONFIG, which enables a config space bit to switch global bypass on and off.

Quoted 8 lines
Quoted 6 lines

Add a boot-bypass option, which defaults to 'on' to be in line with other vIOMMUs and to allow running firmware/bootloader that are unaware of the IOMMU.

See the spec change for more rationale
https://lists.oasis-open.org/archives/virtio-dev/202109/msg00137.html

I guess the kernel bits should be merged in 5.17?

Yes, they should. I can resend at 5.17-rc1 along with a commit updating the linux headers. Or is there a specific process for synchronizing the headers? Looking at git log it looks like scripts/update-linux-headers.sh is run by whomever needs new UAPI features.

Thanks,
Jean

#9 lore raw

Hi Jean,

Quoted 14 lines · Jean-Philippe Brucker

Hi Eric,

Quoted 11 lines · Eric Auger

Hi Jean, Michael,

Quoted 8 lines · Jean-Philippe Brucker

Replace the VIRTIO_IOMMU_F_BYPASS feature with VIRTIO_IOMMU_F_BYPASS_CONFIG, which enables a config space bit to switch global bypass on and off.

Add a boot-bypass option, which defaults to 'on' to be in line with other vIOMMUs and to allow running firmware/bootloader that are unaware of the IOMMU.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

See the spec change for more rationale
https://lists.oasis-open.org/archives/virtio-dev/202109/msg00137.html

I guess the kernel bits should be merged in 5.17?

Yes, they should. I can resend at 5.17-rc1 along with a commit updating the linux headers. Or is there a specific process for synchronizing the headers? Looking at git log it looks like scripts/update-linux-headers.sh is run by whomever needs new UAPI features.

yes you need to generate the header update patch using

scripts/update-linux-headers.sh once the kernel pieces have landed

Thanks

Eric

Quoted 4 lines

Thanks,
Jean

#10 lore raw
In reply to Eric Auger
Quoted 11 lines · Eric Auger

Hi Jean, Michael,

Quoted 8 lines · Jean-Philippe Brucker

Replace the VIRTIO_IOMMU_F_BYPASS feature with VIRTIO_IOMMU_F_BYPASS_CONFIG, which enables a config space bit to switch global bypass on and off.

Add a boot-bypass option, which defaults to 'on' to be in line with other vIOMMUs and to allow running firmware/bootloader that are unaware of the IOMMU.

Quoted 8 lines
Quoted 2 lines

See the spec change for more rationale
https://lists.oasis-open.org/archives/virtio-dev/202109/msg00137.html

I guess the kernel bits should be merged in 5.17?

Thanks

Eric

They are in fact in my tree and set to go into 5.16. They've been in linux-next for a whole cycle now.
But if you feel I'm rushing things, pls let me know. Also, pls let me know whether my tree actually works well for you!

Quoted 13 lines
Quoted 13 lines

Jean-Philippe Brucker (3):
NOMERGE: virtio-iommu: Add definitions for
VIRTIO_IOMMU_F_BYPASS_CONFIG
virtio-iommu: Default to bypass during boot
virtio-iommu: Support bypass domain

 include/hw/virtio/virtio-iommu.h              |  1 +
 include/standard-headers/linux/virtio_iommu.h | 10 +++-
 hw/virtio/virtio-iommu.c                      | 60 ++++++++++++++++---
 hw/virtio/trace-events                        |  4 +-
 4 files changed, 64 insertions(+), 11 deletions(-)
#11 lore raw

Hi Michael,

Quoted 17 lines · Michael S. Tsirkin
Quoted 16 lines · Eric Auger

Hi Jean, Michael,

Quoted 10 lines · Jean-Philippe Brucker

Replace the VIRTIO_IOMMU_F_BYPASS feature with VIRTIO_IOMMU_F_BYPASS_CONFIG, which enables a config space bit to switch global bypass on and off.

Add a boot-bypass option, which defaults to 'on' to be in line with other vIOMMUs and to allow running firmware/bootloader that are unaware of the IOMMU.

See the spec change for more rationale
https://lists.oasis-open.org/archives/virtio-dev/202109/msg00137.html

I guess the kernel bits should be merged in 5.17?

Quoted 8 lines
Quoted 3 lines

Thanks

Eric

They are in fact in my tree and set to go into 5.16. They've been in linux-next for a whole cycle now.
But if you feel I'm rushing things, pls let me know. Also, pls let me know whether my tree actually works well for you!

Thanks, unfortunately I just noticed that those patches are from an older version of the series, the latest being v3
https://lore.kernel.org/linux-iommu/[email protected]/

Since Joerg picked the latest one in his tree [1], I was wondering if you could drop those patches?

51a4c54f35ae iommu/virtio: Support identity-mapped domains 523e55a406aa iommu/virtio: Pass end address to viommu_add_mapping() 97301219dfb2 iommu/virtio: Sort reserved regions
720552613526 iommu/virtio: Support bypass domains
01444b9c772f iommu/virtio: Add definitions for VIRTIO_IOMMU_F_BYPASS_CONFIG

Thanks,
Jean

[1] https://git.kernel.org/pub/scm/linux/kernel/git/joro/iommu.git/