Commit fece6530 authored by Robin Murphy's avatar Robin Murphy Committed by Joerg Roedel

dma-mapping: Add helpers for dma_range_map bounds

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: default avatarRob Herring <robh@kernel.org>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: Hanjun Guo <guohanjun@huawei.com> # For arm64
Reviewed-by: default avatarJason Gunthorpe <jgg@nvidia.com>
Tested-by: default avatarHanjun Guo <guohanjun@huawei.com>
Signed-off-by: default avatarRobin Murphy <robin.murphy@arm.com>
Link: https://lore.kernel.org/r/45ec52f033ec4dfb364e23f48abaf787f612fa53.1713523152.git.robin.murphy@arm.comSigned-off-by: default avatarJoerg Roedel <jroedel@suse.de>
parent 91cfd679
...@@ -8,17 +8,12 @@ ...@@ -8,17 +8,12 @@
void acpi_arch_dma_setup(struct device *dev) void acpi_arch_dma_setup(struct device *dev)
{ {
int ret; int ret;
u64 mask, end = 0; u64 mask, end;
const struct bus_dma_region *map = NULL; const struct bus_dma_region *map = NULL;
ret = acpi_dma_get_range(dev, &map); ret = acpi_dma_get_range(dev, &map);
if (!ret && map) { if (!ret && map) {
const struct bus_dma_region *r = map; end = dma_range_map_max(map);
for (end = 0; r->size; r++) {
if (r->dma_start + r->size - 1 > end)
end = r->dma_start + r->size - 1;
}
mask = DMA_BIT_MASK(ilog2(end) + 1); mask = DMA_BIT_MASK(ilog2(end) + 1);
dev->bus_dma_limit = end; dev->bus_dma_limit = end;
......
...@@ -28,13 +28,7 @@ void acpi_arch_dma_setup(struct device *dev) ...@@ -28,13 +28,7 @@ void acpi_arch_dma_setup(struct device *dev)
ret = acpi_dma_get_range(dev, &map); ret = acpi_dma_get_range(dev, &map);
if (!ret && map) { if (!ret && map) {
const struct bus_dma_region *r = map; end = dma_range_map_max(map);
for (end = 0; r->size; r++) {
if (r->dma_start + r->size - 1 > end)
end = r->dma_start + r->size - 1;
}
dev->dma_range_map = map; dev->dma_range_map = map;
} }
......
...@@ -117,16 +117,9 @@ int of_dma_configure_id(struct device *dev, struct device_node *np, ...@@ -117,16 +117,9 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
if (!force_dma) if (!force_dma)
return ret == -ENODEV ? 0 : ret; return ret == -ENODEV ? 0 : ret;
} else { } else {
const struct bus_dma_region *r = map;
/* Determine the overall bounds of all DMA regions */ /* Determine the overall bounds of all DMA regions */
for (dma_start = ~0; r->size; r++) { dma_start = dma_range_map_min(map);
/* Take lower and upper limits */ end = dma_range_map_max(map);
if (r->dma_start < dma_start)
dma_start = r->dma_start;
if (r->dma_start + r->size > end)
end = r->dma_start + r->size;
}
} }
/* /*
......
...@@ -54,6 +54,24 @@ static inline phys_addr_t translate_dma_to_phys(struct device *dev, ...@@ -54,6 +54,24 @@ static inline phys_addr_t translate_dma_to_phys(struct device *dev,
return (phys_addr_t)-1; 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 #ifdef CONFIG_ARCH_HAS_PHYS_TO_DMA
#include <asm/dma-direct.h> #include <asm/dma-direct.h>
#ifndef phys_to_dma_unencrypted #ifndef phys_to_dma_unencrypted
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment