From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571923486; cv=none; d=zoho.com; s=zohoarc; b=SuCCMOwf3QD11unIOM/yraWqdNj1ORZ8TQ20mGHXLbffCiIKtDUbtoQeUDIi0OOtH6Dli5K4UF7j0RuhZuSXsC5BQDHmnHS/GknNtswRXM4jfbcskU2BrM+ufhTSOG1j3fBkh6ADslmurC6ZBjfmt2yLRGqVjJFh05/YNz0GsIM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571923486; 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=JAV/+CxRxIIGhCdlaH8Zsu0idEZqycry9RWz3B5UTBo=; b=ZLCKUVh1XcU3Fw40Qh1HkViXNPdyeLmgIYuf7ilFckB2XBtyQcKBMtFQedPzj6bzKG41QzPz8ngDdOyoh+wZnWHaQ2kIIW6aJeBg0z8mBitxU3NXnXswCOJh5ZcQ2TF0Z3VivNIZx6Aimm+pX7l1iNUjFTPcVJ+ltEQGsyb4kxk= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571923486214569.8182827005809; Thu, 24 Oct 2019 06:24:46 -0700 (PDT) Received: from localhost ([::1]:42532 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNd6Y-0006tk-8e for importer@patchew.org; Thu, 24 Oct 2019 09:24:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51301) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNcjv-00051r-V6 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNcju-0002BC-5b for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:19 -0400 Received: from mga11.intel.com ([192.55.52.93]:40473) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNcjt-00028t-TP for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:18 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:12 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:09 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156148" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 01/22] update-linux-headers: Import iommu.h Date: Thu, 24 Oct 2019 08:34:22 -0400 Message-Id: <1571920483-3382-2-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 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 f76d773..dfdfdfd 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 Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571924158; cv=none; d=zoho.com; s=zohoarc; b=LD02prx7E7HVr5aYtZhWFuDOgyvp9zzYJG4Yt9olk1FShrBpmIJLsYiVIEYHW5XWDIUI5T3IV8S40TWxQIb8VzC+IzuGAq3IchGxbhtxOTGpN7O3JZKB+nS/zs0f/G01lpiQAUxNcRVxeEIwdHPloC2TNjVgCW3MESQTyckC+yc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571924158; 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=aXlna0sVAItCIqQkcPiHG7r5d7d9b4aRAwoTqG3V0/4=; b=FdaaaGs5j8Mgy59l4RMdZG+daqjA2Ud8TjGHUe2cHitSHWupW5khA1i6S/AltOjLvBAIKeKT3R2b/fMO+VvNkqaZo2sYrEUClKTymRW/4nKnhTkgKz2ST+kYNLLO79cOVPOx7qgneOR+9jeqojRxvG3WJuOcZATCvlL6R0ey97M= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571924158131845.4041907774975; Thu, 24 Oct 2019 06:35:58 -0700 (PDT) Received: from localhost ([::1]:42684 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdHO-0002GZ-VQ for importer@patchew.org; Thu, 24 Oct 2019 09:35:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51369) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckG-0005DK-F4 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckD-0002OK-UM for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:40 -0400 Received: from mga11.intel.com ([192.55.52.93]:40482) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckB-0002MX-N8 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:36 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:15 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:12 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156159" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 02/22] header update VFIO/IOMMU vSVA APIs against 5.4.0-rc3+ Date: Thu, 24 Oct 2019 08:34:23 -0400 Message-Id: <1571920483-3382-3-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 iommu.h header file includes the extensions for vSVA support. e.g. guest PASID bind, iommu fault report related user structures etc. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L --- linux-headers/linux/iommu.h | 324 ++++++++++++++++++++++++++++++++++++++++= ++++ linux-headers/linux/vfio.h | 83 ++++++++++++ 2 files changed, 407 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..872f786 --- /dev/null +++ b/linux-headers/linux/iommu.h @@ -0,0 +1,324 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * IOMMU user API definitions + */ + +#ifndef _IOMMU_H +#define _IOMMU_H + +#include + +#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: API version of this structure + * @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 { +#define IOMMU_PAGE_RESP_VERSION_1 1 + __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: API version of this structure + * @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 { +#define IOMMU_CACHE_INVALIDATE_INFO_VERSION_1 1 + __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; +}; + +/** + * struct iommu_gpasid_bind_data - Information about device and guest PASI= D binding + * @version: Version of this data structure + * @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) + * @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 { +#define IOMMU_GPASID_BIND_VERSION_1 1 + __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 { + 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..e5173b6 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 @@ -794,6 +795,88 @@ 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 +/** + * VFIO_IOMMU_CACHE_INVALIDATE - _IOWR(VFIO_TYPE, VFIO_BASE + 24, + * struct vfio_iommu_type1_cache_invalidate) + * + * Propagate guest IOMMU cache invalidation to the host. + */ +struct vfio_iommu_type1_cache_invalidate { + __u32 argsz; + __u32 flags; + struct iommu_cache_invalidate_info info; +}; +#define VFIO_IOMMU_CACHE_INVALIDATE _IO(VFIO_TYPE, VFIO_BASE + 24) + +/* + * @flag=3DVFIO_IOMMU_PASID_ALLOC, refer to the @min_pasid and @max_pasid = fields + * @flag=3DVFIO_IOMMU_PASID_FREE, refer to @pasid field + */ +struct vfio_iommu_type1_pasid_request { + __u32 argsz; +#define VFIO_IOMMU_PASID_ALLOC (1 << 0) +#define VFIO_IOMMU_PASID_FREE (1 << 1) + __u32 flag; + union { + struct { + int min_pasid; + int max_pasid; + }; + int pasid; + }; +}; + +/** + * VFIO_IOMMU_PASID_REQUEST - _IOWR(VFIO_TYPE, VFIO_BASE + 27, + * struct vfio_iommu_type1_pasid_request) + * + */ +#define VFIO_IOMMU_PASID_REQUEST _IO(VFIO_TYPE, VFIO_BASE + 27) + +enum vfio_iommu_bind_type { + VFIO_IOMMU_BIND_PROCESS, + VFIO_IOMMU_BIND_GUEST_PASID, +}; + +/* + * Supported types: + * - VFIO_IOMMU_BIND_GUEST_PASID: bind guest pasid, which invoked + * by guest, it takes iommu_gpasid_bind_data in data. + */ +struct vfio_iommu_type1_bind { + __u32 argsz; + enum vfio_iommu_bind_type bind_type; + __u8 data[]; +}; + +/* + * VFIO_IOMMU_BIND - _IOWR(VFIO_TYPE, VFIO_BASE + 28, struct vfio_iommu_bi= nd) + * + * 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 + * non-PASID traffic and BIND controls PASID traffic. But this depends on = the + * underlying IOMMU architecture and isn't guaranteed. + * + * 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 + 28) + +/* + * VFIO_IOMMU_UNBIND - _IOWR(VFIO_TYPE, VFIO_BASE + 29, struct vfio_iommu_= bind) + * + * Undo what was done by the corresponding VFIO_IOMMU_BIND ioctl. + */ +#define VFIO_IOMMU_UNBIND _IO(VFIO_TYPE, VFIO_BASE + 29) + /* -------- Additional API for SPAPR TCE (Server POWERPC) IOMMU -------- */ =20 /* --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571927417; cv=none; d=zoho.com; s=zohoarc; b=G4dp2HhIbJn0v/EveD9Jr3nxAAGKHkveyrJRXeClwBpIRJrnVafNBSE/2IplAz0FurZNFkucsy79GnFOomejHaHOM3cdho69in9omo8eNEcgh1NZBx3rLFQZuUNNRboxl0PM2dqqbG+QqSYyyPplmzaezUGBg0Z9Ai6b/SAMEjQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571927417; 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=uULUZ3KMt6F4ewU0o3f9d9i3J+FjjhxOPyChd6dPHGI=; b=RN2j2SZvLZOAwk2BCLxGiTJY88N57ter/3X39JYm5LLWiOSRxR4QPwnwsdbw1NaL/nPGGMOX6U7I+VlwzVwd7DDB0eUFih64TnAWBFN96b3MaWM7DBWTZ9/3RZVQfjxHdcWyxq/3Ra8ifV89/8gWTe3sZuPcgjPnF8Nv8sz4H0s= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571927417840491.54533830048297; Thu, 24 Oct 2019 07:30:17 -0700 (PDT) Received: from localhost ([::1]:44230 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNe80-0007sN-2G for importer@patchew.org; Thu, 24 Oct 2019 10:30:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51435) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckJ-0005Gy-MU for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckE-0002Oi-Eu for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:43 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckD-0002NX-W9 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:38 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:18 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:15 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156167" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 03/22] intel_iommu: modify x-scalable-mode to be string option Date: Thu, 24 Oct 2019 08:34:24 -0400 Message-Id: <1571920483-3382-4-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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" 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"]" - "legacy": gives support for SL page table - "modern": gives support for FL page table, pasid, virtual command - if not configured, means no scalable mode support, if not proper configured, will throw error Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L Signed-off-by: Yi Sun --- hw/i386/intel_iommu.c | 15 +++++++++++++-- hw/i386/intel_iommu_internal.h | 3 +++ include/hw/i386/intel_iommu.h | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 771bed2..4a1a07a 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3019,7 +3019,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), DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true), DEFINE_PROP_END_OF_LIST(), }; @@ -3581,7 +3581,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) { - s->ecap |=3D VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS; + if (!strcmp(s->scalable_mode, "legacy")) { + s->ecap |=3D VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS; + } else if (!strcmp(s->scalable_mode, "modern")) { + s->ecap |=3D VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_PASID + | VTD_ECAP_FLTS | VTD_ECAP_PSS; + } } =20 vtd_reset_caches(s); @@ -3700,6 +3705,12 @@ static bool vtd_decide_config(IntelIOMMUState *s, Er= ror **errp) return false; } =20 + if (s->scalable_mode && + (strcmp(s->scalable_mode, "modern") && + strcmp(s->scalable_mode, "legacy"))) { + error_setg(errp, "Invalid x-scalable-mode config"); + } + return true; } =20 diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index c1235a7..be7b30a 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -190,8 +190,11 @@ #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_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 66b931e..6062588 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -231,7 +231,7 @@ struct IntelIOMMUState { uint32_t version; =20 bool caching_mode; /* RO - is cap CM enabled? */ - bool scalable_mode; /* RO - is Scalable Mode supported? */ + char *scalable_mode; /* RO - Scalable Mode model */ =20 dma_addr_t root; /* Current root table pointer */ bool root_scalable; /* Type of root table (scalable or not= ) */ --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571923297; cv=none; d=zoho.com; s=zohoarc; b=M+oMCjEI5A5ZCg+Hc5ud8RzQ61oUGX9/HOSqx2XVInX+h9JZuHoVZ3N9QHhmW1mcDg1oD3vAaZNFDUeV/y0w5sUqWq/fsLO2dNnDSawuhKBswCSOzCJ8z5oqzX+IziWviyFp9CDxvyJF7qJinXBE3lDMeX8nsMj3+7lBk05C3T8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571923297; 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=mw1J+RUCLgji/dWtB9+VCxH5SLNUYvjXclpzoMQRhTU=; b=kDHg9lf0dh+N2Hz0gpq5c9ldg28dmUYRkGzjhcxqcWGOwsng/TsLWTVm+f7OyWs093eznhjh1A6mw1OrnbZm3/YFmsRUhfJV8dx5DFP8F67vfqViMNHCF63AyJkij161hH2nEl5yWVT7LYiliiNgDDsHkKxQarfgJoJCKjEaLpU= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571923297452145.8159761418965; Thu, 24 Oct 2019 06:21:37 -0700 (PDT) Received: from localhost ([::1]:42424 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNd3X-0002Js-MF for importer@patchew.org; Thu, 24 Oct 2019 09:21:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51406) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckI-0005FS-Em for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckG-0002SO-Cr for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:42 -0400 Received: from mga11.intel.com ([192.55.52.93]:40494) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckF-0002O1-GA for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:39 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:21 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:18 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156175" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 04/22] hw/iommu: introduce IOMMUContext Date: Thu, 24 Oct 2019 08:34:25 -0400 Message-Id: <1571920483-3382-5-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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: Peter Xu This patch adds IOMMUContext as an abstract layer of IOMMU related operations. The current usage of this abstract layer is setup dual- stage IOMMU translation (vSVA) for vIOMMU. To setup dual-stage IOMMU translation, vIOMMU needs to propagate guest changes to host via passthru channels (e.g. VFIO). To have a better abstraction, it is better to avoid direct calling between vIOMMU and VFIO. So we have this new structure to act as abstract layer between VFIO and vIOMMU. So far, it is proposed to provide a notifier mechanism, which registered by VFIO and fired by vIOMMU. For more background, may refer to the discussion below: https://lists.gnu.org/archive/html/qemu-devel/2019-07/msg05022.html Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Signed-off-by: Peter Xu Signed-off-by: Liu Yi L --- hw/Makefile.objs | 1 + hw/iommu/Makefile.objs | 1 + hw/iommu/iommu.c | 66 ++++++++++++++++++++++++++++++++++++++++ include/hw/iommu/iommu.h | 79 ++++++++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 147 insertions(+) create mode 100644 hw/iommu/Makefile.objs create mode 100644 hw/iommu/iommu.c create mode 100644 include/hw/iommu/iommu.h diff --git a/hw/Makefile.objs b/hw/Makefile.objs index ece6cc3..ac19f9c 100644 --- a/hw/Makefile.objs +++ b/hw/Makefile.objs @@ -39,6 +39,7 @@ devices-dirs-y +=3D xen/ devices-dirs-$(CONFIG_MEM_DEVICE) +=3D mem/ 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..0484b79 --- /dev/null +++ b/hw/iommu/Makefile.objs @@ -0,0 +1 @@ +obj-y +=3D iommu.o diff --git a/hw/iommu/iommu.c b/hw/iommu/iommu.c new file mode 100644 index 0000000..2391b0d --- /dev/null +++ b/hw/iommu/iommu.c @@ -0,0 +1,66 @@ +/* + * QEMU abstract of IOMMU context + * + * Copyright (C) 2019 Red Hat Inc. + * + * Authors: Peter Xu , + * 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 "hw/iommu/iommu.h" + +void iommu_ctx_notifier_register(IOMMUContext *iommu_ctx, + IOMMUCTXNotifier *n, + IOMMUCTXNotifyFn fn, + IOMMUCTXEvent event) +{ + n->event =3D event; + n->iommu_ctx_event_notify =3D fn; + QLIST_INSERT_HEAD(&iommu_ctx->iommu_ctx_notifiers, n, node); + return; +} + +void iommu_ctx_notifier_unregister(IOMMUContext *iommu_ctx, + IOMMUCTXNotifier *notifier) +{ + IOMMUCTXNotifier *cur, *next; + + QLIST_FOREACH_SAFE(cur, &iommu_ctx->iommu_ctx_notifiers, node, next) { + if (cur =3D=3D notifier) { + QLIST_REMOVE(cur, node); + break; + } + } +} + +void iommu_ctx_event_notify(IOMMUContext *iommu_ctx, + IOMMUCTXEventData *event_data) +{ + IOMMUCTXNotifier *cur; + + QLIST_FOREACH(cur, &iommu_ctx->iommu_ctx_notifiers, node) { + if ((cur->event =3D=3D event_data->event) && + cur->iommu_ctx_event_notify) { + cur->iommu_ctx_event_notify(cur, event_data); + } + } +} + +void iommu_context_init(IOMMUContext *iommu_ctx) +{ + QLIST_INIT(&iommu_ctx->iommu_ctx_notifiers); +} diff --git a/include/hw/iommu/iommu.h b/include/hw/iommu/iommu.h new file mode 100644 index 0000000..c22c442 --- /dev/null +++ b/include/hw/iommu/iommu.h @@ -0,0 +1,79 @@ +/* + * QEMU abstraction of IOMMU Context + * + * Copyright (C) 2019 Red Hat Inc. + * + * Authors: Peter Xu , + * 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_PCI_PASID_H +#define HW_PCI_PASID_H + +#include "qemu/queue.h" +#ifndef CONFIG_USER_ONLY +#include "exec/hwaddr.h" +#endif + +typedef struct IOMMUContext IOMMUContext; + +enum IOMMUCTXEvent { + IOMMU_CTX_EVENT_NUM, +}; +typedef enum IOMMUCTXEvent IOMMUCTXEvent; + +struct IOMMUCTXEventData { + IOMMUCTXEvent event; + uint64_t length; + void *data; +}; +typedef struct IOMMUCTXEventData IOMMUCTXEventData; + +typedef struct IOMMUCTXNotifier IOMMUCTXNotifier; + +typedef void (*IOMMUCTXNotifyFn)(IOMMUCTXNotifier *notifier, + IOMMUCTXEventData *event_data); + +struct IOMMUCTXNotifier { + IOMMUCTXNotifyFn iommu_ctx_event_notify; + /* + * What events we are listening to. Let's allow multiple event + * registrations from beginning. + */ + IOMMUCTXEvent event; + QLIST_ENTRY(IOMMUCTXNotifier) node; +}; + +/* + * This is an abstraction of IOMMU context. + */ +struct IOMMUContext { + uint32_t pasid; + QLIST_HEAD(, IOMMUCTXNotifier) iommu_ctx_notifiers; +}; + +void iommu_ctx_notifier_register(IOMMUContext *iommu_ctx, + IOMMUCTXNotifier *n, + IOMMUCTXNotifyFn fn, + IOMMUCTXEvent event); +void iommu_ctx_notifier_unregister(IOMMUContext *iommu_ctx, + IOMMUCTXNotifier *notifier); +void iommu_ctx_event_notify(IOMMUContext *iommu_ctx, + IOMMUCTXEventData *event_data); + +void iommu_context_init(IOMMUContext *iommu_ctx); + +#endif --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571923913; cv=none; d=zoho.com; s=zohoarc; b=dGqhgdr8mtsis1EBu40kH+JW3TqEVxbM+87FR/YkARqJTT+O984skNqfHagC30kxblozX+AelrlPUeeoQ6unWV2FSB3GRJFHuDAAflk5poEWdfAssxlhqfFhywR4rNJPR65a+ZA8XpD5TAuiwVy0zOAi7x4LFsj6cxDns1OxY+4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571923913; 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=luG707iQWvt+p9csyBli2w7+oGfoTXTjrc9QoA0/j1s=; b=h4sL8XcB7jmBnXxxA4oUNVf6bWytfnQTFQ873QITpoaeFV1RByPdM+XL+YRtwcZ+PYUPJMDDnx+9jeCiZq1AyoaPb1W7uk8HAd35Jd+ycHz5zalLETo5CVk3oc1Ui/pv7db+UGTu4Tm88xB0dSwClYT9S9DZWwMumfsVX/uTI70= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571923913703197.442417093946; Thu, 24 Oct 2019 06:31:53 -0700 (PDT) Received: from localhost ([::1]:42638 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdDU-0001QE-LN for importer@patchew.org; Thu, 24 Oct 2019 09:31:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51394) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckH-0005Ex-Ta for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckG-0002SH-Cl for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:41 -0400 Received: from mga11.intel.com ([192.55.52.93]:40482) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckF-0002MX-FT for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:39 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:24 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:21 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156186" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 05/22] vfio/common: add iommu_ctx_notifier in container Date: Thu, 24 Oct 2019 08:34:26 -0400 Message-Id: <1571920483-3382-6-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 a list in VFIOContainer to store iommu_ctx_notifier which is based on IOMMUContext. As a preparation of registering iommu_ctx_notifiers. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Signed-off-by: Liu Yi L --- hw/vfio/common.c | 1 + include/hw/vfio/vfio-common.h | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 5ca1148..d418527 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1271,6 +1271,7 @@ static int vfio_connect_container(VFIOGroup *group, A= ddressSpace *as, container->error =3D NULL; QLIST_INIT(&container->giommu_list); QLIST_INIT(&container->hostwin_list); + QLIST_INIT(&container->iommu_ctx_list); =20 ret =3D vfio_init_container(container, group->fd, errp); if (ret) { diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index fd56420..975d12b 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -29,6 +29,7 @@ #ifdef CONFIG_LINUX #include #endif +#include "hw/iommu/iommu.h" =20 #define VFIO_MSG_PREFIX "vfio %s: " =20 @@ -75,6 +76,7 @@ typedef struct VFIOContainer { bool initialized; unsigned long pgsizes; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; + QLIST_HEAD(, VFIOIOMMUContext) iommu_ctx_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list; QLIST_ENTRY(VFIOContainer) next; @@ -88,6 +90,13 @@ typedef struct VFIOGuestIOMMU { QLIST_ENTRY(VFIOGuestIOMMU) giommu_next; } VFIOGuestIOMMU; =20 +typedef struct VFIOIOMMUContext { + VFIOContainer *container; + IOMMUContext *iommu_ctx; + IOMMUCTXNotifier n; + QLIST_ENTRY(VFIOIOMMUContext) iommu_ctx_next; +} VFIOIOMMUContext; + typedef struct VFIOHostDMAWindow { hwaddr min_iova; hwaddr max_iova; --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571925403; cv=none; d=zoho.com; s=zohoarc; b=VbSgWcEPZSoqe+MHGYcbUEi/waF9+GriH44dN9oCTboky+MdqwjYTDQG4xBZH8lUpwj+yEEYmd+82bXexd8beRwV9LSpLqWhGbqIVx6RCi4Q1mufiBjzPPjWmhbIr2o7E930IqqaOyorrCFiyrpB8UNGC7eh+mkCd8AW+CX3jHM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571925403; 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=sN2x2f3tO0Fh39wd7d58E0d8zOpUZ06hCBlAW5mT0Cw=; b=h6pQ57ikW9mH5ir7smVDpD7xFC1cWg//qOwHdVUcdxhvIaz5kXT/xQjp9HhTnhYbnf6fjrZ/bIPMvX9fhRIHhKjnVRm/LOlfnJTlG+VxyroQBQx9pGLOQe9vkY+mhp6ewFVvtBNDWO/r8IlehsH5pofEVFe7SXUG9TzB3hCfpW4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571925403204133.97514588489082; Thu, 24 Oct 2019 06:56:43 -0700 (PDT) Received: from localhost ([::1]:43336 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdbV-00087J-Rp for importer@patchew.org; Thu, 24 Oct 2019 09:56:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51407) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckI-0005FV-GQ for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckF-0002RX-TU for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:42 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckF-0002NX-0F for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:39 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:27 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:24 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156198" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 06/22] hw/pci: modify pci_setup_iommu() to set PCIIOMMUOps Date: Thu, 24 Oct 2019 08:34:27 -0400 Message-Id: <1571920483-3382-7-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 only setting PCIIOMMUFunc. PCIIOMMUFunc is previously used to get an address space for a device in vendor specific way. The PCIIOMMUOps still offers this functionality. Use 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 Signed-off-by: Liu Yi L Reviewed-by: David Gibson Reviewed-by: Peter Xu --- 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/ppce500.c | 6 +++++- hw/pci-host/prep.c | 6 +++++- hw/pci-host/sabre.c | 6 +++++- hw/pci/pci.c | 11 ++++++----- hw/ppc/ppc440_pcix.c | 6 +++++- hw/ppc/spapr_pci.c | 6 +++++- hw/s390x/s390-pci-bus.c | 8 ++++++-- include/hw/pci/pci.h | 8 ++++++-- include/hw/pci/pci_bus.h | 2 +- 15 files changed, 74 insertions(+), 21 deletions(-) diff --git a/hw/alpha/typhoon.c b/hw/alpha/typhoon.c index 179e1f7..b890771 100644 --- a/hw/alpha/typhoon.c +++ b/hw/alpha/typhoon.c @@ -741,6 +741,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; @@ -901,7 +905,7 @@ PCIBus *typhoon_init(ram_addr_t ram_size, ISABus **isa_= bus, "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 245817d..d668514 100644 --- a/hw/arm/smmu-common.c +++ b/hw/arm/smmu-common.c @@ -342,6 +342,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; @@ -436,7 +440,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 ab6969b..dbcff03 100644 --- a/hw/hppa/dino.c +++ b/hw/hppa/dino.c @@ -389,6 +389,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) @@ -508,7 +512,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 d372636..ba6904c 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, @@ -1576,7 +1580,7 @@ static void amdvi_realize(DeviceState *dev, Error **e= rr) =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", err); msi_init(&s->pci.dev, 0, 1, true, false, err); amdvi_init(s); diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 4a1a07a..67a7836 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3666,6 +3666,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); @@ -3782,7 +3786,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. */ pcms->ioapic_as =3D vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC= ); 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 71e9b0d..235d6af 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/ppce500.c b/hw/pci-host/ppce500.c index 8bed8e8..0f907b0 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 85d7ba9..f372524 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 PCIIOMMU 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 fae20ee..79b7565 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 aa05c2b..b5ce9ca 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2615,18 +2615,19 @@ AddressSpace *pci_device_iommu_address_space(PCIDev= ice *dev) PCIBus *bus =3D pci_get_bus(dev); PCIBus *iommu_bus =3D bus; =20 - while(iommu_bus && !iommu_bus->iommu_fn && iommu_bus->parent_dev) { + while (iommu_bus && !iommu_bus->iommu_ops && iommu_bus->parent_dev) { iommu_bus =3D pci_get_bus(iommu_bus->parent_dev); } - if (iommu_bus && iommu_bus->iommu_fn) { - return iommu_bus->iommu_fn(bus, iommu_bus->iommu_opaque, dev->devf= n); + if (iommu_bus && iommu_bus->iommu_ops) { + return iommu_bus->iommu_ops->get_address_space(bus, + iommu_bus->iommu_opaque, dev->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..2c8579c 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_adress_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 01ff41d..83cd857 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 2d2f4a7..14684a0 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/include/hw/pci/pci.h b/include/hw/pci/pci.h index f3f0ffd..d9fed8d 100644 --- a/include/hw/pci/pci.h +++ b/include/hw/pci/pci.h @@ -480,10 +480,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 Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571925557; cv=none; d=zoho.com; s=zohoarc; b=U+oI3O0u0+Tk76grFBpQZ9/D87bODIzg6UvDZafkvWxlQsApUCErMqDqZIDETvCpTVT/alPiroWWyTNB4w4TsVlEf/qPKw2MNSOOYucLuiYE8lqqe8shpy5zLAVKaiN5aqImmEs9UbEjnoawLfiuvj4WXclpbHdLCl0YFvWeHmY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571925557; 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=3xbSOTMY77EA3tWXkqlMF39yFWN8bfOICsQPSQjSU6o=; b=llzZY3XOZSqZdKEse2kfMzVcaos4U4GAhtTd/BobjeYbMfEiduLGpvRjjzR8D4JBSmJO99sHySbb56ny3Su1TpeKMHFo2q6jmHjt6tcaLV68caHk+JT2uBcP13DKYaUVbU3ewTMFLerIfR1RSFMxm1U8IQUvRBsIvy7Fnq9usDg= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571925557052252.4911302900008; Thu, 24 Oct 2019 06:59:17 -0700 (PDT) Received: from localhost ([::1]:43402 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNddz-00041A-4H for importer@patchew.org; Thu, 24 Oct 2019 09:59:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51408) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckI-0005FW-Gz for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckG-0002Su-Ls for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:42 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckG-0002NX-E6 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:40 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:30 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:27 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156206" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 07/22] hw/pci: introduce pci_device_iommu_context() Date: Thu, 24 Oct 2019 08:34:28 -0400 Message-Id: <1571920483-3382-8-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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_iommu_context() to get an iommu_context for a given device. A new callback is added in PCIIOMMUOps. Users who wants to listen to events issued by vIOMMU could use this new interface to get an iommu_context and register their own notifiers, then wait for notifications from vIOMMU. e.g. VFIO is the first user of it to listen to the PASID_ALLOC/PASID_BIND/CACHE_INV events and propagate the events to host. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Signed-off-by: Liu Yi L Reviewed-by: David Gibson Reviewed-by: Peter Xu --- hw/pci/pci.c | 16 ++++++++++++++++ include/hw/pci/pci.h | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/hw/pci/pci.c b/hw/pci/pci.c index b5ce9ca..4e6af06 100644 --- a/hw/pci/pci.c +++ b/hw/pci/pci.c @@ -2625,6 +2625,22 @@ AddressSpace *pci_device_iommu_address_space(PCIDevi= ce *dev) return &address_space_memory; } =20 +IOMMUContext *pci_device_iommu_context(PCIDevice *dev) +{ + PCIBus *bus =3D pci_get_bus(dev); + PCIBus *iommu_bus =3D bus; + + while (iommu_bus && !iommu_bus->iommu_ops && iommu_bus->parent_dev) { + iommu_bus =3D pci_get_bus(iommu_bus->parent_dev); + } + if (iommu_bus && iommu_bus->iommu_ops && + iommu_bus->iommu_ops->get_iommu_context) { + return iommu_bus->iommu_ops->get_iommu_context(bus, + iommu_bus->iommu_opaque, dev->devfn); + } + return NULL; +} + 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 d9fed8d..ccada47 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/iommu.h" + extern bool pci_available; =20 /* PCI bus */ @@ -484,9 +486,12 @@ typedef struct PCIIOMMUOps PCIIOMMUOps; struct PCIIOMMUOps { AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int32_t devfn); + IOMMUContext * (*get_iommu_context)(PCIBus *bus, + void *opaque, int32_t devfn); }; =20 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev); +IOMMUContext *pci_device_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 Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571925707; cv=none; d=zoho.com; s=zohoarc; b=V52F/vvkkE/NIfmubWSw3Q0qQ7NlveztwhtQGB/zr2iNe9iPj6fhsQrU1m78kcepoAhwca6M7DSx+ZIvV3DIiQEd6aVnssyXqwn+qPQnJRJ9yrC/WWzTctNqkV8Hk+XNkL6O+9fgmOgsuRH9DHxkTnazXIhpLbFFIU3hk8AlI+A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571925707; 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=FDP512GTcywkeshR30bs+z9qqwcjwvJCZ+8yxTyDLro=; b=D8vWIrmh9F4eJJsd80nhErRlji/uFeCf8UyUNM/yqLVD0J+74rXFUwwvlYa7oHglWhDqoH9N4Y/xd3+PKqeFmLa8tZL/5ZsbMcthsKfmm0CDxJifq6Dv6bYerk9dEp6qpxbdgLQ8O0/aFNlf5Ru0bXy49vRvix6ZkPduMiNKf48= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571925707186652.3684489130694; Thu, 24 Oct 2019 07:01:47 -0700 (PDT) Received: from localhost ([::1]:43522 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdgO-0000II-FW for importer@patchew.org; Thu, 24 Oct 2019 10:01:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51477) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckM-0005Kg-AX for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckG-0002TH-Tm for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:46 -0400 Received: from mga11.intel.com ([192.55.52.93]:40482) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckG-0002MX-KR for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:40 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:33 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:30 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156218" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 08/22] intel_iommu: provide get_iommu_context() callback Date: Thu, 24 Oct 2019 08:34:29 -0400 Message-Id: <1571920483-3382-9-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 get_iommu_context() callback to return an iommu_context Intel VT-d platform. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 57 ++++++++++++++++++++++++++++++++++++++-= ---- include/hw/i386/intel_iommu.h | 14 ++++++++++- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 67a7836..e9f8692 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3288,22 +3288,33 @@ static const MemoryRegionOps vtd_mem_ir_ops =3D { }, }; =20 -VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devf= n) +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]; + VTDBus *vtd_bus; =20 + vtd_iommu_lock(s); + vtd_bus =3D g_hash_table_lookup(s->vtd_as_by_busptr, &key); 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) + PCI_DEVFN_MAX * \ + (sizeof(VTDAddressSpace *) + sizeof(VTDIOMMUContext *)= )); vtd_bus->bus =3D bus; g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus); } + vtd_iommu_unlock(s); + return vtd_bus; +} + +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); =20 vtd_dev_as =3D vtd_bus->dev_as[devfn]; =20 @@ -3370,6 +3381,27 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s,= PCIBus *bus, int devfn) return vtd_dev_as; } =20 +VTDIOMMUContext *vtd_find_add_ic(IntelIOMMUState *s, + PCIBus *bus, int devfn) +{ + VTDBus *vtd_bus; + VTDIOMMUContext *vtd_dev_ic; + + vtd_bus =3D vtd_find_add_bus(s, bus); + + vtd_dev_ic =3D vtd_bus->dev_ic[devfn]; + + if (!vtd_dev_ic) { + vtd_bus->dev_ic[devfn] =3D vtd_dev_ic =3D + g_malloc0(sizeof(VTDIOMMUContext)); + vtd_dev_ic->vtd_bus =3D vtd_bus; + vtd_dev_ic->devfn =3D (uint8_t)devfn; + vtd_dev_ic->iommu_state =3D s; + iommu_context_init(&vtd_dev_ic->iommu_context); + } + return vtd_dev_ic; +} + static uint64_t get_naturally_aligned_size(uint64_t start, uint64_t size, int gaw) { @@ -3666,8 +3698,21 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus,= void *opaque, int devfn) return &vtd_as->as; } =20 +static IOMMUContext *vtd_dev_iommu_context(PCIBus *bus, + void *opaque, int devfn) +{ + IntelIOMMUState *s =3D opaque; + VTDIOMMUContext *vtd_ic; + + assert(0 <=3D devfn && devfn < PCI_DEVFN_MAX); + + vtd_ic =3D vtd_find_add_ic(s, bus, devfn); + return &vtd_ic->iommu_context; +} + static PCIIOMMUOps vtd_iommu_ops =3D { .get_address_space =3D vtd_host_dma_iommu, + .get_iommu_context =3D vtd_dev_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 6062588..1c580c1 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -68,6 +68,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 VTDIOMMUContext VTDIOMMUContext; =20 /* Context-Entry */ struct VTDContextEntry { @@ -116,9 +117,19 @@ struct VTDAddressSpace { IOVATree *iova_tree; /* Traces mapped IOVA ranges */ }; =20 +struct VTDIOMMUContext { + VTDBus *vtd_bus; + uint8_t devfn; + IOMMUContext iommu_context; + IntelIOMMUState *iommu_state; +}; + struct VTDBus { PCIBus* bus; /* A reference to the bus to provide translation for */ - VTDAddressSpace *dev_as[0]; /* A table of VTDAddressSpace objects inde= xed by devfn */ + /* A table of VTDAddressSpace objects indexed by devfn */ + VTDAddressSpace *dev_as[PCI_DEVFN_MAX]; + /* A table of VTDIOMMUContext objects indexed by devfn */ + VTDIOMMUContext *dev_ic[PCI_DEVFN_MAX]; }; =20 struct VTDIOTLBEntry { @@ -282,5 +293,6 @@ struct IntelIOMMUState { * create a new one if none exists */ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devf= n); +VTDIOMMUContext *vtd_find_add_ic(IntelIOMMUState *s, PCIBus *bus, int devf= n); =20 #endif --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571927221; cv=none; d=zoho.com; s=zohoarc; b=iQFn6vzdKYA0DBWtt5DDzveuD1YkQZPecPex5C+wMYq8Th/mB6Bpl/XZer4GotEAph9XonoP7xP/n9a8UIpQe/4b8ZC+fkqbGt1q31fRL6Hwo1O1W8DV9OeLQGpDfaM3a5M1fLYnFtepYSzcEDkUbpYUQlOvOSljg2GoTTRohvQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571927221; 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=P3BAk4xZr3dRNjXWDb1bX1NuBkKfzq1zO2vvNq25SqM=; b=Hx6sNN7uuVbIGoxA7uASzA7Jh5M2swtmurGWHaYpwuZ3bVn1+DX7nvUSyHj66T7s9TY5LhmzXcI26KlSfB7Al0TJb1KrdQUwANnfhztsXosztWqRvDof+z/OrWCClfAV5JilGC3VqKS/asgtshGNp7cLwcONR4lx/ZIA1602Iao= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571927221257628.422942319192; Thu, 24 Oct 2019 07:27:01 -0700 (PDT) Received: from localhost ([::1]:44192 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNe4p-0007pG-EE for importer@patchew.org; Thu, 24 Oct 2019 10:26:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51416) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckI-0005G2-RM for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckG-0002TN-UA for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:42 -0400 Received: from mga11.intel.com ([192.55.52.93]:40494) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckG-0002O1-KY for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:40 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:36 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:33 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156239" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 09/22] vfio/pci: add iommu_context notifier for pasid alloc/free Date: Thu, 24 Oct 2019 08:34:30 -0400 Message-Id: <1571920483-3382-10-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 pasid alloc/free notifiers for vfio-pci. It is supposed to be fired by vIOMMU. VFIO then sends PASID allocation or free request to host. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Signed-off-by: Liu Yi L --- hw/vfio/common.c | 9 ++++++ hw/vfio/pci.c | 81 ++++++++++++++++++++++++++++++++++++++++++++= ++++ include/hw/iommu/iommu.h | 15 +++++++++ 3 files changed, 105 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index d418527..e6ad21c 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1436,6 +1436,7 @@ static void vfio_disconnect_container(VFIOGroup *grou= p) if (QLIST_EMPTY(&container->group_list)) { VFIOAddressSpace *space =3D container->space; VFIOGuestIOMMU *giommu, *tmp; + VFIOIOMMUContext *giommu_ctx, *ctx; =20 QLIST_REMOVE(container, next); =20 @@ -1446,6 +1447,14 @@ static void vfio_disconnect_container(VFIOGroup *gro= up) g_free(giommu); } =20 + QLIST_FOREACH_SAFE(giommu_ctx, &container->iommu_ctx_list, + iommu_ctx_next, ctx) { + iommu_ctx_notifier_unregister(giommu_ctx->iommu_ctx, + &giommu_ctx->n); + QLIST_REMOVE(giommu_ctx, iommu_ctx_next); + g_free(giommu_ctx); + } + trace_vfio_disconnect_container(container->fd); close(container->fd); g_free(container); diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 12fac39..8721ff6 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2699,11 +2699,80 @@ static void vfio_unregister_req_notifier(VFIOPCIDev= ice *vdev) vdev->req_enabled =3D false; } =20 +static void vfio_register_iommu_ctx_notifier(VFIOPCIDevice *vdev, + IOMMUContext *iommu_ctx, + IOMMUCTXNotifyFn fn, + IOMMUCTXEvent event) +{ + VFIOContainer *container =3D vdev->vbasedev.group->container; + VFIOIOMMUContext *giommu_ctx; + + giommu_ctx =3D g_malloc0(sizeof(*giommu_ctx)); + giommu_ctx->container =3D container; + giommu_ctx->iommu_ctx =3D iommu_ctx; + QLIST_INSERT_HEAD(&container->iommu_ctx_list, + giommu_ctx, + iommu_ctx_next); + iommu_ctx_notifier_register(iommu_ctx, + &giommu_ctx->n, + fn, + event); +} + +static void vfio_iommu_pasid_alloc_notify(IOMMUCTXNotifier *n, + IOMMUCTXEventData *event_data) +{ + VFIOIOMMUContext *giommu_ctx =3D container_of(n, VFIOIOMMUContext, n); + VFIOContainer *container =3D giommu_ctx->container; + IOMMUCTXPASIDReqDesc *pasid_req =3D + (IOMMUCTXPASIDReqDesc *) event_data->data; + struct vfio_iommu_type1_pasid_request req; + unsigned long argsz; + int pasid; + + argsz =3D sizeof(req); + req.argsz =3D argsz; + req.flag =3D VFIO_IOMMU_PASID_ALLOC; + req.min_pasid =3D pasid_req->min_pasid; + req.max_pasid =3D pasid_req->max_pasid; + + pasid =3D ioctl(container->fd, VFIO_IOMMU_PASID_REQUEST, &req); + if (pasid < 0) { + error_report("%s: %d, alloc failed", __func__, -errno); + } + pasid_req->alloc_result =3D pasid; +} + +static void vfio_iommu_pasid_free_notify(IOMMUCTXNotifier *n, + IOMMUCTXEventData *event_data) +{ + VFIOIOMMUContext *giommu_ctx =3D container_of(n, VFIOIOMMUContext, n); + VFIOContainer *container =3D giommu_ctx->container; + IOMMUCTXPASIDReqDesc *pasid_req =3D + (IOMMUCTXPASIDReqDesc *) event_data->data; + struct vfio_iommu_type1_pasid_request req; + unsigned long argsz; + int ret =3D 0; + + argsz =3D sizeof(req); + req.argsz =3D argsz; + req.flag =3D VFIO_IOMMU_PASID_FREE; + req.pasid =3D pasid_req->pasid; + + ret =3D ioctl(container->fd, VFIO_IOMMU_PASID_REQUEST, &req); + if (ret !=3D 0) { + error_report("%s: %d, pasid %u free failed", + __func__, -errno, (unsigned) pasid_req->pasid); + } + pasid_req->free_result =3D ret; +} + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev =3D PCI_VFIO(pdev); VFIODevice *vbasedev_iter; VFIOGroup *group; + IOMMUContext *iommu_context; char *tmp, *subsys, group_path[PATH_MAX], *group_name; Error *err =3D NULL; ssize_t len; @@ -3000,6 +3069,18 @@ static void vfio_realize(PCIDevice *pdev, Error **er= rp) vfio_register_req_notifier(vdev); vfio_setup_resetfn_quirk(vdev); =20 + iommu_context =3D pci_device_iommu_context(pdev); + if (iommu_context) { + vfio_register_iommu_ctx_notifier(vdev, + iommu_context, + vfio_iommu_pasid_alloc_notify, + IOMMU_CTX_EVENT_PASID_ALLOC); + vfio_register_iommu_ctx_notifier(vdev, + iommu_context, + vfio_iommu_pasid_free_notify, + IOMMU_CTX_EVENT_PASID_FREE); + } + return; =20 out_teardown: diff --git a/include/hw/iommu/iommu.h b/include/hw/iommu/iommu.h index c22c442..4352afd 100644 --- a/include/hw/iommu/iommu.h +++ b/include/hw/iommu/iommu.h @@ -31,10 +31,25 @@ typedef struct IOMMUContext IOMMUContext; =20 enum IOMMUCTXEvent { + IOMMU_CTX_EVENT_PASID_ALLOC, + IOMMU_CTX_EVENT_PASID_FREE, IOMMU_CTX_EVENT_NUM, }; typedef enum IOMMUCTXEvent IOMMUCTXEvent; =20 +union IOMMUCTXPASIDReqDesc { + struct { + uint32_t min_pasid; + uint32_t max_pasid; + int32_t alloc_result; /* pasid allocated for the alloc request */ + }; + struct { + uint32_t pasid; /* pasid to be free */ + int free_result; + }; +}; +typedef union IOMMUCTXPASIDReqDesc IOMMUCTXPASIDReqDesc; + struct IOMMUCTXEventData { IOMMUCTXEvent event; uint64_t length; --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571923668; cv=none; d=zoho.com; s=zohoarc; b=FjFcsby3q0UjArTKS1R8jyt4iFk2xDmJrb+ayOV6ncXnibETDouztaB1yZ1NONG2xrLmdG1zlXV12rh8ssbBUIy37movIt20piUDI1Oxq+DelsgeBy+4MzruzdSCqGAHrSQToR9ly7fjwJqp6uf0GgM8bccnvrYzgCY1xR/HBCE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571923668; 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=6mW18+yvdo02juWLJfeZM9FZOmnLECMeVhfzJ1Gxs1Y=; b=hMhkm4EM9I89+BVUf9tS+n4lugyItpmlsL5lJQIBVGXa9FsZ+xlCzaIM0RFzOzx2ey0muykQ/S9iN0Py+Zruhc4Fy9mEoKB4veQ/sjvQbfOWXYngYEycnjfZOaXFDq7p1NxAiV+x+MLwZJ3/az7iVrG8snZGL+hLorHrE/G+OVw= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571923668783620.5134473036447; Thu, 24 Oct 2019 06:27:48 -0700 (PDT) Received: from localhost ([::1]:42598 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNd9V-0002cq-VI for importer@patchew.org; Thu, 24 Oct 2019 09:27:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51431) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckJ-0005Gq-L3 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckH-0002Tc-7x for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:43 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckG-0002NX-Ug for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:41 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:39 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:36 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156260" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 10/22] intel_iommu: add virtual command capability support Date: Thu, 24 Oct 2019 08:34:31 -0400 Message-Id: <1571920483-3382-11-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 virtual command support to Intel vIOMMU per Intel VT-d 3.1 spec. And adds two virtual commands: alloc_pasid and free_pasid. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L Signed-off-by: Yi Sun --- hw/i386/intel_iommu.c | 162 +++++++++++++++++++++++++++++++++++++= +++- hw/i386/intel_iommu_internal.h | 38 ++++++++++ hw/i386/trace-events | 1 + include/hw/i386/intel_iommu.h | 6 +- 4 files changed, 205 insertions(+), 2 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index e9f8692..88b843f 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -944,6 +944,7 @@ static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState= *s, uint8_t bus_num) return vtd_bus; } } + vtd_bus =3D NULL; } return vtd_bus; } @@ -2590,6 +2591,140 @@ static void vtd_handle_iectl_write(IntelIOMMUState = *s) } } =20 +static int vtd_request_pasid_alloc(IntelIOMMUState *s) +{ + VTDBus *vtd_bus; + int bus_n, devfn; + IOMMUCTXEventData event_data; + IOMMUCTXPASIDReqDesc req; + VTDIOMMUContext *vtd_ic; + + event_data.event =3D IOMMU_CTX_EVENT_PASID_ALLOC; + event_data.data =3D &req; + req.min_pasid =3D VTD_MIN_HPASID; + req.max_pasid =3D VTD_MAX_HPASID; + req.alloc_result =3D 0; + event_data.length =3D sizeof(req); + for (bus_n =3D 0; bus_n < PCI_BUS_MAX; bus_n++) { + vtd_bus =3D vtd_find_as_from_bus_num(s, bus_n); + if (!vtd_bus) { + continue; + } + for (devfn =3D 0; devfn < PCI_DEVFN_MAX; devfn++) { + vtd_ic =3D vtd_bus->dev_ic[devfn]; + if (!vtd_ic) { + continue; + } + iommu_ctx_event_notify(&vtd_ic->iommu_context, &event_data); + if (req.alloc_result > 0) { + return req.alloc_result; + } + } + } + return -1; +} + +static int vtd_request_pasid_free(IntelIOMMUState *s, uint32_t pasid) +{ + VTDBus *vtd_bus; + int bus_n, devfn; + IOMMUCTXEventData event_data; + IOMMUCTXPASIDReqDesc req; + VTDIOMMUContext *vtd_ic; + + event_data.event =3D IOMMU_CTX_EVENT_PASID_FREE; + event_data.data =3D &req; + req.pasid =3D pasid; + req.free_result =3D 0; + event_data.length =3D sizeof(req); + for (bus_n =3D 0; bus_n < PCI_BUS_MAX; bus_n++) { + vtd_bus =3D vtd_find_as_from_bus_num(s, bus_n); + if (!vtd_bus) { + continue; + } + for (devfn =3D 0; devfn < PCI_DEVFN_MAX; devfn++) { + vtd_ic =3D vtd_bus->dev_ic[devfn]; + if (!vtd_ic) { + continue; + } + iommu_ctx_event_notify(&vtd_ic->iommu_context, &event_data); + if (req.free_result =3D=3D 0) { + return 0; + } + } + } + return -1; +} + +/* + * If IP is not set, set it and return 0 + * If IP is already set, return -1 + */ +static int vtd_vcmd_rsp_ip_check(IntelIOMMUState *s) +{ + if (!(s->vccap & VTD_VCCAP_PAS) || + (s->vcrsp & 1)) { + return -1; + } + s->vcrsp =3D 1; + vtd_set_quad_raw(s, DMAR_VCRSP_REG, + ((uint64_t) s->vcrsp)); + return 0; +} + +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); + + /* + * 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. + */ + ret =3D vtd_vcmd_rsp_ip_check(s); + if (ret) { + return ret; + } + switch (val & VTD_VCMD_CMD_MASK) { + case VTD_VCMD_ALLOC_PASID: + ret =3D vtd_request_pasid_alloc(s); + if (ret < 0) { + s->vcrsp |=3D VTD_VCRSP_SC(VTD_VCMD_NO_AVAILABLE_PASID); + } else { + s->vcrsp |=3D VTD_VCRSP_RSLT(ret); + } + 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); + printf("Virtual Command: unsupported command!!!\n"); + break; + } + vtd_vcmd_clear_ip(s); + return 0; +} + static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size) { IntelIOMMUState *s =3D opaque; @@ -2879,6 +3014,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); @@ -3617,7 +3769,8 @@ static void vtd_init(IntelIOMMUState *s) s->ecap |=3D VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS; } else if (!strcmp(s->scalable_mode, "modern")) { s->ecap |=3D VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_PASID - | VTD_ECAP_FLTS | VTD_ECAP_PSS; + | VTD_ECAP_FLTS | VTD_ECAP_PSS | VTD_ECAP_VCS; + s->vccap |=3D VTD_VCCAP_PAS; } } =20 @@ -3674,6 +3827,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 diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index be7b30a..8668771 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 */ @@ -193,6 +199,7 @@ #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 @@ -315,6 +322,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_MIN_HPASID 1 +#define VTD_MAX_HPASID 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 c8bc464..43c0314 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 1c580c1..0d49480 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -46,7 +46,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 @@ -282,6 +282,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 */ + /* * Protects IOMMU states in general. Currently it protects the * per-IOMMU IOTLB cache, and context entry cache in VTDAddressSpace. --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571924396; cv=none; d=zoho.com; s=zohoarc; b=DTvsOy4fC4/FvZZYHVJgieo+Xu5rBOG8cJYoQGOTfWITYZA0utmg/Wb/WOsGZZk+8tRjmXRt7TltoQQkmoZJMqWcViu4AqlRUVa5nyK4P4deCI5ipCTZc0J/2Us4L6Esr+lJy1oV4uYO/IaoVZfRM9hFRTBU6twn71KV6ulmvMg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571924396; 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=TxkieizXd5n+ssPDCyjNB6C3j6Ox4X26hqEC/kIMjj8=; b=fNgcogm6FsBCDdIP2ptamxFLVcfWtBJefQ6EvGgNm8zHH3Z0dTU2MVD1cCaj1ZWaS1wDIFsl0rmjgSuFTw0TAyq4+usQPNhhnmzQs20R6ymIHcxPFMCKm4F/ZuySjHT0mp6cebxo3g7Jg5rRHphmuTmjQFWttZliJKKubb6fvik= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571924395929163.60463591838504; Thu, 24 Oct 2019 06:39:55 -0700 (PDT) Received: from localhost ([::1]:42802 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdLC-0003Lm-23 for importer@patchew.org; Thu, 24 Oct 2019 09:39:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51487) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckO-0005QB-Vf for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckJ-0002VZ-Ib for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:48 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckJ-0002NX-4o for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:43 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:42 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:39 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156267" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 11/22] intel_iommu: process pasid cache invalidation Date: Thu, 24 Oct 2019 08:34:32 -0400 Message-Id: <1571920483-3382-12-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 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. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 66 ++++++++++++++++++++++++++++++++++++++= ---- hw/i386/intel_iommu_internal.h | 12 ++++++++ hw/i386/trace-events | 3 ++ 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 88b843f..84ff6f0 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -2335,6 +2335,63 @@ static bool vtd_process_iotlb_desc(IntelIOMMUState *= s, VTDInvDesc *inv_desc) return true; } =20 +static int vtd_pasid_cache_dsi(IntelIOMMUState *s, uint16_t domain_id) +{ + return 0; +} + +static int vtd_pasid_cache_psi(IntelIOMMUState *s, + uint16_t domain_id, uint32_t pasid) +{ + return 0; +} + +static int vtd_pasid_cache_gsi(IntelIOMMUState *s) +{ + return 0; +} + +static bool vtd_process_pasid_desc(IntelIOMMUState *s, + VTDInvDesc *inv_desc) +{ + uint16_t domain_id; + uint32_t pasid; + int ret =3D 0; + + 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; + } + + 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: + ret =3D vtd_pasid_cache_dsi(s, domain_id); + break; + + case VTD_INV_DESC_PASIDC_PASID_SI: + ret =3D vtd_pasid_cache_psi(s, domain_id, pasid); + break; + + case VTD_INV_DESC_PASIDC_GLOBAL: + ret =3D vtd_pasid_cache_gsi(s); + 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 (ret =3D=3D 0) ? true : false; +} + static bool vtd_process_inv_iec_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) { @@ -2441,12 +2498,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 8668771..c6cb28b 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -445,6 +445,18 @@ typedef union VTDInvDesc VTDInvDesc; #define VTD_SPTE_LPAGE_L4_RSVD_MASK(aw) \ (0x880ULL | ~(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 43c0314..6da8bd2 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 Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571925901; cv=none; d=zoho.com; s=zohoarc; b=ktyNM+n/Egrex6+U9NhvH9GgOdfXMouzy9nF8kYpNe59y+GG/FtOM9jFBuhgRRA3GOhmuB7fM3RsMx/jDxe2CKDFB4BezuPi3LVvSqVvk1JQC2SMES+Aefr0czRmGpMnQVUzO1G3vjBMfqyag+JgvA4WqACr3BTGDXrqF6dTv/s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571925901; 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=T6Pu+jsc6TQr+nEwrmUvQDkVcA0RSoArXPb51C56hj4=; b=d1ofeCJADDAA8T8WoErMTwFEC1P+ZTO0TxrlbLnj9py0OpxWbuYJJgKhvfPFlH9y2gDsZRp7CELUzgLb5BSzw2at/DsjlbrM9FN8mI3XIcvO4I7nTqa4aZrbz15Ce6Vyw3X8+jjI+r+aS7ZSUBKe0GFHSufQYOaMpn+yYQqL9ok= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571925901725263.4996288589965; Thu, 24 Oct 2019 07:05:01 -0700 (PDT) Received: from localhost ([::1]:43610 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdjX-0005zI-Gk for importer@patchew.org; Thu, 24 Oct 2019 10:04:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51551) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckY-0005dW-1A for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckW-0002fF-DQ for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:57 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckW-0002NX-1M for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:01:56 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:45 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:42 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156277" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 12/22] intel_iommu: add present bit check for pasid table entries Date: Thu, 24 Oct 2019 08:34:33 -0400 Message-Id: <1571920483-3382-13-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 present bit check for pasid entry (pe) and pasid directory entry (pdire) were missed in previous commits as fpd bit check doesn't require present bit as "Set". This patch adds the present bit check for callers which wants to get a valid pe/pdire. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L Reviewed-by: Peter Xu --- hw/i386/intel_iommu.c | 92 +++++++++++++++++++++++++++++++++-----= ---- hw/i386/intel_iommu_internal.h | 1 + 2 files changed, 74 insertions(+), 19 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 84ff6f0..90b8f6c 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -686,9 +686,18 @@ static inline bool vtd_pe_type_check(X86IOMMUState *x8= 6_iommu, return true; } =20 -static int vtd_get_pasid_dire(dma_addr_t pasid_dir_base, - uint32_t pasid, - VTDPASIDDirEntry *pdire) +static inline bool vtd_pdire_present(VTDPASIDDirEntry *pdire) +{ + return pdire->val & 1; +} + +/** + * Caller of this function should check present bit if wants + * to use pdir entry for futher usage except for fpd bit check. + */ +static int vtd_get_pdire_from_pdir_table(dma_addr_t pasid_dir_base, + uint32_t pasid, + VTDPASIDDirEntry *pdire) { uint32_t index; dma_addr_t addr, entry_size; @@ -703,18 +712,22 @@ static int vtd_get_pasid_dire(dma_addr_t pasid_dir_ba= se, return 0; } =20 -static int vtd_get_pasid_entry(IntelIOMMUState *s, - uint32_t pasid, - VTDPASIDDirEntry *pdire, - VTDPASIDEntry *pe) +static inline bool vtd_pe_present(VTDPASIDEntry *pe) +{ + return pe->val[0] & VTD_PASID_ENTRY_P; +} + +static int vtd_get_pe_in_pasid_leaf_table(IntelIOMMUState *s, + uint32_t pasid, + dma_addr_t addr, + VTDPASIDEntry *pe) { uint32_t index; - dma_addr_t addr, entry_size; + dma_addr_t entry_size; X86IOMMUState *x86_iommu =3D X86_IOMMU_DEVICE(s); =20 index =3D VTD_PASID_TABLE_INDEX(pasid); entry_size =3D VTD_PASID_ENTRY_SIZE; - addr =3D pdire->val & VTD_PASID_TABLE_BASE_ADDR_MASK; addr =3D addr + index * entry_size; if (dma_memory_read(&address_space_memory, addr, pe, entry_size)) { return -VTD_FR_PASID_TABLE_INV; @@ -732,25 +745,54 @@ static int vtd_get_pasid_entry(IntelIOMMUState *s, return 0; } =20 -static int vtd_get_pasid_entry_from_pasid(IntelIOMMUState *s, - dma_addr_t pasid_dir_base, - uint32_t pasid, - VTDPASIDEntry *pe) +/** + * Caller of this function should check present bit if wants + * to use pasid entry for futher usage except for fpd bit check. + */ +static int vtd_get_pe_from_pdire(IntelIOMMUState *s, + uint32_t pasid, + VTDPASIDDirEntry *pdire, + VTDPASIDEntry *pe) +{ + dma_addr_t addr =3D pdire->val & VTD_PASID_TABLE_BASE_ADDR_MASK; + + return vtd_get_pe_in_pasid_leaf_table(s, pasid, addr, pe); +} + +/** + * This function gets a pasid entry from a specified pasid + * table (includes dir and leaf table) with a specified pasid. + * Sanity check should be done to ensure return a present + * pasid entry to caller. + */ +static int vtd_get_pe_from_pasid_table(IntelIOMMUState *s, + dma_addr_t pasid_dir_base, + uint32_t pasid, + VTDPASIDEntry *pe) { int ret; VTDPASIDDirEntry pdire; =20 - ret =3D vtd_get_pasid_dire(pasid_dir_base, pasid, &pdire); + ret =3D vtd_get_pdire_from_pdir_table(pasid_dir_base, + pasid, &pdire); if (ret) { return ret; } =20 - ret =3D vtd_get_pasid_entry(s, pasid, &pdire, pe); + if (!vtd_pdire_present(&pdire)) { + return -VTD_FR_PASID_TABLE_INV; + } + + ret =3D vtd_get_pe_from_pdire(s, pasid, &pdire, pe); if (ret) { return ret; } =20 - return ret; + if (!vtd_pe_present(pe)) { + return -VTD_FR_PASID_TABLE_INV; + } + + return 0; } =20 static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s, @@ -763,7 +805,7 @@ static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *= s, =20 pasid =3D VTD_CE_GET_RID2PASID(ce); pasid_dir_base =3D VTD_CE_GET_PASID_DIR_TABLE(ce); - ret =3D vtd_get_pasid_entry_from_pasid(s, pasid_dir_base, pasid, pe); + ret =3D vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe); =20 return ret; } @@ -781,7 +823,11 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s, pasid =3D VTD_CE_GET_RID2PASID(ce); pasid_dir_base =3D VTD_CE_GET_PASID_DIR_TABLE(ce); =20 - ret =3D vtd_get_pasid_dire(pasid_dir_base, pasid, &pdire); + /* + * No present bit check since fpd is meaningful even + * if the present bit is clear. + */ + ret =3D vtd_get_pdire_from_pdir_table(pasid_dir_base, pasid, &pdire); if (ret) { return ret; } @@ -791,7 +837,15 @@ static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s, return 0; } =20 - ret =3D vtd_get_pasid_entry(s, pasid, &pdire, &pe); + if (!vtd_pdire_present(&pdire)) { + return -VTD_FR_PASID_TABLE_INV; + } + + /* + * No present bit check since fpd is meaningful even + * if the present bit is clear. + */ + ret =3D vtd_get_pe_from_pdire(s, pasid, &pdire, &pe); if (ret) { return ret; } diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index c6cb28b..879211e 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -529,6 +529,7 @@ typedef struct VTDRootEntry VTDRootEntry; #define VTD_PASID_ENTRY_FPD (1ULL << 1) /* Fault Processing Disa= ble */ =20 /* PASID Granular Translation Type Mask */ +#define VTD_PASID_ENTRY_P 1ULL #define VTD_SM_PASID_ENTRY_PGTT (7ULL << 6) #define VTD_SM_PASID_ENTRY_FLT (1ULL << 6) #define VTD_SM_PASID_ENTRY_SLT (2ULL << 6) --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571926083; cv=none; d=zoho.com; s=zohoarc; b=IZ7V6KACh3RuseYteRshU2z704IWgvZ9md5gFpUyahmz9vXjtuvMZ9je2yCZ0A8CTsjH3BfYpABWYZOqY6RNMThqZp/l6aXVta7JP1OM0EEv9Dig12tsuWQpRqrbxQCBetJoxmnEc05ZqeuwjvEMd9oF81F1kQAeUS4vHbOM9MM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571926083; 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=9LPeR9w2/QgSHD5YTjAK72RbCI9tf8oft7VwYMK+iDo=; b=V5JF30Vw1r7LyHkPCdV7UVDdIoGDNLzGMpmnPil1FBOehCwAbKf3w/ezKy7G1zxRfmwK1YCCgyTOU++ES1Q9xqy6C+jiYemVZXX+ByCRehO2Amdd0PlZ8o6HbhkePzN+9tH2gtml5vscLAEUw3HCoHYfeyAyR0lKRARLXd5g7qU= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571926083150209.19474393473058; Thu, 24 Oct 2019 07:08:03 -0700 (PDT) Received: from localhost ([::1]:43656 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdmR-0004Mt-AF for importer@patchew.org; Thu, 24 Oct 2019 10:07:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51711) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckh-0005mk-50 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNcka-0002hV-5T for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:06 -0400 Received: from mga11.intel.com ([192.55.52.93]:40516) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckZ-0002aG-QB for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:00 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:48 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:45 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156288" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 13/22] intel_iommu: add PASID cache management infrastructure Date: Thu, 24 Oct 2019 08:34:34 -0400 Message-Id: <1571920483-3382-14-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 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. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 356 +++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 10 ++ hw/i386/trace-events | 1 + include/hw/i386/intel_iommu.h | 36 ++++- 4 files changed, 402 insertions(+), 1 deletion(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 90b8f6c..d8827c9 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,11 @@ 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 bool vtd_pdire_present(VTDPASIDDirEntry *pdire) { return pdire->val & 1; @@ -2389,19 +2398,361 @@ static bool vtd_process_iotlb_desc(IntelIOMMUState= *s, VTDInvDesc *inv_desc) return true; } =20 +static inline struct pasid_key *vtd_get_pasid_key(uint32_t pasid, + uint16_t sid) +{ + struct pasid_key *key =3D g_malloc0(sizeof(*key)); + key->pasid =3D pasid; + key->sid =3D sid; + return key; +} + +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 bool vtd_pc_is_dom_si(struct VTDPASIDCacheInfo *pc_info) +{ + return pc_info->flags & VTD_PASID_CACHE_DOMSI; +} + +static inline bool vtd_pc_is_pasid_si(struct VTDPASIDCacheInfo *pc_info) +{ + return pc_info->flags & VTD_PASID_CACHE_PASIDSI; +} + +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) +{ + int i =3D 0; + while (i < sizeof(*p1) / sizeof(p1->val)) { + if (p1->val[i] !=3D p2->val[i]) { + return false; + } + i++; + } + return true; +} + +/** + * This function is used to clear pasid_cache_gen of 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; + gboolean remove =3D false; + + did =3D vtd_pe_get_domain_id(&pc_entry->pasid_entry); + pasid =3D vtd_pasid_as->pasid; + devfn =3D vtd_pasid_as->devfn; + + if (pc_entry->pasid_cache_gen && + (vtd_pc_is_dom_si(pc_info) ? (pc_info->domain_id =3D=3D did) : 1) = && + (vtd_pc_is_pasid_si(pc_info) ? (pc_info->pasid =3D=3D pasid) : 1))= { + /* + * Modify pasid_cache_gen to be 0, the cached pasid entry in + * vtd_pasid_as instance is invalid. And vtd_pasid_as instance + * would be treated as invalid in QEMU scope until the pasid + * cache gen is updated in a new pasid binding or updated in + * below logic if found guest pasid entry exists. + */ + remove =3D true; + pc_entry->pasid_cache_gen =3D 0; + if (vtd_bus->dev_ic[devfn]) { + if (!vtd_dev_get_pe_from_pasid(s, + pci_bus_num(vtd_bus->bus), devfn, pasid, &pe)) { + /* + * pasid entry exists, so keep the vtd_pasid_as, and needs + * update the pasid entry cached in vtd_pasid_as. Also, if + * the guest pasid entry doesn't equal to cached pasid ent= ry + * needs to issue a pasid bind to host for passthru devies. + */ + remove =3D false; + pc_entry->pasid_cache_gen =3D s->pasid_cache_gen; + if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry))= { + pc_entry->pasid_entry =3D pe; + /* + * TODO: when pasid-base-iotlb(piotlb) infrastructure = is + * ready, should invalidate QEMU piotlb togehter with = this + * change. + */ + } + } + } + } + + return remove; +} + static int vtd_pasid_cache_dsi(IntelIOMMUState *s, uint16_t domain_id) { + VTDPASIDCacheInfo pc_info; + + trace_vtd_pasid_cache_dsi(domain_id); + + pc_info.flags =3D VTD_PASID_CACHE_DOMSI; + pc_info.domain_id =3D domain_id; + + /* + * Loop all existing pasid caches and update them. + */ + vtd_iommu_lock(s); + g_hash_table_foreach_remove(s->vtd_pasid_as, vtd_flush_pasid, &pc_info= ); + vtd_iommu_unlock(s); + + /* + * TODO: Domain selective PASID cache invalidation + * may be issued wrongly by programmer, to be safe, + * after invalidating the pasid caches, emulator + * needs to replay the pasid bindings by walking guest + * pasid dir and pasid table. + */ return 0; } =20 +/** + * 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, + bool allocate) +{ + 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); + key =3D vtd_get_pasid_key(pasid, sid); + vtd_pasid_as =3D g_hash_table_lookup(s->vtd_pasid_as, key); + + if (!vtd_pasid_as && allocate) { + new_key =3D vtd_get_pasid_key(pasid, sid); + /* + * 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 be fully initialized. + * e.g. the address_space and memory region fields. + */ + 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->context_cache_entry.context_cache_gen =3D 0; + vtd_pasid_as->pasid =3D pasid; + vtd_pasid_as->pasid_cache_entry.pasid_cache_gen =3D 0; + g_hash_table_insert(s->vtd_pasid_as, new_key, vtd_pasid_as); + } + return vtd_pasid_as; +} + + /** + * This function updates the pasid entry cached in &vtd_pasid_as. + * Caller of this function should hold iommu_lock. + */ +static inline void vtd_fill_in_pe_cache( + VTDPASIDAddressSpace *vtd_pasid_as, VTDPASIDEntry *pe) +{ + IntelIOMMUState *s =3D vtd_pasid_as->iommu_state; + VTDPASIDCacheEntry *pc_entry =3D &vtd_pasid_as->pasid_cache_entry; + + pc_entry->pasid_entry =3D *pe; + pc_entry->pasid_cache_gen =3D s->pasid_cache_gen; +} + static int vtd_pasid_cache_psi(IntelIOMMUState *s, uint16_t domain_id, uint32_t pasid) { + VTDPASIDCacheInfo pc_info; + VTDPASIDEntry pe; + VTDBus *vtd_bus; + int bus_n, devfn; + VTDPASIDAddressSpace *vtd_pasid_as; + VTDIOMMUContext *vtd_ic; + + pc_info.flags =3D VTD_PASID_CACHE_DOMSI; + pc_info.domain_id =3D domain_id; + pc_info.flags |=3D VTD_PASID_CACHE_PASIDSI; + pc_info.pasid =3D pasid; + + /* + * Regards to a pasid selective pasid cache invalidation (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 + * + * Here the handling of a PSI is: + * 1) loop all the exisitng 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) + * However, it is not good to always loop all devices. In this + * implementation. We do it in this ways: + * - For devices which have VTDIOMMUContext 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 VTDIOMMUContext instances, it is + * not necessary to create pasid cache at this phase since it + * could be created when vIOMMU do DMA address translation. + * This is not implemented yet since no PASID-capable emulated + * devices today. If we have it in future, the pasid cache shall + * be created there. + */ + + vtd_iommu_lock(s); + g_hash_table_foreach_remove(s->vtd_pasid_as, vtd_flush_pasid, &pc_info= ); + vtd_iommu_unlock(s); + + vtd_iommu_lock(s); + QLIST_FOREACH(vtd_ic, &s->vtd_dev_ic_list, next) { + vtd_bus =3D vtd_ic->vtd_bus; + devfn =3D vtd_ic->devfn; + bus_n =3D pci_bus_num(vtd_bus->bus); + + /* Step 1: fetch vtd_pasid_as and check if it is valid */ + vtd_pasid_as =3D vtd_add_find_pasid_as(s, vtd_bus, + devfn, pasid, true); + if (vtd_pasid_as && + (s->pasid_cache_gen =3D=3D + vtd_pasid_as->pasid_cache_entry.pasid_cache_gen)) { + /* + * pasid_cache_gen equals to s->pasid_cache_gen means + * vtd_pasid_as is valid after the above s->vtd_pasid_as + * updates. Thus no need for the below steps. + */ + continue; + } + + /* + * Step 2: vtd_pasid_as is not valid, it's potentailly a + * new pasid bind. Fetch guest pasid entry. + */ + if (vtd_dev_get_pe_from_pasid(s, bus_n, devfn, pasid, &pe)) { + continue; + } + + /* + * Step 3: pasid entry exists, update pasid cache + * + * Here need to check domain ID since guest pasid entry + * exists. What needs to do are: + * - update the pc_entry in the vtd_pasid_as + * - set proper pc_entry.pasid_cache_gen + * - passdown the latest guest pasid entry config to host + * (will be added in later patch) + */ + if (domain_id =3D=3D vtd_pe_get_domain_id(&pe)) { + vtd_fill_in_pe_cache(vtd_pasid_as, &pe); + } + } + vtd_iommu_unlock(s); return 0; } =20 +/** + * 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 0; + + /* + * Reset pasid cache is a big hammer, so use g_hash_table_foreach_remo= ve + * which will free the vtd_pasid_as instances. + */ + g_hash_table_foreach_remove(s->vtd_pasid_as, vtd_flush_pasid, &pc_info= ); + s->pasid_cache_gen =3D 1; +} + static int vtd_pasid_cache_gsi(IntelIOMMUState *s) { + trace_vtd_pasid_cache_gsi(); + + vtd_iommu_lock(s); + vtd_pasid_cache_reset(s); + vtd_iommu_unlock(s); + + /* + * TODO: Global PASID cache invalidation may be + * issued wrongly by programmer, to be safe, after + * invalidating the pasid caches, emulator needs + * to replay the pasid bindings by walking guest + * pasid dir and pasid table. + */ return 0; } =20 @@ -3660,7 +4011,9 @@ VTDIOMMUContext *vtd_find_add_ic(IntelIOMMUState *s, vtd_dev_ic->devfn =3D (uint8_t)devfn; vtd_dev_ic->iommu_state =3D s; iommu_context_init(&vtd_dev_ic->iommu_context); + QLIST_INSERT_HEAD(&s->vtd_dev_ic_list, vtd_dev_ic, next); } + return vtd_dev_ic; } =20 @@ -4074,6 +4427,7 @@ static void vtd_realize(DeviceState *dev, Error **err= p) } =20 QLIST_INIT(&s->vtd_as_with_notifiers); + QLIST_INIT(&s->vtd_dev_ic_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, @@ -4099,6 +4453,8 @@ 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 879211e..12873e1 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -311,6 +311,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. @@ -482,6 +483,15 @@ struct VTDRootEntry { }; typedef struct VTDRootEntry VTDRootEntry; =20 +struct VTDPASIDCacheInfo { +#define VTD_PASID_CACHE_DOMSI (1ULL << 0); +#define VTD_PASID_CACHE_PASIDSI (1ULL << 1); + uint32_t flags; + uint16_t domain_id; + uint32_t pasid; +}; +typedef struct VTDPASIDCacheInfo VTDPASIDCacheInfo; + /* Masks for struct VTDRootEntry */ #define VTD_ROOT_ENTRY_P 1ULL #define VTD_ROOT_ENTRY_CTP (~0xfffULL) diff --git a/hw/i386/trace-events b/hw/i386/trace-events index 6da8bd2..7912ae1 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -22,6 +22,7 @@ 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_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 diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 0d49480..d693f71 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -69,6 +69,8 @@ typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress; typedef struct VTDPASIDDirEntry VTDPASIDDirEntry; typedef struct VTDPASIDEntry VTDPASIDEntry; typedef struct VTDIOMMUContext VTDIOMMUContext; +typedef struct VTDPASIDCacheEntry VTDPASIDCacheEntry; +typedef struct VTDPASIDAddressSpace VTDPASIDAddressSpace; =20 /* Context-Entry */ struct VTDContextEntry { @@ -101,6 +103,31 @@ struct VTDPASIDEntry { uint64_t val[8]; }; =20 +struct pasid_key { + uint32_t pasid; + uint16_t sid; +}; + +struct VTDPASIDCacheEntry { + /* + * The cache entry is obsolete if + * pasid_cache_gen!=3DIntelIOMMUState.pasid_cache_gen + */ + uint32_t pasid_cache_gen; + 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; @@ -121,6 +148,7 @@ struct VTDIOMMUContext { VTDBus *vtd_bus; uint8_t devfn; IOMMUContext iommu_context; + QLIST_ENTRY(VTDIOMMUContext) next; IntelIOMMUState *iommu_state; }; =20 @@ -269,9 +297,14 @@ 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 objects */ + uint32_t pasid_cache_gen; /* Should be in [1,MAX] */ /* list of registered notifiers */ QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers; =20 + /* list of registered notifiers */ + QLIST_HEAD(, VTDIOMMUContext) vtd_dev_ic_list; + /* interrupt remapping */ bool intr_enabled; /* Whether guest enabled IR */ dma_addr_t intr_root; /* Interrupt remapping table pointer */ @@ -288,7 +321,8 @@ struct IntelIOMMUState { =20 /* * Protects IOMMU states in general. Currently it protects the - * per-IOMMU IOTLB cache, and context entry cache in VTDAddressSpace. + * per-IOMMU IOTLB cache, and context entry cache in VTDAddressSpace, + * and pasid cache in VTDPASIDAddressSpace. */ QemuMutex iommu_lock; }; --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571923488; cv=none; d=zoho.com; s=zohoarc; b=VKBajlI1zjR+PljcqvLrsO6gF+zy46nnBRotIr1qSnq4wYaYmGOtHd/JHmKLqxyw1dJ0xlaaNoU1PDXtL7cS4Vnmy9NXpUpNePr1TLHpK4FsVXIAP1+Jp8UC7rfa0WgonC7tKPQmv/3PP1NgxO4fOsrriXK/2HY+zasplz89Djk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571923488; 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=i8DYoa4Z3aC3K9WCYqEF8RiWqJw4jQu4B6m2qbYGlrs=; b=gG5YEaX5Kj2W4FrRy4pvuYTzdfIvjHfuAYDss1CQZ4xgFhr/IFlwq39fnwp0h47ZzNNf0NihHMTZ4vENL0zDFJZkGR95eGELKerMtj9CgxWdU1QIivWbDQAvVV92k/BluWFfIFEKYTX62UtvnBhCreKUMtR7EkskeGCsHLGDXmI= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571923488637111.73558865782297; Thu, 24 Oct 2019 06:24:48 -0700 (PDT) Received: from localhost ([::1]:42534 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNd6c-0006xw-A2 for importer@patchew.org; Thu, 24 Oct 2019 09:24:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51703) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckg-0005mD-AH for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckd-0002k8-4R for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:06 -0400 Received: from mga11.intel.com ([192.55.52.93]:40529) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckc-0002c5-QZ for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:03 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:51 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:49 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156294" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 14/22] vfio/pci: add iommu_context notifier for pasid bind/unbind Date: Thu, 24 Oct 2019 08:34:35 -0400 Message-Id: <1571920483-3382-15-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 notifier for pasid bind/unbind. VFIO registers this notifier to listen to the dual-stage translation (a.k.a. nested translation) configuration changes and propagate to host. Thus vIOMMU is able to set its translation structures to host. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Signed-off-by: Liu Yi L --- hw/vfio/pci.c | 39 +++++++++++++++++++++++++++++++++++++++ include/hw/iommu/iommu.h | 11 +++++++++++ 2 files changed, 50 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 8721ff6..012b8ed 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2767,6 +2767,41 @@ static void vfio_iommu_pasid_free_notify(IOMMUCTXNot= ifier *n, pasid_req->free_result =3D ret; } =20 +static void vfio_iommu_pasid_bind_notify(IOMMUCTXNotifier *n, + IOMMUCTXEventData *event_data) +{ +#ifdef __linux__ + VFIOIOMMUContext *giommu_ctx =3D container_of(n, VFIOIOMMUContext, n); + VFIOContainer *container =3D giommu_ctx->container; + IOMMUCTXPASIDBindData *pasid_bind =3D + (IOMMUCTXPASIDBindData *) event_data->data; + struct vfio_iommu_type1_bind *bind; + struct iommu_gpasid_bind_data *bind_data; + unsigned long argsz; + + argsz =3D sizeof(*bind) + sizeof(*bind_data); + bind =3D g_malloc0(argsz); + bind->argsz =3D argsz; + bind->bind_type =3D VFIO_IOMMU_BIND_GUEST_PASID; + bind_data =3D (struct iommu_gpasid_bind_data *) &bind->data; + *bind_data =3D *pasid_bind->data; + + if (pasid_bind->flag & IOMMU_CTX_BIND_PASID) { + if (ioctl(container->fd, VFIO_IOMMU_BIND, bind) !=3D 0) { + error_report("%s: pasid (%llu:%llu) bind failed: %d", __func__, + bind_data->gpasid, bind_data->hpasid, -errno); + } + } else if (pasid_bind->flag & IOMMU_CTX_UNBIND_PASID) { + if (ioctl(container->fd, VFIO_IOMMU_UNBIND, bind) !=3D 0) { + error_report("%s: pasid (%llu:%llu) unbind failed: %d", __func= __, + bind_data->gpasid, bind_data->hpasid, -errno); + } + } + + g_free(bind); +#endif +} + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev =3D PCI_VFIO(pdev); @@ -3079,6 +3114,10 @@ static void vfio_realize(PCIDevice *pdev, Error **er= rp) iommu_context, vfio_iommu_pasid_free_notify, IOMMU_CTX_EVENT_PASID_FREE); + vfio_register_iommu_ctx_notifier(vdev, + iommu_context, + vfio_iommu_pasid_bind_notify, + IOMMU_CTX_EVENT_PASID_BIND); } =20 return; diff --git a/include/hw/iommu/iommu.h b/include/hw/iommu/iommu.h index 4352afd..4f21aa1 100644 --- a/include/hw/iommu/iommu.h +++ b/include/hw/iommu/iommu.h @@ -33,6 +33,7 @@ typedef struct IOMMUContext IOMMUContext; enum IOMMUCTXEvent { IOMMU_CTX_EVENT_PASID_ALLOC, IOMMU_CTX_EVENT_PASID_FREE, + IOMMU_CTX_EVENT_PASID_BIND, IOMMU_CTX_EVENT_NUM, }; typedef enum IOMMUCTXEvent IOMMUCTXEvent; @@ -50,6 +51,16 @@ union IOMMUCTXPASIDReqDesc { }; typedef union IOMMUCTXPASIDReqDesc IOMMUCTXPASIDReqDesc; =20 +struct IOMMUCTXPASIDBindData { +#define IOMMU_CTX_BIND_PASID (1 << 0) +#define IOMMU_CTX_UNBIND_PASID (1 << 1) + uint32_t flag; +#ifdef __linux__ + struct iommu_gpasid_bind_data *data; +#endif +}; +typedef struct IOMMUCTXPASIDBindData IOMMUCTXPASIDBindData; + struct IOMMUCTXEventData { IOMMUCTXEvent event; uint64_t length; --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571927839; cv=none; d=zoho.com; s=zohoarc; b=hOI3ZVJbAERv1QKFmqPcww36m1hqPx+L65KiDsZM9+togCcv/sHvKg5l7Jf4YfzU6qjhWUvVRiEhvwVjUv48qx9284rcJQQEzmIFgOsSOiRJeFCU0CK0uvVi37fnFlfvdYkFMterDAXHjFYGk86wYh2FbI1VWWOUxc0ZLNJRA6s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571927839; 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=KoVjFQE4lRtgp/Xz6/l1Ifh83X0aytPSCgKf6NFI3fM=; b=XKXTQFZC/bmj+PdBvIxvXhUU1o0kx0yLhKWcBPKgpD3FC0E4GUdHwO9gytq71Hb7TiyJpfWWBuEqIgGdUwKplyPzu92useM45wbpEcxLJFaQZURkArojSp7Qw/KIfP5rccQ9WVBlyi7x7Wwt+J9NH5cZqBBiUYOTXtfHFltFfss= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571927839911747.2919239663238; Thu, 24 Oct 2019 07:37:19 -0700 (PDT) Received: from localhost ([::1]:44386 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNeEn-0001PA-4S for importer@patchew.org; Thu, 24 Oct 2019 10:37:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51882) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckl-0005rf-W0 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckh-0002qG-55 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:11 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckg-0002NX-K2 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:06 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:54 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:52 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156302" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 15/22] intel_iommu: bind/unbind guest page table to host Date: Thu, 24 Oct 2019 08:34:36 -0400 Message-Id: <1571920483-3382-16-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 captures the guest PASID table entry modifications and propagates the changes to host to setup nested 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 a key part of vSVA support. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 81 ++++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 20 +++++++++++ 2 files changed, 101 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index d8827c9..793b0de 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) \ @@ -695,6 +696,16 @@ static inline uint16_t vtd_pe_get_domain_id(VTDPASIDEn= try *pe) return VTD_SM_PASID_ENTRY_DID((pe)->val[1]); } =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; @@ -1850,6 +1861,67 @@ static void vtd_context_global_invalidate(IntelIOMMU= State *s) vtd_iommu_replay_all(s); } =20 +static void vtd_bind_guest_pasid(IntelIOMMUState *s, VTDBus *vtd_bus, + int devfn, int pasid, VTDPASIDEntry *pe, VTDPASIDOp op) +{ +#ifdef __linux__ + VTDIOMMUContext *vtd_ic; + IOMMUCTXEventData event_data; + IOMMUCTXPASIDBindData bind; + struct iommu_gpasid_bind_data *g_bind_data; + + vtd_ic =3D vtd_bus->dev_ic[devfn]; + if (!vtd_ic) { + return; + } + + g_bind_data =3D g_malloc0(sizeof(*g_bind_data)); + bind.flag =3D 0; + g_bind_data->flags =3D 0; + g_bind_data->vtd.flags =3D 0; + switch (op) { + case VTD_PASID_BIND: + case VTD_PASID_UPDATE: + g_bind_data->version =3D IOMMU_GPASID_BIND_VERSION_1; + 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]); + bind.flag |=3D IOMMU_CTX_BIND_PASID; + break; + + case VTD_PASID_UNBIND: + g_bind_data->gpgd =3D 0; + g_bind_data->addr_width =3D 0; + g_bind_data->hpasid =3D pasid; + bind.flag |=3D IOMMU_CTX_UNBIND_PASID; + break; + + default: + printf("Unknown VTDPASIDOp!!\n"); + break; + } + if (bind.flag) { + event_data.event =3D IOMMU_CTX_EVENT_PASID_BIND; + bind.data =3D g_bind_data; + event_data.data =3D &bind; + iommu_ctx_event_notify(&vtd_ic->iommu_context, &event_data); + } + g_free(g_bind_data); +#endif +} + /* Do a context-cache device-selective invalidation. * @func_mask: FM field after shifting */ @@ -2528,12 +2600,17 @@ static gboolean vtd_flush_pasid(gpointer key, gpoin= ter value, pc_entry->pasid_cache_gen =3D s->pasid_cache_gen; if (!vtd_pasid_entry_compare(&pe, &pc_entry->pasid_entry))= { pc_entry->pasid_entry =3D pe; + vtd_bind_guest_pasid(s, vtd_bus, devfn, + pasid, &pe, VTD_PASID_UPDATE); /* * TODO: when pasid-base-iotlb(piotlb) infrastructure = is * ready, should invalidate QEMU piotlb togehter with = this * change. */ } + } else { + vtd_bind_guest_pasid(s, vtd_bus, devfn, + pasid, NULL, VTD_PASID_UNBIND); } } } @@ -2623,6 +2700,10 @@ static inline void vtd_fill_in_pe_cache( =20 pc_entry->pasid_entry =3D *pe; pc_entry->pasid_cache_gen =3D s->pasid_cache_gen; + vtd_bind_guest_pasid(s, vtd_pasid_as->vtd_bus, + vtd_pasid_as->devfn, + vtd_pasid_as->pasid, + pe, VTD_PASID_UPDATE); } =20 static int vtd_pasid_cache_psi(IntelIOMMUState *s, diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 12873e1..13e02e8 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -483,6 +483,14 @@ struct VTDRootEntry { }; typedef struct VTDRootEntry VTDRootEntry; =20 +enum VTDPASIDOp { + VTD_PASID_BIND, + VTD_PASID_UNBIND, + VTD_PASID_UPDATE, + VTD_OP_NUM +}; +typedef enum VTDPASIDOp VTDPASIDOp; + struct VTDPASIDCacheInfo { #define VTD_PASID_CACHE_DOMSI (1ULL << 0); #define VTD_PASID_CACHE_PASIDSI (1ULL << 1); @@ -549,6 +557,18 @@ 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 +/* Adjusted guest-address-width */ +#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 Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571923756; cv=none; d=zoho.com; s=zohoarc; b=CKm3jIEo47qUpgAYXab4s0Y5rAhoZOrUGXu6mB01BKC+k0rPpK7YtcEyYaGZn8lc7y1M9wzxnNarXuhdZ5xPiCjIwbXJVlULp5mUDLZ5P4ksqxLAQgFMUmqqOYS9z+Tw2x+CXqeujM7uzHRNDeaavZc7xnA2d92OUFKZPm8PR1A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571923756; 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=w9Od7eA44KsdDdl+PsWn169dYykFH9Xbsc/XAlCKZFM=; b=Xrvcna0SjxwnbyKdtSBHkA9p96Hvt9miyUsC3B6/fJDTXFstMev9TmxCbbBYMtJRJEjQbczQ6uyGCBFbP31dw1Czveb4nO0Vd/LiDlkATf+o8Sx8Teg8NNS7K+YfmTpsbkrjcfQXUqJF35sc2xNLg/35YWwq3n9N0HV/oj/2hvM= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 15719237565299.153495429011969; Thu, 24 Oct 2019 06:29:16 -0700 (PDT) Received: from localhost ([::1]:42612 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdAw-000573-LG for importer@patchew.org; Thu, 24 Oct 2019 09:29:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52071) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckt-00061H-MP for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckk-0002vd-Q8 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:19 -0400 Received: from mga11.intel.com ([192.55.52.93]:40516) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckk-0002aG-Cv for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:10 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:01:57 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:55 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156307" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 16/22] intel_iommu: replay guest pasid bindings to host Date: Thu, 24 Oct 2019 08:34:37 -0400 Message-Id: <1571920483-3382-17-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 guest pasid bindings replay for domain-selective(dsi) pasid cache invalidation and global pasid cache invalidation by walking guest pasid table. Reason: Guest OS may flush the pasid cache with wrong granularity. e.g. guest does a svm_bind() but flush the pasid cache with global or domain selective pasid cache invalidation instead of pasid selective(psi) pasid cache invalidation. Regards to such case, it works in host. Per spec, a global or domain selective pasid cache invalidation should be able to cover what a pasid selective invalidation does. The only concern is performance deduction since dsi and global cache invalidation will flush more than psi. To align with native, vIOMMU needs emulator needs to do replay for the two invalidation granularity to reflect the latest pasid bindings in guest pasid table. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 160 +++++++++++++++++++++++++++++++++++++= ---- hw/i386/intel_iommu_internal.h | 1 + 2 files changed, 149 insertions(+), 12 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 793b0de..a9e660c 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -68,6 +68,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 int vtd_update_pe_cache_for_dev(IntelIOMMUState *s, + VTDBus *vtd_bus, int devfn, int pasid, VTDPASIDEntry *pe); =20 static void vtd_panic_require_caching_mode(void) { @@ -2618,6 +2620,111 @@ static gboolean vtd_flush_pasid(gpointer key, gpoin= ter value, return remove; } =20 +/** + * Constant information used during pasid table walk + * @ic: iommu_context + * @flags: indicates if it is domain selective walk + * @did: domain ID of the pasid table walk + */ +typedef struct { + VTDIOMMUContext *ic; +#define VTD_PASID_TABLE_DID_SEL_WALK (1ULL << 0); + uint32_t flags; + uint16_t did; +} vtd_pt_walk_info; + +static bool vtd_sm_pasid_table_walk_one(IntelIOMMUState *s, + dma_addr_t pt_base, + int start, + int end, + vtd_pt_walk_info *info) +{ + VTDPASIDEntry pe; + int pasid =3D start; + int pasid_next; + VTDIOMMUContext *ic =3D info->ic; + + 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)) { + if (vtd_update_pe_cache_for_dev(s, + ic->vtd_bus, ic->devfn, pasid, &pe))= { + error_report_once("%s, bus: %d, devfn: %d, pasid: %d", + __func__, pci_bus_num(ic->vtd_bus->bus), ic->devfn, pasi= d); + return false; + } + } + pasid =3D pasid_next; + } + return true; +} + +/* + * 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. + */ +static void vtd_sm_pasid_table_walk(IntelIOMMUState *s, dma_addr_t pdt_bas= e, + int start, int end, vtd_pt_walk_info *in= fo) +{ + VTDPASIDDirEntry pdire; + int pasid =3D start; + int pasid_next; + dma_addr_t pt_base; + + while (pasid < end) { + pasid_next =3D pasid + VTD_PASID_TBL_ENTRY_NUM; + 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; + if (!vtd_sm_pasid_table_walk_one(s, + pt_base, pasid, pasid_next, info)) { + break; + } + } + pasid =3D pasid_next; + } +} + +/** + * This function replay the guest pasid bindings to hots by + * walking the guest PASID table. This ensures host will have + * latest guest pasid bindings. + */ +static void vtd_replay_guest_pasid_bindings(IntelIOMMUState *s, + uint16_t *did, bool is_dsi) +{ + VTDContextEntry ce; + vtd_pt_walk_info info; + VTDIOMMUContext *vtd_ic; + + if (is_dsi) { + info.flags =3D VTD_PASID_TABLE_DID_SEL_WALK; + info.did =3D *did; + } + + /* + * In this replay, only needs to care about the devices which + * has iommu_context created. For the one not have iommu_context, + * it is not necessary to replay the bindings since their cache + * could be re-created in the next DMA address transaltion. + */ + QLIST_FOREACH(vtd_ic, &s->vtd_dev_ic_list, next) { + if (!vtd_dev_to_context_entry(s, + pci_bus_num(vtd_ic->vtd_bus->bus), + vtd_ic->devfn, &ce)) { + info.ic =3D vtd_ic; + vtd_sm_pasid_table_walk(s, + VTD_CE_GET_PASID_DIR_TABLE(&ce), + 0, + VTD_MAX_HPASID, + &info); + } + } +} + static int vtd_pasid_cache_dsi(IntelIOMMUState *s, uint16_t domain_id) { VTDPASIDCacheInfo pc_info; @@ -2632,15 +2739,17 @@ static int vtd_pasid_cache_dsi(IntelIOMMUState *s, = uint16_t domain_id) */ vtd_iommu_lock(s); g_hash_table_foreach_remove(s->vtd_pasid_as, vtd_flush_pasid, &pc_info= ); - vtd_iommu_unlock(s); =20 /* - * TODO: Domain selective PASID cache invalidation - * may be issued wrongly by programmer, to be safe, - * after invalidating the pasid caches, emulator - * needs to replay the pasid bindings by walking guest - * pasid dir and pasid table. + * Domain selective PASID cache invalidation may be + * issued wrongly by programmer, to be safe, after + * invalidating the pasid caches, emulator needs to + * replay the pasid bindings by walking guest pasid + * dir and pasid table. */ + vtd_replay_guest_pasid_bindings(s, &domain_id, true); + + vtd_iommu_unlock(s); return 0; } =20 @@ -2706,6 +2815,31 @@ static inline void vtd_fill_in_pe_cache( pe, VTD_PASID_UPDATE); } =20 +/** + * This function updates the pasid entry cached in &vtd_pasid_as. + * Caller of this function should hold iommu_lock. + */ +static int vtd_update_pe_cache_for_dev(IntelIOMMUState *s, VTDBus *vtd_bus, + int devfn, int pasid, VTDPASIDEntry *pe) +{ + VTDPASIDAddressSpace *vtd_pasid_as; + + vtd_pasid_as =3D vtd_add_find_pasid_as(s, vtd_bus, + devfn, pasid, true); + if (!vtd_pasid_as) { + error_report_once("%s, fatal error happened!\n", __func__); + return -1; + } + + if (vtd_pasid_as->pasid_cache_entry.pasid_cache_gen =3D=3D + s->pasid_cache_gen) { + return 0; + } + + vtd_fill_in_pe_cache(vtd_pasid_as, pe); + return 0; +} + static int vtd_pasid_cache_psi(IntelIOMMUState *s, uint16_t domain_id, uint32_t pasid) { @@ -2825,15 +2959,17 @@ static int vtd_pasid_cache_gsi(IntelIOMMUState *s) =20 vtd_iommu_lock(s); vtd_pasid_cache_reset(s); - vtd_iommu_unlock(s); =20 /* - * TODO: Global PASID cache invalidation may be - * issued wrongly by programmer, to be safe, after - * invalidating the pasid caches, emulator needs - * to replay the pasid bindings by walking guest - * pasid dir and pasid table. + * Global PASID cache invalidation may be issued + * wrongly by programmer, to be safe, after invalidating + * the pasid caches, emulator needs to replay the + * pasid bindings by walking guest pasid dir and + * pasid table. */ + vtd_replay_guest_pasid_bindings(s, NULL, false); + + vtd_iommu_unlock(s); return 0; } =20 diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index 13e02e8..eab65ef 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -545,6 +545,7 @@ typedef struct VTDPASIDCacheInfo VTDPASIDCacheInfo; #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 --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571924565; cv=none; d=zoho.com; s=zohoarc; b=e5TcCe4YnHBxTJ1sk6UBc24WXmYdi/wXVYgkRQglp7zPyy3wiaehcvA3DhyF91iE0AW1iUcUK6/ihjPj/8NW1TZ5r3V6SDAuP7qoAt+NNpkLiLITAt5+lg2uPahN9CgmvFU3pvEmCMCqkLnSYFGj8Bxp+ohWdldtKnWg9UDRj+4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571924565; 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=v6zYTvbs7Oz7hFSoJDfA9FmQvg3q1bgo4WXkbK6UInU=; b=CRtpHb34a2gOZlhUuVB3Wt09DeNj84gQHsoAquM6ugrv/myCvQ6YIvw1tyRErwAuesOAUmxBt1LWdN4w24pA6Lw453xPM9fQmtEdNoTvu5U0egDkPQ9zG48G4agkxDl9XTSVDVSAHEwWgJb88FVEz1egB4GCcn2rS4QrABo8QnQ= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571924565357461.22123072736986; Thu, 24 Oct 2019 06:42:45 -0700 (PDT) Received: from localhost ([::1]:42848 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdNx-0001PJ-3r for importer@patchew.org; Thu, 24 Oct 2019 09:42:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51971) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckq-0005x1-B5 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckn-0002xm-Ps for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:16 -0400 Received: from mga11.intel.com ([192.55.52.93]:40529) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckn-0002c5-C8 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:13 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:02:00 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:01:58 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156312" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 17/22] intel_iommu: replay pasid binds after context cache invalidation Date: Thu, 24 Oct 2019 08:34:38 -0400 Message-Id: <1571920483-3382-18-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 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 Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 68 ++++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 3 ++ hw/i386/trace-events | 1 + 3 files changed, 72 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index a9e660c..6bceb7f 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -70,6 +70,10 @@ static void vtd_address_space_unmap(VTDAddressSpace *as,= IOMMUNotifier *n); static void vtd_pasid_cache_reset(IntelIOMMUState *s); static int vtd_update_pe_cache_for_dev(IntelIOMMUState *s, VTDBus *vtd_bus, int devfn, int pasid, VTDPASIDEntry *pe); +static void vtd_replay_guest_pasid_bindings(IntelIOMMUState *s, + uint16_t *did, bool is_dsi); +static void vtd_pasid_cache_devsi(IntelIOMMUState *s, + VTDBus *vtd_bus, uint16_t devfn); =20 static void vtd_panic_require_caching_mode(void) { @@ -1861,6 +1865,10 @@ static void vtd_context_global_invalidate(IntelIOMMU= State *s) * VT-d emulation codes. */ vtd_iommu_replay_all(s); + + vtd_iommu_lock(s); + vtd_replay_guest_pasid_bindings(s, NULL, false); + vtd_iommu_unlock(s); } =20 static void vtd_bind_guest_pasid(IntelIOMMUState *s, VTDBus *vtd_bus, @@ -1981,6 +1989,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 passdown to it. + */ + vtd_pasid_cache_devsi(s, vtd_bus, devfn_it); } } } @@ -2516,6 +2540,11 @@ static inline bool vtd_pc_is_pasid_si(struct VTDPASI= DCacheInfo *pc_info) return pc_info->flags & VTD_PASID_CACHE_PASIDSI; } =20 +static inline bool vtd_pc_is_dev_si(struct VTDPASIDCacheInfo *pc_info) +{ + return pc_info->flags & VTD_PASID_CACHE_DEVSI; +} + static inline int vtd_dev_get_pe_from_pasid(IntelIOMMUState *s, uint8_t bus_num, uint8_t devfn, @@ -2578,6 +2607,8 @@ static gboolean vtd_flush_pasid(gpointer key, gpointe= r value, devfn =3D vtd_pasid_as->devfn; =20 if (pc_entry->pasid_cache_gen && + (vtd_pc_is_dev_si(pc_info) ? (((pc_info->devfn =3D=3D devfn)) && + (pc_info->vtd_bus =3D=3D vtd_bus)) : 1) && (vtd_pc_is_dom_si(pc_info) ? (pc_info->domain_id =3D=3D did) : 1) = && (vtd_pc_is_pasid_si(pc_info) ? (pc_info->pasid =3D=3D pasid) : 1))= { /* @@ -2934,6 +2965,43 @@ static int vtd_pasid_cache_psi(IntelIOMMUState *s, return 0; } =20 +static void vtd_pasid_cache_devsi(IntelIOMMUState *s, + VTDBus *vtd_bus, uint16_t devfn) +{ + VTDPASIDCacheInfo pc_info; + VTDContextEntry ce; + vtd_pt_walk_info 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_iommu_lock(s); + g_hash_table_foreach_remove(s->vtd_pasid_as, vtd_flush_pasid, &pc_info= ); + + /* + * To be safe, after invalidating the pasid caches, + * emulator needs to replay the pasid bindings by + * walking guest pasid dir and pasid table. + */ + if (vtd_bus->dev_ic[devfn] && + !vtd_dev_to_context_entry(s, + pci_bus_num(vtd_bus->bus), + devfn, &ce)) { + info.flags =3D 0x0; + info.did =3D 0; + info.ic =3D vtd_bus->dev_ic[devfn]; + vtd_sm_pasid_table_walk(s, + VTD_CE_GET_PASID_DIR_TABLE(&ce), + 0, + VTD_MAX_HPASID, + &info); + } + vtd_iommu_unlock(s); +} + /** * 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 eab65ef..908536c 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -494,9 +494,12 @@ typedef enum VTDPASIDOp VTDPASIDOp; struct VTDPASIDCacheInfo { #define VTD_PASID_CACHE_DOMSI (1ULL << 0); #define VTD_PASID_CACHE_PASIDSI (1ULL << 1); +#define VTD_PASID_CACHE_DEVSI (1ULL << 2); uint32_t flags; uint16_t domain_id; uint32_t pasid; + VTDBus *vtd_bus; + uint16_t devfn; }; typedef struct VTDPASIDCacheInfo VTDPASIDCacheInfo; =20 diff --git a/hw/i386/trace-events b/hw/i386/trace-events index 7912ae1..25bd6a4 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -26,6 +26,7 @@ vtd_pasid_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_pasid_cache_devsi(uint16_t devfn) "Dev slective PC invalidation dev: 0= x%"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 Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571926239; cv=none; d=zoho.com; s=zohoarc; b=ANGv+hF6cqf4DIsf1st0MCHFtrn44zq2wmywxqyatRBubW5RegvkUQfIUtXIWcopAs1svchfSrI1NL+vmQVJkXN01oUOhkngnETLraDkYWkWL74AX7fWqh6m1VlNDKb2+jcC2PHHbH2sfGURC+6+wFgFa1kq4yOqpUU/k5eNySc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571926239; 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=qH3+fXYYObFxwuHR4qNVyX1BnBcnvfsdw6tItboJWR4=; b=E5c6zAq7l/8E/EUynABLhvw9WRdPiWETTUEdm0GyZzj4v0Vmfy63Mav40H/3hYEIAOqWzYhhLCnxD/ALpoBMVIaXgTFzOVOyv5Xq/gBiJtxqWqhFGAj4lS84Oo3gAbJyFrH8QcjSGcgXyYF9TQfx8Uc1r/i+HieUxKVQq+o1Vo4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571926239549889.9064967238855; Thu, 24 Oct 2019 07:10:39 -0700 (PDT) Received: from localhost ([::1]:43690 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdoz-0001CK-JA for importer@patchew.org; Thu, 24 Oct 2019 10:10:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52101) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckx-00065y-F8 for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNcks-00031d-4Q for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:23 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckr-0002NX-7m for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:17 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:02:03 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:02:01 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156326" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 18/22] intel_iommu: do not passdown pasid bind for PASID #0 Date: Thu, 24 Oct 2019 08:34:39 -0400 Message-Id: <1571920483-3382-19-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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" 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 further 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 on the PASIDs which are going to be bound to device. For PASID #0, no need to passdown pasid binding since PASID #0 is used as RID_PASID for requests without pasid. Reason is current Intel vIOMMU supports guest IOVA by shadowing guest 2nd level page table. However, in future, if guest OS uses 1st level page table to store IOVA mappings, thus guest IOVA support will also be done via nested translation. Then vIOMMU could passdown the pasid binding for PASID #0 to host with a special PASID value to indicate host to bind the guest page table to a proper PASID. e.g PASID value from 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 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 6bceb7f..d621455 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1880,6 +1880,16 @@ static void vtd_bind_guest_pasid(IntelIOMMUState *s,= VTDBus *vtd_bus, IOMMUCTXPASIDBindData bind; struct iommu_gpasid_bind_data *g_bind_data; =20 + if (pasid < VTD_MIN_HPASID) { + /* + * If pasid < VTD_HPASID_MIN, this pasid is not allocated + * from host. No need to passdown the changes on it to host. + * TODO: when IOVA over FLPT is ready, this switch should be + * refined. + */ + return; + } + vtd_ic =3D vtd_bus->dev_ic[devfn]; if (!vtd_ic) { return; --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571928039; cv=none; d=zoho.com; s=zohoarc; b=AwBiJJes/wuioBFhN2cgkNPz8pD/XBfPwzuJed1V4FRerq2MEplv9MPhU4PSff9nC2wTbYDQUMEyD+dbNJTANGCu6vXbR+Ow3MOhnk7muuY4+SAvoRN7+Y+Ct/oMEK8UYxyDPdxGNGdD3Iih8iRofOuSAARj7iE06qCn/A7bj8k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571928039; 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=I4eEm8+B3wNp0CFrTPRRxjDMrKUgOvN57rkFAIW6t8c=; b=cnys13HcFZE+jUYHt2obqrrx15SOuDkXHSa1sfRbgT/4iZgBrvBaEn9nWhzrXSxufMU6RYfC1jOWsx8O7v/Ou3+lkR3l7blDvpazcr8SX2hTix/DgjABt/iYBKt7QipHAuvkGT18qIw2+ntWhDGeMLFK9LozlOzXUrQwZVwAstw= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571928039074397.80211000464715; Thu, 24 Oct 2019 07:40:39 -0700 (PDT) Received: from localhost ([::1]:44430 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNeI1-0008KS-4A for importer@patchew.org; Thu, 24 Oct 2019 10:40:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52095) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckw-000654-HD for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNckv-00038G-8T for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:22 -0400 Received: from mga11.intel.com ([192.55.52.93]:40516) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckv-0002aG-0C for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:21 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:02:06 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:02:03 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156333" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 19/22] vfio/pci: add iommu_context notifier for PASID-based iotlb flush Date: Thu, 24 Oct 2019 08:34:40 -0400 Message-Id: <1571920483-3382-20-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 notifier for propagating guest PASID-based iotlb invalidation to host. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Eric Auger Cc: Yi Sun Cc: David Gibson Signed-off-by: Liu Yi L --- hw/vfio/pci.c | 29 +++++++++++++++++++++++++++++ include/hw/iommu/iommu.h | 8 ++++++++ 2 files changed, 37 insertions(+) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 012b8ed..52fe3ed 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2802,6 +2802,31 @@ static void vfio_iommu_pasid_bind_notify(IOMMUCTXNot= ifier *n, #endif } =20 +static void vfio_iommu_cache_inv_notify(IOMMUCTXNotifier *n, + IOMMUCTXEventData *event_data) +{ +#ifdef __linux__ + VFIOIOMMUContext *giommu_ctx =3D container_of(n, VFIOIOMMUContext, n); + VFIOContainer *container =3D giommu_ctx->container; + IOMMUCTXCacheInvInfo *inv_info =3D + (IOMMUCTXCacheInvInfo *) event_data->data; + struct vfio_iommu_type1_cache_invalidate *cache_inv; + unsigned long argsz; + + argsz =3D sizeof(*cache_inv); + cache_inv =3D g_malloc0(argsz); + cache_inv->argsz =3D argsz; + cache_inv->info =3D *inv_info->info; + cache_inv->flags =3D 0; + + if (ioctl(container->fd, VFIO_IOMMU_CACHE_INVALIDATE, cache_inv) !=3D = 0) { + error_report("%s: cache invalidation failed: %d", __func__, -errno= ); + } + + g_free(cache_inv); +#endif +} + static void vfio_realize(PCIDevice *pdev, Error **errp) { VFIOPCIDevice *vdev =3D PCI_VFIO(pdev); @@ -3118,6 +3143,10 @@ static void vfio_realize(PCIDevice *pdev, Error **er= rp) iommu_context, vfio_iommu_pasid_bind_notify, IOMMU_CTX_EVENT_PASID_BIND); + vfio_register_iommu_ctx_notifier(vdev, + iommu_context, + vfio_iommu_cache_inv_notify, + IOMMU_CTX_EVENT_CACHE_INV); } =20 return; diff --git a/include/hw/iommu/iommu.h b/include/hw/iommu/iommu.h index 4f21aa1..452f609 100644 --- a/include/hw/iommu/iommu.h +++ b/include/hw/iommu/iommu.h @@ -34,6 +34,7 @@ enum IOMMUCTXEvent { IOMMU_CTX_EVENT_PASID_ALLOC, IOMMU_CTX_EVENT_PASID_FREE, IOMMU_CTX_EVENT_PASID_BIND, + IOMMU_CTX_EVENT_CACHE_INV, IOMMU_CTX_EVENT_NUM, }; typedef enum IOMMUCTXEvent IOMMUCTXEvent; @@ -61,6 +62,13 @@ struct IOMMUCTXPASIDBindData { }; typedef struct IOMMUCTXPASIDBindData IOMMUCTXPASIDBindData; =20 +struct IOMMUCTXCacheInvInfo { +#ifdef __linux__ + struct iommu_cache_invalidate_info *info; +#endif +}; +typedef struct IOMMUCTXCacheInvInfo IOMMUCTXCacheInvInfo; + struct IOMMUCTXEventData { IOMMUCTXEvent event; uint64_t length; --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571928208; cv=none; d=zoho.com; s=zohoarc; b=GRVTWtYNSkHRyVwltsAuuETYIU1o5LI3vYxD3znX4mCD/mAoJZBM7mY+uff3PIRQmSsYhAcw7884AZePdmPAI5/IvxBj9Vz1ORQHc24gE1Xei8G4YlTV86gdtnaLBC0gbk8MkxTgR2jUZY8k6VnJzGyHml0oOERf0xXZgEZ7X9g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571928208; 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=T+eOJ0odTlhcF/9WJhYtxJ+bit+Uq9dHdIZCuy6jdR0=; b=cnYGtV0HoLLGnjpBTHeRKhi4sihI1UyuAhrzVMQkRDEMud6lxzyFNSFd6viO4cG3pAY3wNgUgUKXdPUoirVrTTwLsDjauE3CZ3vR228UyWK9w0yU2yKr3b2AFU2Ai59XnHfqd68F69UARDbJYEmyYCJ1xUDGvIcNjTTyPetPDhk= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571928208150487.81258818179367; Thu, 24 Oct 2019 07:43:28 -0700 (PDT) Received: from localhost ([::1]:44486 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNeKk-00076X-JH for importer@patchew.org; Thu, 24 Oct 2019 10:43:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52164) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNckz-00069C-Tm for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNcky-00039b-6T for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:25 -0400 Received: from mga11.intel.com ([192.55.52.93]:40529) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNckx-0002c5-UT for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:24 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:02:09 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:02:06 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156340" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 20/22] intel_iommu: process PASID-based iotlb invalidation Date: Thu, 24 Oct 2019 08:34:41 -0400 Message-Id: <1571920483-3382-21-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 the basic PASID-based iotlb (piotlb) invalidation support. piotlb is used during walking Intel VT-d first-level page table. This patch only adds the basic processing. Detailed process will be added in next patc= h. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 57 ++++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 13 ++++++++++ 2 files changed, 70 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index d621455..6cd922f 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3092,6 +3092,59 @@ static bool vtd_process_pasid_desc(IntelIOMMUState *= s, return (ret =3D=3D 0) ? true : false; } =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]); + if (am > VTD_MAMV) { + error_report_once("Invalid am, > max am value, hi: 0x%" PRIx64 + " lo: 0x%" PRIx64, inv_desc->val[1], inv_desc->val[0]); + return false; + } + 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) { @@ -3206,6 +3259,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 908536c..eddfe54 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -458,6 +458,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 Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571926409; cv=none; d=zoho.com; s=zohoarc; b=ILeIQ34YomIu3H1Y0pfvx0h/VS0xucvRcu5vJtUor2UyXldvYhYpHN3Z2VcwXjp3ZkbS5xNq9Ssl6IBeY33bIvZ3COGuhlggcscmPN2mRnIwl+pil7ehg0/vovKlRDoLvsabc0JBn8a/1bDIaPHxbUvI4kHvepR5fBiggkZeyPc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571926409; 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=GzO/ItdhYfmAJoJ9zsirlv+scgZZPbfwFegPdkhGxNA=; b=FXfhZd8ExuMXvQemqUt51ESJeYvmILew61CvudrtSSg+gxhH+EeXWYPQyY9crTTvU8HmRWM9QY18U8u+vEqRhwyOIygiKiLEKt8PU6TuXl3cvIju8wQrjkuTAUtrlLmESlX7M2RRJi9MrE91pEml22rLw7UQVBIsJgX4iZPhj0E= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1571926409623964.409802644544; Thu, 24 Oct 2019 07:13:29 -0700 (PDT) Received: from localhost ([::1]:43750 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNdrg-0008HN-SM for importer@patchew.org; Thu, 24 Oct 2019 10:13:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52295) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNcl4-0006Eg-Tm for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNcl2-0003G7-Mx for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:30 -0400 Received: from mga11.intel.com ([192.55.52.93]:40489) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNcl2-0002NX-Bn for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:28 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:02:12 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:02:09 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156343" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 21/22] intel_iommu: propagate PASID-based iotlb invalidation to host Date: Thu, 24 Oct 2019 08:34:42 -0400 Message-Id: <1571920483-3382-22-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 propagates PASID-based iotlb invalidation to host. Intel VT-d 3.0 supports nested translation in PASID granularity. For guest SVA support, nested translation is enabled for specific PASID. This is also known as dual stage translation which gives better virtualization support. 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. Cc: Kevin Tian Cc: Jacob Pan Cc: Peter Xu Cc: Yi Sun Signed-off-by: Liu Yi L --- hw/i386/intel_iommu.c | 127 +++++++++++++++++++++++++++++++++++++= ++++ hw/i386/intel_iommu_internal.h | 7 +++ 2 files changed, 134 insertions(+) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 6cd922f..5ca9ee1 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3092,15 +3092,142 @@ static bool vtd_process_pasid_desc(IntelIOMMUState= *s, return (ret =3D=3D 0) ? true : false; } =20 +static void vtd_invalidate_piotlb(IntelIOMMUState *s, VTDBus *vtd_bus, + int devfn, IOMMUCTXCacheInvInfo *inv_inf= o) +{ +#ifdef __linux__ + VTDIOMMUContext *vtd_ic; + IOMMUCTXEventData event_data; + vtd_ic =3D vtd_bus->dev_ic[devfn]; + if (!vtd_ic) { + return; + } + event_data.event =3D IOMMU_CTX_EVENT_CACHE_INV; + event_data.data =3D inv_info; + iommu_ctx_event_notify(&vtd_ic->iommu_context, &event_data); +#endif +} + +static inline bool vtd_pasid_cache_valid( + VTDPASIDAddressSpace *vtd_pasid_as) +{ + return (vtd_pasid_as->iommu_state->pasid_cache_gen && + (vtd_pasid_as->iommu_state->pasid_cache_gen + =3D=3D vtd_pasid_as->pasid_cache_entry.pasid_cache_gen)); +} + +/** + * 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; + uint16_t did; + + /* + * Needs to check whether the pasid entry cache stored in + * vtd_pasid_as is valid or not. "invalid" means the pasid + * cache has been flushed, thus host should have done piotlb + * invalidation together with a pasid cache invalidation, so + * no need to pass down piotlb invalidation to host for better + * performance. Only when pasid entry cache is "valid", should + * a piotlb invalidation be propagated to host since it means + * guest just modified a mapping in its page table. + */ + if (!vtd_pasid_cache_valid(vtd_pasid_as)) { + return; + } + + did =3D vtd_pe_get_domain_id( + &(vtd_pasid_as->pasid_cache_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->inv_info); + } + + /* + * 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) { +#ifdef __linux__ + VTDPIOTLBInvInfo piotlb_info; + struct iommu_cache_invalidate_info *cache_info; + IOMMUCTXCacheInvInfo *inv_info =3D &piotlb_info.inv_info; + + cache_info =3D g_malloc0(sizeof(*cache_info)); + cache_info->version =3D IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + 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; + inv_info->info =3D cache_info; + piotlb_info.domain_id =3D domain_id; + piotlb_info.pasid =3D pasid; + + 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(cache_info); +#endif } =20 static void vtd_piotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain= _id, uint32_t pasid, hwaddr addr, uint8_t am, bool= ih) { +#ifdef __linux__ + VTDPIOTLBInvInfo piotlb_info; + struct iommu_cache_invalidate_info *cache_info; + IOMMUCTXCacheInvInfo *inv_info =3D &piotlb_info.inv_info; + + cache_info =3D g_malloc0(sizeof(*cache_info)); + cache_info->version =3D IOMMU_CACHE_INVALIDATE_INFO_VERSION_1; + 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; + inv_info->info =3D cache_info; + piotlb_info.domain_id =3D domain_id; + piotlb_info.pasid =3D pasid; + + 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(cache_info); +#endif } =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 eddfe54..6a83f6c 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -516,6 +516,13 @@ struct VTDPASIDCacheInfo { }; typedef struct VTDPASIDCacheInfo VTDPASIDCacheInfo; =20 +struct VTDPIOTLBInvInfo { + uint16_t domain_id; + uint32_t pasid; + IOMMUCTXCacheInvInfo inv_info; +}; +typedef struct VTDPIOTLBInvInfo VTDPIOTLBInvInfo; + /* Masks for struct VTDRootEntry */ #define VTD_ROOT_ENTRY_P 1ULL #define VTD_ROOT_ENTRY_CTP (~0xfffULL) --=20 2.7.4 From nobody Mon May 6 01:29:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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 (zoho.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=1571928424; cv=none; d=zoho.com; s=zohoarc; b=JjsIOI+li51HF7qyXwlzWHgQ5oPSQ8+s+edTheRjJMvHi/ub4tIWVHVDpsDzOZckzeozVOWpvwyz65fM63J0tKspQj4aFH+i+b+k6M1u24tp5iYRBB6PAwRPTzIkV3RjWZPDIMGv+R6BlDZUZtz8Ue2yH3vXmAf7k/AIkABQ6nE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571928424; 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=bHibpOewv+tlDDY3FVZTU4M5NUaYMqf2kMyll6sS96M=; b=ZWxajUaFOKVPkaGI+1qTneAKCZFuuL6GLOu9LQ5Rl7obRKbje9QJGNynV8MMVP7FYvj9sOVhkVM9JLzWsGU/YUGiTP+4uR5PIOsONFDqSeHQ1WN5St66Wlzt5d3433wConfRQ5zEwUH5NhXeRABdMs27rEY34+v2jHPYFLyPPEg= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 157192842438015.147183120551176; Thu, 24 Oct 2019 07:47:04 -0700 (PDT) Received: from localhost ([::1]:44522 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNeOE-0007SV-JA for importer@patchew.org; Thu, 24 Oct 2019 10:47:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52426) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNclB-0006NB-5h for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNcl5-0003JE-Qy for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:36 -0400 Received: from mga11.intel.com ([192.55.52.93]:40516) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNcl5-0002aG-FE for qemu-devel@nongnu.org; Thu, 24 Oct 2019 09:02:31 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Oct 2019 06:02:15 -0700 Received: from iov.bj.intel.com ([10.238.145.67]) by fmsmga001.fm.intel.com with ESMTP; 24 Oct 2019 06:02:12 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,224,1569308400"; d="scan'208";a="210156361" From: Liu Yi L To: qemu-devel@nongnu.org, mst@redhat.com, pbonzini@redhat.com, alex.williamson@redhat.com, peterx@redhat.com Subject: [RFC v2 22/22] intel_iommu: process PASID-based Device-TLB invalidation Date: Thu, 24 Oct 2019 08:34:43 -0400 Message-Id: <1571920483-3382-23-git-send-email-yi.l.liu@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> References: <1571920483-3382-1-git-send-email-yi.l.liu@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 192.55.52.93 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: tianyu.lan@intel.com, kevin.tian@intel.com, yi.l.liu@intel.com, Yi Sun , kvm@vger.kernel.org, jun.j.tian@intel.com, eric.auger@redhat.com, yi.y.sun@intel.com, jacob.jun.pan@linux.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 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 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 5ca9ee1..1c00e9c 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -3285,6 +3285,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) { @@ -3406,6 +3417,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 6a83f6c..714dc09 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -390,6 +390,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