dma-mapping.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #ifndef __ASM_AVR32_DMA_MAPPING_H
  2. #define __ASM_AVR32_DMA_MAPPING_H
  3. #include <linux/mm.h>
  4. #include <linux/device.h>
  5. #include <linux/scatterlist.h>
  6. #include <asm/processor.h>
  7. #include <asm/cacheflush.h>
  8. #include <asm/io.h>
  9. extern void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  10. int direction);
  11. /*
  12. * Return whether the given device DMA address mask can be supported
  13. * properly. For example, if your device can only drive the low 24-bits
  14. * during bus mastering, then you would pass 0x00ffffff as the mask
  15. * to this function.
  16. */
  17. static inline int dma_supported(struct device *dev, u64 mask)
  18. {
  19. /* Fix when needed. I really don't know of any limitations */
  20. return 1;
  21. }
  22. static inline int dma_set_mask(struct device *dev, u64 dma_mask)
  23. {
  24. if (!dev->dma_mask || !dma_supported(dev, dma_mask))
  25. return -EIO;
  26. *dev->dma_mask = dma_mask;
  27. return 0;
  28. }
  29. /*
  30. * dma_map_single can't fail as it is implemented now.
  31. */
  32. static inline int dma_mapping_error(struct device *dev, dma_addr_t addr)
  33. {
  34. return 0;
  35. }
  36. /**
  37. * dma_alloc_coherent - allocate consistent memory for DMA
  38. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  39. * @size: required memory size
  40. * @handle: bus-specific DMA address
  41. *
  42. * Allocate some uncached, unbuffered memory for a device for
  43. * performing DMA. This function allocates pages, and will
  44. * return the CPU-viewed address, and sets @handle to be the
  45. * device-viewed address.
  46. */
  47. extern void *dma_alloc_coherent(struct device *dev, size_t size,
  48. dma_addr_t *handle, gfp_t gfp);
  49. /**
  50. * dma_free_coherent - free memory allocated by dma_alloc_coherent
  51. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  52. * @size: size of memory originally requested in dma_alloc_coherent
  53. * @cpu_addr: CPU-view address returned from dma_alloc_coherent
  54. * @handle: device-view address returned from dma_alloc_coherent
  55. *
  56. * Free (and unmap) a DMA buffer previously allocated by
  57. * dma_alloc_coherent().
  58. *
  59. * References to memory and mappings associated with cpu_addr/handle
  60. * during and after this call executing are illegal.
  61. */
  62. extern void dma_free_coherent(struct device *dev, size_t size,
  63. void *cpu_addr, dma_addr_t handle);
  64. /**
  65. * dma_alloc_writecombine - allocate write-combining memory for DMA
  66. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  67. * @size: required memory size
  68. * @handle: bus-specific DMA address
  69. *
  70. * Allocate some uncached, buffered memory for a device for
  71. * performing DMA. This function allocates pages, and will
  72. * return the CPU-viewed address, and sets @handle to be the
  73. * device-viewed address.
  74. */
  75. extern void *dma_alloc_writecombine(struct device *dev, size_t size,
  76. dma_addr_t *handle, gfp_t gfp);
  77. /**
  78. * dma_free_coherent - free memory allocated by dma_alloc_writecombine
  79. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  80. * @size: size of memory originally requested in dma_alloc_writecombine
  81. * @cpu_addr: CPU-view address returned from dma_alloc_writecombine
  82. * @handle: device-view address returned from dma_alloc_writecombine
  83. *
  84. * Free (and unmap) a DMA buffer previously allocated by
  85. * dma_alloc_writecombine().
  86. *
  87. * References to memory and mappings associated with cpu_addr/handle
  88. * during and after this call executing are illegal.
  89. */
  90. extern void dma_free_writecombine(struct device *dev, size_t size,
  91. void *cpu_addr, dma_addr_t handle);
  92. /**
  93. * dma_map_single - map a single buffer for streaming DMA
  94. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  95. * @cpu_addr: CPU direct mapped address of buffer
  96. * @size: size of buffer to map
  97. * @dir: DMA transfer direction
  98. *
  99. * Ensure that any data held in the cache is appropriately discarded
  100. * or written back.
  101. *
  102. * The device owns this memory once this call has completed. The CPU
  103. * can regain ownership by calling dma_unmap_single() or dma_sync_single().
  104. */
  105. static inline dma_addr_t
  106. dma_map_single(struct device *dev, void *cpu_addr, size_t size,
  107. enum dma_data_direction direction)
  108. {
  109. dma_cache_sync(dev, cpu_addr, size, direction);
  110. return virt_to_bus(cpu_addr);
  111. }
  112. /**
  113. * dma_unmap_single - unmap a single buffer previously mapped
  114. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  115. * @handle: DMA address of buffer
  116. * @size: size of buffer to map
  117. * @dir: DMA transfer direction
  118. *
  119. * Unmap a single streaming mode DMA translation. The handle and size
  120. * must match what was provided in the previous dma_map_single() call.
  121. * All other usages are undefined.
  122. *
  123. * After this call, reads by the CPU to the buffer are guaranteed to see
  124. * whatever the device wrote there.
  125. */
  126. static inline void
  127. dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  128. enum dma_data_direction direction)
  129. {
  130. }
  131. /**
  132. * dma_map_page - map a portion of a page for streaming DMA
  133. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  134. * @page: page that buffer resides in
  135. * @offset: offset into page for start of buffer
  136. * @size: size of buffer to map
  137. * @dir: DMA transfer direction
  138. *
  139. * Ensure that any data held in the cache is appropriately discarded
  140. * or written back.
  141. *
  142. * The device owns this memory once this call has completed. The CPU
  143. * can regain ownership by calling dma_unmap_page() or dma_sync_single().
  144. */
  145. static inline dma_addr_t
  146. dma_map_page(struct device *dev, struct page *page,
  147. unsigned long offset, size_t size,
  148. enum dma_data_direction direction)
  149. {
  150. return dma_map_single(dev, page_address(page) + offset,
  151. size, direction);
  152. }
  153. /**
  154. * dma_unmap_page - unmap a buffer previously mapped through dma_map_page()
  155. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  156. * @handle: DMA address of buffer
  157. * @size: size of buffer to map
  158. * @dir: DMA transfer direction
  159. *
  160. * Unmap a single streaming mode DMA translation. The handle and size
  161. * must match what was provided in the previous dma_map_single() call.
  162. * All other usages are undefined.
  163. *
  164. * After this call, reads by the CPU to the buffer are guaranteed to see
  165. * whatever the device wrote there.
  166. */
  167. static inline void
  168. dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
  169. enum dma_data_direction direction)
  170. {
  171. dma_unmap_single(dev, dma_address, size, direction);
  172. }
  173. /**
  174. * dma_map_sg - map a set of SG buffers for streaming mode DMA
  175. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  176. * @sg: list of buffers
  177. * @nents: number of buffers to map
  178. * @dir: DMA transfer direction
  179. *
  180. * Map a set of buffers described by scatterlist in streaming
  181. * mode for DMA. This is the scatter-gather version of the
  182. * above pci_map_single interface. Here the scatter gather list
  183. * elements are each tagged with the appropriate dma address
  184. * and length. They are obtained via sg_dma_{address,length}(SG).
  185. *
  186. * NOTE: An implementation may be able to use a smaller number of
  187. * DMA address/length pairs than there are SG table elements.
  188. * (for example via virtual mapping capabilities)
  189. * The routine returns the number of addr/length pairs actually
  190. * used, at most nents.
  191. *
  192. * Device ownership issues as mentioned above for pci_map_single are
  193. * the same here.
  194. */
  195. static inline int
  196. dma_map_sg(struct device *dev, struct scatterlist *sglist, int nents,
  197. enum dma_data_direction direction)
  198. {
  199. int i;
  200. struct scatterlist *sg;
  201. for_each_sg(sglist, sg, nents, i) {
  202. char *virt;
  203. sg->dma_address = page_to_bus(sg_page(sg)) + sg->offset;
  204. virt = sg_virt(sg);
  205. dma_cache_sync(dev, virt, sg->length, direction);
  206. }
  207. return nents;
  208. }
  209. /**
  210. * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  211. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  212. * @sg: list of buffers
  213. * @nents: number of buffers to map
  214. * @dir: DMA transfer direction
  215. *
  216. * Unmap a set of streaming mode DMA translations.
  217. * Again, CPU read rules concerning calls here are the same as for
  218. * pci_unmap_single() above.
  219. */
  220. static inline void
  221. dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  222. enum dma_data_direction direction)
  223. {
  224. }
  225. /**
  226. * dma_sync_single_for_cpu
  227. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  228. * @handle: DMA address of buffer
  229. * @size: size of buffer to map
  230. * @dir: DMA transfer direction
  231. *
  232. * Make physical memory consistent for a single streaming mode DMA
  233. * translation after a transfer.
  234. *
  235. * If you perform a dma_map_single() but wish to interrogate the
  236. * buffer using the cpu, yet do not wish to teardown the DMA mapping,
  237. * you must call this function before doing so. At the next point you
  238. * give the DMA address back to the card, you must first perform a
  239. * dma_sync_single_for_device, and then the device again owns the
  240. * buffer.
  241. */
  242. static inline void
  243. dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  244. size_t size, enum dma_data_direction direction)
  245. {
  246. /*
  247. * No need to do anything since the CPU isn't supposed to
  248. * touch this memory after we flushed it at mapping- or
  249. * sync-for-device time.
  250. */
  251. }
  252. static inline void
  253. dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  254. size_t size, enum dma_data_direction direction)
  255. {
  256. dma_cache_sync(dev, bus_to_virt(dma_handle), size, direction);
  257. }
  258. static inline void
  259. dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  260. unsigned long offset, size_t size,
  261. enum dma_data_direction direction)
  262. {
  263. /* just sync everything, that's all the pci API can do */
  264. dma_sync_single_for_cpu(dev, dma_handle, offset+size, direction);
  265. }
  266. static inline void
  267. dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  268. unsigned long offset, size_t size,
  269. enum dma_data_direction direction)
  270. {
  271. /* just sync everything, that's all the pci API can do */
  272. dma_sync_single_for_device(dev, dma_handle, offset+size, direction);
  273. }
  274. /**
  275. * dma_sync_sg_for_cpu
  276. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  277. * @sg: list of buffers
  278. * @nents: number of buffers to map
  279. * @dir: DMA transfer direction
  280. *
  281. * Make physical memory consistent for a set of streaming
  282. * mode DMA translations after a transfer.
  283. *
  284. * The same as dma_sync_single_for_* but for a scatter-gather list,
  285. * same rules and usage.
  286. */
  287. static inline void
  288. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  289. int nents, enum dma_data_direction direction)
  290. {
  291. /*
  292. * No need to do anything since the CPU isn't supposed to
  293. * touch this memory after we flushed it at mapping- or
  294. * sync-for-device time.
  295. */
  296. }
  297. static inline void
  298. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sglist,
  299. int nents, enum dma_data_direction direction)
  300. {
  301. int i;
  302. struct scatterlist *sg;
  303. for_each_sg(sglist, sg, nents, i)
  304. dma_cache_sync(dev, sg_virt(sg), sg->length, direction);
  305. }
  306. /* Now for the API extensions over the pci_ one */
  307. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  308. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  309. /* drivers/base/dma-mapping.c */
  310. extern int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  311. void *cpu_addr, dma_addr_t dma_addr, size_t size);
  312. extern int dma_common_get_sgtable(struct device *dev, struct sg_table *sgt,
  313. void *cpu_addr, dma_addr_t dma_addr,
  314. size_t size);
  315. #define dma_mmap_coherent(d, v, c, h, s) dma_common_mmap(d, v, c, h, s)
  316. #define dma_get_sgtable(d, t, v, h, s) dma_common_get_sgtable(d, t, v, h, s)
  317. #endif /* __ASM_AVR32_DMA_MAPPING_H */