dma-mapping.h 11 KB

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