From patchwork Thu May 11 14:51:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92686 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4448495vqo; Thu, 11 May 2023 08:08:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4+NrMWtDgKDqg3d7zVUBuXALYwzXdtXFQn2z4vR8gJhhB34Yy1ILS10dKLh6zk0xTUBn5O X-Received: by 2002:a17:90a:8a05:b0:24d:ebf8:b228 with SMTP id w5-20020a17090a8a0500b0024debf8b228mr20347833pjn.19.1683817728160; Thu, 11 May 2023 08:08:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683817728; cv=none; d=google.com; s=arc-20160816; b=dWqZBClPU1ha6/ULKSygyVOFmzunGwxwJHh+cbPWOv3jPD+paJi0mKfHbjwiukks6N 485OgEBgkwxQMNJgkgNLxnBYhdIxYofjORStn//o7M883BmNk8J2Q/RBzVwzJs+LUKGa 1+Cdp8za9RzB5h/QqoZ4vqQxfMGMwUicdkILAwDlYC8z7mWHq4W1E43SmzsE9tgvs2d+ H6Z3r52dt+qgLwPGx8QZLKvG4WmB/M1oKkKT/DRYzEjxd/fqIIJNUmYge132pXGYt5Cu uRThNKLnvVb6EsX8t5bcBNudeytr23fwIEXaYVcpSOHZfr0W+UNSQKnKgfh189Ce/qUW rF5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ogMVZptYYPOer5HX96Lz2NcJVgQOC6bAh8ZCjdBASDM=; b=Foj8qJ4I2wA87fLe9fG+VC9DjLqr00p18+2BHKryuW+EEKeN3yHFPpZt63HMwnFq3t g8UFWx238eWmuKqBdLboiwTDK7Wniz0SqecaDVrtpaMFLd6KkC82TNMxm87yNschuI+T OVWUl+yf5wo/PHiLIIf5KaV13OpY9GkadiQThtZy1YyJhgH+J5/zYu2Y5/yUB558C81L RdqxJqudGF/Chg+qLGDa2PbksowEccrq66glbmsTE7r+IGR+OYt8YenBFgGsxpKCPlxK FwgHGZuRxoNMi4yo0A1CJKsPzWtq7zdWxnMDrW4alec1bHv/too9q1tksbcHxDZniTLD EnqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BpOAacXE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z197-20020a6333ce000000b005305b6bfa33si4194412pgz.873.2023.05.11.08.08.32; Thu, 11 May 2023 08:08:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BpOAacXE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237701AbjEKOwf (ORCPT + 99 others); Thu, 11 May 2023 10:52:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237904AbjEKOwO (ORCPT ); Thu, 11 May 2023 10:52:14 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8A9E268E; Thu, 11 May 2023 07:51:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816675; x=1715352675; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vk4v/ikHz47XNGzTNt9NSUfHhuFlUp40YbtiK2s2Voo=; b=BpOAacXEZgldDUMyzXnYtruIGtPPw9DYgS+WfO4ArwLoEIts0ynlSPhO TUxexM4NuG/Vtil+42YsU9Ejr+Ch0uVgTWfFdOvaq4+ZqrMdJtngz+fyW x3mzhPUoqFeobBP2a4vXyTGHvCzweD0IaWo7NswBTji98yvC57gDdOh/n WZqCfHl8JfBZSGtttzlcED/dnHumEszr5t/yX0KeirHLNcrWZ3t3oIi0g vwjxOF0Apv0X7SDwE1KMTWY067GQ1M1PNQ3iEecI6hQjWPWCxYu67x7bX hDPx9F5pCrlpKFTFOhUTL/luUY21n/cCL3ZjMZ9F3KHC67EZLG1v5SDPx A==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025426" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025426" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355151" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355151" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:13 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 01/10] iommufd: Add data structure for Intel VT-d stage-1 domain allocation Date: Thu, 11 May 2023 07:51:01 -0700 Message-Id: <20230511145110.27707-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765610858402889792?= X-GMAIL-MSGID: =?utf-8?q?1765610858402889792?= This adds IOMMU_HWPT_TYPE_VTD_S1 for stage-1 hw_pagetable of Intel VT-d and the corressponding data structure for userspace specified parameter for the domain allocation. Signed-off-by: Yi Liu --- include/uapi/linux/iommufd.h | 57 ++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 06dcad6ab082..c2658394827a 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -353,9 +353,64 @@ struct iommu_vfio_ioas { /** * enum iommu_hwpt_type - IOMMU HWPT Type * @IOMMU_HWPT_TYPE_DEFAULT: default + * @IOMMU_HWPT_TYPE_VTD_S1: Intel VT-d stage-1 page table */ enum iommu_hwpt_type { IOMMU_HWPT_TYPE_DEFAULT, + IOMMU_HWPT_TYPE_VTD_S1, +}; + +/** + * enum iommu_hwpt_intel_vtd_flags - Intel VT-d stage-1 page + * table entry attributes + * @IOMMU_VTD_PGTBL_SRE: Supervisor request + * @IOMMU_VTD_PGTBL_EAFE: Extended access enable + * @IOMMU_VTD_PGTBL_PCD: Page-level cache disable + * @IOMMU_VTD_PGTBL_PWT: Page-level write through + * @IOMMU_VTD_PGTBL_EMTE: Extended mem type enable + * @IOMMU_VTD_PGTBL_CD: PASID-level cache disable + * @IOMMU_VTD_PGTBL_WPE: Write protect enable + */ +enum iommu_hwpt_intel_vtd_flags { + IOMMU_VTD_PGTBL_SRE = 1 << 0, + IOMMU_VTD_PGTBL_EAFE = 1 << 1, + IOMMU_VTD_PGTBL_PCD = 1 << 2, + IOMMU_VTD_PGTBL_PWT = 1 << 3, + IOMMU_VTD_PGTBL_EMTE = 1 << 4, + IOMMU_VTD_PGTBL_CD = 1 << 5, + IOMMU_VTD_PGTBL_WPE = 1 << 6, + IOMMU_VTD_PGTBL_LAST = 1 << 7, +}; + +/** + * struct iommu_hwpt_intel_vtd - Intel VT-d specific user-managed + * stage-1 page table info + * @flags: Combination of enum iommu_hwpt_intel_vtd_flags + * @pgtbl_addr: The base address of the user-managed stage-1 page table. + * @pat: Page attribute table data to compute effective memory type + * @emt: Extended memory type + * @addr_width: The address width of the untranslated addresses that are + * subjected to the user-managed stage-1 page table. + * @__reserved: Must be 0 + * + * The Intel VT-d specific data for creating hw_pagetable to represent + * the user-managed stage-1 page table that is used in nested translation. + * + * In nested translation, the stage-1 page table locates in the address + * space that defined by the corresponding stage-2 page table. Hence the + * stage-1 page table base address value should not be higher than the + * maximum untranslated address of stage-2 page table. + * + * The paging level of the stage-1 page table should be compatible with + * the hardware iommu. Otherwise, the allocation would be failed. + */ +struct iommu_hwpt_intel_vtd { + __u64 flags; + __u64 pgtbl_addr; + __u32 pat; + __u32 emt; + __u32 addr_width; + __u32 __reserved; }; /** @@ -391,6 +446,8 @@ enum iommu_hwpt_type { * +------------------------------+-------------------------------------+-----------+ * | IOMMU_HWPT_TYPE_DEFAULT | N/A | IOAS | * +------------------------------+-------------------------------------+-----------+ + * | IOMMU_HWPT_TYPE_VTD_S1 | struct iommu_hwpt_intel_vtd | HWPT | + * +------------------------------+-------------------------------------+-----------+ */ struct iommu_hwpt_alloc { __u32 size; From patchwork Thu May 11 14:51:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92680 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4436623vqo; Thu, 11 May 2023 07:55:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7DRxUPBZA+kzD3c5ZZtB0M1GfC5VTnEe5FrRXpzMTvHf/8SYbiM5aXwTIe4N7tb9YEVghp X-Received: by 2002:a17:903:230d:b0:1ad:164:74fc with SMTP id d13-20020a170903230d00b001ad016474fcmr6217848plh.20.1683816922314; Thu, 11 May 2023 07:55:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816922; cv=none; d=google.com; s=arc-20160816; b=XkShqHvyOCFfjsI+BqMVuaYVZlnNhJRAHslfIi1uo/5Nh3/AraEB96Lj2OxkfqJVOw xPLYA4iyDm5Q5Z3rIQK+aIMghM7gmB6f5Ot6cIfyRpIGAMyXIVphrgyztHjsyWxFwEJA 5Y4jo3AdGWS4PZYli+FepgP5NNv0LWlFnFUxAVqh/w98PntlsBPWRU4+EoGIzzTMQRof fudbZNPFEITmzy/oD8DvDgfIo1MP542Fs6qYhejCmYGhxVjK/e+C1RuyYniRhAUUB37N kE6yTvjiR3yMZTTpRid4X8WNhb+mL9G5lb17qx2bVi/YwyXA9FPrjpRJLZe/Hgagptk7 gGxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=R6A1IZRMXEOLRJ/widjC1fKeFcQfLwCpYu4BjpCMCMw=; b=TJaL/skO54WvIMycEYoytunyk34pZVba+xF5/3BYooSZE28cymP+PTho+2nWSUSBMc 04rpKt2Qr3CyythZTbgBZH2u5ZsVneRsRGuUisGRLHUvV2c0oR71xg63Wx1NMzaYT3XN 75HbaqWKuXU9CQl7yqclpLMf9B3l2MTRbLjo5sj0r+BjBmp1GThE27amDReOho/ya/Kh KlMqncz2DiDnyjBCK7tUnsGa2dLglaPvm0FD/htEiVZPxQpxon36K9yFrwZrt7r8oobm 3wvFcnnp5wb0XhaLsU/Z8YooXSB8MG3JZEZGlBn4yxcje7M0AdMPxjrSKzWEmemMEPmx sxGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GhOu+72v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q20-20020a170902bd9400b0019ceaf40d5esi6416774pls.107.2023.05.11.07.55.09; Thu, 11 May 2023 07:55:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GhOu+72v; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238644AbjEKOwb (ORCPT + 99 others); Thu, 11 May 2023 10:52:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238250AbjEKOwP (ORCPT ); Thu, 11 May 2023 10:52:15 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4496C106C5; Thu, 11 May 2023 07:51:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816676; x=1715352676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hJ7ZgLoBzS93EqELSx3NuEeyF7SzKPDPM4Hq5dZ6YEY=; b=GhOu+72vNeKHzqw+wM4B1U+tb1VxGcsiYK0DkJlwd5QlvxUApqhVPN4o X+i4oPB+f3aEKonEPY/N+adu4A7Ml9m5PFvMxPjArBg+swQ26lghUOxVb 4g3nAUqzGIBXFtOBGPhqnZYFbPz+tHX/K87oE8w64nFfAR/SKJb+kd8dZ 7NblF2hjG2lPFSrAEeIZuVp1glBHMgZxFWXmrxNdiFAGIFx+luXRYWM6P +ZtdQlBhbxO3aBPBHvYnC2ZsfxLB3PQ8dXMOSVK/Gjr2Ue2a1hC4Kyd2g 3H8TKxKKErrdPKSM232s0cqM5ZByxH5TXn0GJrC5kxIyUkBg1/LLOV6x4 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025441" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025441" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355157" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355157" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:15 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 02/10] iommu/vt-d: Extend dmar_domain to support nested domain Date: Thu, 11 May 2023 07:51:02 -0700 Message-Id: <20230511145110.27707-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765610013208969965?= X-GMAIL-MSGID: =?utf-8?q?1765610013208969965?= From: Lu Baolu The nested domain fields are exclusive to those that used for a DMA remapping domain. Use union to avoid memory waste. Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.h | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 1c5e1d88862b..e818520f4068 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -596,15 +596,38 @@ struct dmar_domain { spinlock_t lock; /* Protect device tracking lists */ struct list_head devices; /* all devices' list */ - struct dma_pte *pgd; /* virtual address */ - int gaw; /* max guest address width */ - - /* adjusted guest address width, 0 is level 2 30-bit */ - int agaw; int iommu_superpage;/* Level of superpages supported: 0 == 4KiB (no superpages), 1 == 2MiB, 2 == 1GiB, 3 == 512GiB, 4 == 1TiB */ - u64 max_addr; /* maximum mapped address */ + union { + /* DMA remapping domain */ + struct { + /* virtual address */ + struct dma_pte *pgd; + /* max guest address width */ + int gaw; + /* + * adjusted guest address width: + * 0: level 2 30-bit + * 1: level 3 39-bit + * 2: level 4 48-bit + * 3: level 5 57-bit + */ + int agaw; + /* maximum mapped address */ + u64 max_addr; + }; + + /* Nested user domain */ + struct { + /* 2-level page table the user domain nested */ + struct dmar_domain *s2_domain; + /* user page table pointer (in GPA) */ + unsigned long s1_pgtbl; + /* page table attributes */ + struct iommu_hwpt_intel_vtd s1_cfg; + }; + }; struct iommu_domain domain; /* generic domain data structure for iommu core */ From patchwork Thu May 11 14:51:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92679 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4436003vqo; Thu, 11 May 2023 07:54:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4iY2NYeksMuZ0KUlOEZnPqJjj4mq4YxKbzepDMhHf/RWAj+2X29MrduJE58xBoKXvlB4CK X-Received: by 2002:a17:90b:1810:b0:250:981f:365a with SMTP id lw16-20020a17090b181000b00250981f365amr12325875pjb.23.1683816865552; Thu, 11 May 2023 07:54:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683816865; cv=none; d=google.com; s=arc-20160816; b=mLKCadkHzwY5Z85H+Hw3a/RBp82ZhSb+k9/uMZTzRxZXKPGTFgZzh6TjtGgox0xtVU +NF+1dAZ9IRxzrPzFtr6q9/y3l1/wUcDCccxpzVwSRbdTnfGvDxvk3jor5UzMEu7BtX5 CSBnAj+qLtEa58jeOfF5nggBw7wpDHtDay6cSaxe7z5AWZBJMPcZdndl/9tJvyIMNDJh mM/PdiT+nujAmKQ4qVGYDuInaadzMGLyJb7VkqBvLmN+BIY+Mg8udleuhR0BHkLy8BwO w9zY2UNXGPS/++YPE3tVBqFqXF0maCBCsWJZuJfIB7pht6FAZR0B5LekEn5albKQxKgS Z3aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=9Ldt1r0vsZPhX624S+Xn2uD/gmd3wdKfvEZQCSJEmH0=; b=wAy/Zxnyt37Py2aWn3Dbz7KyBeZX38db3ETKMIBvMSUt2Y3avJ1kghP5XCQHfiUyot 969H6v03nv4TkzFc8kdCcyjXm6U8ju8CbqE3KKgOyLuIXSxCpw2dwifirJ1+36xaND3/ iuJCzrbHABqBSSLI+HPUFwTcwczo+K96hUf2vvrWOq0h+tACgVsqFC3XGWUj3Wqkg9HO tX/ioIypnY0RuruAdK5UMc0+Nl5trLB7ETnj5lwkxXV1GWWqL1jyUfnHQCIc7D8R5ejD 62hphrXemPM1AwakNLgFbvUrqXhNjPRfFomW6uck9isbAc7U5xXS7U+cM/RQaoL7pbvG cv5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="RiIPF6/w"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 13-20020a17090a030d00b002507cbb0090si10219989pje.184.2023.05.11.07.54.12; Thu, 11 May 2023 07:54:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="RiIPF6/w"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237973AbjEKOwr (ORCPT + 99 others); Thu, 11 May 2023 10:52:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238680AbjEKOwY (ORCPT ); Thu, 11 May 2023 10:52:24 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC71011545; Thu, 11 May 2023 07:51:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816694; x=1715352694; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5lTJZJlhkGtsEK0N97DhHxz42DKV4pxspayKRRGeGUo=; b=RiIPF6/w+fWuGl9zkt6dSyRmnFosezYdmv8mR1fjvyjjXtOqrnhiiDCi IyT1jxT5EcdySiCyvsKx3JbBAaTpQsdF6EoM6OTIPcyKQQy6ekLmqKnaO Dw/ldOv3yzYFeoouzK/YIBUco1tOnJRnD99NBunDpqIRIpVdSLfa0Hghq luA82wqgR7WTdRAGbHrZbadHJiKoYfzCeOx6lZqBLgJLmt/SliSaHGrre X4VwwF0G7xq5Zyg3joSIKCrUUazH+7k2Bun6ZFsZLF9WrsKmCHbdXs8W6 KhxRL64soLaqIPmAijJmPhDENIjRmumsL6foSZ/Lkgj84pXyPZAkrmLME g==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025452" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025452" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355162" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355162" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:16 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, Jacob Pan Subject: [PATCH v3 03/10] iommu/vt-d: Add helper for nested domain allocation Date: Thu, 11 May 2023 07:51:03 -0700 Message-Id: <20230511145110.27707-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765609953581407827?= X-GMAIL-MSGID: =?utf-8?q?1765609953581407827?= From: Lu Baolu This adds helper for accepting user parameters and allocate a nested domain. Signed-off-by: Jacob Pan Signed-off-by: Lu Baolu Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/intel/Makefile | 2 +- drivers/iommu/intel/iommu.h | 2 ++ drivers/iommu/intel/nested.c | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/intel/nested.c diff --git a/drivers/iommu/intel/Makefile b/drivers/iommu/intel/Makefile index 7af3b8a4f2a0..5dabf081a779 100644 --- a/drivers/iommu/intel/Makefile +++ b/drivers/iommu/intel/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_DMAR_TABLE) += dmar.o -obj-$(CONFIG_INTEL_IOMMU) += iommu.o pasid.o +obj-$(CONFIG_INTEL_IOMMU) += iommu.o pasid.o nested.o obj-$(CONFIG_DMAR_TABLE) += trace.o cap_audit.o obj-$(CONFIG_DMAR_PERF) += perf.o obj-$(CONFIG_INTEL_IOMMU_DEBUGFS) += debugfs.o diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index e818520f4068..8d0c7970c1ad 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -858,6 +858,8 @@ void *alloc_pgtable_page(int node, gfp_t gfp); void free_pgtable_page(void *vaddr); void iommu_flush_write_buffer(struct intel_iommu *iommu); struct intel_iommu *device_to_iommu(struct device *dev, u8 *bus, u8 *devfn); +struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *s2_domain, + const union iommu_domain_user_data *user_data); #ifdef CONFIG_INTEL_IOMMU_SVM void intel_svm_check(struct intel_iommu *iommu); diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c new file mode 100644 index 000000000000..f83931bb44b6 --- /dev/null +++ b/drivers/iommu/intel/nested.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * nested.c - nested mode translation support + * + * Copyright (C) 2023 Intel Corporation + * + * Author: Lu Baolu + * Jacob Pan + */ + +#define pr_fmt(fmt) "DMAR: " fmt + +#include + +#include "iommu.h" + +static void intel_nested_domain_free(struct iommu_domain *domain) +{ + kfree(to_dmar_domain(domain)); +} + +static const struct iommu_domain_ops intel_nested_domain_ops = { + .free = intel_nested_domain_free, +}; + +struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *s2_domain, + const union iommu_domain_user_data *user_data) +{ + const struct iommu_hwpt_intel_vtd *vtd = (struct iommu_hwpt_intel_vtd *)user_data; + struct dmar_domain *domain; + + domain = kzalloc(sizeof(*domain), GFP_KERNEL_ACCOUNT); + if (!domain) + return NULL; + + domain->use_first_level = true; + domain->s2_domain = to_dmar_domain(s2_domain); + domain->s1_pgtbl = vtd->pgtbl_addr; + domain->s1_cfg = *vtd; + domain->domain.ops = &intel_nested_domain_ops; + domain->domain.type = IOMMU_DOMAIN_NESTED; + INIT_LIST_HEAD(&domain->devices); + spin_lock_init(&domain->lock); + xa_init(&domain->iommu_array); + + return &domain->domain; +} From patchwork Thu May 11 14:51:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92690 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4452271vqo; Thu, 11 May 2023 08:13:08 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4L/N3dV/dUDASY19jW1fZpqup/S7eud70+N0xkS4HlbOwuEv7gLYB8su5RGp/8YvOJNkEp X-Received: by 2002:a5d:9bd4:0:b0:769:c95b:29d2 with SMTP id d20-20020a5d9bd4000000b00769c95b29d2mr14301593ion.15.1683817988034; Thu, 11 May 2023 08:13:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683817988; cv=none; d=google.com; s=arc-20160816; b=fClDbm1onnNgBd2YRdjz1m3HzNJM+5c1HYj9jAvDlXW+xyNA411aLkmLqsDG/j6YHb 2ZiC3mgu1Vq3FodJdcRB/vFfd5ouQ7rK1WDbF3UmpP8eHGGG6IYPhBoQTmI9D27e+1Im 52KXJ3DF4Kjm+jFRM1Je+jOqveHgKgMjynrLEViz4oUytEPm/rL+c8hOGiJ+5AZfgoZ0 vH6VB/7Yjcf0acNyxRrm9jcA6Kc63Vimw8FKTUjwXix9i/SbFMRCBB56DsBqW0TLZ+Ke bPPZe6Q4fhdfzf2y9ZaiPP8xfFtPvPTczElbjX5rQBS9iquW3etlZ4X1e/ATd/WKamBk maFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tdedzjRDvAy5Ha10JNYcLT1QugyVG+qmd2qUrwtVZtw=; b=RtS9oVKAyEDTii2TJb+AhjwhB+0MYYTeSahp1Jg6UFe/BdjF4oQrhsFle5bSOm83jo VPUkDRzcyXAMAoW8E3ha+ErbCSDG+BQnCC+J0rNILE0vXwsuaXkxpjnFlm3dvkzBN3yq BkeQBaHlnngrlo6Qj3Mek8wbSUuJq8sxuFFlLQxgd1oCluUmu8B0U4ejA8t3r/shoPUp DxFmAi2TygKbVLER0Nu0rsiWOfyvfnwkL4MTuoT7ysPcD0jNOYacZEU0bkEUX4/Qzpd+ KGa5zkZLRBOubp2skWxF0umJawkmwkCfZdSgGEokquGyuasn/8vJtl6PqytvHLgQBoJK kPbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KIY2KH36; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a2-20020a6b6c02000000b00760ee5c9b12si7680008ioh.53.2023.05.11.08.12.51; Thu, 11 May 2023 08:13:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KIY2KH36; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238654AbjEKOwv (ORCPT + 99 others); Thu, 11 May 2023 10:52:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238684AbjEKOwZ (ORCPT ); Thu, 11 May 2023 10:52:25 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E2611157B; Thu, 11 May 2023 07:51:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816695; x=1715352695; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NigTi5m0fZJzsmo917jxile/cna9k2FwZdUaMHKRWz8=; b=KIY2KH36Kz3odtNi9sseMyvJUWvfZ5MngKO+5a6iZPBMBwTRcLU4FGo9 46FsM8gZZBMCMwJ/OnRK+naNgNjmuWOiHLAB5Nc3GlbV+50lJYVZ798wQ 368rMZr5As7UmsVZmZz8Nln1Z5C3TJUvBjedA7e3HwRO4ZHpEhu0IKAVG krdKLzXgSqmiIeMhgAKr26b4HrxEkmRG+45f8eMsYzDPObW2DXEG9TCKS CLgVOI9FjYWcruJTa4yqEN5SpQIi6WTkOPWt7RLJMx3dYXHJ/CYBNkNrW zzDV6rN0rJcYze5d9AHra3Xfyvd0LYf5ZXDI+E6FfyeReFETEqjyuBDA/ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025465" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025465" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355170" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355170" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:17 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, Jacob Pan Subject: [PATCH v3 04/10] iommu/vt-d: Add helper to setup pasid nested translation Date: Thu, 11 May 2023 07:51:04 -0700 Message-Id: <20230511145110.27707-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765611130179914660?= X-GMAIL-MSGID: =?utf-8?q?1765611130179914660?= From: Lu Baolu The configurations are passed in from the user when the user domain is allocated. This helper interprets these configurations according to the data structure defined in uapi/linux/iommufd.h. The EINVAL error will be returned if any of configurations are not compatible with the hardware capabilities. The caller can retry with another compatible user domain. The encoding of fields of each pasid entry is defined in section 9.6 of the VT-d spec. Signed-off-by: Jacob Pan Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/pasid.c | 151 ++++++++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.h | 2 + 2 files changed, 153 insertions(+) diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c index c5d479770e12..ee4c7b69c6c6 100644 --- a/drivers/iommu/intel/pasid.c +++ b/drivers/iommu/intel/pasid.c @@ -21,6 +21,11 @@ #include "iommu.h" #include "pasid.h" +#define IOMMU_VTD_PGTBL_MTS_MASK (IOMMU_VTD_PGTBL_CD | \ + IOMMU_VTD_PGTBL_EMTE | \ + IOMMU_VTD_PGTBL_PCD | \ + IOMMU_VTD_PGTBL_PWT) + /* * Intel IOMMU system wide PASID name space: */ @@ -335,6 +340,15 @@ static inline void pasid_set_fault_enable(struct pasid_entry *pe) pasid_set_bits(&pe->val[0], 1 << 1, 0); } +/* + * Setup the SRE(Supervisor Request Enable) field (Bit 128) of a + * scalable mode PASID entry. + */ +static inline void pasid_set_sre(struct pasid_entry *pe) +{ + pasid_set_bits(&pe->val[2], 1 << 0, 1); +} + /* * Setup the WPE(Write Protect Enable) field (Bit 132) of a * scalable mode PASID entry. @@ -402,6 +416,15 @@ pasid_set_flpm(struct pasid_entry *pe, u64 value) pasid_set_bits(&pe->val[2], GENMASK_ULL(3, 2), value << 2); } +/* + * Setup the Extended Access Flag Enable (EAFE) field (Bit 135) + * of a scalable mode PASID entry. + */ +static inline void pasid_set_eafe(struct pasid_entry *pe) +{ + pasid_set_bits(&pe->val[2], 1 << 7, 1 << 7); +} + static void pasid_cache_invalidation_with_pasid(struct intel_iommu *iommu, u16 did, u32 pasid) @@ -713,3 +736,131 @@ void intel_pasid_setup_page_snoop_control(struct intel_iommu *iommu, if (!cap_caching_mode(iommu->cap)) devtlb_invalidation_with_pasid(iommu, dev, pasid); } + +/** + * intel_pasid_setup_nested() - Set up PASID entry for nested translation. + * This could be used for guest shared virtual address. In this case, the + * first level page tables are used for GVA-GPA translation in the guest, + * second level page tables are used for GPA-HPA translation. + * + * @iommu: IOMMU which the device belong to + * @dev: Device to be set up for translation + * @pasid: PASID to be programmed in the device PASID table + * @domain: User domain nested on a s2 domain + */ +int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, + u32 pasid, struct dmar_domain *domain) +{ + struct iommu_hwpt_intel_vtd *s1_cfg = &domain->s1_cfg; + pgd_t *s1_gpgd = (pgd_t *)(uintptr_t)domain->s1_pgtbl; + struct dmar_domain *s2_domain = domain->s2_domain; + u16 did = domain_id_iommu(domain, iommu); + struct dma_pte *pgd = s2_domain->pgd; + struct pasid_entry *pte; + int agaw; + + if (!ecap_nest(iommu->ecap)) { + pr_err_ratelimited("%s: No nested translation support\n", + iommu->name); + return -ENODEV; + } + + /* + * Sanity checking performed by caller to make sure address width + * matching in two dimensions: CPU vs. IOMMU, guest vs. host. + */ + switch (s1_cfg->addr_width) { + case ADDR_WIDTH_4LEVEL: + break; +#ifdef CONFIG_X86 + case ADDR_WIDTH_5LEVEL: + if (!cpu_feature_enabled(X86_FEATURE_LA57) || + !cap_fl5lp_support(iommu->cap)) { + dev_err_ratelimited(dev, + "5-level paging not supported\n"); + return -EINVAL; + } + break; +#endif + default: + dev_err_ratelimited(dev, "Invalid guest address width %d\n", + s1_cfg->addr_width); + return -EINVAL; + } + + if ((s1_cfg->flags & IOMMU_VTD_PGTBL_SRE) && !ecap_srs(iommu->ecap)) { + pr_err_ratelimited("No supervisor request support on %s\n", + iommu->name); + return -EINVAL; + } + + if ((s1_cfg->flags & IOMMU_VTD_PGTBL_EAFE) && !ecap_eafs(iommu->ecap)) { + pr_err_ratelimited("No extended access flag support on %s\n", + iommu->name); + return -EINVAL; + } + + /* + * Memory type is only applicable to devices inside processor coherent + * domain. Will add MTS support once coherent devices are available. + */ + if (s1_cfg->flags & IOMMU_VTD_PGTBL_MTS_MASK) { + pr_warn_ratelimited("No memory type support %s\n", + iommu->name); + return -EINVAL; + } + + agaw = iommu_skip_agaw(s2_domain, iommu, &pgd); + if (agaw < 0) { + dev_err_ratelimited(dev, "Invalid domain page table\n"); + return -EINVAL; + } + + /* First level PGD (in GPA) must be supported by the second level. */ + if ((uintptr_t)s1_gpgd > (1ULL << s2_domain->gaw)) { + dev_err_ratelimited(dev, + "Guest PGD %lx not supported, max %llx\n", + (uintptr_t)s1_gpgd, s2_domain->max_addr); + return -EINVAL; + } + + spin_lock(&iommu->lock); + pte = intel_pasid_get_entry(dev, pasid); + if (!pte) { + spin_unlock(&iommu->lock); + return -ENODEV; + } + if (pasid_pte_is_present(pte)) { + spin_unlock(&iommu->lock); + return -EBUSY; + } + + pasid_clear_entry(pte); + + if (s1_cfg->addr_width == ADDR_WIDTH_5LEVEL) + pasid_set_flpm(pte, 1); + + pasid_set_flptr(pte, (uintptr_t)s1_gpgd); + + if (s1_cfg->flags & IOMMU_VTD_PGTBL_SRE) { + pasid_set_sre(pte); + if (s1_cfg->flags & IOMMU_VTD_PGTBL_WPE) + pasid_set_wpe(pte); + } + + if (s1_cfg->flags & IOMMU_VTD_PGTBL_EAFE) + pasid_set_eafe(pte); + + pasid_set_slptr(pte, virt_to_phys(pgd)); + pasid_set_fault_enable(pte); + pasid_set_domain_id(pte, did); + pasid_set_address_width(pte, agaw); + pasid_set_page_snoop(pte, !!ecap_smpwc(iommu->ecap)); + pasid_set_translation_type(pte, PASID_ENTRY_PGTT_NESTED); + pasid_set_present(pte); + spin_unlock(&iommu->lock); + + pasid_flush_caches(iommu, pte, pasid, did); + + return 0; +} diff --git a/drivers/iommu/intel/pasid.h b/drivers/iommu/intel/pasid.h index d6b7d21244b1..864b12848392 100644 --- a/drivers/iommu/intel/pasid.h +++ b/drivers/iommu/intel/pasid.h @@ -111,6 +111,8 @@ int intel_pasid_setup_second_level(struct intel_iommu *iommu, int intel_pasid_setup_pass_through(struct intel_iommu *iommu, struct dmar_domain *domain, struct device *dev, u32 pasid); +int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, + u32 pasid, struct dmar_domain *domain); void intel_pasid_tear_down_entry(struct intel_iommu *iommu, struct device *dev, u32 pasid, bool fault_ignore); From patchwork Thu May 11 14:51:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4440463vqo; Thu, 11 May 2023 08:01:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ442+P0E12QND/r94UHEF2LUh2/9V9JkV91qDtPLLtvjMER5nOxg/gvEVPzFbV5kpwYMS4m X-Received: by 2002:a05:6a20:4296:b0:103:a9ee:d732 with SMTP id o22-20020a056a20429600b00103a9eed732mr5002304pzj.9.1683817259539; Thu, 11 May 2023 08:00:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683817259; cv=none; d=google.com; s=arc-20160816; b=En14hYkbNnAL+q+cCOzYa8MPveUHcf4Q9YTtPKuU1CxHZJNJBGaiUuOaMl4ptJ8XMb p3amVy2G5IOc1Aqs5Gkn6wPpg/GkDrQyL6mJ72WUBfZEApBJc1Qx6V91WVoVEVsQamDH IPoqoDUA5iQovYUj0Uflq5tNsDOtx0MTs3DUXllWuAklDDJMKkwJmn0F3yWzK3SVS+L7 lwPrDKM3s0z6DGI645cZAjGBdmDnz5f8Ndk233X9Y5Ojz6a9nRYuK0v8HHW2TfS5zNwN rDdG4bcFM3kzlxmshnzGRHJcaJciFZv57tBJF7r32vLYznhZ7M2BgDqRfzy3mdzMojCQ Ig1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=S3Jqyvt4Cm/MiEPDKJw/cn0pSUETyMLHr1qqPxoT4rQ=; b=pGYD2SgDUrTa+/yNQhDhFNthFHTJzFy2Le8DdmijCFatp68FoNSQtZb7axvp4UXQIF ZellIzys4w9B1Z4vk/uPxJ1Hg+Qma+d6xE9qbmOdytSo8Jh9oJgWrqNYkwZdr0hSOjKZ QHde/kGxyl+HYLypoe66cFMPyYG/F8e0PQoX1ovAGsUuZl5beF1kejqucOR+h6IA3tiV kWxiHrzb+FGKtDxMMAyxUsYQWV5K4dWO7LA+HMyrPfaA/saEOHi6JXG9RoZGmNCygFwJ +GXDC3AW8uA8P4OrWs2FQPtZoo9rfhHasVoeEqKm/IcDsGmkthvanFduP4be3wDfxP8j m8rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZmxcEkAf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d1-20020a621d01000000b00645e8c9e27esi7649997pfd.386.2023.05.11.08.00.33; Thu, 11 May 2023 08:00:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZmxcEkAf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238421AbjEKOwx (ORCPT + 99 others); Thu, 11 May 2023 10:52:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238594AbjEKOwZ (ORCPT ); Thu, 11 May 2023 10:52:25 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E612F106F4; Thu, 11 May 2023 07:51:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816697; x=1715352697; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0f1JGfnc4TXl9XjUG23GlF5N7yyId/J6T8qWeC3+VfQ=; b=ZmxcEkAfR8cgE1TZGwx5tcSRiQhC+4WuXVjrAJT70rqewg95dN5hxQzg hFDSx0rlT78Yul+VV3R2uw8cTdLU5BwYR9aS1/IpcIYMTvxQU4BfmGxsv bPcwbbETjxIVYn6tnKLcew3xmVNmzNI6h7pyJ0fSAcZD5wYKNJruqy6h5 xG4IJR+MyCBrpnvGpo2y0pm8GRksByUUZWEz4qc6iUJ6ZYzFA95BHWZ6e UkC/bagjW8kQr2Vn0r+P+QhZuRXCd4QTevGs88vJWxks1PgFR0mj/KDIG n1Lp2Jl/+xhcvwh0SapuiJKTAJx4kyCFud/l/mZ1UiKBgNOukY7yJKY7h Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025475" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025475" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355176" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355176" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:17 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 05/10] iommu/vt-d: Make domain attach helpers to be extern Date: Thu, 11 May 2023 07:51:05 -0700 Message-Id: <20230511145110.27707-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765610366353503603?= X-GMAIL-MSGID: =?utf-8?q?1765610366353503603?= hence can be used in the nested.c Suggested-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.c | 17 +++++++---------- drivers/iommu/intel/iommu.h | 8 ++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index b871a6afd803..e6536a43dd82 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -277,7 +277,6 @@ static LIST_HEAD(dmar_satc_units); #define for_each_rmrr_units(rmrr) \ list_for_each_entry(rmrr, &dmar_rmrr_units, list) -static void device_block_translation(struct device *dev); static void intel_iommu_domain_free(struct iommu_domain *domain); int dmar_disabled = !IS_ENABLED(CONFIG_INTEL_IOMMU_DEFAULT_ON); @@ -555,7 +554,7 @@ static unsigned long domain_super_pgsize_bitmap(struct dmar_domain *domain) } /* Some capabilities may be different across iommus */ -static void domain_update_iommu_cap(struct dmar_domain *domain) +void domain_update_iommu_cap(struct dmar_domain *domain) { domain_update_iommu_coherency(domain); domain->iommu_superpage = domain_update_iommu_superpage(domain, NULL); @@ -1740,8 +1739,7 @@ static struct dmar_domain *alloc_domain(unsigned int type) return domain; } -static int domain_attach_iommu(struct dmar_domain *domain, - struct intel_iommu *iommu) +int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) { struct iommu_domain_info *info, *curr; unsigned long ndomains; @@ -1790,8 +1788,7 @@ static int domain_attach_iommu(struct dmar_domain *domain, return ret; } -static void domain_detach_iommu(struct dmar_domain *domain, - struct intel_iommu *iommu) +void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu) { struct iommu_domain_info *info; @@ -3994,7 +3991,7 @@ static void dmar_remove_one_dev_info(struct device *dev) * all DMA requests without PASID from the device are blocked. If the page * table has been set, clean up the data structures. */ -static void device_block_translation(struct device *dev) +void device_block_translation(struct device *dev) { struct device_domain_info *info = dev_iommu_priv_get(dev); struct intel_iommu *iommu = info->iommu; @@ -4101,8 +4098,8 @@ static void intel_iommu_domain_free(struct iommu_domain *domain) domain_exit(to_dmar_domain(domain)); } -static int prepare_domain_attach_device(struct iommu_domain *domain, - struct device *dev) +int prepare_domain_attach_device(struct iommu_domain *domain, + struct device *dev) { struct dmar_domain *dmar_domain = to_dmar_domain(domain); struct intel_iommu *iommu; @@ -4342,7 +4339,7 @@ static void domain_set_force_snooping(struct dmar_domain *domain) PASID_RID2PASID); } -static bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain) +bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain) { struct dmar_domain *dmar_domain = to_dmar_domain(domain); unsigned long flags; diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 8d0c7970c1ad..ccb93aed6cf2 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -852,6 +852,14 @@ int qi_submit_sync(struct intel_iommu *iommu, struct qi_desc *desc, */ #define QI_OPT_WAIT_DRAIN BIT(0) +int domain_attach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu); +void domain_detach_iommu(struct dmar_domain *domain, struct intel_iommu *iommu); +void device_block_translation(struct device *dev); +int prepare_domain_attach_device(struct iommu_domain *domain, + struct device *dev); +bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain); +void domain_update_iommu_cap(struct dmar_domain *domain); + int dmar_ir_support(void); void *alloc_pgtable_page(int node, gfp_t gfp); From patchwork Thu May 11 14:51:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92688 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4450657vqo; Thu, 11 May 2023 08:11:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6VYraZExXQJrZOPQD3y2lK6nuIYraHyPefjTSxKzq3lU4yWB1OlwUJDbSqPsM9VydcmzAC X-Received: by 2002:a05:6a20:394e:b0:100:eb6e:8ceb with SMTP id r14-20020a056a20394e00b00100eb6e8cebmr16433369pzg.34.1683817879195; Thu, 11 May 2023 08:11:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683817879; cv=none; d=google.com; s=arc-20160816; b=kLR3hvYZ9qW4FncFcDTbJzq5dXKJy7r6GQzfXVlWxzjinFxbST2yczaL2TG1i8zdM3 24ifn6Dm1rg/5I1z+RYdZcX37hV9W6qVbHUqRrv/r7EUPFO9vObc2e1RTl0yrINViwLI +c4KMpRUIKpOePCyyUDBtLYbw7bBSYvGuh07YOCuLFsMNpvcJd8pKkOFNL8sJYciImlL SEfEcg2e72UxNOBZ1ecfoHnjgXS2hmvd7/TfSHUgyao1PAUX6YbojH2oSrDnTlC8lDOL 4QYOQWPUM++MK14malO4xn6kMwLpTweQ2KZ+AIPqv/xjVAivg36BKcZEI07RCqJTWaEK 7mtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hEuAkz0sPfvwSxBmfOGtYLezn21ndUd3ZcDzpmsS0J8=; b=Ty5g9O3RlbZ6aal9Qk9J6cUxvMos3PpspRu7SGLBOEImUevBpAsdpiXi79qmvsNFUF QEtgUh/Ml3NNPemwCbyoYQ1oT3Fo8uuyYeoBL+SPO8M2oDDfTzu99jkEKm6T2KFrm2Bn Q2Mxi5BkSXwRqOY1j7lXVM2tiX7njslWh4w0dCT2rn3AeZNcoB+5Q/BiIDFr9xMWvkBY gFMctrg0B2yI8xy7gs5F5hEM6CwKkKU1OEFUT/RPHEqV8SCUd6sGXSvNFaBqKBAdlK9r K2zYf5UFi7r4bIXdm4w8g6Yo4oNThQ3Qjf3X5Rxd+ujk9AsfUXYC/f9jO47iImqccEcU HwzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bT419Osj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x27-20020a63b21b000000b005208c4fa34csi7084469pge.773.2023.05.11.08.11.03; Thu, 11 May 2023 08:11:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bT419Osj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238035AbjEKOxD (ORCPT + 99 others); Thu, 11 May 2023 10:53:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238713AbjEKOw3 (ORCPT ); Thu, 11 May 2023 10:52:29 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C71781162A; Thu, 11 May 2023 07:51:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816715; x=1715352715; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HHmU4NuXTgy/FGhmw0qkoPgFcf2uTx6QDJENWFf5SaI=; b=bT419Osj27Ay+8xpde73BLeqHGF/Zoc2MMNbqhECLd+jeKVdctBzxAWc rmPjjZoe7L1IP1EXa1CyWTfXF+zLfprITS14qFVLfa57FgFL4XMCx+5gu rm7GujrZ91AntvQJO6BIW59gFxe7xrf79u/ZM9Xfn5l/rt2rWrWY/xHEL ruM6zFzvfaExJv6N2o9F3ChMTCWCurWTLDm1ylI+nszbLXkKUFgKf4OMv jI+4Mx1+C7g0nzdjDbE0yyCle5vDV1BFqPEpcWTkAKRifwvrSFanjaRZJ 8rv7dFH7s8J/1jp07HjkOz+Z5wvvpbE67Axw7par2/j3aiPPQzJTLuiQ0 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025493" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025493" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355180" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355180" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:18 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com, Jacob Pan Subject: [PATCH v3 06/10] iommu/vt-d: Set the nested domain to a device Date: Thu, 11 May 2023 07:51:06 -0700 Message-Id: <20230511145110.27707-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765611016622098581?= X-GMAIL-MSGID: =?utf-8?q?1765611016622098581?= hence enable nested domain usage on Intel VT-d. Signed-off-by: Jacob Pan Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/nested.c | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c index f83931bb44b6..fd38424b78f0 100644 --- a/drivers/iommu/intel/nested.c +++ b/drivers/iommu/intel/nested.c @@ -11,8 +11,53 @@ #define pr_fmt(fmt) "DMAR: " fmt #include +#include +#include #include "iommu.h" +#include "pasid.h" + +static int intel_nested_attach_dev(struct iommu_domain *domain, + struct device *dev) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + struct intel_iommu *iommu = info->iommu; + unsigned long flags; + int ret = 0; + + if (info->domain) + device_block_translation(dev); + + /* Is s2_domain compatible with this IOMMU? */ + ret = prepare_domain_attach_device(&dmar_domain->s2_domain->domain, dev); + if (ret) { + dev_err_ratelimited(dev, "s2 domain is not compatible\n"); + return ret; + } + + ret = domain_attach_iommu(dmar_domain, iommu); + if (ret) { + dev_err_ratelimited(dev, "Failed to attach domain to iommu\n"); + return ret; + } + + ret = intel_pasid_setup_nested(iommu, dev, + PASID_RID2PASID, dmar_domain); + if (ret) { + domain_detach_iommu(dmar_domain, iommu); + dev_err_ratelimited(dev, "Failed to setup pasid entry\n"); + return ret; + } + + info->domain = dmar_domain; + spin_lock_irqsave(&dmar_domain->lock, flags); + list_add(&info->link, &dmar_domain->devices); + spin_unlock_irqrestore(&dmar_domain->lock, flags); + domain_update_iommu_cap(dmar_domain); + + return 0; +} static void intel_nested_domain_free(struct iommu_domain *domain) { @@ -20,7 +65,9 @@ static void intel_nested_domain_free(struct iommu_domain *domain) } static const struct iommu_domain_ops intel_nested_domain_ops = { + .attach_dev = intel_nested_attach_dev, .free = intel_nested_domain_free, + .enforce_cache_coherency = intel_iommu_enforce_cache_coherency, }; struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *s2_domain, From patchwork Thu May 11 14:51:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92685 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4448456vqo; Thu, 11 May 2023 08:08:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6DCd5C1ocfp2SZDu6yH+Rc0/gNVbsbhTzdudTA3Z661Go1HQtBAnjfu29eYRAN7H1iGn+x X-Received: by 2002:a05:6602:2096:b0:74c:fe71:5808 with SMTP id a22-20020a056602209600b0074cfe715808mr15561313ioa.6.1683817725872; Thu, 11 May 2023 08:08:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683817725; cv=none; d=google.com; s=arc-20160816; b=Vvu9LCl4VL66KZV72nPz2fXuAEWPpIXiAMCdIqC19L3Y6d4ARPVSWnHFE/WcFSeZ8E X7N/HXjc+orbATD+N6C+3hiuAXAKx5/iGyZcf8672CiCRPzbmWb45s/FswgGhgpq9AAg QDqb57PX3zowVdyaHrbjhLnVXKHkfaTGsQ/Rr7eIoKRGGjMwPtM0AcYFEPH5Aoh2gXqW Gh3gH9YmAvttzY3QLUB+GzAWw1Zh0+usgwUplEUsyhGzrKJFXNY+ewvwVvQnkIq6rPCG Uz2vDh6eN1fT+1AHDSdr4V4CbcbSYXjhMpMquqo8FtdRn20ia4xa4wFA7IF/4lrEocYA 63+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=m+eToG8xdRiOB5DCZ1aIf+9CVJXWwntZ/Xmetgpw8ds=; b=LuNsd8lQjy9umXGrCJKl4kQag7yXwMhg51ujNUrQcpALnVoLihjRffJ6KMGYhehaBt 8iWgpKJDbH9LBxOqrlRIXs2k8+RYYfQdz8x7jhb0aEBGwDcaK5ClzmFnWdXxFXDVCrmE Lp5o5baHazjr3Xyn3MvKMmyMfF/a1DIQIT38F5uGQRoJaPiFAPJqNQIsq7ozy9E9/UN+ J3tVneGYgV0UNUFvPv9apKbMzX4HD7nXkRxbpXFHycBd/Un9bENraYBVvU+2lGoTXbq+ 9kwSiFbqmawMm430kbUED0uIayg4KK7ve27Jhqq/F1BCkDMam2EZDMHj+JiB8xIcFmps 96vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ld8fMPTP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v20-20020a056602015400b00760d2af1113si6753041iot.146.2023.05.11.08.08.29; Thu, 11 May 2023 08:08:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ld8fMPTP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238441AbjEKOxQ (ORCPT + 99 others); Thu, 11 May 2023 10:53:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238641AbjEKOwb (ORCPT ); Thu, 11 May 2023 10:52:31 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24FFC11B4A; Thu, 11 May 2023 07:51:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816717; x=1715352717; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bXtmswM4fcZmRyz4ffkT34RI+fzlROFvWgl83sjywhU=; b=ld8fMPTPArpIWvnkUyNC52zVQHeWyg7AVMoi+qRAtXZEtkzcWJNW9DQl snvWKDsgGkKCwYw0FNAvoUL4Bjk81iI8iHcl3cVJfhjHQQ2FMNK4MneYe izR1TyETNPBhRJ2OhGAiYtLEbHqgxfMAc2ssqkErYXoHXGtOR2mU9TbBF iOdBCa/chxCwCVk5FTiRJIrRBpvL1hj7qPkWqVsowM2p/1rp/sxEYnRsB ytQhqfaO/HE9LeFYEsufB9WtyYjPI+GH8CmB57cVF+lHSVcxC82+p+5hr /lswZ2nJqLBZpQKQvFC3WxpMaopP2x1tcR2GSXFIyg5NOsnamLeh11Dgm g==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025498" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025498" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355185" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355185" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:19 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 07/10] iommu/vt-d: Add iotlb flush for nested domain Date: Thu, 11 May 2023 07:51:07 -0700 Message-Id: <20230511145110.27707-8-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765610855448264770?= X-GMAIL-MSGID: =?utf-8?q?1765610855448264770?= This is needed as the stage-1 page table of the nested domain is maintained outside the iommu subsystem, hence, needs to support iotlb flush requests. This adds the data structure for flushing iotlb for the nested domain allocated with IOMMU_HWPT_TYPE_VTD_S1 type and the related callback to accept iotlb flush request from IOMMUFD. This only exposes the interface for invalidating IOTLB, but no for device-TLB as device-TLB invalidation will be covered automatically in IOTLB invalidation if the affected device is ATS-capable. Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.c | 10 +++--- drivers/iommu/intel/iommu.h | 6 ++++ drivers/iommu/intel/nested.c | 69 ++++++++++++++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 6 ++++ include/uapi/linux/iommufd.h | 59 ++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index e6536a43dd82..5f27cee4656a 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -1474,10 +1474,10 @@ static void iommu_flush_dev_iotlb(struct dmar_domain *domain, spin_unlock_irqrestore(&domain->lock, flags); } -static void iommu_flush_iotlb_psi(struct intel_iommu *iommu, - struct dmar_domain *domain, - unsigned long pfn, unsigned int pages, - int ih, int map) +void iommu_flush_iotlb_psi(struct intel_iommu *iommu, + struct dmar_domain *domain, + unsigned long pfn, unsigned int pages, + int ih, int map) { unsigned int aligned_pages = __roundup_pow_of_two(pages); unsigned int mask = ilog2(aligned_pages); @@ -1550,7 +1550,7 @@ static inline void __mapping_notify_one(struct intel_iommu *iommu, iommu_flush_write_buffer(iommu); } -static void intel_flush_iotlb_all(struct iommu_domain *domain) +void intel_flush_iotlb_all(struct iommu_domain *domain) { struct dmar_domain *dmar_domain = to_dmar_domain(domain); struct iommu_domain_info *info; diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index ccb93aed6cf2..581596d90c1b 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -859,6 +859,12 @@ int prepare_domain_attach_device(struct iommu_domain *domain, struct device *dev); bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain); void domain_update_iommu_cap(struct dmar_domain *domain); +void iommu_flush_iotlb_psi(struct intel_iommu *iommu, + struct dmar_domain *domain, + unsigned long pfn, unsigned int pages, + int ih, int map); +void intel_flush_iotlb_all(struct iommu_domain *domain); + int dmar_ir_support(void); diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c index fd38424b78f0..d13fbcd3f5a6 100644 --- a/drivers/iommu/intel/nested.c +++ b/drivers/iommu/intel/nested.c @@ -64,8 +64,77 @@ static void intel_nested_domain_free(struct iommu_domain *domain) kfree(to_dmar_domain(domain)); } +static void intel_nested_invalidate(struct device *dev, + struct dmar_domain *domain, + u64 addr, unsigned long npages) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct intel_iommu *iommu = info->iommu; + + if (addr == 0 && npages == -1) + intel_flush_iotlb_all(&domain->domain); + else + iommu_flush_iotlb_psi(iommu, domain, + addr >> VTD_PAGE_SHIFT, + npages, 1, 0); +} + +static int intel_nested_cache_invalidate_user(struct iommu_domain *domain, + void *user_data) +{ + struct iommu_hwpt_invalidate_request_intel_vtd *req = user_data; + struct iommu_hwpt_invalidate_intel_vtd *inv_info = user_data; + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + unsigned int entry_size = inv_info->entry_size; + u64 uptr = inv_info->inv_data_uptr; + u64 nr_uptr = inv_info->entry_nr_uptr; + struct device_domain_info *info; + u32 entry_nr, index; + unsigned long flags; + int ret = 0; + + if (WARN_ON(!user_data)) + return 0; + + if (get_user(entry_nr, (uint32_t __user *)u64_to_user_ptr(nr_uptr))) + return -EFAULT; + + if (!entry_nr) + return -EINVAL; + + for (index = 0; index < entry_nr; index++) { + ret = copy_struct_from_user(req, sizeof(*req), + u64_to_user_ptr(uptr + index * entry_size), + entry_size); + if (ret) { + pr_err_ratelimited("Failed to fetch invalidation request\n"); + break; + } + + if (req->__reserved || (req->flags & ~IOMMU_VTD_QI_FLAGS_LEAF) || + !IS_ALIGNED(req->addr, VTD_PAGE_SIZE)) { + ret = -EINVAL; + break; + } + + spin_lock_irqsave(&dmar_domain->lock, flags); + list_for_each_entry(info, &dmar_domain->devices, link) + intel_nested_invalidate(info->dev, dmar_domain, + req->addr, req->npages); + spin_unlock_irqrestore(&dmar_domain->lock, flags); + } + + if (ret && put_user(index, (uint32_t __user *)u64_to_user_ptr(nr_uptr))) + return -EFAULT; + + return ret; +} + static const struct iommu_domain_ops intel_nested_domain_ops = { .attach_dev = intel_nested_attach_dev, + .cache_invalidate_user = intel_nested_cache_invalidate_user, + .cache_invalidate_user_data_len = + sizeof(struct iommu_hwpt_invalidate_intel_vtd), .free = intel_nested_domain_free, .enforce_cache_coherency = intel_iommu_enforce_cache_coherency, }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 39922f83ce34..b338b082950b 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -282,6 +282,12 @@ union ucmd_buffer { #ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test; #endif + /* + * hwpt_type specific structure used in the cache invalidation + * path. + */ + struct iommu_hwpt_invalidate_intel_vtd vtd; + struct iommu_hwpt_invalidate_request_intel_vtd req_vtd; }; struct iommufd_ioctl_op { diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index c2658394827a..2e658fa346ad 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -505,6 +505,63 @@ struct iommu_hw_info { }; #define IOMMU_DEVICE_GET_HW_INFO _IO(IOMMUFD_TYPE, IOMMUFD_CMD_DEVICE_GET_HW_INFO) +/** + * enum iommu_hwpt_intel_vtd_invalidate_flags - Flags for Intel VT-d + * stage-1 page table cache + * invalidation + * @IOMMU_VTD_QI_FLAGS_LEAF: The LEAF flag indicates whether only the + * leaf PTE caching needs to be invalidated + * and other paging structure caches can be + * preserved. + */ +enum iommu_hwpt_intel_vtd_invalidate_flags { + IOMMU_VTD_QI_FLAGS_LEAF = 1 << 0, +}; + +/** + * struct iommu_hwpt_invalidate_request_intel_vtd - Intel VT-d cache invalidation request + * @addr: The start address of the addresses to be invalidated. + * @npages: Number of contiguous 4K pages to be invalidated. + * @flags: Combination of enum iommu_hwpt_intel_vtd_invalidate_flags + * @__reserved: Must be 0 + * + * The Intel VT-d specific invalidation data for user-managed stage-1 cache + * invalidation under nested translation. Userspace uses this structure to + * tell host about the impacted caches after modifying the stage-1 page table. + * + * Invalidating all the caches related to the hw_pagetable by setting + * @addr==0 and @npages==__u64(-1). + */ +struct iommu_hwpt_invalidate_request_intel_vtd { + __u64 addr; + __u64 npages; + __u32 flags; + __u32 __reserved; +}; + +/** + * struct iommu_hwpt_invalidate_intel_vtd - Intel VT-d cache invalidation info + * @flags: Must be 0 + * @entry_size: Size in bytes of each cache invalidation request + * @entry_nr_uptr: User pointer to the number of invalidation requests. + * Kernel reads it to get the number of requests and + * updates the buffer with the number of requests that + * have been processed successfully. This pointer must + * point to a __u32 type of memory location. + * @inv_data_uptr: Pointer to the cache invalidation requests + * + * The Intel VT-d specific invalidation data for a set of cache invalidation + * requests. Kernel loops the requests one-by-one and stops when failure + * is encountered. The number of handled requests is reported to user by + * writing the buffer pointed by @entry_nr_uptr. + */ +struct iommu_hwpt_invalidate_intel_vtd { + __u32 flags; + __u32 entry_size; + __u64 entry_nr_uptr; + __u64 inv_data_uptr; +}; + /** * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) * @size: sizeof(struct iommu_hwpt_invalidate) @@ -520,6 +577,8 @@ struct iommu_hw_info { * +==============================+========================================+ * | @hwpt_type | Data structure in @data_uptr | * +------------------------------+----------------------------------------+ + * | IOMMU_HWPT_TYPE_VTD_S1 | struct iommu_hwpt_invalidate_intel_vtd | + * +------------------------------+----------------------------------------+ */ struct iommu_hwpt_invalidate { __u32 size; From patchwork Thu May 11 14:51:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92691 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4452908vqo; Thu, 11 May 2023 08:13:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Zfr0WQzOFcus7QW5opF4rT1NsWrkRIBzGCIyPtt30PRidmRcOVdUP0EXces5Ov3RJd7jk X-Received: by 2002:a92:d583:0:b0:332:a056:41e8 with SMTP id a3-20020a92d583000000b00332a05641e8mr16058670iln.29.1683818034316; Thu, 11 May 2023 08:13:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683818034; cv=none; d=google.com; s=arc-20160816; b=w9eo80Yhw+q8gTRYDTqyMPtWWfna1EP1WmBSS4sRpUaom25Ju+/ff3d3sYwwWk05p5 v4rj3n/pVKv6XIwu3upB5mzJbxzkSiUo+W+XAmjdNXFRR6150B5MpoP0yhPskn53xZxl 4TPBnY5rEmzhmjVTogsJvEYPyh2Nj8LuHtvIJFJxV1iovO/lq8KMvl8P6MFj0jjYgt7l 2h7BYy9USd9YVu485DngQOcWKyoo8brONSGolq2c3/82AA6/o3f0XOSTr4b6U87GacXO 4noydCOJtga7sKxNY4AOJqdVy0GsG3v1rfC9Dh6mufioY9VGaO5NATNh9W1Yu16nfbVX xRYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=EwN7MFriZt/LL2sVfwfiIKF7eD1Sx64Qd+JXw9U6kT4=; b=gyqGhoXxU5M/E50KisK3q2YAux6R6NQmvvOA/EOpsoJzo+Fh2vOgXTihDtQ9OI9q4l ElJ+mQ2rbQcd3ZD5mNUtbvFz2Vr/MQlL/Iq7iuz12yIaPS+mXC9roNKYsBpeOAxhER5v dSI952XkglzyF/0I1pJ+6eDBF2Z0U6TNd13M1lR+epdDirpGKM4l7NvT08eAMnx4/zU7 DMaFOZsHXKFKOA6Q3sfqiex71FiEMPWS+CTzfE+QyWZxEcEi3xFXetChaESvMDFjly1b AZDoE2aC0dk8+avidhyBXfdDh5VwmBqT0vi1MI/8l1SbKAfPneGs3OgdBb6qUtgo8r2w /SIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gyhkbiid; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 10-20020a92130a000000b00335fc34ba16si918386ilt.51.2023.05.11.08.13.39; Thu, 11 May 2023 08:13:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gyhkbiid; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238708AbjEKOxL (ORCPT + 99 others); Thu, 11 May 2023 10:53:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238054AbjEKOwa (ORCPT ); Thu, 11 May 2023 10:52:30 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE0B91163B; Thu, 11 May 2023 07:51:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816717; x=1715352717; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Vbt/gRKp77bRwjDyaiyibCBpUzoPaDniGF7JiqNVd8w=; b=gyhkbiidSgrhdaPfM1/1RuiYv9+GLsmw5Lu4AZm+Fs21jn14bJCQPJsZ +c5UQnTtf9ahx9ILdMqS10D8RvtUkiSbbBO1PvrXdUfeZe7HfnrxCBbtP bMt7Zqo3z5OMsdZUbl5uVisBgpj1QnzRuITHMEUCSPoot78r9JKr86haM jVp3kytm81XnyHiNUmIJYD6KVSUU7V5wdw/LbkinLrUd13IWQJOuZTv4Q 3g5eINzqHu0PQc+CEa9kzI4/8ZTgqR/l26wFiol9RVm99Kc97hqxhDmDZ L9ILbaBlVLZvwiQlHot1TwuhJZenkomUM7Q41GhH+r0zlrEZ1JW1qvS9B w==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025505" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025505" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355189" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355189" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:19 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 08/10] iommu/vt-d: Add nested domain allocation Date: Thu, 11 May 2023 07:51:08 -0700 Message-Id: <20230511145110.27707-9-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765611179148573386?= X-GMAIL-MSGID: =?utf-8?q?1765611179148573386?= From: Lu Baolu This adds the support for IOMMU_HWPT_TYPE_VTD_S1 type. Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/linux/iommu.h | 1 + 2 files changed, 20 insertions(+) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 5f27cee4656a..51612da4a1ea 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4092,6 +4092,16 @@ static struct iommu_domain *intel_iommu_domain_alloc(unsigned type) return NULL; } +static struct iommu_domain * +intel_iommu_domain_alloc_user(struct device *dev, struct iommu_domain *parent, + const union iommu_domain_user_data *user_data) +{ + if (parent) + return intel_nested_domain_alloc(parent, user_data); + else + return iommu_domain_alloc(dev->bus); +} + static void intel_iommu_domain_free(struct iommu_domain *domain) { if (domain != &si_domain->domain && domain != &blocking_domain) @@ -4736,9 +4746,18 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) intel_pasid_tear_down_entry(iommu, dev, pasid, false); } +static int intel_iommu_domain_user_data_len(u32 hwpt_type) +{ + if (hwpt_type != IOMMU_HWPT_TYPE_VTD_S1) + return -EOPNOTSUPP; + return sizeof(struct iommu_hwpt_intel_vtd); +}; + const struct iommu_ops intel_iommu_ops = { .capable = intel_iommu_capable, .domain_alloc = intel_iommu_domain_alloc, + .domain_alloc_user = intel_iommu_domain_alloc_user, + .domain_alloc_user_data_len = intel_iommu_domain_user_data_len, .probe_device = intel_iommu_probe_device, .probe_finalize = intel_iommu_probe_finalize, .release_device = intel_iommu_release_device, diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c696d8e7f43b..32324f0756de 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -234,6 +234,7 @@ union iommu_domain_user_data { #ifdef CONFIG_IOMMUFD_TEST __u64 test[2]; #endif + struct iommu_hwpt_intel_vtd vtd; }; /** From patchwork Thu May 11 14:51:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92692 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4452907vqo; Thu, 11 May 2023 08:13:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7DbpEyUVabwjq5FK3ciQ+EyCNBxnbZ5mobR/yQ0htMEIX38bdHoLvt0+dWAH1TcbWYySIf X-Received: by 2002:a92:c26a:0:b0:326:1d7e:1238 with SMTP id h10-20020a92c26a000000b003261d7e1238mr15808472ild.16.1683818034030; Thu, 11 May 2023 08:13:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683818034; cv=none; d=google.com; s=arc-20160816; b=utE1B6Uj5bcrB1au7hRk0gjEnF6QI+j+hovTJgdRFqRj+MwJ4iNCurfW3+rVrqP/Z0 68/OUjlwKONhCm+w0HNHFYQsDC8pdZbHE2ua+tk34o27NO7gZO+tdO6g2g9h8sZ5xd4Y aLgsZfM2qYGZO+Tq4br9LthyXDYjuhFj+nYoa5MvC6ylmpZirXOXUxpFbi9Nt3K1rQJV yUdfoQoIf8ohYZ3c7n94MY5iMe69FAr42Iax3r3F9cSXImi2y0Y2haed6AjYsBFTfMYY irVecT+OVTYklocUXDK19atezWprVoOlZJwPfZmRnZIthLDvKNs6xlMBmDDRY6C7N37G cT6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=MICI3pRgOjxk1nvoX0jy3/FmoLQ0rwkzCIaWaiVTUxs=; b=eIjHKXwQUGnD2yn4xu5ni4lZECZUxzSZOmZ4fBUyVehACH4nqhFz9W2xb8r7V1enr8 tZMN0gs6fPkmIvVVAMEBZm89ARNffEdp+nTRX723zBbL6fI7n5z1EhHMLV3Nw2W5NS9y i7j2q6R2r+khzEjk7LcY+Yqr1PfPRyrdc/QZyoBOAprjHYRJXA2HCruNS0/QjjGSvElt 8tADFnOrhC2CVQmH6wVGbtYLongp2gnfQOrHOzxKWp+SWyILsIqN+hJcR0QeurWyVbOo XcIvEb2/xYcMOGJbjEXqbr5k4JFIrmks9L/eK8b5rHzZhlGHz5up2yEO2GLAG7PnAY3U I6Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QZeeo8kU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x11-20020a056638160b00b0040fb1a42b66si5657939jas.194.2023.05.11.08.13.40; Thu, 11 May 2023 08:13:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QZeeo8kU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238192AbjEKOxY (ORCPT + 99 others); Thu, 11 May 2023 10:53:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238652AbjEKOwl (ORCPT ); Thu, 11 May 2023 10:52:41 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56C0BE735; Thu, 11 May 2023 07:52:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816725; x=1715352725; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RiJbUCRtwm5Znm9STMCxtijeo+jb5c7PnzMHxXPV1jU=; b=QZeeo8kUy/k/yZDW3eWkq2hIAW0kqi3m7deAAXzfQVYVnNNIntBDR2WY Pz3NS3yYSNgcKZ7qJ9isIfX5A3GyxH6+PQKYqpnt4TdYEY6knuaMAj+Fa Y29lzwM04B5KU42aXMxT18JaQsYyt7tsb8QBbNc/hyNoXpheMaLI1aEom pQDJ4T7ePlOUJJlzWfRpYjD6ezAH/qaa8WwfDMU4W0XpmjE7EbaIt9nPQ b8g+Qv0+S4m48lptjTKUghbhE0jF1/acIGx03SuNVFdxldp5H5zHStEVM g/c/w5ughn+YpWZa0pGU3qZRpcKhlFs5QJ9ZMUuhtJYJkaZjpm0Vys3M8 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025523" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025523" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355194" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355194" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:20 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 09/10] iommu/vt-d: Implement hw_info for iommu capability query Date: Thu, 11 May 2023 07:51:09 -0700 Message-Id: <20230511145110.27707-10-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765611178883126563?= X-GMAIL-MSGID: =?utf-8?q?1765611178883126563?= Add intel_iommu_hw_info() to report cap_reg and ecap_reg information. Signed-off-by: Lu Baolu Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.c | 19 +++++++++++++++++++ include/uapi/linux/iommufd.h | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 51612da4a1ea..20d4ae1cb8a6 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4746,6 +4746,23 @@ static void intel_iommu_remove_dev_pasid(struct device *dev, ioasid_t pasid) intel_pasid_tear_down_entry(iommu, dev, pasid, false); } +static void *intel_iommu_hw_info(struct device *dev, u32 *length) +{ + struct device_domain_info *info = dev_iommu_priv_get(dev); + struct intel_iommu *iommu = info->iommu; + struct iommu_hw_info_vtd *vtd; + + vtd = kzalloc(sizeof(*vtd), GFP_KERNEL); + if (!vtd) + return ERR_PTR(-ENOMEM); + + vtd->cap_reg = iommu->cap; + vtd->ecap_reg = iommu->ecap; + *length = sizeof(*vtd); + + return vtd; +} + static int intel_iommu_domain_user_data_len(u32 hwpt_type) { if (hwpt_type != IOMMU_HWPT_TYPE_VTD_S1) @@ -4755,6 +4772,7 @@ static int intel_iommu_domain_user_data_len(u32 hwpt_type) const struct iommu_ops intel_iommu_ops = { .capable = intel_iommu_capable, + .hw_info = intel_iommu_hw_info, .domain_alloc = intel_iommu_domain_alloc, .domain_alloc_user = intel_iommu_domain_alloc_user, .domain_alloc_user_data_len = intel_iommu_domain_user_data_len, @@ -4769,6 +4787,7 @@ const struct iommu_ops intel_iommu_ops = { .def_domain_type = device_def_domain_type, .remove_dev_pasid = intel_iommu_remove_dev_pasid, .pgsize_bitmap = SZ_4K, + .hw_info_type = IOMMU_HW_INFO_TYPE_INTEL_VTD, #ifdef CONFIG_INTEL_IOMMU_SVM .page_response = intel_svm_page_response, #endif diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 2e658fa346ad..b46270a4cf46 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -464,9 +464,32 @@ struct iommu_hwpt_alloc { /** * enum iommu_hw_info_type - IOMMU Hardware Info Types + * @IOMMU_HW_INFO_TYPE_INTEL_VTD: Intel VT-d iommu info type */ enum iommu_hw_info_type { IOMMU_HW_INFO_TYPE_NONE, + IOMMU_HW_INFO_TYPE_INTEL_VTD, +}; + +/** + * struct iommu_hw_info_vtd - Intel VT-d hardware information + * + * @flags: Must be 0 + * @__reserved: Must be 0 + * + * @cap_reg: Value of Intel VT-d capability register defined in VT-d spec + * section 11.4.2 Capability Register. + * @ecap_reg: Value of Intel VT-d capability register defined in VT-d spec + * section 11.4.3 Extended Capability Register. + * + * User needs to understand the Intel VT-d specification to decode the + * register value. + */ +struct iommu_hw_info_vtd { + __u32 flags; + __u32 __reserved; + __aligned_u64 cap_reg; + __aligned_u64 ecap_reg; }; /** From patchwork Thu May 11 14:51:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 92687 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4448978vqo; Thu, 11 May 2023 08:09:22 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4m9aWbakfh3W1XkXKmYDAVjkQCXYPnSX9cBL6heQJDu+Bk7E2y6fi+DKazTl/0WMHeu/x4 X-Received: by 2002:a05:6a00:a14:b0:639:a518:3842 with SMTP id p20-20020a056a000a1400b00639a5183842mr29705422pfh.7.1683817762015; Thu, 11 May 2023 08:09:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683817761; cv=none; d=google.com; s=arc-20160816; b=mEG/tTz2Q8WkXpJwXelI9flUbNXHHcmX6tT/44RAeAQYv6qc5Pt6jutmsXS33HjKQ9 tHx4RL/XDFL22ZkmJ7N8kGVFy+XFICLUnrLse+sZPzmhJX1ciBwwH4g+ePuzeV/2+07o 240pPdLfsbhMVuKfylF+BkoYRG92N8SYCL9sMTUDPXszvMs3PtwrcyjGQRxruqu5xfCC AiXKV54xpD05BfqxXpgtWTyfgmZ4DOOQ0NDjpWh8p9BvB5bGxMLE8F0oc4qQrNKDZYVj y66V38zFKNyd78akfmYC4UWvcayW+R2BDcK1VdLzc9YJiV1pSIjstiDhC8g1bAQL/l/X R8KQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=l7sZWTfMXcDkGyhzK8TFiMLCTA6uu9gkmoF31jNUf7A=; b=ZeznaPqAoof2UqKGCyixjO8oAV2KqfgJ9leYgB/XE8Q6ERL5eBjnCLDv7ZZO70i/Kp zZh56Ps2WK0zP054oJ/Rrs4CZYVzhnleFGZNI72Z3FbsnziRYjhIj4ZbnpyczlI0OYz3 Dhu2TbwRNUcfyMVOiPEwW/p7CwLiIcJ0hl0dr1CCyEaqFVtr+e5hVhMAs51zKTxXdyv4 NlvkBWvs/CQpk4X89wwVp5QXakHFlXP7e00w7+lptz1wJTwqaeOCvaF9gwBk1XnqZcuL tLIGGJyGCw4ckIA8om5fgJTJE685nIwk8/nbuNFrKSxNqD1on7+SHuNJ3SP0499r/+2l KPTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fL7fQk+h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w71-20020a627b4a000000b00626007418c3si7749780pfc.289.2023.05.11.08.09.06; Thu, 11 May 2023 08:09:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fL7fQk+h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238198AbjEKOx2 (ORCPT + 99 others); Thu, 11 May 2023 10:53:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238195AbjEKOwl (ORCPT ); Thu, 11 May 2023 10:52:41 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6366711B63; Thu, 11 May 2023 07:52:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1683816726; x=1715352726; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zSBFf3R+KE7eDYqzxRY/XTRifADKsFtYgSSwmUYfOpg=; b=fL7fQk+h0kNFY08et8nEr3Yv6MlXhrooek+fuqhGEUw/TwftHFfRAOgK Lr211iGYErx6ESLe2ZdyCJXefytyAn2VRshalmNTM0VQ842Jl92p9fLJd erb2dk7kuH/FVZkb50X6TCCj4T5AB77XDRmtjkQvkYs49pzSGBL9PKAyb nOpgF9pVmejmqg7H1PhElbD0loL9dJfVkgiU3E8KvCTgp9LuLYEmD589i +Oru0O55Bji1+zcMXqnqQ1+zWgPNTVPETC4NUvTjOS35Ku+2+vUofmaEu nLVCLCAJY0myKmFVk5wHwkKLjVds2lymAYm9QkBLyNKskBVNO2ZUDw+Aq Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="335025529" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="335025529" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 May 2023 07:51:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10707"; a="769355198" X-IronPort-AV: E=Sophos;i="5.99,266,1677571200"; d="scan'208";a="769355198" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga004.fm.intel.com with ESMTP; 11 May 2023 07:51:21 -0700 From: Yi Liu To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, zhenzhong.duan@intel.com Subject: [PATCH v3 10/10] iommu/vt-d: Disallow nesting on domains with read-only mappings Date: Thu, 11 May 2023 07:51:10 -0700 Message-Id: <20230511145110.27707-11-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230511145110.27707-1-yi.l.liu@intel.com> References: <20230511145110.27707-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765610893856475405?= X-GMAIL-MSGID: =?utf-8?q?1765610893856475405?= From: Lu Baolu When remapping hardware is configured by system software in scalable mode as Nested (PGTT=011b) and with PWSNP field Set in the PASID-table-entry, it may Set Accessed bit and Dirty bit (and Extended Access bit if enabled) in first-stage page-table entries even when second-stage mappings indicate that corresponding first-stage page-table is Read-Only. As the result, contents of pages designated by VMM as Read-Only can be modified by IOMMU via PML5E (PML4E for 4-level tables) access as part of address translation process due to DMAs issued by Guest. Disallow the nested translation when there are read-only pages in the corresponding second-stage mappings. And, no read-only pages are allowed to be configured in the second-stage table of a nested translation. For the latter, an alternative is to disallow read-only mappings in any stage-2 domain as long as it's ever been used as a parent. In this way, we can simply replace the user counter with a flag. In concept if the user understands this errata and does expect to enable nested translation it should never install any RO mapping in stage-2 in the entire VM life cycle." Reference from Sapphire Rapids Specification Update [1], errata details, SPR17. [1] https://www.intel.com/content/www/us/en/content-details/772415/content-details.html Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.c | 13 +++++++++++++ drivers/iommu/intel/iommu.h | 4 ++++ drivers/iommu/intel/nested.c | 22 ++++++++++++++++++++-- include/uapi/linux/iommufd.h | 12 +++++++++++- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 20d4ae1cb8a6..42288bd449a0 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -2150,6 +2150,7 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, struct dma_pte *first_pte = NULL, *pte = NULL; unsigned int largepage_lvl = 0; unsigned long lvl_pages = 0; + unsigned long flags; phys_addr_t pteval; u64 attr; @@ -2159,6 +2160,17 @@ __domain_mapping(struct dmar_domain *domain, unsigned long iov_pfn, if ((prot & (DMA_PTE_READ|DMA_PTE_WRITE)) == 0) return -EINVAL; + if (!(prot & DMA_PTE_WRITE) && !domain->read_only_mapped) { + spin_lock_irqsave(&domain->lock, flags); + if (domain->nested_users > 0) { + spin_unlock_irqrestore(&domain->lock, flags); + return -EINVAL; + } + + domain->read_only_mapped = true; + spin_unlock_irqrestore(&domain->lock, flags); + } + attr = prot & (DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP); attr |= DMA_FL_PTE_PRESENT; if (domain->use_first_level) { @@ -4756,6 +4768,7 @@ static void *intel_iommu_hw_info(struct device *dev, u32 *length) if (!vtd) return ERR_PTR(-ENOMEM); + vtd->flags = IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17; vtd->cap_reg = iommu->cap; vtd->ecap_reg = iommu->ecap; *length = sizeof(*vtd); diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 581596d90c1b..95644c6815af 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -616,6 +616,10 @@ struct dmar_domain { int agaw; /* maximum mapped address */ u64 max_addr; + /* domain has mappings with read-only permission */ + bool read_only_mapped; + /* user nested domain count */ + int nested_users; }; /* Nested user domain */ diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c index d13fbcd3f5a6..9092ce28382c 100644 --- a/drivers/iommu/intel/nested.c +++ b/drivers/iommu/intel/nested.c @@ -61,7 +61,14 @@ static int intel_nested_attach_dev(struct iommu_domain *domain, static void intel_nested_domain_free(struct iommu_domain *domain) { - kfree(to_dmar_domain(domain)); + struct dmar_domain *dmar_domain = to_dmar_domain(domain); + struct dmar_domain *s2_domain = dmar_domain->s2_domain; + unsigned long flags; + + spin_lock_irqsave(&s2_domain->lock, flags); + s2_domain->nested_users--; + spin_unlock_irqrestore(&s2_domain->lock, flags); + kfree(dmar_domain); } static void intel_nested_invalidate(struct device *dev, @@ -143,14 +150,25 @@ struct iommu_domain *intel_nested_domain_alloc(struct iommu_domain *s2_domain, const union iommu_domain_user_data *user_data) { const struct iommu_hwpt_intel_vtd *vtd = (struct iommu_hwpt_intel_vtd *)user_data; + struct dmar_domain *s2_dmar_domain = to_dmar_domain(s2_domain); struct dmar_domain *domain; + unsigned long flags; domain = kzalloc(sizeof(*domain), GFP_KERNEL_ACCOUNT); if (!domain) return NULL; + spin_lock_irqsave(&s2_dmar_domain->lock, flags); + if (s2_dmar_domain->read_only_mapped) { + spin_unlock_irqrestore(&s2_dmar_domain->lock, flags); + kfree(domain); + return NULL; + } + s2_dmar_domain->nested_users++; + spin_unlock_irqrestore(&s2_dmar_domain->lock, flags); + domain->use_first_level = true; - domain->s2_domain = to_dmar_domain(s2_domain); + domain->s2_domain = s2_dmar_domain; domain->s1_pgtbl = vtd->pgtbl_addr; domain->s1_cfg = *vtd; domain->domain.ops = &intel_nested_domain_ops; diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index b46270a4cf46..8626f36e0353 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -471,10 +471,20 @@ enum iommu_hw_info_type { IOMMU_HW_INFO_TYPE_INTEL_VTD, }; +/** + * enum iommu_hw_info_vtd_flags - Flags for VT-d hw_info + * @IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17: If set, disallow nesting on domains + * with read-only mapping. + * https://www.intel.com/content/www/us/en/content-details/772415/content-details.html + */ +enum iommu_hw_info_vtd_flags { + IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 = 1 << 0, +}; + /** * struct iommu_hw_info_vtd - Intel VT-d hardware information * - * @flags: Must be 0 + * @flags: Combination of enum iommu_hw_info_vtd_flags * @__reserved: Must be 0 * * @cap_reg: Value of Intel VT-d capability register defined in VT-d spec