2 * Coherent per-device memory handling.
6 #define pr_fmt(fmt) "%s:%d: " fmt, __func__, __LINE__
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dma-attrs.h>
12 #include <linux/dma-contiguous.h>
13 #include <linux/debugfs.h>
14 #include <linux/highmem.h>
15 #include <asm/cacheflush.h>
17 #define RESIZE_MAGIC 0xC11A900d
21 /* number of chunks memory to manage in */
22 unsigned int num_chunks;
23 /* dev to manage cma/coherent memory allocs, if resize allowed */
25 /* device to allocate memory from cma */
26 struct device *cma_dev;
27 /* lock to synchronise heap resizing */
28 struct mutex resize_lock;
29 /* CMA chunk size if resize supported */
30 size_t cma_chunk_size;
31 /* heap current base */
32 phys_addr_t curr_base;
33 /* heap current length */
35 /* heap lowest base */
39 size_t rem_chunk_size;
40 struct dentry *dma_debug_root;
41 int (*update_resize_cfg)(phys_addr_t , size_t);
44 #ifdef CONFIG_ARM_DMA_IOMMU_ALIGNMENT
45 #define DMA_BUF_ALIGNMENT CONFIG_ARM_DMA_IOMMU_ALIGNMENT
47 #define DMA_BUF_ALIGNMENT 8
50 struct dma_coherent_mem {
52 dma_addr_t device_base;
56 unsigned long *bitmap;
59 static bool dma_is_coherent_dev(struct device *dev)
65 h = dev_get_drvdata(dev);
68 if (h->magic != RESIZE_MAGIC)
72 static void dma_debugfs_init(struct device *dev, struct heap_info *heap)
74 if (!heap->dma_debug_root) {
75 heap->dma_debug_root = debugfs_create_dir(dev_name(dev), NULL);
76 if (IS_ERR_OR_NULL(heap->dma_debug_root)) {
77 dev_err(dev, "couldn't create debug files\n");
82 debugfs_create_x32("curr_base", S_IRUGO,
83 heap->dma_debug_root, (u32 *)&heap->curr_base);
84 debugfs_create_x32("curr_len", S_IRUGO,
85 heap->dma_debug_root, (u32 *)&heap->curr_len);
86 debugfs_create_x32("cma_base", S_IRUGO,
87 heap->dma_debug_root, (u32 *)&heap->cma_base);
88 debugfs_create_x32("cma_size", S_IRUGO,
89 heap->dma_debug_root, (u32 *)&heap->cma_len);
90 debugfs_create_x32("cma_chunk_size", S_IRUGO,
91 heap->dma_debug_root, (u32 *)&heap->cma_chunk_size);
92 debugfs_create_x32("num_cma_chunks", S_IRUGO,
93 heap->dma_debug_root, (u32 *)&heap->num_chunks);
96 int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
97 dma_addr_t device_addr, size_t size, int flags)
99 void __iomem *mem_base = NULL;
100 int pages = size >> PAGE_SHIFT;
101 int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
104 (DMA_MEMORY_MAP | DMA_MEMORY_IO | DMA_MEMORY_NOMAP)) == 0)
111 /* FIXME: this routine just ignores DMA_MEMORY_INCLUDES_CHILDREN */
113 dev->dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
116 dev->dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
117 if (!dev->dma_mem->bitmap)
120 if (flags & DMA_MEMORY_NOMAP)
123 mem_base = ioremap(bus_addr, size);
126 dev->dma_mem->virt_base = mem_base;
129 dev->dma_mem->device_base = device_addr;
130 dev->dma_mem->pfn_base = PFN_DOWN(bus_addr);
131 dev->dma_mem->size = pages;
132 dev->dma_mem->flags = flags;
134 if (flags & DMA_MEMORY_MAP)
135 return DMA_MEMORY_MAP;
137 if (flags & DMA_MEMORY_NOMAP)
138 return DMA_MEMORY_NOMAP;
140 return DMA_MEMORY_IO;
150 EXPORT_SYMBOL(dma_declare_coherent_memory);
152 static int declare_coherent_heap(struct device *dev, phys_addr_t base,
157 BUG_ON(dev->dma_mem);
158 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
159 err = dma_declare_coherent_memory(dev, 0,
160 base, size, DMA_MEMORY_NOMAP);
161 if (err & DMA_MEMORY_NOMAP) {
162 dev_dbg(dev, "dma coherent mem base (0x%pa) size (0x%zx)\n",
166 dev_err(dev, "declare dma coherent_mem fail 0x%pa 0x%zx\n",
171 int dma_declare_coherent_resizable_cma_memory(struct device *dev,
172 struct dma_declare_info *dma_info)
176 struct heap_info *heap_info = NULL;
177 struct dma_contiguous_stats stats;
179 if (!dev || !dma_info || !dma_info->name || !dma_info->cma_dev)
182 heap_info = kzalloc(sizeof(*heap_info), GFP_KERNEL);
186 heap_info->magic = RESIZE_MAGIC;
187 heap_info->name = kmalloc(strlen(dma_info->name) + 1, GFP_KERNEL);
188 if (!heap_info->name) {
193 dma_get_contiguous_stats(dma_info->cma_dev, &stats);
194 pr_info("resizable heap=%s, base=0x%pa, size=0x%zx\n",
195 dma_info->name, &stats.base, stats.size);
196 strcpy(heap_info->name, dma_info->name);
197 dev_set_name(dev, "dma-%s", heap_info->name);
198 heap_info->cma_dev = dma_info->cma_dev;
199 heap_info->cma_chunk_size = dma_info->size ? : stats.size;
200 heap_info->cma_base = stats.base;
201 heap_info->cma_len = stats.size;
202 heap_info->curr_base = stats.base;
203 dev_set_name(heap_info->cma_dev, "cma-%s-heap", heap_info->name);
204 mutex_init(&heap_info->resize_lock);
206 if (heap_info->cma_len < heap_info->cma_chunk_size) {
207 dev_err(dev, "error cma_len(0x%zx) < cma_chunk_size(0x%zx)\n",
208 heap_info->cma_len, heap_info->cma_chunk_size);
213 heap_info->num_chunks = div_u64_rem(heap_info->cma_len,
214 (u32)heap_info->cma_chunk_size, (u32 *)&heap_info->rem_chunk_size);
215 if (heap_info->rem_chunk_size) {
216 heap_info->num_chunks++;
217 dev_info(dev, "heap size is not multiple of cma_chunk_size "
218 "heap_info->num_chunks (%d) rem_chunk_size(0x%zx)\n",
219 heap_info->num_chunks, heap_info->rem_chunk_size);
221 heap_info->rem_chunk_size = heap_info->cma_chunk_size;
223 dev_set_name(&heap_info->dev, "%s-heap", heap_info->name);
225 if (dma_info->notifier.ops)
226 heap_info->update_resize_cfg =
227 dma_info->notifier.ops->resize;
229 dev_set_drvdata(dev, heap_info);
230 dma_debugfs_init(dev, heap_info);
232 if (declare_coherent_heap(&heap_info->dev,
233 heap_info->cma_base, heap_info->cma_len))
235 heap_info->dev.dma_mem->size = 0;
237 pr_info("resizable cma heap=%s create successful", heap_info->name);
240 kfree(heap_info->name);
248 EXPORT_SYMBOL(dma_declare_coherent_resizable_cma_memory);
250 static phys_addr_t alloc_from_contiguous_heap(
252 phys_addr_t base, size_t len)
258 dev_dbg(h->cma_dev, "req at base (0x%pa) size (0x%zx)\n",
260 order = get_order(len);
261 count = PAGE_ALIGN(len) >> PAGE_SHIFT;
262 page = dma_alloc_at_from_contiguous(h->cma_dev, count, order, base);
264 dev_err(h->cma_dev, "dma_alloc_at_from_contiguous failed\n");
268 base = page_to_phys(page);
269 dev_dbg(h->cma_dev, "allocated at base (0x%pa) size (0x%zx)\n",
271 BUG_ON(base < h->cma_base ||
272 base - h->cma_base + len > h->cma_len);
276 return DMA_ERROR_CODE;
279 static void release_from_contiguous_heap(
281 phys_addr_t base, size_t len)
283 struct page *page = phys_to_page(base);
284 size_t count = PAGE_ALIGN(len) >> PAGE_SHIFT;
286 dma_release_from_contiguous(h->cma_dev, page, count);
287 dev_dbg(h->cma_dev, "released at base (0x%pa) size (0x%zx)\n",
291 static void get_first_and_last_idx(struct heap_info *h,
292 int *first_alloc_idx, int *last_alloc_idx)
295 *first_alloc_idx = -1;
296 *last_alloc_idx = h->num_chunks;
298 *first_alloc_idx = div_u64(h->curr_base - h->cma_base,
300 *last_alloc_idx = div_u64(h->curr_base - h->cma_base +
301 h->curr_len + h->cma_chunk_size -
303 h->cma_chunk_size) - 1;
307 static void update_alloc_range(struct heap_info *h)
310 h->dev.dma_mem->size = 0;
312 h->dev.dma_mem->size = (h->curr_base - h->cma_base +
313 h->curr_len) >> PAGE_SHIFT;
316 static int heap_resize_locked(struct heap_info *h)
320 phys_addr_t base = -1;
321 size_t len = h->cma_chunk_size;
322 phys_addr_t prev_base = h->curr_base;
323 size_t prev_len = h->curr_len;
324 int alloc_at_idx = 0;
327 phys_addr_t start_addr = 0;
329 get_first_and_last_idx(h, &first_alloc_idx, &last_alloc_idx);
330 pr_debug("req resize, fi=%d,li=%d\n", first_alloc_idx, last_alloc_idx);
332 /* All chunks are in use. Can't grow it. */
333 if (first_alloc_idx == 0 && last_alloc_idx == h->num_chunks - 1)
336 /* All chunks are free. Can allocate anywhere in CMA with
337 * cma_chunk_size alignment.
339 if (first_alloc_idx == -1) {
340 base = alloc_from_contiguous_heap(h, start_addr, len);
341 if (!dma_mapping_error(h->cma_dev, base))
345 /* Free chunk before previously allocated chunk. Attempt
346 * to allocate only immediate previous chunk.
348 if (first_alloc_idx > 0) {
349 alloc_at_idx = first_alloc_idx - 1;
350 start_addr = alloc_at_idx * h->cma_chunk_size + h->cma_base;
351 base = alloc_from_contiguous_heap(h, start_addr, len);
352 if (base == start_addr)
354 BUG_ON(!dma_mapping_error(h->cma_dev, base));
357 /* Free chunk after previously allocated chunk. */
358 if (last_alloc_idx < h->num_chunks - 1) {
359 alloc_at_idx = last_alloc_idx + 1;
360 len = (alloc_at_idx == h->num_chunks - 1) ?
361 h->rem_chunk_size : h->cma_chunk_size;
362 start_addr = alloc_at_idx * h->cma_chunk_size + h->cma_base;
363 base = alloc_from_contiguous_heap(h, start_addr, len);
364 if (base == start_addr)
366 BUG_ON(!dma_mapping_error(h->cma_dev, base));
369 if (dma_mapping_error(h->cma_dev, base))
371 "Failed to allocate contiguous memory on heap grow req\n");
376 if (!h->curr_len || h->curr_base > base)
380 for (i = 0; i < (len >> PAGE_SHIFT); i++) {
381 struct page *page = phys_to_page(i + base);
383 if (PageHighMem(page)) {
384 void *ptr = kmap_atomic(page);
385 dmac_flush_range(ptr, ptr + PAGE_SIZE);
388 void *ptr = page_address(page);
389 dmac_flush_range(ptr, ptr + PAGE_SIZE);
393 /* Handle VPR configuration updates*/
394 if (h->update_resize_cfg) {
395 err = h->update_resize_cfg(h->curr_base, h->curr_len);
397 dev_err(&h->dev, "Failed to update heap resize\n");
400 dev_dbg(&h->dev, "update vpr base to %pa, size=%zx\n",
401 &h->curr_base, h->curr_len);
404 update_alloc_range(h);
406 "grow heap base from=0x%pa to=0x%pa,"
407 " len from=0x%zx to=0x%zx\n",
408 &prev_base, &h->curr_base, prev_len, h->curr_len);
412 release_from_contiguous_heap(h, base, len);
413 h->curr_base = prev_base;
414 h->curr_len = prev_len;
418 /* retval: !0 on success, 0 on failure */
419 static int dma_alloc_from_coherent_dev_at(struct device *dev, ssize_t size,
420 dma_addr_t *dma_handle, void **ret,
421 struct dma_attrs *attrs, ulong start)
423 struct dma_coherent_mem *mem;
424 int order = get_order(size);
435 *dma_handle = DMA_ERROR_CODE;
438 if (unlikely(size > (mem->size << PAGE_SHIFT)))
441 if (order > DMA_BUF_ALIGNMENT)
442 align = (1 << DMA_BUF_ALIGNMENT) - 1;
444 align = (1 << order) - 1;
446 if (dma_get_attr(DMA_ATTR_ALLOC_EXACT_SIZE, attrs))
447 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
451 pageno = bitmap_find_next_zero_area(mem->bitmap, mem->size,
452 start, count, align);
454 if (pageno >= mem->size)
457 bitmap_set(mem->bitmap, pageno, count);
460 * Memory was found in the per-device area.
462 *dma_handle = mem->device_base + (pageno << PAGE_SHIFT);
463 if (!(mem->flags & DMA_MEMORY_NOMAP)) {
464 *ret = mem->virt_base + (pageno << PAGE_SHIFT);
465 memset(*ret, 0, size);
472 * In the case where the allocation can not be satisfied from the
473 * per-device area, try to fall back to generic memory if the
474 * constraints allow it.
476 return mem->flags & DMA_MEMORY_EXCLUSIVE;
479 static int dma_alloc_from_coherent_dev(struct device *dev, ssize_t size,
480 dma_addr_t *dma_handle, void **ret,
481 struct dma_attrs *attrs)
483 return dma_alloc_from_coherent_dev_at(dev, size, dma_handle,
487 /* retval: !0 on success, 0 on failure */
488 static int dma_alloc_from_coherent_heap_dev(struct device *dev, size_t len,
489 dma_addr_t *dma_handle, void **ret,
490 struct dma_attrs *attrs)
492 struct heap_info *h = NULL;
494 *dma_handle = DMA_ERROR_CODE;
495 if (!dma_is_coherent_dev(dev))
498 h = dev_get_drvdata(dev);
501 return DMA_MEMORY_EXCLUSIVE;
502 dma_set_attr(DMA_ATTR_ALLOC_EXACT_SIZE, attrs);
504 mutex_lock(&h->resize_lock);
506 /* Try allocation from already existing CMA chunks */
507 if (dma_alloc_from_coherent_dev_at(
508 &h->dev, len, dma_handle, ret, attrs,
509 (h->curr_base - h->cma_base) >> PAGE_SHIFT)) {
510 dev_dbg(&h->dev, "allocated addr 0x%pa len 0x%zx\n",
515 if (!heap_resize_locked(h))
518 mutex_unlock(&h->resize_lock);
519 return DMA_MEMORY_EXCLUSIVE;
522 /* retval: !0 on success, 0 on failure */
523 static int dma_release_from_coherent_dev(struct device *dev, size_t size,
524 void *vaddr, struct dma_attrs *attrs)
526 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
534 if (mem->flags & DMA_MEMORY_NOMAP)
535 mem_addr = (void *)(uintptr_t)mem->device_base;
537 mem_addr = mem->virt_base;
539 if (mem && vaddr >= mem_addr &&
540 vaddr - mem_addr < mem->size << PAGE_SHIFT) {
542 pageno = (vaddr - mem_addr) >> PAGE_SHIFT;
544 if (dma_get_attr(DMA_ATTR_ALLOC_EXACT_SIZE, attrs))
545 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
547 count = 1 << get_order(size);
549 bitmap_clear(mem->bitmap, pageno, count);
556 static int dma_release_from_coherent_heap_dev(struct device *dev, size_t len,
557 void *base, struct dma_attrs *attrs)
564 struct heap_info *h = NULL;
569 if (!dma_is_coherent_dev(dev))
572 h = dev_get_drvdata(dev);
576 if ((uintptr_t)base < h->cma_base ||
577 len > h->cma_chunk_size ||
578 (uintptr_t)base - h->cma_base > h->cma_len - len) {
583 dma_set_attr(DMA_ATTR_ALLOC_EXACT_SIZE, attrs);
585 mutex_lock(&h->resize_lock);
587 idx = div_u64((uintptr_t)base - h->cma_base, h->cma_chunk_size);
588 dev_dbg(&h->dev, "req free addr (%p) size (0x%zx) idx (%d)\n",
590 err = dma_release_from_coherent_dev(&h->dev, len, base, attrs);
596 get_first_and_last_idx(h, &first_alloc_idx, &last_alloc_idx);
598 /* Check if heap can be shrinked */
599 if (idx == first_alloc_idx || idx == last_alloc_idx) {
600 /* check if entire chunk is free */
601 chunk_size = (idx == h->num_chunks - 1) ? h->rem_chunk_size :
603 resize_err = dma_alloc_from_coherent_dev_at(&h->dev,
604 chunk_size, &dev_base, &ret, attrs,
605 idx * h->cma_chunk_size >> PAGE_SHIFT);
608 } else if (dev_base != h->cma_base + idx * h->cma_chunk_size) {
609 resize_err = dma_release_from_coherent_dev(
611 (void *)(uintptr_t)dev_base, attrs);
616 "prep to remove chunk b=0x%pa, s=0x%zx\n",
617 &dev_base, chunk_size);
618 resize_err = dma_release_from_coherent_dev(
620 (void *)(uintptr_t)dev_base, attrs);
623 dev_err(&h->dev, "failed to rel mem\n");
627 /* Handle VPR configuration updates */
628 if (h->update_resize_cfg) {
629 phys_addr_t new_base = h->curr_base;
630 size_t new_len = h->curr_len - chunk_size;
631 if (h->curr_base == dev_base)
632 new_base += chunk_size;
633 dev_dbg(&h->dev, "update vpr base to %pa, size=%zx\n",
636 h->update_resize_cfg(new_base, new_len);
639 "update resize failed\n");
644 if (h->curr_base == dev_base)
645 h->curr_base += chunk_size;
646 h->curr_len -= chunk_size;
647 update_alloc_range(h);
648 idx == first_alloc_idx ? ++idx : --idx;
649 release_from_contiguous_heap(h, dev_base, chunk_size);
650 dev_dbg(&h->dev, "removed chunk b=0x%pa, s=0x%zx"
651 " new heap b=0x%pa, s=0x%zx\n", &dev_base,
652 chunk_size, &h->curr_base, h->curr_len);
654 if (idx < h->num_chunks)
655 goto check_next_chunk;
658 mutex_unlock(&h->resize_lock);
662 void dma_release_declared_memory(struct device *dev)
664 struct dma_coherent_mem *mem = dev->dma_mem;
670 if (!(mem->flags & DMA_MEMORY_NOMAP))
671 iounmap(mem->virt_base);
676 EXPORT_SYMBOL(dma_release_declared_memory);
678 void *dma_mark_declared_memory_occupied(struct device *dev,
679 dma_addr_t device_addr, size_t size)
681 struct dma_coherent_mem *mem = dev->dma_mem;
684 size += device_addr & ~PAGE_MASK;
687 return ERR_PTR(-EINVAL);
689 pos = (device_addr - mem->device_base) >> PAGE_SHIFT;
690 err = bitmap_allocate_region(mem->bitmap, pos, get_order(size));
693 return mem->virt_base + (pos << PAGE_SHIFT);
695 EXPORT_SYMBOL(dma_mark_declared_memory_occupied);
698 * dma_alloc_from_coherent_attr() - try to allocate memory from the per-device
701 * @dev: device from which we allocate memory
702 * @size: size of requested memory area
703 * @dma_handle: This will be filled with the correct dma handle
704 * @ret: This pointer will be filled with the virtual address
706 * @attrs: DMA Attribute
707 * This function should be only called from per-arch dma_alloc_coherent()
708 * to support allocation from per-device coherent memory pools.
710 * Returns 0 if dma_alloc_coherent_attr should continue with allocating from
711 * generic memory areas, or !0 if dma_alloc_coherent should return @ret.
713 int dma_alloc_from_coherent_attr(struct device *dev, ssize_t size,
714 dma_addr_t *dma_handle, void **ret,
715 struct dma_attrs *attrs)
721 return dma_alloc_from_coherent_dev(dev, size, dma_handle, ret,
724 return dma_alloc_from_coherent_heap_dev(dev, size, dma_handle,
727 EXPORT_SYMBOL(dma_alloc_from_coherent_attr);
730 * dma_release_from_coherent_attr() - try to free the memory allocated from
731 * per-device coherent memory pool
732 * @dev: device from which the memory was allocated
733 * @size: size of the memory area to free
734 * @vaddr: virtual address of allocated pages
735 * @attrs: DMA Attribute
737 * This checks whether the memory was allocated from the per-device
738 * coherent memory pool and if so, releases that memory.
740 * Returns 1 if we correctly released the memory, or 0 if
741 * dma_release_coherent_attr() should proceed with releasing memory from
744 int dma_release_from_coherent_attr(struct device *dev, size_t size, void *vaddr,
745 struct dma_attrs *attrs)
751 return dma_release_from_coherent_dev(dev, size, vaddr, attrs);
753 return dma_release_from_coherent_heap_dev(dev, size, vaddr,
756 EXPORT_SYMBOL(dma_release_from_coherent_attr);
759 * dma_mmap_from_coherent() - try to mmap the memory allocated from
760 * per-device coherent memory pool to userspace
761 * @dev: device from which the memory was allocated
762 * @vma: vm_area for the userspace memory
763 * @vaddr: cpu address returned by dma_alloc_from_coherent
764 * @size: size of the memory buffer allocated by dma_alloc_from_coherent
765 * @ret: result from remap_pfn_range()
767 * This checks whether the memory was allocated from the per-device
768 * coherent memory pool and if so, maps that memory to the provided vma.
770 * Returns 1 if we correctly mapped the memory, or 0 if the caller should
771 * proceed with mapping memory from generic pools.
773 int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
774 void *vaddr, size_t size, int *ret)
776 struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
782 if (mem->flags & DMA_MEMORY_NOMAP)
783 mem_addr = (void *)(uintptr_t)mem->device_base;
785 mem_addr = mem->virt_base;
787 if (mem && vaddr >= mem_addr && vaddr + size <=
788 (mem_addr + (mem->size << PAGE_SHIFT))) {
789 unsigned long off = vma->vm_pgoff;
790 int start = (vaddr - mem_addr) >> PAGE_SHIFT;
791 int user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
792 int count = size >> PAGE_SHIFT;
795 if (off < count && user_count <= count - off) {
796 unsigned pfn = mem->pfn_base + start + off;
797 *ret = remap_pfn_range(vma, vma->vm_start, pfn,
798 user_count << PAGE_SHIFT,
805 EXPORT_SYMBOL(dma_mmap_from_coherent);