Skip to contentSkip to conversation

VIRTIO-IOMMU device

PATCH Superseded Eric Auger 5 participants 28 messages Feb 7, 2020 – Feb 10, 2020

v14 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

Eric Auger

author
#0 lore raw

v14 cover VIRTIO-IOMMU device

This series implements the QEMU virtio-iommu device.

This matches the v0.12 spec (voted) and the corresponding virtio-iommu driver upstreamed in 5.3. All kernel dependencies are resolved for DT integration. The virtio-iommu can be instantiated in ARM virt using:

"-device virtio-iommu-pci,x-dt-binding".

Non DT mode is not yet supported as it has non resolved kernel dependencies [1].

This feature targets 5.0.

Integration with vhost devices and vfio devices is not part of this series. Please follow Bharat's respins [2].

Best Regards

Eric

This series can be found at:
https://github.com/eauger/qemu/tree/v4.2-virtio-iommu-v14

References:
[1] [RFC 00/13] virtio-iommu on non-devicetree platforms
[2] [PATCH RFC v5 0/5] virtio-iommu: VFIO integration

Testing:
- tested with guest using virtio-net-pci (,vhost=off,iommu_platform,disable-modern=off,disable-legacy=on) and virtio-blk-pci
- migration

History:

v13 -> v14:
- added "virtio-iommu-pci: Introduce the x-dt-binding option"
- Removed the mappings gtree ref counting and simply delete the gtree when the last EP is detached from the domain
- call virtio_iommu_detach_endpoint_from_domain from virtio_iommu_put_endpoint

v12 -> v13:
- Take into account Peter's comments
- fix qtest error and accomodate for directory changes in test
- remove "[PATCH v12 01/13] migration: Support QLIST migration" which is now upstream
- fix iommu_find_iommu_pcibus()
- squash commits as requested by Peter
- remove spurious guest log

v11 -> v12:
- took into account Peter and Jean's comments
- use guest features
- restore as_by_bus_num and when attaching devices, check they are actually protected by the IOMMU. Updated the tests accordingly.
- fix the mapping ref counting and make sure mappings are properly cleaned.
- Use CamelCase for data types
- simplify postload callback as suggested by Peter
- add R-bs
- fix mingw compilation issue
- add IOMMU migration priority
- qlist migration load simplified following Juan's suggestion

v10 -> v11:
- introduce virtio_iommu_handle_req macro
- migration support
- introduce DEFINE_PROP_INTERVAL and pass reserved regions through an array of those
- domain gtree simplification

v9 -> v10:
- rebase on 4.1.0-rc2, compliance with 0.12 spec
- removed ACPI part
- cleanup (see individual change logs)
- moved to a PATCH series

v8 -> v9:
- virtio-iommu-pci device needs to be instantiated from the command line (RID is not imposed anymore).
- tail structure properly initialized

v7 -> v8:
- virtio-iommu-pci added
- virt instantiation modified
- DT and ACPI modified to exclude the iommu RID from the mapping
- VIRTIO_IOMMU_F_BYPASS, VIRTIO_F_VERSION_1 features exposed

v6 -> v7:
- rebase on qemu 3.0.0-rc3
- minor update against v0.7
- fix issue with EP not on pci.0 and ACPI probing
- change the instantiation method

v5 -> v6:
- minor update against v0.6 spec
- fix g_hash_table_lookup in virtio_iommu_find_add_as
- replace some error_reports by qemu_log_mask(LOG_GUEST_ERROR, ...)

v4 -> v5:
- event queue and fault reporting
- we now return the IOAPIC MSI region if the virtio-iommu is instantiated in a PC machine.
- we bypass transactions on MSI HW region and fault on reserved ones.
- We support ACPI boot with mach-virt (based on IORT proposal)
- We moved to the new driver naming conventions
- simplified mach-virt instantiation
- worked around the disappearing of pci_find_primary_bus
- in virtio_iommu_translate, check the dev->as is not NULL
- initialize as->device_list in virtio_iommu_get_as
- initialize bufstate.error to false in virtio_iommu_probe

v3 -> v4:
- probe request support although no reserved region is returned at the moment
- unmap semantics less strict, as specified in v0.4
- device registration, attach/detach revisited
- split into smaller patches to ease review
- propose a way to inform the IOMMU mr about the page_size_mask of underlying HW IOMMU, if any
- remove warning associated with the translation of the MSI doorbell

v2 -> v3:
- rebase on top of 2.10-rc0 and especially
[PATCH qemu v9 0/2] memory/iommu: QOM'fy IOMMU MemoryRegion
- add mutex init
- fix as->mappings deletion using g_tree_ref/unref
- when a dev is attached whereas it is already attached to another address space, first detach it
- fix some error values
- page_sizes = TARGET_PAGE_MASK;
- I haven't changed the unmap() semantics yet, waiting for the next virtio-iommu spec revision.

v1 -> v2:
- fix redefinition of viommu_as typedef

Eric Auger (11):
virtio-iommu: Add skeleton
virtio-iommu: Decode the command payload
virtio-iommu: Implement attach/detach command
virtio-iommu: Implement map/unmap
virtio-iommu: Implement translate
virtio-iommu: Implement fault reporting
virtio-iommu-pci: Add virtio iommu pci support
virtio-iommu-pci: Introduce the x-dt-binding option hw/arm/virt: Add the virtio-iommu device tree mappings
virtio-iommu: Support migration
tests: Add virtio-iommu test

 hw/arm/virt.c                     |  54 +-
 hw/virtio/Kconfig                 |   5 +
 hw/virtio/Makefile.objs           |   2 +
 hw/virtio/trace-events            |  20 +
 hw/virtio/virtio-iommu-pci.c      | 106 ++++
 hw/virtio/virtio-iommu.c          | 890 ++++++++++++++++++++++++++++++
 include/hw/arm/virt.h             |   2 +
 include/hw/pci/pci.h              |   1 +
 include/hw/virtio/virtio-iommu.h  |  61 ++
 qdev-monitor.c                    |   1 +
 tests/qtest/Makefile.include      |   2 +
 tests/qtest/libqos/virtio-iommu.c | 177 ++++++
 tests/qtest/libqos/virtio-iommu.h |  45 ++
 tests/qtest/virtio-iommu-test.c   | 306 ++++++++++
 14 files changed, 1664 insertions(+), 8 deletions(-)
 create mode 100644 hw/virtio/virtio-iommu-pci.c
 create mode 100644 hw/virtio/virtio-iommu.c
 create mode 100644 include/hw/virtio/virtio-iommu.h
 create mode 100644 tests/qtest/libqos/virtio-iommu.c
 create mode 100644 tests/qtest/libqos/virtio-iommu.h
 create mode 100644 tests/qtest/virtio-iommu-test.c
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v14 1/11 virtio-iommu: Add skeleton

This patchs adds the skeleton for the virtio-iommu device.

---

v13 -> v14:
- use device_class_set_props
- updated copyright's year

v12 -> v13
- removed IOMMU_PCI_BUS_MAX and IOMMU_PCI_DEVFN_MAX

v11 -> v12:
- remove s_by_bus_num
- drop set_features (rely on default implementation) and acked_features

v9 -> v10:
- mutex initialized here
- initialize tail
- included hw/qdev-properties.h
- removed g_memdup
- removed s->config.domain_range.start = 0;

v9 -> v10:
- expose VIRTIO_IOMMU_F_MMIO feature
- s/domain_bits/domain_range struct
- change error codes
- enforce unmigratable
- Kconfig

v7 -> v8:
- expose VIRTIO_IOMMU_F_BYPASS and VIRTIO_F_VERSION_1 features
- set_config dummy implementation + tracing
- add trace in get_features
- set the features on realize() and store the acked ones
- remove inclusion of linux/virtio_iommu.h

v6 -> v7:
- removed qapi-event.h include
- add primary_bus and associated property

v4 -> v5:
- use the new v0.5 terminology (domain, endpoint)
- add the event virtqueue

v3 -> v4:
- use page_size_mask instead of page_sizes
- added set_features()
- added some traces (reset, set_status, set_features)
- empty virtio_iommu_set_config() as the driver MUST NOT write to device configuration fields
- add get_config trace

v2 -> v3:
- rebase on 2.10-rc0, ie. use IOMMUMemoryRegion and remove iommu_ops.
- advertise VIRTIO_IOMMU_F_MAP_UNMAP feature
- page_sizes set to TARGET_PAGE_SIZE

Conflicts:
hw/virtio/trace-events

---
 hw/virtio/Kconfig                |   5 +
 hw/virtio/Makefile.objs          |   1 +
 hw/virtio/trace-events           |   7 +
 hw/virtio/virtio-iommu.c         | 265 +++++++++++++++++++++++++++++++
 include/hw/virtio/virtio-iommu.h |  57 +++++++
 5 files changed, 335 insertions(+)
 create mode 100644 hw/virtio/virtio-iommu.c
 create mode 100644 include/hw/virtio/virtio-iommu.h
hw/virtio/Kconfig +5 -0
index f87def27a6..d29525b36f 100644
@@ -9,6 +9,11 @@ config VIRTIO_RNG
     default y
     depends on VIRTIO
 
+config VIRTIO_IOMMU
+    bool
+    default y
+    depends on VIRTIO
+
 config VIRTIO_PCI
     bool
     default y if PCI_DEVICES
hw/virtio/Makefile.objs +1 -0
index de0f5fc39b..2fd9da7410 100644
@@ -16,6 +16,7 @@ obj-$(call land,$(CONFIG_VIRTIO_CRYPTO),$(CONFIG_VIRTIO_PCI)) += virtio-crypto-p
 obj-$(CONFIG_VIRTIO_PMEM) += virtio-pmem.o
 common-obj-$(call land,$(CONFIG_VIRTIO_PMEM),$(CONFIG_VIRTIO_PCI)) += virtio-pmem-pci.o
 obj-$(call land,$(CONFIG_VHOST_USER_FS),$(CONFIG_VIRTIO_PCI)) += vhost-user-fs-pci.o
+obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock.o
 
 ifeq ($(CONFIG_VIRTIO_PCI),y)
hw/virtio/trace-events +7 -0
index e28ba48da6..02d93d7f63 100644
@@ -53,3 +53,10 @@ virtio_mmio_write_offset(uint64_t offset, uint64_t value) "virtio_mmio_write off
 virtio_mmio_guest_page(uint64_t size, int shift) "guest page size 0x%" PRIx64 " shift %d"
 virtio_mmio_queue_write(uint64_t value, int max_size) "mmio_queue write 0x%" PRIx64 " max %d"
 virtio_mmio_setting_irq(int level) "virtio_mmio setting IRQ %d"
+
+# hw/virtio/virtio-iommu.c
+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"
hw/virtio/virtio-iommu.c new file +265 -0

265 lines not on this page · raw message

include/hw/virtio/virtio-iommu.h new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#2 lore raw

PATCH v14 2/11 virtio-iommu: Decode the command payload

In reply to Eric Auger

This patch adds the command payload decoding and introduces the functions that will do the actual command handling. Those functions are not yet implemented.

---

v11 -> v12:
- ADded Jean and Peter's R-b

v10 -> v11:
- use a macro for handle command functions

v9 -> v10:
- make virtio_iommu_handle_* more compact and
remove get_payload_size

v7 -> v8:
- handle new domain parameter in detach
- remove reserved checks

v5 -> v6:
- change map/unmap semantics (remove size)

v4 -> v5:
- adopt new v0.5 terminology

v3 -> v4:
- no flags field anymore in struct virtio_iommu_req_unmap
- test reserved on attach/detach, change trace proto
- rebase on v2.10.0.

---
 hw/virtio/trace-events   |  4 +++
 hw/virtio/virtio-iommu.c | 76 +++++++++++++++++++++++++++++++++-------
 2 files changed, 68 insertions(+), 12 deletions(-)
hw/virtio/trace-events +4 -0
index 02d93d7f63..f7141aa2f6 100644
@@ -60,3 +60,7 @@ virtio_iommu_get_features(uint64_t features) "device supports features=0x%"PRIx6
 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_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"
+virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
hw/virtio/virtio-iommu.c +64 -12

95 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#3 lore raw

PATCH v14 3/11 virtio-iommu: Implement attach/detach command

In reply to Eric Auger

This patch implements the endpoint attach/detach to/from a domain.

Domain and endpoint internal datatypes are introduced. Both are stored in RB trees. The domain owns a list of endpoints attached to it. Also helpers to get/put end points and domains are introduced.

As for the IOMMU memory regions, a callback is called on PCI bus enumeration that initializes for a given device on the bus hierarchy an IOMMU memory region. The PCI bus hierarchy is stored locally in IOMMUPciBus and IOMMUDevice objects.

At the time of the enumeration, the bus number may not be computed yet.

So operations that will need to retrieve the IOMMUdevice and its IOMMU memory region from the bus number and devfn, once the bus number is garanteed to be frozen, use an array of IOMMUPciBus, lazily populated.

---

v13 -> v14:
- in virtio_iommu_put_endpoint, if the EP is attached to a domain, call virtio_iommu_detach_endpoint_from_domain()
- remove domain ref counting and simply delete the mappings gtree when the last EP is detached from the domain
- in virtio_iommu_detach_endpoint_from_domain(), return if the ep's domain is unset.

v12 -> v13:
- squashed v12 4, 5, 6 into this patch
- rename virtio_iommu_get_sid into virtio_iommu_get_bdf

v11 -> v12:
- check the device is protected by the iommu on attach
- on detach, check the domain id the device is attached to matches the one used in the detach command
- fix mapping ref counter and destroy the domain when no end-points are attached anymore

---
 hw/virtio/trace-events           |   6 +
 hw/virtio/virtio-iommu.c         | 311 ++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-iommu.h |   3 +
 3 files changed, 318 insertions(+), 2 deletions(-)
hw/virtio/trace-events +6 -0
index f7141aa2f6..15595f8cd7 100644
@@ -64,3 +64,9 @@ 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"
 virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
+virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int flag) "mr=%s rid=%d addr=0x%"PRIx64" flag=%d"
+virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s"
+virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=%d"
+virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d"
+virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d"
+virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d"
hw/virtio/virtio-iommu.c +309 -2

395 lines not on this page · raw message

include/hw/virtio/virtio-iommu.h +3 -0
index d24ba63305..ae88f730cf 100644
@@ -28,6 +28,8 @@
 #define VIRTIO_IOMMU(obj) \
         OBJECT_CHECK(VirtIOIOMMU, (obj), TYPE_VIRTIO_IOMMU)
 
+#define TYPE_VIRTIO_IOMMU_MEMORY_REGION "virtio-iommu-memory-region"
+
 typedef struct IOMMUDevice {
     void         *viommu;
     PCIBus       *bus;
@@ -48,6 +50,7 @@ typedef struct VirtIOIOMMU {
     struct virtio_iommu_config config;
     uint64_t features;
     GHashTable *as_by_busptr;
+    IOMMUPciBus *iommu_pcibus_by_bus_num[PCI_BUS_MAX];
     PCIBus *primary_bus;
     GTree *domains;
     QemuMutex mutex;
Signature · 1 line
2.20.1
#4 lore raw

Re: [PATCH v14 03/11] virtio-iommu: Implement attach/detach command

On Fri, Feb 07, 2020 at 10:31:55AM +0100, Eric Auger wrote:

[...]

Quoted 5 lines

v13 -> v14:
- in virtio_iommu_put_endpoint, if the EP is attached to a domain, call virtio_iommu_detach_endpoint_from_domain()
- remove domain ref counting and simply delete the mappings gtree when the last EP is detached from the domain

Yeh this looks a good optimization! Ref counting protects the domain from being gone when there's still EP in the domain, but since we've got the ep_list in domain after all so it seems to be safe and clearer.

Quoted 2 lines

- in virtio_iommu_detach_endpoint_from_domain(), return if the ep's domain is unset.

[...]

hw/virtio/virtio-iommu.c line 188

Quoted 9 lines
hw/virtio/virtio-iommu.c +9 -0
+static void virtio_iommu_put_domain(gpointer data)
+{
+    VirtIOIOMMUDomain *domain = (VirtIOIOMMUDomain *)data;
+    VirtIOIOMMUEndpoint *iter, *tmp;
+
+    QLIST_FOREACH_SAFE(iter, &domain->endpoint_list, next, tmp) {
+        virtio_iommu_detach_endpoint_from_domain(iter);
+    }
+    trace_virtio_iommu_put_domain(domain->id);

[1]

Quoted 2 lines
(quoted diff) +2 -0
+    g_free(domain);
+}

[...]

Quoted 14 lines

static int virtio_iommu_attach(VirtIOIOMMU *s,
struct virtio_iommu_req_attach *req)

 {
     uint32_t domain_id = le32_to_cpu(req->domain);
     uint32_t ep_id = le32_to_cpu(req->endpoint);
hw/virtio/virtio-iommu.c +5 -1
+    VirtIOIOMMUDomain *domain;
+    VirtIOIOMMUEndpoint *ep;
 
     trace_virtio_iommu_attach(domain_id, ep_id);
 
-    return VIRTIO_IOMMU_S_UNSUPP;
+    ep = virtio_iommu_get_endpoint(s, ep_id);
+    if (!ep) {
+        return VIRTIO_IOMMU_S_NOENT;

hw/virtio/virtio-iommu.c line 253

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    }
+
+    if (ep->domain) {
+        VirtIOIOMMUDomain *previous_domain = ep->domain;
+        /*
+         * the device is already attached to a domain,
+         * detach it first
+         */
+        virtio_iommu_detach_endpoint_from_domain(ep);
+        if (QLIST_EMPTY(&previous_domain->endpoint_list)) {

I feel like we still need:

               g_tree_destroy(previous_domain->mappings);

Or the mappings will be leaked.

To make this simpler, maybe we can destroy the mappings at [1] above. Then we can remove line [2] below too.

Quoted 28 lines
hw/virtio/virtio-iommu.c +17 -1
+            g_tree_remove(s->domains, GUINT_TO_POINTER(previous_domain->id));
+        }
+    }
+
+    domain = virtio_iommu_get_domain(s, domain_id);
+    QLIST_INSERT_HEAD(&domain->endpoint_list, ep, next);
+
+    ep->domain = domain;
+
+    return VIRTIO_IOMMU_S_OK;
 }
 
 static int virtio_iommu_detach(VirtIOIOMMU *s,
@@ -50,10 +268,29 @@ static int virtio_iommu_detach(VirtIOIOMMU *s,
 {
     uint32_t domain_id = le32_to_cpu(req->domain);
     uint32_t ep_id = le32_to_cpu(req->endpoint);
+    VirtIOIOMMUDomain *domain;
+    VirtIOIOMMUEndpoint *ep;
 
     trace_virtio_iommu_detach(domain_id, ep_id);
 
-    return VIRTIO_IOMMU_S_UNSUPP;
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (!ep) {
+        return VIRTIO_IOMMU_S_NOENT;
+    }
+

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

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    domain = ep->domain;
+
+    if (!domain || domain->id != domain_id) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
+    virtio_iommu_detach_endpoint_from_domain(ep);
+
+    if (QLIST_EMPTY(&domain->endpoint_list)) {
+        g_tree_destroy(domain->mappings);

[2]

hw/virtio/virtio-iommu.c out: · line 410

Quoted 9 lines
hw/virtio/virtio-iommu.c +3 -0
+        g_tree_remove(s->domains, GUINT_TO_POINTER(domain->id));
+    }
+    return VIRTIO_IOMMU_S_OK;
 }
 
 static int virtio_iommu_map(VirtIOIOMMU *s,
@@ -172,6 +409,27 @@ out:
     }
 }

Other than that, the whole patch looks good to me.

Thanks,

Signature · 1 line
Peter Xu
author
#5 lore raw

Re: [PATCH v14 03/11] virtio-iommu: Implement attach/detach command

Hi Peter,

Quoted 61 lines · Peter Xu

On Fri, Feb 07, 2020 at 10:31:55AM +0100, Eric Auger wrote:

[...]

Quoted 5 lines

v13 -> v14:
- in virtio_iommu_put_endpoint, if the EP is attached to a domain, call virtio_iommu_detach_endpoint_from_domain()
- remove domain ref counting and simply delete the mappings gtree when the last EP is detached from the domain

Yeh this looks a good optimization! Ref counting protects the domain from being gone when there's still EP in the domain, but since we've got the ep_list in domain after all so it seems to be safe and clearer.

Quoted 2 lines

- in virtio_iommu_detach_endpoint_from_domain(), return if the ep's domain is unset.

[...]

hw/virtio/virtio-iommu.c line 188

Quoted 9 lines
hw/virtio/virtio-iommu.c +9 -0
+static void virtio_iommu_put_domain(gpointer data)
+{
+    VirtIOIOMMUDomain *domain = (VirtIOIOMMUDomain *)data;
+    VirtIOIOMMUEndpoint *iter, *tmp;
+
+    QLIST_FOREACH_SAFE(iter, &domain->endpoint_list, next, tmp) {
+        virtio_iommu_detach_endpoint_from_domain(iter);
+    }
+    trace_virtio_iommu_put_domain(domain->id);

[1]

Quoted 2 lines
(quoted diff) +2 -0
+    g_free(domain);
+}

[...]

Quoted 14 lines

static int virtio_iommu_attach(VirtIOIOMMU *s,
struct virtio_iommu_req_attach *req)

 {
     uint32_t domain_id = le32_to_cpu(req->domain);
     uint32_t ep_id = le32_to_cpu(req->endpoint);
hw/virtio/virtio-iommu.c +5 -1
+    VirtIOIOMMUDomain *domain;
+    VirtIOIOMMUEndpoint *ep;
 
     trace_virtio_iommu_attach(domain_id, ep_id);
 
-    return VIRTIO_IOMMU_S_UNSUPP;
+    ep = virtio_iommu_get_endpoint(s, ep_id);
+    if (!ep) {
+        return VIRTIO_IOMMU_S_NOENT;

hw/virtio/virtio-iommu.c line 253

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    }
+
+    if (ep->domain) {
+        VirtIOIOMMUDomain *previous_domain = ep->domain;
+        /*
+         * the device is already attached to a domain,
+         * detach it first
+         */
+        virtio_iommu_detach_endpoint_from_domain(ep);
+        if (QLIST_EMPTY(&previous_domain->endpoint_list)) {
Quoted 5 lines

I feel like we still need:

               g_tree_destroy(previous_domain->mappings);

Or the mappings will be leaked.

You're fully right :-(

Quoted 3 lines

To make this simpler, maybe we can destroy the mappings at [1] above. Then we can remove line [2] below too.

Yes I chose to destroy the mappings in the put_domain and remove [2].

Quoted 47 lines
Quoted 29 lines
hw/virtio/virtio-iommu.c +17 -1
+            g_tree_remove(s->domains, GUINT_TO_POINTER(previous_domain->id));
+        }
+    }
+
+    domain = virtio_iommu_get_domain(s, domain_id);
+    QLIST_INSERT_HEAD(&domain->endpoint_list, ep, next);
+
+    ep->domain = domain;
+
+    return VIRTIO_IOMMU_S_OK;
 }
 
 static int virtio_iommu_detach(VirtIOIOMMU *s,
@@ -50,10 +268,29 @@ static int virtio_iommu_detach(VirtIOIOMMU *s,
 {
     uint32_t domain_id = le32_to_cpu(req->domain);
     uint32_t ep_id = le32_to_cpu(req->endpoint);
+    VirtIOIOMMUDomain *domain;
+    VirtIOIOMMUEndpoint *ep;
 
     trace_virtio_iommu_detach(domain_id, ep_id);
 
-    return VIRTIO_IOMMU_S_UNSUPP;
+    ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+    if (!ep) {
+        return VIRTIO_IOMMU_S_NOENT;
+    }
+

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

Quoted 10 lines
hw/virtio/virtio-iommu.c +10 -0
+    domain = ep->domain;
+
+    if (!domain || domain->id != domain_id) {
+        return VIRTIO_IOMMU_S_INVAL;
+    }
+
+    virtio_iommu_detach_endpoint_from_domain(ep);
+
+    if (QLIST_EMPTY(&domain->endpoint_list)) {
+        g_tree_destroy(domain->mappings);

[2]

Quoted 5 lines
hw/virtio/virtio-iommu.c +3 -0
+        g_tree_remove(s->domains, GUINT_TO_POINTER(domain->id));
+    }
+    return VIRTIO_IOMMU_S_OK;
 }
 
Quoted 6 lines

hw/virtio/virtio-iommu.c out: · line 410

Quoted 4 lines
hw/virtio/virtio-iommu.c +0 -0
 static int virtio_iommu_map(VirtIOIOMMU *s,
@@ -172,6 +409,27 @@ out:
     }
 }

Other than that, the whole patch looks good to me.

Thank you for the careful review.

Eric

Quoted 3 lines

Thanks,

author
#6 lore raw

PATCH v14 4/11 virtio-iommu: Implement map/unmap

In reply to Eric Auger

This patch implements virtio_iommu_map/unmap.

---

v11 -> v12:
- check unmanaged managed flags on map
- removed 2 qemu_log_mask in unmap()
- fix leak

v10 -> v11:
- revisit the implementation of unmap according to Peter's suggestion
- removed virt_addr and size from viommu_mapping struct
- use g_tree_lookup_extended()
- return VIRTIO_IOMMU_S_RANGE in case a mapping were
to be split on unmap (instead of INVAL)

v5 -> v6:
- use new v0.6 fields
- replace error_report by qemu_log_mask

v3 -> v4:
- implement unmap semantics as specified in v0.4

---
 hw/virtio/trace-events   |  1 +
 hw/virtio/virtio-iommu.c | 63 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 62 insertions(+), 2 deletions(-)
hw/virtio/trace-events +1 -0
index 15595f8cd7..22162d6583 100644
@@ -64,6 +64,7 @@ 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"
 virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
+virtio_iommu_unmap_done(uint32_t domain_id, uint64_t virt_start, uint64_t virt_end) "domain=%d virt_start=0x%"PRIx64" virt_end=0x%"PRIx64
 virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int flag) "mr=%s rid=%d addr=0x%"PRIx64" flag=%d"
 virtio_iommu_init_iommu_mr(char *iommu_mr) "init %s"
 virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=%d"
hw/virtio/virtio-iommu.c +61 -2

93 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#7 lore raw

PATCH v14 5/11 virtio-iommu: Implement translate

In reply to Eric Auger

This patch implements the translate callback

---

v11 -> v12:
- Added Jean's R-b
- s/qemu_log_mask/error_report_once

v10 -> v11:
- take into account the new value struct and use
g_tree_lookup_extended
- switched to error_report_once

v6 -> v7:
- implemented bypass-mode

v5 -> v6:
- replace error_report by qemu_log_mask

v4 -> v5:
- check the device domain is not NULL
- s/printf/error_report
- set flags to IOMMU_NONE in case of all translation faults

---
 hw/virtio/trace-events   |  1 +
 hw/virtio/virtio-iommu.c | 60 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 60 insertions(+), 1 deletion(-)
hw/virtio/trace-events +1 -0
index 22162d6583..095aa8b509 100644
@@ -71,3 +71,4 @@ virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=%d"
 virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d"
 virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d"
 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"
hw/virtio/virtio-iommu.c +59 -1

78 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#8 lore raw

PATCH v14 6/11 virtio-iommu: Implement fault reporting

In reply to Eric Auger

The event queue allows to report asynchronous errors. The translate function now injects faults when relevant.

---
v13 -> v14:
- remove loop
- add Peter's R-b

v12 -> v13:
- return on virtio_error()

v11 -> v12:
- reporting the addr associated with the fault and set the VIRTIO_IOMMU_FAULT_F_ADDRESS flag.
- added cpu_to_le<n>

v10 -> v11:
- change a virtio_error into an error_report_once
(no buffer available for output faults)

---
 hw/virtio/trace-events   |  1 +
 hw/virtio/virtio-iommu.c | 70 +++++++++++++++++++++++++++++++++++++---
 2 files changed, 66 insertions(+), 5 deletions(-)
hw/virtio/trace-events +1 -0
index 095aa8b509..e83500bee9 100644
@@ -72,3 +72,4 @@ virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d"
 virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d"
 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
hw/virtio/virtio-iommu.c +65 -5

107 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#9 lore raw

PATCH v14 7/11 virtio-iommu-pci: Add virtio iommu pci support

In reply to Eric Auger

This patch adds virtio-iommu-pci, which is the pci proxy for the virtio-iommu device.

---

v13 -> v14:
- add device_class_set_props

v11 -> v12:
- added Jean's R-b
- remove the array of intervals. Will be introduced later?

v10 -> v11:
- add the reserved_regions array property

v9 -> v10:
- include "hw/qdev-properties.h" header

v8 -> v9:
- add the msi-bypass property
- create virtio-iommu-pci.c

---
 hw/virtio/Makefile.objs          |  1 +
 hw/virtio/virtio-iommu-pci.c     | 88 ++++++++++++++++++++++++++++++++
 include/hw/pci/pci.h             |  1 +
 include/hw/virtio/virtio-iommu.h |  1 +
 qdev-monitor.c                   |  1 +
 5 files changed, 92 insertions(+)
 create mode 100644 hw/virtio/virtio-iommu-pci.c
hw/virtio/Makefile.objs +1 -0
index 2fd9da7410..4e4d39a0a4 100644
@@ -29,6 +29,7 @@ obj-$(CONFIG_VIRTIO_INPUT_HOST) += virtio-input-host-pci.o
 obj-$(CONFIG_VIRTIO_INPUT) += virtio-input-pci.o
 obj-$(CONFIG_VIRTIO_RNG) += virtio-rng-pci.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio-balloon-pci.o
+obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu-pci.o
 obj-$(CONFIG_VIRTIO_9P) += virtio-9p-pci.o
 obj-$(CONFIG_VIRTIO_SCSI) += virtio-scsi-pci.o
 obj-$(CONFIG_VIRTIO_BLK) += virtio-blk-pci.o
hw/virtio/virtio-iommu-pci.c new file +88 -0

88 lines not on this page · raw message

include/hw/pci/pci.h +1 -0
index 2acd8321af..cfedf5a995 100644
@@ -86,6 +86,7 @@ extern bool pci_available;
 #define PCI_DEVICE_ID_VIRTIO_9P          0x1009
 #define PCI_DEVICE_ID_VIRTIO_VSOCK       0x1012
 #define PCI_DEVICE_ID_VIRTIO_PMEM        0x1013
+#define PCI_DEVICE_ID_VIRTIO_IOMMU       0x1014
 
 #define PCI_VENDOR_ID_REDHAT             0x1b36
 #define PCI_DEVICE_ID_REDHAT_BRIDGE      0x0001
include/hw/virtio/virtio-iommu.h +1 -0
index ae88f730cf..6f67f1020a 100644
@@ -25,6 +25,7 @@
 #include "hw/pci/pci.h"
 
 #define TYPE_VIRTIO_IOMMU "virtio-iommu-device"
+#define TYPE_VIRTIO_IOMMU_PCI "virtio-iommu-device-base"
 #define VIRTIO_IOMMU(obj) \
         OBJECT_CHECK(VirtIOIOMMU, (obj), TYPE_VIRTIO_IOMMU)
 
qdev-monitor.c +1 -0
index 8ce71a206b..dbbe92dfa1 100644
@@ -67,6 +67,7 @@ static const QDevAlias qdev_alias_table[] = {
     { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
     { "virtio-input-host-pci", "virtio-input-host",
             QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
+    { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
     { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
     { "virtio-keyboard-pci", "virtio-keyboard",
             QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
Signature · 1 line
2.20.1
author
#10 lore raw

PATCH v14 8/11 virtio-iommu-pci: Introduce the x-dt-binding option

In reply to Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

---

May be squashed with previous patch

---
 hw/virtio/virtio-iommu-pci.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/virtio/virtio-iommu-pci.c +18 -0
index d539fcce75..3d06e14000 100644
@@ -14,6 +14,7 @@
 #include "virtio-pci.h"
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/qdev-properties.h"
+#include "qapi/error.h"
 
 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 
@@ -27,10 +28,12 @@ typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 struct VirtIOIOMMUPCI {
     VirtIOPCIProxy parent_obj;
     VirtIOIOMMU vdev;
+    bool dt_binding;
 };
 
 static Property virtio_iommu_pci_properties[] = {
     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
+    DEFINE_PROP_BOOL("x-dt-binding", VirtIOIOMMUPCI, dt_binding, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");
+        return;
+    }
+
+    if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
+        error_setg(errp,
+                   "The machine does not implement a virtio-iommu-pci hotplug "
+                   " handler that creates the device tree iommu-map bindings");
+       return;
+    }
     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
     object_property_set_link(OBJECT(dev),
                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
Signature · 1 line
2.20.1
#11 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Hi Eric,

Quoted 14 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

[...]

hw/virtio/virtio-iommu-pci.c static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) · line 50

Quoted 10 lines
hw/virtio/virtio-iommu-pci.c +6 -0
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");

"use -device virtio-iommu-pci,x-dt-binding"?

Can the option be safely removed as soon as we implement a topology description for the remaining platforms? Or will we need to carry it forever for backward-compatibility (ie. ensure that an old command-line invocation that contains this option still works)?

Thanks,
Jean

author
#12 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Hi Jean,

Quoted 31 lines · Jean-Philippe Brucker

Hi Eric,

Quoted 14 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

[...]

hw/virtio/virtio-iommu-pci.c static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) · line 50

Quoted 10 lines
hw/virtio/virtio-iommu-pci.c +6 -0
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");

"use -device virtio-iommu-pci,x-dt-binding"?

Quoted 4 lines

Can the option be safely removed as soon as we implement a topology description for the remaining platforms? Or will we need to carry it forever for backward-compatibility (ie. ensure that an old command-line invocation that contains this option still works)?

"x-" properties are experimental so yes it is bound to be removed as soon as we get the topology description feature.

I now have to figure out how to fix the make-check issue it introduces :-(

Thanks

Eric

Quoted 4 lines

Thanks,
Jean

#13 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

In reply to Jean-Philippe Brucker
Quoted 31 lines · Jean-Philippe Brucker

Hi Eric,

Quoted 14 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

[...]

hw/virtio/virtio-iommu-pci.c static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) · line 50

Quoted 10 lines
hw/virtio/virtio-iommu-pci.c +6 -0
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");

"use -device virtio-iommu-pci,x-dt-binding"?

Quoted 7 lines

Can the option be safely removed as soon as we implement a topology description for the remaining platforms? Or will we need to carry it forever for backward-compatibility (ie. ensure that an old command-line invocation that contains this option still works)?

Thanks,
Jean

I'd worry that if we actually document it then users will come to depend on it for sure, even though it starts with x-.

Signature · 1 line
MST
author
#14 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Hi

Quoted 37 lines · Michael S. Tsirkin
Quoted 36 lines · Jean-Philippe Brucker

Hi Eric,

Quoted 14 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

[...]

hw/virtio/virtio-iommu-pci.c static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) · line 50

Quoted 10 lines
hw/virtio/virtio-iommu-pci.c +6 -0
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");

"use -device virtio-iommu-pci,x-dt-binding"?

Can the option be safely removed as soon as we implement a topology description for the remaining platforms? Or will we need to carry it forever for backward-compatibility (ie. ensure that an old command-line invocation that contains this option still works)?

Quoted 5 lines
Quoted 2 lines

Thanks,
Jean

I'd worry that if we actually document it then users will come to depend on it for sure, even though it starts with x-.

Let's rephrase my previous answer. Once we get the topology description feature we can leave the x-dt-binding property supported but do not test it anymore and document it at deprecated?

Thanks

Eric

Quoted 1 line
#15 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

In reply to Michael S. Tsirkin
Quoted 37 lines · Michael S. Tsirkin
Quoted 36 lines · Jean-Philippe Brucker

Hi Eric,

Quoted 14 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

[...]

hw/virtio/virtio-iommu-pci.c static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) · line 50

Quoted 10 lines
hw/virtio/virtio-iommu-pci.c +6 -0
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");

"use -device virtio-iommu-pci,x-dt-binding"?

Can the option be safely removed as soon as we implement a topology description for the remaining platforms? Or will we need to carry it forever for backward-compatibility (ie. ensure that an old command-line invocation that contains this option still works)?

Quoted 5 lines
Quoted 2 lines

Thanks,
Jean

I'd worry that if we actually document it then users will come to depend on it for sure, even though it starts with x-.

I thought x- parameters can be dropped directly with totally no grarantee... Otherwise how do we differenciate x- with the common parameters, and how do we introduce remove-prone parameters?

Thanks,

Signature · 1 line
Peter Xu
#16 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Quoted 41 lines · Peter Xu
Quoted 40 lines · Michael S. Tsirkin
Quoted 38 lines · Jean-Philippe Brucker

Hi Eric,

Quoted 14 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

[...]

hw/virtio/virtio-iommu-pci.c static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) · line 50

Quoted 10 lines
hw/virtio/virtio-iommu-pci.c +6 -0
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");

"use -device virtio-iommu-pci,x-dt-binding"?

Can the option be safely removed as soon as we implement a topology description for the remaining platforms? Or will we need to carry it forever for backward-compatibility (ie. ensure that an old command-line invocation that contains this option still works)?

Thanks,
Jean

Quoted 8 lines
Quoted 2 lines

I'd worry that if we actually document it then users will come to depend on it for sure, even though it starts with x-.

I thought x- parameters can be dropped directly with totally no grarantee... Otherwise how do we differenciate x- with the common parameters, and how do we introduce remove-prone parameters?

Thanks,

It's all about not breaking users. Yes we document that x- interfaces are unstable. But that documentation is only good for well-behaved users such as libvirt. End-users tend not to read the docs and the subtleties of stable/unstable interface are lost on them, so we really must never actively ask end users to set an x- flag.

Quoted 2 lines
Signature · 1 line
Peter Xu
#17 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Quoted 49 lines · Michael S. Tsirkin
Quoted 48 lines · Peter Xu
Quoted 37 lines · Michael S. Tsirkin
Quoted 36 lines · Jean-Philippe Brucker

Hi Eric,

Quoted 14 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

[...]

hw/virtio/virtio-iommu-pci.c static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) · line 50

Quoted 10 lines
hw/virtio/virtio-iommu-pci.c +6 -0
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");

"use -device virtio-iommu-pci,x-dt-binding"?

Can the option be safely removed as soon as we implement a topology description for the remaining platforms? Or will we need to carry it forever for backward-compatibility (ie. ensure that an old command-line invocation that contains this option still works)?

Quoted 5 lines
Quoted 2 lines

Thanks,
Jean

I'd worry that if we actually document it then users will come to depend on it for sure, even though it starts with x-.

I thought x- parameters can be dropped directly with totally no grarantee... Otherwise how do we differenciate x- with the common parameters, and how do we introduce remove-prone parameters?

Quoted 8 lines
Quoted 1 line

Thanks,

It's all about not breaking users. Yes we document that x- interfaces are unstable. But that documentation is only good for well-behaved users such as libvirt. End-users tend not to read the docs and the subtleties of stable/unstable interface are lost on them, so we really must never actively ask end users to set an x- flag.

Ah I see the point. Yes we should never suggest that, or at least also mention that it's experimental and prone to change. Thanks,

Signature · 1 line
Peter Xu
#18 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

In reply to Eric Auger
Quoted 7 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

Quoted 7 lines

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

how about setting it by default from machine class?
See
[PATCH 1/2] spapr: Disable legacy virtio devices for pseries-5.0 and later [PATCH 2/2] spapr: Enable virtio iommu_platform=on by default which does it for spapr.

Quoted 56 lines

---

May be squashed with previous patch

---
 hw/virtio/virtio-iommu-pci.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/virtio/virtio-iommu-pci.c +18 -0
index d539fcce75..3d06e14000 100644
@@ -14,6 +14,7 @@
 #include "virtio-pci.h"
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/qdev-properties.h"
+#include "qapi/error.h"
 
 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 
@@ -27,10 +28,12 @@ typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 struct VirtIOIOMMUPCI {
     VirtIOPCIProxy parent_obj;
     VirtIOIOMMU vdev;
+    bool dt_binding;
 };
 
 static Property virtio_iommu_pci_properties[] = {
     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
+    DEFINE_PROP_BOOL("x-dt-binding", VirtIOIOMMUPCI, dt_binding, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");
+        return;
+    }
+
+    if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
+        error_setg(errp,
+                   "The machine does not implement a virtio-iommu-pci hotplug "
+                   " handler that creates the device tree iommu-map bindings");
+       return;
+    }
     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
     object_property_set_link(OBJECT(dev),
                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
Signature · 1 line
2.20.1
author
#19 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Hi,

Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

Quoted 4 lines
Quoted 2 lines

how about setting it by default from machine class?

Do you mean in ARM virt machine class? But this wouldn't prevent a user from launching an ACPI booted guest. I thought you wanted the end-user to know what he does.

I don't figure out a way to know if the guest is booted in dt or acpi mode. I can get access to those info:
- whether acpi is enabled
- whether a FW is loaded

But a FW can be loaded, acpi enabled and eventually the guest is DT booted with acpi=off in kernel opts.

Maybe at this point I could only support the case where no FW is loaded. In machvirt I would not register the virtio-iommu-pci hotplug handler in case a FW is loaded. Then I could get rid of the new x-dt-binding prop.

Thoughts?

Eric

Quoted 63 lines

See
[PATCH 1/2] spapr: Disable legacy virtio devices for pseries-5.0 and later [PATCH 2/2] spapr: Enable virtio iommu_platform=on by default which does it for spapr.

Quoted 56 lines

---

May be squashed with previous patch

---
 hw/virtio/virtio-iommu-pci.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/virtio/virtio-iommu-pci.c +18 -0
index d539fcce75..3d06e14000 100644
@@ -14,6 +14,7 @@
 #include "virtio-pci.h"
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/qdev-properties.h"
+#include "qapi/error.h"
 
 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 
@@ -27,10 +28,12 @@ typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 struct VirtIOIOMMUPCI {
     VirtIOPCIProxy parent_obj;
     VirtIOIOMMU vdev;
+    bool dt_binding;
 };
 
 static Property virtio_iommu_pci_properties[] = {
     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
+    DEFINE_PROP_BOOL("x-dt-binding", VirtIOIOMMUPCI, dt_binding, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");
+        return;
+    }
+
+    if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
+        error_setg(errp,
+                   "The machine does not implement a virtio-iommu-pci hotplug "
+                   " handler that creates the device tree iommu-map bindings");
+       return;
+    }
     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
     object_property_set_link(OBJECT(dev),
                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
Signature · 1 line
2.20.1
#20 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Quoted 19 lines · Auger Eric

Hi,

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

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

Quoted 8 lines
Quoted 1 line

how about setting it by default from machine class?

Do you mean in ARM virt machine class? But this wouldn't prevent a user from launching an ACPI booted guest. I thought you wanted the end-user to know what he does.

I don't figure out a way to know if the guest is booted in dt or acpi mode. I can get access to those info:
- whether acpi is enabled

That's the default on virt machine right? Then if there is a FW it can choose either ACPI or DT?

Quoted 10 lines

- whether a FW is loaded

But a FW can be loaded, acpi enabled and eventually the guest is DT booted with acpi=off in kernel opts.

Maybe at this point I could only support the case where no FW is loaded. In machvirt I would not register the virtio-iommu-pci hotplug handler in case a FW is loaded. Then I could get rid of the new x-dt-binding prop.

Thoughts?

Yes, I'm hoping we can get the topology description into next version of Linux, and it would be nicer not to introduce backward-compatible baggage for something that should be solved within a few months. If we have to warn the user then checking the FW seems like a good compromise, and easy to remove later.

Thanks,
Jean

Quoted 66 lines

Eric

Quoted 63 lines

See
[PATCH 1/2] spapr: Disable legacy virtio devices for pseries-5.0 and later [PATCH 2/2] spapr: Enable virtio iommu_platform=on by default which does it for spapr.

Quoted 56 lines

---

May be squashed with previous patch

---
 hw/virtio/virtio-iommu-pci.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/virtio/virtio-iommu-pci.c +18 -0
index d539fcce75..3d06e14000 100644
@@ -14,6 +14,7 @@
 #include "virtio-pci.h"
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/qdev-properties.h"
+#include "qapi/error.h"
 
 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 
@@ -27,10 +28,12 @@ typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 struct VirtIOIOMMUPCI {
     VirtIOPCIProxy parent_obj;
     VirtIOIOMMU vdev;
+    bool dt_binding;
 };
 
 static Property virtio_iommu_pci_properties[] = {
     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
+    DEFINE_PROP_BOOL("x-dt-binding", VirtIOIOMMUPCI, dt_binding, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");
+        return;
+    }
+
+    if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
+        error_setg(errp,
+                   "The machine does not implement a virtio-iommu-pci hotplug "
+                   " handler that creates the device tree iommu-map bindings");
+       return;
+    }
     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
     object_property_set_link(OBJECT(dev),
                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
Signature · 1 line
2.20.1
author
#21 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Hi Jean,

Quoted 25 lines · Jean-Philippe Brucker
Quoted 24 lines · Auger Eric

Hi,

Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

Quoted 4 lines
Quoted 2 lines

how about setting it by default from machine class?

Do you mean in ARM virt machine class? But this wouldn't prevent a user from launching an ACPI booted guest. I thought you wanted the end-user to know what he does.

Quoted 5 lines
Quoted 3 lines

I don't figure out a way to know if the guest is booted in dt or acpi mode. I can get access to those info:
- whether acpi is enabled

That's the default on virt machine right?

I think that's the default for qemu. you can siwtch it off on the cmd line using "-no-acpi"

Then if there is a FW it can

Quoted 1 line

choose either ACPI or DT?

yes that's why I can't actually know which one is going to be used.

Quoted 2 lines
Quoted 2 lines

- whether a FW is loaded

In case there is no FW I am sure this is dt mode. So I would be inclined to only support that mode atm.

Quoted 8 lines
Quoted 8 lines

But a FW can be loaded, acpi enabled and eventually the guest is DT booted with acpi=off in kernel opts.

Maybe at this point I could only support the case where no FW is loaded. In machvirt I would not register the virtio-iommu-pci hotplug handler in case a FW is loaded. Then I could get rid of the new x-dt-binding prop.

Quoted 5 lines
Quoted 1 line

Thoughts?

Yes, I'm hoping we can get the topology description into next version of Linux, and it would be nicer not to introduce backward-compatible baggage for something that should be solved within a few month

I agree. So I am inclined to remove the x-dt-binding prop then I just expose allow the virtio-iommu-pci to be instantiated if no FW is loaded. The constraint will be removed when we get topology description support in kernel.

Thanks

Eric
If we have to

Quoted 73 lines

warn the user then checking the FW seems like a good compromise, and easy to remove later.

Thanks,
Jean

Quoted 66 lines

Eric

Quoted 63 lines

See
[PATCH 1/2] spapr: Disable legacy virtio devices for pseries-5.0 and later [PATCH 2/2] spapr: Enable virtio iommu_platform=on by default which does it for spapr.

Quoted 56 lines

---

May be squashed with previous patch

---
 hw/virtio/virtio-iommu-pci.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/virtio/virtio-iommu-pci.c +18 -0
index d539fcce75..3d06e14000 100644
@@ -14,6 +14,7 @@
 #include "virtio-pci.h"
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/qdev-properties.h"
+#include "qapi/error.h"
 
 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 
@@ -27,10 +28,12 @@ typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 struct VirtIOIOMMUPCI {
     VirtIOPCIProxy parent_obj;
     VirtIOIOMMU vdev;
+    bool dt_binding;
 };
 
 static Property virtio_iommu_pci_properties[] = {
     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
+    DEFINE_PROP_BOOL("x-dt-binding", VirtIOIOMMUPCI, dt_binding, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");
+        return;
+    }
+
+    if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
+        error_setg(errp,
+                   "The machine does not implement a virtio-iommu-pci hotplug "
+                   " handler that creates the device tree iommu-map bindings");
+       return;
+    }
     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
     object_property_set_link(OBJECT(dev),
                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
Signature · 1 line
2.20.1
#22 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

In reply to Auger Eric
Quoted 24 lines · Auger Eric

Hi,

Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

Quoted 4 lines
Quoted 2 lines

how about setting it by default from machine class?

Do you mean in ARM virt machine class? But this wouldn't prevent a user from launching an ACPI booted guest. I thought you wanted the end-user to know what he does.

Quoted 7 lines

I don't figure out a way to know if the guest is booted in dt or acpi mode. I can get access to those info:
- whether acpi is enabled
- whether a FW is loaded

But a FW can be loaded, acpi enabled and eventually the guest is DT booted with acpi=off in kernel opts.

So let's say this configuration does not support the virtio-iommu at the moment. Is that a big deal?

Quoted 7 lines

Maybe at this point I could only support the case where no FW is loaded. In machvirt I would not register the virtio-iommu-pci hotplug handler in case a FW is loaded. Then I could get rid of the new x-dt-binding prop.

Thoughts?

Eric

Also an option.

Quoted 63 lines
Quoted 63 lines

See
[PATCH 1/2] spapr: Disable legacy virtio devices for pseries-5.0 and later [PATCH 2/2] spapr: Enable virtio iommu_platform=on by default which does it for spapr.

Quoted 56 lines

---

May be squashed with previous patch

---
 hw/virtio/virtio-iommu-pci.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/virtio/virtio-iommu-pci.c +18 -0
index d539fcce75..3d06e14000 100644
@@ -14,6 +14,7 @@
 #include "virtio-pci.h"
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/qdev-properties.h"
+#include "qapi/error.h"
 
 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 
@@ -27,10 +28,12 @@ typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 struct VirtIOIOMMUPCI {
     VirtIOPCIProxy parent_obj;
     VirtIOIOMMU vdev;
+    bool dt_binding;
 };
 
 static Property virtio_iommu_pci_properties[] = {
     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
+    DEFINE_PROP_BOOL("x-dt-binding", VirtIOIOMMUPCI, dt_binding, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");
+        return;
+    }
+
+    if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
+        error_setg(errp,
+                   "The machine does not implement a virtio-iommu-pci hotplug "
+                   " handler that creates the device tree iommu-map bindings");
+       return;
+    }
     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
     object_property_set_link(OBJECT(dev),
                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
Signature · 1 line
2.20.1
author
#23 lore raw

Re: [PATCH v14 08/11] virtio-iommu-pci: Introduce the x-dt-binding option

Hi Michael,

Quoted 40 lines · Michael S. Tsirkin
Quoted 24 lines · Auger Eric

Hi,

Quoted 13 lines · Michael S. Tsirkin
Quoted 12 lines · Eric Auger

At the moment, the kernel only supports device tree integration of the virtio-iommu. DT bindings between the PCI root complex and the IOMMU must be created by the machine in conformance to:

Documentation/devicetree/bindings/virtio/iommu.txt.

To make sure the end-user is aware of this, force him to use the temporary device option "x-dt-binding" and also double check the machine has a hotplug handler for the virtio-iommu-pci device. This hotplug handler is in charge of creating those DT bindings.

Quoted 4 lines
Quoted 2 lines

how about setting it by default from machine class?

Do you mean in ARM virt machine class? But this wouldn't prevent a user from launching an ACPI booted guest. I thought you wanted the end-user to know what he does.

Quoted 7 lines

I don't figure out a way to know if the guest is booted in dt or acpi mode. I can get access to those info:
- whether acpi is enabled
- whether a FW is loaded

But a FW can be loaded, acpi enabled and eventually the guest is DT booted with acpi=off in kernel opts.

So let's say this configuration does not support the virtio-iommu at the moment. Is that a big deal?

Quoted 4 lines

Maybe at this point I could only support the case where no FW is loaded. In machvirt I would not register the virtio-iommu-pci hotplug handler in case a FW is loaded. Then I could get rid of the new x-dt-binding prop.

Quoted 5 lines
Quoted 3 lines

Thoughts?

Eric

Also an option.

OK so I will drop the x-dt-binding property and implement this option.

Thanks

Eric

Quoted 66 lines
Quoted 64 lines
Quoted 64 lines

See
[PATCH 1/2] spapr: Disable legacy virtio devices for pseries-5.0 and later [PATCH 2/2] spapr: Enable virtio iommu_platform=on by default which does it for spapr.

Quoted 56 lines

---

May be squashed with previous patch

---
 hw/virtio/virtio-iommu-pci.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
hw/virtio/virtio-iommu-pci.c +18 -0
index d539fcce75..3d06e14000 100644
@@ -14,6 +14,7 @@
 #include "virtio-pci.h"
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/qdev-properties.h"
+#include "qapi/error.h"
 
 typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 
@@ -27,10 +28,12 @@ typedef struct VirtIOIOMMUPCI VirtIOIOMMUPCI;
 struct VirtIOIOMMUPCI {
     VirtIOPCIProxy parent_obj;
     VirtIOIOMMU vdev;
+    bool dt_binding;
 };
 
 static Property virtio_iommu_pci_properties[] = {
     DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
+    DEFINE_PROP_BOOL("x-dt-binding", VirtIOIOMMUPCI, dt_binding, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -39,6 +42,21 @@ static void virtio_iommu_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
     VirtIOIOMMUPCI *dev = VIRTIO_IOMMU_PCI(vpci_dev);
     DeviceState *vdev = DEVICE(&dev->vdev);
 
+    if (!dev->dt_binding) {
+        error_setg(errp,
+                   "Instantiation currently only is possible if the machine "
+                   "creates device tree iommu-map bindings, ie. ACPI is not "
+                   "yet supported");
+        error_append_hint(errp, "use -virtio-iommu-pci,x-dt-binding\n");
+        return;
+    }
+
+    if (!qdev_get_machine_hotplug_handler(DEVICE(vpci_dev))) {
+        error_setg(errp,
+                   "The machine does not implement a virtio-iommu-pci hotplug "
+                   " handler that creates the device tree iommu-map bindings");
+       return;
+    }
     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
     object_property_set_link(OBJECT(dev),
                              OBJECT(pci_get_bus(&vpci_dev->pci_dev)),
Signature · 1 line
2.20.1
author
#24 lore raw

PATCH v14 9/11 hw/arm/virt: Add the virtio-iommu device tree mappings

In reply to Eric Auger

Adds the "virtio,pci-iommu" node in the host bridge node and the RID mapping, excluding the IOMMU RID.

---

v11 -> v12:
- Added Jean's R-b

v10 -> v11:
- remove msi_bypass

v8 -> v9:
- disable msi-bypass property
- addition of the subnode is handled is the hotplug handler and IOMMU RID is notimposed anymore

v6 -> v7:
- align to the smmu instantiation code

v4 -> v5:
- VirtMachineClass no_iommu added in this patch
- Use object_resolve_path_type

---
 hw/arm/virt.c         | 54 ++++++++++++++++++++++++++++++++++++-------
 include/hw/arm/virt.h |  2 ++
 2 files changed, 48 insertions(+), 8 deletions(-)
hw/arm/virt.c +46 -8
index f788fe27d6..274abc10c0 100644
@@ -32,6 +32,7 @@
 #include "qemu-common.h"
 #include "qemu/units.h"
 #include "qemu/option.h"
+#include "monitor/qdev.h"
 #include "qapi/error.h"
 #include "hw/sysbus.h"
 #include "hw/boards.h"
@@ -54,6 +55,7 @@
 #include "qemu/error-report.h"
 #include "qemu/module.h"
 #include "hw/pci-host/gpex.h"
+#include "hw/virtio/virtio-pci.h"
 #include "hw/arm/sysbus-fdt.h"
 #include "hw/platform-bus.h"
 #include "hw/qdev-properties.h"
@@ -71,6 +73,7 @@
 #include "hw/mem/pc-dimm.h"
 #include "hw/mem/nvdimm.h"
 #include "hw/acpi/generic_event_device.h"
+#include "hw/virtio/virtio-iommu.h"
 
 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \
     static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
@@ -1180,6 +1183,30 @@ static void create_smmu(const VirtMachineState *vms,
     g_free(node);
 }
 
+static void create_virtio_iommu(VirtMachineState *vms, Error **errp)
+{
+    const char compat[] = "virtio,pci-iommu";
+    uint16_t bdf = vms->virtio_iommu_bdf;
+    char *node;
+
+    vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
+
+    node = g_strdup_printf("%s/virtio_iommu@%d", vms->pciehb_nodename, bdf);
+    qemu_fdt_add_subnode(vms->fdt, node);
+    qemu_fdt_setprop(vms->fdt, node, "compatible", compat, sizeof(compat));
+    qemu_fdt_setprop_sized_cells(vms->fdt, node, "reg",
+                                 1, bdf << 8, 1, 0, 1, 0,
+                                 1, 0, 1, 0);
+
+    qemu_fdt_setprop_cell(vms->fdt, node, "#iommu-cells", 1);
+    qemu_fdt_setprop_cell(vms->fdt, node, "phandle", vms->iommu_phandle);
+    g_free(node);
+
+    qemu_fdt_setprop_cells(vms->fdt, vms->pciehb_nodename, "iommu-map",
+                           0x0, vms->iommu_phandle, 0x0, bdf,
+                           bdf + 1, vms->iommu_phandle, bdf + 1, 0xffff - bdf);
+}
+
 static void create_pcie(VirtMachineState *vms)
 {
     hwaddr base_mmio = vms->memmap[VIRT_PCIE_MMIO].base;
@@ -1258,7 +1285,7 @@ static void create_pcie(VirtMachineState *vms)
         }
     }
 
-    nodename = g_strdup_printf("/pcie@%" PRIx64, base);
+    nodename = vms->pciehb_nodename = g_strdup_printf("/pcie@%" PRIx64, base);
     qemu_fdt_add_subnode(vms->fdt, nodename);
     qemu_fdt_setprop_string(vms->fdt, nodename,
                             "compatible", "pci-host-ecam-generic");
@@ -1301,13 +1328,16 @@ static void create_pcie(VirtMachineState *vms)
     if (vms->iommu) {
         vms->iommu_phandle = qemu_fdt_alloc_phandle(vms->fdt);
 
-        create_smmu(vms, pci->bus);
-
-        qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map",
-                               0x0, vms->iommu_phandle, 0x0, 0x10000);
+        switch (vms->iommu) {
+        case VIRT_IOMMU_SMMUV3:
+            create_smmu(vms, pci->bus);
+            qemu_fdt_setprop_cells(vms->fdt, nodename, "iommu-map",
+                                   0x0, vms->iommu_phandle, 0x0, 0x10000);
+            break;
+        default:
+            g_assert_not_reached();
+        }
     }
-
-    g_free(nodename);
 }
 
 static void create_platform_bus(VirtMachineState *vms)
@@ -1976,6 +2006,13 @@ static void virt_machine_device_plug_cb(HotplugHandler *hotplug_dev,
     if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
         virt_memory_plug(hotplug_dev, dev, errp);
     }
+    if (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI)) {
+        PCIDevice *pdev = PCI_DEVICE(dev);
+
+        vms->iommu = VIRT_IOMMU_VIRTIO;
+        vms->virtio_iommu_bdf = pci_get_bdf(pdev);
+        create_virtio_iommu(vms, errp);
+    }
 }
 
 static void virt_machine_device_unplug_request_cb(HotplugHandler *hotplug_dev,
@@ -1989,7 +2026,8 @@ static HotplugHandler *virt_machine_get_hotplug_handler(MachineState *machine,
                                                         DeviceState *dev)
 {
     if (object_dynamic_cast(OBJECT(dev), TYPE_SYS_BUS_DEVICE) ||
-       (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM))) {
+       (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) ||
+       (object_dynamic_cast(OBJECT(dev), TYPE_VIRTIO_IOMMU_PCI))) {
         return HOTPLUG_HANDLER(machine);
     }
 
include/hw/arm/virt.h +2 -0
index 71508bf40c..02f500cb8e 100644
@@ -125,8 +125,10 @@ typedef struct {
     bool virt;
     int32_t gic_version;
     VirtIOMMUType iommu;
+    uint16_t virtio_iommu_bdf;
     struct arm_boot_info bootinfo;
     MemMapEntry *memmap;
+    char *pciehb_nodename;
     const int *irqmap;
     int smp_cpus;
     void *fdt;
Signature · 1 line
2.20.1
author
#25 lore raw

PATCH v14 10/11 virtio-iommu: Support migration

In reply to Eric Auger

Add Migration support. We rely on recently added gtree and qlist migration. We only migrate the domain gtree. The endpoint gtree is re-constructed in a post-load operation.

---

v11 -> v12:
- do not migrate the endpoint gtree but reconstruct it from the domain gtree (Peter's suggestion)
- add MIG_PRI_IOMMU

---
 hw/virtio/virtio-iommu.c | 109 +++++++++++++++++++++++++++++++++++----
 1 file changed, 99 insertions(+), 10 deletions(-)
hw/virtio/virtio-iommu.c +99 -10
index 997a36e22e..88717c6305 100644
@@ -643,16 +643,6 @@ static uint64_t virtio_iommu_get_features(VirtIODevice *vdev, uint64_t f,
     return f;
 }
 
-/*
- * Migration is not yet supported: most of the state consists
- * of balanced binary trees which are not yet ready for getting
- * migrated
- */
-static const VMStateDescription vmstate_virtio_iommu_device = {
-    .name = "virtio-iommu-device",
-    .unmigratable = 1,
-};
-
 static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
 {
     guint ua = GPOINTER_TO_UINT(a);
@@ -736,9 +726,108 @@ static void virtio_iommu_instance_init(Object *obj)
 {
 }
 
+#define VMSTATE_INTERVAL                               \
+{                                                      \
+    .name = "interval",                                \
+    .version_id = 1,                                   \
+    .minimum_version_id = 1,                           \
+    .fields = (VMStateField[]) {                       \
+        VMSTATE_UINT64(low, VirtIOIOMMUInterval),      \
+        VMSTATE_UINT64(high, VirtIOIOMMUInterval),     \
+        VMSTATE_END_OF_LIST()                          \
+    }                                                  \
+}
+
+#define VMSTATE_MAPPING                               \
+{                                                     \
+    .name = "mapping",                                \
+    .version_id = 1,                                  \
+    .minimum_version_id = 1,                          \
+    .fields = (VMStateField[]) {                      \
+        VMSTATE_UINT64(phys_addr, VirtIOIOMMUMapping),\
+        VMSTATE_UINT32(flags, VirtIOIOMMUMapping),    \
+        VMSTATE_END_OF_LIST()                         \
+    },                                                \
+}
+
+static const VMStateDescription vmstate_interval_mapping[2] = {
+    VMSTATE_MAPPING,   /* value */
+    VMSTATE_INTERVAL   /* key   */
+};
+
+static int domain_preload(void *opaque)
+{
+    VirtIOIOMMUDomain *domain = opaque;
+
+    domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
+                                       NULL, g_free, g_free);
+    return 0;
+}
+
+static const VMStateDescription vmstate_endpoint = {
+    .name = "endpoint",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32(id, VirtIOIOMMUEndpoint),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static const VMStateDescription vmstate_domain = {
+    .name = "domain",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .pre_load = domain_preload,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32(id, VirtIOIOMMUDomain),
+        VMSTATE_GTREE_V(mappings, VirtIOIOMMUDomain, 1,
+                        vmstate_interval_mapping,
+                        VirtIOIOMMUInterval, VirtIOIOMMUMapping),
+        VMSTATE_QLIST_V(endpoint_list, VirtIOIOMMUDomain, 1,
+                        vmstate_endpoint, VirtIOIOMMUEndpoint, next),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static gboolean reconstruct_endpoints(gpointer key, gpointer value,
+                                      gpointer data)
+{
+    VirtIOIOMMU *s = (VirtIOIOMMU *)data;
+    VirtIOIOMMUDomain *d = (VirtIOIOMMUDomain *)value;
+    VirtIOIOMMUEndpoint *iter;
+
+    QLIST_FOREACH(iter, &d->endpoint_list, next) {
+        iter->domain = d;
+        g_tree_insert(s->endpoints, GUINT_TO_POINTER(iter->id), iter);
+    }
+    return false; /* continue the domain traversal */
+}
+
+static int iommu_post_load(void *opaque, int version_id)
+{
+    VirtIOIOMMU *s = opaque;
+
+    g_tree_foreach(s->domains, reconstruct_endpoints, s);
+    return 0;
+}
+
+static const VMStateDescription vmstate_virtio_iommu_device = {
+    .name = "virtio-iommu-device",
+    .minimum_version_id = 1,
+    .version_id = 1,
+    .post_load = iommu_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_GTREE_DIRECT_KEY_V(domains, VirtIOIOMMU, 1,
+                                   &vmstate_domain, VirtIOIOMMUDomain),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 static const VMStateDescription vmstate_virtio_iommu = {
     .name = "virtio-iommu",
     .minimum_version_id = 1,
+    .priority = MIG_PRI_IOMMU,
     .version_id = 1,
     .fields = (VMStateField[]) {
         VMSTATE_VIRTIO_DEVICE,
Signature · 1 line
2.20.1
author
#26 lore raw

PATCH v14 11/11 tests: Add virtio-iommu test

In reply to Eric Auger

This adds the framework to test the virtio-iommu-pci device and tests exercising the attach/detach, map/unmap API.

To run the tests:
make tests/qtest/qos-test QTEST_QEMU_BINARY=x86_64-softmmu/qemu-system-x86_64 tests/qtest/qos-test V=1

---

v13 -> v14:
- changed copyright

v12 -> v13
- remove probe_size test
- move to qtest directory

---
 tests/qtest/Makefile.include      |   2 +
 tests/qtest/libqos/virtio-iommu.c | 177 +++++++++++++++++
 tests/qtest/libqos/virtio-iommu.h |  45 +++++
 tests/qtest/virtio-iommu-test.c   | 306 ++++++++++++++++++++++++++++++
 4 files changed, 530 insertions(+)
 create mode 100644 tests/qtest/libqos/virtio-iommu.c
 create mode 100644 tests/qtest/libqos/virtio-iommu.h
 create mode 100644 tests/qtest/virtio-iommu-test.c
tests/qtest/Makefile.include +2 -0
index eb0f23b108..f3606c9e2a 100644
@@ -189,6 +189,7 @@ qos-test-obj-y += tests/qtest/libqos/virtio-pci-modern.o
 qos-test-obj-y += tests/qtest/libqos/virtio-rng.o
 qos-test-obj-y += tests/qtest/libqos/virtio-scsi.o
 qos-test-obj-y += tests/qtest/libqos/virtio-serial.o
+qos-test-obj-y += tests/qtest/libqos/virtio-iommu.o
 
 # qos machines:
 qos-test-obj-y += tests/qtest/libqos/aarch64-xlnx-zcu102-machine.o
@@ -228,6 +229,7 @@ qos-test-obj-y += tests/qtest/virtio-net-test.o
 qos-test-obj-y += tests/qtest/virtio-rng-test.o
 qos-test-obj-y += tests/qtest/virtio-scsi-test.o
 qos-test-obj-y += tests/qtest/virtio-serial-test.o
+qos-test-obj-y += tests/qtest/virtio-iommu-test.o
 qos-test-obj-y += tests/qtest/vmxnet3-test.o
 
 check-unit-y += tests/test-qgraph$(EXESUF)
tests/qtest/libqos/virtio-iommu.c new file +177 -0

177 lines not on this page · raw message

tests/qtest/libqos/virtio-iommu.h new file +45 -0

45 lines not on this page · raw message

tests/qtest/virtio-iommu-test.c new file +306 -0

306 lines not on this page · raw message

Signature · 1 line
2.20.1
#27 lore raw
In reply to Eric Auger
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the docker-quick@centos7 build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-centos7 V=1 NETWORK=1
time make docker-test-quick@centos7 SHOW_ENV=1 J=14 NETWORK=1 === TEST SCRIPT END ===

use -virtio-iommu-pci,x-dt-binding
Broken pipe
/tmp/qemu-test/src/tests/qtest/libqtest.c:140: kill_qemu() tried to terminate QEMU process but encountered exit status 1 (expected 0)
ERROR - too few tests run (expected 72, got 39)
make: *** [check-qtest-x86_64] Error 1
make: *** Waiting for unfinished jobs....
TEST iotest-qcow2: 154
TEST iotest-qcow2: 156
---
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['sudo', '-n', 'docker', 'run', '--label', 'com.qemu.instance.uuid=7739344c32be4ab8b27bb7a0936ddbdb', '-u', '1003', '--security-opt', 'seccomp=unconfined', '--rm', '-e', 'TARGET_LIST=', '-e', 'EXTRA_CONFIGURE_OPTS=', '-e', 'V=', '-e', 'J=14', '-e', 'DEBUG=', '-e', 'SHOW_ENV=1', '-e', 'CCACHE_DIR=/var/tmp/ccache', '-v', '/home/patchew2/.cache/qemu-docker-ccache:/var/tmp/ccache:z', '-v', '/var/tmp/patchew-tester-tmp-4k1yy7yj/src/docker-src.2020-02-07-04.59.36.7652:/var/tmp/qemu:z,ro', 'qemu:centos7', '/var/tmp/qemu/run', 'test-quick']' returned non-zero exit status 2. filter=--filter=label=com.qemu.instance.uuid=7739344c32be4ab8b27bb7a0936ddbdb
make[1]: *** [docker-run] Error 1
make[1]: Leaving directory `/var/tmp/patchew-tester-tmp-4k1yy7yj/src'
make: *** [docker-run-test-quick@centos7] Error 2

real 11m54.485s
user 0m8.028s

The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-quick@centos7/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]