dma-default.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #ifdef CONFIG_ISA
  85. if (dev == NULL)
  86. dma_flag = __GFP_DMA;
  87. else
  88. #endif
  89. #if defined(CONFIG_ZONE_DMA32) && defined(CONFIG_ZONE_DMA)
  90. if (dev == NULL || dev->coherent_dma_mask < DMA_BIT_MASK(32))
  91. dma_flag = __GFP_DMA;
  92. else if (dev->coherent_dma_mask < DMA_BIT_MASK(64))
  93. dma_flag = __GFP_DMA32;
  94. else
  95. #endif
  96. #if defined(CONFIG_ZONE_DMA32) && !defined(CONFIG_ZONE_DMA)
  97. if (dev == NULL || dev->coherent_dma_mask < DMA_BIT_MASK(64))
  98. dma_flag = __GFP_DMA32;
  99. else
  100. #endif
  101. #if defined(CONFIG_ZONE_DMA) && !defined(CONFIG_ZONE_DMA32)
  102. if (dev == NULL ||
  103. dev->coherent_dma_mask < DMA_BIT_MASK(sizeof(phys_addr_t) * 8))
  104. dma_flag = __GFP_DMA;
  105. else
  106. #endif
  107. dma_flag = 0;
  108. /* Don't invoke OOM killer */
  109. gfp |= __GFP_NORETRY;
  110. return gfp | dma_flag;
  111. }
  112. static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
  113. dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
  114. {
  115. void *ret;
  116. struct page *page = NULL;
  117. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  118. gfp = massage_gfp_flags(dev, gfp);
  119. if (IS_ENABLED(CONFIG_DMA_CMA) && gfpflags_allow_blocking(gfp))
  120. page = dma_alloc_from_contiguous(dev, count, get_order(size),
  121. gfp);
  122. if (!page)
  123. page = alloc_pages(gfp, get_order(size));
  124. if (!page)
  125. return NULL;
  126. ret = page_address(page);
  127. memset(ret, 0, size);
  128. *dma_handle = plat_map_dma_mem(dev, ret, size);
  129. if (!(attrs & DMA_ATTR_NON_CONSISTENT) &&
  130. !plat_device_is_coherent(dev)) {
  131. dma_cache_wback_inv((unsigned long) ret, size);
  132. ret = UNCAC_ADDR(ret);
  133. }
  134. return ret;
  135. }
  136. static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  137. dma_addr_t dma_handle, unsigned long attrs)
  138. {
  139. unsigned long addr = (unsigned long) vaddr;
  140. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  141. struct page *page = NULL;
  142. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  143. if (!(attrs & DMA_ATTR_NON_CONSISTENT) && !plat_device_is_coherent(dev))
  144. addr = CAC_ADDR(addr);
  145. page = virt_to_page((void *) addr);
  146. if (!dma_release_from_contiguous(dev, page, count))
  147. __free_pages(page, get_order(size));
  148. }
  149. static int mips_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  150. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  151. unsigned long attrs)
  152. {
  153. unsigned long user_count = vma_pages(vma);
  154. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  155. unsigned long addr = (unsigned long)cpu_addr;
  156. unsigned long off = vma->vm_pgoff;
  157. unsigned long pfn;
  158. int ret = -ENXIO;
  159. if (!plat_device_is_coherent(dev))
  160. addr = CAC_ADDR(addr);
  161. pfn = page_to_pfn(virt_to_page((void *)addr));
  162. if (attrs & DMA_ATTR_WRITE_COMBINE)
  163. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  164. else
  165. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  166. if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
  167. return ret;
  168. if (off < count && user_count <= (count - off)) {
  169. ret = remap_pfn_range(vma, vma->vm_start,
  170. pfn + off,
  171. user_count << PAGE_SHIFT,
  172. vma->vm_page_prot);
  173. }
  174. return ret;
  175. }
  176. static inline void __dma_sync_virtual(void *addr, size_t size,
  177. enum dma_data_direction direction)
  178. {
  179. switch (direction) {
  180. case DMA_TO_DEVICE:
  181. dma_cache_wback((unsigned long)addr, size);
  182. break;
  183. case DMA_FROM_DEVICE:
  184. dma_cache_inv((unsigned long)addr, size);
  185. break;
  186. case DMA_BIDIRECTIONAL:
  187. dma_cache_wback_inv((unsigned long)addr, size);
  188. break;
  189. default:
  190. BUG();
  191. }
  192. }
  193. /*
  194. * A single sg entry may refer to multiple physically contiguous
  195. * pages. But we still need to process highmem pages individually.
  196. * If highmem is not configured then the bulk of this loop gets
  197. * optimized out.
  198. */
  199. static inline void __dma_sync(struct page *page,
  200. unsigned long offset, size_t size, enum dma_data_direction direction)
  201. {
  202. size_t left = size;
  203. do {
  204. size_t len = left;
  205. if (PageHighMem(page)) {
  206. void *addr;
  207. if (offset + len > PAGE_SIZE) {
  208. if (offset >= PAGE_SIZE) {
  209. page += offset >> PAGE_SHIFT;
  210. offset &= ~PAGE_MASK;
  211. }
  212. len = PAGE_SIZE - offset;
  213. }
  214. addr = kmap_atomic(page);
  215. __dma_sync_virtual(addr + offset, len, direction);
  216. kunmap_atomic(addr);
  217. } else
  218. __dma_sync_virtual(page_address(page) + offset,
  219. size, direction);
  220. offset = 0;
  221. page++;
  222. left -= len;
  223. } while (left);
  224. }
  225. static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  226. size_t size, enum dma_data_direction direction, unsigned long attrs)
  227. {
  228. if (cpu_needs_post_dma_flush(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  229. __dma_sync(dma_addr_to_page(dev, dma_addr),
  230. dma_addr & ~PAGE_MASK, size, direction);
  231. plat_post_dma_flush(dev);
  232. plat_unmap_dma_mem(dev, dma_addr, size, direction);
  233. }
  234. static int mips_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  235. int nents, enum dma_data_direction direction, unsigned long attrs)
  236. {
  237. int i;
  238. struct scatterlist *sg;
  239. for_each_sg(sglist, sg, nents, i) {
  240. if (!plat_device_is_coherent(dev) &&
  241. !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  242. __dma_sync(sg_page(sg), sg->offset, sg->length,
  243. direction);
  244. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  245. sg->dma_length = sg->length;
  246. #endif
  247. sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
  248. sg->offset;
  249. }
  250. return nents;
  251. }
  252. static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
  253. unsigned long offset, size_t size, enum dma_data_direction direction,
  254. unsigned long attrs)
  255. {
  256. if (!plat_device_is_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
  257. __dma_sync(page, offset, size, direction);
  258. return plat_map_dma_mem_page(dev, page) + offset;
  259. }
  260. static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  261. int nhwentries, enum dma_data_direction direction,
  262. unsigned long attrs)
  263. {
  264. int i;
  265. struct scatterlist *sg;
  266. for_each_sg(sglist, sg, nhwentries, i) {
  267. if (!plat_device_is_coherent(dev) &&
  268. !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
  269. direction != DMA_TO_DEVICE)
  270. __dma_sync(sg_page(sg), sg->offset, sg->length,
  271. direction);
  272. plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
  273. }
  274. }
  275. static void mips_dma_sync_single_for_cpu(struct device *dev,
  276. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  277. {
  278. if (cpu_needs_post_dma_flush(dev))
  279. __dma_sync(dma_addr_to_page(dev, dma_handle),
  280. dma_handle & ~PAGE_MASK, size, direction);
  281. plat_post_dma_flush(dev);
  282. }
  283. static void mips_dma_sync_single_for_device(struct device *dev,
  284. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  285. {
  286. if (!plat_device_is_coherent(dev))
  287. __dma_sync(dma_addr_to_page(dev, dma_handle),
  288. dma_handle & ~PAGE_MASK, size, direction);
  289. }
  290. static void mips_dma_sync_sg_for_cpu(struct device *dev,
  291. struct scatterlist *sglist, int nelems,
  292. enum dma_data_direction direction)
  293. {
  294. int i;
  295. struct scatterlist *sg;
  296. if (cpu_needs_post_dma_flush(dev)) {
  297. for_each_sg(sglist, sg, nelems, i) {
  298. __dma_sync(sg_page(sg), sg->offset, sg->length,
  299. direction);
  300. }
  301. }
  302. plat_post_dma_flush(dev);
  303. }
  304. static void mips_dma_sync_sg_for_device(struct device *dev,
  305. struct scatterlist *sglist, int nelems,
  306. enum dma_data_direction direction)
  307. {
  308. int i;
  309. struct scatterlist *sg;
  310. if (!plat_device_is_coherent(dev)) {
  311. for_each_sg(sglist, sg, nelems, i) {
  312. __dma_sync(sg_page(sg), sg->offset, sg->length,
  313. direction);
  314. }
  315. }
  316. }
  317. static int mips_dma_supported(struct device *dev, u64 mask)
  318. {
  319. return plat_dma_supported(dev, mask);
  320. }
  321. static void mips_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  322. enum dma_data_direction direction)
  323. {
  324. BUG_ON(direction == DMA_NONE);
  325. if (!plat_device_is_coherent(dev))
  326. __dma_sync_virtual(vaddr, size, direction);
  327. }
  328. static const struct dma_map_ops mips_default_dma_map_ops = {
  329. .alloc = mips_dma_alloc_coherent,
  330. .free = mips_dma_free_coherent,
  331. .mmap = mips_dma_mmap,
  332. .map_page = mips_dma_map_page,
  333. .unmap_page = mips_dma_unmap_page,
  334. .map_sg = mips_dma_map_sg,
  335. .unmap_sg = mips_dma_unmap_sg,
  336. .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
  337. .sync_single_for_device = mips_dma_sync_single_for_device,
  338. .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
  339. .sync_sg_for_device = mips_dma_sync_sg_for_device,
  340. .dma_supported = mips_dma_supported,
  341. .cache_sync = mips_dma_cache_sync,
  342. };
  343. const struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
  344. EXPORT_SYMBOL(mips_dma_map_ops);