xref: /linux/drivers/acpi/arm64/dma.c (revision da5b2ad1c2f18834cb1ce429e2e5a5cf5cbdf21b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/acpi.h>
3 #include <linux/acpi_iort.h>
4 #include <linux/device.h>
5 #include <linux/dma-direct.h>
6 
7 void acpi_arch_dma_setup(struct device *dev)
8 {
9 	int ret;
10 	u64 end, mask;
11 	const struct bus_dma_region *map = NULL;
12 
13 	/*
14 	 * If @dev is expected to be DMA-capable then the bus code that created
15 	 * it should have initialised its dma_mask pointer by this point. For
16 	 * now, we'll continue the legacy behaviour of coercing it to the
17 	 * coherent mask if not, but we'll no longer do so quietly.
18 	 */
19 	if (!dev->dma_mask) {
20 		dev_warn(dev, "DMA mask not set\n");
21 		dev->dma_mask = &dev->coherent_dma_mask;
22 	}
23 
24 	if (dev->coherent_dma_mask)
25 		end = dev->coherent_dma_mask;
26 	else
27 		end = (1ULL << 32) - 1;
28 
29 	ret = acpi_dma_get_range(dev, &map);
30 	if (!ret && map) {
31 		end = dma_range_map_max(map);
32 		dev->dma_range_map = map;
33 	}
34 
35 	if (ret == -ENODEV)
36 		ret = iort_dma_get_ranges(dev, &end);
37 	if (!ret) {
38 		/*
39 		 * Limit coherent and dma mask based on size retrieved from
40 		 * firmware.
41 		 */
42 		mask = DMA_BIT_MASK(ilog2(end) + 1);
43 		dev->bus_dma_limit = end;
44 		dev->coherent_dma_mask = min(dev->coherent_dma_mask, mask);
45 		*dev->dma_mask = min(*dev->dma_mask, mask);
46 	}
47 }
48