dma-coherent.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. };
  19. static bool dma_init_coherent_memory(
  20. phys_addr_t phys_addr, dma_addr_t device_addr, size_t size, int flags,
  21. struct dma_coherent_mem **mem)
  22. {
  23. struct dma_coherent_mem *dma_mem = NULL;
  24. void __iomem *mem_base = NULL;
  25. int pages = size >> PAGE_SHIFT;
  26. int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
  27. if ((flags & (DMA_MEMORY_MAP | DMA_MEMORY_IO)) == 0)
  28. goto out;
  29. if (!size)
  30. goto out;
  31. if (flags & DMA_MEMORY_MAP)
  32. mem_base = memremap(phys_addr, size, MEMREMAP_WC);
  33. else
  34. mem_base = ioremap(phys_addr, size);
  35. if (!mem_base)
  36. goto out;
  37. dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
  38. if (!dma_mem)
  39. goto out;
  40. dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  41. if (!dma_mem->bitmap)
  42. goto out;
  43. dma_mem->virt_base = mem_base;
  44. dma_mem->device_base = device_addr;
  45. dma_mem->pfn_base = PFN_DOWN(phys_addr);
  46. dma_mem->size = pages;
  47. dma_mem->flags = flags;
  48. spin_lock_init(&dma_mem->spinlock);
  49. *mem = dma_mem;
  50. return true;
  51. out:
  52. kfree(dma_mem);
  53. if (mem_base) {
  54. if (flags & DMA_MEMORY_MAP)
  55. memunmap(mem_base);
  56. else
  57. iounmap(mem_base);
  58. }
  59. return false;
  60. }
  61. static void dma_release_coherent_memory(struct dma_coherent_mem *mem)
  62. {
  63. if (!mem)
  64. return;
  65. if (mem->flags & DMA_MEMORY_MAP)
  66. memunmap(mem->virt_base);
  67. else
  68. iounmap(mem->virt_base);
  69. kfree(mem->bitmap);
  70. kfree(mem);
  71. }
  72. static int dma_assign_coherent_memory(struct device *dev,
  73. struct dma_coherent_mem *mem)
  74. {
  75. if (dev->dma_mem)
  76. return -EBUSY;
  77. dev->dma_mem = mem;
  78. /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
  79. return 0;
  80. }
  81. int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
  82. dma_addr_t device_addr, size_t size, int flags)
  83. {
  84. struct dma_coherent_mem *mem;
  85. if (!dma_init_coherent_memory(phys_addr, device_addr, size, flags,
  86. &mem))
  87. return 0;
  88. if (dma_assign_coherent_memory(dev, mem) == 0)
  89. return flags & DMA_MEMORY_MAP ? DMA_MEMORY_MAP : DMA_MEMORY_IO;
  90. dma_release_coherent_memory(mem);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(dma_declare_coherent_memory);
  94. void dma_release_declared_memory(struct device *dev)
  95. {
  96. struct dma_coherent_mem *mem = dev->dma_mem;
  97. if (!mem)
  98. return;
  99. dma_release_coherent_memory(mem);
  100. dev->dma_mem = NULL;
  101. }
  102. EXPORT_SYMBOL(dma_release_declared_memory);
  103. void *dma_mark_declared_memory_occupied(struct device *dev,
  104. dma_addr_t device_addr, size_t size)
  105. {
  106. struct dma_coherent_mem *mem = dev->dma_mem;
  107. unsigned long flags;
  108. int pos, err;
  109. size += device_addr & ~PAGE_MASK;
  110. if (!mem)
  111. return ERR_PTR(-EINVAL);
  112. spin_lock_irqsave(&mem->spinlock, flags);
  113. pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
  114. err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
  115. spin_unlock_irqrestore(&mem->spinlock, flags);
  116. if (err != 0)
  117. return ERR_PTR(err);
  118. return mem->virt_base + (pos << PAGE_SHIFT);
  119. }
  120. EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
  121. /**
  122. * dma_alloc_from_coherent() - try to allocate memory from the per-device coherent area
  123. *
  124. * @dev: device from which we allocate memory
  125. * @size: size of requested memory area
  126. * @dma_handle: This will be filled with the correct dma handle
  127. * @ret: This pointer will be filled with the virtual address
  128. * to allocated area.
  129. *
  130. * This function should be only called from per-arch dma_alloc_coherent()
  131. * to support allocation from per-device coherent memory pools.
  132. *
  133. * Returns 0 if dma_alloc_coherent should continue with allocating from
  134. * generic memory areas, or !0 if dma_alloc_coherent should return @ret.
  135. */
  136. int dma_alloc_from_coherent(struct device *dev, ssize_t size,
  137. dma_addr_t *dma_handle, void **ret)
  138. {
  139. struct dma_coherent_mem *mem;
  140. int order = get_order(size);
  141. unsigned long flags;
  142. int pageno;
  143. if (!dev)
  144. return 0;
  145. mem = dev->dma_mem;
  146. if (!mem)
  147. return 0;
  148. *ret = NULL;
  149. spin_lock_irqsave(&mem->spinlock, flags);
  150. if (unlikely(size > (mem->size << PAGE_SHIFT)))
  151. goto err;
  152. pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
  153. if (unlikely(pageno < 0))
  154. goto err;
  155. /*
  156. * Memory was found in the per-device area.
  157. */
  158. *dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
  159. *ret = mem->virt_base + (pageno << PAGE_SHIFT);
  160. if (mem->flags & DMA_MEMORY_MAP)
  161. memset(*ret, 0, size);
  162. else
  163. memset_io(*ret, 0, size);
  164. spin_unlock_irqrestore(&mem->spinlock, flags);
  165. return 1;
  166. err:
  167. spin_unlock_irqrestore(&mem->spinlock, flags);
  168. /*
  169. * In the case where the allocation can not be satisfied from the
  170. * per-device area, try to fall back to generic memory if the
  171. * constraints allow it.
  172. */
  173. return mem->flags & DMA_MEMORY_EXCLUSIVE;
  174. }
  175. EXPORT_SYMBOL(dma_alloc_from_coherent);
  176. /**
  177. * dma_release_from_coherent() - try to free the memory allocated from per-device coherent memory pool
  178. * @dev: device from which the memory was allocated
  179. * @order: the order of pages allocated
  180. * @vaddr: virtual address of allocated pages
  181. *
  182. * This checks whether the memory was allocated from the per-device
  183. * coherent memory pool and if so, releases that memory.
  184. *
  185. * Returns 1 if we correctly released the memory, or 0 if
  186. * dma_release_coherent() should proceed with releasing memory from
  187. * generic pools.
  188. */
  189. int dma_release_from_coherent(struct device *dev, int order, void *vaddr)
  190. {
  191. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  192. if (mem && vaddr >= mem->virt_base && vaddr <
  193. (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  194. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  195. unsigned long flags;
  196. spin_lock_irqsave(&mem->spinlock, flags);
  197. bitmap_release_region(mem->bitmap, page, order);
  198. spin_unlock_irqrestore(&mem->spinlock, flags);
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. EXPORT_SYMBOL(dma_release_from_coherent);
  204. /**
  205. * dma_mmap_from_coherent() - try to mmap the memory allocated from
  206. * per-device coherent memory pool to userspace
  207. * @dev: device from which the memory was allocated
  208. * @vma: vm_area for the userspace memory
  209. * @vaddr: cpu address returned by dma_alloc_from_coherent
  210. * @size: size of the memory buffer allocated by dma_alloc_from_coherent
  211. * @ret: result from remap_pfn_range()
  212. *
  213. * This checks whether the memory was allocated from the per-device
  214. * coherent memory pool and if so, maps that memory to the provided vma.
  215. *
  216. * Returns 1 if we correctly mapped the memory, or 0 if the caller should
  217. * proceed with mapping memory from generic pools.
  218. */
  219. int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
  220. void *vaddr, size_t size, int *ret)
  221. {
  222. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  223. if (mem && vaddr >= mem->virt_base && vaddr + size <=
  224. (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  225. unsigned long off = vma->vm_pgoff;
  226. int start = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  227. int user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  228. int count = size >> PAGE_SHIFT;
  229. *ret = -ENXIO;
  230. if (off < count && user_count <= count - off) {
  231. unsigned long pfn = mem->pfn_base + start + off;
  232. *ret = remap_pfn_range(vma, vma->vm_start, pfn,
  233. user_count << PAGE_SHIFT,
  234. vma->vm_page_prot);
  235. }
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. EXPORT_SYMBOL(dma_mmap_from_coherent);
  241. /*
  242. * Support for reserved memory regions defined in device tree
  243. */
  244. #ifdef CONFIG_OF_RESERVED_MEM
  245. #include <linux/of.h>
  246. #include <linux/of_fdt.h>
  247. #include <linux/of_reserved_mem.h>
  248. static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
  249. {
  250. struct dma_coherent_mem *mem = rmem->priv;
  251. if (!mem &&
  252. !dma_init_coherent_memory(rmem->base, rmem->base, rmem->size,
  253. DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE,
  254. &mem)) {
  255. pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n",
  256. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  257. return -ENODEV;
  258. }
  259. rmem->priv = mem;
  260. dma_assign_coherent_memory(dev, mem);
  261. return 0;
  262. }
  263. static void rmem_dma_device_release(struct reserved_mem *rmem,
  264. struct device *dev)
  265. {
  266. dev->dma_mem = NULL;
  267. }
  268. static const struct reserved_mem_ops rmem_dma_ops = {
  269. .device_init = rmem_dma_device_init,
  270. .device_release = rmem_dma_device_release,
  271. };
  272. static int __init rmem_dma_setup(struct reserved_mem *rmem)
  273. {
  274. unsigned long node = rmem->fdt_node;
  275. if (of_get_flat_dt_prop(node, "reusable", NULL))
  276. return -EINVAL;
  277. #ifdef CONFIG_ARM
  278. if (!of_get_flat_dt_prop(node, "no-map", NULL)) {
  279. pr_err("Reserved memory: regions without no-map are not yet supported\n");
  280. return -EINVAL;
  281. }
  282. #endif
  283. rmem->ops = &rmem_dma_ops;
  284. pr_info("Reserved memory: created DMA memory pool at %pa, size %ld MiB\n",
  285. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  286. return 0;
  287. }
  288. RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup);
  289. #endif