From patchwork Mon Mar 4 23:43:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13581401 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 E9B40C54E49 for ; Tue, 5 Mar 2024 00:51:23 +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=6SjnxwaActRwYmJIBdzUe9zxp2+RQU2ev0bbYRPyLH8=; b=VbBWtZqTeuMk2h KpILziyporNWvAprMYxxJWAmQKnzMUbxsJeMsMyu2TBMyJEbUhAz4skmKyRZ+QiOTy6Ro4Lkn+iAb beF5PY9Vhn5jzAR/wU2OMvbXzs3OZdvp0ZfCkHphZErG5NoLhFNo4GcheYZdk/X7VP402H1JMDO8z pmH6L6EVbl4wf9hoicUkwasBBIctkYSCdOitr3T2Urfh5gM4HNNesAV/NoVXsuPt7ptohAs+ahiSs e4sjwdyHdFpaNJGMVd3Id9f3mo1op9/RkFdFTeZ+VF5lgyjjaXkTIXduNSsfBJHNG3K3LX1Bt6yJn 5wcp0dC4EwNs31qEg39Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhJ1Q-0000000BHil-18gY; Tue, 05 Mar 2024 00:51:08 +0000 Received: from mail-bn8nam11on20601.outbound.protection.outlook.com ([2a01:111:f403:2414::601] helo=NAM11-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rhHzT-0000000B7Kx-2gJ5 for linux-arm-kernel@lists.infradead.org; Mon, 04 Mar 2024 23:45:20 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PlP6N9ZGru3xc8Z52UlA92/1aWlFrB2jNSPOW/tm/yq+nANEGLcrBSNDAV+jscbc0mgNPcD+UHQmQh/bRUYPwq8J+n4hSAuZK9MY4NKKwv79kgdZIz3BLg8LtfQrPrVME/Nf4cvYqg5bcb4Aozn1i2Q48Eb+xC/8Wn4cRZR9KP3JM6cBMqV9vwKidCGgrABKbAZ7osRMR0tZSVOmMM3kpfR5MmRX+Asvao6bfIpUotlveZPYrdYkghq8gGXDNgmBRYm3LMkoH6CoHbf3UtU3WAhUDjCvOzVi1jo7iuADUv2J18FgVNrhaDgHhYWKq8UWk760xNpg/KD1+ypejsb6yg== 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=+dM3axHpkFBC/TBzyBeaVwI4FrKRtUW3DwChFeh3ASk=; b=Ntmomwcd9AQ4P9OAtWYYwgIz3I0ZJB7a3lMMdNK5tXWg/G06TP3xAlC9Ah/KFQCGxnTkFHPI9sLV8D6YOooeXy3z2pua4EuGASZfuoPleyvgWJPKGQpqfL3gWWZB0Bamc6Q3AERWooun5aYchZ/A8ex8FAUkPXiSerCPTC98PLBGXv4cIVdP8sszdrkc6bjg8vszWnYAi6bZrq+odov0ktCqlSTruerHSikqS3jANgirQGfFwJR2b88OpOVnvOHFuAwnwHFq/cW+20dNeMoT3SmYqkJmPwg15TkTml+dvFZbPO5zgZFXvG06+6LNf9u/GgcT4W4IZtmG1FSOt11TOQ== 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=+dM3axHpkFBC/TBzyBeaVwI4FrKRtUW3DwChFeh3ASk=; b=URF1582w/SmdM8V82x+9GsqH1t3WaA0bfyhzRfGfsD5eTI3f4PdlkcxWuqofyKQz+s+/0cVrtW90KtnyiE8wAoubRWl2r5i0QNg4bhQH0tqkWllksbpbvEkCldRcXaczObiea8s6rqHdzHuwKygjH/XAIlWrB4Hogb+SaCgIbEIczkXNgRVke1n1SK+uZOD6t7TMeplwdtS90r0RO2eJ02qlbNS6D5dM3FhnFalbIfe+d4W5HZNdgO9J7HaibnqQxZW1ft+napeaUPFYpG6K8cPWkVwUn4pWvvLkJmqmcO49LiMEqpf07Qcq02d9+EwS2oeVBv3qgKsDuEcqVpREag== 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 DS0PR12MB6584.namprd12.prod.outlook.com (2603:10b6:8:d0::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7339.39; Mon, 4 Mar 2024 23:44:25 +0000 Received: from DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c33c:18db:c570:33b3]) by DM6PR12MB3849.namprd12.prod.outlook.com ([fe80::c33c:18db:c570:33b3%5]) with mapi id 15.20.7339.035; Mon, 4 Mar 2024 23:44:25 +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 v5 05/27] iommu/arm-smmu-v3: Make CD programming use arm_smmu_write_entry() Date: Mon, 4 Mar 2024 19:43:53 -0400 Message-ID: <5-v5-9a37e0c884ce+31e3-smmuv3_newapi_p2_jgg@nvidia.com> In-Reply-To: <0-v5-9a37e0c884ce+31e3-smmuv3_newapi_p2_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR12CA0019.namprd12.prod.outlook.com (2603:10b6:208:a8::32) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|DS0PR12MB6584:EE_ X-MS-Office365-Filtering-Correlation-Id: 458e0238-4ef3-44ef-1b57-08dc3ca50223 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z8KCWieEy7n3NrMiF3zCXII1o/Z9RabLbu9sLWOywjWBeG5le1bIMykne2x7j295uw4m52pTL88Q3MiVeGE7/hBzORZ2BgF1c/icC6GhtKLafvqEn9/EViln7Kuu66kZiqHvlbmVw7FoYgjJ/l+KebURBTkEq2sIsKjyaxf2jOErNbslZveF0vv+w6UDJJsCp5t/J7yWT81xwyDY06yacVK40CvsU0a7zhpVpvi4cwkqDGxaT48FKd1byPQk/0H7dVKU4MQ1mPR4b/X5Ohe9Ob07VMitqE44WeH4IPEeU4acsFy3l1ZUK6e41gRX00+4vsthGVbyyZdCw9mXzzdSWrHWLbJH3oaOwid1fx5LM/9nCCVpovcpBJ74sf7H3xvFzAE+2MYee7BwL57nLiGD+aeJqIT2VyQY1OsxrAHs9Hkx3AbJCk4fc/obP3DzfNiBr57ajYnzsGo1xLs+sSUq1P0gMLFPModGIjfLm8hyxJ59VYrku4SaQuD1ptgbCsc6pB0mIJiC4U5WM+VQwF34bft/iL7TJIVFjUnOdTHD3Mg+nHnIrg4vpL84Qu4IkBbIMnIbkAlECKtpheyX3oWLx4FVBdlY8jSR1Havec5QBOwaec8DvUetXFPVb0pCWXAV0qSFmKEuiRmm6NRUc+Jurtb84Ge0GUvw7CpWmA/bmZ8= 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);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7lcj9lZ5Sh+HJCK2fV1+FI6BcQpDgsszWBmBVv22AZb+8fw/FHMy/A/CFHT3SaYurrQeDRg4fqWFeKAStT/Kp53cd7gcG0UvLbIwpr2CI/j4YabDy9CGzhChFb0GbY32o51ZKBoC7Fv3wL7hkr2Qgg3AZcMsTXZeiec073y33i+H0oe/70/LTq/+d/HLDCFddHIiDyGm2vbxO+dBHaZFLcvGX7NcG2bGvqGeQ1OZuKWGb2dddhVSk/BK9bOf35QjXgbAMfa9qRZScbfG4jz1vpsm10JW0l0sjYhaGkBqYrOkEbnmHtIzxQQhmzBRSeu5WplcJFp/ut4+wy1VLWam4mkg7pviW/1bBQ7A90v02wX+9Q5uePIjfZn1ZVkEo8MtPhxfU+pzi4szs4Kwe5hF1HE5LTQbZrpOd3aZwMMoJ5WvnaXd2u0TxQRuQFQOA0TtbOVlsfreqhaI0mnrdlqnobz8vGllSFHKkXm1SfndQ9d0/AHv3tmuSETFRfWPkxcdLv2Hs1vsi29nmUb8aSfiRDzqiET8hwt5fflw93MY0NiSw9aQHYQCnmZpWf/WaOmeMepyMth9VnYHIYbVxhr9py3GzQcz0TMz80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ca50223 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Mar 2024 23:44:18.3674 (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: hj1ikY8WD48OwQbU9sRriyl4eL6TToqDTs/vYv5XOWr/w/zpNX0OEoYUZd8HmU12 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6584 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240304_154504_078422_4ABC94F2 X-CRM114-Status: GOOD ( 22.50 ) 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 CD table entries and STE's have the same essential programming sequence, just with different types and sizes. Have arm_smmu_write_ctx_desc() generate a target CD and call arm_smmu_write_entry() to do the programming. Due to the way the target CD is generated by modifying the existing CD this alone is not enough for the CD callers to be freed of the ordering requirements. The following patches will make the rest of the CD flow mirror the STE flow with precise CD contents generated in all cases. Currently the logic can't ensure that the CD always conforms to the used requirements until all the CD generation is moved to the new method. Add a temporary no_used_check to disable the assertions. Signed-off-by: Michael Shavit Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe Reviewed-by: Moritz Fischer --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 101 ++++++++++++++------ 1 file changed, 74 insertions(+), 27 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 b7f947e36f596f..237fd6d92c880b 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -57,11 +57,14 @@ struct arm_smmu_entry_writer { struct arm_smmu_entry_writer_ops { unsigned int num_entry_qwords; __le64 v_bit; + bool no_used_check; void (*get_used)(const __le64 *entry, __le64 *used); void (*sync)(struct arm_smmu_entry_writer *writer); }; -#define NUM_ENTRY_QWORDS (sizeof(struct arm_smmu_ste) / sizeof(u64)) +#define NUM_ENTRY_QWORDS \ + (max(sizeof(struct arm_smmu_ste), sizeof(struct arm_smmu_cd)) / \ + sizeof(u64)) static phys_addr_t arm_smmu_msi_cfg[ARM_SMMU_MAX_MSIS][3] = { [EVTQ_MSI_INDEX] = { @@ -1056,7 +1059,8 @@ static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer, * allowed to set a bit to 1 if the used function doesn't say it * is used. */ - WARN_ON_ONCE(target[i] & ~target_used[i]); + if (!writer->ops->no_used_check) + WARN_ON_ONCE(target[i] & ~target_used[i]); /* Bits can change because they are not currently being used */ unused_update[i] = (entry[i] & cur_used[i]) | @@ -1065,7 +1069,8 @@ static u8 arm_smmu_entry_qword_diff(struct arm_smmu_entry_writer *writer, * Each bit indicates that a used bit in a qword needs to be * changed after unused_update is applied. */ - if ((unused_update[i] & target_used[i]) != target[i]) + if ((unused_update[i] & target_used[i]) != + (target[i] & target_used[i])) used_qword_diff |= 1 << i; } return used_qword_diff; @@ -1161,8 +1166,11 @@ static void arm_smmu_write_entry(struct arm_smmu_entry_writer *writer, * in the entry. The target was already sanity checked by * compute_qword_diff(). */ - WARN_ON_ONCE( - entry_set(writer, entry, target, 0, num_entry_qwords)); + if (writer->ops->no_used_check) + entry_set(writer, entry, target, 0, num_entry_qwords); + else + WARN_ON_ONCE(entry_set(writer, entry, target, 0, + num_entry_qwords)); } } @@ -1242,6 +1250,59 @@ static struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, return &l1_desc->l2ptr[idx]; } +struct arm_smmu_cd_writer { + struct arm_smmu_entry_writer writer; + unsigned int ssid; +}; + +static void arm_smmu_get_cd_used(const __le64 *ent, __le64 *used_bits) +{ + used_bits[0] = cpu_to_le64(CTXDESC_CD_0_V); + if (!(ent[0] & cpu_to_le64(CTXDESC_CD_0_V))) + return; + memset(used_bits, 0xFF, sizeof(struct arm_smmu_cd)); + + /* EPD0 means T0SZ/TG0/IR0/OR0/SH0/TTB0 are IGNORED */ + if (ent[0] & cpu_to_le64(CTXDESC_CD_0_TCR_EPD0)) { + used_bits[0] &= ~cpu_to_le64( + CTXDESC_CD_0_TCR_T0SZ | CTXDESC_CD_0_TCR_TG0 | + CTXDESC_CD_0_TCR_IRGN0 | CTXDESC_CD_0_TCR_ORGN0 | + CTXDESC_CD_0_TCR_SH0); + used_bits[1] &= ~cpu_to_le64(CTXDESC_CD_1_TTB0_MASK); + } +} + +static void arm_smmu_cd_writer_sync_entry(struct arm_smmu_entry_writer *writer) +{ + struct arm_smmu_cd_writer *cd_writer = + container_of(writer, struct arm_smmu_cd_writer, writer); + + arm_smmu_sync_cd(writer->master, cd_writer->ssid, true); +} + +static const struct arm_smmu_entry_writer_ops arm_smmu_cd_writer_ops = { + .sync = arm_smmu_cd_writer_sync_entry, + .get_used = arm_smmu_get_cd_used, + .v_bit = cpu_to_le64(CTXDESC_CD_0_V), + .no_used_check = true, + .num_entry_qwords = sizeof(struct arm_smmu_cd) / sizeof(u64), +}; + +static void arm_smmu_write_cd_entry(struct arm_smmu_master *master, int ssid, + struct arm_smmu_cd *cdptr, + const struct arm_smmu_cd *target) +{ + struct arm_smmu_cd_writer cd_writer = { + .writer = { + .ops = &arm_smmu_cd_writer_ops, + .master = master, + }, + .ssid = ssid, + }; + + arm_smmu_write_entry(&cd_writer.writer, cdptr->data, target->data); +} + int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, struct arm_smmu_ctx_desc *cd) { @@ -1258,17 +1319,20 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, */ u64 val; bool cd_live; - struct arm_smmu_cd *cdptr; + struct arm_smmu_cd target; + struct arm_smmu_cd *cdptr = ⌖ + struct arm_smmu_cd *cd_table_entry; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; struct arm_smmu_device *smmu = master->smmu; if (WARN_ON(ssid >= (1 << cd_table->s1cdmax))) return -E2BIG; - cdptr = arm_smmu_get_cd_ptr(master, ssid); - if (!cdptr) + cd_table_entry = arm_smmu_get_cd_ptr(master, ssid); + if (!cd_table_entry) return -ENOMEM; + target = *cd_table_entry; val = le64_to_cpu(cdptr->data[0]); cd_live = !!(val & CTXDESC_CD_0_V); @@ -1290,13 +1354,6 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, cdptr->data[2] = 0; cdptr->data[3] = cpu_to_le64(cd->mair); - /* - * STE may be live, and the SMMU might read dwords of this CD in any - * order. Ensure that it observes valid values before reading - * V=1. - */ - arm_smmu_sync_cd(master, ssid, true); - val = cd->tcr | #ifdef __BIG_ENDIAN CTXDESC_CD_0_ENDI | @@ -1310,18 +1367,8 @@ int arm_smmu_write_ctx_desc(struct arm_smmu_master *master, int ssid, if (cd_table->stall_enabled) val |= CTXDESC_CD_0_S; } - - /* - * The SMMU accesses 64-bit values atomically. See IHI0070Ca 3.21.3 - * "Configuration structures and configuration invalidation completion" - * - * The size of single-copy atomic reads made by the SMMU is - * IMPLEMENTATION DEFINED but must be at least 64 bits. Any single - * field within an aligned 64-bit span of a structure can be altered - * without first making the structure invalid. - */ - WRITE_ONCE(cdptr->data[0], cpu_to_le64(val)); - arm_smmu_sync_cd(master, ssid, true); + cdptr->data[0] = cpu_to_le64(val); + arm_smmu_write_cd_entry(master, ssid, cd_table_entry, &target); return 0; }