dma-mapping.h 13 KB

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