dma.c 1.4 KB

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