From patchwork Wed Nov 1 23:36:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443149 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 E7438C4167D for ; Wed, 1 Nov 2023 23:37:55 +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=xDkT09kwy4nv4KF0uLe4lHUIs2w1dW5kuCAFMclxMJs=; b=qkm3HrCuEA0vy9 nypZu9pTetxD/8pk7gBk2sNicKD3nmoxU3WA8bcnb2w6N+hmdxwt1ohzSakAD9iJUykQvCpAvmUkJ kQrnqFxLEGjkSQW10Ej00AoXzYcHxYcejXEn9ckgy2DZNlCJTG0taFpOkjPj9+3hIIM1KhViGft++ kIBGiCRLsCUH/slIeToK43R/4UPIysP1mQV86x6YxqGeA2dViTjeIQyO22P2iLN7BZri+ChmlwTQk 5YgTnhvfCGS6L+3zrlji0FrTP88SoA1tEnUtuza0HE/IcG1OdLGD+ucQnhv6yE7ln3Cm+VT02WQ8Q 7h6HNHFpmVYCpNHST60Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlt-008Jlo-2z; Wed, 01 Nov 2023 23:37:13 +0000 Received: from mail-bn8nam04on20610.outbound.protection.outlook.com ([2a01:111:f400:7e8d::610] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlj-008JV7-2C for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:08 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SBXxmUD2h6ptv+qZMCsu4+rbkoLQrdWITWK5J3P8IuCipA1GT+wfkkneQ0TEdhDttWgBO2nOXv9NdtQ3nwDglPUMHTNzOuvkNWLOrNnDuXywjaWW7f8HYFdhFGmXtBiVjmKpT1StMpFCwyOYvX+X4cab3QERkXcuT2nAM6DWCOiDLsufyE3rPjPuGjIULMq4OOfRzcUp2pN4319fkfNsqngrUp2ItoeMrkLGMDkDaHqr1n6F30kzDv9baKpHfm7u5suVHQoQbnvEHaGH0N9C6DhReFMPHIecz3MQI7VMDU5AcDuowVYeo5l2LV9NldM2OcQEOfNnxDFK+XlKojRtXA== 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=TKjlQdEbtFpmWK/cm09Lx7F9KngqmRiMtDiqJUitsfE=; b=OgLqYyk551x706af/MebaBLj66HKpNb8UjrCmU7b4fYPkRiohC1XdwNpcnHH34IxsB88PeLIl8PKlFeQayDKMjCxsg1IBciinrR+e6M7KZIkUQgXKRxIgmazItzWjWyeVvQluMsB2o/mnmquWvXNLBXopmeckno1k83kMP9dHE4UWqEgqBM8WsCsyKyRHipqjesHKf5cqawOcQ58EQcfCguif1I7qfPI8zIOjZHG671T7li0myt/u0iH64sWRxTWYEhFozSpt8EepJPzP+U8QGYianFGLF+ydcZ1OuUga7ra0TDZCeRSvIlIqfAP71sZq63BHVWiwYVQAxH6f8vPOw== 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=TKjlQdEbtFpmWK/cm09Lx7F9KngqmRiMtDiqJUitsfE=; b=UzzWdB4pObyqb1MsZpCHK8cb91gh6NodwRoY3ImzT/rJzAREqkorqi+rrVenVvRvqj0sni36G5kRwGJAroASM2hAVdHVoOX/SQzfQ7aeEhVFUL0u2mfZos3upkDUMJyUJq/PQW8Ldy/WZ/A0Ud6yGgBLOP2CyU8Em67gkl7ghJ1NHGrghc6pcqeqgmFHs1KJhEo8oYrZref0snMb7XdHsRzmwbx7LeMJo843xbu/ZDP9CPnbv+beZIBqMVKscdA8KCqw3Us6N+/7Zh4w/qw1D2zhgIWP6OgGdN6x4Ub5rCu4TTdmjzFVhbYDx+xYbglHraRq029QpyI94KhCn5eyfA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:49 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:49 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 01/27] iommu/arm-smmu-v3: Check that the RID domain is S1 in SVA Date: Wed, 1 Nov 2023 20:36:19 -0300 Message-ID: <1-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR02CA0030.namprd02.prod.outlook.com (2603:10b6:208:fc::43) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 6f9ebfce-0b2f-434a-55b5-08dbdb3369e3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OgicdbPfSETUI0sskXKzc0rb7lerWXswQ4hEHZO0D507GXKofcxZMJYB4wt8Ugp6L+VVpIPVDohrmnWUxflYdGX1B1vGS4fsWOH7XY8QyBvUnWC+vPzbjlo0sToMSRgt4t6xZlL+iBCsjNzBVU+rPgWGkUpfRCHfYrKqF7mkUM65yR4dMdmh9ga7SVXRy1xJhyyX1Xj3JOccxPYD2kDyS910ESex0JKEUWLEgyt9YIwkewl692UfsfdtGqp+efDrM+EUnPF7N3Gb+lZ136w8Tm07wQJpplchl93wXdrox0WveLKtCHFxDZnfcEKiBeT5NQgcGUi+eZrfrQ5MYmxwM/mZLdMpFSBmmxw3xI8zvbz7U3hjfPWjQ2IigSZsK7jAovNqS+23q3QpBI/x6vwX5fD1bImEvFDZDKM76LmicYUkrOn/16Fn3YvKS5XwQEjwDqDMreh9SoW5Ua2sAqoqRhvl3vb5L28I/6M6H+B7c9gRH/8KVW3pcwJNvi+Q8v7QYU23PfTQg3obe+UnIElYsIQfYS6VPCDhfSMLWwCXHHqpiMg5dLKbe93XwbNqSenPuaccmVYJ+xRKpL54h/0Nkxpo22SWD4rVocArwiqe7BU= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qIzzX0nVQrnuAJzndr1QLwP6MfX0qkFNun+xCsTtUQH2qH4h7XhZSaUCFEcTSo9x+K7vi14wqf0rt6BtjpqDYoxW/b5XDsfswguNgBKSscj/qP93AnW3VJybYx/L3hg+BwyHc+VyyCDLRZTHA1X1swhg4N1lY7X2Fop9AHoPkGMbc/oi3lugmxcEckxy4vOl4fvg5+JKVPX4wVIBHNH6+xPTYf5yvcxQoUoJxbM+zB/e2eE1aE/Qe+h29Q2i7VzGAG00yjtgEBmoTMUxXcTZc8IwBNX8Ydd5csxhc6c3zVWopeqwJKPdKgAXv+JU5opIIy0rWOrXHzGSYg1yHaMfLdY2lPvEpVFTEwQ4QE29Rf7Rg2GDajZqHcggzBL3k3m+/L8qUF9CBY+R1/Qb6UosM4YfXTSRTu6ng0Hzv7yfq1ua/yrAd5rTkRMBf1hPA8yR9OkR/llfzTWaCtMEDeBOk8DuMsEmtSMmvsWesAEX0J132wph3atKELHyLd0/QPkBPzscLtzP5Gq86/ECpCbORpX/8YeDNrRcYl3Hr7ueLWgcNNGOXNVf0dpdrOYNL9igNJTzM8ofHX15Cx7LUZbsdQp+/P0AFDonfBBnXV3aHwYC4N/XLhrQ87nNMhF/OhIjaEgcHJrT82hU3SXULK7rjc7zr8pWgUTqS3Uoh9jdbKWTTz8ifSUC0eEsS3PxI+48f3h2ekWAAwuh6j/heiqHXjrFpwqyp3pQHr0karxiUpcEQ3ozA08F6Vd5m/SwRlm4by1B2Mkbx+TNXaBEiCjtVGcs5YgNuRxo4hBcm42fdyzbEMyBteCN/dNSBsL1eEsHnNNroXYxQNivu0ehjX8rh+y6EtNYP6jIEKS64Rhl4wrNScr3yURLeQUxS4c2OogRyC7ja8p731QEvbeksZBgBEI0pOav3AZcyFKLizWYArPnsOuh5bbU6gW5EHlk8sP2R5GGO8RSZQGcDtrw90DNOQ9DvOKwax4f0SM20AA5WqFoBludbCmRlJKNrWezwV3E6syNPk15bYV9ngnjT0SYzHLbpYOWKzoTK8P9Aq72gWXG8I6Ewh49pPwpVcMcft/g2+ElQF0k0sJ5SSRRhiKLCGnZT6qTdy8scpA32UtoKScdvng72kyYCeelUUGzc+a6onl00VCeZ69g+UMRcRyjWXRW1Wsaa4fOJbYA30Csce8Ce4m4v3sdgUOMKSDB7D4QzzfqzZt9WE0Svars5vuojgXoUyj5PY1KYsPLRRZaxDpG7vloSXAebXPCA5vobpwlFdIOIgd00KCJNZ6hqze6pS9uIma0B15ggtlT1ynOnf4E3U06hrnws3gpAJLf/8+SOnXPjRjlnsAtibY2aEGZZ8UADOPby97i3ir9juGpro8KCmoc8oHb2x+2EiKppCm1fvNVvpTXaXffL+5Yy8Le5BK3QtjBmgnV7Nf+NaumnAdO9E6BNs8k4/y69VAvDiNnPtADzCwftPZxenyVP0YVKWnpUuyFMmJGAxVt50WlDPB5PR5i4BYVCOwL6w5r6LhpZA5mBVeQlLcAv4UapN+TfKm3CgCNOZX1LQugzhkUWTefTe8Fb79Dzt4HbLRR+Jeq X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6f9ebfce-0b2f-434a-55b5-08dbdb3369e3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.0402 (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: TbhaoeqwhBM4hJZFwBphq7JtkGaUG5QEkfznV1mFnUgUljpScdSc/Jf2XwPMA68x X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163703_729054_8EBB032D X-CRM114-Status: GOOD ( 12.75 ) 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 This code only works if the RID domain is a S1 domain and has already installed the cdtable. Add a to_smmu_domain_safe() which does a robust conversion from struct iommu_domain to the struct arm_smmu_domain. Signed-off-by: Jason Gunthorpe Reviewed-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 7 +++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 7 +++---- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 6 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 353248ab18e76d..bd0566381c58a9 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 @@ -379,8 +379,11 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) int ret; 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); - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + struct arm_smmu_domain *smmu_domain = + to_smmu_domain_safe(iommu_get_domain_for_dev(dev)); + + if (!smmu_domain || smmu_domain->stage != ARM_SMMU_DOMAIN_S1) + return -ENODEV; if (!master || !master->sva_enabled) return -ENODEV; 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 5667521bd18091..43f5531795f0b0 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2486,14 +2486,13 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master *master) static void arm_smmu_detach_dev(struct arm_smmu_master *master) { - struct iommu_domain *domain = iommu_get_domain_for_dev(master->dev); - struct arm_smmu_domain *smmu_domain; + struct arm_smmu_domain *smmu_domain = + to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); unsigned long flags; - if (!domain || !(domain->type & __IOMMU_DOMAIN_PAGING)) + if (!smmu_domain) return; - smmu_domain = to_smmu_domain(domain); arm_smmu_disable_ats(master, smmu_domain); spin_lock_irqsave(&smmu_domain->devices_lock, flags); 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 154808f96718df..6f62184eaa2434 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -740,6 +740,20 @@ static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) return container_of(dom, struct arm_smmu_domain, domain); } +/* + * Check that the domain type has an arm_smmu_domain struct. The global static + * IDENTITY and BLOCKED domains do not. + */ +static inline struct arm_smmu_domain * +to_smmu_domain_safe(struct iommu_domain *domain) +{ + if (!domain) + return NULL; + if (domain->type & __IOMMU_DOMAIN_PAGING) + return to_smmu_domain(domain); + return NULL; +} + extern struct xarray arm_smmu_asid_xa; extern struct mutex arm_smmu_asid_lock; extern struct arm_smmu_ctx_desc quiet_cd; From patchwork Wed Nov 1 23:36:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443213 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 2F78BC001DD for ; Thu, 2 Nov 2023 00:45:50 +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=Ep2oMMv0HvBaphzZl0IvHaNFGm0gIoGBBd1Sdub82tk=; b=ujzdzH6pAatTF8 zny362lQ4gmziTdFKZdcGNK4Sb2+oG4Trb0VPnRnOlKtAfcnyn1LzuENwbL2hZ2sJ2c5GrvM9ZwaI h1Xz0Z+xkHMB3IvbxyezeiniivARYS/1iXkTHi8DnRY2gIa9Y4wpDZbKOWgCAntgCZmcEl3a44jHi Rq+9CihZjdTpe8RRhRTtdFtK+I0WatprYwsVQSaRlM3+baCBmzvJU3Q/YfL/ZUTGz1oEF9jIGKjxX pAVOX77HErcPdg1IIUNwk7EYkQdYVGQVvAX3R8eBQc3Bsw94Rh4aJUccs/WUzyCtbUFTmTNCKUasF n2ozC0GWsusgrGrXzXMg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLqA-008Q1u-2y; Thu, 02 Nov 2023 00:45:42 +0000 Received: from mail-bn8nam04on2061d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::61d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKmH-008JWb-0r for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hzBYDDLxG8tHHqcjqEqBmgyDVYmHvnqz7Q3Sc7X12kUabTRjBol2bJSk9IhlkJBZjXN6wjqwFaJyWiDsgbgyrkCc3397KddHPq1QYx99IgXaD3tCaqdxD125vZSz2iWZ8URpGCYOoz/COqyajxnPpfz6liRK5WD/0DJSINvYhU94DV8L0Kelv7jCoHWrK6OGa0cCagmpc89OakD5xWspyATripxYYfdyXAa+9ezSg6LZOmvQSU9E2zl4luLus7r2+Bl9BneNrhiBbLYkPjxTCKzQCJI3u5LKElXokbh9RbOuu8gjO/qHzSZ2HiXHOOlmx2cF+c1iTUBkGzMnGZFm/w== 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=+EMCuTiPRk6Mi2mH2pRTQ5SvhH7UnnuGyfIoGoBvDBk=; b=aIRbBZdc/NpEc1jTyF8h8YwJ49P0/80Csx0GH8UYIRjKzRPJoq9QOJNa8Kc7q4mUr+Br/dpMkpRU0IMeo3GthfzrFHvGIJdS/jpMh1XdzieTA5/28V/IAdNvZwiWPURK25Evrz+Uex0u+FySbXnz9tMlZuA2Ez6xiYP07m319rIPLIYJbxDj0p0k6plCA+Y/OML7PUI5BA4iwxyrVs/tXeOkx1+OJQJwIobzPTCzmun2qJc1CyHvVIlydG2n+JafTvEmzw4qiwsRl9Nxdk+dmuLYOo0DZ5X8TuhpSU6B0QWh34S6BKtI3EvF4AFV+YzVFpHcSK1RSQwnhQMhdVqWsw== 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=+EMCuTiPRk6Mi2mH2pRTQ5SvhH7UnnuGyfIoGoBvDBk=; b=llHQGusB6CD0FBhbvqlA4etibyeTfnxq7YVpdHR//78Tpx6jJDQaL1EQzbjKSCRwY3dDhEDU+bYTlWG0+lwTV20mTZzJDQc6qIu+1veLWQmMDkuib7I2sDzKH5ESNdemCr/R0nKL+QV5detOuciOoo7TBj9xpBBxkQpGHWzFcsiOL6XBpdOuTvyMw6W+mHTdZHbzajoyMndD2jTnhkfRVDOC+m2+csDjbRgm1RGfoJdGS+AZJB01uvO2jUBDFYJ7F35TbTET0iVRszvax+6JX3qs0CthyHcckqsg3SGdB2t/Jr9wpZU4N5woQ4e6tlo9MyaPKkYc6zBubl8pYf8SmQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:52 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:52 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 02/27] iommu/arm-smmu-v3: Do not allow a SVA domain to be set on the wrong PASID Date: Wed, 1 Nov 2023 20:36:20 -0300 Message-ID: <2-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0300.namprd13.prod.outlook.com (2603:10b6:208:2bc::35) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: e8035978-1512-41f7-b116-08dbdb336a80 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 037dTqp5r1yUOqHaAvFkV50qE2WUAdJ5B5QDt1L1N4wxu6Zq/g2QfzTLqOwrYh3WYkyO39B3mF9KAltifLeX+l3SBPEWoc+wNJSPKb3kLWvCEZcPtmb/hKLz61eKpsUuPiLvlN1lVYrk9gXqESrFm97yCcqzKvIq8UQMeqAkbKRpGq/OaOQqZGsWB/iaWJXOblNS3mj7qy/AY7QEdb5pLGESyxJbm83x14hsTD66gnKJOuJpuOhNuFbw4sVrFB89vW59Xs+tI2w7BGjCYvKE8uhPZmD+aTFUz5LFGbqjcV5kur+dI5Sz05pCU9zVsM4RFCcP1nTsLBS0LdeKMYsaoKHa7vV00g4y91MTBOeD+wX2vIvnNYrzvJL0Xdi0GbW88+VuoyebmZsLnD7Ie93YXCuxPVV36EoFvrec2y3cI86f7A74abq5rbhBeSwmirp1Nb6j5jCKl8V7kIZzvYjUQHjK29l+JJlHKo5vN2WBXdsfHRuh1QlPyhCfJHH6avuqXPKq5QGbNmmvpOnWnLgIyXoL02hWOoj0YAenRctQFr1GSeR2MqogJyJ4AfsArA4O8IUPIRu87MqNsdKJKWRBwgn/P9xeAYs1afJP06Siklc= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(4744005)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: /shs9b9Y7Z2wNSzpEOcsS+NuWuMX6qgtpVYCVRZVWzOybAvmNI5pXnc5famoNHGNeUS+d7O0MAeumebBtHPHth3tLjy7ePZQfDpboPwacrDekS0ETsSAMSGyWH1R3fU2ZbEBDpTzO6YR1z5CGuNcyVbQN1V0fXQbIMCZ5KwnPXnO1poh+aIK4RqIFIzqNN//AyH3Ctd16byIhvTw8K/j8xXVhoCiS+OpeWZfHdxBpNfq/rAkAwm0cqSsw86E3JKp+udvNJ4vhyPbszHKxUoweT+lOFo44v9NObWeLqbZAr5pKoYLoIAwu3P9JNpMU/j7qGSY2ebx9knhbwsm01ubzxy+pgh+vUnbGzsBj4PvND5MTPB4rjalRt5a1ClUio6LkdfCoSA35UXVe2RROjC2h7jbfV1fxCYf/5fiZmjNnSE7XUVs155S9dlHkNIhZo1dx1lc4451UsHUIquyTNffdrFvjKLPJUzmeRP1nb0YhfebwBaHB4yWxgXujzhyPoHjfiblrjgdAjxjBWBdDomu9Sk0az77Ff2bpyHVVEFE+4iXAA8sYg8TDmhJt+4ZVOXmhHYGR8HRBpy8PVarM7IEu9zbyaqBGOeNinn5Ek/cvd6dFSn8cWVsQBitjXw1G2E65GTRaor0a3kQfyndQH/jGk579EhQpCZdtlRE29FSXSwK8vujYyUlEMgyVDVMKBjx+eRMV5CCDawrwt7/3WoVWE70ksuiwkx5IP3jWjkE1IoTsm/zUmloEmGtjGxDWbQ6j9VRQLDq4UmgqogiBGHfB4/zDb7SDLJPlNNIwSff0qKDwi6VwCuxdOtO8oFESmCwZ7RrczDdNdEOk7lsz1k1uxtpe1wLe9befhBKY/JD3gicGYAYNkA9TNWJ6jh0gMwKhmJHASYybLejKZ6EAZe6kvEM+/203qfGbbFP9mAdmYAr0Zpl5Eptyas55np0iw8oyEsG/c6vurpaMaf1UfZpGYMCkefp7CHzGq1Mt73mibg3cFDezhDUswY9gZLjDL9X/stZhx/U1Z9Yg0XYfzWI6ksS45Aqvcvdoyh9c86EiTroiXC4K0hQhNPW+bNlaLIPBQ8V9Kf6PFvXzWQ8KFFGLVXsKr/Tl9fxAV0aMIiU82YZVY/Mrb6WnQTEr49e6iIvf+1XFGEJ/P+OqCo78x8tE/0Zig4vk4A63buccbtxnlJhijBOrQdtiY1qxJ3loBTg9bZb4uQ9oGIwn+A08u63y7WK33//wyDGHnO9I/GDzMley7BPy7RvGSlS8szg1rDQrQtXyzpr6asfEkf7Zh3Lnx9gxa7uBaxyLKBkNgfHeF+GeLFd95MzIxZo2RBu831eYC9hhwj7D5jmIVGFwuBUvdDze0qfxFWrYXtqHcu0ik8BRBMzeXU133FsrxwqzOM/7ZyYj2vgidz7BRXv41mkwz6QQ/rlCtf1RhdSTWBamhgTGOShEKR85YynLvYt+Y2+T5ho5qdAfMfK8nEBT3zft+TmuBXm0iCnz4OGS/0RtlASdwKC/kv4z8a6SfCmF9gcEqDU0RcizZdgrepOYgpmjI+KkhwaS4IW2mXUWNW62JORA9T7YDsTgoEwMuGL9Oej X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e8035978-1512-41f7-b116-08dbdb336a80 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:48.0614 (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: PdiupbbSvF4618rORkZjZ+HEsVlN6Qzsvi5puu1SToJsSESDtEZr1bSRmA3rzNJR X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163737_350999_1A5AD0C4 X-CRM114-Status: GOOD ( 10.76 ) 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 The SVA code is wired to assume that the SVA is programmed onto the mm->pasid. The current core code always does this, so it is fine. Add a check for clarity. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 3 +++ 1 file changed, 3 insertions(+) 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 bd0566381c58a9..ee3d148aafa26b 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 @@ -585,6 +585,9 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, int ret = 0; struct mm_struct *mm = domain->mm; + if (mm->pasid != id) + return -EINVAL; + mutex_lock(&sva_lock); ret = __arm_smmu_sva_bind(dev, mm); mutex_unlock(&sva_lock); From patchwork Wed Nov 1 23:36:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443155 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 D869EC4167B for ; Wed, 1 Nov 2023 23:38:19 +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=lxkkbszueDzHvoALmYgxfhDtEV/5Yh+DiRMofmgq+WI=; b=Enl4SI4Eoy5V3t ypJUJYL++WnkCyAbFR2GYWED2dGnn6mVdtCgaPD1Nxl6p09hxQK3IXMj8ZRirv1duC1g0ljm4nw9f uIRjHgEqPZBoq8ZAG6u/7xxCJSm9KL5Km7zbZoawYsWX4i2pD342SRWGm4zKIsH4NsghB45AJ9x5a 9OW/iIsQi3sQ8YbvyqTYQPKHsohUJdeH75RT+Uz5ueq2GGV7XCiaEtjTmhFUtf9HogtCfrihhmiaE E0rbJPT7JngWvBuStCRSqyvHMsQA1tVYDlrfKSpIBQZRLMPUT+91gxSunzLSFq8XibMXZkTo2K3T9 NEaXJIrrRTo50DGKrAbA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKmN-008KL3-0j; Wed, 01 Nov 2023 23:37:43 +0000 Received: from mail-bn8nam04on2061d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::61d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKm4-008JWb-2D for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:37 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Yl8Db7i8g9n6xNEuuPeZWZUlVYgYdMqNg61C7givzWM/Sa/fYF17Ntg6mlg5duNzboB7G3bn3yZnED5Khw2nydO/xhzDSB2xUxhAwCTKejOQFsZzg3YC8Ic4WHfzGwOOnAPBQPT1fiTaPf1fTZpWhd0nx8r//xKZ9O33tmgIcK5DUJB4ngzlPUtV8YbbCuYVlep62yv9kdY1tjOZ12qvT4+m1oudIr3K8xBP5YizaJF4tgrxpw6Hcy93lLhwqx4EdJDjTV3Jj5C/Fq7dtDq68aD/2EH73/IHs/fa3+Z3brfiYQMXR5U23rlq9YvoYEE83TFo/YGA1o3fPuph4OGqKg== 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=Ru3TDIkTKVGKaglyCTAFEoUuuVkfNmVrCBZubkA2HV4=; b=IuXUHtV4G7rqJxDGEGdwrBnvRJMN7ln80CoW4XOdjVbMKd5NEk298NPWNECaBMSGwakiOUQImSFZO2WrSGva96PElog/bnkQ0PmANGipv0eFaQshRwdRArYgvHQQzmO9CgnSWuBlQ+FbTbxQQ2aNyJIDbK3ebakB6XD1c1rgC7QC5XTOvVlgmFZ8NpPaL71XDQniM2NSmz9KoTPqWNDdYKa6prWJbkp5acwHNQ9SfO4lzwGgU5oTYCsdpuvbOrCR/oWEvBpYO6LNp9gTdOmHyWM6q/BvbUyCaZrRG5utJ9Hu/utl5EBJFPBoifcmj5qBvyshSyNhNrttxD32pg/ysw== 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=Ru3TDIkTKVGKaglyCTAFEoUuuVkfNmVrCBZubkA2HV4=; b=IvmLlphK00QAK9wcpm/bc7cAcjKvEES3QqT6krAnKwKhr6mXdokQjOON9SH4kf8NJbuJzlIgONeBkGkd6eicRTTzpKw2dW9aDrk1L9HZjuj3NvwK5Z15wwU5ljyoTFyQnkviVQMJQljUih79CoHvmycLZ6lGGz0NFb0lEIgGSR0pynMxnd16n5Iq0QTp7BRxKL9eQE4XKTXvoUDYLPtM4kyr81F2wbzjMDW5+VkDwEOGw2JUtHNVE3l/Wn4SEjg8fRuDlt3TniJifgu5p1bQ2T0SLLwLjySUwtOD2XRaxg0A9jkq4iGiF2+902lJCCHZe4b/fXbcjZmLnFUqC27Isw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:51 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:51 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 03/27] iommu/arm-smmu-v3: Do not ATC invalidate the entire domain Date: Wed, 1 Nov 2023 20:36:21 -0300 Message-ID: <3-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1P223CA0022.NAMP223.PROD.OUTLOOK.COM (2603:10b6:208:2c4::27) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: adf60763-feeb-4169-01f6-08dbdb336a51 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: X3rmN4cp/1G/HKgY+AYTUAzzNFZaGKTcUAgRXcRKT1oQQG+cQKU2Kc+p2F8vnefbjnXCFXGikVHoA4jjY8jmH5TB3lqudqCvHG7HqR8UXqJN55MOC0IbjR/YqGfaLlbBCY2mGkFXAntkRS7gvW/eA+/Ga8vDjTQKHJOJMDcPEWz+QVoW83X8TKwwBdd1FXF24pRRALz2biSmjPX79tzKCKVihvrPin+Oyyx5uNNPalaj2eidzRAx34m/v6I9A7tEgJaprTgdzJcFy3OrPhwUgTx1TgQHKtPUKAW89HnfokdZGyUGbFEzeqTZPQH3p5RU3MmkrIu/M9TP1OHe71F+VlrjgkCyu+ErFVETooYpX4WSrSZ4H5g2vR7xJLo7Cwc9fJykM6DAySMJHloMN/xy+CRfWn3Q0hU4Zb/ggQYqsMasVnyt+krAh5hPMeNoNS+MAEOloNGgpQN1bIuqxxHLMjXgdh7FvVEm97rTp7QgnOtjPTnsCaZiQ861AX/4iMSfK3BAnQP1/BB9X9UUeb8mEdTz2LQhyz6Dk1ijFDFEwV1UU22SjeG3NrzHmBm7ACZb6wXAtNXN0be664nnMohKWbsM1zoJOCaY8X17G54r1Ps= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RR+RkkB13ecSQymDTi+sk7OzlzlGsBAJ2fWsjT4kSEUWmFPuThDH6Gk5hr4jnGpUgnZbqtgdbfscpdkMGv1gT0O0Vzi67dqldAWbRYevcb5pLP8mtI838q5kymxaj4CA4qB8FvkGhxX7Yjf2eOsFfNMmAUDesveLvygUCs+YVH8vtNhQVvsXc+ISq6k/2QANk2RCRdDlOwa+k/X7UHaD09hQDNn85xmEFT6C3jamHRyeg5m2EwQ8rwV6nCGN88RDfDrVMZJXcEapIHciJdj1l0ZSxC3UJhhytZfhzTR5XeFkNIz7wUucf/TxQNBXCqujrAu6yNE782svayao0EjcbdrmxN3OZIjedMirwdI/6vw8PZfkSZzboyeQm9jUNK6tqG5LT+ICQ0tYafO8Kh4av/jxKkFZVjj37I6S+JQgcLd2pYzrtfcngxUFfz4HghgjF2pdM/ftcsq9vbZCN5Xvnh5lxDs/TIg5fCIrpc6utatEQlCpfOIf6i6UTOxxTUaaMtlUMe2igTT8F7BcTjNTObk5pKz+bcP37bD0b8p4quSh2izAAWKwIOEMQnYvrVYr6PG+jJnWHhMSh2kqIQJ4WrWWDthR5EQxryyeFyx1Gp8HNRbu+EusEKbr5OA4xotcgn80N6kRGgWLQb285r0x6009tUgAcY6Pwxg1xUA4rx5WKf3AKbaZMt0B8ix1lMW9rJkaHcNv1xwhHDxAMfjtRbJYlWreV+1nefMjFrEa1ak0LtKgaLvIwKf5RCVoVd0JV6Nj80l7aQIMI6+TWO9UcKH1oLjMVKEoV5mSb05NBZoY4f0d19JEn2xi9fNsj6EVAzOs9tFwV1eg193Qx88A/AsQILoU9VS9uYHXTcR5AmykDi0zk5z5eBjMXlx6zvE6/0Z0lMNerJXIpId+SmiDzsYK5H28/wJgJPZbH+iqbiRegcrlOGX3wmZ/d0UBtkh5mtqLuqrpyCqVCYDXy/caYptpX0WaoYReY7CCSOOIUA45RPJEtllh6dFUBF6h7alhbfeu8X24EjewDrSf/Z7s6GBscMoR7WeG8v798WrYhxtOeDgmDHtXY6Z98QvasmNKLd2EoUMSsvKfM6EuxBCJ7rxUqGdAYHoCTU1Vh/I1wL+cN9ZtYW00K8Iy2z054NmulVUTxQtUp4WQaMmrKx4djxDjbVPl9CMLLrBw31VrmkR7e9EFLTe0H6Z5/BO/2t45uikACwDwN2h9heaxEsSc3w39O7068gzHvKHhAAwPuudeKGn/I8mdrkXgfxfOQmlxA5qrt28FewZS3TGHhoigN9iH1JeDBGLAlTq+xwNWTVQF+rJS3nhjfVgbNS2IARM9q/I4jPAzzYhVcQGfdEXn6baSPNeJ2e63BV15znvOfhFR14GZPbltylEZfM7/qMcw7L7t+cl0wiTTX9w1LGeJLeb6h3FBkDHKEAE5kBddE907EN5ctuzO3VGYEi0jzj3jfz9G3hpKYfRcTgKYqs79ajIuWaLzLyLrwMmmwN8yAqdUj1MbrhPzfEeqjpK0yHBVqsufLTdX2Je3M1lJrEzHIZKAlNhDwN45AKsKoX82IHlJKjcLcbx4ENX0TljQtxG/ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: adf60763-feeb-4169-01f6-08dbdb336a51 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.7472 (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: b0DQ/7vEr5S2sZ+nJC+DmcQQjeffeEIomtN81VaYGKhODYzx3MUjNxt6YxF79zm9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163724_785250_35600065 X-CRM114-Status: GOOD ( 11.60 ) 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 At this point we know which master we are going to change the PCI config on, this is the only device we need to invalidate. Switch arm_smmu_atc_inv_domain() for arm_smmu_atc_inv_master(). Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 43f5531795f0b0..9a0eaae586f2e1 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2416,7 +2416,10 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master, pdev = to_pci_dev(master->dev); atomic_inc(&smmu_domain->nr_ats_masters); - arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, 0, 0); + /* + * ATC invalidation of PASID 0 causes the entire ATC to be flushed. + */ + arm_smmu_atc_inv_master(master); if (pci_enable_ats(pdev, stu)) dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu); } From patchwork Wed Nov 1 23:36:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443144 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 0A10BC4167D for ; Wed, 1 Nov 2023 23:37:42 +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=JFv6S/of3jwt72G4+MowYHhRLQz6Qrp+wNvHI/hsRoY=; b=MVx3xmCyAhL3pw uiNZZMXSfnDm0XzEAN4fKIK3uJFOFNB8oRUdAT4mKkMFl8LQdCaKdsmtzi+lbb+6kuxiNi8DJUNeq DU3u9KS6DJ46+a2YFFic1ULOMwZebkdTpExMrUFMXR9aYHFxN9yGvoj32RrKqVjGC8d9Pdj1lkHs3 2jqraa9BDAIlWhXB4lLiwV2DyCkyM9xcRlYyS0ZoXFnLtsjMf8r/lFrsTHUYwa6+lPLSKe3w02xH2 2CBhSVhhM+dPVw49bwSNH4qPjJ+bQKL2wCwKVxaUX995l38yCSKCxQ3X0Pmq7PDW3+ZoXpRWWSuCi AnF/Tx1ZvxT3FKBy3Hmw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKln-008Jdh-2V; Wed, 01 Nov 2023 23:37:07 +0000 Received: from mail-mw2nam12on2060e.outbound.protection.outlook.com ([2a01:111:f400:fe5a::60e] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlg-008JU6-09 for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hC51d7Lk0JVfj7JiEAKKuKZiKw4DbNHW+qd4B5/fe8zOYmnCekcT3N/jpuOHGIZVpfZpn9FcIc/xahZTR5rmU0Az179opGCZ3V8KvUjdW9YqdfBtMYbkXa3KnG6zCKpn9WCs3C6XZ95igZt1E1J7iiZpki7ob1r9yutLdb9ybolAAY6SMgx6hoSh/fdZF3lyyG2wF+POxnhG+4uhHsmpPIhZ7iRvt6o8zrgtY07xVyrWcsAZr+nRDey7nJovkQinjY2ITD/dGpPmdOLIRg/v9jX69ZzOrmgldhgM2H2XCZNfPvNEPOMsweAHDW5m/8R5zJDymShjzWfL/NgjjWiQ5Q== 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=6SBEHUNiHJavSDEijDhFP1X9EW/JTL8xJUkX0CCpjUk=; b=FwiKJnWDvT6u9mEfpDHwTcSDP5BoonQSzkhnNt7ZaS+pi1R+AKcEq0VWb4ljHVtaeigsOO3cEwiIjhnK1CLKcRMLdGUA2+skqd1JQPxmT2EA7RCuSrXOubVTHlRBVEeBho62U5wlIIrfztb/KF7etgt/CB5o34kObfgnlKlydLjtKh+0S01efPPbGSiMrf2Q2lveiZ+SAl4hpenTS1ioisrCJY+T+kfD7oTItqMqYC7EohPxW3Cb9I76tXT+3acD4ZExP6sc5kM5c8XR2BOD3SDyf71AiREjfy0q2EDD93gt/UMJV2CIe/RA+fIgDsR7oesW41rWXoqnaFrea2w7iw== 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=6SBEHUNiHJavSDEijDhFP1X9EW/JTL8xJUkX0CCpjUk=; b=TC7ZQZIFuOGDTALqjegahhlOJmdK6xErzFge8mCa9CvOmLSFCsj/5uBigaIeVBetZovmjdavz4hSlCOSFllGjC1nbP1RW1l6jOJ7ojtPEvhfNMhuuy++cVswXHyeNNUFmDaXVmZwEzicVfmXMfRGraywVGvpj/mdrv6UQ5i57JXYrpSccgvqMm8ScMb7tgVDTt5Gd2NAQpfYXxtSk2eFkgl8y1Vfs2cpjgOjEQWY8SZnIjSqzpM8bpHXtbQLVTkHzwdtiGatmPN78p8IOqgICuPhH/aT82RSfqaITDt/mns4akUQ35p6JLcClGBDwNSfLrKlKB8cSFkkREBkZRJcSA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BL1PR12MB5301.namprd12.prod.outlook.com (2603:10b6:208:31f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.19; Wed, 1 Nov 2023 23:36:46 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:46 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 04/27] iommu/arm-smmu-v3: Add a type for the CD entry Date: Wed, 1 Nov 2023 20:36:22 -0300 Message-ID: <4-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR02CA0033.namprd02.prod.outlook.com (2603:10b6:208:fc::46) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL1PR12MB5301:EE_ X-MS-Office365-Filtering-Correlation-Id: 0166c8af-05a8-46f8-83b6-08dbdb33694f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aF4zVTBT9nYtQcqTwmT2rhqhC7HEHbAltxT8CFM48Frwp1thKMR1OqJYOjJ0lvoR6fUQ768QEMXSIZAMfHtBXFHf2oJHMEPhUdfQmkoJdVTc3rypdBQazK3AMd1kiHvXb1v6c7eIlB3pQ2z43PjHv84PZWRfcwgeM1P+82fgNKJfQIW2r53cS6r84rMTJys5/WueoVlYOPfZHbgI/OFKTzHlEMA7Mt0qYoLIVlOlPrEdSjs08YnhUXZTVsx5ibMfMOl7ODuLopAGoTqWHYgM41ZHyy0MO9RtDy9guKWmUxu8yRGZzSkordxwNWZcKte0llEH2eXWqLDTpqxNWROMFCD5aim82f4zJX6t17s6ODMhCwZuUkadm/yXMnRYzGtG1b3JQ20X0QFRvx9dKx51coZgUslS+1Lunn4pKXAwo/j+4BNNIhdzJP79o1SeURYPYJrctWkrznlVGCbTGbgHmy3JOAZgEujJoXPcLS8EsvaM2caKjqWvTLaS7SVCNrpv8nDz7K+ZzYQl1d3qOuoyd0UcK9Fv2i4q+1RE+BD3Wb6sLZJD0O23FfqyeWSQtX02yo1vzZ0lXzNsw8Sw88Ud0+xt0QveBaZmd5+6R/iJJrBlxZgVGeb2wp4zL9dOVdPuKyr1mL3sBnwQtoAa2UPZ/w== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(136003)(346002)(396003)(39860400002)(376002)(230922051799003)(451199024)(64100799003)(1800799009)(186009)(4326008)(8936002)(83380400001)(86362001)(2906002)(38100700002)(8676002)(36756003)(5660300002)(41300700001)(26005)(66946007)(6506007)(6512007)(478600001)(6486002)(6666004)(54906003)(66476007)(316002)(66556008)(110136005)(2616005)(107886003)(4216001)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: c4TiSmxQAjPpzl/HPiACInZzn/cFHbQVbQ3bhG74PsAzBob6Z81V2nuhPvdtnMAhetYPHofj9DJMVyRyT5nML1lhbBLPZrrkXau7Bop68+VuF04hcTq4kZ447PGDINBeKqkiKarQcWRY8qRbUeWm14yXQGCoUGHMSb5AfPKPu8NSvUX709KD6K40jXo/yk3Va+ufDonImMxbtzM9hmqOF8gYoeze0fpfdqaLj6xIGStO//71NxH1Kk3nxYfyL9gOfgD9hgSVT7bxBC0FMC7PSzZ1sqpof6jDwKqFi5z81B9Y1ULsqCNkfoXQ+nyWP6zB648fCaSvvBz8w0OBrVshiuBrBNetq6CsZGJL9pqXKgjFi0oTFt2x7ShtXfskZ5PfbCCPbW6/4R5yB4ccSb4ZPemCfsXR+xRcbBbPaXQgOANYupVl2x3mhJ185cBbgZMO8NrAP1AwIAZnV2yT+mTe0o++5shMSWj8/JFxeflbI9Cb1t0prw20AyK2Uz3Zo1KEvsgf6rKUOt5QkBpe/+9mqczDo9TQwEOwaFPg1WPnImkHEeYoBjkPgsP9c/uFZwWOyzyoZLLxqAoMRUrOhkK4t/OS2BROM5ptwQ7Vv3vWj+ItU1taQxPqqRLzC5ijyBSaIM9oR8wXWvUAsG0ypViB5+nIsUA/yXifmkKD0d4ZP14KyUd2s3I3fcMoSyONkcdtgs3piIP3n1IuEQ+5Qk2C0Uh7bnnb5uLqhfT5b4UePBjikteYx8nSHgBhLfSv4oAd68EXoeHFpElj/NcO87agkXC5y+Kx5zVpnG1iu4+UK/NfKl15s+5NLpTBIYqIeOXSzrPZRYMOrD6Oukgsxch30cgAHjEW278v4UfH1vxEmSEAwVvc3luKjef+lp00mfr9LZlv0klzrEkOInRCUFQUw4qYx8uOaUmN7OIUnx3ZZ7HrQde6t/TKqBC0ndTofYqf3BbdQki5C5Rqi14O/R0Zbe2k5GRafHNEPXFjkiuyxrRfbs9NRYFIvAl+cx5qQlxHzIxcCEnHGnxGVdpWg7qLaKh6VdeCFZi69Yh6q3Wocf0hwEh6B7eCm6/l+AimWlXfNp4ku9Qa2z6u0fUfhAxlqr0SOZWky46cqeGIZcDY4FY3qWVg5n2M1dig2xOf8Z6nsC255INZKUV4DWJzsvMTHCx85OVsHglCzTrRjJcQEGkARFqBsgWH6XoHtiXeN2hXgu59mOWr0BI7TY3SQ3cfLfo9S6cJhf/ZP4PpvKImzrHmo+H+GshJxTonMYifJ7QBvk66cFi1N3XDwzAkORAC7ym8oSLp7xHRBwb4ebseHhdexQbfhtjzjWyAINqNfkcXjnXgK6puxfm0LCNM6sglQPyXzn962z+hpauy+f/rd462G6IPOvH+4FQrzQqWzD8KHSoLbucS/9+p1OIdJXji6jabKWVKY9wK5cQ5M9LlLbqv9IGcHDy+3bfrYYkquLrC1tQerZFq3RnhDgCvs13Q06uOx6+3k6jNJI2VWbELMgq5EqBD0qjUJUGRl3ShXxX9wukxvGUCZMmxpT+ENcT6L+ZNG4OB6gIhTBrnA/hTUdadb1qoNswu7IEtxdc1W5fg X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0166c8af-05a8-46f8-83b6-08dbdb33694f X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.1203 (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: PDmIfkl1TMapqFUrP53fuXWI+aDbfIEBepvesG1mnNKmkC06ke3H75cjYmur0RNw X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5301 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163700_106545_B830E95F X-CRM114-Status: GOOD ( 13.92 ) 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 Instead of passing a naked __le16 * around to represent a CD table entry wrap it in a "struct arm_smmu_cd" with an array of the correct size. This makes it much clearer which functions will comprise the "CD API". Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 20 +++++++++++--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 7 ++++++- 2 files changed, 17 insertions(+), 10 deletions(-) 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 9a0eaae586f2e1..80cbedcf33aabc 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1118,7 +1118,8 @@ static void arm_smmu_write_cd_l1_desc(__le64 *dst, WRITE_ONCE(*dst, cpu_to_le64(val)); } -static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) +static struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, + u32 ssid) { __le64 *l1ptr; unsigned int idx; @@ -1127,7 +1128,8 @@ static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) - return cd_table->cdtab + ssid * CTXDESC_CD_DWORDS; + return (struct arm_smmu_cd *)(cd_table->cdtab + + ssid * CTXDESC_CD_DWORDS); idx = ssid >> CTXDESC_SPLIT; l1_desc = &cd_table->l1_desc[idx]; @@ -1141,7 +1143,7 @@ static __le64 *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) arm_smmu_sync_cd(master, ssid, false); } idx = ssid & (CTXDESC_L2_ENTRIES - 1); - return l1_desc->l2ptr + idx * CTXDESC_CD_DWORDS; + return &l1_desc->l2ptr[idx]; } int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, @@ -1160,7 +1162,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, */ u64 val; bool cd_live; - __le64 *cdptr; + struct arm_smmu_cd *cdptr; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) @@ -1170,7 +1172,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (!cdptr) return -ENOMEM; - val = le64_to_cpu(cdptr[0]); + val = le64_to_cpu(cdptr->data[0]); cd_live = !!(val & CTXDESC_CD_0_V); if (!cd) { /* (5) */ @@ -1185,9 +1187,9 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, * this substream's traffic */ } else { /* (1) and (2) */ - cdptr[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); - cdptr[2] = 0; - cdptr[3] = cpu_to_le64(cd->mair); + cdptr->data[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); + cdptr->data[2] = 0; + cdptr->data[3] = cpu_to_le64(cd->mair); /* * STE may be live, and the SMMU might read dwords of this CD in any @@ -1219,7 +1221,7 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, * field within an aligned 64-bit span of a structure can be altered * without first making the structure invalid. */ - WRITE_ONCE(cdptr[0], cpu_to_le64(val)); + WRITE_ONCE(cdptr->data[0], cpu_to_le64(val)); arm_smmu_sync_cd(master, ssid, true); return 0; } 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 6f62184eaa2434..24a77e0a97898b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -282,6 +282,11 @@ struct arm_smmu_ste { #define CTXDESC_L1_DESC_L2PTR_MASK GENMASK_ULL(51, 12) #define CTXDESC_CD_DWORDS 8 + +struct arm_smmu_cd { + __le64 data[CTXDESC_CD_DWORDS]; +}; + #define CTXDESC_CD_0_TCR_T0SZ GENMASK_ULL(5, 0) #define CTXDESC_CD_0_TCR_TG0 GENMASK_ULL(7, 6) #define CTXDESC_CD_0_TCR_IRGN0 GENMASK_ULL(9, 8) @@ -591,7 +596,7 @@ struct arm_smmu_ctx_desc { }; struct arm_smmu_l1_ctx_desc { - __le64 *l2ptr; + struct arm_smmu_cd *l2ptr; dma_addr_t l2ptr_dma; }; From patchwork Wed Nov 1 23:36:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443152 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 04330C4167B for ; Wed, 1 Nov 2023 23:37:57 +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=23XHuwepus32aniatAc18Y8Y7v5PSgSZkCZobcX6iZc=; b=k6qL+v1mDfzlib Ok6Ten9VErmYNbQWRFR7dXAlMJxHJtM4fMaiFQM9S4Az+BC7XRH36c5OYqTw1dHO+QB8oszzEarXX mBIh1yY+QlvNPJYvOdOn3ZJYOEJi52iVfy1ytyPg51hpY7GaBQnGQ2kTU7clTtA/emW01b/+G+Wdj jt5MFiXxi6B0Zy1CjFHJtAXMn6XM6g9HbLRFHfFMeFMnP/hwZKL1PUs58WBj2UBZedW4Y29mMffo1 tpOJpv2y6fH5vSVQB+w6Da5qqeS//mFrqeJB78M3Yp7jxuVbHcDsAZlktwG8iZ4dk8hpvDUvVbrOS 29AiuM31TmGEKU8b+q8A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKly-008JsJ-2c; Wed, 01 Nov 2023 23:37:18 +0000 Received: from mail-bn7nam10on2060a.outbound.protection.outlook.com ([2a01:111:f400:7e8a::60a] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlm-008JW7-10 for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MJtkP63x/BVP1HQfm9bJg0/jn6AaG48qljnQlgckaMQMSHuF2hY+7tJ9LjIwsGL9Cm4tfCrQy1CeZiXyYIkPLOYKP9hfjNEcYH53SCY1QF3UuFy8KJ/0VW+scyEpSUXoc/LERgVusSursPi+s/7Q3jk92YqsyS2iuR0B9Dodu5iN9wtlTXw1f4GpcTZ1ajNmvp/3xLQHrz9nWH2m50Txmlz7cT7gK0mTTv9t36iVSaOi03/c0Aml4vo79IvXt36pguOyiPoZRr0NvTwq8ft70u9cANT1FEHJUTJdH1x064cdruhkiLslqa7fsdKceKzAs8vXADeQx4HjVkZZfDlAgg== 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=n6j/Qp3HDuv0CWkABHGS55Q2YSdU9ZL/WLYA0tHmH30=; b=TTigNVWQPBnXILFp/cO3qBly+gXSWLKlAxROPBwjJjCmG8j4ldeZtpecSYeI3S16pJcIZC5ZEvTw63V02ZUd5uQoy6v9M+hR3WuxsSkEYLkjs3wKBDVIbflK8YuvC+REyi/kobxFGgNKJ4dc94z+nfH1DQyp06w5O8LkFwRl2hai6rXx45voMEbabNBbb6kTEB3iA03Fs8igkYJD2MBjlTf80GHhCdU7XKuaGEIewsNtDg9gwa/FAhunIzT04/KSOPu1N0Nwgm1WEWkSsBSUfmjjxBHfLcVUrrsGhDp4Zuj6rABw6pfl4kPjcKnDDY704zQDX5ryCiXNp77urbUK0A== 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=n6j/Qp3HDuv0CWkABHGS55Q2YSdU9ZL/WLYA0tHmH30=; b=eJSWPHTXTGFwrlPQywYIgSaJ5nKRHT4viLOKR5w7p3F/y0KcKDiAYVfW/jeLUm4OebKDTu9/yBqgtux7XdZbH9OgxKNDHeZHEy0oRLUZi8sJlWxye2GVSBWxlvpQIGVdcwBRk0HTcxoBAKnBsl7NZO1If9V/KOGwh6D8RnEee32xF2DgxSy4VXYgcjbUCsgKxAUaD81bVCFNQ47ozK3eR1FIUEjJDS3XirnOAbKXapTQyQnsdYk+7FiVeRWFRRBPGGd67GUYtHgLSg+At+QrvT6sfPcx9Lv3wiPVqddTPBCU97g8nAwpFdlngPDmlyJMOGi3KG3okWIkbdXFqfYukQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA1PR12MB7664.namprd12.prod.outlook.com (2603:10b6:208:423::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.28; Wed, 1 Nov 2023 23:36:53 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:53 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 05/27] iommu/arm-smmu-v3: Make CD programming use arm_smmu_write_entry_step() Date: Wed, 1 Nov 2023 20:36:23 -0300 Message-ID: <5-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0016.namprd17.prod.outlook.com (2603:10b6:208:15e::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB7664:EE_ X-MS-Office365-Filtering-Correlation-Id: 4ab649cb-5c6f-48f0-8746-08dbdb336ab7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /vVWCikm/Oo7ZV2PtQGmAYpDaiCT9hrMU/5G0cqN4IMw/zNFlFHg6sq/X8XkVQ9QvG9ouX0g4mEDLdM4HWuis2thd4xbzoKByDY2eu3bMHz+b9piqLSMJVy4mSe5sR+rCsWBpTycmAotWG7GgI8PaJa+XQEzLcTaz7fUjVvP7NfZymxYAM7GXH8085Odh+CPe+uyk/niw4tcW2SF72/dQtFVcKIGXjKl/6O2E2ZS4wN3oWFr83Bcrp4TPpUF5XatQtpw6iEqaV3T21IOHaOQ/uLwKq5ybSnoMacg9+KJ8oACLjy0TF92LSzfNCBybIG9rXNxLewy7EdAjLjZ3/w1chhYaf3EejGftglw0vUyosqKmA/U6wt142i5Ysu7e/EtpYU0y9AMzN0JkuC4NalZANQNZ+iegFvEcV89asc8By8XELWojhHyVbGtbwEd1Sfkb4OyinnhQvjs6HFJn19Jf8Nw1ry9lfYByd2pJnxpGtD5aQU7RglwBmHOU9rqkRRdjDiDx/M6gdUpbMrdI9LBMeAh9tjmJVR7Zosyz+nUdDKDAMFuxsd777kRPy/1rrwn69BhaijWzD0cjSH1YqZjmTKKb1t39nznr4GTx0d0Qrs= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(39860400002)(396003)(366004)(376002)(136003)(346002)(230922051799003)(1800799009)(451199024)(186009)(64100799003)(110136005)(2906002)(86362001)(41300700001)(8676002)(8936002)(4326008)(5660300002)(36756003)(478600001)(6486002)(6512007)(6506007)(38100700002)(6666004)(54906003)(66556008)(66476007)(107886003)(316002)(2616005)(66946007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1OYi+wjKRuhP3f38GC7IhET78I2nayP2xPiw7vgYgFqh50TkrE1GHZaW5eTkPpWNP5pU+npoTykq1EwPwSSnxyE+oF9q1XGCygH6XfeRRY7uGoR4g2OVC7IDvMPnTbRxWp+FwnbwBvJTaM4ybhzW9PhXJO86Pg5Ac/r22dKAOVzRWNcYVJUN9NU01iltCtN/fL0OJx/U/aCtunfKssIvI+JkEXJOW2SoGHJ+7FBoAiQiHPPVaW+zz3kE5XpLspt7SWzkjRsZ4cMHLrfaMeASHw2zAinvZObyql9ogPY0lVNAaldRMEgmf09Zn7Ntv9iP6TrXy4rO5Zaaf0sFHEiART7WaHGY1WU0kzoVFvh2Ho6DFNNH93Wvty9Qq3vmKKimyMun6Z/hGE6r72TnnP1BT9ncsltgwxVYRBD+329fU4KXldzZ9NhDraI9wAulhvXM1Z7zomPWSxdAir+7SBED2Lwe5WJ8PWCONPyOtpZlB+6hmCMw3WWEiTOmd7GUQEiAUW2RfCt76seNogpQhWzsR2p2ekXBVRlXcKhQsrZeq9tO+fHHxFJrKbr35pE0sqopG9IKNB37MaQF6RsOVIkjO+pqe++njP6RIxThhCqCYK7on1cMRzxUZKXBMGlFHcf+0X9i87Aewo08Utt1vrBWaEaTb9Ui5u0LWODbjdZY23sKzeoVNXV7ypL0ekyOlNumPn1PihLKgUQH/RWHCPtXmlXYP4jYxUg0gRu0GvLTOHTWpF6bDVOwYfDZ5UrPdkfhM9qvcshYCNRhXrXz1oEy33WD+xnxqVUUtjG2/qK1S1prlV2y9BYWJud4EEtHgpoUFtB3OSSp9tQOrquIiKXevU2quC8ihluKS21StjS6BNLlOjEmVWfP62IyULpiqSl6Hz2MpldtN/RZLvokoCj/5rigK4/k/t33w1LQr3fkccQcXIyjDVW7zw24V1T0Tc3akvXU03m+pYj42w8NwnAIxFPt2Y3BjpupdFLLsOFxADiPF+mNTSJ9KJ3EB8WZaVz0NbkrJIKFEWHxLMZQOCoimqYLuB5pcoO3MkygMvAeyWP+tW2cNvfo4Ls+aYqboN1x6xuLlnRKX74HCkueH0sLhnzr3IMHR0xVQFJeX4ctRd2of4al5tRdDyMSq4SS8L28DF+jrRZDIDPV/5S4E+J17iRjFynaJOSi4qrpUP0l7nwlT+XoAYrEFmZNsv3WfTrfbJ879yjLdEueMNT3jGbvtvllCRYoQLdagkHzJLIM6RBvmQ7NVV9pDRJ2HF1yW63XIX9FToPnOimpgnpZlsPwwpYv8rpGeycUrGtNzqbFpJd8FM4CysO+0H5BP/8OiY8W/+/VHoupyqxH6DJOyy5duxLUiiZBoOCP/5HwP2vfBDc3FYbtYXaWEnSkmNwXQOOHGLDI/iC8BLaqBuSJg+jIanjOyUa6Qd9Cnm5CmngQAcATY/nE+fLPOAsz0xxm6n/9rwkHKElqS2bREvnRnOwtzk2212Q9rQGsRQExgtefhpUtDPDkK90aBKhaEVqOR44MnU2xkOx22m79hyZD9NG1HB4BqWxvBawL8uBkUAFh//8nXvy8/SE5oSaiotq7pmcL X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4ab649cb-5c6f-48f0-8746-08dbdb336ab7 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:48.4535 (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: Bh6AlGE1EdSF7OqBi9YQB//8h3rKfhEIAX2J1cX6k2yequ/I0cETIzQw3+1AQMnD X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7664 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163706_423576_968C84FB X-CRM114-Status: GOOD ( 15.65 ) 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 CD table entries and STE's have the same essential programming sequence, just with different types and sizes. Have arm_smmu_write_ctx_desc() generate a target CD and call arm_smmu_write_entry_step() to do the programming. Due to the way the target CD is generated by modifying the existing CD this alone is not enough for the CD callers to be freed of the ordering requirements. The following patches will make the rest of the CD flow mirror the STE flow with precise CD contents generated in all cases. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 79 +++++++++++++++------ 1 file changed, 57 insertions(+), 22 deletions(-) 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 80cbedcf33aabc..042bcc27ace777 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1146,6 +1146,55 @@ static struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, return &l1_desc->l2ptr[idx]; } +static void arm_smmu_get_cd_used(const struct arm_smmu_cd *ent, + struct arm_smmu_cd *used_bits) +{ + memset(used_bits, 0, sizeof(*used_bits)); + + used_bits->data[0] = cpu_to_le64(CTXDESC_CD_0_V); + if (!(ent->data[0] & cpu_to_le64(CTXDESC_CD_0_V))) + return; + memset(used_bits, 0xFF, sizeof(*used_bits)); + + /* EPD0 means T0SZ/TG0/IR0/OR0/SH0/TTB0 are IGNORED */ + if (ent->data[0] & cpu_to_le64(CTXDESC_CD_0_TCR_EPD0)) { + used_bits->data[0] &= ~cpu_to_le64( + CTXDESC_CD_0_TCR_T0SZ | CTXDESC_CD_0_TCR_TG0 | + CTXDESC_CD_0_TCR_IRGN0 | CTXDESC_CD_0_TCR_ORGN0 | + CTXDESC_CD_0_TCR_SH0); + used_bits->data[1] &= ~cpu_to_le64(CTXDESC_CD_1_TTB0_MASK); + } +} + +static bool arm_smmu_write_cd_step(struct arm_smmu_cd *cur, + const struct arm_smmu_cd *target, + const struct arm_smmu_cd *target_used) +{ + struct arm_smmu_cd cur_used; + struct arm_smmu_cd step; + + arm_smmu_get_cd_used(cur, &cur_used); + return arm_smmu_write_entry_step(cur->data, cur_used.data, target->data, + target_used->data, step.data, + cpu_to_le64(CTXDESC_CD_0_V), + ARRAY_SIZE(cur->data)); + +} + +static void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, + struct arm_smmu_cd *cdptr, + const struct arm_smmu_cd *target) +{ + struct arm_smmu_cd target_used; + + arm_smmu_get_cd_used(target, &target_used); + while (true) { + if (arm_smmu_write_cd_step(cdptr, target, &target_used)) + break; + arm_smmu_sync_cd(master, ssid, true); + } +} + int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, struct arm_smmu_ctx_desc *cd) { @@ -1162,16 +1211,19 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, */ u64 val; bool cd_live; - struct arm_smmu_cd *cdptr; + struct arm_smmu_cd target; + struct arm_smmu_cd *cdptr = ⌖ + struct arm_smmu_cd *cd_table_entry; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) return -E2BIG; - cdptr = arm_smmu_get_cd_ptr(master, ssid); - if (!cdptr) + cd_table_entry = arm_smmu_get_cd_ptr(master, ssid); + if (!cd_table_entry) return -ENOMEM; + target = *cd_table_entry; val = le64_to_cpu(cdptr->data[0]); cd_live = !!(val & CTXDESC_CD_0_V); @@ -1191,13 +1243,6 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, cdptr->data[2] = 0; cdptr->data[3] = cpu_to_le64(cd->mair); - /* - * STE may be live, and the SMMU might read dwords of this CD in any - * order. Ensure that it observes valid values before reading - * V=1. - */ - arm_smmu_sync_cd(master, ssid, true); - val = cd->tcr | #ifdef __BIG_ENDIAN CTXDESC_CD_0_ENDI | @@ -1211,18 +1256,8 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (cd_table->stall_enabled) val |= CTXDESC_CD_0_S; } - - /* - * The SMMU accesses 64-bit values atomically. See IHI0070Ca 3.21.3 - * "Configuration structures and configuration invalidation completion" - * - * The size of single-copy atomic reads made by the SMMU is - * IMPLEMENTATION DEFINED but must be at least 64 bits. Any single - * field within an aligned 64-bit span of a structure can be altered - * without first making the structure invalid. - */ - WRITE_ONCE(cdptr->data[0], cpu_to_le64(val)); - arm_smmu_sync_cd(master, ssid, true); + cdptr->data[0] = cpu_to_le64(val); + arm_smmu_write_cd_entry(master, ssid, cd_table_entry, &target); return 0; } From patchwork Wed Nov 1 23:36:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443212 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 2E6D5C001B2 for ; Thu, 2 Nov 2023 00: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=6j8SoRW4e5W7lxVQ8ev+5rbStQmHtXAO+azUXhliPPE=; b=UQKFpBXK6gRF2s 2jLQTjS+m9sjQQgnbg1SUK6bdscshRNBWI3qNbEPjgnS5Ri1d7ZkDU9rHQ6LcFjrIpxZ48VAHo3Rj NnvK7GZnov4RDLbhEe0VOAzs+hdt5yxm/ao7U/jXW9wd8jWV9GDM07kj68JKd0VsvNMbAcek/NQCj FIh1z/qWgjLn2Y/Z+3hrmV3bYYVJ348Y23kxv+hl5ZbCnaXuA7f6mMUhKvoh7VOsndG4JN8GdcQA0 pcVC7WrO/dVDsLYPN/fG0eqGYp3AZaBG9/VVTTRV35JU/bytP7mNr7cf3xstPeCktr4IckJ48sc6m QVEF8MWQSrW5ntSXkStg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq5-008Pyh-1w; Thu, 02 Nov 2023 00:45:37 +0000 Received: from mail-mw2nam12on2060e.outbound.protection.outlook.com ([2a01:111:f400:fe5a::60e] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKls-008JU6-0c for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:23 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OOiHCCcLSaAbuop31nphelQhdm+l3iVnqvF1WIS78DJaK8FCxvQm9UYutcSLVD4pFHtt90Oj5eCKpY3x7kXpRERKwWFGUJ34Rqmy+pvorCRut2gdxKseyA6PU+ujBucWvlC6nHZmvkeyeBCY22BF1pK6hnzEXHE/Bonl4r1+QZ24B8pEKwjKEDaZPIWr08mRY7olBHRSxasAcmx93hWUHVlImbluGV9nwz0K0jAt1jPLTGfU6hl6eUsm+7z/WLaQu1rVjm6ThU45b1sLw+jI/Vxnu9kYlRmId/12uaJGLFmEdq12zU4T8skH1/iXnY5WN7KxJ+50NypW7QacwBQlBw== 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=7LS2bP3r5DGG/DlTKyEtJgI/0axTui5/QeLmFo7u1ss=; b=GLiXosBB72gLeQ7jARHAja67BRQRltxP5bWaAgZtAEpfA68N51gjkU68ZdoeRk2cyMj4GhZFbqBn6CcpzkAHkCExIlBd31Ex0mgTcGAFXIVITK3NfM8BUFpUnG0YbKT3tzFNxYXn0aXbhcxhjoS1shiPgxwAwFCD93FBMDj4rbK2ZbUVueLMoTqh0ZOtzNUJePask3vS/oqv490EFjTuzMFltT0hT0i+0dQpkStdTpeKzfZEp4JBXqqfReOwcx61rHVPJZR5FtFEcBL41jiSgFtegVNE3/908B7ItVVTIBS/Cg81KuQKtJbf0PPblSA8XPPtv7xGaZO6KofY8RR2Bw== 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=7LS2bP3r5DGG/DlTKyEtJgI/0axTui5/QeLmFo7u1ss=; b=EwSpv/3XW38c30saDXyEKrbj68L44lCSzpmFeTPt0evZYVIzOKIHboEvktf1XQRv+csNo78ffmay1HbZI/pkB1mOAKTdCpIR33uhyw0dobtyBQFdbMmaG1/t/BqYqnDSqedvK0kDl6ZyiJIGWWj30IJcw+0e//nPX69BbwZRVb6tApCqwyPdC4k/R9Alns8sBQ2DSwlov2R7sah2N3vGARmmtLUxcfcjp7a7fOnIATI/mbnJ60GHS9FRKHvq6mbVZ2MLlSTgEOn8LbmCLjY6OyciYkU5rI95RwaigU1gr/DDBmL83jirThwfBz8HVWF1EGaaHh1nvxdQkaii2qwOeQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BL1PR12MB5301.namprd12.prod.outlook.com (2603:10b6:208:31f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.19; Wed, 1 Nov 2023 23:36:47 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:47 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 06/27] iommu/arm-smmu-v3: Consolidate clearing a CD table entry Date: Wed, 1 Nov 2023 20:36:24 -0300 Message-ID: <6-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1P223CA0014.NAMP223.PROD.OUTLOOK.COM (2603:10b6:208:2c4::19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL1PR12MB5301:EE_ X-MS-Office365-Filtering-Correlation-Id: c627c21b-4eed-415f-0bc6-08dbdb336978 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: X4KbSG7wLEKkMA9/5Gn81sveZ3jOjFTQ0q+IyMyyartg404w5hA/4wG2kggoyEkaRq/gf1s/P+kW9WwGk+bvGTEtl1OfVSW37Kmqyg59hC16Hy2LMgIxO4npfoAUFvlZVT5xs7uVGT9FHUaYzlOI32ew7AvumaeUwG0DQzMAY61Lr2wVBHBU8ci5obiEQmdeLr/BqCdaaWA2SP8Qa4vRxnL4UMFP6Six0kBvIsEIxbgF77NfSB1LR42YJEnpMzvzXRSe4tPO/vamojjvYh50wB+Ct7ygY3Cq0OLa77hx1yCmIXFkYzvBxEsYoJFEbPEb3O/hc/121LOgoaOZ7J7jKGAkUXGdN1hPpgxfUaS6OCkYhM6WaBWr3J9E0yhGMpKHaMlnHkDt6B/gk20q6jP6XFH0fK+s7SLPs8J/sELHBMTgTVzdjr1eq3ZFWj4Gwfk7NK+Zt0xvlx2wIL2yvbtIaWlmbcVg5AW1z2XKMGEjZdBpjh3KOivaL4hSKmYNPD0u9ieWvaofE59izoMn48d+Ms23cexWho2rLkJhz1iOM0ZIq0NyJRW5SQPJMyjjRbqeJ+GtZIY+QSRTslfgbcTZ1ZXkhzMEblP8FauOA9SXHUY= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(136003)(346002)(396003)(39860400002)(376002)(230922051799003)(451199024)(64100799003)(1800799009)(186009)(4326008)(8936002)(83380400001)(86362001)(2906002)(38100700002)(8676002)(36756003)(5660300002)(41300700001)(26005)(66946007)(6506007)(6512007)(478600001)(6486002)(6666004)(54906003)(66476007)(316002)(66556008)(110136005)(2616005)(107886003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5INyJrOHruc2U+Y/Gy8xlkSMPthLIH3nheKLbiOFIjNoPGkTXWkI9ODj9bACVMiAB+dCHeG4KlKD3312Rm6xJx9oMcEOhizzSaDX5xFFqr9OdKh2YCUu67raAR6NK11BsQMuVECS6LztEhLk2E1m9N5/YEJYcxJpE3B54O3lmlOGVQ3tcHUmwqDoufJoN9yxvHyzOlaiYZRBKyL9Po6nDfBUfqMqHWZearTMEQPiqxuoTHqeCpDT7272Y3ibYf9qVmJUt9U7U/GGuSMACz0QcOEgTKOuvHbwrFHvXQuZg9UIIU0P7+Od3AnNtTD4bwxJ9SxqXUE6Lqip/Uo0vGvrCY/iU/0KhX9oNYMcAlKEerwWkr7tcCAzNQQ5RhatplGDlFey1YiYDY5yOACeNrvvOo33joI59YqwwS5/z8yXDkoKacXRVots6CYOhoty/W6h4UUZ8XBo7k/PVe0tiyjhqa7GnJoa2ssoZIVwWtOs97bqfIzlg2ZUwhy3oEGD8mVoro1KztJlJC5N/2dL8v0fUujmXd4hrmiGzHB+iYzqbgPzRlslak2lPnJjNuu72cCyukISJpNnlHlUuSNjRW59NO51jgb/KGr4tBBGJAvqvjpjYETAhZA2IVbfcpf/V35Rnr3QR1T4T0yYS53y2/Pqwq80b7yj0BOBLtmm0iHvoDDWeoE9zwZBX+86rnRmuso9PEYSEw0WaVLWvBS6uGtrPpqmSZrTlWau8N8dEc5l9L/Ko3amAspfjk4cZ5cGqGUPh8n+Fw/S1h+4D9N5+gTkqprqnGap087BgKn6aCDyHn0vwk5qciVIZDGiJjAlR5HPupKHLqRjwbRn04u1FzNhX0ICMBRaYEUCGNFdp8AdI9gPVsfHtQG2TTV6ZSmWyTUG7kJe8X0ouSkT+d9SSD3H5RfzDNCOIgmnpbJnS1wRwjfpQBu80EBxneBRalyGJ9NN7JzdERkge5kX85aqUpUjG66l51Kwyleh/CpHqDOWR0u+TDnKNVhlTgs+IWO8BtWrFOl8CY/tWw/HsQkwwSTo5jTjzarPLppnZe8ck0lsKFa/nB6cNa9UwUmA3P7BIFDe6QfsCjJnFXgFrbAzH5Oq7nsrMPVMZGjMS0lw3uwC4ZS6HjWy/ic46R0dimj/h4DLS/cNaTB/Ify8M9FQrZIh2c4NLth8f4if1jS3NVbA+Mxtd8mZECmmy5/CviqFIY6F7KTQoi4ySvKn8NzP8QyUw9G4Fy5w4WGvIS9tsxIKL6wi1JtLYXpPnqszFHPDDWg5eZ87KDqiU5rX7tcKqqgAZ2bu+dV484bZicGRVP2WWbimF3C0VdbGl8NCa9PJcAL/7zDw/vVPrDfjJiN5RHjXjN05I5VtVK5e1YqFRGQ88iY2qQ3Ng2GQFhNyVsi+JpQ+cqMWhwGJ5GX6YE8ttPzXLam5BoLs3j+zoFVPuHWLtDs0I8rc5GeHWaHD8QCrijNziz55n7NI849Eoi8HDSyb4MwkHcotYuDKZoxISyBx7zjv/52SltN0GL1OcSU259TdAkk8v4rCWd0C8JC/Q2XhViE1Wp79Y5WR1YaB7ds/kJn+54m094o7z4bd6lRPN8Pt X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c627c21b-4eed-415f-0bc6-08dbdb336978 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.3406 (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: 6v6f77zcV/wg3Rrtf1OLDn4D6HNyByalLpHNiPfsKJQGOAo5b2RuLF1hCcNPbue+ X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5301 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163712_254222_48BADF6B X-CRM114-Status: GOOD ( 12.84 ) 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 A cleared entry is all 0's. Make arm_smmu_clear_cd() do this sequence. If we are clearing an entry and for some reason it is not already allocated in the CD table then something has gone wrong. Move the two SVA flows that clear the CD to this interface. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 9 +++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 20 ++++++++++++++----- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 ++ 3 files changed, 24 insertions(+), 7 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 ee3d148aafa26b..521bfa18879f90 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 @@ -328,7 +328,7 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, ret = arm_smmu_write_ctx_desc(master, mm->pasid, cd); if (ret) { list_for_each_entry_from_reverse(master, &smmu_domain->devices, domain_head) - arm_smmu_write_ctx_desc(master, mm->pasid, NULL); + arm_smmu_clear_cd(master, mm->pasid); break; } } @@ -352,13 +352,18 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) struct mm_struct *mm = smmu_mn->mn.mm; struct arm_smmu_ctx_desc *cd = smmu_mn->cd; struct arm_smmu_domain *smmu_domain = smmu_mn->domain; + struct arm_smmu_master *master; + unsigned long flags; if (!refcount_dec_and_test(&smmu_mn->refs)) return; list_del(&smmu_mn->list); - arm_smmu_update_ctx_desc_devices(smmu_domain, mm->pasid, NULL); + spin_lock_irqsave(&smmu_domain->devices_lock, flags); + list_for_each_entry(master, &smmu_domain->devices, domain_head) + arm_smmu_clear_cd(master, mm->pasid); + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); /* * If we went through clear(), we've already invalidated, and no 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 042bcc27ace777..790e7911714dc8 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1195,6 +1195,19 @@ static void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, } } +void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid) +{ + struct arm_smmu_cd target = {}; + struct arm_smmu_cd *cdptr; + + if (!master->cd_table.cdtab) + return; + cdptr = arm_smmu_get_cd_ptr(master, ssid); + if (WARN_ON(!cdptr)) + return; + arm_smmu_write_cd_entry(master, ssid, cdptr, &target); +} + int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, struct arm_smmu_ctx_desc *cd) { @@ -2622,9 +2635,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) case ARM_SMMU_DOMAIN_S2: arm_smmu_make_s2_domain_ste(&target, master, smmu_domain); arm_smmu_install_ste_for_dev(master, &target); - if (master->cd_table.cdtab) - arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, - NULL); + arm_smmu_clear_cd(master, IOMMU_NO_PASID); break; } @@ -2672,8 +2683,7 @@ static int arm_smmu_attach_dev_ste(struct device *dev, * arm_smmu_domain->devices to avoid races updating the same context * descriptor from arm_smmu_share_asid(). */ - if (master->cd_table.cdtab) - arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, NULL); + arm_smmu_clear_cd(master, IOMMU_NO_PASID); return 0; } 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 24a77e0a97898b..a8e7574ab8e154 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -763,6 +763,8 @@ extern struct xarray arm_smmu_asid_xa; extern struct mutex arm_smmu_asid_lock; extern struct arm_smmu_ctx_desc quiet_cd; +void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid); + int arm_smmu_write_ctx_desc(struct arm_smmu_master *smmu_master, int ssid, struct arm_smmu_ctx_desc *cd); void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); From patchwork Wed Nov 1 23:36:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443205 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 27601C4332F for ; Thu, 2 Nov 2023 00:45:42 +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=Soj9Iv7ojEL51bieCeoFb11Gba07GqkaTAMWtMoO7PU=; b=42hxP+vFUoDeN4 aFG/YVP2dSN72zoR8LVm6MFX4HeSj8o3XRmx0zF0A11FtMLVxO63t23vkUk81Bj00l1TA8bHujnSI bPRBtb7yWNUQpQq/Fj5eZjjavXg+MWx5Eu64JvqrUtTiI2OShGVh27JlqQdicN4xDhitNonfrLnfb KR9HcklKN+YkimWyzSYhGna3djQP6xgOsvUZivRQzQb5taE3/zpUy8IZjgLx1NpN/yP5Qo3FhxK8N 8aqlQ/HhpcWc7VRq1ROKsTLHBpDVqUv8dGK+C2NM9uiYdXuyy+Q7WLpUmNhWEqw8587yphjd8n0KK 7OLiYV1aBO2edPczaI0w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq8-008Q0L-1M; Thu, 02 Nov 2023 00:45:40 +0000 Received: from mail-mw2nam12on2060e.outbound.protection.outlook.com ([2a01:111:f400:fe5a::60e] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKm4-008JU6-0b for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:34 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CuCxtsWwjF3lsZ82Oh1Coh9qo6obO7GiGQMV+35oZ8SOxVgHbuT5Mmz5hz4dz0bKUIy3Pi2ue5PDzHiDsyhnI0q9SeDTleMfIKd1f1NsRvTZpOe55g1+Tj1nNTnUK849Hslgx/bBCX1o1Nl0zIsaSFTiSsVT/1AeOTCMlWNc+AoWqL1EJPgP6oE8evDzt61U5kqd0qVLL6fYdmiQHHmcYgDsix61rbwm1LtZ2F7mWbp+zLFg2lEkawaH0QCf5JV95Wr0zvQgWktpOztUHaIc+ZMw5GLowpj29bvY4kI66+wL7FbVnozP+HIOrFG131Ohu5WDzui+cbzWv+aRJZ7RCg== 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=6Tll8psXY+4Z8He1bBdO292WiMoFZ3RTDzdsz3sPuzM=; b=b0XjkNIGsZ4OZI2MtZ7W1TIY7jyxQM0mB0Xrx3bExZVJfDwoyUVhyzKorL9baH2ypmeiUfFyqZk8iCozcm9jkt2A5Sl4Urv+ertEOtvzWtNm/wAA4k8BXV5LkdvfegmC0feoL3BQFTdCkzwUpwHKL9vw7ow9V2bcj8KthMh4BO8WB/C+lCG2EW/JfX8KSxi5D76v22EIVDdVONRYKD7KNNxLdksEs8OVzYFQ7P/DHXuedwL3tYkBj3r2rO5HErGjm477OfWwpY6CWa+/nYvu0P7cI2wiJykLnzqeaWiaoQ2G81VIgU3ify2dHIqcEnMAzJs8Fo4fucqQcavL+og+8A== 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=6Tll8psXY+4Z8He1bBdO292WiMoFZ3RTDzdsz3sPuzM=; b=VEDaVz1UOd5KhdznErc+JN7pzDWeZaeJ7TZJemzL2u8KLy/iVu6bgNPQU8SjWc2l0ckCFE+5WuN3pGVsD7FrmlH9a6yfFf20vN8VxrcqQqusoe3WjLtaZ3w5Z+IEnlkpatUkzLh6jCE+YjN8GAhNevLGXiWxnac/pdZp4j6oL26T2hQiFMpXnkhjn8jKo9mqr6XPHDv9hLgbf3lx7eJFtg6c0SRaz/Ur8CcxAKvryQ2w+TJZmf0ZC9P4jp08m95mgvegTodOsMpevNGBuktvtG6hRXaPVUWNNrkbergsiaXtn2p1gkh9Jsb8+hHZc5bBhdjd/7rS2666qr3C574k+Q== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BL1PR12MB5301.namprd12.prod.outlook.com (2603:10b6:208:31f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.19; Wed, 1 Nov 2023 23:36:47 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:47 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 07/27] iommu/arm-smmu-v3: Move the CD generation for S1 domains into a function Date: Wed, 1 Nov 2023 20:36:25 -0300 Message-ID: <7-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0280.namprd13.prod.outlook.com (2603:10b6:208:2bc::15) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL1PR12MB5301:EE_ X-MS-Office365-Filtering-Correlation-Id: ed40ebd5-1381-4c06-0bc6-08dbdb336978 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lTQqH0PnGxQRj419f7SpAv3c1dV47wpprccCk0op5ny6IcHUcyMZekPIdkfG3yhJJapBCsY15mWrpze3x/CSQQMQRoqNg5vLceXAse7dEr7N8orNDlFeunI1LrWM52R3Mrw+y6BJgnhEZ3ooCqHR6UbT/7irfPwBJLEC3sBPCwTOtWuD3spkIcGR/pGb6qB7nxaL76L2t6+ZmkqVBkzbmeTL1wXm/q0jpdDFGdPMtINRUYd/AUTZoGjx6mcSK9fYq3hfQqYHRbje1lt83uA+N7gTddcsxPIh7gbW9nj4YB4S98N8zPbDWxSjdnNEbwp6NMjNG1v4a8kpWLMr0+URvXuExfssp2Z9bX/JjS1NoiRzticlrRneLTV9dnP6hQ2MtaOPmnhg3g2OMUhe8VtwJgFn6HCe2ufa0EOx3A5ObEO80MJukp+B+FM7q883daXk9Mve4KKLR52bJfzahslVCOM3IRcyBPh6c/4Qz8Kgkn33bE+Bkdt360kOZWKH8ymT5tP8xFppYuywT2ft8geHVhHQACytScsoFKHUqxhz6qnYApdgyT3n9jWywbjoWtd33mBonI85hH9vFov1tLTS4JQhmDedBLeaHWq48wTNVqreGvqnOhNkvqQ5hctrW+vP/hvQjfFI49W/XVki5MnaZw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(136003)(346002)(396003)(39860400002)(376002)(230922051799003)(451199024)(64100799003)(1800799009)(186009)(4326008)(8936002)(83380400001)(86362001)(2906002)(38100700002)(8676002)(36756003)(5660300002)(41300700001)(26005)(66946007)(6506007)(6512007)(478600001)(6486002)(6666004)(54906003)(66476007)(316002)(66556008)(110136005)(2616005)(107886003)(4216001)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: xjdgMKkeBH0+Fv6PYUNsOWoUFAgNbH6/PB8YdwR+I1bq194UWZmprCddt87D+4zKVnpjJrUwESgwv7SVCcEI3vccjTu7LSp/3Cq99t5vLCiaeKhunej/oS0Xw/ZvMoKdv74nU80UUQZSFBab1MJC/zEB1gjeyYUBLD4uNmLeGVZ3mxScxZq5xS7cYMznzWwGKoF0TwY7KcWUJkkowpD9mtL1jkvtKdZgH6A139PN7XSJYgEczrqsgfok5Yj0uFfdVhcsP1KV6pW0Sp1VKgvyT6dB8+RPp//qLCjf9Vyt1dUXZgzQXoHLhb7KmA5dNHIMYeYNVMP8cYpHUyppBbTugNHBeke4pEbD9h95IWfhZvue11eXOj5KYSeRukIwRdxIs3fYkyXO3oyMjSMQX9+FRYPEHUWaOBAyxaIrSjl8oQ6OY9S2895p+jy5WzRCyDpH7guVrDI36QZ4PKgLGkVG9+3k11ahc7xb6d56LWmHFd+vbMXl4gBCwlnEFQSbAOiNrLeOqEbS/zuOcqq6Qk8LTscYB4Sch+QbGRovd7GwRZ5bJ9ajiP6nrKEYGK4TjLiKksZ40HHpJcQySQcmeJz+mV35IkNDvPShZtSFxNaA2hLKN+B1MqM+aPeHEwM4fVeywaI9EoFbYx7L/dE2WfDFsA8rPaZ0qj4p8ygMUYEx5WYHgjy4eBl/ijV3lOFXJgdy456oFhaUR5TJoPrd0SfUHAdCDTsOKi2ROFOaaIlu0G/9UhJZJE2ltTqRxLPZwNSaNd8T6Q/QnHzE/drEHMUeKqCiIiTgKDmf5qYfq0/OgyUVHT1rh73NkbsQ4LTRRgtETANqUgXU06UyrlLFskzUjBftDEEGMTgtzMVI3B0rK+xbsSSb24zUBy2tiC9LfRBUUIYiMKY8NEZ0thV7CZZ15ghz3zVweDZgJS091ym+Ikkxw9sOWXSyZmj+naNwjx/rOQoWQ6g2nOP487CC7TVDiSsNAGgDiRTbXuOaHkEt7kHnQXsWcxnKH7MRa7bdn3M41FZekRmlyrXEQRHmX4bXJZ+w0sB7sLMI5MfTJnDwdY+UsA6TzA745zwlfhQs8tAQ1+AhPnkdxjdEqqV++umyLZpxMwQ7aDa7noVgqMxc+NJhMq4Ci398kwzFcp5lHzP8ZAU2RpXwRjmYCd+TikIET28rnDsOOom2gQgS0K35pAvNyTCg82OjFNeueq4JK+7cEhBxGInQEkoyj/jClCcHwz+zTueRHtBJ9dvtfzngyY4s4aNUctztjCbxDwGEUyHWUwJgk2s75m+8DB/n51p+kkv2aPQo8UoTUQgRohGRR/Y0bd3qIr0/z2JxTwhtqABjKVUr5N6bda9h5TKIUMTR3KmAQGhEJdcXs7JfR2z2wo7wdb8McRUsR8ipn1zBmrGB+nPypYNVPAIsvao0btmGN04TvpUzd+vzu9s34FHUfHoszKbO7aqVvA0UYTsWYLADr7PMe27SlNp1ZrcpL4bFO60Ma5Pnh4L208bbWoWB7U9jmA2RXGxBcjDFZOEgZOv+FrtFJYCBiqDT56sb30Xl5WY6znqFTiCqZoXAZ+KxoRvqI3/EHjg+gLkbD2y2/hrO X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ed40ebd5-1381-4c06-0bc6-08dbdb336978 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.3655 (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: ayRNVu2u4gVQbIYYzw7d7/PqTyZ2ZDYMbfKk1Witk/FNr/cgHiwbgImXlgufFlQp X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5301 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163724_257384_AECDF480 X-CRM114-Status: GOOD ( 19.73 ) 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 Introduce arm_smmu_make_s1_cd() to build the CD from the paging S1 domain, and reorganize all the places programming S1 domain CD table entries to call it. Split arm_smmu_update_s1_domain_cd_entry() from arm_smmu_update_ctx_desc_devices() so that the S1 path has its own call chain separate from the unrelated SVA path. arm_smmu_update_s1_domain_cd_entry() only works on S1 domains attached to RIDs and refreshes all their CDs. Remove the forced clear of the CD during S1 domain attach, arm_smmu_write_cd_entry() will do this automatically if necessary. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 25 +++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 60 +++++++++++++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 8 +++ 3 files changed, 75 insertions(+), 18 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 521bfa18879f90..04a807774402b2 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 @@ -54,6 +54,29 @@ static void arm_smmu_update_ctx_desc_devices(struct arm_smmu_domain *smmu_domain spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); } +static void +arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) +{ + struct arm_smmu_master *master; + struct arm_smmu_cd target_cd; + unsigned long flags; + + spin_lock_irqsave(&smmu_domain->devices_lock, flags); + list_for_each_entry(master, &smmu_domain->devices, domain_head) { + struct arm_smmu_cd *cdptr; + + /* S1 domains only support RID attachment right now */ + cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + if (WARN_ON(!cdptr)) + continue; + + arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); + arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, + &target_cd); + } + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); +} + /* * Check if the CPU ASID is available on the SMMU side. If a private context * descriptor is using it, try to replace it. @@ -97,7 +120,7 @@ arm_smmu_share_asid(struct mm_struct *mm, u16 asid) * be some overlap between use of both ASIDs, until we invalidate the * TLB. */ - arm_smmu_update_ctx_desc_devices(smmu_domain, IOMMU_NO_PASID, cd); + arm_smmu_update_s1_domain_cd_entry(smmu_domain); /* Invalidate TLB entries previously associated with that context */ arm_smmu_tlb_inv_asid(smmu, asid); 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 790e7911714dc8..46d0a45fb0f525 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1118,8 +1118,8 @@ static void arm_smmu_write_cd_l1_desc(__le64 *dst, WRITE_ONCE(*dst, cpu_to_le64(val)); } -static struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, - u32 ssid) +struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, + u32 ssid) { __le64 *l1ptr; unsigned int idx; @@ -1181,9 +1181,9 @@ static bool arm_smmu_write_cd_step(struct arm_smmu_cd *cur, } -static void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, - struct arm_smmu_cd *cdptr, - const struct arm_smmu_cd *target) +void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, + struct arm_smmu_cd *cdptr, + const struct arm_smmu_cd *target) { struct arm_smmu_cd target_used; @@ -1195,6 +1195,32 @@ static void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, } } +void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, + struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain) +{ + struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; + + memset(target, 0, sizeof(*target)); + + target->data[0] = cpu_to_le64( + cd->tcr | +#ifdef __BIG_ENDIAN + CTXDESC_CD_0_ENDI | +#endif + CTXDESC_CD_0_V | + CTXDESC_CD_0_AA64 | + (master->stall_enabled ? CTXDESC_CD_0_S : 0) | + CTXDESC_CD_0_R | + CTXDESC_CD_0_A | + CTXDESC_CD_0_ASET | + FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) + ); + + target->data[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); + target->data[3] = cpu_to_le64(cd->mair); +} + void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid) { struct arm_smmu_cd target = {}; @@ -2609,29 +2635,29 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); switch (smmu_domain->stage) { - case ARM_SMMU_DOMAIN_S1: + case ARM_SMMU_DOMAIN_S1: { + struct arm_smmu_cd target_cd; + struct arm_smmu_cd *cdptr; + if (!master->cd_table.cdtab) { ret = arm_smmu_alloc_cd_tables(master); if (ret) goto out_list_del; - } else { - /* - * arm_smmu_write_ctx_desc() relies on the entry being - * invalid to work, clear any existing entry. - */ - ret = arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, - NULL); - if (ret) - goto out_list_del; } - ret = arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, &smmu_domain->cd); - if (ret) + cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + if (!cdptr) { + ret = -ENOMEM; goto out_list_del; + } + arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); + arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, + &target_cd); arm_smmu_make_cdtable_ste(&target, master, &master->cd_table); arm_smmu_install_ste_for_dev(master, &target); break; + } case ARM_SMMU_DOMAIN_S2: arm_smmu_make_s2_domain_ste(&target, master, smmu_domain); arm_smmu_install_ste_for_dev(master, &target); 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 a8e7574ab8e154..950f5a08acda6d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -764,6 +764,14 @@ extern struct mutex arm_smmu_asid_lock; extern struct arm_smmu_ctx_desc quiet_cd; void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid); +struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, + u32 ssid); +void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, + struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain); +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_write_ctx_desc(struct arm_smmu_master *smmu_master, int ssid, struct arm_smmu_ctx_desc *cd); From patchwork Wed Nov 1 23:36:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443139 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 50E85C4332F for ; Wed, 1 Nov 2023 23:37:34 +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=FdU+V/RJX3F1/OJ4MgzHGzsOzc4J9CiS2qYcKjkXMDQ=; b=Uo0WFbHjv4pK9Y 4ppoRy/D/1fhFZ8R8vy2eZIjAleLRDB3Mn3RzmVAc0AqFARlWNiHMXOGgTY3YjfBEkOiHekQD2lMD H3FxOrduYeGSsL1gdK935posDn0oqpH8cxZY+uh7mPQKFT5Rpul96UBaFSnvW3lYtet2NtuC1+UjV V7bUq42y1O0NS+z3hrvEI3wjnOiH6DPFveCh7UTb7/d8b1y9kOFRZ5B+abVuU9IG5fmHqVLVofpt+ kbk88Pzva3S6hTtQ95nPx6l68RCVUqqD4brHBCeuATwHDrs2X0gj6lsbI5SySKqrw4v6hcO0ie0p4 9AjH22tKh1gNN5D7acYA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKli-008JXZ-07; Wed, 01 Nov 2023 23:37:02 +0000 Received: from mail-bn8nam04on2062d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::62d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlb-008JUF-2s for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:36:59 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cpIttk+C4FoCNWBPGsJdJebRDZFh/+lcWylsFhLHGx5YHObRoXPtEYLR58pM8ZRncPZ1dLM3J24VSboQGtucB9SE0tjLSrInN4sriC0Cli2FT8Sh7A2fkII6c6Fm/9zW0O1y0jhoXw7mrbB9FZyvGf7G6mWK6pIYOty8DqPDgicRfu8owRS7+Z+UrOdyIMEWxZSan+gwskz/djRRoRUmchCFPN2XDV2H/UpVRiz44UBVNtOqIyCEZWkOtYgE0wCbwCkABD+XN76oYnm3AJdpA+KoKig1ntQCoo18ntxFWh9ZSNM0McBCIsQF+qvu79feLl5/cPcU01DKzz79sKsyUw== 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=65NYd3HCXKH0KM4+CqaRxz3OgMK5ow93sI2dKYbHvXE=; b=W9u1U7RCfqM499d0GyJyzU8UUmgV9cCkanZhMM+8EkBUQrWAgfFtD8j495nYt7jZUfs08PmipU/5+HjiRywc01SUdss+cWrUahwMKPIQB7nTK+iiyasSfPaN2oSvbQJF4a34uaJxuqOJX7W5m2CSFTlYe0TdJ//z+o6OYevRN1ZgAc7dKs5tWs0AA4pm5GU0yT0jxxIsAT3ppNOudowBl0XeLYHRp+/vvKgemre8I8QPYPXTln3n9XshuWNa+xL4jVUhFwqu4DeELqIYMbw7LLyvnnOumvBaA7Eyn/vKLxJ9oJm4W2eL5Mg6CVmyFULV/4Tqv/qQh1dgvk6GcVjUUQ== 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=65NYd3HCXKH0KM4+CqaRxz3OgMK5ow93sI2dKYbHvXE=; b=CwVLAovWndPvLdCOiys4ULt5FNiZavUW1VPZMxLCEwfbX+Y55IsFN3Z1qgp65v6kbpPG+YykLzovtXLsIXzyxBSsW7CJuWJ/zP8MtcXfNCj8Dctql/A2qQSF2MTCnT5lcSDQHcpwa2sMnaMV7BBi+hh8AXbZqU62N+PZ9XKx1q5Z4HzcMzsXCFstTld38gDMqoUEMzDKM7So+jssBItWXUshROIkhGedcjv52PfEANTTeob/0uY1SP8m39fTaiGBlMejpKzGP3PPuVHb28ycSfh3dX0tBGZ2gMQZPRItGCf90h7shj63ttPj/t/k6rAFo9lG/IygAiU42Lyt3gSiMA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:47 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:47 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 08/27] iommu/arm-smmu-v3: Move allocation of the cdtable into arm_smmu_get_cd_ptr() Date: Wed, 1 Nov 2023 20:36:26 -0300 Message-ID: <8-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR19CA0044.namprd19.prod.outlook.com (2603:10b6:208:19b::21) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 7a0d0f35-84e0-452a-6e11-08dbdb33697a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gGLrm4S25mvmfYPss3dvaP3q7yS5v8k3lFkL20VFBwKQDd/84Jc1RrVDMg5uwbSAjkAP0Cx9FSsx30YatzlJxDXobSeVAPvmO/3NIbhI8Xe8jsRy6UgzvDanwt7V+Y2egtPh7Vj+pKVi++rRM57FHya00NskKnsZqa+ZkusLuJ+8/AExWPspbsv8YtmGofHoloL+WpFQEM7xEQAgI86ZjxikJ2mk8gqKLqC1ver7jBx3V3rDa4GUL50Y1TOSvrrWrIi2EHwY14O5wO3z5/Z66WrQaYsInZm43stZJkPsiah5ElpFTqDs0sM0GbvBgd2SKmcUc4RXODRx2RZL/lxEfyAu/+jWrRwD89OAaRxqC4wZ9ubBhRZXQUvMK6+yl7ZEeu/JNDjZHsyRC8Y4olX1X6vhQ7Mz8JaCmxPKg1XGqyY/L6My9vsxCF5dTMf/y3YUCSuRQzryd9pnHpE/hX6BRsS3YHz8CuCug6caPSbqr1zDwIpIelV1F4Sxo0hzQBNKCZxVHKhlGAzGYahK8SwThE5MAZWdJmjsMZ8HzvYjP0RJrqFKDKJB8YIhwRwiAcfGMYFiPeAdGfTyEu5DkZ/2yvT5J0KYFIjWBG8Rxwcn3SYdN9n0Eca/jrCmgh30rNM8tkOfUYzC4ul9WjBw65leFA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007)(4216001)(473944003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: G8QbF/J/x+b/lkw9kpGQWw15rRqihadfrimVwLBQJ0ixCm3sgc2+jlKQhEuQMwHagnYMZAFiRaRNg8NkNzDglqDzwkcfAU6SLdOrD4HadM7/g08mkzFYNvc9M/pM/qTsa/h1oRQvNx4/7Z3Su4x2etffoGbKnTaM/B+TznygzmY6wRx4Nd7AZ3gZb6wW8YcqFM4VklP5pmZIwaRvD44x1kzq9woxDT/hiCySjBbVCXZFDKedUPa1cF8+C20+Occqd6kFUBtPOGe859GEWd8Zk/klmix9GHma7YP1XSuqApcHt5CN+Ib9SRo37cFaAdw8c0FWgRR86bXA5a3QGwA+AV6kEko1wDy9NYIV2SiKVT+arTEnEkmYNsv4FRJeCYChfUNaJZCrlGk3EbKfdErTdER03unUQmyOq3vrqLLHeJjE+g6y0VMRtwIVtkxuelmuz05PA5EvzjUfBU8qv7xesIU0X+CCQwrH4vWvVrOwCWu/EjHuFECjzuaqaYMuDkJqnL+mv3h5X7XEdn/DJ3XNE+WScyYMTYk4z5e5K5WbK3asabR7MkSV7NJ8L72PDsp94XqiZ3ASjKBa88cu8prhwJJPc8R9rfJiaopZI/Wy4tULHeIGRu78RDSq2Ek2dMJjr3VLfDQQ7AWG6TLZIXkKPbDaabYC9nQSqNC2Y25E6/+Iz9bnUy1GonE0muza5T41/MgDshceDdi0UJZptbop0XGTOZPZRYCREPsyeK118WruK6b5pztuttZ6S0oPs0K9n+88dPsrguQHeGhF0tHShGP5vy0dtZqC2e+PQj1/L2SUPNr6cLXxJPoJI3xyuYU2cd20pDReviJKsdqbqOffyaaImiHoqbuN7AiHF1I3rzKCFaiaqBpQKizFhPTHoVTVMbcVVyv0u+b19DK7+E8RHTkAyA2i59XLnW307sAqQ3x26wzMVapfvH2sTqYp2n1kd/PpLb3dddwOYmI8ggXSk1qNYRW1o5TwP/0XlCLbty9KkEfMJAgYR0krVQ8+JhDl7/466yhEian2O+xOlGQK+41cBBRI7EQTyBi0o3hQOc/BKCGAPGG4SMyRsPCva0+NGy7rgYxwwUhGV6tzXO3riH1pPMFrAmIRcI9eDZm8qUjhfXYGeM57Xh5JE0OXcgimfG3kGqC1+zLLwgj99s/grK3uMUlkKX2AOaWvlluYV2Vl38CRHOSBuLC1KI+aYjF5zGVon78dDFhJr97ODXmw0TY7ppb/DXhzlcxfO1YLDqy0OOlU0Xwhgtc16Ng1XQtp/qngXYCkFfpY3yH8Dm1Kd6baAlxIdXX1bxqwrZS8NbHzgqrOZPgYU5LqNZmSsIqlHEMJNpTQNKv1fOqpEnU4vrHArFm64/7faQwKOG4HQtkvJVYSIsZQqUKbXWsU2Prmk3o0CJ9fY1U8uy59+A3N1gByl4gUKQn3SN6JBeXCjBqdDrspz0SfYqyRgd+Y8EQbA18yh9F75gggytXw/XXT7UQLZAFhna+OOEj+lo8KKX9Uc/V5h9g/2tvYzq0zvM9ND0AaS2dRqd/+ft3fwLSuKRNzDlU8Yp9Advk1r8xZCqRfu3nvLtLl1boxVXPt69Uv X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7a0d0f35-84e0-452a-6e11-08dbdb33697a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.4329 (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: oz3a5kjcCGZCrRmcL+40Qxqp3nXjKsLaMIMjkB4SFBlJe9814ApF04pC8h52+MQ5 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163655_948606_490EB0FD X-CRM114-Status: GOOD ( 12.84 ) 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 No reason to force callers to do two steps. Make arm_smmu_get_cd_ptr() able to return an entry in all cases except OOM. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 46d0a45fb0f525..a95e16a83f0196 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -89,6 +89,7 @@ static struct arm_smmu_option_prop arm_smmu_options[] = { static void arm_smmu_rmr_install_bypass_ste(struct arm_smmu_device *smmu); static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain, struct arm_smmu_device *smmu); +static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master); static void parse_driver_options(struct arm_smmu_device *smmu) { @@ -1127,6 +1128,11 @@ struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; + if (!master->cd_table.cdtab) { + if (arm_smmu_alloc_cd_tables(master)) + return NULL; + } + if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) return (struct arm_smmu_cd *)(cd_table->cdtab + ssid * CTXDESC_CD_DWORDS); @@ -2639,12 +2645,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct arm_smmu_cd target_cd; struct arm_smmu_cd *cdptr; - if (!master->cd_table.cdtab) { - ret = arm_smmu_alloc_cd_tables(master); - if (ret) - goto out_list_del; - } - cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); if (!cdptr) { ret = -ENOMEM; From patchwork Wed Nov 1 23:36:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443150 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 EAB3FC41535 for ; Wed, 1 Nov 2023 23:37:55 +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=GNf8F4up7yDwf3R5N34IqcWB1QtUEERQWdQx1bkLVPI=; b=wfKMeA/MAavwfX /NFEvIyx4U97FlDSjJ0waf0mKKlWB3Anrkt+/LY5hd7TQ+OSK3SrULgfCT5nZBZv2F+sIuNLalkmK O9BjedHrWynp2/MkNfJROCc/jm2RPvdHrldRYgDFqb31QPO/d/GRVf4eCEJWIPbXGgOfbCbF7egcP H9h5Y2PweLTLX4yorI+GvMgrk7VZiY714oapecGoStz10lspHcVNciSMgkzA6ph5FuIoF31qfHxqy OC9mvwMO+L2QXVt+8phg8gPMECar7fZqdTswUFueqauxEQxZ2Ind9ANILjcdEzk53S8PsybbTIWVA Me4liEeuVJ8+9aduWb3Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlw-008Jom-2J; Wed, 01 Nov 2023 23:37:16 +0000 Received: from mail-mw2nam12on2060e.outbound.protection.outlook.com ([2a01:111:f400:fe5a::60e] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlk-008JU6-19 for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Td9ITd+0eKJJezfDE1+NytFazkyMuqtOYuyjQ0HEzpfv7NxkfoDWc65CXnO3dW9Ah/rxSh5HCHI0NhcNEH1nMF4aTGzVXtJM6HokCL4tO26dFKaisYq5D3pGoBX+XvJN6Lj1kXHaG03ExqG4kJUKVRejQfSrPRfGkWljXmsY30FIJKDm8NuEWUK5lwdyOtqmCB+QRCAcTC4ditiD37wVBzuiM2Vjq6gc7vC46Yrqrhl0GKdeNf5O0PC6n3EbAYliPkKjzGYqLpPlPv4Za0yD2CLqhXToaSucBrOPu/Uhxq+ZC4ANFzwOFLzUZJpjUZHi1rZNkoJ9aQw/h+EsdEsp5Q== 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=6xKslKn9pVv0Z198zV9SF7XR7Y47z3raRdyktAJza7w=; b=DrirZwQ4EEXld4ZO/ASqipHVyyDkKu+Vc/I68o4WDo71OSqrOYWL/RahRm+nEI9BYNhrU6YCpjt+typIv4vYNjl0yR6/r+qUIJP1dJ0K52eZW+lx3WvVeg5KzSi3E3R03Wlx8gulfqgBITYqX5EjPjhXt4VyuAvAytI+SnYvx209sVtcTmPZt2CNGJDlIqsVAGxW5Ne7z+E9OG+eo6JbQIvw9IvfWMxPwK5R8mq1TYdyBPfVLujKGTTSpwp0HfeyqBCV+3PU7nQ263K6XTembDvCZbOgROdWijr6Ss1rUXNYcS4cHnkTuNPlYUjXReIU8fjZHGgqLjBQN1eIldC/mQ== 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=6xKslKn9pVv0Z198zV9SF7XR7Y47z3raRdyktAJza7w=; b=EW6srVAg9WAvey8kMeoXRl+U07zKFDXUuF6bqEiHmLdeLT5cufQT6A+Pep0baJUEbKeQ0mYHyT03Pl6cTeyGdHtbrEjQcFUR4/TRtW6pDKD71T9fK4SIjOnWJQkyTWPDhP0wcmoApPSODYSddwqdX8uSd2uUIBZ9fxhkyuYonMKx2T5qVkFYOmp7rLHHHZr8gaJK3x4opKEHAl0fYjIWkj1LaBMtZDLzsnc0HR4sZUpsMVVNQHkZfEysYTHyW/1rJl0jE3wCNts5witeCwNAjDPHUUj6xdXBe2oE+B+FVLHwX1Tx2I4sK1kRncJxPpo91Lh+C3fwHNpRjI/89kpDZg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BL1PR12MB5301.namprd12.prod.outlook.com (2603:10b6:208:31f::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.19; Wed, 1 Nov 2023 23:36:46 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:46 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 09/27] iommu/arm-smmu-v3: Allocate the CD table entry in advance Date: Wed, 1 Nov 2023 20:36:27 -0300 Message-ID: <9-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR06CA0019.namprd06.prod.outlook.com (2603:10b6:208:23d::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL1PR12MB5301:EE_ X-MS-Office365-Filtering-Correlation-Id: d7a31fd9-3270-48c0-aaf0-08dbdb336956 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: caZDyBaekgH7UR6vWvixPuAIRssRUEMho8sNPB6R/Ru5aP6vTDfB/jKnuQ+MtJEFEiLlwZWkEJawDRYPmHwAvTj3jvqu85iugZAPYOh2xgOaAbJ4219qoygwOp9z2rYOdEharuFMz0ErV96d5/O0WopBdq9sAmLY6qHtIFwaeHhQ5faX1neIZ8kqI87xjWepC5TXSCNLTziV9yNKyQQ0VhCV4rgJX0PSSHRTXJsKjhRAY+DaHcWKt1axKZNaSanYl69GUr1KKp5NYe9102NDgzQSsulWMJOFYphfS8gkfl/zvFJMBnroyZBwqujJ9V2eKaDgQsmLhQTy2B6tQu5eW0qssTebLN6V04b8B7azU0J46zzWehScxQy8aJe0ke33rMpVQYYCh54GWgjy+370bK+IDeVM7rNbpAcb+ZKfAy17l9G75OThRFAg2+ipr7h9c8weTUU89DEm2tDqFY6js4xh1uRqrUpO0w4hIrr53P7XlP2yq+E6L2QyCUj1NIJxCZ1MQw5lTeXQTfXpe0gM/vX6wzvcpB8SViqV4f+urIjBi+FjjY2MbER1MArKDZiaya+qOtMWhZdBa8Q7sMbaS0ELLGBzD1BNQNuBEhGIsVc= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(136003)(346002)(396003)(39860400002)(376002)(230922051799003)(451199024)(64100799003)(1800799009)(186009)(4326008)(8936002)(83380400001)(86362001)(2906002)(38100700002)(8676002)(36756003)(5660300002)(41300700001)(26005)(66946007)(6506007)(6512007)(478600001)(6486002)(6666004)(54906003)(66476007)(316002)(66556008)(110136005)(2616005)(107886003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HkuY09UZ9rsaJ/rPzIwikQNMJ9Wa7ELkeL57GRCszVbB4uzF3HOfM1mLySfag1oCPg+yrnRJjmb8C1op24UOQboPzVtNHuTWg3xKgolwmfPCUpG/paH4F/4ZPEkNnVi7VArvXo3ISPlXE3QVYVFNuquvOPYt8bqRxJJ9YAlvIOA03F+yQZ9A547IXceGdgvLpV7aRuDJKVm00/pUMSbUdd1jgcMv4H3dXz0xzCQDnqBtMQtEpwAbgDLk7UfyGeGYtlkkcJ4l53sjycWmPb8sehQy+UCdqHOlxOfpaaP29XyafujkLLOFlJ+BGTE8TvPwbJhyyKlzzNWIlyMqHXxibnSL6XfPFN3dS+IcaAp5M8T8ezNXd7ntjX8OYv/W2gnAuLRWZ8Xvy89RC08iXl8gm6aE0S2zNNph71md7i4s2lpTpCaivs3Kj3zYG0vL/ck34m2qEZZB1Fmvci/xshkgbjo2A4imhg9f0eJc0Y4TW5D2BJYrzIwjif7/4HvTN1MLMdoHrTXA2c54xO4AvUV2599In1evdvWj21vYwJIz0TpcuUorLqw/ZIAVap4KCQ+gIqMLHMGS8o3uILXXiNEHtfd8YeTrWytqtiEvjmFzO0FI87q6aW1Xrfp4O4B3VYuUNGEldeWBTz4rmq8bdI3q+XXukbj2T414D0IE1jy+YO3Nd8aOp0UBNjPoFP7D23eoJyYyoUAqvyjGjPm5y0cxQn64GxLZDqnUmt58rc4YknpIfknfxrQXXeVNPDNMnssIiEs2LXpGjn7pzxCwCErXYyKAE6yt3AQW6t4HqlpBx+yu3HUDsmp5IfY4WnoixtnaVxyh0CtVcHlhppMM8XJaOs4SGNVFmiX5+2vrOGm+g0x2FL4ukj0feEqevv/qZ5nGb88g7y6N/0xE42dC4biCRS+LamVoM22cnY8JXzk7aTua22RW8ORQaw0fmmh5p2fEi+kcw/REAdbLaX1ycNJzypGilB1lpz9JbeqQ/K6jDB0ts1L9Pd8qDMN+Jkw2RTXX/ACGwEvt7dzFK/LuW01T2lHOYa/DPtSjKlAAMr3atqvN8XC3bafNZWecZ1u0T2N0ZQzINkdQbGdmzBk6itFd5oc2skVdv3Sv4WVn4JfOS6OmYlos1ZyjC+eAQ9Xmp8H63lCRaKaR/dVnt/GSZ462HA21QJHKReD9+CwJgP4DpOfks290hyt3/PVy2wiX8tFYH5AgSGdOH0ULX0cIaJ0DYZMiC5IvjW4qxgrJcbARLNLdLBdBC+XRkXBgnQMpda7muu1o395WB58fCVt2nX954MoK1kMnGdt/GUaQL0/lr4FX16Z/wkHyBanva9+AMDzmUu3XqMAHE6lVDRi0uN+B1Pq91HMLdChsxj1zHkKVe0BHll5Nj20M/rv8UAcq6nPwnHn0Br2EyUQ6+uwGfwDjJkdRE46YDE9ZtItlpOLN8l4VNp2Gj9PVys10llzrYxsAx8UZJn+y1y5LmYGHKCARZxVJKqcC9udWQeYwfW/AU9hRhh9JhPQhpo+jXdP5Wo2VN6AKD64+Qyhmq43E6IS8P6d21LiSbNpfnmYlkJyU+vpibYgul9HLtYNGg2VYpDEl X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d7a31fd9-3270-48c0-aaf0-08dbdb336956 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.1571 (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: NBC9xmfgPxOus4/6DlRIL8eKpVTq8LPRrmD72lZHdcI2qOeekuxfUmpj2cEPXj97 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5301 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163704_421011_9CB013C6 X-CRM114-Status: GOOD ( 16.41 ) 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 Avoid arm_smmu_attach_dev() having to undo the changes to the smmu_domain->devices list, acquire the cdptr earlier so we don't need to handle that error. Now there is a clear break in arm_smmu_attach_dev() where all the prep-work has been done non-disruptively and we commit to making the HW change, which cannot fail. This completes transforming arm_smmu_attach_dev() so that it does not disturb the HW if it fails. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 24 +++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) 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 a95e16a83f0196..67544a92a7714c 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2596,6 +2596,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct arm_smmu_device *smmu; struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); struct arm_smmu_master *master; + struct arm_smmu_cd *cdptr; if (!fwspec) return -ENOENT; @@ -2624,6 +2625,12 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) if (ret) return ret; + if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { + cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + if (!cdptr) + return -ENOMEM; + } + /* * Prevent arm_smmu_share_asid() from trying to change the ASID * of either the old or new domain while we are working on it. @@ -2643,13 +2650,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) switch (smmu_domain->stage) { case ARM_SMMU_DOMAIN_S1: { struct arm_smmu_cd target_cd; - struct arm_smmu_cd *cdptr; - - cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); - if (!cdptr) { - ret = -ENOMEM; - goto out_list_del; - } arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, @@ -2666,16 +2666,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) } arm_smmu_enable_ats(master, smmu_domain); - goto out_unlock; - -out_list_del: - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_del(&master->domain_head); - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); - -out_unlock: mutex_unlock(&arm_smmu_asid_lock); - return ret; + return 0; } static int arm_smmu_attach_dev_ste(struct device *dev, From patchwork Wed Nov 1 23:36:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443141 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 5F869C4167B for ; Wed, 1 Nov 2023 23:37:38 +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=P/6d5OufiRZCdLT4U1Tf5Vy6nG0d6UlrcXxkTgN2a00=; b=dp3/VO+KPToZi5 542wKFuAjzDGRkgFNCPjbg1KoXVS5S20egRa2cSVUtKMHJ0HoCChCTUBrlVSndQdqltnU7hUueQmg HbJvFAg37t7mlDoYET83s1bM+jhr8FHVIEiO7aGPfuhGIpLYykhBc7USDEAD1sVhWiuJc6eORCCpG 9Bigyw9bQFYqu4uK6nBOrOFqubkQPssSshNzicgZESFPKsLB2h/Lu2aDrR07/+TGYYCne9sVej96/ yf+K4mTp8sIUKh31K+IumLbt8Br7YpIKhkDIgRyldK4B3t9L9XyoKgIrCP42diITnv/iwXirSmXka 1Vljo5CMP7D+Z6TNjQ8w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKln-008Jck-08; Wed, 01 Nov 2023 23:37:07 +0000 Received: from mail-bn8nam04on2062d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::62d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlf-008JUF-2k for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hqblf8sU47+JGvAaGztPRC6ce+5qhhg3Jkt4ZEobD4OuCyQW/OSs7nD3tLCCGBVKUrIkY1bz+r4GFiZViC8pkT1vxYmCPcTnLNEEbPivhHZGI/67Nr0ajF8FfIrxcZNJLKRmg96hlJBoUzIjrViYjtweYXKac+1SZ78TPHLjt3R4ymuf9GlSQGZ6VGSJG19pTvnLPGIOHTm5Q/GVOahfNtZxlb6fjKKMY7WyWXm5CjoxTAlgCnhJWUhHGtFUsjy1JPeYn8R83LphFb/+6Y7gsg3OpQ60TaFJFuUI5VQfQWhRqnvpniwRELyQbzF7Bcotw8mDgyRZITS5J2bCR1XQyg== 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=Ebdqfe9hHI80d66WoIdCKN+fhJlczA2bMUAoKvEZgpY=; b=esyzznktGcrFQmAq3eQJk7X+GP2EnN8EYuLwCKp4xp4ETkxH6gJx9Q6y4ndh+wxwRcG5tEUrDHarptw2iAn3IoJGNf8XGCkqb2ON1ODzsKHqZeMwlkTHl2xZB6yU8WJRGOfqNTX6wX+m/t7+SyvTT25mXlJhATjQ5GHeMKLjqPpNHLOmWjytvwHLuYsJv7DF35hsG8Bv66n05qXI7a4QLHwRgZJv7NF5B9V462b6l+oA656Dtu7fFKGQV+8695lNyVaF9eR9d0Pc1nk27cG8pAVX6EBWVyZMKEoGjYlBh8zjk5VC/LrL49rwBMbA+mRZDN4AtxyoK6LO4owwskEUxA== 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=Ebdqfe9hHI80d66WoIdCKN+fhJlczA2bMUAoKvEZgpY=; b=maOVsj/mnPc7rIqixuG1d+Bfn4hjtMyFrUc5nlOUwCA1H7vlNYk0GT/13pUQMH8pfz71D78HJ6gUcLbhDzYxHbhCbF2XAMbl1Rk19wuYSBrV/AxPLPZv8GVa+IC3F2dg9L1TdHZ9eBwNiTRrGi1fZvWNqMn0PWz0FA4y4mM0xs5TwbpxerQj6QPFszazTf2Il51w8j9vR927hXUdFB+J0MwJNva42sUMWol72Tbv0bwM7R+l2GLr5noMdtPXjA1gzNb5g6lgiv/ffs6N4YZNdGDncgw4r5wrZtyUWlizstCAVIqerYeBUf0/Sl0KeJHmFNNdg6xCFVoEbnuWCjigFw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:48 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:48 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 10/27] iommu/arm-smmu-v3: Move the CD generation for SVA into a function Date: Wed, 1 Nov 2023 20:36:28 -0300 Message-ID: <10-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR06CA0002.namprd06.prod.outlook.com (2603:10b6:208:23d::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 9ae677b0-4098-4393-93c5-08dbdb3369a0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Jntti+7pZopojPGjqECMuwXru/5ROHuDla5y/pZD1tKaZ17NLI3hJ+o0VkGXB1GMjAkzifBufqJYqAO8fCeyw5gRGnWU7Qlikbv5QarWJVDGRXfP06iYL9zY1HOcdndSIGgLhI85Zt1YO+2FzqBKyUJ0Sm5zdnMN/4htFGzF5qcz7hvP9Fr+qg7O+GKP5vKLJoeohN+eyON5WeShFWAL5XxhIuJnCDGmBUYOnbG7U9L3MQtRxDrg78BviHOeaMzL3u73jgzeoHXzwGXHpM6DX/2jJapKGyrpvwr8eBE/+rHXb0yfM26YouiWUe4SbMrylUa6VHYZKNcVp9Or1CVOtGLNKgM76l28ZWD3duDdjJEF1qU6C4g742bBecllOyiJjHkRQanxSBw7WXiH1qPo0uNe//dbHB3CZSfGNR4ft346QmOL3Xvb1JTAN+Yd//p05B4u1qNkcRa+hMi/Xvexk9FMPPO3uCLqluiwj2S/ZxGPqLwH6gAAY0fKGmQkDd1bTDJ4kZkn0jsDotu26z9yjJGYHFdYw+T8J+leTBMV4nPGAO6HqE/HSnegv9UH4tooNzoRbThaS9uljTSzYgak+NDOJCO0Hs/D9+8vHeHgVkyabhTxVLn4H2Gj/00Ec2i3EdC12pjnArNgw5UcAvqwxQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(30864003)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007)(473944003)(414714003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ENTGVIWAMd4M4MxHICXcNIoj2M6UHzOIjb4Z74bJM8D4RDw2/tMCfRX67sTouLkkCLv5jM7HRHWa1Ak5tcTGotRJJbDPnSdhmiKsVX8ewhjRynBrqvlb0n5g6UG2vbOwNQoYcI4pWNq2t1Y1mFxhmXXnmoM1HyabTqdB6nHWpKI2f5IjAFHkuB9jXGm4Nay509ioHvCJ+YrWgFaPNtHIfyKmQgwdkHmpzemT46aQlo9FdSK2aGHB0DxmLyh0tslkdNkO1tYHUYzyVn7R11iKr56DLPBNdczOXw1Lq95WhFsVm9bA62Hsp40oR0mW0Jgg3d7lrPrgmexbQi1qL1FXNgUdGBGwwbXKPTkiX6k8BwvxUB0X1jmbMBhM/WR2hFkdoxpTCQ1d5l59SMFNz7neV4QFqirnqJ/iVDJKQyIknXXN+Xa+k1eFxdZb/qj6IyZzptCqREJAxmKOctu1lyA0rjwE4vsJy9m05sNmkUMPEsZ2pu+0hOGfl03/psJms+TptxjwuSnex1FNMuhlwOFSf+C9M7KlbyYCDs3waDVQQA2yc1QQT/T0DC4u+CzEKfJmixr5R864t+ydmH+RJXpHcsZnRIg5gw9QRyhhPLRVX+0uqUfaOAoQM6sH48SlSO6ZbMZnY/xzFRtdiDC1KSgy5QkfUNwN9Y38QUy3syMWjrp7jhih8GH5r+q63mRWUkQAuPmdvKQ6DB8/zgq8iQTUVw/2ccdI6dp20bVg3ah1IDQ15i0FMKCUn5mxeWrb+ylB4+RQOUhc9JeGsuMRbQ6OLm2eEc09s/VQFuuW+llKOrXdVGsJyJKmqgdQr/FlOD3loCjE8rCvo8c1paddj9NbUV9vvYE3yeXjAIfWaHtNYm7d9EPET72EtV7gHE7/FoqQUumBZqkrA8RqiOEv1oeM93uwR4SEHLwJ6wBaNXE3YWSpashCadiEQTYwvLD9P1zxGBBvMmHvvMylDAWyc244u2aGTJPj2Eht1ycX16Pr2EEJpCWyhPCzCuoRv8+ZtJrkwLDhKSmvAnRcjeKIwp5Lqyk9Pm2yFCxmCkqK4AbcgdFbzYygzhNWZ/xtp/qsR8AhjfC/GW/K14rua/W+YkTIYIPRmvnzo0BHf3mf6xSQJ1oT09/ndTriYD5wex/ZcjH04+JvKcklvj+FUDN26kJf5WdFoeDuauModOb6BjGpuLk1R9WFAk5FUxzEPbvWIOmAiquXync+v8zkzGGcr5KLIbGUU5JGd4zbtrGwhc+r1anCj5SnjwyWVmldpK2bbG7umDm3+OUyIbANKrTpgSAaiEwIjPXp7C6D+lLwGnpBWWd3cBW3gf5+Xky/qUgTMD6eo91vGPCD8r5Wqr4J3NLFuDW4wo9h5UXnRo3BxgECOW92DSQUDFQemjqLyQrDHSmwb6S3IA0vjDZArgOcjAj/Kcv4/EEoXO1Ci0tuhcK+hZqIYt3dZqoHYSTC8LrBmB51byJdWNLBpJZc52vc9AGbc19JurPUtkvqnsVkyzSNkp+DlF/V6+PxDSra1jxt25qSksyikawtC+7hJ3I1RScDKdPGQy+TzKY/E8AgOBRkRk4LCPy94A5+PnJ6RRrDW7cm X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9ae677b0-4098-4393-93c5-08dbdb3369a0 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.6394 (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: 1Y/S9bzOZS6sTjnpl4gegOjyWVBG0VFFSN29XWsZdptzxF5cmLXA7ikOxgdyMbyf X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163659_897331_C19266DD X-CRM114-Status: GOOD ( 24.08 ) 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 Pull all the calculations for building the CD table entry for a mmu_struct into arm_smmu_make_sva_cd(). Call it in the two places installing the SVA CD table entry. Open code the last caller of arm_smmu_update_ctx_desc_devices() and remove the function. Remove arm_smmu_write_ctx_desc() since all callers are gone. Remove quiet_cd since all users are gone, arm_smmu_make_sva_cd() creates the same value. The behavior of quiet_cd changes slightly, the old implementation edited the CD in place to set CTXDESC_CD_0_TCR_EPD0 assuming it was a SVA CD entry. This version generates a full CD entry with a 0 TTB0 and relies on arm_smmu_write_cd_entry() to install it hitlessly. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 145 +++++++++++------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 77 +--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 - 3 files changed, 93 insertions(+), 134 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 04a807774402b2..1f0940b497f3c6 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 @@ -35,25 +35,6 @@ struct arm_smmu_bond { static DEFINE_MUTEX(sva_lock); -/* - * Write the CD to the CD tables for all masters that this domain is attached - * to. Note that this is only used to update existing CD entries in the target - * CD table, for which it's assumed that arm_smmu_write_ctx_desc can't fail. - */ -static void arm_smmu_update_ctx_desc_devices(struct arm_smmu_domain *smmu_domain, - int ssid, - struct arm_smmu_ctx_desc *cd) -{ - struct arm_smmu_master *master; - unsigned long flags; - - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) { - arm_smmu_write_ctx_desc(master, ssid, cd); - } - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); -} - static void arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) { @@ -129,11 +110,76 @@ arm_smmu_share_asid(struct mm_struct *mm, u16 asid) return NULL; } +static u64 page_size_to_cd(void) +{ + static_assert(PAGE_SIZE == SZ_4K || PAGE_SIZE == SZ_16K || + PAGE_SIZE == SZ_64K); + if (PAGE_SIZE == SZ_64K) + return ARM_LPAE_TCR_TG0_64K; + if (PAGE_SIZE == SZ_16K) + return ARM_LPAE_TCR_TG0_16K; + return ARM_LPAE_TCR_TG0_4K; +} + +static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, + struct arm_smmu_master *master, + struct mm_struct *mm, u16 asid) +{ + u64 par; + + memset(target, 0, sizeof(*target)); + + par = cpuid_feature_extract_unsigned_field( + read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1), + ID_AA64MMFR0_EL1_PARANGE_SHIFT); + + target->data[0] = cpu_to_le64( + CTXDESC_CD_0_TCR_EPD1 | +#ifdef __BIG_ENDIAN + CTXDESC_CD_0_ENDI | +#endif + CTXDESC_CD_0_V | + FIELD_PREP(CTXDESC_CD_0_TCR_IPS, par) | + CTXDESC_CD_0_AA64 | + (master->stall_enabled ? CTXDESC_CD_0_S : 0) | + CTXDESC_CD_0_R | + CTXDESC_CD_0_A | + CTXDESC_CD_0_ASET | + FIELD_PREP(CTXDESC_CD_0_ASID, asid)); + + /* + * If no MM is passed then this creates a SVA entry that faults + * everything. arm_smmu_write_cd_entry() can hitlessly go between these + * two entries types since TTB0 is ignored by HW when EPD0 is set. + */ + if (mm) { + target->data[0] |= cpu_to_le64( + FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, + 64ULL - vabits_actual) | + FIELD_PREP(CTXDESC_CD_0_TCR_TG0, page_size_to_cd()) | + FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, + ARM_LPAE_TCR_RGN_WBWA) | + FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, + ARM_LPAE_TCR_RGN_WBWA) | + FIELD_PREP(CTXDESC_CD_0_TCR_SH0, ARM_LPAE_TCR_SH_IS)); + + target->data[1] = cpu_to_le64(virt_to_phys(mm->pgd) & + CTXDESC_CD_1_TTB0_MASK); + } else { + target->data[0] |= cpu_to_le64(CTXDESC_CD_0_TCR_EPD0); + } + + /* + * MAIR value is pretty much constant and global, so we can just get it + * from the current CPU register + */ + target->data[3] = cpu_to_le64(read_sysreg(mair_el1)); +} + static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) { u16 asid; int err = 0; - u64 tcr, par, reg; struct arm_smmu_ctx_desc *cd; struct arm_smmu_ctx_desc *ret = NULL; @@ -167,39 +213,6 @@ static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) if (err) goto out_free_asid; - tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, 64ULL - vabits_actual) | - FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, ARM_LPAE_TCR_RGN_WBWA) | - FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, ARM_LPAE_TCR_RGN_WBWA) | - FIELD_PREP(CTXDESC_CD_0_TCR_SH0, ARM_LPAE_TCR_SH_IS) | - CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64; - - switch (PAGE_SIZE) { - case SZ_4K: - tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_4K); - break; - case SZ_16K: - tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_16K); - break; - case SZ_64K: - tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_TG0, ARM_LPAE_TCR_TG0_64K); - break; - default: - WARN_ON(1); - err = -EINVAL; - goto out_free_asid; - } - - reg = read_sanitised_ftr_reg(SYS_ID_AA64MMFR0_EL1); - par = cpuid_feature_extract_unsigned_field(reg, ID_AA64MMFR0_EL1_PARANGE_SHIFT); - tcr |= FIELD_PREP(CTXDESC_CD_0_TCR_IPS, par); - - cd->ttbr = virt_to_phys(mm->pgd); - cd->tcr = tcr; - /* - * MAIR value is pretty much constant and global, so we can just get it - * from the current CPU register - */ - cd->mair = read_sysreg(mair_el1); cd->asid = asid; cd->mm = mm; @@ -276,6 +289,8 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) { struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn); struct arm_smmu_domain *smmu_domain = smmu_mn->domain; + struct arm_smmu_master *master; + unsigned long flags; mutex_lock(&sva_lock); if (smmu_mn->cleared) { @@ -287,7 +302,18 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events, * but disable translation. */ - arm_smmu_update_ctx_desc_devices(smmu_domain, mm->pasid, &quiet_cd); + spin_lock_irqsave(&smmu_domain->devices_lock, flags); + list_for_each_entry(master, &smmu_domain->devices, domain_head) { + struct arm_smmu_cd target; + struct arm_smmu_cd *cdptr; + + cdptr = arm_smmu_get_cd_ptr(master, mm->pasid); + if (WARN_ON(!cdptr)) + continue; + arm_smmu_make_sva_cd(&target, master, NULL, smmu_mn->cd->asid); + arm_smmu_write_cd_entry(master, mm->pasid, cdptr, &target); + } + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid); arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0); @@ -348,12 +374,19 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, spin_lock_irqsave(&smmu_domain->devices_lock, flags); list_for_each_entry(master, &smmu_domain->devices, domain_head) { - ret = arm_smmu_write_ctx_desc(master, mm->pasid, cd); - if (ret) { + struct arm_smmu_cd target; + struct arm_smmu_cd *cdptr; + + cdptr = arm_smmu_get_cd_ptr(master, mm->pasid); + if (!cdptr) { + ret = -ENOMEM; list_for_each_entry_from_reverse(master, &smmu_domain->devices, domain_head) arm_smmu_clear_cd(master, mm->pasid); break; } + + arm_smmu_make_sva_cd(&target, master, mm, cd->asid); + arm_smmu_write_cd_entry(master, mm->pasid, cdptr, &target); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); if (ret) 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 67544a92a7714c..4ac32bb91ebf27 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -74,12 +74,6 @@ struct arm_smmu_option_prop { DEFINE_XARRAY_ALLOC1(arm_smmu_asid_xa); DEFINE_MUTEX(arm_smmu_asid_lock); -/* - * Special value used by SVA when a process dies, to quiesce a CD without - * disabling it. - */ -struct arm_smmu_ctx_desc quiet_cd = { 0 }; - static struct arm_smmu_option_prop arm_smmu_options[] = { { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" }, { ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"}, @@ -1192,8 +1186,12 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, const struct arm_smmu_cd *target) { struct arm_smmu_cd target_used; + int i; arm_smmu_get_cd_used(target, &target_used); + /* Masks in arm_smmu_get_cd_used() are up to date */ + for (i = 0; i != ARRAY_SIZE(target->data); i++) + WARN_ON_ONCE(target->data[i] & ~target_used.data[i]); while (true) { if (arm_smmu_write_cd_step(cdptr, target, &target_used)) break; @@ -1240,72 +1238,6 @@ void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid) arm_smmu_write_cd_entry(master, ssid, cdptr, &target); } -int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, - struct arm_smmu_ctx_desc *cd) -{ - /* - * This function handles the following cases: - * - * (1) Install primary CD, for normal DMA traffic (SSID = IOMMU_NO_PASID = 0). - * (2) Install a secondary CD, for SID+SSID traffic. - * (3) Update ASID of a CD. Atomically write the first 64 bits of the - * CD, then invalidate the old entry and mappings. - * (4) Quiesce the context without clearing the valid bit. Disable - * translation, and ignore any translation fault. - * (5) Remove a secondary CD. - */ - u64 val; - bool cd_live; - struct arm_smmu_cd target; - struct arm_smmu_cd *cdptr = ⌖ - struct arm_smmu_cd *cd_table_entry; - struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; - - if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) - return -E2BIG; - - cd_table_entry = arm_smmu_get_cd_ptr(master, ssid); - if (!cd_table_entry) - return -ENOMEM; - - target = *cd_table_entry; - val = le64_to_cpu(cdptr->data[0]); - cd_live = !!(val & CTXDESC_CD_0_V); - - if (!cd) { /* (5) */ - val = 0; - } else if (cd == &quiet_cd) { /* (4) */ - val |= CTXDESC_CD_0_TCR_EPD0; - } else if (cd_live) { /* (3) */ - val &= ~CTXDESC_CD_0_ASID; - val |= FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid); - /* - * Until CD+TLB invalidation, both ASIDs may be used for tagging - * this substream's traffic - */ - } else { /* (1) and (2) */ - cdptr->data[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); - cdptr->data[2] = 0; - cdptr->data[3] = cpu_to_le64(cd->mair); - - val = cd->tcr | -#ifdef __BIG_ENDIAN - CTXDESC_CD_0_ENDI | -#endif - CTXDESC_CD_0_R | CTXDESC_CD_0_A | - (cd->mm ? 0 : CTXDESC_CD_0_ASET) | - CTXDESC_CD_0_AA64 | - FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) | - CTXDESC_CD_0_V; - - if (cd_table->stall_enabled) - val |= CTXDESC_CD_0_S; - } - cdptr->data[0] = cpu_to_le64(val); - arm_smmu_write_cd_entry(master, ssid, cd_table_entry, &target); - return 0; -} - static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master) { int ret; @@ -1314,7 +1246,6 @@ static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master) struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; - cd_table->stall_enabled = master->stall_enabled; cd_table->s1cdmax = master->ssid_bits; max_contexts = 1 << cd_table->s1cdmax; 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 950f5a08acda6d..6ed7645938a686 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -608,8 +608,6 @@ struct arm_smmu_ctx_desc_cfg { u8 s1fmt; /* log2 of the maximum number of CDs supported by this table */ u8 s1cdmax; - /* Whether CD entries in this table have the stall bit set. */ - u8 stall_enabled:1; }; struct arm_smmu_s2_cfg { @@ -761,7 +759,6 @@ to_smmu_domain_safe(struct iommu_domain *domain) extern struct xarray arm_smmu_asid_xa; extern struct mutex arm_smmu_asid_lock; -extern struct arm_smmu_ctx_desc quiet_cd; void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid); struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, @@ -773,8 +770,6 @@ 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_write_ctx_desc(struct arm_smmu_master *smmu_master, int ssid, - struct arm_smmu_ctx_desc *cd); 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, From patchwork Wed Nov 1 23:36:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443151 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 042E8C4332F for ; Wed, 1 Nov 2023 23:37:57 +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=S3OByzlpJN3Qj1VF6dr0r18IpeWSQkfEJDcqZv1x9Ns=; b=pPh7rEZJPhwOdn FOMF8o4VHS4L39z+zlxax9vcek0jwGrLMwcmLjVnicn1MkF+iggF8YEG2xcQNEnK6fX9dyNW43PMI 2VqFY9eccy7U6gUegIsf/T6Vc5sz7WHB4bAKG4H71pv/i4v5ltasrYw4/Qwb2QnSrM7wLeqhMt9Mk ArnhrEG1HobOlAoL3No6x1ZDeRBC+RfxPhSmwEuwW+jdof0xlkKo6+ZXMd9dcGVG66TGNTx/f/XmI ikTvzVixrWeL1z9uDzJHHNqSOfQ9WoRsOJbWSCLoS9Q3llb734FFxqJdhgZOExqTAPfSVkD/Bhrmq H2gaXgS658k4FXy3+isw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKm1-008Jvw-2C; Wed, 01 Nov 2023 23:37:21 +0000 Received: from mail-bn8nam04on20612.outbound.protection.outlook.com ([2a01:111:f400:7e8d::612] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKln-008JbE-0L for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LuEZ5rGWHYyoVjDZk52nJ8s32oB5XeWDbaTfQiVg8gc8TGDElceaL+353XEjgnfAgU1OaUtrkEb5wWOYCqIq6lucQPqTVE0MVD0Uy7VJDCgge/JetvR+VqBftN4JCT4QHM6nswl+oKj0UfJbDEUguBXztaQE0JpHPu6+slPVVpxFCmGzVXxDJVbRE5+5tSEjh7aZMQk7b1pm1hNhKuoHvXObLg03Hr5ueR8H7Fj8dECNwXdArL/4yz8KgLCRoY1Q9B1MosFrU7b9UPfoQyEnNjuyR+kRmvMiL4DdyuV8ny4hf49/uzfv/NQ/Vki7DJiF/AAoI+DMOOjaS80IbseH3Q== 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=15Wnk3XLtzYXlaVtr5loPK8SXjDI7/lQEnXmVNRb2mw=; b=M9NpWx1lIRBtBsCCTb1ML6pxVrcgv1A9+dM6ze4nOMMQyGQXlJLDon3kLHo0xECb2x0rBj5UbLDW3i+fZIhcrq88wx/WDICNMWTtyKagsIGBUyfIVwxVkOYvoZuSpfbR6/mPD9nNb0eA4gM0eGSnztqttiV6PW9hN42EyeP9EXyUUxMuagoaE8AGCmqC/u0ju/s5tKazevmC+Kqn9faASbwBbiRZPByr56M20r/6v86+ExI+Pk3B+KLhgHTf8bKA6TnN/WXe3bnoflAeQJgGw+CClNta0M55d+BQaWLTP1yhqLENWWkLX7Levm8XR8tSHNZQcQm6ZhvEuRLNIlr7JQ== 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=15Wnk3XLtzYXlaVtr5loPK8SXjDI7/lQEnXmVNRb2mw=; b=goLPQYJbWyY+uMND1nxgJo5jCti0jjznWGHfCFTgN26p5dMy9d6z/p4BcilL4K/X38zBjLAbnVzn5GveFHTGDdQyt4aAjm0BsjN//oky06tSLnQX14EiREjty66Ta39jjUNaJ7joy9vsaQPtzSX4eUqNfkTDb0GYwUvq4so9UeNyC/Iy5e2nAGfc5CRMDpnnSz9AyfDKTPLPmCqk756fGm/nBesxPVlQdveVjxob7d5Fgw9mYtcRAXs/2ul9Ug26M4nD9yv70Vkoh0UaSGu7n7xrkrFyz8aZfhOr5BR6cqkKPfilDI6/piwBa8cUvpnlPYJyrVXj6viqC37uoQekag== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:49 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:48 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 11/27] iommu/arm-smmu-v3: Lift CD programming out of the SVA notifier code Date: Wed, 1 Nov 2023 20:36:29 -0300 Message-ID: <11-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR13CA0026.namprd13.prod.outlook.com (2603:10b6:208:160::39) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 16b27628-9f81-4b14-b96c-08dbdb3369c6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XGEdnDSDqMcP/Vowsmy9AcjrtWCZ0RwouuNIrq8CXV9pkFS6a1lYPrvepbIZ0FzZJgA+jC9abQqomeLcBG6vLOGT0IgKIsmFWGBBaaBq3Czi1+kWjfwnZ7GHyOkRrVU9zmMiAmYP8k1NaQo2bGOb6kFn28msi6660+vuqVG0jkyoKhcHaYx60MUD8xGPp/A50pi6rj7masVaFOG3jEhHGVVodeCD0SctP+X47r34Tzt9X9h51MB9sy8+tJDIHlMb0h6FZ7Jb2u40Ac+zlB4ZDbd56rbpinbtxvqn89QYXX1VAnCXg6+b8/qNmprShzPfS32z6fo2zDnqgqD3XvhP1pLIpYxw6Aji0ljV4DKwtsFPJaO9TpLtG08JbO4DBrEFvyETfwzF8rMEefClqEGF5OzMmAQ4QC7GHWw4ESrUklWmIHphbta+VmG3TZLI0ixxTjHlQMr+gD8j8tavnw+iY7/KqYzqz0p1h/eA+9cC3wBLMS0tHFj6srPKwuhR7lOOypSJzIgyq9usiikrvI7PIqiMgq28JEAeV62hJ/Eb3ClJXFl8r574VANFlHgDWuTU X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7l97IWaq8X/3KaRgHYheJkhXhnfcwPkKUzpMP6Crkejqo5/a3jHiOtT9Tk7YXEvYEdrruEqCrzx5Cb0Hv25O+KLXUU32qZiDccE9G8d59+7W6qg0ML3/46TNXWPBXKwEPgo/CKfZJu6rrAoSE8JqIXRyFRYa6CdAbNIacAptR0R1XUsq9IzmFLI1JerXjayvJO3vRJrD27aMtCUe/KPIaNlOG2kjL2guJwe+HOIE+UIBKMgyKBFS/ib+6GDlZSc8onqomD1xU+CV70VIKJgclDyZnTM/V2rCZ8UkSDIH3p9g7FZ60kQc8qEP6dznygiX73ZjonXOtlbUUoWnyQ/38HDv12sC1PmuzjFV+ePT2uZ0pXszsrFaMMWlKTw4NJjuFuOcfTlUeLpWcgDQxX1gOzybljKzo3kieUh1G5H1WDpTHve3TrOEbf7XJDrZXqJXBcZW+T/ijZfuvjvivlG2UOqfC+He7hIJH2+XbtayZl4Eo2I4MSdrvN3hYvBWK8qo5tQCM8+sO8XAuxoZtsm9W/U6xxvsUS3+dxsapKWnw6JEQp03TKrKPl6VquKzLzOcH7y0sVC/giRvHlEIzGsEVJHZTqbnZmMgOkDFF5aYXe+v4mpGtyEwQlj2Vb482SuVed3Uyk7Q4HV3ncg2Hgc/tWyMMNiIxvBDjopyuh14TRNENsnkMzlJvfEd3jH6+IraJF6igO62/JLskjukFYo+veqOH6eHZBv2HXjtVu6/6wZHGO2EEUwKAg9rb9sro0YMFN0CGbOPg3Je7H21MELXeC1hqM3ojU7yJ48EIXJf5r3NqNz9jvNZe6La6ePmWT7Ql3xQ9Oq4Z550OYJSAzCiODQnkrRYlAjYkCHc2SAwdrKJCmrKnd/ifZKcWS+9B5mkTQjNuiNfT+iIGQk9YhEt9vxTmLJ5romEXIBBGe5gZFa2bwpdcKy9gz9yHwxVcb5ml8IFnTzxsQAGK1y9GHoaYdm0eTawGaX5bRfkYtQfVOcG4qzhBmdb06QJSUwlPZYRT7Yn/e+KWEDd4qCcoekjFxSEXtbTtrK0bpeWYdvz3PUEiwvAx088WJ8HorduPvkYa7IiXUi+GNiZvDxxvG20e8OQXCAtetvq/QGWKBHJKpdPP/c9ojhDWTRa5CqEn0I2bVNfFgmqSg8xb+0oQegYTRwG2flCCm0TnlU85qaooLyQ60I0Goi24ieFx/1XIgjNIs4bEworIa6MPuL35qNwoGe88+UQnK0gG6Ymm/0VU0Bc1SZjAHhHvCMMRloU2SVyMJBPAhuzl6ZdUGIyPk1rYdubKt0NkCJtz++PsHasHqNhULd4CE1ZVtPxq2bUg6lrw5nSZmZOTzkWiRrvqK5820pIW5E4+olo6qi3YTnm7PRNh8zDSLaAeD+jquEF9g+PloVRQoAGk2ItKe2Fx+LviwS/AqWeuMYPIU0URzHmh6ZRWEDk4P1mArr7xiWonohmUobde6VitiU29j6Q9D0vE/BSAPN/lRgqZ3KEMoIQH9zflmQ7BGhg7QFKevN4A5ISGWtTHheo8ORRn1J6H5K+N4OUVP3NBkS7AXJjOlsAA7xbdHCv4ErC7r4iU90ez+iF X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 16b27628-9f81-4b14-b96c-08dbdb3369c6 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.9156 (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: izDaKHZb/cmrrs70G2vYur/7q8f2yJWIqlu3Mc1Bzmg0rIeqgYtelcIMYy4wqfs1 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163707_206516_E3333820 X-CRM114-Status: GOOD ( 18.00 ) 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 ops->set_dev_pasid()/ops->remove_dev_pasid() should work on a single CD table entry, the one that was actually passed in to the function. The current iterating over the master's list is a hold over from the prior design where the CD table was part of the S1 domain. Lift this code up and out so that we setup the CD only once for the correct thing. The SVA code "works" under a single configuration: - The RID domain is a S1 domain - The programmed PASID is the mm->pasid - Nothing changes while SVA is running (sva_enable) Invalidation will still iterate over the S1 domain's master list. That remains OK after this change, we may do harmless extra ATS invalidations for PASIDs that don't need it. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 55 ++++++------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 24 ++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 ++ 3 files changed, 48 insertions(+), 37 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 1f0940b497f3c6..29469073fc53fe 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 @@ -339,10 +339,8 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, struct mm_struct *mm) { int ret; - unsigned long flags; struct arm_smmu_ctx_desc *cd; struct arm_smmu_mmu_notifier *smmu_mn; - struct arm_smmu_master *master; list_for_each_entry(smmu_mn, &smmu_domain->mmu_notifiers, list) { if (smmu_mn->mn.mm == mm) { @@ -372,32 +370,9 @@ arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, goto err_free_cd; } - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) { - struct arm_smmu_cd target; - struct arm_smmu_cd *cdptr; - - cdptr = arm_smmu_get_cd_ptr(master, mm->pasid); - if (!cdptr) { - ret = -ENOMEM; - list_for_each_entry_from_reverse(master, &smmu_domain->devices, domain_head) - arm_smmu_clear_cd(master, mm->pasid); - break; - } - - arm_smmu_make_sva_cd(&target, master, mm, cd->asid); - arm_smmu_write_cd_entry(master, mm->pasid, cdptr, &target); - } - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); - if (ret) - goto err_put_notifier; - list_add(&smmu_mn->list, &smmu_domain->mmu_notifiers); return smmu_mn; -err_put_notifier: - /* Frees smmu_mn */ - mmu_notifier_put(&smmu_mn->mn); err_free_cd: arm_smmu_free_shared_cd(cd); return ERR_PTR(ret); @@ -408,19 +383,12 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) struct mm_struct *mm = smmu_mn->mn.mm; struct arm_smmu_ctx_desc *cd = smmu_mn->cd; struct arm_smmu_domain *smmu_domain = smmu_mn->domain; - struct arm_smmu_master *master; - unsigned long flags; if (!refcount_dec_and_test(&smmu_mn->refs)) return; list_del(&smmu_mn->list); - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) - arm_smmu_clear_cd(master, mm->pasid); - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); - /* * If we went through clear(), we've already invalidated, and no * new TLB entry can have been formed. @@ -435,7 +403,8 @@ 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, 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_bond *bond; @@ -462,6 +431,7 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm) } list_add(&bond->list, &master->bonds); + arm_smmu_make_sva_cd(target, master, mm, bond->smmu_mn->cd->asid); return 0; err_free_bond: @@ -624,6 +594,8 @@ 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); list_for_each_entry(t, &master->bonds, list) { if (t->mm == mm) { @@ -643,17 +615,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->pasid != id) return -EINVAL; - mutex_lock(&sva_lock); - ret = __arm_smmu_sva_bind(dev, 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 4ac32bb91ebf27..e165859d0d0e51 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2601,6 +2601,30 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return 0; } +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_domain *sid_smmu_domain = + to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); + struct arm_smmu_cd *cdptr; + + if (!sid_smmu_domain || sid_smmu_domain->stage != ARM_SMMU_DOMAIN_S1) + 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 6ed7645938a686..1c756803b7f963 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -770,6 +770,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, From patchwork Wed Nov 1 23:36:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443143 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 F012FC001B5 for ; Wed, 1 Nov 2023 23:37:41 +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=/eUjSOdrlaBKtPnbxnfudxVZU1EPhu2csKTaQJA+SY0=; b=LEo0zyCgSrCROM 4vCc9vYW09YTYTe61GVeW0rS+8+i2vVN92Da+/6SnHJ4iG8kIT0gw/X0RGoZcA0X/kSJm7uPi/NWN T9nbiQAKqbOqIkLwgWUKGp+uFlrklVOH0168wueW5vN3qD+FbrqS55kauBE30aBMDpwNzYUZulDRg gMv1Y8dxWHatBwNf2iztsM4oF4rtigw/EWdboS8UgnTFJki5M6R1A1rOFBD3eSscrfLdaTrtilHZ8 aLUjzMw73QlUQ41Zz3k9sUAfappTKModk7so3k4BN8/Ff7KCFTgPpJLxsmwAPSmypiE8nwzXBKJCP 0PBEzXrV8TEuOY+pClQw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlr-008JjF-2Y; Wed, 01 Nov 2023 23:37:11 +0000 Received: from mail-bn8nam04on2061d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::61d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlh-008JWb-0t for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F+LkRdxjSQJyW3Sn+AHPZy/YSAt2NElNx6Pj2vfdc/sbigcKPL1t5bjm1tEFuOgyC5RRnkSbZyBAd8xpJJCDCIN8Ig3roGj9KLCDBfA4zwM1DRP3VWPyJQ3ukb0Dcydtk/uguutiygOx3jzUSxM/BPOwGSHOyAqtPee/q324qEe3CR9i7IJbdmL+Cnt4rEqA8wpvVrpGuQyAivKrR2QZk7VRrraGlupdnvonlnww7rbeQ6r9pUVoGvC8yL/EesXgnKCme49fo+tQNS7a6/tZB22RVPcQaKG9tjjuarpusvx/Ha5k0Tf81odzgbYYRQnyQdq9L5tLOB+gUM7+s82UGg== 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=h5MLnzAZpw18jF+ZotGpvk/4g44rq0LyVvJ9d2VpTdE=; b=Cr6hbu2i0Rw23hCJlhUbbb+UL5tDqS7FNAwOzdmJDikjJvTJACl/qiHBSPjXwodMaM0b8Hp16EmqrBbb2/gHvsx9gEUfas3mad4UKVpygtTeaS3dEhluDznzf+X0pRqCAcHrMl6l6iYfOuwOPRVyEcjs1fn/Plot28BgN3AgUoxNCwEn3xU8jqh69h3znPfZQu4fet2CDvLF+NmkelJsFTETXVeW1yid0ycBdQiaKw5O1KWrq8C+9Az5iCYUSgt/HZkPW2ApA+1MHlGPvmrhOfSa3l8pW2Qzz06xTf93etcYK5l+Ne/BGPZ+OtEpKvW0jOkZaU9v7NCHxZRTxMchFQ== 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=h5MLnzAZpw18jF+ZotGpvk/4g44rq0LyVvJ9d2VpTdE=; b=kSsmb2r3mpIrtnm0osxgmXLG6GOExsfnnE9vNQiind5SrZ/AgkYkJdhob6rLTNf/9eIYdr+KTrd9gbJjyOdSLduBnk/iMxuib18HtoMvAOud0fMDxFVDYyiNcgXrtjBj3qjX6Gke0iFQx8pLkXLXkieUuYtXzoSQr4/+9MbOFEx4/MylnKtlEpg+PH1/AC4Tc9CqhFmrJAfXTauQAYA7z406NGKwQGEKNpe+PQLIORruRPpKyHWmEVtbQpDZH03kJ0WqSSylxuQOVwV/n3sx/0BrR1UlrlyOnGfkib+Ucob8K9W2N2y5aC0sxJNpTL5d1awRU41nA+6lE3HS+KIjgQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:48 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:48 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 12/27] iommu/arm-smmu-v3: Build the whole CD in arm_smmu_make_s1_cd() Date: Wed, 1 Nov 2023 20:36:30 -0300 Message-ID: <12-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1P223CA0026.NAMP223.PROD.OUTLOOK.COM (2603:10b6:208:2c4::31) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 69374bf7-ba4f-44f6-7fac-08dbdb3369ac X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: G3Qd4Om03HNyo/HLOmZYeQ1K0gGUvsYAADvLsCWO9YtGUEKXbKp/DaIGI/+76ZY5ajX8N9wPrd3dHErP1Brf8FoSh1qxJkSJis+TxEYn5hCms60sbRBAjz00/3dfhuwjB4GiXBEQW4x79f1on1paJb+0MYewliFhmK5XSXZia6iSojEr+VgXEuhlmj/MUkHsOLz2o9JgmvsjWoe5xB93Dy3k7rWyFAd0LLt7Shq1DHRr9TC/p7Rvu3A7QvH2Gi4wd6BSvdPbehAjhY4L6CSFsrPgjlIcskHQqOIhXf/vdCDrAw1tLAOLhNz16NnMSdvOYeYeY8Od4dJg6azQqOxb61bzloWrRZrhsSZM4weffaBY8YS0+p/DV8fLz272ZGBoP+v9WXjjyZMzsgo+HHBcyz3GDcvG/3GXg1WUa8lBfGZFkIGSxQDpgdLFpRNlFMcEWa94I9Tgoifxl9pY3GqK4vCfdmlakQuSTlDcXY6BTrtmkIyK6Gioja80+z9aVLdf4BO3r6xz+e3atgxPsP9Us9VlpPdwgLN+GWDRH9/hEFfp1pBZxQ7mUttr7QImUP/9 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8QaSo2q93dRvw4tgeiPDuZl/VzSbLlU444aVGYaYDmkCWidAICxgOGE+VUeSL2kwT5IZgVE7r6qiuLTMDHyG4+QOYm995zldsWf/Vg0UunZiG+52+XFKzC33aPxHFHtihqW5QuX1aPDlZthMG5590/1e7qCa5jC0rpV2Pyyxa96O/zXno4OgmOkZuIrqOHaGOjJ1DtdJkPZ39JNPY2q1N8EVBqslseL4GHimT87ngo/hQivDdGYtQDBTaX8f4ovc0/GY60Ra/IoT/xBUol5NL9EfJFKa6Pmj60jTOAIne2XWRwppCDhlLnxDicR+JYoT1EBjPbgKEDZV+7/0bYT5QzZJ5La5JHX0oGaIGZmHaIZeVcCztj0livIJfCLoqnONmFD/SIS4MvFhWY98Vaz9jYPT73Sc6+45C1/zvjzDinO0SL8eJ1G+o24iueGjd8/ZdeAuEZRRe9KQRNupSdkc1F+yOfxGhckQ3JUVZZ3zGr2U+53q7LGBIhHzBN01X6KREf5l5K4ic4aEU3DhjliaL0AP40WITKlluQDTEJtivABTLYvVtu7u9XlEA/dRi3L1wkvVsFRYM6j1Q+gOJI4PA0ZymkB+5VJlUPRInfhsXQ4EZ7Rd+24pHak4YDBiWZulgQiAncyuWY13V38nZ51+buGZs9r/g6yk01/MCawFGrinAccfF0WXt5EEvo+CknVsOxRHby6mhzuhE7FgG/qOMIxtkMWwk5rDAqrv3Mw7+U4lK8fbtKSpIUugj0FrEJ8X6d5KN3VSQgxpmHPEmGIIDSMqHsNaUosQwYoSeWgeE/xllcdeinB7BTM20K0Ev/t+BCs493xDp+kG9NG2jgspavKwTALioukp/A8dn/bvmziMWBY88oynzol3+ZroACNb26MhfzsOUAeZii2aI43BDzq8ndyCr6T/K41n9+Z3Fa/087XnncJ1I7sgPCeYctsGmUMBV8Ocns9k0Zrv6dN+BZq/3HhdpDroX0coQ6Gm2YEjDbWZoPS7nNQkv9H073ikhEH+pt1R+ZNMAEXuMevKaXlSyutxrev7ScCTf744STMtuQJuWIIcitzU980ySs8snxUfNFqcVC1L82ROFe7xmWWnAnQY+yWZbpOmkHKRcMsWuNj/gmMPSU5Ep69ljeTaaJsLLFTSPFC2n7BlSz0DkcTVH44fSbx6E3EtLxC6elwKy/ypwFSqQuzfHQsuafxYJr/OjKA5tkRirdI62W3+8C4uGN49Eq66bNcFA2g5CHO1OLuRaVIz3bmEsKF9OwxuwZl4homqXN/byPQT+/611oqoq5yJ65foxYDZ+PnClv6m6m2e8FOnSzp6AI877GKlxlDnTeLqcRVv4pFn/OSeuJs6m7eeIuv3exsGXbUeE/T+OPufUSYcCgl/NmGoZKWh39iWIinXQEl2BS+ivAAeKNCEzbJpMUR5ASSHtq0bovlFoLaahGLEk3EC7TvsGR/8CxfdH+Rfq/BO8E7GZ9v61iORwprsd1HKHeC3XaadH8PkE+gGdpfmVripwqYrocmhoKOTzfnIfstgoCuEc8azZimJNWDJjrYmfRB4aSqOh8mA9KFOl5b8tXqit7h6TW8D X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 69374bf7-ba4f-44f6-7fac-08dbdb3369ac X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.6811 (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: ztkUMZkM0xn09BhrtcvaqCOVfkL1afvRO5YvOf7GdtsP35hhI/bElLaMIgtelPqf X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163701_314583_17279917 X-CRM114-Status: GOOD ( 14.06 ) 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 Half the code was living in arm_smmu_domain_finalise_s1(), just move it here and take the values directly from the pgtbl_ops instead of storing copies. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 47 ++++++++------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 3 -- 2 files changed, 18 insertions(+), 32 deletions(-) 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 e165859d0d0e51..c62c677dca1d9a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1204,15 +1204,25 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, struct arm_smmu_domain *smmu_domain) { struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; + const struct io_pgtable_cfg *pgtbl_cfg = + &io_pgtable_ops_to_pgtable(smmu_domain->pgtbl_ops)->cfg; + typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = + &pgtbl_cfg->arm_lpae_s1_cfg.tcr; memset(target, 0, sizeof(*target)); target->data[0] = cpu_to_le64( - cd->tcr | + FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) | + FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) | + FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) | + FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) | + FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) | + CTXDESC_CD_0_TCR_EPD1 | #ifdef __BIG_ENDIAN CTXDESC_CD_0_ENDI | #endif CTXDESC_CD_0_V | + FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) | CTXDESC_CD_0_AA64 | (master->stall_enabled ? CTXDESC_CD_0_S : 0) | CTXDESC_CD_0_R | @@ -1220,9 +1230,9 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, CTXDESC_CD_0_ASET | FIELD_PREP(CTXDESC_CD_0_ASID, cd->asid) ); - - target->data[1] = cpu_to_le64(cd->ttbr & CTXDESC_CD_1_TTB0_MASK); - target->data[3] = cpu_to_le64(cd->mair); + target->data[1] = cpu_to_le64(pgtbl_cfg->arm_lpae_s1_cfg.ttbr & + CTXDESC_CD_1_TTB0_MASK); + target->data[3] = cpu_to_le64(pgtbl_cfg->arm_lpae_s1_cfg.mair); } void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid) @@ -2241,13 +2251,11 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) } static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, - struct arm_smmu_domain *smmu_domain, - struct io_pgtable_cfg *pgtbl_cfg) + struct arm_smmu_domain *smmu_domain) { int ret; u32 asid; struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; - typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr; refcount_set(&cd->refs, 1); @@ -2255,31 +2263,13 @@ static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, mutex_lock(&arm_smmu_asid_lock); ret = xa_alloc(&arm_smmu_asid_xa, &asid, cd, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); - if (ret) - goto out_unlock; - cd->asid = (u16)asid; - cd->ttbr = pgtbl_cfg->arm_lpae_s1_cfg.ttbr; - cd->tcr = FIELD_PREP(CTXDESC_CD_0_TCR_T0SZ, tcr->tsz) | - FIELD_PREP(CTXDESC_CD_0_TCR_TG0, tcr->tg) | - FIELD_PREP(CTXDESC_CD_0_TCR_IRGN0, tcr->irgn) | - FIELD_PREP(CTXDESC_CD_0_TCR_ORGN0, tcr->orgn) | - FIELD_PREP(CTXDESC_CD_0_TCR_SH0, tcr->sh) | - FIELD_PREP(CTXDESC_CD_0_TCR_IPS, tcr->ips) | - CTXDESC_CD_0_TCR_EPD1 | CTXDESC_CD_0_AA64; - cd->mair = pgtbl_cfg->arm_lpae_s1_cfg.mair; - - mutex_unlock(&arm_smmu_asid_lock); - return 0; - -out_unlock: mutex_unlock(&arm_smmu_asid_lock); return ret; } static int arm_smmu_domain_finalise_s2(struct arm_smmu_device *smmu, - struct arm_smmu_domain *smmu_domain, - struct io_pgtable_cfg *pgtbl_cfg) + struct arm_smmu_domain *smmu_domain) { int vmid; struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; @@ -2303,8 +2293,7 @@ static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain, struct io_pgtable_cfg pgtbl_cfg; struct io_pgtable_ops *pgtbl_ops; int (*finalise_stage_fn)(struct arm_smmu_device *smmu, - struct arm_smmu_domain *smmu_domain, - struct io_pgtable_cfg *pgtbl_cfg); + struct arm_smmu_domain *smmu_domain); /* Restrict the stage to what we can actually support */ if (!(smmu->features & ARM_SMMU_FEAT_TRANS_S1)) @@ -2347,7 +2336,7 @@ static int arm_smmu_domain_finalise(struct arm_smmu_domain *smmu_domain, smmu_domain->domain.geometry.aperture_end = (1UL << pgtbl_cfg.ias) - 1; smmu_domain->domain.geometry.force_aperture = true; - ret = finalise_stage_fn(smmu, smmu_domain, &pgtbl_cfg); + ret = finalise_stage_fn(smmu, smmu_domain); if (ret < 0) { free_io_pgtable_ops(pgtbl_ops); return ret; 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 1c756803b7f963..b7a65e37c51e9e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -587,9 +587,6 @@ struct arm_smmu_strtab_l1_desc { struct arm_smmu_ctx_desc { u16 asid; - u64 ttbr; - u64 tcr; - u64 mair; refcount_t refs; struct mm_struct *mm; From patchwork Wed Nov 1 23:36:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443209 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 16834C4332F for ; Thu, 2 Nov 2023 00:45:46 +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=ed4M5CZLDDjkPU96iUvuJZUBFGOfypr/WZgQNw8PqzY=; b=OvQLZVfBqEZrE/ TbkWKt6mURTAeVi1cISMC5Y8vIXPfPMLtJ0UqwbgolU78hDlj42WP7gpdu7097FEwcRlrIyg8/JxD bMTVZ27L4EJCjvtIvrGuGuDdIYFpxv62kmcYSV+O+OkDSnIWLSEJVunPheP3NzPTAao3Pa1KhbG4T pJM6lUocSAdblPoShCYqvR8Osu++PT+L1MGuQ4YFXJKKLki+b49oKYySy1Cqq9ETxZ/YCa6i99LHd cG8qpSqJb50DBeuSImn3nb+L7IpAp98qlFdr71ujRQyU0gW01wwzmc1WBk1vZfIjOHM3dB8AwrB6J X/HKOeh3neW7W53ubJSg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq6-008Pyl-0I; Thu, 02 Nov 2023 00:45:38 +0000 Received: from mail-bn7nam10on2060a.outbound.protection.outlook.com ([2a01:111:f400:7e8a::60a] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKls-008JW7-39 for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OEW32qYi5V3nCDRrvN0BRCFlr3EYG8k5lK+hbUgpEmihRJg6dd9bwseQ7nvq4Dl4uNj89xkvSKW35ebJXKvwQBjf4epFrxPz1Y20yfhgHvjTyOCkfR9UTTdTJ2Si0rIqI9dwqdDMKXgXcaGIWrgmHsc9M6QWlad/j250HJJFouW1vtyEVKu969LAWpPXB5/daU+rh6GBodUJ53WuSFTGpSkuT1xFVs/VKuL/eRDe8uLmZbHlesHqeiYYN+rkxayDcIwOdm74Q/T8T3QVGs9T9qsSu0YVOK/IOAVIQgVRKsFxBFCawEcBhBaapM769WKldjtr58CVGG1/oEizMXH0Ow== 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=kK7UQ0Se2IJLU7Yj7KUz12M1jB6ZPu/fxr5N7tdzgYg=; b=m720SiVZxvmFdv6kniE4vTaz0o74TdBI41ptKcoqXbCs3PIi4IFWGZeXb74JDazdb9WCm7p23GezrR5hUFXttG41z50qNJ/SjvhrHkvrwDkyKvr62UlQBhTbtPGUMf6WtoACPr7f+a+YuV4Ctm5A649hKCKrvm1CViM8WK/7eQR24v5yL/3YCIcSWn4bOOvtJ2rD0a1YAkCI/vzD/TU/DUWyKY0Timlf+uHZShpkFWL3hWTJ/Zh05irl/vFFz+QGwtPBjwGxBCRh9MV2cGVeAbs6No5YNL79XHmpyjHGGn6WsHidSavFMoJJeEieksg3fMIOWL+qW7wPa5NZwlcFxg== 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=kK7UQ0Se2IJLU7Yj7KUz12M1jB6ZPu/fxr5N7tdzgYg=; b=Dsmp4xQkNBOeLmLHEf4Hthvawus9k8Z4tVwvTtmlIesdU7isOZzGy3pC8faItYyV1PwFolWFmPg/hpv8BAiULN6Nf1L+02jUU2DLAGNsFtCu2o37t73bQKLKxjXSXEt1KTxC013kPCF88xZ3jqtziJki7OSCj0+4eST37MUHSfubA8wPTCUqEHiuFTVcoeQIeJqDGsmpynuTnWYWHAQkW8PmyplLKHzd+et4zKOFZnqcc+931Xa0HbvkDO7OTer2cYlIV1mebAhwfGdP0NJtienLNK5Qz/V/YY/gt0+zyMk1S2uL2mg+TbvWhkaY4RBAnmPje/CUBft3C+Q0LvdLLQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA1PR12MB7664.namprd12.prod.outlook.com (2603:10b6:208:423::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.28; Wed, 1 Nov 2023 23:36:53 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:53 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 13/27] iommu/arm-smmu-v3: Make smmu_domain->devices into an allocated list Date: Wed, 1 Nov 2023 20:36:31 -0300 Message-ID: <13-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0017.namprd17.prod.outlook.com (2603:10b6:208:15e::30) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB7664:EE_ X-MS-Office365-Filtering-Correlation-Id: 091c9dca-2ae8-4e57-4a87-08dbdb336adb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: W8kBDwPCT5QxOBSFo9b8FAkHDTGyqjNl8uzD+R2jmrIR+jXAZQKa8lq8GkWHkRATReX7H+UwSSZnNxjz8PBcsofKFgUU9bcyuajKhFFC6HY6llEHV8TGA1Sv/zfAHa6mYi59SSUtIEUjWSVHUGzy2GiMmko2X5h64B+r7jtoVJFO2gklCxg6pwfcshEJo+PqECel08LaXxRAQy/j21qpKQOYtlBp77L11ZebL3QgPaYo/Etuf1Q1o65T+rE5Tz2gqU2NWTe8sVNevcSpw/5j00Xfs3hiSHINW2NgJocMNZN7Eh88jzu5mR3+UemK4RmVN/u4I8cg09+inSKF47FORKHtc/3tFRsyG7UUbjiPjxCDlDSRf8LfpwC30+H0yGCf3Dieamsj2FJFf7dGu1CWyTqE3xcQX2ZBkX7ynwDlajQNoKU5un8HsIpTVy20nVEwtGBq4xk+v0HU4pwhipC4szfZEbHX4QNjhH8BlKUBO/cW0TmtougVyRwfzcwjc3KT3ZA5miZ3+ydBgf+paBDrPa+ntwo4cpyBxi6FvqDQAM+eEF0SFcpS7zaMzFIxOiVq X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(39860400002)(396003)(366004)(376002)(136003)(346002)(230922051799003)(1800799009)(451199024)(186009)(64100799003)(110136005)(2906002)(86362001)(41300700001)(8676002)(8936002)(4326008)(5660300002)(36756003)(478600001)(6486002)(6512007)(6506007)(38100700002)(6666004)(54906003)(66556008)(66476007)(107886003)(316002)(2616005)(66946007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: k1XtkaWc6OQ8iLqW/I26YR4POSI34tn1jyEs7e0Ip0kwy0c8rJJohTSRoaqwVVia/DyrQdxBDmUHTY6es9iyW3s6JLyY3Kv9cYh4I1ug3ZeNnEuMZssUWJUbHN5cI9n5OvlPWNCJxN6zQWOrWXL36SZEJUtqRfzq6kF4JHr0f8v1eQVQ5ZPAxivNDxcJN0yUqEIMtbAYcG/UlrKmViYlySA2aBhIixLGKo0P40a3Jgj/ZSHmtpFABk1bbZxkhGmxW5JD2t9VWFUTKn+a7+nn8cjctU9cW5bTL1wSwd8MnXfjMgasOLcd2wpyFM2nazCPQZ0A36Z63KHXhSieDu1AU3zHSIjrPNSl2MeMs9KkGnRTJqPzzcU/YSZzG8KFvqvXJQ48oTsPylFtHBXWmHi4k66HmifWREksmdvyHMF9Ff20H/l+B3E0znc6CZ1OOHSu4h5/E2+fGc5ZuVwQ9RHEGxOWP9y786NHk0bnTb3OJa69pg7H2FN+74Ua1ZKn4HKoM/ieTApv98QDN/Q8NdjKq4YEhYde+T4Xjzp+vo6mazTBUVs+KBjcWbhlbqzkuXQpo5EA6eWNKmUmBF5lOtBrCBsBsFvMdGhsgyMq1cjYEZF9FXizYJpz+imEdjPEekcxz5DUw87Zhsib1NDQJN3ITR1eOFMIlNtqeKfEAhS1VvH+FGnJYCozINlLdQkju5lINGZK5FIOT8RVcr3rlH9w/eiDeOaEVtI1zXRy6x+AQt90lQ64SPuAZCc0YuftK3NeJM82bDRV2T2OctTKPY2W+5w4y3JUQatueL9zRQJCmgw5lKLmCFChw1YbidZ5H0H2WcZUPxRR6OZoCEelZCf8wEIzNi8iwwAvu8dOh1e60reY3F4gV1SJxaB661uLrskp1i1ZUqX4duHrfAyNrwf5MpTacs1vPFZCIX5WwiEFezmhaQD6srJvbolWDpCifuTPXGwYts/yJfxwZz7JUF2qYZ0xRuEK5/2jxlry36Vbcsqz2QFJb4NF/2QdiAfFAZ4PIEyyIgmIZvQneUgrCJZjI9gZr4kS/+jRg75CLsgQakFxvcfA8+XlOdj3wBEVAX4dqvIFmLFAtXs4bmQDCmqxEiqVyABSAv0pAJRzn/89n6qxAnB1MFXYxZyhKgV63RMWh1fUo5kW3ne8PnzcY5fd/GWk4b4rbBd5Pjlq9TM/kWsmvweh4jrCwqpGTQcrge5bJZVinJ4m3kpmFsKcEjMQzJB32sMTNOXS7Hv/44zC3bUVp25RVRBRoPACMRbF3dXx99kAyjJFj4AlmYTfXv2GPLbZRj27YqyiH4ShLFR8qNe02kbvXjnESkmnyjQdZCnbOT//5MkAxxxvGg2ArJYHzhoYHT4IbT99ta3YMC1ibVe/EP/FMaGq6NaHjkRCo8ttp8wu0A1pbSzG1PP9Co6T93/fJBXq2r90bs6xdKHfWXcqc0DkY+B0EAh26TBYqo+oKh0mfMhzdZJkDZN1dXzFUIycHfafgxLX1nZatY1y8H0qLy5exFs+GcHztTFxARt+GrDrBQxHnttHAQ2yfeCDc15FHKaDp897jKfGIT5+ktGowfCZJIyClFX/hHUB5Vav X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 091c9dca-2ae8-4e57-4a87-08dbdb336adb X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:48.6408 (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: yYGxzo0VdWqvFU1t9T+Me4wVVfPTGVLmMl+fPU3fv0rD/oBO92zVK0ddZSYbt/qK X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7664 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163713_050861_F9BBCEC1 X-CRM114-Status: GOOD ( 16.52 ) 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 The next patch will need to store the same master twice (with different SSIDs), so allocate memory for each list element. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 11 ++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 38 +++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 7 +++- 3 files changed, 47 insertions(+), 9 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 29469073fc53fe..c541b94917d036 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 @@ -38,12 +38,13 @@ static DEFINE_MUTEX(sva_lock); static void arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) { - struct arm_smmu_master *master; + struct arm_smmu_master_domain *master_domain; struct arm_smmu_cd target_cd; unsigned long flags; spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) { + list_for_each_entry(master_domain, &smmu_domain->devices, devices_elm) { + struct arm_smmu_master *master = master_domain->master; struct arm_smmu_cd *cdptr; /* S1 domains only support RID attachment right now */ @@ -289,7 +290,7 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) { struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn); struct arm_smmu_domain *smmu_domain = smmu_mn->domain; - struct arm_smmu_master *master; + struct arm_smmu_master_domain *master_domain; unsigned long flags; mutex_lock(&sva_lock); @@ -303,7 +304,9 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) * but disable translation. */ spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) { + list_for_each_entry(master_domain, &smmu_domain->devices, + devices_elm) { + struct arm_smmu_master *master = master_domain->master; struct arm_smmu_cd target; struct arm_smmu_cd *cdptr; 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 c62c677dca1d9a..3aec3791ccbe9e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1969,10 +1969,10 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_master *master) int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, unsigned long iova, size_t size) { + struct arm_smmu_master_domain *master_domain; int i; unsigned long flags; struct arm_smmu_cmdq_ent cmd; - struct arm_smmu_master *master; struct arm_smmu_cmdq_batch cmds; if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_ATS)) @@ -2000,7 +2000,10 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, cmds.num = 0; spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_for_each_entry(master, &smmu_domain->devices, domain_head) { + list_for_each_entry(master_domain, &smmu_domain->devices, + devices_elm) { + struct arm_smmu_master *master = master_domain->master; + if (!master->ats_enabled) continue; @@ -2489,10 +2492,27 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master *master) pci_disable_pasid(pdev); } +static struct arm_smmu_master_domain * +arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, + struct arm_smmu_master *master) +{ + struct arm_smmu_master_domain *master_domain; + + lockdep_assert_held(&smmu_domain->devices_lock); + + list_for_each_entry(master_domain, &smmu_domain->devices, + devices_elm) { + if (master_domain->master == master) + return master_domain; + } + return NULL; +} + static void arm_smmu_detach_dev(struct arm_smmu_master *master) { struct arm_smmu_domain *smmu_domain = to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); + struct arm_smmu_master_domain *master_domain; unsigned long flags; if (!smmu_domain) @@ -2501,7 +2521,11 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master) arm_smmu_disable_ats(master, smmu_domain); spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_del(&master->domain_head); + master_domain = arm_smmu_find_master_domain(smmu_domain, master); + if (master_domain) { + list_del(&master_domain->devices_elm); + kfree(master_domain); + } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); master->ats_enabled = false; @@ -2515,6 +2539,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); struct arm_smmu_device *smmu; struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + struct arm_smmu_master_domain *master_domain; struct arm_smmu_master *master; struct arm_smmu_cd *cdptr; @@ -2551,6 +2576,11 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return -ENOMEM; } + master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); + if (!master_domain) + return -ENOMEM; + master_domain->master = master; + /* * Prevent arm_smmu_share_asid() from trying to change the ASID * of either the old or new domain while we are working on it. @@ -2564,7 +2594,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) master->ats_enabled = arm_smmu_ats_supported(master); spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_add(&master->domain_head, &smmu_domain->devices); + list_add(&master_domain->devices_elm, &smmu_domain->devices); spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); switch (smmu_domain->stage) { 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 b7a65e37c51e9e..59bb420af52e28 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -695,7 +695,6 @@ struct arm_smmu_stream { struct arm_smmu_master { struct arm_smmu_device *smmu; struct device *dev; - struct list_head domain_head; struct arm_smmu_stream *streams; /* Locked by the iommu core using the group mutex */ struct arm_smmu_ctx_desc_cfg cd_table; @@ -729,12 +728,18 @@ struct arm_smmu_domain { struct iommu_domain domain; + /* List of struct arm_smmu_master_domain */ struct list_head devices; spinlock_t devices_lock; struct list_head mmu_notifiers; }; +struct arm_smmu_master_domain { + struct list_head devices_elm; + struct arm_smmu_master *master; +}; + static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) { return container_of(dom, struct arm_smmu_domain, domain); From patchwork Wed Nov 1 23:36:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443214 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 CF080C4167D for ; Thu, 2 Nov 2023 00:45:48 +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=WstFI8b2wtjt5TXYDuYsIYmSEhEO4U+0JkXgYvsPj5Y=; b=ycVqSY2jJSxzYM XonrIjjswojZlzMnGe2ZpTdUigybQ0RRTKajcK+oSTp7moaeEQXEZoY5+HJfMMkLreiTOU6gtf6n7 JuxQwF1BHj1Qa2PD11FuqeMmSo4R0UnFUMXbSClmPQbaFEA7MZBZ8J1T+02JKEnF+n60pw8wnyAF8 AgEPWMH5N3/JEETSR96kOU7tG1qruz1Q/s9j0qfLIlmInz/iP1uG4LFoahXwLyJGtjhPoO+EjvgmY OxY3Q3fw7iYFbzgr4O2esdr3lu9ORi1pK8K94zeyNSSBodgVlXFuZ6g16jPmKIo66t1IZVuQLvvMR +Evja5wTiy/FusG8HFIw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq4-008PyZ-2g; Thu, 02 Nov 2023 00:45:36 +0000 Received: from mail-bn8nam04on20610.outbound.protection.outlook.com ([2a01:111:f400:7e8d::610] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlo-008JV7-0u for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jY1bWjNXCVyE7Ib9I7WVNYxtlD5YfzhOs6tR0xyE95KW8wyHe5QbQ+SqQZJpv16guKiPXWBDamt+2/TQ7ZmGMMQtbhSJGmXVS2ngGJDXswt3oa7bx9JqOPQiGZgk59o19DPU7dKmDUltO8sV2x66D86Vc/1EzQbGzaqtdPt+n9ZV0yxevwd4WAnZdrnjJMJa71tXtgOmAq1OxXDg6lBrarU8Nj0lJn88OszxVNaGUlo1MCTVeaZ00kGziNqQOggO3llNlMlij//aKYn9nI8uGqIwBovNCaDraAN82+MDoCyQjsebn170H0Trvw4iYfkz/YTGJfsXmB/Xe95WqgJRdg== 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=5MDw4+qykY7l4ZDmYRoCpipvLJbgBuHNhHPr7KCGe58=; b=KEgqe9LScQgGLP3h6ls/yVHaWzsMeSZVx+mVVQkdNYvjpWb8u+9fWPjODaJh3I0heW5ofTTQU0ImEWV4oxN32CmlxTRsYBQU0ayUtnEkksqgllCOhW1h976Nmdh3Esm0/S05kTG9DBbDnaLQOxxwjxgaDceH497WUM9JDtEjJ3Xmd5tngcSQlwH/QmkAPMuqPXqyF1uq8nZ467wUwYeguNP3Un3GNozV8WnV6L9E0Bx8uTvaRm/r3ghrR/pPOwuNzOWCTW74ySRGWE4H3I1LESGvYyCg6/ZHVy+FhKcVPZKpMSdufJGHHmPW4H3oM7cCVtJ5JEe5FrO1H6WVvIKP+w== 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=5MDw4+qykY7l4ZDmYRoCpipvLJbgBuHNhHPr7KCGe58=; b=sgZOONkNH3vj7KBPpxNkhQ2XMlOSMKAOntpxyLda8z9ftAWRqeKQEWAYnIKgL7F80Y0K8r1EPPby+h4R/FLVqgZT+GUwS9DPIOiqqzU0HiXx82cqxWqmxQfsL2dY8czFQDLjNdIs3V5U0TEgsZMTJET5VcojXbfkAwQDtYYtafiXt9gcgm8xs8CdBM9m7BHOxvdtIlg7vByUnyeG+/Tr1cmUVk5f7KlFzpk4WMR9LEF8D1MsJRAHsBqSquS9k3iImyX+GI67rt35UWrEobz7xK4nbYOfTFGKcFsIEVwhReiTjpht5JrpODpfmsq0iTkoPFT6QVW6u6TEZPMAENo1gQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:49 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:49 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 14/27] iommu/arm-smmu-v3: Make changing domains be hitless for ATS Date: Wed, 1 Nov 2023 20:36:32 -0300 Message-ID: <14-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0021.namprd17.prod.outlook.com (2603:10b6:208:15e::34) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 216dc09d-d917-450d-55b5-08dbdb3369e3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UZNYOwzcL9RHRZ2iAfDQPKVzeoB1C9ZdG/js3rE2UzBYua8XHeG0RLbuTSVSV4sWv/mQ6P0JaMtN7oYTPHjEoqBsiRcK3MSS/nhIYZis6cO/xyVncvekJz5Z3Dh8+yFMOc1sbqACNXPxE/+lgLmYtgrPxwyiIlnzEWD7oDzCyCiGexvmntUNcTp6USpfIFqT6ElZFQ4FSeLn2MnWz4VKhxLZkCB8944MEWtajIuvp+ljL3AZOExZvnsDcsfyiLdJyNbAIgl+rWbDgOzkkU7xZsjxATeNkg+i+jLDSFrGyYyewbNowD1T3eavevYMlagoiPvZG4oaQ0SEesyLV2MOZligV8W9m04ABlFELrMKDTGLvK218tfh9Wj4p9Fe1HQVe3rd96qgcUxzBm2qmAQ9IrOikYMNqh6YhdHk2P4A5dnV6bDV1N3byswRYGJeN2DL5S9bdtlwOX7l01BdSe/W1vqAGslVMXIFAw55LoUG0t3tDL90jl2odiLPzVdlYkg03hymQdHUMhMWe9fZCMz7YyRYGXjCh+mtN7eC/SQzWnyRrEktKg23ElXKkCp8LIYp X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(30864003)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YjnfyBeSS1Q0XgR4skAw4vByxfV4qCOUfBYv5RGeWYtvbElxypC/LikO6Pw9eiDWbrKQbCLpWLIGwM4VDme+jcmtxgXPHIEiyby9FCXVrjX07AKWIWmhrvDP/Mi2cji0mmmL5SBFAoJqGBAYCfkUR6B3NhPL1zMQ0kmvbyMIYy9S5FqydoVSQ5gfc0q4VFx3ihVxJzRUheq2KEgHq7q2DzDHHTLySQmGBS4/E1PYTQfdkEYKHLDRy7I6X6g/7azAntz8KNl1ZJofUtxxangdgQTYbP+5YiIVZ9eFKB0NApIBBFouyYmQVXR2ZVUdur5EZi61KPNSvDZTpEItTkHPRdjCAeXQhHGejgbReLPCdfGej4VYjdszER77dci11Nq4D2xpK1t1BR0n9FEfywnPYmAOqzwOHV8T+EZ7NDwhRZPgo8UPUc7PvcwyG9z1RN4pItCEe53IbgP4s/myfjRsih3yBjBxsg7AnkszTeN+SvHxfRjrsaGi6WAQIZCJ4L6eIUWATEMvMcNPqe9CE4ZLp5/VrBLkAGs6qFunYwRtRx8H2mACMwCnFHMxDCZ3vXOym2Xt4Ll6j09nJWapAD57bgZOng+hZmPVQhx3FVE4Ap2Blw8dWdQuQy+bx9LnON1Niz5L6Ut7LqLQwVVOsFbkNgm9hVwlHjGS0+pkFeTYmboQ7Nt9OdkaVYQQgsg9ntM7Y4mpxH2VPASDeyrKpqx4UWPXc/POE5tAukj5vZKyVy6X0LsEu85bH43EDmZlLSVsgxX7kAlW5F6uWBStPkYuaJDOGmjmu2eWI95EameWeDI4CrA+UPFlFrew9RDQmnp4t2vphlXeTbxBKNGM5Y/wSnPcZm+xZ12FL57i0jTCQgiGHgptQe/trZ9BTJjW58A07QQ7qOO514jUAhFDgdqbbyVts5KV5Sw7kgRLWd/5I21txkItTa1QCe8Y2kcDNnogsCe8uGr9vc+Gn1a2lNk8nJkL4Drvctbe8zIlFBJonue7v9SfHvOwmx7Ph4EWjKG35vdmMQISsRpCwiNaezBRDztWPW/yPfy24hET9ZNf9i417l2JGGsnh8WuwQpPZG6+1ulznzFPEG7Pq72mCw5eRBCiuipCjj6YDtm8KsdG6biZFIYMDYyv4Ha68kOKwHnDJmlk16ajYCVkyDiFyWKt9f8KdUDhT59C+WFqp+WK+y9FnLSmoGm21NZdXOvRBi/+qYZeraOznB9jYSe9UQ/WB1TF/zGpV5aKe7h4ljxuNeAmZ57qbVFJHVF7c3Qrj22Ed9aNwqzPKjLWhGxwskKA8u3ieOYvyJtkrul63hrbn0LePR0HPQKRwUEgncgZlP+BUsORArDJPZ+Jn8cHiHOd//z15f3d/GjdX3VSXULzlP4hb89qTNm3PO5O3P3vC1DpQzJEs+2vhdownWBD71uAtFm0v0t3bcrO2ymTqmZi6GOdNa6IrbdXbC/k0pgTtBsyR7rcQuYp1YN9b+vR6XNy5GNYTnT4KQ8DBy8YXYckcTpmFkofKim/x7aNWOXrjBCMRlqhFwpvIlIiviUY2tggVseCCi9YXe5mEHQOrTXa/2p6MJc8sCQN5Kf0eaRZOQ80 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 216dc09d-d917-450d-55b5-08dbdb3369e3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.0751 (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: ivpuzry8A9Y0uG4CXbS3twEKUec2c9XekyWAQfqFkNfNLqi4zPuZAoJaX5a1rQc7 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163708_339856_8B317FA4 X-CRM114-Status: GOOD ( 31.67 ) 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 The core code allows the domain to be changed on the fly without a forced stop in BLOCKED/IDENTITY. In this flow the driver should just continually maintain the ATS with no change while the STE is updated. ATS relies on a linked list smmu_domain->devices to keep track of which masters have the domain programmed, but this list is also used by arm_smmu_share_asid(), unrelated to ats. Create two new functions to encapsulate this combined logic: arm_smmu_attach_prepare() arm_smmu_attach_commit() Put the ATS disable flow into arm_smmu_attach_dev_ste() - we only disable ATS when going to IDENTITY or BLOCKED domains. Installing a S1/S2 domain always enables the ATS if the PCIe device supports it. The enable flow is now ordered differently to allow it to be hitless: 1) Add the master to the new smmu_domain->devices list 2) Program the STE 3) Enable ATS at PCIe 4) Remove the master from the old smmu_domain This flow ensures that invalidations to either domain will generate an ATC invalidation to the device while the STE is being switched. Thus we don't need to turn off the ATS anymore for correctness. The disable flow is the reverse: 1) Disable ATS at PCIe 2) Program the STE 3) Invalidate the ATC 4) Remove the master from the old smmu_domain Move the nr_ats_masters adjustments to be close to the list manipulations. It is a count of the number of ATS enabled masters currently in the list. This is stricly before and after the STE/CD are revised, and done under a spin_lock which more clearly pairs with the smp_mb() on the read side. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 171 ++++++++++++++------ 1 file changed, 120 insertions(+), 51 deletions(-) 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 3aec3791ccbe9e..2181eebf0aa369 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1490,7 +1490,8 @@ static void arm_smmu_make_bypass_ste(struct arm_smmu_ste *target) static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, struct arm_smmu_master *master, - struct arm_smmu_ctx_desc_cfg *cd_table) + struct arm_smmu_ctx_desc_cfg *cd_table, + bool ats_enabled) { struct arm_smmu_device *smmu = master->smmu; @@ -1512,7 +1513,7 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, STRTAB_STE_1_S1STALLD : 0) | FIELD_PREP(STRTAB_STE_1_EATS, - master->ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0) | + ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0) | FIELD_PREP(STRTAB_STE_1_STRW, (smmu->features & ARM_SMMU_FEAT_E2H) ? STRTAB_STE_1_STRW_EL2 : @@ -1521,7 +1522,8 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, static void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain) + struct arm_smmu_domain *smmu_domain, + bool ats_enabled) { struct arm_smmu_s2_cfg *s2_cfg = &smmu_domain->s2_cfg; const struct io_pgtable_cfg *pgtbl_cfg = @@ -1538,7 +1540,7 @@ static void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, target->data[1] |= cpu_to_le64( FIELD_PREP(STRTAB_STE_1_EATS, - master->ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0)); + ats_enabled ? STRTAB_STE_1_EATS_TRANS : 0)); vtcr_val = FIELD_PREP(STRTAB_STE_2_VTCR_S2T0SZ, vtcr->tsz) | FIELD_PREP(STRTAB_STE_2_VTCR_S2SL0, vtcr->sl) | @@ -2405,22 +2407,16 @@ static bool arm_smmu_ats_supported(struct arm_smmu_master *master) return dev_is_pci(dev) && pci_ats_supported(to_pci_dev(dev)); } -static void arm_smmu_enable_ats(struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain) +static void arm_smmu_enable_ats(struct arm_smmu_master *master) { size_t stu; struct pci_dev *pdev; struct arm_smmu_device *smmu = master->smmu; - /* Don't enable ATS at the endpoint if it's not enabled in the STE */ - if (!master->ats_enabled) - return; - /* Smallest Translation Unit: log2 of the smallest supported granule */ stu = __ffs(smmu->pgsize_bitmap); pdev = to_pci_dev(master->dev); - atomic_inc(&smmu_domain->nr_ats_masters); /* * ATC invalidation of PASID 0 causes the entire ATC to be flushed. */ @@ -2429,22 +2425,6 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master, dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu); } -static void arm_smmu_disable_ats(struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain) -{ - if (!master->ats_enabled) - return; - - pci_disable_ats(to_pci_dev(master->dev)); - /* - * Ensure ATS is disabled at the endpoint before we issue the - * ATC invalidation via the SMMU. - */ - wmb(); - arm_smmu_atc_inv_master(master); - atomic_dec(&smmu_domain->nr_ats_masters); -} - static int arm_smmu_enable_pasid(struct arm_smmu_master *master) { int ret; @@ -2508,40 +2488,116 @@ arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, return NULL; } -static void arm_smmu_detach_dev(struct arm_smmu_master *master) +static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain) { - struct arm_smmu_domain *smmu_domain = - to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); struct arm_smmu_master_domain *master_domain; unsigned long flags; + /* NULL means the old domain is IDENTITY/BLOCKED which we don't track */ if (!smmu_domain) return; - arm_smmu_disable_ats(master, smmu_domain); - spin_lock_irqsave(&smmu_domain->devices_lock, flags); master_domain = arm_smmu_find_master_domain(smmu_domain, master); if (master_domain) { list_del(&master_domain->devices_elm); kfree(master_domain); + if (master->ats_enabled) + atomic_dec(&smmu_domain->nr_ats_masters); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); +} - master->ats_enabled = false; +struct attach_state { + bool want_ats; +}; + +/* + * Prepare to attach a domain to a master. This always goes in the direction of + * enabling the ATS. + */ +static int arm_smmu_attach_prepare(struct arm_smmu_master *master, + struct arm_smmu_domain *smmu_domain, + struct attach_state *state) +{ + struct arm_smmu_master_domain *master_domain; + unsigned long flags; + + /* + * arm_smmu_share_asid() must not see two domains pointing to the same + * arm_smmu_master_domain contents otherwise it could randomly write one + * or the other to the CD. + */ + lockdep_assert_held(&arm_smmu_asid_lock); + + master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); + if (!master_domain) + return -ENOMEM; + master_domain->master = master; + + state->want_ats = arm_smmu_ats_supported(master); + + /* + * During prepare we want the current smmu_domain and new smmu_domain to + * be in the devices list before we change any HW. This ensures that + * both domains will send ATS invalidations to the master until we are + * done. + * + * It is tempting to make this list only track masters that are using + * ATS, but arm_smmu_share_asid() also uses this to change the ASID of a + * domain, unrelated to ATS. + * + * Notice if we are re-attaching the same domain then the list will have + * two identical entries and commit will remove only one of them. + */ + spin_lock_irqsave(&smmu_domain->devices_lock, flags); + if (state->want_ats) + atomic_inc(&smmu_domain->nr_ats_masters); + list_add(&master_domain->devices_elm, &smmu_domain->devices); + spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); + return 0; +} + +/* + * Commit is done after the STE/CD are configured to respond to ATS requests. It + * enables and synchronizes the PCI device's ATC and finishes manipulating the + * smmu_domain->devices list. + */ +static void arm_smmu_attach_commit(struct arm_smmu_master *master, + struct attach_state *state) +{ + lockdep_assert_held(&arm_smmu_asid_lock); + + if (!state->want_ats) { + WARN_ON(master->ats_enabled); + } else if (!master->ats_enabled) { + master->ats_enabled = true; + arm_smmu_enable_ats(master); + } else { + /* + * The translation has changed, flush the ATC. At this point the + * SMMU is translating for the new domain and both the old&new + * domain will issue invalidations. + */ + arm_smmu_atc_inv_master(master); + } + + arm_smmu_remove_master_domain( + master, + to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev))); } static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) { int ret = 0; - unsigned long flags; struct arm_smmu_ste target; struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev); struct arm_smmu_device *smmu; struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); - struct arm_smmu_master_domain *master_domain; struct arm_smmu_master *master; struct arm_smmu_cd *cdptr; + struct attach_state state; if (!fwspec) return -ENOENT; @@ -2576,11 +2632,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return -ENOMEM; } - master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); - if (!master_domain) - return -ENOMEM; - master_domain->master = master; - /* * Prevent arm_smmu_share_asid() from trying to change the ASID * of either the old or new domain while we are working on it. @@ -2589,13 +2640,11 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) */ mutex_lock(&arm_smmu_asid_lock); - arm_smmu_detach_dev(master); - - master->ats_enabled = arm_smmu_ats_supported(master); - - spin_lock_irqsave(&smmu_domain->devices_lock, flags); - list_add(&master_domain->devices_elm, &smmu_domain->devices); - spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); + ret = arm_smmu_attach_prepare(master, smmu_domain, &state); + if (ret) { + mutex_unlock(&arm_smmu_asid_lock); + return ret; + } switch (smmu_domain->stage) { case ARM_SMMU_DOMAIN_S1: { @@ -2604,18 +2653,20 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, &target_cd); - arm_smmu_make_cdtable_ste(&target, master, &master->cd_table); + arm_smmu_make_cdtable_ste(&target, master, &master->cd_table, + state.want_ats); arm_smmu_install_ste_for_dev(master, &target); break; } case ARM_SMMU_DOMAIN_S2: - arm_smmu_make_s2_domain_ste(&target, master, smmu_domain); + arm_smmu_make_s2_domain_ste(&target, master, smmu_domain, + state.want_ats); arm_smmu_install_ste_for_dev(master, &target); arm_smmu_clear_cd(master, IOMMU_NO_PASID); break; } - arm_smmu_enable_ats(master, smmu_domain); + arm_smmu_attach_commit(master, &state); mutex_unlock(&arm_smmu_asid_lock); return 0; } @@ -2648,6 +2699,8 @@ static int arm_smmu_attach_dev_ste(struct device *dev, struct arm_smmu_ste *ste) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_domain *old_domain = + to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); if (arm_smmu_master_sva_enabled(master)) return -EBUSY; @@ -2665,9 +2718,25 @@ static int arm_smmu_attach_dev_ste(struct device *dev, * the stream (STE.EATS == 0b00), causing F_BAD_ATS_TREQ and * F_TRANSL_FORBIDDEN events (IHI0070Ea 5.2 Stream Table Entry). */ - arm_smmu_detach_dev(master); + if (master->ats_enabled) { + pci_disable_ats(to_pci_dev(master->dev)); + /* + * Ensure ATS is disabled at the endpoint before we issue the + * ATC invalidation via the SMMU. + */ + wmb(); + } arm_smmu_install_ste_for_dev(master, ste); + + if (old_domain) { + if (master->ats_enabled) + arm_smmu_atc_inv_master(master); + arm_smmu_remove_master_domain(master, old_domain); + } + + master->ats_enabled = false; + mutex_unlock(&arm_smmu_asid_lock); /* From patchwork Wed Nov 1 23:36:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443148 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 4622FC4332F for ; Wed, 1 Nov 2023 23:37:53 +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=d9Y2S+uTeszP/NPnoz0pXeJ0JSKKJhD5l6fwQCIPwG0=; b=HQxbhVtbw2/ttz qIHBfzp/0dIYZRd6n7FIPm9M/wnyeE4PS3KFp+plXzrXGfapk4DO+tTK/8mhNyPHKHvmk/DFwaWUE EZvctBShXhD+YL+En0izNntZakHm/qS5wxqBeFf2OSYm1iXOs5J1FERXd5i0EVppWNbFgNrsYELvI SvoDSFaioMcMhtz6/zfDj7grIG3iZdQHYnkDhFi2wXCXCiygpX/eKdQTQDbf46q7HBFVAlGFmrlrL RxEbsn3TjOX/U/YDNqUkomr2LTBMTsm5URHc9oSfVz7M4XZP+KDSdZzF/8CzBbRZmpjcJz/f3hcB1 8SSvC0UMOXS6UIjF93NQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlu-008Jmw-2m; Wed, 01 Nov 2023 23:37:14 +0000 Received: from mail-bn8nam04on2062d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::62d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlj-008JUF-31 for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=P2be0mjnAkiLvbNNlXitFf5pBlcqXileYqD6USHFGSiryRSK9Wco2p0HPLg2XkH2LCYBsBCR8QCo8vDyfNX+aAcpEDvRAdcB9djF4akqoerjFd96HfC/SYnVfI2urRtYu6RjFYBBq+OnpuZ3YwHAKktPMTnPC7J//tV5n+5g38hmvyi/SyFAKZqG7oaOexL3czVibgNXtaVNvg99xqvixSYEgrKyJeCvE/Z0x7bE1On4bixPE/Kk/xRELudADUZUSrEC+VWVz8BrMWgZeDfSusVcDdbCL7vcCUL8WVFMS0xrUt+KIg4+QYchw+czba4hCEp08Mxq7tztptwnP/FIyw== 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=eQnrUTakMA6dtB5Knm1CG6ZeLxGQgDwlzM6pmF2tGtU=; b=O4CqDz7mDeyeTCKMEkzCN4Hi6Km+y8KWrU3xcYGDHaUXeO2suTbbANndgM2MVbW2TbLwVk74E7EX5dQtiZlIveJEm68ubxfRoexr/85+THJy216S9Z5Quo+7/MErOXNFYl9+w/ECW+E+O2p4d+LqMg7WnmMNXS1OFByzIWcJuLrkc+4BrVRXRN5y8egl2C5UxAYfOQi24/ZRVnH/pQS/DE8l6zswF5+wvBFJnI4FSXUji9N5eudUp8kw3OEWCu+t0kPvjSJblxY+ti196Azikt1Pwc9sxlkKJe/UnjUNrzvtxKwNfYmUN89XKrsefvB8wuPTY/n3FcihexHUNpe1tw== 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=eQnrUTakMA6dtB5Knm1CG6ZeLxGQgDwlzM6pmF2tGtU=; b=JyI6/Nm32iwzZyZa8JvptxqqDOoJSSflHRT61qH5LzJCfQezpKnnJsAZUB7UEOXBx6bC/8rRAfKdxaIt0p7Icn8wmsHNVTSRzbJ9J2wLEZQXy28P1JvuaWJvsZGsa8ExKMWSWUvbyJ7qmF4YNKrBax+vzgcC2RsbqMQ8ODd3h+DuvTGvj3hisYvvqdMFNaeOcDVEYRdvMPhpPGNJG44KvSJbd2ofmWkDqxn2naFPmC3r/+lDA36aebLxYOKEmQ/Hcr27EcFUOTFFjzb/whWOYF5Flctwk+bjx0bFCNcriXyLLR38D+zM3VTj0iJk9wEq+P3//NGBtvsVMqdsmNL3Uw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:49 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:49 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 15/27] iommu/arm-smmu-v3: Add ssid to struct arm_smmu_master_domain Date: Wed, 1 Nov 2023 20:36:33 -0300 Message-ID: <15-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR19CA0060.namprd19.prod.outlook.com (2603:10b6:208:19b::37) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 64f5a5c9-4239-458b-b96c-08dbdb3369c6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RUylVwDCUMlDYoEJmnNibH8IZbHi5empuZ815AJJm02C6bBGb8jOklFoYJbihGfNFntrYWolQyVKckT8js4qfpQYeUhsCdVN8BBtu6d+xtbeTIMAvbphlgBP0KCjqwoxjF4hdw2QQBE7oZ09iF6F35rRidhHOArT92vwAW5OiNvJts4EV2BstndzxZEqJntb6mOOgatEnHkGujDTnw8xNJZ8Q4NDZCbobhPlB+8ljVthHK8l5qgYtCvoel8MkH9hzkMia0XL1jtl4Bj/jwatu6famY/IzK89citFxiMjxrYUsCfgPjSBesqhfKgn9Ihxqn9VNGAMFO35Xx6HGsRPQ3tODSdxAV7lhSuHy6FV8GXjD0HuIDluIxfEMHaJySDkLkeDWlOQy5Z7nrikvxsAnZQH0+EvfeXhUGzpiQq3q7q1OBEOUYwWSjy+ZfaHufwIph66umBbyEWIrrcIxFDSwbO87JxCak9GgE8DeAbkpZEJE+BHgnVvzeItEHPMDK+bos0h7RQMUI3AUWPCrVfldc/nfuDIIpwZIEV/olwi5AtebhUMnCW0NhKakbf6SPk8 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: K6UBTs6iRENxc8KZ15/bK/d7tnQTyUZmqe3nfIZt0I/cgLQt/s+XipQXmwHVNM1OwtCIDJx3zgIjxbSluROaedM6CjJF8/CMBLuIe01u7bvHCgb7A9sfEU3U/iZult94M+Fv/4mDa3lKwUym0AfbfyjHK1DlyMrkhnTXAqHRtyn4wVtpC+YUuYiv1rd+8gAE/+fa1WirD9sB5gKUkgrq7HTY850OAWppa+8hfiMuBoVl8Yd9tgCbNGV72loQWGJpRhhgSXENEHYz31hB6wsHL4idYRRhzn4ivwPtSGFGSvcl+Wu94TEiUvJDbhUIjK15bo1QauJRQ6lQ7eCPF6203ulWJMcrShCktUyG4ihOgR1oLozrRmM4VQS0bupyEqzZtxDkgIexdDCoTWStkE5h0ph18wySEJktW7BqbI24i+vD8E2VhbQTr9xX+vAeVvyv1w5IlyHbN45qpoSeXOF/hc9AuVLRE79RBWTl16G7w50pjvUWJtFCeKN3U2lM3TPoS1GUhJ8bI7ScWzlfG3n8Wr2Yhv5b3U8UlkI5svlDsyEoQgrPyZxP56HL5l1HeZsjBBpAAPLis50KBlhfBsss99f4BjdxPydG0xTcUj15PeTyUQQaE6MbSz+fmAwL76nu0CGSMieQabXUNT33q6eMkPz5KDHDPKC75fNhlAi5RpJeDgcu+nUmK/5tSFTCU8q0p4eBXrY+UkoQicPesdNyj9C7LREwWahJ//iotPt049LNKh22PtbcmxAe1C2o6Fly31emFUnfxUGrqJtlx/4ev6RFBivvOf66YRacH13U4PKmO/KDcp7RiOpuvsTRW9gbxDMMnqrtrOqTEBXvXOIH35T04VWIi33ku8kQk1vTwcxQQFwJfCoF0f9hQk03+psVR30VImVlbLQGCRt7aSH2MC5I1yT/slhLFIH5Jg1j1JWLPi2yXQyzZHpH2mCZgyLh1JjadlCV8JqxvFo74SOu6Q6WtLqEtsBrN6F+Qw94iNUZcOFnWrlp7RmBMH9OOdBNJG3PGdQPv10MWM+4ci/HpbuncWS6IV2EXgFB+43s4plrqhCTDs0WoviaYSzmxao2Zl440z6f0wRXLaiIEkWUAWqcGC4qGJ7cMZD9Py7DMca3ixuIrXTwyrhJayO9H/s+4BHoXcRat6icKNFJCe7JGg49b20MYJ+EYaKDmG4Q9Fwz06bdJp7l1oQ+WkO4JT7AkHa5FDTomv76FSiP4DWatlChKDrt8kTPS/XsFZHhpd9RZ62Fw48yeXYUslvk+4ONtYs79V/HXfmieTcPUus9i1l9K/oZJ5KUknRgXGihHBPzjIfBbzDgHYpr82uJmvrMGTVzk7akPxxd2W2J9QIqLqKAlPB281jgjqfGjAMjhoQbRr2Htea5jH0dqJn7oqKI105Yuo9LhrVeo4DIWRKqbniRgYwM+kOUm1lBdbi/puRzz3rdkc0jNPi35PcSxnfrGW+wNd+qRc9r1g/j92ssv9ZAf2/zh4eheXouDx8iJz8LvwpS3sTdY7LFV1zSJ3XaWZkTfoz+O96Cnpv2rRe/epP+VHYzSrGeYvWZpP1l0H3nMIz20EdSIHrfIJfu6GK4 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 64f5a5c9-4239-458b-b96c-08dbdb3369c6 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.9204 (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: 3wdqLht75lC4HLvjK+bo/44oJAz4lEoHBDEKzf9Acov0KeBTjf/mF1/jBgMaJfIr X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163704_008302_37789DF2 X-CRM114-Status: GOOD ( 17.35 ) 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 Prepare to allow a S1 domain to be attached to a PASID as well. Keep track of the SSID the domain is using on each master in the arm_smmu_master_domain. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 11 +++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 42 +++++++++++++++---- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 ++- 3 files changed, 41 insertions(+), 17 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 c541b94917d036..4644d4601c0830 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 @@ -47,13 +47,12 @@ arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) struct arm_smmu_master *master = master_domain->master; struct arm_smmu_cd *cdptr; - /* S1 domains only support RID attachment right now */ - cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); + cdptr = arm_smmu_get_cd_ptr(master, master_domain->ssid); if (WARN_ON(!cdptr)) continue; arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); - arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, + arm_smmu_write_cd_entry(master, master_domain->ssid, cdptr, &target_cd); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); @@ -283,7 +282,7 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, smmu_domain); } - arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, start, size); + arm_smmu_atc_inv_domain_sva(smmu_domain, mm->pasid, start, size); } static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) @@ -319,7 +318,7 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid); - arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0); + arm_smmu_atc_inv_domain_sva(smmu_domain, mm->pasid, 0, 0); smmu_mn->cleared = true; mutex_unlock(&sva_lock); @@ -398,7 +397,7 @@ static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) */ if (!smmu_mn->cleared) { arm_smmu_tlb_inv_asid(smmu_domain->smmu, cd->asid); - arm_smmu_atc_inv_domain(smmu_domain, mm->pasid, 0, 0); + arm_smmu_atc_inv_domain_sva(smmu_domain, mm->pasid, 0, 0); } /* Frees smmu_mn */ 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 2181eebf0aa369..d56805d201fc43 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1968,8 +1968,8 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_master *master) return arm_smmu_cmdq_batch_submit(master->smmu, &cmds); } -int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, - unsigned long iova, size_t size) +static int __arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, + ioasid_t ssid, unsigned long iova, size_t size) { struct arm_smmu_master_domain *master_domain; int i; @@ -1997,8 +1997,6 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, if (!atomic_read(&smmu_domain->nr_ats_masters)) return 0; - arm_smmu_atc_inv_to_cmd(ssid, iova, size, &cmd); - cmds.num = 0; spin_lock_irqsave(&smmu_domain->devices_lock, flags); @@ -2009,6 +2007,16 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, if (!master->ats_enabled) continue; + /* + * Non-zero ssid means SVA is co-opting the S1 domain to issue + * invalidations for SVA PASIDs. + */ + if (ssid != IOMMU_NO_PASID) + arm_smmu_atc_inv_to_cmd(ssid, iova, size, &cmd); + else + arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, + &cmd); + for (i = 0; i < master->num_streams; i++) { cmd.atc.sid = master->streams[i].id; arm_smmu_cmdq_batch_add(smmu_domain->smmu, &cmds, &cmd); @@ -2019,6 +2027,19 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, return arm_smmu_cmdq_batch_submit(smmu_domain->smmu, &cmds); } +static int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, + unsigned long iova, size_t size) +{ + return __arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, iova, + size); +} + +int arm_smmu_atc_inv_domain_sva(struct arm_smmu_domain *smmu_domain, + ioasid_t ssid, unsigned long iova, size_t size) +{ + return __arm_smmu_atc_inv_domain(smmu_domain, ssid, iova, size); +} + /* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context(void *cookie) { @@ -2040,7 +2061,7 @@ static void arm_smmu_tlb_inv_context(void *cookie) cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); } - arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, 0, 0); + arm_smmu_atc_inv_domain(smmu_domain, 0, 0); } static void __arm_smmu_tlb_inv_range(struct arm_smmu_cmdq_ent *cmd, @@ -2138,7 +2159,7 @@ static void arm_smmu_tlb_inv_range_domain(unsigned long iova, size_t size, * Unfortunately, this can't be leaf-only since we may have * zapped an entire table. */ - arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, iova, size); + arm_smmu_atc_inv_domain(smmu_domain, iova, size); } void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, @@ -2474,7 +2495,8 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master *master) static struct arm_smmu_master_domain * arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, - struct arm_smmu_master *master) + struct arm_smmu_master *master, + ioasid_t ssid) { struct arm_smmu_master_domain *master_domain; @@ -2482,7 +2504,8 @@ arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, list_for_each_entry(master_domain, &smmu_domain->devices, devices_elm) { - if (master_domain->master == master) + if (master_domain->master == master && + master_domain->ssid == ssid) return master_domain; } return NULL; @@ -2499,7 +2522,8 @@ static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, return; spin_lock_irqsave(&smmu_domain->devices_lock, flags); - master_domain = arm_smmu_find_master_domain(smmu_domain, master); + master_domain = arm_smmu_find_master_domain(smmu_domain, master, + IOMMU_NO_PASID); if (master_domain) { list_del(&master_domain->devices_elm); kfree(master_domain); 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 59bb420af52e28..887669f04b55c1 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -738,6 +738,7 @@ struct arm_smmu_domain { struct arm_smmu_master_domain { struct list_head devices_elm; struct arm_smmu_master *master; + u16 ssid; }; static inline struct arm_smmu_domain *to_smmu_domain(struct iommu_domain *dom) @@ -783,8 +784,8 @@ void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid, size_t granule, bool leaf, struct arm_smmu_domain *smmu_domain); bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd); -int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, int ssid, - unsigned long iova, size_t size); +int arm_smmu_atc_inv_domain_sva(struct arm_smmu_domain *smmu_domain, + ioasid_t ssid, unsigned long iova, size_t size); #ifdef CONFIG_ARM_SMMU_V3_SVA bool arm_smmu_sva_supported(struct arm_smmu_device *smmu); From patchwork Wed Nov 1 23:36:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443210 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 ED540C0018C for ; Thu, 2 Nov 2023 00: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=1H68xBSu4XyW3k24bUX63QKkoorhDwRXvnnEAYL6BDg=; b=XdPsN1ssjAjMaD Bef4BMpeZgpyfv/5okEi/z2BckSgmtWfxOxFsnXZhFMtIJGBsGSGnaPcSfsf0HOKAwI9MqTE4fzDR 5yAHzkL3p8MBgjmM0nFAQwyyniMz2QdDEYiNBSX4s+UPCN2E5tY78TduJar6qTUUZF0tnFyk/WvZQ wPY/0ySph+VXs308ev66Fp35j007AGHEoTpYLodWFjylPjVL94ej8XgERZFuQbp9YMMUjq9eqrqON FwlRYr6d71yoGsPh/lvKNKh+KQNvE95y1q3u34Epfrm8kJeGaeEHEgUAQ8hh2bULnk2TSP6mPUdmn X7VuD2wdYkBHjnKizYNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq9-008Q1D-2c; Thu, 02 Nov 2023 00:45:41 +0000 Received: from mail-bn8nam04on2062d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::62d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKmC-008JUF-1E for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:43 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Yt9eJHDvD0Rhe47JlUBiJQrheGTnemnKInni6HF1YjoPXlirvG/+isLeionFy66Pkse3CuMVyQMWvJmAHAMNMt+UVUr6mo67jna7aFIiU0L4rXXpT0+M4P+lI6YfF/P2XabMnxmE2FKWAq7rfy2cN5+4Kxr9xTKcdKEHzzTLaDlsddGfdzu+h27MUkozwlJPcmET80GcX3VIJMqlAkJMw4ZHQN1vJw6cWwgBVIspLHgT1yv4dVPJFx5SYfbFDohEP3b8qtUA2gO7X2g5WO6wJutfubPMlVyJ73/b0WfWoUGuB8HJ/lfnHnIbB8GfD1MXLnP9+sw4EpfzSAQDXacUOg== 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=cAmskqs4F41e0kctqUbxetDR0tHdrRNtvxoANM2KAMU=; b=FiDIj3xZEyoQv4xBeQhyVvKsQcF/Lxr8Ks1aJsgrvnn0BBs6Oe1Gg8tlJuVZl/DmnTxGQjYPOna/6lOckyT2Sg9+v4YazWECcU8oQIC/tKii6WHsb1Sl+0zVP6+RfRIUyKD7cB5Bf4+AUROhKsW1KFEMeGHouBexSRjvwZZYiiCJzMr/7rR6nW/hIgXOEFpdsycTrLlu9pkjx22MgxxsyfhnO56hHEvdP55nFxa6Vd5q6Gom91D/W0cn3iG/kL3RttnnY354202v8btxWEukWQ2fYSkzdQpouBuDDtWLO4K5N5H1tcvWxZ5vrnY7dfPHMLudkrxGcB2lzi1rheRRkg== 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=cAmskqs4F41e0kctqUbxetDR0tHdrRNtvxoANM2KAMU=; b=kCpLO0V98+gcchqf3AyD/gDA8rKnxT60LHQu+/pYNyN5IFdvSRl5bk3auVtFIJJ/MyX31yT9NOfeqaA8XcBhOHZg2pFdO6j9OAhVCvfOEhRuQUpgzWyL5s9kFYKQQdsjZTO0ypATvxKYM1JL+jfAA8sqqgVE72kVCxLe8url1Cks3qSjHhwvNJbly61PJTp4cQAvviXN6zxavtpu4VG+lLP0EOIJ8A2zR+csNf4mIKcmi25QgywMXGop7YJZcQY1foKEaxRuMvN9kaxaf9Hu2mq6UoKDzymkquuMmP2um6JcpRSvRMkLQewQmoooJFyoINcy94Yd/8rftQB7XWQ0cg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:51 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:51 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 16/27] iommu/arm-smmu-v3: Keep track of valid CD entries in the cd_table Date: Wed, 1 Nov 2023 20:36:34 -0300 Message-ID: <16-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0299.namprd13.prod.outlook.com (2603:10b6:208:2bc::34) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 5af9a6e2-1bf5-4daf-3c08-08dbdb336a6b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sb9IL+7Yk37tUDF8VuMUzZp0ezy58JaShQtzv8vq/+SuEd+icPZYzh/z8QSkrDo7sKPCG8tk2oVw0sEc6C4e4kBD0J7zRZryIoT7bXUnh6rMU4FWSTbJE+17+q2HN2Q49CVLLw0PqySH0r/dAsBdLEAS7Ir63QApESvDgoMiTHHXYOx/6io7Mu5hfH/OH6wtU6Kc/rRZaGzU6g19l18vrsnFNaV4W4QFFVOT0KT+2ID/vMd/7KcmbOCOoHbLRJDsAPnwG4kSBSk6ERR9rnCZxsboM4in4KGhf3JrpuePg2yRxYYiAY7OY6j2nqC6sDNQVc4ByAGOl8+IfKOmwK8CPnq55S4/0s56nhuYovNOcj3veSMKnEDnMBGvVlq4BWf9cYWKzghmGyB5COHaLHFtTw8p8gH3r2KPV8SHr7nAUeL6f1QcNn2ueGvngiIge6yRVVt5oHs5yr7Ukhhe8L5svYcKoN5iXOhVU7tyZpWmRHX9k3YiEWrPUv73pZWAT2oZt07D+LE7vOn0ZaKXjkC5KptRmvkdAE10ydJAp2xPAUsEefrgP5qR9lTF0OgP5FWwmnCbCsB9jipY6jwlp56wQCQmjm7ZpZ/9LehC6Ms0CtfNuGCugpO2ananduMQ6tCMh8HIOoVhGGldNBA8bnCpKQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007)(473944003)(414714003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: PQs5YIKbgQE9JlYMIRj8VbNWf+Qh2QfNj6v7sxI2XBrjJAjUXhfbAI5iOtmYg878OtT7RLTg9NpVkhQ+lPMjjC5ST9tC8U3wPVAPjHTPE/TObOAtpK0C4bopSpWiXPIRekgmByX29ZiTdZee7WO3W3ANED7oFIU8aSHMjqVz2TJE9mFb53tw2x+vFZUR5pZQaFjvzVLIPGfwLKAgsnLQHYPW7qLffhfU4KgeXIC5XH1Z0EXTmwnH8CsxfhpA/iOGurt++Tso9lg4+m6NfWjQGRkaW4UZxzu33PAv84cbWzSyiU3Ksup1PG8ssYVvJWy3q+UZIXolXKMn7bOrdvJGVnU3XC6j6a7Bl9Z9zz6rddkjp33QdPu65WT9bwPznt1oW1z9sZmBvdMfVSsbVd1rsen12CheUu3NI1hA00ZyvKksvdAPUf23BRPCAuPc2rY+UiF29lNecXI8vd2cDq/CBTxkzMM+VDQwMHb9y9UZ5C+/Lgx9YgYQn4Q/0L/oyL+TNZVoG/MBJVuzMeRaBx981JjbgtO203IUN1cQRfOGEi7WrQ0nDME8+/B0gMmVehPknJ76xfjVCyIU4pZgkUkhZIhw4FUni5OQUSB3/PgqyRf61rz+XWZM418XG/CWjJLnUSrCr9oPJgA0Bwr6aZD+PvlHzMDGe7RwEKEOrM5IR7kcpXsxE6GFWLbsuX7PfPE7/mhTbUEaipiJRHgX8PhJeFlzk8ZkcXCNU26PNyz5jammXzqEbnRXBzrljbis9ooNjeUdJQxvpiL0n8Wdg9L3uM+gAmX3wqKXT8WPA8TUrnDaPen/HrsrtQU5l8xE1yAFva3A3oUNtF5mnJcXvlH4pBcAhMwsDE6Y+UyeV8a7yfdzUCAKHkH+kBMeMlwLLPHm9hNKyd2wlZ6xodXI2a2yi9e5fRXvFPJ+OM4rs1ES+bkC3slk5f/Gw2vPzt7QyqpWZcwQFq7mT794SlYvNZSLLmhok5T9JSvU8BSAwBFIF2B5s2T0lLwFM9rlpB0lElcGGLPj5twRKTjnNHpHbOO5Ds/NJe6asUN0/L+BOXN5tMw3Icv5Jhq3sMX5L9VrvmRs5wvRLU+LYadF4EKGn2qNKkjcogaXf04rT5+rcuMlBcejEmEU8cTksJ5RKkFn2agiNt6MIOcMLbwSHLn/qbMhLuWunQbf7OWS9wsMmg9QjGOXZ3l88xWbN0KutFE7sQ3YcSg4vJ5QSBiwH1P8vpyZyIiJ+3hQCUE2TxgqgGq/QD9sdE6IuTKEkw1lXLTIUAq8hlhJHDusfQ0UJ3otYqueyK5a0lv3ex9fwWISYPcRu5pkqk7wXVPQZQvobYcPlJ6VVqMDhOjHNyaCuIkLoW++InU/4vBNpP7DWQDtsjy7KnevG8uz/o0O+Z96pleXWOogygozo3EZjKqOcq65pJJwQA6qDPSZz5yMIm+mQQOGNIR9QVXWOzhlxNPX9xEB8SkfTE7l/EOyQ/x1vUwIMXuuld/5gtZiL4TEpWPeaiwgjEOIMoTndV0lkIB9XHgxcvNUF5+2/m1Wodgrd2XPAkpwOVuygehFo2k8OneFDTC2ArWbF9yZ1oPDQDE9ueo5j553 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5af9a6e2-1bf5-4daf-3c08-08dbdb336a6b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.9476 (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: OCh68t0spsOL5btVajn/nEe6ylg9C43QxNMtPU10x7d0OJog6AeD82SLnq8FpQM5 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163732_453595_702091E8 X-CRM114-Status: GOOD ( 19.40 ) 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 We no longer need a master->sva_enable to control what attaches are allowed. Instead keep track inside the cd_table how many valid CD entries exist, and if the RID has a valid entry. Replace all the attach focused master->sva_enabled tests with a check if the CD has valid entries (or not). If there are any valid entries then the CD table must be currently programmed to the STE. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 5 +--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 26 ++++++++++--------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 10 +++++++ 3 files changed, 25 insertions(+), 16 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 4644d4601c0830..2f818b24d931c2 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 @@ -417,9 +417,6 @@ static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, if (!smmu_domain || smmu_domain->stage != ARM_SMMU_DOMAIN_S1) return -ENODEV; - if (!master || !master->sva_enabled) - return -ENODEV; - bond = kzalloc(sizeof(*bond), GFP_KERNEL); if (!bond) return -ENOMEM; @@ -622,7 +619,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct mm_struct *mm = domain->mm; struct arm_smmu_cd target; - if (mm->pasid != id) + if (mm->pasid != id || !master->cd_table.used_sid) return -EINVAL; if (!arm_smmu_get_cd_ptr(master, id)) 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 d56805d201fc43..877bb8d69c0902 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1186,8 +1186,19 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, const struct arm_smmu_cd *target) { struct arm_smmu_cd target_used; + bool cur_valid = cdptr->data[0] & cpu_to_le64(CTXDESC_CD_0_V); + bool target_valid = target->data[0] & cpu_to_le64(CTXDESC_CD_0_V); int i; + if (cur_valid != target_valid) { + if (cur_valid) + master->cd_table.used_ssids--; + else + master->cd_table.used_ssids++; + } + if (ssid == IOMMU_NO_PASID) + master->cd_table.used_sid = target_valid; + arm_smmu_get_cd_used(target, &target_used); /* Masks in arm_smmu_get_cd_used() are up to date */ for (i = 0; i != ARRAY_SIZE(target->data); i++) @@ -2629,16 +2640,6 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) master = dev_iommu_priv_get(dev); smmu = master->smmu; - /* - * Checking that SVA is disabled ensures that this device isn't bound to - * any mm, and can be safely detached from its old domain. Bonds cannot - * be removed concurrently since we're holding the group mutex. - */ - if (arm_smmu_master_sva_enabled(master)) { - dev_err(dev, "cannot attach - SVA enabled\n"); - return -EBUSY; - } - mutex_lock(&smmu_domain->init_mutex); if (!smmu_domain->smmu) { @@ -2654,7 +2655,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) cdptr = arm_smmu_get_cd_ptr(master, IOMMU_NO_PASID); if (!cdptr) return -ENOMEM; - } + } else if (arm_smmu_ssids_in_use(&master->cd_table)) + return -EBUSY; /* * Prevent arm_smmu_share_asid() from trying to change the ASID @@ -2726,7 +2728,7 @@ static int arm_smmu_attach_dev_ste(struct device *dev, struct arm_smmu_domain *old_domain = to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); - if (arm_smmu_master_sva_enabled(master)) + if (arm_smmu_ssids_in_use(&master->cd_table)) return -EBUSY; /* 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 887669f04b55c1..f72aebaf95f981 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -602,11 +602,21 @@ struct arm_smmu_ctx_desc_cfg { dma_addr_t cdtab_dma; struct arm_smmu_l1_ctx_desc *l1_desc; unsigned int num_l1_ents; + unsigned int used_ssids; + bool used_sid; u8 s1fmt; /* log2 of the maximum number of CDs supported by this table */ u8 s1cdmax; }; +/* True if the cd table has SSIDS > 0 in use. */ +static inline bool arm_smmu_ssids_in_use(struct arm_smmu_ctx_desc_cfg *cd_table) +{ + if (cd_table->used_sid) + return cd_table->used_ssids > 1; + return cd_table->used_ssids; +} + struct arm_smmu_s2_cfg { u16 vmid; }; From patchwork Wed Nov 1 23:36:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443154 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 DB028C4167D for ; Wed, 1 Nov 2023 23:38:11 +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=1EaNgm+HJMFmOwitw55k7cpeXO5BB6PO8IPi5TOp+/0=; b=TO6VXHdAAWNx+r PSPHInjwxgES4S1gq6rU5NdZQc/wuOGV0eY3DoPg42pYhf04Cv8GllAGYNx+239FTK0ENvKClbu1Z yTmai32Dj31sQ89yiLlmfJ8Wy7PDjarqQXZ28E6+hZRSPeOvBx8YZ/dNv1n4ZjqeiZW/NZwP8m39F RN+/VQMa5D4qbVTtI1id2b+L0tKBAuvYD7vjgDc0+U+qWdt9xtU6sNoh1jJcPYuU2XDt+kxJQkEdC D63FcodVi1/jMvkLXY1IVJXDegRZTPE08dOmzD1pB6S3d5v5b4z95Vozk4Vc59N4Rbtze/hf3BIe+ ZQgjOV1cOU+596crRFsA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKmF-008KBV-0s; Wed, 01 Nov 2023 23:37:35 +0000 Received: from mail-bn8nam04on2062d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::62d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKm3-008JUF-2S for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:32 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mC5CmygunVBFighq6MKv2Td71CcbN2p1bud5rQhAuJWo+48ZP3H0HmWkM6tQKUWdhszOsL7ZrPTUrdB0rjDaztWsxcrzDIghOjCV6TOy0tqTfLKw7UVoSepjsm3/ncaDhjKy/5hsaJsV5SpIoLc+5zHhrWavDWeWojwYdIKYFGtWDlVDLdPPE3/1Mr0NW1ImK8+Bnqs3aJr1lgHy7Vn/kzhDlS0HPVPbcWOi0UbBkC1IAuMXA4bA1d1pkcLf/e8ysHet/++yEBGZMHc4Dv0997OIF85mvVF39RYNDkxLSb88WMKyp2gI8b7dbDXZitiCQjoLjhGtXAZfWtZ0tkHwCw== 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=DAROnw1Ms6tjK3l/yhCdIeSefffCoQAZEexrXSu4jWE=; b=mqNmJGyHce7yd5eY1o2974sNoNRpWVnh2il1goPyMoeAp7bEvyo0td6bonBE0qLfv+Mr5Naf6n00MN/OGfCLxwI/wFFEiYK5XYovNX00TyinzaiyT1YIKiU7nzUzKesv5gfbNUHJ1NGVqOX2dqi3130/R7HUQZu47VTIuJHHU03KoUYqwxWsninCtiLLSr640DEm49cnm90lSfae228F54GvFKKAoptA9q81uUD/O8Om/Z2ZojNJbtQKO5HGT6gwsHpmaE5B5vt3SBSLNDRyjK8SlU8mflSrCGXqzO+QOjGqDZrEqPKwRZ4J/zWQJdQZDYFEaETkh/GCHgJcLCoA1A== 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=DAROnw1Ms6tjK3l/yhCdIeSefffCoQAZEexrXSu4jWE=; b=qJ62Bwos4fzaFP65LvyI6C/rwUqMD68PbYeq4DHo1Eyy9nd5WUcufizCD0L9IWqYW7gEuEAJsni1JIVljhPLvVQUDi/3lfTgqPK05nD+w+Wg9aHFn1amwLBWR8LAQeCpCFx6/4HMQetOxU/eTHn1N6qVtJmAm4RTB/58bDE1uXuQJDiCEHY/8by75fq4YKoNKStRZ3XpRKWpV2JPOhOL5RtXXXtTmFkgzBIKnq+XTXIByx6h60eFEODwFVKjofUvA+gKjl5KBPcJy9+mRwkiG23Bgod/xnHFuyuBFa8cftzSh4Vv60ZCTifDLWxeiZ/ytTPK1W3m620CLqoKLfVMrg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:51 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:51 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 17/27] iommu/arm-smmu-v3: Thread SSID through the arm_smmu_attach_*() interface Date: Wed, 1 Nov 2023 20:36:35 -0300 Message-ID: <17-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0265.namprd13.prod.outlook.com (2603:10b6:208:2ba::30) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 98031a4e-52ea-442e-036b-08dbdb336a32 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gmS18KKdjALN61WHISRfmoF2PHNJl/1mh/1NgJaYWH/IxOTQ9qLUCFjilqq6dbSDQdsFFYrp6Ymyjo5niG4RD0pgaa6oTH0BvBP4SegEFxWUHk4HW1tSEkdG/BXZkEldTP33mqzMj3UoTBJQrUirQyPQLq0Xekwowh6VOkNsSok3hTHGgJx3P8DGQDulWIKYPZw4bu7MlmQAaHYVOhoGrTjTyW66L9hdpdaKLAVFZ1vQBpv64GcNUQjLGx3UzYzg2iPK9IIIuQ3p2bnXnCMrr9781J6kI7PUXO0MNSBs/Gy2iuK3jlGLshNxMV+lFM8E8v8ltk/Ac3BEKdyuwwEQlm7XjgkZT//6/H6C+ZA3SASsjAAxKEkliElOGxbvQ3Hsj7/MsPRW0DZqfufEc6LUZzuiNo1rmr8MHEsONtuHT6GaAL0fWBKBNlUo3zSeSH8a4z8cpiZnDNEL2kYgnc6x7TWVV3+omU8gudCLGzjR1iSsvV0CMIjnW6HLykP7KiCcBnl34eKISFMejI22nj6LQAmJ9MzIrAavFrFYwnr5PGLQnroWo7EqsxQmhL8hgEjM X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: DRie91kSlrUiLtPgwwnw48wO4FP4a3O+yMA4/O85Sr7LzdxZgq0PH0xsRCHXXolPFCV2XaGeWa8znGVPpZ+tz4DxA7plnBY4ld8xMrZ2BxpsVbGf5k4sRs/cFmuKt/FiXLALkHfCOEO/KovjcFyFJZi1TjyfUXCHp/qDUv+lHSqgJOThFB7hknZy03CQZsVJCmj5i2GN4B1SuHdnuYgnYkJS8E1thfdvvJCRKeDLDpYI1PkZFmGzqhgtK2oKiRNGoyLzFH6GKjKrSmaGmT/ZwCBAEwgolTAYgvAgBVA4OiSX4s406ilERJVYpVk98BIWhRI2OJgIxkjRQ+dfigvbDBBhMHj+gnNI2Id2qFtU+ZX7EW1yhsBfFdRZyk5kiULBeNzLHgSSo9btb/cbeWqj9e9GzEv8To/Ynv+Z1b49YXnWeEmrxsAjI9RVAfcHLJTMUbyjJdZGdVgf8IBEDio/tv5NwQCI7OZejalGCAOvcIkDDLCAJMYv3n2jaXQd+mj6eWZwU+ivnH5G3XDI7z+OzYDMUlqhj3etFtWyqnRLtz56dN6TUNPeQ67X632WPWTb2CH2gLgYsP1UMBzfIUjS201WmSqtK5YOGIKPQARw+/eCyvA5Dz28p05GPW/W9bdtAqX4dF85WLdrv/hBOyd2WK3R2hrWJax5rHTFXSNPmqVqijg4BVaQNUi0lyvhEHJqw/Eg+JmtYTvRIbK5+Niqpuj4EuX4vbG2k1poGNxG2idEAswkXxNJaV5rT/OqO3IjvQnoXzBCDE1r2LUIwt8NJGOZ6W3f6Tm282FAUHHBMW86xFABtU8rpL4QS3aknfdejlr4HZvDm9rG9k1UlIMdsMuj5eqTkKPx5DE0VNYUIpxnBZAqgnnK1av7i4iHh4b/esfWUYBBUThUDz+ow9cnWsWKGyjc7Vpnetbe0adTXIvGqJAb1PoogM+CcMzEQcnSkExwfpFmo6letw/Bx55P0NUPK350uwntZftMa6J1iiawS+sB6JGwoW4BHDOZu6XgNO96bIM9HCC+0g5oXUFIixpyuTvzdVoYsVREjLwQTTnhEEubfFpFlW5LnVJjPj09ZVZXo0S1uKy7aIulQEAI63r9HqrjAqnRGuj0U04NxvawrFNG50FjRaO6NwddQGD/vemHuICCmXitVXkjePgR1+cceH0mjExdDdv3xDlbLyAewUoar3KAl0nwQLhQZDUviDD0zCaYlfefBZpu9Hk6uemhZJq6uE3cN/pHjQFJnr4cufvufBqeqxvwnP5Su0mwppEkDnMSlmvPMySyQ4DhzGS+pC5W9rK6L+vqsAuc3bIEb74HEDnQe68igfJ8sAoaCBlZSkinw5qRrSvC6eyKx05+80uoRk1TK6STCYMnCK+G0NiSMCzA+4O6MJNkd4qdt6FFpqpnCwAj5y0cmx6i+O+j3VnGFGRk62esw2Dr2F+J1c7D3upK0ja29AUCbYMrjbJM6FqMSme5nYCrEeC0QV50T7kzNrdO7RGPJEVIB0O4/qoia5KIotE9uue1whoFhUW+CXdXn6nVu0guX/pa43H3gF+qI1ycDZ/r7BVViousKLEXloCFWKxVoRVPcop8 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 98031a4e-52ea-442e-036b-08dbdb336a32 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.5478 (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: zmGM++Al5/rYpwKfxG3JsCvJXWYRcOI+p5ywYU8Ij2pSXgAGXrLID3xSSpqcBate X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163723_810519_FBA24F63 X-CRM114-Status: GOOD ( 14.49 ) 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 Allow creating and managing arm_smmu_mater_domain's with a non-zero SSID through the arm_smmu_attach_*() family of functions. This triggers ATC invalidation for the correct SSID in PASID cases and tracks the per-attachment SSID in the struct arm_smmu_master_domain. Generalize arm_smmu_attach_remove() to be able to remove SSID's as well by ensuring the ATC for the PASID is flushed properly. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 33 ++++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) 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 877bb8d69c0902..d9174d609659d2 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1962,13 +1962,14 @@ arm_smmu_atc_inv_to_cmd(int ssid, unsigned long iova, size_t size, cmd->atc.size = log2_span; } -static int arm_smmu_atc_inv_master(struct arm_smmu_master *master) +static int arm_smmu_atc_inv_master(struct arm_smmu_master *master, + ioasid_t ssid) { int i; struct arm_smmu_cmdq_ent cmd; struct arm_smmu_cmdq_batch cmds; - arm_smmu_atc_inv_to_cmd(IOMMU_NO_PASID, 0, 0, &cmd); + arm_smmu_atc_inv_to_cmd(ssid, 0, 0, &cmd); cmds.num = 0; for (i = 0; i < master->num_streams; i++) { @@ -2452,7 +2453,7 @@ static void arm_smmu_enable_ats(struct arm_smmu_master *master) /* * ATC invalidation of PASID 0 causes the entire ATC to be flushed. */ - arm_smmu_atc_inv_master(master); + arm_smmu_atc_inv_master(master, IOMMU_NO_PASID); if (pci_enable_ats(pdev, stu)) dev_err(master->dev, "Failed to enable ATS (STU %zu)\n", stu); } @@ -2523,7 +2524,8 @@ arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain, } static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain) + struct arm_smmu_domain *smmu_domain, + ioasid_t ssid) { struct arm_smmu_master_domain *master_domain; unsigned long flags; @@ -2533,8 +2535,7 @@ static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, return; spin_lock_irqsave(&smmu_domain->devices_lock, flags); - master_domain = arm_smmu_find_master_domain(smmu_domain, master, - IOMMU_NO_PASID); + master_domain = arm_smmu_find_master_domain(smmu_domain, master, ssid); if (master_domain) { list_del(&master_domain->devices_elm); kfree(master_domain); @@ -2554,7 +2555,7 @@ struct attach_state { */ static int arm_smmu_attach_prepare(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, - struct attach_state *state) + ioasid_t ssid, struct attach_state *state) { struct arm_smmu_master_domain *master_domain; unsigned long flags; @@ -2570,6 +2571,7 @@ static int arm_smmu_attach_prepare(struct arm_smmu_master *master, if (!master_domain) return -ENOMEM; master_domain->master = master; + master_domain->ssid = ssid; state->want_ats = arm_smmu_ats_supported(master); @@ -2600,7 +2602,7 @@ static int arm_smmu_attach_prepare(struct arm_smmu_master *master, * smmu_domain->devices list. */ static void arm_smmu_attach_commit(struct arm_smmu_master *master, - struct attach_state *state) + ioasid_t ssid, struct attach_state *state) { lockdep_assert_held(&arm_smmu_asid_lock); @@ -2615,12 +2617,13 @@ static void arm_smmu_attach_commit(struct arm_smmu_master *master, * SMMU is translating for the new domain and both the old&new * domain will issue invalidations. */ - arm_smmu_atc_inv_master(master); + arm_smmu_atc_inv_master(master, ssid); } arm_smmu_remove_master_domain( master, - to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev))); + to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)), + ssid); } static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) @@ -2666,7 +2669,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) */ mutex_lock(&arm_smmu_asid_lock); - ret = arm_smmu_attach_prepare(master, smmu_domain, &state); + ret = arm_smmu_attach_prepare(master, smmu_domain, IOMMU_NO_PASID, + &state); if (ret) { mutex_unlock(&arm_smmu_asid_lock); return ret; @@ -2692,7 +2696,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) break; } - arm_smmu_attach_commit(master, &state); + arm_smmu_attach_commit(master, IOMMU_NO_PASID, &state); mutex_unlock(&arm_smmu_asid_lock); return 0; } @@ -2757,8 +2761,9 @@ static int arm_smmu_attach_dev_ste(struct device *dev, if (old_domain) { if (master->ats_enabled) - arm_smmu_atc_inv_master(master); - arm_smmu_remove_master_domain(master, old_domain); + arm_smmu_atc_inv_master(master, IOMMU_NO_PASID); + arm_smmu_remove_master_domain(master, old_domain, + IOMMU_NO_PASID); } master->ats_enabled = false; From patchwork Wed Nov 1 23:36:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443206 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 103B2C4167D for ; Thu, 2 Nov 2023 00:45:44 +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=IPYEhTV/5GeLoABsX+0aNkPOWFDLif4KMmavLcQVSSg=; b=WyqdAmLOKw8e43 a7VUTkm0xecvQsu8sbmR+AEEkzwqpP37T/ZmUp9iyV0uM0Nva2enynAo7OXuk1fqcayeglnvgWYVj n6ObUeg0VYnxgdfu1VqpRvBBDxapUIzK94+BkvY/BdEhGxzaWw4Dv3VAXl8D3HDHDh8NfbHtuZySY M5cwvLWgfbI/EF3svdIqTPM8KmxzBjdYJh93L64JU0OxGK7X9/4RXCI9T2VJPpx/LjzOFOlstPsP3 axeeQKIdFWZum/e4Vx1Hr7HbNDtvWHCBJ2KA3iiXv8Cp7nSZK2IFZz7QgD9aGdWZUgViV2d9IO6zG A52BqpPI73Wny7hxyZJg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLqB-008Q2F-1P; Thu, 02 Nov 2023 00:45:43 +0000 Received: from mail-bn8nam04on20610.outbound.protection.outlook.com ([2a01:111:f400:7e8d::610] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKmK-008JV7-29 for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:57 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=m2z+9F/0fFpazGcRFSCPK3y22gXdwbGfWk5mUcz9cYLqqcKfJuqqLIygIVoOTuVk3y9YRSTH1kNAvlqpzSfaMfAaH3mlLpB5YguR6VjnbxJzIobC66fDqzhYcU+11niQy7TM2CPaOTdKCFcAr/CIoS1Iyxw3ImxwVL5mZqU4s5AC7aT8uyhNP9IHqp+j3oV8QUHvBIaetvBp9vDalhTJ0djHLW+sRRs8LgrGEhhN8C3NUSSBD6mt0w0YsTM2BZ1gFfR4GqmOxqHEn0HhsoBiewL1a/NUb+nzngzOGlyaEXucNqFOgoh6n3VDMMOeKkJeg5dFGUUStlLXv0EnfNm0LA== 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=r4rURhKAwbTVOUaKwLxWefbMSxB6FAqr6mou4/Mqf1M=; b=NtwmVExJY30oI+j7u1Jjnxwf1Haxvx+0nTezjP3FMZ+mM+3LrEf85gu6BOCx5sSmMWjyqvrXcSkIyE2YRQtY4P53iAKg/qWP2rBK7Ib2KsUZ9Olt5V+0Y7e40W/RWdjfAmsXCd2tsPEG3/x4SrrYWwGo6StUTXbIIUHk4dxlD3rM65jyE+HDzV6XeWdD14apABG5PskFS2CGpSIJaS7kx/RjVfa1j4d6EtEvLk6ZNq5d2MBN32zrjxC0bZNeQn219givIVdQJimCEdgvsetKtfgmN/A2oqfWkFaIbf2/Bq+dDlvIoA30Vxm14ihlWTZ6I59BqZnZyYfIJr9X/5b7SQ== 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=r4rURhKAwbTVOUaKwLxWefbMSxB6FAqr6mou4/Mqf1M=; b=uRb+rTx2HZ5450Qs76Im14xxTo1ehf0CPDLFxgldq5FybTpmv4/qkbheyQUMdTqWac4Ir61vnKM9BfNuerP9pFIi65IRswbMhXx9fJ0hf7NQuyHCIugnKkNBeg32QIWDqFVb5PBkT5U2D7YVxNcOhCjNmWIIcwkRZm8WAJxqpl0c0yygfeW+2EJtygQasZIPf6CiyT/HxL1dzne72lg59nP+Km9+k4akFzVjBJFN3EEP214BIsjkV6TQ+/CIXQ4vofW0GHFY7xU6bcUTV8h1jKyah4mwQ8YP1PLGZlc3ZFZ3YeSjCfNL7jg/YYHQOS//AmSxvMs0gcorvl8476Z3kw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:52 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:52 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 18/27] iommu/arm-smmu-v3: Make SVA allocate a normal arm_smmu_domain Date: Wed, 1 Nov 2023 20:36:36 -0300 Message-ID: <18-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR19CA0041.namprd19.prod.outlook.com (2603:10b6:208:19b::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 10de8ff1-40fe-47a3-9cf6-08dbdb336a8c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tl8Kuh8WvAuG2nnCn9rMjnaLs+rOxcBpnw6VkRcRcrRJ1i7sG7dffIWi03GlIMDDOwnh0fw7zilBsEr3M2svyAJZ9vtCFoAVIKDUjYbrh9iwycfql9ZRSSUjnWjZ/iNzGN0mR/2V0E7OvdXY1XjIp1wdJBEqaSxpoxD/BXrHL0+C1CLcIm27lXxeXXSyk8TaUf1hBB7xBuB3ZoTmGns0S63ntfXwK035ro3jSzOsYW73DTy1vH6nK0IeOU2UqtdmBBn/4BuK8B9ylSSFwNT8dAhxz7FyAlyQXqeQU6FOYFuH09apZu0pUcLAoZ1a7Og4ynZSoSTxikjwEX3eCoKfHvmxcLDtsBlURcyei0AgS4wAwNalehkVYTfqi0G5vWpBC10tUi4WSYdI45NUrqbnp1O2ijQtSN6IQWtmxvrL+ZL7FuQrlHa0Kqgtf/Mk1SOr0hOgw33CCEc9JvKLZx7c9wTSqRH73QRLaVzfsGV+U/ncn7bXCdpn1zBp8gAhSLmrk7YmR9QBEb5BAEZtMzkEGxEE27zvanzFs0N7ajhEp9cDzif0aJ+tkQnrSUYPQ4Hz X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: bkmkQnQiJinwl6vvcQLui+Zfdh1ykhDRGLapewysIDnRdN26gS/vwiI3J0Vh1M6cnlTJDmz1TCgV7qclea2SazPKBLk8/Xg7Bw1MHARQpMppx4eP2penbB/NlJpJCoB3ngiq7zGRm7N4PLtFLiT9hivzpeZTxz777fJWDOfgCG5n+ZITiiqf6+S1NNpKCs4mLwNEhZl9pz4qC+GKpSLGLBd/bsWAuJrAg7MrVlcfUuJvSFn9aub/9F0efEDd8AInUCBjXwC16fYyEa0Pvtz1Np+aKmz2NPi6wDLcX3MiQelZQLvtmcQup2dZSf6RzdUlcOmuCM7DUwOfCIiTKG2KAQ5y/JHmNGeXyzkWoM0rgqsSnXxCxSqM8WzrgZyCTE2CxZ5jp4C2TBk8mY/Jaxjdw5ScHOECdSFDoNUxGCvxkPwrGiSnMRRQbkOAUwM2TSOtPwUOJb2nQfIdASfwxX2kSkkvciJdOrQzWo/FVks4/WMEsQQFUbB9CRwANHcBlTucRuOnIKFsmYvWNIk9wjEhfgBXJIaBWNkov/cLCiy68KORTAAm/+4cshxp22JlBZnoy01lMgi4ynrnAUbqbDMtiOzFmPtzVLU7N7Y8X5qS/pKfRhvrEdZpyPYGzQnSGJJSgWgp7DwYys6acPzp+R0vIsHEZvnNk+LLnXqU4POKuBrtMtgmdOvfDuKaIlp6cn/ydXh0jCW1mS49pgg3uTXisHnxRHs95z8lJPAC9jy0fljapUfbi/paHFv2HhHJ22uR+OY7lkrxI4QCAt8bOh5DD67gmZXcd+rhshMeyOOXVknSsLXQ49RUOiLfZHTj6Q5D1Fz0QYkISawP+E7jbrhRgRI/IhSJYzhh71JDR0OdOG8l8IPmIO7R8Ix8oyZc78vTozFLRwD+BYBXcJNLZkoOzOcRx/Ot+ngbxfYXuQa795ECER80De26Hkzar3xq7s0ieCNYitnH9jBewwHz3fQmykAI+AKCXMNPRYcK2DFYkucVK94b8PUKvyx4Nxjm3eUCAt69N4PQyiTzJWu24pOnLIrF2hb4+u05ZOMFcDhtqmoEBoLlq8KkQ8eXjXvUd8L21r7IcL+IJpWv1/Y0Ep0QBO5sE8H2a1tLBbeWFWsMGuSoFQ6bdiB/dEHaMKwFG13szTALe/RLsougXmb9yge05bKBC8QHlajqm5ZDMbE0vaar4Wgt01dohR+dGP84Hn52FJsvjjOfQ0i41Dld2qJiX3jNvgOc6K50VVglVZbkAo5ERABC0BTK95zNLdRqNv6bwLdsj6EQjRNYIHNJemiyD69jO0/IfmKA5Jb00XTe803bLRwlrw9OH2RtWzuojluYeQan1nHRGPrT5penCSyrC3BMXm5fRabEKgLYUplNQ1iIhEVrbgd60Hz6kMo+eQBEenjOcJax+MCmnvCSF7Z7EVQkj56KbuD4yOUruli1yB5VD+DxcgQ2i0xeExxH0u+mJsU7K1eHFvZM5zTEOm81GXl3DWrZjBLWSbB7dxnTv5/74c1oCbwjgC6e147l/GsYfV3NvX2ijpFyK1tJlvuhBD+tnLUuf2ZB+bQIdvi7EbXLG3FgSuuLRJJywTeeCcHL X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 10de8ff1-40fe-47a3-9cf6-08dbdb336a8c X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:48.1311 (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: WVQCInor/WrmlX0hG9ffybpgNNDxcMNAhH4oOoNC0rpa6Y4IgJMAnXkDUG5b1mSR X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163740_727902_031E306F X-CRM114-Status: GOOD ( 16.88 ) 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 Currently the SVA domain is a naked struct iommu_domain, allocate a struct arm_smmu_domain instead. This is necessary to be able to use the struct arm_master_domain mechanism. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 12 +++---- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 34 +++++++++++-------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 7 ++-- 3 files changed, 30 insertions(+), 23 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 2f818b24d931c2..5894785aa901e8 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 @@ -646,14 +646,14 @@ static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { .free = arm_smmu_sva_domain_free }; -struct iommu_domain *arm_smmu_sva_domain_alloc(void) +struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned type) { - struct iommu_domain *domain; + struct arm_smmu_domain *smmu_domain; - domain = kzalloc(sizeof(*domain), GFP_KERNEL); - if (!domain) + smmu_domain = arm_smmu_domain_alloc(); + if (!smmu_domain) return NULL; - domain->ops = &arm_smmu_sva_domain_ops; + smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; - return domain; + return &smmu_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 d9174d609659d2..326c82fad90b8a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2229,23 +2229,10 @@ static bool arm_smmu_capable(struct device *dev, enum iommu_cap cap) } } -static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) -{ - - if (type == IOMMU_DOMAIN_SVA) - return arm_smmu_sva_domain_alloc(); - return NULL; -} - -static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) +struct arm_smmu_domain *arm_smmu_domain_alloc(void) { struct arm_smmu_domain *smmu_domain; - /* - * Allocate the domain and initialise some of its data structures. - * We can't really do anything meaningful until we've added a - * master. - */ smmu_domain = kzalloc(sizeof(*smmu_domain), GFP_KERNEL); if (!smmu_domain) return NULL; @@ -2255,6 +2242,23 @@ static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) spin_lock_init(&smmu_domain->devices_lock); INIT_LIST_HEAD(&smmu_domain->mmu_notifiers); + return smmu_domain; +} + +static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) +{ + struct arm_smmu_domain *smmu_domain; + + smmu_domain = arm_smmu_domain_alloc(); + if (!smmu_domain) + return NULL; + + /* + * Allocate the domain and initialise some of its data structures. + * We can't really do anything meaningful until we've added a + * master. + */ + if (dev) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); @@ -3207,7 +3211,7 @@ static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_domain, .capable = arm_smmu_capable, - .domain_alloc = arm_smmu_domain_alloc, + .domain_alloc = arm_smmu_sva_domain_alloc, .domain_alloc_paging = arm_smmu_domain_alloc_paging, .probe_device = arm_smmu_probe_device, .release_device = arm_smmu_release_device, 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 f72aebaf95f981..c1181e456b7d5f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -765,7 +765,8 @@ to_smmu_domain_safe(struct iommu_domain *domain) { if (!domain) return NULL; - if (domain->type & __IOMMU_DOMAIN_PAGING) + if (domain->type & __IOMMU_DOMAIN_PAGING || + domain->type == IOMMU_DOMAIN_SVA) return to_smmu_domain(domain); return NULL; } @@ -773,6 +774,8 @@ to_smmu_domain_safe(struct iommu_domain *domain) extern struct xarray arm_smmu_asid_xa; extern struct mutex arm_smmu_asid_lock; +struct arm_smmu_domain *arm_smmu_domain_alloc(void); + void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid); struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid); @@ -805,7 +808,7 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master); int arm_smmu_master_disable_sva(struct arm_smmu_master *master); bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master); void arm_smmu_sva_notifier_synchronize(void); -struct iommu_domain *arm_smmu_sva_domain_alloc(void); +struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned int type); void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id); #else /* CONFIG_ARM_SMMU_V3_SVA */ From patchwork Wed Nov 1 23:36:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443142 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 E7090C4332F for ; Wed, 1 Nov 2023 23:37:39 +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=G3gMhWlskHIbimR1X2bc32gity7N5SOiguDx758acPU=; b=Pe3nCTrkf14Qxo giiLj2UgrMhc1qhOBbO1qdgztbdVcmxecoQ+W9hm8CpZNt0XZ8+0gWdTiA3Gb1BUCCQ87xv0UREoi DtIxTu0HLcszfng7fjjU6O8UDw3PMgCuljf0WJ82zXlvpzCdnsnhPHmHkxKWLVuGzfkup0rUrpB/k MwfFJVzNyi/2/BLk5uvHHOd9Fnj/uQyIEDnX0NmheOXYYT/to1ucpQoQ3lCqcJXBCwGf88Gb9aC1h jwtd0+1M/2ZnN4aRrzz4OTqz32XdRkZwly6OKhCIqhU7L4zT2KgCi/1AFlsPTg3YXVQSK+t/5BnOL CF0WprkKBeIn+jDhnxsw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKll-008JbF-2u; Wed, 01 Nov 2023 23:37:05 +0000 Received: from mail-bn8nam04on20610.outbound.protection.outlook.com ([2a01:111:f400:7e8d::610] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKle-008JV7-0K for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OroIC8hBVbYYWsvu8QNTxhhuIbc/wg8h1VaSu+0dOP7rxbD91VkN0RBni9ZqE0ZA8r3BJ1zHODVKP9Y//N0dYsTCV1iQknE+D2wcKY2UpWq0Xh/x9k/n2DkIPMqQ9zhut9hqr/WJk1XYfT7ftB1j6AHLTBcafuIq3dG9NiaZiSVdddg7584FfIQuNOpVpkHbykQFlxKvZXk0Vc2OOJ+I7J9Hu+lqFyAMP/hiZ+ZLlFG6gCyGt4hTXsL8tNM+96PkqvP2BWu11Fj5jg8WkcxDhu35BFHFsxN+6h56rMFTGgZDannzoQnKU/dgeyPrrGaUDFG8wZ2vxtb4DtBqivIRCw== 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=ku7Dilck+INBq9I8VJlFIh/CUuychxsmLXhaajGORho=; b=NeaWQnXR6HsXuEgDQv2XMdtLOnavt0HEQVb3lP+sCJjB5/UPSDVVbEHzjLpJ2WHe2O4N//ga4gpriZiPqi6f5QKb2u87fTl2hPvEWAf4qWCdAZvuRznn2KIZJzD0D+d6jsxK8Fp/pBFMuQpoave+m/h37RNHWisb/aDKUnrblJI0UevmPtYhbWipQ/7jwhwUtAPX7EwUG1N6F3j2wnfA4uJ6t6jVXxo6KX0P3DTTGJ7vB89Xjx/fSFwhH5v7inIP8x0aJgb3q7a502xPaKG2vZi3cEy86KcCBnmyzNKzoIth2AEUUg1zT03LdkdAp2oAuJJ8fX/AiVSucYp6Qp0mVA== 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=ku7Dilck+INBq9I8VJlFIh/CUuychxsmLXhaajGORho=; b=A4zsnDm5Lm9G9zW9jvkSrMRy5Fr/fy8rWeJ+B2XS5xD7MxHa1Lm9ZKF5npq95CFRLlI0iLx9hz8ja7G1qJQ8Y+dj56xYomKXHrMsUDj/pVHuf/ZbyT+bnK7BKJ9R8h1OavV7rBFpAypev72tpAqdmjm9ea57WhNZDsG8wLHN31djt3B1Ql1BhZolZlzz+Jw0rlKjp5n2Nwy23K8iCNk8wxlVunW03UQ4ptIKcX7u1lEwOLcEJAwuoKvQGbAah2RXaHvhNCwamm8V1yEJD4VaJDWjM2C7B5vnNXEzlk1HLZ9Mr3bq6Vc90yjOq9Bs40zY+6WZBE2l0/3K0n08IvWLRg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:48 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:48 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 19/27] iommu/arm-smmu-v3: Keep track of arm_smmu_master_domain for SVA Date: Wed, 1 Nov 2023 20:36:37 -0300 Message-ID: <19-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR02CA0003.namprd02.prod.outlook.com (2603:10b6:208:fc::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 301b1fcc-84b2-40b9-301b-08dbdb3369bd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2kZ2MmrWhpkQShE3cyiuQrXlV/pEb8pxnBpDUt5D6F9O7maflhMc4Bz0F1qNC93z0lad9P94LjfBxRRZfaVrsrxohsdeLxejuDMywhQo0XaDakLDCUteVM70ZtmWWLQGd/wnDnbK4P6sMnkmt3kyaPfQgJ45XT+DSe8694QEYr1jz/LA2c9j68q916yfyffEMIWkGirUc4KgaQ7kaI1xLUC51md+gZeoWCkF92qnyQs0oVaf7FZvpDCRDmY+zpBfZtrVq/QU13ZkziQKd751dGpX7lPS7MEYHSvf/TmwHwNfoNcMv3lptiK1VuPdZb9O93pnqaDag8yhblzQujp6lthTZLWRbIk6ctOgIa7ZVSeRs8qt805rwA/8lhqVQ1ZGCZ6ys24S72SiJnfZ6UhX7sBcELp2ipW9W26OOAvR0XzsGOQugb2KhSibBPCa+/Pvv1V92JeHX47HUOOGLmwv1sF+xieQ4CMSefAPd/eiF3R9YLrWIC06McO6NOpELYZZIBjYTPf7oSB1//Xm3BqgcJwmRol87hfCCniO7wtsbuTz88ZRICt46JZTGM82ZM22 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: c2oF/74rFOerATxSLqO2z2Y16W4gYejp4VcsAVYUzrlTZfPzRb18BuVtSmATotUdBHSc0bWyTzjExA1FrjdkopLzvdkjt/gNuxHURioDLV2fa3jZY4s+ar4S5Lj2em8KYubdY0y/r6MNo4zpWZx4h+QvC5KKK5McG6WLkaWXR6r2wUjaSKrT51jFBWmPaG2BtHnuZCYl7gXYf7dV6mM+zdSDNWtTrFhWcTl+qao0wJx4BePjEAqHqmeQ98OvIsjZaWFeU5fSbciI6EClc6XsccJklGd7Lt6RRO2kGwvSf2JhXYCxiNjTy0CruJt2O0Nfv3w2qI0Tx4AR3QLWWsk6sL/jLvjh6I9mkahVxrShIw4wEqzikbqOd7LW2X1k46AfJ0RPXvVc4ib3hO6mrlSbccAPR5TSXbwTYDAyOjX7e5AaSpMFDrBHEc0EIk6oFc3FEwDBlB4Sg9mTbR36GPhYF0vstfC+H5d+ANioFXrvvitelMXOa9FbgK3XT9qScNTzj5uRnulbkPl79DIhABoAmBNdsaEUy7rHxELBr9YGCzTe2c7U33qzBUjJit2XSM44LKW33OZc9MRgV7ChO2MhPISm30lvU/9TEVvd5RDHTvHhaL7aiEftvUTsPKwSxvHnyqA9qwqdeboOKcRD4HjtVj89eBjvn+nQ75rQodZkG4o/yh+fQuqncdylJYFylfRqx5qzWSrgxXJA5sCfekaA4TeR5x4xbGnC2ZFlsTwPYnryZk2J5MvILuBcPopruIRtxPmytPpKHh+UMty4HFBfHW3IOXf30gv5No56Md5aJVcjxVYTii0wkMg1ijrbwRF4M8U5/isK6ZsDuwLpic0yHY6rdoRbxqYNSCl3jX9M5fssTsVdaVm/0SGg8u/0dxGP3GY5DgPOS0wtcSBMW+ZzSVeqMmw89DeEvs4ZST8nFLJ/ShR5I2GKBUxI1Xoo+uIej7t7v+AtJVgrENoMyyPvUSvrdg5NHgxNvB7n8DBvVDaeGJbMN2eA8/Jg8osH+fuB5vYgQgPMh+VF62e2rb/d5TUZIju1nY4SfBxUdGHcJ6Dm+RMSPmQxZnHnwcjIjy9MgnVl2LvAAO7Ii2xyDOPNDc0qi7NIvply8CARlgHAeCPuucVhBjPKMrVBSbx4unuclH0IV5Ee0O8PSDERDVBuN0Ztc1VuVbAs9HcW6+NRIbqfLx0TrVNwCLgdiMdVDQIJkY4KQC43IBEb/+E68pZ/sE0NLLwpCPTyPvPaOsRvDGoLmMTo4VTungbHa3RM4NfPBYjCFDYcKQQ2ixwNUKc8DbDJfWhlLOPQtoJtuixsSaKLOMQAjeQbu1trh4vT+pwMJSi8ntonCubnM9KYHpmaII8NNTmKri8LxcHAIwkLKbIdptxojq1RhyUsW1O03A/0kWVrCl69gMJe4Bu9YBZCFeAnvFaz+rJ0Q1SVsMOLjSyLIGXilgCKzW0F1FmXh6WD+5hm360H7idvE/42wy7rwEcR8YvP4cy12bcmTai2zk7vltYwEt8WuZsF/DEZj9zLua2tYwjfprF2qxJtlR/CzoOUDdgv15jju6ZEdzQ21ZRB7oFiBF8LgcTcSzRGR7fb X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 301b1fcc-84b2-40b9-301b-08dbdb3369bd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:46.8139 (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: bs2S/dsQxdBpf+UhNoKVmts1b2jVh+GdA4ug2g1dP3eavN/ZZKQwAqU0IiykKWEe X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163658_138734_C11C8B47 X-CRM114-Status: UNSURE ( 9.72 ) X-CRM114-Notice: Please train this message. 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 Currently the smmu_domain->devices list is unused for SVA domains. Fill it in with the SSID and master of every arm_smmu_set_pasid() using the same logic as the RID attach. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 326c82fad90b8a..23bcdf1630c23e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2712,6 +2712,8 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *sid_smmu_domain = to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); struct arm_smmu_cd *cdptr; + struct attach_state state; + int ret; if (!sid_smmu_domain || sid_smmu_domain->stage != ARM_SMMU_DOMAIN_S1) return -ENODEV; @@ -2719,14 +2721,30 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, cdptr = arm_smmu_get_cd_ptr(master, pasid); if (!cdptr) return -ENOMEM; + + mutex_lock(&arm_smmu_asid_lock); + ret = arm_smmu_attach_prepare(master, smmu_domain, pasid, &state); + if (ret) + goto out_unlock; + arm_smmu_write_cd_entry(master, pasid, cdptr, cd); + + arm_smmu_attach_commit(master, pasid, &state); + +out_unlock: + mutex_unlock(&arm_smmu_asid_lock); return 0; } void arm_smmu_remove_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid) { + mutex_lock(&arm_smmu_asid_lock); arm_smmu_clear_cd(master, pasid); + if (master->ats_enabled) + arm_smmu_atc_inv_master(master, pasid); + arm_smmu_remove_master_domain(master, smmu_domain, pasid); + mutex_unlock(&arm_smmu_asid_lock); } static int arm_smmu_attach_dev_ste(struct device *dev, From patchwork Wed Nov 1 23:36:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443211 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 0FB23C001DB for ; Thu, 2 Nov 2023 00:45:50 +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=iiHe7I0kblUBZ0YSAJBxLYBF7x9qb/TMGQcugUfcpJA=; b=IFg/NJzQl2hf0/ oDzrsm2q0KKXZBdgMsNfnP3AELPAqS52VC90EtfefPwnTfMi+vNtQrLGwWerbuAy92yzZr+4aCnwA SqYQjsWCIRlU1TfzH5SM4WSEkXwtUycx3ssNB4zM40fVVvInzaKasQlu34hFCLtP0A2WrMRaqX2lU M9z9CvYx6WCjM6BpF090Ln5CoWKT1lip9jJG2mbSHAqUFYbTi9z+w3GX3U6k6BKtac6D5fC+wimPc 2l5Hl9KDt6Ub1YrXedmiUlFMoeW2cxLW7KEC6e8Z7R8zI2olu9mQvFhQNCjWc/KN6arlEfx5rztxu pHlg6AdEVMaUtgjO4QxQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq8-008Q0Z-2G; Thu, 02 Nov 2023 00:45:40 +0000 Received: from mail-bn8nam04on20612.outbound.protection.outlook.com ([2a01:111:f400:7e8d::612] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKm7-008JbE-35 for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RcrAXaVNAgeL0QexnizWJriklwyBRWPRH8au+wT2PB3H7hrxnyBzZOuuLkiCLDUok0UlGMLugjQQwBJU+iUakAGD0PXTFwKAaXCkEMursznuImp2gOppZukuYPhzBK2dfeNACoXH4W0bV68zFrZMp7GxGRXtKuuf4vK704TMjavL5XRnHwlXm5g5W/Pho7zbSMcNcSKulctRSLivdvhY8KM+hWz+cvl4St3ZcrC5dwo+EHYS3VzDKDks7D11CRlgn7l6737P/u6e5wHzN5UHbqvaRBewqP3tQLj+mSZ0AWTp+irm/wd40cYIL5K0vHZZdZIlmWVh8dGnFGVy16N8jA== 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=DHXnVeR7Hy04BXg+n8vuj9zFFEzEDyMuDosBTlpCPWo=; b=C+1hL6YqtZ63k1nM1w3sj8SK7Z0AhDbZ0xiUnPYgK0cU1uHmNQtNfo86dpfhGPg/DxyzeRAqnVY1a3X80sRpzx4cndf7jMle3G/OR9d8oi2DU2km/E5NSdEZp0bvPUFl54EL4JInTs034DJxNCz8gNkzWI2/OfP67dDSuFEblZiTX/d7kHrD0EJWSS8IHhG2CUBS4MgBqKn2HeyUqSgOUJC+gflbdKkKWIYXdGZOhmD3RprUrV0iJ0DPFLLvI0fDMHAcFXZxenQ8Eq3hDFuIYN9Vz5qCt2by/8ybC8RQjM9oy3y5Py/5AqNX4UFXmgoxS5PlKvhq939Dgw2Lc3Jqbg== 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=DHXnVeR7Hy04BXg+n8vuj9zFFEzEDyMuDosBTlpCPWo=; b=BI+XTBOLNoDXL4uwse7DiS8bYtEEwYpnbTix/C6elVRZjvUoKqTauiyO3Hxn+PFkju0vmXA/lz0UHA1/K1Al78lZbdzDzqpYO6SGPuogo7pMhlV2a0i25feP/6AP+4dPFsQaZMz2huZ1PaAW4PKX861dxdk1raMBItQcYp6AzTaPSTAtpeE6l3T0La8rW4PJN+GT+yGmf/TM7kISTx0SDfthVgshuV9DanoOjegDQ5rWMtI8gBbXTQG0SIqC3jx9Qs/pE+mMg5P+ddHxyQlkvdUnn2rx5JlqIxAG+VMJc0z6F+5rN5oA+JgmYiZaRHe5IXmT72rR2Or+TkwxBK79TQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:51 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:51 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 20/27] iommu: Add ops->domain_alloc_sva() Date: Wed, 1 Nov 2023 20:36:38 -0300 Message-ID: <20-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR13CA0012.namprd13.prod.outlook.com (2603:10b6:208:160::25) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: e63f6c56-7ff4-4bb9-2910-08dbdb336a58 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8ddvqcmtjtB7EBOlDNVvApM+KsBcQPE4TkpaVj0Hfe+XB4kwjDvM2Fxvnl8sfFdWCiQ6y5CPUoli9fOm+SHVvINbjCM0gMBP9ai1maFDx+7ET0bUG5e2JpSH/nSQhLORxDBugmJLwK61HdLPNoSz0sXTXupSepcDUu0D4n3RZ7iZVYTX1NsXb2F6pxyWwLy19BvdfZfplfJy+pjrSbmrjeWA06CF5KEhL+BQNGd7Pzh/rKzl71BbeF9kNUGUR2Cvj8nOpfZxfWP5ca70BEkvPrCob9CQphkOu8cKVGIMK1K3G75H//JI7pw8pKe/HPPzTD2V2CisL/twXpeS8KEslMlbuYCfT//PmLvGBd4e+0EuLQdFYCIEBBQbZ3ZkCKwyiZ7BrtaD7HWydEW2dPeAmqXwo/K96BiyV6VFVyWmbbhlGoPJ4i6Ac0YSQ+glomIHku9fitMGdHdZP7yIrNR0w4+DB41SMKbvajPnwcfEdip57THbmvK2aZrvu1cBPqPum9CkAXQowKiQugAVbG6B3sfiT6GioUtanQ9Z0ctU8NVMiIbTBSxNAfhfqJAw8J1J X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZJ6y4+uYKbsIzquKHME2ER2G3iie6nZuFNQTG3E6YIhnQArYdeyIggHdUic5wegAwKaYKN2+4fNOPukasef22XsUnVBg17xVbwKrO6QTBYTFZtw7kOAQmaQC4I2rhFzY6XOTE5RQlmZAn2b8fhdB6jDq+wRysxYx82Rr3foM6yzx4mp9UBqmXwwMN8bbY5wDcJ+c/BlhOHTDrFhuR+t0Gpy+wA1r5XxhLqLfKgIeTdGf8kN9MwWIdpsdDgwXMQ3ENNGXDe5JBMt/QZivWd5JBMblLXRgXO+hzmRENqhvC7gh30yYXAqPNSPwiN+4PJP41nI80XaBW8wj2q46cPBsarL2jlU62XNPUIb4rVzpCpO4dK9N6P8wkk3EpMdR2s+bWobJE2f922Hu5nTvEmWSoWIrl4AFt1VGiO3BX2O+zdD+1R8563z8SKE4CBJv4uKe2mrV1Ihsy00gLyy+CZQP6sbiyXONm9dQhAYAdtyyC3hZUOTGR3qxRjKSNFd+9VYh1Zo9gwA8pmsgwNCT6n53xEfPnA4RIX0DwMyJl0YgMyeyMx9ydSftS4+JOjpQZaSNOryTw+VW59XsW7MpBnaEY4HVZM7q+Pw+LnXGyrpUW3K6+jkQzzyOn3XBoHqTu1fvfXs2jkWfbx5NcfXvCszbISVXvgsTVv7GixN/WKA0ubDYoiRCF3HbyLOEZVUgDHnoOeJWE9vWxMBmOVCVt9KvXLIVxGUZx4aBIaAtXOgtzv748Rfo+vrJqSEdPrC0QmSVb3qu8f67DUmZKwRDjUCW6uux3PqfaUMlpDgqNPbemEG5t/HiEbKQyKfH9ctBfv2lwmHggDHNTvo+DL1IhNK/ojoG6V16Pwc/ZWkhXteFdH4VnSwz6P63/BghZwS20MedPhtpdiYcHBUR9Buh6yatHoam5t3GcytgYTR6laRt+hKRKfnKqhKMt+YQV/fm/ON+pex9AyyuzNWPSXW1rlypKIBETbBIMXwJ+P0cpeKzDRB4jP3oWI+LeppcL8k09jE9JvLHLicMlJDScol6fIk6iSqeM4yqUjmtcsYV9/rHVvV0guGvqfpMjL7Qon8gMiDuYdp4EZauwKjwtn0OoY/Ez87qLHpN9ATQ8x4XeOiIMqo2LdLI5jqIw5KulIOgvaSoG65kauYHZtBCtfpa32t5nqjon5eUzSh749Ll5Uun/ODlncux8dhtF6OqNntX/Ktrxbj85iS0reIRPFwv6xeaHdUTd2AnkPaRgoaODamjw6DKKWruBm+J7iQbe6lzflnPXKMT9xivoRXC6S8x6IS05b0CDPc8fFPSxMmYSxdSjfzz1p0V+M802U1Zq4WLn6TF9x1hw9YerXcB82TsTnhcoVv3j6ME88WKAO0nseKB7/B+YhUmISMuHKDTCi9ql/ZLio8cxBRlA7gPh21U2gjsSyKlTEcV/KZPJYF1BXhEWdqhPywULC5/2L3UnoKHTaAUS63Jl0NqTLUzus61/yMjcrmQnu08pceAxnY6hUoMLjkHfTNHm5Wvg/817PZOwuiwZAMdY1H3JBRt1bmJze9UGG9jDGenh46ZRCR74fJOKgpBaFV6f3TCZANBxwLrfM7k X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e63f6c56-7ff4-4bb9-2910-08dbdb336a58 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.8427 (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: lR02FF3+BdwtgiDCiKkTod6kcNaIOTsk18JHRNhkFjJAJ84BCWTxZ0ezP/kavc93 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163728_000938_907A4BD5 X-CRM114-Status: GOOD ( 20.84 ) 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 Make a new op that receives the device and the mm_struct that the SVA domain should be created for. Unlike domain_alloc_paging() the dev argument is never NULL here. This allows drivers to fully initialize the SVA domain and allocate the mmu_notifier during allocation. It allows the notifier lifetime to follow the lifetime of the iommu_domain. Since we have only one call site, upgrade the new op to return ERR_PTR instead of NULL. Change SMMUv3 to use the new op. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 12 +++++++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 +++++- drivers/iommu/iommu-sva.c | 4 ++-- drivers/iommu/iommu.c | 12 +++++++++--- include/linux/iommu.h | 3 +++ 6 files changed, 29 insertions(+), 10 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 5894785aa901e8..991daffbee31aa 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 @@ -638,7 +638,7 @@ static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, static void arm_smmu_sva_domain_free(struct iommu_domain *domain) { - kfree(domain); + kfree(to_smmu_domain(domain)); } static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { @@ -646,14 +646,20 @@ static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { .free = arm_smmu_sva_domain_free }; -struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned type) +struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, + struct mm_struct *mm) { + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_domain *smmu_domain; smmu_domain = arm_smmu_domain_alloc(); if (!smmu_domain) - return NULL; + return ERR_PTR(-ENOMEM); + + smmu_domain->domain.type = IOMMU_DOMAIN_SVA; smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; + smmu_domain->smmu = smmu; return &smmu_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 23bcdf1630c23e..85fc3064675931 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -3229,8 +3229,8 @@ static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_domain, .capable = arm_smmu_capable, - .domain_alloc = arm_smmu_sva_domain_alloc, .domain_alloc_paging = arm_smmu_domain_alloc_paging, + .domain_alloc_sva = arm_smmu_sva_domain_alloc, .probe_device = arm_smmu_probe_device, .release_device = arm_smmu_release_device, .device_group = arm_smmu_device_group, 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 c1181e456b7d5f..48871c8ee8c88c 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -808,7 +808,8 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master); int arm_smmu_master_disable_sva(struct arm_smmu_master *master); bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master); void arm_smmu_sva_notifier_synchronize(void); -struct iommu_domain *arm_smmu_sva_domain_alloc(unsigned int type); +struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, + struct mm_struct *mm); void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id); #else /* CONFIG_ARM_SMMU_V3_SVA */ @@ -854,5 +855,8 @@ static inline void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, ioasid_t id) { } + +#define arm_smmu_sva_domain_alloc NULL + #endif /* CONFIG_ARM_SMMU_V3_SVA */ #endif /* _ARM_SMMU_V3_H */ diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index b78671a8a9143f..a52b206793b420 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -87,8 +87,8 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm /* Allocate a new domain and set it on device pasid. */ domain = iommu_sva_domain_alloc(dev, mm); - if (!domain) { - ret = -ENOMEM; + if (IS_ERR(domain)) { + ret = PTR_ERR(domain); goto out_unlock; } diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f17a1113f3d6a3..899d73062f6e67 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -3543,9 +3543,15 @@ struct iommu_domain *iommu_sva_domain_alloc(struct device *dev, const struct iommu_ops *ops = dev_iommu_ops(dev); struct iommu_domain *domain; - domain = ops->domain_alloc(IOMMU_DOMAIN_SVA); - if (!domain) - return NULL; + if (ops->domain_alloc_sva) { + domain = ops->domain_alloc_sva(dev, mm); + if (IS_ERR(domain)) + return domain; + } else { + domain = ops->domain_alloc(IOMMU_DOMAIN_SVA); + if (!domain) + return ERR_PTR(-ENOMEM); + } domain->type = IOMMU_DOMAIN_SVA; mmgrab(mm); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 6925866ba247b8..87965454d7de57 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -346,6 +346,7 @@ static inline int __iommu_copy_struct_from_user( * Upon failure, ERR_PTR must be returned. * @domain_alloc_paging: Allocate an iommu_domain that can be used for * UNMANAGED, DMA, and DMA_FQ domain types. + * @domain_alloc_sva: Allocate an iommu_domain for Shared Virtual Addressing. * @probe_device: Add device to iommu driver handling * @release_device: Remove device from iommu driver handling * @probe_finalize: Do final setup work after the device is added to an IOMMU @@ -386,6 +387,8 @@ struct iommu_ops { struct device *dev, u32 flags, struct iommu_domain *parent, const struct iommu_user_data *user_data); struct iommu_domain *(*domain_alloc_paging)(struct device *dev); + struct iommu_domain *(*domain_alloc_sva)(struct device *dev, + struct mm_struct *mm); struct iommu_device *(*probe_device)(struct device *dev); void (*release_device)(struct device *dev); From patchwork Wed Nov 1 23:36:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443156 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 4E196C4332F for ; Wed, 1 Nov 2023 23:38:31 +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=WcWjdBEhkeXNyMls2TXPmvkGakkbGFodjExCKPQbW8Y=; b=4VSq+gUi5nnBoP upoEFjOlg3wbbO4pZLGDgxGE9wPOVHpyjPHZVJdGJbooTUw7ayfffueAC0+PQ7mtPYiBv5oFfAkfD to2MFi37D3bght6pgqFvxHQ47A6lpCJfbmOAkmbZ7mUjm0WHGTdxJtX3vssNe1vET7HBynlWsT/B+ JMp+wuA+L5uw+51T9HapPsEIOYkDiW60GBJXSFgesOMX/CZzmMyV+DTyc2JcjG2Hb9tsUNmHB+UmN aNECvJSu3l3YIA461GHH7o3neSgvKSRDWvsNXJrBWoHmdNu/KtppuvMRFewzxtwgNPdU5AkjbBXyL yAXoriHgDJhsisHhNxNA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKmS-008KQp-2G; Wed, 01 Nov 2023 23:37:48 +0000 Received: from mail-bn8nam04on20610.outbound.protection.outlook.com ([2a01:111:f400:7e8d::610] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKm7-008JV7-1r for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:40 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Mh3sERzh7TTljorDFFv8qOlQckvHsvwzVwHxPDlX72mguJqi3qIMT16L/gOJHVaLheIe+vop0IJD4ML9rNEJuHxWTktbKWHDhsHRaW3nyxFnI34Fjn7arvPUPIz0P+KcfNtrY2wrwiltCWbj80MFZPOLZaJERQMUGXgnPawjIn9GJq06vQdeXUis000x7n3ow12xGzfCQ/54YVH0wGLFGs1F1IKEG296/tJfkBe/4S4zAhrGhxr7oRRzGwq23T/wnZ6gf5gIR77sKKtQWtAYjybcFeZtSwt7HltQZV/fzu1BKWmxo2HrcDaFRyPEQjp8R/PG/SZ5FlUmikumC/F/sA== 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=t5R/lXDb/txwwX7B7msB7Jo98lITct0dynjG+6Ae704=; b=Epxbyup4IJ6+lXZr5ZYsRNR9i42skUT3CSeX7+F3a9QTeTO2xiAnUKshMaHgjl2LAUaGj0UM3Yl5XFPdF3eptULnQoIZLKM7pgQ7h6KMwurMnOe+M+kBLnf5NOeRgJHzinY+SDyhCapZeWki4CmqPLaMg2rNK1uqKPg6PkI8ioFcyYZKyO9jHbiHKTxmPOFdVKEcX2VG77mfYR/Gm58p0Ayg6Zi5Ik5060qD9JCrCCQOUJmUhN01RVaqlRI3vvnmTpOD5QtpTsL8+RQY6cmTnCFPcDK5y/STqbNuLn9Cw6MQ+MYfwz/fTWIWtnUG6FsKU8E5DEBHHXyP8B9+nAopdA== 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=t5R/lXDb/txwwX7B7msB7Jo98lITct0dynjG+6Ae704=; b=hwv89pA9C5a6SOqWnwhjXad6b5gXgAlDAuu9Q2tjvN/MCsqxzp93f7Qjil0oCLpvhV3UTc2v3j4y/lsJVNKc/Ih4iTEY/cn/bhgUvvLcrGiS8USl9C3PFvAI7xzYJYEt1zhgs3PVMYUbcVoZe5M5uoiSxRYFGkx4oc5LEr1XxscR2uCbvRfzS3ya2vdmYepCUb5Jm1JFA9niIdbS72q1rbBbwLJ7CFFHHoPCcLIkk1ToYMl7yiqyITS6GDyLDday9WtfZfNSNc0cYfEMVZEmMdIm9BrgTuGh0J5i+vr4gU+biUlFz7oOYvanz2FMjsylAhxrTdIDFxma/eZJ9GoGrA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:50 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:50 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 21/27] iommu/arm-smmu-v3: Put the SVA mmu notifier in the smmu_domain Date: Wed, 1 Nov 2023 20:36:39 -0300 Message-ID: <21-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR19CA0042.namprd19.prod.outlook.com (2603:10b6:208:19b::19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 1fd89712-6381-45a3-52e6-08dbdb336a21 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Vi/8AFvO2hb/uY4E/rhCkrGkRcgtnapnMm6FdBl4mFYPgB836v4BZxfj/bKqbh4xsOZVzroEs2gnOH36LJlvZIDUdIHIJAiMFx5LJRFpvkqxfATXQ6d1XvoHp17UXCfwgWDKcvzmg3NY95s7rw5ayVQfY6Zxi+hJGl3jVYVgjNJ3mRuSqXH/SR8rQQK5XP7v+orRyzcMAZPnGtDc4sswN59AuGZIpkhg3xLhmTIGUBhqhjl8PRR5H2iTxycCetfBEwNw8w1h/vdDZ8x4d2VeMz63ICGaCQTlFTCbCVU9HZPE+LC1m4aVRRoQil4r8LcdBgE/pMviELuLdoA2EQV4OLPH2Xv1te9BUBIKBoKSBkxVGk5glrDkYbnsW6r13JCEHoppoq25uDyOu/tqBAFzDFPb8eQb4/D0upijrLcVYu2T2RH3yBh4eIGwQrr6gGs+gXc/tECYksfz9cgNL/rQJRPZtRpMDOM7TTQCNWEZrcZuNvj/E/mWaiZhBCZxQp0ho6BfRBd2HyRhXwdvYUmGXEhh3q6Z0OYXgEQ1eO2YWy2zbugOiT5sws6jdSJ1VhfY X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(30864003)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Uaomf23sxU2R73uVucFcInrzpj9oolmUsMkKFlZotEUtt6w1mja0Fg9Rb85KOgYnt1RckOQ8njzFVXwlCFOIU0Vdkmhn3AmDIEu0F1abwpsrMhH97B9NFfSj12OwdE3KX+71MrKQv0kNDIHLX/pqNSKwAgFHlCMRsURFMYQ0/T5q/HFzPtXKJVf46BcPN7AoLRKGnpc7Rt2xlrxay9xXTCNX3fA1nAL/JH/0BwOAG1hxzjeg4HEQmpkctCwHkaJJSHJa2AKdhat8mvRn8yOmY7p1f9bUzhOV0Q5cidvckx72AdFgdth1dtbfHiJlk8WJD57Q7+7RTtH48zXbF36bvHszOT4uf6DSeZsbMCAtNiP4CwH6ANM8WG9lOdqAJ7VQoYH3ZnqaFCyKnD2lcoq3JRfSLJwI7vWqOEh9fUlUehPW22bz58A7O2vSqc4JyghLYJibSomPSKKLRHx9mUQ3DLvGT9Tu/8f9shnMTL1IdHaxZaMxUZqiYuODdgtTRE1Ldm1Vw8btePtw8xH419V4nH4KxbVF57GDFpSnJWAoB2mQLreB4efwZOE/VXNOV6NSLOOWN/ElY676ViY83LitG3oRMCz2zyn90Oiq6NnFEDlMAfhFJfLYr2U77y0/CpTC4EsrBl7k2QYzitbGwipGmxWVR6p7naLkYjLPlJ237nvpzSav/+LYBKsfEH8YDHHBOjxaYvzM9TlJl/JUQ7XcZ1M2M5BeR1DitU0eMjfKzLHI6vIjzwHh2IBFptYuvy8RDB/2qVM1z1Hz6SYOJq4MZ8xBNn7l/pW7EusuvvDA5T3xNAe8izLs22a87/CP6BuprGrCaObUd3DBROXv/aq5KBp4DNbIUVZ1AXlcDR1DyUfI1yFoVq2IDxEsgVEv4OZgJE8fvczdA9kcPjop1cv6y2OQwmvg98BVyWtjkpemToJCab3UPU66ZKOYf+V9n5xN5XTBRlIehGzu+sBgC1DWfHogQPsBpBRheM8GE3jslb41QKlJrIdAr5IMKHnk8uGQuBQGM9LWB6pNRQPY2Ula2NLwaw9Y0JzUmAr6C4peqsG2t/6uBo1NPzjFHmHXGoB7gYXQMG2+8agLfXfjqb4IpgGdK2n2qLhYvT5dDlxpnL2RuT5Qvt0aAR95AMHZXe+VtxA0J3Rrswt8U0eKrSznJXLDFfYs95pLY3Yk/OfZQo7yoy9mLQjPt4VnFjmNbJ/co67f96+ZwJfHgemkMcD/WMnQNsBs4otG9ExzA/QvHpi/CQUL2q9pA8HZe53x9MJp+O9iKex+mzLntW+sreJQNw24FctJqnT7PI1VQou4KT1aQHHYDGKdpr+SVGx0bHITIA+fIQnmQKKg16Ttux71r8wMJbwFuCa+W/WLbTZpm0PwbDRol9S7jY/2pGw2vMWvet+szzdZL2qipKVm5QZTaVKIknPJCtjcHvPzUCkxyltfTI2fH7YKvl8eXOP4USiAqad2331DCe8hxMjmVZiTVJheLdBg4CyUn06iFik43mr5KbBXVqzhPofwYQfnu+f4wKIpP4Mijz3y1Wn5XkOpbck8etsgobrZm4slM8metQfuPvnNcwynlQcgXH599gwe X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1fd89712-6381-45a3-52e6-08dbdb336a21 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.4886 (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: 1rGUbESyuVtVbccrAGkTrphEHNg8TeEjGBJ0B/0eVMg7cTBSQnWgvNQwZOpNs15r X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163727_697653_B6FAEAE9 X-CRM114-Status: GOOD ( 21.46 ) 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 This removes all the notifier de-duplication logic in the driver and relies on the core code to de-duplicate and allocate only one SVA domain per mm per smmu instance. This naturally gives a 1:1 relationship between SVA domain and mmu notifier. Remove all of the previous mmu_notifier, bond, shared cd, and cd refcount logic entirely. For the purpose of organizing patches lightly remove BTM support. The next patches will add it back in. BTM is a performance optimization so this is bisection friendly functionally invisible change. The bond/shared_cd/btm/asid allocator are tightly wound together and changing them all at once would make this patch too big. The core issue is that having a single SVA domain per-smmu instance conflicts with the design of having a global ASID table that BTM currently needs, as we would end up having to assign multiple SVA domains to the same ASID. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 384 ++++-------------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 80 +--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 14 +- 3 files changed, 100 insertions(+), 378 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 991daffbee31aa..a3b85aa5e48ce6 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 @@ -13,29 +13,9 @@ #include "../../iommu-sva.h" #include "../../io-pgtable-arm.h" -struct arm_smmu_mmu_notifier { - struct mmu_notifier mn; - struct arm_smmu_ctx_desc *cd; - bool cleared; - refcount_t refs; - struct list_head list; - struct arm_smmu_domain *domain; -}; - -#define mn_to_smmu(mn) container_of(mn, struct arm_smmu_mmu_notifier, mn) - -struct arm_smmu_bond { - struct mm_struct *mm; - struct arm_smmu_mmu_notifier *smmu_mn; - struct list_head list; -}; - -#define sva_to_bond(handle) \ - container_of(handle, struct arm_smmu_bond, sva) - static DEFINE_MUTEX(sva_lock); -static void +static void __maybe_unused arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) { struct arm_smmu_master_domain *master_domain; @@ -58,58 +38,6 @@ arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); } -/* - * Check if the CPU ASID is available on the SMMU side. If a private context - * descriptor is using it, try to replace it. - */ -static struct arm_smmu_ctx_desc * -arm_smmu_share_asid(struct mm_struct *mm, u16 asid) -{ - int ret; - u32 new_asid; - struct arm_smmu_ctx_desc *cd; - struct arm_smmu_device *smmu; - struct arm_smmu_domain *smmu_domain; - - cd = xa_load(&arm_smmu_asid_xa, asid); - if (!cd) - return NULL; - - if (cd->mm) { - if (WARN_ON(cd->mm != mm)) - return ERR_PTR(-EINVAL); - /* All devices bound to this mm use the same cd struct. */ - refcount_inc(&cd->refs); - return cd; - } - - smmu_domain = container_of(cd, struct arm_smmu_domain, cd); - smmu = smmu_domain->smmu; - - ret = xa_alloc(&arm_smmu_asid_xa, &new_asid, cd, - XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); - if (ret) - return ERR_PTR(-ENOSPC); - /* - * Race with unmap: TLB invalidations will start targeting the new ASID, - * which isn't assigned yet. We'll do an invalidate-all on the old ASID - * later, so it doesn't matter. - */ - cd->asid = new_asid; - /* - * Update ASID and invalidate CD in all associated masters. There will - * be some overlap between use of both ASIDs, until we invalidate the - * TLB. - */ - arm_smmu_update_s1_domain_cd_entry(smmu_domain); - - /* Invalidate TLB entries previously associated with that context */ - arm_smmu_tlb_inv_asid(smmu, asid); - - xa_erase(&arm_smmu_asid_xa, asid); - return NULL; -} - static u64 page_size_to_cd(void) { static_assert(PAGE_SIZE == SZ_4K || PAGE_SIZE == SZ_16K || @@ -123,7 +51,8 @@ static u64 page_size_to_cd(void) static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, struct arm_smmu_master *master, - struct mm_struct *mm, u16 asid) + struct mm_struct *mm, u16 asid, + bool btm_invalidation) { u64 par; @@ -144,7 +73,7 @@ static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, (master->stall_enabled ? CTXDESC_CD_0_S : 0) | CTXDESC_CD_0_R | CTXDESC_CD_0_A | - CTXDESC_CD_0_ASET | + (btm_invalidation ? 0 : CTXDESC_CD_0_ASET) | FIELD_PREP(CTXDESC_CD_0_ASID, asid)); /* @@ -176,69 +105,6 @@ static void arm_smmu_make_sva_cd(struct arm_smmu_cd *target, target->data[3] = cpu_to_le64(read_sysreg(mair_el1)); } -static struct arm_smmu_ctx_desc *arm_smmu_alloc_shared_cd(struct mm_struct *mm) -{ - u16 asid; - int err = 0; - struct arm_smmu_ctx_desc *cd; - struct arm_smmu_ctx_desc *ret = NULL; - - /* Don't free the mm until we release the ASID */ - mmgrab(mm); - - asid = arm64_mm_context_get(mm); - if (!asid) { - err = -ESRCH; - goto out_drop_mm; - } - - cd = kzalloc(sizeof(*cd), GFP_KERNEL); - if (!cd) { - err = -ENOMEM; - goto out_put_context; - } - - refcount_set(&cd->refs, 1); - - mutex_lock(&arm_smmu_asid_lock); - ret = arm_smmu_share_asid(mm, asid); - if (ret) { - mutex_unlock(&arm_smmu_asid_lock); - goto out_free_cd; - } - - err = xa_insert(&arm_smmu_asid_xa, asid, cd, GFP_KERNEL); - mutex_unlock(&arm_smmu_asid_lock); - - if (err) - goto out_free_asid; - - cd->asid = asid; - cd->mm = mm; - - return cd; - -out_free_asid: - arm_smmu_free_asid(cd); -out_free_cd: - kfree(cd); -out_put_context: - arm64_mm_context_put(mm); -out_drop_mm: - mmdrop(mm); - return err < 0 ? ERR_PTR(err) : ret; -} - -static void arm_smmu_free_shared_cd(struct arm_smmu_ctx_desc *cd) -{ - if (arm_smmu_free_asid(cd)) { - /* Unpin ASID */ - arm64_mm_context_put(cd->mm); - mmdrop(cd->mm); - kfree(cd); - } -} - /* * Cloned from the MAX_TLBI_OPS in arch/arm64/include/asm/tlbflush.h, this * is used as a threshold to replace per-page TLBI commands to issue in the @@ -253,8 +119,8 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, unsigned long start, unsigned long end) { - struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn); - struct arm_smmu_domain *smmu_domain = smmu_mn->domain; + struct arm_smmu_domain *smmu_domain = + container_of(mn, struct arm_smmu_domain, mmu_notifier); size_t size; /* @@ -271,33 +137,27 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, size = 0; } - if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_BTM)) { + if (smmu_domain->btm_invalidation) { if (!size) arm_smmu_tlb_inv_asid(smmu_domain->smmu, - smmu_mn->cd->asid); + smmu_domain->cd.asid); else arm_smmu_tlb_inv_range_asid(start, size, - smmu_mn->cd->asid, + smmu_domain->cd.asid, PAGE_SIZE, false, smmu_domain); } - arm_smmu_atc_inv_domain_sva(smmu_domain, mm->pasid, start, size); + arm_smmu_atc_inv_domain(smmu_domain, start, size); } static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) { - struct arm_smmu_mmu_notifier *smmu_mn = mn_to_smmu(mn); - struct arm_smmu_domain *smmu_domain = smmu_mn->domain; + struct arm_smmu_domain *smmu_domain = + container_of(mn, struct arm_smmu_domain, mmu_notifier); struct arm_smmu_master_domain *master_domain; unsigned long flags; - mutex_lock(&sva_lock); - if (smmu_mn->cleared) { - mutex_unlock(&sva_lock); - return; - } - /* * DMA may still be running. Keep the cd valid to avoid C_BAD_CD events, * but disable translation. @@ -309,24 +169,26 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) struct arm_smmu_cd target; struct arm_smmu_cd *cdptr; - cdptr = arm_smmu_get_cd_ptr(master, mm->pasid); + cdptr = arm_smmu_get_cd_ptr(master, master_domain->ssid); if (WARN_ON(!cdptr)) continue; - arm_smmu_make_sva_cd(&target, master, NULL, smmu_mn->cd->asid); - arm_smmu_write_cd_entry(master, mm->pasid, cdptr, &target); + arm_smmu_make_sva_cd(&target, master, NULL, + smmu_domain->cd.asid, + smmu_domain->btm_invalidation); + arm_smmu_write_cd_entry(master, master_domain->ssid, cdptr, + &target); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); - arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_mn->cd->asid); - arm_smmu_atc_inv_domain_sva(smmu_domain, mm->pasid, 0, 0); - - smmu_mn->cleared = true; - mutex_unlock(&sva_lock); + arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); } static void arm_smmu_mmu_notifier_free(struct mmu_notifier *mn) { - kfree(mn_to_smmu(mn)); + struct arm_smmu_domain *smmu_domain = + container_of(mn, struct arm_smmu_domain, mmu_notifier); + + kfree(smmu_domain); } static const struct mmu_notifier_ops arm_smmu_mmu_notifier_ops = { @@ -335,109 +197,6 @@ static const struct mmu_notifier_ops arm_smmu_mmu_notifier_ops = { .free_notifier = arm_smmu_mmu_notifier_free, }; -/* Allocate or get existing MMU notifier for this {domain, mm} pair */ -static struct arm_smmu_mmu_notifier * -arm_smmu_mmu_notifier_get(struct arm_smmu_domain *smmu_domain, - struct mm_struct *mm) -{ - int ret; - struct arm_smmu_ctx_desc *cd; - struct arm_smmu_mmu_notifier *smmu_mn; - - list_for_each_entry(smmu_mn, &smmu_domain->mmu_notifiers, list) { - if (smmu_mn->mn.mm == mm) { - refcount_inc(&smmu_mn->refs); - return smmu_mn; - } - } - - cd = arm_smmu_alloc_shared_cd(mm); - if (IS_ERR(cd)) - return ERR_CAST(cd); - - smmu_mn = kzalloc(sizeof(*smmu_mn), GFP_KERNEL); - if (!smmu_mn) { - ret = -ENOMEM; - goto err_free_cd; - } - - refcount_set(&smmu_mn->refs, 1); - smmu_mn->cd = cd; - smmu_mn->domain = smmu_domain; - smmu_mn->mn.ops = &arm_smmu_mmu_notifier_ops; - - ret = mmu_notifier_register(&smmu_mn->mn, mm); - if (ret) { - kfree(smmu_mn); - goto err_free_cd; - } - - list_add(&smmu_mn->list, &smmu_domain->mmu_notifiers); - return smmu_mn; - -err_free_cd: - arm_smmu_free_shared_cd(cd); - return ERR_PTR(ret); -} - -static void arm_smmu_mmu_notifier_put(struct arm_smmu_mmu_notifier *smmu_mn) -{ - struct mm_struct *mm = smmu_mn->mn.mm; - struct arm_smmu_ctx_desc *cd = smmu_mn->cd; - struct arm_smmu_domain *smmu_domain = smmu_mn->domain; - - if (!refcount_dec_and_test(&smmu_mn->refs)) - return; - - list_del(&smmu_mn->list); - - /* - * If we went through clear(), we've already invalidated, and no - * new TLB entry can have been formed. - */ - if (!smmu_mn->cleared) { - arm_smmu_tlb_inv_asid(smmu_domain->smmu, cd->asid); - arm_smmu_atc_inv_domain_sva(smmu_domain, mm->pasid, 0, 0); - } - - /* Frees smmu_mn */ - mmu_notifier_put(&smmu_mn->mn); - arm_smmu_free_shared_cd(cd); -} - -static int __arm_smmu_sva_bind(struct device *dev, struct mm_struct *mm, - struct arm_smmu_cd *target) -{ - int ret; - struct arm_smmu_bond *bond; - struct arm_smmu_master *master = dev_iommu_priv_get(dev); - struct arm_smmu_domain *smmu_domain = - to_smmu_domain_safe(iommu_get_domain_for_dev(dev)); - - if (!smmu_domain || smmu_domain->stage != ARM_SMMU_DOMAIN_S1) - return -ENODEV; - - bond = kzalloc(sizeof(*bond), GFP_KERNEL); - if (!bond) - return -ENOMEM; - - bond->mm = mm; - - bond->smmu_mn = arm_smmu_mmu_notifier_get(smmu_domain, mm); - if (IS_ERR(bond->smmu_mn)) { - ret = PTR_ERR(bond->smmu_mn); - goto err_free_bond; - } - - list_add(&bond->list, &master->bonds); - arm_smmu_make_sva_cd(target, master, mm, bond->smmu_mn->cd->asid); - return 0; - -err_free_bond: - kfree(bond); - return ret; -} - bool arm_smmu_sva_supported(struct arm_smmu_device *smmu) { unsigned long reg, fld; @@ -565,11 +324,6 @@ int arm_smmu_master_enable_sva(struct arm_smmu_master *master) int arm_smmu_master_disable_sva(struct arm_smmu_master *master) { mutex_lock(&sva_lock); - if (!list_empty(&master->bonds)) { - dev_err(master->dev, "cannot disable SVA, device is bound\n"); - mutex_unlock(&sva_lock); - return -EBUSY; - } arm_smmu_master_sva_disable_iopf(master); master->sva_enabled = false; mutex_unlock(&sva_lock); @@ -586,59 +340,54 @@ void arm_smmu_sva_notifier_synchronize(void) mmu_notifier_synchronize(); } -void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t id) -{ - struct mm_struct *mm = domain->mm; - 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); - list_for_each_entry(t, &master->bonds, list) { - if (t->mm == mm) { - bond = t; - break; - } - } - - if (!WARN_ON(!bond)) { - list_del(&bond->list); - arm_smmu_mmu_notifier_put(bond->smmu_mn); - kfree(bond); - } - mutex_unlock(&sva_lock); -} - static int arm_smmu_sva_set_dev_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t id) { + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); struct arm_smmu_master *master = dev_iommu_priv_get(dev); - int ret = 0; - struct mm_struct *mm = domain->mm; struct arm_smmu_cd target; + int ret; - if (mm->pasid != id || !master->cd_table.used_sid) + /* Prevent arm_smmu_mm_release from being called while we are attaching */ + if (!mmget_not_zero(domain->mm)) return -EINVAL; - if (!arm_smmu_get_cd_ptr(master, id)) - return -ENOMEM; + /* + * This does not need the arm_smmu_asid_lock because SVA domains never + * get reassigned + */ + arm_smmu_make_sva_cd(&target, master, smmu_domain->domain.mm, + smmu_domain->cd.asid, + smmu_domain->btm_invalidation); - mutex_lock(&sva_lock); - ret = __arm_smmu_sva_bind(dev, mm, &target); - mutex_unlock(&sva_lock); - if (ret) - return ret; + ret = arm_smmu_set_pasid(master, to_smmu_domain(domain), id, &target); - /* This cannot fail since we preallocated the cdptr */ - arm_smmu_set_pasid(master, to_smmu_domain(domain), id, &target); - return 0; + mmput(domain->mm); + return ret; } static void arm_smmu_sva_domain_free(struct iommu_domain *domain) { - kfree(to_smmu_domain(domain)); + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + + /* + * Ensure the ASID is empty in the iommu cache before allowing reuse. + */ + arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); + + /* + * Notice that the arm_smmu_mm_arch_invalidate_secondary_tlbs op can + * still be called/running at this point. We allow the ASID to be + * reused, and if there is a race then it just suffers harmless + * unnecessary invalidation. + */ + xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); + + /* + * Actual free is defered to the SRCU callback + * arm_smmu_mmu_notifier_free() + */ + mmu_notifier_put(&smmu_domain->mmu_notifier); } static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { @@ -652,6 +401,8 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_domain *smmu_domain; + u32 asid; + int ret; smmu_domain = arm_smmu_domain_alloc(); if (!smmu_domain) @@ -661,5 +412,22 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; smmu_domain->smmu = smmu; + ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, + XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); + if (ret) + goto err_free; + + smmu_domain->cd.asid = asid; + smmu_domain->mmu_notifier.ops = &arm_smmu_mmu_notifier_ops; + ret = mmu_notifier_register(&smmu_domain->mmu_notifier, mm); + if (ret) + goto err_asid; + return &smmu_domain->domain; + +err_asid: + xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); +err_free: + kfree(smmu_domain); + return ERR_PTR(ret); } 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 85fc3064675931..c221ab138ebb87 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1339,22 +1339,6 @@ static void arm_smmu_free_cd_tables(struct arm_smmu_master *master) cd_table->cdtab = NULL; } -bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd) -{ - bool free; - struct arm_smmu_ctx_desc *old_cd; - - if (!cd->asid) - return false; - - free = refcount_dec_and_test(&cd->refs); - if (free) { - old_cd = xa_erase(&arm_smmu_asid_xa, cd->asid); - WARN_ON(old_cd != cd); - } - return free; -} - /* Stream table manipulation functions */ static void arm_smmu_write_strtab_l1_desc(__le64 *dst, struct arm_smmu_strtab_l1_desc *desc) @@ -1980,8 +1964,8 @@ static int arm_smmu_atc_inv_master(struct arm_smmu_master *master, return arm_smmu_cmdq_batch_submit(master->smmu, &cmds); } -static int __arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, - ioasid_t ssid, unsigned long iova, size_t size) +int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, + unsigned long iova, size_t size) { struct arm_smmu_master_domain *master_domain; int i; @@ -2019,15 +2003,7 @@ static int __arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, if (!master->ats_enabled) continue; - /* - * Non-zero ssid means SVA is co-opting the S1 domain to issue - * invalidations for SVA PASIDs. - */ - if (ssid != IOMMU_NO_PASID) - arm_smmu_atc_inv_to_cmd(ssid, iova, size, &cmd); - else - arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, - &cmd); + arm_smmu_atc_inv_to_cmd(master_domain->ssid, iova, size, &cmd); for (i = 0; i < master->num_streams; i++) { cmd.atc.sid = master->streams[i].id; @@ -2039,19 +2015,6 @@ static int __arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, return arm_smmu_cmdq_batch_submit(smmu_domain->smmu, &cmds); } -static int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, - unsigned long iova, size_t size) -{ - return __arm_smmu_atc_inv_domain(smmu_domain, IOMMU_NO_PASID, iova, - size); -} - -int arm_smmu_atc_inv_domain_sva(struct arm_smmu_domain *smmu_domain, - ioasid_t ssid, unsigned long iova, size_t size) -{ - return __arm_smmu_atc_inv_domain(smmu_domain, ssid, iova, size); -} - /* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context(void *cookie) { @@ -2240,7 +2203,6 @@ struct arm_smmu_domain *arm_smmu_domain_alloc(void) mutex_init(&smmu_domain->init_mutex); INIT_LIST_HEAD(&smmu_domain->devices); spin_lock_init(&smmu_domain->devices_lock); - INIT_LIST_HEAD(&smmu_domain->mmu_notifiers); return smmu_domain; } @@ -2281,7 +2243,7 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { /* Prevent SVA from touching the CD while we're freeing it */ mutex_lock(&arm_smmu_asid_lock); - arm_smmu_free_asid(&smmu_domain->cd); + xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); mutex_unlock(&arm_smmu_asid_lock); } else { struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; @@ -2299,11 +2261,9 @@ static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, u32 asid; struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; - refcount_set(&cd->refs, 1); - /* Prevent SVA from modifying the ASID until it is written to the CD */ mutex_lock(&arm_smmu_asid_lock); - ret = xa_alloc(&arm_smmu_asid_xa, &asid, cd, + ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); cd->asid = (u16)asid; mutex_unlock(&arm_smmu_asid_lock); @@ -2715,7 +2675,10 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, struct attach_state state; int ret; - if (!sid_smmu_domain || sid_smmu_domain->stage != ARM_SMMU_DOMAIN_S1) + if (smmu_domain->smmu != master->smmu) + return -EINVAL; + + if (!sid_smmu_domain || !master->cd_table.used_sid) return -ENODEV; cdptr = arm_smmu_get_cd_ptr(master, pasid); @@ -2736,9 +2699,18 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, return 0; } -void arm_smmu_remove_pasid(struct arm_smmu_master *master, - struct arm_smmu_domain *smmu_domain, ioasid_t pasid) +static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) { + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_domain *smmu_domain; + struct iommu_domain *domain; + + domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA); + if (WARN_ON(IS_ERR(domain)) || !domain) + return; + + smmu_domain = to_smmu_domain(domain); + mutex_lock(&arm_smmu_asid_lock); arm_smmu_clear_cd(master, pasid); if (master->ats_enabled) @@ -3032,7 +3004,6 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) master->dev = dev; master->smmu = smmu; - INIT_LIST_HEAD(&master->bonds); dev_iommu_priv_set(dev, master); ret = arm_smmu_insert_master(smmu, master); @@ -3214,17 +3185,6 @@ static int arm_smmu_def_domain_type(struct device *dev) return 0; } -static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) -{ - struct iommu_domain *domain; - - domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA); - if (WARN_ON(IS_ERR(domain)) || !domain) - return; - - arm_smmu_sva_remove_dev_pasid(domain, dev, pasid); -} - static struct iommu_ops arm_smmu_ops = { .identity_domain = &arm_smmu_identity_domain, .blocked_domain = &arm_smmu_blocked_domain, 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 48871c8ee8c88c..a229ad0adf6a49 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -587,9 +587,6 @@ struct arm_smmu_strtab_l1_desc { struct arm_smmu_ctx_desc { u16 asid; - - refcount_t refs; - struct mm_struct *mm; }; struct arm_smmu_l1_ctx_desc { @@ -713,7 +710,6 @@ struct arm_smmu_master { bool stall_enabled; bool sva_enabled; bool iopf_enabled; - struct list_head bonds; unsigned int ssid_bits; }; @@ -742,7 +738,8 @@ struct arm_smmu_domain { struct list_head devices; spinlock_t devices_lock; - struct list_head mmu_notifiers; + struct mmu_notifier mmu_notifier; + bool btm_invalidation; }; struct arm_smmu_master_domain { @@ -796,9 +793,8 @@ 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, struct arm_smmu_domain *smmu_domain); -bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd); -int arm_smmu_atc_inv_domain_sva(struct arm_smmu_domain *smmu_domain, - ioasid_t ssid, unsigned long iova, size_t size); +int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain, + unsigned long iova, size_t size); #ifdef CONFIG_ARM_SMMU_V3_SVA bool arm_smmu_sva_supported(struct arm_smmu_device *smmu); @@ -810,8 +806,6 @@ bool arm_smmu_master_iopf_supported(struct arm_smmu_master *master); void arm_smmu_sva_notifier_synchronize(void); struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, struct mm_struct *mm); -void arm_smmu_sva_remove_dev_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t id); #else /* CONFIG_ARM_SMMU_V3_SVA */ static inline bool arm_smmu_sva_supported(struct arm_smmu_device *smmu) { From patchwork Wed Nov 1 23:36:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443207 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 45DC4C0018A for ; Thu, 2 Nov 2023 00:45:48 +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=m9gup8jOQV7M2mH9a1UfoD7in6YVkGte4QN6cyhvr2A=; b=MTIHbd912uEH5f 2mpCfu4ppmi2DriJImCCTcMFUXM6IkAsNPjpLjvC+Dhn0u+961Ee/f6DUNwqj8lIzyy/ErWefakIc /qbTGqMhmhH2kZBRgfgRcZ5WkLkT0ibF3HUnR2c995ENyVXm4bHTOHVLIy9NslS5h87VDIuxsMFC2 U3g824FdUBiXerPJf8ocLEfV0uN82Gr8fg6Er7u5OzmpHgf6Zdw/v8rC4KrkzWYRNx23qxP8wF2MD 8YPc1Lo6mr47cz/VzgIV8vb4+FE1exvbV9yrL0780iNOX/1gsvE1ysOUmCp5eVEBKfQmBbKz2Prn3 K4vqxC2/dfwqcmo/W1Pw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq6-008Pyr-1t; Thu, 02 Nov 2023 00:45:38 +0000 Received: from mail-bn8nam04on2061d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::61d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKls-008JWb-2R for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eDyTu0FA/6YWhhgcz2L0PhZdEI6nhtqg9ZSWRmI4TAU6tdwXq46wvlYNJW6PM5xZwcXY0HMBYWCFUdjChCdmg6CawuPGjVORK0oksJwbP8dIp7S8L6hkYDJChcEb+nduUFjv9JXV20cHbOzhS+8jco8tpL1zBnyBhJ1JK/HwDPxBvaX03533YweA/MTNrBuK/+0qhsjKVM7xq+Go7qKMsq/B6rnlBTOvXZMSTMbAcBhD7j54LXMfQz7gNPj7O7nR8v8fITPy9pVm4LAjMafhzNmgaAmL9prP5YsVIvjTg39AAlYVdsM6ogwEt5S7NYRalbqqiMhxgoogZWED/h4SMg== 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=D/nZPUhNVxOcPpYbLqvm+70v5pj1odoJ3S3tIdNDHI4=; b=Vk+cPV++jK47/tSX/tcKmIB6pyUzk+iEcZVv16pLYucm76h2Z/iaqApum216Wf+Blcf07ta7Lw3SigjDLBnUxKsUaUY0Z+vx8d2UtKbL5WY5b8ZbN5o2cLZIgWpnDfyhksJjY4Y/Qt4vnWdzE3ArwjxGEYmzil8o8ljVLMRXxVdieJNkg5T2GWGUvF6dcSNtMZCEOW97CgY7ZneMfuFpC9ZQ91STFg/YpdeLco2M5a6uAz4rRMiJC9eVjyPDd6sf2f/ubp4IRKgfOI+d1NWV//y4iX66nydsC4Ys1vPRsITetBBsVtfDspwdBE7JTFr2UL8VVDatHNTxW2t0zcNhkA== 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=D/nZPUhNVxOcPpYbLqvm+70v5pj1odoJ3S3tIdNDHI4=; b=UxGwds3mHmyF564Oobm6UFX/2vzc7S0jQJHjUkAZDFDP561Oe+ijahpgpfE+QhTq/Z7XuIlfvMGFwmbdYbBYCDfM5mLh9gUUiFQPkyFrV1CDtlYAMnqebm0kUK0fcQ0BFJcpcChGIYeNG7vVy4hxfyrFQexI1RKpT91ss2K4+lZOfKjuQm3UJK1RCLz8Mh58GLqUKnBUHnulT0ihqv1eMsZyC94r4Ys8FSAEljFUVejl/DIagy9jjphyJoMjPM64yGIar7ai6WEcrXP6ed7MVWZ9m4MP/wJmUtRIPaG+x/U5TlGF1OU6dRv6dL9V1L9i0HDWyofBMatzmUnikW4nkg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:50 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:50 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 22/27] iommu/arm-smmu-v3: Consolidate freeing the ASID/VMID Date: Wed, 1 Nov 2023 20:36:40 -0300 Message-ID: <22-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR19CA0052.namprd19.prod.outlook.com (2603:10b6:208:19b::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 2022dc1a-c9fe-4cb1-402c-08dbdb336a0e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ui1N5+7sCkW9kVuNI09AA15N9tL4j2hnDRg+lhnug3EHFf6vyX5TawWcSTMl50vP0UuB4ya5NqAEaaeh/SQrsfP7JLsZgNbOi834uWgmXyxNKXQlwHiMEZkqKEzdOw4H+2B0xBckDBn+jFy2kNq4grJHkZxPv7nLXtLVkgzxaIyx0HNR8fwvfdFXc0CdWeJIM79EVyEayMrU6IYUF814+2iNigjsBB4WCUx+TBQIg32X6uzbNlNHeFzxLKk9Dip3XYgCyhmU0uD7lSRzWf5yh1vQStiWN6c4yD9FN44B3bNq7cq+gTLdH6VSFqfbLrfYpTClYgtf98MilHbe66HT/5Qw3WvTg9z71nYf5+y9XZZtmidBb6dI0AJM39Zjy7nUz5/I+kyMqh/SFUK/VoocqkJ3CWeT8OQWWvSRRzOxQBt/0TgG805VQAQhpTw9lgC1G8ddLAWX3EuZ6/oabB+sHUgQvDqsFDZdu6MUqv/9JlIBDsHwAz7x1+OX5qIIUFQ4pQs2QTn+FOHNUL9JnCOUXBA/98Z6OHyyDMf+L0zfpJOYyiRdYZKxLmHCTLGhaNvz X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SKW0e0TXwd65lSadLDnbECK2zc1mKO3gMt46uIoeZPKLk4k6WP6fnm7LbI/Wc9ghJZa/JHg/Oqux9N/OFsfeXtKEMrF7oOad1usazj7d46LX+6X9YohiNCnKZh07dmkLutIFalhAa/mWGy2hoe0oG6I8JWQlfxxWuP50hA8xkmPxRUobH6WM6nHcoc5v3MbWhSN39TGT4xSn7+7Y2mbQCk/ccjOW4sGVIcmCi2Kd5EDeaPBz0mNWNlWj23+mBva5ZDpaSgYBgW5yx8NMjd38BzMEJo7jzlM7FweGMzE6htbVphZSaTeGXs7GO/KHIp3Njur4nRFu1qlp1gCM3Z951aOYqJ/tLC+VN8hnvyWiVEhfA92v4T/huciStJbkeSW8ioBecRq0pTGut2lk0C6WXZtmWGvGNgA4aSU8xelzQl98Yxx1wtnp5guPfswRH7yWG1Ev6k4J29yGIiUFGSzdFu30btouHGmgw2kBwKW5lNCr9bC9AH1SqfkAoKU8+U8WNzCKYI01hCwISzlEst/Pa+fhoYq0NMMRbkWKEE1XAoeXeeT1DdjTWqrP9NOlJq4Dyj9rK0SenguaNI2c7fqZ/f87+BsxKd9ZGFSSWMZMCPChP0dQ1UwzSe/ByMwuSSQXDrofC3GDzw7YyrcEvo5a8f3yLtTlvdRWun+SdV0azUZt4tqVzvU7QSfnuGgQX+FsHMwGKNOux6hrCI/y+vhb020cV0bYs5q22yzDTROJ9WqKKP1HqsmKGvojsX7WyDk7WifKDMJDWLyL6fCq9a1xT78Sc6iWV4X9G3JxVC05tnPC0iUGPLfIq/Zsdeo8J9o5MaW6GCchWc7iR5toypchy7NpjmOZ5gejSlXNRE4L3C+rkbYwNwAEfIrfUCJ+OMBtkv7vt1oCam/4GjZuDcl6YUAnA3GUbvU8vQzrq0lkZFNziiU5+7pfthOLCQZJFd+qgU+eTE/yxKLav3agNan+fj/YDelndyOLLw8nSmP0yuWokeO+vOIZu3rbAV5vnF090UsxJ9BXg8S5Q2nNXvLljtVp/W5ojmxqKd4cZzNyWlu9T1SRe6mQzIKcKx4BaADhPaMOXDmqDiN3rI6bkgfcX2zkGHRZipkWsSeXinBTapOxHw48cgwhSiSWk2arUzC4po3bVhptxrlJr15CaArgyW3lsd6O3nI+uTnnBB6qw9k1YszFt31tt450abwFWX5TPsrrQWOvHj56Yq63T2ol2j7ZjAA6Wqi0u02FnuZA84WbVf9xRR35YF9hOyPUUtzESMNzP4n0qkhztU1WT22OnWj1rEU74euFisu3rvHbQaMtkmb48s37JTAGNM4q0qHnKNeXU4Uiy9n7Dkiay02ECkfsj6B7xQvIn08o+/f/NgobggihPxthEKDchgGU19mmr9scWhvQapWbUXDzujHc2oPMIrH/GdcKKIB7d5VztUjllcw+9nCa+DF5FUNwgLH2oSVFwXkhmnmPcKxcaYPXuf/sCCETOt8xRhAP17hyzj5dumQz3TotCg6DXkHCYRvM2TYHxOdsPzwBGeZrsnCeg80Y5gN5q1Ndh9RBfVFvEYB6kjdgp3Y5xJuCcrlZ/IMD X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2022dc1a-c9fe-4cb1-402c-08dbdb336a0e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.3179 (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: fCMYpz9jOHX2uB5H5SOxxyeyUdoDnFKTIQdf5QrsAhXaSrooQ/Y+ne2m/zsY0vof X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163712_826447_8EC17EA2 X-CRM114-Status: GOOD ( 20.80 ) 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 The SMMUv3 IOTLB is tagged with a VMID/ASID cache tag. Any time the underlying translation is changed these need to be invalidated. At boot time the IOTLB starts out empty and all cache tags are available for allocation. When a tag is taken out of the allocator the code assumes the IOTLB doesn't reference it, and immediately programs it into a STE/CD. If the cache is referencing the tag then it will have stale data and IOMMU will become incoherent. Thus, whenever an ASID/VMID is freed back to the allocator we need to know that the IOTLB doesn't have any references to it. The SVA code correctly had an invalidation here, but the paging code does not. Consolidate freeing the VMID/ASID to one place and consistently flush both ID types before returning to their allocators. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 9 ++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 36 +++++++++++++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 1 + 3 files changed, 29 insertions(+), 17 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 a3b85aa5e48ce6..66de6cb62f9387 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 @@ -370,18 +370,13 @@ static void arm_smmu_sva_domain_free(struct iommu_domain *domain) { struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); - /* - * Ensure the ASID is empty in the iommu cache before allowing reuse. - */ - arm_smmu_tlb_inv_asid(smmu_domain->smmu, smmu_domain->cd.asid); - /* * Notice that the arm_smmu_mm_arch_invalidate_secondary_tlbs op can * still be called/running at this point. We allow the ASID to be * reused, and if there is a race then it just suffers harmless * unnecessary invalidation. */ - xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); + arm_smmu_domain_free_id(smmu_domain); /* * Actual free is defered to the SRCU callback @@ -426,7 +421,7 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, return &smmu_domain->domain; err_asid: - xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); + arm_smmu_domain_free_id(smmu_domain); err_free: kfree(smmu_domain); return ERR_PTR(ret); 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 c221ab138ebb87..7fd376a4e1752a 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2232,25 +2232,41 @@ static struct iommu_domain *arm_smmu_domain_alloc_paging(struct device *dev) return &smmu_domain->domain; } -static void arm_smmu_domain_free(struct iommu_domain *domain) +/* + * Return the domain's ASID or VMID back to the allocator. All IDs in the + * allocator do not have an IOTLB entries referencing them. + */ +void arm_smmu_domain_free_id(struct arm_smmu_domain *smmu_domain) { - struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); struct arm_smmu_device *smmu = smmu_domain->smmu; - free_io_pgtable_ops(smmu_domain->pgtbl_ops); + if ((smmu_domain->stage == ARM_SMMU_DOMAIN_S1 || + smmu_domain->domain.type == IOMMU_DOMAIN_SVA) && + smmu_domain->cd.asid) { + arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); - /* Free the ASID or VMID */ - if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { /* Prevent SVA from touching the CD while we're freeing it */ mutex_lock(&arm_smmu_asid_lock); xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); mutex_unlock(&arm_smmu_asid_lock); - } else { - struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; - if (cfg->vmid) - ida_free(&smmu->vmid_map, cfg->vmid); - } + } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2 && + smmu_domain->s2_cfg.vmid) { + struct arm_smmu_cmdq_ent cmd = { + .opcode = CMDQ_OP_TLBI_S12_VMALL, + .tlbi.vmid = smmu_domain->s2_cfg.vmid + }; + arm_smmu_cmdq_issue_cmd_with_sync(smmu, &cmd); + ida_free(&smmu->vmid_map, smmu_domain->s2_cfg.vmid); + } +} + +static void arm_smmu_domain_free(struct iommu_domain *domain) +{ + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + + free_io_pgtable_ops(smmu_domain->pgtbl_ops); + arm_smmu_domain_free_id(smmu_domain); kfree(smmu_domain); } 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 a229ad0adf6a49..c7c4f4fda31297 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -789,6 +789,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, void arm_smmu_remove_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid); +void arm_smmu_domain_free_id(struct arm_smmu_domain *smmu_domain); 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, From patchwork Wed Nov 1 23:36:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443153 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 96EA2C4332F for ; Wed, 1 Nov 2023 23:38:08 +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=Mras+JcBu5zSiHBM1qkhYHM7RTmiwF87MJ/+tiYlGxc=; b=H5/0eJ/t2T0WRQ kycapk9jBlY/Q1/noFCVeNu6L2Gqre9KMaZ1EF+vhp4OL3X89CMYqxHqg/OKTuGl1Pz5LnJteYajx +wz3Ap1V+ToWEaxtykBiY0yEgjssfR5LJA5VqSqndunc9dVJe5MLwuqP+futAIqJJk9GHypkFuSzi UKm5Pn620IN1EjkahUswj0FhTD2zDDu+J46t+VLWOhcCWjrN+4DojiV6kVLo6PvWNsP4a5noD27/r OrgIUkwO2k7MuiiHdUEwBifsrTOL+QTV6P+5grpP3k3A+uJZjIG27MHq0erx3DjTs7yVUiJh6Qo+0 zfedmFTElaHUqDnI8gmA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKmC-008K8D-0w; Wed, 01 Nov 2023 23:37:32 +0000 Received: from mail-bn8nam04on20612.outbound.protection.outlook.com ([2a01:111:f400:7e8d::612] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlw-008JbE-1D for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TqYrNoTdByrS2aS2UhYeoXzx+xsrbt1pcDmirUdbIpaNAmKWJ/AbB5fYVuxZC9wZi1FYBrGhKoZzmhrmk+H3NsIqH206c20boE3DfCJG7FVgdyQxLog6Sg7XqdhHr9u88quCwJnJ1W3/uD7TbUmaj77lnbvHR4mKq1s5y3QYFEf2LIEFKjm2mY/i9niluVYG9Vb6ipoDPLFwFXoTGmRy3c9/mV6ZA1JwBm8hL9xJYbAUDQwTqBZSn1Y4NkMYNYbAPVIOPuwDk9VsIyihLJ+FGGXBDBFMybDH4N2lxs8atLJ2kNrAvYWvGl0g8b5V3niI9YVTmoYE6Io9ElizLrSrrw== 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=VULoKtsWLd7QUp6F6ejtKW0CvfQbcOavuVCT/vzNU70=; b=av7+z1pI3uPrfWIkAQTGo2ho4viDRpfNEC1wWPxBT6wotIpP99TS0Gax2Dh3NqdRnti/2w/d93nR6f/+2mcMz2F//k/qlBlmSZ4hEAGOtKiGSw4h5iZCxZ5fAMGUwYkqr+EHqUg2SGbgO3KadXf7ERFKcsiwLdnh4TZXru99OrdCjqe/6rlyxKhEjxMwlQHBxhyKIrt78bwL2mKygh8kEzCFfGDjgIrhtrevZm4LiCv5Ou90aYCeVVjDKiM+ekE9h0YRgP8/RgbF3we5Ji2cxVJeexdcsuedNZOuV20x7YcGfIQ+l55eXNsRBj158GZBdn4xaK2tIQKBT2iAQGr3FA== 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=VULoKtsWLd7QUp6F6ejtKW0CvfQbcOavuVCT/vzNU70=; b=uYEL34oRVLzyg5/ym5Pi4XM1MFqJmV/rI/Sy8jqmlG5eiHHZmUS/+cVVvy3M3/vrAI6VjUSd3mxFSlX8OzQWqX5SfOljkgIm89V4j3hlZb5Ym77m31yo1AdtvLIEUsknSXbhy+R77MrAS75o3J8ZF8Kw6tQJKjP05ftxeRoEZBncTogkkuSEhc0wCoiskI+YyHfXIYP2CVMu5r1eJYT1GhHb+kmjTUVAFajvpVJF79w3O6+KcbR7LFRH/NTyeWioP7DhkqYjsUZLT6mp4jFZIAe9xnGZXQ1Vlez+wzPaIHBoBpBP6vpCcFrHOTI2PXzRVNH6zT+4bKz4xHMfr89Iew== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:51 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:51 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 23/27] iommu/arm-smmu-v3: Move the arm_smmu_asid_xa to per-smmu like vmid Date: Wed, 1 Nov 2023 20:36:41 -0300 Message-ID: <23-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0029.namprd17.prod.outlook.com (2603:10b6:208:15e::42) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: e5cd7725-1245-4284-3ec2-08dbdb336a2d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 22WbIdhz676RCA4C8JErwwCYFa7FpdxykWX0t6vU/QZA+On/8dZCiRkIpy1AwJvw8NDooVtqS6BfHVXn3wl1sb1PVvO8VtfH9hBnfTJ2fHDsW01I1o1MLlpWuq2QzXvSWyJXjK4F4x1jwnr3iyeZHLf3d+4oRt4MQDAxfleBit9fgNLaq9A9AMbPqVONo8gAqSKPfj53XxfgqIzygZtijppmMrde6TYvgtnChBarBCeGs+8E0o9Wytw0lNbckJUfMgttWqwX6fTZLuXW9ULySH4M16AF/kn8Aeiji666yPrX7M9c5UzmOgzlHP4iQUnqLTUO/Vdw96MRqDEhDgypBwdfCwE3yvZ6RTcSrilnzM25d74Dxq58vNVmTHyRstYmPSWJgyqIKQYvl69s5SQVs6os2r9v3yuTwIklkWgZkH7eXdhFzcuj2gRo8WEjY6egxIy1vWopfywtZc8YrvJVfq5QtG6yIMBVYnITP4OexGi+igmGBLU2elGBM8L1W21BslLy46LuMjZ+wMQhLkU/ViJn4RiM3CHqpgoenyu5uKk9n0SVcEoySyQHulKajasd X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fTJEDVrXk9jnH92IdiCmG9oCJlifbtB8tnC3XIIrPE6mEIzeB5HMrnpe6GXYWYdxifKNrNYh8djlWPSr/gsMkhwu43lkSWwJvPk4ZI1fgofMStbjQ10DBSMgdG3ZcEzm/mK2FtlO+bcqgwFjVxmqoiDxemtlPO4nXjBEUZWzIDefmU+wHW6nKgT8HHOtDSzmW5ZgEGqP71e1bBKU/pEa6fg4IzjJUrZeS3pINgX25nrYpSGoGca2abTrS2f/ZeXy9gwJU+LXPTd8VagWSKoQGj0TZTAfqmie7FxB8SmXQvAcdrr4r2AYNaSDlJkGIKPXkNzCaaanVIiW2aSSAUCagBtB8nD40S/G7aVNpessCLK/tU8GbkvVDBvf3+oFC/pfDzkHpE3Ul8umGnFQ0JbbdIP0ZwfZmO1SEaaVzwSieM31W6LHaAQU9s7NiYGIr/1xVRLaY/BDBdiKVH/OmgR0wizCck37DmadxXJ8ENlUyvURZg3VlAijSB/A++Q5jWFMk5fjkltDzuNbY2iSUl+kbuSmi/YDipLXpMR5zbmio6hRcXUpvxkuKoXmk9+cEPZCGDY+62N2gXoRph0EC48/jrnzFM72iSaBXUPkFUGASyDFVmnj27NyyUlL7/V5E00B7uSRD3LHW+yn/c9lNFEjDB/CICRK82Sijf8hSMlaqCeLntT0bNdxepp8edOLXuk7IsDt6VM6CIH4QTROyHU5jdAA3CvEyc34TsTOo4GA2zb6YC9N04vN1TZ5PxAAQXFs8omq0qAQzT/bXAN5kTxnNOWRrj3MTgyrt7iw0EqypppuaAR7uc+59z3Wds4T9KsA9IanLVU6yUkra646EzUKlsLOp9MZBY18URoSjw7LpVfFFLS+W4d7PRCGSZW5eG3s0G38HhwRs1zVoIToGEay7JwOFZsJTlT3hjkPgNXxjBmPgFHoyyJgugnLHBWaldAL27FMfGzI/nuPt/+NkHYxIx4eWnr3w2W0Y6EtudyjnnNO1I5UNzApKjES45s5T7R0GqbMKf9JT6ZmoueTAPe7srNhR8jWv2JiHH/a0lvtqaSNYjWyGM+5hjhncBkhKLysmGU/o9xqSeRjJiPBDpkXuLmVDxmbOMXWawTLI4hoEsxJlHlaw6bzG5zo9Egl4hEk9Redo3u4qpI7RhS3DHwaZTCOoMHJq/SRnMh6UsAASr4xJk5My1UdbdKEi8mmXvlP0MJOPTfthxsEAKSmFiQ99yLf0nh7QWV4j3YOI5YKN/zb5bAUnmbF9xhGVziuwgNMuXK/kRJCpBF/2PcUc1Z3glOZKTdplDnMy0/wsoY6e77un7aBW1FaTnw5b7Ea188zoulx0XvDUVuCPkkpg3FkJPWzhQTUqsbp1yFJ+diaa5IPNqPcPUMcPvUjFaQnbA9halk5KJLuWiZw5QxU9kx341Nr+VIij7prt9BOepTTE4km6VAXwdgVz66OsDqjOl9TIu2LAykk1KsJnFGo9kCaKtHlwW9ZyL1fJSnfjEzYcvMkFtVYIVtWhVKXXyoPS9W0xjY45po6lM4jPgv9U/kxZHRRsTQ0Ffia2H2KDZbvLXxDkLGJpthRRe/bB0viLR25 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e5cd7725-1245-4284-3ec2-08dbdb336a2d X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.5089 (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: ehmOn+4pltvYhnmJeMNK3H8z0eOnyBzPifLxbuC039LoA90ZuBsMy/UcCUMseD5C X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163716_488569_A5C53D97 X-CRM114-Status: GOOD ( 18.74 ) 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 The SVA BTM and shared cd code was the only thing keeping this as a global array. Now that is out of the way we can move it to per-smmu. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 2 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 39 +++++++++---------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 +-- 3 files changed, 22 insertions(+), 24 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 66de6cb62f9387..aa238d463cf808 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 @@ -407,7 +407,7 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; smmu_domain->smmu = smmu; - ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, + ret = xa_alloc(&smmu->asid_map, &asid, smmu_domain, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); if (ret) goto err_free; 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 7fd376a4e1752a..db5f2b92af17af 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -71,9 +71,6 @@ struct arm_smmu_option_prop { const char *prop; }; -DEFINE_XARRAY_ALLOC1(arm_smmu_asid_xa); -DEFINE_MUTEX(arm_smmu_asid_lock); - static struct arm_smmu_option_prop arm_smmu_options[] = { { ARM_SMMU_OPT_SKIP_PREFETCH, "hisilicon,broken-prefetch-cmd" }, { ARM_SMMU_OPT_PAGE0_REGS_ONLY, "cavium,cn9900-broken-page1-regspace"}, @@ -2246,9 +2243,9 @@ void arm_smmu_domain_free_id(struct arm_smmu_domain *smmu_domain) arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); /* Prevent SVA from touching the CD while we're freeing it */ - mutex_lock(&arm_smmu_asid_lock); - xa_erase(&arm_smmu_asid_xa, smmu_domain->cd.asid); - mutex_unlock(&arm_smmu_asid_lock); + mutex_lock(&smmu->asid_lock); + xa_erase(&smmu->asid_map, smmu_domain->cd.asid); + mutex_unlock(&smmu->asid_lock); } else if (smmu_domain->stage == ARM_SMMU_DOMAIN_S2 && smmu_domain->s2_cfg.vmid) { struct arm_smmu_cmdq_ent cmd = { @@ -2278,11 +2275,11 @@ static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; /* Prevent SVA from modifying the ASID until it is written to the CD */ - mutex_lock(&arm_smmu_asid_lock); - ret = xa_alloc(&arm_smmu_asid_xa, &asid, smmu_domain, + mutex_lock(&smmu->asid_lock); + ret = xa_alloc(&smmu->asid_map, &asid, smmu_domain, XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); cd->asid = (u16)asid; - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&smmu->asid_lock); return ret; } @@ -2545,7 +2542,7 @@ static int arm_smmu_attach_prepare(struct arm_smmu_master *master, * arm_smmu_master_domain contents otherwise it could randomly write one * or the other to the CD. */ - lockdep_assert_held(&arm_smmu_asid_lock); + lockdep_assert_held(&master->smmu->asid_lock); master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); if (!master_domain) @@ -2584,7 +2581,7 @@ static int arm_smmu_attach_prepare(struct arm_smmu_master *master, static void arm_smmu_attach_commit(struct arm_smmu_master *master, ioasid_t ssid, struct attach_state *state) { - lockdep_assert_held(&arm_smmu_asid_lock); + lockdep_assert_held(&master->smmu->asid_lock); if (!state->want_ats) { WARN_ON(master->ats_enabled); @@ -2647,12 +2644,12 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) * This allows the STE and the smmu_domain->devices list to * be inconsistent during this routine. */ - mutex_lock(&arm_smmu_asid_lock); + mutex_lock(&smmu->asid_lock); ret = arm_smmu_attach_prepare(master, smmu_domain, IOMMU_NO_PASID, &state); if (ret) { - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&smmu->asid_lock); return ret; } @@ -2677,7 +2674,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) } arm_smmu_attach_commit(master, IOMMU_NO_PASID, &state); - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&smmu->asid_lock); return 0; } @@ -2701,7 +2698,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, if (!cdptr) return -ENOMEM; - mutex_lock(&arm_smmu_asid_lock); + mutex_lock(&master->smmu->asid_lock); ret = arm_smmu_attach_prepare(master, smmu_domain, pasid, &state); if (ret) goto out_unlock; @@ -2711,7 +2708,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, arm_smmu_attach_commit(master, pasid, &state); out_unlock: - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&master->smmu->asid_lock); return 0; } @@ -2727,12 +2724,12 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) smmu_domain = to_smmu_domain(domain); - mutex_lock(&arm_smmu_asid_lock); + mutex_lock(&master->smmu->asid_lock); arm_smmu_clear_cd(master, pasid); if (master->ats_enabled) arm_smmu_atc_inv_master(master, pasid); arm_smmu_remove_master_domain(master, smmu_domain, pasid); - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&master->smmu->asid_lock); } static int arm_smmu_attach_dev_ste(struct device *dev, @@ -2749,7 +2746,7 @@ static int arm_smmu_attach_dev_ste(struct device *dev, * Do not allow any ASID to be changed while are working on the STE, * otherwise we could miss invalidations. */ - mutex_lock(&arm_smmu_asid_lock); + mutex_lock(&master->smmu->asid_lock); /* * The SMMU does not support enabling ATS with bypass/abort. When the @@ -2778,7 +2775,7 @@ static int arm_smmu_attach_dev_ste(struct device *dev, master->ats_enabled = false; - mutex_unlock(&arm_smmu_asid_lock); + mutex_unlock(&master->smmu->asid_lock); /* * This has to be done after removing the master from the @@ -3436,6 +3433,8 @@ static int arm_smmu_init_strtab(struct arm_smmu_device *smmu) smmu->strtab_cfg.strtab_base = reg; ida_init(&smmu->vmid_map); + xa_init_flags(&smmu->asid_map, XA_FLAGS_ALLOC1); + mutex_init(&smmu->asid_lock); return 0; } 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 c7c4f4fda31297..efc6bc11bbb838 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -675,6 +675,8 @@ struct arm_smmu_device { #define ARM_SMMU_MAX_ASIDS (1 << 16) unsigned int asid_bits; + struct xarray asid_map; + struct mutex asid_lock; #define ARM_SMMU_MAX_VMIDS (1 << 16) unsigned int vmid_bits; @@ -768,9 +770,6 @@ to_smmu_domain_safe(struct iommu_domain *domain) return NULL; } -extern struct xarray arm_smmu_asid_xa; -extern struct mutex arm_smmu_asid_lock; - struct arm_smmu_domain *arm_smmu_domain_alloc(void); void arm_smmu_clear_cd(struct arm_smmu_master *master, int ssid); From patchwork Wed Nov 1 23:36:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443145 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 9EEBFC001DB for ; Wed, 1 Nov 2023 23:37:46 +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=3z5Y5bUCxiOC/XnlNlH322qOXFd5ggOgQpMIN2dbYBI=; b=5Gd44AlG3M3Vvd X8/l+3MAyPkNknMKXccyAcZwzb31/KMvwIpTp8km+ufSIcZbJuYO1PzIPhBwJSevtJDS1Too75UX0 CHOiDo0dDjQF0FqNyd3WbLcQZcgFctpE0oliyfVdcyjcp6KhGcLLHWBdCpYq320K2mgWbHpgfXx+E 42zDW4xgEqrkXNX8wVggtPusABaCRgXmByxvlE/0d4kyy7Qq68TUZYknH90U07hgIghLprMHMInos Oc2sHYzXkyk34bh2r0zbmj+Ut0I4QlAlJnV6U7r2oBYMRwLUPUoiG+E0l2JCdk6TYHVzIUkN1Us25 /IlFbVOQptYJjW7z3Z0g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlr-008Ji4-0E; Wed, 01 Nov 2023 23:37:11 +0000 Received: from mail-bn7nam10on2060a.outbound.protection.outlook.com ([2a01:111:f400:7e8a::60a] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlh-008JW7-2g for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OY3ytYtETBsLMARxji3j4acgm+w/Zn2xoWXsbqQkKJ6rKNsUlH/DGLPv4dNMZVwjxaI4Hz9vwtaus5z2KrslUfOwNvwYHQF/vAGmjTp288qiGdrHCv4074gYwPp6SPVslUqZn0Q9IYU6JSJzOIBMXDmTYs90sAmP2GihLswvPzPdPAgRAUDLJAxvW1ZcXu+/+fXuVZ9tI37oGlNIumRiP0eW8F+aYOYD0tajp9p7+R+bza9iVIsf/C1Dxf3PM/ZDimKyE4Zyx+r17RdI5OeEDpON/BdI48Wru2ewTwnUqjSn5KH/lwooGj9xPOQvcwMClJKbr3KUsPmbcjXhVMX/6g== 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=+5auoBSDs8TbwGDotDrFC4zK/PXiSIqzlxJfD6+XTIY=; b=Zw65uC63g7rounzLAp/GtpqXXkFutJk/X7Hh+w7/f7TIyiaq6dSpe+uDi6sWUt5M1x5lB3tDKT6cg9tOafuMx6F+wp6k37qwDaczC1rqQijotu8Lp3pW33M5KkzeP/Zvx8Az12uEkEtTwF8mm6k+KEiL3e4ob5od2j59qaNP3YQjzmsJPQ2smFXJycbbfQLRKIsi7o9xRfHz05yetbTNiKneHfvgE8uu948Ov6Y0aF3mrwS0pk3vF0vdER1cWpSq0BdjtozCovd9vTYK2TdJhQfzVeK/kChBFJr3LKK7BastZxUzu9D+uuV4Z8LuTuoNNZUi8c0X/eC5XdQOL+bcAA== 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=+5auoBSDs8TbwGDotDrFC4zK/PXiSIqzlxJfD6+XTIY=; b=kccSg0vHcYW8Sa7+GCtOR9/fVsOhJ5YfUpl5sxfHKK+UxzLC+qOyEZlT+7C1rqYAZd8mek2CMeOY8anPBQnWnS5xgXjOFxWBmSTZoo6wjs8bfYMfql3PO90dV/Gcs5zTIkXbspcE2eHmbaqV0mZVk4gGiIZekrBaMJop7+iNHzFLkfKLBL8Ddk0OiNUmGwfsIvJ5vM5z3KqLK26Pe1fbbWbPIlHOBUJD3ERUn876d7N64KtBdg/b6QH0ObmvBraFiBArrU2U4lHEcODQzsz5F6nNhJkW0NvMxn4wBKj0PfWhayfukjHrSgXEEntfHcJi1tnFo8YBNukdToydt1hzEw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA1PR12MB7664.namprd12.prod.outlook.com (2603:10b6:208:423::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6933.28; Wed, 1 Nov 2023 23:36:52 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:52 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 24/27] iommu/arm-smmu-v3: Bring back SVA BTM support Date: Wed, 1 Nov 2023 20:36:42 -0300 Message-ID: <24-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0289.namprd13.prod.outlook.com (2603:10b6:208:2bc::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB7664:EE_ X-MS-Office365-Filtering-Correlation-Id: e7795015-7ed6-4c8c-3a94-08dbdb336a8a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sW5Qtqq4WG+ai3xr482xyRJYJ9bgy5DQoKja5zNERKuWgTMQOYDtEJ69si7aQsa/1b+aHhCL+186kplVWysC2Rur/HaT4A+WuAqE8bzC8796vZulIhCgh2Q8bYqtbOSj5iSITC6XMrcu1VwBy3ir8uRShIugFrd3Nor4MTypRhW95B5Oc9kxO7rOS+aoM0TaS1vMf6+z1uRDv9P8Ak7DLUGK4D4jJ7IuWcZrYOonY2JIx+MkqU0T3K3NJ1Tz+XSYn+sCSeo41J/TQxPJ2G/F3DcqfmDd83o8WdtUcoKi9mrlFQemPTMS84Ly5AwCc5ZUixF1IbCTRj8KBoODTeetoM0XJd8vM9+KtyXX8g9PN8uBQrh4ftaLIvvEaxe0rWruMe5YfJ1K4o/hbOUZG5PLFFyrTjfzheq/B2hHsy4Gu3F16riduB5P+U2ffYSGDc2h42qdctAQkjoOUYk0MBunXMh9/JhbjRNZ6I8IrsBR2ao1QfESQuV2nNJNsStVHr++N8apNHKqWbBaYxeogv4DvcVH1BJ6BzGHsQqj6DP/2dJZl+UkECT5LiKDVIxw6yDj X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(39860400002)(396003)(366004)(376002)(136003)(346002)(230922051799003)(1800799009)(451199024)(186009)(64100799003)(110136005)(2906002)(86362001)(41300700001)(8676002)(8936002)(4326008)(5660300002)(36756003)(478600001)(6486002)(6512007)(6506007)(38100700002)(6666004)(54906003)(66556008)(66476007)(107886003)(316002)(2616005)(66946007)(26005)(83380400001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: JeYUg8gle5fGK16PbZMbLvwobL+tReGqdxIgPCI2axRus+PtHEV1eBUlvnvWdY4Z7xKDLdNFAzh40uOscU/VYxAEFnngyy9wVQWLJ6pZbdhnJJ9Uwt+3FDnR3yUFEy4qrTphPDAcAbyjtzm/AuwtEYFsq7FxHCVWPl2SB3t9vbaN54H9p2PMecRpqU8B7svha5F7gR70OCeFvaZFzQ5exk7Gvxh6Rx6Lb9obXG2jPhlfgWf4ctURrSmgdNhKhaImJKvowu3Phk6GKzPjKlTfDiGmeTy5tnVHs/iJFTtOVvhwrzDkMUzCSFQpgAc2+l2D2xU8qm121Fi2/zzTMUr2UMwl7ir5ZOs0Zee898JVimF7qlToISGACId2AkJK+RKObo5gMjAJ5jh7W1HGukuFnwPVMETgGNNdF1QEHlOcJlNIOZwHbr09iTCs+I7gz7D4kykUHCr2D0a7uXCUE2GSuDT7TYyAsn2a57opoMt1QzSgQBOo7dFYScRNANgsN9UohoI5wYTNtSAt9A1jXC+BzKUyOk6aSuhCAuMzlROJjJC9h5wp+YwgmL7Idm1JgkZmwXkwcxhS2qi5KcMkcbp6A71tr3I+/TFUeGh0z5WLteJaJgdQK9yptxPJE2IOJyj0P3hFudUbdEYZFebp2ROsC2g/Wb5MOXTl1iHcXTpptaWR5uFK0Vp6gVRG9IiuH7RO0terWIdHuOQFBOKJPTVNGieFdLmexJze/ikq1Vw6z5csWrpgM9l7IrYiSrzDWmewF0d9kQMtR6AHtmxYrIuWTGG9pIKCe5LZfv4Km3wHDALP3JwGvNdHT83CL6NFWZGe6eU0QsCstRPW92feXuxTRuc945Dtnx7EPW9R4JZnQOebSiymTtoP0Z5oi4UROf5YzpG9jrT/RcEjdvZnmJMyKnRTnv4/le/DQvcNe+AhVG+hjT30iMV1Q2suIjvSKlWu29wLAAlPQWvwI5+KsVAAphgyP5JdPmrSSyFCUNxBW5PF9cOzYikyT94ost+mGgdFjo5De/M69l39U6oEqLOFBIGNgmhcdcADofHmYydcTIZadxnDQTaQJ7DZ9WQseVP4BeguXOjXRmPvPZmpg1NHz4EzzkMH9bZGhER1J3/GijDWjDUWwn96u8byQmI8N7rNWzjO/M1NmtJixMkCzrvcGKfudOWXJKo5/Q1Kod7VAiPv1cTn9KirMC5Ro8vORUZXGXygPMPV8AVh+QpzZ9pkEA465ZOvRX2vnBFEblSlEDK0MrEkxgCzsAYhtewQI5qoip2YR5PUU38Qov6Irg0C0j0VxAVDtBzJ3wJ9hu5uTUvUao51v3F+5tqvsU3bFHcVSjiuWy5yEbLRSIFIkxJJyCEVTdXXboSqZNpodm29R+iM5x5I6+Il497PtFIu4vbW3ik85LqqrCHwFZNXi4QuM9nZJaPz78jTBJ0jWdx7Zr0F4/nQdM8GdsUnPBNhm1T8wGwc/z160bJYyw/6KoFSvKQVkzbQOyN90oKAsk61ig/+KLtLWKrDw2nKhfpcGVBJXl4pul03XHJN1uQfsQALEEDzsXr5JrZCUNMkviqZWDThbZDeLtbvgptxbV4V2BAD X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e7795015-7ed6-4c8c-3a94-08dbdb336a8a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:48.1570 (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: pI9bMl4jc6RvLrssprjEMIV88bTiR8vBLm1xfYtt3OwJwjpS/8tl6WWbm1tx94xq X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7664 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163701_942377_C6108916 X-CRM114-Status: GOOD ( 26.11 ) 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 BTM support is a feature where the CPU TLB invalidation can be forwarded to the IOMMU and also invalidate the IOTLB. For this to work the CPU and IOMMU ASID must be the same. Retain the prior SVA design here of keeping the ASID allocator for the IOMMU private to SMMU and force SVA domains to set an ASID that matches the CPU ASID. This requires changing the ASID assigned to a S1 domain if it happens to be overlapping with the required CPU ASID. We hold on to the CPU ASID so long as the SVA iommu_domain exists, so SVA domain conflict is not possible. With the asid per-smmu we no longer have a problem that two per-smmu iommu_domain's would need to share a CPU ASID entry in the IOMMU's xarray. Use the same ASID move algorithm for the S1 domains as before with some streamlining around how the xarray is being used. Do not synchronize the ASID's if BTM mode is not supported. Just leave BTM features off everywhere. Audit all the places that touch cd->asid and think carefully about how the locking works with the change to the cd->asid by the move algorithm. Use xarray internal locking during xa_alloc() instead of double locking. Add a note that concurrent S1 invalidation doesn't fully work. Signed-off-by: Jason Gunthorpe --- .../iommu/arm/arm-smmu-v3/arm-smmu-v3-sva.c | 108 ++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 15 +-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- 3 files changed, 104 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 aa238d463cf808..2e6c3617cdbac5 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 @@ -15,12 +15,33 @@ static DEFINE_MUTEX(sva_lock); -static void __maybe_unused -arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) +static int arm_smmu_realloc_s1_domain_asid(struct arm_smmu_device *smmu, + struct arm_smmu_domain *smmu_domain) { struct arm_smmu_master_domain *master_domain; + u32 old_asid = smmu_domain->cd.asid; struct arm_smmu_cd target_cd; unsigned long flags; + int ret; + + lockdep_assert_held(&smmu->asid_lock); + + /* + * FIXME: The unmap and invalidation path doesn't take any locks but + * this is not fully safe. Since updating the CD tables is not atomic + * there is always a hole where invalidating only one ASID of two active + * ASIDs during unmap will cause the IOTLB to become stale. + * + * This approach is to hopefully shift the racing CPUs to the new ASID + * before we start programming the HW. This increases the chance that + * racing IOPTE changes will pick up an invalidation for the new ASID + * and we achieve eventual consistency. For the brief period where the + * old ASID is still in the CD entries it will become incoherent. + */ + ret = xa_alloc(&smmu->asid_map, &smmu_domain->cd.asid, smmu_domain, + XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); + if (ret) + return ret; spin_lock_irqsave(&smmu_domain->devices_lock, flags); list_for_each_entry(master_domain, &smmu_domain->devices, devices_elm) { @@ -36,6 +57,10 @@ arm_smmu_update_s1_domain_cd_entry(struct arm_smmu_domain *smmu_domain) &target_cd); } spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); + + /* Clean the ASID we are about to assign to a new translation */ + arm_smmu_tlb_inv_asid(smmu, old_asid); + return 0; } static u64 page_size_to_cd(void) @@ -138,12 +163,12 @@ static void arm_smmu_mm_arch_invalidate_secondary_tlbs(struct mmu_notifier *mn, } if (smmu_domain->btm_invalidation) { + ioasid_t asid = READ_ONCE(smmu_domain->cd.asid); + if (!size) - arm_smmu_tlb_inv_asid(smmu_domain->smmu, - smmu_domain->cd.asid); + arm_smmu_tlb_inv_asid(smmu_domain->smmu, asid); else - arm_smmu_tlb_inv_range_asid(start, size, - smmu_domain->cd.asid, + arm_smmu_tlb_inv_range_asid(start, size, asid, PAGE_SIZE, false, smmu_domain); } @@ -172,6 +197,8 @@ static void arm_smmu_mm_release(struct mmu_notifier *mn, struct mm_struct *mm) cdptr = arm_smmu_get_cd_ptr(master, master_domain->ssid); if (WARN_ON(!cdptr)) continue; + + /* An SVA ASID never changes, no asid_lock required */ arm_smmu_make_sva_cd(&target, master, NULL, smmu_domain->cd.asid, smmu_domain->btm_invalidation); @@ -377,6 +404,8 @@ static void arm_smmu_sva_domain_free(struct iommu_domain *domain) * unnecessary invalidation. */ arm_smmu_domain_free_id(smmu_domain); + if (smmu_domain->btm_invalidation) + arm64_mm_context_put(domain->mm); /* * Actual free is defered to the SRCU callback @@ -390,13 +419,72 @@ static const struct iommu_domain_ops arm_smmu_sva_domain_ops = { .free = arm_smmu_sva_domain_free }; +static int arm_smmu_share_asid(struct arm_smmu_device *smmu, + struct arm_smmu_domain *smmu_domain, + struct mm_struct *mm) +{ + struct arm_smmu_domain *old_s1_domain; + int ret; + + if (!(smmu_domain->smmu->features & ARM_SMMU_FEAT_BTM)) + return xa_alloc(&smmu->asid_map, &smmu_domain->cd.asid, + smmu_domain, + XA_LIMIT(1, (1 << smmu->asid_bits) - 1), + GFP_KERNEL); + + /* At this point the caller ensures we have a mmget() */ + smmu_domain->cd.asid = arm64_mm_context_get(mm); + + mutex_lock(&smmu->asid_lock); + old_s1_domain = xa_store(&smmu->asid_map, smmu_domain->cd.asid, + smmu_domain, GFP_KERNEL); + if (xa_err(old_s1_domain)) { + ret = xa_err(old_s1_domain); + goto out_put_asid; + } + + /* + * In BTM mode the CPU ASID and the IOMMU ASID have to be the same. + * Unfortunately we run separate allocators for this and the IOMMU + * ASID can already have been assigned to a S1 domain. SVA domains + * always align to their CPU ASIDs. In this case we change + * the S1 domain's ASID, update the CD entry and flush the caches. + * + * This is a bit tricky, all the places writing to a S1 CD, reading the + * S1 ASID, or doing xa_erase must hold the asid_lock or xa_lock to + * avoid IOTLB incoherence. + */ + if (old_s1_domain) { + if (WARN_ON(old_s1_domain->domain.type == IOMMU_DOMAIN_SVA)) { + ret = -EINVAL; + goto out_restore_s1; + } + ret = arm_smmu_realloc_s1_domain_asid(smmu, old_s1_domain); + if (ret) + goto out_restore_s1; + } + + smmu_domain->btm_invalidation = true; + + ret = 0; + goto out_unlock; + +out_restore_s1: + xa_store(&smmu->asid_map, smmu_domain->cd.asid, old_s1_domain, + GFP_KERNEL); +out_put_asid: + arm64_mm_context_put(mm); +out_unlock: + mutex_unlock(&smmu->asid_lock); + return ret; +} + struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, struct mm_struct *mm) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_domain *smmu_domain; - u32 asid; int ret; smmu_domain = arm_smmu_domain_alloc(); @@ -407,12 +495,10 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, smmu_domain->domain.ops = &arm_smmu_sva_domain_ops; smmu_domain->smmu = smmu; - ret = xa_alloc(&smmu->asid_map, &asid, smmu_domain, - XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); + ret = arm_smmu_share_asid(smmu, smmu_domain, mm); if (ret) goto err_free; - smmu_domain->cd.asid = asid; smmu_domain->mmu_notifier.ops = &arm_smmu_mmu_notifier_ops; ret = mmu_notifier_register(&smmu_domain->mmu_notifier, mm); if (ret) @@ -422,6 +508,8 @@ struct iommu_domain *arm_smmu_sva_domain_alloc(struct device *dev, err_asid: arm_smmu_domain_free_id(smmu_domain); + if (smmu_domain->btm_invalidation) + arm64_mm_context_put(mm); err_free: kfree(smmu_domain); return ERR_PTR(ret); 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 db5f2b92af17af..457b1fd8a9ab0d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1217,6 +1217,8 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr; + lockdep_assert_held(&master->smmu->asid_lock); + memset(target, 0, sizeof(*target)); target->data[0] = cpu_to_le64( @@ -2027,7 +2029,7 @@ static void arm_smmu_tlb_inv_context(void *cookie) * careful, 007. */ if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1) { - arm_smmu_tlb_inv_asid(smmu, smmu_domain->cd.asid); + arm_smmu_tlb_inv_asid(smmu, READ_ONCE(smmu_domain->cd.asid)); } else { cmd.opcode = CMDQ_OP_TLBI_S12_VMALL; cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; @@ -2270,17 +2272,10 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) static int arm_smmu_domain_finalise_s1(struct arm_smmu_device *smmu, struct arm_smmu_domain *smmu_domain) { - int ret; - u32 asid; struct arm_smmu_ctx_desc *cd = &smmu_domain->cd; - /* Prevent SVA from modifying the ASID until it is written to the CD */ - mutex_lock(&smmu->asid_lock); - ret = xa_alloc(&smmu->asid_map, &asid, smmu_domain, - XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); - cd->asid = (u16)asid; - mutex_unlock(&smmu->asid_lock); - return ret; + return xa_alloc(&smmu->asid_map, &cd->asid, smmu_domain, + XA_LIMIT(1, (1 << smmu->asid_bits) - 1), GFP_KERNEL); } static int arm_smmu_domain_finalise_s2(struct arm_smmu_device *smmu, 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 efc6bc11bbb838..96b01db47b0ea5 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -586,7 +586,7 @@ struct arm_smmu_strtab_l1_desc { }; struct arm_smmu_ctx_desc { - u16 asid; + u32 asid; }; struct arm_smmu_l1_ctx_desc { From patchwork Wed Nov 1 23:36:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443215 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 14429C41535 for ; Thu, 2 Nov 2023 00:45:50 +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=KIssYLeHCznw4qbz+YaETMaUImydh49GSCFAtcyZ9so=; b=oI0R6KM8sOIDzH TlpqrUHxrB/lts6RHYD7uIm63kE495y70hpniohCak+eo8KgJqDJmMjtotF+3ounWMEmIE56u1ijZ 3ICG357g15QdDaPEsL1g44dKHPk0dHV63k2/SSu7aSqK0Q5xEw58zWnodbXlk3hbkTcghqV260bdy cbItP9aUj28BQytSsRJct5NFN6nCsRyPlG9CJTkET2M4P8tlVt9ZWjWDjimMgqTrSrly6RKqb9eTl OUcbbKHe5rPZmp4MMA8HGxdWOYAIisX0/FaSWowaDIotyuwCa7cjFKuzdVBhz8mVgk5kJ9+HJAr7q BrQi2TIWVyBdh8isExUA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq7-008Pzr-2K; Thu, 02 Nov 2023 00:45:39 +0000 Received: from mail-bn8nam04on20610.outbound.protection.outlook.com ([2a01:111:f400:7e8d::610] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlw-008JV7-0s for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=F7qP1vShkgLWGfwaWWZcWvSU5luYZpx6tXhMouk4Bm9wzE2IGbS/JwvTqfOAgxYYwFVvOzvn8D0Acd6V0hX6lemXKqAH3uY5FcTdxH+B0vW+qrFb60qar94iCRVjjgNxtihtf6ZucBOdSCZV/DJfHsIbW2DLqh04MpSLdWXl7ur5HadA6IkuwRByPAjrYmCe37h9O/U1VVRwRN1HA9YK2Jm2NDsNrVNhf56tcdhIs5JhC5vJoBoSPDXcKTqO3aSOV4vynfQ766HfTrnzIxXeLDEGYwmYq76gBMDr8ythgKrULNUy11eRz7HumPsj4Wy1/B93xs2ZKKwIyIOiLo5Tvg== 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=4704La64aNqNatk/IcDiwXfTnYYBAB3KYCJ2QqvrqfQ=; b=XKgac/eawKpTi1T9vmhnX4MdViTjcRYzteSLB60Sr3LENRe8ox1iKXWawg8cdi3tgoJ32eL/6Vm8Plt8Elr8+CVgEaUVj4BS3LZxOmtmXdg1A0uI8sl4qyfBGmtNYIc/0tiB6jYg+glI6DV9OwDPiYThenf1IDO5qVzpAOUolhJCznkICoOgmo1xwDoJpT8EZ9JHA6Xo7ZPCQ69K9BYsuGuMTsFbeTfc046kwZ5P/I65sS0LsVvKEW8UCwcmj7yzwAjL/ih5LSvyBt+JcmgSyqJSrzNj7+9wQEkXV5PdQzQPnWPjxoLhLL7Sw4kZoHgZYLsDvLUCOsZJDxdZOYuRaw== 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=4704La64aNqNatk/IcDiwXfTnYYBAB3KYCJ2QqvrqfQ=; b=p5WejSoAt6v0wjnE7QpvEtYQEXh3uP7sE8gyrRlz6M9M7e3z5Uns48dM3LHKm2uY+15pBdKOeFqZ13V6aJUnshc5oxpzifWTbRJISiapZgyRL62chbTmEeyLgP63R6AHxA6Fb2UIM4zsCGuAUuYRxgoLKYQkU0a3KAiDLxfjTc50CgGR/Mk37nO2XvwjDxWatvFmm8GW7puOH4WS2GeLyvqFSw9p9dnl3bSrV6f1l0gWAerwK3ACYJha6cCwmLJtcvEw8tTLxfHG88zvQKpc8St8xrDzBjS7TxkXTvm82gC9NsqL05jSuPZLNyEZDEAJ6m1rZJ1Y+s62Jnw268Ni/A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:50 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:50 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 25/27] iommu/arm-smmu-v3: Allow IDENTITY/BLOCKED to be set while PASID is used Date: Wed, 1 Nov 2023 20:36:43 -0300 Message-ID: <25-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR17CA0003.namprd17.prod.outlook.com (2603:10b6:208:15e::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: a01a0a1d-9d6f-42a9-674b-08dbdb336a15 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JpDbw1LYJV+ESdjfYFk3VuRkVGzPXL5WPjjbvla2R5cCp7LEIC9ts4omtrdFBZg4YqgSlBQ4V2/DP9IL50cB/o6hVzJbRvpAlkX1fSgUfAST3xi/yB9Ru14tyrVIJdwqcQJ7i39yrOTDidwkhJbNXH5oSOMCiNRR5tKSMC2pEmkz5YaRBgi2U/hBhFNG20gGvUUHi3k0VFIrRRsmhtQVckaa91EdwKsu3JzDxA/aY0NxucB4mPjI468foHIpyYywXhApVlMJxg4GQCXnb0Dlcc/ea7/yw/+OJ0skUlmKvRp1BO8suALcs+llyWCotY63IooiAaj8awPGK6yZl96Foypa9z2Hn5tZdn0/c9Y0GxpuZtab07ZOAu4UV1sXfsNpPzLu+AQnBi4rKETCKqjQ8Av6yFfwUc1+ZUemdgZZOKFbi8kCZqdPY91UyfFgvNJC+eJki4aanMnl+K9oUscarsSy1/Ojlmk+wE0fxIxgIeu15r07t7bhyyQaB3FS04AaRWITqNt+eCHZQB6Txu2NV++btuq1Kt5lY549HNjrpe7CcjOkMLyrWcBpGxdVTvy8G0peN4ekMz0vXmduedqE7kWzMAL9TRK2ILPKkC3MUJhm3m0l06yvlry9F2ksbWm585x8fJujcyJMuMgRK2hq0g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(15650500001)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007)(473944003)(414714003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: GTA5Ks2wYjQYuEriEa2V8B4fCEiA3zg/H48BXYzbhDQMm6IfdsfVYfSUv8QCgdYvh/U1i03CLLPKYHZtykRgN/7dqjIXgatglC8QFePeS/Qc7AajXHdWjS6tNrvpS3DYcdWu/H0JM+LVVP0gACxI+962OWF0R89sP/EkDgqjhDB/C78Y/o4FtVKyg3H1XgctpZ56e+LZTdm5vJ6RoiIlM+MErZhdl53cxLMDTn2G+J+actD03q126ulx+PJ9JYAIV56FaB5U0808GVExwITCjfoTIEa9kJW9ELIe6gomli4siZLEt41qpPx2ftpr1YrB4btcShREaP/gqcr/Fji2ApLw/DPqXWtjjp1ZDXGByoYCZtV3dbW9460lYeD0UWz0CTMxUhkBd9rx2AJLyCLf9saVfGJ+Rwb18xPmubVFwHwUEsuqpfnpw3AGeSDsIRGLtEac5UzmEO1Oo61KTRw5KWVaMNq82fMFGrikOjJ0gyjpt3jwpzwkYLcso20iqQmBSMX05JEJdyKPLkpZlB/1EM3oDBQNCsZ2dKBKD8dqbc2tXnEXqBlmAMN5Rf/YjbHDd/4k7iMmYkdHka5vP0FhZ5AptF07vzO9hu1KxcfsMiUfyV8s7V6zDcFbDqylMn1A4+bqC9WDi+7sENqHYdbFWIzmFSF4ycM3KKSt8OVE3+1bJvD6vusI73wZ9hlBX7YXlWWfzdzHEQddcp/uKMztgZtkJTmu6rYxmUH/mrQmuZ1J4wDAiG0VQEtniQINqfZ6tpGybKwpDf4TZm6db89z4gTlyo8VuWRsNlgAYjMQ4jBs2Qp290SSvoStUIeRU4sHj8fgP+cV4OAxrd8wI4i9xP2t2k/9bx6W7KYJpaI4W8n+fFAMiqN6pAOLOZ/FLBntU0yLy5h5J27mz6Uzi8hJ6pnrJSTvkAdROPbAKWzUXHYzjEsAg+6OLDMgyvOU/RzxiVq0KevRysWu38UC02A01kZf4C5ds7fHBpy90odaVXHeAMui9w8HnnLgl3hgj1CDZEqpXXjo1Sm9zABbnl59aT8vuf5ZoKfNrMkzG2+RO1prtqYv+t2Vm00jFycdAKGacQ+wvRiS3fPkKzSNbT82MZBua8diEDArN3KTqkJGO73RhhMce7R50lKdCGQoDj10+EW3MkIJ1HkooEsuz6gxHxP9/Yu2vjEDlATIl44EVDUwmA6XwkuPyP7XPz8yeO73wWDLtlysYWKtmnSPSI35iuzDd9bXLdhycrk3tB312kOsu6AlDB5kn0h7M7mbz8wIaLVlipCwjoUBAQCS/+ewhiUs1hcMrqJYKow8PiIfy+pT7XhSgOHSd6kAVvoWvGYG7W5O/Vuc4EC5Saipq/3ieaUTIbgjf+o6XqWxBUDvEWbWk0/HdnQanpe/iIqSoL4hpB7V/WXsrp9Nqcs7L90vjewni7Om1uPruoShS22bbeg4CBle2VNL19pTEXfW9ChiJU/fP+wls+WtG/hQi8l4eDPPJj1fGDe1A+X7o36wYjiRBdWzrOjhYU0cyTNbCSpyCxygT+bYbX+tvmKaerRHa02SgoDg8tqKO4p7pz1c1hYQEAnmNzeQcm7PBJqf4wjQ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a01a0a1d-9d6f-42a9-674b-08dbdb336a15 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.3582 (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: YgRFcG0EEW9WpUjDu5KSSWViIf7QIHq1gWlcYv0RviofULX4zn8+6VT7mOdHG0nb X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163716_341783_2CEC0D15 X-CRM114-Status: GOOD ( 25.32 ) 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 The HW supports this, use the S1DSS bits to configure the behavior of SSID=0 which is the RID's translation. If SSID's are currently being used in the CD table then just update the S1DSS bits in the STE, remove the master_domain and leave ATS alone. For iommufd the driver design has a small problem that all the unused CD table entries are set with V=0 which will generate an event if VFIO userspace tries to use the CD entry. This patch extends this problem to include the RID as well if PASID is being used. For BLOCKED with used PASIDs the F_STREAM_DISABLED (STRTAB_STE_1_S1DSS_TERMINATE) event is generated on untagged traffic and a substream CD table entry with V=0 (removed pasid) will generate C_BAD_CD. Arguably there is no advantage to using S1DSS over the CD entry 0 with V=0. As we don't yet support PASID in iommufd this is a problem to resolve later, possibly by using EPD0 for unused CD table entries instead of V=0, and not using S1DSS for BLOCKED. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 66 ++++++++++++++------- 1 file changed, 43 insertions(+), 23 deletions(-) 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 457b1fd8a9ab0d..364ac78da16b48 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1485,7 +1485,7 @@ static void arm_smmu_make_bypass_ste(struct arm_smmu_ste *target) static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, struct arm_smmu_master *master, struct arm_smmu_ctx_desc_cfg *cd_table, - bool ats_enabled) + bool ats_enabled, unsigned int s1dss) { struct arm_smmu_device *smmu = master->smmu; @@ -1498,7 +1498,7 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, FIELD_PREP(STRTAB_STE_0_S1CDMAX, cd_table->s1cdmax)); target->data[1] = cpu_to_le64( - FIELD_PREP(STRTAB_STE_1_S1DSS, STRTAB_STE_1_S1DSS_SSID0) | + FIELD_PREP(STRTAB_STE_1_S1DSS, s1dss) | FIELD_PREP(STRTAB_STE_1_S1CIR, STRTAB_STE_1_S1C_CACHE_WBRA) | FIELD_PREP(STRTAB_STE_1_S1COR, STRTAB_STE_1_S1C_CACHE_WBRA) | FIELD_PREP(STRTAB_STE_1_S1CSH, ARM_SMMU_SH_ISH) | @@ -1511,7 +1511,11 @@ static void arm_smmu_make_cdtable_ste(struct arm_smmu_ste *target, FIELD_PREP(STRTAB_STE_1_STRW, (smmu->features & ARM_SMMU_FEAT_E2H) ? STRTAB_STE_1_STRW_EL2 : - STRTAB_STE_1_STRW_NSEL1)); + STRTAB_STE_1_STRW_NSEL1) | + FIELD_PREP(STRTAB_STE_1_SHCFG, + s1dss == STRTAB_STE_1_S1DSS_BYPASS ? + STRTAB_STE_1_SHCFG_INCOMING : + 0)); } static void arm_smmu_make_s2_domain_ste(struct arm_smmu_ste *target, @@ -2656,7 +2660,8 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) arm_smmu_write_cd_entry(master, IOMMU_NO_PASID, cdptr, &target_cd); arm_smmu_make_cdtable_ste(&target, master, &master->cd_table, - state.want_ats); + state.want_ats, + STRTAB_STE_1_S1DSS_SSID0); arm_smmu_install_ste_for_dev(master, &target); break; } @@ -2727,16 +2732,14 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) mutex_unlock(&master->smmu->asid_lock); } -static int arm_smmu_attach_dev_ste(struct device *dev, - struct arm_smmu_ste *ste) +static void arm_smmu_attach_dev_ste(struct device *dev, + struct arm_smmu_ste *ste, + unsigned int s1dss) { struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct arm_smmu_domain *old_domain = to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); - if (arm_smmu_ssids_in_use(&master->cd_table)) - return -EBUSY; - /* * Do not allow any ASID to be changed while are working on the STE, * otherwise we could miss invalidations. @@ -2744,19 +2747,34 @@ static int arm_smmu_attach_dev_ste(struct device *dev, mutex_lock(&master->smmu->asid_lock); /* - * The SMMU does not support enabling ATS with bypass/abort. When the - * STE is in bypass (STE.Config[2:0] == 0b100), ATS Translation Requests - * and Translated transactions are denied as though ATS is disabled for - * the stream (STE.EATS == 0b00), causing F_BAD_ATS_TREQ and - * F_TRANSL_FORBIDDEN events (IHI0070Ea 5.2 Stream Table Entry). + * If the CD table is not in use we can use the provided STE, otherwise + * we use a cdtable STE with the provided S1DSS. */ - if (master->ats_enabled) { - pci_disable_ats(to_pci_dev(master->dev)); + if (!arm_smmu_ssids_in_use(&master->cd_table)) { /* - * Ensure ATS is disabled at the endpoint before we issue the - * ATC invalidation via the SMMU. + * The SMMU does not support enabling ATS with bypass/abort. + * When the STE is in bypass (STE.Config[2:0] == 0b100), ATS + * Translation Requests and Translated transactions are denied + * as though ATS is disabled for the stream (STE.EATS == 0b00), + * causing F_BAD_ATS_TREQ and F_TRANSL_FORBIDDEN events + * (IHI0070Ea 5.2 Stream Table Entry). */ - wmb(); + if (master->ats_enabled) { + pci_disable_ats(to_pci_dev(master->dev)); + /* + * Ensure ATS is disabled at the endpoint before we + * issue the ATC invalidation via the SMMU. + */ + wmb(); + } + } else { + /* + * It also does not support ATS with S1DSS = bypass but we have + * no idea what the other PASIDs are doing so it has to be left + * on. + */ + arm_smmu_make_cdtable_ste(ste, master, &master->cd_table, + master->ats_enabled, s1dss); } arm_smmu_install_ste_for_dev(master, ste); @@ -2768,7 +2786,8 @@ static int arm_smmu_attach_dev_ste(struct device *dev, IOMMU_NO_PASID); } - master->ats_enabled = false; + if (!arm_smmu_ssids_in_use(&master->cd_table)) + master->ats_enabled = false; mutex_unlock(&master->smmu->asid_lock); @@ -2778,7 +2797,6 @@ static int arm_smmu_attach_dev_ste(struct device *dev, * descriptor from arm_smmu_share_asid(). */ arm_smmu_clear_cd(master, IOMMU_NO_PASID); - return 0; } static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, @@ -2787,7 +2805,8 @@ static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, struct arm_smmu_ste ste; arm_smmu_make_bypass_ste(&ste); - return arm_smmu_attach_dev_ste(dev, &ste); + arm_smmu_attach_dev_ste(dev, &ste, STRTAB_STE_1_S1DSS_BYPASS); + return 0; } static const struct iommu_domain_ops arm_smmu_identity_ops = { @@ -2805,7 +2824,8 @@ static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain, struct arm_smmu_ste ste; arm_smmu_make_abort_ste(&ste); - return arm_smmu_attach_dev_ste(dev, &ste); + arm_smmu_attach_dev_ste(dev, &ste, STRTAB_STE_1_S1DSS_TERMINATE); + return 0; } static const struct iommu_domain_ops arm_smmu_blocked_ops = { From patchwork Wed Nov 1 23:36:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443147 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 9FD50C4167B for ; Wed, 1 Nov 2023 23:37:50 +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=wFsm5CgDe44i3D6S5xqUAleL91rvFDrixz4k52AlnFE=; b=kjg9wxi7PaxPLZ 4JiubmtDIYUxoLmMG/nyHEkJeFBX+wswe1hSjQLQ9EYreZTcGqE0Snavont67ZwVarcNOyO/NkrZP +X90bN51lAFojvE2gBJbYN5na5Y4AXqBqZEP7NQSLQJuoD5Su7OEmcLjqKecPI5vxrZMPBZAmEtq1 YPB5JpkvDaEivwliW68qy5eel4Trrrq7Fch+TAV4PwTTVqvUFOZDb4QNXSRl3Bb0ST8ZT+2WptDNd RMtvS2Jw3m+AWYvkhbmSZNM3rIdAnx3cHgNDyUJwPe5WnsE9+8J9jpduTlrUZYHQy2A9A+J4ysnWj CCHksZNyXLHTbFb5UM3Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlx-008JrN-33; Wed, 01 Nov 2023 23:37:17 +0000 Received: from mail-bn8nam04on2061d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::61d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlm-008JWb-0g for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E5mKG70XyCUjN566DLK1teRCNXRZvIPy9fE9UGK46gQJZakQjmKBsFusfOxWaFlXosybkNl+mJ7M5oq7E9A6a3Nq7qaiHAPtl+rSqCrNcnFhlynKtEmVAp3Jmm4DtQuPS9h0+59OC3qbTRuhkGvApQrIJ0JcSe+o0riPxwLclQ09edGpsbUdEelDDITlCDxvmh9jCleEO7SIE6EscNmGGpoIQxfgqFwecnflVcQyIVO18wXxPvniboG5c6nt7Ls7ecE4GBq8ik7c+//DxQRJwP9U9HRAa5OrChc3VPX4OUXhHHl2vWP5NM2QUT4V95HMiXZLdWYp7rNkpTwHfjDY2g== 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=KWO0pPthpukUxfaplwfL45ByubOQ4LutouKSFnvAvDY=; b=bJ6X0YoO3W6hkNC2MszY13AU2CLt+UwsKhXSGOc4hwPd03o953PuGuG7yQICWfbAYmlfcIczNSzXpkszt4Cg2O3R4/qPZFOosfmkg6nSeJ2/Dg7cCuWCTA6m8Cv3G46WU4nrOBNEK+eH8Fil/Be3xEKHuIdFS9v4BXM6+No3EdqTXmfsB0cNCtkPgw6MBXkYjKNYWUjTrg1JNaDmTK4QcpMo81H30Lc9JRAo4uYjg643Xqaa7Fbh8e9lQRV3+LQxBvg8OveeVqg28Ln8l3DUREDYHnargsrr+yNGcswHNR6hlfPxWkgORwYOJ3fRHEJ14uYNEkpJIuaiUSMEJzQ2ng== 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=KWO0pPthpukUxfaplwfL45ByubOQ4LutouKSFnvAvDY=; b=D+aaYUzT+Nmn69jwClX0ps8kz7R8V0OWz3CXFW1dMZiqYR+UfrWJOHcJQw5wiJvxbaoeJrdjIZ+og/oWWapBaoRlkklq/09MMgJC++B4THFRGm8tLTG9yLVdy03WGC89Op2MazeOvOivv2IVk4U/Ko2ZnaXThYdXML6GAbTzQQ2k1HmUHYL3nzp85qmEsP11yIyyavXBsPPnLeodXQPzqOSZRVqe4J3xZwzSI7cpYNDj8OM7+jlXkSmK14HDPsN8g/Co9z71zKMbf53swmkRnaXsugBZ+y7sg+piZKjkeWvT7VVxN1NZ8Br/O9GhFmhs+36Re+3+zBPcY/vKKqVeFQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:49 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:49 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 26/27] iommu/arm-smmu-v3: Allow a PASID to be set when RID is IDENTITY/BLOCKED Date: Wed, 1 Nov 2023 20:36:44 -0300 Message-ID: <26-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR06CA0002.namprd06.prod.outlook.com (2603:10b6:208:23d::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 5ddab9bb-6bd3-4a4d-68bc-08dbdb3369f6 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YHzNpPfNjsK/yooiE4nFxvn48pX7eCR22h57TCKW0oJ/UWUu+do6AhrdbIPGfj6YYEFLEA7DhC9kD+htehycFIGavqdsMviCTlZpTMAixKdg/YTklpQf9Jb7x/k73uY98t9tgOQQrNPs1CH5uia5WzQNyh7BtULqkx2k1uQBmDtNzjdlox4xMWyWlGAl0WIeE5nSOo6yrXrdPTxxbF08nCOgA8zuadEw6YSj9yrL1hO4wL6++r46ZbsxdJ7EQV2ReD6VFnXCredb4vjiAERIk2ZCNltIwprEQbqEvAW5TKcdRpJpvNWhBjDHR2SCVUnBA0pYy3p6tHT+yVIhNBdsFDyrCGi5Racf618iiDxMRPh43WOAy/Mrrx4i5aopNAO+7dG4wPUzwrogHG+8LUb0SSdU0nvJ4FBIVJ+wjHbOPaPGLSell05FgGK88Ykt/DfxuKcrqnOUBF2+CVC5Oa0vfwiwLj95zDVbV2b5XbJs0fupIDyUbrSPX1SFoLcYlp2kThOOEbn5wridZWWY9o4sHnC6aCjNUrjyFxcbyACaeC5uI4Qnn/e+0P8QfAFkKJzZU8UFsgJW1YIVQ7mWxa65uD2ok9oRYo6+NMgY6tHeWI6lsta5w9ALrCdkaS9rWIiZ3qTukG/ReQJ3FfHSgq9J/w== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(15650500001)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007)(473944003)(414714003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: paso3IaX0e4sUC3VhqdPMgE5GtkMAE8r0vKkUDpxtLb+8uFO5WnQrza181aU7ZoRVC855ENENuvGdTaWhsNhXddvQFJxsPIamx0zx+oMEF1I0IBWG8UHt1Iyw6B1LBZgdVBNXwDUb0PUh4oq4jV/Jz4Ub8fqqDufgkHMeu82kzMzFdZLClQYk91TdGx4BFELgn3z89X9LS4D8/zRz12iWw/w2d/i1k3ChiDzHcEwsD6GfuiMmoDL0CzDMyt33CIWnRX9tF8KkywtqhvCJuvqCM4MmGrSUuq2PJmmWYJKIGafVROg6TnuiUfjFnRK6y9T2U7edmnHF/8Tnb8ZesE8qLRbcJznhni8m6cw1p3C+ZHk+otI0WX8ud9cRvGPBR3PKT0Qxq3J1BkQ1b178wr1QU/0iXI8+WvzLYVsIVuwZw5lk2+/txgNy6mcqB98cGtNlpRzHbDJZwrIZ68l+z+zn238yezphY0eo21dUNzEF8fvX+BqUJpwDgUS9kAboRW7EazfVGANlYx7Jglt2fmQmc+uv691mCdirXQOlguuhLHs2/xBv3ExniVtQrYSkqD0qhuMD91eYKWzoNuP8F+hL5/qS7oQmqE08ekCREyNBiWWL+XPXRoqmCm+sx/T0NBJ6rD6k8HvnQF6hBkFgr794EsrOqzEoeT0iHSdi9UXsnX/8AJykatdZ/+TLQYDamryv6ZxMJIHnYPVB+lLY4TGTjJ9hz/hky+TsmqFPBBfLkV8svpbxDjF2SoyP8mqtA39+2IYcxt3VbVwRzJ111/iZTyrikJwG9Esuw1fnR9QnDg9WM1X3QCIpYw+cPTsQEpNq7FmULOlOMUSstMOE3pYTYKkwsN8Ve0TTre1xELVjxi6Ae5R6TjYEEbxlIOts8/AZVOZIcQn42W5vqKHoJjvF39kHNrD/HJAPxFAZJsZsipU5eSk6eGN1xw3lqMTX8drViuEpLd0BL5bVXIWy1pZhTohmGFG7bZp/4ThXmi68xJZ6UbhOadOYJKWagw08w+/LdgN5w/rf2fK9SdjUU8DzA0zDxTKCg/8nTWdacrE67SViim+xJZ6Ye+SZvyweIp4Zeeo2r72FbBOb2iBKA2YdiIkned3V+wCYDKPi5M5PYllQIHTy5g4nNfE/tj1EonKUr8vkO3z1CksEsp2kT6Yvbh2Mzh3exDPeY5l/y4h2PhjcbuSUIGOFg4eZung5By4+quZPjQAo9XC/rf148m/nRWZWg31AFUj9eV8br4P4o5mVQlZ6XfP9Os/uqUA8YPiM3MWi+gJmpwPHyLtFtWzs2aICCD7Ftara9uCZjk3uA9qLSpGphrVg4jInTmjQXv0/mue+Zi2759d9vTPNBlqU7PA01oJj7t9OvU3Z1we74RYgvwGkA2Sewfla5iGgrTpvy/3AOwVWhwwyJzvYtthff99GnYoGV1PiwsNLZPyCn76ROKS6U3MCDmOEAU1z371DZ5gKtQYfCV9avykhnG9BJIqe77mWkQJv/CBTYqO1GxtFx1hFGBkDoFYGLc8pA+p2u4X9RhU1hK8sxVPNSFaaY7JeENzDQhxH4/lLYNVFu/LOB9c2Ri9ahSt9ZJO7DR8 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5ddab9bb-6bd3-4a4d-68bc-08dbdb3369f6 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.1844 (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: IlO5MOe+QSHv+iBlJ9LJj6g/oPr1ML/lT1F1HoKtBYQvWc05Jz1Fe/Is4e+S391S X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163706_289985_894D1C28 X-CRM114-Status: GOOD ( 22.49 ) 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 If the STE doesn't point to the CD table we can upgrade it by reprogramming the STE with the appropriate S1DSS. We may also need to turn on ATS at the same time. Keep track if the installed STE is pointing at the cd_table and the ATS state to trigger this path. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 57 +++++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 6 ++- 2 files changed, 56 insertions(+), 7 deletions(-) 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 364ac78da16b48..0040e5cfbf9d4f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2385,6 +2385,13 @@ static void arm_smmu_install_ste_for_dev(struct arm_smmu_master *master, int i, j; struct arm_smmu_device *smmu = master->smmu; + master->cd_table.in_ste = + FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(target->data[0])) == + STRTAB_STE_0_CFG_S1_TRANS; + master->ste_ats_enabled = + FIELD_GET(STRTAB_STE_1_EATS, le64_to_cpu(target->data[1])) == + STRTAB_STE_1_EATS_TRANS; + for (i = 0; i < master->num_streams; ++i) { u32 sid = master->streams[i].id; struct arm_smmu_ste *step = @@ -2678,21 +2685,48 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return 0; } +static void arm_smmu_update_ste(struct arm_smmu_master *master, + struct iommu_domain *sid_domain, + bool want_ats) +{ + unsigned int s1dss = STRTAB_STE_1_S1DSS_TERMINATE; + struct arm_smmu_ste ste; + + if (master->cd_table.in_ste && master->ste_ats_enabled == want_ats) + return; + + if (sid_domain->type == IOMMU_DOMAIN_IDENTITY) + s1dss = STRTAB_STE_1_S1DSS_BYPASS; + else + WARN_ON(sid_domain->type != IOMMU_DOMAIN_BLOCKED); + + /* + * Change the STE into a cdtable one with SID IDENTITY/BLOCKED behavior + * using s1dss if necessary. The cd_table is already installed then + * the S1DSS is correct and this will just update the EATS. Otherwise + * it installs the entire thing. This will be hitless. + */ + arm_smmu_make_cdtable_ste(&ste, master, &master->cd_table, want_ats, + s1dss); + arm_smmu_install_ste_for_dev(master, &ste); +} + 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_domain *sid_smmu_domain = - to_smmu_domain_safe(iommu_get_domain_for_dev(master->dev)); + struct iommu_domain *sid_domain = iommu_get_domain_for_dev(master->dev); struct arm_smmu_cd *cdptr; struct attach_state state; int ret; - if (smmu_domain->smmu != master->smmu) + if (smmu_domain->smmu != master->smmu || pasid == IOMMU_NO_PASID) return -EINVAL; - if (!sid_smmu_domain || !master->cd_table.used_sid) - return -ENODEV; + if (!master->cd_table.in_ste && + sid_domain->type != IOMMU_DOMAIN_IDENTITY && + sid_domain->type != IOMMU_DOMAIN_BLOCKED) + return -EINVAL; cdptr = arm_smmu_get_cd_ptr(master, pasid); if (!cdptr) @@ -2704,6 +2738,7 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, goto out_unlock; arm_smmu_write_cd_entry(master, pasid, cdptr, cd); + arm_smmu_update_ste(master, sid_domain, state.want_ats); arm_smmu_attach_commit(master, pasid, &state); @@ -2717,6 +2752,7 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) struct arm_smmu_master *master = dev_iommu_priv_get(dev); struct arm_smmu_domain *smmu_domain; struct iommu_domain *domain; + bool last_ssid = master->cd_table.used_ssids == 1; domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA); if (WARN_ON(IS_ERR(domain)) || !domain) @@ -2730,6 +2766,17 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) arm_smmu_atc_inv_master(master, pasid); arm_smmu_remove_master_domain(master, smmu_domain, pasid); mutex_unlock(&master->smmu->asid_lock); + + /* + * When the last user of the CD table goes away downgrade the STE back + * to a non-cd_table one. + */ + if (last_ssid && !master->cd_table.used_sid) { + struct iommu_domain *sid_domain = + iommu_get_domain_for_dev(master->dev); + + sid_domain->ops->attach_dev(sid_domain, master->dev); + } } static void arm_smmu_attach_dev_ste(struct device *dev, 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 96b01db47b0ea5..19628340ccd632 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -600,7 +600,8 @@ struct arm_smmu_ctx_desc_cfg { struct arm_smmu_l1_ctx_desc *l1_desc; unsigned int num_l1_ents; unsigned int used_ssids; - bool used_sid; + u8 used_sid; + u8 in_ste; u8 s1fmt; /* log2 of the maximum number of CDs supported by this table */ u8 s1cdmax; @@ -708,7 +709,8 @@ struct arm_smmu_master { /* Locked by the iommu core using the group mutex */ struct arm_smmu_ctx_desc_cfg cd_table; unsigned int num_streams; - bool ats_enabled; + bool ats_enabled : 1; + bool ste_ats_enabled : 1; bool stall_enabled; bool sva_enabled; bool iopf_enabled; From patchwork Wed Nov 1 23:36:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13443208 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 26145C4167B for ; Thu, 2 Nov 2023 00:45:47 +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=eJ7B8YoItDSWx5TOJnvKeuow36Cfqm1yF3HpLpezskU=; b=X0NtqgDjhUFbSw moU8Rz2HJ8Wfp05SUUHz7ejwZiPOrDn4ZURns+5gwuqHNnCfHdYyEik4awkw1qKBpy+CdpJCQvIKv 7ot2mwns30nYcK7tvScI4muNiwDFZz+u2bsEAKlMLrzwQ+kGJ6rw7sGsMdzVRsF6SbDHAhsGWjFIn vtOdbvSGuKFG83a5d/cJ/7VcRXgoKI0YrajL2cv/Inso1lf7qfLftbpyTd0W5FMYz/yyzSBjHr05z QzZTv2zetJkXlSCXFF51EYCesgrciNeYewcLpBtth3nQo6x0tERr92NYqruUkLGeGJQasS+HtZ/hO 5TtsVvu661dnjA4FUGmw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qyLq5-008Pyb-0L; Thu, 02 Nov 2023 00:45:37 +0000 Received: from mail-bn8nam04on2062d.outbound.protection.outlook.com ([2a01:111:f400:7e8d::62d] helo=NAM04-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qyKlr-008JUF-2k for linux-arm-kernel@lists.infradead.org; Wed, 01 Nov 2023 23:37:23 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=L3toJha1yfQMn46P6pdsq7E/7ROaQXMawoeO9wA9xYvo0ghFgI5OmofgZO27bc3M5FNPkNkN/WhviK0BCwpy9VeHKB+u0NrVFXcH2li/R15MqiUYrng1/Td/PTzNjDjJrh6+i8u4iweGxm9hU5mbthRGYiaCgEm96piYvEtRnC3XtJYL1k6n2MntCQKIcAXBUT+iHAMPLY8O96mERhIKe8HumWIPWmE+4uOMpc6HYrDs05GTl0rFUyUtutMZ1ksQkWSGoaakeD9diswqHMBVZqOAjCME16G6ujVvV+OMff4ieEv8GN99qih7YTQ/FHOP3HyS03wyvWlBtVbsan3fRQ== 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=jGjiY+BsSfMWK/Q13JqlthIt1T1RVrJZ3zIkNmN2cxA=; b=KApcz9qEnRooZZEaxRRz2y7/KS3SgFHfv52A+WYER1KS3exhuL+NQ6UF35ihgf7hgTDLfvYE50Srf7GYkeDOGmdqzpG1iZfaurcA42tmqKRKM+4ycbJM6ys4uXTx0Vr26/dbjJ1eZPboLvY5P7bHos8nvjOBTT7d+Di57ajyJ0umNKd9vpchGeBWtAhyyR0rpgnXpc+OJMatkXvgGro1f0sK68mBtWY0JX5QIuOKTJ89RIGqbmEH3/9TG/Kcu4GzdMpjHxcC8l4Vf4827Td5e0U5fXEKWRP/VOr+eleE5zC9ZK1qKIhYyTEpxA8lfDAHbwQB4QjRkIewp3C2mejsNA== 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=jGjiY+BsSfMWK/Q13JqlthIt1T1RVrJZ3zIkNmN2cxA=; b=XfK97EqDj11fk+QKexnVFNAZ+Ha8SnNp0kiyOGlECIbTTCKLmK4+SRC8GcnEu2YlUqlh8sB5DFEHcu/O+X1hR8+2NawRaZ5neZrlz4FXySeboM/YuE/bvAuS20AJ10AfzEnSWYfRZZm4fxfmAFsrhUcxncgenuxITcdvx54vDvXMUrQ0zBVJGg2VNrvK+pdn+i+wR2rxKaquehStG75pAaqYUj96A1vTqqw9WWzCROTg5B3BRx/e/InmvKpZzdlyxSUIlhz+CVHpkr1BnxwL45U5vSEJIo3FM01KdwyM9ZHZm1N0HMzdenSBYbcaLZebWBGFpZZsW0lx+Qtnm7A/dw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by BN9PR12MB5338.namprd12.prod.outlook.com (2603:10b6:408:103::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Wed, 1 Nov 2023 23:36:50 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::b53a:1092:9be2:cfb9%4]) with mapi id 15.20.6933.027; Wed, 1 Nov 2023 23:36:50 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Jean-Philippe Brucker , Michael Shavit , Nicolin Chen Subject: [PATCH v2 27/27] iommu/arm-smmu-v3: Allow setting a S1 domain to a PASID Date: Wed, 1 Nov 2023 20:36:45 -0300 Message-ID: <27-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v2-16665a652079+5947-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0293.namprd13.prod.outlook.com (2603:10b6:208:2bc::28) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BN9PR12MB5338:EE_ X-MS-Office365-Filtering-Correlation-Id: 7e6136f1-2eca-45b6-402c-08dbdb336a0e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3rti4p1lOdeLenU78WB8B3PpQszTFmmuvqaUn3DehKqi7ZmoQIV1tnOrNPPwN5G8w5WnGw5ZtyVh8gTWqv+49smxe4NZcxIMXfcKy81CwTDW62xt073iY/EAA1jJViBAqISPjROQU0E1YsBaveYl75Pw3HSbN/0akHEXUOEM50O81xWy4bglpn1dRwU7oru7pA1EiaTdT7yMql1smQY7sjictOtJKiXz1BfKIizf1flRwXqOY/elrFCA1rYEtdZpywyh7k2wsxAywcmUS83fk7GFFSECAA5ZNJ44LwpCv0brditYqPtxyvdZeNzC4SDCV1ZnDHlT4qjzp44Ob87Bft3pd2lmwNAtvGu79lLofrKiywnvmCoSAGW+iZZHUw2hXtAcMr1BKdf8iyDcDxjl/pUNJof5vE/foNF3DUTlHaib5g806Lf1yqMuF1dDZjpY18kaVPeCja+/fzfPYhEtF4yZgYX/dlBnUcZTjkvw870gr+KyeKqp7seXK2vxCXrxGFCvvofIKxkX6S5JKcX+ux0ZwAmSG8IU7a6RwKKfb5odFer4K6nqkv7XPvBksnLa X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(366004)(396003)(39860400002)(136003)(346002)(376002)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(6486002)(2906002)(478600001)(36756003)(54906003)(66476007)(86362001)(110136005)(66556008)(66946007)(5660300002)(41300700001)(26005)(107886003)(2616005)(38100700002)(83380400001)(8936002)(8676002)(4326008)(6506007)(316002)(6666004)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3iEjlh0TlyGsReme7WSU6o17cm7wzG+kzyzrpyc3Q4vyLVnDItxBn854SPe3uRnuuLIGOl5tFp8TL90Frb/JGUp+rXT030kwI3rNOrUkm+SQYBE4pX1MOy0biwi4Z/4zCWphMc8HiKj8iDKdIBaYtPk+gO/T7py0gVdouNyV3ldGHV5LH4dx825quhc06j2JSBsgDG8R12kXhv6G0f0FpBXXSCOZeEucFoKdFgKYH1pzQfYkwcmgVewZvnUVCVKQLm4wZTKCZWSw+YYi5yf3XUBQxW8QGpbNQ1Fej1sI1PAT03XPpOQ1krxNPLARo4xTiN/V/6aSJfbZVIYtSM5UUj2ZeYvJVmOiYSUfSC4QZxwrqF+zVHKGGjV4I5cVCtBvRHQH6FjtLO1SBaDtCIjny5bQnld11MNmAZg0jCKJ8PBPl5NiHY7X79ewiwIHzEaw9qyRSJBHb1FzXAX8NFNmoIadZ2yvXPN94edTg1ZhAeYFxf/7ZrqppMk6Y66FYwSl33V+UYH6KFQDUxsfda0RyYFQrONUOKsmHCc9bIbxOZqmxYZSrSyauSUF4Dy+DYbZF2jVvSYjn5TZi2OLluDGx2uLdKPIulqjjo6bNtxUwUbmbTJa2eoXhKrEb7oaUp8Dcote88YehtZhuWN6sEho32/mUiyFzW51sNRO0owcbQHmi3u6FRN+sUYjsPuj+jmhxHtoXPJhQDEIpNgcQglSGc9RhalPcMJEqHDzgyfY2cMzflit65oFuH27RjsY3ic219dsp3ItUvbFVnXkzsvENtxbsAlXRqjJZefOCCOvbxBHx/ls7AiPOgeCBj566TmIAj7ikc/QV9X0ImglWUkJpg/yGTxDmhDbIQHgZ7KItU9YkDHyO0wXH/c+q1FkRz+hxDZfPKyjcctzHZn1LheX26h+9fGsp3CgbBG7IS+ApmvZaYMZisOMwdN7TAHmdyDeoO8+46h5Yx/1s4L9qKZPalYIkvnZv7zXzSsy0cadprP0o9FXXkx8ek4NLh0spZVYzJZPAMb1vLoewqjOkkAuXW41ohoR05ig2IOI50lQzD1TrnWvT1lJOUJgGRmUL1REjH/Yp/LesJKf7jMQHHboe4yAHB6J9QXDKENOL73xU1vgTQ+lta2J+RSnCTI3GIxgaIDfUfpIEBN7P+c+DMTndFpQ526OqzUuttdiIVyPlcX+hQtDxpI5QhTtONYSTA14NappUjcflFYY/kfPopxNZHiZ4zOwfRp7K1KRMBXWr/K0n8a+TuLaoExgyi8Itx6qPCrCMktua2bejeq3fIoCqlN2Fs1omiW604LWKur1/m3Bhge3IEGX3mtxhK/3zSSbOlUQ1+5FjY0SdK3FABFBGi2UhKhAGRwSOHk4wbwJBeX7SFNSQSu9rXYcdRDDvwSmSHN1E2nRDlyn0qgYZnOZiMTk/hs7bdIJxqtJ1vvi0UxX2zydQKEAaBEYh2rPZbwKqxG0qYkL3Y6opJHyU3srj+afoNfiFurY985QQxNQ0OdCRrxIT39PlSZHi/OigGega5VJCwTKMM7P7lhwFVBnco2D/CAByys9gdPyrMof7Suf8gEmoWucl1jLO3uA2hWe X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7e6136f1-2eca-45b6-402c-08dbdb336a0e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Nov 2023 23:36:47.3101 (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: U+xqjQFH8ZCTYSu0zhxlLUsols3keGcouQJ+k3mC/vcAFEAFv40WHJzji82thb+D X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5338 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231101_163711_895224_65CF4508 X-CRM114-Status: GOOD ( 18.41 ) 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 The SVA cleanup made the SSID logic entirely general so all we need to do is call it with the correct cd table entry for a S1 domain. This is slightly tricky because of the ASID and how the locking works, the simple fix is to just update the ASID once we get the right locks. Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 45 +++++++++++++++++++-- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- 2 files changed, 42 insertions(+), 5 deletions(-) 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 0040e5cfbf9d4f..b2b3b6e421cf4d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1217,8 +1217,6 @@ void arm_smmu_make_s1_cd(struct arm_smmu_cd *target, typeof(&pgtbl_cfg->arm_lpae_s1_cfg.tcr) tcr = &pgtbl_cfg->arm_lpae_s1_cfg.tcr; - lockdep_assert_held(&master->smmu->asid_lock); - memset(target, 0, sizeof(*target)); target->data[0] = cpu_to_le64( @@ -2685,6 +2683,36 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return 0; } +static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t id) +{ + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_device *smmu = master->smmu; + struct arm_smmu_cd target_cd; + int ret = 0; + + mutex_lock(&smmu_domain->init_mutex); + if (!smmu_domain->smmu) + ret = arm_smmu_domain_finalise(smmu_domain, smmu); + else if (smmu_domain->smmu != smmu) + ret = -EINVAL; + mutex_unlock(&smmu_domain->init_mutex); + if (ret) + return ret; + + if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1) + return -EINVAL; + + /* + * We can read cd.asid outside the lock because arm_smmu_set_pasid() + * will fix it + */ + arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); + return arm_smmu_set_pasid(master, to_smmu_domain(domain), id, + &target_cd); +} + static void arm_smmu_update_ste(struct arm_smmu_master *master, struct iommu_domain *sid_domain, bool want_ats) @@ -2713,7 +2741,7 @@ static void arm_smmu_update_ste(struct arm_smmu_master *master, 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 *cd) { struct iommu_domain *sid_domain = iommu_get_domain_for_dev(master->dev); struct arm_smmu_cd *cdptr; @@ -2737,6 +2765,14 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, if (ret) goto out_unlock; + /* + * We don't want to obtain to the asid_lock too early, so fix up the + * caller set ASID under the lock in case it changed. + */ + cd->data[0] &= ~cpu_to_le64(CTXDESC_CD_0_ASID); + cd->data[0] |= cpu_to_le64( + FIELD_PREP(CTXDESC_CD_0_ASID, smmu_domain->cd.asid)); + arm_smmu_write_cd_entry(master, pasid, cdptr, cd); arm_smmu_update_ste(master, sid_domain, state.want_ats); @@ -2754,7 +2790,7 @@ static void arm_smmu_remove_dev_pasid(struct device *dev, ioasid_t pasid) struct iommu_domain *domain; bool last_ssid = master->cd_table.used_ssids == 1; - domain = iommu_get_domain_for_dev_pasid(dev, pasid, IOMMU_DOMAIN_SVA); + domain = iommu_get_domain_for_dev_pasid(dev, pasid, 0); if (WARN_ON(IS_ERR(domain)) || !domain) return; @@ -3280,6 +3316,7 @@ static struct iommu_ops arm_smmu_ops = { .owner = THIS_MODULE, .default_domain_ops = &(const struct iommu_domain_ops) { .attach_dev = arm_smmu_attach_dev, + .set_dev_pasid = arm_smmu_s1_set_dev_pasid, .map_pages = arm_smmu_map_pages, .unmap_pages = arm_smmu_unmap_pages, .flush_iotlb_all = arm_smmu_flush_iotlb_all, 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 19628340ccd632..91b23437f41055 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -786,7 +786,7 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, 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 *cd); void arm_smmu_remove_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid);