coherent.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Coherent per-device memory handling.
  4. * Borrowed from i386
  5. */
  6. #include <linux/io.h>
  7. #include <linux/slab.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/dma-direct.h>
  11. #include <linux/dma-map-ops.h>
  12. struct dma_coherent_mem {
  13. void *virt_base;
  14. dma_addr_t device_base;
  15. unsigned long pfn_base;
  16. int size;
  17. unsigned long *bitmap;
  18. spinlock_t spinlock;
  19. bool use_dev_dma_pfn_offset;
  20. };
  21. static inline struct dma_coherent_mem *dev_get_coherent_memory(struct device *dev)
  22. {
  23. if (dev && dev->dma_mem)
  24. return dev->dma_mem;
  25. return NULL;
  26. }
  27. static inline dma_addr_t dma_get_device_base(struct device *dev,
  28. struct dma_coherent_mem * mem)
  29. {
  30. if (mem->use_dev_dma_pfn_offset)
  31. return phys_to_dma(dev, PFN_PHYS(mem->pfn_base));
  32. return mem->device_base;
  33. }
  34. static struct dma_coherent_mem *dma_init_coherent_memory(phys_addr_t phys_addr,
  35. dma_addr_t device_addr, size_t size, bool use_dma_pfn_offset)
  36. {
  37. struct dma_coherent_mem *dma_mem;
  38. int pages = size >> PAGE_SHIFT;
  39. void *mem_base;
  40. if (!size)
  41. return ERR_PTR(-EINVAL);
  42. mem_base = memremap(phys_addr, size, MEMREMAP_WC);
  43. if (!mem_base)
  44. return ERR_PTR(-EINVAL);
  45. dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
  46. if (!dma_mem)
  47. goto out_unmap_membase;
  48. dma_mem->bitmap = bitmap_zalloc(pages, GFP_KERNEL);
  49. if (!dma_mem->bitmap)
  50. goto out_free_dma_mem;
  51. dma_mem->virt_base = mem_base;
  52. dma_mem->device_base = device_addr;
  53. dma_mem->pfn_base = PFN_DOWN(phys_addr);
  54. dma_mem->size = pages;
  55. dma_mem->use_dev_dma_pfn_offset = use_dma_pfn_offset;
  56. spin_lock_init(&dma_mem->spinlock);
  57. return dma_mem;
  58. out_free_dma_mem:
  59. kfree(dma_mem);
  60. out_unmap_membase:
  61. memunmap(mem_base);
  62. pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %zd MiB\n",
  63. &phys_addr, size / SZ_1M);
  64. return ERR_PTR(-ENOMEM);
  65. }
  66. static void _dma_release_coherent_memory(struct dma_coherent_mem *mem)
  67. {
  68. if (!mem)
  69. return;
  70. memunmap(mem->virt_base);
  71. bitmap_free(mem->bitmap);
  72. kfree(mem);
  73. }
  74. static int dma_assign_coherent_memory(struct device *dev,
  75. struct dma_coherent_mem *mem)
  76. {
  77. if (!dev)
  78. return -ENODEV;
  79. if (dev->dma_mem)
  80. return -EBUSY;
  81. dev->dma_mem = mem;
  82. return 0;
  83. }
  84. /*
  85. * Declare a region of memory to be handed out by dma_alloc_coherent() when it
  86. * is asked for coherent memory for this device. This shall only be used
  87. * from platform code, usually based on the device tree description.
  88. *
  89. * phys_addr is the CPU physical address to which the memory is currently
  90. * assigned (this will be ioremapped so the CPU can access the region).
  91. *
  92. * device_addr is the DMA address the device needs to be programmed with to
  93. * actually address this memory (this will be handed out as the dma_addr_t in
  94. * dma_alloc_coherent()).
  95. *
  96. * size is the size of the area (must be a multiple of PAGE_SIZE).
  97. *
  98. * As a simplification for the platforms, only *one* such region of memory may
  99. * be declared per device.
  100. */
  101. int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  102. dma_addr_t device_addr, size_t size)
  103. {
  104. struct dma_coherent_mem *mem;
  105. int ret;
  106. mem = dma_init_coherent_memory(phys_addr, device_addr, size, false);
  107. if (IS_ERR(mem))
  108. return PTR_ERR(mem);
  109. ret = dma_assign_coherent_memory(dev, mem);
  110. if (ret)
  111. _dma_release_coherent_memory(mem);
  112. return ret;
  113. }
  114. void dma_release_coherent_memory(struct device *dev)
  115. {
  116. if (dev) {
  117. _dma_release_coherent_memory(dev->dma_mem);
  118. dev->dma_mem = NULL;
  119. }
  120. }
  121. static void *__dma_alloc_from_coherent(struct device *dev,
  122. struct dma_coherent_mem *mem,
  123. ssize_t size, dma_addr_t *dma_handle)
  124. {
  125. int order = get_order(size);
  126. unsigned long flags;
  127. int pageno;
  128. void *ret;
  129. spin_lock_irqsave(&mem->spinlock, flags);
  130. if (unlikely(size > ((dma_addr_t)mem->size << PAGE_SHIFT)))
  131. goto err;
  132. pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
  133. if (unlikely(pageno < 0))
  134. goto err;
  135. /*
  136. * Memory was found in the coherent area.
  137. */
  138. *dma_handle = dma_get_device_base(dev, mem) +
  139. ((dma_addr_t)pageno << PAGE_SHIFT);
  140. ret = mem->virt_base + ((dma_addr_t)pageno << PAGE_SHIFT);
  141. spin_unlock_irqrestore(&mem->spinlock, flags);
  142. memset(ret, 0, size);
  143. return ret;
  144. err:
  145. spin_unlock_irqrestore(&mem->spinlock, flags);
  146. return NULL;
  147. }
  148. /**
  149. * dma_alloc_from_dev_coherent() - allocate memory from device coherent pool
  150. * @dev: device from which we allocate memory
  151. * @size: size of requested memory area
  152. * @dma_handle: This will be filled with the correct dma handle
  153. * @ret: This pointer will be filled with the virtual address
  154. * to allocated area.
  155. *
  156. * This function should be only called from per-arch dma_alloc_coherent()
  157. * to support allocation from per-device coherent memory pools.
  158. *
  159. * Returns 0 if dma_alloc_coherent should continue with allocating from
  160. * generic memory areas, or !0 if dma_alloc_coherent should return @ret.
  161. */
  162. int dma_alloc_from_dev_coherent(struct device *dev, ssize_t size,
  163. dma_addr_t *dma_handle, void **ret)
  164. {
  165. struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
  166. if (!mem)
  167. return 0;
  168. *ret = __dma_alloc_from_coherent(dev, mem, size, dma_handle);
  169. return 1;
  170. }
  171. static int __dma_release_from_coherent(struct dma_coherent_mem *mem,
  172. int order, void *vaddr)
  173. {
  174. if (mem && vaddr >= mem->virt_base && vaddr <
  175. (mem->virt_base + ((dma_addr_t)mem->size << PAGE_SHIFT))) {
  176. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  177. unsigned long flags;
  178. spin_lock_irqsave(&mem->spinlock, flags);
  179. bitmap_release_region(mem->bitmap, page, order);
  180. spin_unlock_irqrestore(&mem->spinlock, flags);
  181. return 1;
  182. }
  183. return 0;
  184. }
  185. /**
  186. * dma_release_from_dev_coherent() - free memory to device coherent memory pool
  187. * @dev: device from which the memory was allocated
  188. * @order: the order of pages allocated
  189. * @vaddr: virtual address of allocated pages
  190. *
  191. * This checks whether the memory was allocated from the per-device
  192. * coherent memory pool and if so, releases that memory.
  193. *
  194. * Returns 1 if we correctly released the memory, or 0 if the caller should
  195. * proceed with releasing memory from generic pools.
  196. */
  197. int dma_release_from_dev_coherent(struct device *dev, int order, void *vaddr)
  198. {
  199. struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
  200. return __dma_release_from_coherent(mem, order, vaddr);
  201. }
  202. static int __dma_mmap_from_coherent(struct dma_coherent_mem *mem,
  203. struct vm_area_struct *vma, void *vaddr, size_t size, int *ret)
  204. {
  205. if (mem && vaddr >= mem->virt_base && vaddr + size <=
  206. (mem->virt_base + ((dma_addr_t)mem->size << PAGE_SHIFT))) {
  207. unsigned long off = vma->vm_pgoff;
  208. int start = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  209. unsigned long user_count = vma_pages(vma);
  210. int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  211. *ret = -ENXIO;
  212. if (off < count && user_count <= count - off) {
  213. unsigned long pfn = mem->pfn_base + start + off;
  214. *ret = remap_pfn_range(vma, vma->vm_start, pfn,
  215. user_count << PAGE_SHIFT,
  216. vma->vm_page_prot);
  217. }
  218. return 1;
  219. }
  220. return 0;
  221. }
  222. /**
  223. * dma_mmap_from_dev_coherent() - mmap memory from the device coherent pool
  224. * @dev: device from which the memory was allocated
  225. * @vma: vm_area for the userspace memory
  226. * @vaddr: cpu address returned by dma_alloc_from_dev_coherent
  227. * @size: size of the memory buffer allocated
  228. * @ret: result from remap_pfn_range()
  229. *
  230. * This checks whether the memory was allocated from the per-device
  231. * coherent memory pool and if so, maps that memory to the provided vma.
  232. *
  233. * Returns 1 if @vaddr belongs to the device coherent pool and the caller
  234. * should return @ret, or 0 if they should proceed with mapping memory from
  235. * generic areas.
  236. */
  237. int dma_mmap_from_dev_coherent(struct device *dev, struct vm_area_struct *vma,
  238. void *vaddr, size_t size, int *ret)
  239. {
  240. struct dma_coherent_mem *mem = dev_get_coherent_memory(dev);
  241. return __dma_mmap_from_coherent(mem, vma, vaddr, size, ret);
  242. }
  243. #ifdef CONFIG_DMA_GLOBAL_POOL
  244. static struct dma_coherent_mem *dma_coherent_default_memory __ro_after_init;
  245. void *dma_alloc_from_global_coherent(struct device *dev, ssize_t size,
  246. dma_addr_t *dma_handle)
  247. {
  248. if (!dma_coherent_default_memory)
  249. return NULL;
  250. return __dma_alloc_from_coherent(dev, dma_coherent_default_memory, size,
  251. dma_handle);
  252. }
  253. int dma_release_from_global_coherent(int order, void *vaddr)
  254. {
  255. if (!dma_coherent_default_memory)
  256. return 0;
  257. return __dma_release_from_coherent(dma_coherent_default_memory, order,
  258. vaddr);
  259. }
  260. int dma_mmap_from_global_coherent(struct vm_area_struct *vma, void *vaddr,
  261. size_t size, int *ret)
  262. {
  263. if (!dma_coherent_default_memory)
  264. return 0;
  265. return __dma_mmap_from_coherent(dma_coherent_default_memory, vma,
  266. vaddr, size, ret);
  267. }
  268. int dma_init_global_coherent(phys_addr_t phys_addr, size_t size)
  269. {
  270. struct dma_coherent_mem *mem;
  271. mem = dma_init_coherent_memory(phys_addr, phys_addr, size, true);
  272. if (IS_ERR(mem))
  273. return PTR_ERR(mem);
  274. dma_coherent_default_memory = mem;
  275. pr_info("DMA: default coherent area is set\n");
  276. return 0;
  277. }
  278. #endif /* CONFIG_DMA_GLOBAL_POOL */
  279. /*
  280. * Support for reserved memory regions defined in device tree
  281. */
  282. #ifdef CONFIG_OF_RESERVED_MEM
  283. #include <linux/of.h>
  284. #include <linux/of_fdt.h>
  285. #include <linux/of_reserved_mem.h>
  286. #ifdef CONFIG_DMA_GLOBAL_POOL
  287. static phys_addr_t dma_reserved_default_memory_base __initdata;
  288. static phys_addr_t dma_reserved_default_memory_size __initdata;
  289. #endif
  290. static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
  291. {
  292. if (!rmem->priv) {
  293. struct dma_coherent_mem *mem;
  294. mem = dma_init_coherent_memory(rmem->base, rmem->base,
  295. rmem->size, true);
  296. if (IS_ERR(mem))
  297. return PTR_ERR(mem);
  298. rmem->priv = mem;
  299. }
  300. dma_assign_coherent_memory(dev, rmem->priv);
  301. return 0;
  302. }
  303. static void rmem_dma_device_release(struct reserved_mem *rmem,
  304. struct device *dev)
  305. {
  306. if (dev)
  307. dev->dma_mem = NULL;
  308. }
  309. static const struct reserved_mem_ops rmem_dma_ops = {
  310. .device_init = rmem_dma_device_init,
  311. .device_release = rmem_dma_device_release,
  312. };
  313. static int __init rmem_dma_setup(struct reserved_mem *rmem)
  314. {
  315. unsigned long node = rmem->fdt_node;
  316. if (of_get_flat_dt_prop(node, "reusable", NULL))
  317. return -EINVAL;
  318. #ifdef CONFIG_ARM
  319. if (!of_get_flat_dt_prop(node, "no-map", NULL)) {
  320. pr_err("Reserved memory: regions without no-map are not yet supported\n");
  321. return -EINVAL;
  322. }
  323. #endif
  324. #ifdef CONFIG_DMA_GLOBAL_POOL
  325. if (of_get_flat_dt_prop(node, "linux,dma-default", NULL)) {
  326. WARN(dma_reserved_default_memory_size,
  327. "Reserved memory: region for default DMA coherent area is redefined\n");
  328. dma_reserved_default_memory_base = rmem->base;
  329. dma_reserved_default_memory_size = rmem->size;
  330. }
  331. #endif
  332. rmem->ops = &rmem_dma_ops;
  333. pr_info("Reserved memory: created DMA memory pool at %pa, size %ld MiB\n",
  334. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  335. return 0;
  336. }
  337. #ifdef CONFIG_DMA_GLOBAL_POOL
  338. static int __init dma_init_reserved_memory(void)
  339. {
  340. if (!dma_reserved_default_memory_size)
  341. return -ENOMEM;
  342. return dma_init_global_coherent(dma_reserved_default_memory_base,
  343. dma_reserved_default_memory_size);
  344. }
  345. core_initcall(dma_init_reserved_memory);
  346. #endif /* CONFIG_DMA_GLOBAL_POOL */
  347. RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup);
  348. #endif