From patchwork Tue Jun 25 12:37:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13711079 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 32AD3C2BBCA for ; Tue, 25 Jun 2024 12:39:09 +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=E95aCzlBZGQUhqk24tjR7TaUpY2oFlOq0aA6h/qYFV8=; b=ndQdxI0ihItf9w5md3WokFG8Rf C8Qmqkezt44Te67gj2bZyqzotnOOWQjar2W/YnK3tEgk6Cil14onzvUOPWYgRV09vKyt2OiHUcfYR sIkav/X6ibA1WdRgILux53m1CCZdKFzUEALkDc2E7NRJzCuBgunAcOFB9VcWbvOJnoiuBcSDw7M7J 4MncC2ClTrB/577v99Qim6FqaogPQKJR26LM1Xf6gVpOS/eDMX+EFQmnRJkUp/8ggLX02LqZFRZkA ddLMK5pP0LWU0IFhL9NsICjgbAZRNY0/Lj7SqNIpwhxE7dUm5BA0IGBtVx21rGc8qYXajAR4MZVk2 ZNX0n2ww==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sM5Rp-00000002oJg-3x5z; Tue, 25 Jun 2024 12:38:57 +0000 Received: from mail-dm6nam04on20600.outbound.protection.outlook.com ([2a01:111:f403:2409::600] helo=NAM04-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sM5R2-00000002nk8-1CiL for linux-arm-kernel@lists.infradead.org; Tue, 25 Jun 2024 12:38:10 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eRc/JRkbSbffUuu36IFGwxtlU6MjPfhrO7JRodBLA1RXSKBJp6xCtYTgss6k4vISey5J317N1sr83TcFKjnR2f+NRFmHysn5N7/dU7r/trzYA7AHH/w1mjr2GP9LuVy8GxEgYhZafEcrgVyBSe73ET+80CEzCrcpaY3y08Rp2DLXdrjZFXnLjdDmX6X84+dJ6b4gpBYk2YGhzHiVG4bHE/+YnVsNhR8AdbzKPht9U6Nm8Nu/REWZVQzgDpbkkjyZvr4ODnCnhfJgpuARkEU+BBv2ovLwjNHKc8sMJGxqB0Gr9sSIdqFqADnSX8bNmugUX3WJuw+e+ULfvhTS0lxSug== 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=E95aCzlBZGQUhqk24tjR7TaUpY2oFlOq0aA6h/qYFV8=; b=UIDdbBDUDLsXlMfXEUVTaobLt79lHNn6EjjRM8igObkqr/nEaaLKjdfQpfI3Q1jNv6sygtFSt9Qz4vhtoIw6iDE81ymqzzhR+kNL0JV7rTtfA/B+ta+EIHLpWujv5f8cB3E0rHVYj7kfCQplsJsKAMqUCITIG2aO3ee0dlFvtCQrywaixu8ujIU7kWdNDE5QgnQ8CryHB4dfJz9tpOwHjC4lVz2G+DOEXbjS2pK+5902avhfTgQi1VDTQCkHaHoWnMykLUn105yz6NUO45IGScv/UczxjiZ+//W55O3JCk2WZccuDRK2QHEA6IHLOtjNmLu3ZjZl0LxNepOfdw7+lQ== 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=E95aCzlBZGQUhqk24tjR7TaUpY2oFlOq0aA6h/qYFV8=; b=e2Y9HPN3/3HSZasTmWOzzZhUrFub7RzdXwhb8WwGo4LylH9UlvrjPBdrERUjN9khXfQaBZX7/j1PAmU+o5KUFC9DXtC/YJpMJDlJb27oPwRdvM5b6DI3hEn8RkctWEZ5JzxAOAWcB4qLO2XmUSxt/hk7BPjvfxWXdk4I/uweQWkdm+S0oxOrXrbNxRbETgCDI+56XhifFTIsIwFnyPaRx+92wodFFHHmfuUHmul1btw6e0atUylgh1GGuYh9U80mWvMfJk8986CmtSzqpSTnxEe1w9jBW5aBsbwBZCQBanmlHynH7DmQPtG6U9bOXzYHvh657hBn9KGYg6IZXSF9gw== 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:51 +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:51 +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 14/14] iommu/arm-smmu-v3: Allow setting a S1 domain to a PASID Date: Tue, 25 Jun 2024 09:37:45 -0300 Message-ID: <14-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: BLAPR05CA0027.namprd05.prod.outlook.com (2603:10b6:208:335::7) 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: 71ce7bd7-31bb-4e34-520d-08dc95139f0e 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: pKHGIOOMJicn41bpaTJqaeJs5SoSFAUDsb/lsdpLuoZqdWTMD9wDl13WPv9op2LEC2Kd8UEhlMVzW/sMQ34NaLYmHYQwIjmabZxPUMwC5mGjMP2NjdDiqn+fHdqAK4vy7t2euWxnIWnIauSuJYP8ses2/wXEZrbH24Tdb5T5V9lcDiGZpcMgGU7rn6L94AeIuTPupe/D6HU5eB97fRmIPXqRVR0TMzipCAmygeYEMQknniiXBlCgLx+JLmIdVdOIHUUkiX/kI3WqM8uqspjrWhAnm2zJKIb9zakee6lc3K5LVK2cRs/usR7Kvm38QmA0zoO6fPQgTFheTJB2cELkyUJlxqBolENFsh7PRo3gDDwEIuyQ1tnKqGoibWHqWLLmKjIlM9hfvFNfTUt4oD5QpO45gPEd/EBkUUWS0S6gTNHpze35S+V+TkNR+RVrYPZJTYRI6ZbR8W0a3KJLDElR1OqxFRmxJtulvaP0Db592rSFjXzdVzx78nFIAzglje9bxYiwFhmAWbnmZfvyVotKFXYZRiALttdICvZN2tVDx4d9JhtBwl108OkWtgj9YPMffqaoqcqn4Mimk7sCV0WmVHVAV6+8YVolf2HF2SMH746JXuKyy9BBfUH2sc+wrH6aEU80ze9x2T1SRe6VO74HmwL60k2Sitz/baJMMiYXARZIPz/u0XxgCiZcx5/Hbocb/zTcnSWKR4gyKF+K7CjWRB86EWyxCC6mZ2wJZOuIEtJs8Q0haWn7KfdnZa1eGksKEy3f9KSzM/hQdxvAl2JclwEpdapqSrBntSgbIDThMIggLpoGbYBxBKYj6ZNxkdFRaW4T6gUdlhosfTUMwZr647/KvM310zzitJvq6k6OofVy8bh9lsAJKbCnQf4sKMU5IIyh30MAgMlD+JE330OtubTud7ibmiArq0qrawsfx7pCp2vapAhM6M/guk9W68VvnEvsaZ/2u1S9JQFm1bDgWa0McfyhJoX7HWWpEVQ5ZXuJN3I55Iy8/cBdbsGY9H9We7vIsE9l6ILxbVNsB9iLD5VDT2QbgmEv1VmLDrWNKcuyDIz0dCRCfoyaISvioYpgZOyXJx0BFW4TgNO/sVDHBtw2tLLZdHsGXu2yYTphWab7V5sKXv49yTXyhEYZeqWq25askjR8qY3Bnz/g2yHkYxaFYtMQ4VxxU9l6mO9kBNNgKLWTcvNXckC8KAPnqMWIoPyg43zywvv17SxbpGNZB41LOIN3J2vnm3WcDVMXWzw6ioaKI7dFxaP7dkGNPhaVk/G8+tQBDZMsyN43a6D0Ez+j7jGYEIQUbvzTUuwIl2k= 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-A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+r1Ag2v04XvQvxC/oV/N/FjbRF4WYqPMr56UhfE0uQKFtcF7X+BQ438Nv3CmGY4Rse1gOGaTo5abQoeAlgCNiSEG3CvIAZwCBz5JzXm/eoBLSAA4g3RNgfwJhNJIojW3TZhHI9JFzAftV1cJ6TC45UishG/Zbef+kzv5/dfXNrzxOYPfqAug5OTgzb8BUF9b3a/4yc8+Jd2mqRWK667MnH9WNler9rdDQcU6b8ge6b6DLpJzeSIctuzMYcaOMIlu1e3vm4t8NoeGZVLq374+zrIm2xVTkO/qggOwxqjcEKFFE1KZ5n3WKA+RT7xFPTElWx6hpY599T5eayRakDu0qUJvTW95qupw7mkfcito+wztUerjzyHKewbQ51UA+7tRYCjgf+yEp0sKMMoXglUGt3JBvGI3EYNP3kL/cWPL7XxoUYYYnGuGkakdL0aVRpjKpx3VVk1GnlTnVa+mdQjaPzUPrJYR7UuiEO0p4DMIjP7PdfPfIrsu9x8LYAvVvXAdq1D1uDIGRuZPuURfk3T9DyiwZOoESho3teeWmoEEH4AI8d3 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 71ce7bd7-31bb-4e34-520d-08dc95139f0e 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:48.6318 (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: vzFx5WzXhyqWsCHz/iRRSuVA7absWbOVR/J400Q/C7uNs5S0ZqnjIFGh6jKPrHim X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB8946 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240625_053808_459479_E5D19D94 X-CRM114-Status: GOOD ( 17.14 ) 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 The SVA cleanup made the SSID logic entirely general so all we need to do is call it with the correct cd table entry for a S1 domain. This is slightly tricky because of the ASID and how the locking works, the simple fix is to just update the ASID once we get the right locks. 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 | 41 ++++++++++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 2 +- 2 files changed, 41 insertions(+), 2 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 fbe14466f5f379..1e8c996a4be0d2 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2798,6 +2798,36 @@ static int arm_smmu_attach_dev(struct iommu_domain *domain, struct device *dev) return 0; } +static int arm_smmu_s1_set_dev_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t id) +{ + struct arm_smmu_domain *smmu_domain = to_smmu_domain(domain); + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_device *smmu = master->smmu; + struct arm_smmu_cd target_cd; + int ret = 0; + + mutex_lock(&smmu_domain->init_mutex); + if (!smmu_domain->smmu) + ret = arm_smmu_domain_finalise(smmu_domain, smmu); + else if (smmu_domain->smmu != smmu) + ret = -EINVAL; + mutex_unlock(&smmu_domain->init_mutex); + if (ret) + return ret; + + if (smmu_domain->stage != ARM_SMMU_DOMAIN_S1) + return -EINVAL; + + /* + * We can read cd.asid outside the lock because arm_smmu_set_pasid() + * will fix it + */ + arm_smmu_make_s1_cd(&target_cd, master, smmu_domain); + return arm_smmu_set_pasid(master, to_smmu_domain(domain), id, + &target_cd); +} + static void arm_smmu_update_ste(struct arm_smmu_master *master, struct iommu_domain *sid_domain, bool ats_enabled) @@ -2825,7 +2855,7 @@ static void arm_smmu_update_ste(struct arm_smmu_master *master, int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid, - const struct arm_smmu_cd *cd) + struct arm_smmu_cd *cd) { struct iommu_domain *sid_domain = iommu_get_domain_for_dev(master->dev); struct arm_smmu_attach_state state = { @@ -2858,6 +2888,14 @@ int arm_smmu_set_pasid(struct arm_smmu_master *master, if (ret) goto out_unlock; + /* + * We don't want to obtain to the asid_lock too early, so fix up the + * caller set ASID under the lock in case it changed. + */ + cd->data[0] &= ~cpu_to_le64(CTXDESC_CD_0_ASID); + cd->data[0] |= cpu_to_le64( + FIELD_PREP(CTXDESC_CD_0_ASID, smmu_domain->cd.asid)); + arm_smmu_write_cd_entry(master, pasid, cdptr, cd); arm_smmu_update_ste(master, sid_domain, state.ats_enabled); @@ -3376,6 +3414,7 @@ static struct iommu_ops arm_smmu_ops = { .owner = THIS_MODULE, .default_domain_ops = &(const struct iommu_domain_ops) { .attach_dev = arm_smmu_attach_dev, + .set_dev_pasid = arm_smmu_s1_set_dev_pasid, .map_pages = arm_smmu_map_pages, .unmap_pages = arm_smmu_unmap_pages, .flush_iotlb_all = arm_smmu_flush_iotlb_all, diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index cdd426efb384d2..91ec2d49ecbf2e 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -801,7 +801,7 @@ void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, int arm_smmu_set_pasid(struct arm_smmu_master *master, struct arm_smmu_domain *smmu_domain, ioasid_t pasid, - const struct arm_smmu_cd *cd); + struct arm_smmu_cd *cd); void arm_smmu_tlb_inv_asid(struct arm_smmu_device *smmu, u16 asid); void arm_smmu_tlb_inv_range_asid(unsigned long iova, size_t size, int asid,