From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542037; cv=none; d=zohomail.com; s=zohoarc; b=aX+ENgvnhVAA11ivYSGk9+mU4bBie2OwKy5Xd2tam3ESSnxKCjC4GlS7S9HbhJJjO2MuD5nVWvTqRM66zbOF+zjKiB8QIxdwRhgkb8m0tJRA2Jd0ClE08/viI+28aDsRy6cXrGliGjYsG0bbXEoOnQ4bgWoBBVmFGgTqLlAGXkA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542037; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=biXz8h+R8eG4b7+bNGXMlD9uj5alGlyfk+LwDnX58D0=; b=evOjSN9HVyxGwWDcp8adIJlEF05uZeDVMtKb5HjQzR5BpfE8yCs4vzfrqMshhDLQgzKFJfPl+rv1qhaBcjLP1Bafm5ukYF+mw6qx4GCkzljqe8OMBC0Hxehuba88+V5igoD/A89CKBpm1OfQYM7UvwN3kOUQYSPFSd29cVkSoP8= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542037497403.06956672091917; Sun, 29 Mar 2020 21:20:37 -0700 (PDT) Received: from localhost ([::1]:44618 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlud-0002d1-Ka for importer@patchew.org; Mon, 30 Mar 2020 00:20:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37755) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltR-0000qV-Ez for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltP-0007oj-QC for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:20 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltP-0007lr-Hg for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:19 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: bsIjHVh0p96bL+7MrZ08bag99S188cx+4IAQE1Z/YIs07O6Ut6ozzPoEVpkltdSgGXPJvDUPRZ 9iQruKWMqb7A== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: iZiJHfqz2miaNSap2JGHi9aVy40ziL2e/lZmEq/qUR4zcoSWJ1+HD4cjyCDGZk635XwSM9EQl2 r7dheeCbMp3A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632011" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 01/22] scripts/update-linux-headers: Import iommu.h Date: Sun, 29 Mar 2020 21:24:40 -0700 Message-Id: <1585542301-84087-2-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, Cornelia Huck , eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Eric Auger Update the script to import the new iommu.h uapi header. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Michael S. Tsirkin Cc: Cornelia Huck Cc: Paolo Bonzini Acked-by: Cornelia Huck Signed-off-by: Eric Auger --- scripts/update-linux-headers.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/update-linux-headers.sh b/scripts/update-linux-headers= .sh index 29c27f4..5b64ee3 100755 --- a/scripts/update-linux-headers.sh +++ b/scripts/update-linux-headers.sh @@ -141,7 +141,7 @@ done =20 rm -rf "$output/linux-headers/linux" mkdir -p "$output/linux-headers/linux" -for header in kvm.h vfio.h vfio_ccw.h vhost.h \ +for header in kvm.h vfio.h vfio_ccw.h vhost.h iommu.h \ psci.h psp-sev.h userfaultfd.h mman.h; do cp "$tmpdir/include/linux/$header" "$output/linux-headers/linux" done --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542609; cv=none; d=zohomail.com; s=zohoarc; b=UgbJNP0lneCebqcs7baXVVzBS/yoBECm8SSfWn2PZQKRtBi+o+ri9fn54xMdjl53RcJKuRoEWC2/tV0rxbkFxM5VTsff7TuXweuwOnnTumE1tlkl0Bwk55A/9m8qeLnGLOczPrF5LDlpj2NM4lbhHqdcvqyY8IyenV0vnx4mxfw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542609; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=OQ1cnHNXL91Pb4F8GLYb27r8U8LZEX/bynQDF5E/OPE=; b=aXD85rQ4lkR/6PTQDEulPGKGOigKabZ0ETU7WUtbW5rvH2Lp8L8u6X0jgC4MQMf06jPmy+FnG7VpCgZU3gCYjVNYju9WfwhwqE+UElwuw2BnxyVVGiczNgEnctTGUnURh6odKNGqxwX8IcBet+3PTUq54fa3gMEPNuYKqK4KAsM= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542609222615.7766822172766; Sun, 29 Mar 2020 21:30:09 -0700 (PDT) Received: from localhost ([::1]:44782 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm3r-0002pv-UF for importer@patchew.org; Mon, 30 Mar 2020 00:30:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37840) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltT-0000qv-KN for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltQ-0007q2-QV for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:23 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltQ-0007lr-HD for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:20 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: tKKebXCoLk45sE/deLVlkzBhx4PSYS1x2NCy6uOGm+Sc5m+hMe0i3YDt2+eXlGxSPL8AbaG++f 8jxyPqUh7nCw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: XhIbVAuyehZf2tDaIGtVSWyq1iZR/by+EMA2fXitUN3qiHDFioctYMLLYczTxTPTt8calTIBDt Yg2GChnfACmg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632014" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 02/22] header file update VFIO/IOMMU vSVA APIs Date: Sun, 29 Mar 2020 21:24:41 -0700 Message-Id: <1585542301-84087-3-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, Cornelia Huck , eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The kernel uapi/linux/iommu.h header file includes the extensions for vSVA support. e.g. bind gpasid, iommu fault report related user structures and etc. Note: this should be replaced with a full header files update when the vSVA uPAPI is stable. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Michael S. Tsirkin Cc: Cornelia Huck Cc: Paolo Bonzini Signed-off-by: Liu Yi L --- linux-headers/linux/iommu.h | 378 ++++++++++++++++++++++++++++++++++++++++= ++++ linux-headers/linux/vfio.h | 127 +++++++++++++++ 2 files changed, 505 insertions(+) create mode 100644 linux-headers/linux/iommu.h diff --git a/linux-headers/linux/iommu.h b/linux-headers/linux/iommu.h new file mode 100644 index 0000000..9025496 --- /dev/null +++ b/linux-headers/linux/iommu.h @@ -0,0 +1,378 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * IOMMU user API definitions + */ + +#ifndef _IOMMU_H +#define _IOMMU_H + +#include + +/** + * Current version of the IOMMU user API. This is intended for query + * between user and kernel to determine compatible data structures. + * + * UAPI version can be bumped up with the following rules: + * 1. All data structures passed between user and kernel space share + * the same version number. i.e. any extension to any structure + * results in version number increment. + * + * 2. Data structures are open to extension but closed to modification. + * Extension should leverage the padding bytes first where a new + * flag bit is required to indicate the validity of each new member. + * The above rule for padding bytes also applies to adding new union + * members. + * After padding bytes are exhausted, new fields must be added at the + * end of each data structure with 64bit alignment. Flag bits can be + * added without size change but existing ones cannot be altered. + * + * 3. Versions are backward compatible. + * + * 4. Version to size lookup is supported by kernel internal API for each + * API function type. @version is mandatory for new data structures + * and must be at the beginning with type of __u32. + */ +#define IOMMU_UAPI_VERSION 1 +static __inline__ int iommu_get_uapi_version(void) +{ + return IOMMU_UAPI_VERSION; +} + +/* + * Supported UAPI features that can be reported to user space. + * These types represent the capability available in the kernel. + * + * REVISIT: UAPI version also implies the capabilities. Should we + * report them explicitly? + */ +enum IOMMU_UAPI_DATA_TYPES { + IOMMU_UAPI_BIND_GPASID, + IOMMU_UAPI_CACHE_INVAL, + IOMMU_UAPI_PAGE_RESP, + NR_IOMMU_UAPI_TYPE, +}; + +#define IOMMU_UAPI_CAP_MASK ((1 << IOMMU_UAPI_BIND_GPASID) | \ + (1 << IOMMU_UAPI_CACHE_INVAL) | \ + (1 << IOMMU_UAPI_PAGE_RESP)) + +#define IOMMU_FAULT_PERM_READ (1 << 0) /* read */ +#define IOMMU_FAULT_PERM_WRITE (1 << 1) /* write */ +#define IOMMU_FAULT_PERM_EXEC (1 << 2) /* exec */ +#define IOMMU_FAULT_PERM_PRIV (1 << 3) /* privileged */ + +/* Generic fault types, can be expanded IRQ remapping fault */ +enum iommu_fault_type { + IOMMU_FAULT_DMA_UNRECOV =3D 1, /* unrecoverable fault */ + IOMMU_FAULT_PAGE_REQ, /* page request fault */ +}; + +enum iommu_fault_reason { + IOMMU_FAULT_REASON_UNKNOWN =3D 0, + + /* Could not access the PASID table (fetch caused external abort) */ + IOMMU_FAULT_REASON_PASID_FETCH, + + /* PASID entry is invalid or has configuration errors */ + IOMMU_FAULT_REASON_BAD_PASID_ENTRY, + + /* + * PASID is out of range (e.g. exceeds the maximum PASID + * supported by the IOMMU) or disabled. + */ + IOMMU_FAULT_REASON_PASID_INVALID, + + /* + * An external abort occurred fetching (or updating) a translation + * table descriptor + */ + IOMMU_FAULT_REASON_WALK_EABT, + + /* + * Could not access the page table entry (Bad address), + * actual translation fault + */ + IOMMU_FAULT_REASON_PTE_FETCH, + + /* Protection flag check failed */ + IOMMU_FAULT_REASON_PERMISSION, + + /* access flag check failed */ + IOMMU_FAULT_REASON_ACCESS, + + /* Output address of a translation stage caused Address Size fault */ + IOMMU_FAULT_REASON_OOR_ADDRESS, +}; + +/** + * struct iommu_fault_unrecoverable - Unrecoverable fault data + * @reason: reason of the fault, from &enum iommu_fault_reason + * @flags: parameters of this fault (IOMMU_FAULT_UNRECOV_* values) + * @pasid: Process Address Space ID + * @perm: requested permission access using by the incoming transaction + * (IOMMU_FAULT_PERM_* values) + * @addr: offending page address + * @fetch_addr: address that caused a fetch abort, if any + */ +struct iommu_fault_unrecoverable { + __u32 reason; +#define IOMMU_FAULT_UNRECOV_PASID_VALID (1 << 0) +#define IOMMU_FAULT_UNRECOV_ADDR_VALID (1 << 1) +#define IOMMU_FAULT_UNRECOV_FETCH_ADDR_VALID (1 << 2) + __u32 flags; + __u32 pasid; + __u32 perm; + __u64 addr; + __u64 fetch_addr; +}; + +/** + * struct iommu_fault_page_request - Page Request data + * @flags: encodes whether the corresponding fields are valid and whether = this + * is the last page in group (IOMMU_FAULT_PAGE_REQUEST_* values) + * @pasid: Process Address Space ID + * @grpid: Page Request Group Index + * @perm: requested page permissions (IOMMU_FAULT_PERM_* values) + * @addr: page address + * @private_data: device-specific private information + */ +struct iommu_fault_page_request { +#define IOMMU_FAULT_PAGE_REQUEST_PASID_VALID (1 << 0) +#define IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE (1 << 1) +#define IOMMU_FAULT_PAGE_REQUEST_PRIV_DATA (1 << 2) + __u32 flags; + __u32 pasid; + __u32 grpid; + __u32 perm; + __u64 addr; + __u64 private_data[2]; +}; + +/** + * struct iommu_fault - Generic fault data + * @type: fault type from &enum iommu_fault_type + * @padding: reserved for future use (should be zero) + * @event: fault event, when @type is %IOMMU_FAULT_DMA_UNRECOV + * @prm: Page Request message, when @type is %IOMMU_FAULT_PAGE_REQ + * @padding2: sets the fault size to allow for future extensions + */ +struct iommu_fault { + __u32 type; + __u32 padding; + union { + struct iommu_fault_unrecoverable event; + struct iommu_fault_page_request prm; + __u8 padding2[56]; + }; +}; + +/** + * enum iommu_page_response_code - Return status of fault handlers + * @IOMMU_PAGE_RESP_SUCCESS: Fault has been handled and the page tables + * populated, retry the access. This is "Success" in PCI PRI. + * @IOMMU_PAGE_RESP_FAILURE: General error. Drop all subsequent faults from + * this device if possible. This is "Response Failure" in PCI PRI. + * @IOMMU_PAGE_RESP_INVALID: Could not handle this fault, don't retry the + * access. This is "Invalid Request" in PCI PRI. + */ +enum iommu_page_response_code { + IOMMU_PAGE_RESP_SUCCESS =3D 0, + IOMMU_PAGE_RESP_INVALID, + IOMMU_PAGE_RESP_FAILURE, +}; + +/** + * struct iommu_page_response - Generic page response information + * @version: IOMMU_UAPI_VERSION + * @flags: encodes whether the corresponding fields are valid + * (IOMMU_FAULT_PAGE_RESPONSE_* values) + * @pasid: Process Address Space ID + * @grpid: Page Request Group Index + * @code: response code from &enum iommu_page_response_code + */ +struct iommu_page_response { + __u32 version; +#define IOMMU_PAGE_RESP_PASID_VALID (1 << 0) + __u32 flags; + __u32 pasid; + __u32 grpid; + __u32 code; +}; + +/* defines the granularity of the invalidation */ +enum iommu_inv_granularity { + IOMMU_INV_GRANU_DOMAIN, /* domain-selective invalidation */ + IOMMU_INV_GRANU_PASID, /* PASID-selective invalidation */ + IOMMU_INV_GRANU_ADDR, /* page-selective invalidation */ + IOMMU_INV_GRANU_NR, /* number of invalidation granularities */ +}; + +/** + * struct iommu_inv_addr_info - Address Selective Invalidation Structure + * + * @flags: indicates the granularity of the address-selective invalidation + * - If the PASID bit is set, the @pasid field is populated and the invali= dation + * relates to cache entries tagged with this PASID and matching the addr= ess + * range. + * - If ARCHID bit is set, @archid is populated and the invalidation relat= es + * to cache entries tagged with this architecture specific ID and matchi= ng + * the address range. + * - Both PASID and ARCHID can be set as they may tag different caches. + * - If neither PASID or ARCHID is set, global addr invalidation applies. + * - The LEAF flag indicates whether only the leaf PTE caching needs to be + * invalidated and other paging structure caches can be preserved. + * @pasid: process address space ID + * @archid: architecture-specific ID + * @addr: first stage/level input address + * @granule_size: page/block size of the mapping in bytes + * @nb_granules: number of contiguous granules to be invalidated + */ +struct iommu_inv_addr_info { +#define IOMMU_INV_ADDR_FLAGS_PASID (1 << 0) +#define IOMMU_INV_ADDR_FLAGS_ARCHID (1 << 1) +#define IOMMU_INV_ADDR_FLAGS_LEAF (1 << 2) + __u32 flags; + __u32 archid; + __u64 pasid; + __u64 addr; + __u64 granule_size; + __u64 nb_granules; +}; + +/** + * struct iommu_inv_pasid_info - PASID Selective Invalidation Structure + * + * @flags: indicates the granularity of the PASID-selective invalidation + * - If the PASID bit is set, the @pasid field is populated and the invali= dation + * relates to cache entries tagged with this PASID and matching the addr= ess + * range. + * - If the ARCHID bit is set, the @archid is populated and the invalidati= on + * relates to cache entries tagged with this architecture specific ID and + * matching the address range. + * - Both PASID and ARCHID can be set as they may tag different caches. + * - At least one of PASID or ARCHID must be set. + * @pasid: process address space ID + * @archid: architecture-specific ID + */ +struct iommu_inv_pasid_info { +#define IOMMU_INV_PASID_FLAGS_PASID (1 << 0) +#define IOMMU_INV_PASID_FLAGS_ARCHID (1 << 1) + __u32 flags; + __u32 archid; + __u64 pasid; +}; + +/** + * struct iommu_cache_invalidate_info - First level/stage invalidation + * information + * @version: IOMMU_UAPI_VERSION + * @cache: bitfield that allows to select which caches to invalidate + * @granularity: defines the lowest granularity used for the invalidation: + * domain > PASID > addr + * @padding: reserved for future use (should be zero) + * @pasid_info: invalidation data when @granularity is %IOMMU_INV_GRANU_PA= SID + * @addr_info: invalidation data when @granularity is %IOMMU_INV_GRANU_ADDR + * + * Not all the combinations of cache/granularity are valid: + * + * +--------------+---------------+---------------+---------------+ + * | type / | DEV_IOTLB | IOTLB | PASID | + * | granularity | | | cache | + * +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+ + * | DOMAIN | N/A | Y | Y | + * +--------------+---------------+---------------+---------------+ + * | PASID | Y | Y | Y | + * +--------------+---------------+---------------+---------------+ + * | ADDR | Y | Y | N/A | + * +--------------+---------------+---------------+---------------+ + * + * Invalidations by %IOMMU_INV_GRANU_DOMAIN don't take any argument other = than + * @version and @cache. + * + * If multiple cache types are invalidated simultaneously, they all + * must support the used granularity. + */ +struct iommu_cache_invalidate_info { + __u32 version; +/* IOMMU paging structure cache */ +#define IOMMU_CACHE_INV_TYPE_IOTLB (1 << 0) /* IOMMU IOTLB */ +#define IOMMU_CACHE_INV_TYPE_DEV_IOTLB (1 << 1) /* Device IOTLB */ +#define IOMMU_CACHE_INV_TYPE_PASID (1 << 2) /* PASID cache */ +#define IOMMU_CACHE_INV_TYPE_NR (3) + __u8 cache; + __u8 granularity; + __u8 padding[2]; + union { + struct iommu_inv_pasid_info pasid_info; + struct iommu_inv_addr_info addr_info; + }; +}; + +/** + * struct iommu_gpasid_bind_data_vtd - Intel VT-d specific data on device = and guest + * SVA binding. + * + * @flags: VT-d PASID table entry attributes + * @pat: Page attribute table data to compute effective memory type + * @emt: Extended memory type + * + * Only guest vIOMMU selectable and effective options are passed down to + * the host IOMMU. + */ +struct iommu_gpasid_bind_data_vtd { +#define IOMMU_SVA_VTD_GPASID_SRE (1 << 0) /* supervisor request */ +#define IOMMU_SVA_VTD_GPASID_EAFE (1 << 1) /* extended access enable */ +#define IOMMU_SVA_VTD_GPASID_PCD (1 << 2) /* page-level cache disable */ +#define IOMMU_SVA_VTD_GPASID_PWT (1 << 3) /* page-level write through */ +#define IOMMU_SVA_VTD_GPASID_EMTE (1 << 4) /* extended mem type enable */ +#define IOMMU_SVA_VTD_GPASID_CD (1 << 5) /* PASID-level cache disable */ + __u64 flags; + __u32 pat; + __u32 emt; +}; +#define IOMMU_SVA_VTD_GPASID_EMT_MASK (IOMMU_SVA_VTD_GPASID_CD | \ + IOMMU_SVA_VTD_GPASID_EMTE | \ + IOMMU_SVA_VTD_GPASID_PCD | \ + IOMMU_SVA_VTD_GPASID_PWT) +/** + * struct iommu_gpasid_bind_data - Information about device and guest PASI= D binding + * @version: IOMMU_UAPI_VERSION + * @format: PASID table entry format + * @flags: Additional information on guest bind request + * @gpgd: Guest page directory base of the guest mm to bind + * @hpasid: Process address space ID used for the guest mm in host IOMMU + * @gpasid: Process address space ID used for the guest mm in guest IOMMU + * @addr_width: Guest virtual address width + * @padding: Reserved for future use (should be zero) + * @dummy Reserve space for vendor specific data in the union. New + * members added to the union cannot exceed the size of dummy. + * The fixed size union is needed to allow further expansion + * after the end of the union while still maintain backward + * compatibility. + * @vtd: Intel VT-d specific data + * + * Guest to host PASID mapping can be an identity or non-identity, where g= uest + * has its own PASID space. For non-identify mapping, guest to host PASID = lookup + * is needed when VM programs guest PASID into an assigned device. VMM may + * trap such PASID programming then request host IOMMU driver to convert g= uest + * PASID to host PASID based on this bind data. + */ +struct iommu_gpasid_bind_data { + __u32 version; +#define IOMMU_PASID_FORMAT_INTEL_VTD 1 + __u32 format; +#define IOMMU_SVA_GPASID_VAL (1 << 0) /* guest PASID valid */ + __u64 flags; + __u64 gpgd; + __u64 hpasid; + __u64 gpasid; + __u32 addr_width; + __u8 padding[12]; + /* Vendor specific data */ + union { + __u8 dummy[128]; + struct iommu_gpasid_bind_data_vtd vtd; + }; +}; + +#endif /* _IOMMU_H */ diff --git a/linux-headers/linux/vfio.h b/linux-headers/linux/vfio.h index fb10370..29d0071 100644 --- a/linux-headers/linux/vfio.h +++ b/linux-headers/linux/vfio.h @@ -14,6 +14,7 @@ =20 #include #include +#include =20 #define VFIO_API_VERSION 0 =20 @@ -47,6 +48,15 @@ #define VFIO_NOIOMMU_IOMMU 8 =20 /* + * Hardware IOMMUs with two-stage translation capability give userspace + * the ownership of stage-1 translation structures (e.g. page tables). + * VFIO exposes the two-stage IOMMU programming capability to userspace + * based on the IOMMU UAPIs. Therefore user of VFIO_TYPE1_NESTING should + * check the IOMMU UAPI version compatibility. + */ +#define VFIO_NESTING_IOMMU_UAPI 9 + +/* * The IOCTL interface is designed for extensibility by embedding the * structure length (argsz) and flags into structures passed between * kernel and userspace. We therefore use the _IO() macro for these @@ -748,6 +758,15 @@ struct vfio_iommu_type1_info_cap_iova_range { struct vfio_iova_range iova_ranges[]; }; =20 +#define VFIO_IOMMU_TYPE1_INFO_CAP_NESTING 2 + +struct vfio_iommu_type1_info_cap_nesting { + struct vfio_info_cap_header header; +#define VFIO_IOMMU_PASID_REQS (1 << 0) + __u32 nesting_capabilities; + __u32 stage1_formats; +}; + #define VFIO_IOMMU_GET_INFO _IO(VFIO_TYPE, VFIO_BASE + 12) =20 /** @@ -794,6 +813,114 @@ struct vfio_iommu_type1_dma_unmap { #define VFIO_IOMMU_ENABLE _IO(VFIO_TYPE, VFIO_BASE + 15) #define VFIO_IOMMU_DISABLE _IO(VFIO_TYPE, VFIO_BASE + 16) =20 +/* + * PASID (Process Address Space ID) is a PCIe concept which + * has been extended to support DMA isolation in fine-grain. + * With device assigned to user space (e.g. VMs), PASID alloc + * and free need to be system wide. This structure defines + * the info for pasid alloc/free between user space and kernel + * space. + * + * @flag=3DVFIO_IOMMU_PASID_ALLOC, refer to the @alloc_pasid + * @flag=3DVFIO_IOMMU_PASID_FREE, refer to @free_pasid + */ +struct vfio_iommu_type1_pasid_request { + __u32 argsz; +#define VFIO_IOMMU_PASID_ALLOC (1 << 0) +#define VFIO_IOMMU_PASID_FREE (1 << 1) + __u32 flags; + union { + struct { + __u32 min; + __u32 max; + __u32 result; + } alloc_pasid; + __u32 free_pasid; + }; +}; + +#define VFIO_PASID_REQUEST_MASK (VFIO_IOMMU_PASID_ALLOC | \ + VFIO_IOMMU_PASID_FREE) + +/** + * VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 22, + * struct vfio_iommu_type1_pasid_request) + * + * Availability of this feature depends on PASID support in the device, + * its bus, the underlying IOMMU and the CPU architecture. In VFIO, it + * is available after VFIO_SET_IOMMU. + * + * returns: 0 on success, -errno on failure. + */ +#define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 22) + +/** + * Supported flags: + * - VFIO_IOMMU_BIND_GUEST_PGTBL: bind guest page tables to host for + * nesting type IOMMUs. In @data field It takes struct + * iommu_gpasid_bind_data. + * - VFIO_IOMMU_UNBIND_GUEST_PGTBL: undo a bind guest page table operation + * invoked by VFIO_IOMMU_BIND_GUEST_PGTBL. + * + */ +struct vfio_iommu_type1_bind { + __u32 argsz; + __u32 flags; +#define VFIO_IOMMU_BIND_GUEST_PGTBL (1 << 0) +#define VFIO_IOMMU_UNBIND_GUEST_PGTBL (1 << 1) + __u8 data[]; +}; + +#define VFIO_IOMMU_BIND_MASK (VFIO_IOMMU_BIND_GUEST_PGTBL | \ + VFIO_IOMMU_UNBIND_GUEST_PGTBL) + +/** + * VFIO_IOMMU_BIND - _IOW(VFIO_TYPE, VFIO_BASE + 23, + * struct vfio_iommu_type1_bind) + * + * Manage address spaces of devices in this container. Initially a TYPE1 + * container can only have one address space, managed with + * VFIO_IOMMU_MAP/UNMAP_DMA. + * + * An IOMMU of type VFIO_TYPE1_NESTING_IOMMU can be managed by both MAP/UN= MAP + * and BIND ioctls at the same time. MAP/UNMAP acts on the stage-2 (host) = page + * tables, and BIND manages the stage-1 (guest) page tables. Other types of + * IOMMU may allow MAP/UNMAP and BIND to coexist, where MAP/UNMAP controls + * the traffics only require single stage translation while BIND controls = the + * traffics require nesting translation. But this depends on the underlying + * IOMMU architecture and isn't guaranteed. Example of this is the guest S= VA + * traffics, such traffics need nesting translation to gain gVA->gPA and t= hen + * gPA->hPA translation. + * + * Availability of this feature depends on the device, its bus, the underl= ying + * IOMMU and the CPU architecture. + * + * returns: 0 on success, -errno on failure. + */ +#define VFIO_IOMMU_BIND _IO(VFIO_TYPE, VFIO_BASE + 23) + +/** + * VFIO_IOMMU_CACHE_INVALIDATE - _IOW(VFIO_TYPE, VFIO_BASE + 24, + * struct vfio_iommu_type1_cache_invalidate) + * + * Propagate guest IOMMU cache invalidation to the host. The cache + * invalidation information is conveyed by @cache_info, the content + * format would be structures defined in uapi/linux/iommu.h. User + * should be aware of that the struct iommu_cache_invalidate_info + * has a @version field, vfio needs to parse this field before getting + * data from userspace. + * + * Availability of this IOCTL is after VFIO_SET_IOMMU. + * + * returns: 0 on success, -errno on failure. + */ +struct vfio_iommu_type1_cache_invalidate { + __u32 argsz; + __u32 flags; + struct iommu_cache_invalidate_info cache_info; +}; +#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ =20 /* --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542038; cv=none; d=zohomail.com; s=zohoarc; b=ngSA+pkzBFRzxQiQOw1xOsrfr5W3H6bbFgniNaiOPXxUbyVjwQ07n8dQY6c4ECiS5TGI6vXQm9DvfXchLl0sF7vF1MKpKZG6qBvWqq6TFwn/Eh9X/Q+BYB8HRo2cBaju8/Df8Kas82kMHnJ+rxwvRj4p5bMcz4wVGbkolED/+N0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542038; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=wThOpvC8WXfIelIFPLfd0aumh3C8vAjAWQThVBal9jA=; b=EcqOy/5j/lVpBW6OiQWst46gLalyUJd5OyOlLIf7m7dET9CmCBkky8YwpCWeCHM3yVEpyas1+3xhxra0op+VByuH15w1tS37QUbat0egHFYRG6sPbQzwpQfa4jU8p6At9+TuEv7kl8jD5tse7/xAMVTBkqIXUojyxPrYI1C5D8w= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542038811635.3673793613307; Sun, 29 Mar 2020 21:20:38 -0700 (PDT) Received: from localhost ([::1]:44622 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIluf-0002gE-Jc for importer@patchew.org; Mon, 30 Mar 2020 00:20:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37772) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltR-0000qZ-Ko for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltQ-0007pQ-CS for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:21 -0400 Received: from mga07.intel.com ([134.134.136.100]:12573) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltQ-0007o2-5J for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:20 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: E9RHadqledxleVOePZn/yyYNP9XXMCkfmTmUP9e7VrObPrLnJuOJzlDwzwTDvsohmcybhypS0W EE/L7i3PqEHQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: pvzNqe/wRmrOT/Q4jA66Yw2/UoXbFi0XPuJL5NXYzvOeW9gh/4WqsiHVdhFjHd5hGz//eQPJ2J 2/A4NAuMWmfQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632016" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 03/22] vfio: check VFIO_TYPE1_NESTING_IOMMU support Date: Sun, 29 Mar 2020 21:24:42 -0700 Message-Id: <1585542301-84087-4-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" VFIO needs to check VFIO_TYPE1_NESTING_IOMMU support with Kernel before further using it. e.g. requires to check IOMMU UAPI version. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Alex Williamson Signed-off-by: Liu Yi L Signed-off-by: Yi Sun --- hw/vfio/common.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 0b3593b..c276732 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1157,12 +1157,21 @@ static void vfio_put_address_space(VFIOAddressSpace= *space) static int vfio_get_iommu_type(VFIOContainer *container, Error **errp) { - int iommu_types[] =3D { VFIO_TYPE1v2_IOMMU, VFIO_TYPE1_IOMMU, + int iommu_types[] =3D { VFIO_TYPE1_NESTING_IOMMU, + VFIO_TYPE1v2_IOMMU, VFIO_TYPE1_IOMMU, VFIO_SPAPR_TCE_v2_IOMMU, VFIO_SPAPR_TCE_IOMMU }; - int i; + int i, version; =20 for (i =3D 0; i < ARRAY_SIZE(iommu_types); i++) { if (ioctl(container->fd, VFIO_CHECK_EXTENSION, iommu_types[i])) { + if (iommu_types[i] =3D=3D VFIO_TYPE1_NESTING_IOMMU) { + version =3D ioctl(container->fd, VFIO_CHECK_EXTENSION, + VFIO_NESTING_IOMMU_UAPI); + if (version < IOMMU_UAPI_VERSION) { + info_report("IOMMU UAPI incompatible for nesting"); + continue; + } + } return iommu_types[i]; } } @@ -1278,6 +1287,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, } =20 switch (container->iommu_type) { + case VFIO_TYPE1_NESTING_IOMMU: case VFIO_TYPE1v2_IOMMU: case VFIO_TYPE1_IOMMU: { --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542366; cv=none; d=zohomail.com; s=zohoarc; b=XSQewmWf2BAOn5j+ahSSxCmPE9tfzTu9j67aIgPrMsbFyem4fz/DHi5m1TPPMGwW5VDZF9FfEJ9igGdYw3UE+0ErzK56kTUs4g/c4QF0lxM20o8dg+aV6tVxSdQjMsQcZgIbhYA0zacgAcCQiyvpEJr+Y7rsPVQIH4/B6Rqvb9g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542366; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=PqXFCIYqnOJZ0q5+dPhCUCpbK2VbaqAx63impD0uLVk=; b=A460QyB2fB6S0pkAbPmpRRla/la/3Y+XxQAq7gP7oJ1+NICPDkwDdIL/a+7CYRNMhPNHfLu6pQ0QaztmaTH3oOeS9PGtHLCLiGFjuE92RAiHZp13ZuSIAdSh9FWX6GE0nzIuJz2S5QNZTRgKiSGD3MNlxk1445o7glMsgCOr2ec= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542366351445.02053489818366; Sun, 29 Mar 2020 21:26:06 -0700 (PDT) Received: from localhost ([::1]:44728 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlzw-0004iz-QU for importer@patchew.org; Mon, 30 Mar 2020 00:26:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37783) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltS-0000qd-74 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltQ-0007p4-9d for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:22 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltQ-0007lr-22 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:20 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: nd/CUsEXyDCEcV5h8BBfoY0SeUWOML6BcsQorqAqFam3/xnqoksAPrZDmacQSQ7IwksLM5TA2P 5dQFJ1krnlYw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: Pb7T2CtcngefiMZVnGzOB08VFNucDQzpwAcqrWBRWoa50CwjCy0dcOUveAHTaimSL4+XjMyxrr X1ApROc7KCjw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632020" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 04/22] hw/iommu: introduce HostIOMMUContext Date: Sun, 29 Mar 2020 21:24:43 -0700 Message-Id: <1585542301-84087-5-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Currently, many platform vendors provide the capability of dual stage DMA address translation in hardware. For example, nested translation on Intel VT-d scalable mode, nested stage translation on ARM SMMUv3, and etc. In dual stage DMA address translation, there are two stages address translation, stage-1 (a.k.a first-level) and stage-2 (a.k.a second-level) translation structures. Stage-1 translation results are also subjected to stage-2 translation structures. Take vSVA (Virtual Shared Virtual Addressing) as an example, guest IOMMU driver owns stage-1 translation structures (covers GVA->GPA translation), and host IOMMU driver owns stage-2 translation structures (covers GPA->HPA translation). VMM is responsible to bind stage-1 translation structures to host, thus hardware could achieve GVA->GPA and then GPA->HPA translation. For more background on SVA, refer the below links. - https://www.youtube.com/watch?v=3DKq_nfGK5MwQ - https://events19.lfasiallc.com/wp-content/uploads/2017/11/\ Shared-Virtual-Memory-in-KVM_Yi-Liu.pdf In QEMU, vIOMMU emulators expose IOMMUs to VM per their own spec (e.g. Intel VT-d spec). Devices are pass-through to guest via device pass- through components like VFIO. VFIO is a userspace driver framework which exposes host IOMMU programming capability to userspace in a secure manner. e.g. IOVA MAP/UNMAP requests. Thus the major connection between VFIO and vIOMMU are MAP/UNMAP. However, with the dual stage DMA translation support, there are more interactions between vIOMMU and VFIO as below: 1) PASID allocation (allow host to intercept in PASID allocation) 2) bind stage-1 translation structures to host 3) propagate stage-1 cache invalidation to host 4) DMA address translation fault (I/O page fault) servicing etc. With the above new interactions in QEMU, it requires an abstract layer to facilitate the above operations and expose to vIOMMU emulators as an explicit way for vIOMMU emulators call into VFIO. This patch introduces HostIOMMUContext to stand for hardware IOMMU w/ dual stage DMA address translation capability. And introduces HostIOMMUContextClass to provide methods for vIOMMU emulators to propagate dual-stage translation related requests to host. As a beginning, PASID allocation/free are defined to propagate PASID allocation/free requests to host which is helpful for the vendors who manage PASID in system-wide. In future, there will be more operations like bind_stage1_pgtbl, flush_stage1_cache and etc. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Michael S. Tsirkin Signed-off-by: Liu Yi L --- hw/Makefile.objs | 1 + hw/iommu/Makefile.objs | 1 + hw/iommu/host_iommu_context.c | 97 +++++++++++++++++++++++++++++++= ++++ include/hw/iommu/host_iommu_context.h | 75 +++++++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 hw/iommu/Makefile.objs create mode 100644 hw/iommu/host_iommu_context.c create mode 100644 include/hw/iommu/host_iommu_context.h diff --git a/hw/Makefile.objs b/hw/Makefile.objs index 660e2b4..cab83fe 100644 --- a/hw/Makefile.objs +++ b/hw/Makefile.objs @@ -40,6 +40,7 @@ devices-dirs-$(CONFIG_MEM_DEVICE) +=3D mem/ devices-dirs-$(CONFIG_NUBUS) +=3D nubus/ devices-dirs-y +=3D semihosting/ devices-dirs-y +=3D smbios/ +devices-dirs-y +=3D iommu/ endif =20 common-obj-y +=3D $(devices-dirs-y) diff --git a/hw/iommu/Makefile.objs b/hw/iommu/Makefile.objs new file mode 100644 index 0000000..e6eed4e --- /dev/null +++ b/hw/iommu/Makefile.objs @@ -0,0 +1 @@ +obj-y +=3D host_iommu_context.o diff --git a/hw/iommu/host_iommu_context.c b/hw/iommu/host_iommu_context.c new file mode 100644 index 0000000..5fb2223 --- /dev/null +++ b/hw/iommu/host_iommu_context.c @@ -0,0 +1,97 @@ +/* + * QEMU abstract of Host IOMMU + * + * Copyright (C) 2020 Intel Corporation. + * + * Authors: Liu Yi L + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qom/object.h" +#include "qapi/visitor.h" +#include "hw/iommu/host_iommu_context.h" + +int host_iommu_ctx_pasid_alloc(HostIOMMUContext *iommu_ctx, uint32_t min, + uint32_t max, uint32_t *pasid) +{ + HostIOMMUContextClass *hicxc; + + if (!iommu_ctx) { + return -EINVAL; + } + + hicxc =3D HOST_IOMMU_CONTEXT_GET_CLASS(iommu_ctx); + + if (!hicxc) { + return -EINVAL; + } + + if (!(iommu_ctx->flags & HOST_IOMMU_PASID_REQUEST) || + !hicxc->pasid_alloc) { + return -EINVAL; + } + + return hicxc->pasid_alloc(iommu_ctx, min, max, pasid); +} + +int host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_ctx, uint32_t pasid) +{ + HostIOMMUContextClass *hicxc; + + if (!iommu_ctx) { + return -EINVAL; + } + + hicxc =3D HOST_IOMMU_CONTEXT_GET_CLASS(iommu_ctx); + if (!hicxc) { + return -EINVAL; + } + + if (!(iommu_ctx->flags & HOST_IOMMU_PASID_REQUEST) || + !hicxc->pasid_free) { + return -EINVAL; + } + + return hicxc->pasid_free(iommu_ctx, pasid); +} + +void host_iommu_ctx_init(void *_iommu_ctx, size_t instance_size, + const char *mrtypename, + uint64_t flags) +{ + HostIOMMUContext *iommu_ctx; + + object_initialize(_iommu_ctx, instance_size, mrtypename); + iommu_ctx =3D HOST_IOMMU_CONTEXT(_iommu_ctx); + iommu_ctx->flags =3D flags; + iommu_ctx->initialized =3D true; +} + +static const TypeInfo host_iommu_context_info =3D { + .parent =3D TYPE_OBJECT, + .name =3D TYPE_HOST_IOMMU_CONTEXT, + .class_size =3D sizeof(HostIOMMUContextClass), + .instance_size =3D sizeof(HostIOMMUContext), + .abstract =3D true, +}; + +static void host_iommu_ctx_register_types(void) +{ + type_register_static(&host_iommu_context_info); +} + +type_init(host_iommu_ctx_register_types) diff --git a/include/hw/iommu/host_iommu_context.h b/include/hw/iommu/host_= iommu_context.h new file mode 100644 index 0000000..35c4861 --- /dev/null +++ b/include/hw/iommu/host_iommu_context.h @@ -0,0 +1,75 @@ +/* + * QEMU abstraction of Host IOMMU + * + * Copyright (C) 2020 Intel Corporation. + * + * Authors: Liu Yi L + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#ifndef HW_IOMMU_CONTEXT_H +#define HW_IOMMU_CONTEXT_H + +#include "qemu/queue.h" +#include "qemu/thread.h" +#include "qom/object.h" +#include +#ifndef CONFIG_USER_ONLY +#include "exec/hwaddr.h" +#endif + +#define TYPE_HOST_IOMMU_CONTEXT "qemu:host-iommu-context" +#define HOST_IOMMU_CONTEXT(obj) \ + OBJECT_CHECK(HostIOMMUContext, (obj), TYPE_HOST_IOMMU_CONTEXT) +#define HOST_IOMMU_CONTEXT_GET_CLASS(obj) \ + OBJECT_GET_CLASS(HostIOMMUContextClass, (obj), \ + TYPE_HOST_IOMMU_CONTEXT) + +typedef struct HostIOMMUContext HostIOMMUContext; + +typedef struct HostIOMMUContextClass { + /* private */ + ObjectClass parent_class; + + /* Allocate pasid from HostIOMMUContext (a.k.a. host software) */ + int (*pasid_alloc)(HostIOMMUContext *iommu_ctx, + uint32_t min, + uint32_t max, + uint32_t *pasid); + /* Reclaim pasid from HostIOMMUContext (a.k.a. host software) */ + int (*pasid_free)(HostIOMMUContext *iommu_ctx, + uint32_t pasid); +} HostIOMMUContextClass; + +/* + * This is an abstraction of host IOMMU with dual-stage capability + */ +struct HostIOMMUContext { + Object parent_obj; +#define HOST_IOMMU_PASID_REQUEST (1ULL << 0) + uint64_t flags; + bool initialized; +}; + +int host_iommu_ctx_pasid_alloc(HostIOMMUContext *iommu_ctx, uint32_t min, + uint32_t max, uint32_t *pasid); +int host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_ctx, uint32_t pasid); + +void host_iommu_ctx_init(void *_iommu_ctx, size_t instance_size, + const char *mrtypename, + uint64_t flags); +void host_iommu_ctx_destroy(HostIOMMUContext *iommu_ctx); + +#endif --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542212; cv=none; d=zohomail.com; s=zohoarc; b=dvTkO2HOj5+DSg/MR6tJC0/gU45dq95Jxjg2rbIdc4SylFWGj8di6LrQnb2ONPLdAAJEUnW+x+rpapzIG6TTNHoDgzOC884/MRw0d/93ISTs3zsi/cRoCElW7ZNg8pnsna5D4QxQhSQcHOpHzTI3lMsgca8Zflz4+Y2YxLs+LGk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542212; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=nSBa5D3FfioJn3VGJSOjWV7JXyGUxqqr7sJWwiDUZIk=; b=ZCv0qko3cCxPAVvS/SQKZ2HxtwZxjSAHxtY0Ktg1t86OJlwTVORL51OSid8AQllcQNDvhT/CLKkg5Ie4Yuya6Kirc42YkxQWXNGUcAeqJEQ2WnEwgV5GJxQKOcWtJVoEqI30cvtNBHTXqwJ3Nvky1LegzGsRM1W3uJ+YNFqpZec= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542212627266.8485208927922; Sun, 29 Mar 2020 21:23:32 -0700 (PDT) Received: from localhost ([::1]:44668 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlxT-0007Yt-68 for importer@patchew.org; Mon, 30 Mar 2020 00:23:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37863) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltU-0000r5-50 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltQ-0007qD-UN for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:24 -0400 Received: from mga07.intel.com ([134.134.136.100]:12573) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltQ-0007o2-J3 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:20 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: zEgLaah289sJzRpIWS/iMH9t3jk62kYQPXktifA39kQ4cqHoYOz/XdgVIA3npXC0NO7OeNeSNg RmEv4Vr8wbTQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: u2c5hnrb0xX7tiI+yf6x429WJ29Ti+sf7iKOYRzDtSepUfY6He89ouDfT3y61p0A/WTNccL1Vz 2aDH4bWnRNig== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632023" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 05/22] hw/pci: modify pci_setup_iommu() to set PCIIOMMUOps Date: Sun, 29 Mar 2020 21:24:44 -0700 Message-Id: <1585542301-84087-6-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch modifies pci_setup_iommu() to set PCIIOMMUOps instead of setting PCIIOMMUFunc. PCIIOMMUFunc is used to get an address space for a PCI device in vendor specific way. The PCIIOMMUOps still offers this functionality. But using PCIIOMMUOps leaves space to add more iommu related vendor specific operations. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Michael S. Tsirkin Reviewed-by: David Gibson Reviewed-by: Peter Xu Signed-off-by: Liu Yi L --- hw/alpha/typhoon.c | 6 +++++- hw/arm/smmu-common.c | 6 +++++- hw/hppa/dino.c | 6 +++++- hw/i386/amd_iommu.c | 6 +++++- hw/i386/intel_iommu.c | 6 +++++- hw/pci-host/designware.c | 6 +++++- hw/pci-host/pnv_phb3.c | 6 +++++- hw/pci-host/pnv_phb4.c | 6 +++++- hw/pci-host/ppce500.c | 6 +++++- hw/pci-host/prep.c | 6 +++++- hw/pci-host/sabre.c | 6 +++++- hw/pci/pci.c | 12 +++++++----- hw/ppc/ppc440_pcix.c | 6 +++++- hw/ppc/spapr_pci.c | 6 +++++- hw/s390x/s390-pci-bus.c | 8 ++++++-- hw/virtio/virtio-iommu.c | 6 +++++- include/hw/pci/pci.h | 8 ++++++-- include/hw/pci/pci_bus.h | 2 +- 18 files changed, 90 insertions(+), 24 deletions(-) diff --git a/hw/alpha/typhoon.c b/hw/alpha/typhoon.c index 1795e2f..f271de1 100644 --- a/hw/alpha/typhoon.c +++ b/hw/alpha/typhoon.c @@ -740,6 +740,10 @@ static AddressSpace *typhoon_pci_dma_iommu(PCIBus *bus= , void *opaque, int devfn) return &s->pchip.iommu_as; } =20 +static const PCIIOMMUOps typhoon_iommu_ops =3D { + .get_address_space =3D typhoon_pci_dma_iommu, +}; + static void typhoon_set_irq(void *opaque, int irq, int level) { TyphoonState *s =3D opaque; @@ -897,7 +901,7 @@ PCIBus *typhoon_init(MemoryRegion *ram, ISABus **isa_bu= s, qemu_irq *p_rtc_irq, "iommu-typhoon", UINT64_MAX); address_space_init(&s->pchip.iommu_as, MEMORY_REGION(&s->pchip.iommu), "pchip0-pci"); - pci_setup_iommu(b, typhoon_pci_dma_iommu, s); + pci_setup_iommu(b, &typhoon_iommu_ops, s); =20 /* Pchip0 PCI special/interrupt acknowledge, 0x801.F800.0000, 64MB. */ memory_region_init_io(&s->pchip.reg_iack, OBJECT(s), &alpha_pci_iack_o= ps, diff --git a/hw/arm/smmu-common.c b/hw/arm/smmu-common.c index e13a5f4..447146e 100644 --- a/hw/arm/smmu-common.c +++ b/hw/arm/smmu-common.c @@ -343,6 +343,10 @@ static AddressSpace *smmu_find_add_as(PCIBus *bus, voi= d *opaque, int devfn) return &sdev->as; } =20 +static const PCIIOMMUOps smmu_ops =3D { + .get_address_space =3D smmu_find_add_as, +}; + IOMMUMemoryRegion *smmu_iommu_mr(SMMUState *s, uint32_t sid) { uint8_t bus_n, devfn; @@ -437,7 +441,7 @@ static void smmu_base_realize(DeviceState *dev, Error *= *errp) s->smmu_pcibus_by_busptr =3D g_hash_table_new(NULL, NULL); =20 if (s->primary_bus) { - pci_setup_iommu(s->primary_bus, smmu_find_add_as, s); + pci_setup_iommu(s->primary_bus, &smmu_ops, s); } else { error_setg(errp, "SMMU is not attached to any PCI bus!"); } diff --git a/hw/hppa/dino.c b/hw/hppa/dino.c index 2b1b38c..3da4f84 100644 --- a/hw/hppa/dino.c +++ b/hw/hppa/dino.c @@ -459,6 +459,10 @@ static AddressSpace *dino_pcihost_set_iommu(PCIBus *bu= s, void *opaque, return &s->bm_as; } =20 +static const PCIIOMMUOps dino_iommu_ops =3D { + .get_address_space =3D dino_pcihost_set_iommu, +}; + /* * Dino interrupts are connected as shown on Page 78, Table 23 * (Little-endian bit numbers) @@ -580,7 +584,7 @@ PCIBus *dino_init(MemoryRegion *addr_space, memory_region_add_subregion(&s->bm, 0xfff00000, &s->bm_cpu_alias); address_space_init(&s->bm_as, &s->bm, "pci-bm"); - pci_setup_iommu(b, dino_pcihost_set_iommu, s); + pci_setup_iommu(b, &dino_iommu_ops, s); =20 *p_rtc_irq =3D qemu_allocate_irq(dino_set_timer_irq, s, 0); *p_ser_irq =3D qemu_allocate_irq(dino_set_serial_irq, s, 0); diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c index b1175e5..5fec30e 100644 --- a/hw/i386/amd_iommu.c +++ b/hw/i386/amd_iommu.c @@ -1451,6 +1451,10 @@ static AddressSpace *amdvi_host_dma_iommu(PCIBus *bu= s, void *opaque, int devfn) return &iommu_as[devfn]->as; } =20 +static const PCIIOMMUOps amdvi_iommu_ops =3D { + .get_address_space =3D amdvi_host_dma_iommu, +}; + static const MemoryRegionOps mmio_mem_ops =3D { .read =3D amdvi_mmio_read, .write =3D amdvi_mmio_write, @@ -1577,7 +1581,7 @@ static void amdvi_realize(DeviceState *dev, Error **e= rrp) =20 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio); sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, AMDVI_BASE_ADDR); - pci_setup_iommu(bus, amdvi_host_dma_iommu, s); + pci_setup_iommu(bus, &amdvi_iommu_ops, s); s->devid =3D object_property_get_int(OBJECT(&s->pci), "addr", errp); msi_init(&s->pci.dev, 0, 1, true, false, errp); amdvi_init(s); diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index df7ad25..4b22910 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3729,6 +3729,10 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus,= void *opaque, int devfn) return &vtd_as->as; } =20 +static PCIIOMMUOps vtd_iommu_ops =3D { + .get_address_space =3D vtd_host_dma_iommu, +}; + static bool vtd_decide_config(IntelIOMMUState *s, Error **errp) { X86IOMMUState *x86_iommu =3D X86_IOMMU_DEVICE(s); @@ -3840,7 +3844,7 @@ static void vtd_realize(DeviceState *dev, Error **err= p) g_free, g_free); vtd_init(s); sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR); - pci_setup_iommu(bus, vtd_host_dma_iommu, dev); + pci_setup_iommu(bus, &vtd_iommu_ops, dev); /* Pseudo address space under root PCI bus. */ x86ms->ioapic_as =3D vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPI= C); qemu_add_machine_init_done_notifier(&vtd_machine_done_notify); diff --git a/hw/pci-host/designware.c b/hw/pci-host/designware.c index dd24551..4c6338a 100644 --- a/hw/pci-host/designware.c +++ b/hw/pci-host/designware.c @@ -645,6 +645,10 @@ static AddressSpace *designware_pcie_host_set_iommu(PC= IBus *bus, void *opaque, return &s->pci.address_space; } =20 +static const PCIIOMMUOps designware_iommu_ops =3D { + .get_address_space =3D designware_pcie_host_set_iommu, +}; + static void designware_pcie_host_realize(DeviceState *dev, Error **errp) { PCIHostState *pci =3D PCI_HOST_BRIDGE(dev); @@ -686,7 +690,7 @@ static void designware_pcie_host_realize(DeviceState *d= ev, Error **errp) address_space_init(&s->pci.address_space, &s->pci.address_space_root, "pcie-bus-address-space"); - pci_setup_iommu(pci->bus, designware_pcie_host_set_iommu, s); + pci_setup_iommu(pci->bus, &designware_iommu_ops, s); =20 qdev_set_parent_bus(DEVICE(&s->root), BUS(pci->bus)); qdev_init_nofail(DEVICE(&s->root)); diff --git a/hw/pci-host/pnv_phb3.c b/hw/pci-host/pnv_phb3.c index 74618fa..ecfe627 100644 --- a/hw/pci-host/pnv_phb3.c +++ b/hw/pci-host/pnv_phb3.c @@ -961,6 +961,10 @@ static AddressSpace *pnv_phb3_dma_iommu(PCIBus *bus, v= oid *opaque, int devfn) return &ds->dma_as; } =20 +static PCIIOMMUOps pnv_phb3_iommu_ops =3D { + .get_address_space =3D pnv_phb3_dma_iommu, +}; + static void pnv_phb3_instance_init(Object *obj) { PnvPHB3 *phb =3D PNV_PHB3(obj); @@ -1059,7 +1063,7 @@ static void pnv_phb3_realize(DeviceState *dev, Error = **errp) &phb->pci_mmio, &phb->pci_io, 0, 4, TYPE_PNV_PHB3_ROOT_BUS); =20 - pci_setup_iommu(pci->bus, pnv_phb3_dma_iommu, phb); + pci_setup_iommu(pci->bus, &pnv_phb3_iommu_ops, phb); =20 /* Add a single Root port */ qdev_prop_set_uint8(DEVICE(&phb->root), "chassis", phb->chip_id); diff --git a/hw/pci-host/pnv_phb4.c b/hw/pci-host/pnv_phb4.c index 23cf093..04e95e3 100644 --- a/hw/pci-host/pnv_phb4.c +++ b/hw/pci-host/pnv_phb4.c @@ -1148,6 +1148,10 @@ static AddressSpace *pnv_phb4_dma_iommu(PCIBus *bus,= void *opaque, int devfn) return &ds->dma_as; } =20 +static PCIIOMMUOps pnv_phb4_iommu_ops =3D { + .get_address_space =3D pnv_phb4_dma_iommu, +}; + static void pnv_phb4_instance_init(Object *obj) { PnvPHB4 *phb =3D PNV_PHB4(obj); @@ -1205,7 +1209,7 @@ static void pnv_phb4_realize(DeviceState *dev, Error = **errp) pnv_phb4_set_irq, pnv_phb4_map_irq, p= hb, &phb->pci_mmio, &phb->pci_io, 0, 4, TYPE_PNV_PHB4_ROOT_BUS); - pci_setup_iommu(pci->bus, pnv_phb4_dma_iommu, phb); + pci_setup_iommu(pci->bus, &pnv_phb4_iommu_ops, phb); =20 /* Add a single Root port */ qdev_prop_set_uint8(DEVICE(&phb->root), "chassis", phb->chip_id); diff --git a/hw/pci-host/ppce500.c b/hw/pci-host/ppce500.c index d710727..5baf5db 100644 --- a/hw/pci-host/ppce500.c +++ b/hw/pci-host/ppce500.c @@ -439,6 +439,10 @@ static AddressSpace *e500_pcihost_set_iommu(PCIBus *bu= s, void *opaque, return &s->bm_as; } =20 +static const PCIIOMMUOps ppce500_iommu_ops =3D { + .get_address_space =3D e500_pcihost_set_iommu, +}; + static void e500_pcihost_realize(DeviceState *dev, Error **errp) { SysBusDevice *sbd =3D SYS_BUS_DEVICE(dev); @@ -473,7 +477,7 @@ static void e500_pcihost_realize(DeviceState *dev, Erro= r **errp) memory_region_init(&s->bm, OBJECT(s), "bm-e500", UINT64_MAX); memory_region_add_subregion(&s->bm, 0x0, &s->busmem); address_space_init(&s->bm_as, &s->bm, "pci-bm"); - pci_setup_iommu(b, e500_pcihost_set_iommu, s); + pci_setup_iommu(b, &ppce500_iommu_ops, s); =20 pci_create_simple(b, 0, "e500-host-bridge"); =20 diff --git a/hw/pci-host/prep.c b/hw/pci-host/prep.c index 1a02e9a..7c57311 100644 --- a/hw/pci-host/prep.c +++ b/hw/pci-host/prep.c @@ -213,6 +213,10 @@ static AddressSpace *raven_pcihost_set_iommu(PCIBus *b= us, void *opaque, return &s->bm_as; } =20 +static const PCIIOMMUOps raven_iommu_ops =3D { + .get_address_space =3D raven_pcihost_set_iommu, +}; + static void raven_change_gpio(void *opaque, int n, int level) { PREPPCIState *s =3D opaque; @@ -303,7 +307,7 @@ static void raven_pcihost_initfn(Object *obj) memory_region_add_subregion(&s->bm, 0 , &s->bm_pci_memory_alia= s); memory_region_add_subregion(&s->bm, 0x80000000, &s->bm_ram_alias); address_space_init(&s->bm_as, &s->bm, "raven-bm"); - pci_setup_iommu(&s->pci_bus, raven_pcihost_set_iommu, s); + pci_setup_iommu(&s->pci_bus, &raven_iommu_ops, s); =20 h->bus =3D &s->pci_bus; =20 diff --git a/hw/pci-host/sabre.c b/hw/pci-host/sabre.c index 2b8503b..251549b 100644 --- a/hw/pci-host/sabre.c +++ b/hw/pci-host/sabre.c @@ -112,6 +112,10 @@ static AddressSpace *sabre_pci_dma_iommu(PCIBus *bus, = void *opaque, int devfn) return &is->iommu_as; } =20 +static const PCIIOMMUOps sabre_iommu_ops =3D { + .get_address_space =3D sabre_pci_dma_iommu, +}; + static void sabre_config_write(void *opaque, hwaddr addr, uint64_t val, unsigned size) { @@ -402,7 +406,7 @@ static void sabre_realize(DeviceState *dev, Error **err= p) /* IOMMU */ memory_region_add_subregion_overlap(&s->sabre_config, 0x200, sysbus_mmio_get_region(SYS_BUS_DEVICE(s->iommu), 0), 1= ); - pci_setup_iommu(phb->bus, sabre_pci_dma_iommu, s->iommu); + pci_setup_iommu(phb->bus, &sabre_iommu_ops, s->iommu); =20 /* APB secondary busses */ pci_dev =3D pci_create_multifunction(phb->bus, PCI_DEVFN(1, 0), true, diff --git a/hw/pci/pci.c b/hw/pci/pci.c index e1ed667..aa9025c 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2644,7 +2644,7 @@ AddressSpace *pci_device_iommu_address_space(PCIDevic= e *dev) PCIBus *iommu_bus =3D bus; uint8_t devfn =3D dev->devfn; =20 - while (iommu_bus && !iommu_bus->iommu_fn && iommu_bus->parent_dev) { + while (iommu_bus && !iommu_bus->iommu_ops && iommu_bus->parent_dev) { PCIBus *parent_bus =3D pci_get_bus(iommu_bus->parent_dev); =20 /* @@ -2683,15 +2683,17 @@ AddressSpace *pci_device_iommu_address_space(PCIDev= ice *dev) =20 iommu_bus =3D parent_bus; } - if (iommu_bus && iommu_bus->iommu_fn) { - return iommu_bus->iommu_fn(bus, iommu_bus->iommu_opaque, devfn); + if (iommu_bus && iommu_bus->iommu_ops && + iommu_bus->iommu_ops->get_address_space) { + return iommu_bus->iommu_ops->get_address_space(bus, + iommu_bus->iommu_opaque, devfn); } return &address_space_memory; } =20 -void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque) +void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *ops, void *opaque) { - bus->iommu_fn =3D fn; + bus->iommu_ops =3D ops; bus->iommu_opaque =3D opaque; } =20 diff --git a/hw/ppc/ppc440_pcix.c b/hw/ppc/ppc440_pcix.c index 2ee2d4f..7b17ee5 100644 --- a/hw/ppc/ppc440_pcix.c +++ b/hw/ppc/ppc440_pcix.c @@ -442,6 +442,10 @@ static AddressSpace *ppc440_pcix_set_iommu(PCIBus *b, = void *opaque, int devfn) return &s->bm_as; } =20 +static const PCIIOMMUOps ppc440_iommu_ops =3D { + .get_address_space =3D ppc440_pcix_set_iommu, +}; + /* The default pci_host_data_{read,write} functions in pci/pci_host.c * deny access to registers without bit 31 set but our clients want * this to work so we have to override these here */ @@ -487,7 +491,7 @@ static void ppc440_pcix_realize(DeviceState *dev, Error= **errp) memory_region_init(&s->bm, OBJECT(s), "bm-ppc440-pcix", UINT64_MAX); memory_region_add_subregion(&s->bm, 0x0, &s->busmem); address_space_init(&s->bm_as, &s->bm, "pci-bm"); - pci_setup_iommu(h->bus, ppc440_pcix_set_iommu, s); + pci_setup_iommu(h->bus, &ppc440_iommu_ops, s); =20 memory_region_init(&s->container, OBJECT(s), "pci-container", PCI_ALL_= SIZE); memory_region_init_io(&h->conf_mem, OBJECT(s), &pci_host_conf_le_ops, diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index 709a527..729a1cb 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -771,6 +771,10 @@ static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, = void *opaque, int devfn) return &phb->iommu_as; } =20 +static const PCIIOMMUOps spapr_iommu_ops =3D { + .get_address_space =3D spapr_pci_dma_iommu, +}; + static char *spapr_phb_vfio_get_loc_code(SpaprPhbState *sphb, PCIDevice *= pdev) { char *path =3D NULL, *buf =3D NULL, *host =3D NULL; @@ -1950,7 +1954,7 @@ static void spapr_phb_realize(DeviceState *dev, Error= **errp) memory_region_add_subregion(&sphb->iommu_root, SPAPR_PCI_MSI_WINDOW, &sphb->msiwindow); =20 - pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb); + pci_setup_iommu(bus, &spapr_iommu_ops, sphb); =20 pci_bus_set_route_irq_fn(bus, spapr_route_intx_pin_to_irq); =20 diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c index ed8be12..c1c3aa4 100644 --- a/hw/s390x/s390-pci-bus.c +++ b/hw/s390x/s390-pci-bus.c @@ -635,6 +635,10 @@ static AddressSpace *s390_pci_dma_iommu(PCIBus *bus, v= oid *opaque, int devfn) return &iommu->as; } =20 +static const PCIIOMMUOps s390_iommu_ops =3D { + .get_address_space =3D s390_pci_dma_iommu, +}; + static uint8_t set_ind_atomic(uint64_t ind_loc, uint8_t to_be_set) { uint8_t ind_old, ind_new; @@ -748,7 +752,7 @@ static void s390_pcihost_realize(DeviceState *dev, Erro= r **errp) b =3D pci_register_root_bus(dev, NULL, s390_pci_set_irq, s390_pci_map_= irq, NULL, get_system_memory(), get_system_io(), = 0, 64, TYPE_PCI_BUS); - pci_setup_iommu(b, s390_pci_dma_iommu, s); + pci_setup_iommu(b, &s390_iommu_ops, s); =20 bus =3D BUS(b); qbus_set_hotplug_handler(bus, OBJECT(dev), &local_err); @@ -919,7 +923,7 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_d= ev, DeviceState *dev, =20 pdev =3D PCI_DEVICE(dev); pci_bridge_map_irq(pb, dev->id, s390_pci_map_irq); - pci_setup_iommu(&pb->sec_bus, s390_pci_dma_iommu, s); + pci_setup_iommu(&pb->sec_bus, &s390_iommu_ops, s); =20 qbus_set_hotplug_handler(BUS(&pb->sec_bus), OBJECT(s), errp); =20 diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 4cee808..fefc24e 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -235,6 +235,10 @@ static AddressSpace *virtio_iommu_find_add_as(PCIBus *= bus, void *opaque, return &sdev->as; } =20 +static const PCIIOMMUOps virtio_iommu_ops =3D { + .get_address_space =3D virtio_iommu_find_add_as, +}; + static int virtio_iommu_attach(VirtIOIOMMU *s, struct virtio_iommu_req_attach *req) { @@ -682,7 +686,7 @@ static void virtio_iommu_device_realize(DeviceState *de= v, Error **errp) s->as_by_busptr =3D g_hash_table_new_full(NULL, NULL, NULL, g_free); =20 if (s->primary_bus) { - pci_setup_iommu(s->primary_bus, virtio_iommu_find_add_as, s); + pci_setup_iommu(s->primary_bus, &virtio_iommu_ops, s); } else { error_setg(errp, "VIRTIO-IOMMU is not attached to any PCI bus!"); } diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index cfedf5a..ffe192d 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -485,10 +485,14 @@ void pci_bus_get_w64_range(PCIBus *bus, Range *range); =20 void pci_device_deassert_intx(PCIDevice *dev); =20 -typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int); +typedef struct PCIIOMMUOps PCIIOMMUOps; +struct PCIIOMMUOps { + AddressSpace * (*get_address_space)(PCIBus *bus, + void *opaque, int32_t devfn); +}; =20 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev); -void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque); +void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *iommu_ops, void *opaq= ue); =20 static inline void pci_set_byte(uint8_t *config, uint8_t val) diff --git a/include/hw/pci/pci_bus.h b/include/hw/pci/pci_bus.h index 0714f57..c281057 100644 --- a/include/hw/pci/pci_bus.h +++ b/include/hw/pci/pci_bus.h @@ -29,7 +29,7 @@ enum PCIBusFlags { struct PCIBus { BusState qbus; enum PCIBusFlags flags; - PCIIOMMUFunc iommu_fn; + const PCIIOMMUOps *iommu_ops; void *iommu_opaque; uint8_t devfn_min; uint32_t slot_reserved_mask; --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542265; cv=none; d=zohomail.com; s=zohoarc; b=JQED17ftBZ98psqtShmMfBy+jxWhpYVk9CvCGd5I5l1/AKP7FErmTNcLbszpj2y2iK3WUbo/ae4I0AIqLz+WZpVlgFLtt/9+UmjIxmeUjcMUozMxHMayPFK7h+nRJ93wIa96cr5F/3hVELYkSCEt6W/L+YAohv52n9d/4B1ZHoE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542265; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=5OSxubsDlh0kHspi83ZpX9MhFr1gKVjbJeq3jKWw7nQ=; b=F+nqjvI5yDjHceZHiUfnAcseadul+uJB0cMrhd9pesX7Bqz9k/LK7EIlHhn6h6AQQQIFp+cP3De4ImwR2CsCI7LCiG2uMrFRhBzLKuGVtNduC797XZucYmwsiWMxq8jafDzg460v4r12SGLCfHufcpp8vwqNbRRy7qRBW4SymBI= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542265024390.7059146034187; Sun, 29 Mar 2020 21:24:25 -0700 (PDT) Received: from localhost ([::1]:44689 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlyJ-0001KZ-MK for importer@patchew.org; Mon, 30 Mar 2020 00:24:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37803) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltS-0000qh-Py for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltR-0007qV-C7 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:22 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltR-0007lr-3C for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:21 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: RU6MpO1Dpcyk9UqWbmxTF6A6pWcNE+2iC2h7FXuGqzJARoQsEsvgIf8pc4wfPjNIIUIZHHxro+ h1Fg9ef9bAvw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: ZnyNag/5LO2VXlm50QOjwg1JEP7R5FzD+r92UD/SYja02ksOBqmd0NRq6kv3cTJ+nsVW3nupir uFwQ+VQZS6dw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632026" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 06/22] hw/pci: introduce pci_device_set/unset_iommu_context() Date: Sun, 29 Mar 2020 21:24:45 -0700 Message-Id: <1585542301-84087-7-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds pci_device_set/unset_iommu_context() to set/unset host_iommu_context for a given device. New callback is added in PCIIOMMUOps. As such, vIOMMU could make use of host IOMMU capability. e.g setup nested translation. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Michael S. Tsirkin Reviewed-by: Peter Xu Signed-off-by: Liu Yi L --- hw/pci/pci.c | 49 ++++++++++++++++++++++++++++++++++++++++++++----- include/hw/pci/pci.h | 10 ++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index aa9025c..af3c1a1 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2638,7 +2638,8 @@ static void pci_device_class_base_init(ObjectClass *k= lass, void *data) } } =20 -AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) +static void pci_device_get_iommu_bus_devfn(PCIDevice *dev, + PCIBus **pbus, uint8_t *pdevfn) { PCIBus *bus =3D pci_get_bus(dev); PCIBus *iommu_bus =3D bus; @@ -2683,14 +2684,52 @@ AddressSpace *pci_device_iommu_address_space(PCIDev= ice *dev) =20 iommu_bus =3D parent_bus; } - if (iommu_bus && iommu_bus->iommu_ops && - iommu_bus->iommu_ops->get_address_space) { - return iommu_bus->iommu_ops->get_address_space(bus, - iommu_bus->iommu_opaque, devfn); + *pbus =3D iommu_bus; + *pdevfn =3D devfn; +} + +AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) +{ + PCIBus *bus; + uint8_t devfn; + + pci_device_get_iommu_bus_devfn(dev, &bus, &devfn); + if (bus && bus->iommu_ops && + bus->iommu_ops->get_address_space) { + return bus->iommu_ops->get_address_space(bus, + bus->iommu_opaque, devfn); } return &address_space_memory; } =20 +int pci_device_set_iommu_context(PCIDevice *dev, + HostIOMMUContext *iommu_ctx) +{ + PCIBus *bus; + uint8_t devfn; + + pci_device_get_iommu_bus_devfn(dev, &bus, &devfn); + if (bus && bus->iommu_ops && + bus->iommu_ops->set_iommu_context) { + return bus->iommu_ops->set_iommu_context(bus, + bus->iommu_opaque, devfn, iommu_ctx); + } + return -ENOENT; +} + +void pci_device_unset_iommu_context(PCIDevice *dev) +{ + PCIBus *bus; + uint8_t devfn; + + pci_device_get_iommu_bus_devfn(dev, &bus, &devfn); + if (bus && bus->iommu_ops && + bus->iommu_ops->unset_iommu_context) { + bus->iommu_ops->unset_iommu_context(bus, + bus->iommu_opaque, devfn); + } +} + void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *ops, void *opaque) { bus->iommu_ops =3D ops; diff --git a/include/hw/pci/pci.h b/include/hw/pci/pci.h index ffe192d..0ec5680 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -9,6 +9,8 @@ =20 #include "hw/pci/pcie.h" =20 +#include "hw/iommu/host_iommu_context.h" + extern bool pci_available; =20 /* PCI bus */ @@ -489,9 +491,17 @@ typedef struct PCIIOMMUOps PCIIOMMUOps; struct PCIIOMMUOps { AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int32_t devfn); + int (*set_iommu_context)(PCIBus *bus, void *opaque, + int32_t devfn, + HostIOMMUContext *iommu_ctx); + void (*unset_iommu_context)(PCIBus *bus, void *opaque, + int32_t devfn); }; =20 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev); +int pci_device_set_iommu_context(PCIDevice *dev, + HostIOMMUContext *iommu_ctx); +void pci_device_unset_iommu_context(PCIDevice *dev); void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *iommu_ops, void *opaq= ue); =20 static inline void --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542078; cv=none; d=zohomail.com; s=zohoarc; b=c528NAOfnkxsiDvN06mEyk9C1l912VC26uE6sT0dNZVqidKvVikrzP7EEMvzy7pF04KhiNE/pgRX9CgQ63MZbl5MWkRMtXqGoKROtla5LWgjgAwzk3vsFCrUHEGAvc4AS4D23IrwN0SyJUbdKD82Xlts/Kmn9uHZebgHQNPnxPU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542078; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=ah3x8BZp5ZhMfJfMM2KFoZLM7KNnvWCKsTLutxdPK50=; b=D7jmoZK69/QGKpCwkwd2VKIM570Tq9mc67RBmM937ttYTN9WQ8y9zcynbgk0B7Vdk5e0iE0c2Ns4GwTHEGgMlU+CAquFbzhW62aGc/tTw6MnHXD34Krb8GbvgIQ1Kom94j3JOlWOMrYujyaC+wtnafXUkEpnb5K+VuuEZvy4dJk= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542078214809.2632274347603; Sun, 29 Mar 2020 21:21:18 -0700 (PDT) Received: from localhost ([::1]:44636 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlvI-00048H-T2 for importer@patchew.org; Mon, 30 Mar 2020 00:21:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37824) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltT-0000qo-8b for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltR-0007qk-GU for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:23 -0400 Received: from mga07.intel.com ([134.134.136.100]:12573) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltR-0007o2-6w for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:21 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: Oz/XLgqOaoi9LjQ8q5NCUrl7awbYlBaX/VNj803K6NQPX+mZRYBs18F71caBn2qU6BcT94hnXw nL5Ne8MS8Vxg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: OybUe+OIIaTxJ5/AJtn153r7J+TcgAogiJdZ/VmBy0gdRVWIMoukEGfzJFDloOTy8XixGh0oiC AmlbtIUve/Sg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632029" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 07/22] intel_iommu: add set/unset_iommu_context callback Date: Sun, 29 Mar 2020 21:24:46 -0700 Message-Id: <1585542301-84087-8-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds set/unset_iommu_context() impelementation in Intel vIOMMU. For Intel platform, pass-through modules (e.g. VFIO) could set HostIOMMUContext to Intel vIOMMU emulator. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 71 +++++++++++++++++++++++++++++++++++++++= +--- include/hw/i386/intel_iommu.h | 21 ++++++++++--- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 4b22910..fd349c6 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3354,23 +3354,33 @@ static const MemoryRegionOps vtd_mem_ir_ops =3D { }, }; =20 -VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devf= n) +/** + * Fetch a VTDBus instance for given PCIBus. If no existing instance, + * allocate one. + */ +static VTDBus *vtd_find_add_bus(IntelIOMMUState *s, PCIBus *bus) { uintptr_t key =3D (uintptr_t)bus; VTDBus *vtd_bus =3D g_hash_table_lookup(s->vtd_as_by_busptr, &key); - VTDAddressSpace *vtd_dev_as; - char name[128]; =20 if (!vtd_bus) { uintptr_t *new_key =3D g_malloc(sizeof(*new_key)); *new_key =3D (uintptr_t)bus; /* No corresponding free() */ - vtd_bus =3D g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) *= \ - PCI_DEVFN_MAX); + vtd_bus =3D g_malloc0(sizeof(VTDBus)); vtd_bus->bus =3D bus; g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus); } + return vtd_bus; +} =20 +VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devf= n) +{ + VTDBus *vtd_bus; + VTDAddressSpace *vtd_dev_as; + char name[128]; + + vtd_bus =3D vtd_find_add_bus(s, bus); vtd_dev_as =3D vtd_bus->dev_as[devfn]; =20 if (!vtd_dev_as) { @@ -3436,6 +3446,55 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s,= PCIBus *bus, int devfn) return vtd_dev_as; } =20 +static int vtd_dev_set_iommu_context(PCIBus *bus, void *opaque, + int devfn, + HostIOMMUContext *iommu_ctx) +{ + IntelIOMMUState *s =3D opaque; + VTDBus *vtd_bus; + VTDHostIOMMUContext *vtd_dev_icx; + + assert(0 <=3D devfn && devfn < PCI_DEVFN_MAX); + + vtd_bus =3D vtd_find_add_bus(s, bus); + + vtd_iommu_lock(s); + + vtd_dev_icx =3D vtd_bus->dev_icx[devfn]; + + assert(!vtd_dev_icx); + + vtd_bus->dev_icx[devfn] =3D vtd_dev_icx =3D + g_malloc0(sizeof(VTDHostIOMMUContext)); + vtd_dev_icx->vtd_bus =3D vtd_bus; + vtd_dev_icx->devfn =3D (uint8_t)devfn; + vtd_dev_icx->iommu_state =3D s; + vtd_dev_icx->iommu_ctx =3D iommu_ctx; + + vtd_iommu_unlock(s); + + return 0; +} + +static void vtd_dev_unset_iommu_context(PCIBus *bus, void *opaque, int dev= fn) +{ + IntelIOMMUState *s =3D opaque; + VTDBus *vtd_bus; + VTDHostIOMMUContext *vtd_dev_icx; + + assert(0 <=3D devfn && devfn < PCI_DEVFN_MAX); + + vtd_bus =3D vtd_find_add_bus(s, bus); + + vtd_iommu_lock(s); + + vtd_dev_icx =3D vtd_bus->dev_icx[devfn]; + g_free(vtd_dev_icx); + vtd_bus->dev_icx[devfn] =3D NULL; + + vtd_iommu_unlock(s); +} + static uint64_t get_naturally_aligned_size(uint64_t start, uint64_t size, int gaw) { @@ -3731,6 +3790,8 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, = void *opaque, int devfn) =20 static PCIIOMMUOps vtd_iommu_ops =3D { .get_address_space =3D vtd_host_dma_iommu, + .set_iommu_context =3D vtd_dev_set_iommu_context, + .unset_iommu_context =3D vtd_dev_unset_iommu_context, }; =20 static bool vtd_decide_config(IntelIOMMUState *s, Error **errp) diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 3870052..b5fefb9 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -64,6 +64,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry; typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress; typedef struct VTDPASIDDirEntry VTDPASIDDirEntry; typedef struct VTDPASIDEntry VTDPASIDEntry; +typedef struct VTDHostIOMMUContext VTDHostIOMMUContext; =20 /* Context-Entry */ struct VTDContextEntry { @@ -112,10 +113,20 @@ struct VTDAddressSpace { IOVATree *iova_tree; /* Traces mapped IOVA ranges */ }; =20 +struct VTDHostIOMMUContext { + VTDBus *vtd_bus; + uint8_t devfn; + HostIOMMUContext *iommu_ctx; + IntelIOMMUState *iommu_state; +}; + struct VTDBus { - PCIBus* bus; /* A reference to the bus to provide translation for */ + /* A reference to the bus to provide translation for */ + PCIBus *bus; /* A table of VTDAddressSpace objects indexed by devfn */ - VTDAddressSpace *dev_as[]; + VTDAddressSpace *dev_as[PCI_DEVFN_MAX]; + /* A table of VTDHostIOMMUContext objects indexed by devfn */ + VTDHostIOMMUContext *dev_icx[PCI_DEVFN_MAX]; }; =20 struct VTDIOTLBEntry { @@ -269,8 +280,10 @@ struct IntelIOMMUState { bool dma_drain; /* Whether DMA r/w draining enabled */ =20 /* - * Protects IOMMU states in general. Currently it protects the - * per-IOMMU IOTLB cache, and context entry cache in VTDAddressSpace. + * iommu_lock protects below: + * - per-IOMMU IOTLB caches + * - context entry cache in VTDAddressSpace + * - HostIOMMUContext pointer cached in vIOMMU */ QemuMutex iommu_lock; }; --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542551; cv=none; d=zohomail.com; s=zohoarc; b=mIyjKXivaQRgshwcdjacA9GjhfCN1D8wraWMZwyHkQqrdxoGADzH6Xb9VaHBRoEZNESKjFYRQpVruX3CBWkD2w13auL5xaOjRqh53Ae+A9mxSrS7b/eve3FiVRxjSofocYJKbeTk7+NhhmXLtkrt/t65vGyDZed5t8NBfl/qYmo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542551; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=c4NNlja+C6tVyzy3hGnQvkkOlnL+5Wp9TohNWwM+gWo=; b=C0BScmV7BudWSWeBkpMx62bn3WgxAuoVrUT9u1dt9t3XNp0Mg+TeqWJeV1sP1hs8ntQkqUrDkMzk66VLT0Ft3qEZG4Lv8G5Z7lKpTi0Pfs2wXZvefxO0NGgjV2vFyuh7rf5VaPGztcmYzEmJbiQn0tw+Bk1hNk7F3DctvM766Io= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542551912952.1905731245289; Sun, 29 Mar 2020 21:29:11 -0700 (PDT) Received: from localhost ([::1]:44768 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm2w-0001Ws-Oz for importer@patchew.org; Mon, 30 Mar 2020 00:29:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37819) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltT-0000qm-4f for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltR-0007qp-GV for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:23 -0400 Received: from mga07.intel.com ([134.134.136.100]:12574) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltR-0007ph-71 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:21 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: wO3kMvN3Kxwj9DNkQ6ahF2qsv8V/w3mFlQitxPUWn/wlHlqO6dtlQoMqPkMAYQwrmPamqsyu0k LElSoz3uM9Qg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: nwyJtzTxoH0dzO+5cbtTfpjy5b6k1j6xyEnyqeddOqiFJrT/hHpbBEjUXXudkYHShVm3FA1yqp wwPDx9YevpLQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632033" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 08/22] vfio/common: provide PASID alloc/free hooks Date: Sun, 29 Mar 2020 21:24:47 -0700 Message-Id: <1585542301-84087-9-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch defines vfio_host_iommu_context_info, implements the PASID alloc/free hooks defined in HostIOMMUContextClass. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Alex Williamson Signed-off-by: Liu Yi L --- hw/vfio/common.c | 69 +++++++++++++++++++++++++++++++= ++++ include/hw/iommu/host_iommu_context.h | 3 ++ include/hw/vfio/vfio-common.h | 4 ++ 3 files changed, 76 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index c276732..5f3534d 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1179,6 +1179,53 @@ static int vfio_get_iommu_type(VFIOContainer *contai= ner, return -EINVAL; } =20 +static int vfio_host_iommu_ctx_pasid_alloc(HostIOMMUContext *iommu_ctx, + uint32_t min, uint32_t max, + uint32_t *pasid) +{ + VFIOContainer *container =3D container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_pasid_request req; + unsigned long argsz; + int ret; + + argsz =3D sizeof(req); + req.argsz =3D argsz; + req.flags =3D VFIO_IOMMU_PASID_ALLOC; + req.alloc_pasid.min =3D min; + req.alloc_pasid.max =3D max; + + if (ioctl(container->fd, VFIO_IOMMU_PASID_REQUEST, &req)) { + ret =3D -errno; + error_report("%s: %d, alloc failed", __func__, ret); + return ret; + } + *pasid =3D req.alloc_pasid.result; + return 0; +} + +static int vfio_host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_ctx, + uint32_t pasid) +{ + VFIOContainer *container =3D container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_pasid_request req; + unsigned long argsz; + int ret; + + argsz =3D sizeof(req); + req.argsz =3D argsz; + req.flags =3D VFIO_IOMMU_PASID_FREE; + req.free_pasid =3D pasid; + + if (ioctl(container->fd, VFIO_IOMMU_PASID_REQUEST, &req)) { + ret =3D -errno; + error_report("%s: %d, free failed", __func__, ret); + return ret; + } + return 0; +} + static int vfio_init_container(VFIOContainer *container, int group_fd, Error **errp) { @@ -1791,3 +1838,25 @@ int vfio_eeh_as_op(AddressSpace *as, uint32_t op) } return vfio_eeh_container_op(container, op); } + +static void vfio_host_iommu_context_class_init(ObjectClass *klass, + void *data) +{ + HostIOMMUContextClass *hicxc =3D HOST_IOMMU_CONTEXT_CLASS(klass); + + hicxc->pasid_alloc =3D vfio_host_iommu_ctx_pasid_alloc; + hicxc->pasid_free =3D vfio_host_iommu_ctx_pasid_free; +} + +static const TypeInfo vfio_host_iommu_context_info =3D { + .parent =3D TYPE_HOST_IOMMU_CONTEXT, + .name =3D TYPE_VFIO_HOST_IOMMU_CONTEXT, + .class_init =3D vfio_host_iommu_context_class_init, +}; + +static void vfio_register_types(void) +{ + type_register_static(&vfio_host_iommu_context_info); +} + +type_init(vfio_register_types) diff --git a/include/hw/iommu/host_iommu_context.h b/include/hw/iommu/host_= iommu_context.h index 35c4861..227c433 100644 --- a/include/hw/iommu/host_iommu_context.h +++ b/include/hw/iommu/host_iommu_context.h @@ -33,6 +33,9 @@ #define TYPE_HOST_IOMMU_CONTEXT "qemu:host-iommu-context" #define HOST_IOMMU_CONTEXT(obj) \ OBJECT_CHECK(HostIOMMUContext, (obj), TYPE_HOST_IOMMU_CONTEXT) +#define HOST_IOMMU_CONTEXT_CLASS(klass) \ + OBJECT_CLASS_CHECK(HostIOMMUContextClass, (klass), \ + TYPE_HOST_IOMMU_CONTEXT) #define HOST_IOMMU_CONTEXT_GET_CLASS(obj) \ OBJECT_GET_CLASS(HostIOMMUContextClass, (obj), \ TYPE_HOST_IOMMU_CONTEXT) diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index fd56420..0b07303 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -26,12 +26,15 @@ #include "qemu/notify.h" #include "ui/console.h" #include "hw/display/ramfb.h" +#include "hw/iommu/host_iommu_context.h" #ifdef CONFIG_LINUX #include #endif =20 #define VFIO_MSG_PREFIX "vfio %s: " =20 +#define TYPE_VFIO_HOST_IOMMU_CONTEXT "qemu:vfio-host-iommu-context" + enum { VFIO_DEVICE_TYPE_PCI =3D 0, VFIO_DEVICE_TYPE_PLATFORM =3D 1, @@ -71,6 +74,7 @@ typedef struct VFIOContainer { MemoryListener listener; MemoryListener prereg_listener; unsigned iommu_type; + HostIOMMUContext iommu_ctx; Error *error; bool initialized; unsigned long pgsizes; --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542174; cv=none; d=zohomail.com; s=zohoarc; b=YZXzAaNtKTHF3h4XSWKpkn1YpTD8lTM3VL6DD0ARLpriHL0ZmuVg286apidQDKaViRFntlG4hg/xIxq0ZJ5vd6maNgUMx6fg4BVZesMgGWDbg8IhsXiTFmhb2I/24qDnB5S7hzqSF+NVEsDy9iJU1pQ/LMQrST3ikTAbkfqB1Ak= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542174; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=WFdLUV68eY35Y2PPObm0vLFyEGExzZ8z8WZFQkvz/yY=; b=mI/oVu8I7QOUJpTCmoKXfp4AL6W1Oc9DHUIiJtEWdxXWAOnFhD8wdJvOhl/1lsWrqILrjPf0xLOGNe2gXSQAIXAVzNQld+V9Qsd39JkDpAvor6LsMszq+pTn4U9qpEFuhNlL3RP+BitVYW5bsMV/5OScCz3hC0EfBHSHcY02rjk= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542174194550.5512478099674; Sun, 29 Mar 2020 21:22:54 -0700 (PDT) Received: from localhost ([::1]:44660 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlwq-00068a-SY for importer@patchew.org; Mon, 30 Mar 2020 00:22:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37836) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltT-0000qu-IO for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltR-0007rq-WA for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:23 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltR-0007lr-Mg for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:21 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: 4cwxRCCvKpqsPFIJGho1lQZBLNvPuo7n3qPLUZl1XhLwE1zyiL75jntu3qygZ7Pm8TbqqUqdnS rBQcueRtM3ng== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: ZLu7dwmd3UX/hNHIm1tfGJHHzCIBeDN9DLDn6liq+1ASuA8Kazf5kf/F6h0rOrNs3WEHQzBkb6 4iEMKxsuqWfQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632036" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 09/22] vfio/common: init HostIOMMUContext per-container Date: Sun, 29 Mar 2020 21:24:48 -0700 Message-Id: <1585542301-84087-10-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In this patch, QEMU firstly gets iommu info from kernel to check the supported capabilities by a VFIO_IOMMU_TYPE1_NESTING iommu. And inits HostIOMMUContet instance. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Alex Williamson Signed-off-by: Liu Yi L --- hw/vfio/common.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 99 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 5f3534d..44b142c 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1226,10 +1226,89 @@ static int vfio_host_iommu_ctx_pasid_free(HostIOMMU= Context *iommu_ctx, return 0; } =20 +/** + * Get iommu info from host. Caller of this funcion should free + * the memory pointed by the returned pointer stored in @info + * after a successful calling when finished its usage. + */ +static int vfio_get_iommu_info(VFIOContainer *container, + struct vfio_iommu_type1_info **info) +{ + + size_t argsz =3D sizeof(struct vfio_iommu_type1_info); + + *info =3D g_malloc0(argsz); + +retry: + (*info)->argsz =3D argsz; + + if (ioctl(container->fd, VFIO_IOMMU_GET_INFO, *info)) { + g_free(*info); + *info =3D NULL; + return -errno; + } + + if (((*info)->argsz > argsz)) { + argsz =3D (*info)->argsz; + *info =3D g_realloc(*info, argsz); + goto retry; + } + + return 0; +} + +static struct vfio_info_cap_header * +vfio_get_iommu_info_cap(struct vfio_iommu_type1_info *info, uint16_t id) +{ + struct vfio_info_cap_header *hdr; + void *ptr =3D info; + + if (!(info->flags & VFIO_IOMMU_INFO_CAPS)) { + return NULL; + } + + for (hdr =3D ptr + info->cap_offset; hdr !=3D ptr; hdr =3D ptr + hdr->= next) { + if (hdr->id =3D=3D id) { + return hdr; + } + } + + return NULL; +} + +static int vfio_get_nesting_iommu_cap(VFIOContainer *container, + struct vfio_iommu_type1_info_cap_nesting *cap_nesting) +{ + struct vfio_iommu_type1_info *info; + struct vfio_info_cap_header *hdr; + struct vfio_iommu_type1_info_cap_nesting *cap; + int ret; + + ret =3D vfio_get_iommu_info(container, &info); + if (ret) { + return ret; + } + + hdr =3D vfio_get_iommu_info_cap(info, + VFIO_IOMMU_TYPE1_INFO_CAP_NESTING); + if (!hdr) { + g_free(info); + return -errno; + } + + cap =3D container_of(hdr, + struct vfio_iommu_type1_info_cap_nesting, header); + *cap_nesting =3D *cap; + + g_free(info); + return 0; +} + static int vfio_init_container(VFIOContainer *container, int group_fd, Error **errp) { int iommu_type, ret; + uint64_t flags =3D 0; =20 iommu_type =3D vfio_get_iommu_type(container, errp); if (iommu_type < 0) { @@ -1257,6 +1336,26 @@ static int vfio_init_container(VFIOContainer *contai= ner, int group_fd, return -errno; } =20 + if (iommu_type =3D=3D VFIO_TYPE1_NESTING_IOMMU) { + struct vfio_iommu_type1_info_cap_nesting nesting =3D { + .nesting_capabilities =3D 0x0, + .stage1_formats =3D 0, }; + + ret =3D vfio_get_nesting_iommu_cap(container, &nesting); + if (ret) { + error_setg_errno(errp, -ret, + "Failed to get nesting iommu cap"); + return ret; + } + + flags |=3D (nesting.nesting_capabilities & VFIO_IOMMU_PASID_REQS) ? + HOST_IOMMU_PASID_REQUEST : 0; + host_iommu_ctx_init(&container->iommu_ctx, + sizeof(container->iommu_ctx), + TYPE_VFIO_HOST_IOMMU_CONTEXT, + flags); + } + container->iommu_type =3D iommu_type; return 0; } --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542473; cv=none; d=zohomail.com; s=zohoarc; b=AVYlbZntn7TBG6fhYh0j4E5c5d6X96UffeitK7RO/q9PzmFtUFgrEUWdyasufLvV3e/f8NHxOnb/+lGsD+Qtvau55sB8fozcWpkxIK1beLqVRN/57SZocLvwH6K0DdfDBUciqoXRHHtqZHv+vprhE8sIYg6WYOs0Dfe45hEGUBQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542473; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=XOkMs/Rn0nBUajfUtwXPJ7Nt0UmLe4RQ7DilVXdH4NE=; b=Da3hWcZ8xYwPEeDUDO/9++acMo5ENqWBBagOAC3yWue2P3pFH7Q8pbi929z5VHlGd4VYvnvtMHiAM4lrdWT3TVTYvQ0U/Py4fP17zeH4ObFqqARzvpLx5Zqk8MEsqPjSZ0BW6Gzn07ay1XDKxhBUk3yE0wTZ+kibHbSvli/lA8Y= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542473491333.5220616230705; Sun, 29 Mar 2020 21:27:53 -0700 (PDT) Received: from localhost ([::1]:44756 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm1g-00084x-A3 for importer@patchew.org; Mon, 30 Mar 2020 00:27:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37827) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltT-0000qq-9z for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltR-0007rl-Vj for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:23 -0400 Received: from mga07.intel.com ([134.134.136.100]:12573) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltR-0007o2-Ms for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:21 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:15 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: 8/XBvJpv9bwcz+pHZSLdvTl42xLWtx8egm9XQ+k8yrGNcOt9f2RGbmYpUW+KwBG4gfw+hidW8+ dcVM2EnRzeMQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: qgNYyD69MoIiDphwOy365E8SrepQAAZKU4KDP1L17wz+CAzwFAlA8joiAPYPSP7bgSTbs7DeF3 d6fgsDdPh2Bg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632039" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 10/22] vfio/pci: set host iommu context to vIOMMU Date: Sun, 29 Mar 2020 21:24:49 -0700 Message-Id: <1585542301-84087-11-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" For vfio-pci devices, it could use pci_device_set/unset_iommu() to expose host iommu context to vIOMMU emulators. vIOMMU emulators could make use the methods provided by host iommu context. e.g. propagate requests to host iommu. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Alex Williamson Signed-off-by: Liu Yi L --- hw/vfio/pci.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 5e75a95..c140c88 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2717,6 +2717,7 @@ static void vfio_realize(PCIDevice *pdev, Error **err= p) VFIOPCIDevice *vdev =3D PCI_VFIO(pdev); VFIODevice *vbasedev_iter; VFIOGroup *group; + VFIOContainer *container; char *tmp, *subsys, group_path[PATH_MAX], *group_name; Error *err =3D NULL; ssize_t len; @@ -3028,6 +3029,11 @@ static void vfio_realize(PCIDevice *pdev, Error **er= rp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); =20 + container =3D vdev->vbasedev.group->container; + if (container->iommu_ctx.initialized) { + pci_device_set_iommu_context(pdev, &container->iommu_ctx); + } + return; =20 out_deregister: @@ -3072,9 +3078,16 @@ static void vfio_instance_finalize(Object *obj) static void vfio_exitfn(PCIDevice *pdev) { VFIOPCIDevice *vdev =3D PCI_VFIO(pdev); + VFIOContainer *container; =20 vfio_unregister_req_notifier(vdev); vfio_unregister_err_notifier(vdev); + + container =3D vdev->vbasedev.group->container; + if (container->iommu_ctx.initialized) { + pci_device_unset_iommu_context(pdev); + } + pci_device_set_intx_routing_notifier(&vdev->pdev, NULL); if (vdev->irqchip_change_notifier.notify) { kvm_irqchip_remove_change_notifier(&vdev->irqchip_change_notifier); --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542298; cv=none; d=zohomail.com; s=zohoarc; b=UxyvKb6SgdIIGSlu/E8k3D1501Nvr7ylnSjgagGaU8QqaaFKNkWch65AGGpL9xU3ptd6jToXZVguBDaJd0eT10pXg2Y/hs2Am0ktsmeAC/HFDaQtTiJ9C6vdHRekPwcMMHaEAD9aSupELapiQvMk2TwHUQnYv77SeCSrNtceQH4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542298; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=5pGhwQZlExhMqhXCnXsFoLRdCVn7NxXXyeYnKxE2H9Q=; b=TLeug7t8s/EE5HyxjQbLYuKvtRaB+n45AhsAlzccQB96JagUBvm9Zvge8f8eqTBPEXELV8wnjvVTpA1697gt/RcwJf34cvwq38TL9aTJ30uEH5GMkmlDFzhgNuQ/2hsxsBq0knd0nfgF1sNSDlxMahGbeSG0inwuIDzTI42pbmk= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542298337450.7271316609548; Sun, 29 Mar 2020 21:24:58 -0700 (PDT) Received: from localhost ([::1]:44704 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlyr-0002mB-0l for importer@patchew.org; Mon, 30 Mar 2020 00:24:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37902) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlte-0001FY-2n for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltc-00080C-01 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:33 -0400 Received: from mga07.intel.com ([134.134.136.100]:12574) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltb-0007ph-Nt for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:31 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: K97EWIA6rXgd/+v9W28oQPm7gHP2Iw8cALDpvGjBQmXgXSn7j2eN1Fwr8MMwuXlZ6EDM3bicYu /FyXw0DJZ5nA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: /FJB4razN8u/E72qAxo3zTvAWU7bFQAfqj3dYLdtXnljA9Ow5Vrl9fmc/WhVOMV1XC9hZ0UWOG oY4HIvjcFOEA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632043" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 11/22] intel_iommu: add virtual command capability support Date: Sun, 29 Mar 2020 21:24:50 -0700 Message-Id: <1585542301-84087-12-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds virtual command support to Intel vIOMMU per Intel VT-d 3.1 spec. And adds two virtual commands: allocate pasid and free pasid. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Reviewed-by: Peter Xu Signed-off-by: Liu Yi L Signed-off-by: Yi Sun --- hw/i386/intel_iommu.c | 154 +++++++++++++++++++++++++++++++++++++= +++- hw/i386/intel_iommu_internal.h | 37 ++++++++++ hw/i386/trace-events | 1 + include/hw/i386/intel_iommu.h | 10 ++- 4 files changed, 200 insertions(+), 2 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index fd349c6..6c3159f 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -2651,6 +2651,129 @@ static void vtd_handle_iectl_write(IntelIOMMUState = *s) } } =20 +static int vtd_request_pasid_alloc(IntelIOMMUState *s, uint32_t *pasid) +{ + VTDHostIOMMUContext *vtd_dev_icx; + int ret =3D -1; + + vtd_iommu_lock(s); + QLIST_FOREACH(vtd_dev_icx, &s->vtd_dev_icx_list, next) { + HostIOMMUContext *iommu_ctx =3D vtd_dev_icx->iommu_ctx; + + /* + * We'll return the first valid result we got. It's + * a bit hackish in that we don't have a good global + * interface yet to talk to modules like vfio to deliver + * this allocation request, so we're leveraging this + * per-device iommu context to do the same thing just + * to make sure the allocation happens only once. + */ + ret =3D host_iommu_ctx_pasid_alloc(iommu_ctx, VTD_HPASID_MIN, + VTD_HPASID_MAX, pasid); + if (!ret) { + break; + } + } + vtd_iommu_unlock(s); + + return ret; +} + +static int vtd_request_pasid_free(IntelIOMMUState *s, uint32_t pasid) +{ + VTDHostIOMMUContext *vtd_dev_icx; + int ret =3D -1; + + vtd_iommu_lock(s); + QLIST_FOREACH(vtd_dev_icx, &s->vtd_dev_icx_list, next) { + HostIOMMUContext *iommu_ctx =3D vtd_dev_icx->iommu_ctx; + + /* + * Similar with pasid allocation. We'll free the pasid + * on the first successful free operation. It's a bit + * hackish in that we don't have a good global interface + * yet to talk to modules like vfio to deliver this pasid + * free request, so we're leveraging this per-device iommu + * context to do the same thing just to make sure the free + * happens only once. + */ + ret =3D host_iommu_ctx_pasid_free(iommu_ctx, pasid); + if (!ret) { + break; + } + } + vtd_iommu_unlock(s); + + return ret; +} + +/* + * If IP is not set, set it then return. + * If IP is already set, return. + */ +static void vtd_vcmd_set_ip(IntelIOMMUState *s) +{ + s->vcrsp =3D 1; + vtd_set_quad_raw(s, DMAR_VCRSP_REG, + ((uint64_t) s->vcrsp)); +} + +static void vtd_vcmd_clear_ip(IntelIOMMUState *s) +{ + s->vcrsp &=3D (~((uint64_t)(0x1))); + vtd_set_quad_raw(s, DMAR_VCRSP_REG, + ((uint64_t) s->vcrsp)); +} + +/* Handle write to Virtual Command Register */ +static int vtd_handle_vcmd_write(IntelIOMMUState *s, uint64_t val) +{ + uint32_t pasid; + int ret =3D -1; + + trace_vtd_reg_write_vcmd(s->vcrsp, val); + + if (!(s->vccap & VTD_VCCAP_PAS) || + (s->vcrsp & 1)) { + return -1; + } + + /* + * Since vCPU should be blocked when the guest VMCD + * write was trapped to here. Should be no other vCPUs + * try to access VCMD if guest software is well written. + * However, we still emulate the IP bit here in case of + * bad guest software. Also align with the spec. + */ + vtd_vcmd_set_ip(s); + + switch (val & VTD_VCMD_CMD_MASK) { + case VTD_VCMD_ALLOC_PASID: + ret =3D vtd_request_pasid_alloc(s, &pasid); + if (ret) { + s->vcrsp |=3D VTD_VCRSP_SC(VTD_VCMD_NO_AVAILABLE_PASID); + } else { + s->vcrsp |=3D VTD_VCRSP_RSLT(pasid); + } + break; + + case VTD_VCMD_FREE_PASID: + pasid =3D VTD_VCMD_PASID_VALUE(val); + ret =3D vtd_request_pasid_free(s, pasid); + if (ret < 0) { + s->vcrsp |=3D VTD_VCRSP_SC(VTD_VCMD_FREE_INVALID_PASID); + } + break; + + default: + s->vcrsp |=3D VTD_VCRSP_SC(VTD_VCMD_UNDEFINED_CMD); + error_report_once("Virtual Command: unsupported command!!!"); + break; + } + vtd_vcmd_clear_ip(s); + return 0; +} + static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size) { IntelIOMMUState *s =3D opaque; @@ -2939,6 +3062,23 @@ static void vtd_mem_write(void *opaque, hwaddr addr, vtd_set_long(s, addr, val); break; =20 + case DMAR_VCMD_REG: + if (!vtd_handle_vcmd_write(s, val)) { + if (size =3D=3D 4) { + vtd_set_long(s, addr, val); + } else { + vtd_set_quad(s, addr, val); + } + } + break; + + case DMAR_VCMD_REG_HI: + assert(size =3D=3D 4); + if (!vtd_handle_vcmd_write(s, val)) { + vtd_set_long(s, addr, val); + } + break; + default: if (size =3D=3D 4) { vtd_set_long(s, addr, val); @@ -3470,6 +3610,7 @@ static int vtd_dev_set_iommu_context(PCIBus *bus, voi= d *opaque, vtd_dev_icx->devfn =3D (uint8_t)devfn; vtd_dev_icx->iommu_state =3D s; vtd_dev_icx->iommu_ctx =3D iommu_ctx; + QLIST_INSERT_HEAD(&s->vtd_dev_icx_list, vtd_dev_icx, next); =20 vtd_iommu_unlock(s); =20 @@ -3489,7 +3630,10 @@ static void vtd_dev_unset_iommu_context(PCIBus *bus,= void *opaque, int devfn) vtd_iommu_lock(s); =20 vtd_dev_icx =3D vtd_bus->dev_icx[devfn]; - g_free(vtd_dev_icx); + if (vtd_dev_icx) { + QLIST_REMOVE(vtd_dev_icx, next); + g_free(vtd_dev_icx); + } vtd_bus->dev_icx[devfn] =3D NULL; =20 vtd_iommu_unlock(s); @@ -3764,6 +3908,13 @@ static void vtd_init(IntelIOMMUState *s) * Interrupt remapping registers. */ vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0); + + /* + * Virtual Command Definitions + */ + vtd_define_quad(s, DMAR_VCCAP_REG, s->vccap, 0, 0); + vtd_define_quad(s, DMAR_VCMD_REG, 0, 0xffffffffffffffffULL, 0); + vtd_define_quad(s, DMAR_VCRSP_REG, 0, 0, 0); } =20 /* Should not reset address_spaces when reset because devices will still u= se @@ -3878,6 +4029,7 @@ static void vtd_realize(DeviceState *dev, Error **err= p) } =20 QLIST_INIT(&s->vtd_as_with_notifiers); + QLIST_INIT(&s->vtd_dev_icx_list); qemu_mutex_init(&s->iommu_lock); memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num)); memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s, diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 862033e..3fc83f1 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -85,6 +85,12 @@ #define DMAR_MTRRCAP_REG_HI 0x104 #define DMAR_MTRRDEF_REG 0x108 /* MTRR default type */ #define DMAR_MTRRDEF_REG_HI 0x10c +#define DMAR_VCCAP_REG 0xE00 /* Virtual Command Capability Regist= er */ +#define DMAR_VCCAP_REG_HI 0xE04 +#define DMAR_VCMD_REG 0xE10 /* Virtual Command Register */ +#define DMAR_VCMD_REG_HI 0xE14 +#define DMAR_VCRSP_REG 0xE20 /* Virtual Command Reponse Register = */ +#define DMAR_VCRSP_REG_HI 0xE24 =20 /* IOTLB registers */ #define DMAR_IOTLB_REG_OFFSET 0xf0 /* Offset to the IOTLB registers */ @@ -312,6 +318,37 @@ typedef enum VTDFaultReason { =20 #define VTD_CONTEXT_CACHE_GEN_MAX 0xffffffffUL =20 +/* VCCAP_REG */ +#define VTD_VCCAP_PAS (1UL << 0) + +/* + * The basic idea is to let hypervisor to set a range for available + * PASIDs for VMs. One of the reasons is PASID #0 is reserved by + * RID_PASID usage. We have no idea how many reserved PASIDs in future, + * so here just an evaluated value. Honestly, set it as "1" is enough + * at current stage. + */ +#define VTD_HPASID_MIN 1 +#define VTD_HPASID_MAX 0xFFFFF + +/* Virtual Command Register */ +enum { + VTD_VCMD_NULL_CMD =3D 0, + VTD_VCMD_ALLOC_PASID =3D 1, + VTD_VCMD_FREE_PASID =3D 2, + VTD_VCMD_CMD_NUM, +}; + +#define VTD_VCMD_CMD_MASK 0xffUL +#define VTD_VCMD_PASID_VALUE(val) (((val) >> 8) & 0xfffff) + +#define VTD_VCRSP_RSLT(val) ((val) << 8) +#define VTD_VCRSP_SC(val) (((val) & 0x3) << 1) + +#define VTD_VCMD_UNDEFINED_CMD 1ULL +#define VTD_VCMD_NO_AVAILABLE_PASID 2ULL +#define VTD_VCMD_FREE_INVALID_PASID 2ULL + /* Interrupt Entry Cache Invalidation Descriptor: VT-d 6.5.2.7. */ struct VTDInvDescIEC { uint32_t type:4; /* Should always be 0x4 */ diff --git a/hw/i386/trace-events b/hw/i386/trace-events index e48bef2..71536a7 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -51,6 +51,7 @@ vtd_reg_write_gcmd(uint32_t status, uint32_t val) "status= 0x%"PRIx32" value 0x%" vtd_reg_write_fectl(uint32_t value) "value 0x%"PRIx32 vtd_reg_write_iectl(uint32_t value) "value 0x%"PRIx32 vtd_reg_ics_clear_ip(void) "" +vtd_reg_write_vcmd(uint32_t status, uint32_t val) "status 0x%"PRIx32" valu= e 0x%"PRIx32 vtd_dmar_translate(uint8_t bus, uint8_t slot, uint8_t func, uint64_t iova,= uint64_t gpa, uint64_t mask) "dev %02x:%02x.%02x iova 0x%"PRIx64" -> gpa 0= x%"PRIx64" mask 0x%"PRIx64 vtd_dmar_enable(bool en) "enable %d" vtd_dmar_fault(uint16_t sid, int fault, uint64_t addr, bool is_write) "sid= 0x%"PRIx16" fault %d addr 0x%"PRIx64" write %d" diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index b5fefb9..42a58d6 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -42,7 +42,7 @@ #define VTD_SID_TO_BUS(sid) (((sid) >> 8) & 0xff) #define VTD_SID_TO_DEVFN(sid) ((sid) & 0xff) =20 -#define DMAR_REG_SIZE 0x230 +#define DMAR_REG_SIZE 0xF00 #define VTD_HOST_AW_39BIT 39 #define VTD_HOST_AW_48BIT 48 #define VTD_HOST_ADDRESS_WIDTH VTD_HOST_AW_39BIT @@ -118,6 +118,7 @@ struct VTDHostIOMMUContext { uint8_t devfn; HostIOMMUContext *iommu_ctx; IntelIOMMUState *iommu_state; + QLIST_ENTRY(VTDHostIOMMUContext) next; }; =20 struct VTDBus { @@ -269,6 +270,9 @@ struct IntelIOMMUState { /* list of registered notifiers */ QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers; =20 + /* list of VTDHostIOMMUContexts */ + QLIST_HEAD(, VTDHostIOMMUContext) vtd_dev_icx_list; + /* interrupt remapping */ bool intr_enabled; /* Whether guest enabled IR */ dma_addr_t intr_root; /* Interrupt remapping table pointer */ @@ -279,6 +283,10 @@ struct IntelIOMMUState { uint8_t aw_bits; /* Host/IOVA address width (in bits) */ bool dma_drain; /* Whether DMA r/w draining enabled */ =20 + /* Virtual Command Register */ + uint64_t vccap; /* The value of vcmd capability reg */ + uint64_t vcrsp; /* Current value of VCMD RSP REG */ + /* * iommu_lock protects below: * - per-IOMMU IOTLB caches --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542265; cv=none; d=zohomail.com; s=zohoarc; b=RRJiFPFUvfBwxs6aWtubEuUkE5pdh38GPCRaS5j0HxPwA0MZ0fGAoKlSkkOXOkSC/Xr55vZ4M6r2KWrKkQ/lPZs6vfXd/pztCKu0OE+ImY9Q/B1zaR7w1Bd0PYKYeu06jtcIlDRNbgQ0ES65Y6pp8PS3pCMNcwvcEq6d3rWu56c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542265; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=NZe4XvYaRf9N8XNfoA00RH4uWG6rNXy/0kvoku285do=; b=cZQo/qhcwe7bvjHO0ZOP17DxJww72eV8sH2oRK+TOfpl07ImZjGGzU22oiuCQabPw3ySIDXmC2rskjdJk4ZWQM3ehP1B8UfM/2pwN0UQoItkEzZHHI9DIgPPS6/psepbeyWPTWEFpHifoONY8BjMNrUVyX8iI6nNdV6zbivCsAc= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542265191901.5190601331233; Sun, 29 Mar 2020 21:24:25 -0700 (PDT) Received: from localhost ([::1]:44686 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlyJ-0001H4-SD for importer@patchew.org; Mon, 30 Mar 2020 00:24:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37900) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltd-0001FK-TS for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltc-00080g-E0 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:33 -0400 Received: from mga07.intel.com ([134.134.136.100]:12573) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltc-0007o2-5n for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:32 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:15 -0700 IronPort-SDR: wcZpse9GLkXlngo5afC5DJl0IQgK0U+7Pgb3R8XlIS3aGXAc0OMJwX6Fzp3/RXejCPFC8tJP8M zTaGjJTHfU9A== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: LtkkblB0r6THgcsyHMv1gPxEJeZRwAQkWucJcGrEzgpI3Yzp5P2cR3qDC+up5PkANWAnf6Ara5 nYqcsciPnJ0g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632045" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 12/22] intel_iommu: process PASID cache invalidation Date: Sun, 29 Mar 2020 21:24:51 -0700 Message-Id: <1585542301-84087-13-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds PASID cache invalidation handling. When guest enabled PASID usages (e.g. SVA), guest software should issue a proper PASID cache invalidation when caching-mode is exposed. This patch only adds the draft handling of pasid cache invalidation. Detailed handling will be added in subsequent patches. v1 -> v2: remove vtd_pasid_cache_gsi(), vtd_pasid_cache_psi() and vtd_pasid_cache_dsi() Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Reviewed-by: Peter Xu Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 40 +++++++++++++++++++++++++++++++++++---= -- hw/i386/intel_iommu_internal.h | 12 ++++++++++++ hw/i386/trace-events | 3 +++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 6c3159f..2eb60c3 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -2395,6 +2395,37 @@ static bool vtd_process_iotlb_desc(IntelIOMMUState *= s, VTDInvDesc *inv_desc) return true; } =20 +static bool vtd_process_pasid_desc(IntelIOMMUState *s, + VTDInvDesc *inv_desc) +{ + if ((inv_desc->val[0] & VTD_INV_DESC_PASIDC_RSVD_VAL0) || + (inv_desc->val[1] & VTD_INV_DESC_PASIDC_RSVD_VAL1) || + (inv_desc->val[2] & VTD_INV_DESC_PASIDC_RSVD_VAL2) || + (inv_desc->val[3] & VTD_INV_DESC_PASIDC_RSVD_VAL3)) { + error_report_once("non-zero-field-in-pc_inv_desc hi: 0x%" PRIx64 + " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]); + return false; + } + + switch (inv_desc->val[0] & VTD_INV_DESC_PASIDC_G) { + case VTD_INV_DESC_PASIDC_DSI: + break; + + case VTD_INV_DESC_PASIDC_PASID_SI: + break; + + case VTD_INV_DESC_PASIDC_GLOBAL: + break; + + default: + error_report_once("invalid-inv-granu-in-pc_inv_desc hi: 0x%" PRIx64 + " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]); + return false; + } + + return true; +} + static bool vtd_process_inv_iec_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) { @@ -2501,12 +2532,11 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s) } break; =20 - /* - * TODO: the entity of below two cases will be implemented in future s= eries. - * To make guest (which integrates scalable mode support patch set in - * iommu driver) work, just return true is enough so far. - */ case VTD_INV_DESC_PC: + trace_vtd_inv_desc("pasid-cache", inv_desc.val[1], inv_desc.val[0]= ); + if (!vtd_process_pasid_desc(s, &inv_desc)) { + return false; + } break; =20 case VTD_INV_DESC_PIOTLB: diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 3fc83f1..9a76f20 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -444,6 +444,18 @@ typedef union VTDInvDesc VTDInvDesc; (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM))= : \ (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM)) =20 +#define VTD_INV_DESC_PASIDC_G (3ULL << 4) +#define VTD_INV_DESC_PASIDC_PASID(val) (((val) >> 32) & 0xfffffULL) +#define VTD_INV_DESC_PASIDC_DID(val) (((val) >> 16) & VTD_DOMAIN_ID_MASK) +#define VTD_INV_DESC_PASIDC_RSVD_VAL0 0xfff000000000ffc0ULL +#define VTD_INV_DESC_PASIDC_RSVD_VAL1 0xffffffffffffffffULL +#define VTD_INV_DESC_PASIDC_RSVD_VAL2 0xffffffffffffffffULL +#define VTD_INV_DESC_PASIDC_RSVD_VAL3 0xffffffffffffffffULL + +#define VTD_INV_DESC_PASIDC_DSI (0ULL << 4) +#define VTD_INV_DESC_PASIDC_PASID_SI (1ULL << 4) +#define VTD_INV_DESC_PASIDC_GLOBAL (3ULL << 4) + /* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo { uint16_t domain_id; diff --git a/hw/i386/trace-events b/hw/i386/trace-events index 71536a7..f7cd4e5 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -22,6 +22,9 @@ vtd_inv_qi_head(uint16_t head) "read head %d" vtd_inv_qi_tail(uint16_t head) "write tail %d" vtd_inv_qi_fetch(void) "" vtd_context_cache_reset(void) "" +vtd_pasid_cache_gsi(void) "" +vtd_pasid_cache_dsi(uint16_t domain) "Domian slective PC invalidation doma= in 0x%"PRIx16 +vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID slective PC in= validation domain 0x%"PRIx16" pasid 0x%"PRIx32 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present" vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8"= devfn %"PRIu8" not present" vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t d= omain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" d= omain 0x%"PRIx16 --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542687; cv=none; d=zohomail.com; s=zohoarc; b=f2KeJTvpBJ7bPe0976AOSpTNaxt4dd3L+LAfNjsCv2mJ5eeHen2yQ8YfmsBVspLLOLA77tZbkh+CRWpSJ3JpBV1+b1p7xIc/TiyZkAfqCWSYxqJprelAQpji6YrmTSsxWpbzfKH7g17Jnm0onCnLDmg5uyMIhBkkG1jq71ONWU4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542687; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=zyUp4Zs2FAJ/wTGCewpw3nUodCeuxfUXZmK6XC10cNM=; b=OXQFUNGIXUqAgwdnIOiNuaqtqaTv6fgT+f2OvMuxD39LtAFGIXKbhK7zFp+9+5gT68stsGJU82v+tsEc+RbbhPLorIU+2v0oV002ao1uJkpXeD/8mPNbwMFg0zWcZC8VlX/ju6JLp8Dwe146BUDTWq4OOT7Jf6N3ifwk8Fdkbdo= ARC-Authentication-Results: i=1; mx.zohomail.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=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 158554268775311.366818899481814; Sun, 29 Mar 2020 21:31:27 -0700 (PDT) Received: from localhost ([::1]:44808 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm58-0003nv-Am for importer@patchew.org; Mon, 30 Mar 2020 00:31:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37916) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltf-0001JU-VU for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltc-00080m-FV for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:35 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltc-0007lr-6c for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:32 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: emKSZe4H6mHf7KMFrLsENy96o7eXn9KkAQWz+k8OHIdCQ+ajh1IMlq3XYoER3h84LdbGTJ8lOs tr2g8/A+e3JA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: nfSsR5wJg5MyfoPwUiZ1NK43JOJCF7ODD6dG9RlWslyR5raCezwTxL+T6OhA8Yjf5m4QMXZGuR WfR51wjkZeKA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632051" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 13/22] intel_iommu: add PASID cache management infrastructure Date: Sun, 29 Mar 2020 21:24:52 -0700 Message-Id: <1585542301-84087-14-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds a PASID cache management infrastructure based on new added structure VTDPASIDAddressSpace, which is used to track the PASID usage and future PASID tagged DMA address translation support in vIOMMU. struct VTDPASIDAddressSpace { VTDBus *vtd_bus; uint8_t devfn; AddressSpace as; uint32_t pasid; IntelIOMMUState *iommu_state; VTDContextCacheEntry context_cache_entry; QLIST_ENTRY(VTDPASIDAddressSpace) next; VTDPASIDCacheEntry pasid_cache_entry; }; Ideally, a VTDPASIDAddressSpace instance is created when a PASID is bound with a DMA AddressSpace. Intel VT-d spec requires guest software to issue pasid cache invalidation when bind or unbind a pasid with an address space under caching-mode. However, as VTDPASIDAddressSpace instances also act as pasid cache in this implementation, its creation also happens during vIOMMU PASID tagged DMA translation. The creation in this path will not be added in this patch since no PASID-capable emulated devices for now. The implementation in this patch manages VTDPASIDAddressSpace instances per PASID+BDF (lookup and insert will use PASID and BDF) since Intel VT-d spec allows per-BDF PASID Table. When a guest bind a PASID with an AddressSpace, QEMU will capture the guest pasid selective pasid cache invalidation, and allocate remove a VTDPASIDAddressSpace instance per the invalidation reasons: *) a present pasid entry moved to non-present *) a present pasid entry to be a present entry *) a non-present pasid entry moved to present vIOMMU emulator could figure out the reason by fetching latest guest pasid entry. v1 -> v2: - merged this patch with former replay binding patch, makes PSI/DSI/GSI use the unified function to do cache invalidation and pasid binding replay. - dropped pasid_cache_gen in both iommu_state and vtd_pasid_as as it is not necessary so far, we may want it when one day initroduce emulated SVA-capable device. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 473 +++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 18 ++ hw/i386/trace-events | 1 + include/hw/i386/intel_iommu.h | 24 +++ 4 files changed, 516 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 2eb60c3..a7e9973 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -40,6 +40,7 @@ #include "kvm_i386.h" #include "migration/vmstate.h" #include "trace.h" +#include "qemu/jhash.h" =20 /* context entry operations */ #define VTD_CE_GET_RID2PASID(ce) \ @@ -65,6 +66,8 @@ static void vtd_address_space_refresh_all(IntelIOMMUState *s); static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n); =20 +static void vtd_pasid_cache_reset(IntelIOMMUState *s); + static void vtd_panic_require_caching_mode(void) { error_report("We need to set caching-mode=3Don for intel-iommu to enab= le " @@ -276,6 +279,7 @@ static void vtd_reset_caches(IntelIOMMUState *s) vtd_iommu_lock(s); vtd_reset_iotlb_locked(s); vtd_reset_context_cache_locked(s); + vtd_pasid_cache_reset(s); vtd_iommu_unlock(s); } =20 @@ -686,6 +690,16 @@ static inline bool vtd_pe_type_check(X86IOMMUState *x8= 6_iommu, return true; } =20 +static inline uint16_t vtd_pe_get_domain_id(VTDPASIDEntry *pe) +{ + return VTD_SM_PASID_ENTRY_DID((pe)->val[1]); +} + +static inline uint32_t vtd_sm_ce_get_pdt_entry_num(VTDContextEntry *ce) +{ + return 1U << (VTD_SM_CONTEXT_ENTRY_PDTS(ce->val[0]) + 7); +} + static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire) { return pdire->val & 1; @@ -2395,9 +2409,452 @@ static bool vtd_process_iotlb_desc(IntelIOMMUState = *s, VTDInvDesc *inv_desc) return true; } =20 +static inline void vtd_init_pasid_key(uint32_t pasid, + uint16_t sid, + struct pasid_key *key) +{ + key->pasid =3D pasid; + key->sid =3D sid; +} + +static guint vtd_pasid_as_key_hash(gconstpointer v) +{ + struct pasid_key *key =3D (struct pasid_key *)v; + uint32_t a, b, c; + + /* Jenkins hash */ + a =3D b =3D c =3D JHASH_INITVAL + sizeof(*key); + a +=3D key->sid; + b +=3D extract32(key->pasid, 0, 16); + c +=3D extract32(key->pasid, 16, 16); + + __jhash_mix(a, b, c); + __jhash_final(a, b, c); + + return c; +} + +static gboolean vtd_pasid_as_key_equal(gconstpointer v1, gconstpointer v2) +{ + const struct pasid_key *k1 =3D v1; + const struct pasid_key *k2 =3D v2; + + return (k1->pasid =3D=3D k2->pasid) && (k1->sid =3D=3D k2->sid); +} + +static inline int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, + uint8_t bus_num, + uint8_t devfn, + uint32_t pasid, + VTDPASIDEntry *pe) +{ + VTDContextEntry ce; + int ret; + dma_addr_t pasid_dir_base; + + if (!s->root_scalable) { + return -VTD_FR_PASID_TABLE_INV; + } + + ret =3D vtd_dev_to_context_entry(s, bus_num, devfn, &ce); + if (ret) { + return ret; + } + + pasid_dir_base =3D VTD_CE_GET_PASID_DIR_TABLE(&ce); + ret =3D vtd_get_pe_from_pasid_table(s, + pasid_dir_base, pasid, pe); + + return ret; +} + +static bool vtd_pasid_entry_compare(VTDPASIDEntry *p1, VTDPASIDEntry *p2) +{ + return !memcmp(p1, p2, sizeof(*p1)); +} + +/** + * This function fills in the pasid entry in &vtd_pasid_as. Caller + * of this function should hold iommu_lock. + */ +static void vtd_fill_pe_in_cache(IntelIOMMUState *s, + VTDPASIDAddressSpace *vtd_pasid_as, + VTDPASIDEntry *pe) +{ + VTDPASIDCacheEntry *pc_entry =3D &vtd_pasid_as->pasid_cache_entry; + + if (vtd_pasid_entry_compare(pe, &pc_entry->pasid_entry)) { + /* No need to go further as cached pasid entry is latest */ + return; + } + + pc_entry->pasid_entry =3D *pe; + /* + * TODO: + * - send pasid bind to host for passthru devices + */ +} + +/** + * This function is used to clear cached pasid entry in vtd_pasid_as + * instances. Caller of this function should hold iommu_lock. + */ +static gboolean vtd_flush_pasid(gpointer key, gpointer value, + gpointer user_data) +{ + VTDPASIDCacheInfo *pc_info =3D user_data; + VTDPASIDAddressSpace *vtd_pasid_as =3D value; + IntelIOMMUState *s =3D vtd_pasid_as->iommu_state; + VTDPASIDCacheEntry *pc_entry =3D &vtd_pasid_as->pasid_cache_entry; + VTDBus *vtd_bus =3D vtd_pasid_as->vtd_bus; + VTDPASIDEntry pe; + uint16_t did; + uint32_t pasid; + uint16_t devfn; + int ret; + + did =3D vtd_pe_get_domain_id(&pc_entry->pasid_entry); + pasid =3D vtd_pasid_as->pasid; + devfn =3D vtd_pasid_as->devfn; + + switch (pc_info->flags & VTD_PASID_CACHE_INFO_MASK) { + case VTD_PASID_CACHE_FORCE_RESET: + goto remove; + case VTD_PASID_CACHE_PASIDSI: + if (pc_info->pasid !=3D pasid) { + return false; + } + /* Fall through */ + case VTD_PASID_CACHE_DOMSI: + if (pc_info->domain_id !=3D did) { + return false; + } + /* Fall through */ + case VTD_PASID_CACHE_GLOBAL: + break; + default: + error_report("invalid pc_info->flags"); + abort(); + } + + /* + * pasid cache invalidation may indicate a present pasid + * entry to present pasid entry modification. To cover such + * case, vIOMMU emulator needs to fetch latest guest pasid + * entry and check cached pasid entry, then update pasid + * cache and send pasid bind/unbind to host properly. + */ + ret =3D vtd_dev_get_pe_from_pasid(s, pci_bus_num(vtd_bus->bus), + devfn, pasid, &pe); + if (ret) { + /* + * No valid pasid entry in guest memory. e.g. pasid entry + * was modified to be either all-zero or non-present. Either + * case means existing pasid cache should be removed. + */ + goto remove; + } + + vtd_fill_pe_in_cache(s, vtd_pasid_as, &pe); + /* + * TODO: + * - when pasid-base-iotlb(piotlb) infrastructure is ready, + * should invalidate QEMU piotlb togehter with this change. + */ + return false; +remove: + /* + * TODO: + * - send pasid bind to host for passthru devices + * - when pasid-base-iotlb(piotlb) infrastructure is ready, + * should invalidate QEMU piotlb togehter with this change. + */ + return true; +} + +/** + * This function finds or adds a VTDPASIDAddressSpace for a device + * when it is bound to a pasid. Caller of this function should hold + * iommu_lock. + */ +static VTDPASIDAddressSpace *vtd_add_find_pasid_as(IntelIOMMUState *s, + VTDBus *vtd_bus, + int devfn, + uint32_t pasid) +{ + struct pasid_key key; + struct pasid_key *new_key; + VTDPASIDAddressSpace *vtd_pasid_as; + uint16_t sid; + + sid =3D vtd_make_source_id(pci_bus_num(vtd_bus->bus), devfn); + vtd_init_pasid_key(pasid, sid, &key); + vtd_pasid_as =3D g_hash_table_lookup(s->vtd_pasid_as, &key); + + if (!vtd_pasid_as) { + new_key =3D g_malloc0(sizeof(*new_key)); + vtd_init_pasid_key(pasid, sid, new_key); + /* + * Initiate the vtd_pasid_as structure. + * + * This structure here is used to track the guest pasid + * binding and also serves as pasid-cache mangement entry. + * + * TODO: in future, if wants to support the SVA-aware DMA + * emulation, the vtd_pasid_as should have include + * AddressSpace to support DMA emulation. + */ + vtd_pasid_as =3D g_malloc0(sizeof(VTDPASIDAddressSpace)); + vtd_pasid_as->iommu_state =3D s; + vtd_pasid_as->vtd_bus =3D vtd_bus; + vtd_pasid_as->devfn =3D devfn; + vtd_pasid_as->pasid =3D pasid; + g_hash_table_insert(s->vtd_pasid_as, new_key, vtd_pasid_as); + } + return vtd_pasid_as; +} + +/** + * Constant information used during pasid table walk + @vtd_bus, @devfn: device info + * @flags: indicates if it is domain selective walk + * @did: domain ID of the pasid table walk + */ +typedef struct { + VTDBus *vtd_bus; + uint16_t devfn; +#define VTD_PASID_TABLE_DID_SEL_WALK (1ULL << 0) + uint32_t flags; + uint16_t did; +} vtd_pasid_table_walk_info; + +/** + * Caller of this function should hold iommu_lock. + */ +static void vtd_sm_pasid_table_walk_one(IntelIOMMUState *s, + dma_addr_t pt_base, + int start, + int end, + vtd_pasid_table_walk_info *info) +{ + VTDPASIDEntry pe; + int pasid =3D start; + int pasid_next; + VTDPASIDAddressSpace *vtd_pasid_as; + + while (pasid < end) { + pasid_next =3D pasid + 1; + + if (!vtd_get_pe_in_pasid_leaf_table(s, pasid, pt_base, &pe) + && vtd_pe_present(&pe)) { + vtd_pasid_as =3D vtd_add_find_pasid_as(s, + info->vtd_bus, info->devfn, pasid); + if ((info->flags & VTD_PASID_TABLE_DID_SEL_WALK) && + !(info->did =3D=3D vtd_pe_get_domain_id(&pe))) { + pasid =3D pasid_next; + continue; + } + vtd_fill_pe_in_cache(s, vtd_pasid_as, &pe); + } + pasid =3D pasid_next; + } +} + +/* + * Currently, VT-d scalable mode pasid table is a two level table, + * this function aims to loop a range of PASIDs in a given pasid + * table to identify the pasid config in guest. + * Caller of this function should hold iommu_lock. + */ +static void vtd_sm_pasid_table_walk(IntelIOMMUState *s, + dma_addr_t pdt_base, + int start, + int end, + vtd_pasid_table_walk_info *info) +{ + VTDPASIDDirEntry pdire; + int pasid =3D start; + int pasid_next; + dma_addr_t pt_base; + + while (pasid < end) { + pasid_next =3D ((end - pasid) > VTD_PASID_TBL_ENTRY_NUM) ? + (pasid + VTD_PASID_TBL_ENTRY_NUM) : end; + if (!vtd_get_pdire_from_pdir_table(pdt_base, pasid, &pdire) + && vtd_pdire_present(&pdire)) { + pt_base =3D pdire.val & VTD_PASID_TABLE_BASE_ADDR_MASK; + vtd_sm_pasid_table_walk_one(s, pt_base, pasid, pasid_next, inf= o); + } + pasid =3D pasid_next; + } +} + +static void vtd_replay_pasid_bind_for_dev(IntelIOMMUState *s, + int start, int end, + vtd_pasid_table_walk_info *info) +{ + VTDContextEntry ce; + int bus_n, devfn; + + bus_n =3D pci_bus_num(info->vtd_bus->bus); + devfn =3D info->devfn; + + if (!vtd_dev_to_context_entry(s, bus_n, devfn, &ce)) { + uint32_t max_pasid; + + max_pasid =3D vtd_sm_ce_get_pdt_entry_num(&ce) * VTD_PASID_TBL_ENT= RY_NUM; + if (end > max_pasid) { + end =3D max_pasid; + } + vtd_sm_pasid_table_walk(s, + VTD_CE_GET_PASID_DIR_TABLE(&ce), + start, + end, + info); + } +} + +/** + * This function replay the guest pasid bindings to hots by + * walking the guest PASID table. This ensures host will have + * latest guest pasid bindings. Caller should hold iommu_lock. + */ +static void vtd_replay_guest_pasid_bindings(IntelIOMMUState *s, + VTDPASIDCacheInfo *pc_info) +{ + VTDHostIOMMUContext *vtd_dev_icx; + int start =3D 0, end =3D VTD_HPASID_MAX; + vtd_pasid_table_walk_info walk_info =3D {.flags =3D 0}; + + switch (pc_info->flags & VTD_PASID_CACHE_INFO_MASK) { + case VTD_PASID_CACHE_PASIDSI: + start =3D pc_info->pasid; + end =3D pc_info->pasid + 1; + /* + * PASID selective invalidation is within domain, + * thus fall through. + */ + case VTD_PASID_CACHE_DOMSI: + walk_info.did =3D pc_info->domain_id; + walk_info.flags |=3D VTD_PASID_TABLE_DID_SEL_WALK; + /* loop all assigned devices */ + break; + case VTD_PASID_CACHE_FORCE_RESET: + /* For force reset, no need to go further replay */ + return; + case VTD_PASID_CACHE_GLOBAL: + break; + default: + error_report("%s, invalid pc_info->flags", __func__); + abort(); + } + + /* + * In this replay, only needs to care about the devices which + * are backed by host IOMMU. For such devices, their vtd_dev_icx + * instances are in the s->vtd_dev_icx_list. For devices which + * are not backed byhost IOMMU, it is not necessary to replay + * the bindings since their cache could be re-created in the future + * DMA address transaltion. + */ + QLIST_FOREACH(vtd_dev_icx, &s->vtd_dev_icx_list, next) { + walk_info.vtd_bus =3D vtd_dev_icx->vtd_bus; + walk_info.devfn =3D vtd_dev_icx->devfn; + vtd_replay_pasid_bind_for_dev(s, start, end, &walk_info); + } +} + +/** + * This function syncs the pasid bindings between guest and host. + * It includes updating the pasid cache in vIOMMU and updating the + * pasid bindings per guest's latest pasid entry presence. + */ +static void vtd_pasid_cache_sync(IntelIOMMUState *s, + VTDPASIDCacheInfo *pc_info) +{ + /* + * Regards to a pasid cache invalidation, e.g. a PSI. + * it could be either cases of below: + * a) a present pasid entry moved to non-present + * b) a present pasid entry to be a present entry + * c) a non-present pasid entry moved to present + * + * Different invalidation granularity may affect different device + * scope and pasid scope. But for each invalidation granularity, + * it needs to do two steps to sync host and guest pasid binding. + * + * Here is the handling of a PSI: + * 1) loop all the existing vtd_pasid_as instances to update them + * according to the latest guest pasid entry in pasid table. + * this will make sure affected existing vtd_pasid_as instances + * cached the latest pasid entries. Also, during the loop, the + * host should be notified if needed. e.g. pasid unbind or pasid + * update. Should be able to cover case a) and case b). + * + * 2) loop all devices to cover case c) + * - For devices which have HostIOMMUContext instances, + * we loop them and check if guest pasid entry exists. If yes, + * it is case c), we update the pasid cache and also notify + * host. + * - For devices which have no HostIOMMUContext, it is not + * necessary to create pasid cache at this phase since it + * could be created when vIOMMU does DMA address translation. + * This is not yet implemented since there is no emulated + * pasid-capable devices today. If we have such devices in + * future, the pasid cache shall be created there. + * Other granularity follow the same steps, just with different scope + * + */ + + vtd_iommu_lock(s); + /* Step 1: loop all the exisitng vtd_pasid_as instances */ + g_hash_table_foreach_remove(s->vtd_pasid_as, + vtd_flush_pasid, pc_info); + + /* + * Step 2: loop all the exisitng vtd_dev_icx instances. + * Ideally, needs to loop all devices to find if there is any new + * PASID binding regards to the PASID cache invalidation request. + * But it is enough to loop the devices which are backed by host + * IOMMU. For devices backed by vIOMMU (a.k.a emulated devices), + * if new PASID happened on them, their vtd_pasid_as instance could + * be created during future vIOMMU DMA translation. + */ + vtd_replay_guest_pasid_bindings(s, pc_info); + vtd_iommu_unlock(s); +} + +/** + * Caller of this function should hold iommu_lock + */ +static void vtd_pasid_cache_reset(IntelIOMMUState *s) +{ + VTDPASIDCacheInfo pc_info; + + trace_vtd_pasid_cache_reset(); + + pc_info.flags =3D VTD_PASID_CACHE_FORCE_RESET; + + /* + * Reset pasid cache is a big hammer, so use + * g_hash_table_foreach_remove which will free + * the vtd_pasid_as instances. Also, as a big + * hammer, use VTD_PASID_CACHE_FORCE_RESET to + * ensure all the vtd_pasid_as instances are + * dropped, meanwhile the change will be pass + * to host if HostIOMMUContext is available. + */ + g_hash_table_foreach_remove(s->vtd_pasid_as, + vtd_flush_pasid, &pc_info); +} + static bool vtd_process_pasid_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) { + uint16_t domain_id; + uint32_t pasid; + VTDPASIDCacheInfo pc_info; + if ((inv_desc->val[0] & VTD_INV_DESC_PASIDC_RSVD_VAL0) || (inv_desc->val[1] & VTD_INV_DESC_PASIDC_RSVD_VAL1) || (inv_desc->val[2] & VTD_INV_DESC_PASIDC_RSVD_VAL2) || @@ -2407,14 +2864,26 @@ static bool vtd_process_pasid_desc(IntelIOMMUState = *s, return false; } =20 + domain_id =3D VTD_INV_DESC_PASIDC_DID(inv_desc->val[0]); + pasid =3D VTD_INV_DESC_PASIDC_PASID(inv_desc->val[0]); + switch (inv_desc->val[0] & VTD_INV_DESC_PASIDC_G) { case VTD_INV_DESC_PASIDC_DSI: + trace_vtd_pasid_cache_dsi(domain_id); + pc_info.flags =3D VTD_PASID_CACHE_DOMSI; + pc_info.domain_id =3D domain_id; break; =20 case VTD_INV_DESC_PASIDC_PASID_SI: + /* PASID selective implies a DID selective */ + pc_info.flags =3D VTD_PASID_CACHE_PASIDSI; + pc_info.domain_id =3D domain_id; + pc_info.pasid =3D pasid; break; =20 case VTD_INV_DESC_PASIDC_GLOBAL: + trace_vtd_pasid_cache_gsi(); + pc_info.flags =3D VTD_PASID_CACHE_GLOBAL; break; =20 default: @@ -2423,6 +2892,7 @@ static bool vtd_process_pasid_desc(IntelIOMMUState *s, return false; } =20 + vtd_pasid_cache_sync(s, &pc_info); return true; } =20 @@ -4085,6 +4555,9 @@ static void vtd_realize(DeviceState *dev, Error **err= p) g_free, g_free); s->vtd_as_by_busptr =3D g_hash_table_new_full(vtd_uint64_hash, vtd_uin= t64_equal, g_free, g_free); + s->vtd_pasid_as =3D g_hash_table_new_full(vtd_pasid_as_key_hash, + vtd_pasid_as_key_equal, + g_free, g_free); vtd_init(s); sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR); pci_setup_iommu(bus, &vtd_iommu_ops, dev); diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 9a76f20..451ef4c 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -307,6 +307,7 @@ typedef enum VTDFaultReason { VTD_FR_IR_SID_ERR =3D 0x26, /* Invalid Source-ID */ =20 VTD_FR_PASID_TABLE_INV =3D 0x58, /*Invalid PASID table entry */ + VTD_FR_PASID_ENTRY_P =3D 0x59, /* The Present(P) field of pasidt-entry= is 0 */ =20 /* This is not a normal fault reason. We use this to indicate some fau= lts * that are not referenced by the VT-d specification. @@ -511,10 +512,26 @@ typedef struct VTDRootEntry VTDRootEntry; #define VTD_CTX_ENTRY_LEGACY_SIZE 16 #define VTD_CTX_ENTRY_SCALABLE_SIZE 32 =20 +#define VTD_SM_CONTEXT_ENTRY_PDTS(val) (((val) >> 9) & 0x3) #define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw) (0x1e0ULL | ~VTD_HAW_MASK(aw)) #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL =20 +struct VTDPASIDCacheInfo { +#define VTD_PASID_CACHE_FORCE_RESET (1ULL << 0) +#define VTD_PASID_CACHE_GLOBAL (1ULL << 1) +#define VTD_PASID_CACHE_DOMSI (1ULL << 2) +#define VTD_PASID_CACHE_PASIDSI (1ULL << 3) + uint32_t flags; + uint16_t domain_id; + uint32_t pasid; +}; +#define VTD_PASID_CACHE_INFO_MASK (VTD_PASID_CACHE_FORCE_RESET | \ + VTD_PASID_CACHE_GLOBAL | \ + VTD_PASID_CACHE_DOMSI | \ + VTD_PASID_CACHE_PASIDSI) +typedef struct VTDPASIDCacheInfo VTDPASIDCacheInfo; + /* PASID Table Related Definitions */ #define VTD_PASID_DIR_BASE_ADDR_MASK (~0xfffULL) #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL) @@ -526,6 +543,7 @@ typedef struct VTDRootEntry VTDRootEntry; #define VTD_PASID_TABLE_BITS_MASK (0x3fULL) #define VTD_PASID_TABLE_INDEX(pasid) ((pasid) & VTD_PASID_TABLE_BITS_MASK) #define VTD_PASID_ENTRY_FPD (1ULL << 1) /* Fault Processing Disa= ble */ +#define VTD_PASID_TBL_ENTRY_NUM (1ULL << 6) =20 /* PASID Granular Translation Type Mask */ #define VTD_PASID_ENTRY_P 1ULL diff --git a/hw/i386/trace-events b/hw/i386/trace-events index f7cd4e5..60d20c1 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -23,6 +23,7 @@ vtd_inv_qi_tail(uint16_t head) "write tail %d" vtd_inv_qi_fetch(void) "" vtd_context_cache_reset(void) "" vtd_pasid_cache_gsi(void) "" +vtd_pasid_cache_reset(void) "" vtd_pasid_cache_dsi(uint16_t domain) "Domian slective PC invalidation doma= in 0x%"PRIx16 vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID slective PC in= validation domain 0x%"PRIx16" pasid 0x%"PRIx32 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present" diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 42a58d6..626c1cd 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -65,6 +65,8 @@ typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress; typedef struct VTDPASIDDirEntry VTDPASIDDirEntry; typedef struct VTDPASIDEntry VTDPASIDEntry; typedef struct VTDHostIOMMUContext VTDHostIOMMUContext; +typedef struct VTDPASIDCacheEntry VTDPASIDCacheEntry; +typedef struct VTDPASIDAddressSpace VTDPASIDAddressSpace; =20 /* Context-Entry */ struct VTDContextEntry { @@ -97,6 +99,26 @@ struct VTDPASIDEntry { uint64_t val[8]; }; =20 +struct pasid_key { + uint32_t pasid; + uint16_t sid; +}; + +struct VTDPASIDCacheEntry { + struct VTDPASIDEntry pasid_entry; +}; + +struct VTDPASIDAddressSpace { + VTDBus *vtd_bus; + uint8_t devfn; + AddressSpace as; + uint32_t pasid; + IntelIOMMUState *iommu_state; + VTDContextCacheEntry context_cache_entry; + QLIST_ENTRY(VTDPASIDAddressSpace) next; + VTDPASIDCacheEntry pasid_cache_entry; +}; + struct VTDAddressSpace { PCIBus *bus; uint8_t devfn; @@ -267,6 +289,7 @@ struct IntelIOMMUState { =20 GHashTable *vtd_as_by_busptr; /* VTDBus objects indexed by PCIBus* r= eference */ VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX]; /* VTDBus objects indexed = by bus number */ + GHashTable *vtd_pasid_as; /* VTDPASIDAddressSpace instances */ /* list of registered notifiers */ QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers; =20 @@ -292,6 +315,7 @@ struct IntelIOMMUState { * - per-IOMMU IOTLB caches * - context entry cache in VTDAddressSpace * - HostIOMMUContext pointer cached in vIOMMU + * - PASID cache in VTDPASIDAddressSpace */ QemuMutex iommu_lock; }; --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542404; cv=none; d=zohomail.com; s=zohoarc; b=clHxuCX05Dj7nc+ZpQCniEeJnGLtHYQ/bqWiZe6eY7XKFK1Ui4KGoQZDsabkuLl27iMMnSfLzNtWeFsgnGSX5FUyFmhTsXbcepGXcVRDMG81sKkqdllaMsMzMNWCGDy5xxuXkol6+vj4j1d3o1TQhfiB+s87AQbesNDcHSlYCR4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542404; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=WguGwTvWOOPI9cTQ95Vb0FkSzDfnuhffIFGk6VniAag=; b=gGDg6eU4g2Kjc9x2+qRA68Lv/HpqeoImCh1tvDlqlIAqEy+m+xhStIqxa7Qgez83UxyXY/G0+picEI3HXAh/l9gIRS7ZSj3tbtfHFvKa7VKKJ6JDsolP6+CPjCi1rDh6Lf/od3lLc7Ha6vXTtFeOOJZD1FrHvmkKnvH7CdqOynw= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542404559145.59756375509687; Sun, 29 Mar 2020 21:26:44 -0700 (PDT) Received: from localhost ([::1]:44740 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm0Z-0006HA-A4 for importer@patchew.org; Mon, 30 Mar 2020 00:26:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37937) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlto-0001eK-7E for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltm-0008A1-IS for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:44 -0400 Received: from mga07.intel.com ([134.134.136.100]:12574) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltm-0007ph-6q for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:42 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: 6mPZk9vo915ScVXfKOGzYBg4vLGsIFihdXSeOprtHEbw2DnyJ+p86RHzpGlsInd7dirpLCB+FO mer9M0RnjjQA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: GbJrcBr/HNsaPw+ZZqLIV7AQfQvuokwoD+cJyMjbMC2nNjlXE5nLJ2ChG87se9OwxSkje+PgSN CUKpYsfRYwsg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632054" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 14/22] vfio: add bind stage-1 page table support Date: Sun, 29 Mar 2020 21:24:53 -0700 Message-Id: <1585542301-84087-15-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds bind_stage1_pgtbl() definition in HostIOMMUContextClass, also adds corresponding implementation in VFIO. This is to expose a way for vIOMMU to setup dual stage DMA translation for passthru devices on hardware. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Alex Williamson Signed-off-by: Liu Yi L --- hw/iommu/host_iommu_context.c | 47 +++++++++++++++++++++++++++++- hw/vfio/common.c | 55 +++++++++++++++++++++++++++++++= +++- include/hw/iommu/host_iommu_context.h | 26 ++++++++++++++++- 3 files changed, 125 insertions(+), 3 deletions(-) diff --git a/hw/iommu/host_iommu_context.c b/hw/iommu/host_iommu_context.c index 5fb2223..8ae20fe 100644 --- a/hw/iommu/host_iommu_context.c +++ b/hw/iommu/host_iommu_context.c @@ -69,15 +69,60 @@ int host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_c= tx, uint32_t pasid) return hicxc->pasid_free(iommu_ctx, pasid); } =20 +int host_iommu_ctx_bind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1BindData *data) +{ + HostIOMMUContextClass *hicxc; + + if (!iommu_ctx) { + return -EINVAL; + } + + hicxc =3D HOST_IOMMU_CONTEXT_GET_CLASS(iommu_ctx); + if (!hicxc) { + return -EINVAL; + } + + if (!(iommu_ctx->flags & HOST_IOMMU_NESTING) || + !hicxc->bind_stage1_pgtbl) { + return -EINVAL; + } + + return hicxc->bind_stage1_pgtbl(iommu_ctx, data); +} + +int host_iommu_ctx_unbind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1BindData *data) +{ + HostIOMMUContextClass *hicxc; + + if (!iommu_ctx) { + return -EINVAL; + } + + hicxc =3D HOST_IOMMU_CONTEXT_GET_CLASS(iommu_ctx); + if (!hicxc) { + return -EINVAL; + } + + if (!(iommu_ctx->flags & HOST_IOMMU_NESTING) || + !hicxc->unbind_stage1_pgtbl) { + return -EINVAL; + } + + return hicxc->unbind_stage1_pgtbl(iommu_ctx, data); +} + void host_iommu_ctx_init(void *_iommu_ctx, size_t instance_size, const char *mrtypename, - uint64_t flags) + uint64_t flags, uint32_t formats) { HostIOMMUContext *iommu_ctx; =20 object_initialize(_iommu_ctx, instance_size, mrtypename); iommu_ctx =3D HOST_IOMMU_CONTEXT(_iommu_ctx); iommu_ctx->flags =3D flags; + iommu_ctx->stage1_formats =3D formats; iommu_ctx->initialized =3D true; } =20 diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 44b142c..465e4d8 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1226,6 +1226,54 @@ static int vfio_host_iommu_ctx_pasid_free(HostIOMMUC= ontext *iommu_ctx, return 0; } =20 +static int vfio_host_iommu_ctx_bind_stage1_pgtbl(HostIOMMUContext *iommu_c= tx, + DualIOMMUStage1BindData *bind_da= ta) +{ + VFIOContainer *container =3D container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_bind *bind; + unsigned long argsz; + int ret =3D 0; + + argsz =3D sizeof(*bind) + sizeof(bind_data->bind_data); + bind =3D g_malloc0(argsz); + bind->argsz =3D argsz; + bind->flags =3D VFIO_IOMMU_BIND_GUEST_PGTBL; + memcpy(&bind->data, &bind_data->bind_data, sizeof(bind_data->bind_data= )); + + if (ioctl(container->fd, VFIO_IOMMU_BIND, bind)) { + ret =3D -errno; + error_report("%s: pasid (%u) bind failed: %d", + __func__, bind_data->pasid, ret); + } + g_free(bind); + return ret; +} + +static int vfio_host_iommu_ctx_unbind_stage1_pgtbl(HostIOMMUContext *iommu= _ctx, + DualIOMMUStage1BindData *bind_= data) +{ + VFIOContainer *container =3D container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_bind *bind; + unsigned long argsz; + int ret =3D 0; + + argsz =3D sizeof(*bind) + sizeof(bind_data->bind_data); + bind =3D g_malloc0(argsz); + bind->argsz =3D argsz; + bind->flags =3D VFIO_IOMMU_UNBIND_GUEST_PGTBL; + memcpy(&bind->data, &bind_data->bind_data, sizeof(bind_data->bind_data= )); + + if (ioctl(container->fd, VFIO_IOMMU_BIND, bind)) { + ret =3D -errno; + error_report("%s: pasid (%u) unbind failed: %d", + __func__, bind_data->pasid, ret); + } + g_free(bind); + return ret; +} + /** * Get iommu info from host. Caller of this funcion should free * the memory pointed by the returned pointer stored in @info @@ -1350,10 +1398,13 @@ static int vfio_init_container(VFIOContainer *conta= iner, int group_fd, =20 flags |=3D (nesting.nesting_capabilities & VFIO_IOMMU_PASID_REQS) ? HOST_IOMMU_PASID_REQUEST : 0; + flags |=3D HOST_IOMMU_NESTING; + host_iommu_ctx_init(&container->iommu_ctx, sizeof(container->iommu_ctx), TYPE_VFIO_HOST_IOMMU_CONTEXT, - flags); + flags, + nesting.stage1_formats); } =20 container->iommu_type =3D iommu_type; @@ -1945,6 +1996,8 @@ static void vfio_host_iommu_context_class_init(Object= Class *klass, =20 hicxc->pasid_alloc =3D vfio_host_iommu_ctx_pasid_alloc; hicxc->pasid_free =3D vfio_host_iommu_ctx_pasid_free; + hicxc->bind_stage1_pgtbl =3D vfio_host_iommu_ctx_bind_stage1_pgtbl; + hicxc->unbind_stage1_pgtbl =3D vfio_host_iommu_ctx_unbind_stage1_pgtbl; } =20 static const TypeInfo vfio_host_iommu_context_info =3D { diff --git a/include/hw/iommu/host_iommu_context.h b/include/hw/iommu/host_= iommu_context.h index 227c433..44daca9 100644 --- a/include/hw/iommu/host_iommu_context.h +++ b/include/hw/iommu/host_iommu_context.h @@ -41,6 +41,7 @@ TYPE_HOST_IOMMU_CONTEXT) =20 typedef struct HostIOMMUContext HostIOMMUContext; +typedef struct DualIOMMUStage1BindData DualIOMMUStage1BindData; =20 typedef struct HostIOMMUContextClass { /* private */ @@ -54,6 +55,16 @@ typedef struct HostIOMMUContextClass { /* Reclaim pasid from HostIOMMUContext (a.k.a. host software) */ int (*pasid_free)(HostIOMMUContext *iommu_ctx, uint32_t pasid); + /* + * Bind stage-1 page table to a hostIOMMU w/ dual stage + * DMA translation capability. + * @bind_data specifies the bind configurations. + */ + int (*bind_stage1_pgtbl)(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1BindData *bind_data); + /* Undo a previous bind. @bind_data specifies the unbind info. */ + int (*unbind_stage1_pgtbl)(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1BindData *bind_data); } HostIOMMUContextClass; =20 /* @@ -62,17 +73,30 @@ typedef struct HostIOMMUContextClass { struct HostIOMMUContext { Object parent_obj; #define HOST_IOMMU_PASID_REQUEST (1ULL << 0) +#define HOST_IOMMU_NESTING (1ULL << 1) uint64_t flags; + uint32_t stage1_formats; bool initialized; }; =20 +struct DualIOMMUStage1BindData { + uint32_t pasid; + union { + struct iommu_gpasid_bind_data gpasid_bind; + } bind_data; +}; + int host_iommu_ctx_pasid_alloc(HostIOMMUContext *iommu_ctx, uint32_t min, uint32_t max, uint32_t *pasid); int host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_ctx, uint32_t pasid); +int host_iommu_ctx_bind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1BindData *data); +int host_iommu_ctx_unbind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1BindData *data); =20 void host_iommu_ctx_init(void *_iommu_ctx, size_t instance_size, const char *mrtypename, - uint64_t flags); + uint64_t flags, uint32_t formats); void host_iommu_ctx_destroy(HostIOMMUContext *iommu_ctx); =20 #endif --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542208; cv=none; d=zohomail.com; s=zohoarc; b=BNeSWSz6sc4D4s1/LrwUoRqu+YPIa7gHZIJwqDS8CA0gj47+I9BNXhjyIFYaaJXgrGttmOJk767IQ8FiwyQufrruLk87uPd70kBsxMkPZNqBqkhIPNO8muyYwoWpW9zO4fBuAiVFvOe2EkvqwmDLLSVDOtsIjICEtZd6bwsXCIQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542208; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=l31OP0UoXkRH+Zg/Et5nBeJPRKxat+FgmdoMHtVX1iE=; b=eqQhDn7qq/Yj6eq3OjC18PBh+PvrQfFidRawYDHAhZRrFh4huPU5W5aKdZFP07JYM0KmPzCUTsFkS/5BA1OICG5ggTfFN6QOE2OjnCKSnh4x+TSfCq/hjVmM1s9d99PCJuvGdQGLbotB3g8HhYI007ROPQZ2TJYZCWURgHCBC+4= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542208980739.9708080777125; Sun, 29 Mar 2020 21:23:28 -0700 (PDT) Received: from localhost ([::1]:44666 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlxP-0007Rg-JW for importer@patchew.org; Mon, 30 Mar 2020 00:23:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37940) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlto-0001eY-D5 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltm-0008AN-SX for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:44 -0400 Received: from mga07.intel.com ([134.134.136.100]:12573) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltm-0007o2-Kb for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:42 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: 4eGoTEg5DOEq6xt4xjrEoJTvTvrgVZ0D7TXISoklkWsXlxik0K4NGkXDqi9GTLZ+7uVBnsC8YE HwmkK0/MaPUA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: V1/iR13NcaFa1PlQS7ZGGhNzKGcVM3O1RL+u6ldKaqXgJHgFsM8ecmTVVyQpEXR8LSA0YJ0wJG S7vZeEaIjWSQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632056" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 15/22] intel_iommu: bind/unbind guest page table to host Date: Sun, 29 Mar 2020 21:24:54 -0700 Message-Id: <1585542301-84087-16-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch captures the guest PASID table entry modifications and propagates the changes to host to setup dual stage DMA translation. The guest page table is configured as 1st level page table (GVA->GPA) whose translation result would further go through host VT-d 2nd level page table(GPA->HPA) under nested translation mode. This is the key part of vSVA support, and also a key to support IOVA over 1st- level page table for Intel VT-d in virtualization environment. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 98 ++++++++++++++++++++++++++++++++++++++= +--- hw/i386/intel_iommu_internal.h | 18 ++++++++ 2 files changed, 111 insertions(+), 5 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index a7e9973..d87f608 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -41,6 +41,7 @@ #include "migration/vmstate.h" #include "trace.h" #include "qemu/jhash.h" +#include =20 /* context entry operations */ #define VTD_CE_GET_RID2PASID(ce) \ @@ -700,6 +701,16 @@ static inline uint32_t vtd_sm_ce_get_pdt_entry_num(VTD= ContextEntry *ce) return 1U << (VTD_SM_CONTEXT_ENTRY_PDTS(ce->val[0]) + 7); } =20 +static inline uint32_t vtd_pe_get_fl_aw(VTDPASIDEntry *pe) +{ + return 48 + ((pe->val[2] >> 2) & VTD_SM_PASID_ENTRY_FLPM) * 9; +} + +static inline dma_addr_t vtd_pe_get_flpt_base(VTDPASIDEntry *pe) +{ + return pe->val[2] & VTD_SM_PASID_ENTRY_FLPTPTR; +} + static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire) { return pdire->val & 1; @@ -1861,6 +1872,82 @@ static void vtd_context_global_invalidate(IntelIOMMU= State *s) vtd_iommu_replay_all(s); } =20 +/** + * Caller should hold iommu_lock. + */ +static int vtd_bind_guest_pasid(IntelIOMMUState *s, VTDBus *vtd_bus, + int devfn, int pasid, VTDPASIDEntry *pe, + VTDPASIDOp op) +{ + VTDHostIOMMUContext *vtd_dev_icx; + HostIOMMUContext *iommu_ctx; + DualIOMMUStage1BindData *bind_data; + struct iommu_gpasid_bind_data *g_bind_data; + int ret =3D -1; + + vtd_dev_icx =3D vtd_bus->dev_icx[devfn]; + if (!vtd_dev_icx) { + /* means no need to go further, e.g. for emulated devices */ + return 0; + } + + iommu_ctx =3D vtd_dev_icx->iommu_ctx; + if (!iommu_ctx) { + return -EINVAL; + } + + if (!(iommu_ctx->stage1_formats + & IOMMU_PASID_FORMAT_INTEL_VTD)) { + error_report_once("IOMMU Stage 1 format is not compatible!\n"); + return -EINVAL; + } + + bind_data =3D g_malloc0(sizeof(*bind_data)); + bind_data->pasid =3D pasid; + g_bind_data =3D &bind_data->bind_data.gpasid_bind; + + g_bind_data->flags =3D 0; + g_bind_data->vtd.flags =3D 0; + switch (op) { + case VTD_PASID_BIND: + g_bind_data->version =3D IOMMU_UAPI_VERSION; + g_bind_data->format =3D IOMMU_PASID_FORMAT_INTEL_VTD; + g_bind_data->gpgd =3D vtd_pe_get_flpt_base(pe); + g_bind_data->addr_width =3D vtd_pe_get_fl_aw(pe); + g_bind_data->hpasid =3D pasid; + g_bind_data->gpasid =3D pasid; + g_bind_data->flags |=3D IOMMU_SVA_GPASID_VAL; + g_bind_data->vtd.flags =3D + (VTD_SM_PASID_ENTRY_SRE_BIT(pe->val[2]) ? 1 := 0) + | (VTD_SM_PASID_ENTRY_EAFE_BIT(pe->val[2]) ? 1 = : 0) + | (VTD_SM_PASID_ENTRY_PCD_BIT(pe->val[1]) ? 1 := 0) + | (VTD_SM_PASID_ENTRY_PWT_BIT(pe->val[1]) ? 1 := 0) + | (VTD_SM_PASID_ENTRY_EMTE_BIT(pe->val[1]) ? 1 = : 0) + | (VTD_SM_PASID_ENTRY_CD_BIT(pe->val[1]) ? 1 : = 0); + g_bind_data->vtd.pat =3D VTD_SM_PASID_ENTRY_PAT(pe->val[1]); + g_bind_data->vtd.emt =3D VTD_SM_PASID_ENTRY_EMT(pe->val[1]); + ret =3D host_iommu_ctx_bind_stage1_pgtbl(iommu_ctx, bind_data); + break; + case VTD_PASID_UNBIND: + g_bind_data->version =3D IOMMU_UAPI_VERSION; + g_bind_data->format =3D IOMMU_PASID_FORMAT_INTEL_VTD; + g_bind_data->gpgd =3D 0; + g_bind_data->addr_width =3D 0; + g_bind_data->hpasid =3D pasid; + g_bind_data->gpasid =3D pasid; + g_bind_data->flags |=3D IOMMU_SVA_GPASID_VAL; + ret =3D host_iommu_ctx_unbind_stage1_pgtbl(iommu_ctx, bind_data); + break; + default: + error_report_once("Unknown VTDPASIDOp!!!\n"); + break; + } + + g_free(bind_data); + + return ret; +} + /* Do a context-cache device-selective invalidation. * @func_mask: FM field after shifting */ @@ -2489,10 +2576,10 @@ static void vtd_fill_pe_in_cache(IntelIOMMUState *s, } =20 pc_entry->pasid_entry =3D *pe; - /* - * TODO: - * - send pasid bind to host for passthru devices - */ + vtd_bind_guest_pasid(s, vtd_pasid_as->vtd_bus, + vtd_pasid_as->devfn, + vtd_pasid_as->pasid, + pe, VTD_PASID_BIND); } =20 /** @@ -2565,10 +2652,11 @@ static gboolean vtd_flush_pasid(gpointer key, gpoin= ter value, remove: /* * TODO: - * - send pasid bind to host for passthru devices * - when pasid-base-iotlb(piotlb) infrastructure is ready, * should invalidate QEMU piotlb togehter with this change. */ + vtd_bind_guest_pasid(s, vtd_bus, devfn, + pasid, NULL, VTD_PASID_UNBIND); return true; } =20 diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 451ef4c..b9e48ab 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -517,6 +517,13 @@ typedef struct VTDRootEntry VTDRootEntry; #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw) (0x1e0ULL | ~VTD_HAW_MASK(aw)) #define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL =20 +enum VTDPASIDOp { + VTD_PASID_BIND, + VTD_PASID_UNBIND, + VTD_OP_NUM +}; +typedef enum VTDPASIDOp VTDPASIDOp; + struct VTDPASIDCacheInfo { #define VTD_PASID_CACHE_FORCE_RESET (1ULL << 0) #define VTD_PASID_CACHE_GLOBAL (1ULL << 1) @@ -556,6 +563,17 @@ typedef struct VTDPASIDCacheInfo VTDPASIDCacheInfo; #define VTD_SM_PASID_ENTRY_AW 7ULL /* Adjusted guest-address-widt= h */ #define VTD_SM_PASID_ENTRY_DID(val) ((val) & VTD_DOMAIN_ID_MASK) =20 +#define VTD_SM_PASID_ENTRY_FLPM 3ULL +#define VTD_SM_PASID_ENTRY_FLPTPTR (~0xfffULL) +#define VTD_SM_PASID_ENTRY_SRE_BIT(val) (!!((val) & 1ULL)) +#define VTD_SM_PASID_ENTRY_EAFE_BIT(val) (!!(((val) >> 7) & 1ULL)) +#define VTD_SM_PASID_ENTRY_PCD_BIT(val) (!!(((val) >> 31) & 1ULL)) +#define VTD_SM_PASID_ENTRY_PWT_BIT(val) (!!(((val) >> 30) & 1ULL)) +#define VTD_SM_PASID_ENTRY_EMTE_BIT(val) (!!(((val) >> 26) & 1ULL)) +#define VTD_SM_PASID_ENTRY_CD_BIT(val) (!!(((val) >> 25) & 1ULL)) +#define VTD_SM_PASID_ENTRY_PAT(val) (((val) >> 32) & 0xFFFFFFFFULL) +#define VTD_SM_PASID_ENTRY_EMT(val) (((val) >> 27) & 0x7ULL) + /* Second Level Page Translation Pointer*/ #define VTD_SM_PASID_ENTRY_SLPTPTR (~0xfffULL) =20 --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542366; cv=none; d=zohomail.com; s=zohoarc; b=DQYdPfyrPQtlgdCN2cfDKkJ2cWtCa+yBqgVe8pyvbp3t3Le8U5C5Rqrcw/xagNS5S0/TPad+6HP5YacdoVlpyCyEdR4n8xr8TcDKHUJsMMeO4vhPb7ByHO2SbLU0DGOVxQRb/VOVjXbGIl8LJyHZBYqcqI6rnfjG1GCXdl7msds= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542366; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=LQAkAWnsZ+hZDeKpa1B/umuOA5jIz5IiW9u6nYDit4Q=; b=jU3D3d8walgf9GIrbDGrArSECpEcYL4CI8nBfYZzncIwRhE2JeRJv5wpK6/CjNCZI8kTi6fzQxgc89ygsgy7Uqb1Lo0Goj//AXj0W2Ke+MVdyTKBvIhf3ueIXSVsxPPjF1K6jJzXQdYQbn+ViSlXU3L9vZIOp9r4h1MRE1/a8Zg= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542366068510.6588709752025; Sun, 29 Mar 2020 21:26:06 -0700 (PDT) Received: from localhost ([::1]:44726 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlzw-0004hz-IM for importer@patchew.org; Mon, 30 Mar 2020 00:26:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37939) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlto-0001eW-Ce for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltm-0008AT-UN for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:44 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltm-0007lr-M9 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:42 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: nXw5O4PWFORmXIlm/+zafmoP20Vx+8arb+Wm5DsR1oCJQEk0Fxcm76cirW4Z4A6yPy6e5zZqt4 beYSnnil4ZIg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: 4yOv9JYUa/5ITZ2HYkE8F2FBJWneetfk3TUVo60pcztl2lvyINcBidgkKRdu27WmJyNo3zuvmm +SW3K4lcq7pw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632059" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 16/22] intel_iommu: replay pasid binds after context cache invalidation Date: Sun, 29 Mar 2020 21:24:55 -0700 Message-Id: <1585542301-84087-17-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch replays guest pasid bindings after context cache invalidation. This is a behavior to ensure safety. Actually, programmer should issue pasid cache invalidation with proper granularity after issuing a context cache invalidation. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 51 ++++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 6 ++++- hw/i386/trace-events | 1 + 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index d87f608..883aeac 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -68,6 +68,10 @@ static void vtd_address_space_refresh_all(IntelIOMMUStat= e *s); static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n); =20 static void vtd_pasid_cache_reset(IntelIOMMUState *s); +static void vtd_pasid_cache_sync(IntelIOMMUState *s, + VTDPASIDCacheInfo *pc_info); +static void vtd_pasid_cache_devsi(IntelIOMMUState *s, + VTDBus *vtd_bus, uint16_t devfn); =20 static void vtd_panic_require_caching_mode(void) { @@ -1853,7 +1857,10 @@ static void vtd_iommu_replay_all(IntelIOMMUState *s) =20 static void vtd_context_global_invalidate(IntelIOMMUState *s) { + VTDPASIDCacheInfo pc_info; + trace_vtd_inv_desc_cc_global(); + /* Protects context cache */ vtd_iommu_lock(s); s->context_cache_gen++; @@ -1870,6 +1877,9 @@ static void vtd_context_global_invalidate(IntelIOMMUS= tate *s) * VT-d emulation codes. */ vtd_iommu_replay_all(s); + + pc_info.flags =3D VTD_PASID_CACHE_GLOBAL; + vtd_pasid_cache_sync(s, &pc_info); } =20 /** @@ -2005,6 +2015,22 @@ static void vtd_context_device_invalidate(IntelIOMMU= State *s, * happened. */ vtd_sync_shadow_page_table(vtd_as); + /* + * Per spec, context flush should also followed with PASID + * cache and iotlb flush. Regards to a device selective + * context cache invalidation: + * if (emaulted_device) + * modify the pasid cache gen and pasid-based iotlb gen + * value (will be added in following patches) + * else if (assigned_device) + * check if the device has been bound to any pasid + * invoke pasid_unbind regards to each bound pasid + * Here, we have vtd_pasid_cache_devsi() to invalidate pas= id + * caches, while for piotlb in QEMU, we don't have it yet,= so + * no handling. For assigned device, host iommu driver wou= ld + * flush piotlb when a pasid unbind is pass down to it. + */ + vtd_pasid_cache_devsi(s, vtd_bus, devfn_it); } } } @@ -2619,6 +2645,12 @@ static gboolean vtd_flush_pasid(gpointer key, gpoint= er value, /* Fall through */ case VTD_PASID_CACHE_GLOBAL: break; + case VTD_PASID_CACHE_DEVSI: + if (pc_info->vtd_bus !=3D vtd_bus || + pc_info->devfn =3D=3D devfn) { + return false; + } + break; default: error_report("invalid pc_info->flags"); abort(); @@ -2827,6 +2859,11 @@ static void vtd_replay_guest_pasid_bindings(IntelIOM= MUState *s, walk_info.flags |=3D VTD_PASID_TABLE_DID_SEL_WALK; /* loop all assigned devices */ break; + case VTD_PASID_CACHE_DEVSI: + walk_info.vtd_bus =3D pc_info->vtd_bus; + walk_info.devfn =3D pc_info->devfn; + vtd_replay_pasid_bind_for_dev(s, start, end, &walk_info); + return; case VTD_PASID_CACHE_FORCE_RESET: /* For force reset, no need to go further replay */ return; @@ -2912,6 +2949,20 @@ static void vtd_pasid_cache_sync(IntelIOMMUState *s, vtd_iommu_unlock(s); } =20 +static void vtd_pasid_cache_devsi(IntelIOMMUState *s, + VTDBus *vtd_bus, uint16_t devfn) +{ + VTDPASIDCacheInfo pc_info; + + trace_vtd_pasid_cache_devsi(devfn); + + pc_info.flags =3D VTD_PASID_CACHE_DEVSI; + pc_info.vtd_bus =3D vtd_bus; + pc_info.devfn =3D devfn; + + vtd_pasid_cache_sync(s, &pc_info); +} + /** * Caller of this function should hold iommu_lock */ diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index b9e48ab..9122601 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -529,14 +529,18 @@ struct VTDPASIDCacheInfo { #define VTD_PASID_CACHE_GLOBAL (1ULL << 1) #define VTD_PASID_CACHE_DOMSI (1ULL << 2) #define VTD_PASID_CACHE_PASIDSI (1ULL << 3) +#define VTD_PASID_CACHE_DEVSI (1ULL << 4) uint32_t flags; uint16_t domain_id; uint32_t pasid; + VTDBus *vtd_bus; + uint16_t devfn; }; #define VTD_PASID_CACHE_INFO_MASK (VTD_PASID_CACHE_FORCE_RESET | \ VTD_PASID_CACHE_GLOBAL | \ VTD_PASID_CACHE_DOMSI | \ - VTD_PASID_CACHE_PASIDSI) + VTD_PASID_CACHE_PASIDSI | \ + VTD_PASID_CACHE_DEVSI) typedef struct VTDPASIDCacheInfo VTDPASIDCacheInfo; =20 /* PASID Table Related Definitions */ diff --git a/hw/i386/trace-events b/hw/i386/trace-events index 60d20c1..3853fa8 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -26,6 +26,7 @@ vtd_pasid_cache_gsi(void) "" vtd_pasid_cache_reset(void) "" vtd_pasid_cache_dsi(uint16_t domain) "Domian slective PC invalidation doma= in 0x%"PRIx16 vtd_pasid_cache_psi(uint16_t domain, uint32_t pasid) "PASID slective PC in= validation domain 0x%"PRIx16" pasid 0x%"PRIx32 +vtd_pasid_cache_devsi(uint16_t devfn) "Dev selective PC invalidation dev: = 0x%"PRIx16 vtd_re_not_present(uint8_t bus) "Root entry bus %"PRIu8" not present" vtd_ce_not_present(uint8_t bus, uint8_t devfn) "Context entry bus %"PRIu8"= devfn %"PRIu8" not present" vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t d= omain) "IOTLB page hit sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" d= omain 0x%"PRIx16 --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542398; cv=none; d=zohomail.com; s=zohoarc; b=Z7wch2flebj1SO7MOPmr3m851SR+2ULh/YwvyFiSbVwAJU3cOezeIrtjyb0fRIpue3JHlVdJ5bG/3jBid6ccRXnbt+6JPlo9Z1ma4e8BTpLc6EsvoXm3vyLKkLuXXAwXDM5SMgtTuLI0Xk7ZetzsXrdvp9yge6xinvO4YQSXjTs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542398; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=0oYu5FXtbE4PEAXHtciJs2R+9kfHMtMcgWoC14PM0HI=; b=mL3scBCdYbi2f7S6JOI2wZBiURbQ/BO1ziYLXTKr7WstK8MaAKjJgr+w9c0Z+MzJJKzbG3xxuRAi+dewsl5UPbc5S2+oAX7n67Zc3dAV2L2XrEHNhVFidZ+cormtbeozargVHqJqo+351djDU0jHb5PXcPSMUVRyS/8UQN08EQg= ARC-Authentication-Results: i=1; mx.zohomail.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=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 158554239894770.08830567134362; Sun, 29 Mar 2020 21:26:38 -0700 (PDT) Received: from localhost ([::1]:44738 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm0T-00064D-Jj for importer@patchew.org; Mon, 30 Mar 2020 00:26:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37985) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlty-00021k-Pa for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltx-0008Jp-Cr for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:54 -0400 Received: from mga07.intel.com ([134.134.136.100]:12574) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltw-0007ph-Up for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:53 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: tFqJTwRZ3Z8nGG+LOTYyafvpji8Cp0AKX9F+Oo1UBfeDj87wMtcYU5IuCG9sT+jZzAQ1R782WM SPfurmI0LuyA== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: WKbxMxV4QFawnb6moNAw3onpaS6w65NuM+IE7/dlv7IdEQ8DoYLrrIw/9PFfwHlENgccAM65UK jNnkLEArH4mw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632062" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 17/22] intel_iommu: do not pass down pasid bind for PASID #0 Date: Sun, 29 Mar 2020 21:24:56 -0700 Message-Id: <1585542301-84087-18-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" RID_PASID field was introduced in VT-d 3.0 spec, it is used for DMA requests w/o PASID in scalable mode VT-d. It is also known as IOVA. And in VT-d 3.1 spec, there is definition on it: "Implementations not supporting RID_PASID capability (ECAP_REG.RPS is 0b), use a PASID value of 0 to perform address translation for requests without PASID." This patch adds a check against the PASIDs which are going to be bound to device. For PASID #0, it is not necessary to pass down pasid bind request for it since PASID #0 is used as RID_PASID for DMA requests without pasid. Further reason is current Intel vIOMMU supports gIOVA by shadowing guest 2nd level page table. However, in future, if guest IOMMU driver uses 1st level page table to store IOVA mappings, then guest IOVA support will also be done via nested translation. When gIOVA is over FLPT, then vIOMMU should pass down the pasid bind request for PASID #0 to host, host needs to bind the guest IOVA page table to a proper PASID. e.g PASID value in RID_PASID field for PF/VF if ECAP_REG.RPS is clear or default PASID for ADI (Assignable Device Interface in Scalable IOV solution). IOVA over FLPT support on Intel VT-d: https://lkml.org/lkml/2019/9/23/297 Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Reviewed-by: Peter Xu Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 883aeac..074d966 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1895,6 +1895,16 @@ static int vtd_bind_guest_pasid(IntelIOMMUState *s, = VTDBus *vtd_bus, struct iommu_gpasid_bind_data *g_bind_data; int ret =3D -1; =20 + if (pasid < VTD_HPASID_MIN) { + /* + * If pasid < VTD_HPASID_MIN, this pasid is not allocated + * from host. No need to pass down the changes on it to host. + * TODO: when IOVA over FLPT is ready, this switch should be + * refined. + */ + return 0; + } + vtd_dev_icx =3D vtd_bus->dev_icx[devfn]; if (!vtd_dev_icx) { /* means no need to go further, e.g. for emulated devices */ --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542296; cv=none; d=zohomail.com; s=zohoarc; b=C+RrSnPjLswseO209RGNfefijbZ4sMIJW1ijacT1G8SmKqCe4DMpwkVhbCQCDFW6dTRbXylh/qN5g6mS/u6uP/zivK19yR9i60eI02x6RX9ecYkq75RyTxRJDZ36VOdAhEwL28teSly8Lt2ntE7iAfqIKlMfxRqGpMcOB2Go4QU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542296; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=HU9vST7oS9fL6X7PKxwurjcGEN+PKlpHaweBEaJN7Vw=; b=e2Oe77IQZ63k0GqQnDBOhzBZHFPH6uMuc9EfDlYpb9x7dCiiCYXLlwF5Gi0hjr056HA2q2whimcb5Z84I0cYaHJqb8gXJhx4jwMkvlX5C8aeqJDE40q/BMKXi4SVjUI++Eo2MHXhpPUaxf1ZKjNtCRoRgXhPF3y6WujWUx/AFBM= ARC-Authentication-Results: i=1; mx.zohomail.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=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 158554229695597.65626493569596; Sun, 29 Mar 2020 21:24:56 -0700 (PDT) Received: from localhost ([::1]:44702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlyn-0002cx-Mn for importer@patchew.org; Mon, 30 Mar 2020 00:24:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37990) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlty-000228-Tp for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltx-0008K1-FS for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:54 -0400 Received: from mga07.intel.com ([134.134.136.100]:12573) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltx-0007o2-2x for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:53 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: WN7Zzx4Ag+jz8bWiUs/IFLrgNX7NLuUsuq8nuuzLtvcvOi/BE6PYrT5Od0mbg9smviyL2QeGYD pq08WIuszjyQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: dPsQ4FxPl8pOrtzLBeYAm0lXulIKdIkLzDFxbuk0VTSIzzc0BpHDLASu+KpExAdYvZ5thghLzh 8BWKQhn3Hoyw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632066" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 18/22] vfio: add support for flush iommu stage-1 cache Date: Sun, 29 Mar 2020 21:24:57 -0700 Message-Id: <1585542301-84087-19-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds flush_stage1_cache() definition in HostIOMUContextClass. And adds corresponding implementation in VFIO. This is to expose a way for vIOMMU to flush stage-1 cache in host side since guest owns stage-1 translation structures in dual stage DMA translation configuration. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Cc: Alex Williamson Acked-by: Peter Xu Signed-off-by: Liu Yi L --- hw/iommu/host_iommu_context.c | 19 +++++++++++++++++++ hw/vfio/common.c | 25 +++++++++++++++++++++++++ include/hw/iommu/host_iommu_context.h | 14 ++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/hw/iommu/host_iommu_context.c b/hw/iommu/host_iommu_context.c index 8ae20fe..e884752 100644 --- a/hw/iommu/host_iommu_context.c +++ b/hw/iommu/host_iommu_context.c @@ -113,6 +113,25 @@ int host_iommu_ctx_unbind_stage1_pgtbl(HostIOMMUContex= t *iommu_ctx, return hicxc->unbind_stage1_pgtbl(iommu_ctx, data); } =20 +int host_iommu_ctx_flush_stage1_cache(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1Cache *cache) +{ + HostIOMMUContextClass *hicxc; + + hicxc =3D HOST_IOMMU_CONTEXT_GET_CLASS(iommu_ctx); + + if (!hicxc) { + return -EINVAL; + } + + if (!(iommu_ctx->flags & HOST_IOMMU_NESTING) || + !hicxc->flush_stage1_cache) { + return -EINVAL; + } + + return hicxc->flush_stage1_cache(iommu_ctx, cache); +} + void host_iommu_ctx_init(void *_iommu_ctx, size_t instance_size, const char *mrtypename, uint64_t flags, uint32_t formats) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 465e4d8..6b730b6 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1274,6 +1274,30 @@ static int vfio_host_iommu_ctx_unbind_stage1_pgtbl(H= ostIOMMUContext *iommu_ctx, return ret; } =20 +static int vfio_host_iommu_ctx_flush_stage1_cache(HostIOMMUContext *iommu_= ctx, + DualIOMMUStage1Cache *cache) +{ + VFIOContainer *container =3D container_of(iommu_ctx, + VFIOContainer, iommu_ctx); + struct vfio_iommu_type1_cache_invalidate *cache_inv; + unsigned long argsz; + int ret =3D 0; + + argsz =3D sizeof(*cache_inv) + sizeof(cache->cache_info); + cache_inv =3D g_malloc0(argsz); + cache_inv->argsz =3D argsz; + cache_inv->flags =3D 0; + memcpy(&cache_inv->cache_info, &cache->cache_info, + sizeof(cache->cache_info)); + + if (ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, cache_inv)) { + error_report("%s: iommu cache flush failed: %d", __func__, -errno); + ret =3D -errno; + } + g_free(cache_inv); + return ret; +} + /** * Get iommu info from host. Caller of this funcion should free * the memory pointed by the returned pointer stored in @info @@ -1998,6 +2022,7 @@ static void vfio_host_iommu_context_class_init(Object= Class *klass, hicxc->pasid_free =3D vfio_host_iommu_ctx_pasid_free; hicxc->bind_stage1_pgtbl =3D vfio_host_iommu_ctx_bind_stage1_pgtbl; hicxc->unbind_stage1_pgtbl =3D vfio_host_iommu_ctx_unbind_stage1_pgtbl; + hicxc->flush_stage1_cache =3D vfio_host_iommu_ctx_flush_stage1_cache; } =20 static const TypeInfo vfio_host_iommu_context_info =3D { diff --git a/include/hw/iommu/host_iommu_context.h b/include/hw/iommu/host_= iommu_context.h index 44daca9..69b1b7b 100644 --- a/include/hw/iommu/host_iommu_context.h +++ b/include/hw/iommu/host_iommu_context.h @@ -42,6 +42,7 @@ =20 typedef struct HostIOMMUContext HostIOMMUContext; typedef struct DualIOMMUStage1BindData DualIOMMUStage1BindData; +typedef struct DualIOMMUStage1Cache DualIOMMUStage1Cache; =20 typedef struct HostIOMMUContextClass { /* private */ @@ -65,6 +66,12 @@ typedef struct HostIOMMUContextClass { /* Undo a previous bind. @bind_data specifies the unbind info. */ int (*unbind_stage1_pgtbl)(HostIOMMUContext *iommu_ctx, DualIOMMUStage1BindData *bind_data); + /* + * Propagate stage-1 cache flush to host IOMMU, cache + * info specifid in @cache + */ + int (*flush_stage1_cache)(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1Cache *cache); } HostIOMMUContextClass; =20 /* @@ -86,6 +93,11 @@ struct DualIOMMUStage1BindData { } bind_data; }; =20 +struct DualIOMMUStage1Cache { + uint32_t pasid; + struct iommu_cache_invalidate_info cache_info; +}; + int host_iommu_ctx_pasid_alloc(HostIOMMUContext *iommu_ctx, uint32_t min, uint32_t max, uint32_t *pasid); int host_iommu_ctx_pasid_free(HostIOMMUContext *iommu_ctx, uint32_t pasid); @@ -93,6 +105,8 @@ int host_iommu_ctx_bind_stage1_pgtbl(HostIOMMUContext *i= ommu_ctx, DualIOMMUStage1BindData *data); int host_iommu_ctx_unbind_stage1_pgtbl(HostIOMMUContext *iommu_ctx, DualIOMMUStage1BindData *data); +int host_iommu_ctx_flush_stage1_cache(HostIOMMUContext *iommu_ctx, + DualIOMMUStage1Cache *cache); =20 void host_iommu_ctx_init(void *_iommu_ctx, size_t instance_size, const char *mrtypename, --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542472; cv=none; d=zohomail.com; s=zohoarc; b=MjZhK/FiIU8AMnWrlA85sJDplyZjg/McSrRg2+YjOC795AvIGlsfE/bptTAfaETDxfk+Cqkf4kdv1mESby6ePfmZSr+d6BdymCDervZy96Ec7pvwjqNrUE/6wJ5H72xfJDox8tUkPa1BdwonqG+3ILbiRcImtYP2BegkcDDUPK8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542472; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=a4tQmZkQYixPzaaQi5ti5x6/VpQkG070lgNE+3C90+E=; b=e9nJIw2P8Ve20SFROBiLkGX4hhL7PeS6GFa3ywWdr+1URVNK8ofq6wXvXb38FWj5s3hoERWAja7xPxcoE7sL/gdOJTfHUTF/YTYY4v1zGrhi2i/8exeIFG6X6yO06KFoGY0oEkAeYCmHCSfYOQsFclWF2/3oHKomyzVPzhon5MY= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542472317442.51223152933244; Sun, 29 Mar 2020 21:27:52 -0700 (PDT) Received: from localhost ([::1]:44754 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm1f-00082p-3N for importer@patchew.org; Mon, 30 Mar 2020 00:27:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37986) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlty-00021s-Pe for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltx-0008Jv-ES for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:54 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltx-0007lr-4b for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:53 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: Mi516NdkQhHDBX7i1JAjVz0ebkr5tE13UDCYGsXZJrvlLJR/SEYnVoYaDqYYixNrDSqQjxnm8P AIYHEfjkFLZg== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: 5wXW8PikPRmt9jWZPfyttUQ97i4sEEdSI7WEtgLxvlTU7G1KfFdi5WO6+mATpk4doYtDzEhpTr QHi0rV4f+Zcw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632069" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 19/22] intel_iommu: process PASID-based iotlb invalidation Date: Sun, 29 Mar 2020 21:24:58 -0700 Message-Id: <1585542301-84087-20-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds the basic PASID-based iotlb (piotlb) invalidation support. piotlb is used during walking Intel VT-d 1st level page table. This patch only adds the basic processing. Detailed handling will be added in next patch. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Signed-off-by: Liu Yi L Reviewed-by: Peter Xu --- hw/i386/intel_iommu.c | 53 ++++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 13 +++++++++++ 2 files changed, 66 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 074d966..6114dd8 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3045,6 +3045,55 @@ static bool vtd_process_pasid_desc(IntelIOMMUState *= s, return true; } =20 +static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s, + uint16_t domain_id, + uint32_t pasid) +{ +} + +static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain= _id, + uint32_t pasid, hwaddr addr, uint8_= t am, + bool ih) +{ +} + +static bool vtd_process_piotlb_desc(IntelIOMMUState *s, + VTDInvDesc *inv_desc) +{ + uint16_t domain_id; + uint32_t pasid; + uint8_t am; + hwaddr addr; + + if ((inv_desc->val[0] & VTD_INV_DESC_PIOTLB_RSVD_VAL0) || + (inv_desc->val[1] & VTD_INV_DESC_PIOTLB_RSVD_VAL1)) { + error_report_once("non-zero-field-in-piotlb_inv_desc hi: 0x%" PRIx= 64 + " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]); + return false; + } + + domain_id =3D VTD_INV_DESC_PIOTLB_DID(inv_desc->val[0]); + pasid =3D VTD_INV_DESC_PIOTLB_PASID(inv_desc->val[0]); + switch (inv_desc->val[0] & VTD_INV_DESC_IOTLB_G) { + case VTD_INV_DESC_PIOTLB_ALL_IN_PASID: + vtd_piotlb_pasid_invalidate(s, domain_id, pasid); + break; + + case VTD_INV_DESC_PIOTLB_PSI_IN_PASID: + am =3D VTD_INV_DESC_PIOTLB_AM(inv_desc->val[1]); + addr =3D (hwaddr) VTD_INV_DESC_PIOTLB_ADDR(inv_desc->val[1]); + vtd_piotlb_page_invalidate(s, domain_id, pasid, addr, am, + VTD_INV_DESC_PIOTLB_IH(inv_desc->val[1]= )); + break; + + default: + error_report_once("Invalid granularity in P-IOTLB desc hi: 0x%" PR= Ix64 + " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]); + return false; + } + return true; +} + static bool vtd_process_inv_iec_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) { @@ -3159,6 +3208,10 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s) break; =20 case VTD_INV_DESC_PIOTLB: + trace_vtd_inv_desc("p-iotlb", inv_desc.val[1], inv_desc.val[0]); + if (!vtd_process_piotlb_desc(s, &inv_desc)) { + return false; + } break; =20 case VTD_INV_DESC_WAIT: diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 9122601..5a49d5b 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -457,6 +457,19 @@ typedef union VTDInvDesc VTDInvDesc; #define VTD_INV_DESC_PASIDC_PASID_SI (1ULL << 4) #define VTD_INV_DESC_PASIDC_GLOBAL (3ULL << 4) =20 +#define VTD_INV_DESC_PIOTLB_ALL_IN_PASID (2ULL << 4) +#define VTD_INV_DESC_PIOTLB_PSI_IN_PASID (3ULL << 4) + +#define VTD_INV_DESC_PIOTLB_RSVD_VAL0 0xfff000000000ffc0ULL +#define VTD_INV_DESC_PIOTLB_RSVD_VAL1 0xf80ULL + +#define VTD_INV_DESC_PIOTLB_PASID(val) (((val) >> 32) & 0xfffffULL) +#define VTD_INV_DESC_PIOTLB_DID(val) (((val) >> 16) & \ + VTD_DOMAIN_ID_MASK) +#define VTD_INV_DESC_PIOTLB_ADDR(val) ((val) & ~0xfffULL) +#define VTD_INV_DESC_PIOTLB_AM(val) ((val) & 0x3fULL) +#define VTD_INV_DESC_PIOTLB_IH(val) (((val) >> 6) & 0x1) + /* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo { uint16_t domain_id; --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542551; cv=none; d=zohomail.com; s=zohoarc; b=J5FgFT6zhUwooiWs59KgeSD2q0lZa1t7YrKYTLDRkiZ35+WmJUJRGnOC1O4p4uthYZyFiebgDASxYWfhpTFQJZ2/krndG4k4v6HXN14Z1OTOlTfzD8bVqvua7olYsfoqz2cV3Lf03mh6IcV28OVZrYg78OUnXI6FheSmvxfzXfE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542551; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=6HZddE/fFa+2omaVLNdPJnPd3Qie1uoAs6WmF6/PRWo=; b=gN17ZiYWLe8wBPOd7VyM3cKV0JtUbmUG3kjR5+Gc/a/Zigj5NJPgsJat5HT9l1rXNhxOh6VOYQh1Ek8ZX97JwACx2JiS7NijikuMvAVm21mQOSKmgyZBCYUYKuXQir5y1HbxS+t+8cnBysJzKj6wjB+wsYKpRDWy/kOBJ8MJpb4= ARC-Authentication-Results: i=1; mx.zohomail.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=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 158554255164866.5247313803128; Sun, 29 Mar 2020 21:29:11 -0700 (PDT) Received: from localhost ([::1]:44766 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm2w-0001Vl-5m for importer@patchew.org; Mon, 30 Mar 2020 00:29:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38023) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlu9-0002D2-Nj for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:20:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIlu7-0008Ul-RK for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:20:05 -0400 Received: from mga07.intel.com ([134.134.136.100]:12574) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIlu7-0007ph-JR for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:20:03 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: M6kRrJMeU37LIEFjFqYvm6ShzlBbNX1LC67UsafFKOCRrP2/OvAyjfb6MWTh9m6v/gDbJVy1mD /n6IonmZZGIQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: wOgM4EatcQOrvGteBb4pGpHWeGaB0HRittS2zv5UqECBTSjiHpIC/c2gweZ9D9nSAkYobGP1Wx odtMhe2MdkjQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632072" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 20/22] intel_iommu: propagate PASID-based iotlb invalidation to host Date: Sun, 29 Mar 2020 21:24:59 -0700 Message-Id: <1585542301-84087-21-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch propagates PASID-based iotlb invalidation to host. Intel VT-d 3.0 supports nested translation in PASID granular. Guest SVA support could be implemented by configuring nested translation on specific PASID. This is also known as dual stage DMA translation. Under such configuration, guest owns the GVA->GPA translation which is configured as first level page table in host side for a specific pasid, and host owns GPA->HPA translation. As guest owns first level translation table, piotlb invalidation should be propagated to host since host IOMMU will cache first level page table related mappings during DMA address translation. This patch traps the guest PASID-based iotlb flush and propagate it to host. v1 -> v2: removed the valid check to vtd_pasid_as instance as v2 ensures all vtd_pasid_as instance should be valid Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 117 +++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 7 +++ 2 files changed, 124 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 6114dd8..02ad90a 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3045,16 +3045,133 @@ static bool vtd_process_pasid_desc(IntelIOMMUState= *s, return true; } =20 +/** + * Caller of this function should hold iommu_lock. + */ +static void vtd_invalidate_piotlb(IntelIOMMUState *s, + VTDBus *vtd_bus, + int devfn, + DualIOMMUStage1Cache *stage1_cache) +{ + VTDHostIOMMUContext *vtd_dev_icx; + HostIOMMUContext *iommu_ctx; + + vtd_dev_icx =3D vtd_bus->dev_icx[devfn]; + if (!vtd_dev_icx) { + goto out; + } + iommu_ctx =3D vtd_dev_icx->iommu_ctx; + if (!iommu_ctx) { + goto out; + } + if (host_iommu_ctx_flush_stage1_cache(iommu_ctx, stage1_cache)) { + error_report("Cache flush failed"); + } +out: + return; +} + +/** + * This function is a loop function for the s->vtd_pasid_as + * list with VTDPIOTLBInvInfo as execution filter. It propagates + * the piotlb invalidation to host. Caller of this function + * should hold iommu_lock. + */ +static void vtd_flush_pasid_iotlb(gpointer key, gpointer value, + gpointer user_data) +{ + VTDPIOTLBInvInfo *piotlb_info =3D user_data; + VTDPASIDAddressSpace *vtd_pasid_as =3D value; + VTDPASIDCacheEntry *pc_entry =3D &vtd_pasid_as->pasid_cache_entry; + uint16_t did; + + did =3D vtd_pe_get_domain_id(&pc_entry->pasid_entry); + + if ((piotlb_info->domain_id =3D=3D did) && + (piotlb_info->pasid =3D=3D vtd_pasid_as->pasid)) { + vtd_invalidate_piotlb(vtd_pasid_as->iommu_state, + vtd_pasid_as->vtd_bus, + vtd_pasid_as->devfn, + piotlb_info->stage1_cache); + } + + /* + * TODO: needs to add QEMU piotlb flush when QEMU piotlb + * infrastructure is ready. For now, it is enough for passthru + * devices. + */ +} + static void vtd_piotlb_pasid_invalidate(IntelIOMMUState *s, uint16_t domain_id, uint32_t pasid) { + VTDPIOTLBInvInfo piotlb_info; + DualIOMMUStage1Cache *stage1_cache; + struct iommu_cache_invalidate_info *cache_info; + + stage1_cache =3D g_malloc0(sizeof(*stage1_cache)); + stage1_cache->pasid =3D pasid; + + cache_info =3D &stage1_cache->cache_info; + cache_info->version =3D IOMMU_UAPI_VERSION; + cache_info->cache =3D IOMMU_CACHE_INV_TYPE_IOTLB; + cache_info->granularity =3D IOMMU_INV_GRANU_PASID; + cache_info->pasid_info.pasid =3D pasid; + cache_info->pasid_info.flags =3D IOMMU_INV_PASID_FLAGS_PASID; + + piotlb_info.domain_id =3D domain_id; + piotlb_info.pasid =3D pasid; + piotlb_info.stage1_cache =3D stage1_cache; + + vtd_iommu_lock(s); + /* + * Here loops all the vtd_pasid_as instances in s->vtd_pasid_as + * to find out the affected devices since piotlb invalidation + * should check pasid cache per architecture point of view. + */ + g_hash_table_foreach(s->vtd_pasid_as, + vtd_flush_pasid_iotlb, &piotlb_info); + vtd_iommu_unlock(s); + g_free(stage1_cache); } =20 static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain= _id, uint32_t pasid, hwaddr addr, uint8_= t am, bool ih) { + VTDPIOTLBInvInfo piotlb_info; + DualIOMMUStage1Cache *stage1_cache; + struct iommu_cache_invalidate_info *cache_info; + + stage1_cache =3D g_malloc0(sizeof(*stage1_cache)); + stage1_cache->pasid =3D pasid; + + cache_info =3D &stage1_cache->cache_info; + cache_info->version =3D IOMMU_UAPI_VERSION; + cache_info->cache =3D IOMMU_CACHE_INV_TYPE_IOTLB; + cache_info->granularity =3D IOMMU_INV_GRANU_ADDR; + cache_info->addr_info.flags =3D IOMMU_INV_ADDR_FLAGS_PASID; + cache_info->addr_info.flags |=3D ih ? IOMMU_INV_ADDR_FLAGS_LEAF : 0; + cache_info->addr_info.pasid =3D pasid; + cache_info->addr_info.addr =3D addr; + cache_info->addr_info.granule_size =3D 1 << (12 + am); + cache_info->addr_info.nb_granules =3D 1; + + piotlb_info.domain_id =3D domain_id; + piotlb_info.pasid =3D pasid; + piotlb_info.stage1_cache =3D stage1_cache; + + vtd_iommu_lock(s); + /* + * Here loops all the vtd_pasid_as instances in s->vtd_pasid_as + * to find out the affected devices since piotlb invalidation + * should check pasid cache per architecture point of view. + */ + g_hash_table_foreach(s->vtd_pasid_as, + vtd_flush_pasid_iotlb, &piotlb_info); + vtd_iommu_unlock(s); + g_free(stage1_cache); } =20 static bool vtd_process_piotlb_desc(IntelIOMMUState *s, diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 5a49d5b..85ebaa5 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -556,6 +556,13 @@ struct VTDPASIDCacheInfo { VTD_PASID_CACHE_DEVSI) typedef struct VTDPASIDCacheInfo VTDPASIDCacheInfo; =20 +struct VTDPIOTLBInvInfo { + uint16_t domain_id; + uint32_t pasid; + DualIOMMUStage1Cache *stage1_cache; +}; +typedef struct VTDPIOTLBInvInfo VTDPIOTLBInvInfo; + /* PASID Table Related Definitions */ #define VTD_PASID_DIR_BASE_ADDR_MASK (~0xfffULL) #define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL) --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542757; cv=none; d=zohomail.com; s=zohoarc; b=hQm7D8UbRrORQWQd5c7MqTPoRRCdolPMjQB76XybAnJZR3/EfPXZXJkRi1L0eOweLi0gXaAIWZkvzYZuU/kZbuGe+oq8xDYlWKblXux8Emet2bW55oErIfu1JigQrSxGFQQIq3TwsNVCLt3dRadJEHxf+mekCwgf2fPWNxra974= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542757; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=FGybsoacroVD9bN2P5xTHIHip7fRU1OzEkRXcS9wYTA=; b=gMqaXYkPBcmU+sl5rZT0RQxMvmUqcBwlaO0FDLlriPLtmooNcWcoY5OYYQAo2BA/Nl6GbbwjNGWWD4OM6X6WOrt99Pn91ENr/UouVGzx04oqZEssElNF908gvWRSnoE+ZxDa++ynVudevlb1GEym/s/r6YoUrdT2+SUV61iRhYQ= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542757903491.0719965172078; Sun, 29 Mar 2020 21:32:37 -0700 (PDT) Received: from localhost ([::1]:44822 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIm6G-0004p2-Ou for importer@patchew.org; Mon, 30 Mar 2020 00:32:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38022) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlu9-0002D1-O5 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:20:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIlu7-0008V0-UF for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:20:05 -0400 Received: from mga07.intel.com ([134.134.136.100]:12564) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIlu7-0007lr-LE for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:20:03 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: 5/WggoJ5m6OPbJucKYax4KwuEQTgfVBj4Q+sOG4tOyWLOZgdKYdrt4k06T/hkM15M/tK9LrWUT hv91oGYzc0OQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: bhSR2WyRyj9S55bWdsPAwjhbTN9BWNgjEQo2AuhhQg28ji+jubcVq9EZ1/ou47rvzb4xHZPGv3 X1tJNHQGOYtA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632074" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 21/22] intel_iommu: process PASID-based Device-TLB invalidation Date: Sun, 29 Mar 2020 21:25:00 -0700 Message-Id: <1585542301-84087-22-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 134.134.136.100 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds an empty handling for PASID-based Device-TLB invalidation. For now it is enough as it is not necessary to propagate it to host for passthru device and also there is no emulated device has device tlb. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Reviewed-by: Peter Xu Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 18 ++++++++++++++++++ hw/i386/intel_iommu_internal.h | 1 + 2 files changed, 19 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 02ad90a..e8877d4 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3224,6 +3224,17 @@ static bool vtd_process_inv_iec_desc(IntelIOMMUState= *s, return true; } =20 +static bool vtd_process_device_piotlb_desc(IntelIOMMUState *s, + VTDInvDesc *inv_desc) +{ + /* + * no need to handle it for passthru device, for emulated + * devices with device tlb, it may be required, but for now, + * return is enough + */ + return true; +} + static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) { @@ -3345,6 +3356,13 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s) } break; =20 + case VTD_INV_DESC_DEV_PIOTLB: + trace_vtd_inv_desc("device-piotlb", inv_desc.hi, inv_desc.lo); + if (!vtd_process_device_piotlb_desc(s, &inv_desc)) { + return false; + } + break; + case VTD_INV_DESC_DEVICE: trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo); if (!vtd_process_device_iotlb_desc(s, &inv_desc)) { diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 85ebaa5..4910e63 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -386,6 +386,7 @@ typedef union VTDInvDesc VTDInvDesc; #define VTD_INV_DESC_WAIT 0x5 /* Invalidation Wait Descripto= r */ #define VTD_INV_DESC_PIOTLB 0x6 /* PASID-IOTLB Invalidate Desc= */ #define VTD_INV_DESC_PC 0x7 /* PASID-cache Invalidate Desc= */ +#define VTD_INV_DESC_DEV_PIOTLB 0x8 /* PASID-based-DIOTLB inv_desc= */ #define VTD_INV_DESC_NONE 0 /* Not an Invalidate Descripto= r */ =20 /* Masks for Invalidation Wait Descriptor*/ --=20 2.7.4 From nobody Sun May 5 18:24:48 2024 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; 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=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1585542176; cv=none; d=zohomail.com; s=zohoarc; b=f8LPzOuMroCs0L+Uu6XIqlHwoFeP1oOPoF5Etk5PJFio1PivNEz0LstzSmBfqS59LAzC4UyDmgXO2s5UDMkw/2ts2pBb8QE1q1iPfJRNEmKgY00EIMPLNQeMQgz6p+Jww0JPhVv0nbBn2ZuxUCAXq0ow+ZSRp5OZ0EFlux1/oCQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1585542176; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=4x9oE2tKswxiLZ8GwjKDXI5qTBstAyJc5g4jH55Ysqk=; b=JgakjXN5/bqn7cDhkMFSQF7P4mKLB8X1TkfpdWDkqpAynHifVMBpHAXZocn3SjmlpE3RLx+JByMfDUInY3BlqZUUKkL4bdrQmrOUwuG+jPQd4I9pDTUdkWtH3c+0+ro0il2KfjUvbIm0sD7p59LApNkDDoEgQsZdztPyihbG/rI= ARC-Authentication-Results: i=1; mx.zohomail.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=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 1585542176162176.90793796462867; Sun, 29 Mar 2020 21:22:56 -0700 (PDT) Received: from localhost ([::1]:44662 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIlws-0006Dk-TY for importer@patchew.org; Mon, 30 Mar 2020 00:22:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37797) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jIltS-0000qe-LA for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jIltP-0007oO-H8 for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:22 -0400 Received: from mga01.intel.com ([192.55.52.88]:18787) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jIltP-0007mU-8Z for qemu-devel@nongnu.org; Mon, 30 Mar 2020 00:19:19 -0400 Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Mar 2020 21:19:16 -0700 Received: from jacob-builder.jf.intel.com ([10.7.199.155]) by orsmga001.jf.intel.com with ESMTP; 29 Mar 2020 21:19:16 -0700 IronPort-SDR: xzsKzNd4p8erqmFAP9yfrR/CqY9D4j4r3Lxmto8j27CmrUUSWQhmqb2FcxbWgBxwlt3heRAShe LfI/FINmNDIw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False IronPort-SDR: 8AuPz3n5vDxx5X0yJbnIPoxzxD7YdloS1wPUiSu0nxnOO50u347KzvfslERG6rieMjzVAodbk9 7Rquhbd4Cjpg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.72,322,1580803200"; d="scan'208";a="327632077" From: Liu Yi L To: qemu-devel@nongnu.org, alex.williamson@redhat.com, peterx@redhat.com Subject: [PATCH v2 22/22] intel_iommu: modify x-scalable-mode to be string option Date: Sun, 29 Mar 2020 21:25:01 -0700 Message-Id: <1585542301-84087-23-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> References: <1585542301-84087-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 192.55.52.88 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, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , Eduardo Habkost , kvm@vger.kernel.org, mst@redhat.com, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, Jacob Pan , pbonzini@redhat.com, hao.wu@intel.com, Richard Henderson , david@gibson.dropbear.id.au Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Intel VT-d 3.0 introduces scalable mode, and it has a bunch of capabilities related to scalable mode translation, thus there are multiple combinations. While this vIOMMU implementation wants simplify it for user by providing typical combinations. User could config it by "x-scalable-mode" option. The usage is as below: "-device intel-iommu,x-scalable-mode=3D["legacy"|"modern"|"off"]" - "legacy": gives support for SL page table - "modern": gives support for FL page table, pasid, virtual command - "off": no scalable mode support - if not configured, means no scalable mode support, if not proper configured, will throw error Note: this patch is supposed to be merged when the whole vSVA patch series were merged. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Cc: Paolo Bonzini Cc: Richard Henderson Cc: Eduardo Habkost Signed-off-by: Liu Yi L Signed-off-by: Yi Sun Reviewed-by: Peter Xu --- hw/i386/intel_iommu.c | 30 ++++++++++++++++++++++++++++-- hw/i386/intel_iommu_internal.h | 4 ++++ include/hw/i386/intel_iommu.h | 2 ++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index e8877d4..2e745e8 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -4056,7 +4056,7 @@ static Property vtd_properties[] =3D { DEFINE_PROP_UINT8("aw-bits", IntelIOMMUState, aw_bits, VTD_HOST_ADDRESS_WIDTH), DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE), - DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FA= LSE), + DEFINE_PROP_STRING("x-scalable-mode", IntelIOMMUState, scalable_mode_s= tr), DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true), DEFINE_PROP_END_OF_LIST(), }; @@ -4688,8 +4688,12 @@ static void vtd_init(IntelIOMMUState *s) } =20 /* TODO: read cap/ecap from host to decide which cap to be exposed. */ - if (s->scalable_mode) { + if (s->scalable_mode && !s->scalable_modern) { s->ecap |=3D VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS; + } else if (s->scalable_mode && s->scalable_modern) { + s->ecap |=3D VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_PASID + | VTD_ECAP_FLTS | VTD_ECAP_PSS | VTD_ECAP_VCS; + s->vccap |=3D VTD_VCCAP_PAS; } =20 vtd_reset_caches(s); @@ -4821,6 +4825,28 @@ static bool vtd_decide_config(IntelIOMMUState *s, Er= ror **errp) return false; } =20 + if (s->scalable_mode_str && + (strcmp(s->scalable_mode_str, "off") && + strcmp(s->scalable_mode_str, "modern") && + strcmp(s->scalable_mode_str, "legacy"))) { + error_setg(errp, "Invalid x-scalable-mode config," + "Please use \"modern\", \"legacy\" or \"off\""); + return false; + } + + if (s->scalable_mode_str && + !strcmp(s->scalable_mode_str, "legacy")) { + s->scalable_mode =3D true; + s->scalable_modern =3D false; + } else if (s->scalable_mode_str && + !strcmp(s->scalable_mode_str, "modern")) { + s->scalable_mode =3D true; + s->scalable_modern =3D true; + } else { + s->scalable_mode =3D false; + s->scalable_modern =3D false; + } + return true; } =20 diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 4910e63..e0719bc 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -196,8 +196,12 @@ #define VTD_ECAP_PT (1ULL << 6) #define VTD_ECAP_MHMV (15ULL << 20) #define VTD_ECAP_SRS (1ULL << 31) +#define VTD_ECAP_PSS (19ULL << 35) +#define VTD_ECAP_PASID (1ULL << 40) #define VTD_ECAP_SMTS (1ULL << 43) +#define VTD_ECAP_VCS (1ULL << 44) #define VTD_ECAP_SLTS (1ULL << 46) +#define VTD_ECAP_FLTS (1ULL << 47) =20 /* CAP_REG */ /* (offset >> 4) << 24 */ diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 626c1cd..3831ba7 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -263,6 +263,8 @@ struct IntelIOMMUState { =20 bool caching_mode; /* RO - is cap CM enabled? */ bool scalable_mode; /* RO - is Scalable Mode supported? */ + char *scalable_mode_str; /* RO - admin's Scalable Mode config */ + bool scalable_modern; /* RO - is modern SM supported? */ =20 dma_addr_t root; /* Current root table pointer */ bool root_scalable; /* Type of root table (scalable or not= ) */ --=20 2.7.4