dma-mapping.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #ifndef _ASM_DMA_MAPPING_H_
  2. #define _ASM_DMA_MAPPING_H_
  3. /*
  4. * IOMMU interface. See Documentation/DMA-mapping.txt and DMA-API.txt for
  5. * documentation.
  6. */
  7. #include <linux/scatterlist.h>
  8. #include <asm/io.h>
  9. #include <asm/swiotlb.h>
  10. struct dma_mapping_ops {
  11. int (*mapping_error)(dma_addr_t dma_addr);
  12. void* (*alloc_coherent)(struct device *dev, size_t size,
  13. dma_addr_t *dma_handle, gfp_t gfp);
  14. void (*free_coherent)(struct device *dev, size_t size,
  15. void *vaddr, dma_addr_t dma_handle);
  16. dma_addr_t (*map_single)(struct device *hwdev, void *ptr,
  17. size_t size, int direction);
  18. /* like map_single, but doesn't check the device mask */
  19. dma_addr_t (*map_simple)(struct device *hwdev, char *ptr,
  20. size_t size, int direction);
  21. void (*unmap_single)(struct device *dev, dma_addr_t addr,
  22. size_t size, int direction);
  23. void (*sync_single_for_cpu)(struct device *hwdev,
  24. dma_addr_t dma_handle, size_t size,
  25. int direction);
  26. void (*sync_single_for_device)(struct device *hwdev,
  27. dma_addr_t dma_handle, size_t size,
  28. int direction);
  29. void (*sync_single_range_for_cpu)(struct device *hwdev,
  30. dma_addr_t dma_handle, unsigned long offset,
  31. size_t size, int direction);
  32. void (*sync_single_range_for_device)(struct device *hwdev,
  33. dma_addr_t dma_handle, unsigned long offset,
  34. size_t size, int direction);
  35. void (*sync_sg_for_cpu)(struct device *hwdev,
  36. struct scatterlist *sg, int nelems,
  37. int direction);
  38. void (*sync_sg_for_device)(struct device *hwdev,
  39. struct scatterlist *sg, int nelems,
  40. int direction);
  41. int (*map_sg)(struct device *hwdev, struct scatterlist *sg,
  42. int nents, int direction);
  43. void (*unmap_sg)(struct device *hwdev,
  44. struct scatterlist *sg, int nents,
  45. int direction);
  46. int (*dma_supported)(struct device *hwdev, u64 mask);
  47. int is_phys;
  48. };
  49. extern const struct dma_mapping_ops *dma_ops;
  50. #ifdef CONFIG_X86_32
  51. # include "dma-mapping_32.h"
  52. #else
  53. # include "dma-mapping_64.h"
  54. #endif
  55. static inline dma_addr_t
  56. dma_map_single(struct device *hwdev, void *ptr, size_t size,
  57. int direction)
  58. {
  59. BUG_ON(!valid_dma_direction(direction));
  60. return dma_ops->map_single(hwdev, ptr, size, direction);
  61. }
  62. static inline void
  63. dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size,
  64. int direction)
  65. {
  66. BUG_ON(!valid_dma_direction(direction));
  67. if (dma_ops->unmap_single)
  68. dma_ops->unmap_single(dev, addr, size, direction);
  69. }
  70. static inline int
  71. dma_map_sg(struct device *hwdev, struct scatterlist *sg,
  72. int nents, int direction)
  73. {
  74. BUG_ON(!valid_dma_direction(direction));
  75. return dma_ops->map_sg(hwdev, sg, nents, direction);
  76. }
  77. static inline void
  78. dma_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nents,
  79. int direction)
  80. {
  81. BUG_ON(!valid_dma_direction(direction));
  82. if (dma_ops->unmap_sg)
  83. dma_ops->unmap_sg(hwdev, sg, nents, direction);
  84. }
  85. static inline void
  86. dma_sync_single_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
  87. size_t size, int direction)
  88. {
  89. BUG_ON(!valid_dma_direction(direction));
  90. if (dma_ops->sync_single_for_cpu)
  91. dma_ops->sync_single_for_cpu(hwdev, dma_handle, size,
  92. direction);
  93. flush_write_buffers();
  94. }
  95. static inline void
  96. dma_sync_single_for_device(struct device *hwdev, dma_addr_t dma_handle,
  97. size_t size, int direction)
  98. {
  99. BUG_ON(!valid_dma_direction(direction));
  100. if (dma_ops->sync_single_for_device)
  101. dma_ops->sync_single_for_device(hwdev, dma_handle, size,
  102. direction);
  103. flush_write_buffers();
  104. }
  105. static inline void
  106. dma_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dma_handle,
  107. unsigned long offset, size_t size, int direction)
  108. {
  109. BUG_ON(!valid_dma_direction(direction));
  110. if (dma_ops->sync_single_range_for_cpu)
  111. dma_ops->sync_single_range_for_cpu(hwdev, dma_handle, offset,
  112. size, direction);
  113. flush_write_buffers();
  114. }
  115. static inline void
  116. dma_sync_single_range_for_device(struct device *hwdev, dma_addr_t dma_handle,
  117. unsigned long offset, size_t size,
  118. int direction)
  119. {
  120. BUG_ON(!valid_dma_direction(direction));
  121. if (dma_ops->sync_single_range_for_device)
  122. dma_ops->sync_single_range_for_device(hwdev, dma_handle,
  123. offset, size, direction);
  124. flush_write_buffers();
  125. }
  126. #endif