• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * A fairly generic DMA-API to IOMMU-API glue layer.
4  *
5  * Copyright (C) 2014-2015 ARM Ltd.
6  *
7  * based in part on arch/arm/mm/dma-mapping.c:
8  * Copyright (C) 2000-2004 Russell King
9  */
10 
11 #include <linux/acpi_iort.h>
12 #include <linux/device.h>
13 #include <linux/dma-map-ops.h>
14 #include <linux/dma-iommu.h>
15 #include <linux/gfp.h>
16 #include <linux/huge_mm.h>
17 #include <linux/iommu.h>
18 #include <linux/iova.h>
19 #include <linux/irq.h>
20 #include <linux/mm.h>
21 #include <linux/mutex.h>
22 #include <linux/pci.h>
23 #include <linux/swiotlb.h>
24 #include <linux/scatterlist.h>
25 #include <linux/vmalloc.h>
26 #include <linux/crash_dump.h>
27 #include <linux/dma-direct.h>
28 #include <trace/events/swiotlb.h>
29 #include <trace/hooks/iommu.h>
30 
31 struct iommu_dma_msi_page {
32 	struct list_head	list;
33 	dma_addr_t		iova;
34 	phys_addr_t		phys;
35 };
36 
37 enum iommu_dma_cookie_type {
38 	IOMMU_DMA_IOVA_COOKIE,
39 	IOMMU_DMA_MSI_COOKIE,
40 };
41 
42 struct iommu_dma_cookie {
43 	enum iommu_dma_cookie_type	type;
44 	union {
45 		/* Full allocator for IOMMU_DMA_IOVA_COOKIE */
46 		struct iova_domain	iovad;
47 		/* Trivial linear page allocator for IOMMU_DMA_MSI_COOKIE */
48 		dma_addr_t		msi_iova;
49 	};
50 	struct list_head		msi_page_list;
51 
52 	/* Domain for flush queue callback; NULL if flush queue not in use */
53 	struct iommu_domain		*fq_domain;
54 };
55 
56 struct iommu_dma_cookie_ext {
57 	struct iommu_dma_cookie		cookie;
58 	struct mutex			mutex;
59 };
60 
61 static DEFINE_STATIC_KEY_FALSE(iommu_deferred_attach_enabled);
62 bool iommu_dma_forcedac __read_mostly;
63 
iommu_dma_forcedac_setup(char * str)64 static int __init iommu_dma_forcedac_setup(char *str)
65 {
66 	int ret = kstrtobool(str, &iommu_dma_forcedac);
67 
68 	if (!ret && iommu_dma_forcedac)
69 		pr_info("Forcing DAC for PCI devices\n");
70 	return ret;
71 }
72 early_param("iommu.forcedac", iommu_dma_forcedac_setup);
73 
iommu_dma_entry_dtor(unsigned long data)74 static void iommu_dma_entry_dtor(unsigned long data)
75 {
76 	struct page *freelist = (struct page *)data;
77 
78 	while (freelist) {
79 		unsigned long p = (unsigned long)page_address(freelist);
80 
81 		freelist = freelist->freelist;
82 		free_page(p);
83 	}
84 }
85 
cookie_msi_granule(struct iommu_dma_cookie * cookie)86 static inline size_t cookie_msi_granule(struct iommu_dma_cookie *cookie)
87 {
88 	if (cookie->type == IOMMU_DMA_IOVA_COOKIE)
89 		return cookie->iovad.granule;
90 	return PAGE_SIZE;
91 }
92 
cookie_alloc(enum iommu_dma_cookie_type type)93 static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
94 {
95 	struct iommu_dma_cookie_ext *cookie;
96 
97 	cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
98 	if (cookie) {
99 		INIT_LIST_HEAD(&cookie->cookie.msi_page_list);
100 		cookie->cookie.type = type;
101 		mutex_init(&cookie->mutex);
102 	}
103 	return &cookie->cookie;
104 }
105 
106 /**
107  * iommu_get_dma_cookie - Acquire DMA-API resources for a domain
108  * @domain: IOMMU domain to prepare for DMA-API usage
109  *
110  * IOMMU drivers should normally call this from their domain_alloc
111  * callback when domain->type == IOMMU_DOMAIN_DMA.
112  */
iommu_get_dma_cookie(struct iommu_domain * domain)113 int iommu_get_dma_cookie(struct iommu_domain *domain)
114 {
115 	if (domain->iova_cookie)
116 		return -EEXIST;
117 
118 	domain->iova_cookie = cookie_alloc(IOMMU_DMA_IOVA_COOKIE);
119 	if (!domain->iova_cookie)
120 		return -ENOMEM;
121 
122 	return 0;
123 }
124 EXPORT_SYMBOL(iommu_get_dma_cookie);
125 
126 /**
127  * iommu_get_msi_cookie - Acquire just MSI remapping resources
128  * @domain: IOMMU domain to prepare
129  * @base: Start address of IOVA region for MSI mappings
130  *
131  * Users who manage their own IOVA allocation and do not want DMA API support,
132  * but would still like to take advantage of automatic MSI remapping, can use
133  * this to initialise their own domain appropriately. Users should reserve a
134  * contiguous IOVA region, starting at @base, large enough to accommodate the
135  * number of PAGE_SIZE mappings necessary to cover every MSI doorbell address
136  * used by the devices attached to @domain.
137  */
iommu_get_msi_cookie(struct iommu_domain * domain,dma_addr_t base)138 int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
139 {
140 	struct iommu_dma_cookie *cookie;
141 
142 	if (domain->type != IOMMU_DOMAIN_UNMANAGED)
143 		return -EINVAL;
144 
145 	if (domain->iova_cookie)
146 		return -EEXIST;
147 
148 	cookie = cookie_alloc(IOMMU_DMA_MSI_COOKIE);
149 	if (!cookie)
150 		return -ENOMEM;
151 
152 	cookie->msi_iova = base;
153 	domain->iova_cookie = cookie;
154 	return 0;
155 }
156 EXPORT_SYMBOL(iommu_get_msi_cookie);
157 
158 /**
159  * iommu_put_dma_cookie - Release a domain's DMA mapping resources
160  * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() or
161  *          iommu_get_msi_cookie()
162  *
163  * IOMMU drivers should normally call this from their domain_free callback.
164  */
iommu_put_dma_cookie(struct iommu_domain * domain)165 void iommu_put_dma_cookie(struct iommu_domain *domain)
166 {
167 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
168 	struct iommu_dma_msi_page *msi, *tmp;
169 
170 	if (!cookie)
171 		return;
172 
173 	if (cookie->type == IOMMU_DMA_IOVA_COOKIE && cookie->iovad.granule)
174 		put_iova_domain(&cookie->iovad);
175 
176 	list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) {
177 		list_del(&msi->list);
178 		kfree(msi);
179 	}
180 	kfree(cookie);
181 	domain->iova_cookie = NULL;
182 }
183 EXPORT_SYMBOL(iommu_put_dma_cookie);
184 
185 /**
186  * iommu_dma_get_resv_regions - Reserved region driver helper
187  * @dev: Device from iommu_get_resv_regions()
188  * @list: Reserved region list from iommu_get_resv_regions()
189  *
190  * IOMMU drivers can use this to implement their .get_resv_regions callback
191  * for general non-IOMMU-specific reservations. Currently, this covers GICv3
192  * ITS region reservation on ACPI based ARM platforms that may require HW MSI
193  * reservation.
194  */
iommu_dma_get_resv_regions(struct device * dev,struct list_head * list)195 void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)
196 {
197 
198 	if (!is_of_node(dev_iommu_fwspec_get(dev)->iommu_fwnode))
199 		iort_iommu_msi_get_resv_regions(dev, list);
200 
201 }
202 EXPORT_SYMBOL(iommu_dma_get_resv_regions);
203 
cookie_init_hw_msi_region(struct iommu_dma_cookie * cookie,phys_addr_t start,phys_addr_t end)204 static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie,
205 		phys_addr_t start, phys_addr_t end)
206 {
207 	struct iova_domain *iovad = &cookie->iovad;
208 	struct iommu_dma_msi_page *msi_page;
209 	int i, num_pages;
210 
211 	start -= iova_offset(iovad, start);
212 	num_pages = iova_align(iovad, end - start) >> iova_shift(iovad);
213 
214 	for (i = 0; i < num_pages; i++) {
215 		msi_page = kmalloc(sizeof(*msi_page), GFP_KERNEL);
216 		if (!msi_page)
217 			return -ENOMEM;
218 
219 		msi_page->phys = start;
220 		msi_page->iova = start;
221 		INIT_LIST_HEAD(&msi_page->list);
222 		list_add(&msi_page->list, &cookie->msi_page_list);
223 		start += iovad->granule;
224 	}
225 
226 	return 0;
227 }
228 
iova_reserve_pci_windows(struct pci_dev * dev,struct iova_domain * iovad)229 static int iova_reserve_pci_windows(struct pci_dev *dev,
230 		struct iova_domain *iovad)
231 {
232 	struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
233 	struct resource_entry *window;
234 	unsigned long lo, hi;
235 	phys_addr_t start = 0, end;
236 
237 	resource_list_for_each_entry(window, &bridge->windows) {
238 		if (resource_type(window->res) != IORESOURCE_MEM)
239 			continue;
240 
241 		lo = iova_pfn(iovad, window->res->start - window->offset);
242 		hi = iova_pfn(iovad, window->res->end - window->offset);
243 		reserve_iova(iovad, lo, hi);
244 	}
245 
246 	/* Get reserved DMA windows from host bridge */
247 	resource_list_for_each_entry(window, &bridge->dma_ranges) {
248 		end = window->res->start - window->offset;
249 resv_iova:
250 		if (end > start) {
251 			lo = iova_pfn(iovad, start);
252 			hi = iova_pfn(iovad, end);
253 			reserve_iova(iovad, lo, hi);
254 		} else if (end < start) {
255 			/* dma_ranges list should be sorted */
256 			dev_err(&dev->dev,
257 				"Failed to reserve IOVA [%pa-%pa]\n",
258 				&start, &end);
259 			return -EINVAL;
260 		}
261 
262 		start = window->res->end - window->offset + 1;
263 		/* If window is last entry */
264 		if (window->node.next == &bridge->dma_ranges &&
265 		    end != ~(phys_addr_t)0) {
266 			end = ~(phys_addr_t)0;
267 			goto resv_iova;
268 		}
269 	}
270 
271 	return 0;
272 }
273 
iova_reserve_iommu_regions(struct device * dev,struct iommu_domain * domain)274 static int iova_reserve_iommu_regions(struct device *dev,
275 		struct iommu_domain *domain)
276 {
277 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
278 	struct iova_domain *iovad = &cookie->iovad;
279 	struct iommu_resv_region *region;
280 	LIST_HEAD(resv_regions);
281 	int ret = 0;
282 
283 	if (dev_is_pci(dev)) {
284 		ret = iova_reserve_pci_windows(to_pci_dev(dev), iovad);
285 		if (ret)
286 			return ret;
287 	}
288 
289 	iommu_get_resv_regions(dev, &resv_regions);
290 	list_for_each_entry(region, &resv_regions, list) {
291 		unsigned long lo, hi;
292 
293 		/* We ARE the software that manages these! */
294 		if (region->type == IOMMU_RESV_SW_MSI)
295 			continue;
296 
297 		lo = iova_pfn(iovad, region->start);
298 		hi = iova_pfn(iovad, region->start + region->length - 1);
299 		reserve_iova(iovad, lo, hi);
300 
301 		if (region->type == IOMMU_RESV_MSI)
302 			ret = cookie_init_hw_msi_region(cookie, region->start,
303 					region->start + region->length);
304 		if (ret)
305 			break;
306 	}
307 	iommu_put_resv_regions(dev, &resv_regions);
308 
309 	return ret;
310 }
311 
iommu_dma_flush_iotlb_all(struct iova_domain * iovad)312 static void iommu_dma_flush_iotlb_all(struct iova_domain *iovad)
313 {
314 	struct iommu_dma_cookie *cookie;
315 	struct iommu_domain *domain;
316 
317 	cookie = container_of(iovad, struct iommu_dma_cookie, iovad);
318 	domain = cookie->fq_domain;
319 
320 	domain->ops->flush_iotlb_all(domain);
321 }
322 
dev_is_untrusted(struct device * dev)323 static bool dev_is_untrusted(struct device *dev)
324 {
325 	return dev_is_pci(dev) && to_pci_dev(dev)->untrusted;
326 }
327 
dev_use_swiotlb(struct device * dev)328 static bool dev_use_swiotlb(struct device *dev)
329 {
330 	return IS_ENABLED(CONFIG_SWIOTLB) && dev_is_untrusted(dev);
331 }
332 
333 /* sysfs updates are serialised by the mutex of the group owning @domain */
iommu_dma_init_fq(struct iommu_domain * domain)334 int iommu_dma_init_fq(struct iommu_domain *domain)
335 {
336 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
337 	int ret;
338 
339 	if (cookie->fq_domain)
340 		return 0;
341 
342 	ret = init_iova_flush_queue(&cookie->iovad, iommu_dma_flush_iotlb_all,
343 				    iommu_dma_entry_dtor);
344 	if (ret) {
345 		pr_warn("iova flush queue initialization failed\n");
346 		return ret;
347 	}
348 	/*
349 	 * Prevent incomplete iovad->fq being observable. Pairs with path from
350 	 * __iommu_dma_unmap() through iommu_dma_free_iova() to queue_iova()
351 	 */
352 	smp_wmb();
353 	WRITE_ONCE(cookie->fq_domain, domain);
354 	return 0;
355 }
356 
357 /**
358  * iommu_dma_init_domain - Initialise a DMA mapping domain
359  * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
360  * @base: IOVA at which the mappable address space starts
361  * @limit: Last address of the IOVA space
362  * @dev: Device the domain is being initialised for
363  *
364  * @base and @limit + 1 should be exact multiples of IOMMU page granularity to
365  * avoid rounding surprises. If necessary, we reserve the page at address 0
366  * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
367  * any change which could make prior IOVAs invalid will fail.
368  */
iommu_dma_init_domain(struct iommu_domain * domain,dma_addr_t base,dma_addr_t limit,struct device * dev)369 static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
370 				 dma_addr_t limit, struct device *dev)
371 {
372 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
373 	struct iommu_dma_cookie_ext *cookie_ext;
374 	unsigned long order, base_pfn;
375 	struct iova_domain *iovad;
376 	int ret;
377 
378 	if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE)
379 		return -EINVAL;
380 
381 	iovad = &cookie->iovad;
382 
383 	/* Use the smallest supported page size for IOVA granularity */
384 	order = __ffs(domain->pgsize_bitmap);
385 	base_pfn = max_t(unsigned long, 1, base >> order);
386 
387 	/* Check the domain allows at least some access to the device... */
388 	if (domain->geometry.force_aperture) {
389 		if (base > domain->geometry.aperture_end ||
390 		    limit < domain->geometry.aperture_start) {
391 			pr_warn("specified DMA range outside IOMMU capability\n");
392 			return -EFAULT;
393 		}
394 		/* ...then finally give it a kicking to make sure it fits */
395 		base_pfn = max_t(unsigned long, base_pfn,
396 				domain->geometry.aperture_start >> order);
397 	}
398 
399 	/* start_pfn is always nonzero for an already-initialised domain */
400 	cookie_ext = container_of(cookie, struct iommu_dma_cookie_ext, cookie);
401 	mutex_lock(&cookie_ext->mutex);
402 	if (iovad->start_pfn) {
403 		if (1UL << order != iovad->granule ||
404 		    base_pfn != iovad->start_pfn) {
405 			pr_warn("Incompatible range for DMA domain\n");
406 			ret = -EFAULT;
407 			goto done_unlock;
408 		}
409 
410 		ret = 0;
411 		goto done_unlock;
412 	}
413 
414 	init_iova_domain(iovad, 1UL << order, base_pfn);
415 
416 	/* If the FQ fails we can simply fall back to strict mode */
417 	if (domain->type == IOMMU_DOMAIN_DMA_FQ && iommu_dma_init_fq(domain))
418 		domain->type = IOMMU_DOMAIN_DMA;
419 
420 	ret = iova_reserve_iommu_regions(dev, domain);
421 
422 done_unlock:
423 	mutex_unlock(&cookie_ext->mutex);
424 	return ret;
425 }
426 
427 /*
428  * Should be called prior to using dma-apis.
429  */
iommu_dma_enable_best_fit_algo(struct device * dev)430 int iommu_dma_enable_best_fit_algo(struct device *dev)
431 {
432 	struct iommu_domain *domain;
433 	struct iova_domain *iovad;
434 
435 	domain = iommu_get_domain_for_dev(dev);
436 	if (!domain || !domain->iova_cookie)
437 		return -EINVAL;
438 
439 	iovad = &((struct iommu_dma_cookie *)domain->iova_cookie)->iovad;
440 	iovad->best_fit = true;
441 	return 0;
442 }
443 EXPORT_SYMBOL(iommu_dma_enable_best_fit_algo);
444 
445 /**
446  * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
447  *                    page flags.
448  * @dir: Direction of DMA transfer
449  * @coherent: Is the DMA master cache-coherent?
450  * @attrs: DMA attributes for the mapping
451  *
452  * Return: corresponding IOMMU API page protection flags
453  */
dma_info_to_prot(enum dma_data_direction dir,bool coherent,unsigned long attrs)454 static int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
455 		     unsigned long attrs)
456 {
457 	int prot = coherent ? IOMMU_CACHE : 0;
458 
459 	if (attrs & DMA_ATTR_PRIVILEGED)
460 		prot |= IOMMU_PRIV;
461 	if (attrs & DMA_ATTR_SYS_CACHE_ONLY)
462 		prot |= IOMMU_SYS_CACHE;
463 	if (attrs & DMA_ATTR_SYS_CACHE_ONLY_NWA)
464 		prot |= IOMMU_SYS_CACHE_NWA;
465 
466 	switch (dir) {
467 	case DMA_BIDIRECTIONAL:
468 		return prot | IOMMU_READ | IOMMU_WRITE;
469 	case DMA_TO_DEVICE:
470 		return prot | IOMMU_READ;
471 	case DMA_FROM_DEVICE:
472 		return prot | IOMMU_WRITE;
473 	default:
474 		return 0;
475 	}
476 }
477 
iommu_dma_alloc_iova(struct iommu_domain * domain,size_t size,u64 dma_limit,struct device * dev)478 static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
479 		size_t size, u64 dma_limit, struct device *dev)
480 {
481 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
482 	struct iova_domain *iovad = &cookie->iovad;
483 	unsigned long shift, iova_len, iova = 0;
484 
485 	if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
486 		cookie->msi_iova += size;
487 		return cookie->msi_iova - size;
488 	}
489 
490 	shift = iova_shift(iovad);
491 	iova_len = size >> shift;
492 	/*
493 	 * Freeing non-power-of-two-sized allocations back into the IOVA caches
494 	 * will come back to bite us badly, so we have to waste a bit of space
495 	 * rounding up anything cacheable to make sure that can't happen. The
496 	 * order of the unadjusted size will still match upon freeing.
497 	 */
498 	if (iova_len < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1)))
499 		iova_len = roundup_pow_of_two(iova_len);
500 
501 	dma_limit = min_not_zero(dma_limit, dev->bus_dma_limit);
502 
503 	if (domain->geometry.force_aperture)
504 		dma_limit = min(dma_limit, (u64)domain->geometry.aperture_end);
505 
506 	/* Try to get PCI devices a SAC address */
507 	if (dma_limit > DMA_BIT_MASK(32) && !iommu_dma_forcedac && dev_is_pci(dev))
508 		iova = alloc_iova_fast(iovad, iova_len,
509 				       DMA_BIT_MASK(32) >> shift, false);
510 
511 	if (!iova)
512 		iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift,
513 				       true);
514 
515 	trace_android_vh_iommu_iovad_alloc_iova(dev, iovad, (dma_addr_t)iova << shift, size);
516 
517 	return (dma_addr_t)iova << shift;
518 }
519 
iommu_dma_free_iova(struct iommu_dma_cookie * cookie,dma_addr_t iova,size_t size,struct iommu_iotlb_gather * gather)520 static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
521 		dma_addr_t iova, size_t size, struct iommu_iotlb_gather *gather)
522 {
523 	struct iova_domain *iovad = &cookie->iovad;
524 
525 	/* The MSI case is only ever cleaning up its most recent allocation */
526 	if (cookie->type == IOMMU_DMA_MSI_COOKIE)
527 		cookie->msi_iova -= size;
528 	else if (gather && gather->queued)
529 		queue_iova(iovad, iova_pfn(iovad, iova),
530 				size >> iova_shift(iovad),
531 				(unsigned long)gather->freelist);
532 	else
533 		free_iova_fast(iovad, iova_pfn(iovad, iova),
534 				size >> iova_shift(iovad));
535 
536 	trace_android_vh_iommu_iovad_free_iova(iovad, iova, size);
537 }
538 
__iommu_dma_unmap(struct device * dev,dma_addr_t dma_addr,size_t size)539 static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr,
540 		size_t size)
541 {
542 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
543 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
544 	struct iova_domain *iovad = &cookie->iovad;
545 	size_t iova_off = iova_offset(iovad, dma_addr);
546 	struct iommu_iotlb_gather iotlb_gather;
547 	size_t unmapped;
548 
549 	dma_addr -= iova_off;
550 	size = iova_align(iovad, size + iova_off);
551 	iommu_iotlb_gather_init(&iotlb_gather);
552 	iotlb_gather.queued = READ_ONCE(cookie->fq_domain);
553 
554 	unmapped = iommu_unmap_fast(domain, dma_addr, size, &iotlb_gather);
555 	WARN_ON(unmapped != size);
556 
557 	if (!iotlb_gather.queued)
558 		iommu_iotlb_sync(domain, &iotlb_gather);
559 	iommu_dma_free_iova(cookie, dma_addr, size, &iotlb_gather);
560 }
561 
__iommu_dma_map(struct device * dev,phys_addr_t phys,size_t size,int prot,u64 dma_mask)562 static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
563 		size_t size, int prot, u64 dma_mask)
564 {
565 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
566 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
567 	struct iova_domain *iovad = &cookie->iovad;
568 	size_t iova_off = iova_offset(iovad, phys);
569 	dma_addr_t iova;
570 
571 	if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
572 	    iommu_deferred_attach(dev, domain))
573 		return DMA_MAPPING_ERROR;
574 
575 	size = iova_align(iovad, size + iova_off);
576 
577 	iova = iommu_dma_alloc_iova(domain, size, dma_mask, dev);
578 	if (!iova)
579 		return DMA_MAPPING_ERROR;
580 
581 	if (iommu_map_atomic(domain, iova, phys - iova_off, size, prot)) {
582 		iommu_dma_free_iova(cookie, iova, size, NULL);
583 		return DMA_MAPPING_ERROR;
584 	}
585 	return iova + iova_off;
586 }
587 
__iommu_dma_free_pages(struct page ** pages,int count)588 static void __iommu_dma_free_pages(struct page **pages, int count)
589 {
590 	while (count--)
591 		__free_page(pages[count]);
592 	kvfree(pages);
593 }
594 
__iommu_dma_alloc_pages(struct device * dev,unsigned int count,unsigned long order_mask,gfp_t gfp)595 static struct page **__iommu_dma_alloc_pages(struct device *dev,
596 		unsigned int count, unsigned long order_mask, gfp_t gfp)
597 {
598 	struct page **pages;
599 	unsigned int i = 0, nid = dev_to_node(dev);
600 
601 	order_mask &= (2U << MAX_ORDER) - 1;
602 	if (!order_mask)
603 		return NULL;
604 
605 	pages = kvzalloc(count * sizeof(*pages), GFP_KERNEL);
606 	if (!pages)
607 		return NULL;
608 
609 	/* IOMMU can map any pages, so himem can also be used here */
610 	gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
611 
612 	/* It makes no sense to muck about with huge pages */
613 	gfp &= ~__GFP_COMP;
614 
615 	while (count) {
616 		struct page *page = NULL;
617 		unsigned int order_size;
618 
619 		/*
620 		 * Higher-order allocations are a convenience rather
621 		 * than a necessity, hence using __GFP_NORETRY until
622 		 * falling back to minimum-order allocations.
623 		 */
624 		for (order_mask &= (2U << __fls(count)) - 1;
625 		     order_mask; order_mask &= ~order_size) {
626 			unsigned int order = __fls(order_mask);
627 			gfp_t alloc_flags = gfp;
628 
629 			order_size = 1U << order;
630 			if (order_mask > order_size)
631 				alloc_flags |= __GFP_NORETRY;
632 			trace_android_vh_adjust_alloc_flags(order, &alloc_flags);
633 			page = alloc_pages_node(nid, alloc_flags, order);
634 			if (!page)
635 				continue;
636 			if (order)
637 				split_page(page, order);
638 			break;
639 		}
640 		if (!page) {
641 			__iommu_dma_free_pages(pages, i);
642 			return NULL;
643 		}
644 		count -= order_size;
645 		while (order_size--)
646 			pages[i++] = page++;
647 	}
648 	return pages;
649 }
650 
651 /*
652  * If size is less than PAGE_SIZE, then a full CPU page will be allocated,
653  * but an IOMMU which supports smaller pages might not map the whole thing.
654  */
__iommu_dma_alloc_noncontiguous(struct device * dev,size_t size,struct sg_table * sgt,gfp_t gfp,pgprot_t prot,unsigned long attrs)655 static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
656 		size_t size, struct sg_table *sgt, gfp_t gfp, pgprot_t prot,
657 		unsigned long attrs)
658 {
659 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
660 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
661 	struct iova_domain *iovad = &cookie->iovad;
662 	bool coherent = dev_is_dma_coherent(dev);
663 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
664 	unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
665 	struct page **pages;
666 	dma_addr_t iova;
667 	ssize_t ret;
668 
669 	if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
670 	    iommu_deferred_attach(dev, domain))
671 		return NULL;
672 
673 	min_size = alloc_sizes & -alloc_sizes;
674 	if (min_size < PAGE_SIZE) {
675 		min_size = PAGE_SIZE;
676 		alloc_sizes |= PAGE_SIZE;
677 	} else {
678 		size = ALIGN(size, min_size);
679 	}
680 	if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
681 		alloc_sizes = min_size;
682 
683 	count = PAGE_ALIGN(size) >> PAGE_SHIFT;
684 	pages = __iommu_dma_alloc_pages(dev, count, alloc_sizes >> PAGE_SHIFT,
685 					gfp);
686 	if (!pages)
687 		return NULL;
688 
689 	size = iova_align(iovad, size);
690 	iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev);
691 	if (!iova)
692 		goto out_free_pages;
693 
694 	if (sg_alloc_table_from_pages(sgt, pages, count, 0, size, GFP_KERNEL))
695 		goto out_free_iova;
696 
697 	if (!(ioprot & IOMMU_CACHE)) {
698 		struct scatterlist *sg;
699 		int i;
700 
701 		for_each_sg(sgt->sgl, sg, sgt->orig_nents, i)
702 			arch_dma_prep_coherent(sg_page(sg), sg->length);
703 	}
704 
705 	ret = iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot);
706 	if (ret < 0 || ret < size)
707 		goto out_free_sg;
708 
709 	sgt->sgl->dma_address = iova;
710 	sgt->sgl->dma_length = size;
711 	return pages;
712 
713 out_free_sg:
714 	sg_free_table(sgt);
715 out_free_iova:
716 	iommu_dma_free_iova(cookie, iova, size, NULL);
717 out_free_pages:
718 	__iommu_dma_free_pages(pages, count);
719 	return NULL;
720 }
721 
iommu_dma_alloc_remap(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp,pgprot_t prot,unsigned long attrs)722 static void *iommu_dma_alloc_remap(struct device *dev, size_t size,
723 		dma_addr_t *dma_handle, gfp_t gfp, pgprot_t prot,
724 		unsigned long attrs)
725 {
726 	struct page **pages;
727 	struct sg_table sgt;
728 	void *vaddr;
729 
730 	pages = __iommu_dma_alloc_noncontiguous(dev, size, &sgt, gfp, prot,
731 						attrs);
732 	if (!pages)
733 		return NULL;
734 	*dma_handle = sgt.sgl->dma_address;
735 	sg_free_table(&sgt);
736 	vaddr = dma_common_pages_remap(pages, size, prot,
737 			__builtin_return_address(0));
738 	if (!vaddr)
739 		goto out_unmap;
740 	return vaddr;
741 
742 out_unmap:
743 	__iommu_dma_unmap(dev, *dma_handle, size);
744 	__iommu_dma_free_pages(pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
745 	return NULL;
746 }
747 
748 #ifdef CONFIG_DMA_REMAP
iommu_dma_alloc_noncontiguous(struct device * dev,size_t size,enum dma_data_direction dir,gfp_t gfp,unsigned long attrs)749 static struct sg_table *iommu_dma_alloc_noncontiguous(struct device *dev,
750 		size_t size, enum dma_data_direction dir, gfp_t gfp,
751 		unsigned long attrs)
752 {
753 	struct dma_sgt_handle *sh;
754 
755 	sh = kmalloc(sizeof(*sh), gfp);
756 	if (!sh)
757 		return NULL;
758 
759 	sh->pages = __iommu_dma_alloc_noncontiguous(dev, size, &sh->sgt, gfp,
760 						    PAGE_KERNEL, attrs);
761 	if (!sh->pages) {
762 		kfree(sh);
763 		return NULL;
764 	}
765 	return &sh->sgt;
766 }
767 
iommu_dma_free_noncontiguous(struct device * dev,size_t size,struct sg_table * sgt,enum dma_data_direction dir)768 static void iommu_dma_free_noncontiguous(struct device *dev, size_t size,
769 		struct sg_table *sgt, enum dma_data_direction dir)
770 {
771 	struct dma_sgt_handle *sh = sgt_handle(sgt);
772 
773 	__iommu_dma_unmap(dev, sgt->sgl->dma_address, size);
774 	__iommu_dma_free_pages(sh->pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
775 	sg_free_table(&sh->sgt);
776 	kfree(sh);
777 }
778 #endif /* CONFIG_DMA_REMAP */
779 
iommu_dma_sync_single_for_cpu(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir)780 static void iommu_dma_sync_single_for_cpu(struct device *dev,
781 		dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
782 {
783 	phys_addr_t phys;
784 
785 	if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev))
786 		return;
787 
788 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
789 	if (!dev_is_dma_coherent(dev))
790 		arch_sync_dma_for_cpu(phys, size, dir);
791 
792 	if (is_swiotlb_buffer(dev, phys))
793 		swiotlb_sync_single_for_cpu(dev, phys, size, dir);
794 }
795 
iommu_dma_sync_single_for_device(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir)796 static void iommu_dma_sync_single_for_device(struct device *dev,
797 		dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
798 {
799 	phys_addr_t phys;
800 
801 	if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev))
802 		return;
803 
804 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
805 	if (is_swiotlb_buffer(dev, phys))
806 		swiotlb_sync_single_for_device(dev, phys, size, dir);
807 
808 	if (!dev_is_dma_coherent(dev))
809 		arch_sync_dma_for_device(phys, size, dir);
810 }
811 
iommu_dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sgl,int nelems,enum dma_data_direction dir)812 static void iommu_dma_sync_sg_for_cpu(struct device *dev,
813 		struct scatterlist *sgl, int nelems,
814 		enum dma_data_direction dir)
815 {
816 	struct scatterlist *sg;
817 	int i;
818 
819 	if (dev_use_swiotlb(dev))
820 		for_each_sg(sgl, sg, nelems, i)
821 			iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
822 						      sg->length, dir);
823 	else if (!dev_is_dma_coherent(dev))
824 		for_each_sg(sgl, sg, nelems, i)
825 			arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
826 }
827 
iommu_dma_sync_sg_for_device(struct device * dev,struct scatterlist * sgl,int nelems,enum dma_data_direction dir)828 static void iommu_dma_sync_sg_for_device(struct device *dev,
829 		struct scatterlist *sgl, int nelems,
830 		enum dma_data_direction dir)
831 {
832 	struct scatterlist *sg;
833 	int i;
834 
835 	if (dev_use_swiotlb(dev))
836 		for_each_sg(sgl, sg, nelems, i)
837 			iommu_dma_sync_single_for_device(dev,
838 							 sg_dma_address(sg),
839 							 sg->length, dir);
840 	else if (!dev_is_dma_coherent(dev))
841 		for_each_sg(sgl, sg, nelems, i)
842 			arch_sync_dma_for_device(sg_phys(sg), sg->length, dir);
843 }
844 
iommu_dma_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,unsigned long attrs)845 static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
846 		unsigned long offset, size_t size, enum dma_data_direction dir,
847 		unsigned long attrs)
848 {
849 	phys_addr_t phys = page_to_phys(page) + offset;
850 	bool coherent = dev_is_dma_coherent(dev);
851 	int prot = dma_info_to_prot(dir, coherent, attrs);
852 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
853 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
854 	struct iova_domain *iovad = &cookie->iovad;
855 	dma_addr_t iova, dma_mask = dma_get_mask(dev);
856 
857 	/*
858 	 * If both the physical buffer start address and size are
859 	 * page aligned, we don't need to use a bounce page.
860 	 */
861 	if (dev_use_swiotlb(dev) && iova_offset(iovad, phys | size)) {
862 		void *padding_start;
863 		size_t padding_size, aligned_size;
864 
865 		trace_swiotlb_bounced(dev, phys, size, swiotlb_force);
866 
867 		aligned_size = iova_align(iovad, size);
868 		phys = swiotlb_tbl_map_single(dev, phys, size, aligned_size,
869 					      iova_mask(iovad), dir, attrs);
870 
871 		if (phys == DMA_MAPPING_ERROR)
872 			return DMA_MAPPING_ERROR;
873 
874 		/* Cleanup the padding area. */
875 		padding_start = phys_to_virt(phys);
876 		padding_size = aligned_size;
877 
878 		if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
879 		    (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) {
880 			padding_start += size;
881 			padding_size -= size;
882 		}
883 
884 		memset(padding_start, 0, padding_size);
885 	}
886 
887 	if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
888 		arch_sync_dma_for_device(phys, size, dir);
889 
890 	iova = __iommu_dma_map(dev, phys, size, prot, dma_mask);
891 	if (iova == DMA_MAPPING_ERROR && is_swiotlb_buffer(dev, phys))
892 		swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs);
893 	return iova;
894 }
895 
iommu_dma_unmap_page(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir,unsigned long attrs)896 static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
897 		size_t size, enum dma_data_direction dir, unsigned long attrs)
898 {
899 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
900 	phys_addr_t phys;
901 
902 	phys = iommu_iova_to_phys(domain, dma_handle);
903 	if (WARN_ON(!phys))
904 		return;
905 
906 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && !dev_is_dma_coherent(dev))
907 		arch_sync_dma_for_cpu(phys, size, dir);
908 
909 	__iommu_dma_unmap(dev, dma_handle, size);
910 
911 	if (unlikely(is_swiotlb_buffer(dev, phys)))
912 		swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs);
913 }
914 
915 /*
916  * Prepare a successfully-mapped scatterlist to give back to the caller.
917  *
918  * At this point the segments are already laid out by iommu_dma_map_sg() to
919  * avoid individually crossing any boundaries, so we merely need to check a
920  * segment's start address to avoid concatenating across one.
921  */
__finalise_sg(struct device * dev,struct scatterlist * sg,int nents,dma_addr_t dma_addr)922 static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
923 		dma_addr_t dma_addr)
924 {
925 	struct scatterlist *s, *cur = sg;
926 	unsigned long seg_mask = dma_get_seg_boundary(dev);
927 	unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
928 	int i, count = 0;
929 
930 	for_each_sg(sg, s, nents, i) {
931 		/* Restore this segment's original unaligned fields first */
932 		unsigned int s_iova_off = sg_dma_address(s);
933 		unsigned int s_length = sg_dma_len(s);
934 		unsigned int s_iova_len = s->length;
935 
936 		s->offset += s_iova_off;
937 		s->length = s_length;
938 		sg_dma_address(s) = DMA_MAPPING_ERROR;
939 		sg_dma_len(s) = 0;
940 
941 		/*
942 		 * Now fill in the real DMA data. If...
943 		 * - there is a valid output segment to append to
944 		 * - and this segment starts on an IOVA page boundary
945 		 * - but doesn't fall at a segment boundary
946 		 * - and wouldn't make the resulting output segment too long
947 		 */
948 		if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
949 		    (max_len - cur_len >= s_length)) {
950 			/* ...then concatenate it with the previous one */
951 			cur_len += s_length;
952 		} else {
953 			/* Otherwise start the next output segment */
954 			if (i > 0)
955 				cur = sg_next(cur);
956 			cur_len = s_length;
957 			count++;
958 
959 			sg_dma_address(cur) = dma_addr + s_iova_off;
960 		}
961 
962 		sg_dma_len(cur) = cur_len;
963 		dma_addr += s_iova_len;
964 
965 		if (s_length + s_iova_off < s_iova_len)
966 			cur_len = 0;
967 	}
968 	return count;
969 }
970 
971 /*
972  * If mapping failed, then just restore the original list,
973  * but making sure the DMA fields are invalidated.
974  */
__invalidate_sg(struct scatterlist * sg,int nents)975 static void __invalidate_sg(struct scatterlist *sg, int nents)
976 {
977 	struct scatterlist *s;
978 	int i;
979 
980 	for_each_sg(sg, s, nents, i) {
981 		if (sg_dma_address(s) != DMA_MAPPING_ERROR)
982 			s->offset += sg_dma_address(s);
983 		if (sg_dma_len(s))
984 			s->length = sg_dma_len(s);
985 		sg_dma_address(s) = DMA_MAPPING_ERROR;
986 		sg_dma_len(s) = 0;
987 	}
988 }
989 
iommu_dma_unmap_sg_swiotlb(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)990 static void iommu_dma_unmap_sg_swiotlb(struct device *dev, struct scatterlist *sg,
991 		int nents, enum dma_data_direction dir, unsigned long attrs)
992 {
993 	struct scatterlist *s;
994 	int i;
995 
996 	for_each_sg(sg, s, nents, i)
997 		iommu_dma_unmap_page(dev, sg_dma_address(s),
998 				sg_dma_len(s), dir, attrs);
999 }
1000 
iommu_dma_map_sg_swiotlb(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)1001 static int iommu_dma_map_sg_swiotlb(struct device *dev, struct scatterlist *sg,
1002 		int nents, enum dma_data_direction dir, unsigned long attrs)
1003 {
1004 	struct scatterlist *s;
1005 	int i;
1006 
1007 	for_each_sg(sg, s, nents, i) {
1008 		sg_dma_address(s) = iommu_dma_map_page(dev, sg_page(s),
1009 				s->offset, s->length, dir, attrs);
1010 		if (sg_dma_address(s) == DMA_MAPPING_ERROR)
1011 			goto out_unmap;
1012 		sg_dma_len(s) = s->length;
1013 	}
1014 
1015 	return nents;
1016 
1017 out_unmap:
1018 	iommu_dma_unmap_sg_swiotlb(dev, sg, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
1019 	return -EIO;
1020 }
1021 
1022 /*
1023  * The DMA API client is passing in a scatterlist which could describe
1024  * any old buffer layout, but the IOMMU API requires everything to be
1025  * aligned to IOMMU pages. Hence the need for this complicated bit of
1026  * impedance-matching, to be able to hand off a suitably-aligned list,
1027  * but still preserve the original offsets and sizes for the caller.
1028  */
iommu_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)1029 static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
1030 		int nents, enum dma_data_direction dir, unsigned long attrs)
1031 {
1032 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
1033 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
1034 	struct iova_domain *iovad = &cookie->iovad;
1035 	struct scatterlist *s, *prev = NULL;
1036 	int prot = dma_info_to_prot(dir, dev_is_dma_coherent(dev), attrs);
1037 	dma_addr_t iova;
1038 	size_t iova_len = 0;
1039 	unsigned long mask = dma_get_seg_boundary(dev);
1040 	ssize_t ret;
1041 	int i;
1042 
1043 	if (static_branch_unlikely(&iommu_deferred_attach_enabled)) {
1044 		ret = iommu_deferred_attach(dev, domain);
1045 		if (ret)
1046 			goto out;
1047 	}
1048 
1049 	if (dev_use_swiotlb(dev))
1050 		return iommu_dma_map_sg_swiotlb(dev, sg, nents, dir, attrs);
1051 
1052 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1053 		iommu_dma_sync_sg_for_device(dev, sg, nents, dir);
1054 
1055 	/*
1056 	 * Work out how much IOVA space we need, and align the segments to
1057 	 * IOVA granules for the IOMMU driver to handle. With some clever
1058 	 * trickery we can modify the list in-place, but reversibly, by
1059 	 * stashing the unaligned parts in the as-yet-unused DMA fields.
1060 	 */
1061 	for_each_sg(sg, s, nents, i) {
1062 		size_t s_iova_off = iova_offset(iovad, s->offset);
1063 		size_t s_length = s->length;
1064 		size_t pad_len = (mask - iova_len + 1) & mask;
1065 
1066 		sg_dma_address(s) = s_iova_off;
1067 		sg_dma_len(s) = s_length;
1068 		s->offset -= s_iova_off;
1069 		s_length = iova_align(iovad, s_length + s_iova_off);
1070 		s->length = s_length;
1071 
1072 		/*
1073 		 * Due to the alignment of our single IOVA allocation, we can
1074 		 * depend on these assumptions about the segment boundary mask:
1075 		 * - If mask size >= IOVA size, then the IOVA range cannot
1076 		 *   possibly fall across a boundary, so we don't care.
1077 		 * - If mask size < IOVA size, then the IOVA range must start
1078 		 *   exactly on a boundary, therefore we can lay things out
1079 		 *   based purely on segment lengths without needing to know
1080 		 *   the actual addresses beforehand.
1081 		 * - The mask must be a power of 2, so pad_len == 0 if
1082 		 *   iova_len == 0, thus we cannot dereference prev the first
1083 		 *   time through here (i.e. before it has a meaningful value).
1084 		 */
1085 		if (pad_len && pad_len < s_length - 1) {
1086 			prev->length += pad_len;
1087 			iova_len += pad_len;
1088 		}
1089 
1090 		iova_len += s_length;
1091 		prev = s;
1092 	}
1093 
1094 	iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
1095 	if (!iova) {
1096 		ret = -ENOMEM;
1097 		goto out_restore_sg;
1098 	}
1099 
1100 	/*
1101 	 * We'll leave any physical concatenation to the IOMMU driver's
1102 	 * implementation - it knows better than we do.
1103 	 */
1104 	ret = iommu_map_sg_atomic(domain, iova, sg, nents, prot);
1105 	if (ret < 0 || ret < iova_len)
1106 		goto out_free_iova;
1107 
1108 	return __finalise_sg(dev, sg, nents, iova);
1109 
1110 out_free_iova:
1111 	iommu_dma_free_iova(cookie, iova, iova_len, NULL);
1112 out_restore_sg:
1113 	__invalidate_sg(sg, nents);
1114 out:
1115 	if (ret != -ENOMEM)
1116 		return -EINVAL;
1117 	return ret;
1118 }
1119 
iommu_dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)1120 static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
1121 		int nents, enum dma_data_direction dir, unsigned long attrs)
1122 {
1123 	dma_addr_t start, end;
1124 	struct scatterlist *tmp;
1125 	int i;
1126 
1127 	if (dev_use_swiotlb(dev)) {
1128 		iommu_dma_unmap_sg_swiotlb(dev, sg, nents, dir, attrs);
1129 		return;
1130 	}
1131 
1132 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1133 		iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
1134 
1135 	/*
1136 	 * The scatterlist segments are mapped into a single
1137 	 * contiguous IOVA allocation, so this is incredibly easy.
1138 	 */
1139 	start = sg_dma_address(sg);
1140 	for_each_sg(sg_next(sg), tmp, nents - 1, i) {
1141 		if (sg_dma_len(tmp) == 0)
1142 			break;
1143 		sg = tmp;
1144 	}
1145 	end = sg_dma_address(sg) + sg_dma_len(sg);
1146 	__iommu_dma_unmap(dev, start, end - start);
1147 }
1148 
iommu_dma_map_resource(struct device * dev,phys_addr_t phys,size_t size,enum dma_data_direction dir,unsigned long attrs)1149 static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
1150 		size_t size, enum dma_data_direction dir, unsigned long attrs)
1151 {
1152 	return __iommu_dma_map(dev, phys, size,
1153 			dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO,
1154 			dma_get_mask(dev));
1155 }
1156 
iommu_dma_unmap_resource(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir,unsigned long attrs)1157 static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
1158 		size_t size, enum dma_data_direction dir, unsigned long attrs)
1159 {
1160 	__iommu_dma_unmap(dev, handle, size);
1161 }
1162 
__iommu_dma_free(struct device * dev,size_t size,void * cpu_addr)1163 static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)
1164 {
1165 	size_t alloc_size = PAGE_ALIGN(size);
1166 	int count = alloc_size >> PAGE_SHIFT;
1167 	struct page *page = NULL, **pages = NULL;
1168 
1169 	/* Non-coherent atomic allocation? Easy */
1170 	if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
1171 	    dma_free_from_pool(dev, cpu_addr, alloc_size))
1172 		return;
1173 
1174 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
1175 		/*
1176 		 * If it the address is remapped, then it's either non-coherent
1177 		 * or highmem CMA, or an iommu_dma_alloc_remap() construction.
1178 		 */
1179 		pages = dma_common_find_pages(cpu_addr);
1180 		if (!pages)
1181 			page = vmalloc_to_page(cpu_addr);
1182 		dma_common_free_remap(cpu_addr, alloc_size);
1183 	} else {
1184 		/* Lowmem means a coherent atomic or CMA allocation */
1185 		page = virt_to_page(cpu_addr);
1186 	}
1187 
1188 	if (pages)
1189 		__iommu_dma_free_pages(pages, count);
1190 	if (page)
1191 		dma_free_contiguous(dev, page, alloc_size);
1192 }
1193 
iommu_dma_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle,unsigned long attrs)1194 static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
1195 		dma_addr_t handle, unsigned long attrs)
1196 {
1197 	__iommu_dma_unmap(dev, handle, size);
1198 	__iommu_dma_free(dev, size, cpu_addr);
1199 }
1200 
iommu_dma_alloc_pages(struct device * dev,size_t size,struct page ** pagep,gfp_t gfp,unsigned long attrs)1201 static void *iommu_dma_alloc_pages(struct device *dev, size_t size,
1202 		struct page **pagep, gfp_t gfp, unsigned long attrs)
1203 {
1204 	bool coherent = dev_is_dma_coherent(dev);
1205 	size_t alloc_size = PAGE_ALIGN(size);
1206 	int node = dev_to_node(dev);
1207 	struct page *page = NULL;
1208 	void *cpu_addr;
1209 
1210 	page = dma_alloc_contiguous(dev, alloc_size, gfp);
1211 	if (!page)
1212 		page = alloc_pages_node(node, gfp, get_order(alloc_size));
1213 	if (!page)
1214 		return NULL;
1215 
1216 	if (IS_ENABLED(CONFIG_DMA_REMAP) && (!coherent || PageHighMem(page))) {
1217 		pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs);
1218 
1219 		cpu_addr = dma_common_contiguous_remap(page, alloc_size,
1220 				prot, __builtin_return_address(0));
1221 		if (!cpu_addr)
1222 			goto out_free_pages;
1223 
1224 		if (!coherent)
1225 			arch_dma_prep_coherent(page, size);
1226 	} else {
1227 		cpu_addr = page_address(page);
1228 	}
1229 
1230 	*pagep = page;
1231 	memset(cpu_addr, 0, alloc_size);
1232 	return cpu_addr;
1233 out_free_pages:
1234 	dma_free_contiguous(dev, page, alloc_size);
1235 	return NULL;
1236 }
1237 
iommu_dma_alloc(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp,unsigned long attrs)1238 static void *iommu_dma_alloc(struct device *dev, size_t size,
1239 		dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
1240 {
1241 	bool coherent = dev_is_dma_coherent(dev);
1242 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
1243 	struct page *page = NULL;
1244 	void *cpu_addr;
1245 
1246 	gfp |= __GFP_ZERO;
1247 
1248 	if (IS_ENABLED(CONFIG_DMA_REMAP) && gfpflags_allow_blocking(gfp) &&
1249 	    !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
1250 		return iommu_dma_alloc_remap(dev, size, handle, gfp,
1251 				dma_pgprot(dev, PAGE_KERNEL, attrs), attrs);
1252 	}
1253 
1254 	if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
1255 	    !gfpflags_allow_blocking(gfp) && !coherent)
1256 		page = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &cpu_addr,
1257 					       gfp, NULL);
1258 	else
1259 		cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs);
1260 	if (!cpu_addr)
1261 		return NULL;
1262 
1263 	*handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot,
1264 			dev->coherent_dma_mask);
1265 	if (*handle == DMA_MAPPING_ERROR) {
1266 		__iommu_dma_free(dev, size, cpu_addr);
1267 		return NULL;
1268 	}
1269 
1270 	return cpu_addr;
1271 }
1272 
iommu_dma_mmap(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs)1273 static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
1274 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
1275 		unsigned long attrs)
1276 {
1277 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1278 	unsigned long pfn, off = vma->vm_pgoff;
1279 	int ret;
1280 
1281 	vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
1282 
1283 	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
1284 		return ret;
1285 
1286 	if (off >= nr_pages || vma_pages(vma) > nr_pages - off)
1287 		return -ENXIO;
1288 
1289 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
1290 		struct page **pages = dma_common_find_pages(cpu_addr);
1291 
1292 		if (pages)
1293 			return vm_map_pages(vma, pages, nr_pages);
1294 		pfn = vmalloc_to_pfn(cpu_addr);
1295 	} else {
1296 		pfn = page_to_pfn(virt_to_page(cpu_addr));
1297 	}
1298 
1299 	return remap_pfn_range(vma, vma->vm_start, pfn + off,
1300 			       vma->vm_end - vma->vm_start,
1301 			       vma->vm_page_prot);
1302 }
1303 
iommu_dma_get_sgtable(struct device * dev,struct sg_table * sgt,void * cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs)1304 static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
1305 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
1306 		unsigned long attrs)
1307 {
1308 	struct page *page;
1309 	int ret;
1310 
1311 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
1312 		struct page **pages = dma_common_find_pages(cpu_addr);
1313 
1314 		if (pages) {
1315 			return sg_alloc_table_from_pages(sgt, pages,
1316 					PAGE_ALIGN(size) >> PAGE_SHIFT,
1317 					0, size, GFP_KERNEL);
1318 		}
1319 
1320 		page = vmalloc_to_page(cpu_addr);
1321 	} else {
1322 		page = virt_to_page(cpu_addr);
1323 	}
1324 
1325 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
1326 	if (!ret)
1327 		sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
1328 	return ret;
1329 }
1330 
iommu_dma_get_merge_boundary(struct device * dev)1331 static unsigned long iommu_dma_get_merge_boundary(struct device *dev)
1332 {
1333 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
1334 
1335 	return (1UL << __ffs(domain->pgsize_bitmap)) - 1;
1336 }
1337 
1338 static const struct dma_map_ops iommu_dma_ops = {
1339 	.alloc			= iommu_dma_alloc,
1340 	.free			= iommu_dma_free,
1341 	.alloc_pages		= dma_common_alloc_pages,
1342 	.free_pages		= dma_common_free_pages,
1343 #ifdef CONFIG_DMA_REMAP
1344 	.alloc_noncontiguous	= iommu_dma_alloc_noncontiguous,
1345 	.free_noncontiguous	= iommu_dma_free_noncontiguous,
1346 #endif
1347 	.mmap			= iommu_dma_mmap,
1348 	.get_sgtable		= iommu_dma_get_sgtable,
1349 	.map_page		= iommu_dma_map_page,
1350 	.unmap_page		= iommu_dma_unmap_page,
1351 	.map_sg			= iommu_dma_map_sg,
1352 	.unmap_sg		= iommu_dma_unmap_sg,
1353 	.sync_single_for_cpu	= iommu_dma_sync_single_for_cpu,
1354 	.sync_single_for_device	= iommu_dma_sync_single_for_device,
1355 	.sync_sg_for_cpu	= iommu_dma_sync_sg_for_cpu,
1356 	.sync_sg_for_device	= iommu_dma_sync_sg_for_device,
1357 	.map_resource		= iommu_dma_map_resource,
1358 	.unmap_resource		= iommu_dma_unmap_resource,
1359 	.get_merge_boundary	= iommu_dma_get_merge_boundary,
1360 };
1361 
1362 /*
1363  * The IOMMU core code allocates the default DMA domain, which the underlying
1364  * IOMMU driver needs to support via the dma-iommu layer.
1365  */
iommu_setup_dma_ops(struct device * dev,u64 dma_base,u64 dma_limit)1366 void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit)
1367 {
1368 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1369 
1370 	if (!domain)
1371 		goto out_err;
1372 
1373 	/*
1374 	 * The IOMMU core code allocates the default DMA domain, which the
1375 	 * underlying IOMMU driver needs to support via the dma-iommu layer.
1376 	 */
1377 	if (iommu_is_dma_domain(domain)) {
1378 		if (iommu_dma_init_domain(domain, dma_base, dma_limit, dev))
1379 			goto out_err;
1380 		dev->dma_ops = &iommu_dma_ops;
1381 	}
1382 
1383 	return;
1384 out_err:
1385 	 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
1386 		 dev_name(dev));
1387 }
1388 EXPORT_SYMBOL_GPL(iommu_setup_dma_ops);
1389 
iommu_dma_get_msi_page(struct device * dev,phys_addr_t msi_addr,struct iommu_domain * domain)1390 static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
1391 		phys_addr_t msi_addr, struct iommu_domain *domain)
1392 {
1393 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
1394 	struct iommu_dma_msi_page *msi_page;
1395 	dma_addr_t iova;
1396 	int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1397 	size_t size = cookie_msi_granule(cookie);
1398 
1399 	msi_addr &= ~(phys_addr_t)(size - 1);
1400 	list_for_each_entry(msi_page, &cookie->msi_page_list, list)
1401 		if (msi_page->phys == msi_addr)
1402 			return msi_page;
1403 
1404 	msi_page = kzalloc(sizeof(*msi_page), GFP_KERNEL);
1405 	if (!msi_page)
1406 		return NULL;
1407 
1408 	iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
1409 	if (!iova)
1410 		goto out_free_page;
1411 
1412 	if (iommu_map(domain, iova, msi_addr, size, prot))
1413 		goto out_free_iova;
1414 
1415 	INIT_LIST_HEAD(&msi_page->list);
1416 	msi_page->phys = msi_addr;
1417 	msi_page->iova = iova;
1418 	list_add(&msi_page->list, &cookie->msi_page_list);
1419 	return msi_page;
1420 
1421 out_free_iova:
1422 	iommu_dma_free_iova(cookie, iova, size, NULL);
1423 out_free_page:
1424 	kfree(msi_page);
1425 	return NULL;
1426 }
1427 
iommu_dma_prepare_msi(struct msi_desc * desc,phys_addr_t msi_addr)1428 int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
1429 {
1430 	struct device *dev = msi_desc_to_dev(desc);
1431 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1432 	struct iommu_dma_msi_page *msi_page;
1433 	static DEFINE_MUTEX(msi_prepare_lock); /* see below */
1434 
1435 	if (!domain || !domain->iova_cookie) {
1436 		desc->iommu_cookie = NULL;
1437 		return 0;
1438 	}
1439 
1440 	/*
1441 	 * In fact the whole prepare operation should already be serialised by
1442 	 * irq_domain_mutex further up the callchain, but that's pretty subtle
1443 	 * on its own, so consider this locking as failsafe documentation...
1444 	 */
1445 	mutex_lock(&msi_prepare_lock);
1446 	msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
1447 	mutex_unlock(&msi_prepare_lock);
1448 
1449 	msi_desc_set_iommu_cookie(desc, msi_page);
1450 
1451 	if (!msi_page)
1452 		return -ENOMEM;
1453 	return 0;
1454 }
1455 
iommu_dma_compose_msi_msg(struct msi_desc * desc,struct msi_msg * msg)1456 void iommu_dma_compose_msi_msg(struct msi_desc *desc,
1457 			       struct msi_msg *msg)
1458 {
1459 	struct device *dev = msi_desc_to_dev(desc);
1460 	const struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1461 	const struct iommu_dma_msi_page *msi_page;
1462 
1463 	msi_page = msi_desc_get_iommu_cookie(desc);
1464 
1465 	if (!domain || !domain->iova_cookie || WARN_ON(!msi_page))
1466 		return;
1467 
1468 	msg->address_hi = upper_32_bits(msi_page->iova);
1469 	msg->address_lo &= cookie_msi_granule(domain->iova_cookie) - 1;
1470 	msg->address_lo += lower_32_bits(msi_page->iova);
1471 }
1472 
iommu_dma_init(void)1473 static int iommu_dma_init(void)
1474 {
1475 	if (is_kdump_kernel())
1476 		static_branch_enable(&iommu_deferred_attach_enabled);
1477 
1478 	return iova_cache_get();
1479 }
1480 arch_initcall(iommu_dma_init);
1481