dma-default.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. * Note that the R14000 and R16000 should also be checked for in this
  58. * condition. However this function is only called on non-I/O-coherent
  59. * systems and only the R10000 and R12000 are used in such systems, the
  60. * SGI IP28 Indigo² rsp. SGI IP32 aka O2.
  61. */
  62. static inline int cpu_needs_post_dma_flush(struct device *dev)
  63. {
  64. return !plat_device_is_coherent(dev) &&
  65. (boot_cpu_type() == CPU_R10000 ||
  66. boot_cpu_type() == CPU_R12000 ||
  67. boot_cpu_type() == CPU_BMIPS5000);
  68. }
  69. static gfp_t massage_gfp_flags(const struct device *dev, gfp_t gfp)
  70. {
  71. gfp_t dma_flag;
  72. /* ignore region specifiers */
  73. gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
  74. #ifdef CONFIG_ISA
  75. if (dev == NULL)
  76. dma_flag = __GFP_DMA;
  77. else
  78. #endif
  79. #if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA)
  80. if (dev->coherent_dma_mask < DMA_BIT_MASK(32))
  81. dma_flag = __GFP_DMA;
  82. else if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  83. dma_flag = __GFP_DMA32;
  84. else
  85. #endif
  86. #if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA)
  87. if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  88. dma_flag = __GFP_DMA32;
  89. else
  90. #endif
  91. #if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32)
  92. if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  93. dma_flag = __GFP_DMA;
  94. else
  95. #endif
  96. dma_flag = 0;
  97. /* Don't invoke OOM killer */
  98. gfp |= __GFP_NORETRY;
  99. return gfp | dma_flag;
  100. }
  101. void *dma_alloc_noncoherent(struct device *dev, size_t size,
  102. dma_addr_t * dma_handle, gfp_t gfp)
  103. {
  104. void *ret;
  105. gfp = massage_gfp_flags(dev, gfp);
  106. ret = (void *) __get_free_pages(gfp, get_order(size));
  107. if (ret != NULL) {
  108. memset(ret, 0, size);
  109. *dma_handle = plat_map_dma_mem(dev, ret, size);
  110. }
  111. return ret;
  112. }
  113. EXPORT_SYMBOL(dma_alloc_noncoherent);
  114. static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
  115. dma_addr_t * dma_handle, gfp_t gfp, struct dma_attrs *attrs)
  116. {
  117. void *ret;
  118. struct page *page = NULL;
  119. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  120. if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))
  121. return ret;
  122. gfp = massage_gfp_flags(dev, gfp);
  123. if (IS_ENABLED(CONFIG_DMA_CMA) && !(gfp & GFP_ATOMIC))
  124. page = dma_alloc_from_contiguous(dev,
  125. count, get_order(size));
  126. if (!page)
  127. page = alloc_pages(gfp, get_order(size));
  128. if (!page)
  129. return NULL;
  130. ret = page_address(page);
  131. memset(ret, 0, size);
  132. *dma_handle = plat_map_dma_mem(dev, ret, size);
  133. if (!plat_device_is_coherent(dev)) {
  134. dma_cache_wback_inv((unsigned long) ret, size);
  135. if (!hw_coherentio)
  136. ret = UNCAC_ADDR(ret);
  137. }
  138. return ret;
  139. }
  140. void dma_free_noncoherent(struct device *dev, size_t size, void *vaddr,
  141. dma_addr_t dma_handle)
  142. {
  143. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  144. free_pages((unsigned long) vaddr, get_order(size));
  145. }
  146. EXPORT_SYMBOL(dma_free_noncoherent);
  147. static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  148. dma_addr_t dma_handle, struct dma_attrs *attrs)
  149. {
  150. unsigned long addr = (unsigned long) vaddr;
  151. int order = get_order(size);
  152. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  153. struct page *page = NULL;
  154. if (dma_release_from_coherent(dev, order, vaddr))
  155. return;
  156. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  157. if (!plat_device_is_coherent(dev) && !hw_coherentio)
  158. addr = CAC_ADDR(addr);
  159. page = virt_to_page((void *) addr);
  160. if (!dma_release_from_contiguous(dev, page, count))
  161. __free_pages(page, get_order(size));
  162. }
  163. static inline void __dma_sync_virtual(void *addr, size_t size,
  164. enum dma_data_direction direction)
  165. {
  166. switch (direction) {
  167. case DMA_TO_DEVICE:
  168. dma_cache_wback((unsigned long)addr, size);
  169. break;
  170. case DMA_FROM_DEVICE:
  171. dma_cache_inv((unsigned long)addr, size);
  172. break;
  173. case DMA_BIDIRECTIONAL:
  174. dma_cache_wback_inv((unsigned long)addr, size);
  175. break;
  176. default:
  177. BUG();
  178. }
  179. }
  180. /*
  181. * A single sg entry may refer to multiple physically contiguous
  182. * pages. But we still need to process highmem pages individually.
  183. * If highmem is not configured then the bulk of this loop gets
  184. * optimized out.
  185. */
  186. static inline void __dma_sync(struct page *page,
  187. unsigned long offset, size_t size, enum dma_data_direction direction)
  188. {
  189. size_t left = size;
  190. do {
  191. size_t len = left;
  192. if (PageHighMem(page)) {
  193. void *addr;
  194. if (offset + len > PAGE_SIZE) {
  195. if (offset >= PAGE_SIZE) {
  196. page += offset >> PAGE_SHIFT;
  197. offset &= ~PAGE_MASK;
  198. }
  199. len = PAGE_SIZE - offset;
  200. }
  201. addr = kmap_atomic(page);
  202. __dma_sync_virtual(addr + offset, len, direction);
  203. kunmap_atomic(addr);
  204. } else
  205. __dma_sync_virtual(page_address(page) + offset,
  206. size, direction);
  207. offset = 0;
  208. page++;
  209. left -= len;
  210. } while (left);
  211. }
  212. static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  213. size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
  214. {
  215. if (cpu_needs_post_dma_flush(dev))
  216. __dma_sync(dma_addr_to_page(dev, dma_addr),
  217. dma_addr & ~PAGE_MASK, size, direction);
  218. plat_post_dma_flush(dev);
  219. plat_unmap_dma_mem(dev, dma_addr, size, direction);
  220. }
  221. static int mips_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  222. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  223. {
  224. int i;
  225. struct scatterlist *sg;
  226. for_each_sg(sglist, sg, nents, i) {
  227. if (!plat_device_is_coherent(dev))
  228. __dma_sync(sg_page(sg), sg->offset, sg->length,
  229. direction);
  230. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  231. sg->dma_length = sg->length;
  232. #endif
  233. sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
  234. sg->offset;
  235. }
  236. return nents;
  237. }
  238. static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
  239. unsigned long offset, size_t size, enum dma_data_direction direction,
  240. struct dma_attrs *attrs)
  241. {
  242. if (!plat_device_is_coherent(dev))
  243. __dma_sync(page, offset, size, direction);
  244. return plat_map_dma_mem_page(dev, page) + offset;
  245. }
  246. static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  247. int nhwentries, enum dma_data_direction direction,
  248. struct dma_attrs *attrs)
  249. {
  250. int i;
  251. struct scatterlist *sg;
  252. for_each_sg(sglist, sg, nhwentries, i) {
  253. if (!plat_device_is_coherent(dev) &&
  254. direction != DMA_TO_DEVICE)
  255. __dma_sync(sg_page(sg), sg->offset, sg->length,
  256. direction);
  257. plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
  258. }
  259. }
  260. static void mips_dma_sync_single_for_cpu(struct device *dev,
  261. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  262. {
  263. if (cpu_needs_post_dma_flush(dev))
  264. __dma_sync(dma_addr_to_page(dev, dma_handle),
  265. dma_handle & ~PAGE_MASK, size, direction);
  266. plat_post_dma_flush(dev);
  267. }
  268. static void mips_dma_sync_single_for_device(struct device *dev,
  269. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  270. {
  271. if (!plat_device_is_coherent(dev))
  272. __dma_sync(dma_addr_to_page(dev, dma_handle),
  273. dma_handle & ~PAGE_MASK, size, direction);
  274. }
  275. static void mips_dma_sync_sg_for_cpu(struct device *dev,
  276. struct scatterlist *sglist, int nelems,
  277. enum dma_data_direction direction)
  278. {
  279. int i;
  280. struct scatterlist *sg;
  281. if (cpu_needs_post_dma_flush(dev)) {
  282. for_each_sg(sglist, sg, nelems, i) {
  283. __dma_sync(sg_page(sg), sg->offset, sg->length,
  284. direction);
  285. }
  286. }
  287. plat_post_dma_flush(dev);
  288. }
  289. static void mips_dma_sync_sg_for_device(struct device *dev,
  290. struct scatterlist *sglist, int nelems,
  291. enum dma_data_direction direction)
  292. {
  293. int i;
  294. struct scatterlist *sg;
  295. if (!plat_device_is_coherent(dev)) {
  296. for_each_sg(sglist, sg, nelems, i) {
  297. __dma_sync(sg_page(sg), sg->offset, sg->length,
  298. direction);
  299. }
  300. }
  301. }
  302. int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  303. {
  304. return 0;
  305. }
  306. int mips_dma_supported(struct device *dev, u64 mask)
  307. {
  308. return plat_dma_supported(dev, mask);
  309. }
  310. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  311. enum dma_data_direction direction)
  312. {
  313. BUG_ON(direction == DMA_NONE);
  314. if (!plat_device_is_coherent(dev))
  315. __dma_sync_virtual(vaddr, size, direction);
  316. }
  317. EXPORT_SYMBOL(dma_cache_sync);
  318. static struct dma_map_ops mips_default_dma_map_ops = {
  319. .alloc = mips_dma_alloc_coherent,
  320. .free = mips_dma_free_coherent,
  321. .map_page = mips_dma_map_page,
  322. .unmap_page = mips_dma_unmap_page,
  323. .map_sg = mips_dma_map_sg,
  324. .unmap_sg = mips_dma_unmap_sg,
  325. .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
  326. .sync_single_for_device = mips_dma_sync_single_for_device,
  327. .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
  328. .sync_sg_for_device = mips_dma_sync_sg_for_device,
  329. .mapping_error = mips_dma_mapping_error,
  330. .dma_supported = mips_dma_supported
  331. };
  332. struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
  333. EXPORT_SYMBOL(mips_dma_map_ops);
  334. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  335. static int __init mips_dma_init(void)
  336. {
  337. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  338. return 0;
  339. }
  340. fs_initcall(mips_dma_init);