[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-arm] [RFC v5 09/22] virtio-iommu: Register attached endpoints
From: |
Eric Auger |
Subject: |
[Qemu-arm] [RFC v5 09/22] virtio-iommu: Register attached endpoints |
Date: |
Fri, 19 Jan 2018 14:49:02 +0000 |
This patch introduce domain and endpoint internal
datatypes. Both are stored in RB trees. The domain
owns a list of endpoints attached to it.
It is assumed the endpoint ID corresponds to the PCI BDF.
Signed-off-by: Eric Auger <address@hidden>
---
v4 -> v5:
- initialize as->endpoint_list
v3 -> v4:
- new separate patch
---
hw/virtio/trace-events | 4 ++
hw/virtio/virtio-iommu.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 127 insertions(+)
diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events
index 1739db5..59c75da 100644
--- a/hw/virtio/trace-events
+++ b/hw/virtio/trace-events
@@ -38,3 +38,7 @@ virtio_iommu_map(uint32_t domain_id, uint64_t phys_addr,
uint64_t virt_addr, uin
virtio_iommu_unmap(uint32_t domain_id, uint64_t virt_addr, uint64_t size)
"domain=%d virt_addr=0x%"PRIx64" size=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"
diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c
index 5d335d1..cca2614 100644
--- a/hw/virtio/virtio-iommu.c
+++ b/hw/virtio/virtio-iommu.c
@@ -34,15 +34,118 @@
#include "hw/virtio/virtio-bus.h"
#include "hw/virtio/virtio-access.h"
#include "hw/virtio/virtio-iommu.h"
+#include "hw/pci/pci_bus.h"
+#include "hw/pci/pci.h"
/* Max size */
#define VIOMMU_DEFAULT_QUEUE_SIZE 256
+typedef struct viommu_domain {
+ uint32_t id;
+ GTree *mappings;
+ QLIST_HEAD(, viommu_endpoint) endpoint_list;
+} viommu_domain;
+
+typedef struct viommu_endpoint {
+ uint32_t id;
+ viommu_domain *domain;
+ QLIST_ENTRY(viommu_endpoint) next;
+ VirtIOIOMMU *viommu;
+} viommu_endpoint;
+
+typedef struct viommu_interval {
+ uint64_t low;
+ uint64_t high;
+} viommu_interval;
+
static inline uint16_t virtio_iommu_get_sid(IOMMUDevice *dev)
{
return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn);
}
+static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
+{
+ viommu_interval *inta = (viommu_interval *)a;
+ viommu_interval *intb = (viommu_interval *)b;
+
+ if (inta->high <= intb->low) {
+ return -1;
+ } else if (intb->high <= inta->low) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+static void virtio_iommu_detach_endpoint_from_domain(viommu_endpoint *ep)
+{
+ QLIST_REMOVE(ep, next);
+ ep->domain = NULL;
+}
+
+static viommu_endpoint *virtio_iommu_get_endpoint(VirtIOIOMMU *s,
+ uint32_t ep_id)
+{
+ viommu_endpoint *ep;
+
+ ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(ep_id));
+ if (ep) {
+ return ep;
+ }
+ ep = g_malloc0(sizeof(*ep));
+ ep->id = ep_id;
+ ep->viommu = s;
+ trace_virtio_iommu_get_endpoint(ep_id);
+ g_tree_insert(s->endpoints, GUINT_TO_POINTER(ep_id), ep);
+ return ep;
+}
+
+static void virtio_iommu_put_endpoint(gpointer data)
+{
+ viommu_endpoint *ep = (viommu_endpoint *)data;
+
+ if (ep->domain) {
+ virtio_iommu_detach_endpoint_from_domain(ep);
+ g_tree_unref(ep->domain->mappings);
+ }
+
+ trace_virtio_iommu_put_endpoint(ep->id);
+ g_free(ep);
+}
+
+viommu_domain *virtio_iommu_get_domain(VirtIOIOMMU *s, uint32_t domain_id);
+viommu_domain *virtio_iommu_get_domain(VirtIOIOMMU *s, uint32_t domain_id)
+{
+ viommu_domain *domain;
+
+ domain = g_tree_lookup(s->domains, GUINT_TO_POINTER(domain_id));
+ if (domain) {
+ return domain;
+ }
+ domain = g_malloc0(sizeof(*domain));
+ domain->id = domain_id;
+ domain->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,
+ NULL, (GDestroyNotify)g_free,
+ (GDestroyNotify)g_free);
+ g_tree_insert(s->domains, GUINT_TO_POINTER(domain_id), domain);
+ QLIST_INIT(&domain->endpoint_list);
+ trace_virtio_iommu_get_domain(domain_id);
+ return domain;
+}
+
+static void virtio_iommu_put_domain(gpointer data)
+{
+ viommu_domain *domain = (viommu_domain *)data;
+ viommu_endpoint *iter, *tmp;
+
+ QLIST_FOREACH_SAFE(iter, &domain->endpoint_list, next, tmp) {
+ virtio_iommu_detach_endpoint_from_domain(iter);
+ }
+ g_tree_destroy(domain->mappings);
+ trace_virtio_iommu_put_domain(domain->id);
+ g_free(domain);
+}
+
static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque,
int devfn)
{
@@ -68,6 +171,8 @@ static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus,
void *opaque,
sdev->bus = bus;
sdev->devfn = devfn;
+ virtio_iommu_get_endpoint(s, PCI_BUILD_BDF(pci_bus_num(bus), devfn));
+
trace_virtio_iommu_init_iommu_mr(name);
memory_region_init_iommu(&sdev->iommu_mr, sizeof(sdev->iommu_mr),
@@ -332,6 +437,13 @@ static const VMStateDescription
vmstate_virtio_iommu_device = {
},
};
+static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
+{
+ uint ua = GPOINTER_TO_UINT(a);
+ uint ub = GPOINTER_TO_UINT(b);
+ return (ua > ub) - (ua < ub);
+}
+
static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
{
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
@@ -358,10 +470,17 @@ static void virtio_iommu_device_realize(DeviceState *dev,
Error **errp)
s->config.page_size_mask = TARGET_PAGE_MASK;
s->config.input_range.end = -1UL;
+ qemu_mutex_init(&s->mutex);
+
memset(s->as_by_bus_num, 0, sizeof(s->as_by_bus_num));
s->as_by_busptr = g_hash_table_new(NULL, NULL);
pci_setup_iommu(pcibus, virtio_iommu_find_add_as, s);
+
+ s->domains = g_tree_new_full((GCompareDataFunc)int_cmp,
+ NULL, NULL, virtio_iommu_put_domain);
+ s->endpoints = g_tree_new_full((GCompareDataFunc)int_cmp,
+ NULL, NULL, virtio_iommu_put_endpoint);
return;
err:
error_setg(&error_fatal, "virtio-iommu: no pci bus identified");
@@ -370,6 +489,10 @@ err:
static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp)
{
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ VirtIOIOMMU *s = VIRTIO_IOMMU(dev);
+
+ g_tree_destroy(s->domains);
+ g_tree_destroy(s->endpoints);
virtio_cleanup(vdev);
}
--
1.9.1
- [Qemu-arm] [RFC v5 00/22] VIRTIO-IOMMU device, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 01/22] machine: Add a get_primary_pci_bus callback, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 02/22] hw/arm/virt: Implement get_primary_pci_bus, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 03/22] pc: Implement get_primary_pci_bus, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 04/22] update-linux-headers: Import virtio_iommu.h, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 05/22] linux-headers: Partial update for virtio-iommu, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 06/22] virtio-iommu: Add skeleton, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 07/22] virtio-iommu: Decode the command payload, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 08/22] virtio-iommu: Add the iommu regions, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 09/22] virtio-iommu: Register attached endpoints,
Eric Auger <=
- [Qemu-arm] [RFC v5 10/22] virtio-iommu: Implement attach/detach command, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 11/22] virtio-iommu: Implement map/unmap, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 12/22] virtio-iommu: Implement translate, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 13/22] virtio-iommu: Implement probe request, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 14/22] virtio-iommu: Add an msi_bypass property, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 15/22] virtio-iommu: Implement fault reporting, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 16/22] virtio_iommu: Handle reserved regions in translation process, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 17/22] hw/arm/virt: Add virtio-iommu to the virt board, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 18/22] hw/arm/virt-acpi-build: Add virtio-iommu node in IORT table, Eric Auger, 2018/01/19
- [Qemu-arm] [RFC v5 19/22] memory.h: Add set_page_size_mask IOMMUMemoryRegion callback, Eric Auger, 2018/01/19