dma-default.c 9.9 KB

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