xref: /openbmc/linux/drivers/iommu/dma-iommu.c (revision a9f4d93d)
1*a9f4d93dSChristoph Hellwig // SPDX-License-Identifier: GPL-2.0
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 &&
229aadad097SSrinath Mannam 		    end != ~(dma_addr_t)0) {
230aadad097SSrinath Mannam 			end = ~(dma_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;
46292aec09cSChristoph Hellwig 	size_t iova_off = 0;
46392aec09cSChristoph Hellwig 	dma_addr_t iova;
46492aec09cSChristoph Hellwig 
46592aec09cSChristoph Hellwig 	if (cookie->type == IOMMU_DMA_IOVA_COOKIE) {
46692aec09cSChristoph Hellwig 		iova_off = iova_offset(&cookie->iovad, phys);
46792aec09cSChristoph Hellwig 		size = iova_align(&cookie->iovad, size + iova_off);
46892aec09cSChristoph Hellwig 	}
46992aec09cSChristoph Hellwig 
47092aec09cSChristoph Hellwig 	iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
47192aec09cSChristoph Hellwig 	if (!iova)
47292aec09cSChristoph Hellwig 		return DMA_MAPPING_ERROR;
47392aec09cSChristoph Hellwig 
47492aec09cSChristoph Hellwig 	if (iommu_map(domain, iova, phys - iova_off, size, prot)) {
47592aec09cSChristoph Hellwig 		iommu_dma_free_iova(cookie, iova, size);
47692aec09cSChristoph Hellwig 		return DMA_MAPPING_ERROR;
47792aec09cSChristoph Hellwig 	}
47892aec09cSChristoph Hellwig 	return iova + iova_off;
47992aec09cSChristoph Hellwig }
48092aec09cSChristoph Hellwig 
4810db2e5d1SRobin Murphy static void __iommu_dma_free_pages(struct page **pages, int count)
4820db2e5d1SRobin Murphy {
4830db2e5d1SRobin Murphy 	while (count--)
4840db2e5d1SRobin Murphy 		__free_page(pages[count]);
4850db2e5d1SRobin Murphy 	kvfree(pages);
4860db2e5d1SRobin Murphy }
4870db2e5d1SRobin Murphy 
488c4b17afbSGanapatrao Kulkarni static struct page **__iommu_dma_alloc_pages(struct device *dev,
489c4b17afbSGanapatrao Kulkarni 		unsigned int count, unsigned long order_mask, gfp_t gfp)
4900db2e5d1SRobin Murphy {
4910db2e5d1SRobin Murphy 	struct page **pages;
492c4b17afbSGanapatrao Kulkarni 	unsigned int i = 0, nid = dev_to_node(dev);
4933b6b7e19SRobin Murphy 
4943b6b7e19SRobin Murphy 	order_mask &= (2U << MAX_ORDER) - 1;
4953b6b7e19SRobin Murphy 	if (!order_mask)
4963b6b7e19SRobin Murphy 		return NULL;
4970db2e5d1SRobin Murphy 
498c4b17afbSGanapatrao Kulkarni 	pages = kvzalloc(count * sizeof(*pages), GFP_KERNEL);
4990db2e5d1SRobin Murphy 	if (!pages)
5000db2e5d1SRobin Murphy 		return NULL;
5010db2e5d1SRobin Murphy 
5020db2e5d1SRobin Murphy 	/* IOMMU can map any pages, so himem can also be used here */
5030db2e5d1SRobin Murphy 	gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
5040db2e5d1SRobin Murphy 
5050db2e5d1SRobin Murphy 	while (count) {
5060db2e5d1SRobin Murphy 		struct page *page = NULL;
5073b6b7e19SRobin Murphy 		unsigned int order_size;
5080db2e5d1SRobin Murphy 
5090db2e5d1SRobin Murphy 		/*
5100db2e5d1SRobin Murphy 		 * Higher-order allocations are a convenience rather
5110db2e5d1SRobin Murphy 		 * than a necessity, hence using __GFP_NORETRY until
5123b6b7e19SRobin Murphy 		 * falling back to minimum-order allocations.
5130db2e5d1SRobin Murphy 		 */
5143b6b7e19SRobin Murphy 		for (order_mask &= (2U << __fls(count)) - 1;
5153b6b7e19SRobin Murphy 		     order_mask; order_mask &= ~order_size) {
5163b6b7e19SRobin Murphy 			unsigned int order = __fls(order_mask);
517c4b17afbSGanapatrao Kulkarni 			gfp_t alloc_flags = gfp;
5183b6b7e19SRobin Murphy 
5193b6b7e19SRobin Murphy 			order_size = 1U << order;
520c4b17afbSGanapatrao Kulkarni 			if (order_mask > order_size)
521c4b17afbSGanapatrao Kulkarni 				alloc_flags |= __GFP_NORETRY;
522c4b17afbSGanapatrao Kulkarni 			page = alloc_pages_node(nid, alloc_flags, order);
5230db2e5d1SRobin Murphy 			if (!page)
5240db2e5d1SRobin Murphy 				continue;
5253b6b7e19SRobin Murphy 			if (!order)
5260db2e5d1SRobin Murphy 				break;
5273b6b7e19SRobin Murphy 			if (!PageCompound(page)) {
5280db2e5d1SRobin Murphy 				split_page(page, order);
5290db2e5d1SRobin Murphy 				break;
5303b6b7e19SRobin Murphy 			} else if (!split_huge_page(page)) {
5313b6b7e19SRobin Murphy 				break;
5320db2e5d1SRobin Murphy 			}
5333b6b7e19SRobin Murphy 			__free_pages(page, order);
5340db2e5d1SRobin Murphy 		}
5350db2e5d1SRobin Murphy 		if (!page) {
5360db2e5d1SRobin Murphy 			__iommu_dma_free_pages(pages, i);
5370db2e5d1SRobin Murphy 			return NULL;
5380db2e5d1SRobin Murphy 		}
5393b6b7e19SRobin Murphy 		count -= order_size;
5403b6b7e19SRobin Murphy 		while (order_size--)
5410db2e5d1SRobin Murphy 			pages[i++] = page++;
5420db2e5d1SRobin Murphy 	}
5430db2e5d1SRobin Murphy 	return pages;
5440db2e5d1SRobin Murphy }
5450db2e5d1SRobin Murphy 
5464c360aceSRobin Murphy static struct page **__iommu_dma_get_pages(void *cpu_addr)
5474c360aceSRobin Murphy {
5484c360aceSRobin Murphy 	struct vm_struct *area = find_vm_area(cpu_addr);
5494c360aceSRobin Murphy 
5504c360aceSRobin Murphy 	if (!area || !area->pages)
5514c360aceSRobin Murphy 		return NULL;
5524c360aceSRobin Murphy 	return area->pages;
5534c360aceSRobin Murphy }
5544c360aceSRobin Murphy 
5550db2e5d1SRobin Murphy /**
55621b95aafSChristoph Hellwig  * iommu_dma_alloc_remap - Allocate and map a buffer contiguous in IOVA space
5570db2e5d1SRobin Murphy  * @dev: Device to allocate memory for. Must be a real device
5580db2e5d1SRobin Murphy  *	 attached to an iommu_dma_domain
5590db2e5d1SRobin Murphy  * @size: Size of buffer in bytes
56021b95aafSChristoph Hellwig  * @dma_handle: Out argument for allocated DMA handle
5610db2e5d1SRobin Murphy  * @gfp: Allocation flags
5623b6b7e19SRobin Murphy  * @attrs: DMA attributes for this allocation
5630db2e5d1SRobin Murphy  *
5640db2e5d1SRobin Murphy  * If @size is less than PAGE_SIZE, then a full CPU page will be allocated,
5650db2e5d1SRobin Murphy  * but an IOMMU which supports smaller pages might not map the whole thing.
5660db2e5d1SRobin Murphy  *
56721b95aafSChristoph Hellwig  * Return: Mapped virtual address, or NULL on failure.
5680db2e5d1SRobin Murphy  */
56921b95aafSChristoph Hellwig static void *iommu_dma_alloc_remap(struct device *dev, size_t size,
57021b95aafSChristoph Hellwig 		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
5710db2e5d1SRobin Murphy {
57243c5bf11SRobin Murphy 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
573842fe519SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
574842fe519SRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
57521b95aafSChristoph Hellwig 	bool coherent = dev_is_dma_coherent(dev);
57621b95aafSChristoph Hellwig 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
57721b95aafSChristoph Hellwig 	pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs);
57821b95aafSChristoph Hellwig 	unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
5790db2e5d1SRobin Murphy 	struct page **pages;
5800db2e5d1SRobin Murphy 	struct sg_table sgt;
581842fe519SRobin Murphy 	dma_addr_t iova;
58221b95aafSChristoph Hellwig 	void *vaddr;
5830db2e5d1SRobin Murphy 
58421b95aafSChristoph Hellwig 	*dma_handle = DMA_MAPPING_ERROR;
5850db2e5d1SRobin Murphy 
5863b6b7e19SRobin Murphy 	min_size = alloc_sizes & -alloc_sizes;
5873b6b7e19SRobin Murphy 	if (min_size < PAGE_SIZE) {
5883b6b7e19SRobin Murphy 		min_size = PAGE_SIZE;
5893b6b7e19SRobin Murphy 		alloc_sizes |= PAGE_SIZE;
5903b6b7e19SRobin Murphy 	} else {
5913b6b7e19SRobin Murphy 		size = ALIGN(size, min_size);
5923b6b7e19SRobin Murphy 	}
59300085f1eSKrzysztof Kozlowski 	if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
5943b6b7e19SRobin Murphy 		alloc_sizes = min_size;
5953b6b7e19SRobin Murphy 
5963b6b7e19SRobin Murphy 	count = PAGE_ALIGN(size) >> PAGE_SHIFT;
597c4b17afbSGanapatrao Kulkarni 	pages = __iommu_dma_alloc_pages(dev, count, alloc_sizes >> PAGE_SHIFT,
598c4b17afbSGanapatrao Kulkarni 					gfp);
5990db2e5d1SRobin Murphy 	if (!pages)
6000db2e5d1SRobin Murphy 		return NULL;
6010db2e5d1SRobin Murphy 
602842fe519SRobin Murphy 	size = iova_align(iovad, size);
603842fe519SRobin Murphy 	iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev);
6040db2e5d1SRobin Murphy 	if (!iova)
6050db2e5d1SRobin Murphy 		goto out_free_pages;
6060db2e5d1SRobin Murphy 
6070db2e5d1SRobin Murphy 	if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL))
6080db2e5d1SRobin Murphy 		goto out_free_iova;
6090db2e5d1SRobin Murphy 
61021b95aafSChristoph Hellwig 	if (!(ioprot & IOMMU_CACHE)) {
61123f88e0aSChristoph Hellwig 		struct scatterlist *sg;
61223f88e0aSChristoph Hellwig 		int i;
61323f88e0aSChristoph Hellwig 
61423f88e0aSChristoph Hellwig 		for_each_sg(sgt.sgl, sg, sgt.orig_nents, i)
61523f88e0aSChristoph Hellwig 			arch_dma_prep_coherent(sg_page(sg), sg->length);
6160db2e5d1SRobin Murphy 	}
6170db2e5d1SRobin Murphy 
61821b95aafSChristoph Hellwig 	if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, ioprot)
6190db2e5d1SRobin Murphy 			< size)
6200db2e5d1SRobin Murphy 		goto out_free_sg;
6210db2e5d1SRobin Murphy 
62221b95aafSChristoph Hellwig 	vaddr = dma_common_pages_remap(pages, size, VM_USERMAP, prot,
62321b95aafSChristoph Hellwig 			__builtin_return_address(0));
62421b95aafSChristoph Hellwig 	if (!vaddr)
62521b95aafSChristoph Hellwig 		goto out_unmap;
6260db2e5d1SRobin Murphy 
62721b95aafSChristoph Hellwig 	*dma_handle = iova;
62821b95aafSChristoph Hellwig 	sg_free_table(&sgt);
62921b95aafSChristoph Hellwig 	return vaddr;
63021b95aafSChristoph Hellwig 
63121b95aafSChristoph Hellwig out_unmap:
63221b95aafSChristoph Hellwig 	__iommu_dma_unmap(dev, iova, size);
6330db2e5d1SRobin Murphy out_free_sg:
6340db2e5d1SRobin Murphy 	sg_free_table(&sgt);
6350db2e5d1SRobin Murphy out_free_iova:
636842fe519SRobin Murphy 	iommu_dma_free_iova(cookie, iova, size);
6370db2e5d1SRobin Murphy out_free_pages:
6380db2e5d1SRobin Murphy 	__iommu_dma_free_pages(pages, count);
6390db2e5d1SRobin Murphy 	return NULL;
6400db2e5d1SRobin Murphy }
6410db2e5d1SRobin Murphy 
6420db2e5d1SRobin Murphy /**
64306d60728SChristoph Hellwig  * __iommu_dma_mmap - Map a buffer into provided user VMA
64406d60728SChristoph Hellwig  * @pages: Array representing buffer from __iommu_dma_alloc()
6450db2e5d1SRobin Murphy  * @size: Size of buffer in bytes
6460db2e5d1SRobin Murphy  * @vma: VMA describing requested userspace mapping
6470db2e5d1SRobin Murphy  *
6480db2e5d1SRobin Murphy  * Maps the pages of the buffer in @pages into @vma. The caller is responsible
6490db2e5d1SRobin Murphy  * for verifying the correct size and protection of @vma beforehand.
6500db2e5d1SRobin Murphy  */
65106d60728SChristoph Hellwig static int __iommu_dma_mmap(struct page **pages, size_t size,
65206d60728SChristoph Hellwig 		struct vm_area_struct *vma)
6530db2e5d1SRobin Murphy {
654b0d0084fSSouptick Joarder 	return vm_map_pages(vma, pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
6550db2e5d1SRobin Murphy }
6560db2e5d1SRobin Murphy 
65706d60728SChristoph Hellwig static void iommu_dma_sync_single_for_cpu(struct device *dev,
65806d60728SChristoph Hellwig 		dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
65906d60728SChristoph Hellwig {
66006d60728SChristoph Hellwig 	phys_addr_t phys;
66106d60728SChristoph Hellwig 
66206d60728SChristoph Hellwig 	if (dev_is_dma_coherent(dev))
66306d60728SChristoph Hellwig 		return;
66406d60728SChristoph Hellwig 
66506d60728SChristoph Hellwig 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
66606d60728SChristoph Hellwig 	arch_sync_dma_for_cpu(dev, phys, size, dir);
66706d60728SChristoph Hellwig }
66806d60728SChristoph Hellwig 
66906d60728SChristoph Hellwig static void iommu_dma_sync_single_for_device(struct device *dev,
67006d60728SChristoph Hellwig 		dma_addr_t dma_handle, size_t size, enum dma_data_direction dir)
67106d60728SChristoph Hellwig {
67206d60728SChristoph Hellwig 	phys_addr_t phys;
67306d60728SChristoph Hellwig 
67406d60728SChristoph Hellwig 	if (dev_is_dma_coherent(dev))
67506d60728SChristoph Hellwig 		return;
67606d60728SChristoph Hellwig 
67706d60728SChristoph Hellwig 	phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
67806d60728SChristoph Hellwig 	arch_sync_dma_for_device(dev, phys, size, dir);
67906d60728SChristoph Hellwig }
68006d60728SChristoph Hellwig 
68106d60728SChristoph Hellwig static void iommu_dma_sync_sg_for_cpu(struct device *dev,
68206d60728SChristoph Hellwig 		struct scatterlist *sgl, int nelems,
68306d60728SChristoph Hellwig 		enum dma_data_direction dir)
68406d60728SChristoph Hellwig {
68506d60728SChristoph Hellwig 	struct scatterlist *sg;
68606d60728SChristoph Hellwig 	int i;
68706d60728SChristoph Hellwig 
68806d60728SChristoph Hellwig 	if (dev_is_dma_coherent(dev))
68906d60728SChristoph Hellwig 		return;
69006d60728SChristoph Hellwig 
69106d60728SChristoph Hellwig 	for_each_sg(sgl, sg, nelems, i)
69206d60728SChristoph Hellwig 		arch_sync_dma_for_cpu(dev, sg_phys(sg), sg->length, dir);
69306d60728SChristoph Hellwig }
69406d60728SChristoph Hellwig 
69506d60728SChristoph Hellwig static void iommu_dma_sync_sg_for_device(struct device *dev,
69606d60728SChristoph Hellwig 		struct scatterlist *sgl, int nelems,
69706d60728SChristoph Hellwig 		enum dma_data_direction dir)
69806d60728SChristoph Hellwig {
69906d60728SChristoph Hellwig 	struct scatterlist *sg;
70006d60728SChristoph Hellwig 	int i;
70106d60728SChristoph Hellwig 
70206d60728SChristoph Hellwig 	if (dev_is_dma_coherent(dev))
70306d60728SChristoph Hellwig 		return;
70406d60728SChristoph Hellwig 
70506d60728SChristoph Hellwig 	for_each_sg(sgl, sg, nelems, i)
70606d60728SChristoph Hellwig 		arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, dir);
70706d60728SChristoph Hellwig }
70806d60728SChristoph Hellwig 
70906d60728SChristoph Hellwig static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
71006d60728SChristoph Hellwig 		unsigned long offset, size_t size, enum dma_data_direction dir,
71106d60728SChristoph Hellwig 		unsigned long attrs)
71206d60728SChristoph Hellwig {
71306d60728SChristoph Hellwig 	phys_addr_t phys = page_to_phys(page) + offset;
71406d60728SChristoph Hellwig 	bool coherent = dev_is_dma_coherent(dev);
715b61d271eSRobin Murphy 	int prot = dma_info_to_prot(dir, coherent, attrs);
71606d60728SChristoph Hellwig 	dma_addr_t dma_handle;
71706d60728SChristoph Hellwig 
718b61d271eSRobin Murphy 	dma_handle =__iommu_dma_map(dev, phys, size, prot);
71906d60728SChristoph Hellwig 	if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
72006d60728SChristoph Hellwig 	    dma_handle != DMA_MAPPING_ERROR)
72106d60728SChristoph Hellwig 		arch_sync_dma_for_device(dev, phys, size, dir);
72206d60728SChristoph Hellwig 	return dma_handle;
72306d60728SChristoph Hellwig }
72406d60728SChristoph Hellwig 
72506d60728SChristoph Hellwig static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle,
72606d60728SChristoph Hellwig 		size_t size, enum dma_data_direction dir, unsigned long attrs)
72706d60728SChristoph Hellwig {
72806d60728SChristoph Hellwig 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
72906d60728SChristoph Hellwig 		iommu_dma_sync_single_for_cpu(dev, dma_handle, size, dir);
730b61d271eSRobin Murphy 	__iommu_dma_unmap(dev, dma_handle, size);
73106d60728SChristoph Hellwig }
73206d60728SChristoph Hellwig 
7330db2e5d1SRobin Murphy /*
7340db2e5d1SRobin Murphy  * Prepare a successfully-mapped scatterlist to give back to the caller.
735809eac54SRobin Murphy  *
736809eac54SRobin Murphy  * At this point the segments are already laid out by iommu_dma_map_sg() to
737809eac54SRobin Murphy  * avoid individually crossing any boundaries, so we merely need to check a
738809eac54SRobin Murphy  * segment's start address to avoid concatenating across one.
7390db2e5d1SRobin Murphy  */
7400db2e5d1SRobin Murphy static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
7410db2e5d1SRobin Murphy 		dma_addr_t dma_addr)
7420db2e5d1SRobin Murphy {
743809eac54SRobin Murphy 	struct scatterlist *s, *cur = sg;
744809eac54SRobin Murphy 	unsigned long seg_mask = dma_get_seg_boundary(dev);
745809eac54SRobin Murphy 	unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
746809eac54SRobin Murphy 	int i, count = 0;
7470db2e5d1SRobin Murphy 
7480db2e5d1SRobin Murphy 	for_each_sg(sg, s, nents, i) {
749809eac54SRobin Murphy 		/* Restore this segment's original unaligned fields first */
750809eac54SRobin Murphy 		unsigned int s_iova_off = sg_dma_address(s);
7510db2e5d1SRobin Murphy 		unsigned int s_length = sg_dma_len(s);
752809eac54SRobin Murphy 		unsigned int s_iova_len = s->length;
7530db2e5d1SRobin Murphy 
754809eac54SRobin Murphy 		s->offset += s_iova_off;
7550db2e5d1SRobin Murphy 		s->length = s_length;
756cad34be7SChristoph Hellwig 		sg_dma_address(s) = DMA_MAPPING_ERROR;
757809eac54SRobin Murphy 		sg_dma_len(s) = 0;
758809eac54SRobin Murphy 
759809eac54SRobin Murphy 		/*
760809eac54SRobin Murphy 		 * Now fill in the real DMA data. If...
761809eac54SRobin Murphy 		 * - there is a valid output segment to append to
762809eac54SRobin Murphy 		 * - and this segment starts on an IOVA page boundary
763809eac54SRobin Murphy 		 * - but doesn't fall at a segment boundary
764809eac54SRobin Murphy 		 * - and wouldn't make the resulting output segment too long
765809eac54SRobin Murphy 		 */
766809eac54SRobin Murphy 		if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
767809eac54SRobin Murphy 		    (cur_len + s_length <= max_len)) {
768809eac54SRobin Murphy 			/* ...then concatenate it with the previous one */
769809eac54SRobin Murphy 			cur_len += s_length;
770809eac54SRobin Murphy 		} else {
771809eac54SRobin Murphy 			/* Otherwise start the next output segment */
772809eac54SRobin Murphy 			if (i > 0)
773809eac54SRobin Murphy 				cur = sg_next(cur);
774809eac54SRobin Murphy 			cur_len = s_length;
775809eac54SRobin Murphy 			count++;
776809eac54SRobin Murphy 
777809eac54SRobin Murphy 			sg_dma_address(cur) = dma_addr + s_iova_off;
7780db2e5d1SRobin Murphy 		}
779809eac54SRobin Murphy 
780809eac54SRobin Murphy 		sg_dma_len(cur) = cur_len;
781809eac54SRobin Murphy 		dma_addr += s_iova_len;
782809eac54SRobin Murphy 
783809eac54SRobin Murphy 		if (s_length + s_iova_off < s_iova_len)
784809eac54SRobin Murphy 			cur_len = 0;
785809eac54SRobin Murphy 	}
786809eac54SRobin Murphy 	return count;
7870db2e5d1SRobin Murphy }
7880db2e5d1SRobin Murphy 
7890db2e5d1SRobin Murphy /*
7900db2e5d1SRobin Murphy  * If mapping failed, then just restore the original list,
7910db2e5d1SRobin Murphy  * but making sure the DMA fields are invalidated.
7920db2e5d1SRobin Murphy  */
7930db2e5d1SRobin Murphy static void __invalidate_sg(struct scatterlist *sg, int nents)
7940db2e5d1SRobin Murphy {
7950db2e5d1SRobin Murphy 	struct scatterlist *s;
7960db2e5d1SRobin Murphy 	int i;
7970db2e5d1SRobin Murphy 
7980db2e5d1SRobin Murphy 	for_each_sg(sg, s, nents, i) {
799cad34be7SChristoph Hellwig 		if (sg_dma_address(s) != DMA_MAPPING_ERROR)
80007b48ac4SRobin Murphy 			s->offset += sg_dma_address(s);
8010db2e5d1SRobin Murphy 		if (sg_dma_len(s))
8020db2e5d1SRobin Murphy 			s->length = sg_dma_len(s);
803cad34be7SChristoph Hellwig 		sg_dma_address(s) = DMA_MAPPING_ERROR;
8040db2e5d1SRobin Murphy 		sg_dma_len(s) = 0;
8050db2e5d1SRobin Murphy 	}
8060db2e5d1SRobin Murphy }
8070db2e5d1SRobin Murphy 
8080db2e5d1SRobin Murphy /*
8090db2e5d1SRobin Murphy  * The DMA API client is passing in a scatterlist which could describe
8100db2e5d1SRobin Murphy  * any old buffer layout, but the IOMMU API requires everything to be
8110db2e5d1SRobin Murphy  * aligned to IOMMU pages. Hence the need for this complicated bit of
8120db2e5d1SRobin Murphy  * impedance-matching, to be able to hand off a suitably-aligned list,
8130db2e5d1SRobin Murphy  * but still preserve the original offsets and sizes for the caller.
8140db2e5d1SRobin Murphy  */
81506d60728SChristoph Hellwig static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
81606d60728SChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs)
8170db2e5d1SRobin Murphy {
81843c5bf11SRobin Murphy 	struct iommu_domain *domain = iommu_get_dma_domain(dev);
819842fe519SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
820842fe519SRobin Murphy 	struct iova_domain *iovad = &cookie->iovad;
8210db2e5d1SRobin Murphy 	struct scatterlist *s, *prev = NULL;
82206d60728SChristoph Hellwig 	int prot = dma_info_to_prot(dir, dev_is_dma_coherent(dev), attrs);
823842fe519SRobin Murphy 	dma_addr_t iova;
8240db2e5d1SRobin Murphy 	size_t iova_len = 0;
825809eac54SRobin Murphy 	unsigned long mask = dma_get_seg_boundary(dev);
8260db2e5d1SRobin Murphy 	int i;
8270db2e5d1SRobin Murphy 
82806d60728SChristoph Hellwig 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
82906d60728SChristoph Hellwig 		iommu_dma_sync_sg_for_device(dev, sg, nents, dir);
83006d60728SChristoph Hellwig 
8310db2e5d1SRobin Murphy 	/*
8320db2e5d1SRobin Murphy 	 * Work out how much IOVA space we need, and align the segments to
8330db2e5d1SRobin Murphy 	 * IOVA granules for the IOMMU driver to handle. With some clever
8340db2e5d1SRobin Murphy 	 * trickery we can modify the list in-place, but reversibly, by
835809eac54SRobin Murphy 	 * stashing the unaligned parts in the as-yet-unused DMA fields.
8360db2e5d1SRobin Murphy 	 */
8370db2e5d1SRobin Murphy 	for_each_sg(sg, s, nents, i) {
838809eac54SRobin Murphy 		size_t s_iova_off = iova_offset(iovad, s->offset);
8390db2e5d1SRobin Murphy 		size_t s_length = s->length;
840809eac54SRobin Murphy 		size_t pad_len = (mask - iova_len + 1) & mask;
8410db2e5d1SRobin Murphy 
842809eac54SRobin Murphy 		sg_dma_address(s) = s_iova_off;
8430db2e5d1SRobin Murphy 		sg_dma_len(s) = s_length;
844809eac54SRobin Murphy 		s->offset -= s_iova_off;
845809eac54SRobin Murphy 		s_length = iova_align(iovad, s_length + s_iova_off);
8460db2e5d1SRobin Murphy 		s->length = s_length;
8470db2e5d1SRobin Murphy 
8480db2e5d1SRobin Murphy 		/*
849809eac54SRobin Murphy 		 * Due to the alignment of our single IOVA allocation, we can
850809eac54SRobin Murphy 		 * depend on these assumptions about the segment boundary mask:
851809eac54SRobin Murphy 		 * - If mask size >= IOVA size, then the IOVA range cannot
852809eac54SRobin Murphy 		 *   possibly fall across a boundary, so we don't care.
853809eac54SRobin Murphy 		 * - If mask size < IOVA size, then the IOVA range must start
854809eac54SRobin Murphy 		 *   exactly on a boundary, therefore we can lay things out
855809eac54SRobin Murphy 		 *   based purely on segment lengths without needing to know
856809eac54SRobin Murphy 		 *   the actual addresses beforehand.
857809eac54SRobin Murphy 		 * - The mask must be a power of 2, so pad_len == 0 if
858809eac54SRobin Murphy 		 *   iova_len == 0, thus we cannot dereference prev the first
859809eac54SRobin Murphy 		 *   time through here (i.e. before it has a meaningful value).
8600db2e5d1SRobin Murphy 		 */
861809eac54SRobin Murphy 		if (pad_len && pad_len < s_length - 1) {
8620db2e5d1SRobin Murphy 			prev->length += pad_len;
8630db2e5d1SRobin Murphy 			iova_len += pad_len;
8640db2e5d1SRobin Murphy 		}
8650db2e5d1SRobin Murphy 
8660db2e5d1SRobin Murphy 		iova_len += s_length;
8670db2e5d1SRobin Murphy 		prev = s;
8680db2e5d1SRobin Murphy 	}
8690db2e5d1SRobin Murphy 
870842fe519SRobin Murphy 	iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
8710db2e5d1SRobin Murphy 	if (!iova)
8720db2e5d1SRobin Murphy 		goto out_restore_sg;
8730db2e5d1SRobin Murphy 
8740db2e5d1SRobin Murphy 	/*
8750db2e5d1SRobin Murphy 	 * We'll leave any physical concatenation to the IOMMU driver's
8760db2e5d1SRobin Murphy 	 * implementation - it knows better than we do.
8770db2e5d1SRobin Murphy 	 */
878842fe519SRobin Murphy 	if (iommu_map_sg(domain, iova, sg, nents, prot) < iova_len)
8790db2e5d1SRobin Murphy 		goto out_free_iova;
8800db2e5d1SRobin Murphy 
881842fe519SRobin Murphy 	return __finalise_sg(dev, sg, nents, iova);
8820db2e5d1SRobin Murphy 
8830db2e5d1SRobin Murphy out_free_iova:
884842fe519SRobin Murphy 	iommu_dma_free_iova(cookie, iova, iova_len);
8850db2e5d1SRobin Murphy out_restore_sg:
8860db2e5d1SRobin Murphy 	__invalidate_sg(sg, nents);
8870db2e5d1SRobin Murphy 	return 0;
8880db2e5d1SRobin Murphy }
8890db2e5d1SRobin Murphy 
89006d60728SChristoph Hellwig static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg,
89106d60728SChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs)
8920db2e5d1SRobin Murphy {
893842fe519SRobin Murphy 	dma_addr_t start, end;
894842fe519SRobin Murphy 	struct scatterlist *tmp;
895842fe519SRobin Murphy 	int i;
89606d60728SChristoph Hellwig 
89706d60728SChristoph Hellwig 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) == 0)
89806d60728SChristoph Hellwig 		iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
89906d60728SChristoph Hellwig 
9000db2e5d1SRobin Murphy 	/*
9010db2e5d1SRobin Murphy 	 * The scatterlist segments are mapped into a single
9020db2e5d1SRobin Murphy 	 * contiguous IOVA allocation, so this is incredibly easy.
9030db2e5d1SRobin Murphy 	 */
904842fe519SRobin Murphy 	start = sg_dma_address(sg);
905842fe519SRobin Murphy 	for_each_sg(sg_next(sg), tmp, nents - 1, i) {
906842fe519SRobin Murphy 		if (sg_dma_len(tmp) == 0)
907842fe519SRobin Murphy 			break;
908842fe519SRobin Murphy 		sg = tmp;
909842fe519SRobin Murphy 	}
910842fe519SRobin Murphy 	end = sg_dma_address(sg) + sg_dma_len(sg);
911b61d271eSRobin Murphy 	__iommu_dma_unmap(dev, start, end - start);
9120db2e5d1SRobin Murphy }
9130db2e5d1SRobin Murphy 
91406d60728SChristoph Hellwig static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
91551f8cc9eSRobin Murphy 		size_t size, enum dma_data_direction dir, unsigned long attrs)
91651f8cc9eSRobin Murphy {
91751f8cc9eSRobin Murphy 	return __iommu_dma_map(dev, phys, size,
918b61d271eSRobin Murphy 			dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO);
91951f8cc9eSRobin Murphy }
92051f8cc9eSRobin Murphy 
92106d60728SChristoph Hellwig static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
92251f8cc9eSRobin Murphy 		size_t size, enum dma_data_direction dir, unsigned long attrs)
92351f8cc9eSRobin Murphy {
924b61d271eSRobin Murphy 	__iommu_dma_unmap(dev, handle, size);
92551f8cc9eSRobin Murphy }
92651f8cc9eSRobin Murphy 
9278553f6e6SRobin Murphy static void __iommu_dma_free(struct device *dev, size_t size, void *cpu_addr)
928bcf4b9c4SRobin Murphy {
929bcf4b9c4SRobin Murphy 	size_t alloc_size = PAGE_ALIGN(size);
930bcf4b9c4SRobin Murphy 	int count = alloc_size >> PAGE_SHIFT;
931bcf4b9c4SRobin Murphy 	struct page *page = NULL, **pages = NULL;
932bcf4b9c4SRobin Murphy 
933bcf4b9c4SRobin Murphy 	/* Non-coherent atomic allocation? Easy */
934e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
935e6475eb0SChristoph Hellwig 	    dma_free_from_pool(cpu_addr, alloc_size))
936bcf4b9c4SRobin Murphy 		return;
937bcf4b9c4SRobin Murphy 
938e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
939bcf4b9c4SRobin Murphy 		/*
940bcf4b9c4SRobin Murphy 		 * If it the address is remapped, then it's either non-coherent
941bcf4b9c4SRobin Murphy 		 * or highmem CMA, or an iommu_dma_alloc_remap() construction.
942bcf4b9c4SRobin Murphy 		 */
943bcf4b9c4SRobin Murphy 		pages = __iommu_dma_get_pages(cpu_addr);
944bcf4b9c4SRobin Murphy 		if (!pages)
945bcf4b9c4SRobin Murphy 			page = vmalloc_to_page(cpu_addr);
946bcf4b9c4SRobin Murphy 		dma_common_free_remap(cpu_addr, alloc_size, VM_USERMAP);
947bcf4b9c4SRobin Murphy 	} else {
948bcf4b9c4SRobin Murphy 		/* Lowmem means a coherent atomic or CMA allocation */
949bcf4b9c4SRobin Murphy 		page = virt_to_page(cpu_addr);
950bcf4b9c4SRobin Murphy 	}
951bcf4b9c4SRobin Murphy 
952bcf4b9c4SRobin Murphy 	if (pages)
953bcf4b9c4SRobin Murphy 		__iommu_dma_free_pages(pages, count);
954bcf4b9c4SRobin Murphy 	if (page && !dma_release_from_contiguous(dev, page, count))
955bcf4b9c4SRobin Murphy 		__free_pages(page, get_order(alloc_size));
956bcf4b9c4SRobin Murphy }
957bcf4b9c4SRobin Murphy 
9588553f6e6SRobin Murphy static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr,
9598553f6e6SRobin Murphy 		dma_addr_t handle, unsigned long attrs)
9608553f6e6SRobin Murphy {
9618553f6e6SRobin Murphy 	__iommu_dma_unmap(dev, handle, size);
9628553f6e6SRobin Murphy 	__iommu_dma_free(dev, size, cpu_addr);
9638553f6e6SRobin Murphy }
9648553f6e6SRobin Murphy 
965ee1ef05dSChristoph Hellwig static void *iommu_dma_alloc_pages(struct device *dev, size_t size,
966ee1ef05dSChristoph Hellwig 		struct page **pagep, gfp_t gfp, unsigned long attrs)
96706d60728SChristoph Hellwig {
96806d60728SChristoph Hellwig 	bool coherent = dev_is_dma_coherent(dev);
9699ad5d6edSRobin Murphy 	size_t alloc_size = PAGE_ALIGN(size);
9709a4ab94aSChristoph Hellwig 	struct page *page = NULL;
9719ad5d6edSRobin Murphy 	void *cpu_addr;
97206d60728SChristoph Hellwig 
9739a4ab94aSChristoph Hellwig 	if (gfpflags_allow_blocking(gfp))
9749ad5d6edSRobin Murphy 		page = dma_alloc_from_contiguous(dev, alloc_size >> PAGE_SHIFT,
9759ad5d6edSRobin Murphy 						 get_order(alloc_size),
9769a4ab94aSChristoph Hellwig 						 gfp & __GFP_NOWARN);
9779a4ab94aSChristoph Hellwig 	if (!page)
9789ad5d6edSRobin Murphy 		page = alloc_pages(gfp, get_order(alloc_size));
97906d60728SChristoph Hellwig 	if (!page)
98006d60728SChristoph Hellwig 		return NULL;
98106d60728SChristoph Hellwig 
982e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && (!coherent || PageHighMem(page))) {
9838680aa5aSRobin Murphy 		pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs);
9848680aa5aSRobin Murphy 
9859ad5d6edSRobin Murphy 		cpu_addr = dma_common_contiguous_remap(page, alloc_size,
9869ad5d6edSRobin Murphy 				VM_USERMAP, prot, __builtin_return_address(0));
9879ad5d6edSRobin Murphy 		if (!cpu_addr)
988ee1ef05dSChristoph Hellwig 			goto out_free_pages;
989072bebc0SRobin Murphy 
99006d60728SChristoph Hellwig 		if (!coherent)
9919ad5d6edSRobin Murphy 			arch_dma_prep_coherent(page, size);
9928680aa5aSRobin Murphy 	} else {
9939ad5d6edSRobin Murphy 		cpu_addr = page_address(page);
9948680aa5aSRobin Murphy 	}
995ee1ef05dSChristoph Hellwig 
996ee1ef05dSChristoph Hellwig 	*pagep = page;
9979ad5d6edSRobin Murphy 	memset(cpu_addr, 0, alloc_size);
9989ad5d6edSRobin Murphy 	return cpu_addr;
999072bebc0SRobin Murphy out_free_pages:
10009ad5d6edSRobin Murphy 	if (!dma_release_from_contiguous(dev, page, alloc_size >> PAGE_SHIFT))
10019ad5d6edSRobin Murphy 		__free_pages(page, get_order(alloc_size));
1002072bebc0SRobin Murphy 	return NULL;
100306d60728SChristoph Hellwig }
100406d60728SChristoph Hellwig 
1005ee1ef05dSChristoph Hellwig static void *iommu_dma_alloc(struct device *dev, size_t size,
1006ee1ef05dSChristoph Hellwig 		dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
1007ee1ef05dSChristoph Hellwig {
1008ee1ef05dSChristoph Hellwig 	bool coherent = dev_is_dma_coherent(dev);
1009ee1ef05dSChristoph Hellwig 	int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
1010ee1ef05dSChristoph Hellwig 	struct page *page = NULL;
1011ee1ef05dSChristoph Hellwig 	void *cpu_addr;
1012ee1ef05dSChristoph Hellwig 
1013ee1ef05dSChristoph Hellwig 	gfp |= __GFP_ZERO;
1014ee1ef05dSChristoph Hellwig 
1015e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && gfpflags_allow_blocking(gfp) &&
1016ee1ef05dSChristoph Hellwig 	    !(attrs & DMA_ATTR_FORCE_CONTIGUOUS))
1017ee1ef05dSChristoph Hellwig 		return iommu_dma_alloc_remap(dev, size, handle, gfp, attrs);
1018ee1ef05dSChristoph Hellwig 
1019e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) &&
1020e6475eb0SChristoph Hellwig 	    !gfpflags_allow_blocking(gfp) && !coherent)
1021ee1ef05dSChristoph Hellwig 		cpu_addr = dma_alloc_from_pool(PAGE_ALIGN(size), &page, gfp);
1022ee1ef05dSChristoph Hellwig 	else
1023ee1ef05dSChristoph Hellwig 		cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs);
1024ee1ef05dSChristoph Hellwig 	if (!cpu_addr)
1025ee1ef05dSChristoph Hellwig 		return NULL;
1026ee1ef05dSChristoph Hellwig 
1027ee1ef05dSChristoph Hellwig 	*handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot);
1028ee1ef05dSChristoph Hellwig 	if (*handle == DMA_MAPPING_ERROR) {
1029ee1ef05dSChristoph Hellwig 		__iommu_dma_free(dev, size, cpu_addr);
1030ee1ef05dSChristoph Hellwig 		return NULL;
1031ee1ef05dSChristoph Hellwig 	}
1032ee1ef05dSChristoph Hellwig 
1033ee1ef05dSChristoph Hellwig 	return cpu_addr;
1034ee1ef05dSChristoph Hellwig }
1035ee1ef05dSChristoph Hellwig 
103606d60728SChristoph Hellwig static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma,
103706d60728SChristoph Hellwig 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
103806d60728SChristoph Hellwig 		unsigned long attrs)
103906d60728SChristoph Hellwig {
104006d60728SChristoph Hellwig 	unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1041efd9f10bSChristoph Hellwig 	unsigned long pfn, off = vma->vm_pgoff;
104206d60728SChristoph Hellwig 	int ret;
104306d60728SChristoph Hellwig 
104406d60728SChristoph Hellwig 	vma->vm_page_prot = arch_dma_mmap_pgprot(dev, vma->vm_page_prot, attrs);
104506d60728SChristoph Hellwig 
104606d60728SChristoph Hellwig 	if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
104706d60728SChristoph Hellwig 		return ret;
104806d60728SChristoph Hellwig 
104906d60728SChristoph Hellwig 	if (off >= nr_pages || vma_pages(vma) > nr_pages - off)
105006d60728SChristoph Hellwig 		return -ENXIO;
105106d60728SChristoph Hellwig 
1052e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
1053efd9f10bSChristoph Hellwig 		struct page **pages = __iommu_dma_get_pages(cpu_addr);
105406d60728SChristoph Hellwig 
1055efd9f10bSChristoph Hellwig 		if (pages)
10564c360aceSRobin Murphy 			return __iommu_dma_mmap(pages, size, vma);
1057efd9f10bSChristoph Hellwig 		pfn = vmalloc_to_pfn(cpu_addr);
1058efd9f10bSChristoph Hellwig 	} else {
1059efd9f10bSChristoph Hellwig 		pfn = page_to_pfn(virt_to_page(cpu_addr));
1060efd9f10bSChristoph Hellwig 	}
1061efd9f10bSChristoph Hellwig 
1062efd9f10bSChristoph Hellwig 	return remap_pfn_range(vma, vma->vm_start, pfn + off,
1063efd9f10bSChristoph Hellwig 			       vma->vm_end - vma->vm_start,
1064efd9f10bSChristoph Hellwig 			       vma->vm_page_prot);
106506d60728SChristoph Hellwig }
106606d60728SChristoph Hellwig 
106706d60728SChristoph Hellwig static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt,
106806d60728SChristoph Hellwig 		void *cpu_addr, dma_addr_t dma_addr, size_t size,
106906d60728SChristoph Hellwig 		unsigned long attrs)
107006d60728SChristoph Hellwig {
10713fb3378bSChristoph Hellwig 	struct page *page;
10723fb3378bSChristoph Hellwig 	int ret;
107306d60728SChristoph Hellwig 
1074e6475eb0SChristoph Hellwig 	if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
10753fb3378bSChristoph Hellwig 		struct page **pages = __iommu_dma_get_pages(cpu_addr);
10763fb3378bSChristoph Hellwig 
10773fb3378bSChristoph Hellwig 		if (pages) {
10783fb3378bSChristoph Hellwig 			return sg_alloc_table_from_pages(sgt, pages,
10793fb3378bSChristoph Hellwig 					PAGE_ALIGN(size) >> PAGE_SHIFT,
10803fb3378bSChristoph Hellwig 					0, size, GFP_KERNEL);
108106d60728SChristoph Hellwig 		}
108206d60728SChristoph Hellwig 
10833fb3378bSChristoph Hellwig 		page = vmalloc_to_page(cpu_addr);
10843fb3378bSChristoph Hellwig 	} else {
10853fb3378bSChristoph Hellwig 		page = virt_to_page(cpu_addr);
108606d60728SChristoph Hellwig 	}
108706d60728SChristoph Hellwig 
10883fb3378bSChristoph Hellwig 	ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
10893fb3378bSChristoph Hellwig 	if (!ret)
10903fb3378bSChristoph Hellwig 		sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
10913fb3378bSChristoph Hellwig 	return ret;
109206d60728SChristoph Hellwig }
109306d60728SChristoph Hellwig 
109406d60728SChristoph Hellwig static const struct dma_map_ops iommu_dma_ops = {
109506d60728SChristoph Hellwig 	.alloc			= iommu_dma_alloc,
109606d60728SChristoph Hellwig 	.free			= iommu_dma_free,
109706d60728SChristoph Hellwig 	.mmap			= iommu_dma_mmap,
109806d60728SChristoph Hellwig 	.get_sgtable		= iommu_dma_get_sgtable,
109906d60728SChristoph Hellwig 	.map_page		= iommu_dma_map_page,
110006d60728SChristoph Hellwig 	.unmap_page		= iommu_dma_unmap_page,
110106d60728SChristoph Hellwig 	.map_sg			= iommu_dma_map_sg,
110206d60728SChristoph Hellwig 	.unmap_sg		= iommu_dma_unmap_sg,
110306d60728SChristoph Hellwig 	.sync_single_for_cpu	= iommu_dma_sync_single_for_cpu,
110406d60728SChristoph Hellwig 	.sync_single_for_device	= iommu_dma_sync_single_for_device,
110506d60728SChristoph Hellwig 	.sync_sg_for_cpu	= iommu_dma_sync_sg_for_cpu,
110606d60728SChristoph Hellwig 	.sync_sg_for_device	= iommu_dma_sync_sg_for_device,
110706d60728SChristoph Hellwig 	.map_resource		= iommu_dma_map_resource,
110806d60728SChristoph Hellwig 	.unmap_resource		= iommu_dma_unmap_resource,
110906d60728SChristoph Hellwig };
111006d60728SChristoph Hellwig 
111106d60728SChristoph Hellwig /*
111206d60728SChristoph Hellwig  * The IOMMU core code allocates the default DMA domain, which the underlying
111306d60728SChristoph Hellwig  * IOMMU driver needs to support via the dma-iommu layer.
111406d60728SChristoph Hellwig  */
111506d60728SChristoph Hellwig void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size)
111606d60728SChristoph Hellwig {
111706d60728SChristoph Hellwig 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
111806d60728SChristoph Hellwig 
111906d60728SChristoph Hellwig 	if (!domain)
112006d60728SChristoph Hellwig 		goto out_err;
112106d60728SChristoph Hellwig 
112206d60728SChristoph Hellwig 	/*
112306d60728SChristoph Hellwig 	 * The IOMMU core code allocates the default DMA domain, which the
112406d60728SChristoph Hellwig 	 * underlying IOMMU driver needs to support via the dma-iommu layer.
112506d60728SChristoph Hellwig 	 */
112606d60728SChristoph Hellwig 	if (domain->type == IOMMU_DOMAIN_DMA) {
112706d60728SChristoph Hellwig 		if (iommu_dma_init_domain(domain, dma_base, size, dev))
112806d60728SChristoph Hellwig 			goto out_err;
112906d60728SChristoph Hellwig 		dev->dma_ops = &iommu_dma_ops;
113006d60728SChristoph Hellwig 	}
113106d60728SChristoph Hellwig 
113206d60728SChristoph Hellwig 	return;
113306d60728SChristoph Hellwig out_err:
113406d60728SChristoph Hellwig 	 pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n",
113506d60728SChristoph Hellwig 		 dev_name(dev));
113606d60728SChristoph Hellwig }
113706d60728SChristoph Hellwig 
113844bb7e24SRobin Murphy static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
113944bb7e24SRobin Murphy 		phys_addr_t msi_addr, struct iommu_domain *domain)
114044bb7e24SRobin Murphy {
114144bb7e24SRobin Murphy 	struct iommu_dma_cookie *cookie = domain->iova_cookie;
114244bb7e24SRobin Murphy 	struct iommu_dma_msi_page *msi_page;
1143842fe519SRobin Murphy 	dma_addr_t iova;
114444bb7e24SRobin Murphy 	int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1145fdbe574eSRobin Murphy 	size_t size = cookie_msi_granule(cookie);
114644bb7e24SRobin Murphy 
1147fdbe574eSRobin Murphy 	msi_addr &= ~(phys_addr_t)(size - 1);
114844bb7e24SRobin Murphy 	list_for_each_entry(msi_page, &cookie->msi_page_list, list)
114944bb7e24SRobin Murphy 		if (msi_page->phys == msi_addr)
115044bb7e24SRobin Murphy 			return msi_page;
115144bb7e24SRobin Murphy 
115244bb7e24SRobin Murphy 	msi_page = kzalloc(sizeof(*msi_page), GFP_ATOMIC);
115344bb7e24SRobin Murphy 	if (!msi_page)
115444bb7e24SRobin Murphy 		return NULL;
115544bb7e24SRobin Murphy 
1156b61d271eSRobin Murphy 	iova = __iommu_dma_map(dev, msi_addr, size, prot);
1157cad34be7SChristoph Hellwig 	if (iova == DMA_MAPPING_ERROR)
115844bb7e24SRobin Murphy 		goto out_free_page;
115944bb7e24SRobin Murphy 
116044bb7e24SRobin Murphy 	INIT_LIST_HEAD(&msi_page->list);
1161a44e6657SRobin Murphy 	msi_page->phys = msi_addr;
1162a44e6657SRobin Murphy 	msi_page->iova = iova;
116344bb7e24SRobin Murphy 	list_add(&msi_page->list, &cookie->msi_page_list);
116444bb7e24SRobin Murphy 	return msi_page;
116544bb7e24SRobin Murphy 
116644bb7e24SRobin Murphy out_free_page:
116744bb7e24SRobin Murphy 	kfree(msi_page);
116844bb7e24SRobin Murphy 	return NULL;
116944bb7e24SRobin Murphy }
117044bb7e24SRobin Murphy 
1171ece6e6f0SJulien Grall int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
117244bb7e24SRobin Murphy {
1173ece6e6f0SJulien Grall 	struct device *dev = msi_desc_to_dev(desc);
117444bb7e24SRobin Murphy 	struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
117544bb7e24SRobin Murphy 	struct iommu_dma_cookie *cookie;
117644bb7e24SRobin Murphy 	struct iommu_dma_msi_page *msi_page;
117744bb7e24SRobin Murphy 	unsigned long flags;
117844bb7e24SRobin Murphy 
1179ece6e6f0SJulien Grall 	if (!domain || !domain->iova_cookie) {
1180ece6e6f0SJulien Grall 		desc->iommu_cookie = NULL;
1181ece6e6f0SJulien Grall 		return 0;
1182ece6e6f0SJulien Grall 	}
118344bb7e24SRobin Murphy 
118444bb7e24SRobin Murphy 	cookie = domain->iova_cookie;
118544bb7e24SRobin Murphy 
118644bb7e24SRobin Murphy 	/*
118744bb7e24SRobin Murphy 	 * We disable IRQs to rule out a possible inversion against
118844bb7e24SRobin Murphy 	 * irq_desc_lock if, say, someone tries to retarget the affinity
118944bb7e24SRobin Murphy 	 * of an MSI from within an IPI handler.
119044bb7e24SRobin Murphy 	 */
119144bb7e24SRobin Murphy 	spin_lock_irqsave(&cookie->msi_lock, flags);
119244bb7e24SRobin Murphy 	msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
119344bb7e24SRobin Murphy 	spin_unlock_irqrestore(&cookie->msi_lock, flags);
119444bb7e24SRobin Murphy 
1195ece6e6f0SJulien Grall 	msi_desc_set_iommu_cookie(desc, msi_page);
1196ece6e6f0SJulien Grall 
1197ece6e6f0SJulien Grall 	if (!msi_page)
1198ece6e6f0SJulien Grall 		return -ENOMEM;
1199ece6e6f0SJulien Grall 	return 0;
120044bb7e24SRobin Murphy }
1201ece6e6f0SJulien Grall 
1202ece6e6f0SJulien Grall void iommu_dma_compose_msi_msg(struct msi_desc *desc,
1203ece6e6f0SJulien Grall 			       struct msi_msg *msg)
1204ece6e6f0SJulien Grall {
1205ece6e6f0SJulien Grall 	struct device *dev = msi_desc_to_dev(desc);
1206ece6e6f0SJulien Grall 	const struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1207ece6e6f0SJulien Grall 	const struct iommu_dma_msi_page *msi_page;
1208ece6e6f0SJulien Grall 
1209ece6e6f0SJulien Grall 	msi_page = msi_desc_get_iommu_cookie(desc);
1210ece6e6f0SJulien Grall 
1211ece6e6f0SJulien Grall 	if (!domain || !domain->iova_cookie || WARN_ON(!msi_page))
1212ece6e6f0SJulien Grall 		return;
1213ece6e6f0SJulien Grall 
1214ece6e6f0SJulien Grall 	msg->address_hi = upper_32_bits(msi_page->iova);
1215ece6e6f0SJulien Grall 	msg->address_lo &= cookie_msi_granule(domain->iova_cookie) - 1;
1216ece6e6f0SJulien Grall 	msg->address_lo += lower_32_bits(msi_page->iova);
121744bb7e24SRobin Murphy }
121806d60728SChristoph Hellwig 
121906d60728SChristoph Hellwig static int iommu_dma_init(void)
122006d60728SChristoph Hellwig {
122106d60728SChristoph Hellwig 	return iova_cache_get();
122206d60728SChristoph Hellwig }
122306d60728SChristoph Hellwig arch_initcall(iommu_dma_init);
1224