{"id":815319,"url":"http://patchwork.ozlabs.org/api/patches/815319/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/1505807208-9063-6-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-6-git-send-email-eric.auger@redhat.com>","list_archive_url":null,"date":"2017-09-19T07:46:37","name":"[RFC,v4,05/16] virtio-iommu: Add the iommu regions","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"fbf3e48f5b44ea619adcde47b899fb57d0ba5dfe","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-6-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/815319/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/815319/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-mx06.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx06.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 3xxFSR0NPmz9s82\n\tfor <incoming@patchwork.ozlabs.org>;\n\tTue, 19 Sep 2017 17:51:35 +1000 (AEST)","from localhost ([::1]:40605 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 1duDJd-0001zY-3n\n\tfor incoming@patchwork.ozlabs.org; Tue, 19 Sep 2017 03:51:33 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:47460)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <eric.auger@redhat.com>) id 1duDGS-0008Js-Ub\n\tfor qemu-devel@nongnu.org; Tue, 19 Sep 2017 03:48:18 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <eric.auger@redhat.com>) id 1duDGP-0000s0-B1\n\tfor qemu-devel@nongnu.org; Tue, 19 Sep 2017 03:48:16 -0400","from mx1.redhat.com ([209.132.183.28]:51638)\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 1duDGC-0000lp-S7; Tue, 19 Sep 2017 03:48:01 -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 34F42267DB;\n\tTue, 19 Sep 2017 07:47:59 +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 E2D8B61342;\n\tTue, 19 Sep 2017 07:47:52 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 34F42267DB","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:37 +0200","Message-Id":"<1505807208-9063-6-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.30]);\n\tTue, 19 Sep 2017 07:47:59 +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 05/16] virtio-iommu: Add the iommu regions","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 initializes the iommu memory regions so that\nPCIe end point transactions get translated. The translation function\nis not yet implemented at that stage.\n\nSigned-off-by: Eric Auger <eric.auger@redhat.com>\n\n---\nv3 -> v4:\n- add trace_virtio_iommu_init_iommu_mr\n\nv2 -> v3:\n- use IOMMUMemoryRegion\n- iommu mr name built with BDF\n- rename smmu_get_sid into virtio_iommu_get_sid and use PCI_BUILD_BDF\n---\n hw/virtio/trace-events           |   2 +\n hw/virtio/virtio-iommu.c         | 119 +++++++++++++++++++++++++++++++++++++++\n include/hw/virtio/virtio-iommu.h |   2 +\n 3 files changed, 123 insertions(+)","diff":"diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events\nindex a35dc62..bc65356 100644\n--- a/hw/virtio/trace-events\n+++ b/hw/virtio/trace-events\n@@ -36,3 +36,5 @@ virtio_iommu_attach(uint32_t as, uint32_t dev) \"as=%d dev=%d\"\n virtio_iommu_detach(uint32_t dev) \"dev=%d\"\n virtio_iommu_map(uint32_t as, uint64_t phys_addr, uint64_t virt_addr, uint64_t size, uint32_t flags) \"as= %d phys_addr=0x%\"PRIx64\" virt_addr=0x%\"PRIx64\" size=0x%\"PRIx64\" flags=%d\"\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\"\ndiff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c\nindex 51f9003..f4cb76f 100644\n--- a/hw/virtio/virtio-iommu.c\n+++ b/hw/virtio/virtio-iommu.c\n@@ -23,6 +23,7 @@\n #include \"hw/virtio/virtio.h\"\n #include \"sysemu/kvm.h\"\n #include \"qapi-event.h\"\n+#include \"qemu/error-report.h\"\n #include \"trace.h\"\n \n #include \"standard-headers/linux/virtio_ids.h\"\n@@ -35,6 +36,66 @@\n /* Max size */\n #define VIOMMU_DEFAULT_QUEUE_SIZE 256\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 AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque,\n+                                              int devfn)\n+{\n+    VirtIOIOMMU *s = opaque;\n+    uintptr_t key = (uintptr_t)bus;\n+    IOMMUPciBus *sbus = g_hash_table_lookup(s->as_by_busptr, &key);\n+    IOMMUDevice *sdev;\n+\n+    if (!sbus) {\n+        uintptr_t *new_key = g_malloc(sizeof(*new_key));\n+\n+        *new_key = (uintptr_t)bus;\n+        sbus = g_malloc0(sizeof(IOMMUPciBus) +\n+                         sizeof(IOMMUDevice *) * IOMMU_PCI_DEVFN_MAX);\n+        sbus->bus = bus;\n+        g_hash_table_insert(s->as_by_busptr, new_key, sbus);\n+    }\n+\n+    sdev = sbus->pbdev[devfn];\n+    if (!sdev) {\n+        char *name = g_strdup_printf(\"%s-%d-%d\",\n+                                     TYPE_VIRTIO_IOMMU_MEMORY_REGION,\n+                                     pci_bus_num(bus), devfn);\n+        sdev = sbus->pbdev[devfn] = g_malloc0(sizeof(IOMMUDevice));\n+\n+        sdev->viommu = s;\n+        sdev->bus = bus;\n+        sdev->devfn = devfn;\n+\n+        trace_virtio_iommu_init_iommu_mr(name);\n+\n+        memory_region_init_iommu(&sdev->iommu_mr, sizeof(sdev->iommu_mr),\n+                                 TYPE_VIRTIO_IOMMU_MEMORY_REGION,\n+                                 OBJECT(s), name,\n+                                 UINT64_MAX);\n+        address_space_init(&sdev->as,\n+                           MEMORY_REGION(&sdev->iommu_mr), TYPE_VIRTIO_IOMMU);\n+    }\n+\n+    return &sdev->as;\n+\n+}\n+\n+static void virtio_iommu_init_as(VirtIOIOMMU *s)\n+{\n+    PCIBus *pcibus = pci_find_primary_bus();\n+\n+    if (pcibus) {\n+        pci_setup_iommu(pcibus, virtio_iommu_find_add_as, s);\n+    } else {\n+        error_report(\"No PCI bus, virtio-iommu is not registered\");\n+    }\n+}\n+\n+\n static int virtio_iommu_attach(VirtIOIOMMU *s,\n                                struct virtio_iommu_req_attach *req)\n {\n@@ -215,6 +276,26 @@ static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq)\n     }\n }\n \n+static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr,\n+                                            IOMMUAccessFlags flag)\n+{\n+    IOMMUDevice *sdev = container_of(mr, IOMMUDevice, iommu_mr);\n+    uint32_t sid;\n+\n+    IOMMUTLBEntry entry = {\n+        .target_as = &address_space_memory,\n+        .iova = addr,\n+        .translated_addr = addr,\n+        .addr_mask = ~(hwaddr)0,\n+        .perm = IOMMU_NONE,\n+    };\n+\n+    sid = virtio_iommu_get_sid(sdev);\n+\n+    trace_virtio_iommu_translate(mr->parent_obj.name, sid, addr, flag);\n+    return entry;\n+}\n+\n static void virtio_iommu_get_config(VirtIODevice *vdev, uint8_t *config_data)\n {\n     VirtIOIOMMU *dev = VIRTIO_IOMMU(vdev);\n@@ -265,6 +346,21 @@ static const VMStateDescription vmstate_virtio_iommu_device = {\n     },\n };\n \n+/*****************************\n+ * Hash Table\n+ *****************************/\n+\n+static inline gboolean as_uint64_equal(gconstpointer v1, gconstpointer v2)\n+{\n+    return *((const uint64_t *)v1) == *((const uint64_t *)v2);\n+}\n+\n+static inline guint as_uint64_hash(gconstpointer v)\n+{\n+    return (guint)*(const uint64_t *)v;\n+}\n+\n+\n static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)\n {\n     VirtIODevice *vdev = VIRTIO_DEVICE(dev);\n@@ -278,6 +374,13 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)\n \n     s->config.page_size_mask = TARGET_PAGE_MASK;\n     s->config.input_range.end = -1UL;\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+    virtio_iommu_init_as(s);\n }\n \n static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp)\n@@ -335,6 +438,14 @@ static void virtio_iommu_class_init(ObjectClass *klass, void *data)\n     vdc->vmsd = &vmstate_virtio_iommu_device;\n }\n \n+static void virtio_iommu_memory_region_class_init(ObjectClass *klass,\n+                                                  void *data)\n+{\n+    IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);\n+\n+    imrc->translate = virtio_iommu_translate;\n+}\n+\n static const TypeInfo virtio_iommu_info = {\n     .name = TYPE_VIRTIO_IOMMU,\n     .parent = TYPE_VIRTIO_DEVICE,\n@@ -343,9 +454,17 @@ static const TypeInfo virtio_iommu_info = {\n     .class_init = virtio_iommu_class_init,\n };\n \n+static const TypeInfo virtio_iommu_memory_region_info = {\n+    .parent = TYPE_IOMMU_MEMORY_REGION,\n+    .name = TYPE_VIRTIO_IOMMU_MEMORY_REGION,\n+    .class_init = virtio_iommu_memory_region_class_init,\n+};\n+\n+\n static void virtio_register_types(void)\n {\n     type_register_static(&virtio_iommu_info);\n+    type_register_static(&virtio_iommu_memory_region_info);\n }\n \n type_init(virtio_register_types)\ndiff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-iommu.h\nindex 6716cdb..f9c988f 100644\n--- a/include/hw/virtio/virtio-iommu.h\n+++ b/include/hw/virtio/virtio-iommu.h\n@@ -28,6 +28,8 @@\n #define VIRTIO_IOMMU(obj) \\\n         OBJECT_CHECK(VirtIOIOMMU, (obj), TYPE_VIRTIO_IOMMU)\n \n+#define TYPE_VIRTIO_IOMMU_MEMORY_REGION \"virtio-iommu-memory-region\"\n+\n #define IOMMU_PCI_BUS_MAX      256\n #define IOMMU_PCI_DEVFN_MAX    256\n \n","prefixes":["RFC","v4","05/16"]}