From patchwork Tue Dec 3 22:10:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 13893046 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 C034AE74AFF for ; Tue, 3 Dec 2024 22:21:30 +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:Content-Type: Content-Transfer-Encoding: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=XSbcYsAxYmf6k1AkDUXyPnnb77Nj5Kh7FPmP8GN3YFw=; b=UP1bOcCqoFW80QOLnAagO7a0UO 9sgQrkz8dgDvcibldGsb3zFjV/O7ogSo0/nIH9cEsMqoi9suV9Jm9wqRANvLVrgbDKG7jPdpie5Jx tYsMouMYIGfWQCIDOtaWwwZIUm/Lz+/C0LxJ9nXiPZElL4uNXTJ3Yn2Z+7H/bZmgEaeXV5I0TcLzM nCDOuR/lPX39pLdlzOr/AOw/PpzGg1GOLKXU+2QbBndbWE5WL2oOUklkJ137iub6V5NFlQNIxzkPQ JRHP8d4cQ7DnbmlC97fSsUtc+8JEIKyzc5zm1xdesIrl7nmp9pkKsuLK6eMjm/EUfXwbV3BP+EosY /xnnH5Mw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tIbGf-0000000AtzE-3WL9; Tue, 03 Dec 2024 22:21:17 +0000 Received: from mail-dm6nam10on2062d.outbound.protection.outlook.com ([2a01:111:f403:2413::62d] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tIb71-0000000ArzG-2Gy1 for linux-arm-kernel@lists.infradead.org; Tue, 03 Dec 2024 22:11:20 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vOLIR+GctDBqHWosc3m5u+xj1aKUjHWeHdDtoKy/WaQn86dPcRMDe9rHiXyqfj1HDz9pDNvi+w4tNVLrXM/DB+cOBawY0tdGtOU7QTMyc4DOGr9YU5a+jlxf0w3wuXv4hg4uZjNW499dJGdN8aVuEUDwSkxXzvMNs3qb/vX80WdDJaDEAm6GaLVkr6CQ0h55ReUANixodw7kNIe97SK6CTGJoZiRlydeJ/N0HuCnNn3NzxGZF90mpv3CB0MDS2FGfQX4DboxZQxsVjGhJtnnQdismPeIxadTw9huIzbtGOz1SFP4B/zuGAGtl+4XgKFpX+iHo9dqUU65IMpa5hmLbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=XSbcYsAxYmf6k1AkDUXyPnnb77Nj5Kh7FPmP8GN3YFw=; b=ObVyCQt64R8nNgzc/jjeM82nc30h+txrnTdah+2X5+W1/01g2/Mx+nz+n4vupStKvINrZhtWFb4BBkRYrJYnrbo2pVBvmISYxcDOsQGwGvXaH3eIfUixLK7SumfkScN7R59wbdaW9d7HtJ3vYmZCY5ZI1TJWaH7SxXoloIM/i6G8n0IkXNgiuuPweA4jQICJeiAgCrJvfTwEPyUS8h34xrVYJoxDJyTLoiHBvp1pD0l9RbC1Fx5OiCciXqS8Ckkm2G4R0JknWuocmUNgZ6Jw3sj0SzubiofRUsPcnUg7vBJ/NOF/5mRmeBreGWa/wFBJcZJd+XnmzE/HzmH3kVeeGw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) smtp.rcpttodomain=google.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) 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=XSbcYsAxYmf6k1AkDUXyPnnb77Nj5Kh7FPmP8GN3YFw=; b=l1/8nYzapD3U37CY/pOHhwpoq3F9StSOSO6654c/ZoMO1tesTIvlx3kpFPyGisRMKYn/p0KQNJ7LBL4/W0BoDJTOTF/IYTdMHeT+h8Lf0Sf1fjceicTkbNYoXssZGOV8i1ZYU/ybqxUwfYfJZwOqrm9JlClamxNGj5qtgEWcqbK2nPACmlm3guZoH1LTE3ZF7eryNc3hpPRMbeXddknIJqABYOulb4cpdaXAwxz8+Wg2ptziyBysbqvasj/5GK7jxdkHHgsAfvv7c5EFixbKJLkR7Y2sp/hvi2l7/SAUCzIcMpij3HjoZng5+i984+OWlifrLlsTRhnX66ZXLHn/bw== Received: from MW4PR03CA0310.namprd03.prod.outlook.com (2603:10b6:303:dd::15) by DM4PR12MB5939.namprd12.prod.outlook.com (2603:10b6:8:6a::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8207.17; Tue, 3 Dec 2024 22:11:07 +0000 Received: from SJ1PEPF00002327.namprd03.prod.outlook.com (2603:10b6:303:dd:cafe::98) by MW4PR03CA0310.outlook.office365.com (2603:10b6:303:dd::15) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8207.17 via Frontend Transport; Tue, 3 Dec 2024 22:11:07 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by SJ1PEPF00002327.mail.protection.outlook.com (10.167.242.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8230.7 via Frontend Transport; Tue, 3 Dec 2024 22:11:07 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 3 Dec 2024 14:10:49 -0800 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Tue, 3 Dec 2024 14:10:49 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.14) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Tue, 3 Dec 2024 14:10:48 -0800 From: Nicolin Chen To: , , CC: , , , , , , , , , , , , , , , , , , , Subject: [PATCH v2 12/13] iommu/arm-smmu-v3: Introduce struct arm_smmu_vmaster Date: Tue, 3 Dec 2024 14:10:17 -0800 Message-ID: <6270c905fc8537ffc51f6f2ab2db0601c9f08a8d.1733263737.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ1PEPF00002327:EE_|DM4PR12MB5939:EE_ X-MS-Office365-Filtering-Correlation-Id: eb341530-a642-4640-f842-08dd13e762cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|36860700013|82310400026|376014|7416014; X-Microsoft-Antispam-Message-Info: DM8LjERzKgXdcbVLjZIMRXffWRvm1MvsKlQSROduad6Z+HbBNn5vJr4I5tGpnYolroWwtIeLdQsnU4s0yyonaeATMe9O6Wp4mRusa14HASRYKjOqSHdU1IGsYQV2DJLjlzY/VRVxrfinBalt1KCIz+vGcE9zFK2O2cwrWAETTES3ERenkjRi4XKQHW7750QTn+3AyFyvhfdpc3MMFGb/uQ4M7jnq6ONaXhjRLZDAt1xYoOAqOlVxRsUCXrSFSWoyEb3wIid6Hj1YT3ICMGn1SLCTIXEDkjuklO2d3RH9f+Ye2BHteMKV4YnyNVUX9iSqiL4B539npYHPBAa6iPsVCiHgqdiYuhMyFAVOFD4sG9KaWxCCTA9o5doJLsTIeQ/rde6hi2Pk41uDiDUiTqUBuJYVE01xU8q4KoD7rfGQ2S8e8eIuXobqelhqlAr+ntIwuQD1UaaBYGYzzHpOmdBkTJmxveNw8UdRhNttt6pvsxU2BFE8Szkj6tLyNJzUA24Yy47Q/+NB1X9fK/upzB43MyTg2D4dvSGq+I4LA25wdPsDBtkb6ZXZKLWma+0RWvf5C9RCizL6J0Cu4tL5dLs6srKZ/dKr5wzuLBmxX6ANNYt1nnX0m1OS3viar1riMV+ZbLh79ZrJWkMSUdPTxkcb2skpPQQNvgPY5/DX8v17jqq0W03YmDdMsdQqjknHg1LVzy06YBdpSDKwIbDvY9pFIZykcGLwL0w0oNpAaQ4ZCnQ5gENPK/VnqvaYdIJT9aWut7TaATDLoOY6FsOdDM935TBKjSaAAjDa2OsuBXIDkxz2tAJO83cMOCX7xK0gJJyqzLqa4wZaTG6l7RygytTAg2gtnbZigJbUiwsX/SPQ6CIwyG/VjnzuchwE1ocQuxyYkGFbC2Ul7QLmW6GMWIx1kZpgdxJnAFVtXNLd8HuYC/3glnPQBquDffGolmq65LSqVvAFaEcQHS2DViCwbDUfnGSvSaAhT/aIOQOgorVQzoVDLuysgClQTBo22i6HJo6suhxjPOJm9THXUrhTQyDdNqeQrD3ecHqn0xLRkElm9Dd3PpOZqHX+tBt0tujnXN9fulOZMQyoQb1++ifkyq5CNTBEbKwM8bSJgjqU8Ebs3nxf+epPosLxx9bGT76iiXG8+vf2FpAflgn55qdJ8FAZJKPCrJ7PMrNKMkmmMjrWIxExDktCeCUdBXXUvMcIdi0tgaxMDy671Zc/2hqUuXSaSDoVN8bdnxpPVaXwPzMp3RgPTDjryHAhxWJva2X5+pjcz/5ZfAmYRLaLWeL26Vq8MEtIaVygHkk7cejXsX876KRaTiP6+fP9lA6j3h9mDbb1KRiItQ9WJAwhVaL5ISFLooNsd/ujRvWXeV+X4WsKMuWqaXEdez0fIMnehe1fOIkZdDTtZEUjU5SkBhGFF/pLPoi3MqQebyWIJAebSibb+XyBoYKzFk/zp1QiIYliUyRi X-Forefront-Antispam-Report: CIP:216.228.118.233;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge2.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(36860700013)(82310400026)(376014)(7416014);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Dec 2024 22:11:07.2360 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: eb341530-a642-4640-f842-08dd13e762cd X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.233];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SJ1PEPF00002327.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5939 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241203_141119_633997_C2B8D6CD X-CRM114-Status: GOOD ( 21.00 ) 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 Use it to store all vSMMU-related data. The vsid (Virtual Stream ID) will be the first use case. Then, add a rw_semaphore to protect it. Also add a pair of arm_smmu_attach_prepare/commit_vmaster helpers and put them in the existing arm_smmu_attach_prepare/commit(). Note that identity and blocked ops don't call arm_smmu_attach_prepare/commit(), thus simply call the new helpers at the top. Signed-off-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 23 +++++++++ .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 49 +++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 32 +++++++++++- 3 files changed, 103 insertions(+), 1 deletion(-) 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 0107d3f333a1..ec7cff33a0b1 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -789,11 +789,18 @@ struct arm_smmu_stream { struct rb_node node; }; +struct arm_smmu_vmaster { + struct arm_vsmmu *vsmmu; + unsigned long vsid; +}; + /* SMMU private data for each master */ struct arm_smmu_master { struct arm_smmu_device *smmu; struct device *dev; struct arm_smmu_stream *streams; + struct arm_smmu_vmaster *vmaster; + struct rw_semaphore vmaster_rwsem; /* Locked by the iommu core using the group mutex */ struct arm_smmu_ctx_desc_cfg cd_table; unsigned int num_streams; @@ -943,6 +950,7 @@ struct arm_smmu_attach_state { bool disable_ats; ioasid_t ssid; /* Resulting state */ + struct arm_smmu_vmaster *vmaster; bool ats_enabled; }; @@ -1026,9 +1034,24 @@ struct iommufd_viommu *arm_vsmmu_alloc(struct device *dev, struct iommu_domain *parent, struct iommufd_ctx *ictx, unsigned int viommu_type); +int arm_smmu_attach_prepare_vmaster(struct arm_smmu_attach_state *state, + struct iommu_domain *domain); +void arm_smmu_attach_commit_vmaster(struct arm_smmu_attach_state *state); #else #define arm_smmu_hw_info NULL #define arm_vsmmu_alloc NULL + +static inline int +arm_smmu_attach_prepare_vmaster(struct arm_smmu_attach_state *state, + struct iommu_domain *domain) +{ + return 0; /* NOP */ +} + +static inline void +arm_smmu_attach_commit_vmaster(struct arm_smmu_attach_state *state) +{ +} #endif /* CONFIG_ARM_SMMU_V3_IOMMUFD */ #endif /* _ARM_SMMU_V3_H */ diff --git a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c index 6cc14d82399f..3a77eca949e6 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c @@ -85,6 +85,55 @@ static void arm_smmu_make_nested_domain_ste( } } +int arm_smmu_attach_prepare_vmaster(struct arm_smmu_attach_state *state, + struct iommu_domain *domain) +{ + struct arm_smmu_nested_domain *nested_domain; + struct arm_smmu_vmaster *vmaster; + unsigned long vsid; + unsigned int cfg; + + iommu_group_mutex_assert(state->master->dev); + + if (domain->type != IOMMU_DOMAIN_NESTED) + return 0; + nested_domain = to_smmu_nested_domain(domain); + + /* Skip ABORT/BYPASS or invalid vSTE */ + cfg = FIELD_GET(STRTAB_STE_0_CFG, le64_to_cpu(nested_domain->ste[0])); + if (cfg == STRTAB_STE_0_CFG_ABORT || cfg == STRTAB_STE_0_CFG_BYPASS) + return 0; + if (!(nested_domain->ste[0] & cpu_to_le64(STRTAB_STE_0_V))) + return 0; + + vsid = iommufd_viommu_get_vdev_id(&nested_domain->vsmmu->core, + state->master->dev); + /* Fail the attach if vSID is not correct set by the user space */ + if (!vsid) + return -ENOENT; + + vmaster = kzalloc(sizeof(*vmaster), GFP_KERNEL); + if (!vmaster) + return -ENOMEM; + vmaster->vsmmu = nested_domain->vsmmu; + vmaster->vsid = vsid; + state->vmaster = vmaster; + + return 0; +} + +void arm_smmu_attach_commit_vmaster(struct arm_smmu_attach_state *state) +{ + struct arm_smmu_master *master = state->master; + + down_write(&master->vmaster_rwsem); + if (state->vmaster != master->vmaster) { + kfree(master->vmaster); + master->vmaster = state->vmaster; + } + up_write(&master->vmaster_rwsem); +} + static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, struct device *dev) { 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 e4ebd9e12ad4..6a6113b36360 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -2730,6 +2730,7 @@ int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state, struct arm_smmu_domain *smmu_domain = to_smmu_domain_devices(new_domain); unsigned long flags; + int ret; /* * arm_smmu_share_asid() must not see two domains pointing to the same @@ -2754,9 +2755,15 @@ int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state, } if (smmu_domain) { + ret = arm_smmu_attach_prepare_vmaster(state, new_domain); + if (ret) + return ret; + master_domain = kzalloc(sizeof(*master_domain), GFP_KERNEL); - if (!master_domain) + if (!master_domain) { + kfree(state->vmaster); return -ENOMEM; + } master_domain->master = master; master_domain->ssid = state->ssid; if (new_domain->type == IOMMU_DOMAIN_NESTED) @@ -2783,6 +2790,7 @@ int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state, spin_unlock_irqrestore(&smmu_domain->devices_lock, flags); kfree(master_domain); + kfree(state->vmaster); return -EINVAL; } @@ -2815,6 +2823,8 @@ void arm_smmu_attach_commit(struct arm_smmu_attach_state *state) lockdep_assert_held(&arm_smmu_asid_lock); + arm_smmu_attach_commit_vmaster(state); + if (state->ats_enabled && !master->ats_enabled) { arm_smmu_enable_ats(master); } else if (state->ats_enabled && master->ats_enabled) { @@ -3094,8 +3104,17 @@ static void arm_smmu_attach_dev_ste(struct iommu_domain *domain, static int arm_smmu_attach_dev_identity(struct iommu_domain *domain, struct device *dev) { + int ret; struct arm_smmu_ste ste; struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_attach_state state = { + .master = master, + }; + + ret = arm_smmu_attach_prepare_vmaster(&state, domain); + if (ret) + return ret; + arm_smmu_attach_commit_vmaster(&state); arm_smmu_make_bypass_ste(master->smmu, &ste); arm_smmu_attach_dev_ste(domain, dev, &ste, STRTAB_STE_1_S1DSS_BYPASS); @@ -3114,7 +3133,17 @@ static struct iommu_domain arm_smmu_identity_domain = { static int arm_smmu_attach_dev_blocked(struct iommu_domain *domain, struct device *dev) { + int ret; struct arm_smmu_ste ste; + struct arm_smmu_master *master = dev_iommu_priv_get(dev); + struct arm_smmu_attach_state state = { + .master = master, + }; + + ret = arm_smmu_attach_prepare_vmaster(&state, domain); + if (ret) + return ret; + arm_smmu_attach_commit_vmaster(&state); arm_smmu_make_abort_ste(&ste); arm_smmu_attach_dev_ste(domain, dev, &ste, @@ -3345,6 +3374,7 @@ static struct iommu_device *arm_smmu_probe_device(struct device *dev) master->dev = dev; master->smmu = smmu; + init_rwsem(&master->vmaster_rwsem); dev_iommu_priv_set(dev, master); ret = arm_smmu_insert_master(smmu, master);