1 // SPDX-License-Identifier: GPL-2.0-only
3 * A fairly generic DMA-API to IOMMU-API glue layer.
5 * Copyright (C) 2014-2015 ARM Ltd.
7 * based in part on arch/arm/mm/dma-mapping.c:
8 * Copyright (C) 2000-2004 Russell King
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>
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>
33 #include "dma-iommu.h"
35 struct iommu_dma_msi_page {
36 struct list_head list;
41 enum iommu_dma_cookie_type {
42 IOMMU_DMA_IOVA_COOKIE,
46 struct iommu_dma_cookie {
47 enum iommu_dma_cookie_type type;
49 /* Full allocator for IOMMU_DMA_IOVA_COOKIE */
51 struct iova_domain iovad;
53 struct iova_fq __percpu *fq; /* Flush queue */
54 /* Number of TLB flushes that have been started */
55 atomic64_t fq_flush_start_cnt;
56 /* Number of TLB flushes that have been finished */
57 atomic64_t fq_flush_finish_cnt;
58 /* Timer to regularily empty the flush queues */
59 struct timer_list fq_timer;
60 /* 1 when timer is active, 0 when not */
63 /* Trivial linear page allocator for IOMMU_DMA_MSI_COOKIE */
66 struct list_head msi_page_list;
68 /* Domain for flush queue callback; NULL if flush queue not in use */
69 struct iommu_domain *fq_domain;
73 static DEFINE_STATIC_KEY_FALSE(iommu_deferred_attach_enabled);
74 bool iommu_dma_forcedac __read_mostly;
76 static int __init iommu_dma_forcedac_setup(char *str)
78 int ret = kstrtobool(str, &iommu_dma_forcedac);
80 if (!ret && iommu_dma_forcedac)
81 pr_info("Forcing DAC for PCI devices\n");
84 early_param("iommu.forcedac", iommu_dma_forcedac_setup);
86 /* Number of entries per flush queue */
87 #define IOVA_FQ_SIZE 256
89 /* Timeout (in ms) after which entries are flushed from the queue */
90 #define IOVA_FQ_TIMEOUT 10
92 /* Flush queue entry for deferred flushing */
93 struct iova_fq_entry {
94 unsigned long iova_pfn;
96 struct list_head freelist;
97 u64 counter; /* Flush counter when this entry was added */
100 /* Per-CPU flush queue structure */
102 struct iova_fq_entry entries[IOVA_FQ_SIZE];
103 unsigned int head, tail;
107 #define fq_ring_for_each(i, fq) \
108 for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE)
110 static inline bool fq_full(struct iova_fq *fq)
112 assert_spin_locked(&fq->lock);
113 return (((fq->tail + 1) % IOVA_FQ_SIZE) == fq->head);
116 static inline unsigned int fq_ring_add(struct iova_fq *fq)
118 unsigned int idx = fq->tail;
120 assert_spin_locked(&fq->lock);
122 fq->tail = (idx + 1) % IOVA_FQ_SIZE;
127 static void fq_ring_free(struct iommu_dma_cookie *cookie, struct iova_fq *fq)
129 u64 counter = atomic64_read(&cookie->fq_flush_finish_cnt);
132 assert_spin_locked(&fq->lock);
134 fq_ring_for_each(idx, fq) {
136 if (fq->entries[idx].counter >= counter)
139 put_pages_list(&fq->entries[idx].freelist);
140 free_iova_fast(&cookie->iovad,
141 fq->entries[idx].iova_pfn,
142 fq->entries[idx].pages);
144 fq->head = (fq->head + 1) % IOVA_FQ_SIZE;
148 static void fq_flush_iotlb(struct iommu_dma_cookie *cookie)
150 atomic64_inc(&cookie->fq_flush_start_cnt);
151 cookie->fq_domain->ops->flush_iotlb_all(cookie->fq_domain);
152 atomic64_inc(&cookie->fq_flush_finish_cnt);
155 static void fq_flush_timeout(struct timer_list *t)
157 struct iommu_dma_cookie *cookie = from_timer(cookie, t, fq_timer);
160 atomic_set(&cookie->fq_timer_on, 0);
161 fq_flush_iotlb(cookie);
163 for_each_possible_cpu(cpu) {
167 fq = per_cpu_ptr(cookie->fq, cpu);
168 spin_lock_irqsave(&fq->lock, flags);
169 fq_ring_free(cookie, fq);
170 spin_unlock_irqrestore(&fq->lock, flags);
174 static void queue_iova(struct iommu_dma_cookie *cookie,
175 unsigned long pfn, unsigned long pages,
176 struct list_head *freelist)
183 * Order against the IOMMU driver's pagetable update from unmapping
184 * @pte, to guarantee that fq_flush_iotlb() observes that if called
185 * from a different CPU before we release the lock below. Full barrier
186 * so it also pairs with iommu_dma_init_fq() to avoid seeing partially
187 * written fq state here.
191 fq = raw_cpu_ptr(cookie->fq);
192 spin_lock_irqsave(&fq->lock, flags);
195 * First remove all entries from the flush queue that have already been
196 * flushed out on another CPU. This makes the fq_full() check below less
199 fq_ring_free(cookie, fq);
202 fq_flush_iotlb(cookie);
203 fq_ring_free(cookie, fq);
206 idx = fq_ring_add(fq);
208 fq->entries[idx].iova_pfn = pfn;
209 fq->entries[idx].pages = pages;
210 fq->entries[idx].counter = atomic64_read(&cookie->fq_flush_start_cnt);
211 list_splice(freelist, &fq->entries[idx].freelist);
213 spin_unlock_irqrestore(&fq->lock, flags);
215 /* Avoid false sharing as much as possible. */
216 if (!atomic_read(&cookie->fq_timer_on) &&
217 !atomic_xchg(&cookie->fq_timer_on, 1))
218 mod_timer(&cookie->fq_timer,
219 jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT));
222 static void iommu_dma_free_fq(struct iommu_dma_cookie *cookie)
229 del_timer_sync(&cookie->fq_timer);
230 /* The IOVAs will be torn down separately, so just free our queued pages */
231 for_each_possible_cpu(cpu) {
232 struct iova_fq *fq = per_cpu_ptr(cookie->fq, cpu);
234 fq_ring_for_each(idx, fq)
235 put_pages_list(&fq->entries[idx].freelist);
238 free_percpu(cookie->fq);
241 /* sysfs updates are serialised by the mutex of the group owning @domain */
242 int iommu_dma_init_fq(struct iommu_domain *domain)
244 struct iommu_dma_cookie *cookie = domain->iova_cookie;
245 struct iova_fq __percpu *queue;
248 if (cookie->fq_domain)
251 atomic64_set(&cookie->fq_flush_start_cnt, 0);
252 atomic64_set(&cookie->fq_flush_finish_cnt, 0);
254 queue = alloc_percpu(struct iova_fq);
256 pr_warn("iova flush queue initialization failed\n");
260 for_each_possible_cpu(cpu) {
261 struct iova_fq *fq = per_cpu_ptr(queue, cpu);
266 spin_lock_init(&fq->lock);
268 for (i = 0; i < IOVA_FQ_SIZE; i++)
269 INIT_LIST_HEAD(&fq->entries[i].freelist);
274 timer_setup(&cookie->fq_timer, fq_flush_timeout, 0);
275 atomic_set(&cookie->fq_timer_on, 0);
277 * Prevent incomplete fq state being observable. Pairs with path from
278 * __iommu_dma_unmap() through iommu_dma_free_iova() to queue_iova()
281 WRITE_ONCE(cookie->fq_domain, domain);
285 static inline size_t cookie_msi_granule(struct iommu_dma_cookie *cookie)
287 if (cookie->type == IOMMU_DMA_IOVA_COOKIE)
288 return cookie->iovad.granule;
292 static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
294 struct iommu_dma_cookie *cookie;
296 cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
298 INIT_LIST_HEAD(&cookie->msi_page_list);
305 * iommu_get_dma_cookie - Acquire DMA-API resources for a domain
306 * @domain: IOMMU domain to prepare for DMA-API usage
308 int iommu_get_dma_cookie(struct iommu_domain *domain)
310 if (domain->iova_cookie)
313 domain->iova_cookie = cookie_alloc(IOMMU_DMA_IOVA_COOKIE);
314 if (!domain->iova_cookie)
317 mutex_init(&domain->iova_cookie->mutex);
322 * iommu_get_msi_cookie - Acquire just MSI remapping resources
323 * @domain: IOMMU domain to prepare
324 * @base: Start address of IOVA region for MSI mappings
326 * Users who manage their own IOVA allocation and do not want DMA API support,
327 * but would still like to take advantage of automatic MSI remapping, can use
328 * this to initialise their own domain appropriately. Users should reserve a
329 * contiguous IOVA region, starting at @base, large enough to accommodate the
330 * number of PAGE_SIZE mappings necessary to cover every MSI doorbell address
331 * used by the devices attached to @domain.
333 int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
335 struct iommu_dma_cookie *cookie;
337 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
340 if (domain->iova_cookie)
343 cookie = cookie_alloc(IOMMU_DMA_MSI_COOKIE);
347 cookie->msi_iova = base;
348 domain->iova_cookie = cookie;
351 EXPORT_SYMBOL(iommu_get_msi_cookie);
354 * iommu_put_dma_cookie - Release a domain's DMA mapping resources
355 * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() or
356 * iommu_get_msi_cookie()
358 void iommu_put_dma_cookie(struct iommu_domain *domain)
360 struct iommu_dma_cookie *cookie = domain->iova_cookie;
361 struct iommu_dma_msi_page *msi, *tmp;
366 if (cookie->type == IOMMU_DMA_IOVA_COOKIE && cookie->iovad.granule) {
367 iommu_dma_free_fq(cookie);
368 put_iova_domain(&cookie->iovad);
371 list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) {
372 list_del(&msi->list);
376 domain->iova_cookie = NULL;
380 * iommu_dma_get_resv_regions - Reserved region driver helper
381 * @dev: Device from iommu_get_resv_regions()
382 * @list: Reserved region list from iommu_get_resv_regions()
384 * IOMMU drivers can use this to implement their .get_resv_regions callback
385 * for general non-IOMMU-specific reservations. Currently, this covers GICv3
386 * ITS region reservation on ACPI based ARM platforms that may require HW MSI
389 void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)
392 if (!is_of_node(dev_iommu_fwspec_get(dev)->iommu_fwnode))
393 iort_iommu_get_resv_regions(dev, list);
396 of_iommu_get_resv_regions(dev, list);
398 EXPORT_SYMBOL(iommu_dma_get_resv_regions);
400 static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie,
401 phys_addr_t start, phys_addr_t end)
403 struct iova_domain *iovad = &cookie->iovad;
404 struct iommu_dma_msi_page *msi_page;
407 start -= iova_offset(iovad, start);
408 num_pages = iova_align(iovad, end - start) >> iova_shift(iovad);
410 for (i = 0; i < num_pages; i++) {
411 msi_page = kmalloc(sizeof(*msi_page), GFP_KERNEL);
415 msi_page->phys = start;
416 msi_page->iova = start;
417 INIT_LIST_HEAD(&msi_page->list);
418 list_add(&msi_page->list, &cookie->msi_page_list);
419 start += iovad->granule;
425 static int iommu_dma_ranges_sort(void *priv, const struct list_head *a,
426 const struct list_head *b)
428 struct resource_entry *res_a = list_entry(a, typeof(*res_a), node);
429 struct resource_entry *res_b = list_entry(b, typeof(*res_b), node);
431 return res_a->res->start > res_b->res->start;
434 static int iova_reserve_pci_windows(struct pci_dev *dev,
435 struct iova_domain *iovad)
437 struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
438 struct resource_entry *window;
439 unsigned long lo, hi;
440 phys_addr_t start = 0, end;
442 resource_list_for_each_entry(window, &bridge->windows) {
443 if (resource_type(window->res) != IORESOURCE_MEM)
446 lo = iova_pfn(iovad, window->res->start - window->offset);
447 hi = iova_pfn(iovad, window->res->end - window->offset);
448 reserve_iova(iovad, lo, hi);
451 /* Get reserved DMA windows from host bridge */
452 list_sort(NULL, &bridge->dma_ranges, iommu_dma_ranges_sort);
453 resource_list_for_each_entry(window, &bridge->dma_ranges) {
454 end = window->res->start - window->offset;
457 lo = iova_pfn(iovad, start);
458 hi = iova_pfn(iovad, end);
459 reserve_iova(iovad, lo, hi);
460 } else if (end < start) {
461 /* DMA ranges should be non-overlapping */
463 "Failed to reserve IOVA [%pa-%pa]\n",
468 start = window->res->end - window->offset + 1;
469 /* If window is last entry */
470 if (window->node.next == &bridge->dma_ranges &&
471 end != ~(phys_addr_t)0) {
472 end = ~(phys_addr_t)0;
480 static int iova_reserve_iommu_regions(struct device *dev,
481 struct iommu_domain *domain)
483 struct iommu_dma_cookie *cookie = domain->iova_cookie;
484 struct iova_domain *iovad = &cookie->iovad;
485 struct iommu_resv_region *region;
486 LIST_HEAD(resv_regions);
489 if (dev_is_pci(dev)) {
490 ret = iova_reserve_pci_windows(to_pci_dev(dev), iovad);
495 iommu_get_resv_regions(dev, &resv_regions);
496 list_for_each_entry(region, &resv_regions, list) {
497 unsigned long lo, hi;
499 /* We ARE the software that manages these! */
500 if (region->type == IOMMU_RESV_SW_MSI)
503 lo = iova_pfn(iovad, region->start);
504 hi = iova_pfn(iovad, region->start + region->length - 1);
505 reserve_iova(iovad, lo, hi);
507 if (region->type == IOMMU_RESV_MSI)
508 ret = cookie_init_hw_msi_region(cookie, region->start,
509 region->start + region->length);
513 iommu_put_resv_regions(dev, &resv_regions);
518 static bool dev_is_untrusted(struct device *dev)
520 return dev_is_pci(dev) && to_pci_dev(dev)->untrusted;
523 static bool dev_use_swiotlb(struct device *dev, size_t size,
524 enum dma_data_direction dir)
526 return IS_ENABLED(CONFIG_SWIOTLB) &&
527 (dev_is_untrusted(dev) ||
528 dma_kmalloc_needs_bounce(dev, size, dir));
531 static bool dev_use_sg_swiotlb(struct device *dev, struct scatterlist *sg,
532 int nents, enum dma_data_direction dir)
534 struct scatterlist *s;
537 if (!IS_ENABLED(CONFIG_SWIOTLB))
540 if (dev_is_untrusted(dev))
544 * If kmalloc() buffers are not DMA-safe for this device and
545 * direction, check the individual lengths in the sg list. If any
546 * element is deemed unsafe, use the swiotlb for bouncing.
548 if (!dma_kmalloc_safe(dev, dir)) {
549 for_each_sg(sg, s, nents, i)
550 if (!dma_kmalloc_size_aligned(s->length))
558 * iommu_dma_init_domain - Initialise a DMA mapping domain
559 * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
560 * @base: IOVA at which the mappable address space starts
561 * @limit: Last address of the IOVA space
562 * @dev: Device the domain is being initialised for
564 * @base and @limit + 1 should be exact multiples of IOMMU page granularity to
565 * avoid rounding surprises. If necessary, we reserve the page at address 0
566 * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
567 * any change which could make prior IOVAs invalid will fail.
569 static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
570 dma_addr_t limit, struct device *dev)
572 struct iommu_dma_cookie *cookie = domain->iova_cookie;
573 unsigned long order, base_pfn;
574 struct iova_domain *iovad;
577 if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE)
580 iovad = &cookie->iovad;
582 /* Use the smallest supported page size for IOVA granularity */
583 order = __ffs(domain->pgsize_bitmap);
584 base_pfn = max_t(unsigned long, 1, base >> order);
586 /* Check the domain allows at least some access to the device... */
587 if (domain->geometry.force_aperture) {
588 if (base > domain->geometry.aperture_end ||
589 limit < domain->geometry.aperture_start) {
590 pr_warn("specified DMA range outside IOMMU capability\n");
593 /* ...then finally give it a kicking to make sure it fits */
594 base_pfn = max_t(unsigned long, base_pfn,
595 domain->geometry.aperture_start >> order);
598 /* start_pfn is always nonzero for an already-initialised domain */
599 mutex_lock(&cookie->mutex);
600 if (iovad->start_pfn) {
601 if (1UL << order != iovad->granule ||
602 base_pfn != iovad->start_pfn) {
603 pr_warn("Incompatible range for DMA domain\n");
612 init_iova_domain(iovad, 1UL << order, base_pfn);
613 ret = iova_domain_init_rcaches(iovad);
617 /* If the FQ fails we can simply fall back to strict mode */
618 if (domain->type == IOMMU_DOMAIN_DMA_FQ &&
619 (!device_iommu_capable(dev, IOMMU_CAP_DEFERRED_FLUSH) || iommu_dma_init_fq(domain)))
620 domain->type = IOMMU_DOMAIN_DMA;
622 ret = iova_reserve_iommu_regions(dev, domain);
625 mutex_unlock(&cookie->mutex);
630 * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
632 * @dir: Direction of DMA transfer
633 * @coherent: Is the DMA master cache-coherent?
634 * @attrs: DMA attributes for the mapping
636 * Return: corresponding IOMMU API page protection flags
638 static int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
641 int prot = coherent ? IOMMU_CACHE : 0;
643 if (attrs & DMA_ATTR_PRIVILEGED)
647 case DMA_BIDIRECTIONAL:
648 return prot | IOMMU_READ | IOMMU_WRITE;
650 return prot | IOMMU_READ;
651 case DMA_FROM_DEVICE:
652 return prot | IOMMU_WRITE;
658 static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
659 size_t size, u64 dma_limit, struct device *dev)
661 struct iommu_dma_cookie *cookie = domain->iova_cookie;
662 struct iova_domain *iovad = &cookie->iovad;
663 unsigned long shift, iova_len, iova;
665 if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
666 cookie->msi_iova += size;
667 return cookie->msi_iova - size;
670 shift = iova_shift(iovad);
671 iova_len = size >> shift;
673 dma_limit = min_not_zero(dma_limit, dev->bus_dma_limit);
675 if (domain->geometry.force_aperture)
676 dma_limit = min(dma_limit, (u64)domain->geometry.aperture_end);
679 * Try to use all the 32-bit PCI addresses first. The original SAC vs.
680 * DAC reasoning loses relevance with PCIe, but enough hardware and
681 * firmware bugs are still lurking out there that it's safest not to
682 * venture into the 64-bit space until necessary.
684 * If your device goes wrong after seeing the notice then likely either
685 * its driver is not setting DMA masks accurately, the hardware has
686 * some inherent bug in handling >32-bit addresses, or not all the
687 * expected address bits are wired up between the device and the IOMMU.
689 if (dma_limit > DMA_BIT_MASK(32) && dev->iommu->pci_32bit_workaround) {
690 iova = alloc_iova_fast(iovad, iova_len,
691 DMA_BIT_MASK(32) >> shift, false);
695 dev->iommu->pci_32bit_workaround = false;
696 dev_notice(dev, "Using %d-bit DMA addresses\n", bits_per(dma_limit));
699 iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift, true);
701 return (dma_addr_t)iova << shift;
704 static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
705 dma_addr_t iova, size_t size, struct iommu_iotlb_gather *gather)
707 struct iova_domain *iovad = &cookie->iovad;
709 /* The MSI case is only ever cleaning up its most recent allocation */
710 if (cookie->type == IOMMU_DMA_MSI_COOKIE)
711 cookie->msi_iova -= size;
712 else if (gather && gather->queued)
713 queue_iova(cookie, iova_pfn(iovad, iova),
714 size >> iova_shift(iovad),
717 free_iova_fast(iovad, iova_pfn(iovad, iova),
718 size >> iova_shift(iovad));
721 static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr,
724 struct iommu_domain *domain = iommu_get_dma_domain(dev);
725 struct iommu_dma_cookie *cookie = domain->iova_cookie;
726 struct iova_domain *iovad = &cookie->iovad;
727 size_t iova_off = iova_offset(iovad, dma_addr);
728 struct iommu_iotlb_gather iotlb_gather;
731 dma_addr -= iova_off;
732 size = iova_align(iovad, size + iova_off);
733 iommu_iotlb_gather_init(&iotlb_gather);
734 iotlb_gather.queued = READ_ONCE(cookie->fq_domain);
736 unmapped = iommu_unmap_fast(domain, dma_addr, size, &iotlb_gather);
737 WARN_ON(unmapped != size);
739 if (!iotlb_gather.queued)
740 iommu_iotlb_sync(domain, &iotlb_gather);
741 iommu_dma_free_iova(cookie, dma_addr, size, &iotlb_gather);
744 static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
745 size_t size, int prot, u64 dma_mask)
747 struct iommu_domain *domain = iommu_get_dma_domain(dev);
748 struct iommu_dma_cookie *cookie = domain->iova_cookie;
749 struct iova_domain *iovad = &cookie->iovad;
750 size_t iova_off = iova_offset(iovad, phys);
753 if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
754 iommu_deferred_attach(dev, domain))
755 return DMA_MAPPING_ERROR;
757 size = iova_align(iovad, size + iova_off);
759 iova = iommu_dma_alloc_iova(domain, size, dma_mask, dev);
761 return DMA_MAPPING_ERROR;
763 if (iommu_map(domain, iova, phys - iova_off, size, prot, GFP_ATOMIC)) {
764 iommu_dma_free_iova(cookie, iova, size, NULL);
765 return DMA_MAPPING_ERROR;
767 return iova + iova_off;
770 static void __iommu_dma_free_pages(struct page **pages, int count)
773 __free_page(pages[count]);
777 static struct page **__iommu_dma_alloc_pages(struct device *dev,
778 unsigned int count, unsigned long order_mask, gfp_t gfp)
781 unsigned int i = 0, nid = dev_to_node(dev);
783 order_mask &= GENMASK(MAX_ORDER, 0);
787 pages = kvcalloc(count, sizeof(*pages), GFP_KERNEL);
791 /* IOMMU can map any pages, so himem can also be used here */
792 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
795 struct page *page = NULL;
796 unsigned int order_size;
799 * Higher-order allocations are a convenience rather
800 * than a necessity, hence using __GFP_NORETRY until
801 * falling back to minimum-order allocations.
803 for (order_mask &= GENMASK(__fls(count), 0);
804 order_mask; order_mask &= ~order_size) {
805 unsigned int order = __fls(order_mask);
806 gfp_t alloc_flags = gfp;
808 order_size = 1U << order;
809 if (order_mask > order_size)
810 alloc_flags |= __GFP_NORETRY;
811 page = alloc_pages_node(nid, alloc_flags, order);
815 split_page(page, order);
819 __iommu_dma_free_pages(pages, i);
830 * If size is less than PAGE_SIZE, then a full CPU page will be allocated,
831 * but an IOMMU which supports smaller pages might not map the whole thing.
833 static struct page **__iommu_dma_alloc_noncontiguous(struct device *dev,
834 size_t size, struct sg_table *sgt, gfp_t gfp, pgprot_t prot,
837 struct iommu_domain *domain = iommu_get_dma_domain(dev);
838 struct iommu_dma_cookie *cookie = domain->iova_cookie;
839 struct iova_domain *iovad = &cookie->iovad;
840 bool coherent = dev_is_dma_coherent(dev);
841 int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
842 unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
847 if (static_branch_unlikely(&iommu_deferred_attach_enabled) &&
848 iommu_deferred_attach(dev, domain))
851 min_size = alloc_sizes & -alloc_sizes;
852 if (min_size < PAGE_SIZE) {
853 min_size = PAGE_SIZE;
854 alloc_sizes |= PAGE_SIZE;
856 size = ALIGN(size, min_size);
858 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
859 alloc_sizes = min_size;
861 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
862 pages = __iommu_dma_alloc_pages(dev, count, alloc_sizes >> PAGE_SHIFT,
867 size = iova_align(iovad, size);
868 iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev);
873 * Remove the zone/policy flags from the GFP - these are applied to the
874 * __iommu_dma_alloc_pages() but are not used for the supporting
875 * internal allocations that follow.
877 gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM | __GFP_COMP);
879 if (sg_alloc_table_from_pages(sgt, pages, count, 0, size, gfp))
882 if (!(ioprot & IOMMU_CACHE)) {
883 struct scatterlist *sg;
886 for_each_sg(sgt->sgl, sg, sgt->orig_nents, i)
887 arch_dma_prep_coherent(sg_page(sg), sg->length);
890 ret = iommu_map_sg(domain, iova, sgt->sgl, sgt->orig_nents, ioprot,
892 if (ret < 0 || ret < size)
895 sgt->sgl->dma_address = iova;
896 sgt->sgl->dma_length = size;
902 iommu_dma_free_iova(cookie, iova, size, NULL);
904 __iommu_dma_free_pages(pages, count);
908 static void *iommu_dma_alloc_remap(struct device *dev, size_t size,
909 dma_addr_t *dma_handle, gfp_t gfp, pgprot_t prot,
916 pages = __iommu_dma_alloc_noncontiguous(dev, size, &sgt, gfp, prot,
920 *dma_handle = sgt.sgl->dma_address;
922 vaddr = dma_common_pages_remap(pages, size, prot,
923 __builtin_return_address(0));
929 __iommu_dma_unmap(dev, *dma_handle, size);
930 __iommu_dma_free_pages(pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
934 static struct sg_table *iommu_dma_alloc_noncontiguous(struct device *dev,
935 size_t size, enum dma_data_direction dir, gfp_t gfp,
938 struct dma_sgt_handle *sh;
940 sh = kmalloc(sizeof(*sh), gfp);
944 sh->pages = __iommu_dma_alloc_noncontiguous(dev, size, &sh->sgt, gfp,
953 static void iommu_dma_free_noncontiguous(struct device *dev, size_t size,
954 struct sg_table *sgt, enum dma_data_direction dir)
956 struct dma_sgt_handle *sh = sgt_handle(sgt);
958 __iommu_dma_unmap(dev, sgt->sgl->dma_address, size);
959 __iommu_dma_free_pages(sh->pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
960 sg_free_table(&sh->sgt);
964 static void iommu_dma_sync_single_for_cpu(struct device *dev,
965 dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
969 if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir))
972 phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
973 if (!dev_is_dma_coherent(dev))
974 arch_sync_dma_for_cpu(phys, size, dir);
976 if (is_swiotlb_buffer(dev, phys))
977 swiotlb_sync_single_for_cpu(dev, phys, size, dir);
980 static void iommu_dma_sync_single_for_device(struct device *dev,
981 dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
985 if (dev_is_dma_coherent(dev) && !dev_use_swiotlb(dev, size, dir))
988 phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
989 if (is_swiotlb_buffer(dev, phys))
990 swiotlb_sync_single_for_device(dev, phys, size, dir);
992 if (!dev_is_dma_coherent(dev))
993 arch_sync_dma_for_device(phys, size, dir);
996 static void iommu_dma_sync_sg_for_cpu(struct device *dev,
997 struct scatterlist *sgl, int nelems,
998 enum dma_data_direction dir)
1000 struct scatterlist *sg;
1003 if (sg_dma_is_swiotlb(sgl))
1004 for_each_sg(sgl, sg, nelems, i)
1005 iommu_dma_sync_single_for_cpu(dev, sg_dma_address(sg),
1007 else if (!dev_is_dma_coherent(dev))
1008 for_each_sg(sgl, sg, nelems, i)
1009 arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
1012 static void iommu_dma_sync_sg_for_device(struct device *dev,
1013 struct scatterlist *sgl, int nelems,
1014 enum dma_data_direction dir)
1016 struct scatterlist *sg;
1019 if (sg_dma_is_swiotlb(sgl))
1020 for_each_sg(sgl, sg, nelems, i)
1021 iommu_dma_sync_single_for_device(dev,
1024 else if (!dev_is_dma_coherent(dev))
1025 for_each_sg(sgl, sg, nelems, i)
1026 arch_sync_dma_for_device(sg_phys(sg), sg->length, dir);
1029 static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
1030 unsigned long offset, size_t size, enum dma_data_direction dir,
1031 unsigned long attrs)
1033 phys_addr_t phys = page_to_phys(page) + offset;
1034 bool coherent = dev_is_dma_coherent(dev);
1035 int prot = dma_info_to_prot(dir, coherent, attrs);
1036 struct iommu_domain *domain = iommu_get_dma_domain(dev);
1037 struct iommu_dma_cookie *cookie = domain->iova_cookie;
1038 struct iova_domain *iovad = &cookie->iovad;
1039 dma_addr_t iova, dma_mask = dma_get_mask(dev);
1042 * If both the physical buffer start address and size are
1043 * page aligned, we don't need to use a bounce page.
1045 if (dev_use_swiotlb(dev, size, dir) &&
1046 iova_offset(iovad, phys | size)) {
1047 void *padding_start;
1048 size_t padding_size, aligned_size;
1050 if (!is_swiotlb_active(dev)) {
1051 dev_warn_once(dev, "DMA bounce buffers are inactive, unable to map unaligned transaction.\n");
1052 return DMA_MAPPING_ERROR;
1055 aligned_size = iova_align(iovad, size);
1056 phys = swiotlb_tbl_map_single(dev, phys, size, aligned_size,
1057 iova_mask(iovad), dir, attrs);
1059 if (phys == DMA_MAPPING_ERROR)
1060 return DMA_MAPPING_ERROR;
1062 /* Cleanup the padding area. */
1063 padding_start = phys_to_virt(phys);
1064 padding_size = aligned_size;
1066 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
1067 (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) {
1068 padding_start += size;
1069 padding_size -= size;
1072 memset(padding_start, 0, padding_size);
1075 if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1076 arch_sync_dma_for_device(phys, size, dir);
1078 iova = __iommu_dma_map(dev, phys, size, prot, dma_mask);
1079 if (iova == DMA_MAPPING_ERROR && is_swiotlb_buffer(dev, phys))
1080 swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs);
1084 static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
1085 size_t size, enum dma_data_direction dir, unsigned long attrs)
1087 struct iommu_domain *domain = iommu_get_dma_domain(dev);
1090 phys = iommu_iova_to_phys(domain, dma_handle);
1094 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) && !dev_is_dma_coherent(dev))
1095 arch_sync_dma_for_cpu(phys, size, dir);
1097 __iommu_dma_unmap(dev, dma_handle, size);
1099 if (unlikely(is_swiotlb_buffer(dev, phys)))
1100 swiotlb_tbl_unmap_single(dev, phys, size, dir, attrs);
1104 * Prepare a successfully-mapped scatterlist to give back to the caller.
1106 * At this point the segments are already laid out by iommu_dma_map_sg() to
1107 * avoid individually crossing any boundaries, so we merely need to check a
1108 * segment's start address to avoid concatenating across one.
1110 static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
1111 dma_addr_t dma_addr)
1113 struct scatterlist *s, *cur = sg;
1114 unsigned long seg_mask = dma_get_seg_boundary(dev);
1115 unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
1118 for_each_sg(sg, s, nents, i) {
1119 /* Restore this segment's original unaligned fields first */
1120 dma_addr_t s_dma_addr = sg_dma_address(s);
1121 unsigned int s_iova_off = sg_dma_address(s);
1122 unsigned int s_length = sg_dma_len(s);
1123 unsigned int s_iova_len = s->length;
1125 sg_dma_address(s) = DMA_MAPPING_ERROR;
1128 if (sg_dma_is_bus_address(s)) {
1132 sg_dma_unmark_bus_address(s);
1133 sg_dma_address(cur) = s_dma_addr;
1134 sg_dma_len(cur) = s_length;
1135 sg_dma_mark_bus_address(cur);
1141 s->offset += s_iova_off;
1142 s->length = s_length;
1145 * Now fill in the real DMA data. If...
1146 * - there is a valid output segment to append to
1147 * - and this segment starts on an IOVA page boundary
1148 * - but doesn't fall at a segment boundary
1149 * - and wouldn't make the resulting output segment too long
1151 if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
1152 (max_len - cur_len >= s_length)) {
1153 /* ...then concatenate it with the previous one */
1154 cur_len += s_length;
1156 /* Otherwise start the next output segment */
1162 sg_dma_address(cur) = dma_addr + s_iova_off;
1165 sg_dma_len(cur) = cur_len;
1166 dma_addr += s_iova_len;
1168 if (s_length + s_iova_off < s_iova_len)
1175 * If mapping failed, then just restore the original list,
1176 * but making sure the DMA fields are invalidated.
1178 static void __invalidate_sg(struct scatterlist *sg, int nents)
1180 struct scatterlist *s;
1183 for_each_sg(sg, s, nents, i) {
1184 if (sg_dma_is_bus_address(s)) {
1185 sg_dma_unmark_bus_address(s);
1187 if (sg_dma_address(s) != DMA_MAPPING_ERROR)
1188 s->offset += sg_dma_address(s);
1190 s->length = sg_dma_len(s);
1192 sg_dma_address(s) = DMA_MAPPING_ERROR;
1197 static void iommu_dma_unmap_sg_swiotlb(struct device *dev, struct scatterlist *sg,
1198 int nents, enum dma_data_direction dir, unsigned long attrs)
1200 struct scatterlist *s;
1203 for_each_sg(sg, s, nents, i)
1204 iommu_dma_unmap_page(dev, sg_dma_address(s),
1205 sg_dma_len(s), dir, attrs);
1208 static int iommu_dma_map_sg_swiotlb(struct device *dev, struct scatterlist *sg,
1209 int nents, enum dma_data_direction dir, unsigned long attrs)
1211 struct scatterlist *s;
1214 sg_dma_mark_swiotlb(sg);
1216 for_each_sg(sg, s, nents, i) {
1217 sg_dma_address(s) = iommu_dma_map_page(dev, sg_page(s),
1218 s->offset, s->length, dir, attrs);
1219 if (sg_dma_address(s) == DMA_MAPPING_ERROR)
1221 sg_dma_len(s) = s->length;
1227 iommu_dma_unmap_sg_swiotlb(dev, sg, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
1232 * The DMA API client is passing in a scatterlist which could describe
1233 * any old buffer layout, but the IOMMU API requires everything to be
1234 * aligned to IOMMU pages. Hence the need for this complicated bit of
1235 * impedance-matching, to be able to hand off a suitably-aligned list,
1236 * but still preserve the original offsets and sizes for the caller.
1238 static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
1239 int nents, enum dma_data_direction dir, unsigned long attrs)
1241 struct iommu_domain *domain = iommu_get_dma_domain(dev);
1242 struct iommu_dma_cookie *cookie = domain->iova_cookie;
1243 struct iova_domain *iovad = &cookie->iovad;
1244 struct scatterlist *s, *prev = NULL;
1245 int prot = dma_info_to_prot(dir, dev_is_dma_coherent(dev), attrs);
1246 struct pci_p2pdma_map_state p2pdma_state = {};
1247 enum pci_p2pdma_map_type map;
1249 size_t iova_len = 0;
1250 unsigned long mask = dma_get_seg_boundary(dev);
1254 if (static_branch_unlikely(&iommu_deferred_attach_enabled)) {
1255 ret = iommu_deferred_attach(dev, domain);
1260 if (dev_use_sg_swiotlb(dev, sg, nents, dir))
1261 return iommu_dma_map_sg_swiotlb(dev, sg, nents, dir, attrs);
1263 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1264 iommu_dma_sync_sg_for_device(dev, sg, nents, dir);
1267 * Work out how much IOVA space we need, and align the segments to
1268 * IOVA granules for the IOMMU driver to handle. With some clever
1269 * trickery we can modify the list in-place, but reversibly, by
1270 * stashing the unaligned parts in the as-yet-unused DMA fields.
1272 for_each_sg(sg, s, nents, i) {
1273 size_t s_iova_off = iova_offset(iovad, s->offset);
1274 size_t s_length = s->length;
1275 size_t pad_len = (mask - iova_len + 1) & mask;
1277 if (is_pci_p2pdma_page(sg_page(s))) {
1278 map = pci_p2pdma_map_segment(&p2pdma_state, dev, s);
1280 case PCI_P2PDMA_MAP_BUS_ADDR:
1282 * iommu_map_sg() will skip this segment as
1283 * it is marked as a bus address,
1284 * __finalise_sg() will copy the dma address
1285 * into the output segment.
1288 case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
1290 * Mapping through host bridge should be
1291 * mapped with regular IOVAs, thus we
1292 * do nothing here and continue below.
1297 goto out_restore_sg;
1301 sg_dma_address(s) = s_iova_off;
1302 sg_dma_len(s) = s_length;
1303 s->offset -= s_iova_off;
1304 s_length = iova_align(iovad, s_length + s_iova_off);
1305 s->length = s_length;
1308 * Due to the alignment of our single IOVA allocation, we can
1309 * depend on these assumptions about the segment boundary mask:
1310 * - If mask size >= IOVA size, then the IOVA range cannot
1311 * possibly fall across a boundary, so we don't care.
1312 * - If mask size < IOVA size, then the IOVA range must start
1313 * exactly on a boundary, therefore we can lay things out
1314 * based purely on segment lengths without needing to know
1315 * the actual addresses beforehand.
1316 * - The mask must be a power of 2, so pad_len == 0 if
1317 * iova_len == 0, thus we cannot dereference prev the first
1318 * time through here (i.e. before it has a meaningful value).
1320 if (pad_len && pad_len < s_length - 1) {
1321 prev->length += pad_len;
1322 iova_len += pad_len;
1325 iova_len += s_length;
1330 return __finalise_sg(dev, sg, nents, 0);
1332 iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
1335 goto out_restore_sg;
1339 * We'll leave any physical concatenation to the IOMMU driver's
1340 * implementation - it knows better than we do.
1342 ret = iommu_map_sg(domain, iova, sg, nents, prot, GFP_ATOMIC);
1343 if (ret < 0 || ret < iova_len)
1346 return __finalise_sg(dev, sg, nents, iova);
1349 iommu_dma_free_iova(cookie, iova, iova_len, NULL);
1351 __invalidate_sg(sg, nents);
1353 if (ret != -ENOMEM && ret != -EREMOTEIO)
1358 static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
1359 int nents, enum dma_data_direction dir, unsigned long attrs)
1361 dma_addr_t end = 0, start;
1362 struct scatterlist *tmp;
1365 if (sg_dma_is_swiotlb(sg)) {
1366 iommu_dma_unmap_sg_swiotlb(dev, sg, nents, dir, attrs);
1370 if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
1371 iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
1374 * The scatterlist segments are mapped into a single
1375 * contiguous IOVA allocation, the start and end points
1376 * just have to be determined.
1378 for_each_sg(sg, tmp, nents, i) {
1379 if (sg_dma_is_bus_address(tmp)) {
1380 sg_dma_unmark_bus_address(tmp);
1384 if (sg_dma_len(tmp) == 0)
1387 start = sg_dma_address(tmp);
1392 for_each_sg(tmp, tmp, nents, i) {
1393 if (sg_dma_is_bus_address(tmp)) {
1394 sg_dma_unmark_bus_address(tmp);
1398 if (sg_dma_len(tmp) == 0)
1401 end = sg_dma_address(tmp) + sg_dma_len(tmp);
1405 __iommu_dma_unmap(dev, start, end - start);
1408 static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
1409 size_t size, enum dma_data_direction dir, unsigned long attrs)
1411 return __iommu_dma_map(dev, phys, size,
1412 dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO,
1416 static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
1417 size_t size, enum dma_data_direction dir, unsigned long attrs)
1419 __iommu_dma_unmap(dev, handle, size);
1422 static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)
1424 size_t alloc_size = PAGE_ALIGN(size);
1425 int count = alloc_size >> PAGE_SHIFT;
1426 struct page *page = NULL, **pages = NULL;
1428 /* Non-coherent atomic allocation? Easy */
1429 if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
1430 dma_free_from_pool(dev, cpu_addr, alloc_size))
1433 if (is_vmalloc_addr(cpu_addr)) {
1435 * If it the address is remapped, then it's either non-coherent
1436 * or highmem CMA, or an iommu_dma_alloc_remap() construction.
1438 pages = dma_common_find_pages(cpu_addr);
1440 page = vmalloc_to_page(cpu_addr);
1441 dma_common_free_remap(cpu_addr, alloc_size);
1443 /* Lowmem means a coherent atomic or CMA allocation */
1444 page = virt_to_page(cpu_addr);
1448 __iommu_dma_free_pages(pages, count);
1450 dma_free_contiguous(dev, page, alloc_size);
1453 static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
1454 dma_addr_t handle, unsigned long attrs)
1456 __iommu_dma_unmap(dev, handle, size);
1457 __iommu_dma_free(dev, size, cpu_addr);
1460 static void *iommu_dma_alloc_pages(struct device *dev, size_t size,
1461 struct page **pagep, gfp_t gfp, unsigned long attrs)
1463 bool coherent = dev_is_dma_coherent(dev);
1464 size_t alloc_size = PAGE_ALIGN(size);
1465 int node = dev_to_node(dev);
1466 struct page *page = NULL;
1469 page = dma_alloc_contiguous(dev, alloc_size, gfp);
1471 page = alloc_pages_node(node, gfp, get_order(alloc_size));
1475 if (!coherent || PageHighMem(page)) {
1476 pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs);
1478 cpu_addr = dma_common_contiguous_remap(page, alloc_size,
1479 prot, __builtin_return_address(0));
1481 goto out_free_pages;
1484 arch_dma_prep_coherent(page, size);
1486 cpu_addr = page_address(page);
1490 memset(cpu_addr, 0, alloc_size);
1493 dma_free_contiguous(dev, page, alloc_size);
1497 static void *iommu_dma_alloc(struct device *dev, size_t size,
1498 dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
1500 bool coherent = dev_is_dma_coherent(dev);
1501 int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
1502 struct page *page = NULL;
1507 if (gfpflags_allow_blocking(gfp) &&
1508 !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
1509 return iommu_dma_alloc_remap(dev, size, handle, gfp,
1510 dma_pgprot(dev, PAGE_KERNEL, attrs), attrs);
1513 if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
1514 !gfpflags_allow_blocking(gfp) && !coherent)
1515 page = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &cpu_addr,
1518 cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs);
1522 *handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot,
1523 dev->coherent_dma_mask);
1524 if (*handle == DMA_MAPPING_ERROR) {
1525 __iommu_dma_free(dev, size, cpu_addr);
1532 static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
1533 void *cpu_addr, dma_addr_t dma_addr, size_t size,
1534 unsigned long attrs)
1536 unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1537 unsigned long pfn, off = vma->vm_pgoff;
1540 vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
1542 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
1545 if (off >= nr_pages || vma_pages(vma) > nr_pages - off)
1548 if (is_vmalloc_addr(cpu_addr)) {
1549 struct page **pages = dma_common_find_pages(cpu_addr);
1552 return vm_map_pages(vma, pages, nr_pages);
1553 pfn = vmalloc_to_pfn(cpu_addr);
1555 pfn = page_to_pfn(virt_to_page(cpu_addr));
1558 return remap_pfn_range(vma, vma->vm_start, pfn + off,
1559 vma->vm_end - vma->vm_start,
1563 static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
1564 void *cpu_addr, dma_addr_t dma_addr, size_t size,
1565 unsigned long attrs)
1570 if (is_vmalloc_addr(cpu_addr)) {
1571 struct page **pages = dma_common_find_pages(cpu_addr);
1574 return sg_alloc_table_from_pages(sgt, pages,
1575 PAGE_ALIGN(size) >> PAGE_SHIFT,
1576 0, size, GFP_KERNEL);
1579 page = vmalloc_to_page(cpu_addr);
1581 page = virt_to_page(cpu_addr);
1584 ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
1586 sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
1590 static unsigned long iommu_dma_get_merge_boundary(struct device *dev)
1592 struct iommu_domain *domain = iommu_get_dma_domain(dev);
1594 return (1UL << __ffs(domain->pgsize_bitmap)) - 1;
1597 static size_t iommu_dma_opt_mapping_size(void)
1599 return iova_rcache_range();
1602 static const struct dma_map_ops iommu_dma_ops = {
1603 .flags = DMA_F_PCI_P2PDMA_SUPPORTED,
1604 .alloc = iommu_dma_alloc,
1605 .free = iommu_dma_free,
1606 .alloc_pages = dma_common_alloc_pages,
1607 .free_pages = dma_common_free_pages,
1608 .alloc_noncontiguous = iommu_dma_alloc_noncontiguous,
1609 .free_noncontiguous = iommu_dma_free_noncontiguous,
1610 .mmap = iommu_dma_mmap,
1611 .get_sgtable = iommu_dma_get_sgtable,
1612 .map_page = iommu_dma_map_page,
1613 .unmap_page = iommu_dma_unmap_page,
1614 .map_sg = iommu_dma_map_sg,
1615 .unmap_sg = iommu_dma_unmap_sg,
1616 .sync_single_for_cpu = iommu_dma_sync_single_for_cpu,
1617 .sync_single_for_device = iommu_dma_sync_single_for_device,
1618 .sync_sg_for_cpu = iommu_dma_sync_sg_for_cpu,
1619 .sync_sg_for_device = iommu_dma_sync_sg_for_device,
1620 .map_resource = iommu_dma_map_resource,
1621 .unmap_resource = iommu_dma_unmap_resource,
1622 .get_merge_boundary = iommu_dma_get_merge_boundary,
1623 .opt_mapping_size = iommu_dma_opt_mapping_size,
1627 * The IOMMU core code allocates the default DMA domain, which the underlying
1628 * IOMMU driver needs to support via the dma-iommu layer.
1630 void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 dma_limit)
1632 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1638 * The IOMMU core code allocates the default DMA domain, which the
1639 * underlying IOMMU driver needs to support via the dma-iommu layer.
1641 if (iommu_is_dma_domain(domain)) {
1642 if (iommu_dma_init_domain(domain, dma_base, dma_limit, dev))
1644 dev->dma_ops = &iommu_dma_ops;
1649 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
1652 EXPORT_SYMBOL_GPL(iommu_setup_dma_ops);
1654 static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
1655 phys_addr_t msi_addr, struct iommu_domain *domain)
1657 struct iommu_dma_cookie *cookie = domain->iova_cookie;
1658 struct iommu_dma_msi_page *msi_page;
1660 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1661 size_t size = cookie_msi_granule(cookie);
1663 msi_addr &= ~(phys_addr_t)(size - 1);
1664 list_for_each_entry(msi_page, &cookie->msi_page_list, list)
1665 if (msi_page->phys == msi_addr)
1668 msi_page = kzalloc(sizeof(*msi_page), GFP_KERNEL);
1672 iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
1676 if (iommu_map(domain, iova, msi_addr, size, prot, GFP_KERNEL))
1679 INIT_LIST_HEAD(&msi_page->list);
1680 msi_page->phys = msi_addr;
1681 msi_page->iova = iova;
1682 list_add(&msi_page->list, &cookie->msi_page_list);
1686 iommu_dma_free_iova(cookie, iova, size, NULL);
1693 * iommu_dma_prepare_msi() - Map the MSI page in the IOMMU domain
1694 * @desc: MSI descriptor, will store the MSI page
1695 * @msi_addr: MSI target address to be mapped
1697 * Return: 0 on success or negative error code if the mapping failed.
1699 int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
1701 struct device *dev = msi_desc_to_dev(desc);
1702 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1703 struct iommu_dma_msi_page *msi_page;
1704 static DEFINE_MUTEX(msi_prepare_lock); /* see below */
1706 if (!domain || !domain->iova_cookie) {
1707 desc->iommu_cookie = NULL;
1712 * In fact the whole prepare operation should already be serialised by
1713 * irq_domain_mutex further up the callchain, but that's pretty subtle
1714 * on its own, so consider this locking as failsafe documentation...
1716 mutex_lock(&msi_prepare_lock);
1717 msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
1718 mutex_unlock(&msi_prepare_lock);
1720 msi_desc_set_iommu_cookie(desc, msi_page);
1728 * iommu_dma_compose_msi_msg() - Apply translation to an MSI message
1729 * @desc: MSI descriptor prepared by iommu_dma_prepare_msi()
1730 * @msg: MSI message containing target physical address
1732 void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
1734 struct device *dev = msi_desc_to_dev(desc);
1735 const struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1736 const struct iommu_dma_msi_page *msi_page;
1738 msi_page = msi_desc_get_iommu_cookie(desc);
1740 if (!domain || !domain->iova_cookie || WARN_ON(!msi_page))
1743 msg->address_hi = upper_32_bits(msi_page->iova);
1744 msg->address_lo &= cookie_msi_granule(domain->iova_cookie) - 1;
1745 msg->address_lo += lower_32_bits(msi_page->iova);
1748 static int iommu_dma_init(void)
1750 if (is_kdump_kernel())
1751 static_branch_enable(&iommu_deferred_attach_enabled);
1753 return iova_cache_get();
1755 arch_initcall(iommu_dma_init);