From patchwork Tue Aug 6 23:31:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13755482 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 B7CAAC52D6F for ; Tue, 6 Aug 2024 23:35:46 +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=avacmMrQ5myRDCNuypGXcU1mUVf8sauVWQzFhQgoo4o=; b=c2jZu9Epp6y87NUq0TGt4XWSoy 5zqcxnqwWc7sWBRJ349Eo2GGKp6FUc3/bxXfQXpbmfvzGdLiO3TI3Z+R0RT7JWMV/oUlmaB5Yciux Ziu8jcM6ijvoQ3qgt+7zukH2bpyZmSWNaasfC+QewrxW7dRf7xexVyEN27u61TvQ1iZC5FdezPpqy bqOITWMbgvfrQ4PUit6lf3ddVvZsohHmjoRywjH5O8nf+dGglVvOXsiUKyKqLjTRgAGmzYO8RZYMZ S2+EViCDfsI5JN0HCbx3s1OHnIwLHQTa5sQ8jq4OI4nQpeZ4jhVzulARDyuAbhd3lbFzqsmPYp88d +kSBHgpg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbTiJ-00000003KYn-1r39; Tue, 06 Aug 2024 23:35:35 +0000 Received: from mail-dm3nam02on20601.outbound.protection.outlook.com ([2a01:111:f403:2405::601] helo=NAM02-DM3-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbTef-00000003Jej-1f7G for linux-arm-kernel@lists.infradead.org; Tue, 06 Aug 2024 23:31:51 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ltgEMeE3eQVBWIXlVrvE+b5IQ68jBXW6A4wrKBn5cTsZFHAkX58Wq9TA4rcTPN8RiAkjTE5TvGaDhXZw35ss5ZX/8pO75wwnfOp5wmBJBTmXLVZn63Sf/dldNmK/WRgaA/3Ib1wD+1gryyRdOxaPAFtPHL30ADrOkBQw6za5Sf4BX287Q+gLoXd7rgyMxoQTifKnEPLUgUpGSeoLfa4Kg7VXLB3OTRzX4mEvChqj9hYrSNf0q80dtwX7ZBaqnUoZ3r6qydacrkbNxAexiKsI1VtoK9u/WHR9gCx4euo8VmTUl/co0T66DGt/N35rh6CBOx/k5wcS39D7RWxO5dzUdQ== 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=avacmMrQ5myRDCNuypGXcU1mUVf8sauVWQzFhQgoo4o=; b=MSQzFPeWFdAyZEbHiGiVrSuKDnDYalCT4Q2MfIdyfEjAegVvst7PV70Xd8cKxFJNGEvxCEkOuZczNVstWzwKBAu54j7b0icOPy9up6pnShFD4mqLBU2ncyvLo07xkqr8tNhIH7aUpRAiiqpkQs1TmGkAACMjNa7lVC5/vqLArs6/CeeMw234Iz7bLGGA9xGbr909lGa7BTWjl7DRJs1J49C52f6sObyJiiVoi23dcThqrAsTilN3ceFX3VCzQKKiw/XrJVLvuufiyC0MzZwo0HC04tlAr1DkrYiWECjpY4ntxuIiRUKTkXwM4VhuWOJsG9SastdVLIvqI79LPasR2A== 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=avacmMrQ5myRDCNuypGXcU1mUVf8sauVWQzFhQgoo4o=; b=HInd/qodDRFjO2oMqtys3ulw0zU4QcHRJ8Ud3Y/a/ObmfdlGcdJagdylnmWWGkUuS1woJad5/PPFOVFKxeYJsRSWo+E049iSLYjrbYY4Wng7A54TjRP/1U+901QpYMWe43leO6eiXUcPqHgv0RhvDUDS85u+58Bnb7ZH/Ywx+Gk1h9LZqlckjDZNIcVEARJELQeIKgT6q953wvstnNodvu9h2pxmnyvdblGU8eqMQUnBekTqsq0oIVtw0G+kutoOBYOx6baRJ5MVQdR4L5hs+JUfOHLnFVAFnczrInyg4Aj6CpSnrkbrWnbmEpYK6fVSL+ZU6CV5hgQWClSYNP+jug== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) by MN0PR12MB6248.namprd12.prod.outlook.com (2603:10b6:208:3c0::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7828.26; Tue, 6 Aug 2024 23:31:29 +0000 Received: from CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8]) by CH3PR12MB7763.namprd12.prod.outlook.com ([fe80::8b63:dd80:c182:4ce8%3]) with mapi id 15.20.7849.008; Tue, 6 Aug 2024 23:31:29 +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 v3 9/9] iommu/arm-smmu-v3: Reorganize struct arm_smmu_ctx_desc_cfg Date: Tue, 6 Aug 2024 20:31:23 -0300 Message-ID: <9-v3-9fef8cdc2ff6+150d1-smmuv3_tidy_jgg@nvidia.com> In-Reply-To: <0-v3-9fef8cdc2ff6+150d1-smmuv3_tidy_jgg@nvidia.com> References: X-ClientProxiedBy: BN9PR03CA0410.namprd03.prod.outlook.com (2603:10b6:408:111::25) To CH3PR12MB7763.namprd12.prod.outlook.com (2603:10b6:610:145::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR12MB7763:EE_|MN0PR12MB6248:EE_ X-MS-Office365-Filtering-Correlation-Id: afca6dc3-d63f-46c6-b202-08dcb66fe342 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|376014; X-Microsoft-Antispam-Message-Info: N4rrqHRTCglOh/bOy55zC6OepBVees8eNT3BQK3X3P5hLTFSgcnDcqX8oJ2oV1siRLOBSIBCA26D8kAHfmjgceA+zQhSEmYm3Bdxuo0U7TlsSNrLClEbMbX5MHb7fHDNrSJ9XfIpYehQcXtsOWYYrl6Bwk6t2/TCCIaAEThs5uTX0lRtoJT7smU7WJSjlP8VEDdGahRlpp6oJmcjOS6l1Zjt8UesbgdXehcWojpCq/zNpuRGb5E0Mkd+/T2+3nHtkJ9AlN2mI8qqosnB7/mgEWRYQpqdIx/lp2Qi04FpwQpeN58qFas4QxxdLdMcW82qEtDptqHOY+WRcdRQQXmjJAk2/pwJVk9xiabCE3lgFHVsQn29i+193dRZ7Vom6ab9BvnQZCu/nY8UhDsRCCMI/eOjP4ZdsdHz97CNJUFmFquTQjLxybawTfTc9TknPf7FstYq20ym77/n8dAIsjLP1td4GmzAcwuA5IQ4104lEXZtc7RQo8C37OGz7o2NqB6cZYIextsgrW6pXb/LZRok+QR3TuVkPol0WXk7nAT720oohYPrpnqz0hrndcZLQSs/YHobrPSU4tRfkFiNdbAcF4CbtAKpL/IG6GWecBJpHZkqPn54vrgfj5ivESOvPfTrcPpHd79jM16L02HwcxEmJqPCwREhTAWeeUvY1XTIUwOvb8Cxv7MJdQfcgtxX7psODXLjBKuNsoufWKtGRZeB9HBi2MtMA6OXQt2beOnwnMLYVTWJV5zbDEeXru+FMXpZnGXZjyMV5N1Bn6Yz3psZUq6v+XGDfuLhMawIaMqaxIdHJnDcp6G6J4WjQJ5H62HWyDmJUAGaKQn6JH1ulCyutB4mda1DfCfWbIz0wBOhlBNXSTKIJ2B1jgvxKOP9dnKVQjuFX2TL0YrLK4DveM1raB2pFULauD3NVg4WFID/KBvkGFAWTaRnHFMg8tyzIOaBsnG7BPV7byU2KTR0q5+pOAhQNxor8VlsVT8rYKh3R08+AmFNA0NoyFjPFzaT2aRHXBGaDorgjuQL/gpNBqVbQF9D1hDuLmK512l7AwOWd8soYYwB7nbi8WcMEdvoBu1YSwt4qKCfJ9xBl1u9XcKUtyJSwml/iZzc7AntJC2xVDKVNg5oOJ858+1idJlcTKJhzE6X+wLr/1dM9iG5KkTyvRsEgpuOatLhV8eOP4LsvBu0vfCkReNNqmZwhkVm1jyuGXA9e3YepFkEqm9Naf6oSRVgvb1J8s0tnfJLq8XBy79+gQ9Rut2oW8H829POhvL6EcNLggDEgs5H+kdC4mePf5A2xPRj0fQLJIoXGJ2lFlM4P8heN1B1JoiF0kaWmV9APOP9lEM23fSJB9Pc6T8vMg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH3PR12MB7763.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sgfWjqYYRo1JadIcLyXxiLGEUaVh3OvcUWdGvjgIwTNuKduAnowAOjzi5K5MZv1eUv7+J6Xwnn71kgYfv3OeryHMD7LxaSgSfhFMGnlUTg0CrN83iDno0hII+nQ0P9nHL2Q7Z2YilyK0kVe27SveccRFtv7GLs+Mm9tXX/crCeJ2obhQhxnBPqz0V+FpUW6aWEGbU6KxsZGrWuSi8x1fTKb0IKqPNqYGZA1FkT3CpYgLIHe8HvGL3rNTmdbchiKeS6BuaKs1Qha/QSaX9FtwGvMqczIZWDDtTcDu2+UE5SkHCo+h+Dr4BbleJzgvhOPhnOw5hE2uvo6xQfX/EEn/DOwqXNhemSDdbvUsLP7rLTOdeshM+pWMMqwCm/CqvnJ1FgDovdSl/IXVyCTUg1KHPXmcoYtKvGco3Q8FNyiIB9oBeWUICm8MMgiA2cbSsSoPIPXPLZ/k2H7P48v/PsPLcsYOMU4lO552lacwXS72BXPrfxs4DLgBn78AYFSLaRjojQA7qapHT+/4aVfYFa8CREZMF7gBW67aKXBC6MrRbHjqc9PVx88qx2HfYFpLhp5ILB8o2PtG+KIzkvNR+OzuAFPZN6obH48jYGSDtQa5dMOVCSBo2whyBq7D4P0aHu+GNmtYJh+MKlVIq/2LExe9IVBtm4No9bp+l4yvEWesKv8h+nhj/Q1fqLHY43ikig7OA9CaVpQaGC7JnTiccQy5N9zG/uQJzO4iqHPZEc2uEYngmiG4uvaxB844EXCJH/RKRAVtuhSacBPGtgVXWKIechD6jCp6vwZ6CtyCe+Mrnfvs3kPfwAHiJlJ/V/pLpaMKD+7+dZe1BxyNSf8of1hLjmgMVCAwTNpv0qLwpdlLEp8cHuyj3YWeo5896coz+tRkcRJqRnxUOye9q9+JGlW/5iY2+uLnsnNfOQNmIiKZTVOye4a9EAkULR0Pf8PNcmrh2gTLR4tdU3PdpyeBzehWpqM7Aby9yPdMAz9ZSwxXabrSmd0DXpeVGA0QNWZsTmyFCgabupbfWFtr4B5w38k8in8Gd8n8hK/B/S0oNHVd9qbKkbqhk0/EQi7jR15piCLnFPAwZ6lGSOdITdZVUlE/86MQ2HdZMsPX64qXECR0muLOrzpF4zERS0hW2GWFRQNngJeb2DC5NknsH1P+rS4lAz4ACALHkIFgaGclfNsUcWcpTP7pZuZp6PpSU1MR6ENyzfRmHZ9UqbsZZ4ZktAgKvzKXyAWLgHl/rmnLOiMXONa8nQbDXWmv4IZEfCb0MY8gpxOZGVplGzKtAOzqGCsjVUkHbPCVPjsct21+XL4QoBE2HT8bBbDqgbumqDdc23g/VbSdeNp+I9he08W0/cs8oWOQXfvs1bdqofTbEEyLk0Pn9x2PO0Ptcd+XZ7Ez7fqC62dH9XGOonvTGf7dBQh6hqdldwrpM3VPIfOIHlwwcj3zdilLma+8RCHYKnMGLXW187jsI/SO8/0PYwAotRaa662RVYfXvPe9hTF8FdiMB9C2+61yr/HL0+SVfFDdJQDXKcGkYBj66UplWcgNXrKN7xZB7OZTSg1CZvNG/J1oXm7CYpeRZrpZt7GfjSeRLjk1 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: afca6dc3-d63f-46c6-b202-08dcb66fe342 X-MS-Exchange-CrossTenant-AuthSource: CH3PR12MB7763.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2024 23:31:25.1948 (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: MF9OSrz3FQdiFk9yDjKnxCpJxdxtlgLTncFNIXmTFNKuOAQXwxzzL+fQjP64wAoI X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6248 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240806_163149_504432_46AB7D1A X-CRM114-Status: GOOD ( 17.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 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). Tested-by: Nicolin Chen Reviewed-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- 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 70b37d7f0e245d..e5db5325f7eaed 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -1196,19 +1196,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)]; } static struct arm_smmu_cd *arm_smmu_alloc_cd_ptr(struct arm_smmu_master *master, @@ -1220,30 +1220,25 @@ static 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); } @@ -1363,7 +1358,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)) @@ -1373,8 +1368,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; @@ -1385,71 +1378,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); } /* Stream table manipulation functions */ @@ -3299,7 +3288,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 681804a3f86bec..8851a7abb5f0f3 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -619,15 +619,19 @@ struct arm_smmu_ctx_desc { u16 asid; }; -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; unsigned int used_ssids; u8 in_ste; u8 s1fmt; @@ -635,6 +639,12 @@ struct arm_smmu_ctx_desc_cfg { u8 s1cdmax; }; +static inline bool +arm_smmu_cdtab_allocated(struct arm_smmu_ctx_desc_cfg *cfg) +{ + return cfg->linear.table || cfg->l2.l1tab; +} + /* True if the cd table has SSIDS > 0 in use. */ static inline bool arm_smmu_ssids_in_use(struct arm_smmu_ctx_desc_cfg *cd_table) {