dma-mapping.h 12 KB

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