dma-default.c 11 KB

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