From patchwork Wed Oct 11 00:33:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13416483 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 084ADCD98E3 for ; Wed, 11 Oct 2023 00:34:51 +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=7RP21VbDWijP2woCzs/NPoMaS8wCzlXNLKIog0zhnYc=; b=SOWpwYFCIwZ9ST TPh8EkRVFbrreuhfVcxqUCqkAgc2JFLb6x8owJKbbMuByxtHq5ExTLUX8/pomqbDwsnSsONxT5r23 uuVFV98pRxXgUcVKKiBIbtLIzKwUb4U/AN7npkioCIUUWm4nxyHzl1YPgQnqaRc2VyY4tpQ0j3n6m tTkKMghUZMGLr/v9kVExuFg27f0nZNbgGyPp+MBlZg+fg1nT/yXguFwFeV5fv3NLWTob/YcVdim41 8JmrjcCYcFSTAVYludXMIcSC8waqSXLB33l1ELKgxbiwhIPVR59rspiM1MsJauw2I38zQHVyAm1os TaXO0d4NvXG7dqZ4Va6g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qqNAn-00ERH7-03; Wed, 11 Oct 2023 00:34:01 +0000 Received: from mail-mw2nam12on20610.outbound.protection.outlook.com ([2a01:111:f400:fe5a::610] helo=NAM12-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qqNAX-00EQyX-1z for linux-arm-kernel@lists.infradead.org; Wed, 11 Oct 2023 00:33:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jRtMK8t8XfpaMwdc8uVLuVcULYOH1Pg/8Q4kcAhTg/9L4lsPK8fmBlIcfpidjbDToO97/ZeSYgfN+mbfBI9QThb375AzgYkkg0rLO5pCe4uLjdUjBi2GU5cet3BvBhzQD7GKaOJOGMyx/TD6BuUikQ9qL1KXRlfd3bHeQONtZf7bckFtP8SQZq+EbUO9ZNjg14jxsYpWmzk2coxznRrDkvEeIpjVdBqOU9pr6Xz1JmlqVx0a0yahA8bP4BdLRQrTZpxYltzOryx7pSa8G7y+gQQnlT5Hib9GLYReaU8lcqGCak9cKjku6VZUkLnql2WaUORG/wL2J07uczHsVb/J+Q== 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=pJEnKe35tbWpzXhz/4/6PxYmS6R8IirgNIvg5EWAmQA=; b=U894Pof9lDu8ohu1Xt8U8H3F9UgFpDLje+xP59LImETWy2TJP8IZbZoGqb+cHFcfaq9DJO0qg+s8A1OUSOMsNqL0U7mDHv1I1fJv3sJPG6ZaXYlgalNudk++rzXyKPP6YvpdFQHqpi6cevHCcseLEKGDSMWWdrCd5crMe5ldSXsbHbVrP7+nqOFmmlfMURVFU1xxhcoR0fl8k+w5TQG/3Maolq+RbnxCoV1GP6vYS8dL9DEfyDAw3Z7qo2F+OrA+P9010C+pk4EysPYBVGQrUCryOa/49fdwHameJVLW4a6+f230zA1tHWIMgrdOJGIttHhyrnG18IDG+pUyPUqo5A== 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=pJEnKe35tbWpzXhz/4/6PxYmS6R8IirgNIvg5EWAmQA=; b=MyCeV9/B069RO2i/4u/iSJ16+bEG+aum3GPjGD34bmHA8IEd1LduRJEkY03EtEgJpGo83RBjT3NGAOyM1kDkZ5+SXqs8X7uysz6KlSwgVE8d5kwJ935x0k5TCVzvjz1VfNdmbZMRmoeMtNsp85Zmw22AAcKAxoCeACSR6Gqo1zA5lQc1KPPJDihOZf4apJS+MWAYIi0+wrThoQNcgvf0HmX2ChDPJbb80fO7vepz1hK+kNP4UMvUdvBDQ+4T29zfi8Zs3vatLS7pXLXPlm9Q8E1Eg8neA8scnUdk+FTYVbJd6RfLSzdA54U3r/yqTKfYUFx5yR6N544H7SOTe5iimQ== 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 IA1PR12MB6625.namprd12.prod.outlook.com (2603:10b6:208:3a3::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6863.42; Wed, 11 Oct 2023 00:33:29 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3f66:c2b6:59eb:78c2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3f66:c2b6:59eb:78c2%6]) with mapi id 15.20.6863.032; Wed, 11 Oct 2023 00:33:29 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Michael Shavit , Nicolin Chen Subject: [PATCH 12/19] iommu/arm-smmu-v3: Put writing the context descriptor in the right order Date: Tue, 10 Oct 2023 21:33:18 -0300 Message-ID: <12-v1-e289ca9121be+2be-smmuv3_newapi_p1_jgg@nvidia.com> In-Reply-To: <0-v1-e289ca9121be+2be-smmuv3_newapi_p1_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR16CA0058.namprd16.prod.outlook.com (2603:10b6:208:234::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_|IA1PR12MB6625:EE_ X-MS-Office365-Filtering-Correlation-Id: 227a7608-1f02-4c45-8741-08dbc9f1af24 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pMGm+AVeb2uhpstDnRSPVathqK962dhP3O2U+ZQ+VwoBFYzrx0akHD8CQSztGQco3eZVjyofRUky3hOdkkpCroaUbzBzJ64qSxb9WhqKwWzMSqI/fU8foiCRl5S3V5OLNgzCcsJhwy9X7/JJhm6uCzMt1EU+S09xImGc2Dv0xpr2P8g8uJ7n3KDBPyZesI2nLtFLt46xEHDnNnDUB1O7zB8VF1ecPU3Rv9c1NnvNVoDb7ne/ciCroax6ZkKagGCIXwJduc7nSZRYoERQLcgJ3aM2XounloP5Oqsnhl2CB+5JE2pTQvETuBNiNgVjYIqMaBj9d5me0fktcY+fO7Xrd7nQzCojqk8t5S1pxMULhuqr8sYXOju0nxkSQpMSeavAmd0S4/JNHmfSFzrbjBpgip7TDDym9Rt0PcK6646KSwibWNKkSJn+SZSSy/8NAlTquU88erzxY3hwTw6nyZph7LGuxANsuZVTGRVF2fklLrxhaFjxNjoacHLCXhvJWgws6DID4k/8vzAzkhZQDXK654EInkEjHhT5idJHCD+YI31MwAmagTZJqj0aLGMb6iBd 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)(396003)(366004)(376002)(346002)(39860400002)(136003)(230922051799003)(1800799009)(186009)(451199024)(64100799003)(66899024)(107886003)(6512007)(36756003)(38100700002)(86362001)(26005)(83380400001)(2906002)(6666004)(2616005)(6506007)(478600001)(6486002)(8936002)(8676002)(4326008)(316002)(41300700001)(110136005)(66946007)(66556008)(66476007)(54906003)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 052nL4YJA3qcdlPIAqiMJr92XFfOccimhqfK+dmGz39ExcWmTiYb10/jQexd7kx6vn3pzwMM3zAdUrYtXCMcpBhwpIA5n3N6MbdImpt2Spt/NN1CxHfoSdR1//h2y300lF0gsb+VchKHeCXuy0pspOvH9oelLvXc5jGn+N7yoEfiu1WJbZNEvL+TESA/ZfOxAbPOZNV0pBlc5c7/k/3pGj6gV1ZXt2VL4hzypZA4mEyAjEKCBBKh6wUisklS+xuLjdZWUYAiTIiSwxJj7YrluS1UNonCS2fzyIJ5R8aKEUvN/8L2uBPl4TEfGWfg5wqesMtxFkQWxBa0B4VzfRtu6qoGMazzl4YUuMuiV6dpXpdbfg08Y8bG67cckRaoRDARUmu7lBnk2jRjvi+zAl6WcWG/Y1FlGpXt6GRlQ7ySAldI4aqP6P32PufYmCqj2NMZACUsUEOU205yBXdvqnobjdFJXI6YoolQqPilF9z2gYY85NeAcy5v9PvfOjgL1tkEI8XJ9YTgUSppPoEBK3ADZ8ETejocFo40DN7v6QmpgQdZVEyrR6klIuoka5r4lkYbodRdmLBINl4kawSTkTEEfy7aEOELOuhDQcueqDrT+k7987RJ/hIwIITPFWiPA/DWRFZMEvu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12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Oct 2023 00:33:26.6989 (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: iq3F9it+PY/mEYB8v/oXwgfQg6+ps3Egu2LulBqgF6xHLHscblSFL81PicxjUFiC X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6625 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231010_173345_684437_81233AB7 X-CRM114-Status: GOOD ( 20.23 ) 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. 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 b84cd91dc5e596..540f38bb44873e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2470,14 +2470,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) @@ -2554,6 +2546,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); @@ -2563,15 +2566,23 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) } 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); + 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;