dma-default.c 8.8 KB

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