dma-mapping.h 13 KB

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