From nobody Mon Feb 9 19:54:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=marvell.com ARC-Seal: i=1; a=rsa-sha256; t=1584110434; cv=none; d=zohomail.com; s=zohoarc; b=KYaaUdBl5qNS19OwSC85ydVnwSC4XGigWxoWNnoAQmvuhNU45gRuDQsSob+1hjUaii+eL2wC76ZQe6szpDNmV7Rt+jxGMA3sTFa8KSn3C0iFZhZDbuO/2fHqYWcn3y7IpEpsG3TG8C8TsWWz7PKRK4iMk4T6he4l7j6enRr5osw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1584110434; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=LjywHiaOudVGVnAq4/GJUsKnBJ0ndzG4l4NRbVS34SM=; b=Epe05npDlfnzYUi/1p+uZiUhXVpXXS5O1IP/McfY3E/mp3TFSwfXgDo0rVKEWsd8MZQ4ZRE4MLlrJHGzVcQBtALxo3QE3eDqJ94zDNwbHECDntW/vQ1TwlRbT1Y1FUiecYZUgbU5R5CGUmxmRapdQR/DNxg+eBIXzAuJyKQeE/8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1584110434767271.58240654683175; Fri, 13 Mar 2020 07:40:34 -0700 (PDT) Received: from localhost ([::1]:60116 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jClUH-0001Bd-Lh for importer@patchew.org; Fri, 13 Mar 2020 10:40:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32845) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jCf3c-0005uf-4w for qemu-devel@nongnu.org; Fri, 13 Mar 2020 03:48:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jCf3a-0001pE-U2 for qemu-devel@nongnu.org; Fri, 13 Mar 2020 03:48:36 -0400 Received: from mx0a-0016f401.pphosted.com ([67.231.148.174]:40254 helo=mx0b-0016f401.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jCf3Y-0001cn-1v; Fri, 13 Mar 2020 03:48:32 -0400 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 02D7iq87022612; Fri, 13 Mar 2020 00:48:29 -0700 Received: from sc-exch02.marvell.com ([199.233.58.182]) by mx0a-0016f401.pphosted.com with ESMTP id 2yqt7t2va8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Fri, 13 Mar 2020 00:48:29 -0700 Received: from SC-EXCH04.marvell.com (10.93.176.84) by SC-EXCH02.marvell.com (10.93.176.82) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 13 Mar 2020 00:48:28 -0700 Received: from bbhushan2.marvell.com (10.93.176.43) by SC-EXCH04.marvell.com (10.93.176.84) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Fri, 13 Mar 2020 00:48:24 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=pfpt0818; bh=LjywHiaOudVGVnAq4/GJUsKnBJ0ndzG4l4NRbVS34SM=; b=c6eKI6qkQOk7Iv5QgcY/3s1pJzpwcfn4kj+4NYmjwgMSD8S4p5IiNwGrEHwlpz8n1hd/ qPyHPw8TWxXQnBbdhR+040pX2/jxWeGjyOl933BCU0npAP6VnhQnfqKveipwMk7hugK1 9olorg6YkcC/tRT4fdM1ra/ESE313aP88FZ05t9l/3z4Ggi3DTu90A7s8RyPv0zisLeY FWCdlYcTa7aky3vxW6Ox8xPjF1hISYtCSvYmyAJF+kpQW0pIQ5uqTH3PHbYwbQDigFIh zqE6MViHKzf4x+iikQjtDWHGhJRKBiu1Bu+Kv+JzIHZBc4sXhR0eqF8SOY0rLKoKjNY+ xQ== From: Bharat Bhushan To: , , , , , , , , , , , Subject: [PATCH v7 2/5] virtio-iommu: Add iommu notifier for map/unmap Date: Fri, 13 Mar 2020 13:18:08 +0530 Message-ID: <20200313074811.27175-3-bbhushan2@marvell.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200313074811.27175-1-bbhushan2@marvell.com> References: <20200313074811.27175-1-bbhushan2@marvell.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.572 definitions=2020-03-13_03:2020-03-11, 2020-03-13 signatures=0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 67.231.148.174 X-Mailman-Approved-At: Fri, 13 Mar 2020 10:36:05 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eric Auger , Bharat Bhushan Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch extends VIRTIO_IOMMU_T_MAP/UNMAP request to notify registered iommu-notifier. Which will call vfio notifier to map/unmap region in iommu. Signed-off-by: Bharat Bhushan Signed-off-by: Eric Auger --- hw/virtio/trace-events | 2 + hw/virtio/virtio-iommu.c | 66 +++++++++++++++++++++++++++++++- include/hw/virtio/virtio-iommu.h | 6 +++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index e83500bee9..d94a1cd8a3 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -73,3 +73,5 @@ virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain= =3D%d" virtio_iommu_put_domain(uint32_t domain_id) "Free domain=3D%d" 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_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoin= t, uint64_t addr) "FAULT reason=3D%d flags=3D%d endpoint=3D%d address =3D0x= %"PRIx64 +virtio_iommu_notify_map(const char *name, uint64_t iova, uint64_t paddr, u= int64_t map_size) "mr=3D%s iova=3D0x%"PRIx64" pa=3D0x%" PRIx64" size=3D0x%"= PRIx64 +virtio_iommu_notify_unmap(const char *name, uint64_t iova, uint64_t map_si= ze) "mr=3D%s iova=3D0x%"PRIx64" size=3D0x%"PRIx64 diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 4cee8083bc..e51344a53e 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -123,6 +123,38 @@ static gint interval_cmp(gconstpointer a, gconstpointe= r b, gpointer user_data) } } =20 +static void virtio_iommu_notify_map(IOMMUMemoryRegion *mr, hwaddr iova, + hwaddr paddr, hwaddr size) +{ + IOMMUTLBEntry entry; + + entry.target_as =3D &address_space_memory; + entry.addr_mask =3D size - 1; + + entry.iova =3D iova; + trace_virtio_iommu_notify_map(mr->parent_obj.name, iova, paddr, size); + entry.perm =3D IOMMU_RW; + entry.translated_addr =3D paddr; + + memory_region_notify_iommu(mr, 0, entry); +} + +static void virtio_iommu_notify_unmap(IOMMUMemoryRegion *mr, hwaddr iova, + hwaddr size) +{ + IOMMUTLBEntry entry; + + entry.target_as =3D &address_space_memory; + entry.addr_mask =3D size - 1; + + entry.iova =3D iova; + trace_virtio_iommu_notify_unmap(mr->parent_obj.name, iova, size); + entry.perm =3D IOMMU_NONE; + entry.translated_addr =3D 0; + + memory_region_notify_iommu(mr, 0, entry); +} + static void virtio_iommu_detach_endpoint_from_domain(VirtIOIOMMUEndpoint *= ep) { if (!ep->domain) { @@ -307,9 +339,12 @@ static int virtio_iommu_map(VirtIOIOMMU *s, uint64_t virt_start =3D le64_to_cpu(req->virt_start); uint64_t virt_end =3D le64_to_cpu(req->virt_end); uint32_t flags =3D le32_to_cpu(req->flags); + hwaddr size =3D virt_end - virt_start + 1; + VirtioIOMMUNotifierNode *node; VirtIOIOMMUDomain *domain; VirtIOIOMMUInterval *interval; VirtIOIOMMUMapping *mapping; + VirtIOIOMMUEndpoint *ep; =20 if (flags & ~VIRTIO_IOMMU_MAP_F_MASK) { return VIRTIO_IOMMU_S_INVAL; @@ -339,9 +374,37 @@ static int virtio_iommu_map(VirtIOIOMMU *s, =20 g_tree_insert(domain->mappings, interval, mapping); =20 + /* All devices in an address-space share mapping */ + QLIST_FOREACH(node, &s->notifiers_list, next) { + QLIST_FOREACH(ep, &domain->endpoint_list, next) { + if (ep->id =3D=3D node->iommu_dev->devfn) { + virtio_iommu_notify_map(&node->iommu_dev->iommu_mr, + virt_start, phys_start, size); + } + } + } + return VIRTIO_IOMMU_S_OK; } =20 +static void virtio_iommu_remove_mapping(VirtIOIOMMU *s, VirtIOIOMMUDomain = *domain, + VirtIOIOMMUInterval *interval) +{ + VirtioIOMMUNotifierNode *node; + VirtIOIOMMUEndpoint *ep; + + QLIST_FOREACH(node, &s->notifiers_list, next) { + QLIST_FOREACH(ep, &domain->endpoint_list, next) { + if (ep->id =3D=3D node->iommu_dev->devfn) { + virtio_iommu_notify_unmap(&node->iommu_dev->iommu_mr, + interval->low, + interval->high - interval->low += 1); + } + } + } + g_tree_remove(domain->mappings, (gpointer)(interval)); +} + static int virtio_iommu_unmap(VirtIOIOMMU *s, struct virtio_iommu_req_unmap *req) { @@ -368,7 +431,7 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s, uint64_t current_high =3D iter_key->high; =20 if (interval.low <=3D current_low && interval.high >=3D current_hi= gh) { - g_tree_remove(domain->mappings, iter_key); + virtio_iommu_remove_mapping(s, domain, iter_key); trace_virtio_iommu_unmap_done(domain_id, current_low, current_= high); } else { ret =3D VIRTIO_IOMMU_S_RANGE; @@ -655,6 +718,7 @@ static void virtio_iommu_device_realize(DeviceState *de= v, Error **errp) VirtIODevice *vdev =3D VIRTIO_DEVICE(dev); VirtIOIOMMU *s =3D VIRTIO_IOMMU(dev); =20 + QLIST_INIT(&s->notifiers_list); virtio_init(vdev, "virtio-iommu", VIRTIO_ID_IOMMU, sizeof(struct virtio_iommu_config)); =20 diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-io= mmu.h index 6f67f1020a..4539c8ae72 100644 --- a/include/hw/virtio/virtio-iommu.h +++ b/include/hw/virtio/virtio-iommu.h @@ -44,6 +44,11 @@ typedef struct IOMMUPciBus { IOMMUDevice *pbdev[0]; /* Parent array is sparse, so dynamically allo= c */ } IOMMUPciBus; =20 +typedef struct VirtioIOMMUNotifierNode { + IOMMUDevice *iommu_dev; + QLIST_ENTRY(VirtioIOMMUNotifierNode) next; +} VirtioIOMMUNotifierNode; + typedef struct VirtIOIOMMU { VirtIODevice parent_obj; VirtQueue *req_vq; @@ -56,6 +61,7 @@ typedef struct VirtIOIOMMU { GTree *domains; QemuMutex mutex; GTree *endpoints; + QLIST_HEAD(, VirtioIOMMUNotifierNode) notifiers_list; } VirtIOIOMMU; =20 #endif --=20 2.17.1