From nobody Tue Nov 4 11:01:33 2025 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 1505807790436222.4265949178689; Tue, 19 Sep 2017 00:56:30 -0700 (PDT) Received: from localhost ([::1]:40634 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duDOP-0006LT-J1 for importer@patchew.org; Tue, 19 Sep 2017 03:56:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48018) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1duDHZ-0000qk-On for qemu-devel@nongnu.org; Tue, 19 Sep 2017 03:49:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1duDHY-0001PU-90 for qemu-devel@nongnu.org; Tue, 19 Sep 2017 03:49:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46580) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1duDHR-0001Lc-QM; Tue, 19 Sep 2017 03:49:18 -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 B4783C0546EC; Tue, 19 Sep 2017 07:49:16 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-116-197.ams2.redhat.com [10.36.116.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9CFF61348; Tue, 19 Sep 2017 07:48:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com B4783C0546EC Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.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, 19 Sep 2017 09:46:42 +0200 Message-Id: <1505807208-9063-11-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 (mx1.redhat.com [10.5.110.32]); Tue, 19 Sep 2017 07:49:16 +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 v4 10/16] virtio-iommu: Implement probe request 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, linuc.decode@gmail.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 implements the PROBE request. At the moment, no reserved regions are returned. At the moment reserved regions are stored per device. Signed-off-by: Eric Auger --- Waiting for clarifications on v0.4 spec --- hw/virtio/trace-events | 2 + hw/virtio/virtio-iommu.c | 173 +++++++++++++++++++++++++++++++++++++++++++= +++- 2 files changed, 173 insertions(+), 2 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 9010fbd..9ccfad1 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -46,3 +46,5 @@ virtio_iommu_unmap_left_interval(uint64_t low, uint64_t h= igh, uint64_t next_low, 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_out(uint64_t virt_addr, uint64_t phys_addr, uint32_= t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=3D%d" +virtio_iommu_fill_resv_property(uint32_t devid, uint8_t subtype, uint64_t = addr, uint64_t size, uint32_t flags, size_t filled) "dev=3D %d, subtype=3D%= d addr=3D0x%"PRIx64" size=3D0x%"PRIx64" flags=3D%d filled=3D0x%lx" +virtio_iommu_fill_none_property(uint32_t devid) "devid=3D%d" diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index db46a91..281b0f8 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -37,6 +37,11 @@ =20 /* Max size */ #define VIOMMU_DEFAULT_QUEUE_SIZE 256 +#define VIOMMU_PROBE_SIZE 512 + +#define SUPPORTED_PROBE_PROPERTIES (\ + VIRTIO_IOMMU_PROBE_T_NONE | \ + VIRTIO_IOMMU_PROBE_T_RESV_MEM) =20 typedef struct viommu_as { uint32_t id; @@ -49,6 +54,7 @@ typedef struct viommu_dev { viommu_as *as; QLIST_ENTRY(viommu_dev) next; VirtIOIOMMU *viommu; + GTree *reserved_regions; } viommu_dev; =20 typedef struct viommu_interval { @@ -63,6 +69,13 @@ typedef struct viommu_mapping { uint32_t flags; } viommu_mapping; =20 +typedef struct viommu_property_buffer { + viommu_dev *dev; + size_t filled; + uint8_t *start; + bool error; +} viommu_property_buffer; + static inline uint16_t virtio_iommu_get_sid(IOMMUDevice *dev) { return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn); @@ -101,6 +114,9 @@ static viommu_dev *virtio_iommu_get_dev(VirtIOIOMMU *s,= uint32_t devid) dev->viommu =3D s; trace_virtio_iommu_get_dev(devid); g_tree_insert(s->devices, GUINT_TO_POINTER(devid), dev); + dev->reserved_regions =3D g_tree_new_full((GCompareDataFunc)interval_c= mp, + NULL, (GDestroyNotify)g_free, + (GDestroyNotify)g_free); return dev; } =20 @@ -114,6 +130,7 @@ static void virtio_iommu_put_dev(gpointer data) } =20 trace_virtio_iommu_put_dev(dev->id); + g_tree_destroy(dev->reserved_regions); g_free(dev); } =20 @@ -369,6 +386,123 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s, return VIRTIO_IOMMU_S_INVAL; } =20 +static gboolean virtio_iommu_fill_resv_mem_prop(gpointer key, + gpointer value, + gpointer data) +{ + struct virtio_iommu_probe_resv_mem *resv =3D + (struct virtio_iommu_probe_resv_mem *)value; + struct virtio_iommu_probe_property *prop; + struct virtio_iommu_probe_resv_mem *current; + viommu_property_buffer *bufstate =3D (viommu_property_buffer *)data; + size_t size =3D sizeof(*resv), total_size; + + total_size =3D size + 4; + + if (bufstate->filled >=3D VIOMMU_PROBE_SIZE) { + bufstate->error =3D true; + return true; + } + prop =3D (struct virtio_iommu_probe_property *) + (bufstate->start + bufstate->filled); + prop->type =3D cpu_to_le16(VIRTIO_IOMMU_PROBE_T_RESV_MEM) & + VIRTIO_IOMMU_PROBE_T_MASK; + prop->length =3D size; + + current =3D (struct virtio_iommu_probe_resv_mem *)prop->value; + *current =3D *resv; + bufstate->filled +=3D total_size; + trace_virtio_iommu_fill_resv_property(bufstate->dev->id, + resv->subtype, resv->addr, + resv->size, resv->flags, + bufstate->filled); + return false; +} + +static int virtio_iommu_fill_none_prop(viommu_property_buffer *bufstate) +{ + struct virtio_iommu_probe_property *prop; + + prop =3D (struct virtio_iommu_probe_property *) + (bufstate->start + bufstate->filled); + prop->type =3D cpu_to_le16(VIRTIO_IOMMU_PROBE_T_NONE) + & VIRTIO_IOMMU_PROBE_T_MASK; + prop->length =3D 0; + bufstate->filled +=3D 4; + trace_virtio_iommu_fill_none_property(bufstate->dev->id); + return 0; +} + +static int virtio_iommu_fill_property(int devid, int type, + viommu_property_buffer *bufstate) +{ + int ret =3D -ENOSPC; + + if (bufstate->filled + 4 >=3D VIOMMU_PROBE_SIZE) { + bufstate->error =3D true; + goto out; + } + + switch (type) { + case VIRTIO_IOMMU_PROBE_T_NONE: + ret =3D virtio_iommu_fill_none_prop(bufstate); + break; + case VIRTIO_IOMMU_PROBE_T_RESV_MEM: + { + viommu_dev *dev =3D bufstate->dev; + + g_tree_foreach(dev->reserved_regions, + virtio_iommu_fill_resv_mem_prop, + bufstate); + if (!bufstate->error) { + ret =3D 0; + } + break; + } + default: + ret =3D -ENOENT; + break; + } +out: + if (ret) { + error_report("%s property of type=3D%d could not be filled (%d)," + " remaining size =3D 0x%lx", + __func__, type, ret, bufstate->filled); + } + return ret; +} + +static int virtio_iommu_probe(VirtIOIOMMU *s, + struct virtio_iommu_req_probe *req, + uint8_t *buf) +{ + uint32_t devid =3D le32_to_cpu(req->device); + int16_t prop_types =3D SUPPORTED_PROBE_PROPERTIES, type; + viommu_property_buffer bufstate; + viommu_dev *dev; + int ret; + + dev =3D g_tree_lookup(s->devices, GUINT_TO_POINTER(devid)); + if (!dev) { + return -EINVAL; + } + + bufstate.start =3D buf; + bufstate.filled =3D 0; + bufstate.dev =3D dev; + + while ((type =3D ctz32(prop_types)) !=3D 32) { + ret =3D virtio_iommu_fill_property(devid, 1 << type, &bufstate); + if (ret) { + break; + } + prop_types &=3D ~(1 << type); + } + virtio_iommu_fill_property(devid, VIRTIO_IOMMU_PROBE_T_NONE, &bufstate= ); + + return VIRTIO_IOMMU_S_OK; +} + #define get_payload_size(req) (\ sizeof((req)) - sizeof(struct virtio_iommu_req_tail)) =20 @@ -433,6 +567,24 @@ static int virtio_iommu_handle_unmap(VirtIOIOMMU *s, return virtio_iommu_unmap(s, &req); } =20 +static int virtio_iommu_handle_probe(VirtIOIOMMU *s, + struct iovec *iov, + unsigned int iov_cnt, + uint8_t *buf) +{ + struct virtio_iommu_req_probe req; + size_t sz, payload_sz; + + payload_sz =3D get_payload_size(req); + + sz =3D iov_to_buf(iov, iov_cnt, 0, &req, payload_sz); + if (sz !=3D payload_sz) { + return VIRTIO_IOMMU_S_INVAL; + } + + return virtio_iommu_probe(s, &req, buf); +} + static void virtio_iommu_handle_command(VirtIODevice *vdev, VirtQueue *vq) { VirtIOIOMMU *s =3D VIRTIO_IOMMU(vdev); @@ -477,16 +629,31 @@ static void virtio_iommu_handle_command(VirtIODevice = *vdev, VirtQueue *vq) case VIRTIO_IOMMU_T_UNMAP: tail.status =3D virtio_iommu_handle_unmap(s, iov, iov_cnt); break; + case VIRTIO_IOMMU_T_PROBE: + { + struct virtio_iommu_req_tail *ptail; + uint8_t *buf =3D g_malloc0(s->config.probe_size + sizeof(tail)= ); + + ptail =3D (struct virtio_iommu_req_tail *)buf + s->config.prob= e_size; + ptail->status =3D virtio_iommu_handle_probe(s, iov, iov_cnt, b= uf); + + sz =3D iov_from_buf(elem->in_sg, elem->in_num, 0, + buf, s->config.probe_size + sizeof(tail)); + g_free(buf); + assert(sz =3D=3D s->config.probe_size + sizeof(tail)); + goto push; + } default: tail.status =3D VIRTIO_IOMMU_S_UNSUPP; } - qemu_mutex_unlock(&s->mutex); =20 sz =3D iov_from_buf(elem->in_sg, elem->in_num, 0, &tail, sizeof(tail)); assert(sz =3D=3D sizeof(tail)); =20 - virtqueue_push(vq, elem, sizeof(tail)); +push: + qemu_mutex_unlock(&s->mutex); + virtqueue_push(vq, elem, sz); virtio_notify(vdev, vq); g_free(elem); } @@ -574,6 +741,7 @@ static uint64_t virtio_iommu_get_features(VirtIODevice = *vdev, uint64_t f, virtio_add_feature(&f, VIRTIO_RING_F_INDIRECT_DESC); virtio_add_feature(&f, VIRTIO_IOMMU_F_INPUT_RANGE); virtio_add_feature(&f, VIRTIO_IOMMU_F_MAP_UNMAP); + virtio_add_feature(&f, VIRTIO_IOMMU_F_PROBE); return f; } =20 @@ -631,6 +799,7 @@ static void virtio_iommu_device_realize(DeviceState *de= v, Error **errp) =20 s->config.page_size_mask =3D TARGET_PAGE_MASK; s->config.input_range.end =3D -1UL; + s->config.probe_size =3D VIOMMU_PROBE_SIZE; =20 qemu_mutex_init(&s->mutex); =20 --=20 2.5.5