arm64: Implement custom mmap functions for dma mapping
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / mm / dma-mapping.c
1 /*
2  * SWIOTLB-based DMA API implementation
3  *
4  * Copyright (C) 2012 ARM Ltd.
5  * Author: Catalin Marinas <catalin.marinas@arm.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <linux/gfp.h>
21 #include <linux/export.h>
22 #include <linux/slab.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/dma-contiguous.h>
25 #include <linux/vmalloc.h>
26 #include <linux/swiotlb.h>
27
28 #include <asm/cacheflush.h>
29
30 struct dma_map_ops *dma_ops;
31 EXPORT_SYMBOL(dma_ops);
32
33 static void *__dma_alloc_coherent(struct device *dev, size_t size,
34                                   dma_addr_t *dma_handle, gfp_t flags,
35                                   struct dma_attrs *attrs)
36 {
37         if (IS_ENABLED(CONFIG_ZONE_DMA) &&
38             dev->coherent_dma_mask <= DMA_BIT_MASK(32))
39                 flags |= GFP_DMA;
40         if (IS_ENABLED(CONFIG_DMA_CMA)) {
41                 struct page *page;
42
43                 size = PAGE_ALIGN(size);
44                 page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
45                                                         get_order(size));
46                 if (!page)
47                         return NULL;
48
49                 *dma_handle = phys_to_dma(dev, page_to_phys(page));
50                 return page_address(page);
51         } else {
52                 return swiotlb_alloc_coherent(dev, size, dma_handle, flags);
53         }
54 }
55
56 static void __dma_free_coherent(struct device *dev, size_t size,
57                                 void *vaddr, dma_addr_t dma_handle,
58                                 struct dma_attrs *attrs)
59 {
60         if (dev == NULL) {
61                 WARN_ONCE(1, "Use an actual device structure for DMA allocation\n");
62                 return;
63         }
64
65         if (IS_ENABLED(CONFIG_DMA_CMA)) {
66                 phys_addr_t paddr = dma_to_phys(dev, dma_handle);
67
68                 dma_release_from_contiguous(dev,
69                                         phys_to_page(paddr),
70                                         size >> PAGE_SHIFT);
71         } else {
72                 swiotlb_free_coherent(dev, size, vaddr, dma_handle);
73         }
74 }
75
76 static void *__dma_alloc_noncoherent(struct device *dev, size_t size,
77                                      dma_addr_t *dma_handle, gfp_t flags,
78                                      struct dma_attrs *attrs)
79 {
80         struct page *page, **map;
81         void *ptr, *coherent_ptr;
82         int order, i;
83
84         size = PAGE_ALIGN(size);
85         order = get_order(size);
86
87         ptr = __dma_alloc_coherent(dev, size, dma_handle, flags, attrs);
88         if (!ptr)
89                 goto no_mem;
90         map = kmalloc(sizeof(struct page *) << order, flags & ~GFP_DMA);
91         if (!map)
92                 goto no_map;
93
94         /* remove any dirty cache lines on the kernel alias */
95         __dma_flush_range(ptr, ptr + size);
96
97         /* create a coherent mapping */
98         page = virt_to_page(ptr);
99         for (i = 0; i < (size >> PAGE_SHIFT); i++)
100                 map[i] = page + i;
101         coherent_ptr = vmap(map, size >> PAGE_SHIFT, VM_MAP,
102                             pgprot_dmacoherent(pgprot_default));
103         kfree(map);
104         if (!coherent_ptr)
105                 goto no_map;
106
107         return coherent_ptr;
108
109 no_map:
110         __dma_free_coherent(dev, size, ptr, *dma_handle, attrs);
111 no_mem:
112         *dma_handle = ~0;
113         return NULL;
114 }
115
116 static void __dma_free_noncoherent(struct device *dev, size_t size,
117                                    void *vaddr, dma_addr_t dma_handle,
118                                    struct dma_attrs *attrs)
119 {
120         void *swiotlb_addr = phys_to_virt(dma_to_phys(dev, dma_handle));
121
122         vunmap(vaddr);
123         __dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
124 }
125
126 static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
127                                      unsigned long offset, size_t size,
128                                      enum dma_data_direction dir,
129                                      struct dma_attrs *attrs)
130 {
131         dma_addr_t dev_addr;
132
133         dev_addr = swiotlb_map_page(dev, page, offset, size, dir, attrs);
134         __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
135
136         return dev_addr;
137 }
138
139
140 static void __swiotlb_unmap_page(struct device *dev, dma_addr_t dev_addr,
141                                  size_t size, enum dma_data_direction dir,
142                                  struct dma_attrs *attrs)
143 {
144         __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
145         swiotlb_unmap_page(dev, dev_addr, size, dir, attrs);
146 }
147
148 static int __swiotlb_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
149                                   int nelems, enum dma_data_direction dir,
150                                   struct dma_attrs *attrs)
151 {
152         struct scatterlist *sg;
153         int i, ret;
154
155         ret = swiotlb_map_sg_attrs(dev, sgl, nelems, dir, attrs);
156         for_each_sg(sgl, sg, ret, i)
157                 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
158                                sg->length, dir);
159
160         return ret;
161 }
162
163 static void __swiotlb_unmap_sg_attrs(struct device *dev,
164                                      struct scatterlist *sgl, int nelems,
165                                      enum dma_data_direction dir,
166                                      struct dma_attrs *attrs)
167 {
168         struct scatterlist *sg;
169         int i;
170
171         for_each_sg(sgl, sg, nelems, i)
172                 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
173                                  sg->length, dir);
174         swiotlb_unmap_sg_attrs(dev, sgl, nelems, dir, attrs);
175 }
176
177 static void __swiotlb_sync_single_for_cpu(struct device *dev,
178                                           dma_addr_t dev_addr, size_t size,
179                                           enum dma_data_direction dir)
180 {
181         __dma_unmap_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
182         swiotlb_sync_single_for_cpu(dev, dev_addr, size, dir);
183 }
184
185 static void __swiotlb_sync_single_for_device(struct device *dev,
186                                              dma_addr_t dev_addr, size_t size,
187                                              enum dma_data_direction dir)
188 {
189         swiotlb_sync_single_for_device(dev, dev_addr, size, dir);
190         __dma_map_area(phys_to_virt(dma_to_phys(dev, dev_addr)), size, dir);
191 }
192
193 static void __swiotlb_sync_sg_for_cpu(struct device *dev,
194                                       struct scatterlist *sgl, int nelems,
195                                       enum dma_data_direction dir)
196 {
197         struct scatterlist *sg;
198         int i;
199
200         for_each_sg(sgl, sg, nelems, i)
201                 __dma_unmap_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
202                                  sg->length, dir);
203         swiotlb_sync_sg_for_cpu(dev, sgl, nelems, dir);
204 }
205
206 static void __swiotlb_sync_sg_for_device(struct device *dev,
207                                          struct scatterlist *sgl, int nelems,
208                                          enum dma_data_direction dir)
209 {
210         struct scatterlist *sg;
211         int i;
212
213         swiotlb_sync_sg_for_device(dev, sgl, nelems, dir);
214         for_each_sg(sgl, sg, nelems, i)
215                 __dma_map_area(phys_to_virt(dma_to_phys(dev, sg->dma_address)),
216                                sg->length, dir);
217 }
218
219 /* vma->vm_page_prot must be set appropriately before calling this function */
220 static int __dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
221                              void *cpu_addr, dma_addr_t dma_addr, size_t size)
222 {
223         int ret = -ENXIO;
224         unsigned long nr_vma_pages = (vma->vm_end - vma->vm_start) >>
225                                         PAGE_SHIFT;
226         unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
227         unsigned long pfn = dma_to_phys(dev, dma_addr) >> PAGE_SHIFT;
228         unsigned long off = vma->vm_pgoff;
229
230         if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
231                 return ret;
232
233         if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
234                 ret = remap_pfn_range(vma, vma->vm_start,
235                                       pfn + off,
236                                       vma->vm_end - vma->vm_start,
237                                       vma->vm_page_prot);
238         }
239
240         return ret;
241 }
242
243 static int __swiotlb_mmap_noncoherent(struct device *dev,
244                 struct vm_area_struct *vma,
245                 void *cpu_addr, dma_addr_t dma_addr, size_t size,
246                 struct dma_attrs *attrs)
247 {
248         vma->vm_page_prot = pgprot_dmacoherent(vma->vm_page_prot);
249         return __dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
250 }
251
252 static int __swiotlb_mmap_coherent(struct device *dev,
253                 struct vm_area_struct *vma,
254                 void *cpu_addr, dma_addr_t dma_addr, size_t size,
255                 struct dma_attrs *attrs)
256 {
257         /* Just use whatever page_prot attributes were specified */
258         return __dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
259 }
260
261 struct dma_map_ops noncoherent_swiotlb_dma_ops = {
262         .alloc = __dma_alloc_noncoherent,
263         .free = __dma_free_noncoherent,
264         .mmap = __swiotlb_mmap_noncoherent,
265         .map_page = __swiotlb_map_page,
266         .unmap_page = __swiotlb_unmap_page,
267         .map_sg = __swiotlb_map_sg_attrs,
268         .unmap_sg = __swiotlb_unmap_sg_attrs,
269         .sync_single_for_cpu = __swiotlb_sync_single_for_cpu,
270         .sync_single_for_device = __swiotlb_sync_single_for_device,
271         .sync_sg_for_cpu = __swiotlb_sync_sg_for_cpu,
272         .sync_sg_for_device = __swiotlb_sync_sg_for_device,
273         .dma_supported = swiotlb_dma_supported,
274         .mapping_error = swiotlb_dma_mapping_error,
275 };
276 EXPORT_SYMBOL(noncoherent_swiotlb_dma_ops);
277
278 struct dma_map_ops coherent_swiotlb_dma_ops = {
279         .alloc = __dma_alloc_coherent,
280         .free = __dma_free_coherent,
281         .mmap = __swiotlb_mmap_coherent,
282         .map_page = swiotlb_map_page,
283         .unmap_page = swiotlb_unmap_page,
284         .map_sg = swiotlb_map_sg_attrs,
285         .unmap_sg = swiotlb_unmap_sg_attrs,
286         .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
287         .sync_single_for_device = swiotlb_sync_single_for_device,
288         .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
289         .sync_sg_for_device = swiotlb_sync_sg_for_device,
290         .dma_supported = swiotlb_dma_supported,
291         .mapping_error = swiotlb_dma_mapping_error,
292 };
293 EXPORT_SYMBOL(coherent_swiotlb_dma_ops);
294
295 extern int swiotlb_late_init_with_default_size(size_t default_size);
296
297 static int __init swiotlb_late_init(void)
298 {
299         size_t swiotlb_size = min(SZ_64M, MAX_ORDER_NR_PAGES << PAGE_SHIFT);
300
301         dma_ops = &noncoherent_swiotlb_dma_ops;
302
303         return swiotlb_late_init_with_default_size(swiotlb_size);
304 }
305 subsys_initcall(swiotlb_late_init);
306
307 #define PREALLOC_DMA_DEBUG_ENTRIES      4096
308
309 static int __init dma_debug_do_init(void)
310 {
311         dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
312         return 0;
313 }
314 fs_initcall(dma_debug_do_init);