dma-default.c 8.4 KB

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