dma-mapping.h 14 KB

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