Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp4756225rdh; Wed, 29 Nov 2023 09:44:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IGjqCkC4Omq34owhkZkxaASAd3c8/yjJpvshWpAjUkNL/iPs1QMEs6JzHay0eaWxYUP+Gx8 X-Received: by 2002:a05:6a20:4287:b0:18c:432b:c075 with SMTP id o7-20020a056a20428700b0018c432bc075mr16214678pzj.42.1701279848606; Wed, 29 Nov 2023 09:44:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701279848; cv=none; d=google.com; s=arc-20160816; b=AzL8X2s2cvyoXD3rXaxWF/aod1GkxSd5KffHwIn29prFUOahrecU+/nlzMH4z4LTsF 4vIJ9z3JCKdMIWXQZysLk96S0bAAiG+Nbe0wOfUv3U5PsRWuFy1pXnD39VaSlsKDot5i QAq9IL5wF09ByvKlUC/3yPjwZWdBJJYswOnxsMjcO4pe3elpA/m8nC1lCA9tuYVwUH2P 5/qII7TiwRk1DOVyxXXliYgcQKdqRit1KjseUQ3pF2LicEwTJ74KUWzkRdMdl6WLP3lG oj4L8s5uY7/qM9FDJm9OVL90RJXeYabsl2rbh6dWKPGqBy3RY21DV/2VxNCO76/F3B5z yabg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=mEkPR7Qddxy43t5YZARqZXrFibTw/7tnAXEFa0RAjUE=; fh=GNBXEYaBv/m3jVEMpTGTRjnFoUB0WOii+EMJk4vaWPQ=; b=cdkna+WJwvw/aHtDEGQc54ANcoB44IRFd3IZn40R68SlhKKifzaefVeItaH74N6yBd o3HFYZ3x74lbB/FgSyWUnrIAhrUClOQUjblTJPDmWY16BWaICzlmfoTZxpwMdUiMDdCW Vz4tEwmp+hMFztxIUh1is96ViXZ+nCjH4CfYuGhSEwg4yWdJY6FqVQazGL+Ebq4EHuBb fna/MJN98mZ2WHaVVGLLNHvHJxvxVcRfXRTi7ACxzma83BqTM9945vkS07T58YqSX+4w bnhYNcGUGmxfyFGsBPlfQNy5tMlJUDEj38FdkjbNwYUjQFYNiwoFp+dUgo1mhjw1wfOh IaUA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id ka9-20020a056a00938900b006bd0842622asi14661206pfb.239.2023.11.29.09.44.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Nov 2023 09:44:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id B47EC8040820; Wed, 29 Nov 2023 09:44:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232463AbjK2Rnf (ORCPT + 99 others); Wed, 29 Nov 2023 12:43:35 -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: 8bit X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 29 Nov 2023 09:44:05 -0800 (PST) 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 --- 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 = 0; + u64 mask, end; const struct bus_dma_region *map = NULL; ret = acpi_dma_get_range(dev, &map); if (!ret && map) { - const struct bus_dma_region *r = map; - - for (end = 0; r->size; r++) { - if (r->dma_start + r->size - 1 > end) - end = r->dma_start + r->size - 1; - } + end = dma_range_map_max(map); mask = DMA_BIT_MASK(ilog2(end) + 1); dev->bus_dma_limit = 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) ret = acpi_dma_get_range(dev, &map); if (!ret && map) { - const struct bus_dma_region *r = map; - - for (end = 0; r->size; r++) { - if (r->dma_start + r->size - 1 > end) - end = r->dma_start + r->size - 1; - } - + end = dma_range_map_max(map); dev->dma_range_map = map; } 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 device_node *np, if (!force_dma) return ret == -ENODEV ? 0 : ret; } else { - const struct bus_dma_region *r = map; - /* Determine the overall bounds of all DMA regions */ - for (dma_start = ~0; r->size; r++) { - /* Take lower and upper limits */ - if (r->dma_start < dma_start) - dma_start = r->dma_start; - if (r->dma_start + r->size > end) - end = r->dma_start + r->size; - } + dma_start = dma_range_map_min(map); + end = dma_range_map_max(map); } /* 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 device *dev, return (phys_addr_t)-1; } +static inline dma_addr_t dma_range_map_min(const struct bus_dma_region *map) +{ + dma_addr_t ret = U64_MAX; + + for (; map->size; map++) + ret = min(ret, map->dma_start); + return ret; +} + +static inline dma_addr_t dma_range_map_max(const struct bus_dma_region *map) +{ + dma_addr_t ret = 0; + + for (; map->size; map++) + ret = max(ret, map->dma_start + map->size - 1); + return ret; +} + #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA #include #ifndef phys_to_dma_unencrypted -- 2.39.2.101.g768bb238c484.dirty