dma-coherent.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. int dma_memory_map;
  144. if (!dev)
  145. return 0;
  146. mem = dev->dma_mem;
  147. if (!mem)
  148. return 0;
  149. *ret = NULL;
  150. spin_lock_irqsave(&mem->spinlock, flags);
  151. if (unlikely(size > (mem->size << PAGE_SHIFT)))
  152. goto err;
  153. pageno = bitmap_find_free_region(mem->bitmap, mem->size, order);
  154. if (unlikely(pageno < 0))
  155. goto err;
  156. /*
  157. * Memory was found in the per-device area.
  158. */
  159. *dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
  160. *ret = mem->virt_base + (pageno << PAGE_SHIFT);
  161. dma_memory_map = (mem->flags & DMA_MEMORY_MAP);
  162. spin_unlock_irqrestore(&mem->spinlock, flags);
  163. if (dma_memory_map)
  164. memset(*ret, 0, size);
  165. else
  166. memset_io(*ret, 0, size);
  167. return 1;
  168. err:
  169. spin_unlock_irqrestore(&mem->spinlock, flags);
  170. /*
  171. * In the case where the allocation can not be satisfied from the
  172. * per-device area, try to fall back to generic memory if the
  173. * constraints allow it.
  174. */
  175. return mem->flags & DMA_MEMORY_EXCLUSIVE;
  176. }
  177. EXPORT_SYMBOL(dma_alloc_from_coherent);
  178. /**
  179. * dma_release_from_coherent() - try to free the memory allocated from per-device coherent memory pool
  180. * @dev: device from which the memory was allocated
  181. * @order: the order of pages allocated
  182. * @vaddr: virtual address of allocated pages
  183. *
  184. * This checks whether the memory was allocated from the per-device
  185. * coherent memory pool and if so, releases that memory.
  186. *
  187. * Returns 1 if we correctly released the memory, or 0 if
  188. * dma_release_coherent() should proceed with releasing memory from
  189. * generic pools.
  190. */
  191. int dma_release_from_coherent(struct device *dev, int order, void *vaddr)
  192. {
  193. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  194. if (mem && vaddr >= mem->virt_base && vaddr <
  195. (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  196. int page = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  197. unsigned long flags;
  198. spin_lock_irqsave(&mem->spinlock, flags);
  199. bitmap_release_region(mem->bitmap, page, order);
  200. spin_unlock_irqrestore(&mem->spinlock, flags);
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(dma_release_from_coherent);
  206. /**
  207. * dma_mmap_from_coherent() - try to mmap the memory allocated from
  208. * per-device coherent memory pool to userspace
  209. * @dev: device from which the memory was allocated
  210. * @vma: vm_area for the userspace memory
  211. * @vaddr: cpu address returned by dma_alloc_from_coherent
  212. * @size: size of the memory buffer allocated by dma_alloc_from_coherent
  213. * @ret: result from remap_pfn_range()
  214. *
  215. * This checks whether the memory was allocated from the per-device
  216. * coherent memory pool and if so, maps that memory to the provided vma.
  217. *
  218. * Returns 1 if we correctly mapped the memory, or 0 if the caller should
  219. * proceed with mapping memory from generic pools.
  220. */
  221. int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
  222. void *vaddr, size_t size, int *ret)
  223. {
  224. struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
  225. if (mem && vaddr >= mem->virt_base && vaddr + size <=
  226. (mem->virt_base + (mem->size << PAGE_SHIFT))) {
  227. unsigned long off = vma->vm_pgoff;
  228. int start = (vaddr - mem->virt_base) >> PAGE_SHIFT;
  229. int user_count = vma_pages(vma);
  230. int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
  231. *ret = -ENXIO;
  232. if (off < count && user_count <= count - off) {
  233. unsigned long pfn = mem->pfn_base + start + off;
  234. *ret = remap_pfn_range(vma, vma->vm_start, pfn,
  235. user_count << PAGE_SHIFT,
  236. vma->vm_page_prot);
  237. }
  238. return 1;
  239. }
  240. return 0;
  241. }
  242. EXPORT_SYMBOL(dma_mmap_from_coherent);
  243. /*
  244. * Support for reserved memory regions defined in device tree
  245. */
  246. #ifdef CONFIG_OF_RESERVED_MEM
  247. #include <linux/of.h>
  248. #include <linux/of_fdt.h>
  249. #include <linux/of_reserved_mem.h>
  250. static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
  251. {
  252. struct dma_coherent_mem *mem = rmem->priv;
  253. if (!mem &&
  254. !dma_init_coherent_memory(rmem->base, rmem->base, rmem->size,
  255. DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE,
  256. &mem)) {
  257. pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n",
  258. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  259. return -ENODEV;
  260. }
  261. rmem->priv = mem;
  262. dma_assign_coherent_memory(dev, mem);
  263. return 0;
  264. }
  265. static void rmem_dma_device_release(struct reserved_mem *rmem,
  266. struct device *dev)
  267. {
  268. dev->dma_mem = NULL;
  269. }
  270. static const struct reserved_mem_ops rmem_dma_ops = {
  271. .device_init = rmem_dma_device_init,
  272. .device_release = rmem_dma_device_release,
  273. };
  274. static int __init rmem_dma_setup(struct reserved_mem *rmem)
  275. {
  276. unsigned long node = rmem->fdt_node;
  277. if (of_get_flat_dt_prop(node, "reusable", NULL))
  278. return -EINVAL;
  279. #ifdef CONFIG_ARM
  280. if (!of_get_flat_dt_prop(node, "no-map", NULL)) {
  281. pr_err("Reserved memory: regions without no-map are not yet supported\n");
  282. return -EINVAL;
  283. }
  284. #endif
  285. rmem->ops = &rmem_dma_ops;
  286. pr_info("Reserved memory: created DMA memory pool at %pa, size %ld MiB\n",
  287. &rmem->base, (unsigned long)rmem->size / SZ_1M);
  288. return 0;
  289. }
  290. RESERVEDMEM_OF_DECLARE(dma, "shared-dma-pool", rmem_dma_setup);
  291. #endif