dma-mapping.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2004 IBM
  3. *
  4. * Implements the generic device dma API for powerpc.
  5. * the pci and vio busses
  6. */
  7. #ifndef _ASM_DMA_MAPPING_H
  8. #define _ASM_DMA_MAPPING_H
  9. #ifdef __KERNEL__
  10. #include <linux/types.h>
  11. #include <linux/cache.h>
  12. /* need struct page definitions */
  13. #include <linux/mm.h>
  14. #include <linux/scatterlist.h>
  15. #include <asm/io.h>
  16. #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
  17. #ifdef CONFIG_NOT_COHERENT_CACHE
  18. /*
  19. * DMA-consistent mapping functions for PowerPCs that don't support
  20. * cache snooping. These allocate/free a region of uncached mapped
  21. * memory space for use with DMA devices. Alternatively, you could
  22. * allocate the space "normally" and use the cache management functions
  23. * to ensure it is consistent.
  24. */
  25. extern void *__dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp);
  26. extern void __dma_free_coherent(size_t size, void *vaddr);
  27. extern void __dma_sync(void *vaddr, size_t size, int direction);
  28. extern void __dma_sync_page(struct page *page, unsigned long offset,
  29. size_t size, int direction);
  30. #else /* ! CONFIG_NOT_COHERENT_CACHE */
  31. /*
  32. * Cache coherent cores.
  33. */
  34. #define __dma_alloc_coherent(gfp, size, handle) NULL
  35. #define __dma_free_coherent(size, addr) ((void)0)
  36. #define __dma_sync(addr, size, rw) ((void)0)
  37. #define __dma_sync_page(pg, off, sz, rw) ((void)0)
  38. #endif /* ! CONFIG_NOT_COHERENT_CACHE */
  39. #ifdef CONFIG_PPC64
  40. /*
  41. * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
  42. */
  43. struct dma_mapping_ops {
  44. void * (*alloc_coherent)(struct device *dev, size_t size,
  45. dma_addr_t *dma_handle, gfp_t flag);
  46. void (*free_coherent)(struct device *dev, size_t size,
  47. void *vaddr, dma_addr_t dma_handle);
  48. dma_addr_t (*map_single)(struct device *dev, void *ptr,
  49. size_t size, enum dma_data_direction direction);
  50. void (*unmap_single)(struct device *dev, dma_addr_t dma_addr,
  51. size_t size, enum dma_data_direction direction);
  52. int (*map_sg)(struct device *dev, struct scatterlist *sg,
  53. int nents, enum dma_data_direction direction);
  54. void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
  55. int nents, enum dma_data_direction direction);
  56. int (*dma_supported)(struct device *dev, u64 mask);
  57. int (*set_dma_mask)(struct device *dev, u64 dma_mask);
  58. };
  59. static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
  60. {
  61. /* We don't handle the NULL dev case for ISA for now. We could
  62. * do it via an out of line call but it is not needed for now. The
  63. * only ISA DMA device we support is the floppy and we have a hack
  64. * in the floppy driver directly to get a device for us.
  65. */
  66. if (unlikely(dev == NULL || dev->archdata.dma_ops == NULL))
  67. return NULL;
  68. return dev->archdata.dma_ops;
  69. }
  70. static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
  71. {
  72. dev->archdata.dma_ops = ops;
  73. }
  74. static inline int dma_supported(struct device *dev, u64 mask)
  75. {
  76. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  77. if (unlikely(dma_ops == NULL))
  78. return 0;
  79. if (dma_ops->dma_supported == NULL)
  80. return 1;
  81. return dma_ops->dma_supported(dev, mask);
  82. }
  83. /* We have our own implementation of pci_set_dma_mask() */
  84. #define HAVE_ARCH_PCI_SET_DMA_MASK
  85. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  86. {
  87. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  88. if (unlikely(dma_ops == NULL))
  89. return -EIO;
  90. if (dma_ops->set_dma_mask != NULL)
  91. return dma_ops->set_dma_mask(dev, dma_mask);
  92. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  93. return -EIO;
  94. *dev->dma_mask = dma_mask;
  95. return 0;
  96. }
  97. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  98. dma_addr_t *dma_handle, gfp_t flag)
  99. {
  100. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  101. BUG_ON(!dma_ops);
  102. return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
  103. }
  104. static inline void dma_free_coherent(struct device *dev, size_t size,
  105. void *cpu_addr, dma_addr_t dma_handle)
  106. {
  107. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  108. BUG_ON(!dma_ops);
  109. dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
  110. }
  111. static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
  112. size_t size,
  113. enum dma_data_direction direction)
  114. {
  115. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  116. BUG_ON(!dma_ops);
  117. return dma_ops->map_single(dev, cpu_addr, size, direction);
  118. }
  119. static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
  120. size_t size,
  121. enum dma_data_direction direction)
  122. {
  123. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  124. BUG_ON(!dma_ops);
  125. dma_ops->unmap_single(dev, dma_addr, size, direction);
  126. }
  127. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  128. unsigned long offset, size_t size,
  129. enum dma_data_direction direction)
  130. {
  131. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  132. BUG_ON(!dma_ops);
  133. return dma_ops->map_single(dev, page_address(page) + offset, size,
  134. direction);
  135. }
  136. static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  137. size_t size,
  138. enum dma_data_direction direction)
  139. {
  140. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  141. BUG_ON(!dma_ops);
  142. dma_ops->unmap_single(dev, dma_address, size, direction);
  143. }
  144. static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
  145. int nents, enum dma_data_direction direction)
  146. {
  147. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  148. BUG_ON(!dma_ops);
  149. return dma_ops->map_sg(dev, sg, nents, direction);
  150. }
  151. static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  152. int nhwentries,
  153. enum dma_data_direction direction)
  154. {
  155. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  156. BUG_ON(!dma_ops);
  157. dma_ops->unmap_sg(dev, sg, nhwentries, direction);
  158. }
  159. /*
  160. * Available generic sets of operations
  161. */
  162. extern struct dma_mapping_ops dma_iommu_ops;
  163. extern struct dma_mapping_ops dma_direct_ops;
  164. #else /* CONFIG_PPC64 */
  165. #define dma_supported(dev, mask) (1)
  166. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  167. {
  168. if (!dev->dma_mask || !dma_supported(dev, mask))
  169. return -EIO;
  170. *dev->dma_mask = dma_mask;
  171. return 0;
  172. }
  173. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  174. dma_addr_t * dma_handle,
  175. gfp_t gfp)
  176. {
  177. #ifdef CONFIG_NOT_COHERENT_CACHE
  178. return __dma_alloc_coherent(size, dma_handle, gfp);
  179. #else
  180. void *ret;
  181. /* ignore region specifiers */
  182. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  183. if (dev == NULL || dev->coherent_dma_mask < 0xffffffff)
  184. gfp |= GFP_DMA;
  185. ret = (void *)__get_free_pages(gfp, get_order(size));
  186. if (ret != NULL) {
  187. memset(ret, 0, size);
  188. *dma_handle = virt_to_bus(ret);
  189. }
  190. return ret;
  191. #endif
  192. }
  193. static inline void
  194. dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  195. dma_addr_t dma_handle)
  196. {
  197. #ifdef CONFIG_NOT_COHERENT_CACHE
  198. __dma_free_coherent(size, vaddr);
  199. #else
  200. free_pages((unsigned long)vaddr, get_order(size));
  201. #endif
  202. }
  203. static inline dma_addr_t
  204. dma_map_single(struct device *dev, void *ptr, size_t size,
  205. enum dma_data_direction direction)
  206. {
  207. BUG_ON(direction == DMA_NONE);
  208. __dma_sync(ptr, size, direction);
  209. return virt_to_bus(ptr);
  210. }
  211. static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
  212. size_t size,
  213. enum dma_data_direction direction)
  214. {
  215. /* We do nothing. */
  216. }
  217. static inline dma_addr_t
  218. dma_map_page(struct device *dev, struct page *page,
  219. unsigned long offset, size_t size,
  220. enum dma_data_direction direction)
  221. {
  222. BUG_ON(direction == DMA_NONE);
  223. __dma_sync_page(page, offset, size, direction);
  224. return page_to_bus(page) + offset;
  225. }
  226. static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  227. size_t size,
  228. enum dma_data_direction direction)
  229. {
  230. /* We do nothing. */
  231. }
  232. static inline int
  233. dma_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
  234. enum dma_data_direction direction)
  235. {
  236. struct scatterlist *sg;
  237. int i;
  238. BUG_ON(direction == DMA_NONE);
  239. for_each_sg(sgl, sg, nents, i) {
  240. BUG_ON(!sg_page(sg));
  241. __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
  242. sg->dma_address = page_to_bus(sg_page(sg)) + sg->offset;
  243. }
  244. return nents;
  245. }
  246. static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  247. int nhwentries,
  248. enum dma_data_direction direction)
  249. {
  250. /* We don't do anything here. */
  251. }
  252. #endif /* CONFIG_PPC64 */
  253. static inline void dma_sync_single_for_cpu(struct device *dev,
  254. dma_addr_t dma_handle, size_t size,
  255. enum dma_data_direction direction)
  256. {
  257. BUG_ON(direction == DMA_NONE);
  258. __dma_sync(bus_to_virt(dma_handle), size, direction);
  259. }
  260. static inline void dma_sync_single_for_device(struct device *dev,
  261. dma_addr_t dma_handle, size_t size,
  262. enum dma_data_direction direction)
  263. {
  264. BUG_ON(direction == DMA_NONE);
  265. __dma_sync(bus_to_virt(dma_handle), size, direction);
  266. }
  267. static inline void dma_sync_sg_for_cpu(struct device *dev,
  268. struct scatterlist *sgl, int nents,
  269. enum dma_data_direction direction)
  270. {
  271. struct scatterlist *sg;
  272. int i;
  273. BUG_ON(direction == DMA_NONE);
  274. for_each_sg(sgl, sg, nents, i)
  275. __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
  276. }
  277. static inline void dma_sync_sg_for_device(struct device *dev,
  278. struct scatterlist *sgl, int nents,
  279. enum dma_data_direction direction)
  280. {
  281. struct scatterlist *sg;
  282. int i;
  283. BUG_ON(direction == DMA_NONE);
  284. for_each_sg(sgl, sg, nents, i)
  285. __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
  286. }
  287. static inline int dma_mapping_error(dma_addr_t dma_addr)
  288. {
  289. #ifdef CONFIG_PPC64
  290. return (dma_addr == DMA_ERROR_CODE);
  291. #else
  292. return 0;
  293. #endif
  294. }
  295. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  296. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  297. #ifdef CONFIG_NOT_COHERENT_CACHE
  298. #define dma_is_consistent(d, h) (0)
  299. #else
  300. #define dma_is_consistent(d, h) (1)
  301. #endif
  302. static inline int dma_get_cache_alignment(void)
  303. {
  304. #ifdef CONFIG_PPC64
  305. /* no easy way to get cache size on all processors, so return
  306. * the maximum possible, to be safe */
  307. return (1 << INTERNODE_CACHE_SHIFT);
  308. #else
  309. /*
  310. * Each processor family will define its own L1_CACHE_SHIFT,
  311. * L1_CACHE_BYTES wraps to this, so this is always safe.
  312. */
  313. return L1_CACHE_BYTES;
  314. #endif
  315. }
  316. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  317. dma_addr_t dma_handle, unsigned long offset, size_t size,
  318. enum dma_data_direction direction)
  319. {
  320. /* just sync everything for now */
  321. dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction);
  322. }
  323. static inline void dma_sync_single_range_for_device(struct device *dev,
  324. dma_addr_t dma_handle, unsigned long offset, size_t size,
  325. enum dma_data_direction direction)
  326. {
  327. /* just sync everything for now */
  328. dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
  329. }
  330. static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  331. enum dma_data_direction direction)
  332. {
  333. BUG_ON(direction == DMA_NONE);
  334. __dma_sync(vaddr, size, (int)direction);
  335. }
  336. #endif /* __KERNEL__ */
  337. #endif /* _ASM_DMA_MAPPING_H */