From nobody Wed Nov 19 04:30:25 2025 Delivered-To: importer@patchew.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=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1614252589; cv=none; d=zohomail.com; s=zohoarc; b=laPM4oB3QCHterJ8Hj3UgsWIeNTRfmwJ8UA1dPYv14JDb9wLomGevmda+zFoou2DPlsRuqiBs3VTd8mbjzgD6e2AJRSdpjLVUvywD+6w8pI9j7nqiqq3GUE6h70DZfWftFZLpqScNNr2Al5bZYhiVf5kr2FWKcNIqVVNwKkt3mc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614252589; h=Content-Type:Content-Transfer-Encoding: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=cWbL4Mb0TKZWCvorlO9kATvKH/2dIG1rF8fYiUg9+JI=; b=OVDM0KiUEKoRGYWyMH+r7aOZnxQv3yO03BUhdkdJDm7jI6xFYC4N9Wqq7djhrbPbWNFuMuWrJBA6WutuD5kIdEDkfz6cJyrvrlFkWYc9o99rYXQ+pfnBepnyWd8p4fzwckj1slKYFB0IIUNJrLyehzo4koeCj21CME/Pd840LFs= 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 1614252589881922.9399115973496; Thu, 25 Feb 2021 03:29:49 -0800 (PST) Received: from localhost ([::1]:59844 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lFEq4-00056Y-Iz for importer@patchew.org; Thu, 25 Feb 2021 06:29:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55368) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lFEJC-0000ga-1a for qemu-devel@nongnu.org; Thu, 25 Feb 2021 05:55:50 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:47698) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1lFEJA-0005tQ-3Y for qemu-devel@nongnu.org; Thu, 25 Feb 2021 05:55:49 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-28-WngWogd6Mg2dwYMH6rW9zw-1; Thu, 25 Feb 2021 05:55:43 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 729B8107ACE3; Thu, 25 Feb 2021 10:55:41 +0000 (UTC) Received: from laptop.redhat.com (ovpn-112-150.ams2.redhat.com [10.36.112.150]) by smtp.corp.redhat.com (Postfix) with ESMTP id EA5615D9D7; Thu, 25 Feb 2021 10:55:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614250547; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cWbL4Mb0TKZWCvorlO9kATvKH/2dIG1rF8fYiUg9+JI=; b=BNyq16DosMtI4etwYKmqkTy+RL23g+VipXQKBaktj4zDoUXaOlHd3UPc7XxbYZ8t9EwBkH MPMYnxkPyljAPiTuaZQy1W5vwvEw6OatduD8IifsCIBxT8SvrtI6mAnm3imD+ZgfX1PC3i D0buSFUwAabWs5Kmde2UgdSVtPoV79c= X-MC-Unique: WngWogd6Mg2dwYMH6rW9zw-1 From: Eric Auger To: eric.auger.pro@gmail.com, eric.auger@redhat.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, peter.maydell@linaro.org, mst@redhat.com, alex.williamson@redhat.com, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com Subject: [RFC v8 14/28] vfio: Set up nested stage mappings Date: Thu, 25 Feb 2021 11:52:19 +0100 Message-Id: <20210225105233.650545-15-eric.auger@redhat.com> In-Reply-To: <20210225105233.650545-1-eric.auger@redhat.com> References: <20210225105233.650545-1-eric.auger@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eric.auger@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eric.auger@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action 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: jean-philippe@linaro.org, tnowicki@marvell.com, maz@kernel.org, jiangkunkun@huawei.com, zhangfei.gao@foxmail.com, peterx@redhat.com, shameerali.kolothum.thodi@huawei.com, yuzenghui@huawei.com, zhangfei.gao@linaro.org, will@kernel.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" In nested mode, legacy vfio_iommu_map_notify cannot be used as there is no "caching" mode and we do not trap on map. On Intel, vfio_iommu_map_notify was used to DMA map the RAM through the host single stage. With nested mode, we need to setup the stage 2 and the stage 1 separately. This patch introduces a prereg_listener to setup the stage 2 mapping. The stage 1 mapping, owned by the guest, is passed to the host when the guest invalidates the stage 1 configuration, through a dedicated PCIPASIDOps callback. Guest IOTLB invalidations are cascaded downto the host through another IOMMU MR UNMAP notifier. Signed-off-by: Eric Auger --- v7 -> v8: - properly handle new IOMMUTLBEntry fields and especially propagate DOMAIN and PASID based invalidations v6 -> v7: - remove PASID based invalidation v5 -> v6: - add error_report_err() - remove the abort in case of nested stage case v4 -> v5: - use VFIO_IOMMU_SET_PASID_TABLE - use PCIPASIDOps for config notification v3 -> v4: - use iommu_inv_pasid_info for ASID invalidation v2 -> v3: - use VFIO_IOMMU_ATTACH_PASID_TABLE - new user API - handle leaf v1 -> v2: - adapt to uapi changes - pass the asid - pass IOMMU_NOTIFIER_S1_CFG when initializing the config notifier --- hw/vfio/common.c | 139 +++++++++++++++++++++++++++++++++++++++++-- hw/vfio/pci.c | 21 +++++++ hw/vfio/trace-events | 2 + 3 files changed, 157 insertions(+), 5 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index e02fb2a3ef..9bd40f5299 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -600,6 +600,73 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, v= oid **vaddr, return true; } =20 +/* Propagate a guest IOTLB invalidation to the host (nested mode) */ +static void vfio_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) +{ + VFIOGuestIOMMU *giommu =3D container_of(n, VFIOGuestIOMMU, n); + struct vfio_iommu_type1_cache_invalidate ustruct =3D {}; + VFIOContainer *container =3D giommu->container; + int ret; + + assert(iotlb->perm =3D=3D IOMMU_NONE); + + ustruct.argsz =3D sizeof(ustruct); + ustruct.flags =3D 0; + ustruct.info.argsz =3D sizeof(struct iommu_cache_invalidate_info); + ustruct.info.version =3D IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + ustruct.info.cache =3D IOMMU_CACHE_INV_TYPE_IOTLB; + + switch (iotlb->granularity) { + case IOMMU_INV_GRAN_DOMAIN: + ustruct.info.granularity =3D IOMMU_INV_GRANU_DOMAIN; + break; + case IOMMU_INV_GRAN_PASID: + { + struct iommu_inv_pasid_info *pasid_info; + int archid =3D -1; + + pasid_info =3D &ustruct.info.granu.pasid_info; + ustruct.info.granularity =3D IOMMU_INV_GRANU_PASID; + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + pasid_info->flags |=3D IOMMU_INV_ADDR_FLAGS_ARCHID; + archid =3D iotlb->arch_id; + } + pasid_info->archid =3D archid; + trace_vfio_iommu_asid_inv_iotlb(archid); + break; + } + case IOMMU_INV_GRAN_ADDR: + { + hwaddr start =3D iotlb->iova + giommu->iommu_offset; + struct iommu_inv_addr_info *addr_info; + size_t size =3D iotlb->addr_mask + 1; + int archid =3D -1; + + addr_info =3D &ustruct.info.granu.addr_info; + ustruct.info.granularity =3D IOMMU_INV_GRANU_ADDR; + if (iotlb->leaf) { + addr_info->flags |=3D IOMMU_INV_ADDR_FLAGS_LEAF; + } + if (iotlb->flags & IOMMU_INV_FLAGS_ARCHID) { + addr_info->flags |=3D IOMMU_INV_ADDR_FLAGS_ARCHID; + archid =3D iotlb->arch_id; + } + addr_info->archid =3D archid; + addr_info->addr =3D start; + addr_info->granule_size =3D size; + addr_info->nb_granules =3D 1; + trace_vfio_iommu_addr_inv_iotlb(archid, start, size, + 1, iotlb->leaf); + break; + } + } + + ret =3D ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, &ustruct); + if (ret) { + error_report("%p: failed to invalidate CACHE (%d)", container, ret= ); + } +} + static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) { VFIOGuestIOMMU *giommu =3D container_of(n, VFIOGuestIOMMU, n); @@ -781,6 +848,35 @@ static void vfio_dma_unmap_ram_section(VFIOContainer *= container, } } =20 +static void vfio_prereg_listener_region_add(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container =3D + container_of(listener, VFIOContainer, prereg_listener); + Error *err =3D NULL; + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_map_ram_section(container, section, &err); + if (err) { + error_report_err(err); + } +} +static void vfio_prereg_listener_region_del(MemoryListener *listener, + MemoryRegionSection *section) +{ + VFIOContainer *container =3D + container_of(listener, VFIOContainer, prereg_listener); + + if (!memory_region_is_ram(section->mr)) { + return; + } + + vfio_dma_unmap_ram_section(container, section); +} + static void vfio_listener_region_add(MemoryListener *listener, MemoryRegionSection *section) { @@ -883,9 +979,10 @@ static void vfio_listener_region_add(MemoryListener *l= istener, memory_region_ref(section->mr); =20 if (memory_region_is_iommu(section->mr)) { + IOMMUNotify notify; VFIOGuestIOMMU *giommu; IOMMUMemoryRegion *iommu_mr =3D IOMMU_MEMORY_REGION(section->mr); - int iommu_idx; + int iommu_idx, flags; =20 trace_vfio_listener_region_add_iommu(iova, end); /* @@ -904,8 +1001,18 @@ static void vfio_listener_region_add(MemoryListener *= listener, llend =3D int128_sub(llend, int128_one()); iommu_idx =3D memory_region_iommu_attrs_to_index(iommu_mr, MEMTXATTRS_UNSPECIF= IED); - iommu_notifier_init(&giommu->n, vfio_iommu_map_notify, - IOMMU_NOTIFIER_IOTLB_EVENTS, + + if (container->iommu_type =3D=3D VFIO_TYPE1_NESTING_IOMMU) { + /* IOTLB unmap notifier to propagate guest IOTLB invalidations= */ + flags =3D IOMMU_NOTIFIER_UNMAP; + notify =3D vfio_iommu_unmap_notify; + } else { + /* MAP/UNMAP IOTLB notifier */ + flags =3D IOMMU_NOTIFIER_IOTLB_EVENTS; + notify =3D vfio_iommu_map_notify; + } + + iommu_notifier_init(&giommu->n, notify, flags, section->offset_within_region, int128_get64(llend), iommu_idx); @@ -925,7 +1032,9 @@ static void vfio_listener_region_add(MemoryListener *l= istener, goto fail; } QLIST_INSERT_HEAD(&container->giommu_list, giommu, giommu_next); - memory_region_iommu_replay(giommu->iommu, &giommu->n); + if (flags & IOMMU_NOTIFIER_MAP) { + memory_region_iommu_replay(giommu->iommu, &giommu->n); + } =20 return; } @@ -1172,10 +1281,16 @@ static const MemoryListener vfio_memory_listener = =3D { .log_sync =3D vfio_listerner_log_sync, }; =20 +static MemoryListener vfio_memory_prereg_listener =3D { + .region_add =3D vfio_prereg_listener_region_add, + .region_del =3D vfio_prereg_listener_region_del, +}; + static void vfio_listener_release(VFIOContainer *container) { memory_listener_unregister(&container->listener); - if (container->iommu_type =3D=3D VFIO_SPAPR_TCE_v2_IOMMU) { + if (container->iommu_type =3D=3D VFIO_SPAPR_TCE_v2_IOMMU || + container->iommu_type =3D=3D VFIO_TYPE1_NESTING_IOMMU) { memory_listener_unregister(&container->prereg_listener); } } @@ -1825,6 +1940,20 @@ static int vfio_connect_container(VFIOGroup *group, = AddressSpace *as, vfio_get_iommu_info_migration(container, info); } g_free(info); + + if (container->iommu_type =3D=3D VFIO_TYPE1_NESTING_IOMMU) { + container->prereg_listener =3D vfio_memory_prereg_listener; + memory_listener_register(&container->prereg_listener, + &address_space_memory); + if (container->error) { + memory_listener_unregister(&container->prereg_listener); + ret =3D -1; + error_propagate_prepend(errp, container->error, + "RAM memory listener initialization fa= iled " + "for container"); + goto free_container_exit; + } + } break; } case VFIO_SPAPR_TCE_v2_IOMMU: diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index f74be78209..b28e58db34 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2759,6 +2759,25 @@ static void vfio_unregister_req_notifier(VFIOPCIDevi= ce *vdev) vdev->req_enabled =3D false; } =20 +static int vfio_iommu_set_pasid_table(PCIBus *bus, int32_t devfn, + IOMMUConfig *config) +{ + PCIDevice *pdev =3D bus->devices[devfn]; + VFIOPCIDevice *vdev =3D DO_UPCAST(VFIOPCIDevice, pdev, pdev); + VFIOContainer *container =3D vdev->vbasedev.group->container; + struct vfio_iommu_type1_set_pasid_table info; + + info.argsz =3D sizeof(info); + info.flags =3D VFIO_PASID_TABLE_FLAG_SET; + memcpy(&info.config, &config->pasid_cfg, sizeof(config->pasid_cfg)); + + return ioctl(container->fd, VFIO_IOMMU_SET_PASID_TABLE, &info); +} + +static PCIPASIDOps vfio_pci_pasid_ops =3D { + .set_pasid_table =3D vfio_iommu_set_pasid_table, +}; + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev =3D VFIO_PCI(pdev); @@ -3070,6 +3089,8 @@ static void vfio_realize(PCIDevice *pdev, Error **err= p) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); =20 + pci_setup_pasid_ops(pdev, &vfio_pci_pasid_ops); + return; =20 out_deregister: diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 304c1d8b19..35fd74833c 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -120,6 +120,8 @@ vfio_region_sparse_mmap_header(const char *name, int in= dex, int nr_areas) "Devic vfio_region_sparse_mmap_entry(int i, unsigned long start, unsigned long en= d) "sparse entry %d [0x%lx - 0x%lx]" vfio_get_dev_region(const char *name, int index, uint32_t type, uint32_t s= ubtype) "%s index %d, %08x/%0x8" vfio_dma_unmap_overflow_workaround(void) "" +vfio_iommu_addr_inv_iotlb(int asid, uint64_t addr, uint64_t size, uint64_t= nb_granules, bool leaf) "nested IOTLB invalidate asid=3D%d, addr=3D0x%"PRI= x64" granule_size=3D0x%"PRIx64" nb_granules=3D0x%"PRIx64" leaf=3D%d" +vfio_iommu_asid_inv_iotlb(int asid) "nested IOTLB invalidate asid=3D%d" =20 # platform.c vfio_platform_base_device_init(char *name, int groupid) "%s belongs to gro= up #%d" --=20 2.26.2