From patchwork Mon Feb 26 17:07:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13572539 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 08E8EC54E55 for ; Mon, 26 Feb 2024 17:11: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=f0z3njykMraIUM3MvbnTiq+7bbKe4zoYepKFKicGAdk=; b=xJ3jjP5/s+PJIF LUsNcJb1CIEDdH4w/eYbnAskhQmFUFeJE7GO2thCyNDumfduMPAAeBsJO77vBkRBOQAbuSOVWJ0+s dU7uMhtQP3b0Ir5MUxARQJE8WF5V9fbLJY+mkSU1Ix4Pf9Vn7PToC+7RyAuNZH0lxruyUm8VIFnxu tDVyqA0P2134hbU+lFhMRQ/c54TL9PX4ccyPQGQ/VQH4p5xA2zwe5/67Xof8CHCU6/0gFkTi+lY/0 du/FuRTwzdVzxTUGOdRXhXnjRcs/tIUKP6njJVUzk56x9H/F7ul+IggLwxjC51eZhcewnmLf1giIC 1tLoeeuuaJ2bvpnEYk0w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1reeVl-00000001rUn-3gnZ; Mon, 26 Feb 2024 17:11:29 +0000 Received: from mail-bn8nam12on20601.outbound.protection.outlook.com ([2a01:111:f403:2418::601] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1reeSI-00000001pm4-1pa2 for linux-arm-kernel@lists.infradead.org; Mon, 26 Feb 2024 17:08:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ii7AMo2pNYdY7MKO1R2B59sYXTX8bpnsVlMag+KmgnaMbeJFH/jGWEzfKq4XBz6VHQsAxGqPfIMRZz9IIx9b1UCMw8IoP6SSBS1WWUBw8Wdg165DgW8Zmfa5CEZ/EFUVqOe2mKm9pxDaBoRswjmMhYSG8oZI+Hxk/b8JcAja1pRxYKbCSwqQ7VAd4qcm2LZ0bSm1g6JFnwd066xpGb+hq06izmtWsBPf7DzUJRxcYwAVTkwmyBgJ1of1RdNop+7jEwzGCz9vy3zqS3TzQuYdIZ9OOI+0c15hGL9ZaPp/r8ieoMwqo/Qk7f60z5IrIweWUXfoig2H/5IJBekRDNxKSg== 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=5T/3Da5SgzU/Vy0hm23N8FJaF0+S35B8BrDUE/AdJb0=; b=k9WtwxmXPcwPaZYd5+/X1pcas+00QMW/3qTVoXQYky/N/ETMVkeyH7POzywAEjMXZfHsOEVEW+OVszHoUF432oketkAcj/MJvfktA5DBmCJ/NyzFaYr9oOtw7JgAjqm5gBdeTR5hwsrATrr0Owwmggm9XxA+E73de0VOPbTMcSAfa85/D66KfpZ3GUnHSPYayL0nOE6zESnekDNZLrUnG8WHVRpBvwI9jRFfEpTzVFy6XYs9bHWG2ugFo2ekVty4wkNaP93KWJVZ6o9nZShEMniYIPcfRnTaQWTRpuZcx+f+f/etoYSCg+DrAmh53G6FJLPaATRefPdBmo6Yzh9QQg== 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=5T/3Da5SgzU/Vy0hm23N8FJaF0+S35B8BrDUE/AdJb0=; b=O71fJvpsIe1Rlyp/OImnNOk7yc3Ncz4oN+EWYiPedXH017ke7MVknVqaxYQ5/oTdG/ZZ03aWtXUojF8GBMVvAE5sZWKj70+JAqiLwidQIP7GpjurbDOmVcRs9vRx4wATLy9K1z2nWp1d1gPfdwwiCXDYC2aoRIZT1fKsJmdbR8MHAWXcnZbhUHEQfNpjKrbjrJzU6SiUBjZaMBM1gQ0oxhjc90aoz8UtCld7AkQ70Q2LE4xUNupbwC8yZMqP1/ysARmJZwWUL1Fo3DGUqDcMzgNRoSwna3v+HSy5vFJ2idcnB1sTxuf3ceBAVCgOjIXQPzeFti+4ieQJwVTypz/QCg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) by SN7PR12MB6768.namprd12.prod.outlook.com (2603:10b6:806:268::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.33; Mon, 26 Feb 2024 17:07:37 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::2e3e:c7c0:84da:3941]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::2e3e:c7c0:84da:3941%6]) with mapi id 15.20.7316.034; Mon, 26 Feb 2024 17:07:37 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Lu Baolu , Jean-Philippe Brucker , Joerg Roedel , Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi , Mostafa Saleh , Zhangfei Gao Subject: [PATCH v6 08/16] iommu/arm-smmu-v3: Put writing the context descriptor in the right order Date: Mon, 26 Feb 2024 13:07:19 -0400 Message-ID: <8-v6-96275f25c39d+2d4-smmuv3_newapi_p1_jgg@nvidia.com> In-Reply-To: <0-v6-96275f25c39d+2d4-smmuv3_newapi_p1_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR1501CA0023.namprd15.prod.outlook.com (2603:10b6:207:17::36) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|SN7PR12MB6768:EE_ X-MS-Office365-Filtering-Correlation-Id: f91f606e-66e6-4179-e82d-08dc36ed6b1e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pxvbKKz4UcfQpJ+q3p1ASe2PATlWa768uEiyr+sbQxIjGKAP8sesSKlEtJTzbR/jgQFmBYDCRzF+FBGe2Y2ZJ9IPTBBRGYUgtaJ96JMisF1cTLxHEc7TxZYmqYekTQfAwPaPUMS7u/R+WPIjJsdeMFwTMZn6FxA5XYSyaxnFmyKgqQWScwkpht2qWvdhONR5YwzMNd9aV5bj64bLAxLOUu5Jk6Yt3a7mWlPucaSb5xSTa5mlqrTXQI5aF58CRc6oZqMDCgURApVVl1SnZZQdNMvMgUIOb6rCe988k+9Y2wjpIFAWjlVqQ7iaqmkabIBdHEQBF9JrluDpZInOYsqR08YiqcYZO6sInic93h4KSHcc8LwyVuwphb1Mi0v414wn7hZSTrIhJxmXfqCI3PWM9R+dg9LxUSiIJ46Q75Ktv2Mf0HEjwXfxZD5KeHE8HWfXicrVEbcTwTXk7Is+XXdT0BfKtK9hXwGErm/ZnjkrZLY/9gQd1PzH4rQa3Kvi4Z480frpJLAJzQTCZspbo+XvbNGMJKhCiS8zV7mRy+/Y2VGdPIhspWio1IbxWaF8xWm4BtKxmEfNajScIDW+cgIjQF9aXaLB6bEmuAEojZIloofMamzR9XARKtTPTjmaSEOajKtMUijEGSeQFa8yLNfCYg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR12MB3849.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: s9Z06JI+2c5MrxMHD0sO49YrXtpdNv+hM2CWr6h2u3p9K6edHkp7Mx+6cAFPnjqdikVW08WZcSmQ5lPtaP9zZ5qHB9viEQv1BpIe4LcH3lh4RkQ4UN88LfiRSlUqEk2A1lQLnC8mFTnnz39ppbamh7D9UTakdQIaAcfWyivo9GhlLGzAiPhD24EkvQULtYkkUxXNjgGazrkBnpIbkxxdo/yjrknzGpOtJ1FV/VukP32veB4It+DE8FvoplTF3r0qiYz06/4IMg6M0rDiLfmSL3eRCeTjSc/GOwSx0RYPBoORaCB73RCxYEUsUR70jO4bFJueTqAL1lqi8bWvbtgr6AhxeOL58R8eXHjf9wuiV3CzwQ3n4+pW18w66d/EjWmr3nrDEwEH1nevjDqU83k0LuOBSPkWFzIM9CelO3QSJ3Z/mhpYteUklYxbxU574wfEHLmoNYxHVhtT/9ZMDiBJ49Af2kwKuWU5IMHrFsFN+/w0w4GG0sW7T7x7WwxWAeFK3octYMUexuc5Avi7JjGnhqLXD4MLKI4ySZKNAnnYvtRPa+as3lafe+c9feEDGrUJZ0xVyw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4179-e82d-08dc36ed6b1e X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Feb 2024 17:07:31.3411 (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: uIXQYka1EiC7prSrbhPxJlRokmgbUtXCBBfXIhhciKPYnD1zuU6Z0zaDzKgxvZJO X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB6768 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240226_090758_789085_EE1AA967 X-CRM114-Status: GOOD ( 19.93 ) 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 Get closer to the IOMMU API ideal that changes between domains can be hitless. The ordering for the CD table entry is not entirely clean from this perspective. When switching away from a STE with a CD table programmed in it we should write the new STE first, then clear any old data in the CD entry. If we are programming a CD table for the first time to a STE then the CD entry should be programmed before the STE is loaded. If we are replacing a CD table entry when the STE already points at the CD entry then we just need to do the make/break sequence. Lift this code out of arm_smmu_detach_dev() so it can all be sequenced properly. The only other caller is arm_smmu_release_device() and it is going to free the cdtable anyhow, so it doesn't matter what is in it. Reviewed-by: Michael Shavit Reviewed-by: Nicolin Chen Reviewed-by: Mostafa Saleh Tested-by: Shameer Kolothum Tested-by: Nicolin Chen Tested-by: Moritz Fischer Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 29 ++++++++++++++------- 1 file changed, 20 insertions(+), 9 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 597a8c5f965899..ec05743ee20847 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2522,14 +2522,6 @@ static void arm_smmu_detach_dev(struct arm_smmu_master *master) master->domain = NULL; master->ats_enabled = false; - /* - * Clearing the CD entry isn't strictly required to detach the domain - * since the table is uninstalled anyway, but it helps avoid confusion - * in the call to arm_smmu_write_ctx_desc on the next attach (which - * expects the entry to be empty). - */ - if (smmu_domain->stage == ARM_SMMU_DOMAIN_S1 && master->cd_table.cdtab) - arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, NULL); } static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) @@ -2606,6 +2598,17 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) master->domain = NULL; 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) { + master->domain = NULL; + goto out_list_del; + } } ret = arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, &smmu_domain->cd); @@ -2615,15 +2618,23 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) } arm_smmu_make_cdtable_ste(&target, master); + 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); + if (master->cd_table.cdtab) + arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, + NULL); break; case ARM_SMMU_DOMAIN_BYPASS: arm_smmu_make_bypass_ste(&target); + arm_smmu_install_ste_for_dev(master, &target); + if (master->cd_table.cdtab) + arm_smmu_write_ctx_desc(master, IOMMU_NO_PASID, + NULL); break; } - arm_smmu_install_ste_for_dev(master, &target); arm_smmu_enable_ats(master); goto out_unlock;