dma-default.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <linux/highmem.h>
  18. #include <asm/cache.h>
  19. #include <asm/io.h>
  20. #include <dma-coherence.h>
  21. int coherentio = 0; /* User defined DMA coherency from command line. */
  22. EXPORT_SYMBOL_GPL(coherentio);
  23. int hw_coherentio = 0; /* Actual hardware supported DMA coherency setting. */
  24. static int __init setcoherentio(char *str)
  25. {
  26. coherentio = 1;
  27. pr_info("Hardware DMA cache coherency (command line)\n");
  28. return 0;
  29. }
  30. early_param("coherentio", setcoherentio);
  31. static int __init setnocoherentio(char *str)
  32. {
  33. coherentio = 0;
  34. pr_info("Software DMA cache coherency (command line)\n");
  35. return 0;
  36. }
  37. early_param("nocoherentio", setnocoherentio);
  38. static inline struct page *dma_addr_to_page(struct device *dev,
  39. dma_addr_t dma_addr)
  40. {
  41. return pfn_to_page(
  42. plat_dma_addr_to_phys(dev, dma_addr) >> PAGE_SHIFT);
  43. }
  44. /*
  45. * The affected CPUs below in 'cpu_needs_post_dma_flush()' can
  46. * speculatively fill random cachelines with stale data at any time,
  47. * requiring an extra flush post-DMA.
  48. *
  49. * Warning on the terminology - Linux calls an uncached area coherent;
  50. * MIPS terminology calls memory areas with hardware maintained coherency
  51. * coherent.
  52. */
  53. static inline int cpu_needs_post_dma_flush(struct device *dev)
  54. {
  55. return !plat_device_is_coherent(dev) &&
  56. (boot_cpu_type() == CPU_R10000 ||
  57. boot_cpu_type() == CPU_R12000 ||
  58. boot_cpu_type() == CPU_BMIPS5000);
  59. }
  60. static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
  61. {
  62. gfp_t dma_flag;
  63. /* ignore region specifiers */
  64. gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  65. #ifdef CONFIG_ISA
  66. if (dev == NULL)
  67. dma_flag = __GFP_DMA;
  68. else
  69. #endif
  70. #if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA)
  71. if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
  72. dma_flag = __GFP_DMA;
  73. else if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  74. dma_flag = __GFP_DMA32;
  75. else
  76. #endif
  77. #if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA)
  78. if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  79. dma_flag = __GFP_DMA32;
  80. else
  81. #endif
  82. #if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32)
  83. if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  84. dma_flag = __GFP_DMA;
  85. else
  86. #endif
  87. dma_flag = 0;
  88. /* Don't invoke OOM killer */
  89. gfp |= __GFP_NORETRY;
  90. return gfp | dma_flag;
  91. }
  92. void *dma_alloc_noncoherent(struct device *dev, size_t size,
  93. dma_addr_t * dma_handle, gfp_t gfp)
  94. {
  95. void *ret;
  96. gfp = massage_gfp_flags(dev, gfp);
  97. ret = (void *) __get_free_pages(gfp, get_order(size));
  98. if (ret != NULL) {
  99. memset(ret, 0, size);
  100. *dma_handle = plat_map_dma_mem(dev, ret, size);
  101. }
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(dma_alloc_noncoherent);
  105. static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
  106. dma_addr_t * dma_handle, gfp_t gfp, struct dma_attrs *attrs)
  107. {
  108. void *ret;
  109. if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
  110. return ret;
  111. gfp = massage_gfp_flags(dev, gfp);
  112. ret = (void *) __get_free_pages(gfp, get_order(size));
  113. if (ret) {
  114. memset(ret, 0, size);
  115. *dma_handle = plat_map_dma_mem(dev, ret, size);
  116. if (!plat_device_is_coherent(dev)) {
  117. dma_cache_wback_inv((unsigned long) ret, size);
  118. if (!hw_coherentio)
  119. ret = UNCAC_ADDR(ret);
  120. }
  121. }
  122. return ret;
  123. }
  124. void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  125. dma_addr_t dma_handle)
  126. {
  127. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  128. free_pages((unsigned long) vaddr, get_order(size));
  129. }
  130. EXPORT_SYMBOL(dma_free_noncoherent);
  131. static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  132. dma_addr_t dma_handle, struct dma_attrs *attrs)
  133. {
  134. unsigned long addr = (unsigned long) vaddr;
  135. int order = get_order(size);
  136. if (dma_release_from_coherent(dev, order, vaddr))
  137. return;
  138. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  139. if (!plat_device_is_coherent(dev) && !hw_coherentio)
  140. addr = CAC_ADDR(addr);
  141. free_pages(addr, get_order(size));
  142. }
  143. static inline void __dma_sync_virtual(void *addr, size_t size,
  144. enum dma_data_direction direction)
  145. {
  146. switch (direction) {
  147. case DMA_TO_DEVICE:
  148. dma_cache_wback((unsigned long)addr, size);
  149. break;
  150. case DMA_FROM_DEVICE:
  151. dma_cache_inv((unsigned long)addr, size);
  152. break;
  153. case DMA_BIDIRECTIONAL:
  154. dma_cache_wback_inv((unsigned long)addr, size);
  155. break;
  156. default:
  157. BUG();
  158. }
  159. }
  160. /*
  161. * A single sg entry may refer to multiple physically contiguous
  162. * pages. But we still need to process highmem pages individually.
  163. * If highmem is not configured then the bulk of this loop gets
  164. * optimized out.
  165. */
  166. static inline void __dma_sync(struct page *page,
  167. unsigned long offset, size_t size, enum dma_data_direction direction)
  168. {
  169. size_t left = size;
  170. do {
  171. size_t len = left;
  172. if (PageHighMem(page)) {
  173. void *addr;
  174. if (offset + len > PAGE_SIZE) {
  175. if (offset >= PAGE_SIZE) {
  176. page += offset >> PAGE_SHIFT;
  177. offset &= ~PAGE_MASK;
  178. }
  179. len = PAGE_SIZE - offset;
  180. }
  181. addr = kmap_atomic(page);
  182. __dma_sync_virtual(addr + offset, len, direction);
  183. kunmap_atomic(addr);
  184. } else
  185. __dma_sync_virtual(page_address(page) + offset,
  186. size, direction);
  187. offset = 0;
  188. page++;
  189. left -= len;
  190. } while (left);
  191. }
  192. static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  193. size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
  194. {
  195. if (cpu_needs_post_dma_flush(dev))
  196. __dma_sync(dma_addr_to_page(dev, dma_addr),
  197. dma_addr & ~PAGE_MASK, size, direction);
  198. plat_unmap_dma_mem(dev, dma_addr, size, direction);
  199. }
  200. static int mips_dma_map_sg(struct device *dev, struct scatterlist *sg,
  201. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  202. {
  203. int i;
  204. for (i = 0; i < nents; i++, sg++) {
  205. if (!plat_device_is_coherent(dev))
  206. __dma_sync(sg_page(sg), sg->offset, sg->length,
  207. direction);
  208. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  209. sg->dma_length = sg->length;
  210. #endif
  211. sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
  212. sg->offset;
  213. }
  214. return nents;
  215. }
  216. static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
  217. unsigned long offset, size_t size, enum dma_data_direction direction,
  218. struct dma_attrs *attrs)
  219. {
  220. if (!plat_device_is_coherent(dev))
  221. __dma_sync(page, offset, size, direction);
  222. return plat_map_dma_mem_page(dev, page) + offset;
  223. }
  224. static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
  225. int nhwentries, enum dma_data_direction direction,
  226. struct dma_attrs *attrs)
  227. {
  228. int i;
  229. for (i = 0; i < nhwentries; i++, sg++) {
  230. if (!plat_device_is_coherent(dev) &&
  231. direction != DMA_TO_DEVICE)
  232. __dma_sync(sg_page(sg), sg->offset, sg->length,
  233. direction);
  234. plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
  235. }
  236. }
  237. static void mips_dma_sync_single_for_cpu(struct device *dev,
  238. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  239. {
  240. if (cpu_needs_post_dma_flush(dev))
  241. __dma_sync(dma_addr_to_page(dev, dma_handle),
  242. dma_handle & ~PAGE_MASK, size, direction);
  243. }
  244. static void mips_dma_sync_single_for_device(struct device *dev,
  245. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  246. {
  247. plat_extra_sync_for_device(dev);
  248. if (!plat_device_is_coherent(dev))
  249. __dma_sync(dma_addr_to_page(dev, dma_handle),
  250. dma_handle & ~PAGE_MASK, size, direction);
  251. }
  252. static void mips_dma_sync_sg_for_cpu(struct device *dev,
  253. struct scatterlist *sg, int nelems, enum dma_data_direction direction)
  254. {
  255. int i;
  256. /* Make sure that gcc doesn't leave the empty loop body. */
  257. for (i = 0; i < nelems; i++, sg++) {
  258. if (cpu_needs_post_dma_flush(dev))
  259. __dma_sync(sg_page(sg), sg->offset, sg->length,
  260. direction);
  261. }
  262. }
  263. static void mips_dma_sync_sg_for_device(struct device *dev,
  264. struct scatterlist *sg, int nelems, enum dma_data_direction direction)
  265. {
  266. int i;
  267. /* Make sure that gcc doesn't leave the empty loop body. */
  268. for (i = 0; i < nelems; i++, sg++) {
  269. if (!plat_device_is_coherent(dev))
  270. __dma_sync(sg_page(sg), sg->offset, sg->length,
  271. direction);
  272. }
  273. }
  274. int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  275. {
  276. return plat_dma_mapping_error(dev, dma_addr);
  277. }
  278. int mips_dma_supported(struct device *dev, u64 mask)
  279. {
  280. return plat_dma_supported(dev, mask);
  281. }
  282. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  283. enum dma_data_direction direction)
  284. {
  285. BUG_ON(direction == DMA_NONE);
  286. plat_extra_sync_for_device(dev);
  287. if (!plat_device_is_coherent(dev))
  288. __dma_sync_virtual(vaddr, size, direction);
  289. }
  290. EXPORT_SYMBOL(dma_cache_sync);
  291. static struct dma_map_ops mips_default_dma_map_ops = {
  292. .alloc = mips_dma_alloc_coherent,
  293. .free = mips_dma_free_coherent,
  294. .map_page = mips_dma_map_page,
  295. .unmap_page = mips_dma_unmap_page,
  296. .map_sg = mips_dma_map_sg,
  297. .unmap_sg = mips_dma_unmap_sg,
  298. .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
  299. .sync_single_for_device = mips_dma_sync_single_for_device,
  300. .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
  301. .sync_sg_for_device = mips_dma_sync_sg_for_device,
  302. .mapping_error = mips_dma_mapping_error,
  303. .dma_supported = mips_dma_supported
  304. };
  305. struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
  306. EXPORT_SYMBOL(mips_dma_map_ops);
  307. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  308. static int __init mips_dma_init(void)
  309. {
  310. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  311. return 0;
  312. }
  313. fs_initcall(mips_dma_init);