dma-default.c 12 KB

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