{"id":815323,"url":"http://patchwork.ozlabs.org/api/patches/815323/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/1505807208-9063-7-git-send-email-eric.auger@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<1505807208-9063-7-git-send-email-eric.auger@redhat.com>","list_archive_url":null,"date":"2017-09-19T07:46:38","name":"[RFC,v4,06/16] virtio-iommu: Register attached devices","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"2701be8e93c043e93e3531dcf0104b8f37b69a05","submitter":{"id":69187,"url":"http://patchwork.ozlabs.org/api/people/69187/?format=json","name":"Eric Auger","email":"eric.auger@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/1505807208-9063-7-git-send-email-eric.auger@redhat.com/mbox/","series":[{"id":3796,"url":"http://patchwork.ozlabs.org/api/series/3796/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=3796","date":"2017-09-19T07:46:32","name":"VIRTIO-IOMMU device","version":4,"mbox":"http://patchwork.ozlabs.org/series/3796/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/815323/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/815323/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx03.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=eric.auger@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xxFWz3p0Tz9s7M\n\tfor <incoming@patchwork.ozlabs.org>;\n\tTue, 19 Sep 2017 17:54:39 +1000 (AEST)","from localhost ([::1]:40617 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1duDMb-0004a8-4H\n\tfor incoming@patchwork.ozlabs.org; Tue, 19 Sep 2017 03:54:37 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:47507)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <eric.auger@redhat.com>) id 1duDGV-0008Ne-Jn\n\tfor qemu-devel@nongnu.org; Tue, 19 Sep 2017 03:48:21 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <eric.auger@redhat.com>) id 1duDGU-0000v2-GS\n\tfor qemu-devel@nongnu.org; Tue, 19 Sep 2017 03:48:19 -0400","from mx1.redhat.com ([209.132.183.28]:40892)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <eric.auger@redhat.com>)\n\tid 1duDGO-0000qh-5U; Tue, 19 Sep 2017 03:48:12 -0400","from smtp.corp.redhat.com\n\t(int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 1784D7E445;\n\tTue, 19 Sep 2017 07:48:11 +0000 (UTC)","from localhost.localdomain.com (ovpn-116-197.ams2.redhat.com\n\t[10.36.116.197])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 8BE5260466;\n\tTue, 19 Sep 2017 07:47:59 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 1784D7E445","From":"Eric Auger <eric.auger@redhat.com>","To":"eric.auger.pro@gmail.com, eric.auger@redhat.com, peter.maydell@linaro.org,\n\talex.williamson@redhat.com, mst@redhat.com, qemu-arm@nongnu.org,\n\tqemu-devel@nongnu.org, jean-philippe.brucker@arm.com","Date":"Tue, 19 Sep 2017 09:46:38 +0200","Message-Id":"<1505807208-9063-7-git-send-email-eric.auger@redhat.com>","In-Reply-To":"<1505807208-9063-1-git-send-email-eric.auger@redhat.com>","References":"<1505807208-9063-1-git-send-email-eric.auger@redhat.com>","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.11","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.27]);\n\tTue, 19 Sep 2017 07:48:11 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"[Qemu-devel] [RFC v4 06/16] virtio-iommu: Register attached devices","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"wei@redhat.com, kevin.tian@intel.com, marc.zyngier@arm.com,\n\ttn@semihalf.com, \n\twill.deacon@arm.com, drjones@redhat.com, peterx@redhat.com,\n\tlinuc.decode@gmail.com, bharat.bhushan@nxp.com,\n\tchristoffer.dall@linaro.org","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"This patch introduce address space and device internal\ndatatypes. Both are stored in RB trees. The address space\nowns a list of devices attached to it.\n\nIt is assumed the devid corresponds to the PCI BDF.\n\nSigned-off-by: Eric Auger <eric.auger@redhat.com>\n\n---\nv3 -> v4:\n- new separate patch\n---\n hw/virtio/trace-events   |   4 ++\n hw/virtio/virtio-iommu.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 124 insertions(+)","diff":"diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events\nindex bc65356..74b92d3 100644\n--- a/hw/virtio/trace-events\n+++ b/hw/virtio/trace-events\n@@ -38,3 +38,7 @@ virtio_iommu_map(uint32_t as, uint64_t phys_addr, uint64_t virt_addr, uint64_t s\n virtio_iommu_unmap(uint32_t as, uint64_t virt_addr, uint64_t size) \"as= %d virt_addr=0x%\"PRIx64\" size=0x%\"PRIx64\n virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int flag) \"mr=%s rid=%d addr=0x%\"PRIx64\" flag=%d\"\n virtio_iommu_init_iommu_mr(char *iommu_mr) \"init %s\"\n+virtio_iommu_get_dev(uint32_t devid) \"Alloc devid=%d\"\n+virtio_iommu_put_dev(uint32_t devid) \"Free devid=%d\"\n+virtio_iommu_get_as(uint32_t asid) \"Alloc asid=%d\"\n+virtio_iommu_put_as(uint32_t asid) \"Free asid=%d\"\ndiff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c\nindex f4cb76f..41a4bbc 100644\n--- a/hw/virtio/virtio-iommu.c\n+++ b/hw/virtio/virtio-iommu.c\n@@ -32,15 +32,116 @@\n #include \"hw/virtio/virtio-bus.h\"\n #include \"hw/virtio/virtio-access.h\"\n #include \"hw/virtio/virtio-iommu.h\"\n+#include \"hw/pci/pci_bus.h\"\n+#include \"hw/pci/pci.h\"\n \n /* Max size */\n #define VIOMMU_DEFAULT_QUEUE_SIZE 256\n \n+typedef struct viommu_as {\n+    uint32_t id;\n+    GTree *mappings;\n+    QLIST_HEAD(, viommu_dev) device_list;\n+} viommu_as;\n+\n+typedef struct viommu_dev {\n+    uint32_t id;\n+    viommu_as *as;\n+    QLIST_ENTRY(viommu_dev) next;\n+    VirtIOIOMMU *viommu;\n+} viommu_dev;\n+\n+typedef struct viommu_interval {\n+    uint64_t low;\n+    uint64_t high;\n+} viommu_interval;\n+\n static inline uint16_t virtio_iommu_get_sid(IOMMUDevice *dev)\n {\n     return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn);\n }\n \n+static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data)\n+{\n+    viommu_interval *inta = (viommu_interval *)a;\n+    viommu_interval *intb = (viommu_interval *)b;\n+\n+    if (inta->high <= intb->low) {\n+        return -1;\n+    } else if (intb->high <= inta->low) {\n+        return 1;\n+    } else {\n+        return 0;\n+    }\n+}\n+\n+static void virtio_iommu_detach_dev_from_as(viommu_dev *dev)\n+{\n+    QLIST_REMOVE(dev, next);\n+    dev->as = NULL;\n+}\n+\n+static viommu_dev *virtio_iommu_get_dev(VirtIOIOMMU *s, uint32_t devid)\n+{\n+    viommu_dev *dev;\n+\n+    dev = g_tree_lookup(s->devices, GUINT_TO_POINTER(devid));\n+    if (dev) {\n+        return dev;\n+    }\n+    dev = g_malloc0(sizeof(*dev));\n+    dev->id = devid;\n+    dev->viommu = s;\n+    trace_virtio_iommu_get_dev(devid);\n+    g_tree_insert(s->devices, GUINT_TO_POINTER(devid), dev);\n+    return dev;\n+}\n+\n+static void virtio_iommu_put_dev(gpointer data)\n+{\n+    viommu_dev *dev = (viommu_dev *)data;\n+\n+    if (dev->as) {\n+        virtio_iommu_detach_dev_from_as(dev);\n+        g_tree_unref(dev->as->mappings);\n+    }\n+\n+    trace_virtio_iommu_put_dev(dev->id);\n+    g_free(dev);\n+}\n+\n+viommu_as *virtio_iommu_get_as(VirtIOIOMMU *s, uint32_t asid);\n+viommu_as *virtio_iommu_get_as(VirtIOIOMMU *s, uint32_t asid)\n+{\n+    viommu_as *as;\n+\n+    as = g_tree_lookup(s->address_spaces, GUINT_TO_POINTER(asid));\n+    if (as) {\n+        return as;\n+    }\n+    as = g_malloc0(sizeof(*as));\n+    as->id = asid;\n+    as->mappings = g_tree_new_full((GCompareDataFunc)interval_cmp,\n+                                   NULL, (GDestroyNotify)g_free,\n+                                   (GDestroyNotify)g_free);\n+    g_tree_insert(s->address_spaces, GUINT_TO_POINTER(asid), as);\n+    trace_virtio_iommu_get_as(asid);\n+    return as;\n+}\n+\n+static void virtio_iommu_put_as(gpointer data)\n+{\n+    viommu_as *as = (viommu_as *)data;\n+    viommu_dev *iter, *tmp;\n+\n+    QLIST_FOREACH_SAFE(iter, &as->device_list, next, tmp) {\n+        virtio_iommu_detach_dev_from_as(iter);\n+    }\n+    g_tree_destroy(as->mappings);\n+    trace_virtio_iommu_put_as(as->id);\n+    g_free(as);\n+}\n+\n static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque,\n                                               int devfn)\n {\n@@ -70,6 +171,8 @@ static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque,\n         sdev->bus = bus;\n         sdev->devfn = devfn;\n \n+        virtio_iommu_get_dev(s, PCI_BUILD_BDF(pci_bus_num(bus), devfn));\n+\n         trace_virtio_iommu_init_iommu_mr(name);\n \n         memory_region_init_iommu(&sdev->iommu_mr, sizeof(sdev->iommu_mr),\n@@ -360,6 +463,12 @@ static inline guint as_uint64_hash(gconstpointer v)\n     return (guint)*(const uint64_t *)v;\n }\n \n+static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)\n+{\n+    uint ua = GPOINTER_TO_UINT(a);\n+    uint ub = GPOINTER_TO_UINT(b);\n+    return (ua > ub) - (ua < ub);\n+}\n \n static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)\n {\n@@ -375,17 +484,28 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)\n     s->config.page_size_mask = TARGET_PAGE_MASK;\n     s->config.input_range.end = -1UL;\n \n+    qemu_mutex_init(&s->mutex);\n+\n     memset(s->as_by_bus_num, 0, sizeof(s->as_by_bus_num));\n     s->as_by_busptr = g_hash_table_new_full(as_uint64_hash,\n                                             as_uint64_equal,\n                                             g_free, g_free);\n \n+    s->address_spaces = g_tree_new_full((GCompareDataFunc)int_cmp,\n+                                         NULL, NULL, virtio_iommu_put_as);\n+    s->devices = g_tree_new_full((GCompareDataFunc)int_cmp,\n+                                 NULL, NULL, virtio_iommu_put_dev);\n+\n     virtio_iommu_init_as(s);\n }\n \n static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp)\n {\n     VirtIODevice *vdev = VIRTIO_DEVICE(dev);\n+    VirtIOIOMMU *s = VIRTIO_IOMMU(dev);\n+\n+    g_tree_destroy(s->address_spaces);\n+    g_tree_destroy(s->devices);\n \n     virtio_cleanup(vdev);\n }\n","prefixes":["RFC","v4","06/16"]}