From patchwork Thu Jan 25 23:57: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: 13531924 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 AB0C2C47DDF for ; Fri, 26 Jan 2024 01:05:03 +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=Mo9Nhn9BpUkdcQvsH9eYkMjQ2Tye6Y59F06Fvq3YteQ=; b=qneUo/kp26pJTp v6ck9alBTVnl57eNmuSkFC9ypjJli9y79XHWtA0DnM0bMPqAkebdxHFmhkKzohNx8iR/xLGW1f02u EnZsq7I0cq2IHFcgZOnIr7HjinWUfEVLrZbDRls/uc8RKZvuuEotfK429ubizdfIxkbqevmYFkN/E UDV0OmfAerNZ6LRVNVuIRw6aaCKltViM1W8r5PM1rnKLfMw/F3iH4ZNubSs07uOXtl3DWnq4699Xt hHaS2QvHf1WKUQg4VZ8CRllD4evkKL9iuUC7ClvSeUB7Hvs0+e7KiMacaKm0TtvFxceLa//FwDFLE SSQdnPeS3kBtZiQFCpwg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rTAeI-00000002hQe-2XHu; Fri, 26 Jan 2024 01:04:50 +0000 Received: from mail-mw2nam04on20601.outbound.protection.outlook.com ([2a01:111:f403:240a::601] helo=NAM04-MW2-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rT9bP-00000002XEl-34zk for linux-arm-kernel@lists.infradead.org; Thu, 25 Jan 2024 23:57:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SsDzhlcNeF2GPRI2Dhwy6+2zkOIFctG6+tlshNsf1G6hqCyz9FN+0v3/AW77WzQkKCmoEYduTaV3Aoj0n2TnTKXg365r5qGg7tDYGjtB8BH7zO2AW5/n9MdG+GrSrent92dN4GRUi5cpkSpMinAiYJU3jf1ll9AVkrwzN4gSRQsvF7x7woMTb3Z+ehoHMQRYbiplCgBppyYfo4CHGB1mtM0jNGZgC+fghJiwQjvXQaPSsSMKAJ68YvIhBtE67s+KE3sRuFC7kvBuxwPde71Q5g0pZ+XUPodHmlPZ3t89HI9uhIEJTu9+UbfPs8Z1N6QybydnMjmlTRVzKMJ/px8nZQ== 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=nay4gjr6wba5e2ti9n+Ho2HOVD6oZCV///UMW2Nm2sY=; b=mu59LPneSTrS/8SFvJuOuiAeK/EdU+KFFeHKVEAa371831GNesUjlKdgX0t3h7Z0uPp6S6J/tUX4x0PFkYltYCowMFINXbeEnlJ1d63bntRDL9OkDhPIY/ZrdCRGHsY2FMOS80PZy3cuusADlXLrGP9nNPEGwd/LojBA0SlHCo9KVURoBwIlRN102MawXH8crCjmxzZbVTit12FJljydeH2L8qBh1B/ylBitEwxZrzQ4WfUK/Vy0sah1q32kvm/m//yRm2TdwsNU7EoVq3Bj+SV9ES62n4UGzL23OUuB6k/9Gn5OHkISwv1gdiLFKy5Zlgz3PxQm07N9nfVBc8JkmQ== 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=nay4gjr6wba5e2ti9n+Ho2HOVD6oZCV///UMW2Nm2sY=; b=RmMai96XVEIAlTCy7ul87rk51CIE7kzdfKR0kV6DqWS63K7GxBdISXI2ESllY4/Ev08nNigb/6oC3eXYh6n/6PA5rp5xEkvcnNYUecA389UbKdBmDPlcXdiNIa4+yJFpnD5VLBRHLcZUTR0Pqkz4ecT589LvoWkPjFPUCCI9C3SURiPFGymx1NwenbyX38Ynk3iwu6C5HOBPoPM6sp3QBqNaBa5zjZM1PnSbudfZfs89Kz54LFeGeM/5JubPhBPLX+g5LCaa6rUN3PFkrtdlp+30VRM11ddJxItWqRgX70fYEhDMtQs97+XiZkWekCpkrJX6q+/mS5enSk23yUnQ0Q== 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 SN7PR12MB7881.namprd12.prod.outlook.com (2603:10b6:806:34a::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.24; Thu, 25 Jan 2024 23:57:34 +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.7228.022; Thu, 25 Jan 2024 23:57:34 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Moritz Fischer , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameer Kolothum Subject: [PATCH v4 09/16] iommu/arm-smmu-v3: Put writing the context descriptor in the right order Date: Thu, 25 Jan 2024 19:57:19 -0400 Message-ID: <9-v4-c93b774edcc4+42d2b-smmuv3_newapi_p1_jgg@nvidia.com> In-Reply-To: <0-v4-c93b774edcc4+42d2b-smmuv3_newapi_p1_jgg@nvidia.com> References: X-ClientProxiedBy: SA9PR11CA0030.namprd11.prod.outlook.com (2603:10b6:806:6e::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_|SN7PR12MB7881:EE_ X-MS-Office365-Filtering-Correlation-Id: fe0b80df-bbf4-4184-1809-08dc1e0163e0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8IDKs9i7Is/UNNW8kXHG5GB+bq8AiWH3+jqJv6Vh6JSA6QWKd4DyhjFtnQP+3yxbRlDxj2c9TuZY2TG0Az5wEk/CBxfWFw1009rZcD0R0rUTteatO9JNxMukPqpJSsEEIBvGjTTnbW8+CkgIMW009fpNZpUYlpJyqM65arWxlfPEyrXT+q57uaJVyYC52al2VT/UdUt7dDPRrIo+r8OPyc/blsVMOPmZ+km9Nguo7E/2PHp4z7Wrv+8oTvQjsqnW8jXHq/5ExExg891HAp6ts4A47Crbw2V2M1OIfGfHYGiOu4KwasbmYODUkWkndBVmIbx3KgABpwU+F02/1eblEVxPVumYHUOQKGsmiqh2LZs9Iv6FnS+BAmLyyFaM+ihuiM3WVKDxX/yy0E67ngmLrjeQhtWiD7a5/FoZBdnA8Q9Sr6nIB5/VwHNNjxZFM6tSE6TF1GqbJ9icVkrIawQNm3jWLKIcm73N2XEG9LqdgkYUSqm7jcqJBeg4mmM5sUP/kwpNCz5GKXLaI8xdh37UuhWyeIDM6w28O7fuTiOQv3puj++80b/JqxMvFYk2bUaOsscySrsXaSWwsgAEC1PDuKqdq3dRRiFsf6tq2vj01pI= 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)(376002)(136003)(396003)(346002)(39860400002)(366004)(230922051799003)(451199024)(64100799003)(1800799012)(186009)(83380400001)(36756003)(66556008)(86362001)(54906003)(110136005)(66946007)(66476007)(26005)(38100700002)(6512007)(2906002)(2616005)(7416002)(478600001)(6666004)(41300700001)(6506007)(6486002)(5660300002)(66899024)(316002)(8676002)(4326008)(8936002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kvvuxkt4f+ES4pfOFaqmgrfv2sPzokMl8U4pELWLD+01i0kO3+HMNujF01G082RooPIHLhUF2uxFA2J75fApzm1+F24WKTOYYa/6CzNcMc+iu36rv7djJnxLmbYKksUgV026es9DcUH9Dcs+cNfTxp3VZcxboCj+I8vDhLW3UcVpEYerr2olaPyUJQu+BsvdTlTYOCaiChGH8OdBsmWl2YiHE6V358MzKez1rzX/cgk7CTExXEhY6hSJ2fbE0GMV1b7fCj2sKx+CrnJPhFwtY1iH5ZPKvmhZfkyDEnRZ2bxbemurZFX3aVxyifVC6csHVoOJE3V6/73Zgp1RzRjap8I/f/FumyDLZB768O1j3kSrcuwGzgnvN/2IgUHpy5Ucz/zdRZWF9JpzRZS4sR1qNrFyYka3kaiuofJ7Vs4TQ8X8RwfZ2aDJAmYzk6ro1s37+haEcRpdrX0E+FfJuXcz0Ij2W1kvZIsOnck7Vi0UwNh92fzANwzXHCIu2De2Z8cNOcsTMyv3Cvx50x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OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fe0b80df-bbf4-4184-1809-08dc1e0163e0 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Jan 2024 23:57:30.0179 (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: NYnoiNrHuRto8qnwsPo9TVDK6sszWNj8BEP2PwamchVe4B/GLRjaoGZ/7sc1tTP6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7881 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240125_155747_878808_626E18EE X-CRM114-Status: GOOD ( 20.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 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 --- 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 bf5698643afc50..09b40ba35a9cee 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2548,14 +2548,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) @@ -2632,6 +2624,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); @@ -2641,15 +2644,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;