dma-mapping.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 0;
  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. 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. consistent_sync(virt, sg->length, dir);
  233. }
  234. return nents;
  235. }
  236. #else
  237. extern int dma_map_sg(struct device *, struct scatterlist *, int, enum dma_data_direction);
  238. #endif
  239. /**
  240. * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  241. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  242. * @sg: list of buffers
  243. * @nents: number of buffers to map
  244. * @dir: DMA transfer direction
  245. *
  246. * Unmap a set of streaming mode DMA translations.
  247. * Again, CPU read rules concerning calls here are the same as for
  248. * dma_unmap_single() above.
  249. */
  250. #ifndef CONFIG_DMABOUNCE
  251. static inline void
  252. dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
  253. enum dma_data_direction dir)
  254. {
  255. /* nothing to do */
  256. }
  257. #else
  258. extern void dma_unmap_sg(struct device *, struct scatterlist *, int, enum dma_data_direction);
  259. #endif
  260. /**
  261. * dma_sync_single_for_cpu
  262. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  263. * @handle: DMA address of buffer
  264. * @size: size of buffer to map
  265. * @dir: DMA transfer direction
  266. *
  267. * Make physical memory consistent for a single streaming mode DMA
  268. * translation after a transfer.
  269. *
  270. * If you perform a dma_map_single() but wish to interrogate the
  271. * buffer using the cpu, yet do not wish to teardown the PCI dma
  272. * mapping, you must call this function before doing so. At the
  273. * next point you give the PCI dma address back to the card, you
  274. * must first the perform a dma_sync_for_device, and then the
  275. * device again owns the buffer.
  276. */
  277. #ifndef CONFIG_DMABOUNCE
  278. static inline void
  279. dma_sync_single_for_cpu(struct device *dev, dma_addr_t handle, size_t size,
  280. enum dma_data_direction dir)
  281. {
  282. consistent_sync((void *)dma_to_virt(dev, handle), size, dir);
  283. }
  284. static inline void
  285. dma_sync_single_for_device(struct device *dev, dma_addr_t handle, size_t size,
  286. enum dma_data_direction dir)
  287. {
  288. consistent_sync((void *)dma_to_virt(dev, handle), size, dir);
  289. }
  290. #else
  291. extern void dma_sync_single_for_cpu(struct device*, dma_addr_t, size_t, enum dma_data_direction);
  292. extern void dma_sync_single_for_device(struct device*, dma_addr_t, size_t, enum dma_data_direction);
  293. #endif
  294. /**
  295. * dma_sync_sg_for_cpu
  296. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  297. * @sg: list of buffers
  298. * @nents: number of buffers to map
  299. * @dir: DMA transfer direction
  300. *
  301. * Make physical memory consistent for a set of streaming
  302. * mode DMA translations after a transfer.
  303. *
  304. * The same as dma_sync_single_for_* but for a scatter-gather list,
  305. * same rules and usage.
  306. */
  307. #ifndef CONFIG_DMABOUNCE
  308. static inline void
  309. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nents,
  310. enum dma_data_direction dir)
  311. {
  312. int i;
  313. for (i = 0; i < nents; i++, sg++) {
  314. char *virt = page_address(sg->page) + sg->offset;
  315. consistent_sync(virt, sg->length, dir);
  316. }
  317. }
  318. static inline void
  319. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nents,
  320. enum dma_data_direction dir)
  321. {
  322. int i;
  323. for (i = 0; i < nents; i++, sg++) {
  324. char *virt = page_address(sg->page) + sg->offset;
  325. consistent_sync(virt, sg->length, dir);
  326. }
  327. }
  328. #else
  329. extern void dma_sync_sg_for_cpu(struct device*, struct scatterlist*, int, enum dma_data_direction);
  330. extern void dma_sync_sg_for_device(struct device*, struct scatterlist*, int, enum dma_data_direction);
  331. #endif
  332. #ifdef CONFIG_DMABOUNCE
  333. /*
  334. * For SA-1111, IXP425, and ADI systems the dma-mapping functions are "magic"
  335. * and utilize bounce buffers as needed to work around limited DMA windows.
  336. *
  337. * On the SA-1111, a bug limits DMA to only certain regions of RAM.
  338. * On the IXP425, the PCI inbound window is 64MB (256MB total RAM)
  339. * On some ADI engineering sytems, PCI inbound window is 32MB (12MB total RAM)
  340. *
  341. * The following are helper functions used by the dmabounce subystem
  342. *
  343. */
  344. /**
  345. * dmabounce_register_dev
  346. *
  347. * @dev: valid struct device pointer
  348. * @small_buf_size: size of buffers to use with small buffer pool
  349. * @large_buf_size: size of buffers to use with large buffer pool (can be 0)
  350. *
  351. * This function should be called by low-level platform code to register
  352. * a device as requireing DMA buffer bouncing. The function will allocate
  353. * appropriate DMA pools for the device.
  354. *
  355. */
  356. extern int dmabounce_register_dev(struct device *, unsigned long, unsigned long);
  357. /**
  358. * dmabounce_unregister_dev
  359. *
  360. * @dev: valid struct device pointer
  361. *
  362. * This function should be called by low-level platform code when device
  363. * that was previously registered with dmabounce_register_dev is removed
  364. * from the system.
  365. *
  366. */
  367. extern void dmabounce_unregister_dev(struct device *);
  368. /**
  369. * dma_needs_bounce
  370. *
  371. * @dev: valid struct device pointer
  372. * @dma_handle: dma_handle of unbounced buffer
  373. * @size: size of region being mapped
  374. *
  375. * Platforms that utilize the dmabounce mechanism must implement
  376. * this function.
  377. *
  378. * The dmabounce routines call this function whenever a dma-mapping
  379. * is requested to determine whether a given buffer needs to be bounced
  380. * or not. The function must return 0 if the the buffer is OK for
  381. * DMA access and 1 if the buffer needs to be bounced.
  382. *
  383. */
  384. extern int dma_needs_bounce(struct device*, dma_addr_t, size_t);
  385. #endif /* CONFIG_DMABOUNCE */
  386. #endif /* __KERNEL__ */
  387. #endif