dma-coherent.c 8.6 KB

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