dma-mapping.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #ifndef ASMARM_DMA_MAPPING_H
  2. #define ASMARM_DMA_MAPPING_H
  3. #ifdef __KERNEL__
  4. #include <linux/mm.h> /* need struct page */
  5. #include <asm/scatterlist.h>
  6. /*
  7. * DMA-consistent mapping functions. These allocate/free a region of
  8. * uncached, unwrite-buffered mapped memory space for use with DMA
  9. * devices. This is the "generic" version. The PCI specific version
  10. * is in pci.h
  11. */
  12. extern void consistent_sync(void *kaddr, size_t size, int rw);
  13. /*
  14. * Return whether the given device DMA address mask can be supported
  15. * properly. For example, if your device can only drive the low 24-bits
  16. * during bus mastering, then you would pass 0x00ffffff as the mask
  17. * to this function.
  18. *
  19. * FIXME: This should really be a platform specific issue - we should
  20. * return false if GFP_DMA allocations may not satisfy the supplied 'mask'.
  21. */
  22. static inline int dma_supported(struct device *dev, u64 mask)
  23. {
  24. return dev->dma_mask && *dev->dma_mask != 0;
  25. }
  26. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  27. {
  28. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  29. return -EIO;
  30. *dev->dma_mask = dma_mask;
  31. return 0;
  32. }
  33. static inline int dma_get_cache_alignment(void)
  34. {
  35. return 32;
  36. }
  37. static inline int dma_is_consistent(dma_addr_t handle)
  38. {
  39. return !!arch_is_coherent();
  40. }
  41. /*
  42. * DMA errors are defined by all-bits-set in the DMA address.
  43. */
  44. static inline int dma_mapping_error(dma_addr_t dma_addr)
  45. {
  46. return dma_addr == ~0;
  47. }
  48. /**
  49. * dma_alloc_coherent - allocate consistent memory for DMA
  50. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  51. * @size: required memory size
  52. * @handle: bus-specific DMA address
  53. *
  54. * Allocate some uncached, unbuffered memory for a device for
  55. * performing DMA. This function allocates pages, and will
  56. * return the CPU-viewed address, and sets @handle to be the
  57. * device-viewed address.
  58. */
  59. extern void *
  60. dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp);
  61. /**
  62. * dma_free_coherent - free memory allocated by dma_alloc_coherent
  63. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  64. * @size: size of memory originally requested in dma_alloc_coherent
  65. * @cpu_addr: CPU-view address returned from dma_alloc_coherent
  66. * @handle: device-view address returned from dma_alloc_coherent
  67. *
  68. * Free (and unmap) a DMA buffer previously allocated by
  69. * dma_alloc_coherent().
  70. *
  71. * References to memory and mappings associated with cpu_addr/handle
  72. * during and after this call executing are illegal.
  73. */
  74. extern void
  75. dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
  76. dma_addr_t handle);
  77. /**
  78. * dma_mmap_coherent - map a coherent DMA allocation into user space
  79. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  80. * @vma: vm_area_struct describing requested user mapping
  81. * @cpu_addr: kernel CPU-view address returned from dma_alloc_coherent
  82. * @handle: device-view address returned from dma_alloc_coherent
  83. * @size: size of memory originally requested in dma_alloc_coherent
  84. *
  85. * Map a coherent DMA buffer previously allocated by dma_alloc_coherent
  86. * into user space. The coherent DMA buffer must not be freed by the
  87. * driver until the user space mapping has been released.
  88. */
  89. int dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
  90. void *cpu_addr, dma_addr_t handle, size_t size);
  91. /**
  92. * dma_alloc_writecombine - allocate writecombining memory for DMA
  93. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  94. * @size: required memory size
  95. * @handle: bus-specific DMA address
  96. *
  97. * Allocate some uncached, buffered memory for a device for
  98. * performing DMA. This function allocates pages, and will
  99. * return the CPU-viewed address, and sets @handle to be the
  100. * device-viewed address.
  101. */
  102. extern void *
  103. dma_alloc_writecombine(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp);
  104. #define dma_free_writecombine(dev,size,cpu_addr,handle) \
  105. dma_free_coherent(dev,size,cpu_addr,handle)
  106. int dma_mmap_writecombine(struct device *dev, struct vm_area_struct *vma,
  107. void *cpu_addr, dma_addr_t handle, size_t size);
  108. /**
  109. * dma_map_single - map a single buffer for streaming DMA
  110. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  111. * @cpu_addr: CPU direct mapped address of buffer
  112. * @size: size of buffer to map
  113. * @dir: DMA transfer direction
  114. *
  115. * Ensure that any data held in the cache is appropriately discarded
  116. * or written back.
  117. *
  118. * The device owns this memory once this call has completed. The CPU
  119. * can regain ownership by calling dma_unmap_single() or
  120. * dma_sync_single_for_cpu().
  121. */
  122. #ifndef CONFIG_DMABOUNCE
  123. static inline dma_addr_t
  124. dma_map_single(struct device *dev, void *cpu_addr, size_t size,
  125. enum dma_data_direction dir)
  126. {
  127. if (!arch_is_coherent())
  128. consistent_sync(cpu_addr, size, dir);
  129. return virt_to_dma(dev, (unsigned long)cpu_addr);
  130. }
  131. #else
  132. extern dma_addr_t dma_map_single(struct device *,void *, size_t, enum dma_data_direction);
  133. #endif
  134. /**
  135. * dma_map_page - map a portion of a page for streaming DMA
  136. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  137. * @page: page that buffer resides in
  138. * @offset: offset into page for start of buffer
  139. * @size: size of buffer to map
  140. * @dir: DMA transfer direction
  141. *
  142. * Ensure that any data held in the cache is appropriately discarded
  143. * or written back.
  144. *
  145. * The device owns this memory once this call has completed. The CPU
  146. * can regain ownership by calling dma_unmap_page() or
  147. * dma_sync_single_for_cpu().
  148. */
  149. static inline dma_addr_t
  150. dma_map_page(struct device *dev, struct page *page,
  151. unsigned long offset, size_t size,
  152. enum dma_data_direction dir)
  153. {
  154. return dma_map_single(dev, page_address(page) + offset, size, (int)dir);
  155. }
  156. /**
  157. * dma_unmap_single - unmap a single buffer previously mapped
  158. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  159. * @handle: DMA address of buffer
  160. * @size: size of buffer to map
  161. * @dir: DMA transfer direction
  162. *
  163. * Unmap a single streaming mode DMA translation. The handle and size
  164. * must match what was provided in the previous dma_map_single() call.
  165. * All other usages are undefined.
  166. *
  167. * After this call, reads by the CPU to the buffer are guaranteed to see
  168. * whatever the device wrote there.
  169. */
  170. #ifndef CONFIG_DMABOUNCE
  171. static inline void
  172. dma_unmap_single(struct device *dev, dma_addr_t handle, size_t size,
  173. enum dma_data_direction dir)
  174. {
  175. /* nothing to do */
  176. }
  177. #else
  178. extern void dma_unmap_single(struct device *, dma_addr_t, size_t, enum dma_data_direction);
  179. #endif
  180. /**
  181. * dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
  182. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  183. * @handle: DMA address of buffer
  184. * @size: size of buffer to map
  185. * @dir: DMA transfer direction
  186. *
  187. * Unmap a single streaming mode DMA translation. The handle and size
  188. * must match what was provided in the previous dma_map_single() call.
  189. * All other usages are undefined.
  190. *
  191. * After this call, reads by the CPU to the buffer are guaranteed to see
  192. * whatever the device wrote there.
  193. */
  194. static inline void
  195. dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
  196. enum dma_data_direction dir)
  197. {
  198. dma_unmap_single(dev, handle, size, (int)dir);
  199. }
  200. /**
  201. * dma_map_sg - map a set of SG buffers for streaming mode DMA
  202. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  203. * @sg: list of buffers
  204. * @nents: number of buffers to map
  205. * @dir: DMA transfer direction
  206. *
  207. * Map a set of buffers described by scatterlist in streaming
  208. * mode for DMA. This is the scatter-gather version of the
  209. * above dma_map_single interface. Here the scatter gather list
  210. * elements are each tagged with the appropriate dma address
  211. * and length. They are obtained via sg_dma_{address,length}(SG).
  212. *
  213. * NOTE: An implementation may be able to use a smaller number of
  214. * DMA address/length pairs than there are SG table elements.
  215. * (for example via virtual mapping capabilities)
  216. * The routine returns the number of addr/length pairs actually
  217. * used, at most nents.
  218. *
  219. * Device ownership issues as mentioned above for dma_map_single are
  220. * the same here.
  221. */
  222. #ifndef CONFIG_DMABOUNCE
  223. static inline int
  224. dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  225. enum dma_data_direction dir)
  226. {
  227. int i;
  228. for (i = 0; i < nents; i++, sg++) {
  229. char *virt;
  230. sg->dma_address = page_to_dma(dev, sg->page) + sg->offset;
  231. virt = page_address(sg->page) + sg->offset;
  232. if (!arch_is_coherent())
  233. consistent_sync(virt, sg->length, dir);
  234. }
  235. return nents;
  236. }
  237. #else
  238. extern int dma_map_sg(struct device *, struct scatterlist *, int, enum dma_data_direction);
  239. #endif
  240. /**
  241. * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  242. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  243. * @sg: list of buffers
  244. * @nents: number of buffers to map
  245. * @dir: DMA transfer direction
  246. *
  247. * Unmap a set of streaming mode DMA translations.
  248. * Again, CPU read rules concerning calls here are the same as for
  249. * dma_unmap_single() above.
  250. */
  251. #ifndef CONFIG_DMABOUNCE
  252. static inline void
  253. dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  254. enum dma_data_direction dir)
  255. {
  256. /* nothing to do */
  257. }
  258. #else
  259. extern void dma_unmap_sg(struct device *, struct scatterlist *, int, enum dma_data_direction);
  260. #endif
  261. /**
  262. * dma_sync_single_for_cpu
  263. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  264. * @handle: DMA address of buffer
  265. * @size: size of buffer to map
  266. * @dir: DMA transfer direction
  267. *
  268. * Make physical memory consistent for a single streaming mode DMA
  269. * translation after a transfer.
  270. *
  271. * If you perform a dma_map_single() but wish to interrogate the
  272. * buffer using the cpu, yet do not wish to teardown the PCI dma
  273. * mapping, you must call this function before doing so. At the
  274. * next point you give the PCI dma address back to the card, you
  275. * must first the perform a dma_sync_for_device, and then the
  276. * device again owns the buffer.
  277. */
  278. #ifndef CONFIG_DMABOUNCE
  279. static inline void
  280. dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle, size_t size,
  281. enum dma_data_direction dir)
  282. {
  283. if (!arch_is_coherent())
  284. consistent_sync((void *)dma_to_virt(dev, handle), size, dir);
  285. }
  286. static inline void
  287. dma_sync_single_for_device(struct device *dev, dma_addr_t handle, size_t size,
  288. enum dma_data_direction dir)
  289. {
  290. if (!arch_is_coherent())
  291. consistent_sync((void *)dma_to_virt(dev, handle), size, dir);
  292. }
  293. #else
  294. extern void dma_sync_single_for_cpu(struct device*, dma_addr_t, size_t, enum dma_data_direction);
  295. extern void dma_sync_single_for_device(struct device*, dma_addr_t, size_t, enum dma_data_direction);
  296. #endif
  297. /**
  298. * dma_sync_sg_for_cpu
  299. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  300. * @sg: list of buffers
  301. * @nents: number of buffers to map
  302. * @dir: DMA transfer direction
  303. *
  304. * Make physical memory consistent for a set of streaming
  305. * mode DMA translations after a transfer.
  306. *
  307. * The same as dma_sync_single_for_* but for a scatter-gather list,
  308. * same rules and usage.
  309. */
  310. #ifndef CONFIG_DMABOUNCE
  311. static inline void
  312. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
  313. enum dma_data_direction dir)
  314. {
  315. int i;
  316. for (i = 0; i < nents; i++, sg++) {
  317. char *virt = page_address(sg->page) + sg->offset;
  318. if (!arch_is_coherent())
  319. consistent_sync(virt, sg->length, dir);
  320. }
  321. }
  322. static inline void
  323. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
  324. enum dma_data_direction dir)
  325. {
  326. int i;
  327. for (i = 0; i < nents; i++, sg++) {
  328. char *virt = page_address(sg->page) + sg->offset;
  329. if (!arch_is_coherent())
  330. consistent_sync(virt, sg->length, dir);
  331. }
  332. }
  333. #else
  334. extern void dma_sync_sg_for_cpu(struct device*, struct scatterlist*, int, enum dma_data_direction);
  335. extern void dma_sync_sg_for_device(struct device*, struct scatterlist*, int, enum dma_data_direction);
  336. #endif
  337. #ifdef CONFIG_DMABOUNCE
  338. /*
  339. * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
  340. * and utilize bounce buffers as needed to work around limited DMA windows.
  341. *
  342. * On the SA-1111, a bug limits DMA to only certain regions of RAM.
  343. * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
  344. * On some ADI engineering sytems, PCI inbound window is 32MB (12MB total RAM)
  345. *
  346. * The following are helper functions used by the dmabounce subystem
  347. *
  348. */
  349. /**
  350. * dmabounce_register_dev
  351. *
  352. * @dev: valid struct device pointer
  353. * @small_buf_size: size of buffers to use with small buffer pool
  354. * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
  355. *
  356. * This function should be called by low-level platform code to register
  357. * a device as requireing DMA buffer bouncing. The function will allocate
  358. * appropriate DMA pools for the device.
  359. *
  360. */
  361. extern int dmabounce_register_dev(struct device *, unsigned long, unsigned long);
  362. /**
  363. * dmabounce_unregister_dev
  364. *
  365. * @dev: valid struct device pointer
  366. *
  367. * This function should be called by low-level platform code when device
  368. * that was previously registered with dmabounce_register_dev is removed
  369. * from the system.
  370. *
  371. */
  372. extern void dmabounce_unregister_dev(struct device *);
  373. /**
  374. * dma_needs_bounce
  375. *
  376. * @dev: valid struct device pointer
  377. * @dma_handle: dma_handle of unbounced buffer
  378. * @size: size of region being mapped
  379. *
  380. * Platforms that utilize the dmabounce mechanism must implement
  381. * this function.
  382. *
  383. * The dmabounce routines call this function whenever a dma-mapping
  384. * is requested to determine whether a given buffer needs to be bounced
  385. * or not. The function must return 0 if the the buffer is OK for
  386. * DMA access and 1 if the buffer needs to be bounced.
  387. *
  388. */
  389. extern int dma_needs_bounce(struct device*, dma_addr_t, size_t);
  390. #endif /* CONFIG_DMABOUNCE */
  391. #endif /* __KERNEL__ */
  392. #endif