From nobody Thu Dec 18 06:21:54 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8C56C4167B for ; Wed, 29 Nov 2023 17:43:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232715AbjK2Rnh (ORCPT ); Wed, 29 Nov 2023 12:43:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231434AbjK2Rn2 (ORCPT ); Wed, 29 Nov 2023 12:43:28 -0500 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 828D613E; Wed, 29 Nov 2023 09:43:33 -0800 (PST) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 410FD1595; Wed, 29 Nov 2023 09:44:20 -0800 (PST) Received: from e121345-lin.cambridge.arm.com (e121345-lin.cambridge.arm.com [10.1.196.40]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 3A6DA3F73F; Wed, 29 Nov 2023 09:43:29 -0800 (PST) From: Robin Murphy To: Joerg Roedel , Christoph Hellwig Cc: Vineet Gupta , Russell King , Catalin Marinas , Will Deacon , Huacai Chen , WANG Xuerui , Thomas Bogendoerfer , Paul Walmsley , Palmer Dabbelt , Albert Ou , Lorenzo Pieralisi , Hanjun Guo , Sudeep Holla , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Suravee Suthikulpanit , David Woodhouse , Lu Baolu , Niklas Schnelle , Matthew Rosato , Gerald Schaefer , Jean-Philippe Brucker , Rob Herring , Frank Rowand , Marek Szyprowski , Jason Gunthorpe , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-acpi@vger.kernel.org, iommu@lists.linux.dev, devicetree@vger.kernel.org Subject: [PATCH 4/7] dma-mapping: Add helpers for dma_range_map bounds Date: Wed, 29 Nov 2023 17:43:01 +0000 Message-Id: X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Several places want to compute the lower and/or upper bounds of a dma_range_map, so let's factor that out into reusable helpers. Signed-off-by: Robin Murphy Reviewed-by: Christoph Hellwig Reviewed-by: Jason Gunthorpe --- arch/loongarch/kernel/dma.c | 9 ++------- drivers/acpi/arm64/dma.c | 8 +------- drivers/of/device.c | 11 ++--------- include/linux/dma-direct.h | 18 ++++++++++++++++++ 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/arch/loongarch/kernel/dma.c b/arch/loongarch/kernel/dma.c index 7a9c6a9dd2d0..429555fb4e13 100644 --- a/arch/loongarch/kernel/dma.c +++ b/arch/loongarch/kernel/dma.c @@ -8,17 +8,12 @@ void acpi_arch_dma_setup(struct device *dev) { int ret; - u64 mask, end =3D 0; + u64 mask, end; const struct bus_dma_region *map =3D NULL; =20 ret =3D acpi_dma_get_range(dev, &map); if (!ret && map) { - const struct bus_dma_region *r =3D map; - - for (end =3D 0; r->size; r++) { - if (r->dma_start + r->size - 1 > end) - end =3D r->dma_start + r->size - 1; - } + end =3D dma_range_map_max(map); =20 mask =3D DMA_BIT_MASK(ilog2(end) + 1); dev->bus_dma_limit =3D end; diff --git a/drivers/acpi/arm64/dma.c b/drivers/acpi/arm64/dma.c index b98a149f8d50..52b2abf88689 100644 --- a/drivers/acpi/arm64/dma.c +++ b/drivers/acpi/arm64/dma.c @@ -28,13 +28,7 @@ void acpi_arch_dma_setup(struct device *dev) =20 ret =3D acpi_dma_get_range(dev, &map); if (!ret && map) { - const struct bus_dma_region *r =3D map; - - for (end =3D 0; r->size; r++) { - if (r->dma_start + r->size - 1 > end) - end =3D r->dma_start + r->size - 1; - } - + end =3D dma_range_map_max(map); dev->dma_range_map =3D map; } =20 diff --git a/drivers/of/device.c b/drivers/of/device.c index 51062a831970..66879edb4a61 100644 --- a/drivers/of/device.c +++ b/drivers/of/device.c @@ -117,16 +117,9 @@ int of_dma_configure_id(struct device *dev, struct dev= ice_node *np, if (!force_dma) return ret =3D=3D -ENODEV ? 0 : ret; } else { - const struct bus_dma_region *r =3D map; - /* Determine the overall bounds of all DMA regions */ - for (dma_start =3D ~0; r->size; r++) { - /* Take lower and upper limits */ - if (r->dma_start < dma_start) - dma_start =3D r->dma_start; - if (r->dma_start + r->size > end) - end =3D r->dma_start + r->size; - } + dma_start =3D dma_range_map_min(map); + end =3D dma_range_map_max(map); } =20 /* diff --git a/include/linux/dma-direct.h b/include/linux/dma-direct.h index 3eb3589ff43e..b77e3863daab 100644 --- a/include/linux/dma-direct.h +++ b/include/linux/dma-direct.h @@ -54,6 +54,24 @@ static inline phys_addr_t translate_dma_to_phys(struct d= evice *dev, return (phys_addr_t)-1; } =20 +static inline dma_addr_t dma_range_map_min(const struct bus_dma_region *ma= p) +{ + dma_addr_t ret =3D U64_MAX; + + for (; map->size; map++) + ret =3D min(ret, map->dma_start); + return ret; +} + +static inline dma_addr_t dma_range_map_max(const struct bus_dma_region *ma= p) +{ + dma_addr_t ret =3D 0; + + for (; map->size; map++) + ret =3D max(ret, map->dma_start + map->size - 1); + return ret; +} + #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA #include #ifndef phys_to_dma_unencrypted --=20 2.39.2.101.g768bb238c484.dirty