dma-mapping.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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, gfp_t 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. #else /* ! CONFIG_NOT_COHERENT_CACHE */
  26. /*
  27. * Cache coherent cores.
  28. */
  29. #define __dma_alloc_coherent(gfp, size, handle) NULL
  30. #define __dma_free_coherent(size, addr) do { } while (0)
  31. #define __dma_sync(addr, size, rw) do { } while (0)
  32. #define __dma_sync_page(pg, off, sz, rw) do { } while (0)
  33. #endif /* ! CONFIG_NOT_COHERENT_CACHE */
  34. #define dma_supported(dev, mask) (1)
  35. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  36. {
  37. if (!dev->dma_mask || !dma_supported(dev, mask))
  38. return -EIO;
  39. *dev->dma_mask = dma_mask;
  40. return 0;
  41. }
  42. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  43. dma_addr_t * dma_handle,
  44. gfp_t gfp)
  45. {
  46. #ifdef CONFIG_NOT_COHERENT_CACHE
  47. return __dma_alloc_coherent(size, dma_handle, gfp);
  48. #else
  49. void *ret;
  50. /* ignore region specifiers */
  51. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  52. if (dev == NULL || dev->coherent_dma_mask < 0xffffffff)
  53. gfp |= GFP_DMA;
  54. ret = (void *)__get_free_pages(gfp, get_order(size));
  55. if (ret != NULL) {
  56. memset(ret, 0, size);
  57. *dma_handle = virt_to_bus(ret);
  58. }
  59. return ret;
  60. #endif
  61. }
  62. static inline void
  63. dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  64. dma_addr_t dma_handle)
  65. {
  66. #ifdef CONFIG_NOT_COHERENT_CACHE
  67. __dma_free_coherent(size, vaddr);
  68. #else
  69. free_pages((unsigned long)vaddr, get_order(size));
  70. #endif
  71. }
  72. static inline dma_addr_t
  73. dma_map_single(struct device *dev, void *ptr, size_t size,
  74. enum dma_data_direction direction)
  75. {
  76. BUG_ON(direction == DMA_NONE);
  77. __dma_sync(ptr, size, direction);
  78. return virt_to_bus(ptr);
  79. }
  80. /* We do nothing. */
  81. #define dma_unmap_single(dev, addr, size, dir) do { } while (0)
  82. static inline dma_addr_t
  83. dma_map_page(struct device *dev, struct page *page,
  84. unsigned long offset, size_t size,
  85. enum dma_data_direction direction)
  86. {
  87. BUG_ON(direction == DMA_NONE);
  88. __dma_sync_page(page, offset, size, direction);
  89. return page_to_bus(page) + offset;
  90. }
  91. /* We do nothing. */
  92. #define dma_unmap_page(dev, handle, size, dir) do { } while (0)
  93. static inline int
  94. dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  95. enum dma_data_direction direction)
  96. {
  97. int i;
  98. BUG_ON(direction == DMA_NONE);
  99. for (i = 0; i < nents; i++, sg++) {
  100. BUG_ON(!sg->page);
  101. __dma_sync_page(sg->page, sg->offset, sg->length, direction);
  102. sg->dma_address = page_to_bus(sg->page) + sg->offset;
  103. }
  104. return nents;
  105. }
  106. /* We don't do anything here. */
  107. #define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
  108. static inline void
  109. dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  110. size_t size,
  111. enum dma_data_direction direction)
  112. {
  113. BUG_ON(direction == DMA_NONE);
  114. __dma_sync(bus_to_virt(dma_handle), size, direction);
  115. }
  116. static inline void
  117. dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  118. size_t size,
  119. enum dma_data_direction direction)
  120. {
  121. BUG_ON(direction == DMA_NONE);
  122. __dma_sync(bus_to_virt(dma_handle), size, direction);
  123. }
  124. static inline void
  125. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
  126. enum dma_data_direction direction)
  127. {
  128. int i;
  129. BUG_ON(direction == DMA_NONE);
  130. for (i = 0; i < nents; i++, sg++)
  131. __dma_sync_page(sg->page, sg->offset, sg->length, direction);
  132. }
  133. static inline void
  134. dma_sync_sg_for_device(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. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  143. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  144. #ifdef CONFIG_NOT_COHERENT_CACHE
  145. #define dma_is_consistent(d) (0)
  146. #else
  147. #define dma_is_consistent(d) (1)
  148. #endif
  149. static inline int dma_get_cache_alignment(void)
  150. {
  151. /*
  152. * Each processor family will define its own L1_CACHE_SHIFT,
  153. * L1_CACHE_BYTES wraps to this, so this is always safe.
  154. */
  155. return L1_CACHE_BYTES;
  156. }
  157. static inline void
  158. dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  159. unsigned long offset, size_t size,
  160. enum dma_data_direction direction)
  161. {
  162. /* just sync everything for now */
  163. dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction);
  164. }
  165. static inline void
  166. dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  167. unsigned long offset, size_t size,
  168. enum dma_data_direction direction)
  169. {
  170. /* just sync everything for now */
  171. dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
  172. }
  173. static inline void dma_cache_sync(void *vaddr, size_t size,
  174. enum dma_data_direction direction)
  175. {
  176. __dma_sync(vaddr, size, (int)direction);
  177. }
  178. static inline int dma_mapping_error(dma_addr_t dma_addr)
  179. {
  180. return 0;
  181. }
  182. #endif /* __ASM_PPC_DMA_MAPPING_H */