dma-mapping.h 10 KB

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