dma-mapping.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. dma_addr_t (*map_single)(struct device *dev, void *ptr,
  56. size_t size, enum dma_data_direction direction,
  57. struct dma_attrs *attrs);
  58. void (*unmap_single)(struct device *dev, dma_addr_t dma_addr,
  59. size_t size, enum dma_data_direction direction,
  60. struct dma_attrs *attrs);
  61. int (*map_sg)(struct device *dev, struct scatterlist *sg,
  62. int nents, enum dma_data_direction direction,
  63. struct dma_attrs *attrs);
  64. void (*unmap_sg)(struct device *dev, struct scatterlist *sg,
  65. int nents, enum dma_data_direction direction,
  66. struct dma_attrs *attrs);
  67. int (*dma_supported)(struct device *dev, u64 mask);
  68. int (*set_dma_mask)(struct device *dev, u64 dma_mask);
  69. dma_addr_t (*map_page)(struct device *dev, struct page *page,
  70. unsigned long offset, size_t size,
  71. enum dma_data_direction direction,
  72. struct dma_attrs *attrs);
  73. void (*unmap_page)(struct device *dev,
  74. dma_addr_t dma_address, size_t size,
  75. enum dma_data_direction direction,
  76. struct dma_attrs *attrs);
  77. };
  78. /*
  79. * Available generic sets of operations
  80. */
  81. #ifdef CONFIG_PPC64
  82. extern struct dma_mapping_ops dma_iommu_ops;
  83. #endif
  84. extern struct dma_mapping_ops dma_direct_ops;
  85. static inline struct dma_mapping_ops *get_dma_ops(struct device *dev)
  86. {
  87. /* We don't handle the NULL dev case for ISA for now. We could
  88. * do it via an out of line call but it is not needed for now. The
  89. * only ISA DMA device we support is the floppy and we have a hack
  90. * in the floppy driver directly to get a device for us.
  91. */
  92. if (unlikely(dev == NULL) || dev->archdata.dma_ops == NULL) {
  93. #ifdef CONFIG_PPC64
  94. return NULL;
  95. #else
  96. /* Use default on 32-bit if dma_ops is not set up */
  97. /* TODO: Long term, we should fix drivers so that dev and
  98. * archdata dma_ops are set up for all buses.
  99. */
  100. return &dma_direct_ops;
  101. #endif
  102. }
  103. return dev->archdata.dma_ops;
  104. }
  105. static inline void set_dma_ops(struct device *dev, struct dma_mapping_ops *ops)
  106. {
  107. dev->archdata.dma_ops = ops;
  108. }
  109. static inline int dma_supported(struct device *dev, u64 mask)
  110. {
  111. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  112. if (unlikely(dma_ops == NULL))
  113. return 0;
  114. if (dma_ops->dma_supported == NULL)
  115. return 1;
  116. return dma_ops->dma_supported(dev, mask);
  117. }
  118. /* We have our own implementation of pci_set_dma_mask() */
  119. #define HAVE_ARCH_PCI_SET_DMA_MASK
  120. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  121. {
  122. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  123. if (unlikely(dma_ops == NULL))
  124. return -EIO;
  125. if (dma_ops->set_dma_mask != NULL)
  126. return dma_ops->set_dma_mask(dev, dma_mask);
  127. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  128. return -EIO;
  129. *dev->dma_mask = dma_mask;
  130. return 0;
  131. }
  132. /*
  133. * TODO: map_/unmap_single will ideally go away, to be completely
  134. * replaced by map/unmap_page. Until then, we allow dma_ops to have
  135. * one or the other, or both by checking to see if the specific
  136. * function requested exists; and if not, falling back on the other set.
  137. */
  138. static inline dma_addr_t dma_map_single_attrs(struct device *dev,
  139. void *cpu_addr,
  140. size_t size,
  141. enum dma_data_direction direction,
  142. struct dma_attrs *attrs)
  143. {
  144. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  145. BUG_ON(!dma_ops);
  146. if (dma_ops->map_single)
  147. return dma_ops->map_single(dev, cpu_addr, size, direction,
  148. attrs);
  149. return dma_ops->map_page(dev, virt_to_page(cpu_addr),
  150. (unsigned long)cpu_addr % PAGE_SIZE, size,
  151. direction, attrs);
  152. }
  153. static inline void dma_unmap_single_attrs(struct device *dev,
  154. dma_addr_t dma_addr,
  155. size_t size,
  156. enum dma_data_direction direction,
  157. struct dma_attrs *attrs)
  158. {
  159. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  160. BUG_ON(!dma_ops);
  161. if (dma_ops->unmap_single) {
  162. dma_ops->unmap_single(dev, dma_addr, size, direction, attrs);
  163. return;
  164. }
  165. dma_ops->unmap_page(dev, dma_addr, size, direction, attrs);
  166. }
  167. static inline dma_addr_t dma_map_page_attrs(struct device *dev,
  168. struct page *page,
  169. unsigned long offset, size_t size,
  170. enum dma_data_direction direction,
  171. struct dma_attrs *attrs)
  172. {
  173. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  174. BUG_ON(!dma_ops);
  175. if (dma_ops->map_page)
  176. return dma_ops->map_page(dev, page, offset, size, direction,
  177. attrs);
  178. return dma_ops->map_single(dev, page_address(page) + offset, size,
  179. 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. if (dma_ops->unmap_page) {
  190. dma_ops->unmap_page(dev, dma_address, size, direction, attrs);
  191. return;
  192. }
  193. dma_ops->unmap_single(dev, dma_address, size, direction, attrs);
  194. }
  195. static inline int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
  196. int nents, enum dma_data_direction direction,
  197. struct dma_attrs *attrs)
  198. {
  199. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  200. BUG_ON(!dma_ops);
  201. return dma_ops->map_sg(dev, sg, nents, direction, attrs);
  202. }
  203. static inline void dma_unmap_sg_attrs(struct device *dev,
  204. struct scatterlist *sg,
  205. int nhwentries,
  206. enum dma_data_direction direction,
  207. struct dma_attrs *attrs)
  208. {
  209. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  210. BUG_ON(!dma_ops);
  211. dma_ops->unmap_sg(dev, sg, nhwentries, direction, attrs);
  212. }
  213. static inline void *dma_alloc_coherent(struct device *dev, size_t size,
  214. dma_addr_t *dma_handle, gfp_t flag)
  215. {
  216. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  217. BUG_ON(!dma_ops);
  218. return dma_ops->alloc_coherent(dev, size, dma_handle, flag);
  219. }
  220. static inline void dma_free_coherent(struct device *dev, size_t size,
  221. void *cpu_addr, dma_addr_t dma_handle)
  222. {
  223. struct dma_mapping_ops *dma_ops = get_dma_ops(dev);
  224. BUG_ON(!dma_ops);
  225. dma_ops->free_coherent(dev, size, cpu_addr, dma_handle);
  226. }
  227. static inline dma_addr_t dma_map_single(struct device *dev, void *cpu_addr,
  228. size_t size,
  229. enum dma_data_direction direction)
  230. {
  231. return dma_map_single_attrs(dev, cpu_addr, size, direction, NULL);
  232. }
  233. static inline void dma_unmap_single(struct device *dev, dma_addr_t dma_addr,
  234. size_t size,
  235. enum dma_data_direction direction)
  236. {
  237. dma_unmap_single_attrs(dev, dma_addr, size, direction, NULL);
  238. }
  239. static inline dma_addr_t dma_map_page(struct device *dev, struct page *page,
  240. unsigned long offset, size_t size,
  241. enum dma_data_direction direction)
  242. {
  243. return dma_map_page_attrs(dev, page, offset, size, direction, NULL);
  244. }
  245. static inline void dma_unmap_page(struct device *dev, dma_addr_t dma_address,
  246. size_t size,
  247. enum dma_data_direction direction)
  248. {
  249. dma_unmap_page_attrs(dev, dma_address, size, direction, NULL);
  250. }
  251. static inline int dma_map_sg(struct device *dev, struct scatterlist *sg,
  252. int nents, enum dma_data_direction direction)
  253. {
  254. return dma_map_sg_attrs(dev, sg, nents, direction, NULL);
  255. }
  256. static inline void dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  257. int nhwentries,
  258. enum dma_data_direction direction)
  259. {
  260. dma_unmap_sg_attrs(dev, sg, nhwentries, direction, NULL);
  261. }
  262. static inline void dma_sync_single_for_cpu(struct device *dev,
  263. dma_addr_t dma_handle, size_t size,
  264. enum dma_data_direction direction)
  265. {
  266. BUG_ON(direction == DMA_NONE);
  267. __dma_sync(bus_to_virt(dma_handle), size, direction);
  268. }
  269. static inline void dma_sync_single_for_device(struct device *dev,
  270. dma_addr_t dma_handle, size_t size,
  271. enum dma_data_direction direction)
  272. {
  273. BUG_ON(direction == DMA_NONE);
  274. __dma_sync(bus_to_virt(dma_handle), size, direction);
  275. }
  276. static inline void dma_sync_sg_for_cpu(struct device *dev,
  277. struct scatterlist *sgl, int nents,
  278. enum dma_data_direction direction)
  279. {
  280. struct scatterlist *sg;
  281. int i;
  282. BUG_ON(direction == DMA_NONE);
  283. for_each_sg(sgl, sg, nents, i)
  284. __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
  285. }
  286. static inline void dma_sync_sg_for_device(struct device *dev,
  287. struct scatterlist *sgl, int nents,
  288. enum dma_data_direction direction)
  289. {
  290. struct scatterlist *sg;
  291. int i;
  292. BUG_ON(direction == DMA_NONE);
  293. for_each_sg(sgl, sg, nents, i)
  294. __dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
  295. }
  296. static inline int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  297. {
  298. #ifdef CONFIG_PPC64
  299. return (dma_addr == DMA_ERROR_CODE);
  300. #else
  301. return 0;
  302. #endif
  303. }
  304. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  305. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  306. #ifdef CONFIG_NOT_COHERENT_CACHE
  307. #define dma_is_consistent(d, h) (0)
  308. #else
  309. #define dma_is_consistent(d, h) (1)
  310. #endif
  311. static inline int dma_get_cache_alignment(void)
  312. {
  313. #ifdef CONFIG_PPC64
  314. /* no easy way to get cache size on all processors, so return
  315. * the maximum possible, to be safe */
  316. return (1 << INTERNODE_CACHE_SHIFT);
  317. #else
  318. /*
  319. * Each processor family will define its own L1_CACHE_SHIFT,
  320. * L1_CACHE_BYTES wraps to this, so this is always safe.
  321. */
  322. return L1_CACHE_BYTES;
  323. #endif
  324. }
  325. static inline void dma_sync_single_range_for_cpu(struct device *dev,
  326. dma_addr_t dma_handle, unsigned long offset, size_t size,
  327. enum dma_data_direction direction)
  328. {
  329. /* just sync everything for now */
  330. dma_sync_single_for_cpu(dev, dma_handle, offset + size, direction);
  331. }
  332. static inline void dma_sync_single_range_for_device(struct device *dev,
  333. dma_addr_t dma_handle, unsigned long offset, size_t size,
  334. enum dma_data_direction direction)
  335. {
  336. /* just sync everything for now */
  337. dma_sync_single_for_device(dev, dma_handle, offset + size, direction);
  338. }
  339. static inline void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  340. enum dma_data_direction direction)
  341. {
  342. BUG_ON(direction == DMA_NONE);
  343. __dma_sync(vaddr, size, (int)direction);
  344. }
  345. #endif /* __KERNEL__ */
  346. #endif /* _ASM_DMA_MAPPING_H */