dma-mapping.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <linux/dma-attrs.h>
  16. #include <asm/io.h>
  17. #include <asm/swiotlb.h>
  18. #define DMA_ERROR_CODE (~(dma_addr_t)0x0)
  19. /* Some dma direct funcs must be visible for use in other dma_ops */
  20. extern void *dma_direct_alloc_coherent(struct device *dev, size_t size,
  21. dma_addr_t *dma_handle, gfp_t flag);
  22. extern void dma_direct_free_coherent(struct device *dev, size_t size,
  23. void *vaddr, dma_addr_t dma_handle);
  24. extern unsigned long get_dma_direct_offset(struct device *dev);
  25. #ifdef CONFIG_NOT_COHERENT_CACHE
  26. /*
  27. * DMA-consistent mapping functions for PowerPCs that don't support
  28. * cache snooping. These allocate/free a region of uncached mapped
  29. * memory space for use with DMA devices. Alternatively, you could
  30. * allocate the space "normally" and use the cache management functions
  31. * to ensure it is consistent.
  32. */
  33. struct device;
  34. extern void *__dma_alloc_coherent(struct device *dev, size_t size,
  35. dma_addr_t *handle, gfp_t gfp);
  36. extern void __dma_free_coherent(size_t size, void *vaddr);
  37. extern void __dma_sync(void *vaddr, size_t size, int direction);
  38. extern void __dma_sync_page(struct page *page, unsigned long offset,
  39. size_t size, int direction);
  40. #else /* ! CONFIG_NOT_COHERENT_CACHE */
  41. /*
  42. * Cache coherent cores.
  43. */
  44. #define __dma_alloc_coherent(dev, gfp, size, handle) NULL
  45. #define __dma_free_coherent(size, addr) ((void)0)
  46. #define __dma_sync(addr, size, rw) ((void)0)
  47. #define __dma_sync_page(pg, off, sz, rw) ((void)0)
  48. #endif /* ! CONFIG_NOT_COHERENT_CACHE */
  49. static inline unsigned long device_to_mask(struct device *dev)
  50. {
  51. if (dev->dma_mask && *dev->dma_mask)
  52. return *dev->dma_mask;
  53. /* Assume devices without mask can take 32 bit addresses */
  54. return 0xfffffffful;
  55. }
  56. /*
  57. * DMA operations are abstracted for G5 vs. i/pSeries, PCI vs. VIO
  58. */
  59. struct dma_mapping_ops {
  60. void * (*alloc_coherent)(struct device *dev, size_t size,
  61. dma_addr_t *dma_handle, gfp_t flag);
  62. void (*free_coherent)(struct device *dev, size_t size,
  63. void *vaddr, dma_addr_t dma_handle);
  64. int (*map_sg)(struct device *dev, struct scatterlist *sg,
  65. int nents, enum dma_data_direction direction,
  66. struct dma_attrs *attrs);
  67. void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
  68. int nents, enum dma_data_direction direction,
  69. struct dma_attrs *attrs);
  70. int (*dma_supported)(struct device *dev, u64 mask);
  71. int (*set_dma_mask)(struct device *dev, u64 dma_mask);
  72. dma_addr_t (*map_page)(struct device *dev, struct page *page,
  73. unsigned long offset, size_t size,
  74. enum dma_data_direction direction,
  75. struct dma_attrs *attrs);
  76. void (*unmap_page)(struct device *dev,
  77. dma_addr_t dma_address, size_t size,
  78. enum dma_data_direction direction,
  79. struct dma_attrs *attrs);
  80. int (*addr_needs_map)(struct device *dev, dma_addr_t addr,
  81. size_t size);
  82. #ifdef CONFIG_PPC_NEED_DMA_SYNC_OPS
  83. void (*sync_single_range_for_cpu)(struct device *hwdev,
  84. dma_addr_t dma_handle, unsigned long offset,
  85. size_t size,
  86. enum dma_data_direction direction);
  87. void (*sync_single_range_for_device)(struct device *hwdev,
  88. dma_addr_t dma_handle, unsigned long offset,
  89. size_t size,
  90. enum dma_data_direction direction);
  91. void (*sync_sg_for_cpu)(struct device *hwdev,
  92. struct scatterlist *sg, int nelems,
  93. enum dma_data_direction direction);
  94. void (*sync_sg_for_device)(struct device *hwdev,
  95. struct scatterlist *sg, int nelems,
  96. enum dma_data_direction direction);
  97. #endif
  98. };
  99. /*
  100. * Available generic sets of operations
  101. */
  102. #ifdef CONFIG_PPC64
  103. extern struct dma_mapping_ops dma_iommu_ops;
  104. #endif
  105. extern struct dma_mapping_ops dma_direct_ops;
  106. static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
  107. {
  108. /* We don't handle the NULL dev case for ISA for now. We could
  109. * do it via an out of line call but it is not needed for now. The
  110. * only ISA DMA device we support is the floppy and we have a hack
  111. * in the floppy driver directly to get a device for us.
  112. */
  113. if (unlikely(dev == NULL))
  114. return NULL;
  115. return dev->archdata.dma_ops;
  116. }
  117. static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
  118. {
  119. dev->archdata.dma_ops = ops;
  120. }
  121. static inline int dma_supported(struct device *dev, u64 mask)
  122. {
  123. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  124. if (unlikely(dma_ops == NULL))
  125. return 0;
  126. if (dma_ops->dma_supported == NULL)
  127. return 1;
  128. return dma_ops->dma_supported(dev, mask);
  129. }
  130. /* We have our own implementation of pci_set_dma_mask() */
  131. #define HAVE_ARCH_PCI_SET_DMA_MASK
  132. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  133. {
  134. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  135. if (unlikely(dma_ops == NULL))
  136. return -EIO;
  137. if (dma_ops->set_dma_mask != NULL)
  138. return dma_ops->set_dma_mask(dev, dma_mask);
  139. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  140. return -EIO;
  141. *dev->dma_mask = dma_mask;
  142. return 0;
  143. }
  144. /*
  145. * map_/unmap_single actually call through to map/unmap_page now that all the
  146. * dma_mapping_ops have been converted over. We just have to get the page and
  147. * offset to pass through to map_page
  148. */
  149. static inline dma_addr_t dma_map_single_attrs(struct device *dev,
  150. void *cpu_addr,
  151. size_t size,
  152. enum dma_data_direction direction,
  153. struct dma_attrs *attrs)
  154. {
  155. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  156. BUG_ON(!dma_ops);
  157. return dma_ops->map_page(dev, virt_to_page(cpu_addr),
  158. (unsigned long)cpu_addr % PAGE_SIZE, size,
  159. direction, attrs);
  160. }
  161. static inline void dma_unmap_single_attrs(struct device *dev,
  162. dma_addr_t dma_addr,
  163. size_t size,
  164. enum dma_data_direction direction,
  165. struct dma_attrs *attrs)
  166. {
  167. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  168. BUG_ON(!dma_ops);
  169. dma_ops->unmap_page(dev, dma_addr, size, direction, attrs);
  170. }
  171. static inline dma_addr_t dma_map_page_attrs(struct device *dev,
  172. struct page *page,
  173. unsigned long offset, size_t size,
  174. enum dma_data_direction direction,
  175. struct dma_attrs *attrs)
  176. {
  177. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  178. BUG_ON(!dma_ops);
  179. return dma_ops->map_page(dev, page, offset, size, direction, attrs);
  180. }
  181. static inline void dma_unmap_page_attrs(struct device *dev,
  182. dma_addr_t dma_address,
  183. size_t size,
  184. enum dma_data_direction direction,
  185. struct dma_attrs *attrs)
  186. {
  187. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  188. BUG_ON(!dma_ops);
  189. dma_ops->unmap_page(dev, dma_address, size, direction, attrs);
  190. }
  191. static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  192. int nents, enum dma_data_direction direction,
  193. struct dma_attrs *attrs)
  194. {
  195. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  196. BUG_ON(!dma_ops);
  197. return dma_ops->map_sg(dev, sg, nents, direction, attrs);
  198. }
  199. static inline void dma_unmap_sg_attrs(struct device *dev,
  200. struct scatterlist *sg,
  201. int nhwentries,
  202. enum dma_data_direction direction,
  203. struct dma_attrs *attrs)
  204. {
  205. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  206. BUG_ON(!dma_ops);
  207. dma_ops->unmap_sg(dev, sg, nhwentries, direction, attrs);
  208. }
  209. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  210. dma_addr_t *dma_handle, gfp_t flag)
  211. {
  212. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  213. BUG_ON(!dma_ops);
  214. return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
  215. }
  216. static inline void dma_free_coherent(struct device *dev, size_t size,
  217. void *cpu_addr, dma_addr_t dma_handle)
  218. {
  219. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  220. BUG_ON(!dma_ops);
  221. dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
  222. }
  223. static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
  224. size_t size,
  225. enum dma_data_direction direction)
  226. {
  227. return dma_map_single_attrs(dev, cpu_addr, size, direction, NULL);
  228. }
  229. static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
  230. size_t size,
  231. enum dma_data_direction direction)
  232. {
  233. dma_unmap_single_attrs(dev, dma_addr, size, direction, NULL);
  234. }
  235. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  236. unsigned long offset, size_t size,
  237. enum dma_data_direction direction)
  238. {
  239. return dma_map_page_attrs(dev, page, offset, size, direction, NULL);
  240. }
  241. static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  242. size_t size,
  243. enum dma_data_direction direction)
  244. {
  245. dma_unmap_page_attrs(dev, dma_address, size, direction, NULL);
  246. }
  247. static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
  248. int nents, enum dma_data_direction direction)
  249. {
  250. return dma_map_sg_attrs(dev, sg, nents, direction, NULL);
  251. }
  252. static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  253. int nhwentries,
  254. enum dma_data_direction direction)
  255. {
  256. dma_unmap_sg_attrs(dev, sg, nhwentries, direction, NULL);
  257. }
  258. #ifdef CONFIG_PPC_NEED_DMA_SYNC_OPS
  259. static inline void dma_sync_single_for_cpu(struct device *dev,
  260. dma_addr_t dma_handle, size_t size,
  261. enum dma_data_direction direction)
  262. {
  263. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  264. BUG_ON(!dma_ops);
  265. dma_ops->sync_single_range_for_cpu(dev, dma_handle, 0,
  266. size, direction);
  267. }
  268. static inline void dma_sync_single_for_device(struct device *dev,
  269. dma_addr_t dma_handle, size_t size,
  270. enum dma_data_direction direction)
  271. {
  272. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  273. BUG_ON(!dma_ops);
  274. dma_ops->sync_single_range_for_device(dev, dma_handle,
  275. 0, size, direction);
  276. }
  277. static inline void dma_sync_sg_for_cpu(struct device *dev,
  278. struct scatterlist *sgl, int nents,
  279. enum dma_data_direction direction)
  280. {
  281. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  282. BUG_ON(!dma_ops);
  283. dma_ops->sync_sg_for_cpu(dev, sgl, nents, direction);
  284. }
  285. static inline void dma_sync_sg_for_device(struct device *dev,
  286. struct scatterlist *sgl, int nents,
  287. enum dma_data_direction direction)
  288. {
  289. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  290. BUG_ON(!dma_ops);
  291. dma_ops->sync_sg_for_device(dev, sgl, nents, direction);
  292. }
  293. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  294. dma_addr_t dma_handle, unsigned long offset, size_t size,
  295. enum dma_data_direction direction)
  296. {
  297. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  298. BUG_ON(!dma_ops);
  299. dma_ops->sync_single_range_for_cpu(dev, dma_handle,
  300. offset, size, direction);
  301. }
  302. static inline void dma_sync_single_range_for_device(struct device *dev,
  303. dma_addr_t dma_handle, unsigned long offset, size_t size,
  304. enum dma_data_direction direction)
  305. {
  306. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  307. BUG_ON(!dma_ops);
  308. dma_ops->sync_single_range_for_device(dev, dma_handle, offset,
  309. size, direction);
  310. }
  311. #else /* CONFIG_PPC_NEED_DMA_SYNC_OPS */
  312. static inline void dma_sync_single_for_cpu(struct device *dev,
  313. dma_addr_t dma_handle, size_t size,
  314. enum dma_data_direction direction)
  315. {
  316. }
  317. static inline void dma_sync_single_for_device(struct device *dev,
  318. dma_addr_t dma_handle, size_t size,
  319. enum dma_data_direction direction)
  320. {
  321. }
  322. static inline void dma_sync_sg_for_cpu(struct device *dev,
  323. struct scatterlist *sgl, int nents,
  324. enum dma_data_direction direction)
  325. {
  326. }
  327. static inline void dma_sync_sg_for_device(struct device *dev,
  328. struct scatterlist *sgl, int nents,
  329. enum dma_data_direction direction)
  330. {
  331. }
  332. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  333. dma_addr_t dma_handle, unsigned long offset, size_t size,
  334. enum dma_data_direction direction)
  335. {
  336. }
  337. static inline void dma_sync_single_range_for_device(struct device *dev,
  338. dma_addr_t dma_handle, unsigned long offset, size_t size,
  339. enum dma_data_direction direction)
  340. {
  341. }
  342. #endif
  343. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  344. {
  345. #ifdef CONFIG_PPC64
  346. return (dma_addr == DMA_ERROR_CODE);
  347. #else
  348. return 0;
  349. #endif
  350. }
  351. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  352. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  353. #ifdef CONFIG_NOT_COHERENT_CACHE
  354. #define dma_is_consistent(d, h) (0)
  355. #else
  356. #define dma_is_consistent(d, h) (1)
  357. #endif
  358. static inline int dma_get_cache_alignment(void)
  359. {
  360. #ifdef CONFIG_PPC64
  361. /* no easy way to get cache size on all processors, so return
  362. * the maximum possible, to be safe */
  363. return (1 << INTERNODE_CACHE_SHIFT);
  364. #else
  365. /*
  366. * Each processor family will define its own L1_CACHE_SHIFT,
  367. * L1_CACHE_BYTES wraps to this, so this is always safe.
  368. */
  369. return L1_CACHE_BYTES;
  370. #endif
  371. }
  372. static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  373. enum dma_data_direction direction)
  374. {
  375. BUG_ON(direction == DMA_NONE);
  376. __dma_sync(vaddr, size, (int)direction);
  377. }
  378. #endif /* __KERNEL__ */
  379. #endif /* _ASM_DMA_MAPPING_H */