Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Robin Murphy and committed by
Joerg Roedel
ba503cf4 0c345792

+8 -11
+8 -11
drivers/of/device.c
··· 96 96 const struct bus_dma_region *map = NULL; 97 97 struct device_node *bus_np; 98 98 u64 dma_start = 0; 99 - u64 mask, end, size = 0; 99 + u64 mask, end = 0; 100 100 bool coherent; 101 101 int iommu_ret; 102 102 int ret; ··· 118 118 return ret == -ENODEV ? 0 : ret; 119 119 } else { 120 120 const struct bus_dma_region *r = map; 121 - u64 dma_end = 0; 122 121 123 122 /* Determine the overall bounds of all DMA regions */ 124 123 for (dma_start = ~0; r->size; r++) { 125 124 /* Take lower and upper limits */ 126 125 if (r->dma_start < dma_start) 127 126 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; 130 129 } 131 - size = dma_end - dma_start; 132 130 } 133 131 134 132 /* ··· 140 142 dev->dma_mask = &dev->coherent_dma_mask; 141 143 } 142 144 143 - if (!size && dev->coherent_dma_mask) 144 - size = max(dev->coherent_dma_mask, dev->coherent_dma_mask + 1); 145 - else if (!size) 146 - size = 1ULL << 32; 145 + if (!end && dev->coherent_dma_mask) 146 + end = dev->coherent_dma_mask; 147 + else if (!end) 148 + end = (1ULL << 32) - 1; 147 149 148 150 /* 149 151 * Limit coherent and dma mask based on size and default mask 150 152 * set by the driver. 151 153 */ 152 - end = dma_start + size - 1; 153 154 mask = DMA_BIT_MASK(ilog2(end) + 1); 154 155 dev->coherent_dma_mask &= mask; 155 156 *dev->dma_mask &= mask; ··· 182 185 } else 183 186 dev_dbg(dev, "device is behind an iommu\n"); 184 187 185 - arch_setup_dma_ops(dev, dma_start, size, coherent); 188 + arch_setup_dma_ops(dev, dma_start, end - dma_start + 1, coherent); 186 189 187 190 if (iommu_ret) 188 191 of_dma_set_restricted_buffer(dev, np);