dma-mapping.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef __ASM_SH_DMA_MAPPING_H
  2. #define __ASM_SH_DMA_MAPPING_H
  3. #include <linux/mm.h>
  4. #include <linux/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, sg_virt(&sg[i]), sg[i].length, dir);
  69. #endif
  70. sg[i].dma_address = sg_phys(&sg[i]);
  71. }
  72. return nents;
  73. }
  74. #define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
  75. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  76. unsigned long offset, size_t size,
  77. enum dma_data_direction dir)
  78. {
  79. return dma_map_single(dev, page_address(page) + offset, size, dir);
  80. }
  81. static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  82. size_t size, enum dma_data_direction dir)
  83. {
  84. dma_unmap_single(dev, dma_address, size, dir);
  85. }
  86. static inline void dma_sync_single(struct device *dev, dma_addr_t dma_handle,
  87. size_t size, enum dma_data_direction dir)
  88. {
  89. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  90. if (dev->bus == &pci_bus_type)
  91. return;
  92. #endif
  93. dma_cache_sync(dev, phys_to_virt(dma_handle), size, dir);
  94. }
  95. static inline void dma_sync_single_range(struct device *dev,
  96. dma_addr_t dma_handle,
  97. unsigned long offset, size_t size,
  98. enum dma_data_direction dir)
  99. {
  100. #if defined(CONFIG_PCI) && !defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  101. if (dev->bus == &pci_bus_type)
  102. return;
  103. #endif
  104. dma_cache_sync(dev, phys_to_virt(dma_handle) + offset, size, dir);
  105. }
  106. static inline void dma_sync_sg(struct device *dev, struct scatterlist *sg,
  107. int nelems, enum dma_data_direction dir)
  108. {
  109. int i;
  110. for (i = 0; i < nelems; i++) {
  111. #if !defined(CONFIG_PCI) || defined(CONFIG_SH_PCIDMA_NONCOHERENT)
  112. dma_cache_sync(dev, sg_virt(&sg[i]), sg[i].length, dir);
  113. #endif
  114. sg[i].dma_address = sg_phys(&sg[i]);
  115. }
  116. }
  117. static inline void dma_sync_single_for_cpu(struct device *dev,
  118. dma_addr_t dma_handle, size_t size,
  119. enum dma_data_direction dir)
  120. {
  121. dma_sync_single(dev, dma_handle, size, dir);
  122. }
  123. static inline void dma_sync_single_for_device(struct device *dev,
  124. dma_addr_t dma_handle,
  125. size_t size,
  126. enum dma_data_direction dir)
  127. {
  128. dma_sync_single(dev, dma_handle, size, dir);
  129. }
  130. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  131. dma_addr_t dma_handle,
  132. unsigned long offset,
  133. size_t size,
  134. enum dma_data_direction direction)
  135. {
  136. dma_sync_single_for_cpu(dev, dma_handle+offset, size, direction);
  137. }
  138. static inline void dma_sync_single_range_for_device(struct device *dev,
  139. dma_addr_t dma_handle,
  140. unsigned long offset,
  141. size_t size,
  142. enum dma_data_direction direction)
  143. {
  144. dma_sync_single_for_device(dev, dma_handle+offset, size, direction);
  145. }
  146. static inline void dma_sync_sg_for_cpu(struct device *dev,
  147. struct scatterlist *sg, int nelems,
  148. enum dma_data_direction dir)
  149. {
  150. dma_sync_sg(dev, sg, nelems, dir);
  151. }
  152. static inline void dma_sync_sg_for_device(struct device *dev,
  153. struct scatterlist *sg, int nelems,
  154. enum dma_data_direction dir)
  155. {
  156. dma_sync_sg(dev, sg, nelems, dir);
  157. }
  158. static inline int dma_get_cache_alignment(void)
  159. {
  160. /*
  161. * Each processor family will define its own L1_CACHE_SHIFT,
  162. * L1_CACHE_BYTES wraps to this, so this is always safe.
  163. */
  164. return L1_CACHE_BYTES;
  165. }
  166. static inline int dma_mapping_error(dma_addr_t dma_addr)
  167. {
  168. return dma_addr == 0;
  169. }
  170. #endif /* __ASM_SH_DMA_MAPPING_H */