Skip to content

Commit ba503cf

Browse files
rmurphy-armjoergroedel
authored andcommitted
OF: Simplify DMA range calculations
Juggling start, end, and size values for a range is somewhat redundant and a little hard to follow. Consolidate down to just using inclusive start and end, which saves us worrying about size overflows for full 64-bit ranges (note that passing a potentially-overflowed value through to arch_setup_dma_ops() is benign for all current implementations, and this is working towards removing that anyway). Acked-by: Rob Herring <robh@kernel.org> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Robin Murphy <robin.murphy@arm.com> Link: https://lore.kernel.org/r/3e0a72fe3d79eae660e4284bb32f2cb39868ccd7.1713523152.git.robin.murphy@arm.com Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent 0c34579 commit ba503cf

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

drivers/of/device.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
9696
const struct bus_dma_region *map = NULL;
9797
struct device_node *bus_np;
9898
u64 dma_start = 0;
99-
u64 mask, end, size = 0;
99+
u64 mask, end = 0;
100100
bool coherent;
101101
int iommu_ret;
102102
int ret;
@@ -118,17 +118,15 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
118118
return ret == -ENODEV ? 0 : ret;
119119
} else {
120120
const struct bus_dma_region *r = map;
121-
u64 dma_end = 0;
122121

123122
/* Determine the overall bounds of all DMA regions */
124123
for (dma_start = ~0; r->size; r++) {
125124
/* Take lower and upper limits */
126125
if (r->dma_start < dma_start)
127126
dma_start = r->dma_start;
128-
if (r->dma_start + r->size > dma_end)
129-
dma_end = r->dma_start + r->size;
127+
if (r->dma_start + r->size > end)
128+
end = r->dma_start + r->size;
130129
}
131-
size = dma_end - dma_start;
132130
}
133131

134132
/*
@@ -142,16 +140,15 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
142140
dev->dma_mask = &dev->coherent_dma_mask;
143141
}
144142

145-
if (!size && dev->coherent_dma_mask)
146-
size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1);
147-
else if (!size)
148-
size = 1ULL << 32;
143+
if (!end && dev->coherent_dma_mask)
144+
end = dev->coherent_dma_mask;
145+
else if (!end)
146+
end = (1ULL << 32) - 1;
149147

150148
/*
151149
* Limit coherent and dma mask based on size and default mask
152150
* set by the driver.
153151
*/
154-
end = dma_start + size - 1;
155152
mask = DMA_BIT_MASK(ilog2(end) + 1);
156153
dev->coherent_dma_mask &= mask;
157154
*dev->dma_mask &= mask;
@@ -185,7 +182,7 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
185182
} else
186183
dev_dbg(dev, "device is behind an iommu\n");
187184

188-
arch_setup_dma_ops(dev, dma_start, size, coherent);
185+
arch_setup_dma_ops(dev, dma_start, end - dma_start + 1, coherent);
189186

190187
if (iommu_ret)
191188
of_dma_set_restricted_buffer(dev, np);

0 commit comments

Comments
 (0)