Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ARM: dma-mapping: add support for IOMMU mapper

This patch add a complete implementation of DMA-mapping API for
devices which have IOMMU support.

This implementation tries to optimize dma address space usage by remapping
all possible physical memory chunks into a single dma address space chunk.

DMA address space is managed on top of the bitmap stored in the
dma_iommu_mapping structure stored in device->archdata. Platform setup
code has to initialize parameters of the dma address space (base address,
size, allocation precision order) with arm_iommu_create_mapping() function.
To reduce the size of the bitmap, all allocations are aligned to the
specified order of base 4 KiB pages.

dma_alloc_* functions allocate physical memory in chunks, each with
alloc_pages() function to avoid failing if the physical memory gets
fragmented. In worst case the allocated buffer is composed of 4 KiB page
chunks.

dma_map_sg() function minimizes the total number of dma address space
chunks by merging of physical memory chunks into one larger dma address
space chunk. If requested chunk (scatter list entry) boundaries
match physical page boundaries, most calls to dma_map_sg() requests will
result in creating only one chunk in dma address space.

dma_map_page() simply creates a mapping for the given page(s) in the dma
address space.

All dma functions also perform required cache operation like their
counterparts from the arm linear physical memory mapping version.

This patch contains code and fixes kindly provided by:
- Krishna Reddy <vdumpa@nvidia.com>,
- Andrzej Pietrasiewicz <andrzej.p@samsung.com>,
- Hiroshi DOYU <hdoyu@nvidia.com>

Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Acked-by: Kyungmin Park <kyungmin.park@samsung.com>
Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Tested-By: Subash Patel <subash.ramaswamy@linaro.org>

+747 -12
+8
arch/arm/Kconfig
··· 46 46 config ARM_HAS_SG_CHAIN 47 47 bool 48 48 49 + config NEED_SG_DMA_LENGTH 50 + bool 51 + 52 + config ARM_DMA_USE_IOMMU 53 + select NEED_SG_DMA_LENGTH 54 + select ARM_HAS_SG_CHAIN 55 + bool 56 + 49 57 config HAVE_PWM 50 58 bool 51 59
+3
arch/arm/include/asm/device.h
··· 14 14 #ifdef CONFIG_IOMMU_API 15 15 void *iommu; /* private IOMMU data */ 16 16 #endif 17 + #ifdef CONFIG_ARM_DMA_USE_IOMMU 18 + struct dma_iommu_mapping *mapping; 19 + #endif 17 20 }; 18 21 19 22 struct omap_device;
+34
arch/arm/include/asm/dma-iommu.h
··· 1 + #ifndef ASMARM_DMA_IOMMU_H 2 + #define ASMARM_DMA_IOMMU_H 3 + 4 + #ifdef __KERNEL__ 5 + 6 + #include <linux/mm_types.h> 7 + #include <linux/scatterlist.h> 8 + #include <linux/dma-debug.h> 9 + #include <linux/kmemcheck.h> 10 + 11 + struct dma_iommu_mapping { 12 + /* iommu specific data */ 13 + struct iommu_domain *domain; 14 + 15 + void *bitmap; 16 + size_t bits; 17 + unsigned int order; 18 + dma_addr_t base; 19 + 20 + spinlock_t lock; 21 + struct kref kref; 22 + }; 23 + 24 + struct dma_iommu_mapping * 25 + arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size, 26 + int order); 27 + 28 + void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping); 29 + 30 + int arm_iommu_attach_device(struct device *dev, 31 + struct dma_iommu_mapping *mapping); 32 + 33 + #endif /* __KERNEL__ */ 34 + #endif
+701 -11
arch/arm/mm/dma-mapping.c
··· 19 19 #include <linux/dma-mapping.h> 20 20 #include <linux/highmem.h> 21 21 #include <linux/slab.h> 22 + #include <linux/iommu.h> 23 + #include <linux/vmalloc.h> 22 24 23 25 #include <asm/memory.h> 24 26 #include <asm/highmem.h> ··· 28 26 #include <asm/tlbflush.h> 29 27 #include <asm/sizes.h> 30 28 #include <asm/mach/arch.h> 29 + #include <asm/dma-iommu.h> 31 30 32 31 #include "mm.h" 33 32 ··· 158 155 return mask; 159 156 } 160 157 158 + static void __dma_clear_buffer(struct page *page, size_t size) 159 + { 160 + void *ptr; 161 + /* 162 + * Ensure that the allocated pages are zeroed, and that any data 163 + * lurking in the kernel direct-mapped region is invalidated. 164 + */ 165 + ptr = page_address(page); 166 + if (ptr) { 167 + memset(ptr, 0, size); 168 + dmac_flush_range(ptr, ptr + size); 169 + outer_flush_range(__pa(ptr), __pa(ptr) + size); 170 + } 171 + } 172 + 161 173 /* 162 174 * Allocate a DMA buffer for 'dev' of size 'size' using the 163 175 * specified gfp mask. Note that 'size' must be page aligned. ··· 181 163 { 182 164 unsigned long order = get_order(size); 183 165 struct page *page, *p, *e; 184 - void *ptr; 185 166 u64 mask = get_coherent_dma_mask(dev); 186 167 187 168 #ifdef CONFIG_DMA_API_DEBUG ··· 209 192 for (p = page + (size >> PAGE_SHIFT), e = page + (1 << order); p < e; p++) 210 193 __free_page(p); 211 194 212 - /* 213 - * Ensure that the allocated pages are zeroed, and that any data 214 - * lurking in the kernel direct-mapped region is invalidated. 215 - */ 216 - ptr = page_address(page); 217 - memset(ptr, 0, size); 218 - dmac_flush_range(ptr, ptr + size); 219 - outer_flush_range(__pa(ptr), __pa(ptr) + size); 195 + __dma_clear_buffer(page, size); 220 196 221 197 return page; 222 198 } ··· 358 348 u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); 359 349 360 350 pte = consistent_pte[idx] + off; 361 - c->vm_pages = page; 351 + c->priv = page; 362 352 363 353 do { 364 354 BUG_ON(!pte_none(*pte)); ··· 519 509 c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr); 520 510 if (c) { 521 511 unsigned long off = vma->vm_pgoff; 512 + struct page *pages = c->priv; 522 513 523 514 kern_size = (c->vm_end - c->vm_start) >> PAGE_SHIFT; 524 515 525 516 if (off < kern_size && 526 517 user_size <= (kern_size - off)) { 527 518 ret = remap_pfn_range(vma, vma->vm_start, 528 - page_to_pfn(c->vm_pages) + off, 519 + page_to_pfn(pages) + off, 529 520 user_size << PAGE_SHIFT, 530 521 vma->vm_page_prot); 531 522 } ··· 665 654 int i, j; 666 655 667 656 for_each_sg(sg, s, nents, i) { 657 + #ifdef CONFIG_NEED_SG_DMA_LENGTH 658 + s->dma_length = s->length; 659 + #endif 668 660 s->dma_address = ops->map_page(dev, sg_page(s), s->offset, 669 661 s->length, dir, attrs); 670 662 if (dma_mapping_error(dev, s->dma_address)) ··· 776 762 return 0; 777 763 } 778 764 fs_initcall(dma_debug_do_init); 765 + 766 + #ifdef CONFIG_ARM_DMA_USE_IOMMU 767 + 768 + /* IOMMU */ 769 + 770 + static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping, 771 + size_t size) 772 + { 773 + unsigned int order = get_order(size); 774 + unsigned int align = 0; 775 + unsigned int count, start; 776 + unsigned long flags; 777 + 778 + count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) + 779 + (1 << mapping->order) - 1) >> mapping->order; 780 + 781 + if (order > mapping->order) 782 + align = (1 << (order - mapping->order)) - 1; 783 + 784 + spin_lock_irqsave(&mapping->lock, flags); 785 + start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0, 786 + count, align); 787 + if (start > mapping->bits) { 788 + spin_unlock_irqrestore(&mapping->lock, flags); 789 + return DMA_ERROR_CODE; 790 + } 791 + 792 + bitmap_set(mapping->bitmap, start, count); 793 + spin_unlock_irqrestore(&mapping->lock, flags); 794 + 795 + return mapping->base + (start << (mapping->order + PAGE_SHIFT)); 796 + } 797 + 798 + static inline void __free_iova(struct dma_iommu_mapping *mapping, 799 + dma_addr_t addr, size_t size) 800 + { 801 + unsigned int start = (addr - mapping->base) >> 802 + (mapping->order + PAGE_SHIFT); 803 + unsigned int count = ((size >> PAGE_SHIFT) + 804 + (1 << mapping->order) - 1) >> mapping->order; 805 + unsigned long flags; 806 + 807 + spin_lock_irqsave(&mapping->lock, flags); 808 + bitmap_clear(mapping->bitmap, start, count); 809 + spin_unlock_irqrestore(&mapping->lock, flags); 810 + } 811 + 812 + static struct page **__iommu_alloc_buffer(struct device *dev, size_t size, gfp_t gfp) 813 + { 814 + struct page **pages; 815 + int count = size >> PAGE_SHIFT; 816 + int array_size = count * sizeof(struct page *); 817 + int i = 0; 818 + 819 + if (array_size <= PAGE_SIZE) 820 + pages = kzalloc(array_size, gfp); 821 + else 822 + pages = vzalloc(array_size); 823 + if (!pages) 824 + return NULL; 825 + 826 + while (count) { 827 + int j, order = __ffs(count); 828 + 829 + pages[i] = alloc_pages(gfp | __GFP_NOWARN, order); 830 + while (!pages[i] && order) 831 + pages[i] = alloc_pages(gfp | __GFP_NOWARN, --order); 832 + if (!pages[i]) 833 + goto error; 834 + 835 + if (order) 836 + split_page(pages[i], order); 837 + j = 1 << order; 838 + while (--j) 839 + pages[i + j] = pages[i] + j; 840 + 841 + __dma_clear_buffer(pages[i], PAGE_SIZE << order); 842 + i += 1 << order; 843 + count -= 1 << order; 844 + } 845 + 846 + return pages; 847 + error: 848 + while (--i) 849 + if (pages[i]) 850 + __free_pages(pages[i], 0); 851 + if (array_size < PAGE_SIZE) 852 + kfree(pages); 853 + else 854 + vfree(pages); 855 + return NULL; 856 + } 857 + 858 + static int __iommu_free_buffer(struct device *dev, struct page **pages, size_t size) 859 + { 860 + int count = size >> PAGE_SHIFT; 861 + int array_size = count * sizeof(struct page *); 862 + int i; 863 + for (i = 0; i < count; i++) 864 + if (pages[i]) 865 + __free_pages(pages[i], 0); 866 + if (array_size < PAGE_SIZE) 867 + kfree(pages); 868 + else 869 + vfree(pages); 870 + return 0; 871 + } 872 + 873 + /* 874 + * Create a CPU mapping for a specified pages 875 + */ 876 + static void * 877 + __iommu_alloc_remap(struct page **pages, size_t size, gfp_t gfp, pgprot_t prot) 878 + { 879 + struct arm_vmregion *c; 880 + size_t align; 881 + size_t count = size >> PAGE_SHIFT; 882 + int bit; 883 + 884 + if (!consistent_pte[0]) { 885 + pr_err("%s: not initialised\n", __func__); 886 + dump_stack(); 887 + return NULL; 888 + } 889 + 890 + /* 891 + * Align the virtual region allocation - maximum alignment is 892 + * a section size, minimum is a page size. This helps reduce 893 + * fragmentation of the DMA space, and also prevents allocations 894 + * smaller than a section from crossing a section boundary. 895 + */ 896 + bit = fls(size - 1); 897 + if (bit > SECTION_SHIFT) 898 + bit = SECTION_SHIFT; 899 + align = 1 << bit; 900 + 901 + /* 902 + * Allocate a virtual address in the consistent mapping region. 903 + */ 904 + c = arm_vmregion_alloc(&consistent_head, align, size, 905 + gfp & ~(__GFP_DMA | __GFP_HIGHMEM), NULL); 906 + if (c) { 907 + pte_t *pte; 908 + int idx = CONSISTENT_PTE_INDEX(c->vm_start); 909 + int i = 0; 910 + u32 off = CONSISTENT_OFFSET(c->vm_start) & (PTRS_PER_PTE-1); 911 + 912 + pte = consistent_pte[idx] + off; 913 + c->priv = pages; 914 + 915 + do { 916 + BUG_ON(!pte_none(*pte)); 917 + 918 + set_pte_ext(pte, mk_pte(pages[i], prot), 0); 919 + pte++; 920 + off++; 921 + i++; 922 + if (off >= PTRS_PER_PTE) { 923 + off = 0; 924 + pte = consistent_pte[++idx]; 925 + } 926 + } while (i < count); 927 + 928 + dsb(); 929 + 930 + return (void *)c->vm_start; 931 + } 932 + return NULL; 933 + } 934 + 935 + /* 936 + * Create a mapping in device IO address space for specified pages 937 + */ 938 + static dma_addr_t 939 + __iommu_create_mapping(struct device *dev, struct page **pages, size_t size) 940 + { 941 + struct dma_iommu_mapping *mapping = dev->archdata.mapping; 942 + unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; 943 + dma_addr_t dma_addr, iova; 944 + int i, ret = DMA_ERROR_CODE; 945 + 946 + dma_addr = __alloc_iova(mapping, size); 947 + if (dma_addr == DMA_ERROR_CODE) 948 + return dma_addr; 949 + 950 + iova = dma_addr; 951 + for (i = 0; i < count; ) { 952 + unsigned int next_pfn = page_to_pfn(pages[i]) + 1; 953 + phys_addr_t phys = page_to_phys(pages[i]); 954 + unsigned int len, j; 955 + 956 + for (j = i + 1; j < count; j++, next_pfn++) 957 + if (page_to_pfn(pages[j]) != next_pfn) 958 + break; 959 + 960 + len = (j - i) << PAGE_SHIFT; 961 + ret = iommu_map(mapping->domain, iova, phys, len, 0); 962 + if (ret < 0) 963 + goto fail; 964 + iova += len; 965 + i = j; 966 + } 967 + return dma_addr; 968 + fail: 969 + iommu_unmap(mapping->domain, dma_addr, iova-dma_addr); 970 + __free_iova(mapping, dma_addr, size); 971 + return DMA_ERROR_CODE; 972 + } 973 + 974 + static int __iommu_remove_mapping(struct device *dev, dma_addr_t iova, size_t size) 975 + { 976 + struct dma_iommu_mapping *mapping = dev->archdata.mapping; 977 + 978 + /* 979 + * add optional in-page offset from iova to size and align 980 + * result to page size 981 + */ 982 + size = PAGE_ALIGN((iova & ~PAGE_MASK) + size); 983 + iova &= PAGE_MASK; 984 + 985 + iommu_unmap(mapping->domain, iova, size); 986 + __free_iova(mapping, iova, size); 987 + return 0; 988 + } 989 + 990 + static void *arm_iommu_alloc_attrs(struct device *dev, size_t size, 991 + dma_addr_t *handle, gfp_t gfp, struct dma_attrs *attrs) 992 + { 993 + pgprot_t prot = __get_dma_pgprot(attrs, pgprot_kernel); 994 + struct page **pages; 995 + void *addr = NULL; 996 + 997 + *handle = DMA_ERROR_CODE; 998 + size = PAGE_ALIGN(size); 999 + 1000 + pages = __iommu_alloc_buffer(dev, size, gfp); 1001 + if (!pages) 1002 + return NULL; 1003 + 1004 + *handle = __iommu_create_mapping(dev, pages, size); 1005 + if (*handle == DMA_ERROR_CODE) 1006 + goto err_buffer; 1007 + 1008 + addr = __iommu_alloc_remap(pages, size, gfp, prot); 1009 + if (!addr) 1010 + goto err_mapping; 1011 + 1012 + return addr; 1013 + 1014 + err_mapping: 1015 + __iommu_remove_mapping(dev, *handle, size); 1016 + err_buffer: 1017 + __iommu_free_buffer(dev, pages, size); 1018 + return NULL; 1019 + } 1020 + 1021 + static int arm_iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma, 1022 + void *cpu_addr, dma_addr_t dma_addr, size_t size, 1023 + struct dma_attrs *attrs) 1024 + { 1025 + struct arm_vmregion *c; 1026 + 1027 + vma->vm_page_prot = __get_dma_pgprot(attrs, vma->vm_page_prot); 1028 + c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr); 1029 + 1030 + if (c) { 1031 + struct page **pages = c->priv; 1032 + 1033 + unsigned long uaddr = vma->vm_start; 1034 + unsigned long usize = vma->vm_end - vma->vm_start; 1035 + int i = 0; 1036 + 1037 + do { 1038 + int ret; 1039 + 1040 + ret = vm_insert_page(vma, uaddr, pages[i++]); 1041 + if (ret) { 1042 + pr_err("Remapping memory, error: %d\n", ret); 1043 + return ret; 1044 + } 1045 + 1046 + uaddr += PAGE_SIZE; 1047 + usize -= PAGE_SIZE; 1048 + } while (usize > 0); 1049 + } 1050 + return 0; 1051 + } 1052 + 1053 + /* 1054 + * free a page as defined by the above mapping. 1055 + * Must not be called with IRQs disabled. 1056 + */ 1057 + void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr, 1058 + dma_addr_t handle, struct dma_attrs *attrs) 1059 + { 1060 + struct arm_vmregion *c; 1061 + size = PAGE_ALIGN(size); 1062 + 1063 + c = arm_vmregion_find(&consistent_head, (unsigned long)cpu_addr); 1064 + if (c) { 1065 + struct page **pages = c->priv; 1066 + __dma_free_remap(cpu_addr, size); 1067 + __iommu_remove_mapping(dev, handle, size); 1068 + __iommu_free_buffer(dev, pages, size); 1069 + } 1070 + } 1071 + 1072 + /* 1073 + * Map a part of the scatter-gather list into contiguous io address space 1074 + */ 1075 + static int __map_sg_chunk(struct device *dev, struct scatterlist *sg, 1076 + size_t size, dma_addr_t *handle, 1077 + enum dma_data_direction dir) 1078 + { 1079 + struct dma_iommu_mapping *mapping = dev->archdata.mapping; 1080 + dma_addr_t iova, iova_base; 1081 + int ret = 0; 1082 + unsigned int count; 1083 + struct scatterlist *s; 1084 + 1085 + size = PAGE_ALIGN(size); 1086 + *handle = DMA_ERROR_CODE; 1087 + 1088 + iova_base = iova = __alloc_iova(mapping, size); 1089 + if (iova == DMA_ERROR_CODE) 1090 + return -ENOMEM; 1091 + 1092 + for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) { 1093 + phys_addr_t phys = page_to_phys(sg_page(s)); 1094 + unsigned int len = PAGE_ALIGN(s->offset + s->length); 1095 + 1096 + if (!arch_is_coherent()) 1097 + __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); 1098 + 1099 + ret = iommu_map(mapping->domain, iova, phys, len, 0); 1100 + if (ret < 0) 1101 + goto fail; 1102 + count += len >> PAGE_SHIFT; 1103 + iova += len; 1104 + } 1105 + *handle = iova_base; 1106 + 1107 + return 0; 1108 + fail: 1109 + iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE); 1110 + __free_iova(mapping, iova_base, size); 1111 + return ret; 1112 + } 1113 + 1114 + /** 1115 + * arm_iommu_map_sg - map a set of SG buffers for streaming mode DMA 1116 + * @dev: valid struct device pointer 1117 + * @sg: list of buffers 1118 + * @nents: number of buffers to map 1119 + * @dir: DMA transfer direction 1120 + * 1121 + * Map a set of buffers described by scatterlist in streaming mode for DMA. 1122 + * The scatter gather list elements are merged together (if possible) and 1123 + * tagged with the appropriate dma address and length. They are obtained via 1124 + * sg_dma_{address,length}. 1125 + */ 1126 + int arm_iommu_map_sg(struct device *dev, struct scatterlist *sg, int nents, 1127 + enum dma_data_direction dir, struct dma_attrs *attrs) 1128 + { 1129 + struct scatterlist *s = sg, *dma = sg, *start = sg; 1130 + int i, count = 0; 1131 + unsigned int offset = s->offset; 1132 + unsigned int size = s->offset + s->length; 1133 + unsigned int max = dma_get_max_seg_size(dev); 1134 + 1135 + for (i = 1; i < nents; i++) { 1136 + s = sg_next(s); 1137 + 1138 + s->dma_address = DMA_ERROR_CODE; 1139 + s->dma_length = 0; 1140 + 1141 + if (s->offset || (size & ~PAGE_MASK) || size + s->length > max) { 1142 + if (__map_sg_chunk(dev, start, size, &dma->dma_address, 1143 + dir) < 0) 1144 + goto bad_mapping; 1145 + 1146 + dma->dma_address += offset; 1147 + dma->dma_length = size - offset; 1148 + 1149 + size = offset = s->offset; 1150 + start = s; 1151 + dma = sg_next(dma); 1152 + count += 1; 1153 + } 1154 + size += s->length; 1155 + } 1156 + if (__map_sg_chunk(dev, start, size, &dma->dma_address, dir) < 0) 1157 + goto bad_mapping; 1158 + 1159 + dma->dma_address += offset; 1160 + dma->dma_length = size - offset; 1161 + 1162 + return count+1; 1163 + 1164 + bad_mapping: 1165 + for_each_sg(sg, s, count, i) 1166 + __iommu_remove_mapping(dev, sg_dma_address(s), sg_dma_len(s)); 1167 + return 0; 1168 + } 1169 + 1170 + /** 1171 + * arm_iommu_unmap_sg - unmap a set of SG buffers mapped by dma_map_sg 1172 + * @dev: valid struct device pointer 1173 + * @sg: list of buffers 1174 + * @nents: number of buffers to unmap (same as was passed to dma_map_sg) 1175 + * @dir: DMA transfer direction (same as was passed to dma_map_sg) 1176 + * 1177 + * Unmap a set of streaming mode DMA translations. Again, CPU access 1178 + * rules concerning calls here are the same as for dma_unmap_single(). 1179 + */ 1180 + void arm_iommu_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, 1181 + enum dma_data_direction dir, struct dma_attrs *attrs) 1182 + { 1183 + struct scatterlist *s; 1184 + int i; 1185 + 1186 + for_each_sg(sg, s, nents, i) { 1187 + if (sg_dma_len(s)) 1188 + __iommu_remove_mapping(dev, sg_dma_address(s), 1189 + sg_dma_len(s)); 1190 + if (!arch_is_coherent()) 1191 + __dma_page_dev_to_cpu(sg_page(s), s->offset, 1192 + s->length, dir); 1193 + } 1194 + } 1195 + 1196 + /** 1197 + * arm_iommu_sync_sg_for_cpu 1198 + * @dev: valid struct device pointer 1199 + * @sg: list of buffers 1200 + * @nents: number of buffers to map (returned from dma_map_sg) 1201 + * @dir: DMA transfer direction (same as was passed to dma_map_sg) 1202 + */ 1203 + void arm_iommu_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, 1204 + int nents, enum dma_data_direction dir) 1205 + { 1206 + struct scatterlist *s; 1207 + int i; 1208 + 1209 + for_each_sg(sg, s, nents, i) 1210 + if (!arch_is_coherent()) 1211 + __dma_page_dev_to_cpu(sg_page(s), s->offset, s->length, dir); 1212 + 1213 + } 1214 + 1215 + /** 1216 + * arm_iommu_sync_sg_for_device 1217 + * @dev: valid struct device pointer 1218 + * @sg: list of buffers 1219 + * @nents: number of buffers to map (returned from dma_map_sg) 1220 + * @dir: DMA transfer direction (same as was passed to dma_map_sg) 1221 + */ 1222 + void arm_iommu_sync_sg_for_device(struct device *dev, struct scatterlist *sg, 1223 + int nents, enum dma_data_direction dir) 1224 + { 1225 + struct scatterlist *s; 1226 + int i; 1227 + 1228 + for_each_sg(sg, s, nents, i) 1229 + if (!arch_is_coherent()) 1230 + __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); 1231 + } 1232 + 1233 + 1234 + /** 1235 + * arm_iommu_map_page 1236 + * @dev: valid struct device pointer 1237 + * @page: page that buffer resides in 1238 + * @offset: offset into page for start of buffer 1239 + * @size: size of buffer to map 1240 + * @dir: DMA transfer direction 1241 + * 1242 + * IOMMU aware version of arm_dma_map_page() 1243 + */ 1244 + static dma_addr_t arm_iommu_map_page(struct device *dev, struct page *page, 1245 + unsigned long offset, size_t size, enum dma_data_direction dir, 1246 + struct dma_attrs *attrs) 1247 + { 1248 + struct dma_iommu_mapping *mapping = dev->archdata.mapping; 1249 + dma_addr_t dma_addr; 1250 + int ret, len = PAGE_ALIGN(size + offset); 1251 + 1252 + if (!arch_is_coherent()) 1253 + __dma_page_cpu_to_dev(page, offset, size, dir); 1254 + 1255 + dma_addr = __alloc_iova(mapping, len); 1256 + if (dma_addr == DMA_ERROR_CODE) 1257 + return dma_addr; 1258 + 1259 + ret = iommu_map(mapping->domain, dma_addr, page_to_phys(page), len, 0); 1260 + if (ret < 0) 1261 + goto fail; 1262 + 1263 + return dma_addr + offset; 1264 + fail: 1265 + __free_iova(mapping, dma_addr, len); 1266 + return DMA_ERROR_CODE; 1267 + } 1268 + 1269 + /** 1270 + * arm_iommu_unmap_page 1271 + * @dev: valid struct device pointer 1272 + * @handle: DMA address of buffer 1273 + * @size: size of buffer (same as passed to dma_map_page) 1274 + * @dir: DMA transfer direction (same as passed to dma_map_page) 1275 + * 1276 + * IOMMU aware version of arm_dma_unmap_page() 1277 + */ 1278 + static void arm_iommu_unmap_page(struct device *dev, dma_addr_t handle, 1279 + size_t size, enum dma_data_direction dir, 1280 + struct dma_attrs *attrs) 1281 + { 1282 + struct dma_iommu_mapping *mapping = dev->archdata.mapping; 1283 + dma_addr_t iova = handle & PAGE_MASK; 1284 + struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); 1285 + int offset = handle & ~PAGE_MASK; 1286 + int len = PAGE_ALIGN(size + offset); 1287 + 1288 + if (!iova) 1289 + return; 1290 + 1291 + if (!arch_is_coherent()) 1292 + __dma_page_dev_to_cpu(page, offset, size, dir); 1293 + 1294 + iommu_unmap(mapping->domain, iova, len); 1295 + __free_iova(mapping, iova, len); 1296 + } 1297 + 1298 + static void arm_iommu_sync_single_for_cpu(struct device *dev, 1299 + dma_addr_t handle, size_t size, enum dma_data_direction dir) 1300 + { 1301 + struct dma_iommu_mapping *mapping = dev->archdata.mapping; 1302 + dma_addr_t iova = handle & PAGE_MASK; 1303 + struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); 1304 + unsigned int offset = handle & ~PAGE_MASK; 1305 + 1306 + if (!iova) 1307 + return; 1308 + 1309 + if (!arch_is_coherent()) 1310 + __dma_page_dev_to_cpu(page, offset, size, dir); 1311 + } 1312 + 1313 + static void arm_iommu_sync_single_for_device(struct device *dev, 1314 + dma_addr_t handle, size_t size, enum dma_data_direction dir) 1315 + { 1316 + struct dma_iommu_mapping *mapping = dev->archdata.mapping; 1317 + dma_addr_t iova = handle & PAGE_MASK; 1318 + struct page *page = phys_to_page(iommu_iova_to_phys(mapping->domain, iova)); 1319 + unsigned int offset = handle & ~PAGE_MASK; 1320 + 1321 + if (!iova) 1322 + return; 1323 + 1324 + __dma_page_cpu_to_dev(page, offset, size, dir); 1325 + } 1326 + 1327 + struct dma_map_ops iommu_ops = { 1328 + .alloc = arm_iommu_alloc_attrs, 1329 + .free = arm_iommu_free_attrs, 1330 + .mmap = arm_iommu_mmap_attrs, 1331 + 1332 + .map_page = arm_iommu_map_page, 1333 + .unmap_page = arm_iommu_unmap_page, 1334 + .sync_single_for_cpu = arm_iommu_sync_single_for_cpu, 1335 + .sync_single_for_device = arm_iommu_sync_single_for_device, 1336 + 1337 + .map_sg = arm_iommu_map_sg, 1338 + .unmap_sg = arm_iommu_unmap_sg, 1339 + .sync_sg_for_cpu = arm_iommu_sync_sg_for_cpu, 1340 + .sync_sg_for_device = arm_iommu_sync_sg_for_device, 1341 + }; 1342 + 1343 + /** 1344 + * arm_iommu_create_mapping 1345 + * @bus: pointer to the bus holding the client device (for IOMMU calls) 1346 + * @base: start address of the valid IO address space 1347 + * @size: size of the valid IO address space 1348 + * @order: accuracy of the IO addresses allocations 1349 + * 1350 + * Creates a mapping structure which holds information about used/unused 1351 + * IO address ranges, which is required to perform memory allocation and 1352 + * mapping with IOMMU aware functions. 1353 + * 1354 + * The client device need to be attached to the mapping with 1355 + * arm_iommu_attach_device function. 1356 + */ 1357 + struct dma_iommu_mapping * 1358 + arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size, 1359 + int order) 1360 + { 1361 + unsigned int count = size >> (PAGE_SHIFT + order); 1362 + unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long); 1363 + struct dma_iommu_mapping *mapping; 1364 + int err = -ENOMEM; 1365 + 1366 + if (!count) 1367 + return ERR_PTR(-EINVAL); 1368 + 1369 + mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL); 1370 + if (!mapping) 1371 + goto err; 1372 + 1373 + mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL); 1374 + if (!mapping->bitmap) 1375 + goto err2; 1376 + 1377 + mapping->base = base; 1378 + mapping->bits = BITS_PER_BYTE * bitmap_size; 1379 + mapping->order = order; 1380 + spin_lock_init(&mapping->lock); 1381 + 1382 + mapping->domain = iommu_domain_alloc(bus); 1383 + if (!mapping->domain) 1384 + goto err3; 1385 + 1386 + kref_init(&mapping->kref); 1387 + return mapping; 1388 + err3: 1389 + kfree(mapping->bitmap); 1390 + err2: 1391 + kfree(mapping); 1392 + err: 1393 + return ERR_PTR(err); 1394 + } 1395 + 1396 + static void release_iommu_mapping(struct kref *kref) 1397 + { 1398 + struct dma_iommu_mapping *mapping = 1399 + container_of(kref, struct dma_iommu_mapping, kref); 1400 + 1401 + iommu_domain_free(mapping->domain); 1402 + kfree(mapping->bitmap); 1403 + kfree(mapping); 1404 + } 1405 + 1406 + void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping) 1407 + { 1408 + if (mapping) 1409 + kref_put(&mapping->kref, release_iommu_mapping); 1410 + } 1411 + 1412 + /** 1413 + * arm_iommu_attach_device 1414 + * @dev: valid struct device pointer 1415 + * @mapping: io address space mapping structure (returned from 1416 + * arm_iommu_create_mapping) 1417 + * 1418 + * Attaches specified io address space mapping to the provided device, 1419 + * this replaces the dma operations (dma_map_ops pointer) with the 1420 + * IOMMU aware version. More than one client might be attached to 1421 + * the same io address space mapping. 1422 + */ 1423 + int arm_iommu_attach_device(struct device *dev, 1424 + struct dma_iommu_mapping *mapping) 1425 + { 1426 + int err; 1427 + 1428 + err = iommu_attach_device(mapping->domain, dev); 1429 + if (err) 1430 + return err; 1431 + 1432 + kref_get(&mapping->kref); 1433 + dev->archdata.mapping = mapping; 1434 + set_dma_ops(dev, &iommu_ops); 1435 + 1436 + pr_info("Attached IOMMU controller to %s device.\n", dev_name(dev)); 1437 + return 0; 1438 + } 1439 + 1440 + #endif
+1 -1
arch/arm/mm/vmregion.h
··· 17 17 struct list_head vm_list; 18 18 unsigned long vm_start; 19 19 unsigned long vm_end; 20 - struct page *vm_pages; 20 + void *priv; 21 21 int vm_active; 22 22 const void *caller; 23 23 };