dma-mapping.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef __ASM_SH_DMA_MAPPING_H
  2. #define __ASM_SH_DMA_MAPPING_H
  3. #include <linux/mm.h>
  4. #include <asm/scatterlist.h>
  5. #include <asm/cacheflush.h>
  6. #include <asm/io.h>
  7. extern struct bus_type pci_bus_type;
  8. /* arch/sh/mm/consistent.c */
  9. extern void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *handle);
  10. extern void consistent_free(void *vaddr, size_t size);
  11. extern void consistent_sync(void *vaddr, size_t size, int direction);
  12. #define dma_supported(dev, mask) (1)
  13. static inline int dma_set_mask(struct device *dev, u64 mask)
  14. {
  15. if (!dev->dma_mask || !dma_supported(dev, mask))
  16. return -EIO;
  17. *dev->dma_mask = mask;
  18. return 0;
  19. }
  20. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  21. dma_addr_t *dma_handle, gfp_t flag)
  22. {
  23. if (sh_mv.mv_consistent_alloc) {
  24. void *ret;
  25. ret = sh_mv.mv_consistent_alloc(dev, size, dma_handle, flag);
  26. if (ret != NULL)
  27. return ret;
  28. }
  29. return consistent_alloc(flag, size, dma_handle);
  30. }
  31. static inline void dma_free_coherent(struct device *dev, size_t size,
  32. void *vaddr, dma_addr_t dma_handle)
  33. {
  34. if (sh_mv.mv_consistent_free) {
  35. int ret;
  36. ret = sh_mv.mv_consistent_free(dev, size, vaddr, dma_handle);
  37. if (ret == 0)
  38. return;
  39. }
  40. consistent_free(vaddr, size);
  41. }
  42. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  43. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  44. #define dma_is_consistent(d, h) (1)
  45. static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  46. enum dma_data_direction dir)
  47. {
  48. consistent_sync(vaddr, size, (int)dir);
  49. }
  50. static inline dma_addr_t dma_map_single(struct device *dev,
  51. void *ptr, size_t size,
  52. enum dma_data_direction dir)
  53. {
  54. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  55. if (dev->bus == &pci_bus_type)
  56. return virt_to_phys(ptr);
  57. #endif
  58. dma_cache_sync(dev, ptr, size, dir);
  59. return virt_to_phys(ptr);
  60. }
  61. #define dma_unmap_single(dev, addr, size, dir) do { } while (0)
  62. static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
  63. int nents, enum dma_data_direction dir)
  64. {
  65. int i;
  66. for (i = 0; i < nents; i++) {
  67. #if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  68. dma_cache_sync(dev, page_address(sg[i].page) + sg[i].offset,
  69. sg[i].length, dir);
  70. #endif
  71. sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
  72. }
  73. return nents;
  74. }
  75. #define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
  76. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  77. unsigned long offset, size_t size,
  78. enum dma_data_direction dir)
  79. {
  80. return dma_map_single(dev, page_address(page) + offset, size, dir);
  81. }
  82. static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  83. size_t size, enum dma_data_direction dir)
  84. {
  85. dma_unmap_single(dev, dma_address, size, dir);
  86. }
  87. static inline void dma_sync_single(struct device *dev, dma_addr_t dma_handle,
  88. size_t size, enum dma_data_direction dir)
  89. {
  90. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  91. if (dev->bus == &pci_bus_type)
  92. return;
  93. #endif
  94. dma_cache_sync(dev, phys_to_virt(dma_handle), size, dir);
  95. }
  96. static inline void dma_sync_single_range(struct device *dev,
  97. dma_addr_t dma_handle,
  98. unsigned long offset, size_t size,
  99. enum dma_data_direction dir)
  100. {
  101. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  102. if (dev->bus == &pci_bus_type)
  103. return;
  104. #endif
  105. dma_cache_sync(dev, phys_to_virt(dma_handle) + offset, size, dir);
  106. }
  107. static inline void dma_sync_sg(struct device *dev, struct scatterlist *sg,
  108. int nelems, enum dma_data_direction dir)
  109. {
  110. int i;
  111. for (i = 0; i < nelems; i++) {
  112. #if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  113. dma_cache_sync(dev, page_address(sg[i].page) + sg[i].offset,
  114. sg[i].length, dir);
  115. #endif
  116. sg[i].dma_address = page_to_phys(sg[i].page) + sg[i].offset;
  117. }
  118. }
  119. static inline void dma_sync_single_for_cpu(struct device *dev,
  120. dma_addr_t dma_handle, size_t size,
  121. enum dma_data_direction dir)
  122. {
  123. dma_sync_single(dev, dma_handle, size, dir);
  124. }
  125. static inline void dma_sync_single_for_device(struct device *dev,
  126. dma_addr_t dma_handle,
  127. size_t size,
  128. enum dma_data_direction dir)
  129. {
  130. dma_sync_single(dev, dma_handle, size, dir);
  131. }
  132. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  133. dma_addr_t dma_handle,
  134. unsigned long offset,
  135. size_t size,
  136. enum dma_data_direction direction)
  137. {
  138. dma_sync_single_for_cpu(dev, dma_handle+offset, size, direction);
  139. }
  140. static inline void dma_sync_single_range_for_device(struct device *dev,
  141. dma_addr_t dma_handle,
  142. unsigned long offset,
  143. size_t size,
  144. enum dma_data_direction direction)
  145. {
  146. dma_sync_single_for_device(dev, dma_handle+offset, size, direction);
  147. }
  148. static inline void dma_sync_sg_for_cpu(struct device *dev,
  149. struct scatterlist *sg, int nelems,
  150. enum dma_data_direction dir)
  151. {
  152. dma_sync_sg(dev, sg, nelems, dir);
  153. }
  154. static inline void dma_sync_sg_for_device(struct device *dev,
  155. struct scatterlist *sg, int nelems,
  156. enum dma_data_direction dir)
  157. {
  158. dma_sync_sg(dev, sg, nelems, dir);
  159. }
  160. static inline int dma_get_cache_alignment(void)
  161. {
  162. /*
  163. * Each processor family will define its own L1_CACHE_SHIFT,
  164. * L1_CACHE_BYTES wraps to this, so this is always safe.
  165. */
  166. return L1_CACHE_BYTES;
  167. }
  168. static inline int dma_mapping_error(dma_addr_t dma_addr)
  169. {
  170. return dma_addr == 0;
  171. }
  172. #endif /* __ASM_SH_DMA_MAPPING_H */