dma-mapping.h 10 KB

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