direct.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2018 Christoph Hellwig.
  4. *
  5. * DMA operations that map physical memory directly without using an IOMMU.
  6. */
  7. #ifndef _KERNEL_DMA_DIRECT_H
  8. #define _KERNEL_DMA_DIRECT_H
  9. #include <linux/dma-direct.h>
  10. int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
  11. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  12. unsigned long attrs);
  13. bool dma_direct_can_mmap(struct device *dev);
  14. int dma_direct_mmap(struct device *dev, struct vm_area_struct *vma,
  15. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  16. unsigned long attrs);
  17. bool dma_direct_need_sync(struct device *dev, dma_addr_t dma_addr);
  18. int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
  19. enum dma_data_direction dir, unsigned long attrs);
  20. size_t dma_direct_max_mapping_size(struct device *dev);
  21. #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE) || \
  22. defined(CONFIG_SWIOTLB)
  23. void dma_direct_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
  24. int nents, enum dma_data_direction dir);
  25. #else
  26. static inline void dma_direct_sync_sg_for_device(struct device *dev,
  27. struct scatterlist *sgl, int nents, enum dma_data_direction dir)
  28. {
  29. }
  30. #endif
  31. #if defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU) || \
  32. defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL) || \
  33. defined(CONFIG_SWIOTLB)
  34. void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
  35. int nents, enum dma_data_direction dir, unsigned long attrs);
  36. void dma_direct_sync_sg_for_cpu(struct device *dev,
  37. struct scatterlist *sgl, int nents, enum dma_data_direction dir);
  38. #else
  39. static inline void dma_direct_unmap_sg(struct device *dev,
  40. struct scatterlist *sgl, int nents, enum dma_data_direction dir,
  41. unsigned long attrs)
  42. {
  43. }
  44. static inline void dma_direct_sync_sg_for_cpu(struct device *dev,
  45. struct scatterlist *sgl, int nents, enum dma_data_direction dir)
  46. {
  47. }
  48. #endif
  49. static inline void dma_direct_sync_single_for_device(struct device *dev,
  50. dma_addr_t addr, size_t size, enum dma_data_direction dir)
  51. {
  52. phys_addr_t paddr = dma_to_phys(dev, addr);
  53. if (unlikely(is_swiotlb_buffer(paddr)))
  54. swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_DEVICE);
  55. if (!dev_is_dma_coherent(dev))
  56. arch_sync_dma_for_device(paddr, size, dir);
  57. }
  58. static inline void dma_direct_sync_single_for_cpu(struct device *dev,
  59. dma_addr_t addr, size_t size, enum dma_data_direction dir)
  60. {
  61. phys_addr_t paddr = dma_to_phys(dev, addr);
  62. if (!dev_is_dma_coherent(dev)) {
  63. arch_sync_dma_for_cpu(paddr, size, dir);
  64. arch_sync_dma_for_cpu_all();
  65. }
  66. if (unlikely(is_swiotlb_buffer(paddr)))
  67. swiotlb_tbl_sync_single(dev, paddr, size, dir, SYNC_FOR_CPU);
  68. if (dir == DMA_FROM_DEVICE)
  69. arch_dma_mark_clean(paddr, size);
  70. }
  71. static inline dma_addr_t dma_direct_map_page(struct device *dev,
  72. struct page *page, unsigned long offset, size_t size,
  73. enum dma_data_direction dir, unsigned long attrs)
  74. {
  75. phys_addr_t phys = page_to_phys(page) + offset;
  76. dma_addr_t dma_addr = phys_to_dma(dev, phys);
  77. if (unlikely(swiotlb_force == SWIOTLB_FORCE))
  78. return swiotlb_map(dev, phys, size, dir, attrs);
  79. if (unlikely(!dma_capable(dev, dma_addr, size, true))) {
  80. if (swiotlb_force != SWIOTLB_NO_FORCE)
  81. return swiotlb_map(dev, phys, size, dir, attrs);
  82. dev_WARN_ONCE(dev, 1,
  83. "DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
  84. &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
  85. return DMA_MAPPING_ERROR;
  86. }
  87. if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  88. arch_sync_dma_for_device(phys, size, dir);
  89. return dma_addr;
  90. }
  91. static inline void dma_direct_unmap_page(struct device *dev, dma_addr_t addr,
  92. size_t size, enum dma_data_direction dir, unsigned long attrs)
  93. {
  94. phys_addr_t phys = dma_to_phys(dev, addr);
  95. if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  96. dma_direct_sync_single_for_cpu(dev, addr, size, dir);
  97. if (unlikely(is_swiotlb_buffer(phys)))
  98. swiotlb_tbl_unmap_single(dev, phys, size, size, dir,
  99. attrs | DMA_ATTR_SKIP_CPU_SYNC);
  100. }
  101. #endif /* _KERNEL_DMA_DIRECT_H */