From patchwork Fri Feb 9 16:50:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Murphy X-Patchwork-Id: 199004 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp985834dyd; Fri, 9 Feb 2024 08:56:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IGond5YfBlfSbV76Wg78Epn+XjZ/qNQw+crb2cu/Rn6jsFSnPnVxJHR4Rsc5FAEnviskfs3 X-Received: by 2002:a05:6402:649:b0:560:9255:7580 with SMTP id u9-20020a056402064900b0056092557580mr2130813edx.17.1707497761892; Fri, 09 Feb 2024 08:56:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707497761; cv=pass; d=google.com; s=arc-20160816; b=IBHlHPt5uzh/F1mCNjNIAKBBt37X5dnkN3NjgCGudF1p58Veo6amPoTfNp+NNvlqv6 fNqyQ7w2qtpfZUORp8I7CmDM4CDI5RaNWa8uHF3D920dT7uy92AKF8tW6jBaVZzfoWRp x8mtSVMg9paB2W8kF0/VBuQgTjBwzrJfKpEriPlIOjN+Szd+SXVFYFinlpoBw46t2hTh VRjHBXx8FS2EhOco9mIdgYQ5R+tWbwBfxHNzvWmWPo53YLnh1MtpChezPSaJsgzF3qev bik1zuofr3yTGjtRTflVJbGR9IOh4RiVltfhDS94toF9VAwkVhHNcKZ9pnXIOPG+eZNa 52dg== 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=dabkLraXIIijrJwTv2DH8ou0CHhgI6xbXomRUibwvN8=; fh=swCsI5k4yfFjchCGAtly0b9TCGiYLQ0R5uhNmkAJhaA=; b=NwU+UbdL3Rop8IzFn7e00T88FXUxS65M434L352nJOhSfbzMM4XU1lGhmN0Qma+KvU 8jQ27fh3ByHG3ag5d/qmqFO77vx+w3lebAtXX+2c6va7+P8ObYAGyYqYLL8mdm6w5ybM /DZUHt/fUJKk0t0uuSz69zmkv9fo4fDDvF7/SgxU7L9P64X3TmeMIOK9i3oiM+giF7Jr JW8V5S6QAXX2JKe4iXRauh+ES+G8cE067S/xs0kAg/Or2a8TbrsK/0cCVS+9qIpM9WhC CLfScXO1AYV3NY3fNswBMA7IbUBIbpWpDAt0cdwMA3ueyYi1sfJO7XLl6o7aNJ7GZU1F tfZA==; 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-59676-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59676-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCVGqeEKRqJNPrRWTWwVFEXPb9Ee3ZuwIJR4OsTBDRDc8fkXbD6Ug4QNKy0R8EmXBWw6ZI9NwFWn+Ocbr7GmfVQWH1HcRQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id d15-20020a056402078f00b00560f3a68af4si1072940edy.355.2024.02.09.08.56.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:56:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-59676-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; 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-59676-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-59676-ouuuleilei=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 83AA71F211F6 for ; Fri, 9 Feb 2024 16:56:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 28D7A12BEA7; Fri, 9 Feb 2024 16:50:44 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9798812B15D; Fri, 9 Feb 2024 16:50:39 +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=1707497442; cv=none; b=XmN0xumVhp0jkPWiMVaZl0HiP8CBOsQgqy46oEMCwrzNdBvcsQ8qNeOsYW/NWH4zy9ixFGkCVpNjAXexbDASFV4FA3CR5j4wI2RVNcKddecop8kI5ZZsizGA1KPe0AaC4C/VBicvkBgNccR7xCiazAl7qpiG4OQ8oKfeMifX1gY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707497442; c=relaxed/simple; bh=xTtinJLnaC/I9VC5UbTSJ2+ulMHcbqG/ydth+2lej9Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q++7b1V9VxKSQQFC75cgdNZTWGNhJlFAtS959fSvFf8+Gk1Aqz5MlOX2UGXRGMjMEOYmfFe/57CO2uisJ88JTbfrwx0/nxfhxIjnznXVNIfHakw3oqSK29foV4WV6RxYz7O6YUAUlyDKDOr+IvJCJ7CkMAq1FrCju/F6QrxEE5Y= 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 11730DA7; Fri, 9 Feb 2024 08:51:21 -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 08BD53F64C; Fri, 9 Feb 2024 08:50:34 -0800 (PST) From: Robin Murphy To: 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 , Christoph Hellwig , Jason Gunthorpe Subject: [PATCH v3 4/7] dma-mapping: Add helpers for dma_range_map bounds Date: Fri, 9 Feb 2024 16:50:01 +0000 Message-Id: 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790441173400062946 X-GMAIL-MSGID: 1790441173400062946 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: Jason Gunthorpe 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