From nobody Tue Nov 26 20:29:14 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; 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=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1705315308; cv=none; d=zohomail.com; s=zohoarc; b=Q9FNwD3zNeD3f+6pOFq9it0eKO3DpaV5d2X/MJHhK5tnQ2F++fG8RbzUvsjwSWMlQ89Yko6niQBWJ0qYj7Oero8goycYMk/VTlMn5nIYG9mB8qlpS1HqO17n+JcGqPB1I043vacAh79CasQyjUBzJND2v6/cElMpdxvYfY0EBFs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1705315308; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=59ZvWAhHdTNDzwbxh6mTKHQTCQfAYuceEIYwTI/u6KE=; b=H5LMZ0pZYB6Ig35tEfKcLaQLVBYj8hoAcJBbmAT61E7LlL7QGa7mE55TL6QKe8dicKUXiFN2nD42RSjHjeeyrmjpGC+ZQJ3yuI1s8dhcdMwKP+tpNOxH8nh+bOlxn4Laht/BKFKbLDMU1xjAR12BT0NZ6xHpGoB1eEqYnyj+VQM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1705315308915769.6744142328904; Mon, 15 Jan 2024 02:41:48 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rPKOU-0001ym-HD; Mon, 15 Jan 2024 05:40:38 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rPKON-0001mQ-K9 for qemu-devel@nongnu.org; Mon, 15 Jan 2024 05:40:33 -0500 Received: from mgamail.intel.com ([192.198.163.8]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rPKOJ-0003Bt-HW for qemu-devel@nongnu.org; Mon, 15 Jan 2024 05:40:30 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jan 2024 02:40:24 -0800 Received: from spr-s2600bt.bj.intel.com ([10.240.192.124]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jan 2024 02:40:19 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705315227; x=1736851227; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Pq4tna4liAgU6SEEAHI54jtuLdPFcBw1b4/wcNEBEC4=; b=cc7N4IIykWCyVkj5bpIZosTQslTGUhbsMfKBs4ZskCWmBiBmaCtTDPu6 dVHjKk0fJTVDrm2A8QtQpwm/PANdgplctu7K/O4JQ12e0C4ITe/wFS70v lGfpoo29yMJ8iWpsOKqZlz1bA/buch2aCoHhS6zkfTIJZikhBCtJUYNws uHW06IN5lAF6d8abFG7cK7Pbt2c3H/1XL6ifHn9jiO3Ko3wbnFOQZeMbI sGG9pfAshEacAW84XJPbajyVuqWaMHzQFeuZxql8o76hypz775n64MO17 6wSf9A+5Hwt84Glb7FRWFO2bJv48R5GrylStyiD/tWSsFt4Fd6Xg4sBhx g==; X-IronPort-AV: E=McAfee;i="6600,9927,10953"; a="13067726" X-IronPort-AV: E=Sophos;i="6.04,196,1695711600"; d="scan'208";a="13067726" X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10953"; a="874065360" X-IronPort-AV: E=Sophos;i="6.04,196,1695711600"; d="scan'208";a="874065360" From: Zhenzhong Duan To: qemu-devel@nongnu.org Cc: alex.williamson@redhat.com, clg@redhat.com, eric.auger@redhat.com, peterx@redhat.com, jasowang@redhat.com, mst@redhat.com, jgg@nvidia.com, nicolinc@nvidia.com, joao.m.martins@oracle.com, kevin.tian@intel.com, yi.l.liu@intel.com, yi.y.sun@intel.com, chao.p.peng@intel.com, Zhenzhong Duan , Marcel Apfelbaum , Paolo Bonzini , Richard Henderson , Eduardo Habkost Subject: [PATCH rfcv1 11/23] intel_iommu: ERRATA_772415 workaround Date: Mon, 15 Jan 2024 18:37:23 +0800 Message-Id: <20240115103735.132209-12-zhenzhong.duan@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240115103735.132209-1-zhenzhong.duan@intel.com> References: <20240115103735.132209-1-zhenzhong.duan@intel.com> MIME-Version: 1.0 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=192.198.163.8; envelope-from=zhenzhong.duan@intel.com; helo=mgamail.intel.com X-Spam_score_int: -48 X-Spam_score: -4.9 X-Spam_bar: ---- X-Spam_report: (-4.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.758, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1705315309500100001 Content-Type: text/plain; charset="utf-8" On a system influenced by ERRATA_772415, IOMMU_HW_INFO_VTD_ERRATA_772415_SP= R17 is repored by IOMMU_DEVICE_GET_HW_INFO. Due to this errata, even the readon= ly range mapped on stage-2 page table could still be written. Reference from 4th Gen Intel Xeon Processor Scalable Family Specification Update, Errata Details, SPR17. [0] https://edc.intel.com/content/www/us/en/design/products-and-solutions/p= rocessors-and-chipsets/eagle-stream/sapphire-rapids-specification-update We utilize the new added IOMMUFD container/ioas/hwpt management framework in VTD. Add a check to create new VTDIOASContainer to hold RW-only mappings, then this VTDIOASContainer can be used as backend for device with ERRATA_772415. See below diagram for details: IntelIOMMUState | V .------------------. .------------------. .-------------------. | VTDIOASContainer |--->| VTDIOASContainer |--->| VTDIOASContainer |--= >... | (iommufd0,RW&RO) | | (iommufd1,RW&RO) | | (iommufd0,RW only)| .------------------. .------------------. .-------------------. | | | | .-->... | V V .-------------------. .-------------------. .------------= ---. | VTDS2Hwpt(CC) |--->| VTDS2Hwpt(non-CC) |-->... | VTDS2Hwpt(C= C) |-->... .-------------------. .-------------------. .------------= ---. | | | | | | | | .-----------. .-----------. .------------. .------------. | IOMMUFD | | IOMMUFD | | IOMMUFD | | IOMMUFD | | Device(CC)| | Device(CC)| | Device | | Device(CC) | | (iommufd0)| | (iommufd0)| | (non-CC) | | (errata) | | | | | | (iommufd0) | | (iommufd0) | .-----------. .-----------. .------------. .------------. Suggested-by: Yi Liu Signed-off-by: Zhenzhong Duan --- include/hw/i386/intel_iommu.h | 2 ++ hw/i386/intel_iommu.c | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index d3122cf699..72702e10a2 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -108,6 +108,7 @@ struct pasid_key { struct VTDIOASContainer { IOMMUFDBackend *iommufd; uint32_t ioas_id; + uint32_t errata; MemoryListener listener; QLIST_HEAD(, VTDS2Hwpt) s2_hwpt_list; QLIST_ENTRY(VTDIOASContainer) next; @@ -200,6 +201,7 @@ struct VTDIOMMUFDDevice { PCIBus *bus; uint8_t devfn; IOMMUFDDevice *idev; + uint32_t errata; IntelIOMMUState *iommu_state; QLIST_ENTRY(VTDIOMMUFDDevice) next; }; diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index df93fcacd8..8f9a59ae6f 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -2121,7 +2121,8 @@ static void vtd_context_global_invalidate(IntelIOMMUS= tate *s) vtd_iommu_replay_all(s); } =20 -static bool iommufd_listener_skipped_section(MemoryRegionSection *section) +static bool iommufd_listener_skipped_section(VTDIOASContainer *container, + MemoryRegionSection *section) { return !memory_region_is_ram(section->mr) || memory_region_is_protected(section->mr) || @@ -2131,7 +2132,8 @@ static bool iommufd_listener_skipped_section(MemoryRe= gionSection *section) * are never accessed by the CPU and beyond the address width of * some IOMMU hardware. TODO: VFIO should tell us the IOMMU wi= dth. */ - section->offset_within_address_space & (1ULL << 63); + section->offset_within_address_space & (1ULL << 63) || + (container->errata && section->readonly); } =20 static void iommufd_listener_region_add_s2domain(MemoryListener *listener, @@ -2147,7 +2149,7 @@ static void iommufd_listener_region_add_s2domain(Memo= ryListener *listener, Error *err =3D NULL; int ret; =20 - if (iommufd_listener_skipped_section(section)) { + if (iommufd_listener_skipped_section(container, section)) { return; } iova =3D REAL_HOST_PAGE_ALIGN(section->offset_within_address_space); @@ -2198,7 +2200,7 @@ static void iommufd_listener_region_del_s2domain(Memo= ryListener *listener, Int128 llend, llsize; int ret; =20 - if (iommufd_listener_skipped_section(section)) { + if (iommufd_listener_skipped_section(container, section)) { return; } iova =3D REAL_HOST_PAGE_ALIGN(section->offset_within_address_space); @@ -2468,7 +2470,8 @@ static int vtd_device_attach_iommufd(VTDIOMMUFDDevice= *vtd_idev, =20 /* try to attach to an existing container in this space */ QLIST_FOREACH(container, &s->containers, next) { - if (container->iommufd !=3D iommufd) { + if (container->iommufd !=3D iommufd || + container->errata !=3D vtd_idev->errata) { continue; } =20 @@ -2495,6 +2498,7 @@ static int vtd_device_attach_iommufd(VTDIOMMUFDDevice= *vtd_idev, container =3D g_malloc0(sizeof(*container)); container->iommufd =3D iommufd; container->ioas_id =3D ioas_id; + container->errata =3D vtd_idev->errata; QLIST_INIT(&container->s2_hwpt_list); =20 if (vtd_device_attach_container(vtd_idev, container, @@ -5002,10 +5006,11 @@ static bool vtd_sync_hw_info(IntelIOMMUState *s, st= ruct iommu_hw_info_vtd *vtd, * could bind guest page table to host. */ static bool vtd_check_idev(IntelIOMMUState *s, IOMMUFDDevice *idev, - Error **errp) + uint32_t *flags, Error **errp) { struct iommu_hw_info_vtd vtd; enum iommu_hw_info_type type =3D IOMMU_HW_INFO_TYPE_INTEL_VTD; + bool passed; =20 if (iommufd_device_get_info(idev, &type, sizeof(vtd), &vtd)) { error_setg(errp, "Failed to get IOMMU capability!!!"); @@ -5017,7 +5022,11 @@ static bool vtd_check_idev(IntelIOMMUState *s, IOMMU= FDDevice *idev, return false; } =20 - return vtd_sync_hw_info(s, &vtd, errp); + passed =3D vtd_sync_hw_info(s, &vtd, errp); + if (passed) { + *flags =3D vtd.flags; + } + return passed; } =20 static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t dev= fn, @@ -5030,6 +5039,7 @@ static int vtd_dev_set_iommu_device(PCIBus *bus, void= *opaque, int32_t devfn, .devfn =3D devfn, }; struct vtd_as_key *new_key; + uint32_t flags; =20 assert(0 <=3D devfn && devfn < PCI_DEVFN_MAX); =20 @@ -5042,7 +5052,7 @@ static int vtd_dev_set_iommu_device(PCIBus *bus, void= *opaque, int32_t devfn, return -1; } =20 - if (!vtd_check_idev(s, idev, errp)) { + if (!vtd_check_idev(s, idev, &flags, errp)) { return -1; } =20 @@ -5064,6 +5074,7 @@ static int vtd_dev_set_iommu_device(PCIBus *bus, void= *opaque, int32_t devfn, vtd_idev->devfn =3D (uint8_t)devfn; vtd_idev->iommu_state =3D s; vtd_idev->idev =3D idev; + vtd_idev->errata =3D flags & IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17; QLIST_INSERT_HEAD(&s->vtd_idev_list, vtd_idev, next); =20 g_hash_table_insert(s->vtd_iommufd_dev, new_key, vtd_idev); --=20 2.34.1