xref: /linux/drivers/iommu/dma-iommu.c (revision 61307b7be41a1f1039d1d1368810a1d92cb97b44)
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/atomic.h>
13 #include <linux/crash_dump.h>
14 #include <linux/device.h>
15 #include <linux/dma-direct.h>
16 #include <linux/dma-map-ops.h>
17 #include <linux/gfp.h>
18 #include <linux/huge_mm.h>
19 #include <linux/iommu.h>
20 #include <linux/iova.h>
21 #include <linux/irq.h>
22 #include <linux/list_sort.h>
23 #include <linux/memremap.h>
24 #include <linux/mm.h>
25 #include <linux/mutex.h>
26 #include <linux/of_iommu.h>
27 #include <linux/pci.h>
28 #include <linux/scatterlist.h>
29 #include <linux/spinlock.h>
30 #include <linux/swiotlb.h>
31 #include <linux/vmalloc.h>
32 #include <trace/events/swiotlb.h>
33 
34 #include "dma-iommu.h"
35 #include "iommu-pages.h"
36 
37 struct iommu_dma_msi_page {
38 	struct list_head	list;
39 	dma_addr_t		iova;
40 	phys_addr_t		phys;
41 };
42 
43 enum iommu_dma_cookie_type {
44 	IOMMU_DMA_IOVA_COOKIE,
45 	IOMMU_DMA_MSI_COOKIE,
46 };
47 
48 enum iommu_dma_queue_type {
49 	IOMMU_DMA_OPTS_PER_CPU_QUEUE,
50 	IOMMU_DMA_OPTS_SINGLE_QUEUE,
51 };
52 
53 struct iommu_dma_options {
54 	enum iommu_dma_queue_type qt;
55 	size_t		fq_size;
56 	unsigned int	fq_timeout;
57 };
58 
59 struct iommu_dma_cookie {
60 	enum iommu_dma_cookie_type	type;
61 	union {
62 		/* Full allocator for IOMMU_DMA_IOVA_COOKIE */
63 		struct {
64 			struct iova_domain	iovad;
65 			/* Flush queue */
66 			union {
67 				struct iova_fq	*single_fq;
68 				struct iova_fq	__percpu *percpu_fq;
69 			};
70 			/* Number of TLB flushes that have been started */
71 			atomic64_t		fq_flush_start_cnt;
72 			/* Number of TLB flushes that have been finished */
73 			atomic64_t		fq_flush_finish_cnt;
74 			/* Timer to regularily empty the flush queues */
75 			struct timer_list	fq_timer;
76 			/* 1 when timer is active, 0 when not */
77 			atomic_t		fq_timer_on;
78 		};
79 		/* Trivial linear page allocator for IOMMU_DMA_MSI_COOKIE */
80 		dma_addr_t		msi_iova;
81 	};
82 	struct list_head		msi_page_list;
83 
84 	/* Domain for flush queue callback; NULL if flush queue not in use */
85 	struct iommu_domain		*fq_domain;
86 	/* Options for dma-iommu use */
87 	struct iommu_dma_options	options;
88 	struct mutex			mutex;
89 };
90 
91 static DEFINE_STATIC_KEY_FALSE(iommu_deferred_attach_enabled);
92 bool iommu_dma_forcedac __read_mostly;
93 
94 static int __init iommu_dma_forcedac_setup(char *str)
95 {
96 	int ret = kstrtobool(str, &iommu_dma_forcedac);
97 
98 	if (!ret && iommu_dma_forcedac)
99 		pr_info("Forcing DAC for PCI devices\n");
100 	return ret;
101 }
102 early_param("iommu.forcedac", iommu_dma_forcedac_setup);
103 
104 /* Number of entries per flush queue */
105 #define IOVA_DEFAULT_FQ_SIZE	256
106 #define IOVA_SINGLE_FQ_SIZE	32768
107 
108 /* Timeout (in ms) after which entries are flushed from the queue */
109 #define IOVA_DEFAULT_FQ_TIMEOUT	10
110 #define IOVA_SINGLE_FQ_TIMEOUT	1000
111 
112 /* Flush queue entry for deferred flushing */
113 struct iova_fq_entry {
114 	unsigned long iova_pfn;
115 	unsigned long pages;
116 	struct list_head freelist;
117 	u64 counter; /* Flush counter when this entry was added */
118 };
119 
120 /* Per-CPU flush queue structure */
121 struct iova_fq {
122 	spinlock_t lock;
123 	unsigned int head, tail;
124 	unsigned int mod_mask;
125 	struct iova_fq_entry entries[];
126 };
127 
128 #define fq_ring_for_each(i, fq) \
129 	for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) & (fq)->mod_mask)
130 
131 static inline bool fq_full(struct iova_fq *fq)
132 {
133 	assert_spin_locked(&fq->lock);
134 	return (((fq->tail + 1) & fq->mod_mask) == fq->head);
135 }
136 
137 static inline unsigned int fq_ring_add(struct iova_fq *fq)
138 {
139 	unsigned int idx = fq->tail;
140 
141 	assert_spin_locked(&fq->lock);
142 
143 	fq->tail = (idx + 1) & fq->mod_mask;
144 
145 	return idx;
146 }
147 
148 static void fq_ring_free_locked(struct iommu_dma_cookie *cookie, struct iova_fq *fq)
149 {
150 	u64 counter = atomic64_read(&cookie->fq_flush_finish_cnt);
151 	unsigned int idx;
152 
153 	assert_spin_locked(&fq->lock);
154 
155 	fq_ring_for_each(idx, fq) {
156 
157 		if (fq->entries[idx].counter >= counter)
158 			break;
159 
160 		iommu_put_pages_list(&fq->entries[idx].freelist);
161 		free_iova_fast(&cookie->iovad,
162 			       fq->entries[idx].iova_pfn,
163 			       fq->entries[idx].pages);
164 
165 		fq->head = (fq->head + 1) & fq->mod_mask;
166 	}
167 }
168 
169 static void fq_ring_free(struct iommu_dma_cookie *cookie, struct iova_fq *fq)
170 {
171 	unsigned long flags;
172 
173 	spin_lock_irqsave(&fq->lock, flags);
174 	fq_ring_free_locked(cookie, fq);
175 	spin_unlock_irqrestore(&fq->lock, flags);
176 }
177 
178 static void fq_flush_iotlb(struct iommu_dma_cookie *cookie)
179 {
180 	atomic64_inc(&cookie->fq_flush_start_cnt);
181 	cookie->fq_domain->ops->flush_iotlb_all(cookie->fq_domain);
182 	atomic64_inc(&cookie->fq_flush_finish_cnt);
183 }
184 
185 static void fq_flush_timeout(struct timer_list *t)
186 {
187 	struct iommu_dma_cookie *cookie = from_timer(cookie, t, fq_timer);
188 	int cpu;
189 
190 	atomic_set(&cookie->fq_timer_on, 0);
191 	fq_flush_iotlb(cookie);
192 
193 	if (cookie->options.qt == IOMMU_DMA_OPTS_SINGLE_QUEUE) {
194 		fq_ring_free(cookie, cookie->single_fq);
195 	} else {
196 		for_each_possible_cpu(cpu)
197 			fq_ring_free(cookie, per_cpu_ptr(cookie->percpu_fq, cpu));
198 	}
199 }
200 
201 static void queue_iova(struct iommu_dma_cookie *cookie,
202 		unsigned long pfn, unsigned long pages,
203 		struct list_head *freelist)
204 {
205 	struct iova_fq *fq;
206 	unsigned long flags;
207 	unsigned int idx;
208 
209 	/*
210 	 * Order against the IOMMU driver's pagetable update from unmapping
211 	 * @pte, to guarantee that fq_flush_iotlb() observes that if called
212 	 * from a different CPU before we release the lock below. Full barrier
213 	 * so it also pairs with iommu_dma_init_fq() to avoid seeing partially
214 	 * written fq state here.
215 	 */
216 	smp_mb();
217 
218 	if (cookie->options.qt == IOMMU_DMA_OPTS_SINGLE_QUEUE)
219 		fq = cookie->single_fq;
220 	else
221 		fq = raw_cpu_ptr(cookie->percpu_fq);
222 
223 	spin_lock_irqsave(&fq->lock, flags);
224 
225 	/*
226 	 * First remove all entries from the flush queue that have already been
227 	 * flushed out on another CPU. This makes the fq_full() check below less
228 	 * likely to be true.
229 	 */
230 	fq_ring_free_locked(cookie, fq);
231 
232 	if (fq_full(fq)) {
233 		fq_flush_iotlb(cookie);
234 		fq_ring_free_locked(cookie, fq);
235 	}
236 
237 	idx = fq_ring_add(fq);
238 
239 	fq->entries[idx].iova_pfn = pfn;
240 	fq->entries[idx].pages    = pages;
241 	fq->entries[idx].counter  = atomic64_read(&cookie->fq_flush_start_cnt);
242 	list_splice(freelist, &fq->entries[idx].freelist);
243 
244 	spin_unlock_irqrestore(&fq->lock, flags);
245 
246 	/* Avoid false sharing as much as possible. */
247 	if (!atomic_read(&cookie->fq_timer_on) &&
248 	    !atomic_xchg(&cookie->fq_timer_on, 1))
249 		mod_timer(&cookie->fq_timer,
250 			  jiffies + msecs_to_jiffies(cookie->options.fq_timeout));
251 }
252 
253 static void iommu_dma_free_fq_single(struct iova_fq *fq)
254 {
255 	int idx;
256 
257 	fq_ring_for_each(idx, fq)
258 		iommu_put_pages_list(&fq->entries[idx].freelist);
259 	vfree(fq);
260 }
261 
262 static void iommu_dma_free_fq_percpu(struct iova_fq __percpu *percpu_fq)
263 {
264 	int cpu, idx;
265 
266 	/* The IOVAs will be torn down separately, so just free our queued pages */
267 	for_each_possible_cpu(cpu) {
268 		struct iova_fq *fq = per_cpu_ptr(percpu_fq, cpu);
269 
270 		fq_ring_for_each(idx, fq)
271 			iommu_put_pages_list(&fq->entries[idx].freelist);
272 	}
273 
274 	free_percpu(percpu_fq);
275 }
276 
277 static void iommu_dma_free_fq(struct iommu_dma_cookie *cookie)
278 {
279 	if (!cookie->fq_domain)
280 		return;
281 
282 	del_timer_sync(&cookie->fq_timer);
283 	if (cookie->options.qt == IOMMU_DMA_OPTS_SINGLE_QUEUE)
284 		iommu_dma_free_fq_single(cookie->single_fq);
285 	else
286 		iommu_dma_free_fq_percpu(cookie->percpu_fq);
287 }
288 
289 static void iommu_dma_init_one_fq(struct iova_fq *fq, size_t fq_size)
290 {
291 	int i;
292 
293 	fq->head = 0;
294 	fq->tail = 0;
295 	fq->mod_mask = fq_size - 1;
296 
297 	spin_lock_init(&fq->lock);
298 
299 	for (i = 0; i < fq_size; i++)
300 		INIT_LIST_HEAD(&fq->entries[i].freelist);
301 }
302 
303 static int iommu_dma_init_fq_single(struct iommu_dma_cookie *cookie)
304 {
305 	size_t fq_size = cookie->options.fq_size;
306 	struct iova_fq *queue;
307 
308 	queue = vmalloc(struct_size(queue, entries, fq_size));
309 	if (!queue)
310 		return -ENOMEM;
311 	iommu_dma_init_one_fq(queue, fq_size);
312 	cookie->single_fq = queue;
313 
314 	return 0;
315 }
316 
317 static int iommu_dma_init_fq_percpu(struct iommu_dma_cookie *cookie)
318 {
319 	size_t fq_size = cookie->options.fq_size;
320 	struct iova_fq __percpu *queue;
321 	int cpu;
322 
323 	queue = __alloc_percpu(struct_size(queue, entries, fq_size),
324 			       __alignof__(*queue));
325 	if (!queue)
326 		return -ENOMEM;
327 
328 	for_each_possible_cpu(cpu)
329 		iommu_dma_init_one_fq(per_cpu_ptr(queue, cpu), fq_size);
330 	cookie->percpu_fq = queue;
331 	return 0;
332 }
333 
334 /* sysfs updates are serialised by the mutex of the group owning @domain */
335 int iommu_dma_init_fq(struct iommu_domain *domain)
336 {
337 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
338 	int rc;
339 
340 	if (cookie->fq_domain)
341 		return 0;
342 
343 	atomic64_set(&cookie->fq_flush_start_cnt,  0);
344 	atomic64_set(&cookie->fq_flush_finish_cnt, 0);
345 
346 	if (cookie->options.qt == IOMMU_DMA_OPTS_SINGLE_QUEUE)
347 		rc = iommu_dma_init_fq_single(cookie);
348 	else
349 		rc = iommu_dma_init_fq_percpu(cookie);
350 
351 	if (rc) {
352 		pr_warn("iova flush queue initialization failed\n");
353 		return -ENOMEM;
354 	}
355 
356 	timer_setup(&cookie->fq_timer, fq_flush_timeout, 0);
357 	atomic_set(&cookie->fq_timer_on, 0);
358 	/*
359 	 * Prevent incomplete fq state being observable. Pairs with path from
360 	 * __iommu_dma_unmap() through iommu_dma_free_iova() to queue_iova()
361 	 */
362 	smp_wmb();
363 	WRITE_ONCE(cookie->fq_domain, domain);
364 	return 0;
365 }
366 
367 static inline size_t cookie_msi_granule(struct iommu_dma_cookie *cookie)
368 {
369 	if (cookie->type == IOMMU_DMA_IOVA_COOKIE)
370 		return cookie->iovad.granule;
371 	return PAGE_SIZE;
372 }
373 
374 static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
375 {
376 	struct iommu_dma_cookie *cookie;
377 
378 	cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
379 	if (cookie) {
380 		INIT_LIST_HEAD(&cookie->msi_page_list);
381 		cookie->type = type;
382 	}
383 	return cookie;
384 }
385 
386 /**
387  * iommu_get_dma_cookie - Acquire DMA-API resources for a domain
388  * @domain: IOMMU domain to prepare for DMA-API usage
389  */
390 int iommu_get_dma_cookie(struct iommu_domain *domain)
391 {
392 	if (domain->iova_cookie)
393 		return -EEXIST;
394 
395 	domain->iova_cookie = cookie_alloc(IOMMU_DMA_IOVA_COOKIE);
396 	if (!domain->iova_cookie)
397 		return -ENOMEM;
398 
399 	mutex_init(&domain->iova_cookie->mutex);
400 	return 0;
401 }
402 
403 /**
404  * iommu_get_msi_cookie - Acquire just MSI remapping resources
405  * @domain: IOMMU domain to prepare
406  * @base: Start address of IOVA region for MSI mappings
407  *
408  * Users who manage their own IOVA allocation and do not want DMA API support,
409  * but would still like to take advantage of automatic MSI remapping, can use
410  * this to initialise their own domain appropriately. Users should reserve a
411  * contiguous IOVA region, starting at @base, large enough to accommodate the
412  * number of PAGE_SIZE mappings necessary to cover every MSI doorbell address
413  * used by the devices attached to @domain.
414  */
415 int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
416 {
417 	struct iommu_dma_cookie *cookie;
418 
419 	if (domain->type != IOMMU_DOMAIN_UNMANAGED)
420 		return -EINVAL;
421 
422 	if (domain->iova_cookie)
423 		return -EEXIST;
424 
425 	cookie = cookie_alloc(IOMMU_DMA_MSI_COOKIE);
426 	if (!cookie)
427 		return -ENOMEM;
428 
429 	cookie->msi_iova = base;
430 	domain->iova_cookie = cookie;
431 	return 0;
432 }
433 EXPORT_SYMBOL(iommu_get_msi_cookie);
434 
435 /**
436  * iommu_put_dma_cookie - Release a domain's DMA mapping resources
437  * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() or
438  *          iommu_get_msi_cookie()
439  */
440 void iommu_put_dma_cookie(struct iommu_domain *domain)
441 {
442 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
443 	struct iommu_dma_msi_page *msi, *tmp;
444 
445 	if (!cookie)
446 		return;
447 
448 	if (cookie->type == IOMMU_DMA_IOVA_COOKIE && cookie->iovad.granule) {
449 		iommu_dma_free_fq(cookie);
450 		put_iova_domain(&cookie->iovad);
451 	}
452 
453 	list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) {
454 		list_del(&msi->list);
455 		kfree(msi);
456 	}
457 	kfree(cookie);
458 	domain->iova_cookie = NULL;
459 }
460 
461 /**
462  * iommu_dma_get_resv_regions - Reserved region driver helper
463  * @dev: Device from iommu_get_resv_regions()
464  * @list: Reserved region list from iommu_get_resv_regions()
465  *
466  * IOMMU drivers can use this to implement their .get_resv_regions callback
467  * for general non-IOMMU-specific reservations. Currently, this covers GICv3
468  * ITS region reservation on ACPI based ARM platforms that may require HW MSI
469  * reservation.
470  */
471 void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)
472 {
473 
474 	if (!is_of_node(dev_iommu_fwspec_get(dev)->iommu_fwnode))
475 		iort_iommu_get_resv_regions(dev, list);
476 
477 	if (dev->of_node)
478 		of_iommu_get_resv_regions(dev, list);
479 }
480 EXPORT_SYMBOL(iommu_dma_get_resv_regions);
481 
482 static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie,
483 		phys_addr_t start, phys_addr_t end)
484 {
485 	struct iova_domain *iovad = &cookie->iovad;
486 	struct iommu_dma_msi_page *msi_page;
487 	int i, num_pages;
488 
489 	start -= iova_offset(iovad, start);
490 	num_pages = iova_align(iovad, end - start) >> iova_shift(iovad);
491 
492 	for (i = 0; i < num_pages; i++) {
493 		msi_page = kmalloc(sizeof(*msi_page), GFP_KERNEL);
494 		if (!msi_page)
495 			return -ENOMEM;
496 
497 		msi_page->phys = start;
498 		msi_page->iova = start;
499 		INIT_LIST_HEAD(&msi_page->list);
500 		list_add(&msi_page->list, &cookie->msi_page_list);
501 		start += iovad->granule;
502 	}
503 
504 	return 0;
505 }
506 
507 static int iommu_dma_ranges_sort(void *priv, const struct list_head *a,
508 		const struct list_head *b)
509 {
510 	struct resource_entry *res_a = list_entry(a, typeof(*res_a), node);
511 	struct resource_entry *res_b = list_entry(b, typeof(*res_b), node);
512 
513 	return res_a->res->start > res_b->res->start;
514 }
515 
516 static int iova_reserve_pci_windows(struct pci_dev *dev,
517 		struct iova_domain *iovad)
518 {
519 	struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
520 	struct resource_entry *window;
521 	unsigned long lo, hi;
522 	phys_addr_t start = 0, end;
523 
524 	resource_list_for_each_entry(window, &bridge->windows) {
525 		if (resource_type(window->res) != IORESOURCE_MEM)
526 			continue;
527 
528 		lo = iova_pfn(iovad, window->res->start - window->offset);
529 		hi = iova_pfn(iovad, window->res->end - window->offset);
530 		reserve_iova(iovad, lo, hi);
531 	}
532 
533 	/* Get reserved DMA windows from host bridge */
534 	list_sort(NULL, &bridge->dma_ranges, iommu_dma_ranges_sort);
535 	resource_list_for_each_entry(window, &bridge->dma_ranges) {
536 		end = window->res->start - window->offset;
537 resv_iova:
538 		if (end > start) {
539 			lo = iova_pfn(iovad, start);
540 			hi = iova_pfn(iovad, end);
541 			reserve_iova(iovad, lo, hi);
542 		} else if (end < start) {
543 			/* DMA ranges should be non-overlapping */
544 			dev_err(&dev->dev,
545 				"Failed to reserve IOVA [%pa-%pa]\n",
546 				&start, &end);
547 			return -EINVAL;
548 		}
549 
550 		start = window->res->end - window->offset + 1;
551 		/* If window is last entry */
552 		if (window->node.next == &bridge->dma_ranges &&
553 		    end != ~(phys_addr_t)0) {
554 			end = ~(phys_addr_t)0;
555 			goto resv_iova;
556 		}
557 	}
558 
559 	return 0;
560 }
561 
562 static int iova_reserve_iommu_regions(struct device *dev,
563 		struct iommu_domain *domain)
564 {
565 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
566 	struct iova_domain *iovad = &cookie->iovad;
567 	struct iommu_resv_region *region;
568 	LIST_HEAD(resv_regions);
569 	int ret = 0;
570 
571 	if (dev_is_pci(dev)) {
572 		ret = iova_reserve_pci_windows(to_pci_dev(dev), iovad);
573 		if (ret)
574 			return ret;
575 	}
576 
577 	iommu_get_resv_regions(dev, &resv_regions);
578 	list_for_each_entry(region, &resv_regions, list) {
579 		unsigned long lo, hi;
580 
581 		/* We ARE the software that manages these! */
582 		if (region->type == IOMMU_RESV_SW_MSI)
583 			continue;
584 
585 		lo = iova_pfn(iovad, region->start);
586 		hi = iova_pfn(iovad, region->start + region->length - 1);
587 		reserve_iova(iovad, lo, hi);
588 
589 		if (region->type == IOMMU_RESV_MSI)
590 			ret = cookie_init_hw_msi_region(cookie, region->start,
591 					region->start + region->length);
592 		if (ret)
593 			break;
594 	}
595 	iommu_put_resv_regions(dev, &resv_regions);
596 
597 	return ret;
598 }
599 
600 static bool dev_is_untrusted(struct device *dev)
601 {
602 	return dev_is_pci(dev) && to_pci_dev(dev)->untrusted;
603 }
604 
605 static bool dev_use_swiotlb(struct device *dev, size_t size,
606 			    enum dma_data_direction dir)
607 {
608 	return IS_ENABLED(CONFIG_SWIOTLB) &&
609 		(dev_is_untrusted(dev) ||
610 		 dma_kmalloc_needs_bounce(dev, size, dir));
611 }
612 
613 static bool dev_use_sg_swiotlb(struct device *dev, struct scatterlist *sg,
614 			       int nents, enum dma_data_direction dir)
615 {
616 	struct scatterlist *s;
617 	int i;
618 
619 	if (!IS_ENABLED(CONFIG_SWIOTLB))
620 		return false;
621 
622 	if (dev_is_untrusted(dev))
623 		return true;
624 
625 	/*
626 	 * If kmalloc() buffers are not DMA-safe for this device and
627 	 * direction, check the individual lengths in the sg list. If any
628 	 * element is deemed unsafe, use the swiotlb for bouncing.
629 	 */
630 	if (!dma_kmalloc_safe(dev, dir)) {
631 		for_each_sg(sg, s, nents, i)
632 			if (!dma_kmalloc_size_aligned(s->length))
633 				return true;
634 	}
635 
636 	return false;
637 }
638 
639 /**
640  * iommu_dma_init_options - Initialize dma-iommu options
641  * @options: The options to be initialized
642  * @dev: Device the options are set for
643  *
644  * This allows tuning dma-iommu specific to device properties
645  */
646 static void iommu_dma_init_options(struct iommu_dma_options *options,
647 				   struct device *dev)
648 {
649 	/* Shadowing IOTLB flushes do better with a single large queue */
650 	if (dev->iommu->shadow_on_flush) {
651 		options->qt = IOMMU_DMA_OPTS_SINGLE_QUEUE;
652 		options->fq_timeout = IOVA_SINGLE_FQ_TIMEOUT;
653 		options->fq_size = IOVA_SINGLE_FQ_SIZE;
654 	} else {
655 		options->qt = IOMMU_DMA_OPTS_PER_CPU_QUEUE;
656 		options->fq_size = IOVA_DEFAULT_FQ_SIZE;
657 		options->fq_timeout = IOVA_DEFAULT_FQ_TIMEOUT;
658 	}
659 }
660 
661 /**
662  * iommu_dma_init_domain - Initialise a DMA mapping domain
663  * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
664  * @dev: Device the domain is being initialised for
665  *
666  * If the geometry and dma_range_map include address 0, we reserve that page
667  * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
668  * any change which could make prior IOVAs invalid will fail.
669  */
670 static int iommu_dma_init_domain(struct iommu_domain *domain, struct device *dev)
671 {
672 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
673 	const struct bus_dma_region *map = dev->dma_range_map;
674 	unsigned long order, base_pfn;
675 	struct iova_domain *iovad;
676 	int ret;
677 
678 	if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE)
679 		return -EINVAL;
680 
681 	iovad = &cookie->iovad;
682 
683 	/* Use the smallest supported page size for IOVA granularity */
684 	order = __ffs(domain->pgsize_bitmap);
685 	base_pfn = 1;
686 
687 	/* Check the domain allows at least some access to the device... */
688 	if (map) {
689 		dma_addr_t base = dma_range_map_min(map);
690 		if (base > domain->geometry.aperture_end ||
691 		    dma_range_map_max(map) < domain->geometry.aperture_start) {
692 			pr_warn("specified DMA range outside IOMMU capability\n");
693 			return -EFAULT;
694 		}
695 		/* ...then finally give it a kicking to make sure it fits */
696 		base_pfn = max(base, domain->geometry.aperture_start) >> order;
697 	}
698 
699 	/* start_pfn is always nonzero for an already-initialised domain */
700 	mutex_lock(&cookie->mutex);
701 	if (iovad->start_pfn) {
702 		if (1UL << order != iovad->granule ||
703 		    base_pfn != iovad->start_pfn) {
704 			pr_warn("Incompatible range for DMA domain\n");
705 			ret = -EFAULT;
706 			goto done_unlock;
707 		}
708 
709 		ret = 0;
710 		goto done_unlock;
711 	}
712 
713 	init_iova_domain(iovad, 1UL << order, base_pfn);
714 	ret = iova_domain_init_rcaches(iovad);
715 	if (ret)
716 		goto done_unlock;
717 
718 	iommu_dma_init_options(&cookie->options, dev);
719 
720 	/* If the FQ fails we can simply fall back to strict mode */
721 	if (domain->type == IOMMU_DOMAIN_DMA_FQ &&
722 	    (!device_iommu_capable(dev, IOMMU_CAP_DEFERRED_FLUSH) || iommu_dma_init_fq(domain)))
723 		domain->type = IOMMU_DOMAIN_DMA;
724 
725 	ret = iova_reserve_iommu_regions(dev, domain);
726 
727 done_unlock:
728 	mutex_unlock(&cookie->mutex);
729 	return ret;
730 }
731 
732 /**
733  * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
734  *                    page flags.
735  * @dir: Direction of DMA transfer
736  * @coherent: Is the DMA master cache-coherent?
737  * @attrs: DMA attributes for the mapping
738  *
739  * Return: corresponding IOMMU API page protection flags
740  */
741 static int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
742 		     unsigned long attrs)
743 {
744 	int prot = coherent ? IOMMU_CACHE : 0;
745 
746 	if (attrs & DMA_ATTR_PRIVILEGED)
747 		prot |= IOMMU_PRIV;
748 
749 	switch (dir) {
750 	case DMA_BIDIRECTIONAL:
751 		return prot | IOMMU_READ | IOMMU_WRITE;
752 	case DMA_TO_DEVICE:
753 		return prot | IOMMU_READ;
754 	case DMA_FROM_DEVICE:
755 		return prot | IOMMU_WRITE;
756 	default:
757 		return 0;
758 	}
759 }
760 
761 static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
762 		size_t size, u64 dma_limit, struct device *dev)
763 {
764 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
765 	struct iova_domain *iovad = &cookie->iovad;
766 	unsigned long shift, iova_len, iova;
767 
768 	if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
769 		cookie->msi_iova += size;
770 		return cookie->msi_iova - size;
771 	}
772 
773 	shift = iova_shift(iovad);
774 	iova_len = size >> shift;
775 
776 	dma_limit = min_not_zero(dma_limit, dev->bus_dma_limit);
777 
778 	if (domain->geometry.force_aperture)
779 		dma_limit = min(dma_limit, (u64)domain->geometry.aperture_end);
780 
781 	/*
782 	 * Try to use all the 32-bit PCI addresses first. The original SAC vs.
783 	 * DAC reasoning loses relevance with PCIe, but enough hardware and
784 	 * firmware bugs are still lurking out there that it's safest not to
785 	 * venture into the 64-bit space until necessary.
786 	 *
787 	 * If your device goes wrong after seeing the notice then likely either
788 	 * its driver is not setting DMA masks accurately, the hardware has
789 	 * some inherent bug in handling >32-bit addresses, or not all the
790 	 * expected address bits are wired up between the device and the IOMMU.
791 	 */
792 	if (dma_limit > DMA_BIT_MASK(32) && dev->iommu->pci_32bit_workaround) {
793 		iova = alloc_iova_fast(iovad, iova_len,
794 				       DMA_BIT_MASK(32) >> shift, false);
795 		if (iova)
796 			goto done;
797 
798 		dev->iommu->pci_32bit_workaround = false;
799 		dev_notice(dev, "Using %d-bit DMA addresses\n", bits_per(dma_limit));
800 	}
801 
802 	iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift, true);
803 done:
804 	return (dma_addr_t)iova << shift;
805 }
806 
807 static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
808 		dma_addr_t iova, size_t size, struct iommu_iotlb_gather *gather)
809 {
810 	struct iova_domain *iovad = &cookie->iovad;
811 
812 	/* The MSI case is only ever cleaning up its most recent allocation */
813 	if (cookie->type == IOMMU_DMA_MSI_COOKIE)
814 		cookie->msi_iova -= size;
815 	else if (gather && gather->queued)
816 		queue_iova(cookie, iova_pfn(iovad, iova),
817 				size >> iova_shift(iovad),
818 				&gather->freelist);
819 	else
820 		free_iova_fast(iovad, iova_pfn(iovad, iova),
821 				size >> iova_shift(iovad));
822 }
823 
824 static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr,
825 		size_t size)
826 {
827 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
828 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
829 	struct iova_domain *iovad = &cookie->iovad;
830 	size_t iova_off = iova_offset(iovad, dma_addr);
831 	struct iommu_iotlb_gather iotlb_gather;
832 	size_t unmapped;
833 
834 	dma_addr -= iova_off;
835 	size = iova_align(iovad, size + iova_off);
836 	iommu_iotlb_gather_init(&iotlb_gather);
837 	iotlb_gather.queued = READ_ONCE(cookie->fq_domain);
838 
839 	unmapped = iommu_unmap_fast(domain, dma_addr, size, &iotlb_gather);
840 	WARN_ON(unmapped != size);
841 
842 	if (!iotlb_gather.queued)
843 		iommu_iotlb_sync(domain, &iotlb_gather);
844 	iommu_dma_free_iova(cookie, dma_addr, size, &iotlb_gather);
845 }
846 
847 static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
848 		size_t size, int prot, u64 dma_mask)
849 {
850 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
851 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
852 	struct iova_domain *iovad = &cookie->iovad;
853 	size_t iova_off = iova_offset(iovad, phys);
854 	dma_addr_t iova;
855 
856 	if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
857 	    iommu_deferred_attach(dev, domain))
858 		return DMA_MAPPING_ERROR;
859 
860 	/* If anyone ever wants this we'd need support in the IOVA allocator */
861 	if (dev_WARN_ONCE(dev, dma_get_min_align_mask(dev) > iova_mask(iovad),
862 	    "Unsupported alignment constraint\n"))
863 		return DMA_MAPPING_ERROR;
864 
865 	size = iova_align(iovad, size + iova_off);
866 
867 	iova = iommu_dma_alloc_iova(domain, size, dma_mask, dev);
868 	if (!iova)
869 		return DMA_MAPPING_ERROR;
870 
871 	if (iommu_map(domain, iova, phys - iova_off, size, prot, GFP_ATOMIC)) {
872 		iommu_dma_free_iova(cookie, iova, size, NULL);
873 		return DMA_MAPPING_ERROR;
874 	}
875 	return iova + iova_off;
876 }
877 
878 static void __iommu_dma_free_pages(struct page **pages, int count)
879 {
880 	while (count--)
881 		__free_page(pages[count]);
882 	kvfree(pages);
883 }
884 
885 static struct page **__iommu_dma_alloc_pages(struct device *dev,
886 		unsigned int count, unsigned long order_mask, gfp_t gfp)
887 {
888 	struct page **pages;
889 	unsigned int i = 0, nid = dev_to_node(dev);
890 
891 	order_mask &= GENMASK(MAX_PAGE_ORDER, 0);
892 	if (!order_mask)
893 		return NULL;
894 
895 	pages = kvcalloc(count, sizeof(*pages), GFP_KERNEL);
896 	if (!pages)
897 		return NULL;
898 
899 	/* IOMMU can map any pages, so himem can also be used here */
900 	gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
901 
902 	while (count) {
903 		struct page *page = NULL;
904 		unsigned int order_size;
905 
906 		/*
907 		 * Higher-order allocations are a convenience rather
908 		 * than a necessity, hence using __GFP_NORETRY until
909 		 * falling back to minimum-order allocations.
910 		 */
911 		for (order_mask &= GENMASK(__fls(count), 0);
912 		     order_mask; order_mask &= ~order_size) {
913 			unsigned int order = __fls(order_mask);
914 			gfp_t alloc_flags = gfp;
915 
916 			order_size = 1U << order;
917 			if (order_mask > order_size)
918 				alloc_flags |= __GFP_NORETRY;
919 			page = alloc_pages_node(nid, alloc_flags, order);
920 			if (!page)
921 				continue;
922 			if (order)
923 				split_page(page, order);
924 			break;
925 		}
926 		if (!page) {
927 			__iommu_dma_free_pages(pages, i);
928 			return NULL;
929 		}
930 		count -= order_size;
931 		while (order_size--)
932 			pages[i++] = page++;
933 	}
934 	return pages;
935 }
936 
937 /*
938  * If size is less than PAGE_SIZE, then a full CPU page will be allocated,
939  * but an IOMMU which supports smaller pages might not map the whole thing.
940  */
941 static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
942 		size_t size, struct sg_table *sgt, gfp_t gfp, pgprot_t prot,
943 		unsigned long attrs)
944 {
945 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
946 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
947 	struct iova_domain *iovad = &cookie->iovad;
948 	bool coherent = dev_is_dma_coherent(dev);
949 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
950 	unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
951 	struct page **pages;
952 	dma_addr_t iova;
953 	ssize_t ret;
954 
955 	if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
956 	    iommu_deferred_attach(dev, domain))
957 		return NULL;
958 
959 	min_size = alloc_sizes & -alloc_sizes;
960 	if (min_size < PAGE_SIZE) {
961 		min_size = PAGE_SIZE;
962 		alloc_sizes |= PAGE_SIZE;
963 	} else {
964 		size = ALIGN(size, min_size);
965 	}
966 	if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
967 		alloc_sizes = min_size;
968 
969 	count = PAGE_ALIGN(size) >> PAGE_SHIFT;
970 	pages = __iommu_dma_alloc_pages(dev, count, alloc_sizes >> PAGE_SHIFT,
971 					gfp);
972 	if (!pages)
973 		return NULL;
974 
975 	size = iova_align(iovad, size);
976 	iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev);
977 	if (!iova)
978 		goto out_free_pages;
979 
980 	/*
981 	 * Remove the zone/policy flags from the GFP - these are applied to the
982 	 * __iommu_dma_alloc_pages() but are not used for the supporting
983 	 * internal allocations that follow.
984 	 */
985 	gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM | __GFP_COMP);
986 
987 	if (sg_alloc_table_from_pages(sgt, pages, count, 0, size, gfp))
988 		goto out_free_iova;
989 
990 	if (!(ioprot & IOMMU_CACHE)) {
991 		struct scatterlist *sg;
992 		int i;
993 
994 		for_each_sg(sgt->sgl, sg, sgt->orig_nents, i)
995 			arch_dma_prep_coherent(sg_page(sg), sg->length);
996 	}
997 
998 	ret = iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, ioprot,
999 			   gfp);
1000 	if (ret < 0 || ret < size)
1001 		goto out_free_sg;
1002 
1003 	sgt->sgl->dma_address = iova;
1004 	sgt->sgl->dma_length = size;
1005 	return pages;
1006 
1007 out_free_sg:
1008 	sg_free_table(sgt);
1009 out_free_iova:
1010 	iommu_dma_free_iova(cookie, iova, size, NULL);
1011 out_free_pages:
1012 	__iommu_dma_free_pages(pages, count);
1013 	return NULL;
1014 }
1015 
1016 static void *iommu_dma_alloc_remap(struct device *dev, size_t size,
1017 		dma_addr_t *dma_handle, gfp_t gfp, pgprot_t prot,
1018 		unsigned long attrs)
1019 {
1020 	struct page **pages;
1021 	struct sg_table sgt;
1022 	void *vaddr;
1023 
1024 	pages = __iommu_dma_alloc_noncontiguous(dev, size, &sgt, gfp, prot,
1025 						attrs);
1026 	if (!pages)
1027 		return NULL;
1028 	*dma_handle = sgt.sgl->dma_address;
1029 	sg_free_table(&sgt);
1030 	vaddr = dma_common_pages_remap(pages, size, prot,
1031 			__builtin_return_address(0));
1032 	if (!vaddr)
1033 		goto out_unmap;
1034 	return vaddr;
1035 
1036 out_unmap:
1037 	__iommu_dma_unmap(dev, *dma_handle, size);
1038 	__iommu_dma_free_pages(pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
1039 	return NULL;
1040 }
1041 
1042 static struct sg_table *iommu_dma_alloc_noncontiguous(struct device *dev,
1043 		size_t size, enum dma_data_direction dir, gfp_t gfp,
1044 		unsigned long attrs)
1045 {
1046 	struct dma_sgt_handle *sh;
1047 
1048 	sh = kmalloc(sizeof(*sh), gfp);
1049 	if (!sh)
1050 		return NULL;
1051 
1052 	sh->pages = __iommu_dma_alloc_noncontiguous(dev, size, &sh->sgt, gfp,
1053 						    PAGE_KERNEL, attrs);
1054 	if (!sh->pages) {
1055 		kfree(sh);
1056 		return NULL;
1057 	}
1058 	return &sh->sgt;
1059 }
1060 
1061 static void iommu_dma_free_noncontiguous(struct device *dev, size_t size,
1062 		struct sg_table *sgt, enum dma_data_direction dir)
1063 {
1064 	struct dma_sgt_handle *sh = sgt_handle(sgt);
1065 
1066 	__iommu_dma_unmap(dev, sgt->sgl->dma_address, size);
1067 	__iommu_dma_free_pages(sh->pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
1068 	sg_free_table(&sh->sgt);
1069 	kfree(sh);
1070 }
1071 
1072 static void iommu_dma_sync_single_for_cpu(struct device *dev,
1073 		dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
1074 {
1075 	phys_addr_t phys;
1076 
1077 	if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir))
1078 		return;
1079 
1080 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
1081 	if (!dev_is_dma_coherent(dev))
1082 		arch_sync_dma_for_cpu(phys, size, dir);
1083 
1084 	if (is_swiotlb_buffer(dev, phys))
1085 		swiotlb_sync_single_for_cpu(dev, phys, size, dir);
1086 }
1087 
1088 static void iommu_dma_sync_single_for_device(struct device *dev,
1089 		dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
1090 {
1091 	phys_addr_t phys;
1092 
1093 	if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir))
1094 		return;
1095 
1096 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
1097 	if (is_swiotlb_buffer(dev, phys))
1098 		swiotlb_sync_single_for_device(dev, phys, size, dir);
1099 
1100 	if (!dev_is_dma_coherent(dev))
1101 		arch_sync_dma_for_device(phys, size, dir);
1102 }
1103 
1104 static void iommu_dma_sync_sg_for_cpu(struct device *dev,
1105 		struct scatterlist *sgl, int nelems,
1106 		enum dma_data_direction dir)
1107 {
1108 	struct scatterlist *sg;
1109 	int i;
1110 
1111 	if (sg_dma_is_swiotlb(sgl))
1112 		for_each_sg(sgl, sg, nelems, i)
1113 			iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
1114 						      sg->length, dir);
1115 	else if (!dev_is_dma_coherent(dev))
1116 		for_each_sg(sgl, sg, nelems, i)
1117 			arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
1118 }
1119 
1120 static void iommu_dma_sync_sg_for_device(struct device *dev,
1121 		struct scatterlist *sgl, int nelems,
1122 		enum dma_data_direction dir)
1123 {
1124 	struct scatterlist *sg;
1125 	int i;
1126 
1127 	if (sg_dma_is_swiotlb(sgl))
1128 		for_each_sg(sgl, sg, nelems, i)
1129 			iommu_dma_sync_single_for_device(dev,
1130 							 sg_dma_address(sg),
1131 							 sg->length, dir);
1132 	else if (!dev_is_dma_coherent(dev))
1133 		for_each_sg(sgl, sg, nelems, i)
1134 			arch_sync_dma_for_device(sg_phys(sg), sg->length, dir);
1135 }
1136 
1137 static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
1138 		unsigned long offset, size_t size, enum dma_data_direction dir,
1139 		unsigned long attrs)
1140 {
1141 	phys_addr_t phys = page_to_phys(page) + offset;
1142 	bool coherent = dev_is_dma_coherent(dev);
1143 	int prot = dma_info_to_prot(dir, coherent, attrs);
1144 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
1145 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
1146 	struct iova_domain *iovad = &cookie->iovad;
1147 	dma_addr_t iova, dma_mask = dma_get_mask(dev);
1148 
1149 	/*
1150 	 * If both the physical buffer start address and size are
1151 	 * page aligned, we don't need to use a bounce page.
1152 	 */
1153 	if (dev_use_swiotlb(dev, size, dir) &&
1154 	    iova_offset(iovad, phys | size)) {
1155 		void *padding_start;
1156 		size_t padding_size, aligned_size;
1157 
1158 		if (!is_swiotlb_active(dev)) {
1159 			dev_warn_once(dev, "DMA bounce buffers are inactive, unable to map unaligned transaction.\n");
1160 			return DMA_MAPPING_ERROR;
1161 		}
1162 
1163 		trace_swiotlb_bounced(dev, phys, size);
1164 
1165 		aligned_size = iova_align(iovad, size);
1166 		phys = swiotlb_tbl_map_single(dev, phys, size, aligned_size,
1167 					      iova_mask(iovad), dir, attrs);
1168 
1169 		if (phys == DMA_MAPPING_ERROR)
1170 			return DMA_MAPPING_ERROR;
1171 
1172 		/* Cleanup the padding area. */
1173 		padding_start = phys_to_virt(phys);
1174 		padding_size = aligned_size;
1175 
1176 		if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
1177 		    (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) {
1178 			padding_start += size;
1179 			padding_size -= size;
1180 		}
1181 
1182 		memset(padding_start, 0, padding_size);
1183 	}
1184 
1185 	if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1186 		arch_sync_dma_for_device(phys, size, dir);
1187 
1188 	iova = __iommu_dma_map(dev, phys, size, prot, dma_mask);
1189 	if (iova == DMA_MAPPING_ERROR && is_swiotlb_buffer(dev, phys))
1190 		swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs);
1191 	return iova;
1192 }
1193 
1194 static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
1195 		size_t size, enum dma_data_direction dir, unsigned long attrs)
1196 {
1197 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
1198 	phys_addr_t phys;
1199 
1200 	phys = iommu_iova_to_phys(domain, dma_handle);
1201 	if (WARN_ON(!phys))
1202 		return;
1203 
1204 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && !dev_is_dma_coherent(dev))
1205 		arch_sync_dma_for_cpu(phys, size, dir);
1206 
1207 	__iommu_dma_unmap(dev, dma_handle, size);
1208 
1209 	if (unlikely(is_swiotlb_buffer(dev, phys)))
1210 		swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs);
1211 }
1212 
1213 /*
1214  * Prepare a successfully-mapped scatterlist to give back to the caller.
1215  *
1216  * At this point the segments are already laid out by iommu_dma_map_sg() to
1217  * avoid individually crossing any boundaries, so we merely need to check a
1218  * segment's start address to avoid concatenating across one.
1219  */
1220 static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
1221 		dma_addr_t dma_addr)
1222 {
1223 	struct scatterlist *s, *cur = sg;
1224 	unsigned long seg_mask = dma_get_seg_boundary(dev);
1225 	unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
1226 	int i, count = 0;
1227 
1228 	for_each_sg(sg, s, nents, i) {
1229 		/* Restore this segment's original unaligned fields first */
1230 		dma_addr_t s_dma_addr = sg_dma_address(s);
1231 		unsigned int s_iova_off = sg_dma_address(s);
1232 		unsigned int s_length = sg_dma_len(s);
1233 		unsigned int s_iova_len = s->length;
1234 
1235 		sg_dma_address(s) = DMA_MAPPING_ERROR;
1236 		sg_dma_len(s) = 0;
1237 
1238 		if (sg_dma_is_bus_address(s)) {
1239 			if (i > 0)
1240 				cur = sg_next(cur);
1241 
1242 			sg_dma_unmark_bus_address(s);
1243 			sg_dma_address(cur) = s_dma_addr;
1244 			sg_dma_len(cur) = s_length;
1245 			sg_dma_mark_bus_address(cur);
1246 			count++;
1247 			cur_len = 0;
1248 			continue;
1249 		}
1250 
1251 		s->offset += s_iova_off;
1252 		s->length = s_length;
1253 
1254 		/*
1255 		 * Now fill in the real DMA data. If...
1256 		 * - there is a valid output segment to append to
1257 		 * - and this segment starts on an IOVA page boundary
1258 		 * - but doesn't fall at a segment boundary
1259 		 * - and wouldn't make the resulting output segment too long
1260 		 */
1261 		if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
1262 		    (max_len - cur_len >= s_length)) {
1263 			/* ...then concatenate it with the previous one */
1264 			cur_len += s_length;
1265 		} else {
1266 			/* Otherwise start the next output segment */
1267 			if (i > 0)
1268 				cur = sg_next(cur);
1269 			cur_len = s_length;
1270 			count++;
1271 
1272 			sg_dma_address(cur) = dma_addr + s_iova_off;
1273 		}
1274 
1275 		sg_dma_len(cur) = cur_len;
1276 		dma_addr += s_iova_len;
1277 
1278 		if (s_length + s_iova_off < s_iova_len)
1279 			cur_len = 0;
1280 	}
1281 	return count;
1282 }
1283 
1284 /*
1285  * If mapping failed, then just restore the original list,
1286  * but making sure the DMA fields are invalidated.
1287  */
1288 static void __invalidate_sg(struct scatterlist *sg, int nents)
1289 {
1290 	struct scatterlist *s;
1291 	int i;
1292 
1293 	for_each_sg(sg, s, nents, i) {
1294 		if (sg_dma_is_bus_address(s)) {
1295 			sg_dma_unmark_bus_address(s);
1296 		} else {
1297 			if (sg_dma_address(s) != DMA_MAPPING_ERROR)
1298 				s->offset += sg_dma_address(s);
1299 			if (sg_dma_len(s))
1300 				s->length = sg_dma_len(s);
1301 		}
1302 		sg_dma_address(s) = DMA_MAPPING_ERROR;
1303 		sg_dma_len(s) = 0;
1304 	}
1305 }
1306 
1307 static void iommu_dma_unmap_sg_swiotlb(struct device *dev, struct scatterlist *sg,
1308 		int nents, enum dma_data_direction dir, unsigned long attrs)
1309 {
1310 	struct scatterlist *s;
1311 	int i;
1312 
1313 	for_each_sg(sg, s, nents, i)
1314 		iommu_dma_unmap_page(dev, sg_dma_address(s),
1315 				sg_dma_len(s), dir, attrs);
1316 }
1317 
1318 static int iommu_dma_map_sg_swiotlb(struct device *dev, struct scatterlist *sg,
1319 		int nents, enum dma_data_direction dir, unsigned long attrs)
1320 {
1321 	struct scatterlist *s;
1322 	int i;
1323 
1324 	sg_dma_mark_swiotlb(sg);
1325 
1326 	for_each_sg(sg, s, nents, i) {
1327 		sg_dma_address(s) = iommu_dma_map_page(dev, sg_page(s),
1328 				s->offset, s->length, dir, attrs);
1329 		if (sg_dma_address(s) == DMA_MAPPING_ERROR)
1330 			goto out_unmap;
1331 		sg_dma_len(s) = s->length;
1332 	}
1333 
1334 	return nents;
1335 
1336 out_unmap:
1337 	iommu_dma_unmap_sg_swiotlb(dev, sg, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
1338 	return -EIO;
1339 }
1340 
1341 /*
1342  * The DMA API client is passing in a scatterlist which could describe
1343  * any old buffer layout, but the IOMMU API requires everything to be
1344  * aligned to IOMMU pages. Hence the need for this complicated bit of
1345  * impedance-matching, to be able to hand off a suitably-aligned list,
1346  * but still preserve the original offsets and sizes for the caller.
1347  */
1348 static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
1349 		int nents, enum dma_data_direction dir, unsigned long attrs)
1350 {
1351 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
1352 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
1353 	struct iova_domain *iovad = &cookie->iovad;
1354 	struct scatterlist *s, *prev = NULL;
1355 	int prot = dma_info_to_prot(dir, dev_is_dma_coherent(dev), attrs);
1356 	struct pci_p2pdma_map_state p2pdma_state = {};
1357 	enum pci_p2pdma_map_type map;
1358 	dma_addr_t iova;
1359 	size_t iova_len = 0;
1360 	unsigned long mask = dma_get_seg_boundary(dev);
1361 	ssize_t ret;
1362 	int i;
1363 
1364 	if (static_branch_unlikely(&iommu_deferred_attach_enabled)) {
1365 		ret = iommu_deferred_attach(dev, domain);
1366 		if (ret)
1367 			goto out;
1368 	}
1369 
1370 	if (dev_use_sg_swiotlb(dev, sg, nents, dir))
1371 		return iommu_dma_map_sg_swiotlb(dev, sg, nents, dir, attrs);
1372 
1373 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1374 		iommu_dma_sync_sg_for_device(dev, sg, nents, dir);
1375 
1376 	/*
1377 	 * Work out how much IOVA space we need, and align the segments to
1378 	 * IOVA granules for the IOMMU driver to handle. With some clever
1379 	 * trickery we can modify the list in-place, but reversibly, by
1380 	 * stashing the unaligned parts in the as-yet-unused DMA fields.
1381 	 */
1382 	for_each_sg(sg, s, nents, i) {
1383 		size_t s_iova_off = iova_offset(iovad, s->offset);
1384 		size_t s_length = s->length;
1385 		size_t pad_len = (mask - iova_len + 1) & mask;
1386 
1387 		if (is_pci_p2pdma_page(sg_page(s))) {
1388 			map = pci_p2pdma_map_segment(&p2pdma_state, dev, s);
1389 			switch (map) {
1390 			case PCI_P2PDMA_MAP_BUS_ADDR:
1391 				/*
1392 				 * iommu_map_sg() will skip this segment as
1393 				 * it is marked as a bus address,
1394 				 * __finalise_sg() will copy the dma address
1395 				 * into the output segment.
1396 				 */
1397 				continue;
1398 			case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
1399 				/*
1400 				 * Mapping through host bridge should be
1401 				 * mapped with regular IOVAs, thus we
1402 				 * do nothing here and continue below.
1403 				 */
1404 				break;
1405 			default:
1406 				ret = -EREMOTEIO;
1407 				goto out_restore_sg;
1408 			}
1409 		}
1410 
1411 		sg_dma_address(s) = s_iova_off;
1412 		sg_dma_len(s) = s_length;
1413 		s->offset -= s_iova_off;
1414 		s_length = iova_align(iovad, s_length + s_iova_off);
1415 		s->length = s_length;
1416 
1417 		/*
1418 		 * Due to the alignment of our single IOVA allocation, we can
1419 		 * depend on these assumptions about the segment boundary mask:
1420 		 * - If mask size >= IOVA size, then the IOVA range cannot
1421 		 *   possibly fall across a boundary, so we don't care.
1422 		 * - If mask size < IOVA size, then the IOVA range must start
1423 		 *   exactly on a boundary, therefore we can lay things out
1424 		 *   based purely on segment lengths without needing to know
1425 		 *   the actual addresses beforehand.
1426 		 * - The mask must be a power of 2, so pad_len == 0 if
1427 		 *   iova_len == 0, thus we cannot dereference prev the first
1428 		 *   time through here (i.e. before it has a meaningful value).
1429 		 */
1430 		if (pad_len && pad_len < s_length - 1) {
1431 			prev->length += pad_len;
1432 			iova_len += pad_len;
1433 		}
1434 
1435 		iova_len += s_length;
1436 		prev = s;
1437 	}
1438 
1439 	if (!iova_len)
1440 		return __finalise_sg(dev, sg, nents, 0);
1441 
1442 	iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
1443 	if (!iova) {
1444 		ret = -ENOMEM;
1445 		goto out_restore_sg;
1446 	}
1447 
1448 	/*
1449 	 * We'll leave any physical concatenation to the IOMMU driver's
1450 	 * implementation - it knows better than we do.
1451 	 */
1452 	ret = iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
1453 	if (ret < 0 || ret < iova_len)
1454 		goto out_free_iova;
1455 
1456 	return __finalise_sg(dev, sg, nents, iova);
1457 
1458 out_free_iova:
1459 	iommu_dma_free_iova(cookie, iova, iova_len, NULL);
1460 out_restore_sg:
1461 	__invalidate_sg(sg, nents);
1462 out:
1463 	if (ret != -ENOMEM && ret != -EREMOTEIO)
1464 		return -EINVAL;
1465 	return ret;
1466 }
1467 
1468 static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
1469 		int nents, enum dma_data_direction dir, unsigned long attrs)
1470 {
1471 	dma_addr_t end = 0, start;
1472 	struct scatterlist *tmp;
1473 	int i;
1474 
1475 	if (sg_dma_is_swiotlb(sg)) {
1476 		iommu_dma_unmap_sg_swiotlb(dev, sg, nents, dir, attrs);
1477 		return;
1478 	}
1479 
1480 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1481 		iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
1482 
1483 	/*
1484 	 * The scatterlist segments are mapped into a single
1485 	 * contiguous IOVA allocation, the start and end points
1486 	 * just have to be determined.
1487 	 */
1488 	for_each_sg(sg, tmp, nents, i) {
1489 		if (sg_dma_is_bus_address(tmp)) {
1490 			sg_dma_unmark_bus_address(tmp);
1491 			continue;
1492 		}
1493 
1494 		if (sg_dma_len(tmp) == 0)
1495 			break;
1496 
1497 		start = sg_dma_address(tmp);
1498 		break;
1499 	}
1500 
1501 	nents -= i;
1502 	for_each_sg(tmp, tmp, nents, i) {
1503 		if (sg_dma_is_bus_address(tmp)) {
1504 			sg_dma_unmark_bus_address(tmp);
1505 			continue;
1506 		}
1507 
1508 		if (sg_dma_len(tmp) == 0)
1509 			break;
1510 
1511 		end = sg_dma_address(tmp) + sg_dma_len(tmp);
1512 	}
1513 
1514 	if (end)
1515 		__iommu_dma_unmap(dev, start, end - start);
1516 }
1517 
1518 static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
1519 		size_t size, enum dma_data_direction dir, unsigned long attrs)
1520 {
1521 	return __iommu_dma_map(dev, phys, size,
1522 			dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO,
1523 			dma_get_mask(dev));
1524 }
1525 
1526 static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
1527 		size_t size, enum dma_data_direction dir, unsigned long attrs)
1528 {
1529 	__iommu_dma_unmap(dev, handle, size);
1530 }
1531 
1532 static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)
1533 {
1534 	size_t alloc_size = PAGE_ALIGN(size);
1535 	int count = alloc_size >> PAGE_SHIFT;
1536 	struct page *page = NULL, **pages = NULL;
1537 
1538 	/* Non-coherent atomic allocation? Easy */
1539 	if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
1540 	    dma_free_from_pool(dev, cpu_addr, alloc_size))
1541 		return;
1542 
1543 	if (is_vmalloc_addr(cpu_addr)) {
1544 		/*
1545 		 * If it the address is remapped, then it's either non-coherent
1546 		 * or highmem CMA, or an iommu_dma_alloc_remap() construction.
1547 		 */
1548 		pages = dma_common_find_pages(cpu_addr);
1549 		if (!pages)
1550 			page = vmalloc_to_page(cpu_addr);
1551 		dma_common_free_remap(cpu_addr, alloc_size);
1552 	} else {
1553 		/* Lowmem means a coherent atomic or CMA allocation */
1554 		page = virt_to_page(cpu_addr);
1555 	}
1556 
1557 	if (pages)
1558 		__iommu_dma_free_pages(pages, count);
1559 	if (page)
1560 		dma_free_contiguous(dev, page, alloc_size);
1561 }
1562 
1563 static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
1564 		dma_addr_t handle, unsigned long attrs)
1565 {
1566 	__iommu_dma_unmap(dev, handle, size);
1567 	__iommu_dma_free(dev, size, cpu_addr);
1568 }
1569 
1570 static void *iommu_dma_alloc_pages(struct device *dev, size_t size,
1571 		struct page **pagep, gfp_t gfp, unsigned long attrs)
1572 {
1573 	bool coherent = dev_is_dma_coherent(dev);
1574 	size_t alloc_size = PAGE_ALIGN(size);
1575 	int node = dev_to_node(dev);
1576 	struct page *page = NULL;
1577 	void *cpu_addr;
1578 
1579 	page = dma_alloc_contiguous(dev, alloc_size, gfp);
1580 	if (!page)
1581 		page = alloc_pages_node(node, gfp, get_order(alloc_size));
1582 	if (!page)
1583 		return NULL;
1584 
1585 	if (!coherent || PageHighMem(page)) {
1586 		pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs);
1587 
1588 		cpu_addr = dma_common_contiguous_remap(page, alloc_size,
1589 				prot, __builtin_return_address(0));
1590 		if (!cpu_addr)
1591 			goto out_free_pages;
1592 
1593 		if (!coherent)
1594 			arch_dma_prep_coherent(page, size);
1595 	} else {
1596 		cpu_addr = page_address(page);
1597 	}
1598 
1599 	*pagep = page;
1600 	memset(cpu_addr, 0, alloc_size);
1601 	return cpu_addr;
1602 out_free_pages:
1603 	dma_free_contiguous(dev, page, alloc_size);
1604 	return NULL;
1605 }
1606 
1607 static void *iommu_dma_alloc(struct device *dev, size_t size,
1608 		dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
1609 {
1610 	bool coherent = dev_is_dma_coherent(dev);
1611 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
1612 	struct page *page = NULL;
1613 	void *cpu_addr;
1614 
1615 	gfp |= __GFP_ZERO;
1616 
1617 	if (gfpflags_allow_blocking(gfp) &&
1618 	    !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
1619 		return iommu_dma_alloc_remap(dev, size, handle, gfp,
1620 				dma_pgprot(dev, PAGE_KERNEL, attrs), attrs);
1621 	}
1622 
1623 	if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
1624 	    !gfpflags_allow_blocking(gfp) && !coherent)
1625 		page = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &cpu_addr,
1626 					       gfp, NULL);
1627 	else
1628 		cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs);
1629 	if (!cpu_addr)
1630 		return NULL;
1631 
1632 	*handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot,
1633 			dev->coherent_dma_mask);
1634 	if (*handle == DMA_MAPPING_ERROR) {
1635 		__iommu_dma_free(dev, size, cpu_addr);
1636 		return NULL;
1637 	}
1638 
1639 	return cpu_addr;
1640 }
1641 
1642 static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
1643 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
1644 		unsigned long attrs)
1645 {
1646 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1647 	unsigned long pfn, off = vma->vm_pgoff;
1648 	int ret;
1649 
1650 	vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
1651 
1652 	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
1653 		return ret;
1654 
1655 	if (off >= nr_pages || vma_pages(vma) > nr_pages - off)
1656 		return -ENXIO;
1657 
1658 	if (is_vmalloc_addr(cpu_addr)) {
1659 		struct page **pages = dma_common_find_pages(cpu_addr);
1660 
1661 		if (pages)
1662 			return vm_map_pages(vma, pages, nr_pages);
1663 		pfn = vmalloc_to_pfn(cpu_addr);
1664 	} else {
1665 		pfn = page_to_pfn(virt_to_page(cpu_addr));
1666 	}
1667 
1668 	return remap_pfn_range(vma, vma->vm_start, pfn + off,
1669 			       vma->vm_end - vma->vm_start,
1670 			       vma->vm_page_prot);
1671 }
1672 
1673 static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
1674 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
1675 		unsigned long attrs)
1676 {
1677 	struct page *page;
1678 	int ret;
1679 
1680 	if (is_vmalloc_addr(cpu_addr)) {
1681 		struct page **pages = dma_common_find_pages(cpu_addr);
1682 
1683 		if (pages) {
1684 			return sg_alloc_table_from_pages(sgt, pages,
1685 					PAGE_ALIGN(size) >> PAGE_SHIFT,
1686 					0, size, GFP_KERNEL);
1687 		}
1688 
1689 		page = vmalloc_to_page(cpu_addr);
1690 	} else {
1691 		page = virt_to_page(cpu_addr);
1692 	}
1693 
1694 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
1695 	if (!ret)
1696 		sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
1697 	return ret;
1698 }
1699 
1700 static unsigned long iommu_dma_get_merge_boundary(struct device *dev)
1701 {
1702 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
1703 
1704 	return (1UL << __ffs(domain->pgsize_bitmap)) - 1;
1705 }
1706 
1707 static size_t iommu_dma_opt_mapping_size(void)
1708 {
1709 	return iova_rcache_range();
1710 }
1711 
1712 static size_t iommu_dma_max_mapping_size(struct device *dev)
1713 {
1714 	if (dev_is_untrusted(dev))
1715 		return swiotlb_max_mapping_size(dev);
1716 
1717 	return SIZE_MAX;
1718 }
1719 
1720 static const struct dma_map_ops iommu_dma_ops = {
1721 	.flags			= DMA_F_PCI_P2PDMA_SUPPORTED,
1722 	.alloc			= iommu_dma_alloc,
1723 	.free			= iommu_dma_free,
1724 	.alloc_pages_op		= dma_common_alloc_pages,
1725 	.free_pages		= dma_common_free_pages,
1726 	.alloc_noncontiguous	= iommu_dma_alloc_noncontiguous,
1727 	.free_noncontiguous	= iommu_dma_free_noncontiguous,
1728 	.mmap			= iommu_dma_mmap,
1729 	.get_sgtable		= iommu_dma_get_sgtable,
1730 	.map_page		= iommu_dma_map_page,
1731 	.unmap_page		= iommu_dma_unmap_page,
1732 	.map_sg			= iommu_dma_map_sg,
1733 	.unmap_sg		= iommu_dma_unmap_sg,
1734 	.sync_single_for_cpu	= iommu_dma_sync_single_for_cpu,
1735 	.sync_single_for_device	= iommu_dma_sync_single_for_device,
1736 	.sync_sg_for_cpu	= iommu_dma_sync_sg_for_cpu,
1737 	.sync_sg_for_device	= iommu_dma_sync_sg_for_device,
1738 	.map_resource		= iommu_dma_map_resource,
1739 	.unmap_resource		= iommu_dma_unmap_resource,
1740 	.get_merge_boundary	= iommu_dma_get_merge_boundary,
1741 	.opt_mapping_size	= iommu_dma_opt_mapping_size,
1742 	.max_mapping_size       = iommu_dma_max_mapping_size,
1743 };
1744 
1745 void iommu_setup_dma_ops(struct device *dev)
1746 {
1747 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1748 
1749 	if (dev_is_pci(dev))
1750 		dev->iommu->pci_32bit_workaround = !iommu_dma_forcedac;
1751 
1752 	if (iommu_is_dma_domain(domain)) {
1753 		if (iommu_dma_init_domain(domain, dev))
1754 			goto out_err;
1755 		dev->dma_ops = &iommu_dma_ops;
1756 	} else if (dev->dma_ops == &iommu_dma_ops) {
1757 		/* Clean up if we've switched *from* a DMA domain */
1758 		dev->dma_ops = NULL;
1759 	}
1760 
1761 	return;
1762 out_err:
1763 	 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
1764 		 dev_name(dev));
1765 }
1766 
1767 static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
1768 		phys_addr_t msi_addr, struct iommu_domain *domain)
1769 {
1770 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
1771 	struct iommu_dma_msi_page *msi_page;
1772 	dma_addr_t iova;
1773 	int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1774 	size_t size = cookie_msi_granule(cookie);
1775 
1776 	msi_addr &= ~(phys_addr_t)(size - 1);
1777 	list_for_each_entry(msi_page, &cookie->msi_page_list, list)
1778 		if (msi_page->phys == msi_addr)
1779 			return msi_page;
1780 
1781 	msi_page = kzalloc(sizeof(*msi_page), GFP_KERNEL);
1782 	if (!msi_page)
1783 		return NULL;
1784 
1785 	iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
1786 	if (!iova)
1787 		goto out_free_page;
1788 
1789 	if (iommu_map(domain, iova, msi_addr, size, prot, GFP_KERNEL))
1790 		goto out_free_iova;
1791 
1792 	INIT_LIST_HEAD(&msi_page->list);
1793 	msi_page->phys = msi_addr;
1794 	msi_page->iova = iova;
1795 	list_add(&msi_page->list, &cookie->msi_page_list);
1796 	return msi_page;
1797 
1798 out_free_iova:
1799 	iommu_dma_free_iova(cookie, iova, size, NULL);
1800 out_free_page:
1801 	kfree(msi_page);
1802 	return NULL;
1803 }
1804 
1805 /**
1806  * iommu_dma_prepare_msi() - Map the MSI page in the IOMMU domain
1807  * @desc: MSI descriptor, will store the MSI page
1808  * @msi_addr: MSI target address to be mapped
1809  *
1810  * Return: 0 on success or negative error code if the mapping failed.
1811  */
1812 int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
1813 {
1814 	struct device *dev = msi_desc_to_dev(desc);
1815 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1816 	struct iommu_dma_msi_page *msi_page;
1817 	static DEFINE_MUTEX(msi_prepare_lock); /* see below */
1818 
1819 	if (!domain || !domain->iova_cookie) {
1820 		desc->iommu_cookie = NULL;
1821 		return 0;
1822 	}
1823 
1824 	/*
1825 	 * In fact the whole prepare operation should already be serialised by
1826 	 * irq_domain_mutex further up the callchain, but that's pretty subtle
1827 	 * on its own, so consider this locking as failsafe documentation...
1828 	 */
1829 	mutex_lock(&msi_prepare_lock);
1830 	msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
1831 	mutex_unlock(&msi_prepare_lock);
1832 
1833 	msi_desc_set_iommu_cookie(desc, msi_page);
1834 
1835 	if (!msi_page)
1836 		return -ENOMEM;
1837 	return 0;
1838 }
1839 
1840 /**
1841  * iommu_dma_compose_msi_msg() - Apply translation to an MSI message
1842  * @desc: MSI descriptor prepared by iommu_dma_prepare_msi()
1843  * @msg: MSI message containing target physical address
1844  */
1845 void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
1846 {
1847 	struct device *dev = msi_desc_to_dev(desc);
1848 	const struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1849 	const struct iommu_dma_msi_page *msi_page;
1850 
1851 	msi_page = msi_desc_get_iommu_cookie(desc);
1852 
1853 	if (!domain || !domain->iova_cookie || WARN_ON(!msi_page))
1854 		return;
1855 
1856 	msg->address_hi = upper_32_bits(msi_page->iova);
1857 	msg->address_lo &= cookie_msi_granule(domain->iova_cookie) - 1;
1858 	msg->address_lo += lower_32_bits(msi_page->iova);
1859 }
1860 
1861 static int iommu_dma_init(void)
1862 {
1863 	if (is_kdump_kernel())
1864 		static_branch_enable(&iommu_deferred_attach_enabled);
1865 
1866 	return iova_cache_get();
1867 }
1868 arch_initcall(iommu_dma_init);
1869