From patchwork Tue Jun 4 00:15:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13684498 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 42CE7C25B75 for ; Tue, 4 Jun 2024 00:16:28 +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=z0uzfHWO+WAwdxAPJzquHXfq+emUnAlQIR1jrZLrqXY=; b=SM8y6gE9sQMt+v SF2CcGsEGTILMoEifw/H4quOJoG1fbyQHs3LnE7pjVq+PJL+3T0hrAhq8Tkur+1alATd671PvgtGE KsteGy6ngX1hTTCTbHxzuKszRhPx96p05L70YM62zc+O44GGfoiRSRQS0wBZdJPKWTEZRJ04/ihsz kqipMMTWlgob7ZESKK7UcOsbvAY++KO++o0lcPhuaRb3bGSytJvbMMI8y1/JosVxMwdim4NJNVkT/ BwSS4R9CZNI7a3bMi+9iLeBDGFfR7VFMM9fFh5pKq5tWpOicL+f3fbs4gPTTiMGEF45S4ADocfSUc y/mC54ljNPPrwNi+OkBw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sEHqX-00000000hZV-1Ezr; Tue, 04 Jun 2024 00:16:13 +0000 Received: from mail-dm6nam10on2062d.outbound.protection.outlook.com ([2a01:111:f400:7e88::62d] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sEHqV-00000000hYg-07VX for linux-arm-kernel@lists.infradead.org; Tue, 04 Jun 2024 00:16:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jIr6DmEjSa0v3eTC3mpW+yiALalUy5Wlw692ZOyURG2FszQdp/CiWPrBvaGtgteLNrxoxLXaFmdLx/5/Hgo+8ALAkQay98fW8fACd+PdMsNGcmf0yYOBfVDk8y/MMQZHLaTEQvCmJXOEUQv7g+AI1DiIidxUKdpVOLM7xSqEqP4ralnnIauarNS0+S+1I4hX2blWB7VQFgq0wsT6BdWsrMnr4aqrNIQmu8W571QzJDDdOIHg7g9XwuDTJm+H1ip+Ytu4r3fIovBZK5lkl977Qtp/B6D41wzCwti78KuEVcIPKD5nIPBOp91GEayMdP4w6Xrw/gXjhxkBSPIKJOzH2g== 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=avdBgAJxEmMvTklD4lK2mlU8xg+oDLKwffa5vs/jyok=; b=II3feL8LkOdrARLpvBbziVtaIjDjIwD1FPefHEGoOw+QS89fuklwWdXBZkYYr/qAhyLf4WzNPjMzjC294xRAacM7UlkewhdcuxVv4q3CLq2XuwfnqOzXw4KYmZsARbHFkHYXvNgswTcg7Xg3vofBZHGZw5vE/gJ3gliujM5H7VCjszN0nhsAipt5e9S1+t0yC95WTx9S78DUArBWG1W1EOARQIE41t7WsIOst4BhrCaOv9OWmyQs2X2FSXNVwPnmTs9H4/LvdjXo2gKoorVOUT5yZ0hjZnnUG0Ano+hp/SOeUsMekpPJwjoU7mhJMynhaZSY+2dYjhiwLPmpMmFOog== 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=avdBgAJxEmMvTklD4lK2mlU8xg+oDLKwffa5vs/jyok=; b=jqeVHnQKOiUwoJ/SBcf0GTmJ09HfWzG/aqcg9vNlSJp0LOz6JUkP8DFaJaQTkBE9McoG2yJUIH2m0cRso2nj6lBaCaGkWcjfTWdgkevI/X1babJtwlx19p0x49kbQC6lP3uAOa3R/X9Vd5wHE8qj6P+JDc67eU75wudRzJaR0YFRDO8jn25CjkfN307vNqw77fKfODmLo7m6ZRIlXSiDy9xdiO4VlFf6aUPtpl/jmEisvV20I07Z9EtcWzMJHDXOtwbSqHUkyFZETrVdYnf/GZL1kprgv93JrTj/VwXeFUBsSBA4tl7x0zX50WgRNmBdHUuh4TgNUXC+7+vy8rpFcA== 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 MN0PR12MB6296.namprd12.prod.outlook.com (2603:10b6:208:3d3::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.21; Tue, 4 Jun 2024 00:16:03 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%4]) with mapi id 15.20.7633.021; Tue, 4 Jun 2024 00:16:03 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Eric Auger , Jean-Philippe Brucker , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH v8 07/14] iommu/arm-smmu-v3: Thread SSID through the arm_smmu_attach_*() interface Date: Mon, 3 Jun 2024 21:15:52 -0300 Message-ID: <7-v8-6f85cdc10ce7+563e-smmuv3_newapi_p2b_jgg@nvidia.com> In-Reply-To: <0-v8-6f85cdc10ce7+563e-smmuv3_newapi_p2b_jgg@nvidia.com> References: X-ClientProxiedBy: BLAP220CA0005.NAMP220.PROD.OUTLOOK.COM (2603:10b6:208:32c::10) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|MN0PR12MB6296:EE_ X-MS-Office365-Filtering-Correlation-Id: 2300a7e2-0261-42bc-581b-08dc842b849c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|376005|7416005|1800799015|366007; X-Microsoft-Antispam-Message-Info: ECFv/XVQXazhb3CvaXKV8hR9eicKg7cd9scOz9wIFD3eK4IeaJE/GS+KCzPbbvr6grfgjBldEaVsM+YVJGHv4KYngO3UyKNtE++tkZCkmzIMRotPGrQie0hL+VOtz+OeZ8ULVb+O8aLXeaU+Lgs5nsNOUlZ0xUaPR0tEwVz330zxKTMkNbjwNkp61lIGp0R5UGSHYCA8tvOXsFtq+cJ6LbETc9F799+Ih9Joi0uAEu6aaZzxHXQjJZfC9G+9Lg2Oouz9UgY0XxE4rojA2mzp/4nlEzKJtN6eZst+ry/WTRkS9Z7lAyKy9wD4I1DuXJ7tzciUS0GsGD2hMzlcvgrKTU7epUXFPvwNL4I4lPLINkJXFLA7L3QNGOnLHoX6aTeuH6ToF1V4G1EkQ1B1V4m5iPkB0vdUOUa+Mqxig+KtH/CBtmWyGSjnQlRxXW6iJxontnpDfSWeAnA7vZItR4SD4k6RJlasJjabfZmVCZ3lHVQXu4wcaT9Pupmfb0akYMp68DWRHvJ/xIyzRq6noH43ELv7+WF4O885B4LKSh4SoRomxOcW5/EBL/tkMhvYIxtN+karRB5EQWAxRKQ/mAp7tvJT3N6+1tp1AvLnVAlNKjM7uw7nKBGCecZaFvlAws2RgJuABW5goWFwD6IlI45opT/bpEbOq0eBEx904Fg3MEfOK612BgMCnS2PWF+oGpNEsnHoWGaOxUme0nO+HsNITqPf+uMC6S18oMYzyU3aOgaENuHD9HlEhjQb+OqQ6iI6WvqZSu4AVWdeVbUwTVPNVYVqquLNTw4uixeQ/5D+4fwd9nsDvDnF3qz5GpOYqOyWcpMl2TVOvKHjEWRyPTFpoj6MV+KA5yJ5E5b6gGrZ8RgcAK+tOqjB1wnklWGWzIvONDONlN1/jx0WqVWXhescxF/moxK8VxF58BVNyxw7jY1B+tXXYpygoCgpqJ/wgGaIy3fV0GO6EISA8HgCwaVWbLtUJWaieBLpywKdc0Ki5a/vNcGVWbzWkldInTYrPbrukRmWAk+vm/yJS2JbpkTCqewsAtki7/2NUeJU68KlBW7awqjw3W1tB4Qij/BXp63qIauYwewqaLMfXXmLjNjvqfJb7sodtn8iRRAJ8QbINq4J8oCWX2e7CFVXe9HyfG7IoHVntVOkrbuiC8vSGJvR5EyN9/JRmzswt7k93VjUPeNaFKLueSiODb6wZxOZRrfMAVsvhBollo6hrGRPE24n6gkIRqpDEeR3H9JUWNUf0HMFOopOuHMmEfo17AqUSoD8zQQSjjriHKSFiXANhqJ1/w== 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)(376005)(7416005)(1800799015)(366007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-Message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jAUfMN0rxUDYC44lyHR80XD+TEjmPOgCT61NBxFT6KRA/oVUOG17GaPi/TVcMSA9+NNxnfW0PX4jBpj3u0T6nh0jtEfOROG5bKHuRiyb3iNcuZUHV4qOrAJCQ0XQNghhLJS3l910e5ZknvfEMc7kyLm47DfaSpsQJ5jYpfl6M80jpfrmrGe32bwJvcElLYM+n8roS9t0eIX/3buGM1pcZC3ja1GW3QR6oBH+di23pZ09ralSeD5PO4kQoPB8ld1ysOX7mYwGVKR6QBKIUPKbEMENempZbM7M1pfIIWwqJohjiPMMk6tfdqXbrhU/e3X9Q8ZPuAwnFEsa7TuuZUF8giAwGi12o1DcEoBjSvrIc1HDI5YWxcs6pwwtRBGxUVTxO2Rwgeb6RegNY/vjKNAM81EvjNvJY4hpsTH02Gh8I47aiLgEQR3E73eFxX37r90AtzyCrTX4dyGWbqwM+JhwkKzLBW4X/hvsIxLogjfXcgDm2vukKct0o29aPizWkClqFLmiNd8zwcMtl5vIOfvujM7jSvQH9RtJU= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2300a7e2-0261-42bc-581b-08dc842b849c X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Jun 2024 00:16:02.4201 (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: nyQeUAoxyfwT+C23oGr1IXYjH6Vc6QblIZ+k56IZr0Jw/hO5Qvj5EvCtDSDILt0q X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6296 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240603_171611_171972_329104BD X-CRM114-Status: GOOD ( 16.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 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. Tested-by: Nicolin Chen Tested-by: Shameer Kolothum Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 26 ++++++++++++++------- 1 file changed, 17 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 e7cd1ddc03517d..56e2fe52df530d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2005,13 +2005,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++) { @@ -2494,7 +2495,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); } @@ -2581,7 +2582,8 @@ to_smmu_domain_devices(struct iommu_domain *domain) } static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, - struct iommu_domain *domain) + struct iommu_domain *domain, + ioasid_t ssid) { struct arm_smmu_domain *smmu_domain = to_smmu_domain_devices(domain); struct arm_smmu_master_domain *master_domain; @@ -2591,8 +2593,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); @@ -2606,6 +2607,7 @@ struct arm_smmu_attach_state { /* Inputs */ struct iommu_domain *old_domain; struct arm_smmu_master *master; + ioasid_t ssid; /* Resulting state */ bool ats_enabled; }; @@ -2664,6 +2666,7 @@ static int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state, if (!master_domain) return -ENOMEM; master_domain->master = master; + master_domain->ssid = state->ssid; /* * During prepare we want the current smmu_domain and new @@ -2711,17 +2714,20 @@ static void arm_smmu_attach_commit(struct arm_smmu_attach_state *state) if (state->ats_enabled && !master->ats_enabled) { arm_smmu_enable_ats(master); - } else if (master->ats_enabled) { + } else if (state->ats_enabled && master->ats_enabled) { /* * 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_atc_inv_master(master, state->ssid); + } else if (!state->ats_enabled && master->ats_enabled) { + /* ATS is being switched off, invalidate the entire ATC */ + arm_smmu_atc_inv_master(master, IOMMU_NO_PASID); } master->ats_enabled = state->ats_enabled; - arm_smmu_remove_master_domain(master, state->old_domain); + arm_smmu_remove_master_domain(master, state->old_domain, state->ssid); } static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) @@ -2733,6 +2739,7 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); struct arm_smmu_attach_state state = { .old_domain = iommu_get_domain_for_dev(dev), + .ssid = IOMMU_NO_PASID, }; struct arm_smmu_master *master; struct arm_smmu_cd *cdptr; @@ -2830,6 +2837,7 @@ static int arm_smmu_attach_dev_ste(struct iommu_domain *domain, struct arm_smmu_attach_state state = { .master = master, .old_domain = iommu_get_domain_for_dev(dev), + .ssid = IOMMU_NO_PASID, }; if (arm_smmu_ssids_in_use(&master->cd_table))