xref: /openbmc/linux/drivers/iommu/dma-iommu.c (revision 5cf45379)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20db2e5d1SRobin Murphy /*
30db2e5d1SRobin Murphy  * A fairly generic DMA-API to IOMMU-API glue layer.
40db2e5d1SRobin Murphy  *
50db2e5d1SRobin Murphy  * Copyright (C) 2014-2015 ARM Ltd.
60db2e5d1SRobin Murphy  *
70db2e5d1SRobin Murphy  * based in part on arch/arm/mm/dma-mapping.c:
80db2e5d1SRobin Murphy  * Copyright (C) 2000-2004 Russell King
90db2e5d1SRobin Murphy  */
100db2e5d1SRobin Murphy 
11f51dc892SShameer Kolothum #include <linux/acpi_iort.h>
120db2e5d1SRobin Murphy #include <linux/device.h>
1306d60728SChristoph Hellwig #include <linux/dma-contiguous.h>
140db2e5d1SRobin Murphy #include <linux/dma-iommu.h>
15af751d43SChristoph Hellwig #include <linux/dma-noncoherent.h>
165b11e9cdSRobin Murphy #include <linux/gfp.h>
170db2e5d1SRobin Murphy #include <linux/huge_mm.h>
180db2e5d1SRobin Murphy #include <linux/iommu.h>
190db2e5d1SRobin Murphy #include <linux/iova.h>
2044bb7e24SRobin Murphy #include <linux/irq.h>
210db2e5d1SRobin Murphy #include <linux/mm.h>
22fade1ec0SRobin Murphy #include <linux/pci.h>
235b11e9cdSRobin Murphy #include <linux/scatterlist.h>
245b11e9cdSRobin Murphy #include <linux/vmalloc.h>
250db2e5d1SRobin Murphy 
2644bb7e24SRobin Murphy struct iommu_dma_msi_page {
2744bb7e24SRobin Murphy 	struct list_head	list;
2844bb7e24SRobin Murphy 	dma_addr_t		iova;
2944bb7e24SRobin Murphy 	phys_addr_t		phys;
3044bb7e24SRobin Murphy };
3144bb7e24SRobin Murphy 
32fdbe574eSRobin Murphy enum iommu_dma_cookie_type {
33fdbe574eSRobin Murphy 	IOMMU_DMA_IOVA_COOKIE,
34fdbe574eSRobin Murphy 	IOMMU_DMA_MSI_COOKIE,
35fdbe574eSRobin Murphy };
36fdbe574eSRobin Murphy 
3744bb7e24SRobin Murphy struct iommu_dma_cookie {
38fdbe574eSRobin Murphy 	enum iommu_dma_cookie_type	type;
39fdbe574eSRobin Murphy 	union {
40fdbe574eSRobin Murphy 		/* Full allocator for IOMMU_DMA_IOVA_COOKIE */
4144bb7e24SRobin Murphy 		struct iova_domain	iovad;
42fdbe574eSRobin Murphy 		/* Trivial linear page allocator for IOMMU_DMA_MSI_COOKIE */
43fdbe574eSRobin Murphy 		dma_addr_t		msi_iova;
44fdbe574eSRobin Murphy 	};
4544bb7e24SRobin Murphy 	struct list_head		msi_page_list;
4644bb7e24SRobin Murphy 	spinlock_t			msi_lock;
472da274cdSZhen Lei 
482da274cdSZhen Lei 	/* Domain for flush queue callback; NULL if flush queue not in use */
492da274cdSZhen Lei 	struct iommu_domain		*fq_domain;
5044bb7e24SRobin Murphy };
5144bb7e24SRobin Murphy 
52fdbe574eSRobin Murphy static inline size_t cookie_msi_granule(struct iommu_dma_cookie *cookie)
53fdbe574eSRobin Murphy {
54fdbe574eSRobin Murphy 	if (cookie->type == IOMMU_DMA_IOVA_COOKIE)
55fdbe574eSRobin Murphy 		return cookie->iovad.granule;
56fdbe574eSRobin Murphy 	return PAGE_SIZE;
57fdbe574eSRobin Murphy }
58fdbe574eSRobin Murphy 
59fdbe574eSRobin Murphy static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
60fdbe574eSRobin Murphy {
61fdbe574eSRobin Murphy 	struct iommu_dma_cookie *cookie;
62fdbe574eSRobin Murphy 
63fdbe574eSRobin Murphy 	cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
64fdbe574eSRobin Murphy 	if (cookie) {
65fdbe574eSRobin Murphy 		spin_lock_init(&cookie->msi_lock);
66fdbe574eSRobin Murphy 		INIT_LIST_HEAD(&cookie->msi_page_list);
67fdbe574eSRobin Murphy 		cookie->type = type;
68fdbe574eSRobin Murphy 	}
69fdbe574eSRobin Murphy 	return cookie;
7044bb7e24SRobin Murphy }
7144bb7e24SRobin Murphy 
720db2e5d1SRobin Murphy /**
730db2e5d1SRobin Murphy  * iommu_get_dma_cookie - Acquire DMA-API resources for a domain
740db2e5d1SRobin Murphy  * @domain: IOMMU domain to prepare for DMA-API usage
750db2e5d1SRobin Murphy  *
760db2e5d1SRobin Murphy  * IOMMU drivers should normally call this from their domain_alloc
770db2e5d1SRobin Murphy  * callback when domain->type == IOMMU_DOMAIN_DMA.
780db2e5d1SRobin Murphy  */
790db2e5d1SRobin Murphy int iommu_get_dma_cookie(struct iommu_domain *domain)
800db2e5d1SRobin Murphy {
810db2e5d1SRobin Murphy 	if (domain->iova_cookie)
820db2e5d1SRobin Murphy 		return -EEXIST;
830db2e5d1SRobin Murphy 
84fdbe574eSRobin Murphy 	domain->iova_cookie = cookie_alloc(IOMMU_DMA_IOVA_COOKIE);
85fdbe574eSRobin Murphy 	if (!domain->iova_cookie)
8644bb7e24SRobin Murphy 		return -ENOMEM;
870db2e5d1SRobin Murphy 
8844bb7e24SRobin Murphy 	return 0;
890db2e5d1SRobin Murphy }
900db2e5d1SRobin Murphy EXPORT_SYMBOL(iommu_get_dma_cookie);
910db2e5d1SRobin Murphy 
920db2e5d1SRobin Murphy /**
93fdbe574eSRobin Murphy  * iommu_get_msi_cookie - Acquire just MSI remapping resources
94fdbe574eSRobin Murphy  * @domain: IOMMU domain to prepare
95fdbe574eSRobin Murphy  * @base: Start address of IOVA region for MSI mappings
96fdbe574eSRobin Murphy  *
97fdbe574eSRobin Murphy  * Users who manage their own IOVA allocation and do not want DMA API support,
98fdbe574eSRobin Murphy  * but would still like to take advantage of automatic MSI remapping, can use
99fdbe574eSRobin Murphy  * this to initialise their own domain appropriately. Users should reserve a
100fdbe574eSRobin Murphy  * contiguous IOVA region, starting at @base, large enough to accommodate the
101fdbe574eSRobin Murphy  * number of PAGE_SIZE mappings necessary to cover every MSI doorbell address
102fdbe574eSRobin Murphy  * used by the devices attached to @domain.
103fdbe574eSRobin Murphy  */
104fdbe574eSRobin Murphy int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
105fdbe574eSRobin Murphy {
106fdbe574eSRobin Murphy 	struct iommu_dma_cookie *cookie;
107fdbe574eSRobin Murphy 
108fdbe574eSRobin Murphy 	if (domain->type != IOMMU_DOMAIN_UNMANAGED)
109fdbe574eSRobin Murphy 		return -EINVAL;
110fdbe574eSRobin Murphy 
111fdbe574eSRobin Murphy 	if (domain->iova_cookie)
112fdbe574eSRobin Murphy 		return -EEXIST;
113fdbe574eSRobin Murphy 
114fdbe574eSRobin Murphy 	cookie = cookie_alloc(IOMMU_DMA_MSI_COOKIE);
115fdbe574eSRobin Murphy 	if (!cookie)
116fdbe574eSRobin Murphy 		return -ENOMEM;
117fdbe574eSRobin Murphy 
118fdbe574eSRobin Murphy 	cookie->msi_iova = base;
119fdbe574eSRobin Murphy 	domain->iova_cookie = cookie;
120fdbe574eSRobin Murphy 	return 0;
121fdbe574eSRobin Murphy }
122fdbe574eSRobin Murphy EXPORT_SYMBOL(iommu_get_msi_cookie);
123fdbe574eSRobin Murphy 
124fdbe574eSRobin Murphy /**
1250db2e5d1SRobin Murphy  * iommu_put_dma_cookie - Release a domain's DMA mapping resources
126fdbe574eSRobin Murphy  * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() or
127fdbe574eSRobin Murphy  *          iommu_get_msi_cookie()
1280db2e5d1SRobin Murphy  *
1290db2e5d1SRobin Murphy  * IOMMU drivers should normally call this from their domain_free callback.
1300db2e5d1SRobin Murphy  */
1310db2e5d1SRobin Murphy void iommu_put_dma_cookie(struct iommu_domain *domain)
1320db2e5d1SRobin Murphy {
13344bb7e24SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
13444bb7e24SRobin Murphy 	struct iommu_dma_msi_page *msi, *tmp;
1350db2e5d1SRobin Murphy 
13644bb7e24SRobin Murphy 	if (!cookie)
1370db2e5d1SRobin Murphy 		return;
1380db2e5d1SRobin Murphy 
139fdbe574eSRobin Murphy 	if (cookie->type == IOMMU_DMA_IOVA_COOKIE && cookie->iovad.granule)
14044bb7e24SRobin Murphy 		put_iova_domain(&cookie->iovad);
14144bb7e24SRobin Murphy 
14244bb7e24SRobin Murphy 	list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) {
14344bb7e24SRobin Murphy 		list_del(&msi->list);
14444bb7e24SRobin Murphy 		kfree(msi);
14544bb7e24SRobin Murphy 	}
14644bb7e24SRobin Murphy 	kfree(cookie);
1470db2e5d1SRobin Murphy 	domain->iova_cookie = NULL;
1480db2e5d1SRobin Murphy }
1490db2e5d1SRobin Murphy EXPORT_SYMBOL(iommu_put_dma_cookie);
1500db2e5d1SRobin Murphy 
151273df963SRobin Murphy /**
152273df963SRobin Murphy  * iommu_dma_get_resv_regions - Reserved region driver helper
153273df963SRobin Murphy  * @dev: Device from iommu_get_resv_regions()
154273df963SRobin Murphy  * @list: Reserved region list from iommu_get_resv_regions()
155273df963SRobin Murphy  *
156273df963SRobin Murphy  * IOMMU drivers can use this to implement their .get_resv_regions callback
157cd2c9fcfSShameer Kolothum  * for general non-IOMMU-specific reservations. Currently, this covers GICv3
158cd2c9fcfSShameer Kolothum  * ITS region reservation on ACPI based ARM platforms that may require HW MSI
159cd2c9fcfSShameer Kolothum  * reservation.
160273df963SRobin Murphy  */
161273df963SRobin Murphy void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)
162fade1ec0SRobin Murphy {
163fade1ec0SRobin Murphy 
16498cc4f71SJoerg Roedel 	if (!is_of_node(dev_iommu_fwspec_get(dev)->iommu_fwnode))
165cd2c9fcfSShameer Kolothum 		iort_iommu_msi_get_resv_regions(dev, list);
166f51dc892SShameer Kolothum 
167fade1ec0SRobin Murphy }
168273df963SRobin Murphy EXPORT_SYMBOL(iommu_dma_get_resv_regions);
169fade1ec0SRobin Murphy 
1707c1b058cSRobin Murphy static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie,
1717c1b058cSRobin Murphy 		phys_addr_t start, phys_addr_t end)
1727c1b058cSRobin Murphy {
1737c1b058cSRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
1747c1b058cSRobin Murphy 	struct iommu_dma_msi_page *msi_page;
1757c1b058cSRobin Murphy 	int i, num_pages;
1767c1b058cSRobin Murphy 
1777c1b058cSRobin Murphy 	start -= iova_offset(iovad, start);
1787c1b058cSRobin Murphy 	num_pages = iova_align(iovad, end - start) >> iova_shift(iovad);
1797c1b058cSRobin Murphy 
1807c1b058cSRobin Murphy 	msi_page = kcalloc(num_pages, sizeof(*msi_page), GFP_KERNEL);
1817c1b058cSRobin Murphy 	if (!msi_page)
1827c1b058cSRobin Murphy 		return -ENOMEM;
1837c1b058cSRobin Murphy 
1847c1b058cSRobin Murphy 	for (i = 0; i < num_pages; i++) {
1857c1b058cSRobin Murphy 		msi_page[i].phys = start;
1867c1b058cSRobin Murphy 		msi_page[i].iova = start;
1877c1b058cSRobin Murphy 		INIT_LIST_HEAD(&msi_page[i].list);
1887c1b058cSRobin Murphy 		list_add(&msi_page[i].list, &cookie->msi_page_list);
1897c1b058cSRobin Murphy 		start += iovad->granule;
1907c1b058cSRobin Murphy 	}
1917c1b058cSRobin Murphy 
1927c1b058cSRobin Murphy 	return 0;
1937c1b058cSRobin Murphy }
1947c1b058cSRobin Murphy 
195aadad097SSrinath Mannam static int iova_reserve_pci_windows(struct pci_dev *dev,
196cd2c9fcfSShameer Kolothum 		struct iova_domain *iovad)
197cd2c9fcfSShameer Kolothum {
198cd2c9fcfSShameer Kolothum 	struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
199cd2c9fcfSShameer Kolothum 	struct resource_entry *window;
200cd2c9fcfSShameer Kolothum 	unsigned long lo, hi;
201aadad097SSrinath Mannam 	phys_addr_t start = 0, end;
202cd2c9fcfSShameer Kolothum 
203cd2c9fcfSShameer Kolothum 	resource_list_for_each_entry(window, &bridge->windows) {
204cd2c9fcfSShameer Kolothum 		if (resource_type(window->res) != IORESOURCE_MEM)
205cd2c9fcfSShameer Kolothum 			continue;
206cd2c9fcfSShameer Kolothum 
207cd2c9fcfSShameer Kolothum 		lo = iova_pfn(iovad, window->res->start - window->offset);
208cd2c9fcfSShameer Kolothum 		hi = iova_pfn(iovad, window->res->end - window->offset);
209cd2c9fcfSShameer Kolothum 		reserve_iova(iovad, lo, hi);
210cd2c9fcfSShameer Kolothum 	}
211aadad097SSrinath Mannam 
212aadad097SSrinath Mannam 	/* Get reserved DMA windows from host bridge */
213aadad097SSrinath Mannam 	resource_list_for_each_entry(window, &bridge->dma_ranges) {
214aadad097SSrinath Mannam 		end = window->res->start - window->offset;
215aadad097SSrinath Mannam resv_iova:
216aadad097SSrinath Mannam 		if (end > start) {
217aadad097SSrinath Mannam 			lo = iova_pfn(iovad, start);
218aadad097SSrinath Mannam 			hi = iova_pfn(iovad, end);
219aadad097SSrinath Mannam 			reserve_iova(iovad, lo, hi);
220aadad097SSrinath Mannam 		} else {
221aadad097SSrinath Mannam 			/* dma_ranges list should be sorted */
222aadad097SSrinath Mannam 			dev_err(&dev->dev, "Failed to reserve IOVA\n");
223aadad097SSrinath Mannam 			return -EINVAL;
224aadad097SSrinath Mannam 		}
225aadad097SSrinath Mannam 
226aadad097SSrinath Mannam 		start = window->res->end - window->offset + 1;
227aadad097SSrinath Mannam 		/* If window is last entry */
228aadad097SSrinath Mannam 		if (window->node.next == &bridge->dma_ranges &&
22929fcea8cSArnd Bergmann 		    end != ~(phys_addr_t)0) {
23029fcea8cSArnd Bergmann 			end = ~(phys_addr_t)0;
231aadad097SSrinath Mannam 			goto resv_iova;
232aadad097SSrinath Mannam 		}
233aadad097SSrinath Mannam 	}
234aadad097SSrinath Mannam 
235aadad097SSrinath Mannam 	return 0;
236cd2c9fcfSShameer Kolothum }
237cd2c9fcfSShameer Kolothum 
2387c1b058cSRobin Murphy static int iova_reserve_iommu_regions(struct device *dev,
2397c1b058cSRobin Murphy 		struct iommu_domain *domain)
2407c1b058cSRobin Murphy {
2417c1b058cSRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
2427c1b058cSRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
2437c1b058cSRobin Murphy 	struct iommu_resv_region *region;
2447c1b058cSRobin Murphy 	LIST_HEAD(resv_regions);
2457c1b058cSRobin Murphy 	int ret = 0;
2467c1b058cSRobin Murphy 
247aadad097SSrinath Mannam 	if (dev_is_pci(dev)) {
248aadad097SSrinath Mannam 		ret = iova_reserve_pci_windows(to_pci_dev(dev), iovad);
249aadad097SSrinath Mannam 		if (ret)
250aadad097SSrinath Mannam 			return ret;
251aadad097SSrinath Mannam 	}
252cd2c9fcfSShameer Kolothum 
2537c1b058cSRobin Murphy 	iommu_get_resv_regions(dev, &resv_regions);
2547c1b058cSRobin Murphy 	list_for_each_entry(region, &resv_regions, list) {
2557c1b058cSRobin Murphy 		unsigned long lo, hi;
2567c1b058cSRobin Murphy 
2577c1b058cSRobin Murphy 		/* We ARE the software that manages these! */
2587c1b058cSRobin Murphy 		if (region->type == IOMMU_RESV_SW_MSI)
2597c1b058cSRobin Murphy 			continue;
2607c1b058cSRobin Murphy 
2617c1b058cSRobin Murphy 		lo = iova_pfn(iovad, region->start);
2627c1b058cSRobin Murphy 		hi = iova_pfn(iovad, region->start + region->length - 1);
2637c1b058cSRobin Murphy 		reserve_iova(iovad, lo, hi);
2647c1b058cSRobin Murphy 
2657c1b058cSRobin Murphy 		if (region->type == IOMMU_RESV_MSI)
2667c1b058cSRobin Murphy 			ret = cookie_init_hw_msi_region(cookie, region->start,
2677c1b058cSRobin Murphy 					region->start + region->length);
2687c1b058cSRobin Murphy 		if (ret)
2697c1b058cSRobin Murphy 			break;
2707c1b058cSRobin Murphy 	}
2717c1b058cSRobin Murphy 	iommu_put_resv_regions(dev, &resv_regions);
2727c1b058cSRobin Murphy 
2737c1b058cSRobin Murphy 	return ret;
2747c1b058cSRobin Murphy }
2757c1b058cSRobin Murphy 
2762da274cdSZhen Lei static void iommu_dma_flush_iotlb_all(struct iova_domain *iovad)
2772da274cdSZhen Lei {
2782da274cdSZhen Lei 	struct iommu_dma_cookie *cookie;
2792da274cdSZhen Lei 	struct iommu_domain *domain;
2802da274cdSZhen Lei 
2812da274cdSZhen Lei 	cookie = container_of(iovad, struct iommu_dma_cookie, iovad);
2822da274cdSZhen Lei 	domain = cookie->fq_domain;
2832da274cdSZhen Lei 	/*
2842da274cdSZhen Lei 	 * The IOMMU driver supporting DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE
2852da274cdSZhen Lei 	 * implies that ops->flush_iotlb_all must be non-NULL.
2862da274cdSZhen Lei 	 */
2872da274cdSZhen Lei 	domain->ops->flush_iotlb_all(domain);
2882da274cdSZhen Lei }
2892da274cdSZhen Lei 
2900db2e5d1SRobin Murphy /**
2910db2e5d1SRobin Murphy  * iommu_dma_init_domain - Initialise a DMA mapping domain
2920db2e5d1SRobin Murphy  * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
2930db2e5d1SRobin Murphy  * @base: IOVA at which the mappable address space starts
2940db2e5d1SRobin Murphy  * @size: Size of IOVA space
295fade1ec0SRobin Murphy  * @dev: Device the domain is being initialised for
2960db2e5d1SRobin Murphy  *
2970db2e5d1SRobin Murphy  * @base and @size should be exact multiples of IOMMU page granularity to
2980db2e5d1SRobin Murphy  * avoid rounding surprises. If necessary, we reserve the page at address 0
2990db2e5d1SRobin Murphy  * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
3000db2e5d1SRobin Murphy  * any change which could make prior IOVAs invalid will fail.
3010db2e5d1SRobin Murphy  */
30206d60728SChristoph Hellwig static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
303fade1ec0SRobin Murphy 		u64 size, struct device *dev)
3040db2e5d1SRobin Murphy {
305fdbe574eSRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
306fdbe574eSRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
307c61a4633SShaokun Zhang 	unsigned long order, base_pfn;
3082da274cdSZhen Lei 	int attr;
3090db2e5d1SRobin Murphy 
310fdbe574eSRobin Murphy 	if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE)
311fdbe574eSRobin Murphy 		return -EINVAL;
3120db2e5d1SRobin Murphy 
3130db2e5d1SRobin Murphy 	/* Use the smallest supported page size for IOVA granularity */
314d16e0faaSRobin Murphy 	order = __ffs(domain->pgsize_bitmap);
3150db2e5d1SRobin Murphy 	base_pfn = max_t(unsigned long, 1, base >> order);
3160db2e5d1SRobin Murphy 
3170db2e5d1SRobin Murphy 	/* Check the domain allows at least some access to the device... */
3180db2e5d1SRobin Murphy 	if (domain->geometry.force_aperture) {
3190db2e5d1SRobin Murphy 		if (base > domain->geometry.aperture_end ||
3200db2e5d1SRobin Murphy 		    base + size <= domain->geometry.aperture_start) {
3210db2e5d1SRobin Murphy 			pr_warn("specified DMA range outside IOMMU capability\n");
3220db2e5d1SRobin Murphy 			return -EFAULT;
3230db2e5d1SRobin Murphy 		}
3240db2e5d1SRobin Murphy 		/* ...then finally give it a kicking to make sure it fits */
3250db2e5d1SRobin Murphy 		base_pfn = max_t(unsigned long, base_pfn,
3260db2e5d1SRobin Murphy 				domain->geometry.aperture_start >> order);
3270db2e5d1SRobin Murphy 	}
3280db2e5d1SRobin Murphy 
329f51d7bb7SRobin Murphy 	/* start_pfn is always nonzero for an already-initialised domain */
3300db2e5d1SRobin Murphy 	if (iovad->start_pfn) {
3310db2e5d1SRobin Murphy 		if (1UL << order != iovad->granule ||
332f51d7bb7SRobin Murphy 		    base_pfn != iovad->start_pfn) {
3330db2e5d1SRobin Murphy 			pr_warn("Incompatible range for DMA domain\n");
3340db2e5d1SRobin Murphy 			return -EFAULT;
3350db2e5d1SRobin Murphy 		}
3367c1b058cSRobin Murphy 
3370db2e5d1SRobin Murphy 		return 0;
3380db2e5d1SRobin Murphy 	}
3397c1b058cSRobin Murphy 
340aa3ac946SZhen Lei 	init_iova_domain(iovad, 1UL << order, base_pfn);
3412da274cdSZhen Lei 
3422da274cdSZhen Lei 	if (!cookie->fq_domain && !iommu_domain_get_attr(domain,
3432da274cdSZhen Lei 			DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, &attr) && attr) {
3442da274cdSZhen Lei 		cookie->fq_domain = domain;
3452da274cdSZhen Lei 		init_iova_flush_queue(iovad, iommu_dma_flush_iotlb_all, NULL);
3462da274cdSZhen Lei 	}
3472da274cdSZhen Lei 
3487c1b058cSRobin Murphy 	if (!dev)
3497c1b058cSRobin Murphy 		return 0;
3507c1b058cSRobin Murphy 
3517c1b058cSRobin Murphy 	return iova_reserve_iommu_regions(dev, domain);
3527c1b058cSRobin Murphy }
3530db2e5d1SRobin Murphy 
3540db2e5d1SRobin Murphy /**
355737c85caSMitchel Humpherys  * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
356737c85caSMitchel Humpherys  *                    page flags.
3570db2e5d1SRobin Murphy  * @dir: Direction of DMA transfer
3580db2e5d1SRobin Murphy  * @coherent: Is the DMA master cache-coherent?
359737c85caSMitchel Humpherys  * @attrs: DMA attributes for the mapping
3600db2e5d1SRobin Murphy  *
3610db2e5d1SRobin Murphy  * Return: corresponding IOMMU API page protection flags
3620db2e5d1SRobin Murphy  */
36306d60728SChristoph Hellwig static int dma_info_to_prot(enum dma_data_direction dir, bool coherent,
364737c85caSMitchel Humpherys 		     unsigned long attrs)
3650db2e5d1SRobin Murphy {
3660db2e5d1SRobin Murphy 	int prot = coherent ? IOMMU_CACHE : 0;
3670db2e5d1SRobin Murphy 
368737c85caSMitchel Humpherys 	if (attrs & DMA_ATTR_PRIVILEGED)
369737c85caSMitchel Humpherys 		prot |= IOMMU_PRIV;
370737c85caSMitchel Humpherys 
3710db2e5d1SRobin Murphy 	switch (dir) {
3720db2e5d1SRobin Murphy 	case DMA_BIDIRECTIONAL:
3730db2e5d1SRobin Murphy 		return prot | IOMMU_READ | IOMMU_WRITE;
3740db2e5d1SRobin Murphy 	case DMA_TO_DEVICE:
3750db2e5d1SRobin Murphy 		return prot | IOMMU_READ;
3760db2e5d1SRobin Murphy 	case DMA_FROM_DEVICE:
3770db2e5d1SRobin Murphy 		return prot | IOMMU_WRITE;
3780db2e5d1SRobin Murphy 	default:
3790db2e5d1SRobin Murphy 		return 0;
3800db2e5d1SRobin Murphy 	}
3810db2e5d1SRobin Murphy }
3820db2e5d1SRobin Murphy 
383842fe519SRobin Murphy static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
384842fe519SRobin Murphy 		size_t size, dma_addr_t dma_limit, struct device *dev)
3850db2e5d1SRobin Murphy {
386a44e6657SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
387a44e6657SRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
388bb65a64cSRobin Murphy 	unsigned long shift, iova_len, iova = 0;
3890db2e5d1SRobin Murphy 
390a44e6657SRobin Murphy 	if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
391a44e6657SRobin Murphy 		cookie->msi_iova += size;
392a44e6657SRobin Murphy 		return cookie->msi_iova - size;
393a44e6657SRobin Murphy 	}
394a44e6657SRobin Murphy 
395a44e6657SRobin Murphy 	shift = iova_shift(iovad);
396a44e6657SRobin Murphy 	iova_len = size >> shift;
397bb65a64cSRobin Murphy 	/*
398bb65a64cSRobin Murphy 	 * Freeing non-power-of-two-sized allocations back into the IOVA caches
399bb65a64cSRobin Murphy 	 * will come back to bite us badly, so we have to waste a bit of space
400bb65a64cSRobin Murphy 	 * rounding up anything cacheable to make sure that can't happen. The
401bb65a64cSRobin Murphy 	 * order of the unadjusted size will still match upon freeing.
402bb65a64cSRobin Murphy 	 */
403bb65a64cSRobin Murphy 	if (iova_len < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1)))
404bb65a64cSRobin Murphy 		iova_len = roundup_pow_of_two(iova_len);
405a44e6657SRobin Murphy 
40603bfdc31SRobin Murphy 	if (dev->bus_dma_mask)
40703bfdc31SRobin Murphy 		dma_limit &= dev->bus_dma_mask;
40803bfdc31SRobin Murphy 
409c987ff0dSRobin Murphy 	if (domain->geometry.force_aperture)
410c987ff0dSRobin Murphy 		dma_limit = min(dma_limit, domain->geometry.aperture_end);
411122fac03SRobin Murphy 
412122fac03SRobin Murphy 	/* Try to get PCI devices a SAC address */
413122fac03SRobin Murphy 	if (dma_limit > DMA_BIT_MASK(32) && dev_is_pci(dev))
414538d5b33STomasz Nowicki 		iova = alloc_iova_fast(iovad, iova_len,
415538d5b33STomasz Nowicki 				       DMA_BIT_MASK(32) >> shift, false);
416122fac03SRobin Murphy 
417bb65a64cSRobin Murphy 	if (!iova)
418538d5b33STomasz Nowicki 		iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift,
419538d5b33STomasz Nowicki 				       true);
420bb65a64cSRobin Murphy 
421bb65a64cSRobin Murphy 	return (dma_addr_t)iova << shift;
4220db2e5d1SRobin Murphy }
4230db2e5d1SRobin Murphy 
424842fe519SRobin Murphy static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie,
425842fe519SRobin Murphy 		dma_addr_t iova, size_t size)
4260db2e5d1SRobin Murphy {
427842fe519SRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
4280db2e5d1SRobin Murphy 
429a44e6657SRobin Murphy 	/* The MSI case is only ever cleaning up its most recent allocation */
430bb65a64cSRobin Murphy 	if (cookie->type == IOMMU_DMA_MSI_COOKIE)
431a44e6657SRobin Murphy 		cookie->msi_iova -= size;
4322da274cdSZhen Lei 	else if (cookie->fq_domain)	/* non-strict mode */
4332da274cdSZhen Lei 		queue_iova(iovad, iova_pfn(iovad, iova),
4342da274cdSZhen Lei 				size >> iova_shift(iovad), 0);
435bb65a64cSRobin Murphy 	else
4361cc896edSRobin Murphy 		free_iova_fast(iovad, iova_pfn(iovad, iova),
4371cc896edSRobin Murphy 				size >> iova_shift(iovad));
438842fe519SRobin Murphy }
439842fe519SRobin Murphy 
440b61d271eSRobin Murphy static void __iommu_dma_unmap(struct device *dev, dma_addr_t dma_addr,
441842fe519SRobin Murphy 		size_t size)
442842fe519SRobin Murphy {
443b61d271eSRobin Murphy 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
444a44e6657SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
445a44e6657SRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
446842fe519SRobin Murphy 	size_t iova_off = iova_offset(iovad, dma_addr);
447842fe519SRobin Murphy 
448842fe519SRobin Murphy 	dma_addr -= iova_off;
449842fe519SRobin Murphy 	size = iova_align(iovad, size + iova_off);
450842fe519SRobin Murphy 
4512da274cdSZhen Lei 	WARN_ON(iommu_unmap_fast(domain, dma_addr, size) != size);
4522da274cdSZhen Lei 	if (!cookie->fq_domain)
4532da274cdSZhen Lei 		iommu_tlb_sync(domain);
454a44e6657SRobin Murphy 	iommu_dma_free_iova(cookie, dma_addr, size);
4550db2e5d1SRobin Murphy }
4560db2e5d1SRobin Murphy 
45792aec09cSChristoph Hellwig static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
458b61d271eSRobin Murphy 		size_t size, int prot)
45992aec09cSChristoph Hellwig {
460b61d271eSRobin Murphy 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
46192aec09cSChristoph Hellwig 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
4628af23fadSRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
4638af23fadSRobin Murphy 	size_t iova_off = iova_offset(iovad, phys);
46492aec09cSChristoph Hellwig 	dma_addr_t iova;
46592aec09cSChristoph Hellwig 
4668af23fadSRobin Murphy 	size = iova_align(iovad, size + iova_off);
46792aec09cSChristoph Hellwig 
46892aec09cSChristoph Hellwig 	iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
46992aec09cSChristoph Hellwig 	if (!iova)
47092aec09cSChristoph Hellwig 		return DMA_MAPPING_ERROR;
47192aec09cSChristoph Hellwig 
47292aec09cSChristoph Hellwig 	if (iommu_map(domain, iova, phys - iova_off, size, prot)) {
47392aec09cSChristoph Hellwig 		iommu_dma_free_iova(cookie, iova, size);
47492aec09cSChristoph Hellwig 		return DMA_MAPPING_ERROR;
47592aec09cSChristoph Hellwig 	}
47692aec09cSChristoph Hellwig 	return iova + iova_off;
47792aec09cSChristoph Hellwig }
47892aec09cSChristoph Hellwig 
4790db2e5d1SRobin Murphy static void __iommu_dma_free_pages(struct page **pages, int count)
4800db2e5d1SRobin Murphy {
4810db2e5d1SRobin Murphy 	while (count--)
4820db2e5d1SRobin Murphy 		__free_page(pages[count]);
4830db2e5d1SRobin Murphy 	kvfree(pages);
4840db2e5d1SRobin Murphy }
4850db2e5d1SRobin Murphy 
486c4b17afbSGanapatrao Kulkarni static struct page **__iommu_dma_alloc_pages(struct device *dev,
487c4b17afbSGanapatrao Kulkarni 		unsigned int count, unsigned long order_mask, gfp_t gfp)
4880db2e5d1SRobin Murphy {
4890db2e5d1SRobin Murphy 	struct page **pages;
490c4b17afbSGanapatrao Kulkarni 	unsigned int i = 0, nid = dev_to_node(dev);
4913b6b7e19SRobin Murphy 
4923b6b7e19SRobin Murphy 	order_mask &= (2U << MAX_ORDER) - 1;
4933b6b7e19SRobin Murphy 	if (!order_mask)
4943b6b7e19SRobin Murphy 		return NULL;
4950db2e5d1SRobin Murphy 
496c4b17afbSGanapatrao Kulkarni 	pages = kvzalloc(count * sizeof(*pages), GFP_KERNEL);
4970db2e5d1SRobin Murphy 	if (!pages)
4980db2e5d1SRobin Murphy 		return NULL;
4990db2e5d1SRobin Murphy 
5000db2e5d1SRobin Murphy 	/* IOMMU can map any pages, so himem can also be used here */
5010db2e5d1SRobin Murphy 	gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
5020db2e5d1SRobin Murphy 
5030db2e5d1SRobin Murphy 	while (count) {
5040db2e5d1SRobin Murphy 		struct page *page = NULL;
5053b6b7e19SRobin Murphy 		unsigned int order_size;
5060db2e5d1SRobin Murphy 
5070db2e5d1SRobin Murphy 		/*
5080db2e5d1SRobin Murphy 		 * Higher-order allocations are a convenience rather
5090db2e5d1SRobin Murphy 		 * than a necessity, hence using __GFP_NORETRY until
5103b6b7e19SRobin Murphy 		 * falling back to minimum-order allocations.
5110db2e5d1SRobin Murphy 		 */
5123b6b7e19SRobin Murphy 		for (order_mask &= (2U << __fls(count)) - 1;
5133b6b7e19SRobin Murphy 		     order_mask; order_mask &= ~order_size) {
5143b6b7e19SRobin Murphy 			unsigned int order = __fls(order_mask);
515c4b17afbSGanapatrao Kulkarni 			gfp_t alloc_flags = gfp;
5163b6b7e19SRobin Murphy 
5173b6b7e19SRobin Murphy 			order_size = 1U << order;
518c4b17afbSGanapatrao Kulkarni 			if (order_mask > order_size)
519c4b17afbSGanapatrao Kulkarni 				alloc_flags |= __GFP_NORETRY;
520c4b17afbSGanapatrao Kulkarni 			page = alloc_pages_node(nid, alloc_flags, order);
5210db2e5d1SRobin Murphy 			if (!page)
5220db2e5d1SRobin Murphy 				continue;
5233b6b7e19SRobin Murphy 			if (!order)
5240db2e5d1SRobin Murphy 				break;
5253b6b7e19SRobin Murphy 			if (!PageCompound(page)) {
5260db2e5d1SRobin Murphy 				split_page(page, order);
5270db2e5d1SRobin Murphy 				break;
5283b6b7e19SRobin Murphy 			} else if (!split_huge_page(page)) {
5293b6b7e19SRobin Murphy 				break;
5300db2e5d1SRobin Murphy 			}
5313b6b7e19SRobin Murphy 			__free_pages(page, order);
5320db2e5d1SRobin Murphy 		}
5330db2e5d1SRobin Murphy 		if (!page) {
5340db2e5d1SRobin Murphy 			__iommu_dma_free_pages(pages, i);
5350db2e5d1SRobin Murphy 			return NULL;
5360db2e5d1SRobin Murphy 		}
5373b6b7e19SRobin Murphy 		count -= order_size;
5383b6b7e19SRobin Murphy 		while (order_size--)
5390db2e5d1SRobin Murphy 			pages[i++] = page++;
5400db2e5d1SRobin Murphy 	}
5410db2e5d1SRobin Murphy 	return pages;
5420db2e5d1SRobin Murphy }
5430db2e5d1SRobin Murphy 
5440db2e5d1SRobin Murphy /**
54521b95aafSChristoph Hellwig  * iommu_dma_alloc_remap - Allocate and map a buffer contiguous in IOVA space
5460db2e5d1SRobin Murphy  * @dev: Device to allocate memory for. Must be a real device
5470db2e5d1SRobin Murphy  *	 attached to an iommu_dma_domain
5480db2e5d1SRobin Murphy  * @size: Size of buffer in bytes
54921b95aafSChristoph Hellwig  * @dma_handle: Out argument for allocated DMA handle
5500db2e5d1SRobin Murphy  * @gfp: Allocation flags
5513b6b7e19SRobin Murphy  * @attrs: DMA attributes for this allocation
5520db2e5d1SRobin Murphy  *
5530db2e5d1SRobin Murphy  * If @size is less than PAGE_SIZE, then a full CPU page will be allocated,
5540db2e5d1SRobin Murphy  * but an IOMMU which supports smaller pages might not map the whole thing.
5550db2e5d1SRobin Murphy  *
55621b95aafSChristoph Hellwig  * Return: Mapped virtual address, or NULL on failure.
5570db2e5d1SRobin Murphy  */
55821b95aafSChristoph Hellwig static void *iommu_dma_alloc_remap(struct device *dev, size_t size,
55921b95aafSChristoph Hellwig 		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
5600db2e5d1SRobin Murphy {
56143c5bf11SRobin Murphy 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
562842fe519SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
563842fe519SRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
56421b95aafSChristoph Hellwig 	bool coherent = dev_is_dma_coherent(dev);
56521b95aafSChristoph Hellwig 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
56633dcb37cSChristoph Hellwig 	pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs);
56721b95aafSChristoph Hellwig 	unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
5680db2e5d1SRobin Murphy 	struct page **pages;
5690db2e5d1SRobin Murphy 	struct sg_table sgt;
570842fe519SRobin Murphy 	dma_addr_t iova;
57121b95aafSChristoph Hellwig 	void *vaddr;
5720db2e5d1SRobin Murphy 
57321b95aafSChristoph Hellwig 	*dma_handle = DMA_MAPPING_ERROR;
5740db2e5d1SRobin Murphy 
5753b6b7e19SRobin Murphy 	min_size = alloc_sizes & -alloc_sizes;
5763b6b7e19SRobin Murphy 	if (min_size < PAGE_SIZE) {
5773b6b7e19SRobin Murphy 		min_size = PAGE_SIZE;
5783b6b7e19SRobin Murphy 		alloc_sizes |= PAGE_SIZE;
5793b6b7e19SRobin Murphy 	} else {
5803b6b7e19SRobin Murphy 		size = ALIGN(size, min_size);
5813b6b7e19SRobin Murphy 	}
58200085f1eSKrzysztof Kozlowski 	if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
5833b6b7e19SRobin Murphy 		alloc_sizes = min_size;
5843b6b7e19SRobin Murphy 
5853b6b7e19SRobin Murphy 	count = PAGE_ALIGN(size) >> PAGE_SHIFT;
586c4b17afbSGanapatrao Kulkarni 	pages = __iommu_dma_alloc_pages(dev, count, alloc_sizes >> PAGE_SHIFT,
587c4b17afbSGanapatrao Kulkarni 					gfp);
5880db2e5d1SRobin Murphy 	if (!pages)
5890db2e5d1SRobin Murphy 		return NULL;
5900db2e5d1SRobin Murphy 
591842fe519SRobin Murphy 	size = iova_align(iovad, size);
592842fe519SRobin Murphy 	iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev);
5930db2e5d1SRobin Murphy 	if (!iova)
5940db2e5d1SRobin Murphy 		goto out_free_pages;
5950db2e5d1SRobin Murphy 
5960db2e5d1SRobin Murphy 	if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL))
5970db2e5d1SRobin Murphy 		goto out_free_iova;
5980db2e5d1SRobin Murphy 
59921b95aafSChristoph Hellwig 	if (!(ioprot & IOMMU_CACHE)) {
60023f88e0aSChristoph Hellwig 		struct scatterlist *sg;
60123f88e0aSChristoph Hellwig 		int i;
60223f88e0aSChristoph Hellwig 
60323f88e0aSChristoph Hellwig 		for_each_sg(sgt.sgl, sg, sgt.orig_nents, i)
60423f88e0aSChristoph Hellwig 			arch_dma_prep_coherent(sg_page(sg), sg->length);
6050db2e5d1SRobin Murphy 	}
6060db2e5d1SRobin Murphy 
60721b95aafSChristoph Hellwig 	if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, ioprot)
6080db2e5d1SRobin Murphy 			< size)
6090db2e5d1SRobin Murphy 		goto out_free_sg;
6100db2e5d1SRobin Murphy 
61151231740SChristoph Hellwig 	vaddr = dma_common_pages_remap(pages, size, prot,
61221b95aafSChristoph Hellwig 			__builtin_return_address(0));
61321b95aafSChristoph Hellwig 	if (!vaddr)
61421b95aafSChristoph Hellwig 		goto out_unmap;
6150db2e5d1SRobin Murphy 
61621b95aafSChristoph Hellwig 	*dma_handle = iova;
61721b95aafSChristoph Hellwig 	sg_free_table(&sgt);
61821b95aafSChristoph Hellwig 	return vaddr;
61921b95aafSChristoph Hellwig 
62021b95aafSChristoph Hellwig out_unmap:
62121b95aafSChristoph Hellwig 	__iommu_dma_unmap(dev, iova, size);
6220db2e5d1SRobin Murphy out_free_sg:
6230db2e5d1SRobin Murphy 	sg_free_table(&sgt);
6240db2e5d1SRobin Murphy out_free_iova:
625842fe519SRobin Murphy 	iommu_dma_free_iova(cookie, iova, size);
6260db2e5d1SRobin Murphy out_free_pages:
6270db2e5d1SRobin Murphy 	__iommu_dma_free_pages(pages, count);
6280db2e5d1SRobin Murphy 	return NULL;
6290db2e5d1SRobin Murphy }
6300db2e5d1SRobin Murphy 
6310db2e5d1SRobin Murphy /**
63206d60728SChristoph Hellwig  * __iommu_dma_mmap - Map a buffer into provided user VMA
63306d60728SChristoph Hellwig  * @pages: Array representing buffer from __iommu_dma_alloc()
6340db2e5d1SRobin Murphy  * @size: Size of buffer in bytes
6350db2e5d1SRobin Murphy  * @vma: VMA describing requested userspace mapping
6360db2e5d1SRobin Murphy  *
6370db2e5d1SRobin Murphy  * Maps the pages of the buffer in @pages into @vma. The caller is responsible
6380db2e5d1SRobin Murphy  * for verifying the correct size and protection of @vma beforehand.
6390db2e5d1SRobin Murphy  */
64006d60728SChristoph Hellwig static int __iommu_dma_mmap(struct page **pages, size_t size,
64106d60728SChristoph Hellwig 		struct vm_area_struct *vma)
6420db2e5d1SRobin Murphy {
643b0d0084fSSouptick Joarder 	return vm_map_pages(vma, pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
6440db2e5d1SRobin Murphy }
6450db2e5d1SRobin Murphy 
64606d60728SChristoph Hellwig static void iommu_dma_sync_single_for_cpu(struct device *dev,
64706d60728SChristoph Hellwig 		dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
6480db2e5d1SRobin Murphy {
64906d60728SChristoph Hellwig 	phys_addr_t phys;
6500db2e5d1SRobin Murphy 
65106d60728SChristoph Hellwig 	if (dev_is_dma_coherent(dev))
65206d60728SChristoph Hellwig 		return;
65306d60728SChristoph Hellwig 
65406d60728SChristoph Hellwig 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
65506d60728SChristoph Hellwig 	arch_sync_dma_for_cpu(dev, phys, size, dir);
6561cc896edSRobin Murphy }
6571cc896edSRobin Murphy 
65806d60728SChristoph Hellwig static void iommu_dma_sync_single_for_device(struct device *dev,
65906d60728SChristoph Hellwig 		dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
66051f8cc9eSRobin Murphy {
66106d60728SChristoph Hellwig 	phys_addr_t phys;
66206d60728SChristoph Hellwig 
66306d60728SChristoph Hellwig 	if (dev_is_dma_coherent(dev))
66406d60728SChristoph Hellwig 		return;
66506d60728SChristoph Hellwig 
66606d60728SChristoph Hellwig 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
66706d60728SChristoph Hellwig 	arch_sync_dma_for_device(dev, phys, size, dir);
66851f8cc9eSRobin Murphy }
66951f8cc9eSRobin Murphy 
67006d60728SChristoph Hellwig static void iommu_dma_sync_sg_for_cpu(struct device *dev,
67106d60728SChristoph Hellwig 		struct scatterlist *sgl, int nelems,
67206d60728SChristoph Hellwig 		enum dma_data_direction dir)
6730db2e5d1SRobin Murphy {
67406d60728SChristoph Hellwig 	struct scatterlist *sg;
67506d60728SChristoph Hellwig 	int i;
67606d60728SChristoph Hellwig 
67706d60728SChristoph Hellwig 	if (dev_is_dma_coherent(dev))
67806d60728SChristoph Hellwig 		return;
67906d60728SChristoph Hellwig 
68006d60728SChristoph Hellwig 	for_each_sg(sgl, sg, nelems, i)
68106d60728SChristoph Hellwig 		arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
68206d60728SChristoph Hellwig }
68306d60728SChristoph Hellwig 
68406d60728SChristoph Hellwig static void iommu_dma_sync_sg_for_device(struct device *dev,
68506d60728SChristoph Hellwig 		struct scatterlist *sgl, int nelems,
68606d60728SChristoph Hellwig 		enum dma_data_direction dir)
68706d60728SChristoph Hellwig {
68806d60728SChristoph Hellwig 	struct scatterlist *sg;
68906d60728SChristoph Hellwig 	int i;
69006d60728SChristoph Hellwig 
69106d60728SChristoph Hellwig 	if (dev_is_dma_coherent(dev))
69206d60728SChristoph Hellwig 		return;
69306d60728SChristoph Hellwig 
69406d60728SChristoph Hellwig 	for_each_sg(sgl, sg, nelems, i)
69506d60728SChristoph Hellwig 		arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, dir);
69606d60728SChristoph Hellwig }
69706d60728SChristoph Hellwig 
69806d60728SChristoph Hellwig static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
69906d60728SChristoph Hellwig 		unsigned long offset, size_t size, enum dma_data_direction dir,
70006d60728SChristoph Hellwig 		unsigned long attrs)
70106d60728SChristoph Hellwig {
70206d60728SChristoph Hellwig 	phys_addr_t phys = page_to_phys(page) + offset;
70306d60728SChristoph Hellwig 	bool coherent = dev_is_dma_coherent(dev);
704b61d271eSRobin Murphy 	int prot = dma_info_to_prot(dir, coherent, attrs);
70506d60728SChristoph Hellwig 	dma_addr_t dma_handle;
70606d60728SChristoph Hellwig 
707b61d271eSRobin Murphy 	dma_handle =__iommu_dma_map(dev, phys, size, prot);
70806d60728SChristoph Hellwig 	if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
70906d60728SChristoph Hellwig 	    dma_handle != DMA_MAPPING_ERROR)
71006d60728SChristoph Hellwig 		arch_sync_dma_for_device(dev, phys, size, dir);
71106d60728SChristoph Hellwig 	return dma_handle;
71206d60728SChristoph Hellwig }
71306d60728SChristoph Hellwig 
71406d60728SChristoph Hellwig static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
71506d60728SChristoph Hellwig 		size_t size, enum dma_data_direction dir, unsigned long attrs)
71606d60728SChristoph Hellwig {
71706d60728SChristoph Hellwig 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
71806d60728SChristoph Hellwig 		iommu_dma_sync_single_for_cpu(dev, dma_handle, size, dir);
719b61d271eSRobin Murphy 	__iommu_dma_unmap(dev, dma_handle, size);
7200db2e5d1SRobin Murphy }
7210db2e5d1SRobin Murphy 
7220db2e5d1SRobin Murphy /*
7230db2e5d1SRobin Murphy  * Prepare a successfully-mapped scatterlist to give back to the caller.
724809eac54SRobin Murphy  *
725809eac54SRobin Murphy  * At this point the segments are already laid out by iommu_dma_map_sg() to
726809eac54SRobin Murphy  * avoid individually crossing any boundaries, so we merely need to check a
727809eac54SRobin Murphy  * segment's start address to avoid concatenating across one.
7280db2e5d1SRobin Murphy  */
7290db2e5d1SRobin Murphy static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
7300db2e5d1SRobin Murphy 		dma_addr_t dma_addr)
7310db2e5d1SRobin Murphy {
732809eac54SRobin Murphy 	struct scatterlist *s, *cur = sg;
733809eac54SRobin Murphy 	unsigned long seg_mask = dma_get_seg_boundary(dev);
734809eac54SRobin Murphy 	unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
735809eac54SRobin Murphy 	int i, count = 0;
7360db2e5d1SRobin Murphy 
7370db2e5d1SRobin Murphy 	for_each_sg(sg, s, nents, i) {
738809eac54SRobin Murphy 		/* Restore this segment's original unaligned fields first */
739809eac54SRobin Murphy 		unsigned int s_iova_off = sg_dma_address(s);
7400db2e5d1SRobin Murphy 		unsigned int s_length = sg_dma_len(s);
741809eac54SRobin Murphy 		unsigned int s_iova_len = s->length;
7420db2e5d1SRobin Murphy 
743809eac54SRobin Murphy 		s->offset += s_iova_off;
7440db2e5d1SRobin Murphy 		s->length = s_length;
745cad34be7SChristoph Hellwig 		sg_dma_address(s) = DMA_MAPPING_ERROR;
746809eac54SRobin Murphy 		sg_dma_len(s) = 0;
747809eac54SRobin Murphy 
748809eac54SRobin Murphy 		/*
749809eac54SRobin Murphy 		 * Now fill in the real DMA data. If...
750809eac54SRobin Murphy 		 * - there is a valid output segment to append to
751809eac54SRobin Murphy 		 * - and this segment starts on an IOVA page boundary
752809eac54SRobin Murphy 		 * - but doesn't fall at a segment boundary
753809eac54SRobin Murphy 		 * - and wouldn't make the resulting output segment too long
754809eac54SRobin Murphy 		 */
755809eac54SRobin Murphy 		if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
756ab2cbeb0SRobin Murphy 		    (max_len - cur_len >= s_length)) {
757809eac54SRobin Murphy 			/* ...then concatenate it with the previous one */
758809eac54SRobin Murphy 			cur_len += s_length;
759809eac54SRobin Murphy 		} else {
760809eac54SRobin Murphy 			/* Otherwise start the next output segment */
761809eac54SRobin Murphy 			if (i > 0)
762809eac54SRobin Murphy 				cur = sg_next(cur);
763809eac54SRobin Murphy 			cur_len = s_length;
764809eac54SRobin Murphy 			count++;
765809eac54SRobin Murphy 
766809eac54SRobin Murphy 			sg_dma_address(cur) = dma_addr + s_iova_off;
7670db2e5d1SRobin Murphy 		}
768809eac54SRobin Murphy 
769809eac54SRobin Murphy 		sg_dma_len(cur) = cur_len;
770809eac54SRobin Murphy 		dma_addr += s_iova_len;
771809eac54SRobin Murphy 
772809eac54SRobin Murphy 		if (s_length + s_iova_off < s_iova_len)
773809eac54SRobin Murphy 			cur_len = 0;
774809eac54SRobin Murphy 	}
775809eac54SRobin Murphy 	return count;
7760db2e5d1SRobin Murphy }
7770db2e5d1SRobin Murphy 
7780db2e5d1SRobin Murphy /*
7790db2e5d1SRobin Murphy  * If mapping failed, then just restore the original list,
7800db2e5d1SRobin Murphy  * but making sure the DMA fields are invalidated.
7810db2e5d1SRobin Murphy  */
7820db2e5d1SRobin Murphy static void __invalidate_sg(struct scatterlist *sg, int nents)
7830db2e5d1SRobin Murphy {
7840db2e5d1SRobin Murphy 	struct scatterlist *s;
7850db2e5d1SRobin Murphy 	int i;
7860db2e5d1SRobin Murphy 
7870db2e5d1SRobin Murphy 	for_each_sg(sg, s, nents, i) {
788cad34be7SChristoph Hellwig 		if (sg_dma_address(s) != DMA_MAPPING_ERROR)
78907b48ac4SRobin Murphy 			s->offset += sg_dma_address(s);
7900db2e5d1SRobin Murphy 		if (sg_dma_len(s))
7910db2e5d1SRobin Murphy 			s->length = sg_dma_len(s);
792cad34be7SChristoph Hellwig 		sg_dma_address(s) = DMA_MAPPING_ERROR;
7930db2e5d1SRobin Murphy 		sg_dma_len(s) = 0;
7940db2e5d1SRobin Murphy 	}
7950db2e5d1SRobin Murphy }
7960db2e5d1SRobin Murphy 
7970db2e5d1SRobin Murphy /*
7980db2e5d1SRobin Murphy  * The DMA API client is passing in a scatterlist which could describe
7990db2e5d1SRobin Murphy  * any old buffer layout, but the IOMMU API requires everything to be
8000db2e5d1SRobin Murphy  * aligned to IOMMU pages. Hence the need for this complicated bit of
8010db2e5d1SRobin Murphy  * impedance-matching, to be able to hand off a suitably-aligned list,
8020db2e5d1SRobin Murphy  * but still preserve the original offsets and sizes for the caller.
8030db2e5d1SRobin Murphy  */
80406d60728SChristoph Hellwig static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
80506d60728SChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs)
8060db2e5d1SRobin Murphy {
80743c5bf11SRobin Murphy 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
808842fe519SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
809842fe519SRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
8100db2e5d1SRobin Murphy 	struct scatterlist *s, *prev = NULL;
81106d60728SChristoph Hellwig 	int prot = dma_info_to_prot(dir, dev_is_dma_coherent(dev), attrs);
812842fe519SRobin Murphy 	dma_addr_t iova;
8130db2e5d1SRobin Murphy 	size_t iova_len = 0;
814809eac54SRobin Murphy 	unsigned long mask = dma_get_seg_boundary(dev);
8150db2e5d1SRobin Murphy 	int i;
8160db2e5d1SRobin Murphy 
81706d60728SChristoph Hellwig 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
81806d60728SChristoph Hellwig 		iommu_dma_sync_sg_for_device(dev, sg, nents, dir);
81906d60728SChristoph Hellwig 
8200db2e5d1SRobin Murphy 	/*
8210db2e5d1SRobin Murphy 	 * Work out how much IOVA space we need, and align the segments to
8220db2e5d1SRobin Murphy 	 * IOVA granules for the IOMMU driver to handle. With some clever
8230db2e5d1SRobin Murphy 	 * trickery we can modify the list in-place, but reversibly, by
824809eac54SRobin Murphy 	 * stashing the unaligned parts in the as-yet-unused DMA fields.
8250db2e5d1SRobin Murphy 	 */
8260db2e5d1SRobin Murphy 	for_each_sg(sg, s, nents, i) {
827809eac54SRobin Murphy 		size_t s_iova_off = iova_offset(iovad, s->offset);
8280db2e5d1SRobin Murphy 		size_t s_length = s->length;
829809eac54SRobin Murphy 		size_t pad_len = (mask - iova_len + 1) & mask;
8300db2e5d1SRobin Murphy 
831809eac54SRobin Murphy 		sg_dma_address(s) = s_iova_off;
8320db2e5d1SRobin Murphy 		sg_dma_len(s) = s_length;
833809eac54SRobin Murphy 		s->offset -= s_iova_off;
834809eac54SRobin Murphy 		s_length = iova_align(iovad, s_length + s_iova_off);
8350db2e5d1SRobin Murphy 		s->length = s_length;
8360db2e5d1SRobin Murphy 
8370db2e5d1SRobin Murphy 		/*
838809eac54SRobin Murphy 		 * Due to the alignment of our single IOVA allocation, we can
839809eac54SRobin Murphy 		 * depend on these assumptions about the segment boundary mask:
840809eac54SRobin Murphy 		 * - If mask size >= IOVA size, then the IOVA range cannot
841809eac54SRobin Murphy 		 *   possibly fall across a boundary, so we don't care.
842809eac54SRobin Murphy 		 * - If mask size < IOVA size, then the IOVA range must start
843809eac54SRobin Murphy 		 *   exactly on a boundary, therefore we can lay things out
844809eac54SRobin Murphy 		 *   based purely on segment lengths without needing to know
845809eac54SRobin Murphy 		 *   the actual addresses beforehand.
846809eac54SRobin Murphy 		 * - The mask must be a power of 2, so pad_len == 0 if
847809eac54SRobin Murphy 		 *   iova_len == 0, thus we cannot dereference prev the first
848809eac54SRobin Murphy 		 *   time through here (i.e. before it has a meaningful value).
8490db2e5d1SRobin Murphy 		 */
850809eac54SRobin Murphy 		if (pad_len && pad_len < s_length - 1) {
8510db2e5d1SRobin Murphy 			prev->length += pad_len;
8520db2e5d1SRobin Murphy 			iova_len += pad_len;
8530db2e5d1SRobin Murphy 		}
8540db2e5d1SRobin Murphy 
8550db2e5d1SRobin Murphy 		iova_len += s_length;
8560db2e5d1SRobin Murphy 		prev = s;
8570db2e5d1SRobin Murphy 	}
8580db2e5d1SRobin Murphy 
859842fe519SRobin Murphy 	iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
8600db2e5d1SRobin Murphy 	if (!iova)
8610db2e5d1SRobin Murphy 		goto out_restore_sg;
8620db2e5d1SRobin Murphy 
8630db2e5d1SRobin Murphy 	/*
8640db2e5d1SRobin Murphy 	 * We'll leave any physical concatenation to the IOMMU driver's
8650db2e5d1SRobin Murphy 	 * implementation - it knows better than we do.
8660db2e5d1SRobin Murphy 	 */
867842fe519SRobin Murphy 	if (iommu_map_sg(domain, iova, sg, nents, prot) < iova_len)
8680db2e5d1SRobin Murphy 		goto out_free_iova;
8690db2e5d1SRobin Murphy 
870842fe519SRobin Murphy 	return __finalise_sg(dev, sg, nents, iova);
8710db2e5d1SRobin Murphy 
8720db2e5d1SRobin Murphy out_free_iova:
873842fe519SRobin Murphy 	iommu_dma_free_iova(cookie, iova, iova_len);
8740db2e5d1SRobin Murphy out_restore_sg:
8750db2e5d1SRobin Murphy 	__invalidate_sg(sg, nents);
8760db2e5d1SRobin Murphy 	return 0;
8770db2e5d1SRobin Murphy }
8780db2e5d1SRobin Murphy 
87906d60728SChristoph Hellwig static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
88006d60728SChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs)
8810db2e5d1SRobin Murphy {
882842fe519SRobin Murphy 	dma_addr_t start, end;
883842fe519SRobin Murphy 	struct scatterlist *tmp;
884842fe519SRobin Murphy 	int i;
88506d60728SChristoph Hellwig 
8861b961423SNathan Chancellor 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
88706d60728SChristoph Hellwig 		iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
88806d60728SChristoph Hellwig 
8890db2e5d1SRobin Murphy 	/*
8900db2e5d1SRobin Murphy 	 * The scatterlist segments are mapped into a single
8910db2e5d1SRobin Murphy 	 * contiguous IOVA allocation, so this is incredibly easy.
8920db2e5d1SRobin Murphy 	 */
893842fe519SRobin Murphy 	start = sg_dma_address(sg);
894842fe519SRobin Murphy 	for_each_sg(sg_next(sg), tmp, nents - 1, i) {
895842fe519SRobin Murphy 		if (sg_dma_len(tmp) == 0)
896842fe519SRobin Murphy 			break;
897842fe519SRobin Murphy 		sg = tmp;
898842fe519SRobin Murphy 	}
899842fe519SRobin Murphy 	end = sg_dma_address(sg) + sg_dma_len(sg);
900b61d271eSRobin Murphy 	__iommu_dma_unmap(dev, start, end - start);
9010db2e5d1SRobin Murphy }
9020db2e5d1SRobin Murphy 
90306d60728SChristoph Hellwig static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
90451f8cc9eSRobin Murphy 		size_t size, enum dma_data_direction dir, unsigned long attrs)
90551f8cc9eSRobin Murphy {
90651f8cc9eSRobin Murphy 	return __iommu_dma_map(dev, phys, size,
907b61d271eSRobin Murphy 			dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO);
90851f8cc9eSRobin Murphy }
90951f8cc9eSRobin Murphy 
91006d60728SChristoph Hellwig static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
91151f8cc9eSRobin Murphy 		size_t size, enum dma_data_direction dir, unsigned long attrs)
91251f8cc9eSRobin Murphy {
913b61d271eSRobin Murphy 	__iommu_dma_unmap(dev, handle, size);
91451f8cc9eSRobin Murphy }
91551f8cc9eSRobin Murphy 
9168553f6e6SRobin Murphy static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)
917bcf4b9c4SRobin Murphy {
918bcf4b9c4SRobin Murphy 	size_t alloc_size = PAGE_ALIGN(size);
919bcf4b9c4SRobin Murphy 	int count = alloc_size >> PAGE_SHIFT;
920bcf4b9c4SRobin Murphy 	struct page *page = NULL, **pages = NULL;
921bcf4b9c4SRobin Murphy 
922bcf4b9c4SRobin Murphy 	/* Non-coherent atomic allocation? Easy */
923e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
924e6475eb0SChristoph Hellwig 	    dma_free_from_pool(cpu_addr, alloc_size))
925bcf4b9c4SRobin Murphy 		return;
926bcf4b9c4SRobin Murphy 
927e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
928bcf4b9c4SRobin Murphy 		/*
929bcf4b9c4SRobin Murphy 		 * If it the address is remapped, then it's either non-coherent
930bcf4b9c4SRobin Murphy 		 * or highmem CMA, or an iommu_dma_alloc_remap() construction.
931bcf4b9c4SRobin Murphy 		 */
9325cf45379SChristoph Hellwig 		pages = dma_common_find_pages(cpu_addr);
933bcf4b9c4SRobin Murphy 		if (!pages)
934bcf4b9c4SRobin Murphy 			page = vmalloc_to_page(cpu_addr);
93551231740SChristoph Hellwig 		dma_common_free_remap(cpu_addr, alloc_size);
936bcf4b9c4SRobin Murphy 	} else {
937bcf4b9c4SRobin Murphy 		/* Lowmem means a coherent atomic or CMA allocation */
938bcf4b9c4SRobin Murphy 		page = virt_to_page(cpu_addr);
939bcf4b9c4SRobin Murphy 	}
940bcf4b9c4SRobin Murphy 
941bcf4b9c4SRobin Murphy 	if (pages)
942bcf4b9c4SRobin Murphy 		__iommu_dma_free_pages(pages, count);
943591fcf3bSNicolin Chen 	if (page)
944591fcf3bSNicolin Chen 		dma_free_contiguous(dev, page, alloc_size);
945bcf4b9c4SRobin Murphy }
946bcf4b9c4SRobin Murphy 
9478553f6e6SRobin Murphy static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
9488553f6e6SRobin Murphy 		dma_addr_t handle, unsigned long attrs)
9498553f6e6SRobin Murphy {
9508553f6e6SRobin Murphy 	__iommu_dma_unmap(dev, handle, size);
9518553f6e6SRobin Murphy 	__iommu_dma_free(dev, size, cpu_addr);
9528553f6e6SRobin Murphy }
9538553f6e6SRobin Murphy 
954ee1ef05dSChristoph Hellwig static void *iommu_dma_alloc_pages(struct device *dev, size_t size,
955ee1ef05dSChristoph Hellwig 		struct page **pagep, gfp_t gfp, unsigned long attrs)
95606d60728SChristoph Hellwig {
95706d60728SChristoph Hellwig 	bool coherent = dev_is_dma_coherent(dev);
9589ad5d6edSRobin Murphy 	size_t alloc_size = PAGE_ALIGN(size);
95990ae409fSChristoph Hellwig 	int node = dev_to_node(dev);
9609a4ab94aSChristoph Hellwig 	struct page *page = NULL;
9619ad5d6edSRobin Murphy 	void *cpu_addr;
96206d60728SChristoph Hellwig 
963591fcf3bSNicolin Chen 	page = dma_alloc_contiguous(dev, alloc_size, gfp);
96406d60728SChristoph Hellwig 	if (!page)
96590ae409fSChristoph Hellwig 		page = alloc_pages_node(node, gfp, get_order(alloc_size));
96690ae409fSChristoph Hellwig 	if (!page)
96706d60728SChristoph Hellwig 		return NULL;
96806d60728SChristoph Hellwig 
969e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && (!coherent || PageHighMem(page))) {
97033dcb37cSChristoph Hellwig 		pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs);
9718680aa5aSRobin Murphy 
9729ad5d6edSRobin Murphy 		cpu_addr = dma_common_contiguous_remap(page, alloc_size,
97351231740SChristoph Hellwig 				prot, __builtin_return_address(0));
9749ad5d6edSRobin Murphy 		if (!cpu_addr)
975ee1ef05dSChristoph Hellwig 			goto out_free_pages;
976072bebc0SRobin Murphy 
97706d60728SChristoph Hellwig 		if (!coherent)
9789ad5d6edSRobin Murphy 			arch_dma_prep_coherent(page, size);
9798680aa5aSRobin Murphy 	} else {
9809ad5d6edSRobin Murphy 		cpu_addr = page_address(page);
9818680aa5aSRobin Murphy 	}
982ee1ef05dSChristoph Hellwig 
983ee1ef05dSChristoph Hellwig 	*pagep = page;
9849ad5d6edSRobin Murphy 	memset(cpu_addr, 0, alloc_size);
9859ad5d6edSRobin Murphy 	return cpu_addr;
986072bebc0SRobin Murphy out_free_pages:
987591fcf3bSNicolin Chen 	dma_free_contiguous(dev, page, alloc_size);
988072bebc0SRobin Murphy 	return NULL;
98906d60728SChristoph Hellwig }
99006d60728SChristoph Hellwig 
991ee1ef05dSChristoph Hellwig static void *iommu_dma_alloc(struct device *dev, size_t size,
992ee1ef05dSChristoph Hellwig 		dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
993ee1ef05dSChristoph Hellwig {
994ee1ef05dSChristoph Hellwig 	bool coherent = dev_is_dma_coherent(dev);
995ee1ef05dSChristoph Hellwig 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
996ee1ef05dSChristoph Hellwig 	struct page *page = NULL;
997ee1ef05dSChristoph Hellwig 	void *cpu_addr;
998ee1ef05dSChristoph Hellwig 
999ee1ef05dSChristoph Hellwig 	gfp |= __GFP_ZERO;
1000ee1ef05dSChristoph Hellwig 
1001e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && gfpflags_allow_blocking(gfp) &&
1002ee1ef05dSChristoph Hellwig 	    !(attrs & DMA_ATTR_FORCE_CONTIGUOUS))
1003ee1ef05dSChristoph Hellwig 		return iommu_dma_alloc_remap(dev, size, handle, gfp, attrs);
1004ee1ef05dSChristoph Hellwig 
1005e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
1006e6475eb0SChristoph Hellwig 	    !gfpflags_allow_blocking(gfp) && !coherent)
1007ee1ef05dSChristoph Hellwig 		cpu_addr = dma_alloc_from_pool(PAGE_ALIGN(size), &page, gfp);
1008ee1ef05dSChristoph Hellwig 	else
1009ee1ef05dSChristoph Hellwig 		cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs);
1010ee1ef05dSChristoph Hellwig 	if (!cpu_addr)
1011ee1ef05dSChristoph Hellwig 		return NULL;
1012ee1ef05dSChristoph Hellwig 
1013ee1ef05dSChristoph Hellwig 	*handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot);
1014ee1ef05dSChristoph Hellwig 	if (*handle == DMA_MAPPING_ERROR) {
1015ee1ef05dSChristoph Hellwig 		__iommu_dma_free(dev, size, cpu_addr);
1016ee1ef05dSChristoph Hellwig 		return NULL;
1017ee1ef05dSChristoph Hellwig 	}
1018ee1ef05dSChristoph Hellwig 
1019ee1ef05dSChristoph Hellwig 	return cpu_addr;
1020ee1ef05dSChristoph Hellwig }
1021ee1ef05dSChristoph Hellwig 
102206d60728SChristoph Hellwig static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
102306d60728SChristoph Hellwig 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
102406d60728SChristoph Hellwig 		unsigned long attrs)
102506d60728SChristoph Hellwig {
102606d60728SChristoph Hellwig 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1027efd9f10bSChristoph Hellwig 	unsigned long pfn, off = vma->vm_pgoff;
102806d60728SChristoph Hellwig 	int ret;
102906d60728SChristoph Hellwig 
103033dcb37cSChristoph Hellwig 	vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
103106d60728SChristoph Hellwig 
103206d60728SChristoph Hellwig 	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
103306d60728SChristoph Hellwig 		return ret;
103406d60728SChristoph Hellwig 
103506d60728SChristoph Hellwig 	if (off >= nr_pages || vma_pages(vma) > nr_pages - off)
103606d60728SChristoph Hellwig 		return -ENXIO;
103706d60728SChristoph Hellwig 
1038e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
10395cf45379SChristoph Hellwig 		struct page **pages = dma_common_find_pages(cpu_addr);
104006d60728SChristoph Hellwig 
1041efd9f10bSChristoph Hellwig 		if (pages)
10424c360aceSRobin Murphy 			return __iommu_dma_mmap(pages, size, vma);
1043efd9f10bSChristoph Hellwig 		pfn = vmalloc_to_pfn(cpu_addr);
1044efd9f10bSChristoph Hellwig 	} else {
1045efd9f10bSChristoph Hellwig 		pfn = page_to_pfn(virt_to_page(cpu_addr));
1046efd9f10bSChristoph Hellwig 	}
1047efd9f10bSChristoph Hellwig 
1048efd9f10bSChristoph Hellwig 	return remap_pfn_range(vma, vma->vm_start, pfn + off,
1049efd9f10bSChristoph Hellwig 			       vma->vm_end - vma->vm_start,
1050efd9f10bSChristoph Hellwig 			       vma->vm_page_prot);
105106d60728SChristoph Hellwig }
105206d60728SChristoph Hellwig 
105306d60728SChristoph Hellwig static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
105406d60728SChristoph Hellwig 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
105506d60728SChristoph Hellwig 		unsigned long attrs)
105606d60728SChristoph Hellwig {
10573fb3378bSChristoph Hellwig 	struct page *page;
10583fb3378bSChristoph Hellwig 	int ret;
105906d60728SChristoph Hellwig 
1060e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
10615cf45379SChristoph Hellwig 		struct page **pages = dma_common_find_pages(cpu_addr);
10623fb3378bSChristoph Hellwig 
10633fb3378bSChristoph Hellwig 		if (pages) {
10643fb3378bSChristoph Hellwig 			return sg_alloc_table_from_pages(sgt, pages,
10653fb3378bSChristoph Hellwig 					PAGE_ALIGN(size) >> PAGE_SHIFT,
10663fb3378bSChristoph Hellwig 					0, size, GFP_KERNEL);
106706d60728SChristoph Hellwig 		}
106806d60728SChristoph Hellwig 
10693fb3378bSChristoph Hellwig 		page = vmalloc_to_page(cpu_addr);
10703fb3378bSChristoph Hellwig 	} else {
10713fb3378bSChristoph Hellwig 		page = virt_to_page(cpu_addr);
107206d60728SChristoph Hellwig 	}
107306d60728SChristoph Hellwig 
10743fb3378bSChristoph Hellwig 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
10753fb3378bSChristoph Hellwig 	if (!ret)
10763fb3378bSChristoph Hellwig 		sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
10773fb3378bSChristoph Hellwig 	return ret;
107806d60728SChristoph Hellwig }
107906d60728SChristoph Hellwig 
1080158a6d3cSYoshihiro Shimoda static unsigned long iommu_dma_get_merge_boundary(struct device *dev)
1081158a6d3cSYoshihiro Shimoda {
1082158a6d3cSYoshihiro Shimoda 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
1083158a6d3cSYoshihiro Shimoda 
1084158a6d3cSYoshihiro Shimoda 	return (1UL << __ffs(domain->pgsize_bitmap)) - 1;
1085158a6d3cSYoshihiro Shimoda }
1086158a6d3cSYoshihiro Shimoda 
108706d60728SChristoph Hellwig static const struct dma_map_ops iommu_dma_ops = {
108806d60728SChristoph Hellwig 	.alloc			= iommu_dma_alloc,
108906d60728SChristoph Hellwig 	.free			= iommu_dma_free,
109006d60728SChristoph Hellwig 	.mmap			= iommu_dma_mmap,
109106d60728SChristoph Hellwig 	.get_sgtable		= iommu_dma_get_sgtable,
109206d60728SChristoph Hellwig 	.map_page		= iommu_dma_map_page,
109306d60728SChristoph Hellwig 	.unmap_page		= iommu_dma_unmap_page,
109406d60728SChristoph Hellwig 	.map_sg			= iommu_dma_map_sg,
109506d60728SChristoph Hellwig 	.unmap_sg		= iommu_dma_unmap_sg,
109606d60728SChristoph Hellwig 	.sync_single_for_cpu	= iommu_dma_sync_single_for_cpu,
109706d60728SChristoph Hellwig 	.sync_single_for_device	= iommu_dma_sync_single_for_device,
109806d60728SChristoph Hellwig 	.sync_sg_for_cpu	= iommu_dma_sync_sg_for_cpu,
109906d60728SChristoph Hellwig 	.sync_sg_for_device	= iommu_dma_sync_sg_for_device,
110006d60728SChristoph Hellwig 	.map_resource		= iommu_dma_map_resource,
110106d60728SChristoph Hellwig 	.unmap_resource		= iommu_dma_unmap_resource,
1102158a6d3cSYoshihiro Shimoda 	.get_merge_boundary	= iommu_dma_get_merge_boundary,
110306d60728SChristoph Hellwig };
110406d60728SChristoph Hellwig 
110506d60728SChristoph Hellwig /*
110606d60728SChristoph Hellwig  * The IOMMU core code allocates the default DMA domain, which the underlying
110706d60728SChristoph Hellwig  * IOMMU driver needs to support via the dma-iommu layer.
110806d60728SChristoph Hellwig  */
110906d60728SChristoph Hellwig void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size)
111006d60728SChristoph Hellwig {
111106d60728SChristoph Hellwig 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
111206d60728SChristoph Hellwig 
111306d60728SChristoph Hellwig 	if (!domain)
111406d60728SChristoph Hellwig 		goto out_err;
111506d60728SChristoph Hellwig 
111606d60728SChristoph Hellwig 	/*
111706d60728SChristoph Hellwig 	 * The IOMMU core code allocates the default DMA domain, which the
111806d60728SChristoph Hellwig 	 * underlying IOMMU driver needs to support via the dma-iommu layer.
111906d60728SChristoph Hellwig 	 */
112006d60728SChristoph Hellwig 	if (domain->type == IOMMU_DOMAIN_DMA) {
112106d60728SChristoph Hellwig 		if (iommu_dma_init_domain(domain, dma_base, size, dev))
112206d60728SChristoph Hellwig 			goto out_err;
112306d60728SChristoph Hellwig 		dev->dma_ops = &iommu_dma_ops;
112406d60728SChristoph Hellwig 	}
112506d60728SChristoph Hellwig 
112606d60728SChristoph Hellwig 	return;
112706d60728SChristoph Hellwig out_err:
112806d60728SChristoph Hellwig 	 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
112906d60728SChristoph Hellwig 		 dev_name(dev));
113044bb7e24SRobin Murphy }
113144bb7e24SRobin Murphy 
113244bb7e24SRobin Murphy static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
113344bb7e24SRobin Murphy 		phys_addr_t msi_addr, struct iommu_domain *domain)
113444bb7e24SRobin Murphy {
113544bb7e24SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
113644bb7e24SRobin Murphy 	struct iommu_dma_msi_page *msi_page;
1137842fe519SRobin Murphy 	dma_addr_t iova;
113844bb7e24SRobin Murphy 	int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1139fdbe574eSRobin Murphy 	size_t size = cookie_msi_granule(cookie);
114044bb7e24SRobin Murphy 
1141fdbe574eSRobin Murphy 	msi_addr &= ~(phys_addr_t)(size - 1);
114244bb7e24SRobin Murphy 	list_for_each_entry(msi_page, &cookie->msi_page_list, list)
114344bb7e24SRobin Murphy 		if (msi_page->phys == msi_addr)
114444bb7e24SRobin Murphy 			return msi_page;
114544bb7e24SRobin Murphy 
114644bb7e24SRobin Murphy 	msi_page = kzalloc(sizeof(*msi_page), GFP_ATOMIC);
114744bb7e24SRobin Murphy 	if (!msi_page)
114844bb7e24SRobin Murphy 		return NULL;
114944bb7e24SRobin Murphy 
11508af23fadSRobin Murphy 	iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
11518af23fadSRobin Murphy 	if (!iova)
115244bb7e24SRobin Murphy 		goto out_free_page;
115344bb7e24SRobin Murphy 
11548af23fadSRobin Murphy 	if (iommu_map(domain, iova, msi_addr, size, prot))
11558af23fadSRobin Murphy 		goto out_free_iova;
11568af23fadSRobin Murphy 
115744bb7e24SRobin Murphy 	INIT_LIST_HEAD(&msi_page->list);
1158a44e6657SRobin Murphy 	msi_page->phys = msi_addr;
1159a44e6657SRobin Murphy 	msi_page->iova = iova;
116044bb7e24SRobin Murphy 	list_add(&msi_page->list, &cookie->msi_page_list);
116144bb7e24SRobin Murphy 	return msi_page;
116244bb7e24SRobin Murphy 
11638af23fadSRobin Murphy out_free_iova:
11648af23fadSRobin Murphy 	iommu_dma_free_iova(cookie, iova, size);
116544bb7e24SRobin Murphy out_free_page:
116644bb7e24SRobin Murphy 	kfree(msi_page);
116744bb7e24SRobin Murphy 	return NULL;
116844bb7e24SRobin Murphy }
116944bb7e24SRobin Murphy 
1170ece6e6f0SJulien Grall int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
117144bb7e24SRobin Murphy {
1172ece6e6f0SJulien Grall 	struct device *dev = msi_desc_to_dev(desc);
117344bb7e24SRobin Murphy 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
117444bb7e24SRobin Murphy 	struct iommu_dma_cookie *cookie;
117544bb7e24SRobin Murphy 	struct iommu_dma_msi_page *msi_page;
117644bb7e24SRobin Murphy 	unsigned long flags;
117744bb7e24SRobin Murphy 
1178ece6e6f0SJulien Grall 	if (!domain || !domain->iova_cookie) {
1179ece6e6f0SJulien Grall 		desc->iommu_cookie = NULL;
1180ece6e6f0SJulien Grall 		return 0;
1181ece6e6f0SJulien Grall 	}
118244bb7e24SRobin Murphy 
118344bb7e24SRobin Murphy 	cookie = domain->iova_cookie;
118444bb7e24SRobin Murphy 
118544bb7e24SRobin Murphy 	/*
118644bb7e24SRobin Murphy 	 * We disable IRQs to rule out a possible inversion against
118744bb7e24SRobin Murphy 	 * irq_desc_lock if, say, someone tries to retarget the affinity
118844bb7e24SRobin Murphy 	 * of an MSI from within an IPI handler.
118944bb7e24SRobin Murphy 	 */
119044bb7e24SRobin Murphy 	spin_lock_irqsave(&cookie->msi_lock, flags);
119144bb7e24SRobin Murphy 	msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
119244bb7e24SRobin Murphy 	spin_unlock_irqrestore(&cookie->msi_lock, flags);
119344bb7e24SRobin Murphy 
1194ece6e6f0SJulien Grall 	msi_desc_set_iommu_cookie(desc, msi_page);
1195ece6e6f0SJulien Grall 
1196ece6e6f0SJulien Grall 	if (!msi_page)
1197ece6e6f0SJulien Grall 		return -ENOMEM;
1198ece6e6f0SJulien Grall 	return 0;
119944bb7e24SRobin Murphy }
1200ece6e6f0SJulien Grall 
1201ece6e6f0SJulien Grall void iommu_dma_compose_msi_msg(struct msi_desc *desc,
1202ece6e6f0SJulien Grall 			       struct msi_msg *msg)
1203ece6e6f0SJulien Grall {
1204ece6e6f0SJulien Grall 	struct device *dev = msi_desc_to_dev(desc);
1205ece6e6f0SJulien Grall 	const struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1206ece6e6f0SJulien Grall 	const struct iommu_dma_msi_page *msi_page;
1207ece6e6f0SJulien Grall 
1208ece6e6f0SJulien Grall 	msi_page = msi_desc_get_iommu_cookie(desc);
1209ece6e6f0SJulien Grall 
1210ece6e6f0SJulien Grall 	if (!domain || !domain->iova_cookie || WARN_ON(!msi_page))
1211ece6e6f0SJulien Grall 		return;
1212ece6e6f0SJulien Grall 
1213ece6e6f0SJulien Grall 	msg->address_hi = upper_32_bits(msi_page->iova);
1214ece6e6f0SJulien Grall 	msg->address_lo &= cookie_msi_granule(domain->iova_cookie) - 1;
1215ece6e6f0SJulien Grall 	msg->address_lo += lower_32_bits(msi_page->iova);
121644bb7e24SRobin Murphy }
121706d60728SChristoph Hellwig 
121806d60728SChristoph Hellwig static int iommu_dma_init(void)
121906d60728SChristoph Hellwig {
122006d60728SChristoph Hellwig 	return iova_cache_get();
12210db2e5d1SRobin Murphy }
122206d60728SChristoph Hellwig arch_initcall(iommu_dma_init);
1223