dma-mapping.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, int gfp)
  53. {
  54. #ifdef CONFIG_NOT_COHERENT_CACHE
  55. return __dma_alloc_coherent(size, dma_handle, gfp);
  56. #else
  57. void *ret;
  58. /* ignore region specifiers */
  59. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  60. if (dev == NULL || dev->coherent_dma_mask < 0xffffffff)
  61. gfp |= GFP_DMA;
  62. ret = (void *)__get_free_pages(gfp, get_order(size));
  63. if (ret != NULL) {
  64. memset(ret, 0, size);
  65. *dma_handle = virt_to_bus(ret);
  66. }
  67. return ret;
  68. #endif
  69. }
  70. static inline void
  71. dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  72. dma_addr_t dma_handle)
  73. {
  74. #ifdef CONFIG_NOT_COHERENT_CACHE
  75. __dma_free_coherent(size, vaddr);
  76. #else
  77. free_pages((unsigned long)vaddr, get_order(size));
  78. #endif
  79. }
  80. static inline dma_addr_t
  81. dma_map_single(struct device *dev, void *ptr, size_t size,
  82. enum dma_data_direction direction)
  83. {
  84. BUG_ON(direction == DMA_NONE);
  85. __dma_sync(ptr, size, direction);
  86. return virt_to_bus(ptr);
  87. }
  88. /* We do nothing. */
  89. #define dma_unmap_single(dev, addr, size, dir) do { } while (0)
  90. static inline dma_addr_t
  91. dma_map_page(struct device *dev, struct page *page,
  92. unsigned long offset, size_t size,
  93. enum dma_data_direction direction)
  94. {
  95. BUG_ON(direction == DMA_NONE);
  96. __dma_sync_page(page, offset, size, direction);
  97. return (page - mem_map) * PAGE_SIZE + PCI_DRAM_OFFSET + offset;
  98. }
  99. /* We do nothing. */
  100. #define dma_unmap_page(dev, handle, size, dir) do { } while (0)
  101. static inline int
  102. dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  103. enum dma_data_direction direction)
  104. {
  105. int i;
  106. BUG_ON(direction == DMA_NONE);
  107. for (i = 0; i < nents; i++, sg++) {
  108. BUG_ON(!sg->page);
  109. __dma_sync_page(sg->page, sg->offset, sg->length, direction);
  110. sg->dma_address = page_to_bus(sg->page) + sg->offset;
  111. }
  112. return nents;
  113. }
  114. /* We don't do anything here. */
  115. #define dma_unmap_sg(dev, sg, nents, dir) do { } while (0)
  116. static inline void
  117. dma_sync_single_for_cpu(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_single_for_device(struct device *dev, dma_addr_t dma_handle,
  126. size_t size,
  127. enum dma_data_direction direction)
  128. {
  129. BUG_ON(direction == DMA_NONE);
  130. __dma_sync(bus_to_virt(dma_handle), size, direction);
  131. }
  132. static inline void
  133. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
  134. enum dma_data_direction direction)
  135. {
  136. int i;
  137. BUG_ON(direction == DMA_NONE);
  138. for (i = 0; i < nents; i++, sg++)
  139. __dma_sync_page(sg->page, sg->offset, sg->length, direction);
  140. }
  141. static inline void
  142. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
  143. enum dma_data_direction direction)
  144. {
  145. int i;
  146. BUG_ON(direction == DMA_NONE);
  147. for (i = 0; i < nents; i++, sg++)
  148. __dma_sync_page(sg->page, sg->offset, sg->length, direction);
  149. }
  150. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  151. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  152. #ifdef CONFIG_NOT_COHERENT_CACHE
  153. #define dma_is_consistent(d) (0)
  154. #else
  155. #define dma_is_consistent(d) (1)
  156. #endif
  157. static inline int dma_get_cache_alignment(void)
  158. {
  159. /*
  160. * Each processor family will define its own L1_CACHE_SHIFT,
  161. * L1_CACHE_BYTES wraps to this, so this is always safe.
  162. */
  163. return L1_CACHE_BYTES;
  164. }
  165. static inline void
  166. dma_sync_single_range_for_cpu(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_cpu(dev, dma_handle, offset + size, direction);
  172. }
  173. static inline void
  174. dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  175. unsigned long offset, size_t size,
  176. enum dma_data_direction direction)
  177. {
  178. /* just sync everything for now */
  179. dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
  180. }
  181. static inline void dma_cache_sync(void *vaddr, size_t size,
  182. enum dma_data_direction direction)
  183. {
  184. __dma_sync(vaddr, size, (int)direction);
  185. }
  186. static inline int dma_mapping_error(dma_addr_t dma_addr)
  187. {
  188. return 0;
  189. }
  190. #endif /* __ASM_PPC_DMA_MAPPING_H */