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 	if (dev->dma_range_map) {
30 		dev_dbg(dev, "dma_range_map already set\n");
31 		return;
32 	}
33 
34 	ret = acpi_dma_get_range(dev, &map);
35 	if (!ret && map) {
36 		end = dma_range_map_max(map);
37 		dev->dma_range_map = map;
38 	}
39 
40 	if (ret == -ENODEV)
41 		ret = iort_dma_get_ranges(dev, &end);
42 	if (!ret) {
43 		/*
44 		 * Limit coherent and dma mask based on size retrieved from
45 		 * firmware.
46 		 */
47 		mask = DMA_BIT_MASK(ilog2(end) + 1);
48 		dev->bus_dma_limit = end;
49 		dev->coherent_dma_mask = min(dev->coherent_dma_mask, mask);
50 		*dev->dma_mask = min(*dev->dma_mask, mask);
51 	}
52 }
53