dma-mapping.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * SWIOTLB-based DMA API implementation
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Author: Catalin Marinas <catalin.marinas@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/gfp.h>
  20. #include <linux/export.h>
  21. #include <linux/slab.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dma-contiguous.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/swiotlb.h>
  28. #include <linux/amba/bus.h>
  29. #include <asm/cacheflush.h>
  30. struct dma_map_ops *dma_ops;
  31. EXPORT_SYMBOL(dma_ops);
  32. static pgprot_t __get_dma_pgprot(struct dma_attrs *attrs, pgprot_t prot,
  33. bool coherent)
  34. {
  35. if (!coherent || dma_get_attr(DMA_ATTR_WRITE_COMBINE, attrs))
  36. return pgprot_writecombine(prot);
  37. return prot;
  38. }
  39. static void *__dma_alloc_coherent(struct device *dev, size_t size,
  40. dma_addr_t *dma_handle, gfp_t flags,
  41. struct dma_attrs *attrs)
  42. {
  43. if (dev == NULL) {
  44. WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
  45. return NULL;
  46. }
  47. if (IS_ENABLED(CONFIG_ZONE_DMA) &&
  48. dev->coherent_dma_mask <= DMA_BIT_MASK(32))
  49. flags |= GFP_DMA;
  50. if (IS_ENABLED(CONFIG_DMA_CMA)) {
  51. struct page *page;
  52. size = PAGE_ALIGN(size);
  53. page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
  54. get_order(size));
  55. if (!page)
  56. return NULL;
  57. *dma_handle = phys_to_dma(dev, page_to_phys(page));
  58. return page_address(page);
  59. } else {
  60. return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
  61. }
  62. }
  63. static void __dma_free_coherent(struct device *dev, size_t size,
  64. void *vaddr, dma_addr_t dma_handle,
  65. struct dma_attrs *attrs)
  66. {
  67. if (dev == NULL) {
  68. WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
  69. return;
  70. }
  71. if (IS_ENABLED(CONFIG_DMA_CMA)) {
  72. phys_addr_t paddr = dma_to_phys(dev, dma_handle);
  73. dma_release_from_contiguous(dev,
  74. phys_to_page(paddr),
  75. size >> PAGE_SHIFT);
  76. } else {
  77. swiotlb_free_coherent(dev, size, vaddr, dma_handle);
  78. }
  79. }
  80. static void *__dma_alloc_noncoherent(struct device *dev, size_t size,
  81. dma_addr_t *dma_handle, gfp_t flags,
  82. struct dma_attrs *attrs)
  83. {
  84. struct page *page, **map;
  85. void *ptr, *coherent_ptr;
  86. int order, i;
  87. size = PAGE_ALIGN(size);
  88. order = get_order(size);
  89. ptr = __dma_alloc_coherent(dev, size, dma_handle, flags, attrs);
  90. if (!ptr)
  91. goto no_mem;
  92. map = kmalloc(sizeof(struct page *) << order, flags & ~GFP_DMA);
  93. if (!map)
  94. goto no_map;
  95. /* remove any dirty cache lines on the kernel alias */
  96. __dma_flush_range(ptr, ptr + size);
  97. /* create a coherent mapping */
  98. page = virt_to_page(ptr);
  99. for (i = 0; i < (size >> PAGE_SHIFT); i++)
  100. map[i] = page + i;
  101. coherent_ptr = vmap(map, size >> PAGE_SHIFT, VM_MAP,
  102. __get_dma_pgprot(attrs, __pgprot(PROT_NORMAL_NC), false));
  103. kfree(map);
  104. if (!coherent_ptr)
  105. goto no_map;
  106. return coherent_ptr;
  107. no_map:
  108. __dma_free_coherent(dev, size, ptr, *dma_handle, attrs);
  109. no_mem:
  110. *dma_handle = ~0;
  111. return NULL;
  112. }
  113. static void __dma_free_noncoherent(struct device *dev, size_t size,
  114. void *vaddr, dma_addr_t dma_handle,
  115. struct dma_attrs *attrs)
  116. {
  117. void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
  118. vunmap(vaddr);
  119. __dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
  120. }
  121. static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
  122. unsigned long offset, size_t size,
  123. enum dma_data_direction dir,
  124. struct dma_attrs *attrs)
  125. {
  126. dma_addr_t dev_addr;
  127. dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
  128. __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
  129. return dev_addr;
  130. }
  131. static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
  132. size_t size, enum dma_data_direction dir,
  133. struct dma_attrs *attrs)
  134. {
  135. __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
  136. swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
  137. }
  138. static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
  139. int nelems, enum dma_data_direction dir,
  140. struct dma_attrs *attrs)
  141. {
  142. struct scatterlist *sg;
  143. int i, ret;
  144. ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
  145. for_each_sg(sgl, sg, ret, i)
  146. __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
  147. sg->length, dir);
  148. return ret;
  149. }
  150. static void __swiotlb_unmap_sg_attrs(struct device *dev,
  151. struct scatterlist *sgl, int nelems,
  152. enum dma_data_direction dir,
  153. struct dma_attrs *attrs)
  154. {
  155. struct scatterlist *sg;
  156. int i;
  157. for_each_sg(sgl, sg, nelems, i)
  158. __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
  159. sg->length, dir);
  160. swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
  161. }
  162. static void __swiotlb_sync_single_for_cpu(struct device *dev,
  163. dma_addr_t dev_addr, size_t size,
  164. enum dma_data_direction dir)
  165. {
  166. __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
  167. swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
  168. }
  169. static void __swiotlb_sync_single_for_device(struct device *dev,
  170. dma_addr_t dev_addr, size_t size,
  171. enum dma_data_direction dir)
  172. {
  173. swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
  174. __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
  175. }
  176. static void __swiotlb_sync_sg_for_cpu(struct device *dev,
  177. struct scatterlist *sgl, int nelems,
  178. enum dma_data_direction dir)
  179. {
  180. struct scatterlist *sg;
  181. int i;
  182. for_each_sg(sgl, sg, nelems, i)
  183. __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
  184. sg->length, dir);
  185. swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
  186. }
  187. static void __swiotlb_sync_sg_for_device(struct device *dev,
  188. struct scatterlist *sgl, int nelems,
  189. enum dma_data_direction dir)
  190. {
  191. struct scatterlist *sg;
  192. int i;
  193. swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
  194. for_each_sg(sgl, sg, nelems, i)
  195. __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
  196. sg->length, dir);
  197. }
  198. /* vma->vm_page_prot must be set appropriately before calling this function */
  199. static int __dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
  200. void *cpu_addr, dma_addr_t dma_addr, size_t size)
  201. {
  202. int ret = -ENXIO;
  203. unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
  204. PAGE_SHIFT;
  205. unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  206. unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
  207. unsigned long off = vma->vm_pgoff;
  208. if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
  209. return ret;
  210. if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
  211. ret = remap_pfn_range(vma, vma->vm_start,
  212. pfn + off,
  213. vma->vm_end - vma->vm_start,
  214. vma->vm_page_prot);
  215. }
  216. return ret;
  217. }
  218. static int __swiotlb_mmap_noncoherent(struct device *dev,
  219. struct vm_area_struct *vma,
  220. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  221. struct dma_attrs *attrs)
  222. {
  223. vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot, false);
  224. return __dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
  225. }
  226. static int __swiotlb_mmap_coherent(struct device *dev,
  227. struct vm_area_struct *vma,
  228. void *cpu_addr, dma_addr_t dma_addr, size_t size,
  229. struct dma_attrs *attrs)
  230. {
  231. /* Just use whatever page_prot attributes were specified */
  232. return __dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
  233. }
  234. struct dma_map_ops noncoherent_swiotlb_dma_ops = {
  235. .alloc = __dma_alloc_noncoherent,
  236. .free = __dma_free_noncoherent,
  237. .mmap = __swiotlb_mmap_noncoherent,
  238. .map_page = __swiotlb_map_page,
  239. .unmap_page = __swiotlb_unmap_page,
  240. .map_sg = __swiotlb_map_sg_attrs,
  241. .unmap_sg = __swiotlb_unmap_sg_attrs,
  242. .sync_single_for_cpu = __swiotlb_sync_single_for_cpu,
  243. .sync_single_for_device = __swiotlb_sync_single_for_device,
  244. .sync_sg_for_cpu = __swiotlb_sync_sg_for_cpu,
  245. .sync_sg_for_device = __swiotlb_sync_sg_for_device,
  246. .dma_supported = swiotlb_dma_supported,
  247. .mapping_error = swiotlb_dma_mapping_error,
  248. };
  249. EXPORT_SYMBOL(noncoherent_swiotlb_dma_ops);
  250. struct dma_map_ops coherent_swiotlb_dma_ops = {
  251. .alloc = __dma_alloc_coherent,
  252. .free = __dma_free_coherent,
  253. .mmap = __swiotlb_mmap_coherent,
  254. .map_page = swiotlb_map_page,
  255. .unmap_page = swiotlb_unmap_page,
  256. .map_sg = swiotlb_map_sg_attrs,
  257. .unmap_sg = swiotlb_unmap_sg_attrs,
  258. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  259. .sync_single_for_device = swiotlb_sync_single_for_device,
  260. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  261. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  262. .dma_supported = swiotlb_dma_supported,
  263. .mapping_error = swiotlb_dma_mapping_error,
  264. };
  265. EXPORT_SYMBOL(coherent_swiotlb_dma_ops);
  266. static int dma_bus_notifier(struct notifier_block *nb,
  267. unsigned long event, void *_dev)
  268. {
  269. struct device *dev = _dev;
  270. if (event != BUS_NOTIFY_ADD_DEVICE)
  271. return NOTIFY_DONE;
  272. if (of_property_read_bool(dev->of_node, "dma-coherent"))
  273. set_dma_ops(dev, &coherent_swiotlb_dma_ops);
  274. return NOTIFY_OK;
  275. }
  276. static struct notifier_block platform_bus_nb = {
  277. .notifier_call = dma_bus_notifier,
  278. };
  279. static struct notifier_block amba_bus_nb = {
  280. .notifier_call = dma_bus_notifier,
  281. };
  282. extern int swiotlb_late_init_with_default_size(size_t default_size);
  283. static int __init swiotlb_late_init(void)
  284. {
  285. size_t swiotlb_size = min(SZ_64M, MAX_ORDER_NR_PAGES << PAGE_SHIFT);
  286. /*
  287. * These must be registered before of_platform_populate().
  288. */
  289. bus_register_notifier(&platform_bus_type, &platform_bus_nb);
  290. bus_register_notifier(&amba_bustype, &amba_bus_nb);
  291. dma_ops = &noncoherent_swiotlb_dma_ops;
  292. return swiotlb_late_init_with_default_size(swiotlb_size);
  293. }
  294. arch_initcall(swiotlb_late_init);
  295. #define PREALLOC_DMA_DEBUG_ENTRIES 4096
  296. static int __init dma_debug_do_init(void)
  297. {
  298. dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
  299. return 0;
  300. }
  301. fs_initcall(dma_debug_do_init);