dma-mapping-common.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef _ASM_GENERIC_DMA_MAPPING_H
  2. #define _ASM_GENERIC_DMA_MAPPING_H
  3. #include <linux/kmemcheck.h>
  4. #include <linux/scatterlist.h>
  5. #include <linux/dma-debug.h>
  6. #include <linux/dma-attrs.h>
  7. static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
  8. size_t size,
  9. enum dma_data_direction dir,
  10. struct dma_attrs *attrs)
  11. {
  12. struct dma_map_ops *ops = get_dma_ops(dev);
  13. dma_addr_t addr;
  14. kmemcheck_mark_initialized(ptr, size);
  15. BUG_ON(!valid_dma_direction(dir));
  16. addr = ops->map_page(dev, virt_to_page(ptr),
  17. (unsigned long)ptr & ~PAGE_MASK, size,
  18. dir, attrs);
  19. debug_dma_map_page(dev, virt_to_page(ptr),
  20. (unsigned long)ptr & ~PAGE_MASK, size,
  21. dir, addr, true);
  22. return addr;
  23. }
  24. static inline void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
  25. size_t size,
  26. enum dma_data_direction dir,
  27. struct dma_attrs *attrs)
  28. {
  29. struct dma_map_ops *ops = get_dma_ops(dev);
  30. BUG_ON(!valid_dma_direction(dir));
  31. if (ops->unmap_page)
  32. ops->unmap_page(dev, addr, size, dir, attrs);
  33. debug_dma_unmap_page(dev, addr, size, dir, true);
  34. }
  35. static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  36. int nents, enum dma_data_direction dir,
  37. struct dma_attrs *attrs)
  38. {
  39. struct dma_map_ops *ops = get_dma_ops(dev);
  40. int i, ents;
  41. struct scatterlist *s;
  42. for_each_sg(sg, s, nents, i)
  43. kmemcheck_mark_initialized(sg_virt(s), s->length);
  44. BUG_ON(!valid_dma_direction(dir));
  45. ents = ops->map_sg(dev, sg, nents, dir, attrs);
  46. debug_dma_map_sg(dev, sg, nents, ents, dir);
  47. return ents;
  48. }
  49. static inline void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
  50. int nents, enum dma_data_direction dir,
  51. struct dma_attrs *attrs)
  52. {
  53. struct dma_map_ops *ops = get_dma_ops(dev);
  54. BUG_ON(!valid_dma_direction(dir));
  55. debug_dma_unmap_sg(dev, sg, nents, dir);
  56. if (ops->unmap_sg)
  57. ops->unmap_sg(dev, sg, nents, dir, attrs);
  58. }
  59. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  60. size_t offset, size_t size,
  61. enum dma_data_direction dir)
  62. {
  63. struct dma_map_ops *ops = get_dma_ops(dev);
  64. dma_addr_t addr;
  65. kmemcheck_mark_initialized(page_address(page) + offset, size);
  66. BUG_ON(!valid_dma_direction(dir));
  67. addr = ops->map_page(dev, page, offset, size, dir, NULL);
  68. debug_dma_map_page(dev, page, offset, size, dir, addr, false);
  69. return addr;
  70. }
  71. static inline void dma_unmap_page(struct device *dev, dma_addr_t addr,
  72. size_t size, enum dma_data_direction dir)
  73. {
  74. struct dma_map_ops *ops = get_dma_ops(dev);
  75. BUG_ON(!valid_dma_direction(dir));
  76. if (ops->unmap_page)
  77. ops->unmap_page(dev, addr, size, dir, NULL);
  78. debug_dma_unmap_page(dev, addr, size, dir, false);
  79. }
  80. static inline void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
  81. size_t size,
  82. enum dma_data_direction dir)
  83. {
  84. struct dma_map_ops *ops = get_dma_ops(dev);
  85. BUG_ON(!valid_dma_direction(dir));
  86. if (ops->sync_single_for_cpu)
  87. ops->sync_single_for_cpu(dev, addr, size, dir);
  88. debug_dma_sync_single_for_cpu(dev, addr, size, dir);
  89. }
  90. static inline void dma_sync_single_for_device(struct device *dev,
  91. dma_addr_t addr, size_t size,
  92. enum dma_data_direction dir)
  93. {
  94. struct dma_map_ops *ops = get_dma_ops(dev);
  95. BUG_ON(!valid_dma_direction(dir));
  96. if (ops->sync_single_for_device)
  97. ops->sync_single_for_device(dev, addr, size, dir);
  98. debug_dma_sync_single_for_device(dev, addr, size, dir);
  99. }
  100. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  101. dma_addr_t addr,
  102. unsigned long offset,
  103. size_t size,
  104. enum dma_data_direction dir)
  105. {
  106. dma_sync_single_for_cpu(dev, addr + offset, size, dir);
  107. }
  108. static inline void dma_sync_single_range_for_device(struct device *dev,
  109. dma_addr_t addr,
  110. unsigned long offset,
  111. size_t size,
  112. enum dma_data_direction dir)
  113. {
  114. dma_sync_single_for_device(dev, addr + offset, size, dir);
  115. }
  116. static inline void
  117. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  118. int nelems, enum dma_data_direction dir)
  119. {
  120. struct dma_map_ops *ops = get_dma_ops(dev);
  121. BUG_ON(!valid_dma_direction(dir));
  122. if (ops->sync_sg_for_cpu)
  123. ops->sync_sg_for_cpu(dev, sg, nelems, dir);
  124. debug_dma_sync_sg_for_cpu(dev, sg, nelems, dir);
  125. }
  126. static inline void
  127. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  128. int nelems, enum dma_data_direction dir)
  129. {
  130. struct dma_map_ops *ops = get_dma_ops(dev);
  131. BUG_ON(!valid_dma_direction(dir));
  132. if (ops->sync_sg_for_device)
  133. ops->sync_sg_for_device(dev, sg, nelems, dir);
  134. debug_dma_sync_sg_for_device(dev, sg, nelems, dir);
  135. }
  136. #define dma_map_single(d, a, s, r) dma_map_single_attrs(d, a, s, r, NULL)
  137. #define dma_unmap_single(d, a, s, r) dma_unmap_single_attrs(d, a, s, r, NULL)
  138. #define dma_map_sg(d, s, n, r) dma_map_sg_attrs(d, s, n, r, NULL)
  139. #define dma_unmap_sg(d, s, n, r) dma_unmap_sg_attrs(d, s, n, r, NULL)
  140. #endif