dma-coherent.c 11 KB

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