dma-mapping.h 12 KB

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