From patchwork Tue Oct 24 15:14: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: 157602 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2026828vqx; Tue, 24 Oct 2023 08:34:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3xetlhW3+/uY6ncS1XxR7z9VCBLh1yNZkbIp5H2TI7xWmJJXr+dqVNS6qYtpESObqigHi X-Received: by 2002:a17:90a:e38a:b0:27d:36df:8472 with SMTP id b10-20020a17090ae38a00b0027d36df8472mr10022869pjz.27.1698161696564; Tue, 24 Oct 2023 08:34:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698161696; cv=none; d=google.com; s=arc-20160816; b=R7cf7pPDCA23OtARJsoSi77hr1F/Nt3VbD0s95f817ikbV+mWaft1GDwruY3t/fiKF U4iBakPSEAAekWPzL6AA5cPvXXAv18AEfmGeu/8M25knevXqVcITSWNNi9Sk/ocLA8l1 WDBOnNbHwXQgHBPRHQF1tz8oLUelfQdFveF7YqmfMgcuDE2mIUWhLJx8g7qJNkcRzZyh A5Xl1Cyzd2k+HBhO0EnwA2SjW/z2dBYTsTL8BhzXveKcolwR+nvP7iLWCT37mZ0UDcEv MDjWX0xLo9k01qrpVs7sZKnlHuI1us6O2JY5KoigVqDpYKxg/GmqTMe3wZMBITWCykXw ZTvw== 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=kzwdjKnb2KJDK/CGVaLIcDB2TYOpDRhSiZRVJTPf4mg=; fh=ncJBVmsnOSqrX1O37yfYEzicwaA2e7ARxnsU7aiysyE=; b=Jkf8iufplrAMz6hPpa/L7pP1wWzAfZqwxqZAuTx9YR6Dpu1bHz0/Wvr0rNm1RUVJTM t2VbQsovz9opTm/1xlU3HGqBjKYb1ILUHO9Hz9bTMF4yx34g+/n08lGQoRqDyvTFLQCu 5k9iiY8jw/4oLutfeiIjCdK3ludqmPvfQ4rHYbbYQpb6djwxGwj7gh6HdrBGza4XxtA9 JKX7ZyO2I6aV3yPHt772Qd6zsgktJZIfoBcNU7wQGL8qEmWeCFN01qD6aaCxiRSK4OYv Y3/KAvCV5o2lkKeBhaFBfYnhzypGnBoGkGUSqRZoGdlhlMSrYFjzMuQC7HRftuchIdqn KCpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="U8/XyhXS"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id h16-20020a17090ac39000b0026f9b5fe711si10877931pjt.108.2023.10.24.08.34.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:34:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="U8/XyhXS"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id CFC89802C698; Tue, 24 Oct 2023 08:34:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343802AbjJXPeu (ORCPT + 27 others); Tue, 24 Oct 2023 11:34:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234931AbjJXPep (ORCPT ); Tue, 24 Oct 2023 11:34:45 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57E831FCB; Tue, 24 Oct 2023 08:25:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698161146; x=1729697146; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HxxDxm+pmr+CG9d+qPDASdBIZigfxNwo47Fkj5u+VjE=; b=U8/XyhXSMDHlz9avuLIpfC/0Tfq7DO9/PustChdZxkudDit69FbYnzcx 8Jb72EI9L5+pFW5C9qKiDpQPX3SUY1+E5haTp6MtCfFHBfq+MAKrhdKLH PZdP6gML/U6UjLCEKxWTbXTbEd2LGh6UhJuRl2R6uZ2rr3vMRBmtGdK9S fyf6o9areE3d/tViQZwwURb72Y8Y2ZrN5x8wwBUjis+r4ch7JVYLH25za t5+NoFzJuYNmcWwa1jW5D/r2SqhFBen78QBdA894eqZsEDFHOKcKb5oof SfPBUvsbBLRAVnK27r/KZ1WmdDirQ3XWTXANjbW2VZ5CV6szIAphh5cBm A==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="418212082" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="418212082" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 08:14:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="1005688893" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="1005688893" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 24 Oct 2023 08:14: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, joao.m.martins@oracle.com Subject: [PATCH v7 1/8] iommufd: Add data structure for Intel VT-d stage-1 domain allocation Date: Tue, 24 Oct 2023 08:14:05 -0700 Message-Id: <20231024151412.50046-2-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024151412.50046-1-yi.l.liu@intel.com> References: <20231024151412.50046-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, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:34:55 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780651599219454654 X-GMAIL-MSGID: 1780651599219454654 This adds IOMMU_HWPT_DATA_VTD_S1 for stage-1 hw_pagetable of Intel VT-d and the corressponding data structure for userspace specified parameter for the domain allocation. Reviewed-by: Kevin Tian Signed-off-by: Yi Liu --- include/uapi/linux/iommufd.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index aa9282ebbc7e..3a66d3888393 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -362,12 +362,42 @@ enum iommufd_hwpt_alloc_flags { IOMMU_HWPT_ALLOC_DIRTY_TRACKING = 1 << 1, }; +/** + * enum iommu_hwpt_vtd_s1_flags - Intel VT-d stage-1 page table + * entry attributes + * @IOMMU_VTD_S1_SRE: Supervisor request + * @IOMMU_VTD_S1_EAFE: Extended access enable + * @IOMMU_VTD_S1_WPE: Write protect enable + */ +enum iommu_hwpt_vtd_s1_flags { + IOMMU_VTD_S1_SRE = 1 << 0, + IOMMU_VTD_S1_EAFE = 1 << 1, + IOMMU_VTD_S1_WPE = 1 << 2, +}; + +/** + * struct iommu_hwpt_vtd_s1 - Intel VT-d stage-1 page table + * info (IOMMU_HWPT_DATA_VTD_S1) + * @flags: Combination of enum iommu_hwpt_vtd_s1_flags + * @pgtbl_addr: The base address of the stage-1 page table. + * @addr_width: The address width of the stage-1 page table + * @__reserved: Must be 0 + */ +struct iommu_hwpt_vtd_s1 { + __aligned_u64 flags; + __aligned_u64 pgtbl_addr; + __u32 addr_width; + __u32 __reserved; +}; + /** * enum iommu_hwpt_data_type - IOMMU HWPT Data Type * @IOMMU_HWPT_DATA_NONE: no data + * @IOMMU_HWPT_DATA_VTD_S1: Intel VT-d stage-1 page table */ enum iommu_hwpt_data_type { IOMMU_HWPT_DATA_NONE, + IOMMU_HWPT_DATA_VTD_S1, }; /** From patchwork Tue Oct 24 15:14: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: 157603 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2026943vqx; Tue, 24 Oct 2023 08:35:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGNdxDJUgkle2wz5eeLefKwbSIvCET8bOIosEt/6lYuz+YanMOOa9dh1dqhFw53pyjzoj6K X-Received: by 2002:a17:90a:e00a:b0:276:e9ba:60c7 with SMTP id u10-20020a17090ae00a00b00276e9ba60c7mr9534910pjy.13.1698161705108; Tue, 24 Oct 2023 08:35:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698161705; cv=none; d=google.com; s=arc-20160816; b=tijQrLiGNuXhECPuscgPSkwjhKSmvitDekgGqM3s0NW63Z8rGBMRxF86zNUhyR90G+ dtXeDux2z8okA5B3O5yd25e9BHdxVpOkJAsD3vy+Qly/N35EIJ5aTEKw7w5Ax0t92I/D 8iLhQ04THjmAcWu3SwxUUUWcQ3SuOnAfRnOSVfDQn119NQ2Xy8VdhihgSkuNjkafywsI eedMx3G53oHo9vLOY6NV+su3dEgtY+HGF5FqX3iyN8KKazdBG3AJW+fQbEbfJ45Ukprs McyiWx+4DrGOtoEpdF5Fwyu2zIDWiPx3uKm71ZkJMipVi7hJgOBwnKH69X+Fi04sjT7j W9wA== 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=Kt/ncdOuugML9g067HcMfCRx6l4G81FqgVQv3Up3Dr0=; fh=ncJBVmsnOSqrX1O37yfYEzicwaA2e7ARxnsU7aiysyE=; b=IDnPD14wQiNMeapb52BNjcSIC7Rs37XAa6zTtCMo3h6mqPEjXBiJnkmH76ZiLg1A5G gCMzeNs7UWtHA5nNx1ZIf/aP+9js85wGhC9NdluXSstlG+asOW3TBV36PJjSDi6SZxvd UAFc9FyveTUTz3bjCfgp3qwMECJ6Tw9TX4wjGyyQl9cZEYwKVvm090nmvMpJeJ7Rv0T4 AoGzxYmeN9Wz9AQBrLq98M/TnsVa6wmuJDknpG7s294AfgAvIuify1m4Gx510LgglQGV L1/JSHNKjqqvesj4FRLqLnw8Ah1fmk5RcRzCVzAx55D+RNSrI/3bgws7ppFT0CchQCWQ In9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ADUvOSiu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id mh9-20020a17090b4ac900b0027763b64ea1si11850848pjb.44.2023.10.24.08.35.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:35:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ADUvOSiu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 0C197807FD4B; Tue, 24 Oct 2023 08:34:56 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234961AbjJXPer (ORCPT + 27 others); Tue, 24 Oct 2023 11:34:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234879AbjJXPeo (ORCPT ); Tue, 24 Oct 2023 11:34:44 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C6981FCF; Tue, 24 Oct 2023 08:25:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698161147; x=1729697147; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GhzoVAbGJrM+7KU7vz4DC0AvB+1skOqf7bv981Kl1Qk=; b=ADUvOSiufnTGz8XKyQUQhgd49PhWUn7LczUPjsI7D5V9vgY4d0lbPKW4 gJzt1FOZFls3BiWTF5BS+SHvZ2LURBOLfBSvTViVq5fODQ6buz+5XOqAF mTl3zfCjOPN2FihroxOf5UjUVCfUftv5VMCtXlu8PgQs11Du9EaQM3qr+ lmu5CHCEg+gU4O1YezPcFgi1guDbebgNt1X+WM1LE4jLWqpN0QzVUeU5Z 41N6X3qjuNfsdTN1A++HdRlziEIcDe5aoAI/uKs4ISVSKeQ8i5nEgzqC7 8NTWSbZdgfS3vejUkITU7xoke/okDHf4FMHpDHIUUVsOVyhoH/P9acS62 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="418212098" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="418212098" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 08:14:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="1005688900" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="1005688900" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 24 Oct 2023 08:14:14 -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, joao.m.martins@oracle.com Subject: [PATCH v7 2/8] iommu/vt-d: Extend dmar_domain to support nested domain Date: Tue, 24 Oct 2023 08:14:06 -0700 Message-Id: <20231024151412.50046-3-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024151412.50046-1-yi.l.liu@intel.com> References: <20231024151412.50046-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:34:56 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780651608304905789 X-GMAIL-MSGID: 1780651608304905789 From: Lu Baolu The nested domain fields are exclusive to those that used for a DMA remapping domain. Use union to avoid memory waste. Reviewed-by: Kevin Tian Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.h | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index cc7301579773..244f111ea0bb 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -25,6 +25,7 @@ #include #include +#include /* * VT-d hardware uses 4KiB page size regardless of host page size. @@ -605,15 +606,38 @@ struct dmar_domain { struct list_head devices; /* all devices' list */ struct list_head dev_pasids; /* all attached pasids */ - 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 { + /* parent page table which the user domain is nested on */ + struct dmar_domain *s2_domain; + /* user page table pointer (in GPA) */ + unsigned long s1_pgtbl; + /* page table attributes */ + struct iommu_hwpt_vtd_s1 s1_cfg; + }; + }; struct iommu_domain domain; /* generic domain data structure for iommu core */ From patchwork Tue Oct 24 15:14: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: 157604 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2026961vqx; Tue, 24 Oct 2023 08:35:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH8z4j83u1WY8Jibinz7GrqefdtGXGMvsDallH248srCJiA8y0/R4wah9iwotsZc+YdkN+l X-Received: by 2002:a17:902:f342:b0:1ca:7294:a55a with SMTP id q2-20020a170902f34200b001ca7294a55amr7354175ple.64.1698161706865; Tue, 24 Oct 2023 08:35:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698161706; cv=none; d=google.com; s=arc-20160816; b=py/BrZqCQamrKc92cWx9ila7H6hMAyNJCEbJxKihDCZpB8k6tpioUDHJEbFnmlvmyQ M/2LggVIPPuViISQa9Sav4+PXQww7CVFSvMAD0sCa10/wQWe5fhudoHoolbOXbS45R2P mT5S0Be93NO1hAWX3MK8ER84ihd62fB77MuH96oeB8meiKVbSp5GSdP7Aun7LHGLZH/q fTcDswxCAAs2feGsVNwbntPnAh1Z+oDjW6+fxrkSLkNliHx3o+lYXBCIA7hZI2WdJPcD BVBpKfsYANOrWw/ksOLf9QRpbvz9CF136N+zIFDlUMgMM818glq6w9csnafZSd9xHqTf gjTg== 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=hmmTAwwpMSrLpMmAAXBw+sYO98sXMof4nwAKnUjtOgk=; fh=4LHR+mUxVAoNBe0WLhXgnzbMgf2Oz4cgAvP5fO84YrI=; b=NuObgmy0WcuTd/kcoUHVLnV3L5MWfs1JCOc2W0u33+udyEQG2v+zc78IJNG7guakwI OzSAzLXBYTOpiEADuGFckPv5Icu57Zr7KAj9H+51WM1dVosMvwsCAUTJFoSxKt6+UezJ rZWa3XytNVwr20eqgJ5CO2ZzUuNI/wsXe4NgPuP91xVtg7YsWaMwHKs+5+JJf2l3a3Kn ENsUwPXBiQKHchz/DSJ3lROOVvKPbEnQpLcMwUaUd4iNZ9QVl0jDd72MoWRLv+bMqnS8 i2m9qTHjuxcRrE1FDnWt3xbPrfWV6kFuYI15hQ+zW6oCln+NcTRZIjmCWGlXCXD0OfoW WlNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LG3E6WeK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id z22-20020a1709028f9600b001c62cfff799si8442575plo.176.2023.10.24.08.35.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:35:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LG3E6WeK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id B837C802B071; Tue, 24 Oct 2023 08:35:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343803AbjJXPe4 (ORCPT + 27 others); Tue, 24 Oct 2023 11:34:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343730AbjJXPer (ORCPT ); Tue, 24 Oct 2023 11:34:47 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E74C01FD5; Tue, 24 Oct 2023 08:25:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698161148; x=1729697148; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yc31Hj1XFcKLSYQzFgKTkbbG88zsmwpxwtOXR4n5QGQ=; b=LG3E6WeKKdR/ODTzkvmVYWD8z0cmG9QwjyAiLbR1hgecsQ5oJNckBt1g JcvleUG34DJvszhXIjnNlJ2/VrlsXB1gR8KV9oaT8vlZER1/radjJq5KG yNV77uyV/ZPpPZtlMLWaQcLT6VobCKBZur/h5bApBpdgV7QgM0AEvu0Zf TTvw/2z7f3wj1Xv6yxQbfyrglK5LmEMgqwRkqgR0+Nd9sEbg/nTpCZT6T jLlqiL8+VXCn9ggyypzj/5lmP2YwBLOnJV7eZlbPyJn5RgY48mrxHvAyu n5ox25ZxmhzW+E+4spoZTTf5Ghwkm9Baf8xcAvpvfUg319eX8Xu/1GzgN Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="418212129" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="418212129" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 08:14:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="1005688908" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="1005688908" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 24 Oct 2023 08:14: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, joao.m.martins@oracle.com, Jacob Pan Subject: [PATCH v7 3/8] iommu/vt-d: Add helper for nested domain allocation Date: Tue, 24 Oct 2023 08:14:07 -0700 Message-Id: <20231024151412.50046-4-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024151412.50046-1-yi.l.liu@intel.com> References: <20231024151412.50046-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, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:35:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780651609872038349 X-GMAIL-MSGID: 1780651609872038349 From: Lu Baolu This adds helper for accepting user parameters and allocate a nested domain. Reviewed-by: Kevin Tian Signed-off-by: Jacob Pan Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/Makefile | 2 +- drivers/iommu/intel/iommu.h | 2 ++ drivers/iommu/intel/nested.c | 55 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 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 244f111ea0bb..43108424f315 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -884,6 +884,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 struct iommu_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..859c60bead05 --- /dev/null +++ b/drivers/iommu/intel/nested.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * nested.c - nested mode translation support + * + * Copyright (C) 2023 Intel Corporation + * + * Author: Lu Baolu + * Jacob Pan + * Yi Liu + */ + +#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 struct iommu_user_data *user_data) +{ + struct iommu_hwpt_vtd_s1 vtd; + struct dmar_domain *domain; + int ret; + + ret = iommu_copy_struct_from_user(&vtd, user_data, + IOMMU_HWPT_DATA_VTD_S1, __reserved); + if (ret) + return ERR_PTR(ret); + + domain = kzalloc(sizeof(*domain), GFP_KERNEL_ACCOUNT); + if (!domain) + return ERR_PTR(-ENOMEM); + + 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); + INIT_LIST_HEAD(&domain->dev_pasids); + spin_lock_init(&domain->lock); + xa_init(&domain->iommu_array); + + return &domain->domain; +} From patchwork Tue Oct 24 15:14: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: 157593 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2021282vqx; Tue, 24 Oct 2023 08:27:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEk5NI/2u5FrTKlYVwl6Lr6GwAcpiQQ8HBy8EIeBqIsPAD+hMFw+csYeLRfXbGjVIuHecEa X-Received: by 2002:a17:903:186:b0:1b6:bced:1dc2 with SMTP id z6-20020a170903018600b001b6bced1dc2mr10739387plg.0.1698161257263; Tue, 24 Oct 2023 08:27:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698161257; cv=none; d=google.com; s=arc-20160816; b=wOZ+jcAYJ+T+SjmjDfPEl/zP6BDCgl8I2cZrtz9LSiI2QdBMMp+xzDa24WncV8SL62 JKlieCycRbifpQvYMWzKdRsMUqgTd38XWW1fOw9OWCI1Sx2rwZ8/YM0WiWs0KQEUAItA cR0NIqgXkQvfTTwiFJsy2/yIDQ0Bc4CxdoMa2MIWXvb2E4HELqxwxQjGYzX1fVjL6jcL qKhhJibN6rHsGQGcIyhIfM9I5/ZNysYTcWcTbQoruRKOQWwnMJttem0Sch16yZhXq+0m 11RBigvUo3eDdMAAQcKLOxL9jEt6e0sm1oQuWMR95+8FRCsooVlM+1URdTkpuu9Lp653 7h9w== 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=8FS6vv+Mtb39wAcwdw5yjGg9G/d/s1F8e0XWIVkD8c4=; fh=4LHR+mUxVAoNBe0WLhXgnzbMgf2Oz4cgAvP5fO84YrI=; b=M3x2IKEEbCf+X8mD0maZGIauOZpmu2gecCagElxmny5b8JrNMAg3/IQYmurs1S762U ZJhOPp5RTvtNTeTEXwNJQWf48rUe4a1bDHK+MWxJ7+2sSBrZv7TfhbTyc/kH1HReJx7N 1dvxISGwnHxaPips/2wcDJhgYqwCvlqNxYzSxzyWdygA7En0QyAIzgnPK/8JX2+tqYA0 Hoj/2W1AHauH9zuxdzOvzRFX1/qlteWr5WDo09LbwnXRsqSKZv7R8tNEOpMT0tNk5ri7 sRXSQKjl+6LeDq+6TqBliZ7gtIpt1JQtPnfFwfLcNkqQOYdlJxbu11v6VbiOnuCr3iG3 8YQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="N8g/xM/i"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id j8-20020a170903024800b001c752da513asi8406703plh.405.2023.10.24.08.27.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:27:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="N8g/xM/i"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 60F1780C651F; Tue, 24 Oct 2023 08:27:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234782AbjJXP1R (ORCPT + 27 others); Tue, 24 Oct 2023 11:27:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43212 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234779AbjJXP1C (ORCPT ); Tue, 24 Oct 2023 11:27:02 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19085210C; Tue, 24 Oct 2023 08:25:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698161150; x=1729697150; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dAd8yqkYmiu7MLFmlRMuk0Qk6KIOoPZsI4zu663OHR0=; b=N8g/xM/iKWuw3/uOf0dw1x2BmjhX4P9d38WtjfLSPqdltbOe/1+dezRV sLrhHNXFk1x2q7Evqopt4RO+v4wWvRcLMV+A3Nzar1AFDkkVEYfa8Yygh +vJ9WPPw3SMg/fPBAP+QW3FC3zd5F7aNCqq0r896O7vTtrEfE1quRqdio 4OIk4R3CH/ZyeaCsfPdK0G6zi+swOMhhfU+Dii04fMGyssbQAwqBDgyWF 1TeCX6/WadDLmar8BZ8bFD4d34Him8EADqEOwYbUUvUgPJCNuffyuHFEy hiEcCLk3186/8BxRTagUkSDlAvi4uX8qAKVtjVQKuX3O0xY+RDmx/054L w==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="418212151" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="418212151" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 08:14:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="1005688917" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="1005688917" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 24 Oct 2023 08:14: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, joao.m.martins@oracle.com, Jacob Pan Subject: [PATCH v7 4/8] iommu/vt-d: Add helper to setup pasid nested translation Date: Tue, 24 Oct 2023 08:14:08 -0700 Message-Id: <20231024151412.50046-5-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024151412.50046-1-yi.l.liu@intel.com> References: <20231024151412.50046-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:27:32 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780651138234317041 X-GMAIL-MSGID: 1780651138234317041 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 | 112 ++++++++++++++++++++++++++++++++++++ drivers/iommu/intel/pasid.h | 2 + 2 files changed, 114 insertions(+) diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c index b9264b9174e8..74e8e4c17e81 100644 --- a/drivers/iommu/intel/pasid.c +++ b/drivers/iommu/intel/pasid.c @@ -370,6 +370,15 @@ static inline bool pasid_get_ssade(struct pasid_entry *pe) return pasid_get_bits(&pe->val[0]) & (1 << 9); } +/* + * 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. @@ -437,6 +446,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) @@ -822,3 +840,97 @@ 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. + * @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 stage-1 domain nested on a stage-2 domain + * + * This is used for nested translation. The input domain should be + * nested type and nested on a parent with 'is_nested_parent' flag + * set. + */ +int intel_pasid_setup_nested(struct intel_iommu *iommu, struct device *dev, + u32 pasid, struct dmar_domain *domain) +{ + struct iommu_hwpt_vtd_s1 *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; + + /* Address width should match the address width supported by hardware */ + switch (s1_cfg->addr_width) { + case ADDR_WIDTH_4LEVEL: + break; + case ADDR_WIDTH_5LEVEL: + if (!cap_fl5lp_support(iommu->cap)) { + dev_err_ratelimited(dev, + "5-level paging not supported\n"); + return -EINVAL; + } + break; + default: + dev_err_ratelimited(dev, "Invalid stage-1 address width %d\n", + s1_cfg->addr_width); + return -EINVAL; + } + + if ((s1_cfg->flags & IOMMU_VTD_S1_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_S1_EAFE) && !ecap_eafs(iommu->ecap)) { + pr_err_ratelimited("No extended access flag support on %s\n", + iommu->name); + 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_S1_SRE) { + pasid_set_sre(pte); + if (s1_cfg->flags & IOMMU_VTD_S1_WPE) + pasid_set_wpe(pte); + } + + if (s1_cfg->flags & IOMMU_VTD_S1_EAFE) + pasid_set_eafe(pte); + + if (s2_domain->force_snooping) + pasid_set_pgsnp(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, s2_domain->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 958050b093aa..dd37611175cc 100644 --- a/drivers/iommu/intel/pasid.h +++ b/drivers/iommu/intel/pasid.h @@ -113,6 +113,8 @@ int intel_pasid_setup_dirty_tracking(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 Tue Oct 24 15:14: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: 157595 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2021704vqx; Tue, 24 Oct 2023 08:28:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHv3u7CRig9MvOwN3PEuUeZOKmd4o5etkVlCiq1rWByLvs4VUZDcCA4zvYruV2aFUu3DSuZ X-Received: by 2002:a05:6a21:7906:b0:175:59b5:5a61 with SMTP id bg6-20020a056a21790600b0017559b55a61mr2720906pzc.60.1698161301645; Tue, 24 Oct 2023 08:28:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698161301; cv=none; d=google.com; s=arc-20160816; b=NJPEQcu0ku5VQTcMTsALpUMghNqPP1t+5+lVybyoh9Umrxi0NpDadW3FysgCWpojPf CuE7nuqrzrDz6zZl2F0GcOldCQcEEsEX4MbaoHFnEN0mt9dZtNtX2JSgbM1bxbsyYc73 mlOwq7xIt4GGYatqsD8JFBqIIL1dmE2N2Z3ww38Eu0kL+gteh2xJBh0G82GAN+L3QEZW HhORyHTxlTEkUhckFcU5KyhxrWzeTelBFBqwaw+7jx3HmuvpF0XJpaHf8o22v8Es4LdB giWNr7d4H1fbUeXbM7db9LQRbfkSZ6cJ4s4+Mah/HCXFAxvYmBSX77T5jMENSGKxtl0t aO5g== 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=l4Gq2Fe2/woFoRpKme2PEbsqz/YYYShE7F2bnoXMt5Y=; fh=ncJBVmsnOSqrX1O37yfYEzicwaA2e7ARxnsU7aiysyE=; b=u4GGQW7v6FBVu2BGnBdZqYmwcfC3xdaUGeYWup7CLg3mdjrJMsxltXj/N12Bx8KElZ OL4qA30twYo+pyYCzXFm8p3cZisjxk0pX9Am/s7Fj346wPYgtzS43hIanEgMfYUe8gfR K+a2pSBkYZHRRB4OY+p4CWxHhGLtH7NEYX9rpQ0yHh5QSxO+u+puVO3bcqW4viizAYml xMWjGQ6uhspyMaxFUrj7Rj5+TJ2FYvNMOZSMZWwxSdRoTHbar4hGRG9RSK0tqPJOZHAO O9BGQp2YRwr6sMF1MoDROdgnD3kyBoVXqXxlFz/Bpym+sHC4Wezdt+VKt6P5319GGEsX bHHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TfNxncsi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id cw10-20020a056a00450a00b00690f22edc20si8478208pfb.333.2023.10.24.08.28.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:28:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TfNxncsi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id B604C80C5CB6; Tue, 24 Oct 2023 08:28:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234846AbjJXP1c (ORCPT + 27 others); Tue, 24 Oct 2023 11:27:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234893AbjJXP1D (ORCPT ); Tue, 24 Oct 2023 11:27:03 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E34F2699; Tue, 24 Oct 2023 08:25:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698161151; x=1729697151; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yaXPXgiHPft1t+FfYx+gcc9MnjEET03QhJxsh9MS9DU=; b=TfNxncsiPQGawNwwvRp6vU0YMdU2d0et4x7RhQnWuY/6YnI00BF0JgS+ TMS36MLXIwYqKD3BZmMnGMFatZzjSTM7x1xHV8qV0VoTNVHZKWFTJMvf8 chun5fBr79PN1KvbL3o8CkCZZ5gj3O5qlH3gfDQrAsgKWJJE7KlyRwTeV w9VELrZcJ1s3c5mFWMkRAmdgyDehFaJ6L8LRjttaQAZguc8fNYG41UoE4 oRVZ28iogysKFUjVXgJ5dTJy15ZiRbgGa2V1mX4O9xexALstUWtCbKHog 0GKNB1g3WEIDdxX8X0jjYO99TiP/2VYdXAk6kIV33T6+rlWXaHHBkWPbZ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="418212168" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="418212168" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 08:14:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="1005688921" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="1005688921" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 24 Oct 2023 08:14: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, joao.m.martins@oracle.com Subject: [PATCH v7 5/8] iommu/vt-d: Make domain attach helpers to be extern Date: Tue, 24 Oct 2023 08:14:09 -0700 Message-Id: <20231024151412.50046-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024151412.50046-1-yi.l.liu@intel.com> References: <20231024151412.50046-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:28:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780651185055390320 X-GMAIL-MSGID: 1780651185055390320 This makes the helpers visible to nested.c. Suggested-by: Lu Baolu Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.c | 15 ++++++--------- drivers/iommu/intel/iommu.h | 7 +++++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index cb64759b3d95..292baa64188b 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -282,7 +282,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); @@ -561,7 +560,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); @@ -1779,8 +1778,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; @@ -1829,8 +1827,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; @@ -3975,7 +3972,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; @@ -4128,8 +4125,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; diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index 43108424f315..d5aaaedf2094 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -878,6 +878,13 @@ 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); +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 Tue Oct 24 15:14: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: 157596 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2021754vqx; Tue, 24 Oct 2023 08:28:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF5d7sSeNQK2zBos14LaCfN1qHJ7dspc7OYAytpE/A5dkVPm3BLE7CJTMtEbAorzrSTaR2N X-Received: by 2002:a17:903:11c9:b0:1c8:a68e:7f1 with SMTP id q9-20020a17090311c900b001c8a68e07f1mr11574373plh.66.1698161306932; Tue, 24 Oct 2023 08:28:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698161306; cv=none; d=google.com; s=arc-20160816; b=GTInryJzwvWAtyuCHxyrFy1fPhpTwKuNlUA1TfhyvUgItMBP86GHElO3pHfuE9UMZr bwLXw3Q5NoF7/Dl5Vy4HMMwMGkYep81YLJNYemRHAgindWUgg0+y3zVw29pnpoI76+HY cLbP73JbtRxa9y92kQH+dNoW8OPHaygRfo1LNqsSWmpFWSUA0/h6lJ+v8XdrpCqD/vUL XK5yro1iiBZXPqugllnHmxelq3n+g0WszPm5zhzYwRrQhapuU63MH39km/lwcWo3x0Tq T4i8alLB6ZpNLZbJYiRoIJY2gUVhoo0AR3+45DqOj9fj7OIiVo7fst4pRgH/vl8OFDrJ lMBA== 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=R9nQGI+cCEoo6mqrg5fxsPokzzHg6CjMMav5X0hnjM0=; fh=4LHR+mUxVAoNBe0WLhXgnzbMgf2Oz4cgAvP5fO84YrI=; b=MjgcbyIj34WKJXiE3iaJbDlJO4p2SXU9HyzzfovvOCW31krWPnOtX8/k8780KVTRwn lvqDSwTgTqT3u0QcXNok6T5oc4yVz7Ih7MHnQXAovlVhZ/DOPelO+dgil0qY8Ji/WgRt LGFFWPyB3fThCiA1kcdsNpZXvcK95YKp0ORozHLjWY1BBWLw9yDdHMxVboepNaYRibWP f2hdR5pqiB9Gy0OcFGgSfJSsz6wq90gjywq9BZXwKjyiaVeZTKdXv7cxAKmnoSCN86L0 VVmXdkvkLmfW9XzSAodLrbstRP2ES4dpbhtAhak0E3HFhFAozFX2t3JwCX+YaK7bv0U9 +XpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DginE+ml; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id y12-20020a170902ed4c00b001c9d690baa4si8401593plb.532.2023.10.24.08.28.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:28:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DginE+ml; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id E4EA780A79A1; Tue, 24 Oct 2023 08:28:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343816AbjJXP11 (ORCPT + 27 others); Tue, 24 Oct 2023 11:27:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234986AbjJXP1E (ORCPT ); Tue, 24 Oct 2023 11:27:04 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B43226A4; Tue, 24 Oct 2023 08:25:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698161152; x=1729697152; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1Qo/FTvzNZXKNcxE6JDGjTJkXacRYoQ7dUGXNQJl+9Q=; b=DginE+mlI2OivvuUve7WK+eyFi21yK/OmGUGR68xD9gS6q7iW1tyxmoc w6DDnNBxgXim5st25gpBw3YIbBLz9sciLizZvSFmXX2YF9dPNjPfgiwQL KnTJwBQACg+tCokIJ5N2y3bDz5mnY1NJ2QVc/5ercZvxCl/inNVmT0uqY ZD+NQrmf8DsUEK6EnVnrKyMYQC5VU3VJrR1WEwRehm59ZBn7ClLt254cr dlyD5mc4CKQZzZQFlY4+gZ+iky1LIE6G7ggU4YyoIgGqCJjTRJuiMArxI YoLapnFYxE7TtqEipAq2vbwhIu/8b/+wGapILaHdvgk67Ms7isS6L7rLv Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="418212186" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="418212186" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 08:14:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="1005688925" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="1005688925" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 24 Oct 2023 08:14: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, joao.m.martins@oracle.com, Jacob Pan Subject: [PATCH v7 6/8] iommu/vt-d: Set the nested domain to a device Date: Tue, 24 Oct 2023 08:14:10 -0700 Message-Id: <20231024151412.50046-7-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024151412.50046-1-yi.l.liu@intel.com> References: <20231024151412.50046-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:28:24 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780651190293498835 X-GMAIL-MSGID: 1780651190293498835 This adds the helper for setting the nested domain to a device hence enable nested domain usage on Intel VT-d. Signed-off-by: Jacob Pan Signed-off-by: Lu Baolu Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/iommu/intel/nested.c | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/drivers/iommu/intel/nested.c b/drivers/iommu/intel/nested.c index 859c60bead05..d9197dd72748 100644 --- a/drivers/iommu/intel/nested.c +++ b/drivers/iommu/intel/nested.c @@ -12,8 +12,61 @@ #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); + + if (iommu->agaw < dmar_domain->s2_domain->agaw) { + dev_err_ratelimited(dev, "Adjusted guest address width not compatible\n"); + return -ENODEV; + } + + /* + * Stage-1 domain cannot work alone, it is nested on a s2_domain. + * The s2_domain will be used in nested translation, hence needs + * to ensure the s2_domain is 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, + IOMMU_NO_PASID, 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); + + return 0; +} static void intel_nested_domain_free(struct iommu_domain *domain) { @@ -21,6 +74,7 @@ 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, }; From patchwork Tue Oct 24 15:14:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 157594 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2021565vqx; Tue, 24 Oct 2023 08:28:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGOTejmmEOjogfD+oR5iXTsSpj0upNzvQnNOA+DBGiSFQ11d/CxbJESFmG6DLHJNVtAXWG0 X-Received: by 2002:a05:6a00:15c2:b0:693:3be8:feba with SMTP id o2-20020a056a0015c200b006933be8febamr10335448pfu.19.1698161284739; Tue, 24 Oct 2023 08:28:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698161284; cv=none; d=google.com; s=arc-20160816; b=0ibeZ6ruX1E5Q0/KvNbDN2PwcNvxskVs/jnaJWQiZWjAf2IFYn/Da1WvfmWy44VICt HALFgQlotw0BE+SQcnbj7oqA14pkeNraS3KUAWj2R0df4GkzgkuVw6tB+5opIGbmsna4 TWlpEMYQzP8Va+tw0ZMT/piSTiRnvqRaJnuFf0eUbTGdw2ypm6oNs5foyru7TFdcvYDT Plk1Q0IQmsynVFv3BOmJfsKmPlP1iXSD4HCmQq1y5M3AU3GkWH/2AARH6605rBdk7oJK olr+5n+1hRHbNgdenw7wQO3gqwWcI8n7CdU+i4UCfacL3KkSYamTGiT9OK/5X2Mx9HtE GsYA== 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=KCq35yXEMbgdVUDgF8pUfoW+hr5Jd1/w4dy23zNbjfI=; fh=ncJBVmsnOSqrX1O37yfYEzicwaA2e7ARxnsU7aiysyE=; b=YZXeHaLXP9c6cjEI2ZvBrgV/YoFE09XxwLXo8HRMMwt2XksW4/nguyANVf0TV8Z7bv /jwEcHpmnUPwCJ+XSonTK6HrjGnEJYuB8gCk9im0Iy01OSQGx6QpY+mjrbbRc+v57hEL JTLwD6hyRVTRPXAtPW1r6408JnbswRujzZgHDWwzknWMRFhFtQYsBdDn8ZikdOt9l/LV xN9Lg8KykN/T/yWee5GRm5fNGl6kBSfLqfquxZbEJQBiDD+3MO0wXj/RxUDO7UYM/Em3 MIK5hlJi5thnM0RUNOt3k1n/tdndf4bNQaABR2Qa63riZ+bCYKx35P1sfM3Jk4l8+63D 1bfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GzCTkamU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id j191-20020a6380c8000000b005a1d8816535si968315pgd.5.2023.10.24.08.28.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:28:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GzCTkamU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id A7DC5802BD63; Tue, 24 Oct 2023 08:28:01 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343773AbjJXP1Y (ORCPT + 27 others); Tue, 24 Oct 2023 11:27:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234992AbjJXP1E (ORCPT ); Tue, 24 Oct 2023 11:27:04 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CA3726A8; Tue, 24 Oct 2023 08:25:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698161153; x=1729697153; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8bknCuaXgjYm3S8328FfTMjKxCUCRzktFyfoYpNNj9c=; b=GzCTkamULyCM+t/oVAfkKoE4HSzJi5VSWcaG9X7NJmfMJ4NVxC+cgaE0 yVTZOhXbcrMyjtB0cEWZp+4t45GaD81DEt0Zuqxun++vkdyA96egzMgUA PuS6Y7vDgRAEzus/YjSCC372OAuaXWOp11L0AX0qWmk/lQgtpF5V1DrBh xr9V+mrQeQUazR5M64hBlZByAu3gltlwqWzfPZDnK9wTVHst/ptcSBfGk G5kgXk+FtHF3VJMaXQQV7pQtqoUCtsGAQJnMB7EQNHl+NvMteUz5LGkIl 0/7yW853+hea17rEGnbXD2X+H2iHf5rH81rCg9SGQd5F40uzFD5tQlZjo Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="418212209" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="418212209" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 08:14:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="1005688928" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="1005688928" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 24 Oct 2023 08:14: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, joao.m.martins@oracle.com Subject: [PATCH v7 7/8] iommu/vt-d: Add nested domain allocation Date: Tue, 24 Oct 2023 08:14:11 -0700 Message-Id: <20231024151412.50046-8-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024151412.50046-1-yi.l.liu@intel.com> References: <20231024151412.50046-1-yi.l.liu@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:28:01 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780651167427232092 X-GMAIL-MSGID: 1780651167427232092 From: Lu Baolu This adds the support for IOMMU_HWPT_DATA_VTD_S1 type. And 'nested_parent' is added to mark the nested parent domain to sanitize the input parent domain. Signed-off-by: Lu Baolu Signed-off-by: Yi Liu Reviewed-by: Kevin Tian --- drivers/iommu/intel/iommu.c | 67 +++++++++++++++++++++++++------------ drivers/iommu/intel/iommu.h | 1 + 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 292baa64188b..85366862fb5e 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -4077,46 +4077,69 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags, struct iommu_domain *parent, const struct iommu_user_data *user_data) { - struct iommu_domain *domain; struct intel_iommu *iommu; bool dirty_tracking; + bool nested_parent; if (flags & (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING))) return ERR_PTR(-EOPNOTSUPP); - if (parent || user_data) - return ERR_PTR(-EOPNOTSUPP); - iommu = device_to_iommu(dev, NULL, NULL); if (!iommu) return ERR_PTR(-ENODEV); - if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !nested_supported(iommu)) - return ERR_PTR(-EOPNOTSUPP); - + nested_parent = flags & IOMMU_HWPT_ALLOC_NEST_PARENT; dirty_tracking = (flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING); - if (dirty_tracking && !ssads_supported(iommu)) - return ERR_PTR(-EOPNOTSUPP); - /* - * domain_alloc_user op needs to fully initialize a domain - * before return, so uses iommu_domain_alloc() here for - * simple. - */ - domain = iommu_domain_alloc(dev->bus); - if (!domain) - domain = ERR_PTR(-ENOMEM); + if (!user_data) { /* Must be PAGING domain */ + struct iommu_domain *domain; - if (!IS_ERR(domain) && dirty_tracking) { - if (to_dmar_domain(domain)->use_first_level) { - iommu_domain_free(domain); + if (nested_parent && !nested_supported(iommu)) return ERR_PTR(-EOPNOTSUPP); + if (dirty_tracking && !ssads_supported(iommu)) + return ERR_PTR(-EOPNOTSUPP); + if (parent) + return ERR_PTR(-EINVAL); + + /* + * domain_alloc_user op needs to fully initialize a domain + * before return, so uses iommu_domain_alloc() here for + * simple. + */ + domain = iommu_domain_alloc(dev->bus); + if (!domain) + return ERR_PTR(-ENOMEM); + + if (nested_parent) + to_dmar_domain(domain)->nested_parent = true; + + if (dirty_tracking) { + if (to_dmar_domain(domain)->use_first_level) { + iommu_domain_free(domain); + return ERR_PTR(-EOPNOTSUPP); + } + domain->dirty_ops = &intel_dirty_ops; } - domain->dirty_ops = &intel_dirty_ops; + + return domain; } - return domain; + /* Must be nested domain */ + if (user_data->type != IOMMU_HWPT_DATA_VTD_S1) + return ERR_PTR(-EOPNOTSUPP); + if (!nested_supported(iommu)) + return ERR_PTR(-EOPNOTSUPP); + if (!parent || parent->ops != intel_iommu_ops.default_domain_ops) + return ERR_PTR(-EINVAL); + if (!to_dmar_domain(parent)->nested_parent) + return ERR_PTR(-EINVAL); + if (nested_parent) + return ERR_PTR(-EINVAL); + if (dirty_tracking) + return ERR_PTR(-EINVAL); + + return intel_nested_domain_alloc(parent, user_data); } static void intel_iommu_domain_free(struct iommu_domain *domain) diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h index d5aaaedf2094..65e660eb1f47 100644 --- a/drivers/iommu/intel/iommu.h +++ b/drivers/iommu/intel/iommu.h @@ -601,6 +601,7 @@ struct dmar_domain { * level. */ u8 dirty_tracking:1; /* Dirty tracking is enabled */ + u8 nested_parent:1; /* Has other domains nested on it */ spinlock_t lock; /* Protect device tracking lists */ struct list_head devices; /* all devices' list */ From patchwork Tue Oct 24 15:14:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yi Liu X-Patchwork-Id: 157591 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2021154vqx; Tue, 24 Oct 2023 08:27:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFtb4zQ0VJXm+TX1TM3jBeznYdzQYxr3dPZPvM900P48gAGEGvUpqUSnVrvmhaC9mkaTBXw X-Received: by 2002:a05:6a20:3c93:b0:13d:8876:4c97 with SMTP id b19-20020a056a203c9300b0013d88764c97mr3269778pzj.16.1698161247936; Tue, 24 Oct 2023 08:27:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698161247; cv=none; d=google.com; s=arc-20160816; b=jRRmsg9jtztADtAATAOQUlJjwgZzvag7K2KdHxBEbzhNvZwC4pY6yPi+DAhzTfAg2P IwYxsRu206LZY9+Sf0K9KDXn/ykVk7J99mTg1yP8CnrmYINz2c9CuYXC20IfTG+fOXDs eRLeM043AMKaGNT/+oru2ZH80eDjxuL3H197udDHLz/193Q9kny7lYBL40qN+W9+PY/+ +khp5JigC24HcIrwVmiMWV2Pnv1m5eCWJg28qFn9VSWRHQdjwCZf0877L7MTm9aNGJ1H iiUGVcP9L3c/7zDwWDJRWPAhVwV6Pdb6bFNCeCiSDiE4airb67nWuXqDH/leaEn+zp2H Ja3A== 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=5aNGRQv+z4tKSO5snxoOlURnqCTEFQdYvNDf/vddssc=; fh=ncJBVmsnOSqrX1O37yfYEzicwaA2e7ARxnsU7aiysyE=; b=dVvTPBadqIrwYwPloHXmEleks9shwksD1SW+IZM8eujexxgBtfoz+chN4fi8p/r4Br Ub8J/Imndy+sKbngek7P7EyEcgV1yHD0v9vxp2WgZAxFKhwbDMFjDIWt+B5BalwpX85C kH6WWw2ppdTHeZHStlgSA7VBTzl8M116q9DSrNX7yQa8Lw7uXyiroZruhIgbbIABuwfm vLEEg1WwUzZYaKAK346r8wnAexFq0w+CLcg0b5hSezcRfWxidFyjyxZ8ilN2SK9EQylO W7ShDz6U1ffvH4OUzWyCDy4Sk3C//BvN9pVDTzt3Aweo9OXa/DPxW6yqUQ+5F7d3a2dh uAZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Zb8y2Tq7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id r12-20020a63ec4c000000b00577616e3ad9si8277622pgj.871.2023.10.24.08.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 08:27:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Zb8y2Tq7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 080D5802C698; Tue, 24 Oct 2023 08:27:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234821AbjJXP1U (ORCPT + 27 others); Tue, 24 Oct 2023 11:27:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234820AbjJXP1F (ORCPT ); Tue, 24 Oct 2023 11:27:05 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A65626B0; Tue, 24 Oct 2023 08:25:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698161154; x=1729697154; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EvYYap6jVoXeJ39dE5VX3YkM1fWw70nq/hojBtiJ2Lc=; b=Zb8y2Tq7DrRWnrpOPGfFV6DBz4Izfm0ojzpUClO4PpC7cTI4WBZJJe2C VwGW7wNy464ZsNipgPIO4OdRgMrQmupeEKwd8/qv6qmW4XlLmtRiq2J5u QSje+2MZM97HQf7JGYSqhHlkH8RXR2K+QKf3+LCbWwdoCuKxwZMfehVjL pcpmkQuNVPUBfzGzpou6V6Toqi/Fl3TWLPqyci78pmFGX5+0DTwsQmL9f MhPhV5f9rZee/+vH327H9G+PVsB/SddCmAMBerbfNSPPJiEyPa4llfKCC uEiVkA7u8iF5TlqFFPN2oBV33g/Y4CwZm8XQlaSuiZ16hdQVAjeO25npW w==; X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="418212230" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="418212230" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Oct 2023 08:14:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10873"; a="1005688935" X-IronPort-AV: E=Sophos;i="6.03,248,1694761200"; d="scan'208";a="1005688935" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga006.fm.intel.com with ESMTP; 24 Oct 2023 08:14: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, joao.m.martins@oracle.com Subject: [PATCH v7 8/8] iommu/vt-d: Disallow read-only mappings to nest parent domain Date: Tue, 24 Oct 2023 08:14:12 -0700 Message-Id: <20231024151412.50046-9-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231024151412.50046-1-yi.l.liu@intel.com> References: <20231024151412.50046-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, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 24 Oct 2023 08:27:27 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780651128545708655 X-GMAIL-MSGID: 1780651128545708655 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. This disallows read-only mappings in the domain that is supposed to be used as nested parent. Reference from Sapphire Rapids Specification Update [1], errata details, SPR17. Userspace should know this limitation by checking the IOMMU_HW_INFO_VTD_ERRATA_772415_SPR17 flag reported in the IOMMU_GET_HW_INFO ioctl. [1] https://www.intel.com/content/www/us/en/content-details/772415/content-details.html Reviewed-by: Kevin Tian Signed-off-by: Lu Baolu Signed-off-by: Yi Liu --- drivers/iommu/intel/iommu.c | 6 ++++++ include/uapi/linux/iommufd.h | 12 +++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/intel/iommu.c b/drivers/iommu/intel/iommu.c index 85366862fb5e..1a50d3f53713 100644 --- a/drivers/iommu/intel/iommu.c +++ b/drivers/iommu/intel/iommu.c @@ -2194,6 +2194,11 @@ __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->nested_parent) { + pr_err_ratelimited("Read-only mapping is disallowed on the domain which serves as the parent in a nested configuration, due to HW errata (ERRATA_772415_SPR17)\n"); + return -EINVAL; + } + attr = prot & (DMA_PTE_READ | DMA_PTE_WRITE | DMA_PTE_SNP); attr |= DMA_FL_PTE_PRESENT; if (domain->use_first_level) { @@ -4872,6 +4877,7 @@ static void *intel_iommu_hw_info(struct device *dev, u32 *length, u32 *type) 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/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 3a66d3888393..cb7d17e3b4e7 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -444,10 +444,20 @@ struct iommu_hwpt_alloc { }; #define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) +/** + * 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