dma.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. void acpi_arch_dma_setup(struct device *dev)
  7. {
  8. int ret;
  9. u64 end, mask;
  10. const struct bus_dma_region *map = NULL;
  11. /*
  12. * If @dev is expected to be DMA-capable then the bus code that created
  13. * it should have initialised its dma_mask pointer by this point. For
  14. * now, we'll continue the legacy behaviour of coercing it to the
  15. * coherent mask if not, but we'll no longer do so quietly.
  16. */
  17. if (!dev->dma_mask) {
  18. dev_warn(dev, "DMA mask not set\n");
  19. dev->dma_mask = &dev->coherent_dma_mask;
  20. }
  21. if (dev->coherent_dma_mask)
  22. end = dev->coherent_dma_mask;
  23. else
  24. end = (1ULL << 32) - 1;
  25. if (dev->dma_range_map) {
  26. dev_dbg(dev, "dma_range_map already set\n");
  27. return;
  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. if (ret == -ENODEV)
  35. ret = iort_dma_get_ranges(dev, &end);
  36. if (!ret) {
  37. /*
  38. * Limit coherent and dma mask based on size retrieved from
  39. * firmware.
  40. */
  41. mask = DMA_BIT_MASK(ilog2(end) + 1);
  42. dev->bus_dma_limit = end;
  43. dev->coherent_dma_mask = min(dev->coherent_dma_mask, mask);
  44. *dev->dma_mask = min(*dev->dma_mask, mask);
  45. }
  46. }