dma-default.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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(sizeof(phys_addr_t) * 8))
  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. static void *mips_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. static void *mips_dma_alloc_coherent(struct device *dev, size_t size,
  114. dma_addr_t * dma_handle, gfp_t gfp, struct dma_attrs *attrs)
  115. {
  116. void *ret;
  117. struct page *page = NULL;
  118. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  119. /*
  120. * XXX: seems like the coherent and non-coherent implementations could
  121. * be consolidated.
  122. */
  123. if (dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs))
  124. return mips_dma_alloc_noncoherent(dev, size, dma_handle, gfp);
  125. gfp = massage_gfp_flags(dev, gfp);
  126. if (IS_ENABLED(CONFIG_DMA_CMA) && gfpflags_allow_blocking(gfp))
  127. page = dma_alloc_from_contiguous(dev,
  128. count, get_order(size));
  129. if (!page)
  130. page = alloc_pages(gfp, get_order(size));
  131. if (!page)
  132. return NULL;
  133. ret = page_address(page);
  134. memset(ret, 0, size);
  135. *dma_handle = plat_map_dma_mem(dev, ret, size);
  136. if (!plat_device_is_coherent(dev)) {
  137. dma_cache_wback_inv((unsigned long) ret, size);
  138. if (!hw_coherentio)
  139. ret = UNCAC_ADDR(ret);
  140. }
  141. return ret;
  142. }
  143. static void mips_dma_free_noncoherent(struct device *dev, size_t size,
  144. void *vaddr, dma_addr_t dma_handle)
  145. {
  146. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  147. free_pages((unsigned long) vaddr, get_order(size));
  148. }
  149. static void mips_dma_free_coherent(struct device *dev, size_t size, void *vaddr,
  150. dma_addr_t dma_handle, struct dma_attrs *attrs)
  151. {
  152. unsigned long addr = (unsigned long) vaddr;
  153. unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  154. struct page *page = NULL;
  155. if (dma_get_attr(DMA_ATTR_NON_CONSISTENT, attrs)) {
  156. mips_dma_free_noncoherent(dev, size, vaddr, dma_handle);
  157. return;
  158. }
  159. plat_unmap_dma_mem(dev, dma_handle, size, DMA_BIDIRECTIONAL);
  160. if (!plat_device_is_coherent(dev) && !hw_coherentio)
  161. addr = CAC_ADDR(addr);
  162. page = virt_to_page((void *) addr);
  163. if (!dma_release_from_contiguous(dev, page, count))
  164. __free_pages(page, get_order(size));
  165. }
  166. static int mips_dma_mmap(struct device *dev, struct vm_area_struct *vma,
  167. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  168. struct dma_attrs *attrs)
  169. {
  170. unsigned long user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  171. unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  172. unsigned long addr = (unsigned long)cpu_addr;
  173. unsigned long off = vma->vm_pgoff;
  174. unsigned long pfn;
  175. int ret = -ENXIO;
  176. if (!plat_device_is_coherent(dev) && !hw_coherentio)
  177. addr = CAC_ADDR(addr);
  178. pfn = page_to_pfn(virt_to_page((void *)addr));
  179. if (dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs))
  180. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  181. else
  182. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  183. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  184. return ret;
  185. if (off < count && user_count <= (count - off)) {
  186. ret = remap_pfn_range(vma, vma->vm_start,
  187. pfn + off,
  188. user_count << PAGE_SHIFT,
  189. vma->vm_page_prot);
  190. }
  191. return ret;
  192. }
  193. static inline void __dma_sync_virtual(void *addr, size_t size,
  194. enum dma_data_direction direction)
  195. {
  196. switch (direction) {
  197. case DMA_TO_DEVICE:
  198. dma_cache_wback((unsigned long)addr, size);
  199. break;
  200. case DMA_FROM_DEVICE:
  201. dma_cache_inv((unsigned long)addr, size);
  202. break;
  203. case DMA_BIDIRECTIONAL:
  204. dma_cache_wback_inv((unsigned long)addr, size);
  205. break;
  206. default:
  207. BUG();
  208. }
  209. }
  210. /*
  211. * A single sg entry may refer to multiple physically contiguous
  212. * pages. But we still need to process highmem pages individually.
  213. * If highmem is not configured then the bulk of this loop gets
  214. * optimized out.
  215. */
  216. static inline void __dma_sync(struct page *page,
  217. unsigned long offset, size_t size, enum dma_data_direction direction)
  218. {
  219. size_t left = size;
  220. do {
  221. size_t len = left;
  222. if (PageHighMem(page)) {
  223. void *addr;
  224. if (offset + len > PAGE_SIZE) {
  225. if (offset >= PAGE_SIZE) {
  226. page += offset >> PAGE_SHIFT;
  227. offset &= ~PAGE_MASK;
  228. }
  229. len = PAGE_SIZE - offset;
  230. }
  231. addr = kmap_atomic(page);
  232. __dma_sync_virtual(addr + offset, len, direction);
  233. kunmap_atomic(addr);
  234. } else
  235. __dma_sync_virtual(page_address(page) + offset,
  236. size, direction);
  237. offset = 0;
  238. page++;
  239. left -= len;
  240. } while (left);
  241. }
  242. static void mips_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  243. size_t size, enum dma_data_direction direction, struct dma_attrs *attrs)
  244. {
  245. if (cpu_needs_post_dma_flush(dev))
  246. __dma_sync(dma_addr_to_page(dev, dma_addr),
  247. dma_addr & ~PAGE_MASK, size, direction);
  248. plat_post_dma_flush(dev);
  249. plat_unmap_dma_mem(dev, dma_addr, size, direction);
  250. }
  251. static int mips_dma_map_sg(struct device *dev, struct scatterlist *sglist,
  252. int nents, enum dma_data_direction direction, struct dma_attrs *attrs)
  253. {
  254. int i;
  255. struct scatterlist *sg;
  256. for_each_sg(sglist, sg, nents, i) {
  257. if (!plat_device_is_coherent(dev))
  258. __dma_sync(sg_page(sg), sg->offset, sg->length,
  259. direction);
  260. #ifdef CONFIG_NEED_SG_DMA_LENGTH
  261. sg->dma_length = sg->length;
  262. #endif
  263. sg->dma_address = plat_map_dma_mem_page(dev, sg_page(sg)) +
  264. sg->offset;
  265. }
  266. return nents;
  267. }
  268. static dma_addr_t mips_dma_map_page(struct device *dev, struct page *page,
  269. unsigned long offset, size_t size, enum dma_data_direction direction,
  270. struct dma_attrs *attrs)
  271. {
  272. if (!plat_device_is_coherent(dev))
  273. __dma_sync(page, offset, size, direction);
  274. return plat_map_dma_mem_page(dev, page) + offset;
  275. }
  276. static void mips_dma_unmap_sg(struct device *dev, struct scatterlist *sglist,
  277. int nhwentries, enum dma_data_direction direction,
  278. struct dma_attrs *attrs)
  279. {
  280. int i;
  281. struct scatterlist *sg;
  282. for_each_sg(sglist, sg, nhwentries, i) {
  283. if (!plat_device_is_coherent(dev) &&
  284. direction != DMA_TO_DEVICE)
  285. __dma_sync(sg_page(sg), sg->offset, sg->length,
  286. direction);
  287. plat_unmap_dma_mem(dev, sg->dma_address, sg->length, direction);
  288. }
  289. }
  290. static void mips_dma_sync_single_for_cpu(struct device *dev,
  291. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  292. {
  293. if (cpu_needs_post_dma_flush(dev))
  294. __dma_sync(dma_addr_to_page(dev, dma_handle),
  295. dma_handle & ~PAGE_MASK, size, direction);
  296. plat_post_dma_flush(dev);
  297. }
  298. static void mips_dma_sync_single_for_device(struct device *dev,
  299. dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)
  300. {
  301. if (!plat_device_is_coherent(dev))
  302. __dma_sync(dma_addr_to_page(dev, dma_handle),
  303. dma_handle & ~PAGE_MASK, size, direction);
  304. }
  305. static void mips_dma_sync_sg_for_cpu(struct device *dev,
  306. struct scatterlist *sglist, int nelems,
  307. enum dma_data_direction direction)
  308. {
  309. int i;
  310. struct scatterlist *sg;
  311. if (cpu_needs_post_dma_flush(dev)) {
  312. for_each_sg(sglist, sg, nelems, i) {
  313. __dma_sync(sg_page(sg), sg->offset, sg->length,
  314. direction);
  315. }
  316. }
  317. plat_post_dma_flush(dev);
  318. }
  319. static void mips_dma_sync_sg_for_device(struct device *dev,
  320. struct scatterlist *sglist, int nelems,
  321. enum dma_data_direction direction)
  322. {
  323. int i;
  324. struct scatterlist *sg;
  325. if (!plat_device_is_coherent(dev)) {
  326. for_each_sg(sglist, sg, nelems, i) {
  327. __dma_sync(sg_page(sg), sg->offset, sg->length,
  328. direction);
  329. }
  330. }
  331. }
  332. int mips_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
  333. {
  334. return 0;
  335. }
  336. int mips_dma_supported(struct device *dev, u64 mask)
  337. {
  338. return plat_dma_supported(dev, mask);
  339. }
  340. void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
  341. enum dma_data_direction direction)
  342. {
  343. BUG_ON(direction == DMA_NONE);
  344. if (!plat_device_is_coherent(dev))
  345. __dma_sync_virtual(vaddr, size, direction);
  346. }
  347. EXPORT_SYMBOL(dma_cache_sync);
  348. static struct dma_map_ops mips_default_dma_map_ops = {
  349. .alloc = mips_dma_alloc_coherent,
  350. .free = mips_dma_free_coherent,
  351. .mmap = mips_dma_mmap,
  352. .map_page = mips_dma_map_page,
  353. .unmap_page = mips_dma_unmap_page,
  354. .map_sg = mips_dma_map_sg,
  355. .unmap_sg = mips_dma_unmap_sg,
  356. .sync_single_for_cpu = mips_dma_sync_single_for_cpu,
  357. .sync_single_for_device = mips_dma_sync_single_for_device,
  358. .sync_sg_for_cpu = mips_dma_sync_sg_for_cpu,
  359. .sync_sg_for_device = mips_dma_sync_sg_for_device,
  360. .mapping_error = mips_dma_mapping_error,
  361. .dma_supported = mips_dma_supported
  362. };
  363. struct dma_map_ops *mips_dma_map_ops = &mips_default_dma_map_ops;
  364. EXPORT_SYMBOL(mips_dma_map_ops);
  365. #define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
  366. static int __init mips_dma_init(void)
  367. {
  368. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  369. return 0;
  370. }
  371. fs_initcall(mips_dma_init);