From patchwork Tue Jun 11 00:31:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13692724 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 3C45EC27C4F for ; Tue, 11 Jun 2024 00:47:34 +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=cGxLUffMqq3mIbQsu3nGUBsEsW+BvRSuVHeK/u5RYRI=; b=h+bJ1KRqOr30qq 5bMrcA5Jvvzr8rxUhIvyjiQw6ScWCffAADdN7J8jeoWdpvAFylMbTwEewWhAtjS/23cv9kyEXVgwC iFi/uwpBtBHrDeYbVtffGdiham8QRBE70U/JQCtRt8AVK0gQHQw7NfBtAuq+qYAmTJKB+LqgEIB5a MxJ0lNMBTxmXImxII99C9IHSjfP8fiLIaBXSC1iMTP9YT/7c3s5rw0TbGP2yd6dekHb5EYQTl7Ax1 +xBsEQw9ln8vXKlR0H1X137YDZq5sMeIDGdaJjdm5MYEe0RS4Vxm4P2BMf5VcrBDj7aL3wBAIn1XU GT+DJi2htGURFAE4Mw4g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGpfT-000000070MB-2Occ; Tue, 11 Jun 2024 00:47:19 +0000 Received: from mail-bn7nam10on20626.outbound.protection.outlook.com ([2a01:111:f403:2009::626] helo=NAM10-BN7-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGpfQ-000000070LF-49mk for linux-arm-kernel@lists.infradead.org; Tue, 11 Jun 2024 00:47:18 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mrBLX66Zj3xrkWf5jDbmE3MyqaOTSnmaUs2JXqyMRAjnohMS9tP0LQ8V2yNlwdfYllmA6Qh6VKxtdlMrgmi80VxtzyR6xom9lGBRqb3leBNssHUWbxoaYNOChms8X7gVLyBjVCY8o67Wj+qJeKsTcta7S4wmTTz6P3BRZBG8I5wiu8GQHYu/k30a4ubqrtxT+3+yz6kcfH+M8XC6g/HTdT6I2ziLd4qe/J7SL+jyjuuYk2Kdl2Xc39NJ8XrOZvPIgQJjNoFRjY8w0yc2wW4XQU7isZP6dS66ToiF/XvuaseA6PRhaMJcgwaW4xCeD814I4D3k3RiEZ2W8itsScHOZw== 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=egCXRcBlnMbohCVjqhJ6vFKAuzzIRmlFwYHe+rj6s8g=; b=VKPpLGadlHaFwjkJzMOFQw8MCs4q8hv47Ux6bxCtrDObX5ul7pu1NNA26K1ADKf1DkAt+Ws8SAEvNDBuJ8VI8aNvCsos/E3Mgn2qaK8r/IHaO7v6ExpWs0lgZIjqlXZmuwxvkVPxqpM9/1Y0WQtLYebRQt/mU5EpyYFZzOPT/XFHF515j3xnGUET2cr83Ps/LI/c2rQo7URwR4Rvpyk8zfTJJLYJMpaVDR/KglOIt8zIaBfKDieU47jcdHnO0vUWIKgjisQXJemetAnQPz6Yk6rVnKUaStbMk1bFSKEhLx3Xv4lVQHeAJmXZ8Er/N18eGLai+/KXncmROmavN1GDBg== 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=egCXRcBlnMbohCVjqhJ6vFKAuzzIRmlFwYHe+rj6s8g=; b=ATauL9se+g8IUf/Ss2gxmCpX7mXWY103n0JmG5vwx7AAhbHqytSPZdHaotUwUnSDTobVclDADQNZ8A5pvoVsR4zqO1D1J32ebf2g68GmyHPH11eEtry7CXNDrJDeVRDtSTEBlbM3X3mofTAVMWpQJEcEX1jUiAKeNCqV/ABQF71kkutyMIsTBSpmXQ3TR2UsGY7ZkAbzUYRg2RDiZH7Oq4Aile/I5btvrsr0Q+O+XcTX38CPJi1eOW1VDFsZKdU982YbZozuTPOjKJPLdegm6Olh3l5eOTw2bur3rFkOg1TNnWnfn2ea/N+hue3JKoAIc9lngG5pScgI+rAuGdvIjw== 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 MN6PR12MB8565.namprd12.prod.outlook.com (2603:10b6:208:47d::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7633.36; Tue, 11 Jun 2024 00:31:23 +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.036; Tue, 11 Jun 2024 00:31:23 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Joerg Roedel , linux-arm-kernel@lists.infradead.org, Robin Murphy , Will Deacon Cc: Michael Shavit , Nicolin Chen , patches@lists.linux.dev, Ryan Roberts , Mostafa Saleh Subject: [PATCH v2 10/10] iommu/arm-smmu-v3: Reorganize struct arm_smmu_ctx_desc_cfg Date: Mon, 10 Jun 2024 21:31:19 -0300 Message-ID: <10-v2-318ed5f6983b+198f-smmuv3_tidy_jgg@nvidia.com> In-Reply-To: <0-v2-318ed5f6983b+198f-smmuv3_tidy_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR22CA0014.namprd22.prod.outlook.com (2603:10b6:208:238::19) To DM6PR12MB3849.namprd12.prod.outlook.com (2603:10b6:5:1c7::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM6PR12MB3849:EE_|MN6PR12MB8565:EE_ X-MS-Office365-Filtering-Correlation-Id: 4f344b75-ffa2-43f5-09a1-08dc89add133 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230031|376005|1800799015|366007; X-Microsoft-Antispam-Message-Info: wUxp8mdqq88Grn1+Pu1niyok+mj4fvRznl+BFxwJwz5s0z0xZW3VZLTxjTosZXNaj7U83LFIWmBNDFkJ10HG5s0sH1aV0wYUSrfL+gGZWfcui2csCmQMfAdvBq3JlI0R4yqQd+47mzhmUZBllfs0rpf2/8x7ra9M+3C/N8wa0Zs8k88r4gOV1vZ/sHXhl035s7IGzQEQPmwIkDgJH7GIn3K1HVj2Uu7xSuX0lWUC2XvAoD29sSogIAYrTBtbYSmbaMIzyW1BnilJj5q0nVakz7WnvVQQW83WDXm9j8w055BVAlAPeKOcnZs4kSEUjeZvRFmCmgLiuheA4Qyy0wlwiIXl1B6s928b33edOvebi6cyuu9Dbzkpq23n9qHccHGVTz3FNx1QDEzREjt/qrF7ky86vjjiyFpvjqKlr3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uaQSmEw3ZK6qnx6BZ0x1LFtJg0HTNY5/0+kfOMSWTXKMUT4JY8hhuUI2njup7XSN8aN+yE9uP3e0wzW3IRsJHCoLEldqgty6icspL/w6WjiDyMhl8WVwtdTIQjoLduz/pD63Xb2FJ44GrwPBggzveAlvB9F+518Kt8iJ6jvvpPshOr9Fi8/7H94R5Ujdk7yhyFNs/Ky2ktlZtGyf1bAKuQoNw24PIHxiun7Xoygrbx9/Mjyldppe1xcVoS92k9uYumqg0r+rv540FfAoXt3g/JV8rDMfoZSPbV86XJ6qW1rkb21gvP8c1q/4LIURqMopa21dGV5WJ8f913/yE7anJ7yhYJEs7OdVezm73zRg6PSmyXvXtNSyyQ5qd+xkVi83o47TUOS2ZbIZmAZ8BEx1YOLz9YV3Dmw8Cc1h8ox8EDV2NN/5AllYGFolxJeQQxJ5Frnx61V/EBAO8hozuu1tD7K7FK68gTkgO+uurFWv7fPtPS6IaxAGFsUhUrpRsipsQ8P340L+n3rYhMO5fWl7maPwH+agQRlawrH6BwR2MzvBezNt8FQQ5if4QBhY= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4f344b75-ffa2-43f5-09a1-08dc89add133 X-MS-Exchange-CrossTenant-AuthSource: DM6PR12MB3849.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jun 2024 00:31:21.4652 (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: GZlRxfVIz/H/h3I3ke/p0ZIhJvFmOPpFaD+WxuVbuf/5d2a8NG7LYwzpMoPKwdKe X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN6PR12MB8565 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240610_174717_116933_BA442CBE X-CRM114-Status: GOOD ( 17.20 ) 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 members here are being used for both the linear and the 2 level case, with the meaning of each item slightly different in the two cases. Split it into a clean union where both cases have their own struct with their own logical names and correct types. Adjust all the users to detect linear/2lvl and use the right sub structure and types consistently. Remove CTXDESC_CD_DWORDS by changing the last places to use sizeof(struct arm_smmu_cd). Signed-off-by: Jason Gunthorpe Reviewed-by: Nicolin Chen --- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 119 +++++++++----------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 24 ++-- 2 files changed, 71 insertions(+), 72 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 49e019131ef3b5..b9994c46c52f25 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1184,19 +1184,19 @@ static dma_addr_t arm_smmu_cd_l1_get_desc(const struct arm_smmu_cdtab_l1 *src) struct arm_smmu_cd *arm_smmu_get_cd_ptr(struct arm_smmu_master *master, u32 ssid) { - struct arm_smmu_l1_ctx_desc *l1_desc; + struct arm_smmu_cdtab_l2 *l2; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; - if (!cd_table->cdtab) + if (!arm_smmu_cdtab_allocated(cd_table)) return NULL; if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_LINEAR) - return &((struct arm_smmu_cd *)cd_table->cdtab)[ssid]; + return &cd_table->linear.table[ssid]; - l1_desc = &cd_table->l1_desc[arm_smmu_cdtab_l1_idx(ssid)]; - if (!l1_desc->l2ptr) + l2 = cd_table->l2.l2ptrs[arm_smmu_cdtab_l1_idx(ssid)]; + if (!l2) return NULL; - return &l1_desc->l2ptr->cds[arm_smmu_cdtab_l2_idx(ssid)]; + return &l2->cds[arm_smmu_cdtab_l2_idx(ssid)]; } struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, @@ -1208,30 +1208,25 @@ struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, might_sleep(); iommu_group_mutex_assert(master->dev); - if (!cd_table->cdtab) { + if (!arm_smmu_cdtab_allocated(cd_table)) { if (arm_smmu_alloc_cd_tables(master)) return NULL; } if (cd_table->s1fmt == STRTAB_STE_0_S1FMT_64K_L2) { unsigned int idx = arm_smmu_cdtab_l1_idx(ssid); - struct arm_smmu_l1_ctx_desc *l1_desc; + struct arm_smmu_cdtab_l2 **l2ptr = &cd_table->l2.l2ptrs[idx]; - l1_desc = &cd_table->l1_desc[idx]; - if (!l1_desc->l2ptr) { + if (!*l2ptr) { dma_addr_t l2ptr_dma; - l1_desc->l2ptr = dma_alloc_coherent( - smmu->dev, - CTXDESC_L2_ENTRIES * sizeof(struct arm_smmu_cd), - &l2ptr_dma, GFP_KERNEL); - if (!l1_desc->l2ptr) + *l2ptr = dma_alloc_coherent(smmu->dev, sizeof(**l2ptr), + &l2ptr_dma, GFP_KERNEL); + if (!*l2ptr) return NULL; - arm_smmu_write_cd_l1_desc( - &((struct arm_smmu_cdtab_l1 *) - cd_table->cdtab)[idx], - l2ptr_dma); + arm_smmu_write_cd_l1_desc(&cd_table->l2.l1tab[idx], + l2ptr_dma); /* An invalid L1CD can be cached */ arm_smmu_sync_cd(master, ssid, false); } @@ -1336,7 +1331,7 @@ void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid) struct arm_smmu_cd target = {}; struct arm_smmu_cd *cdptr; - if (!master->cd_table.cdtab) + if (!arm_smmu_cdtab_allocated(&master->cd_table)) return; cdptr = arm_smmu_get_cd_ptr(master, ssid); if (WARN_ON(!cdptr)) @@ -1346,8 +1341,6 @@ void arm_smmu_clear_cd(struct arm_smmu_master *master, ioasid_t ssid) static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master) { - int ret; - size_t l1size; size_t max_contexts; struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; @@ -1358,71 +1351,67 @@ static int arm_smmu_alloc_cd_tables(struct arm_smmu_master *master) if (!(smmu->features & ARM_SMMU_FEAT_2_LVL_CDTAB) || max_contexts <= CTXDESC_L2_ENTRIES) { cd_table->s1fmt = STRTAB_STE_0_S1FMT_LINEAR; - cd_table->num_l1_ents = max_contexts; + cd_table->linear.num_ents = max_contexts; - l1size = max_contexts * sizeof(struct arm_smmu_cd); + cd_table->linear.table = dma_alloc_coherent( + smmu->dev, max_contexts * sizeof(struct arm_smmu_cd), + &cd_table->cdtab_dma, GFP_KERNEL); + if (!cd_table->linear.table) + return -ENOMEM; } else { cd_table->s1fmt = STRTAB_STE_0_S1FMT_64K_L2; - cd_table->num_l1_ents = DIV_ROUND_UP(max_contexts, - CTXDESC_L2_ENTRIES); + cd_table->l2.num_l1_ents = + DIV_ROUND_UP(max_contexts, CTXDESC_L2_ENTRIES); - cd_table->l1_desc = kcalloc(cd_table->num_l1_ents, - sizeof(*cd_table->l1_desc), - GFP_KERNEL); - if (!cd_table->l1_desc) + cd_table->l2.l2ptrs = kcalloc(cd_table->l2.num_l1_ents, + sizeof(*cd_table->l2.l2ptrs), + GFP_KERNEL); + if (!cd_table->l2.l2ptrs) return -ENOMEM; - l1size = cd_table->num_l1_ents * - sizeof(struct arm_smmu_cdtab_l1); + cd_table->l2.l1tab = dma_alloc_coherent( + smmu->dev, + cd_table->l2.num_l1_ents * + sizeof(struct arm_smmu_cdtab_l1), + &cd_table->cdtab_dma, GFP_KERNEL); + if (!cd_table->l2.l1tab) { + kfree(cd_table->l2.l2ptrs); + cd_table->l2.l2ptrs = NULL; + return -ENOMEM; + } } - - cd_table->cdtab = dma_alloc_coherent(smmu->dev, l1size, - &cd_table->cdtab_dma, GFP_KERNEL); - if (!cd_table->cdtab) { - dev_warn(smmu->dev, "failed to allocate context descriptor\n"); - ret = -ENOMEM; - goto err_free_l1; - } - return 0; - -err_free_l1: - if (cd_table->l1_desc) { - kfree(cd_table->l1_desc); - cd_table->l1_desc = NULL; - } - return ret; } static void arm_smmu_free_cd_tables(struct arm_smmu_master *master) { int i; - size_t l1size; struct arm_smmu_device *smmu = master->smmu; struct arm_smmu_ctx_desc_cfg *cd_table = &master->cd_table; - if (cd_table->l1_desc) { - for (i = 0; i < cd_table->num_l1_ents; i++) { - if (!cd_table->l1_desc[i].l2ptr) + if (cd_table->s1fmt != STRTAB_STE_0_S1FMT_LINEAR) { + for (i = 0; i < cd_table->l2.num_l1_ents; i++) { + if (!cd_table->l2.l2ptrs[i]) continue; dma_free_coherent(smmu->dev, - sizeof(*cd_table->l1_desc[i].l2ptr), - cd_table->l1_desc[i].l2ptr, - arm_smmu_cd_l1_get_desc(&( - (struct arm_smmu_cdtab_l1 *) - cd_table->cdtab)[i])); + sizeof(*cd_table->l2.l2ptrs[i]), + cd_table->l2.l2ptrs[i], + arm_smmu_cd_l1_get_desc( + &cd_table->l2.l1tab[i])); } - kfree(cd_table->l1_desc); + kfree(cd_table->l2.l2ptrs); - l1size = cd_table->num_l1_ents * - sizeof(struct arm_smmu_cdtab_l1); + dma_free_coherent(smmu->dev, + cd_table->l2.num_l1_ents * + sizeof(struct arm_smmu_cdtab_l1), + cd_table->l2.l1tab, cd_table->cdtab_dma); } else { - l1size = cd_table->num_l1_ents * sizeof(struct arm_smmu_cd); + dma_free_coherent(smmu->dev, + cd_table->linear.num_ents * + sizeof(struct arm_smmu_cd), + cd_table->linear.table, cd_table->cdtab_dma); } - - dma_free_coherent(smmu->dev, l1size, cd_table->cdtab, - cd_table->cdtab_dma); } bool arm_smmu_free_asid(struct arm_smmu_ctx_desc *cd) @@ -2937,7 +2926,7 @@ static void arm_smmu_release_device(struct device *dev) arm_smmu_disable_pasid(master); arm_smmu_remove_master(master); - if (master->cd_table.cdtab) + if (arm_smmu_cdtab_allocated(&master->cd_table)) arm_smmu_free_cd_tables(master); kfree(master); } 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 7ed8d53423cf56..35879064c0f725 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -616,20 +616,30 @@ struct arm_smmu_ctx_desc { struct mm_struct *mm; }; -struct arm_smmu_l1_ctx_desc { - struct arm_smmu_cdtab_l2 *l2ptr; -}; - struct arm_smmu_ctx_desc_cfg { - __le64 *cdtab; + union { + struct { + struct arm_smmu_cd *table; + unsigned int num_ents; + } linear; + struct { + struct arm_smmu_cdtab_l1 *l1tab; + struct arm_smmu_cdtab_l2 **l2ptrs; + unsigned int num_l1_ents; + } l2; + }; dma_addr_t cdtab_dma; - struct arm_smmu_l1_ctx_desc *l1_desc; - unsigned int num_l1_ents; u8 s1fmt; /* log2 of the maximum number of CDs supported by this table */ u8 s1cdmax; }; +static inline bool +arm_smmu_cdtab_allocated(struct arm_smmu_ctx_desc_cfg *cfg) +{ + return cfg->linear.table || cfg->l2.l1tab; +} + struct arm_smmu_s2_cfg { u16 vmid; };