From patchwork Mon Mar 4 23:44:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13581365 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5DA1AC54E49 for ; Mon, 4 Mar 2024 23:45:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=GzviVju8aAcuatuEGNlhuGJjx+qs94rgSE0ay6u79AY=; b=GFYnOemC5izjm0 3b+cvUuGBUa21zVjYWP1FKnEpfgEjWxBRs+vSTUuOmuq+LaBVD2G1FZoWhq2X5dPLWCvPJCSN+B/4 oxfT8sdSVMEmFA3gz9uCqorrR5905Zo1Dz5bmeYm7sj35avv/TCgonMUfCdULMyGHpzHsp7isU41j 3LnWdF4+7HiMhubyq8yjhUPkwJT9Bis8ffKCj9C4VpmZ37xVzpi1hc/3G+iQF8hNP4JVhTM9vNT6D 1CQp0lgKW7LRWE0hoVcttdUvgYY0GA49TadgckZrlCBn34K9daAoj4RVujVvvlr+jDvbYyL5444op mfMNf6ieHi/z6LtDlX/w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhI03-0000000B832-0OFL; Mon, 04 Mar 2024 23:45:39 +0000 Received: from mail-mw2nam10on20601.outbound.protection.outlook.com ([2a01:111:f403:2412::601] helo=NAM10-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhHz7-0000000B7Au-0OWT for linux-arm-kernel@lists.infradead.org; Mon, 04 Mar 2024 23:44:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X2e693pV3HBWp+vmH3vExyDUgMlkhJCuxBeOJzf/uANM3zFpPRnRF5swY5z/M1nx9cZ2PkKMLdNdMWcMF6gzhfpiTKlYURH40iKwh84ffuOoG4zxUm8c4+o8zU5bkb+KOZOVW+GWpYZBhw2K3VAK2cpZU1r6KKd3JEcvda4vpUPRYHYBDc0kEQyKZIn9Y6Qg4dB2bP6JTPx7Qy8AkTs/jpWxsPgGGBnLsl0vUG+ncO34f0rNKzTw4OJIOJB7ummMZXTvgYcCyDB5Vg+IpIg3JWIGnz/6J+sqR+M82OT6XYWWZ8RDdHa6XKib0EXOoecRjwARdV0LLMsEzyqyw53/EA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=qa8mXWX8hmnXz9ypuGLj01PRBuGZ4u4hEq+IQOpCPAc=; b=BXyAhmv+2NMq4ER/HuC7Dvzhajc+h4XVbOSLnwEsRrxLKas+SqmViojToN1nchecqKEXprXGBWu8T83l6+N0N+ydavxYqi309PMGtSCGhi6BVGUcqi9C1agnpQy+qsjYrsuxhckFkfvUKqU6+y0AKSpDWp0avKnPguzWimZpKd/7ykHLom7nmkxim0rUd07MsTVwF4u6jMx8psrAlXFMV8Fj7X8AbRm5aMCR8F7JBlQlMPSnLXjieWIEbNRd2HqizshMBdlamBNLNb79iXL05OwQBqqvRMWEq22PT4BJDiMK2ekqf7VPvAFNMqknIlXK40Ij9BSfGsw+8/GZIwgxQg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=qa8mXWX8hmnXz9ypuGLj01PRBuGZ4u4hEq+IQOpCPAc=; b=E+AAhfk8EQTO9Wzi8fwL4i2/5r5hqtoc1OzyZgd04sZP0/8t/6d7PuiO+z+MaUnmwoUc/oNMizJSSIKATofjxHNN/XDtlQVo9k4U+IQtak84PQ0koI8eZJuk0hWtH2zx6/U/of1hq68Bny0NzQ2sZCdL2YoKR/PgXk6eoGbU4Q/jb/hQieXFcUu/tbEu+FPyIC71Zl3GUzRC/ckNf4YCkMOcyNqaPbPeQ4jL4rClWaKndLe9yQVfqMC013ZR+CAJGWui1sXMmrQl35fMwDVkjxWJYT1uwp6IRhOOkpqTWQOnkI4nB4lwID+KDfYeH1MdGhuiEKqnI75PlrTra3/C1g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by MW6PR12MB8949.namprd12.prod.outlook.com (2603:10b6:303:248::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7339.38; Mon, 4 Mar 2024 23:44:22 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c33c:18db:c570:33b3]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c33c:18db:c570:33b3%5]) with mapi id 15.20.7339.035; Mon, 4 Mar 2024 23:44:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH v5 12/27] iommu/arm-smmu-v3: Start building a generic PASID layer Date: Mon, 4 Mar 2024 19:44:00 -0400 Message-ID: <12-v5-9a37e0c884ce+31e3-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v5-9a37e0c884ce+31e3-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR12CA0018.namprd12.prod.outlook.com (2603:10b6:208:a8::31) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|MW6PR12MB8949:EE_ X-MS-Office365-Filtering-Correlation-Id: ea3135e5-6b63-4bd3-f8e4-08dc3ca5014f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: LPJ0xAZOKj8cBoxlk5DebfTNqDnhti7f581AGg2J5Vixt/b1Qmr1J1oYCheqwor0Zq9wf++1mI5a1USW36D1To3WleC1Z9sXQzb5t2XGjceoTiLTrwvFl7ZVP+VEpB5uXmgn/ck6UL34F2RKRBWiBHoExx6rAz+INGUNjb1MJPSJn9TGocvmwSIWHbZil2r+8/s8maycPhI6sxFk68BoBhMAXR/viFk+VsGOl2GO2PUDhgVnFd5oAYjT1by77u3GvjRQueSg7TKWzxHSlQ7qGawvMpuR68ni/Jt4IY8cZ8Ic72yEKKQ5ZeJWC0ITXIC2rRhZrhHvuNp4KeYWUYRjrTqQhARC1Tfw4eonPNeCeUraXVoKLBEw8AWXNkMVxrGb3T3DmIHLnawI+n3cHz9RzgSFQ/xvBMcL1m7tU1+sbmD3o2UPnaQMl38Fe6uEsnUV8PlSv4iNV3Tz/nCZubRQZoIPC24dDudKHE8E4gpymlt3TTaz2d3bRqD6HNOMQVbNpAOFi15HafvVzCDmfMcmUmsD5SbXOPAgm3++wyXZfXZBz1UTZXXxxU7vWhyxwttQUyRwfjsQp3p7pkFKlCibDOYXoOqb5iXfiBve8KgZywdwCYT/M80JQ+sF/V/LwRcMypsEGZM9lj/lRa8qH6y6Vx8tDMj/ffkI4p1doNtLQvc= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(376005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: c7ritClFodtROkR1VrF6ozTcNAYd9g/qR+BpmWv9MVu3Q85Uo42vvBdH1iGVS53KCVQAPIpSGjaKGTdilEOjLiaB+1cV2nXCrcXgaifkPwC7vhOlS/SO5BM8A+UGFXof5J4pDUZKdrhvQUxZJIWjDWiSSi/3ZsRQb8MWwY5f9mknCaSx2EbXeEWzhmtabnZ7+iFz3ay4Ec1zpwFodfT+1aYApuHnj8cM5vNUdop7LQsVGCVT61RmbYhMhpWMg+3GhZ0c5Vjz3VlKQd/07y0W66sqWeX1bYguLjNR1qK6M3JT/p5KQQzFGeJE+JpYABrEb3KNLr+08TGRDaoneLZlhPk88wbWvr2QJye4pV8VwbY/wIvNNoXJiWeLwz1cdJBrDopCXE3M50PB64vrQAtNnjuW8vSVPceig70LMwvA8U2nwp7dOUR2oTE8YZVEOHMBUphiWpcWuJbzYHNeN/rCdV7hz9HWdTifH19xOIhlnZlPcM5uyMl69ozwLEokisaOIvHt05su6SNH2/Ziq0bOBZWWVe9mNeoH3ndiXiXIIHomH6azpo2uEv0mKBkQj5/uIqHkLL8kt+7cRRW7FvrxMvA5qPJb8k5gXmEjY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enant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Mar 2024 23:44:16.9909 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: YfVYTwnudVoXkkLTuKvLuLXedcH1Ohxw1GK7TJLrrHKiOK8L7iBkCwDGYt0ifdjB X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB8949 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240304_154442_604935_F0056884 X-CRM114-Status: GOOD ( 17.47 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add arm_smmu_set_pasid()/arm_smmu_remove_pasid() which are to be used by callers that already constructed the arm_smmu_cd they wish to program. These functions will encapsulate the shared logic to setup a CD entry that will be shared by SVA and S1 domain cases. Prior fixes had already moved this logic most up into __arm_smmu_sva_bind(), move it to it's final home. This does not yet relieve the SVA restrictions: - The RID domain is a S1 domain - The programmed PASID is the mm_get_enqcmd_pasid() - Nothing changes while SVA is running (sva_enable) Also, SVA invalidation will still iterate over the S1 domain's master list. Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 39 +++++++++---------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 29 ++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 +++ 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c index 159350a78d4cf9..e1fa2074c0b37b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c @@ -416,12 +416,10 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) arm_smmu_free_shared_cd(cd); } -static int __arm_smmu_sva_bind(struct device *dev, ioasid_t pasid, - struct mm_struct *mm) +static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, + struct arm_smmu_cd *target) { int ret; - struct arm_smmu_cd target; - struct arm_smmu_cd *cdptr; struct arm_smmu_bond *bond; struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct iommu_domain *domain = iommu_get_domain_for_dev(dev); @@ -448,19 +446,10 @@ static int __arm_smmu_sva_bind(struct device *dev, ioasid_t pasid, goto err_free_bond; } - cdptr = arm_smmu_get_cd_ptr(master, mm_get_enqcmd_pasid(mm)); - if (!cdptr) { - ret = -ENOMEM; - goto err_put_notifier; - } - arm_smmu_make_sva_cd(&target, master, mm, bond->smmu_mn->cd->asid); - arm_smmu_write_cd_entry(master, pasid, cdptr, &target); - + arm_smmu_make_sva_cd(target, master, mm, bond->smmu_mn->cd->asid); list_add(&bond->list, &master->bonds); return 0; -err_put_notifier: - arm_smmu_mmu_notifier_put(bond->smmu_mn); err_free_bond: kfree(bond); return ret; @@ -621,10 +610,9 @@ void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, struct arm_smmu_bond *bond = NULL, *t; struct arm_smmu_master *master = dev_iommu_priv_get(dev); + arm_smmu_remove_pasid(master, to_smmu_domain(domain), id); + mutex_lock(&sva_lock); - - arm_smmu_clear_cd(master, id); - list_for_each_entry(t, &master->bonds, list) { if (t->mm == mm) { bond = t; @@ -643,17 +631,26 @@ void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id) { + struct arm_smmu_master *master = dev_iommu_priv_get(dev); int ret = 0; struct mm_struct *mm = domain->mm; + struct arm_smmu_cd target; if (mm_get_enqcmd_pasid(mm) != id) return -EINVAL; - mutex_lock(&sva_lock); - ret = __arm_smmu_sva_bind(dev, id, mm); - mutex_unlock(&sva_lock); + if (!arm_smmu_get_cd_ptr(master, id)) + return -ENOMEM; - return ret; + mutex_lock(&sva_lock); + ret = __arm_smmu_sva_bind(dev, mm, &target); + mutex_unlock(&sva_lock); + if (ret) + return ret; + + /* This cannot fail since we preallocated the cdptr */ + arm_smmu_set_pasid(master, to_smmu_domain(domain), id, &target); + return 0; } static void arm_smmu_sva_domain_free(struct iommu_domain *domain) diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index dfdd48cf217c4e..7a0be7dd719793 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2656,6 +2656,35 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return 0; } +static bool arm_smmu_is_s1_domain(struct iommu_domain *domain) +{ + if (!domain || !(domain->type & __IOMMU_DOMAIN_PAGING)) + return false; + return to_smmu_domain(domain)->stage == ARM_SMMU_DOMAIN_S1; +} + +int arm_smmu_set_pasid(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain, ioasid_t pasid, + const struct arm_smmu_cd *cd) +{ + struct arm_smmu_cd *cdptr; + + if (!arm_smmu_is_s1_domain(iommu_get_domain_for_dev(master->dev))) + return -ENODEV; + + cdptr = arm_smmu_get_cd_ptr(master, pasid); + if (!cdptr) + return -ENOMEM; + arm_smmu_write_cd_entry(master, pasid, cdptr, cd); + return 0; +} + +void arm_smmu_remove_pasid(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain, ioasid_t pasid) +{ + arm_smmu_clear_cd(master, pasid); +} + static int arm_smmu_attach_dev_ste(struct device *dev, struct arm_smmu_ste *ste) { diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index 468cd33b80ac35..ab0a6587e07ec8 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -753,6 +753,12 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, struct arm_smmu_cd *cdptr, const struct arm_smmu_cd *target); +int arm_smmu_set_pasid(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain, ioasid_t pasid, + const struct arm_smmu_cd *cd); +void arm_smmu_remove_pasid(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain, ioasid_t pasid); + void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, size_t granule, bool leaf,