blob: 579e983009240afa36e2ad1d71aaa986515cc11a [file] [log] [blame]
Robin Murphy0db2e5d2015-10-01 20:13:58 +01001/*
2 * A fairly generic DMA-API to IOMMU-API glue layer.
3 *
4 * Copyright (C) 2014-2015 ARM Ltd.
5 *
6 * based in part on arch/arm/mm/dma-mapping.c:
7 * Copyright (C) 2000-2004 Russell King
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <linux/device.h>
23#include <linux/dma-iommu.h>
Robin Murphy5b11e9c2015-12-18 17:01:46 +000024#include <linux/gfp.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010025#include <linux/huge_mm.h>
26#include <linux/iommu.h>
27#include <linux/iova.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010028#include <linux/irq.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010029#include <linux/mm.h>
Robin Murphyfade1ec2016-09-12 17:14:00 +010030#include <linux/pci.h>
Robin Murphy5b11e9c2015-12-18 17:01:46 +000031#include <linux/scatterlist.h>
32#include <linux/vmalloc.h>
Patrick Daly23301482017-10-12 16:18:25 -070033#include <linux/arm-smmu-errata.h>
34#include <soc/qcom/secure_buffer.h>
Robin Murphy0db2e5d2015-10-01 20:13:58 +010035
Robin Murphy44bb7e22016-09-12 17:13:59 +010036struct iommu_dma_msi_page {
37 struct list_head list;
38 dma_addr_t iova;
39 phys_addr_t phys;
40};
41
42struct iommu_dma_cookie {
43 struct iova_domain iovad;
44 struct list_head msi_page_list;
45 spinlock_t msi_lock;
Patrick Daly23301482017-10-12 16:18:25 -070046 u32 min_iova_align;
47 struct page *guard_page;
Prakash Guptac2e909a2018-03-29 11:23:06 +053048 u32 force_guard_page_len;
Robin Murphy44bb7e22016-09-12 17:13:59 +010049};
50
51static inline struct iova_domain *cookie_iovad(struct iommu_domain *domain)
52{
53 return &((struct iommu_dma_cookie *)domain->iova_cookie)->iovad;
54}
55
Robin Murphy0db2e5d2015-10-01 20:13:58 +010056int iommu_dma_init(void)
57{
58 return iova_cache_get();
59}
60
61/**
62 * iommu_get_dma_cookie - Acquire DMA-API resources for a domain
63 * @domain: IOMMU domain to prepare for DMA-API usage
64 *
65 * IOMMU drivers should normally call this from their domain_alloc
66 * callback when domain->type == IOMMU_DOMAIN_DMA.
67 */
68int iommu_get_dma_cookie(struct iommu_domain *domain)
69{
Robin Murphy44bb7e22016-09-12 17:13:59 +010070 struct iommu_dma_cookie *cookie;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010071
72 if (domain->iova_cookie)
73 return -EEXIST;
74
Robin Murphy44bb7e22016-09-12 17:13:59 +010075 cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
76 if (!cookie)
77 return -ENOMEM;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010078
Robin Murphy44bb7e22016-09-12 17:13:59 +010079 spin_lock_init(&cookie->msi_lock);
80 INIT_LIST_HEAD(&cookie->msi_page_list);
81 domain->iova_cookie = cookie;
82 return 0;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010083}
84EXPORT_SYMBOL(iommu_get_dma_cookie);
85
86/**
87 * iommu_put_dma_cookie - Release a domain's DMA mapping resources
88 * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
89 *
90 * IOMMU drivers should normally call this from their domain_free callback.
91 */
92void iommu_put_dma_cookie(struct iommu_domain *domain)
93{
Robin Murphy44bb7e22016-09-12 17:13:59 +010094 struct iommu_dma_cookie *cookie = domain->iova_cookie;
95 struct iommu_dma_msi_page *msi, *tmp;
Robin Murphy0db2e5d2015-10-01 20:13:58 +010096
Robin Murphy44bb7e22016-09-12 17:13:59 +010097 if (!cookie)
Robin Murphy0db2e5d2015-10-01 20:13:58 +010098 return;
99
Robin Murphy44bb7e22016-09-12 17:13:59 +0100100 if (cookie->iovad.granule)
101 put_iova_domain(&cookie->iovad);
102
103 list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list) {
104 list_del(&msi->list);
105 kfree(msi);
106 }
107 kfree(cookie);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100108 domain->iova_cookie = NULL;
109}
110EXPORT_SYMBOL(iommu_put_dma_cookie);
111
Robin Murphyfade1ec2016-09-12 17:14:00 +0100112static void iova_reserve_pci_windows(struct pci_dev *dev,
113 struct iova_domain *iovad)
114{
115 struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
116 struct resource_entry *window;
117 unsigned long lo, hi;
118
119 resource_list_for_each_entry(window, &bridge->windows) {
Robin Murphyf0c31c62017-03-16 17:00:17 +0000120 if (resource_type(window->res) != IORESOURCE_MEM)
Robin Murphyfade1ec2016-09-12 17:14:00 +0100121 continue;
122
123 lo = iova_pfn(iovad, window->res->start - window->offset);
124 hi = iova_pfn(iovad, window->res->end - window->offset);
125 reserve_iova(iovad, lo, hi);
126 }
127}
128
Patrick Daly23301482017-10-12 16:18:25 -0700129static int iommu_dma_arm_smmu_errata_init(struct iommu_domain *domain)
130{
131 struct iommu_dma_cookie *cookie = domain->iova_cookie;
132 int vmid = VMID_HLOS;
133 int min_iova_align = 0;
Prakash Guptac2e909a2018-03-29 11:23:06 +0530134 int force_iova_guard_page = 0;
135
Patrick Daly23301482017-10-12 16:18:25 -0700136
137 iommu_domain_get_attr(domain,
Patrick Daly83174c12017-10-26 12:31:15 -0700138 DOMAIN_ATTR_MMU500_ERRATA_MIN_ALIGN,
Patrick Daly23301482017-10-12 16:18:25 -0700139 &min_iova_align);
140 iommu_domain_get_attr(domain, DOMAIN_ATTR_SECURE_VMID, &vmid);
Prakash Guptac2e909a2018-03-29 11:23:06 +0530141 iommu_domain_get_attr(domain,
142 DOMAIN_ATTR_FORCE_IOVA_GUARD_PAGE,
143 &force_iova_guard_page);
144
Patrick Daly23301482017-10-12 16:18:25 -0700145 if (vmid >= VMID_LAST || vmid < 0)
146 vmid = VMID_HLOS;
147
Prakash Guptac2e909a2018-03-29 11:23:06 +0530148 cookie->min_iova_align = (min_iova_align) ? ARM_SMMU_MIN_IOVA_ALIGN :
149 PAGE_SIZE;
150
151 if (force_iova_guard_page)
152 cookie->force_guard_page_len = PAGE_SIZE;
153
154 cookie->guard_page =
155 arm_smmu_errata_get_guard_page(vmid);
156 if (!cookie->guard_page)
157 return -ENOMEM;
158
Patrick Daly23301482017-10-12 16:18:25 -0700159 return 0;
160}
161
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100162/**
163 * iommu_dma_init_domain - Initialise a DMA mapping domain
164 * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
165 * @base: IOVA at which the mappable address space starts
166 * @size: Size of IOVA space
Robin Murphyfade1ec2016-09-12 17:14:00 +0100167 * @dev: Device the domain is being initialised for
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100168 *
169 * @base and @size should be exact multiples of IOMMU page granularity to
170 * avoid rounding surprises. If necessary, we reserve the page at address 0
171 * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
172 * any change which could make prior IOVAs invalid will fail.
173 */
Robin Murphyfade1ec2016-09-12 17:14:00 +0100174int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,
175 u64 size, struct device *dev)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100176{
Robin Murphy44bb7e22016-09-12 17:13:59 +0100177 struct iova_domain *iovad = cookie_iovad(domain);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100178 unsigned long order, base_pfn, end_pfn;
179
180 if (!iovad)
181 return -ENODEV;
182
Patrick Daly23301482017-10-12 16:18:25 -0700183 if (iommu_dma_arm_smmu_errata_init(domain))
184 return -ENODEV;
185
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100186 /* Use the smallest supported page size for IOVA granularity */
Robin Murphyd16e0fa2016-04-07 18:42:06 +0100187 order = __ffs(domain->pgsize_bitmap);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100188 base_pfn = max_t(unsigned long, 1, base >> order);
189 end_pfn = (base + size - 1) >> order;
190
191 /* Check the domain allows at least some access to the device... */
192 if (domain->geometry.force_aperture) {
193 if (base > domain->geometry.aperture_end ||
194 base + size <= domain->geometry.aperture_start) {
195 pr_warn("specified DMA range outside IOMMU capability\n");
196 return -EFAULT;
197 }
198 /* ...then finally give it a kicking to make sure it fits */
199 base_pfn = max_t(unsigned long, base_pfn,
200 domain->geometry.aperture_start >> order);
201 end_pfn = min_t(unsigned long, end_pfn,
202 domain->geometry.aperture_end >> order);
203 }
204
205 /* All we can safely do with an existing domain is enlarge it */
206 if (iovad->start_pfn) {
207 if (1UL << order != iovad->granule ||
208 base_pfn != iovad->start_pfn ||
209 end_pfn < iovad->dma_32bit_pfn) {
210 pr_warn("Incompatible range for DMA domain\n");
211 return -EFAULT;
212 }
213 iovad->dma_32bit_pfn = end_pfn;
214 } else {
215 init_iova_domain(iovad, 1UL << order, base_pfn, end_pfn);
Robin Murphyfade1ec2016-09-12 17:14:00 +0100216 if (dev && dev_is_pci(dev))
217 iova_reserve_pci_windows(to_pci_dev(dev), iovad);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100218 }
219 return 0;
220}
221EXPORT_SYMBOL(iommu_dma_init_domain);
222
223/**
224 * dma_direction_to_prot - Translate DMA API directions to IOMMU API page flags
225 * @dir: Direction of DMA transfer
226 * @coherent: Is the DMA master cache-coherent?
227 *
228 * Return: corresponding IOMMU API page protection flags
229 */
230int dma_direction_to_prot(enum dma_data_direction dir, bool coherent)
231{
232 int prot = coherent ? IOMMU_CACHE : 0;
233
234 switch (dir) {
235 case DMA_BIDIRECTIONAL:
236 return prot | IOMMU_READ | IOMMU_WRITE;
237 case DMA_TO_DEVICE:
238 return prot | IOMMU_READ;
239 case DMA_FROM_DEVICE:
240 return prot | IOMMU_WRITE;
241 default:
242 return 0;
243 }
244}
245
Robin Murphy0e080022017-03-31 15:46:05 +0100246static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
247 size_t size, dma_addr_t dma_limit, struct device *dev)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100248{
Robin Murphy44bb7e22016-09-12 17:13:59 +0100249 struct iova_domain *iovad = cookie_iovad(domain);
Patrick Daly23301482017-10-12 16:18:25 -0700250 struct iommu_dma_cookie *cookie = domain->iova_cookie;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100251 unsigned long shift = iova_shift(iovad);
Patrick Daly23301482017-10-12 16:18:25 -0700252 unsigned long iova_len;
Robin Murphye53859e2017-03-31 15:46:07 +0100253 unsigned long iova = 0;
Patrick Daly67e617b2017-07-20 21:07:08 -0700254 dma_addr_t limit;
Patrick Daly23301482017-10-12 16:18:25 -0700255 unsigned long guard_len;
256 dma_addr_t ret_iova;
Robin Murphye53859e2017-03-31 15:46:07 +0100257
Patrick Daly23301482017-10-12 16:18:25 -0700258 if (cookie->min_iova_align)
Prakash Guptac2e909a2018-03-29 11:23:06 +0530259 guard_len = ALIGN(size + cookie->force_guard_page_len,
260 cookie->min_iova_align) - size;
Patrick Daly23301482017-10-12 16:18:25 -0700261 else
262 guard_len = 0;
263 iova_len = (size + guard_len) >> shift;
Robin Murphye53859e2017-03-31 15:46:07 +0100264 /*
265 * Freeing non-power-of-two-sized allocations back into the IOVA caches
266 * will come back to bite us badly, so we have to waste a bit of space
267 * rounding up anything cacheable to make sure that can't happen. The
268 * order of the unadjusted size will still match upon freeing.
269 */
270 if (iova_len < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1)))
271 iova_len = roundup_pow_of_two(iova_len);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100272
Robin Murphyc987ff02016-08-09 17:31:35 +0100273 if (domain->geometry.force_aperture)
274 dma_limit = min(dma_limit, domain->geometry.aperture_end);
Robin Murphy0e080022017-03-31 15:46:05 +0100275
Patrick Daly67e617b2017-07-20 21:07:08 -0700276 /*
277 * Ensure iova is within range specified in iommu_dma_init_domain().
278 * This also prevents unnecessary work iterating through the entire
279 * rb_tree.
280 */
281 limit = min_t(dma_addr_t, dma_limit >> shift, iovad->dma_32bit_pfn);
282 iova = alloc_iova_fast(iovad, iova_len, limit);
Robin Murphye53859e2017-03-31 15:46:07 +0100283
Patrick Daly23301482017-10-12 16:18:25 -0700284 ret_iova = (dma_addr_t)iova << shift;
285
286 if (guard_len &&
287 iommu_map(domain, ret_iova + size,
288 page_to_phys(cookie->guard_page),
289 guard_len, ARM_SMMU_GUARD_PROT)) {
290
291 free_iova_fast(iovad, iova, iova_len);
292 return 0;
293 }
294
295 return ret_iova;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100296}
297
Patrick Daly23301482017-10-12 16:18:25 -0700298static void iommu_dma_free_iova(struct iommu_domain *domain,
299 struct iommu_dma_cookie *cookie,
Robin Murphy0e080022017-03-31 15:46:05 +0100300 dma_addr_t iova, size_t size)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100301{
Robin Murphy0e080022017-03-31 15:46:05 +0100302 struct iova_domain *iovad = &cookie->iovad;
Robin Murphye53859e2017-03-31 15:46:07 +0100303 unsigned long shift = iova_shift(iovad);
Patrick Daly23301482017-10-12 16:18:25 -0700304 unsigned long guard_len;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100305
Prakash Guptac2e909a2018-03-29 11:23:06 +0530306 if (cookie->min_iova_align)
307 guard_len = ALIGN(size + cookie->force_guard_page_len,
308 cookie->min_iova_align) - size;
309 else
Patrick Daly23301482017-10-12 16:18:25 -0700310 guard_len = 0;
Prakash Guptac2e909a2018-03-29 11:23:06 +0530311
312 if (guard_len)
313 iommu_unmap(domain, iova + size, guard_len);
Patrick Daly23301482017-10-12 16:18:25 -0700314
315 free_iova_fast(iovad, iova >> shift, (size + guard_len) >> shift);
Robin Murphy0e080022017-03-31 15:46:05 +0100316}
317
318static void __iommu_dma_unmap(struct iommu_domain *domain, dma_addr_t dma_addr,
319 size_t size)
320{
321 struct iova_domain *iovad = cookie_iovad(domain);
322 size_t iova_off = iova_offset(iovad, dma_addr);
323
324 dma_addr -= iova_off;
325 size = iova_align(iovad, size + iova_off);
326
327 WARN_ON(iommu_unmap(domain, dma_addr, size) != size);
Patrick Daly23301482017-10-12 16:18:25 -0700328 iommu_dma_free_iova(domain, domain->iova_cookie, dma_addr, size);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100329}
330
331static void __iommu_dma_free_pages(struct page **pages, int count)
332{
333 while (count--)
334 __free_page(pages[count]);
335 kvfree(pages);
336}
337
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100338static struct page **__iommu_dma_alloc_pages(unsigned int count,
339 unsigned long order_mask, gfp_t gfp)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100340{
341 struct page **pages;
342 unsigned int i = 0, array_size = count * sizeof(*pages);
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100343
344 order_mask &= (2U << MAX_ORDER) - 1;
345 if (!order_mask)
346 return NULL;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100347
348 if (array_size <= PAGE_SIZE)
349 pages = kzalloc(array_size, GFP_KERNEL);
350 else
351 pages = vzalloc(array_size);
352 if (!pages)
353 return NULL;
354
355 /* IOMMU can map any pages, so himem can also be used here */
356 gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
357
358 while (count) {
359 struct page *page = NULL;
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100360 unsigned int order_size;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100361
362 /*
363 * Higher-order allocations are a convenience rather
364 * than a necessity, hence using __GFP_NORETRY until
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100365 * falling back to minimum-order allocations.
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100366 */
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100367 for (order_mask &= (2U << __fls(count)) - 1;
368 order_mask; order_mask &= ~order_size) {
369 unsigned int order = __fls(order_mask);
370
371 order_size = 1U << order;
Vinayak Menon9d373842019-01-02 20:44:21 +0530372 page = alloc_pages(order ?
373 (gfp | __GFP_NORETRY) &
374 ~__GFP_RECLAIM : gfp, order);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100375 if (!page)
376 continue;
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100377 if (!order)
378 break;
379 if (!PageCompound(page)) {
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100380 split_page(page, order);
381 break;
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100382 } else if (!split_huge_page(page)) {
383 break;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100384 }
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100385 __free_pages(page, order);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100386 }
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100387 if (!page) {
388 __iommu_dma_free_pages(pages, i);
389 return NULL;
390 }
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100391 count -= order_size;
392 while (order_size--)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100393 pages[i++] = page++;
394 }
395 return pages;
396}
397
398/**
399 * iommu_dma_free - Free a buffer allocated by iommu_dma_alloc()
400 * @dev: Device which owns this buffer
401 * @pages: Array of buffer pages as returned by iommu_dma_alloc()
402 * @size: Size of buffer in bytes
403 * @handle: DMA address of buffer
404 *
405 * Frees both the pages associated with the buffer, and the array
406 * describing them
407 */
408void iommu_dma_free(struct device *dev, struct page **pages, size_t size,
409 dma_addr_t *handle)
410{
Robin Murphy0e080022017-03-31 15:46:05 +0100411 __iommu_dma_unmap(iommu_get_domain_for_dev(dev), *handle, size);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100412 __iommu_dma_free_pages(pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
413 *handle = DMA_ERROR_CODE;
414}
415
416/**
417 * iommu_dma_alloc - Allocate and map a buffer contiguous in IOVA space
418 * @dev: Device to allocate memory for. Must be a real device
419 * attached to an iommu_dma_domain
420 * @size: Size of buffer in bytes
421 * @gfp: Allocation flags
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100422 * @attrs: DMA attributes for this allocation
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100423 * @prot: IOMMU mapping flags
424 * @handle: Out argument for allocated DMA handle
425 * @flush_page: Arch callback which must ensure PAGE_SIZE bytes from the
426 * given VA/PA are visible to the given non-coherent device.
427 *
428 * If @size is less than PAGE_SIZE, then a full CPU page will be allocated,
429 * but an IOMMU which supports smaller pages might not map the whole thing.
430 *
431 * Return: Array of struct page pointers describing the buffer,
432 * or NULL on failure.
433 */
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100434struct page **iommu_dma_alloc(struct device *dev, size_t size, gfp_t gfp,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700435 unsigned long attrs, int prot, dma_addr_t *handle,
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100436 void (*flush_page)(struct device *, const void *, phys_addr_t))
437{
438 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
Robin Murphy0e080022017-03-31 15:46:05 +0100439 struct iommu_dma_cookie *cookie = domain->iova_cookie;
440 struct iova_domain *iovad = &cookie->iovad;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100441 struct page **pages;
442 struct sg_table sgt;
Robin Murphy0e080022017-03-31 15:46:05 +0100443 dma_addr_t iova;
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100444 unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100445
446 *handle = DMA_ERROR_CODE;
447
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100448 min_size = alloc_sizes & -alloc_sizes;
449 if (min_size < PAGE_SIZE) {
450 min_size = PAGE_SIZE;
451 alloc_sizes |= PAGE_SIZE;
452 } else {
453 size = ALIGN(size, min_size);
454 }
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700455 if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES)
Robin Murphy3b6b7e12016-04-13 17:29:10 +0100456 alloc_sizes = min_size;
457
458 count = PAGE_ALIGN(size) >> PAGE_SHIFT;
459 pages = __iommu_dma_alloc_pages(count, alloc_sizes >> PAGE_SHIFT, gfp);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100460 if (!pages)
461 return NULL;
462
Robin Murphy0e080022017-03-31 15:46:05 +0100463 size = iova_align(iovad, size);
464 iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100465 if (!iova)
466 goto out_free_pages;
467
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100468 if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL))
469 goto out_free_iova;
470
471 if (!(prot & IOMMU_CACHE)) {
472 struct sg_mapping_iter miter;
473 /*
474 * The CPU-centric flushing implied by SG_MITER_TO_SG isn't
475 * sufficient here, so skip it by using the "wrong" direction.
476 */
477 sg_miter_start(&miter, sgt.sgl, sgt.orig_nents, SG_MITER_FROM_SG);
478 while (sg_miter_next(&miter))
479 flush_page(dev, miter.addr, page_to_phys(miter.page));
480 sg_miter_stop(&miter);
481 }
482
Robin Murphy0e080022017-03-31 15:46:05 +0100483 if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, prot)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100484 < size)
485 goto out_free_sg;
486
Robin Murphy0e080022017-03-31 15:46:05 +0100487 *handle = iova;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100488 sg_free_table(&sgt);
489 return pages;
490
491out_free_sg:
492 sg_free_table(&sgt);
493out_free_iova:
Patrick Daly23301482017-10-12 16:18:25 -0700494 iommu_dma_free_iova(domain, cookie, iova, size);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100495out_free_pages:
496 __iommu_dma_free_pages(pages, count);
497 return NULL;
498}
499
500/**
501 * iommu_dma_mmap - Map a buffer into provided user VMA
502 * @pages: Array representing buffer from iommu_dma_alloc()
503 * @size: Size of buffer in bytes
504 * @vma: VMA describing requested userspace mapping
505 *
506 * Maps the pages of the buffer in @pages into @vma. The caller is responsible
507 * for verifying the correct size and protection of @vma beforehand.
508 */
509
510int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma)
511{
512 unsigned long uaddr = vma->vm_start;
513 unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
514 int ret = -ENXIO;
515
516 for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
517 ret = vm_insert_page(vma, uaddr, pages[i]);
518 if (ret)
519 break;
520 uaddr += PAGE_SIZE;
521 }
522 return ret;
523}
524
Robin Murphyc84ed862016-11-14 12:16:26 +0000525static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys,
526 size_t size, int prot)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100527{
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100528 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
Robin Murphy0e080022017-03-31 15:46:05 +0100529 struct iommu_dma_cookie *cookie = domain->iova_cookie;
530 struct iova_domain *iovad = &cookie->iovad;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100531 size_t iova_off = iova_offset(iovad, phys);
Robin Murphy0e080022017-03-31 15:46:05 +0100532 dma_addr_t iova;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100533
Robin Murphy0e080022017-03-31 15:46:05 +0100534 size = iova_align(iovad, size + iova_off);
535 iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100536 if (!iova)
537 return DMA_ERROR_CODE;
538
Robin Murphy0e080022017-03-31 15:46:05 +0100539 if (iommu_map(domain, iova, phys - iova_off, size, prot)) {
Patrick Daly23301482017-10-12 16:18:25 -0700540 iommu_dma_free_iova(domain, cookie, iova, size);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100541 return DMA_ERROR_CODE;
542 }
Robin Murphy0e080022017-03-31 15:46:05 +0100543 return iova + iova_off;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100544}
545
Robin Murphyc84ed862016-11-14 12:16:26 +0000546dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page,
547 unsigned long offset, size_t size, int prot)
548{
549 return __iommu_dma_map(dev, page_to_phys(page) + offset, size, prot);
550}
551
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100552void iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, size_t size,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700553 enum dma_data_direction dir, unsigned long attrs)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100554{
Robin Murphy0e080022017-03-31 15:46:05 +0100555 __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle, size);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100556}
557
558/*
559 * Prepare a successfully-mapped scatterlist to give back to the caller.
Robin Murphy809eac52016-04-11 12:32:31 +0100560 *
561 * At this point the segments are already laid out by iommu_dma_map_sg() to
562 * avoid individually crossing any boundaries, so we merely need to check a
563 * segment's start address to avoid concatenating across one.
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100564 */
565static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents,
566 dma_addr_t dma_addr)
567{
Robin Murphy809eac52016-04-11 12:32:31 +0100568 struct scatterlist *s, *cur = sg;
569 unsigned long seg_mask = dma_get_seg_boundary(dev);
570 unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
571 int i, count = 0;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100572
573 for_each_sg(sg, s, nents, i) {
Robin Murphy809eac52016-04-11 12:32:31 +0100574 /* Restore this segment's original unaligned fields first */
575 unsigned int s_iova_off = sg_dma_address(s);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100576 unsigned int s_length = sg_dma_len(s);
Robin Murphy809eac52016-04-11 12:32:31 +0100577 unsigned int s_iova_len = s->length;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100578
Robin Murphy809eac52016-04-11 12:32:31 +0100579 s->offset += s_iova_off;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100580 s->length = s_length;
Robin Murphy809eac52016-04-11 12:32:31 +0100581 sg_dma_address(s) = DMA_ERROR_CODE;
582 sg_dma_len(s) = 0;
583
584 /*
585 * Now fill in the real DMA data. If...
586 * - there is a valid output segment to append to
587 * - and this segment starts on an IOVA page boundary
588 * - but doesn't fall at a segment boundary
589 * - and wouldn't make the resulting output segment too long
590 */
591 if (cur_len && !s_iova_off && (dma_addr & seg_mask) &&
Robin Murphy10f13d92019-07-29 17:46:00 +0100592 (max_len - cur_len >= s_length)) {
Robin Murphy809eac52016-04-11 12:32:31 +0100593 /* ...then concatenate it with the previous one */
594 cur_len += s_length;
595 } else {
596 /* Otherwise start the next output segment */
597 if (i > 0)
598 cur = sg_next(cur);
599 cur_len = s_length;
600 count++;
601
602 sg_dma_address(cur) = dma_addr + s_iova_off;
603 }
604
605 sg_dma_len(cur) = cur_len;
606 dma_addr += s_iova_len;
607
608 if (s_length + s_iova_off < s_iova_len)
609 cur_len = 0;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100610 }
Robin Murphy809eac52016-04-11 12:32:31 +0100611 return count;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100612}
613
614/*
615 * If mapping failed, then just restore the original list,
616 * but making sure the DMA fields are invalidated.
617 */
618static void __invalidate_sg(struct scatterlist *sg, int nents)
619{
620 struct scatterlist *s;
621 int i;
622
623 for_each_sg(sg, s, nents, i) {
624 if (sg_dma_address(s) != DMA_ERROR_CODE)
Robin Murphy07b48ac2016-03-10 19:28:12 +0000625 s->offset += sg_dma_address(s);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100626 if (sg_dma_len(s))
627 s->length = sg_dma_len(s);
628 sg_dma_address(s) = DMA_ERROR_CODE;
629 sg_dma_len(s) = 0;
630 }
631}
632
633/*
634 * The DMA API client is passing in a scatterlist which could describe
635 * any old buffer layout, but the IOMMU API requires everything to be
636 * aligned to IOMMU pages. Hence the need for this complicated bit of
637 * impedance-matching, to be able to hand off a suitably-aligned list,
638 * but still preserve the original offsets and sizes for the caller.
639 */
640int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg,
641 int nents, int prot)
642{
643 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
Robin Murphy0e080022017-03-31 15:46:05 +0100644 struct iommu_dma_cookie *cookie = domain->iova_cookie;
645 struct iova_domain *iovad = &cookie->iovad;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100646 struct scatterlist *s, *prev = NULL;
Robin Murphy0e080022017-03-31 15:46:05 +0100647 dma_addr_t iova;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100648 size_t iova_len = 0;
Robin Murphy809eac52016-04-11 12:32:31 +0100649 unsigned long mask = dma_get_seg_boundary(dev);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100650 int i;
651
652 /*
653 * Work out how much IOVA space we need, and align the segments to
654 * IOVA granules for the IOMMU driver to handle. With some clever
655 * trickery we can modify the list in-place, but reversibly, by
Robin Murphy809eac52016-04-11 12:32:31 +0100656 * stashing the unaligned parts in the as-yet-unused DMA fields.
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100657 */
658 for_each_sg(sg, s, nents, i) {
Robin Murphy809eac52016-04-11 12:32:31 +0100659 size_t s_iova_off = iova_offset(iovad, s->offset);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100660 size_t s_length = s->length;
Robin Murphy809eac52016-04-11 12:32:31 +0100661 size_t pad_len = (mask - iova_len + 1) & mask;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100662
Robin Murphy809eac52016-04-11 12:32:31 +0100663 sg_dma_address(s) = s_iova_off;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100664 sg_dma_len(s) = s_length;
Robin Murphy809eac52016-04-11 12:32:31 +0100665 s->offset -= s_iova_off;
666 s_length = iova_align(iovad, s_length + s_iova_off);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100667 s->length = s_length;
668
669 /*
Robin Murphy809eac52016-04-11 12:32:31 +0100670 * Due to the alignment of our single IOVA allocation, we can
671 * depend on these assumptions about the segment boundary mask:
672 * - If mask size >= IOVA size, then the IOVA range cannot
673 * possibly fall across a boundary, so we don't care.
674 * - If mask size < IOVA size, then the IOVA range must start
675 * exactly on a boundary, therefore we can lay things out
676 * based purely on segment lengths without needing to know
677 * the actual addresses beforehand.
678 * - The mask must be a power of 2, so pad_len == 0 if
679 * iova_len == 0, thus we cannot dereference prev the first
680 * time through here (i.e. before it has a meaningful value).
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100681 */
Robin Murphy809eac52016-04-11 12:32:31 +0100682 if (pad_len && pad_len < s_length - 1) {
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100683 prev->length += pad_len;
684 iova_len += pad_len;
685 }
686
687 iova_len += s_length;
688 prev = s;
689 }
690
Robin Murphy0e080022017-03-31 15:46:05 +0100691 iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100692 if (!iova)
693 goto out_restore_sg;
694
695 /*
696 * We'll leave any physical concatenation to the IOMMU driver's
697 * implementation - it knows better than we do.
698 */
Robin Murphy0e080022017-03-31 15:46:05 +0100699 if (iommu_map_sg(domain, iova, sg, nents, prot) < iova_len)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100700 goto out_free_iova;
701
Robin Murphy0e080022017-03-31 15:46:05 +0100702 return __finalise_sg(dev, sg, nents, iova);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100703
704out_free_iova:
Patrick Daly23301482017-10-12 16:18:25 -0700705 iommu_dma_free_iova(domain, cookie, iova, iova_len);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100706out_restore_sg:
707 __invalidate_sg(sg, nents);
708 return 0;
709}
710
711void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700712 enum dma_data_direction dir, unsigned long attrs)
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100713{
Robin Murphy0e080022017-03-31 15:46:05 +0100714 dma_addr_t start, end;
715 struct scatterlist *tmp;
716 int i;
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100717 /*
718 * The scatterlist segments are mapped into a single
719 * contiguous IOVA allocation, so this is incredibly easy.
720 */
Robin Murphy0e080022017-03-31 15:46:05 +0100721 start = sg_dma_address(sg);
722 for_each_sg(sg_next(sg), tmp, nents - 1, i) {
723 if (sg_dma_len(tmp) == 0)
724 break;
725 sg = tmp;
726 }
727 end = sg_dma_address(sg) + sg_dma_len(sg);
728 __iommu_dma_unmap(iommu_get_domain_for_dev(dev), start, end - start);
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100729}
730
Robin Murphyc84ed862016-11-14 12:16:26 +0000731dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys,
732 size_t size, enum dma_data_direction dir, unsigned long attrs)
733{
734 return __iommu_dma_map(dev, phys, size,
735 dma_direction_to_prot(dir, false) | IOMMU_MMIO);
736}
737
738void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle,
739 size_t size, enum dma_data_direction dir, unsigned long attrs)
740{
Robin Murphy0e080022017-03-31 15:46:05 +0100741 __iommu_dma_unmap(iommu_get_domain_for_dev(dev), handle, size);
Robin Murphyc84ed862016-11-14 12:16:26 +0000742}
743
Robin Murphy0db2e5d2015-10-01 20:13:58 +0100744int iommu_dma_supported(struct device *dev, u64 mask)
745{
746 /*
747 * 'Special' IOMMUs which don't have the same addressing capability
748 * as the CPU will have to wait until we have some way to query that
749 * before they'll be able to use this framework.
750 */
751 return 1;
752}
753
754int iommu_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
755{
756 return dma_addr == DMA_ERROR_CODE;
757}
Robin Murphy44bb7e22016-09-12 17:13:59 +0100758
759static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev,
760 phys_addr_t msi_addr, struct iommu_domain *domain)
761{
762 struct iommu_dma_cookie *cookie = domain->iova_cookie;
763 struct iommu_dma_msi_page *msi_page;
764 struct iova_domain *iovad = &cookie->iovad;
Robin Murphy0e080022017-03-31 15:46:05 +0100765 dma_addr_t iova;
Robin Murphy44bb7e22016-09-12 17:13:59 +0100766 int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
767
768 msi_addr &= ~(phys_addr_t)iova_mask(iovad);
769 list_for_each_entry(msi_page, &cookie->msi_page_list, list)
770 if (msi_page->phys == msi_addr)
771 return msi_page;
772
773 msi_page = kzalloc(sizeof(*msi_page), GFP_ATOMIC);
774 if (!msi_page)
775 return NULL;
776
Robin Murphy0e080022017-03-31 15:46:05 +0100777 iova = iommu_dma_alloc_iova(domain, iovad->granule, dma_get_mask(dev),
778 dev);
Robin Murphy44bb7e22016-09-12 17:13:59 +0100779 if (!iova)
780 goto out_free_page;
781
782 msi_page->phys = msi_addr;
Robin Murphy0e080022017-03-31 15:46:05 +0100783 msi_page->iova = iova;
Robin Murphy44bb7e22016-09-12 17:13:59 +0100784 if (iommu_map(domain, msi_page->iova, msi_addr, iovad->granule, prot))
785 goto out_free_iova;
786
787 INIT_LIST_HEAD(&msi_page->list);
788 list_add(&msi_page->list, &cookie->msi_page_list);
789 return msi_page;
790
791out_free_iova:
Patrick Daly23301482017-10-12 16:18:25 -0700792 iommu_dma_free_iova(domain, cookie, iova, iovad->granule);
Robin Murphy44bb7e22016-09-12 17:13:59 +0100793out_free_page:
794 kfree(msi_page);
795 return NULL;
796}
797
798void iommu_dma_map_msi_msg(int irq, struct msi_msg *msg)
799{
800 struct device *dev = msi_desc_to_dev(irq_get_msi_desc(irq));
801 struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
802 struct iommu_dma_cookie *cookie;
803 struct iommu_dma_msi_page *msi_page;
804 phys_addr_t msi_addr = (u64)msg->address_hi << 32 | msg->address_lo;
805 unsigned long flags;
806
807 if (!domain || !domain->iova_cookie)
808 return;
809
810 cookie = domain->iova_cookie;
811
812 /*
813 * We disable IRQs to rule out a possible inversion against
814 * irq_desc_lock if, say, someone tries to retarget the affinity
815 * of an MSI from within an IPI handler.
816 */
817 spin_lock_irqsave(&cookie->msi_lock, flags);
818 msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
819 spin_unlock_irqrestore(&cookie->msi_lock, flags);
820
821 if (WARN_ON(!msi_page)) {
822 /*
823 * We're called from a void callback, so the best we can do is
824 * 'fail' by filling the message with obviously bogus values.
825 * Since we got this far due to an IOMMU being present, it's
826 * not like the existing address would have worked anyway...
827 */
828 msg->address_hi = ~0U;
829 msg->address_lo = ~0U;
830 msg->data = ~0U;
831 } else {
832 msg->address_hi = upper_32_bits(msi_page->iova);
833 msg->address_lo &= iova_mask(&cookie->iovad);
834 msg->address_lo += lower_32_bits(msi_page->iova);
835 }
836}