Lines Matching +full:page +full:- +full:size
1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/mm/dma-mapping.c
5 * Copyright (C) 2000-2004 Russell King
17 #include <linux/dma-direct.h>
18 #include <linux/dma-map-ops.h>
33 #include <asm/dma-iommu.h>
36 #include <asm/xen/xen-ops.h>
43 size_t size; member
53 size_t size; member
55 struct page *page; member
64 struct page **ret_page);
84 if (buf->virt == virt) { in arm_dma_buffer_find()
85 list_del(&buf->list); in arm_dma_buffer_find()
107 static void __dma_clear_buffer(struct page *page, size_t size, int coherent_flag) in __dma_clear_buffer() argument
111 * lurking in the kernel direct-mapped region is invalidated. in __dma_clear_buffer()
113 if (PageHighMem(page)) { in __dma_clear_buffer()
114 phys_addr_t base = __pfn_to_phys(page_to_pfn(page)); in __dma_clear_buffer()
115 phys_addr_t end = base + size; in __dma_clear_buffer()
116 while (size > 0) { in __dma_clear_buffer()
117 void *ptr = kmap_atomic(page); in __dma_clear_buffer()
122 page++; in __dma_clear_buffer()
123 size -= PAGE_SIZE; in __dma_clear_buffer()
128 void *ptr = page_address(page); in __dma_clear_buffer()
129 memset(ptr, 0, size); in __dma_clear_buffer()
131 dmac_flush_range(ptr, ptr + size); in __dma_clear_buffer()
132 outer_flush_range(__pa(ptr), __pa(ptr) + size); in __dma_clear_buffer()
138 * Allocate a DMA buffer for 'dev' of size 'size' using the
139 * specified gfp mask. Note that 'size' must be page aligned.
141 static struct page *__dma_alloc_buffer(struct device *dev, size_t size, in __dma_alloc_buffer() argument
144 unsigned long order = get_order(size); in __dma_alloc_buffer()
145 struct page *page, *p, *e; in __dma_alloc_buffer() local
147 page = alloc_pages(gfp, order); in __dma_alloc_buffer()
148 if (!page) in __dma_alloc_buffer()
152 * Now split the huge page and free the excess pages in __dma_alloc_buffer()
154 split_page(page, order); in __dma_alloc_buffer()
155 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++) in __dma_alloc_buffer()
158 __dma_clear_buffer(page, size, coherent_flag); in __dma_alloc_buffer()
160 return page; in __dma_alloc_buffer()
164 * Free a DMA buffer. 'size' must be page aligned.
166 static void __dma_free_buffer(struct page *page, size_t size) in __dma_free_buffer() argument
168 struct page *e = page + (size >> PAGE_SHIFT); in __dma_free_buffer()
170 while (page < e) { in __dma_free_buffer()
171 __free_page(page); in __dma_free_buffer()
172 page++; in __dma_free_buffer()
176 static void *__alloc_from_contiguous(struct device *dev, size_t size,
177 pgprot_t prot, struct page **ret_page,
181 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp,
182 pgprot_t prot, struct page **ret_page,
204 struct page *page; in atomic_pool_init() local
207 atomic_pool = gen_pool_create(PAGE_SHIFT, -1); in atomic_pool_init()
211 * The atomic pool is only used for non-coherent allocations in atomic_pool_init()
216 &page, atomic_pool_init, true, NORMAL, in atomic_pool_init()
220 &page, atomic_pool_init, true); in atomic_pool_init()
225 page_to_phys(page), in atomic_pool_init()
226 atomic_pool_size, -1); in atomic_pool_init()
244 return -ENOMEM; in atomic_pool_init()
254 unsigned long size; member
261 void __init dma_contiguous_early_fixup(phys_addr_t base, unsigned long size) in dma_contiguous_early_fixup() argument
264 dma_mmu_remap[dma_mmu_remap_num].size = size; in dma_contiguous_early_fixup()
273 phys_addr_t end = start + dma_mmu_remap[i].size; in dma_contiguous_remap()
284 map.length = end - start; in dma_contiguous_remap()
288 * Clear previous low-memory mapping to ensure that the in dma_contiguous_remap()
310 struct page *page = virt_to_page((void *)addr); in __dma_update_pte() local
313 set_pte_ext(pte, mk_pte(page, prot), 0); in __dma_update_pte()
317 static void __dma_remap(struct page *page, size_t size, pgprot_t prot) in __dma_remap() argument
319 unsigned long start = (unsigned long) page_address(page); in __dma_remap()
320 unsigned end = start + size; in __dma_remap()
322 apply_to_page_range(&init_mm, start, size, __dma_update_pte, &prot); in __dma_remap()
326 static void *__alloc_remap_buffer(struct device *dev, size_t size, gfp_t gfp, in __alloc_remap_buffer() argument
327 pgprot_t prot, struct page **ret_page, in __alloc_remap_buffer()
330 struct page *page; in __alloc_remap_buffer() local
334 * non-coherent in __alloc_remap_buffer()
336 page = __dma_alloc_buffer(dev, size, gfp, NORMAL); in __alloc_remap_buffer()
337 if (!page) in __alloc_remap_buffer()
342 ptr = dma_common_contiguous_remap(page, size, prot, caller); in __alloc_remap_buffer()
344 __dma_free_buffer(page, size); in __alloc_remap_buffer()
349 *ret_page = page; in __alloc_remap_buffer()
353 static void *__alloc_from_pool(size_t size, struct page **ret_page) in __alloc_from_pool() argument
363 val = gen_pool_alloc(atomic_pool, size); in __alloc_from_pool()
374 static bool __in_atomic_pool(void *start, size_t size) in __in_atomic_pool() argument
376 return gen_pool_has_addr(atomic_pool, (unsigned long)start, size); in __in_atomic_pool()
379 static int __free_from_pool(void *start, size_t size) in __free_from_pool() argument
381 if (!__in_atomic_pool(start, size)) in __free_from_pool()
384 gen_pool_free(atomic_pool, (unsigned long)start, size); in __free_from_pool()
389 static void *__alloc_from_contiguous(struct device *dev, size_t size, in __alloc_from_contiguous() argument
390 pgprot_t prot, struct page **ret_page, in __alloc_from_contiguous()
394 unsigned long order = get_order(size); in __alloc_from_contiguous()
395 size_t count = size >> PAGE_SHIFT; in __alloc_from_contiguous()
396 struct page *page; in __alloc_from_contiguous() local
399 page = dma_alloc_from_contiguous(dev, count, order, gfp & __GFP_NOWARN); in __alloc_from_contiguous()
400 if (!page) in __alloc_from_contiguous()
403 __dma_clear_buffer(page, size, coherent_flag); in __alloc_from_contiguous()
408 if (PageHighMem(page)) { in __alloc_from_contiguous()
409 ptr = dma_common_contiguous_remap(page, size, prot, caller); in __alloc_from_contiguous()
411 dma_release_from_contiguous(dev, page, count); in __alloc_from_contiguous()
415 __dma_remap(page, size, prot); in __alloc_from_contiguous()
416 ptr = page_address(page); in __alloc_from_contiguous()
420 *ret_page = page; in __alloc_from_contiguous()
424 static void __free_from_contiguous(struct device *dev, struct page *page, in __free_from_contiguous() argument
425 void *cpu_addr, size_t size, bool want_vaddr) in __free_from_contiguous() argument
428 if (PageHighMem(page)) in __free_from_contiguous()
429 dma_common_free_remap(cpu_addr, size); in __free_from_contiguous()
431 __dma_remap(page, size, PAGE_KERNEL); in __free_from_contiguous()
433 dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT); in __free_from_contiguous()
444 static void *__alloc_simple_buffer(struct device *dev, size_t size, gfp_t gfp, in __alloc_simple_buffer() argument
445 struct page **ret_page) in __alloc_simple_buffer()
447 struct page *page; in __alloc_simple_buffer() local
449 page = __dma_alloc_buffer(dev, size, gfp, COHERENT); in __alloc_simple_buffer()
450 if (!page) in __alloc_simple_buffer()
453 *ret_page = page; in __alloc_simple_buffer()
454 return page_address(page); in __alloc_simple_buffer()
458 struct page **ret_page) in simple_allocator_alloc()
460 return __alloc_simple_buffer(args->dev, args->size, args->gfp, in simple_allocator_alloc()
466 __dma_free_buffer(args->page, args->size); in simple_allocator_free()
475 struct page **ret_page) in cma_allocator_alloc()
477 return __alloc_from_contiguous(args->dev, args->size, args->prot, in cma_allocator_alloc()
478 ret_page, args->caller, in cma_allocator_alloc()
479 args->want_vaddr, args->coherent_flag, in cma_allocator_alloc()
480 args->gfp); in cma_allocator_alloc()
485 __free_from_contiguous(args->dev, args->page, args->cpu_addr, in cma_allocator_free()
486 args->size, args->want_vaddr); in cma_allocator_free()
495 struct page **ret_page) in pool_allocator_alloc()
497 return __alloc_from_pool(args->size, ret_page); in pool_allocator_alloc()
502 __free_from_pool(args->cpu_addr, args->size); in pool_allocator_free()
511 struct page **ret_page) in remap_allocator_alloc()
513 return __alloc_remap_buffer(args->dev, args->size, args->gfp, in remap_allocator_alloc()
514 args->prot, ret_page, args->caller, in remap_allocator_alloc()
515 args->want_vaddr); in remap_allocator_alloc()
520 if (args->want_vaddr) in remap_allocator_free()
521 dma_common_free_remap(args->cpu_addr, args->size); in remap_allocator_free()
523 __dma_free_buffer(args->page, args->size); in remap_allocator_free()
531 static void *__dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, in __dma_alloc() argument
535 u64 mask = min_not_zero(dev->coherent_dma_mask, dev->bus_dma_limit); in __dma_alloc()
536 struct page *page = NULL; in __dma_alloc() local
542 .size = PAGE_ALIGN(size), in __dma_alloc()
552 if (limit && size >= limit) { in __dma_alloc()
554 size, mask); in __dma_alloc()
568 * Following is a work-around (a.k.a. hack) to prevent pages in __dma_alloc()
582 buf->allocator = &cma_allocator; in __dma_alloc()
584 buf->allocator = &simple_allocator; in __dma_alloc()
586 buf->allocator = &remap_allocator; in __dma_alloc()
588 buf->allocator = &pool_allocator; in __dma_alloc()
590 addr = buf->allocator->alloc(&args, &page); in __dma_alloc()
592 if (page) { in __dma_alloc()
595 *handle = phys_to_dma(dev, page_to_phys(page)); in __dma_alloc()
596 buf->virt = args.want_vaddr ? addr : page; in __dma_alloc()
599 list_add(&buf->list, &arm_dma_bufs); in __dma_alloc()
605 return args.want_vaddr ? addr : page; in __dma_alloc()
611 static void __arm_dma_free(struct device *dev, size_t size, void *cpu_addr, in __arm_dma_free() argument
615 struct page *page = phys_to_page(dma_to_phys(dev, handle)); in __arm_dma_free() local
619 .size = PAGE_ALIGN(size), in __arm_dma_free()
621 .page = page, in __arm_dma_free()
629 buf->allocator->free(&args); in __arm_dma_free()
633 static void dma_cache_maint_page(struct page *page, unsigned long offset, in dma_cache_maint_page() argument
634 size_t size, enum dma_data_direction dir, in dma_cache_maint_page() argument
638 size_t left = size; in dma_cache_maint_page()
640 pfn = page_to_pfn(page) + offset / PAGE_SIZE; in dma_cache_maint_page()
653 page = pfn_to_page(pfn); in dma_cache_maint_page()
655 if (PageHighMem(page)) { in dma_cache_maint_page()
657 len = PAGE_SIZE - offset; in dma_cache_maint_page()
660 vaddr = kmap_atomic(page); in dma_cache_maint_page()
664 vaddr = kmap_high_get(page); in dma_cache_maint_page()
667 kunmap_high(page); in dma_cache_maint_page()
671 vaddr = page_address(page) + offset; in dma_cache_maint_page()
676 left -= len; in dma_cache_maint_page()
683 * Use the driver DMA support - see dma-mapping.h (dma_sync_*)
685 static void __dma_page_cpu_to_dev(struct page *page, unsigned long off, in __dma_page_cpu_to_dev() argument
686 size_t size, enum dma_data_direction dir) in __dma_page_cpu_to_dev() argument
690 dma_cache_maint_page(page, off, size, dir, dmac_map_area); in __dma_page_cpu_to_dev()
692 paddr = page_to_phys(page) + off; in __dma_page_cpu_to_dev()
694 outer_inv_range(paddr, paddr + size); in __dma_page_cpu_to_dev()
696 outer_clean_range(paddr, paddr + size); in __dma_page_cpu_to_dev()
698 /* FIXME: non-speculating: flush on bidirectional mappings? */ in __dma_page_cpu_to_dev()
701 static void __dma_page_dev_to_cpu(struct page *page, unsigned long off, in __dma_page_dev_to_cpu() argument
702 size_t size, enum dma_data_direction dir) in __dma_page_dev_to_cpu() argument
704 phys_addr_t paddr = page_to_phys(page) + off; in __dma_page_dev_to_cpu()
706 /* FIXME: non-speculating: not required */ in __dma_page_dev_to_cpu()
709 outer_inv_range(paddr, paddr + size); in __dma_page_dev_to_cpu()
711 dma_cache_maint_page(page, off, size, dir, dmac_unmap_area); in __dma_page_dev_to_cpu()
715 * Mark the D-cache clean for these pages to avoid extra flushing. in __dma_page_dev_to_cpu()
717 if (dir != DMA_TO_DEVICE && size >= PAGE_SIZE) { in __dma_page_dev_to_cpu()
719 size_t left = size; in __dma_page_dev_to_cpu()
721 pfn = page_to_pfn(page) + off / PAGE_SIZE; in __dma_page_dev_to_cpu()
725 left -= PAGE_SIZE - off; in __dma_page_dev_to_cpu()
728 page = pfn_to_page(pfn++); in __dma_page_dev_to_cpu()
729 set_bit(PG_dcache_clean, &page->flags); in __dma_page_dev_to_cpu()
730 left -= PAGE_SIZE; in __dma_page_dev_to_cpu()
761 size_t size) in __alloc_iova() argument
763 unsigned int order = get_order(size); in __alloc_iova()
766 size_t mapping_size = mapping->bits << PAGE_SHIFT; in __alloc_iova()
774 count = PAGE_ALIGN(size) >> PAGE_SHIFT; in __alloc_iova()
775 align = (1 << order) - 1; in __alloc_iova()
777 spin_lock_irqsave(&mapping->lock, flags); in __alloc_iova()
778 for (i = 0; i < mapping->nr_bitmaps; i++) { in __alloc_iova()
779 start = bitmap_find_next_zero_area(mapping->bitmaps[i], in __alloc_iova()
780 mapping->bits, 0, count, align); in __alloc_iova()
782 if (start > mapping->bits) in __alloc_iova()
785 bitmap_set(mapping->bitmaps[i], start, count); in __alloc_iova()
792 * address range of size bytes. in __alloc_iova()
794 if (i == mapping->nr_bitmaps) { in __alloc_iova()
796 spin_unlock_irqrestore(&mapping->lock, flags); in __alloc_iova()
800 start = bitmap_find_next_zero_area(mapping->bitmaps[i], in __alloc_iova()
801 mapping->bits, 0, count, align); in __alloc_iova()
803 if (start > mapping->bits) { in __alloc_iova()
804 spin_unlock_irqrestore(&mapping->lock, flags); in __alloc_iova()
808 bitmap_set(mapping->bitmaps[i], start, count); in __alloc_iova()
810 spin_unlock_irqrestore(&mapping->lock, flags); in __alloc_iova()
812 iova = mapping->base + (mapping_size * i); in __alloc_iova()
819 dma_addr_t addr, size_t size) in __free_iova() argument
822 size_t mapping_size = mapping->bits << PAGE_SHIFT; in __free_iova()
827 if (!size) in __free_iova()
830 bitmap_index = (u32) (addr - mapping->base) / (u32) mapping_size; in __free_iova()
831 BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions); in __free_iova()
833 bitmap_base = mapping->base + mapping_size * bitmap_index; in __free_iova()
835 start = (addr - bitmap_base) >> PAGE_SHIFT; in __free_iova()
837 if (addr + size > bitmap_base + mapping_size) { in __free_iova()
846 count = size >> PAGE_SHIFT; in __free_iova()
848 spin_lock_irqsave(&mapping->lock, flags); in __free_iova()
849 bitmap_clear(mapping->bitmaps[bitmap_index], start, count); in __free_iova()
850 spin_unlock_irqrestore(&mapping->lock, flags); in __free_iova()
856 static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, in __iommu_alloc_buffer() argument
860 struct page **pages; in __iommu_alloc_buffer()
861 int count = size >> PAGE_SHIFT; in __iommu_alloc_buffer()
862 int array_size = count * sizeof(struct page *); in __iommu_alloc_buffer()
875 unsigned long order = get_order(size); in __iommu_alloc_buffer()
876 struct page *page; in __iommu_alloc_buffer() local
878 page = dma_alloc_from_contiguous(dev, count, order, in __iommu_alloc_buffer()
880 if (!page) in __iommu_alloc_buffer()
883 __dma_clear_buffer(page, size, coherent_flag); in __iommu_alloc_buffer()
886 pages[i] = page + i; in __iommu_alloc_buffer()
893 order_idx = ARRAY_SIZE(iommu_order_array) - 1; in __iommu_alloc_buffer()
912 /* See if it's easy to allocate a high-order chunk */ in __iommu_alloc_buffer()
929 while (--j) in __iommu_alloc_buffer()
935 count -= 1 << order; in __iommu_alloc_buffer()
940 while (i--) in __iommu_alloc_buffer()
947 static int __iommu_free_buffer(struct device *dev, struct page **pages, in __iommu_free_buffer()
948 size_t size, unsigned long attrs) in __iommu_free_buffer() argument
950 int count = size >> PAGE_SHIFT; in __iommu_free_buffer()
969 __iommu_create_mapping(struct device *dev, struct page **pages, size_t size, in __iommu_create_mapping() argument
973 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; in __iommu_create_mapping()
977 dma_addr = __alloc_iova(mapping, size); in __iommu_create_mapping()
993 len = (j - i) << PAGE_SHIFT; in __iommu_create_mapping()
994 ret = iommu_map(mapping->domain, iova, phys, len, in __iommu_create_mapping()
1003 iommu_unmap(mapping->domain, dma_addr, iova-dma_addr); in __iommu_create_mapping()
1004 __free_iova(mapping, dma_addr, size); in __iommu_create_mapping()
1008 static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size) in __iommu_remove_mapping() argument
1013 * add optional in-page offset from iova to size and align in __iommu_remove_mapping()
1014 * result to page size in __iommu_remove_mapping()
1016 size = PAGE_ALIGN((iova & ~PAGE_MASK) + size); in __iommu_remove_mapping()
1019 iommu_unmap(mapping->domain, iova, size); in __iommu_remove_mapping()
1020 __free_iova(mapping, iova, size); in __iommu_remove_mapping()
1024 static struct page **__atomic_get_pages(void *addr) in __atomic_get_pages()
1026 struct page *page; in __atomic_get_pages() local
1030 page = phys_to_page(phys); in __atomic_get_pages()
1032 return (struct page **)page; in __atomic_get_pages()
1035 static struct page **__iommu_get_pages(void *cpu_addr, unsigned long attrs) in __iommu_get_pages()
1046 static void *__iommu_alloc_simple(struct device *dev, size_t size, gfp_t gfp, in __iommu_alloc_simple() argument
1050 struct page *page; in __iommu_alloc_simple() local
1054 addr = __alloc_simple_buffer(dev, size, gfp, &page); in __iommu_alloc_simple()
1056 addr = __alloc_from_pool(size, &page); in __iommu_alloc_simple()
1060 *handle = __iommu_create_mapping(dev, &page, size, attrs); in __iommu_alloc_simple()
1067 __free_from_pool(addr, size); in __iommu_alloc_simple()
1072 dma_addr_t handle, size_t size, int coherent_flag) in __iommu_free_atomic() argument
1074 __iommu_remove_mapping(dev, handle, size); in __iommu_free_atomic()
1076 __dma_free_buffer(virt_to_page(cpu_addr), size); in __iommu_free_atomic()
1078 __free_from_pool(cpu_addr, size); in __iommu_free_atomic()
1081 static void *arm_iommu_alloc_attrs(struct device *dev, size_t size, in arm_iommu_alloc_attrs() argument
1085 struct page **pages; in arm_iommu_alloc_attrs()
1087 int coherent_flag = dev->dma_coherent ? COHERENT : NORMAL; in arm_iommu_alloc_attrs()
1090 size = PAGE_ALIGN(size); in arm_iommu_alloc_attrs()
1093 return __iommu_alloc_simple(dev, size, gfp, handle, in arm_iommu_alloc_attrs()
1097 * Following is a work-around (a.k.a. hack) to prevent pages in arm_iommu_alloc_attrs()
1105 pages = __iommu_alloc_buffer(dev, size, gfp, attrs, coherent_flag); in arm_iommu_alloc_attrs()
1109 *handle = __iommu_create_mapping(dev, pages, size, attrs); in arm_iommu_alloc_attrs()
1116 addr = dma_common_pages_remap(pages, size, prot, in arm_iommu_alloc_attrs()
1124 __iommu_remove_mapping(dev, *handle, size); in arm_iommu_alloc_attrs()
1126 __iommu_free_buffer(dev, pages, size, attrs); in arm_iommu_alloc_attrs()
1131 void *cpu_addr, dma_addr_t dma_addr, size_t size, in arm_iommu_mmap_attrs() argument
1134 struct page **pages = __iommu_get_pages(cpu_addr, attrs); in arm_iommu_mmap_attrs()
1135 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT; in arm_iommu_mmap_attrs()
1139 return -ENXIO; in arm_iommu_mmap_attrs()
1141 if (vma->vm_pgoff >= nr_pages) in arm_iommu_mmap_attrs()
1142 return -ENXIO; in arm_iommu_mmap_attrs()
1144 if (!dev->dma_coherent) in arm_iommu_mmap_attrs()
1145 vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot); in arm_iommu_mmap_attrs()
1155 * free a page as defined by the above mapping.
1158 static void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr, in arm_iommu_free_attrs() argument
1161 int coherent_flag = dev->dma_coherent ? COHERENT : NORMAL; in arm_iommu_free_attrs()
1162 struct page **pages; in arm_iommu_free_attrs()
1163 size = PAGE_ALIGN(size); in arm_iommu_free_attrs()
1165 if (coherent_flag == COHERENT || __in_atomic_pool(cpu_addr, size)) { in arm_iommu_free_attrs()
1166 __iommu_free_atomic(dev, cpu_addr, handle, size, coherent_flag); in arm_iommu_free_attrs()
1177 dma_common_free_remap(cpu_addr, size); in arm_iommu_free_attrs()
1179 __iommu_remove_mapping(dev, handle, size); in arm_iommu_free_attrs()
1180 __iommu_free_buffer(dev, pages, size, attrs); in arm_iommu_free_attrs()
1185 size_t size, unsigned long attrs) in arm_iommu_get_sgtable() argument
1187 unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; in arm_iommu_get_sgtable()
1188 struct page **pages = __iommu_get_pages(cpu_addr, attrs); in arm_iommu_get_sgtable()
1191 return -ENXIO; in arm_iommu_get_sgtable()
1193 return sg_alloc_table_from_pages(sgt, pages, count, 0, size, in arm_iommu_get_sgtable()
1198 * Map a part of the scatter-gather list into contiguous io address space
1201 size_t size, dma_addr_t *handle, in __map_sg_chunk() argument
1211 size = PAGE_ALIGN(size); in __map_sg_chunk()
1214 iova_base = iova = __alloc_iova(mapping, size); in __map_sg_chunk()
1216 return -ENOMEM; in __map_sg_chunk()
1218 for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) { in __map_sg_chunk()
1220 unsigned int len = PAGE_ALIGN(s->offset + s->length); in __map_sg_chunk()
1222 if (!dev->dma_coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) in __map_sg_chunk()
1223 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); in __map_sg_chunk()
1227 ret = iommu_map(mapping->domain, iova, phys, len, prot); in __map_sg_chunk()
1237 iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE); in __map_sg_chunk()
1238 __free_iova(mapping, iova_base, size); in __map_sg_chunk()
1243 * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA
1259 unsigned int offset = s->offset; in arm_iommu_map_sg()
1260 unsigned int size = s->offset + s->length; in arm_iommu_map_sg() local
1266 s->dma_length = 0; in arm_iommu_map_sg()
1268 if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) { in arm_iommu_map_sg()
1269 ret = __map_sg_chunk(dev, start, size, in arm_iommu_map_sg()
1270 &dma->dma_address, dir, attrs); in arm_iommu_map_sg()
1274 dma->dma_address += offset; in arm_iommu_map_sg()
1275 dma->dma_length = size - offset; in arm_iommu_map_sg()
1277 size = offset = s->offset; in arm_iommu_map_sg()
1282 size += s->length; in arm_iommu_map_sg()
1284 ret = __map_sg_chunk(dev, start, size, &dma->dma_address, dir, attrs); in arm_iommu_map_sg()
1288 dma->dma_address += offset; in arm_iommu_map_sg()
1289 dma->dma_length = size - offset; in arm_iommu_map_sg()
1296 if (ret == -ENOMEM) in arm_iommu_map_sg()
1298 return -EINVAL; in arm_iommu_map_sg()
1302 * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg
1323 if (!dev->dma_coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) in arm_iommu_unmap_sg()
1324 __dma_page_dev_to_cpu(sg_page(s), s->offset, in arm_iommu_unmap_sg()
1325 s->length, dir); in arm_iommu_unmap_sg()
1343 if (dev->dma_coherent) in arm_iommu_sync_sg_for_cpu()
1347 __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir); in arm_iommu_sync_sg_for_cpu()
1365 if (dev->dma_coherent) in arm_iommu_sync_sg_for_device()
1369 __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); in arm_iommu_sync_sg_for_device()
1375 * @page: page that buffer resides in
1376 * @offset: offset into page for start of buffer
1377 * @size: size of buffer to map
1382 static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page, in arm_iommu_map_page() argument
1383 unsigned long offset, size_t size, enum dma_data_direction dir, in arm_iommu_map_page() argument
1388 int ret, prot, len = PAGE_ALIGN(size + offset); in arm_iommu_map_page()
1390 if (!dev->dma_coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) in arm_iommu_map_page()
1391 __dma_page_cpu_to_dev(page, offset, size, dir); in arm_iommu_map_page()
1399 ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, prot); in arm_iommu_map_page()
1413 * @size: size of buffer (same as passed to dma_map_page)
1419 size_t size, enum dma_data_direction dir, unsigned long attrs) in arm_iommu_unmap_page() argument
1423 struct page *page; in arm_iommu_unmap_page() local
1425 int len = PAGE_ALIGN(size + offset); in arm_iommu_unmap_page()
1430 if (!dev->dma_coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) { in arm_iommu_unmap_page()
1431 page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); in arm_iommu_unmap_page()
1432 __dma_page_dev_to_cpu(page, offset, size, dir); in arm_iommu_unmap_page()
1435 iommu_unmap(mapping->domain, iova, len); in arm_iommu_unmap_page()
1440 * arm_iommu_map_resource - map a device resource for DMA
1443 * @size: size of resource to map
1447 phys_addr_t phys_addr, size_t size, in arm_iommu_map_resource() argument
1455 size_t len = PAGE_ALIGN(size + offset); in arm_iommu_map_resource()
1463 ret = iommu_map(mapping->domain, dma_addr, addr, len, prot); in arm_iommu_map_resource()
1474 * arm_iommu_unmap_resource - unmap a device DMA resource
1477 * @size: size of resource to map
1481 size_t size, enum dma_data_direction dir, in arm_iommu_unmap_resource() argument
1487 size_t len = PAGE_ALIGN(size + offset); in arm_iommu_unmap_resource()
1492 iommu_unmap(mapping->domain, iova, len); in arm_iommu_unmap_resource()
1497 dma_addr_t handle, size_t size, enum dma_data_direction dir) in arm_iommu_sync_single_for_cpu() argument
1501 struct page *page; in arm_iommu_sync_single_for_cpu() local
1504 if (dev->dma_coherent || !iova) in arm_iommu_sync_single_for_cpu()
1507 page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); in arm_iommu_sync_single_for_cpu()
1508 __dma_page_dev_to_cpu(page, offset, size, dir); in arm_iommu_sync_single_for_cpu()
1512 dma_addr_t handle, size_t size, enum dma_data_direction dir) in arm_iommu_sync_single_for_device() argument
1516 struct page *page; in arm_iommu_sync_single_for_device() local
1519 if (dev->dma_coherent || !iova) in arm_iommu_sync_single_for_device()
1522 page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); in arm_iommu_sync_single_for_device()
1523 __dma_page_cpu_to_dev(page, offset, size, dir); in arm_iommu_sync_single_for_device()
1550 * @size: maximum size of the valid IO address space
1560 arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, u64 size) in arm_iommu_create_mapping() argument
1562 unsigned int bits = size >> PAGE_SHIFT; in arm_iommu_create_mapping()
1566 int err = -ENOMEM; in arm_iommu_create_mapping()
1568 /* currently only 32-bit DMA address space is supported */ in arm_iommu_create_mapping()
1569 if (size > DMA_BIT_MASK(32) + 1) in arm_iommu_create_mapping()
1570 return ERR_PTR(-ERANGE); in arm_iommu_create_mapping()
1573 return ERR_PTR(-EINVAL); in arm_iommu_create_mapping()
1584 mapping->bitmap_size = bitmap_size; in arm_iommu_create_mapping()
1585 mapping->bitmaps = kcalloc(extensions, sizeof(unsigned long *), in arm_iommu_create_mapping()
1587 if (!mapping->bitmaps) in arm_iommu_create_mapping()
1590 mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL); in arm_iommu_create_mapping()
1591 if (!mapping->bitmaps[0]) in arm_iommu_create_mapping()
1594 mapping->nr_bitmaps = 1; in arm_iommu_create_mapping()
1595 mapping->extensions = extensions; in arm_iommu_create_mapping()
1596 mapping->base = base; in arm_iommu_create_mapping()
1597 mapping->bits = BITS_PER_BYTE * bitmap_size; in arm_iommu_create_mapping()
1599 spin_lock_init(&mapping->lock); in arm_iommu_create_mapping()
1601 mapping->domain = iommu_domain_alloc(bus); in arm_iommu_create_mapping()
1602 if (!mapping->domain) in arm_iommu_create_mapping()
1605 kref_init(&mapping->kref); in arm_iommu_create_mapping()
1608 kfree(mapping->bitmaps[0]); in arm_iommu_create_mapping()
1610 kfree(mapping->bitmaps); in arm_iommu_create_mapping()
1624 iommu_domain_free(mapping->domain); in release_iommu_mapping()
1625 for (i = 0; i < mapping->nr_bitmaps; i++) in release_iommu_mapping()
1626 kfree(mapping->bitmaps[i]); in release_iommu_mapping()
1627 kfree(mapping->bitmaps); in release_iommu_mapping()
1635 if (mapping->nr_bitmaps >= mapping->extensions) in extend_iommu_mapping()
1636 return -EINVAL; in extend_iommu_mapping()
1638 next_bitmap = mapping->nr_bitmaps; in extend_iommu_mapping()
1639 mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size, in extend_iommu_mapping()
1641 if (!mapping->bitmaps[next_bitmap]) in extend_iommu_mapping()
1642 return -ENOMEM; in extend_iommu_mapping()
1644 mapping->nr_bitmaps++; in extend_iommu_mapping()
1652 kref_put(&mapping->kref, release_iommu_mapping); in arm_iommu_release_mapping()
1661 err = iommu_attach_device(mapping->domain, dev); in __arm_iommu_attach_device()
1665 kref_get(&mapping->kref); in __arm_iommu_attach_device()
1704 * This overwrites the dma_ops pointer with appropriate non-IOMMU ops.
1716 iommu_detach_device(mapping->domain, dev); in arm_iommu_detach_device()
1717 kref_put(&mapping->kref, release_iommu_mapping); in arm_iommu_detach_device()
1725 static void arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size, in arm_setup_iommu_dma_ops() argument
1730 mapping = arm_iommu_create_mapping(dev->bus, dma_base, size); in arm_setup_iommu_dma_ops()
1732 pr_warn("Failed to create %llu-byte IOMMU mapping for device %s\n", in arm_setup_iommu_dma_ops()
1733 size, dev_name(dev)); in arm_setup_iommu_dma_ops()
1760 static void arm_setup_iommu_dma_ops(struct device *dev, u64 dma_base, u64 size, in arm_setup_iommu_dma_ops() argument
1769 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, in arch_setup_dma_ops() argument
1773 * Due to legacy code that sets the ->dma_coherent flag from a bus in arch_setup_dma_ops()
1774 * notifier we can't just assign coherent to the ->dma_coherent flag in arch_setup_dma_ops()
1779 dev->dma_coherent = true; in arch_setup_dma_ops()
1786 if (dev->dma_ops) in arch_setup_dma_ops()
1790 arm_setup_iommu_dma_ops(dev, dma_base, size, iommu, coherent); in arch_setup_dma_ops()
1793 dev->archdata.dma_ops_setup = true; in arch_setup_dma_ops()
1798 if (!dev->archdata.dma_ops_setup) in arch_teardown_dma_ops()
1802 /* Let arch_setup_dma_ops() start again from scratch upon re-probe */ in arch_teardown_dma_ops()
1806 void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, in arch_sync_dma_for_device() argument
1809 __dma_page_cpu_to_dev(phys_to_page(paddr), paddr & (PAGE_SIZE - 1), in arch_sync_dma_for_device()
1810 size, dir); in arch_sync_dma_for_device()
1813 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, in arch_sync_dma_for_cpu() argument
1816 __dma_page_dev_to_cpu(phys_to_page(paddr), paddr & (PAGE_SIZE - 1), in arch_sync_dma_for_cpu()
1817 size, dir); in arch_sync_dma_for_cpu()
1820 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, in arch_dma_alloc() argument
1823 return __dma_alloc(dev, size, dma_handle, gfp, in arch_dma_alloc()
1828 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, in arch_dma_free() argument
1831 __arm_dma_free(dev, size, cpu_addr, dma_handle, attrs, false); in arch_dma_free()