dma-mapping.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * This is based on both include/asm-sh/dma-mapping.h and
  3. * include/asm-ppc/pci.h
  4. */
  5. #ifndef __ASM_PPC_DMA_MAPPING_H
  6. #define __ASM_PPC_DMA_MAPPING_H
  7. #include <linux/config.h>
  8. /* need struct page definitions */
  9. #include <linux/mm.h>
  10. #include <asm/scatterlist.h>
  11. #include <asm/io.h>
  12. #ifdef CONFIG_NOT_COHERENT_CACHE
  13. /*
  14. * DMA-consistent mapping functions for PowerPCs that don't support
  15. * cache snooping. These allocate/free a region of uncached mapped
  16. * memory space for use with DMA devices. Alternatively, you could
  17. * allocate the space "normally" and use the cache management functions
  18. * to ensure it is consistent.
  19. */
  20. extern void *__dma_alloc_coherent(size_t size, dma_addr_t *handle, int gfp);
  21. extern void __dma_free_coherent(size_t size, void *vaddr);
  22. extern void __dma_sync(void *vaddr, size_t size, int direction);
  23. extern void __dma_sync_page(struct page *page, unsigned long offset,
  24. size_t size, int direction);
  25. #define dma_cache_inv(_start,_size) \
  26. invalidate_dcache_range(_start, (_start + _size))
  27. #define dma_cache_wback(_start,_size) \
  28. clean_dcache_range(_start, (_start + _size))
  29. #define dma_cache_wback_inv(_start,_size) \
  30. flush_dcache_range(_start, (_start + _size))
  31. #else /* ! CONFIG_NOT_COHERENT_CACHE */
  32. /*
  33. * Cache coherent cores.
  34. */
  35. #define dma_cache_inv(_start,_size) do { } while (0)
  36. #define dma_cache_wback(_start,_size) do { } while (0)
  37. #define dma_cache_wback_inv(_start,_size) do { } while (0)
  38. #define __dma_alloc_coherent(gfp, size, handle) NULL
  39. #define __dma_free_coherent(size, addr) do { } while (0)
  40. #define __dma_sync(addr, size, rw) do { } while (0)
  41. #define __dma_sync_page(pg, off, sz, rw) do { } while (0)
  42. #endif /* ! CONFIG_NOT_COHERENT_CACHE */
  43. #define dma_supported(dev, mask) (1)
  44. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  45. {
  46. if (!dev->dma_mask || !dma_supported(dev, mask))
  47. return -EIO;
  48. *dev->dma_mask = dma_mask;
  49. return 0;
  50. }
  51. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  52. dma_addr_t * dma_handle,
  53. unsigned int __nocast gfp)
  54. {
  55. #ifdef CONFIG_NOT_COHERENT_CACHE
  56. return __dma_alloc_coherent(size, dma_handle, gfp);
  57. #else
  58. void *ret;
  59. /* ignore region specifiers */
  60. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  61. if (dev == NULL || dev->coherent_dma_mask < 0xffffffff)
  62. gfp |= GFP_DMA;
  63. ret = (void *)__get_free_pages(gfp, get_order(size));
  64. if (ret != NULL) {
  65. memset(ret, 0, size);
  66. *dma_handle = virt_to_bus(ret);
  67. }
  68. return ret;
  69. #endif
  70. }
  71. static inline void
  72. dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  73. dma_addr_t dma_handle)
  74. {
  75. #ifdef CONFIG_NOT_COHERENT_CACHE
  76. __dma_free_coherent(size, vaddr);
  77. #else
  78. free_pages((unsigned long)vaddr, get_order(size));
  79. #endif
  80. }
  81. static inline dma_addr_t
  82. dma_map_single(struct device *dev, void *ptr, size_t size,
  83. enum dma_data_direction direction)
  84. {
  85. BUG_ON(direction == DMA_NONE);
  86. __dma_sync(ptr, size, direction);
  87. return virt_to_bus(ptr);
  88. }
  89. /* We do nothing. */
  90. #define dma_unmap_single(dev, addr, size, dir) do { } while (0)
  91. static inline dma_addr_t
  92. dma_map_page(struct device *dev, struct page *page,
  93. unsigned long offset, size_t size,
  94. enum dma_data_direction direction)
  95. {
  96. BUG_ON(direction == DMA_NONE);
  97. __dma_sync_page(page, offset, size, direction);
  98. return page_to_bus(page) + offset;
  99. }
  100. /* We do nothing. */
  101. #define dma_unmap_page(dev, handle, size, dir) do { } while (0)
  102. static inline int
  103. dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  104. enum dma_data_direction direction)
  105. {
  106. int i;
  107. BUG_ON(direction == DMA_NONE);
  108. for (i = 0; i < nents; i++, sg++) {
  109. BUG_ON(!sg->page);
  110. __dma_sync_page(sg->page, sg->offset, sg->length, direction);
  111. sg->dma_address = page_to_bus(sg->page) + sg->offset;
  112. }
  113. return nents;
  114. }
  115. /* We don't do anything here. */
  116. #define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
  117. static inline void
  118. dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  119. size_t size,
  120. enum dma_data_direction direction)
  121. {
  122. BUG_ON(direction == DMA_NONE);
  123. __dma_sync(bus_to_virt(dma_handle), size, direction);
  124. }
  125. static inline void
  126. dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  127. size_t size,
  128. enum dma_data_direction direction)
  129. {
  130. BUG_ON(direction == DMA_NONE);
  131. __dma_sync(bus_to_virt(dma_handle), size, direction);
  132. }
  133. static inline void
  134. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
  135. enum dma_data_direction direction)
  136. {
  137. int i;
  138. BUG_ON(direction == DMA_NONE);
  139. for (i = 0; i < nents; i++, sg++)
  140. __dma_sync_page(sg->page, sg->offset, sg->length, direction);
  141. }
  142. static inline void
  143. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
  144. enum dma_data_direction direction)
  145. {
  146. int i;
  147. BUG_ON(direction == DMA_NONE);
  148. for (i = 0; i < nents; i++, sg++)
  149. __dma_sync_page(sg->page, sg->offset, sg->length, direction);
  150. }
  151. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  152. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  153. #ifdef CONFIG_NOT_COHERENT_CACHE
  154. #define dma_is_consistent(d) (0)
  155. #else
  156. #define dma_is_consistent(d) (1)
  157. #endif
  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 void
  167. dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  168. unsigned long offset, size_t size,
  169. enum dma_data_direction direction)
  170. {
  171. /* just sync everything for now */
  172. dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction);
  173. }
  174. static inline void
  175. dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  176. unsigned long offset, size_t size,
  177. enum dma_data_direction direction)
  178. {
  179. /* just sync everything for now */
  180. dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
  181. }
  182. static inline void dma_cache_sync(void *vaddr, size_t size,
  183. enum dma_data_direction direction)
  184. {
  185. __dma_sync(vaddr, size, (int)direction);
  186. }
  187. static inline int dma_mapping_error(dma_addr_t dma_addr)
  188. {
  189. return 0;
  190. }
  191. #endif /* __ASM_PPC_DMA_MAPPING_H */