dma-mapping.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 *sg, int nents,
  197. enum dma_data_direction direction)
  198. {
  199. int i;
  200. for (i = 0; i < nents; i++) {
  201. char *virt;
  202. sg[i].dma_address = page_to_bus(sg_page(&sg[i])) + sg[i].offset;
  203. virt = sg_virt(&sg[i]);
  204. dma_cache_sync(dev, virt, sg[i].length, direction);
  205. }
  206. return nents;
  207. }
  208. /**
  209. * dma_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
  210. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  211. * @sg: list of buffers
  212. * @nents: number of buffers to map
  213. * @dir: DMA transfer direction
  214. *
  215. * Unmap a set of streaming mode DMA translations.
  216. * Again, CPU read rules concerning calls here are the same as for
  217. * pci_unmap_single() above.
  218. */
  219. static inline void
  220. dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  221. enum dma_data_direction direction)
  222. {
  223. }
  224. /**
  225. * dma_sync_single_for_cpu
  226. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  227. * @handle: DMA address of buffer
  228. * @size: size of buffer to map
  229. * @dir: DMA transfer direction
  230. *
  231. * Make physical memory consistent for a single streaming mode DMA
  232. * translation after a transfer.
  233. *
  234. * If you perform a dma_map_single() but wish to interrogate the
  235. * buffer using the cpu, yet do not wish to teardown the DMA mapping,
  236. * you must call this function before doing so. At the next point you
  237. * give the DMA address back to the card, you must first perform a
  238. * dma_sync_single_for_device, and then the device again owns the
  239. * buffer.
  240. */
  241. static inline void
  242. dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  243. size_t size, enum dma_data_direction direction)
  244. {
  245. /*
  246. * No need to do anything since the CPU isn't supposed to
  247. * touch this memory after we flushed it at mapping- or
  248. * sync-for-device time.
  249. */
  250. }
  251. static inline void
  252. dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  253. size_t size, enum dma_data_direction direction)
  254. {
  255. dma_cache_sync(dev, bus_to_virt(dma_handle), size, direction);
  256. }
  257. static inline void
  258. dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  259. unsigned long offset, size_t size,
  260. enum dma_data_direction direction)
  261. {
  262. /* just sync everything, that's all the pci API can do */
  263. dma_sync_single_for_cpu(dev, dma_handle, offset+size, direction);
  264. }
  265. static inline void
  266. dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  267. unsigned long offset, size_t size,
  268. enum dma_data_direction direction)
  269. {
  270. /* just sync everything, that's all the pci API can do */
  271. dma_sync_single_for_device(dev, dma_handle, offset+size, direction);
  272. }
  273. /**
  274. * dma_sync_sg_for_cpu
  275. * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
  276. * @sg: list of buffers
  277. * @nents: number of buffers to map
  278. * @dir: DMA transfer direction
  279. *
  280. * Make physical memory consistent for a set of streaming
  281. * mode DMA translations after a transfer.
  282. *
  283. * The same as dma_sync_single_for_* but for a scatter-gather list,
  284. * same rules and usage.
  285. */
  286. static inline void
  287. dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  288. int nents, enum dma_data_direction direction)
  289. {
  290. /*
  291. * No need to do anything since the CPU isn't supposed to
  292. * touch this memory after we flushed it at mapping- or
  293. * sync-for-device time.
  294. */
  295. }
  296. static inline void
  297. dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  298. int nents, enum dma_data_direction direction)
  299. {
  300. int i;
  301. for (i = 0; i < nents; i++) {
  302. dma_cache_sync(dev, sg_virt(&sg[i]), sg[i].length, direction);
  303. }
  304. }
  305. /* Now for the API extensions over the pci_ one */
  306. #define dma_alloc_noncoherent(d, s, h, f) dma_alloc_coherent(d, s, h, f)
  307. #define dma_free_noncoherent(d, s, v, h) dma_free_coherent(d, s, v, h)
  308. #endif /* __ASM_AVR32_DMA_MAPPING_H */