xref: /openbmc/linux/arch/arm64/mm/dma-mapping.c (revision d6fc9fcb)
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/acpi.h>
22 #include <linux/memblock.h>
23 #include <linux/cache.h>
24 #include <linux/export.h>
25 #include <linux/slab.h>
26 #include <linux/genalloc.h>
27 #include <linux/dma-direct.h>
28 #include <linux/dma-noncoherent.h>
29 #include <linux/dma-contiguous.h>
30 #include <linux/vmalloc.h>
31 #include <linux/swiotlb.h>
32 #include <linux/pci.h>
33 
34 #include <asm/cacheflush.h>
35 
36 pgprot_t arch_dma_mmap_pgprot(struct device *dev, pgprot_t prot,
37 		unsigned long attrs)
38 {
39 	if (!dev_is_dma_coherent(dev) || (attrs & DMA_ATTR_WRITE_COMBINE))
40 		return pgprot_writecombine(prot);
41 	return prot;
42 }
43 
44 void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
45 		size_t size, enum dma_data_direction dir)
46 {
47 	__dma_map_area(phys_to_virt(paddr), size, dir);
48 }
49 
50 void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
51 		size_t size, enum dma_data_direction dir)
52 {
53 	__dma_unmap_area(phys_to_virt(paddr), size, dir);
54 }
55 
56 void arch_dma_prep_coherent(struct page *page, size_t size)
57 {
58 	__dma_flush_area(page_address(page), size);
59 }
60 
61 #ifdef CONFIG_IOMMU_DMA
62 static int __swiotlb_get_sgtable_page(struct sg_table *sgt,
63 				      struct page *page, size_t size)
64 {
65 	int ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
66 
67 	if (!ret)
68 		sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
69 
70 	return ret;
71 }
72 
73 static int __swiotlb_mmap_pfn(struct vm_area_struct *vma,
74 			      unsigned long pfn, size_t size)
75 {
76 	int ret = -ENXIO;
77 	unsigned long nr_vma_pages = vma_pages(vma);
78 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
79 	unsigned long off = vma->vm_pgoff;
80 
81 	if (off < nr_pages && nr_vma_pages <= (nr_pages - off)) {
82 		ret = remap_pfn_range(vma, vma->vm_start,
83 				      pfn + off,
84 				      vma->vm_end - vma->vm_start,
85 				      vma->vm_page_prot);
86 	}
87 
88 	return ret;
89 }
90 #endif /* CONFIG_IOMMU_DMA */
91 
92 static int __init arm64_dma_init(void)
93 {
94 	WARN_TAINT(ARCH_DMA_MINALIGN < cache_line_size(),
95 		   TAINT_CPU_OUT_OF_SPEC,
96 		   "ARCH_DMA_MINALIGN smaller than CTR_EL0.CWG (%d < %d)",
97 		   ARCH_DMA_MINALIGN, cache_line_size());
98 	return dma_atomic_pool_init(GFP_DMA32, __pgprot(PROT_NORMAL_NC));
99 }
100 arch_initcall(arm64_dma_init);
101 
102 #ifdef CONFIG_IOMMU_DMA
103 #include <linux/dma-iommu.h>
104 #include <linux/platform_device.h>
105 #include <linux/amba/bus.h>
106 
107 /* Thankfully, all cache ops are by VA so we can ignore phys here */
108 static void flush_page(struct device *dev, const void *virt, phys_addr_t phys)
109 {
110 	__dma_flush_area(virt, PAGE_SIZE);
111 }
112 
113 static void *__iommu_alloc_attrs(struct device *dev, size_t size,
114 				 dma_addr_t *handle, gfp_t gfp,
115 				 unsigned long attrs)
116 {
117 	bool coherent = dev_is_dma_coherent(dev);
118 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
119 	size_t iosize = size;
120 	void *addr;
121 
122 	if (WARN(!dev, "cannot create IOMMU mapping for unknown device\n"))
123 		return NULL;
124 
125 	size = PAGE_ALIGN(size);
126 
127 	/*
128 	 * Some drivers rely on this, and we probably don't want the
129 	 * possibility of stale kernel data being read by devices anyway.
130 	 */
131 	gfp |= __GFP_ZERO;
132 
133 	if (!gfpflags_allow_blocking(gfp)) {
134 		struct page *page;
135 		/*
136 		 * In atomic context we can't remap anything, so we'll only
137 		 * get the virtually contiguous buffer we need by way of a
138 		 * physically contiguous allocation.
139 		 */
140 		if (coherent) {
141 			page = alloc_pages(gfp, get_order(size));
142 			addr = page ? page_address(page) : NULL;
143 		} else {
144 			addr = dma_alloc_from_pool(size, &page, gfp);
145 		}
146 		if (!addr)
147 			return NULL;
148 
149 		*handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
150 		if (*handle == DMA_MAPPING_ERROR) {
151 			if (coherent)
152 				__free_pages(page, get_order(size));
153 			else
154 				dma_free_from_pool(addr, size);
155 			addr = NULL;
156 		}
157 	} else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
158 		pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs);
159 		struct page *page;
160 
161 		page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT,
162 					get_order(size), gfp & __GFP_NOWARN);
163 		if (!page)
164 			return NULL;
165 
166 		*handle = iommu_dma_map_page(dev, page, 0, iosize, ioprot);
167 		if (*handle == DMA_MAPPING_ERROR) {
168 			dma_release_from_contiguous(dev, page,
169 						    size >> PAGE_SHIFT);
170 			return NULL;
171 		}
172 		addr = dma_common_contiguous_remap(page, size, VM_USERMAP,
173 						   prot,
174 						   __builtin_return_address(0));
175 		if (addr) {
176 			if (!coherent)
177 				__dma_flush_area(page_to_virt(page), iosize);
178 			memset(addr, 0, size);
179 		} else {
180 			iommu_dma_unmap_page(dev, *handle, iosize, 0, attrs);
181 			dma_release_from_contiguous(dev, page,
182 						    size >> PAGE_SHIFT);
183 		}
184 	} else {
185 		pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs);
186 		struct page **pages;
187 
188 		pages = iommu_dma_alloc(dev, iosize, gfp, attrs, ioprot,
189 					handle, flush_page);
190 		if (!pages)
191 			return NULL;
192 
193 		addr = dma_common_pages_remap(pages, size, VM_USERMAP, prot,
194 					      __builtin_return_address(0));
195 		if (!addr)
196 			iommu_dma_free(dev, pages, iosize, handle);
197 	}
198 	return addr;
199 }
200 
201 static void __iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr,
202 			       dma_addr_t handle, unsigned long attrs)
203 {
204 	size_t iosize = size;
205 
206 	size = PAGE_ALIGN(size);
207 	/*
208 	 * @cpu_addr will be one of 4 things depending on how it was allocated:
209 	 * - A remapped array of pages for contiguous allocations.
210 	 * - A remapped array of pages from iommu_dma_alloc(), for all
211 	 *   non-atomic allocations.
212 	 * - A non-cacheable alias from the atomic pool, for atomic
213 	 *   allocations by non-coherent devices.
214 	 * - A normal lowmem address, for atomic allocations by
215 	 *   coherent devices.
216 	 * Hence how dodgy the below logic looks...
217 	 */
218 	if (dma_in_atomic_pool(cpu_addr, size)) {
219 		iommu_dma_unmap_page(dev, handle, iosize, 0, 0);
220 		dma_free_from_pool(cpu_addr, size);
221 	} else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
222 		struct page *page = vmalloc_to_page(cpu_addr);
223 
224 		iommu_dma_unmap_page(dev, handle, iosize, 0, attrs);
225 		dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
226 		dma_common_free_remap(cpu_addr, size, VM_USERMAP);
227 	} else if (is_vmalloc_addr(cpu_addr)){
228 		struct vm_struct *area = find_vm_area(cpu_addr);
229 
230 		if (WARN_ON(!area || !area->pages))
231 			return;
232 		iommu_dma_free(dev, area->pages, iosize, &handle);
233 		dma_common_free_remap(cpu_addr, size, VM_USERMAP);
234 	} else {
235 		iommu_dma_unmap_page(dev, handle, iosize, 0, 0);
236 		__free_pages(virt_to_page(cpu_addr), get_order(size));
237 	}
238 }
239 
240 static int __iommu_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
241 			      void *cpu_addr, dma_addr_t dma_addr, size_t size,
242 			      unsigned long attrs)
243 {
244 	struct vm_struct *area;
245 	int ret;
246 
247 	vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs);
248 
249 	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
250 		return ret;
251 
252 	if (!is_vmalloc_addr(cpu_addr)) {
253 		unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
254 		return __swiotlb_mmap_pfn(vma, pfn, size);
255 	}
256 
257 	if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
258 		/*
259 		 * DMA_ATTR_FORCE_CONTIGUOUS allocations are always remapped,
260 		 * hence in the vmalloc space.
261 		 */
262 		unsigned long pfn = vmalloc_to_pfn(cpu_addr);
263 		return __swiotlb_mmap_pfn(vma, pfn, size);
264 	}
265 
266 	area = find_vm_area(cpu_addr);
267 	if (WARN_ON(!area || !area->pages))
268 		return -ENXIO;
269 
270 	return iommu_dma_mmap(area->pages, size, vma);
271 }
272 
273 static int __iommu_get_sgtable(struct device *dev, struct sg_table *sgt,
274 			       void *cpu_addr, dma_addr_t dma_addr,
275 			       size_t size, unsigned long attrs)
276 {
277 	unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
278 	struct vm_struct *area = find_vm_area(cpu_addr);
279 
280 	if (!is_vmalloc_addr(cpu_addr)) {
281 		struct page *page = virt_to_page(cpu_addr);
282 		return __swiotlb_get_sgtable_page(sgt, page, size);
283 	}
284 
285 	if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
286 		/*
287 		 * DMA_ATTR_FORCE_CONTIGUOUS allocations are always remapped,
288 		 * hence in the vmalloc space.
289 		 */
290 		struct page *page = vmalloc_to_page(cpu_addr);
291 		return __swiotlb_get_sgtable_page(sgt, page, size);
292 	}
293 
294 	if (WARN_ON(!area || !area->pages))
295 		return -ENXIO;
296 
297 	return sg_alloc_table_from_pages(sgt, area->pages, count, 0, size,
298 					 GFP_KERNEL);
299 }
300 
301 static void __iommu_sync_single_for_cpu(struct device *dev,
302 					dma_addr_t dev_addr, size_t size,
303 					enum dma_data_direction dir)
304 {
305 	phys_addr_t phys;
306 
307 	if (dev_is_dma_coherent(dev))
308 		return;
309 
310 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dev_addr);
311 	arch_sync_dma_for_cpu(dev, phys, size, dir);
312 }
313 
314 static void __iommu_sync_single_for_device(struct device *dev,
315 					   dma_addr_t dev_addr, size_t size,
316 					   enum dma_data_direction dir)
317 {
318 	phys_addr_t phys;
319 
320 	if (dev_is_dma_coherent(dev))
321 		return;
322 
323 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dev_addr);
324 	arch_sync_dma_for_device(dev, phys, size, dir);
325 }
326 
327 static dma_addr_t __iommu_map_page(struct device *dev, struct page *page,
328 				   unsigned long offset, size_t size,
329 				   enum dma_data_direction dir,
330 				   unsigned long attrs)
331 {
332 	bool coherent = dev_is_dma_coherent(dev);
333 	int prot = dma_info_to_prot(dir, coherent, attrs);
334 	dma_addr_t dev_addr = iommu_dma_map_page(dev, page, offset, size, prot);
335 
336 	if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
337 	    dev_addr != DMA_MAPPING_ERROR)
338 		__dma_map_area(page_address(page) + offset, size, dir);
339 
340 	return dev_addr;
341 }
342 
343 static void __iommu_unmap_page(struct device *dev, dma_addr_t dev_addr,
344 			       size_t size, enum dma_data_direction dir,
345 			       unsigned long attrs)
346 {
347 	if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
348 		__iommu_sync_single_for_cpu(dev, dev_addr, size, dir);
349 
350 	iommu_dma_unmap_page(dev, dev_addr, size, dir, attrs);
351 }
352 
353 static void __iommu_sync_sg_for_cpu(struct device *dev,
354 				    struct scatterlist *sgl, int nelems,
355 				    enum dma_data_direction dir)
356 {
357 	struct scatterlist *sg;
358 	int i;
359 
360 	if (dev_is_dma_coherent(dev))
361 		return;
362 
363 	for_each_sg(sgl, sg, nelems, i)
364 		arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
365 }
366 
367 static void __iommu_sync_sg_for_device(struct device *dev,
368 				       struct scatterlist *sgl, int nelems,
369 				       enum dma_data_direction dir)
370 {
371 	struct scatterlist *sg;
372 	int i;
373 
374 	if (dev_is_dma_coherent(dev))
375 		return;
376 
377 	for_each_sg(sgl, sg, nelems, i)
378 		arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, dir);
379 }
380 
381 static int __iommu_map_sg_attrs(struct device *dev, struct scatterlist *sgl,
382 				int nelems, enum dma_data_direction dir,
383 				unsigned long attrs)
384 {
385 	bool coherent = dev_is_dma_coherent(dev);
386 
387 	if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
388 		__iommu_sync_sg_for_device(dev, sgl, nelems, dir);
389 
390 	return iommu_dma_map_sg(dev, sgl, nelems,
391 				dma_info_to_prot(dir, coherent, attrs));
392 }
393 
394 static void __iommu_unmap_sg_attrs(struct device *dev,
395 				   struct scatterlist *sgl, int nelems,
396 				   enum dma_data_direction dir,
397 				   unsigned long attrs)
398 {
399 	if ((attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
400 		__iommu_sync_sg_for_cpu(dev, sgl, nelems, dir);
401 
402 	iommu_dma_unmap_sg(dev, sgl, nelems, dir, attrs);
403 }
404 
405 static const struct dma_map_ops iommu_dma_ops = {
406 	.alloc = __iommu_alloc_attrs,
407 	.free = __iommu_free_attrs,
408 	.mmap = __iommu_mmap_attrs,
409 	.get_sgtable = __iommu_get_sgtable,
410 	.map_page = __iommu_map_page,
411 	.unmap_page = __iommu_unmap_page,
412 	.map_sg = __iommu_map_sg_attrs,
413 	.unmap_sg = __iommu_unmap_sg_attrs,
414 	.sync_single_for_cpu = __iommu_sync_single_for_cpu,
415 	.sync_single_for_device = __iommu_sync_single_for_device,
416 	.sync_sg_for_cpu = __iommu_sync_sg_for_cpu,
417 	.sync_sg_for_device = __iommu_sync_sg_for_device,
418 	.map_resource = iommu_dma_map_resource,
419 	.unmap_resource = iommu_dma_unmap_resource,
420 };
421 
422 static int __init __iommu_dma_init(void)
423 {
424 	return iommu_dma_init();
425 }
426 arch_initcall(__iommu_dma_init);
427 
428 static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
429 				  const struct iommu_ops *ops)
430 {
431 	struct iommu_domain *domain;
432 
433 	if (!ops)
434 		return;
435 
436 	/*
437 	 * The IOMMU core code allocates the default DMA domain, which the
438 	 * underlying IOMMU driver needs to support via the dma-iommu layer.
439 	 */
440 	domain = iommu_get_domain_for_dev(dev);
441 
442 	if (!domain)
443 		goto out_err;
444 
445 	if (domain->type == IOMMU_DOMAIN_DMA) {
446 		if (iommu_dma_init_domain(domain, dma_base, size, dev))
447 			goto out_err;
448 
449 		dev->dma_ops = &iommu_dma_ops;
450 	}
451 
452 	return;
453 
454 out_err:
455 	 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
456 		 dev_name(dev));
457 }
458 
459 void arch_teardown_dma_ops(struct device *dev)
460 {
461 	dev->dma_ops = NULL;
462 }
463 
464 #else
465 
466 static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
467 				  const struct iommu_ops *iommu)
468 { }
469 
470 #endif  /* CONFIG_IOMMU_DMA */
471 
472 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
473 			const struct iommu_ops *iommu, bool coherent)
474 {
475 	dev->dma_coherent = coherent;
476 	__iommu_setup_dma_ops(dev, dma_base, size, iommu);
477 
478 #ifdef CONFIG_XEN
479 	if (xen_initial_domain())
480 		dev->dma_ops = xen_dma_ops;
481 #endif
482 }
483