Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp772574lqt; Fri, 19 Apr 2024 09:56:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVADJEFtFoTNqnilMp+dliXBOqzN+9lFBm7dSxHKAucaKKAj4zeVHjeTUVLp1vRbN4Sx/6GnnolY2ci2wIUiK+w4Zhl+pqIbkMCLbALow== X-Google-Smtp-Source: AGHT+IHGPLVzyrLS29xe1Kh4X7i3g4on4gjc8ucNteZOOwN4ksdXcDZgBthCmb1yYypdLi3STzu3 X-Received: by 2002:a05:6a21:7884:b0:1a3:c515:202c with SMTP id bf4-20020a056a21788400b001a3c515202cmr3330300pzc.62.1713545776069; Fri, 19 Apr 2024 09:56:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713545776; cv=pass; d=google.com; s=arc-20160816; b=y41QIWG+nN5zQ2FZS6lcu5wIbQDxzqfqM8B3H9p4tGUJVUiadgasB8loSV18gqNVMJ o/7kta2uj3G/Om6YnGlcAjKddSx8aPLOGz/ilBvgtK9bCghwj0J0ilEGe5Z1XjsKA3NX qTe6/zoNmvu9X7OUOwbpMgJq6fKUDYAM4URyYC/OavlzHT5zHLKl/mQENWhGTRsHszbm 20lAmGXY0jxAwd59pIWZWVfC6lT0mIy2aiN9+TH9eZP80tFkdxScRwr/mQw2CgSMwpkZ Hm9LIMMTX8KU9CqBTizIJ5FvgtWphkT3td8jPSyMzgCGHHA1wJ05jSSH8/qXJ666N49F CZRg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=yqSRWyJun/A2zSdwxQsMDuB7ZjXkkDRklpRMdDGuGXs=; fh=GJ1PANsXexk7H82iWwbLkoddLQ1QGjX0jddvZkCL7kQ=; b=gJT0CQK4JtoS9WET4wQETrohXXmnf0/7g4Astabx2k09vcTZLp6AroFZPvWs6ayM/h njrVulcM1Jf9ejBXhhEIGylc17lN+R6emaVRjSGR8fgclwi+/FHh7Wopa+VRT2zU1cmS QYwZO9dHaD1qhvp+RKmeYQzHvPQDxoZuvLynCzRjEYAVcDGmcHq7pEgfhE5d+hUi0w8C 14tbm5qBkXNpVCsPSNAluT0VfKvtXj2BAO8PxE3u9Wp3RFB7/+3ZR276Eh2LB+jyz5Te J7IXJ9W6Q0vs2A6GIVjvmDBS89fN+Lb5GC1CwdVZTDmdfLOWMx8qwDkLqdg/58mZcPhw jcdw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-151788-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151788-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id s124-20020a625e82000000b006f085e53773si3541227pfb.344.2024.04.19.09.56.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 09:56:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151788-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-151788-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151788-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id AA8802823D0 for ; Fri, 19 Apr 2024 16:56:05 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58A6613BC20; Fri, 19 Apr 2024 16:55:26 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 54FE5139583; Fri, 19 Apr 2024 16:55:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713545725; cv=none; b=l9VFKzGVu6Awex2KxxzCoSMmGeMjHjOTC1V1cRy846wurZ5Rw0MmS370dfq+ai/H7zAOjdSagF2bVolYuew1yffPUqq7nl//dO+H8PxHYhojcCzAIEOMki0mPg4s2QNhY9weIa9jAuB6WmIAmPjKEufM26AjD8vDxVBPHuxHTFk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713545725; c=relaxed/simple; bh=7pikaz8fCqgc0LdKzAXlFZuiOFcIYFbQN/pB2NtzKL0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oFK+4QSfkCdkaQPHt1+kqfN4i42TuxaZUA4hDE6ffNXHwL1LF+f2vxLTng85PcB1aHpUloR9gR2kaI9xtUWS/8NN1M/Gt9IdebLzP3izGVBgqcKwTl0MLQh6lABZeIfdM0PNjWlKYpB4xSAZfk20VWqjZ621xTN/Y4np/ra44+c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 2C313153B; Fri, 19 Apr 2024 09:55:52 -0700 (PDT) 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 EC63C3F792; Fri, 19 Apr 2024 09:55:18 -0700 (PDT) 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, Rob Herring , Jason Gunthorpe Subject: [PATCH v4 4/7] dma-mapping: Add helpers for dma_range_map bounds Date: Fri, 19 Apr 2024 17:54:43 +0100 Message-Id: <45ec52f033ec4dfb364e23f48abaf787f612fa53.1713523152.git.robin.murphy@arm.com> X-Mailer: git-send-email 2.39.2.101.g768bb238c484.dirty In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Acked-by: Rob Herring Reviewed-by: Christoph Hellwig Reviewed-by: Hanjun Guo # For arm64 Reviewed-by: Jason Gunthorpe Tested-by: Hanjun Guo Signed-off-by: Robin Murphy --- v2: fix warning for 32-bit builds --- 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 841ccd3a19d1..9e7963972fa7 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..edbe13d00776 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 = (dma_addr_t)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