From nobody Mon Feb 9 16:50:46 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1501580410469371.74688944313584; Tue, 1 Aug 2017 02:40:10 -0700 (PDT) Received: from localhost ([::1]:41406 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dcTeq-000360-Vu for importer@patchew.org; Tue, 01 Aug 2017 05:40:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38409) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dcTa9-00081X-0l for qemu-devel@nongnu.org; Tue, 01 Aug 2017 05:35:19 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dcTa5-0002Nl-Na for qemu-devel@nongnu.org; Tue, 01 Aug 2017 05:35:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44898) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dcTZw-00022g-6s; Tue, 01 Aug 2017 05:35:04 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E49F910D9C6; Tue, 1 Aug 2017 09:35:02 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-181.ams2.redhat.com [10.36.116.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F9966047A; Tue, 1 Aug 2017 09:34:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E49F910D9C6 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=eric.auger@redhat.com From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, peter.maydell@linaro.org, alex.williamson@redhat.com, mst@redhat.com, qemu-arm@nongnu.org, qemu-devel@nongnu.org, jean-philippe.brucker@arm.com Date: Tue, 1 Aug 2017 11:33:12 +0200 Message-Id: <1501579994-3320-7-git-send-email-eric.auger@redhat.com> In-Reply-To: <1501579994-3320-1-git-send-email-eric.auger@redhat.com> References: <1501579994-3320-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 (mx1.redhat.com [10.5.110.26]); Tue, 01 Aug 2017 09:35:03 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC v3 6/8] virtio-iommu: Implement the translation and commands X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: wei@redhat.com, kevin.tian@intel.com, marc.zyngier@arm.com, tn@semihalf.com, will.deacon@arm.com, drjones@redhat.com, peterx@redhat.com, bharat.bhushan@nxp.com, christoffer.dall@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds the actual implementation for the translation routine and the virtio-iommu commands. Signed-off-by: Eric Auger --- v2 -> v3: - init the mutex - return VIRTIO_IOMMU_S_INVAL is reserved field is not null on attach/detach commands - on attach, when the device is already attached to an address space, detach it first instead of returning an error - remove nr_devices and use g_tree_ref/unref to destroy the as->mappings on last device detach - map/unmap: return NOENT instead of INVAL error if as does not exist - remove flags argument from attach/detach trace functions v1 -> v2: - fix compilation issue reported by autobuild system --- hw/virtio/trace-events | 10 +- hw/virtio/virtio-iommu.c | 232 +++++++++++++++++++++++++++++++++++++++++++= ++-- 2 files changed, 232 insertions(+), 10 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 341dbdf..8db3d91 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -28,8 +28,14 @@ virtio_balloon_to_target(uint64_t target, uint32_t num_p= ages) "balloon target: % =20 # hw/virtio/virtio-iommu.c # -virtio_iommu_attach(uint32_t as, uint32_t dev, uint32_t flags) "as=3D%d de= v=3D%d flags=3D%d" -virtio_iommu_detach(uint32_t dev, uint32_t flags) "dev=3D%d flags=3D%d" +virtio_iommu_attach(uint32_t as, uint32_t dev) "as=3D%d dev=3D%d" +virtio_iommu_detach(uint32_t dev) "dev=3D%d" virtio_iommu_map(uint32_t as, uint64_t phys_addr, uint64_t virt_addr, uint= 64_t size, uint32_t flags) "as=3D %d phys_addr=3D0x%"PRIx64" virt_addr=3D0x= %"PRIx64" size=3D0x%"PRIx64" flags=3D%d" virtio_iommu_unmap(uint32_t as, uint64_t virt_addr, uint64_t size, uint32_= t reserved) "as=3D %d virt_addr=3D0x%"PRIx64" size=3D0x%"PRIx64" reserved= =3D%d" virtio_iommu_translate(const char *name, uint32_t rid, uint64_t iova, int = flag) "mr=3D%s rid=3D%d addr=3D0x%"PRIx64" flag=3D%d" +virtio_iommu_new_asid(uint32_t asid) "Allocate a new asid=3D%d" +virtio_iommu_new_devid(uint32_t devid) "Allocate a new devid=3D%d" +virtio_iommu_unmap_left_interval(uint64_t low, uint64_t high, uint64_t nex= t_low, uint64_t next_high) "Unmap left [0x%"PRIx64",0x%"PRIx64"], new inter= val=3D[0x%"PRIx64",0x%"PRIx64"]" +virtio_iommu_unmap_right_interval(uint64_t low, uint64_t high, uint64_t ne= xt_low, uint64_t next_high) "Unmap right [0x%"PRIx64",0x%"PRIx64"], new int= erval=3D[0x%"PRIx64",0x%"PRIx64"]" +virtio_iommu_unmap_inc_interval(uint64_t low, uint64_t high) "Unmap inc [0= x%"PRIx64",0x%"PRIx64"]" +virtio_iommu_translate_result(uint64_t virt_addr, uint64_t phys_addr, uint= 32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=3D%d" diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index e663d9e..9217587 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -32,10 +32,36 @@ #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" =20 /* Max size */ #define VIOMMU_DEFAULT_QUEUE_SIZE 256 =20 +typedef struct viommu_as viommu_as; + +typedef struct viommu_mapping { + uint64_t virt_addr; + uint64_t phys_addr; + uint64_t size; + uint32_t flags; +} viommu_mapping; + +typedef struct viommu_interval { + uint64_t low; + uint64_t high; +} viommu_interval; + +typedef struct viommu_dev { + uint32_t id; + viommu_as *as; +} viommu_dev; + +struct viommu_as { + uint32_t id; + GTree *mappings; +}; + static inline uint16_t virtio_iommu_get_sid(IOMMUDevice *dev) { return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn); @@ -93,6 +119,29 @@ static void virtio_iommu_init_as(VirtIOIOMMU *s) } } =20 +static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_d= ata) +{ + viommu_interval *inta =3D (viommu_interval *)a; + viommu_interval *intb =3D (viommu_interval *)b; + + if (inta->high <=3D intb->low) { + return -1; + } else if (intb->high <=3D inta->low) { + return 1; + } else { + return 0; + } +} + +static void virtio_iommu_detach_dev(VirtIOIOMMU *s, viommu_dev *dev) +{ + viommu_as *as =3D dev->as; + + trace_virtio_iommu_detach(dev->id); + + g_tree_remove(s->devices, GUINT_TO_POINTER(dev->id)); + g_tree_unref(as->mappings); +} =20 static int virtio_iommu_attach(VirtIOIOMMU *s, struct virtio_iommu_req_attach *req) @@ -100,10 +149,42 @@ static int virtio_iommu_attach(VirtIOIOMMU *s, uint32_t asid =3D le32_to_cpu(req->address_space); uint32_t devid =3D le32_to_cpu(req->device); uint32_t reserved =3D le32_to_cpu(req->reserved); + viommu_as *as; + viommu_dev *dev; + + trace_virtio_iommu_attach(asid, devid); + + if (reserved) { + return VIRTIO_IOMMU_S_INVAL; + } + + dev =3D g_tree_lookup(s->devices, GUINT_TO_POINTER(devid)); + if (dev) { + /* + * the device is already attached to an address space, + * detach it first + */ + virtio_iommu_detach_dev(s, dev); + } =20 - trace_virtio_iommu_attach(asid, devid, reserved); + as =3D g_tree_lookup(s->address_spaces, GUINT_TO_POINTER(asid)); + if (!as) { + as =3D g_malloc0(sizeof(*as)); + as->id =3D asid; + as->mappings =3D g_tree_new_full((GCompareDataFunc)interval_cmp, + NULL, NULL, (GDestroyNotify)g_fre= e); + g_tree_insert(s->address_spaces, GUINT_TO_POINTER(asid), as); + trace_virtio_iommu_new_asid(asid); + } =20 - return VIRTIO_IOMMU_S_UNSUPP; + dev =3D g_malloc0(sizeof(*dev)); + dev->as =3D as; + dev->id =3D devid; + trace_virtio_iommu_new_devid(devid); + g_tree_insert(s->devices, GUINT_TO_POINTER(devid), dev); + g_tree_ref(as->mappings); + + return VIRTIO_IOMMU_S_OK; } =20 static int virtio_iommu_detach(VirtIOIOMMU *s, @@ -111,10 +192,20 @@ static int virtio_iommu_detach(VirtIOIOMMU *s, { uint32_t devid =3D le32_to_cpu(req->device); uint32_t reserved =3D le32_to_cpu(req->reserved); + viommu_dev *dev; + + if (reserved) { + return VIRTIO_IOMMU_S_INVAL; + } + + dev =3D g_tree_lookup(s->devices, GUINT_TO_POINTER(devid)); + if (!dev) { + return VIRTIO_IOMMU_S_INVAL; + } =20 - trace_virtio_iommu_detach(devid, reserved); + virtio_iommu_detach_dev(s, dev); =20 - return VIRTIO_IOMMU_S_UNSUPP; + return VIRTIO_IOMMU_S_OK; } =20 static int virtio_iommu_map(VirtIOIOMMU *s, @@ -125,10 +216,37 @@ static int virtio_iommu_map(VirtIOIOMMU *s, uint64_t virt_addr =3D le64_to_cpu(req->virt_addr); uint64_t size =3D le64_to_cpu(req->size); uint32_t flags =3D le32_to_cpu(req->flags); + viommu_as *as; + viommu_interval *interval; + viommu_mapping *mapping; + + interval =3D g_malloc0(sizeof(*interval)); + + interval->low =3D virt_addr; + interval->high =3D virt_addr + size - 1; + + as =3D g_tree_lookup(s->address_spaces, GUINT_TO_POINTER(asid)); + if (!as) { + return VIRTIO_IOMMU_S_NOENT; + } + + mapping =3D g_tree_lookup(as->mappings, (gpointer)interval); + if (mapping) { + g_free(interval); + return VIRTIO_IOMMU_S_INVAL; + } =20 trace_virtio_iommu_map(asid, phys_addr, virt_addr, size, flags); =20 - return VIRTIO_IOMMU_S_UNSUPP; + mapping =3D g_malloc0(sizeof(*mapping)); + mapping->virt_addr =3D virt_addr; + mapping->phys_addr =3D phys_addr; + mapping->size =3D size; + mapping->flags =3D flags; + + g_tree_insert(as->mappings, interval, mapping); + + return VIRTIO_IOMMU_S_OK; } =20 static int virtio_iommu_unmap(VirtIOIOMMU *s, @@ -138,10 +256,64 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s, uint64_t virt_addr =3D le64_to_cpu(req->virt_addr); uint64_t size =3D le64_to_cpu(req->size); uint32_t flags =3D le32_to_cpu(req->flags); + viommu_mapping *mapping; + viommu_interval interval; + viommu_as *as; =20 trace_virtio_iommu_unmap(asid, virt_addr, size, flags); =20 - return VIRTIO_IOMMU_S_UNSUPP; + as =3D g_tree_lookup(s->address_spaces, GUINT_TO_POINTER(asid)); + if (!as) { + error_report("%s: no as", __func__); + return VIRTIO_IOMMU_S_NOENT; + } + interval.low =3D virt_addr; + interval.high =3D virt_addr + size - 1; + + mapping =3D g_tree_lookup(as->mappings, (gpointer)&interval); + + while (mapping) { + viommu_interval current; + uint64_t low =3D mapping->virt_addr; + uint64_t high =3D mapping->virt_addr + mapping->size - 1; + + current.low =3D low; + current.high =3D high; + + if (low =3D=3D interval.low && size >=3D mapping->size) { + g_tree_remove(as->mappings, (gpointer)¤t); + interval.low =3D high + 1; + trace_virtio_iommu_unmap_left_interval(current.low, current.hi= gh, + interval.low, interval.high); + } else if (high =3D=3D interval.high && size >=3D mapping->size) { + trace_virtio_iommu_unmap_right_interval(current.low, current.h= igh, + interval.low, interval.high); + g_tree_remove(as->mappings, (gpointer)¤t); + interval.high =3D low - 1; + } else if (low > interval.low && high < interval.high) { + trace_virtio_iommu_unmap_inc_interval(current.low, current.hig= h); + g_tree_remove(as->mappings, (gpointer)¤t); + } else { + break; + } + if (interval.low >=3D interval.high) { + return VIRTIO_IOMMU_S_OK; + } else { + mapping =3D g_tree_lookup(as->mappings, (gpointer)&interval); + } + } + + if (mapping) { + error_report("****** %s: Unmap 0x%"PRIx64" size=3D0x%"PRIx64 + " from 0x%"PRIx64" size=3D0x%"PRIx64" is not supporte= d", + __func__, interval.low, size, + mapping->virt_addr, mapping->size); + } else { + error_report("****** %s: no mapping for [0x%"PRIx64",0x%"PRIx64"]", + __func__, interval.low, interval.high); + } + + return VIRTIO_IOMMU_S_INVAL; } =20 #define get_payload_size(req) (\ @@ -271,19 +443,46 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemo= ryRegion *mr, hwaddr addr, IOMMUAccessFlags flag) { IOMMUDevice *sdev =3D container_of(mr, IOMMUDevice, iommu_mr); + VirtIOIOMMU *s =3D sdev->viommu; uint32_t sid; + viommu_dev *dev; + viommu_mapping *mapping; + viommu_interval interval; + + interval.low =3D addr; + interval.high =3D addr + 1; =20 IOMMUTLBEntry entry =3D { .target_as =3D &address_space_memory, .iova =3D addr, .translated_addr =3D addr, - .addr_mask =3D ~(hwaddr)0, - .perm =3D IOMMU_NONE, + .addr_mask =3D (1 << 12) - 1, /* TODO */ + .perm =3D 3, }; =20 sid =3D virtio_iommu_get_sid(sdev); =20 trace_virtio_iommu_translate(mr->parent_obj.name, sid, addr, flag); + qemu_mutex_lock(&s->mutex); + + dev =3D g_tree_lookup(s->devices, GUINT_TO_POINTER(sid)); + if (!dev) { + /* device cannot be attached to another as */ + printf("%s sid=3D%d is not known!!\n", __func__, sid); + goto unlock; + } + + mapping =3D g_tree_lookup(dev->as->mappings, (gpointer)&interval); + if (!mapping) { + printf("%s no mapping for 0x%"PRIx64" for sid=3D%d\n", __func__, + addr, sid); + goto unlock; + } + entry.translated_addr =3D addr - mapping->virt_addr + mapping->phys_ad= dr, + trace_virtio_iommu_translate_result(addr, entry.translated_addr, sid); + +unlock: + qemu_mutex_unlock(&s->mutex); return entry; } =20 @@ -347,6 +546,12 @@ static inline guint as_uint64_hash(gconstpointer v) return (guint)*(const uint64_t *)v; } =20 +static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data) +{ + uint ua =3D GPOINTER_TO_UINT(a); + uint ub =3D GPOINTER_TO_UINT(b); + return (ua > ub) - (ua < ub); +} =20 static void virtio_iommu_device_realize(DeviceState *dev, Error **errp) { @@ -362,17 +567,28 @@ static void virtio_iommu_device_realize(DeviceState *= dev, Error **errp) s->config.page_sizes =3D TARGET_PAGE_MASK; s->config.input_range.end =3D -1UL; =20 + qemu_mutex_init(&s->mutex); + memset(s->as_by_bus_num, 0, sizeof(s->as_by_bus_num)); s->as_by_busptr =3D g_hash_table_new_full(as_uint64_hash, as_uint64_equal, g_free, g_free); =20 + s->address_spaces =3D g_tree_new_full((GCompareDataFunc)int_cmp, + NULL, NULL, (GDestroyNotify)g_fre= e); + s->devices =3D g_tree_new_full((GCompareDataFunc)int_cmp, + NULL, NULL, (GDestroyNotify)g_fre= e); + virtio_iommu_init_as(s); } =20 static void virtio_iommu_device_unrealize(DeviceState *dev, Error **errp) { VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); + VirtIOIOMMU *s =3D VIRTIO_IOMMU(dev); + + g_tree_destroy(s->address_spaces); + g_tree_destroy(s->devices); =20 virtio_cleanup(vdev); } --=20 2.5.5