From patchwork Tue Feb 6 15:12:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13547501 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 5A7D6C48297 for ; Tue, 6 Feb 2024 15:13:37 +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=A8YQnbxlAspzK//OLYkDtOD0NjzfxWbwEGySpfu5qYI=; b=q9sJL/oFdlGFxG jReUNzxm4x96Zf7OH1/Viw8kkX9RVU/eO3VobsthCg1M6pEBamCG+sXc8ZX0UhSC0owvEkOh4RIv4 d+GnCg0Z/Lfe6OZETzKhbDeltcD7ATcx6KdenmXVPpvKF1xn/J9VgMffdcYK3/Usm3a27mGpQISwX bsG4YHJUf6TrRkH3SG63Bs+9IgBFA6SJzre1tpO1ho10u3vKBYvluHVaAdPhTQnE6LbAEICl+tKZE C+ayBa5U8LWLJfGt54TUDgN3aa1x5f9H/10Xe5rVQq264ESdCQbn4uFU6M8rJQcGB4fkip/lesnZc v2jsO+cVllthCGiGkCtw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rXN8U-000000081Vb-2zXm; Tue, 06 Feb 2024 15:13:22 +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 1rXN8K-000000081Kg-3Nlo for linux-arm-kernel@lists.infradead.org; Tue, 06 Feb 2024 15:13:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mkpq93f+rJlteNq8VEShUIlG7UjLR+Ipc0NjT1v6P3JxPv+KJlTMOUpF8gv+9N29gHPLaWS+TM3C8rDZOXTkzqGcqTaC2M/KruIEYcTxGEXpgsnHNKP8ZjUTqiEqHNNj2+L4pbaVKKlz391uQYsI9Yds2CctynRc+RKbZiQCmF+HANiGea75hutqEFv6cwnOS1XErBbdwl5QfIyV2QnQLfZQ7OnDskoTn5T3pKXIziAb2Cy7AMof8Bo/QCMbDpTE/lcYbdOAOedBDdQ03tSeDqSPrsdniUZFpXqceVgBhnInP3ZPFyKYHZVEyQSu+Kt0bCJHt0fkCrMPbm+qjOHqKw== 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=UM/ppWWIu5pAtBD0apLAHYEv4grf7RF/g6mQ9fRdN7s=; b=YgQzqHn0851jn/ysNuRNrE9wIp0STgdelnvMS7iV7XIe4ZnM6gsdyeikiq0SFHVuTaWy/awaKU2S6VYoItGKzXprhuqXDg+R5uF7yWS0h3FcVghU37t3nJC8Luv5CFwd/CODkIIj656k8iliqqxj6AHhPu+qnH3dNInsAIyBlcVyR9Ys8Jno460aNwYsR10y/OHjEDToZlB9vc5DaZuJGZxuhZI+o5EE7yiMO3Rz86piWGEKrF0onos/aLlAvEOwZV+FQtSXNHMJ4Z0paxOtTIfx467Xv+xnL7Rkz7Do3vy7Ts6QG+zcR7o8SJGCe4cpURcLRxMRHy7JyrZZMVVTQQ== 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=UM/ppWWIu5pAtBD0apLAHYEv4grf7RF/g6mQ9fRdN7s=; b=doRbDtcVLJ16Hkm1/jNUg3hvCviE8M4HnuM32DKOPa2pNNlAq9MKGVcLOvHNzgANZBcMZVokfaBd+YC5eRZclzRBqfypJdA6z0HbyeLcRWB6/ebj/T4OHfRKvzu3vrTTsHO29mBxOxhbOZhSrsMsKkgDcBryObSx+iqWBzDQWnOyjTcAz/aOoJauMk0WloO8Ec2MKWw8ty4/qOZm8r2Y1pxKsvkvdOCsHAyNzDXoV2WKSfhSCHoPvOssP98v/OGJKYF0C18xlU/9p6RpS9MFD91NM1MkZ++Ebx8zyB7FaLu5AQ0RcOEtMBhVvWqJ44NGJkg1fL547WnOjTuJXSFeHA== 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 SJ0PR12MB6733.namprd12.prod.outlook.com (2603:10b6:a03:477::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7270.16; Tue, 6 Feb 2024 15:13:02 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::96dd:1160:6472:9873]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::96dd:1160:6472:9873%6]) with mapi id 15.20.7270.012; Tue, 6 Feb 2024 15:13:02 +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, Shameer Kolothum , Mostafa Saleh , Zhangfei Gao Subject: [PATCH v5 09/17] iommu/arm-smmu-v3: Put writing the context descriptor in the right order Date: Tue, 6 Feb 2024 11:12:46 -0400 Message-ID: <9-v5-cd1be8dd9c71+3fa-smmuv3_newapi_p1_jgg@nvidia.com> In-Reply-To: <0-v5-cd1be8dd9c71+3fa-smmuv3_newapi_p1_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR13CA0023.namprd13.prod.outlook.com (2603:10b6:208:160::36) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6733:EE_ X-MS-Office365-Filtering-Correlation-Id: 5d032be7-de04-4f45-79c5-08dc2726189e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4VoyJUJaSaeCnKPFYFw3kqlG5aaKu4XN4qtRqhFxa8VKoY2Buhn3yG1lMd1W7pDxIAnEORd6bTXh/fD9PfkfKuzF5aTEvMAQ21V3Ufo5kZ4lZdFBz/fciEvDrOJHtPwpexL+tRJ8ySSIvZBQ9Mk+OvlEDdj23Geyz2PLDmzAxugQs6NahxlD879XjxiM6njsdE4fZB0qBHg9+07KDoHz0OCeapeF3cFj1/NTW1RCxWHzZeAZFTg3dediAeHoUTaTRhCoBJaeQdF9QBsFqEKb7COAGQ+xAx1+yg0MYhp1mesUJ+A9MPbte3DElaPso+RV3OapCEsyf0uQDt/6fTia4ttzuv3lkNp1DlH9FoaMuJGsUqTc1Y4Vd1Fnv1KCeTcXwcj3ri8+lZFCU803QRaaJYY/eHF7GRz23T0uVRGIgmSnJcRJqyzOD5bI4c31rJ176uFJqqgmdrS4dc6BxV66VglDdysx3jgCPBUjMJBShckeZBrmEhAO+AuqEMBEAY35YzBMh742KYh486FbfhhlBzqVKEPqMvzbzwjZuIwOrj4ye44/i+UuG2SdPqwlLOr5s+R6dhiT9RVIo0BkAP9b4/Kpd6c/srUooFj2pr9cL8k= 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)(346002)(39860400002)(366004)(376002)(396003)(136003)(230922051799003)(64100799003)(186009)(1800799012)(451199024)(54906003)(316002)(83380400001)(110136005)(38100700002)(2616005)(6506007)(6666004)(26005)(2906002)(7416002)(6512007)(6486002)(86362001)(5660300002)(478600001)(8676002)(66946007)(66556008)(4326008)(8936002)(36756003)(66476007)(41300700001)(66899024)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MIH4ovvasTh8gOhpEcX6J/IKZoc8/8cLRs4G+gELNoQZdzDxt8MZm4tm+eW6w9CURuCwk4qKuA3vMb67rJkKgonGAjL0GZtzGt5dt5EMBCXfZ3lgKtjfTjhIZ/Wl/zKCOzNotSv2f2yBZukRuzrnEE6XEP7NTWDPYv0rcvvHN3DYHr5VeL6M/ePRZkaI8hlvIjb+WaMZjtgxusiiFduDlbZnVcpx4SY95s3hP0bnJCx46AstMnE4bjxcUBR2Ec7eZRRXBOV7OrTg6P2HS6scNtWFdBuduXTJlZKzk8wzG3tpopHm1lr26PZvk3yG/r2WGpjzS2mX+ATPSh4TZpzNb3dl5l79CUkmDYyzaL6ygJTxd9zd6nYJpN89i7hWIwCnCR4D9nRP7RbFz295Vl219NAQlDT2pq/BEQzWDIt8OBmjtddDr8mhvs5SCMk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BvXqrKySHV/CxbkYvVwr9KI4PIshG9l96lJb8acKdmVvMZV85OAQILGDpCquVtJHHI/y8sjxOxHC2UIP36zkqQ+WD X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5d032be7-de04-4f45-79c5-08dc2726189e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Feb 2024 15:12:55.6387 (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: DmDnbubN437alRQQZvgsfbRdH4VhVDyyT2efCqlDauQMO5bJBun/wDsQNHITj9li X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6733 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240206_071312_968296_F09245E0 X-CRM114-Status: GOOD ( 20.05 ) 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 Tested-by: Shameer Kolothum Tested-by: Nicolin Chen Tested-by: Moritz Fischer Signed-off-by: Jason Gunthorpe Reviewed-by: Mostafa Saleh --- 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 340f3dc82c9ce0..2a6ac0af932c54 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2575,14 +2575,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) @@ -2659,6 +2651,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); @@ -2668,15 +2671,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;