dma-mapping.h 14 KB

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