dma-default.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
  7. * Copyright (C) 2000, 2001, 06 Ralf Baechle <ralf@linux-mips.org>
  8. * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/mm.h>
  13. #include <linux/module.h>
  14. #include <linux/string.h>
  15. #include <asm/cache.h>
  16. #include <asm/io.h>
  17. #include <dma-coherence.h>
  18. /*
  19. * Warning on the terminology - Linux calls an uncached area coherent;
  20. * MIPS terminology calls memory areas with hardware maintained coherency
  21. * coherent.
  22. */
  23. static inline int cpu_is_noncoherent_r10000(struct device *dev)
  24. {
  25. return !plat_device_is_coherent(dev) &&
  26. (current_cpu_data.cputype == CPU_R10000 &&
  27. current_cpu_data.cputype == CPU_R12000);
  28. }
  29. void *dma_alloc_noncoherent(struct device *dev, size_t size,
  30. dma_addr_t * dma_handle, gfp_t gfp)
  31. {
  32. void *ret;
  33. /* ignore region specifiers */
  34. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  35. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  36. gfp |= GFP_DMA;
  37. ret = (void *) __get_free_pages(gfp, get_order(size));
  38. if (ret != NULL) {
  39. memset(ret, 0, size);
  40. *dma_handle = plat_map_dma_mem(dev, ret, size);
  41. }
  42. return ret;
  43. }
  44. EXPORT_SYMBOL(dma_alloc_noncoherent);
  45. void *dma_alloc_coherent(struct device *dev, size_t size,
  46. dma_addr_t * dma_handle, gfp_t gfp)
  47. {
  48. void *ret;
  49. /* ignore region specifiers */
  50. gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
  51. if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
  52. gfp |= GFP_DMA;
  53. ret = (void *) __get_free_pages(gfp, get_order(size));
  54. if (ret) {
  55. memset(ret, 0, size);
  56. *dma_handle = plat_map_dma_mem(dev, ret, size);
  57. if (!plat_device_is_coherent(dev)) {
  58. dma_cache_wback_inv((unsigned long) ret, size);
  59. ret = UNCAC_ADDR(ret);
  60. }
  61. }
  62. return ret;
  63. }
  64. EXPORT_SYMBOL(dma_alloc_coherent);
  65. void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  66. dma_addr_t dma_handle)
  67. {
  68. free_pages((unsigned long) vaddr, get_order(size));
  69. }
  70. EXPORT_SYMBOL(dma_free_noncoherent);
  71. void dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  72. dma_addr_t dma_handle)
  73. {
  74. unsigned long addr = (unsigned long) vaddr;
  75. if (!plat_device_is_coherent(dev))
  76. addr = CAC_ADDR(addr);
  77. free_pages(addr, get_order(size));
  78. }
  79. EXPORT_SYMBOL(dma_free_coherent);
  80. static inline void __dma_sync(unsigned long addr, size_t size,
  81. enum dma_data_direction direction)
  82. {
  83. switch (direction) {
  84. case DMA_TO_DEVICE:
  85. dma_cache_wback(addr, size);
  86. break;
  87. case DMA_FROM_DEVICE:
  88. dma_cache_inv(addr, size);
  89. break;
  90. case DMA_BIDIRECTIONAL:
  91. dma_cache_wback_inv(addr, size);
  92. break;
  93. default:
  94. BUG();
  95. }
  96. }
  97. dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size,
  98. enum dma_data_direction direction)
  99. {
  100. unsigned long addr = (unsigned long) ptr;
  101. if (!plat_device_is_coherent(dev))
  102. __dma_sync(addr, size, direction);
  103. return plat_map_dma_mem(dev, ptr, size);
  104. }
  105. EXPORT_SYMBOL(dma_map_single);
  106. void dma_unmap_single(struct device *dev, dma_addr_t dma_addr, size_t size,
  107. enum dma_data_direction direction)
  108. {
  109. if (cpu_is_noncoherent_r10000(dev))
  110. __dma_sync(plat_dma_addr_to_phys(dma_addr) + PAGE_OFFSET, size,
  111. direction);
  112. plat_unmap_dma_mem(dma_addr);
  113. }
  114. EXPORT_SYMBOL(dma_unmap_single);
  115. int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents,
  116. enum dma_data_direction direction)
  117. {
  118. int i;
  119. BUG_ON(direction == DMA_NONE);
  120. for (i = 0; i < nents; i++, sg++) {
  121. unsigned long addr;
  122. addr = (unsigned long) page_address(sg->page);
  123. if (!plat_device_is_coherent(dev) && addr)
  124. __dma_sync(addr + sg->offset, sg->length, direction);
  125. sg->dma_address = plat_map_dma_mem_page(dev, sg->page) +
  126. sg->offset;
  127. }
  128. return nents;
  129. }
  130. EXPORT_SYMBOL(dma_map_sg);
  131. dma_addr_t dma_map_page(struct device *dev, struct page *page,
  132. unsigned long offset, size_t size, enum dma_data_direction direction)
  133. {
  134. BUG_ON(direction == DMA_NONE);
  135. if (!plat_device_is_coherent(dev)) {
  136. unsigned long addr;
  137. addr = (unsigned long) page_address(page) + offset;
  138. dma_cache_wback_inv(addr, size);
  139. }
  140. return plat_map_dma_mem_page(dev, page) + offset;
  141. }
  142. EXPORT_SYMBOL(dma_map_page);
  143. void dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
  144. enum dma_data_direction direction)
  145. {
  146. BUG_ON(direction == DMA_NONE);
  147. if (!plat_device_is_coherent(dev) && direction != DMA_TO_DEVICE) {
  148. unsigned long addr;
  149. addr = plat_dma_addr_to_phys(dma_address);
  150. dma_cache_wback_inv(addr, size);
  151. }
  152. plat_unmap_dma_mem(dma_address);
  153. }
  154. EXPORT_SYMBOL(dma_unmap_page);
  155. void dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nhwentries,
  156. enum dma_data_direction direction)
  157. {
  158. unsigned long addr;
  159. int i;
  160. BUG_ON(direction == DMA_NONE);
  161. for (i = 0; i < nhwentries; i++, sg++) {
  162. if (!plat_device_is_coherent(dev) &&
  163. direction != DMA_TO_DEVICE) {
  164. addr = (unsigned long) page_address(sg->page);
  165. if (addr)
  166. __dma_sync(addr + sg->offset, sg->length,
  167. direction);
  168. }
  169. plat_unmap_dma_mem(sg->dma_address);
  170. }
  171. }
  172. EXPORT_SYMBOL(dma_unmap_sg);
  173. void dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle,
  174. size_t size, enum dma_data_direction direction)
  175. {
  176. BUG_ON(direction == DMA_NONE);
  177. if (cpu_is_noncoherent_r10000(dev)) {
  178. unsigned long addr;
  179. addr = PAGE_OFFSET + plat_dma_addr_to_phys(dma_handle);
  180. __dma_sync(addr, size, direction);
  181. }
  182. }
  183. EXPORT_SYMBOL(dma_sync_single_for_cpu);
  184. void dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
  185. size_t size, enum dma_data_direction direction)
  186. {
  187. BUG_ON(direction == DMA_NONE);
  188. if (!plat_device_is_coherent(dev)) {
  189. unsigned long addr;
  190. addr = PAGE_OFFSET + plat_dma_addr_to_phys(dma_handle);
  191. __dma_sync(addr, size, direction);
  192. }
  193. }
  194. EXPORT_SYMBOL(dma_sync_single_for_device);
  195. void dma_sync_single_range_for_cpu(struct device *dev, dma_addr_t dma_handle,
  196. unsigned long offset, size_t size, enum dma_data_direction direction)
  197. {
  198. BUG_ON(direction == DMA_NONE);
  199. if (cpu_is_noncoherent_r10000(dev)) {
  200. unsigned long addr;
  201. addr = PAGE_OFFSET + plat_dma_addr_to_phys(dma_handle);
  202. __dma_sync(addr + offset, size, direction);
  203. }
  204. }
  205. EXPORT_SYMBOL(dma_sync_single_range_for_cpu);
  206. void dma_sync_single_range_for_device(struct device *dev, dma_addr_t dma_handle,
  207. unsigned long offset, size_t size, enum dma_data_direction direction)
  208. {
  209. BUG_ON(direction == DMA_NONE);
  210. if (!plat_device_is_coherent(dev)) {
  211. unsigned long addr;
  212. addr = PAGE_OFFSET + plat_dma_addr_to_phys(dma_handle);
  213. __dma_sync(addr + offset, size, direction);
  214. }
  215. }
  216. EXPORT_SYMBOL(dma_sync_single_range_for_device);
  217. void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems,
  218. enum dma_data_direction direction)
  219. {
  220. int i;
  221. BUG_ON(direction == DMA_NONE);
  222. /* Make sure that gcc doesn't leave the empty loop body. */
  223. for (i = 0; i < nelems; i++, sg++) {
  224. if (cpu_is_noncoherent_r10000(dev))
  225. __dma_sync((unsigned long)page_address(sg->page),
  226. sg->length, direction);
  227. plat_unmap_dma_mem(sg->dma_address);
  228. }
  229. }
  230. EXPORT_SYMBOL(dma_sync_sg_for_cpu);
  231. void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems,
  232. enum dma_data_direction direction)
  233. {
  234. int i;
  235. BUG_ON(direction == DMA_NONE);
  236. /* Make sure that gcc doesn't leave the empty loop body. */
  237. for (i = 0; i < nelems; i++, sg++) {
  238. if (!plat_device_is_coherent(dev))
  239. __dma_sync((unsigned long)page_address(sg->page),
  240. sg->length, direction);
  241. plat_unmap_dma_mem(sg->dma_address);
  242. }
  243. }
  244. EXPORT_SYMBOL(dma_sync_sg_for_device);
  245. int dma_mapping_error(dma_addr_t dma_addr)
  246. {
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(dma_mapping_error);
  250. int dma_supported(struct device *dev, u64 mask)
  251. {
  252. /*
  253. * we fall back to GFP_DMA when the mask isn't all 1s,
  254. * so we can't guarantee allocations that must be
  255. * within a tighter range than GFP_DMA..
  256. */
  257. if (mask < 0x00ffffff)
  258. return 0;
  259. return 1;
  260. }
  261. EXPORT_SYMBOL(dma_supported);
  262. int dma_is_consistent(struct device *dev, dma_addr_t dma_addr)
  263. {
  264. return plat_device_is_coherent(dev);
  265. }
  266. EXPORT_SYMBOL(dma_is_consistent);
  267. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  268. enum dma_data_direction direction)
  269. {
  270. BUG_ON(direction == DMA_NONE);
  271. if (!plat_device_is_coherent(dev))
  272. dma_cache_wback_inv((unsigned long)vaddr, size);
  273. }
  274. EXPORT_SYMBOL(dma_cache_sync);