From patchwork Tue Jun 25 12:37:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13711094 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 99E6CC2BBCA for ; Tue, 25 Jun 2024 12:52:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding: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=WtEi5+8vA/edyn/3LkOJ4tMVBbAgVrmh1gPHsAWFYAY=; b=yNwVnL97DcMmbMqKtelOI0j7AC CQm8zgqMncAhnrRCQO7eUYDlqWAc3VZ9aZW6twGiZ8ztJT+HPQk1WSiBuqBfT5dHW3sOri3mDjBUU oFl4vtXP43sv9OnSux5Jcv9Rx9s/Jdw4Jck5awA4jkFlPRJcmhvNuHC9Bq71wzUlPdq+ojhakXlpM iyrv5K789pr+EJraIo3j8ydgXWiay5pUL+Qfk4MgPzio6v9BC/ZH8tFziPrSVQrmDOXkyNdrCSMn1 cga6gOWyhHKbOG0zuLQQP9TugWM8EuGLJWIHWOmuwouGtKQBv7vDE7VBkQ377CCkXCGxl2FMKc9dg rDSV/6Vg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sM5f7-00000002rDZ-2Vg0; Tue, 25 Jun 2024 12:52:41 +0000 Received: from mail-dm6nam11on20600.outbound.protection.outlook.com ([2a01:111:f403:2415::600] helo=NAM11-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sM5f2-00000002rCp-1Ogv for linux-arm-kernel@lists.infradead.org; Tue, 25 Jun 2024 12:52:37 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YIG0yxYflZAkXSOtBOHS50iKtvyxMLEwWRlAN1mroE/w0xXEWiGp463AGQkRwDpQ6q6lLSX9rdZoIz118U1VyC1Vx49yoFhffpQmnsGFh40tKopkX7PVcHrPRd8sZad6y7FSuUVWiyt64xs/AgftadH14R5RKYhXgemrSrKOZpgJWoeWG18PCelXXVq+b0Ch4UmrN85rKS3XIz0WrZdYdeKfiL+U0b0ue/y1GEvURvBCqNF9vqBMoPpkYy1eCsPmV8agUX5k6RVxC3wbjpQ7K/l1Q73aEnvc1ybsSGcVwbQ79PkVyXI6/TPTz0sy4JtOO7S0pJAkXQjXglXbpkZdXw== 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=WtEi5+8vA/edyn/3LkOJ4tMVBbAgVrmh1gPHsAWFYAY=; b=cV6nsGNw5ElT4f7cU8XlirdknoP8Wiv3Egg4LebsQ+qPv+OhhWecM4BS6cOHUiOQLAYn2rv4u+NaHOoK3BljdtAodJWAZK+EXeurYjmXGrYiHfN/aEvbvVSGXxyghiXJerMC23gpPFZY5lqQnjVBi3wlk9tLSLF6IAM8g4tSsykVS4MQeRVwI8x1XUXkyI+/iiDiEF1LmFdsNTGo1ONjWZPu+AUHYMNzhqi+hk9Ijqq45OVxM0WaZn4Y+zF5mwMQ5oTOLTX1uEIyE1sORXd7g38e9hq2mg304PXfjIcUFFHLHyCKJP1RoEhWHrR/OeAVk/vxz59aLA/yqqYX+qQccg== 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=WtEi5+8vA/edyn/3LkOJ4tMVBbAgVrmh1gPHsAWFYAY=; b=aYEjyiHDPBogbmfI4P2sVJkmVmxfKG4qVilNOOtJFLD6PDKZ8TsY74jGcS36VFAVMBqA683MoIujRGaogMrYWaGhetZl3TxqrJRg51ils82X/2ckoTv3SaN1QCoXE6F6a06YeWac+xzIQa6rWsB9OOFbeKYEUQfDKV8qOE3G6nUnwutK2yEZ6yiyHFPfp64OycfdoWeZUhmiHnafWOoUVj9N8ulPP3gpeasslBlPkJ+J/tAgJcPKwUzd2Gwsp/fUXccvHCA3rB23GJCyVeecOBj0VIicJ28keb2NILylWKYCO1uoShBqRYarWNbS5ZA4eJC2/2ew2wue5nanrRkxZg== 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 SA1PR12MB8946.namprd12.prod.outlook.com (2603:10b6:806:375::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7698.26; Tue, 25 Jun 2024 12:37:47 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c296:774b:a5fc:965e%6]) with mapi id 15.20.7698.025; Tue, 25 Jun 2024 12:37:47 +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 , Jerry Snitselaar , Moritz Fischer , Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Shameerali Kolothum Thodi Subject: [PATCH v9 07/14] iommu/arm-smmu-v3: Thread SSID through the arm_smmu_attach_*() interface Date: Tue, 25 Jun 2024 09:37:38 -0300 Message-ID: <7-v9-5cd718286059+79186-smmuv3_newapi_p2b_jgg@nvidia.com> In-Reply-To: <0-v9-5cd718286059+79186-smmuv3_newapi_p2b_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR13CA0034.namprd13.prod.outlook.com (2603:10b6:208:160::47) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|SA1PR12MB8946:EE_ X-MS-Office365-Filtering-Correlation-Id: c3a430ea-115a-49ca-bf43-08dc95139dba X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230037|7416011|366013|1800799021|376011; X-Microsoft-Antispam-Message-Info: IIeCOJ1bMtWyE9Y3VIhHi4eI+YY9r4YWopAp5VAxd7og1xkIwFfPwEcO7GjHTbAJrHGW9um0iwcAw9RyudX6rGro5kGupRcPRuxWgrB0CXP45NKHIs+Z8ZlXItUa9ozRYD3/YNLC1BewmmdQ1FF1NPN68pY5qS/ImQH5UHGPHyfSQkybIMm9Th/W0coLdi1YgwhUfloRMd70ABfQxfyDcEZ41afZNQgAyWAfON8L6ZN77AYY8Y8/YCwrNx4R2AN2P/PloAFpcB18zHz/WN3+MCT7q2Qcp0EuPWU0jRAPD5/Z9mHD3TsjVEWak+TMRm4EKLJmfY93LZdaBdU6CSyspUno2j3Yfc/yd2s4MiRYvcBhcWrcPrq+fwUAa46GFHx8GgswTzkZ0fThDP3TtfEAkJPFtBdKVBaq0z+5hs/1aZwacFxaXI+uG3/QxQjTALqDMDw3Wf5ItZOx09WDQXX2Goa3t00IRp0S9TRJANp7eb83pnr2Gj7IWvGIDG2J8wJi0eOCL7t0U4TkfqP+DACb9m8zYeGb16VkMyEg4cvBtpOY/n/WLemCgYv6ZY5X9PTEWarRMZlUhwDQ84C8GzC+w1IFIoc+OOwCzN12qYg/+yuNirg8H28GtBdpSLeHdNhq0LesH4gWE/SnzIgiNs+3Xhmo35oEkMKTRNr2IpfGN+9Qpz5VjL7l55rP7S0eXtkJhMs/Wy9f8udP8GTLXDvvdMQ/3Af6lVRtO93KfnK/czls4TKXNhxdnj5YIxlZaVMYgRbYy7YrEzCPhIqyWz8OJRQ3swpMDQ+ny3LDOOU5AWNhW+dZlQzChGrhzy8M9551e9jmySGza0v0vaNIcdqbI//eCBSStsSZQ1gKZGJHM7tWeR+objDKG2HUQLrIDrd8WMv8M33Rig/IYwGmADHWUtzo8f1N0xXmwPI4ZZ7VpkHKMioRYxbpxxAS3DDAg1nxeYi1sIERkK6MYU/HicvAIw64oSiCDvV/LBQdette4XC161y08tk0OvHtZ/pnOTqcdPiHcxTKIpzwShCSUBzZt2CDwm61DrPBEYz8nvbY55Ix1rqf4lHmSm+uXRv4RYdW9i+9WWrau9ofgqR+42nxkUEWE4bEhDRoU82My/6ent3day23Dw7ZwCSq1ORlBHThy2+VhHhfs0LPitGShXzyPL65zrdTXfTWHQp62NU+sBpVaeBZnfNwlJb/PViPAY4g410xsEdPoKpM0NNZWqYeGhQf5CoqN5wZbQwA7yF2eIw+rQXUOavHuzwTV4MAj6rFf7WBWThZsUSXvwtmfzjZh11OI4JlP6XuIAb0A1cDpCs= 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:(13230037)(7416011)(366013)(1800799021)(376011);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ut1H+y6iEdKMbewZnPMRI9tQJDm8ShB2tFa2wGa6yXukYPuAYojQFDBfsxVwjHnAAKy9dROXoCFdSc1nAIwRcbTQ+4ns+did5JOXJTAtgvvihyQhIIdq5ZdIIXviHIsWM0IFiPNA6J9OUOQGkgTbKp3COx08gtoidH2PwKREn2Xzm1hNpwLtFtK3WGix6OBCHy3GA5YHyfk5ilTHtuv0kTgrFaf1sqO2QDD9o8btDie/aUdO369PpYsVaQzF6PzrzH04SkUYk0PfN59M5CXFanVebmsxNIu6yEt8WcDIkQHHZkqAx27AtN5na28n4IXT4gDeqds7dQuGBQCvccF39CgisjGKbswpkLqDuLewTh3lr9jWUY4jfeYFDJT0g4/a5ZhvhYUfwSUApnsCp41IgpEkcIiunoUp6jYzvPX6ar2Oewcjx7jOklJnbqadkte7vnOrfx7kQQw456SIq+/OMIlnCZgsKVor27InTGFByEkrr7KDtjrSzFKi8/DBg4/c1G9H1RAAm4CfNmxMsI/ZfD25RPWCqnqQUnKOSskWn/kKjunWFbtWvUI3+k9lYcypVp7SRt/FP5wFCuL2DiAFLF3Hzpzfhs5LosItQoTap6elr5C6vZuUVewrvJ2275pz6PHzxt6grazgbc2wbHWw9WCd3ZIcik+d4HO4/M4NJSggNfKfHZaqDWMfcVSOstzDaRXnjgmyR+Ocswtf+CDVrg6M/oPyDXiL1r73eM5QlOa6cnSKojJSclnIJ1LzCunNg2DqGe+XQCqUQRyR558dasZJVFT1i2ELAfe2dKdmD9nipENj6+ryPtYqO0XTkZ4R877nMVKUC8geORSOtdOq66vgKJVzqdrEqZ6m740VEyH7YKKKG6xiO8w5OGfTpu0uXC6Ty4biqRuc8nNUOPl2T4rJagmXAI+5TtEQRQtbmUFjCTKzYEFBa+gi3s+cj5W5UD++N/arTViMrAV4XG47vfncznET1kRUDsgf+YEFBMN126jfF6JcEzaB/+WfMc5H6BOsdxOIKYZHrgo5ZoxDVNx3yhhJZ0PK1NYGA3vK5JZUqP1L9hoJi4f/PJqhTJ2cZ+9Jlhdu7DWkwWfUBYXqqXugjmcBPAJBVkIL1p45VN1uNwFaTGWhiZr0AVKWJhoP9ZcBh4Fu+Jnm8q27yRxxH1SFKMv/o9d0vz1F3BUJwSINy3qosTMB6z19Lcss+F54FHZodnazUQAYT5sKh9uVcffTz4/YvxlZQr15Pmbj4/3hfnrsShBc2fnt5jUSDAYSa4pJQ8SxYnVh4mhYxZ6enUmkowmRzmnwAvwuXn5/GxQ1D/HLzsQpphpyhBg+CjP1QtyqGeN1P2WyW1HigtslrAhbmsydyhFyLsYmSRYIZxZSVLucFeXibcJmX/yJdch6wLE0QSaFetzWfWqfBxb10UjRqt7QOzgt2/Hq2lpvNLzL34FSTH4iYCZ9GeKFOXDOFOu3JPlxgJmdvvvruO1ZDH8IAyYwfE6Ie3kEvLHzy3ngXvEiKki2yIUPxcBtkb4JVV/vfbXRNQL9isJf0bHmhqyTQHYii3OVOZzN+lm3z9orve+woNtoiVedtvPZ5B9A X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c3a430ea-115a-49ca-bf43-08dc95139dba X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Jun 2024 12:37:46.3933 (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: 2a8Dz9WC78qmiP5lJQIm730qjc+l8FeluHFG90K/nNqn5xEzVpHIglPliCUsFXcw X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8946 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240625_055236_391930_1B36DAFA X-CRM114-Status: GOOD ( 15.75 ) 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 Reviewed-by: Jerry Snitselaar 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 38d1465a44b479..f6634c37601b89 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; }; @@ -2663,6 +2665,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 @@ -2710,17 +2713,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) @@ -2732,6 +2738,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; @@ -2829,6 +2836,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))