• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * A fairly generic DMA-API to IOMMU-API glue layer.
4  *
5  * Copyright (C) 2014-2015 ARM Ltd.
6  *
7  * based in part on arch/arm/mm/dma-mapping.c:
8  * Copyright (C) 2000-2004 Russell King
9  */
10 
11 #include <linux/acpi_iort.h>
12 #include <linux/device.h>
13 #include <linux/dma-map-ops.h>
14 #include <linux/dma-iommu.h>
15 #include <linux/gfp.h>
16 #include <linux/huge_mm.h>
17 #include <linux/iommu.h>
18 #include <linux/iova.h>
19 #include <linux/irq.h>
20 #include <linux/mm.h>
21 #include <linux/mutex.h>
22 #include <linux/pci.h>
23 #include <linux/scatterlist.h>
24 #include <linux/vmalloc.h>
25 #include <linux/crash_dump.h>
26 
27 #define DMA_IOMMU_BIT_MASK_VALUE 32
28 
29 struct iommu_dma_msi_page {
30     struct list_head list;
31     dma_addr_t iova;
32     phys_addr_t phys;
33 };
34 
35 enum iommu_dma_cookie_type {
36     IOMMU_DMA_IOVA_COOKIE,
37     IOMMU_DMA_MSI_COOKIE,
38 };
39 
40 struct iommu_dma_cookie {
41     enum iommu_dma_cookie_type type;
42     union {
43         /* Full allocator for IOMMU_DMA_IOVA_COOKIE */
44         struct iova_domain iovad;
45         /* Trivial linear page allocator for IOMMU_DMA_MSI_COOKIE */
46         dma_addr_t msi_iova;
47     };
48     struct list_head msi_page_list;
49 
50     /* Domain for flush queue callback; NULL if flush queue not in use */
51     struct iommu_domain *fq_domain;
52 };
53 
cookie_msi_granule(struct iommu_dma_cookie * cookie)54 static inline size_t cookie_msi_granule(struct iommu_dma_cookie *cookie)
55 {
56     if (cookie->type == IOMMU_DMA_IOVA_COOKIE) {
57         return cookie->iovad.granule;
58     }
59     return PAGE_SIZE;
60 }
61 
cookie_alloc(enum iommu_dma_cookie_type type)62 static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)
63 {
64     struct iommu_dma_cookie *cookie;
65 
66     cookie = kzalloc(sizeof(*cookie), GFP_KERNEL);
67     if (cookie) {
68         INIT_LIST_HEAD(&cookie->msi_page_list);
69         cookie->type = type;
70     }
71     return cookie;
72 }
73 
74 /**
75  * iommu_get_dma_cookie - Acquire DMA-API resources for a domain
76  * @domain: IOMMU domain to prepare for DMA-API usage
77  *
78  * IOMMU drivers should normally call this from their domain_alloc
79  * callback when domain->type == IOMMU_DOMAIN_DMA.
80  */
iommu_get_dma_cookie(struct iommu_domain * domain)81 int iommu_get_dma_cookie(struct iommu_domain *domain)
82 {
83     if (domain->iova_cookie) {
84         return -EEXIST;
85     }
86 
87     domain->iova_cookie = cookie_alloc(IOMMU_DMA_IOVA_COOKIE);
88     if (!domain->iova_cookie) {
89         return -ENOMEM;
90     }
91 
92     return 0;
93 }
94 EXPORT_SYMBOL(iommu_get_dma_cookie);
95 
96 /**
97  * iommu_get_msi_cookie - Acquire just MSI remapping resources
98  * @domain: IOMMU domain to prepare
99  * @base: Start address of IOVA region for MSI mappings
100  *
101  * Users who manage their own IOVA allocation and do not want DMA API support,
102  * but would still like to take advantage of automatic MSI remapping, can use
103  * this to initialise their own domain appropriately. Users should reserve a
104  * contiguous IOVA region, starting at @base, large enough to accommodate the
105  * number of PAGE_SIZE mappings necessary to cover every MSI doorbell address
106  * used by the devices attached to @domain.
107  */
iommu_get_msi_cookie(struct iommu_domain * domain,dma_addr_t base)108 int iommu_get_msi_cookie(struct iommu_domain *domain, dma_addr_t base)
109 {
110     struct iommu_dma_cookie *cookie;
111 
112     if (domain->type != IOMMU_DOMAIN_UNMANAGED) {
113         return -EINVAL;
114     }
115 
116     if (domain->iova_cookie) {
117         return -EEXIST;
118     }
119 
120     cookie = cookie_alloc(IOMMU_DMA_MSI_COOKIE);
121     if (!cookie) {
122         return -ENOMEM;
123     }
124 
125     cookie->msi_iova = base;
126     domain->iova_cookie = cookie;
127     return 0;
128 }
129 EXPORT_SYMBOL(iommu_get_msi_cookie);
130 
131 /**
132  * iommu_put_dma_cookie - Release a domain's DMA mapping resources
133  * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie() or
134  *          iommu_get_msi_cookie()
135  *
136  * IOMMU drivers should normally call this from their domain_free callback.
137  */
iommu_put_dma_cookie(struct iommu_domain * domain)138 void iommu_put_dma_cookie(struct iommu_domain *domain)
139 {
140     struct iommu_dma_cookie *cookie = domain->iova_cookie;
141     struct iommu_dma_msi_page *msi, *tmp;
142 
143     if (!cookie) {
144         return;
145     }
146 
147     if (cookie->type == IOMMU_DMA_IOVA_COOKIE && cookie->iovad.granule) {
148         put_iova_domain(&cookie->iovad);
149     }
150 
151     list_for_each_entry_safe(msi, tmp, &cookie->msi_page_list, list)
152     {
153         list_del(&msi->list);
154         kfree(msi);
155     }
156     kfree(cookie);
157     domain->iova_cookie = NULL;
158 }
159 EXPORT_SYMBOL(iommu_put_dma_cookie);
160 
161 /**
162  * iommu_dma_get_resv_regions - Reserved region driver helper
163  * @dev: Device from iommu_get_resv_regions()
164  * @list: Reserved region list from iommu_get_resv_regions()
165  *
166  * IOMMU drivers can use this to implement their .get_resv_regions callback
167  * for general non-IOMMU-specific reservations. Currently, this covers GICv3
168  * ITS region reservation on ACPI based ARM platforms that may require HW MSI
169  * reservation.
170  */
iommu_dma_get_resv_regions(struct device * dev,struct list_head * list)171 void iommu_dma_get_resv_regions(struct device *dev, struct list_head *list)
172 {
173     if (!is_of_node(dev_iommu_fwspec_get(dev)->iommu_fwnode)) {
174         iort_iommu_msi_get_resv_regions(dev, list);
175     }
176 }
177 EXPORT_SYMBOL(iommu_dma_get_resv_regions);
178 
cookie_init_hw_msi_region(struct iommu_dma_cookie * cookie,phys_addr_t start,phys_addr_t end)179 static int cookie_init_hw_msi_region(struct iommu_dma_cookie *cookie, phys_addr_t start, phys_addr_t end)
180 {
181     struct iova_domain *iovad = &cookie->iovad;
182     struct iommu_dma_msi_page *msi_page;
183     int i, num_pages;
184 
185     start -= iova_offset(iovad, start);
186     num_pages = iova_align(iovad, end - start) >> iova_shift(iovad);
187 
188     for (i = 0; i < num_pages; i++) {
189         msi_page = kmalloc(sizeof(*msi_page), GFP_KERNEL);
190         if (!msi_page) {
191             return -ENOMEM;
192         }
193 
194         msi_page->phys = start;
195         msi_page->iova = start;
196         INIT_LIST_HEAD(&msi_page->list);
197         list_add(&msi_page->list, &cookie->msi_page_list);
198         start += iovad->granule;
199     }
200 
201     return 0;
202 }
203 
iova_reserve_pci_windows(struct pci_dev * dev,struct iova_domain * iovad)204 static int iova_reserve_pci_windows(struct pci_dev *dev, struct iova_domain *iovad)
205 {
206     struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
207     struct resource_entry *window;
208     unsigned long lo, hi;
209     phys_addr_t start = 0, end;
210 
211     resource_list_for_each_entry(window, &bridge->windows)
212     {
213         if (resource_type(window->res) != IORESOURCE_MEM) {
214             continue;
215         }
216 
217         lo = iova_pfn(iovad, window->res->start - window->offset);
218         hi = iova_pfn(iovad, window->res->end - window->offset);
219         reserve_iova(iovad, lo, hi);
220     }
221 
222     /* Get reserved DMA windows from host bridge */
223     resource_list_for_each_entry(window, &bridge->dma_ranges)
224     {
225         end = window->res->start - window->offset;
226     resv_iova:
227         if (end > start) {
228             lo = iova_pfn(iovad, start);
229             hi = iova_pfn(iovad, end);
230             reserve_iova(iovad, lo, hi);
231         } else if (end < start) {
232             /* dma_ranges list should be sorted */
233             dev_err(&dev->dev, "Failed to reserve IOVA [%pa-%pa]\n", &start, &end);
234             return -EINVAL;
235         }
236 
237         start = window->res->end - window->offset + 1;
238         /* If window is last entry */
239         if (window->node.next == &bridge->dma_ranges && end != ~(phys_addr_t)0) {
240             end = ~(phys_addr_t)0;
241             goto resv_iova;
242         }
243     }
244 
245     return 0;
246 }
247 
iova_reserve_iommu_regions(struct device * dev,struct iommu_domain * domain)248 static int iova_reserve_iommu_regions(struct device *dev, struct iommu_domain *domain)
249 {
250     struct iommu_dma_cookie *cookie = domain->iova_cookie;
251     struct iova_domain *iovad = &cookie->iovad;
252     struct iommu_resv_region *region;
253     LIST_HEAD(resv_regions);
254     int ret = 0;
255 
256     if (dev_is_pci(dev)) {
257         ret = iova_reserve_pci_windows(to_pci_dev(dev), iovad);
258         if (ret) {
259             return ret;
260         }
261     }
262 
263     iommu_get_resv_regions(dev, &resv_regions);
264     list_for_each_entry(region, &resv_regions, list)
265     {
266         unsigned long lo, hi;
267 
268         /* We ARE the software that manages these! */
269         if (region->type == IOMMU_RESV_SW_MSI) {
270             continue;
271         }
272 
273         lo = iova_pfn(iovad, region->start);
274         hi = iova_pfn(iovad, region->start + region->length - 1);
275         reserve_iova(iovad, lo, hi);
276 
277         if (region->type == IOMMU_RESV_MSI) {
278             ret = cookie_init_hw_msi_region(cookie, region->start, region->start + region->length);
279         }
280         if (ret) {
281             break;
282         }
283     }
284     iommu_put_resv_regions(dev, &resv_regions);
285 
286     return ret;
287 }
288 
iommu_dma_flush_iotlb_all(struct iova_domain * iovad)289 static void iommu_dma_flush_iotlb_all(struct iova_domain *iovad)
290 {
291     struct iommu_dma_cookie *cookie;
292     struct iommu_domain *domain;
293 
294     cookie = container_of(iovad, struct iommu_dma_cookie, iovad);
295     domain = cookie->fq_domain;
296     /*
297      * The IOMMU driver supporting DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE
298      * implies that ops->flush_iotlb_all must be non-NULL.
299      */
300     domain->ops->flush_iotlb_all(domain);
301 }
302 
303 /**
304  * iommu_dma_init_domain - Initialise a DMA mapping domain
305  * @domain: IOMMU domain previously prepared by iommu_get_dma_cookie()
306  * @base: IOVA at which the mappable address space starts
307  * @size: Size of IOVA space
308  * @dev: Device the domain is being initialised for
309  *
310  * @base and @size should be exact multiples of IOMMU page granularity to
311  * avoid rounding surprises. If necessary, we reserve the page at address 0
312  * to ensure it is an invalid IOVA. It is safe to reinitialise a domain, but
313  * any change which could make prior IOVAs invalid will fail.
314  */
iommu_dma_init_domain(struct iommu_domain * domain,dma_addr_t base,u64 size,struct device * dev)315 static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base, u64 size, struct device *dev)
316 {
317     struct iommu_dma_cookie *cookie = domain->iova_cookie;
318     unsigned long order, base_pfn;
319     struct iova_domain *iovad;
320     int attr;
321 
322     if (!cookie || cookie->type != IOMMU_DMA_IOVA_COOKIE) {
323         return -EINVAL;
324     }
325 
326     iovad = &cookie->iovad;
327 
328     /* Use the smallest supported page size for IOVA granularity */
329     order = __ffs(domain->pgsize_bitmap);
330     base_pfn = max_t(unsigned long, 1, base >> order);
331 
332     /* Check the domain allows at least some access to the device... */
333     if (domain->geometry.force_aperture) {
334         if (base > domain->geometry.aperture_end || base + size <= domain->geometry.aperture_start) {
335             pr_warn("specified DMA range outside IOMMU capability\n");
336             return -EFAULT;
337         }
338         /* ...then finally give it a kicking to make sure it fits */
339         base_pfn = max_t(unsigned long, base_pfn, domain->geometry.aperture_start >> order);
340     }
341 
342     /* start_pfn is always nonzero for an already-initialised domain */
343     if (iovad->start_pfn) {
344         if (1UL << order != iovad->granule || base_pfn != iovad->start_pfn) {
345             pr_warn("Incompatible range for DMA domain\n");
346             return -EFAULT;
347         }
348 
349         return 0;
350     }
351 
352     init_iova_domain(iovad, 1UL << order, base_pfn);
353 
354     if (!cookie->fq_domain && !iommu_domain_get_attr(domain, DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE, &attr) && attr) {
355         if (init_iova_flush_queue(iovad, iommu_dma_flush_iotlb_all, NULL)) {
356             pr_warn("iova flush queue initialization failed\n");
357         } else {
358             cookie->fq_domain = domain;
359         }
360     }
361 
362     if (!dev) {
363         return 0;
364     }
365 
366     return iova_reserve_iommu_regions(dev, domain);
367 }
368 
iommu_dma_deferred_attach(struct device * dev,struct iommu_domain * domain)369 static int iommu_dma_deferred_attach(struct device *dev, struct iommu_domain *domain)
370 {
371     const struct iommu_ops *ops = domain->ops;
372 
373     if (!is_kdump_kernel()) {
374         return 0;
375     }
376 
377     if (unlikely(ops->is_attach_deferred && ops->is_attach_deferred(domain, dev))) {
378         return iommu_attach_device(domain, dev);
379     }
380 
381     return 0;
382 }
383 
384 /*
385  * Should be called prior to using dma-apis
386  */
iommu_dma_reserve_iova(struct device * dev,dma_addr_t base,u64 size)387 int iommu_dma_reserve_iova(struct device *dev, dma_addr_t base, u64 size)
388 {
389     struct iommu_domain *domain;
390     struct iommu_dma_cookie *cookie;
391     struct iova_domain *iovad;
392     unsigned long pfn_lo, pfn_hi;
393 
394     domain = iommu_get_domain_for_dev(dev);
395     if (!domain || !domain->iova_cookie) {
396         return -EINVAL;
397     }
398 
399     cookie = domain->iova_cookie;
400     iovad = &cookie->iovad;
401 
402     /* iova will be freed automatically by put_iova_domain() */
403     pfn_lo = iova_pfn(iovad, base);
404     pfn_hi = iova_pfn(iovad, base + size - 1);
405     if (!reserve_iova(iovad, pfn_lo, pfn_hi)) {
406         return -EINVAL;
407     }
408 
409     return 0;
410 }
411 EXPORT_SYMBOL(iommu_dma_reserve_iova);
412 
413 /*
414  * Should be called prior to using dma-apis.
415  */
iommu_dma_enable_best_fit_algo(struct device * dev)416 int iommu_dma_enable_best_fit_algo(struct device *dev)
417 {
418     struct iommu_domain *domain;
419     struct iova_domain *iovad;
420 
421     domain = iommu_get_domain_for_dev(dev);
422     if (!domain || !domain->iova_cookie) {
423         return -EINVAL;
424     }
425 
426     iovad = &((struct iommu_dma_cookie *)domain->iova_cookie)->iovad;
427     iovad->best_fit = true;
428     return 0;
429 }
430 EXPORT_SYMBOL(iommu_dma_enable_best_fit_algo);
431 
432 /**
433  * dma_info_to_prot - Translate DMA API directions and attributes to IOMMU API
434  *                    page flags.
435  * @dir: Direction of DMA transfer
436  * @coherent: Is the DMA master cache-coherent?
437  * @attrs: DMA attributes for the mapping
438  *
439  * Return: corresponding IOMMU API page protection flags
440  */
dma_info_to_prot(enum dma_data_direction dir,bool coherent,unsigned long attrs)441 static int dma_info_to_prot(enum dma_data_direction dir, bool coherent, unsigned long attrs)
442 {
443     int prot = coherent ? IOMMU_CACHE : 0;
444 
445     if (attrs & DMA_ATTR_PRIVILEGED) {
446         prot |= IOMMU_PRIV;
447     }
448     if (attrs & DMA_ATTR_SYS_CACHE_ONLY) {
449         prot |= IOMMU_SYS_CACHE_ONLY;
450     }
451     if (attrs & DMA_ATTR_SYS_CACHE_ONLY_NWA) {
452         prot |= IOMMU_SYS_CACHE_ONLY_NWA;
453     }
454 
455     switch (dir) {
456         case DMA_BIDIRECTIONAL:
457             return prot | IOMMU_READ | IOMMU_WRITE;
458         case DMA_TO_DEVICE:
459             return prot | IOMMU_READ;
460         case DMA_FROM_DEVICE:
461             return prot | IOMMU_WRITE;
462         default:
463             return 0;
464     }
465 }
466 
iommu_dma_alloc_iova(struct iommu_domain * domain,size_t size,u64 dma_limit,struct device * dev)467 static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain, size_t size, u64 dma_limit, struct device *dev)
468 {
469     struct iommu_dma_cookie *cookie = domain->iova_cookie;
470     struct iova_domain *iovad = &cookie->iovad;
471     unsigned long shift, iova_len, iova = 0;
472 
473     if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
474         cookie->msi_iova += size;
475         return cookie->msi_iova - size;
476     }
477 
478     shift = iova_shift(iovad);
479     iova_len = size >> shift;
480     /*
481      * Freeing non-power-of-two-sized allocations back into the IOVA caches
482      * will come back to bite us badly, so we have to waste a bit of space
483      * rounding up anything cacheable to make sure that can't happen. The
484      * order of the unadjusted size will still match upon freeing.
485      */
486     if (iova_len < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1))) {
487         iova_len = roundup_pow_of_two(iova_len);
488     }
489 
490     dma_limit = min_not_zero(dma_limit, dev->bus_dma_limit);
491 
492     if (domain->geometry.force_aperture) {
493         dma_limit = min(dma_limit, (u64)domain->geometry.aperture_end);
494     }
495 
496     /* Try to get PCI devices a SAC address */
497     if (dma_limit > DMA_BIT_MASK(DMA_IOMMU_BIT_MASK_VALUE) && dev_is_pci(dev)) {
498         iova = alloc_iova_fast(iovad, iova_len, DMA_BIT_MASK(DMA_IOMMU_BIT_MASK_VALUE) >> shift, false);
499     }
500 
501     if (!iova) {
502         iova = alloc_iova_fast(iovad, iova_len, dma_limit >> shift, true);
503     }
504 
505     return (dma_addr_t)iova << shift;
506 }
507 
iommu_dma_free_iova(struct iommu_dma_cookie * cookie,dma_addr_t iova,size_t size)508 static void iommu_dma_free_iova(struct iommu_dma_cookie *cookie, dma_addr_t iova, size_t size)
509 {
510     struct iova_domain *iovad = &cookie->iovad;
511 
512     /* The MSI case is only ever cleaning up its most recent allocation */
513     if (cookie->type == IOMMU_DMA_MSI_COOKIE) {
514         cookie->msi_iova -= size;
515     } else if (cookie->fq_domain) { /* non-strict mode */
516         queue_iova(iovad, iova_pfn(iovad, iova), size >> iova_shift(iovad), 0);
517     } else {
518         free_iova_fast(iovad, iova_pfn(iovad, iova), size >> iova_shift(iovad));
519     }
520 }
521 
iommu_dma_unmap_ext(struct device * dev,dma_addr_t dma_addr,size_t size)522 static void iommu_dma_unmap_ext(struct device *dev, dma_addr_t dma_addr, size_t size)
523 {
524     struct iommu_domain *domain = iommu_get_dma_domain(dev);
525     struct iommu_dma_cookie *cookie = domain->iova_cookie;
526     struct iova_domain *iovad = &cookie->iovad;
527     size_t iova_off = iova_offset(iovad, dma_addr);
528     struct iommu_iotlb_gather iotlb_gather;
529     size_t unmapped;
530 
531     dma_addr -= iova_off;
532     size = iova_align(iovad, size + iova_off);
533     iommu_iotlb_gather_init(&iotlb_gather);
534 
535     unmapped = iommu_unmap_fast(domain, dma_addr, size, &iotlb_gather);
536     WARN_ON(unmapped != size);
537 
538     if (!cookie->fq_domain) {
539         iommu_iotlb_sync(domain, &iotlb_gather);
540     }
541     iommu_dma_free_iova(cookie, dma_addr, size);
542 }
543 
iommu_dma_map_ext(struct device * dev,phys_addr_t phys,size_t size,int prot,u64 dma_mask)544 static dma_addr_t iommu_dma_map_ext(struct device *dev, phys_addr_t phys, size_t size, int prot, u64 dma_mask)
545 {
546     struct iommu_domain *domain = iommu_get_dma_domain(dev);
547     struct iommu_dma_cookie *cookie = domain->iova_cookie;
548     struct iova_domain *iovad = &cookie->iovad;
549     size_t iova_off = iova_offset(iovad, phys);
550     dma_addr_t iova;
551 
552     if (unlikely(iommu_dma_deferred_attach(dev, domain))) {
553         return DMA_MAPPING_ERROR;
554     }
555 
556     size = iova_align(iovad, size + iova_off);
557 
558     iova = iommu_dma_alloc_iova(domain, size, dma_mask, dev);
559     if (!iova) {
560         return DMA_MAPPING_ERROR;
561     }
562 
563     if (iommu_map_atomic(domain, iova, phys - iova_off, size, prot)) {
564         iommu_dma_free_iova(cookie, iova, size);
565         return DMA_MAPPING_ERROR;
566     }
567     return iova + iova_off;
568 }
569 
iommu_dma_free_pages_ext(struct page ** pages,int count)570 static void iommu_dma_free_pages_ext(struct page **pages, int count)
571 {
572     while (count--) {
573         __free_page(pages[count]);
574     }
575     kvfree(pages);
576 }
577 
iommu_dma_alloc_pages_ext(struct device * dev,unsigned int count,unsigned long order_mask,gfp_t gfp)578 static struct page **iommu_dma_alloc_pages_ext(struct device *dev, unsigned int count, unsigned long order_mask,
579                                                gfp_t gfp)
580 {
581     struct page **pages;
582     unsigned int i = 0, nid = dev_to_node(dev);
583 
584     order_mask &= (2U << MAX_ORDER) - 1;
585     if (!order_mask) {
586         return NULL;
587     }
588 
589     pages = kvzalloc(count * sizeof(*pages), GFP_KERNEL);
590     if (!pages) {
591         return NULL;
592     }
593 
594     /* IOMMU can map any pages, so himem can also be used here */
595     gfp |= __GFP_NOWARN | __GFP_HIGHMEM;
596 
597     /* It makes no sense to muck about with huge pages */
598     gfp &= ~__GFP_COMP;
599 
600     while (count) {
601         struct page *page = NULL;
602         unsigned int order_size;
603 
604         /*
605          * Higher-order allocations are a convenience rather
606          * than a necessity, hence using __GFP_NORETRY until
607          * falling back to minimum-order allocations.
608          */
609         for (order_mask &= (2U << __fls(count)) - 1; order_mask; order_mask &= ~order_size) {
610             unsigned int order = __fls(order_mask);
611             gfp_t alloc_flags = gfp;
612 
613             order_size = 1U << order;
614             if (order_mask > (unsigned long)order_size) {
615                 alloc_flags |= __GFP_NORETRY;
616             }
617             page = alloc_pages_node(nid, alloc_flags, order);
618             if (!page) {
619                 continue;
620             }
621             if (order) {
622                 split_page(page, order);
623             }
624             break;
625         }
626         if (!page) {
627             iommu_dma_free_pages_ext(pages, i);
628             return NULL;
629         }
630         count -= order_size;
631         while (order_size--) {
632             pages[i++] = page++;
633         }
634     }
635     return pages;
636 }
637 
638 /**
639  * iommu_dma_alloc_remap - Allocate and map a buffer contiguous in IOVA space
640  * @dev: Device to allocate memory for. Must be a real device
641  *     attached to an iommu_dma_domain
642  * @size: Size of buffer in bytes
643  * @dma_handle: Out argument for allocated DMA handle
644  * @gfp: Allocation flags
645  * @prot: pgprot_t to use for the remapped mapping
646  * @attrs: DMA attributes for this allocation
647  *
648  * If @size is less than PAGE_SIZE, then a full CPU page will be allocated,
649  * but an IOMMU which supports smaller pages might not map the whole thing.
650  *
651  * Return: Mapped virtual address, or NULL on failure.
652  */
iommu_dma_alloc_remap(struct device * dev,size_t size,dma_addr_t * dma_handle,gfp_t gfp,pgprot_t prot,unsigned long attrs)653 static void *iommu_dma_alloc_remap(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t gfp, pgprot_t prot,
654                                    unsigned long attrs)
655 {
656     struct iommu_domain *domain = iommu_get_dma_domain(dev);
657     struct iommu_dma_cookie *cookie = domain->iova_cookie;
658     struct iova_domain *iovad = &cookie->iovad;
659     bool coherent = dev_is_dma_coherent(dev);
660     int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
661     unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap;
662     struct page **pages;
663     struct sg_table sgt;
664     dma_addr_t iova;
665     void *vaddr;
666 
667     *dma_handle = DMA_MAPPING_ERROR;
668 
669     if (unlikely(iommu_dma_deferred_attach(dev, domain))) {
670         return NULL;
671     }
672 
673     min_size = alloc_sizes & -alloc_sizes;
674     if (min_size < PAGE_SIZE) {
675         min_size = PAGE_SIZE;
676         alloc_sizes |= PAGE_SIZE;
677     } else {
678         size = ALIGN(size, min_size);
679     }
680     if (attrs & DMA_ATTR_ALLOC_SINGLE_PAGES) {
681         alloc_sizes = min_size;
682     }
683 
684     count = PAGE_ALIGN(size) >> PAGE_SHIFT;
685     pages = iommu_dma_alloc_pages_ext(dev, count, alloc_sizes >> PAGE_SHIFT, gfp);
686     if (!pages) {
687         return NULL;
688     }
689 
690     size = iova_align(iovad, size);
691     iova = iommu_dma_alloc_iova(domain, size, dev->coherent_dma_mask, dev);
692     if (!iova) {
693         goto out_free_pages;
694     }
695 
696     if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL)) {
697         goto out_free_iova;
698     }
699 
700     if (!(ioprot & IOMMU_CACHE)) {
701         struct scatterlist *sg;
702         int i;
703 
704         for_each_sg(sgt.sgl, sg, sgt.orig_nents, i) arch_dma_prep_coherent(sg_page(sg), sg->length);
705     }
706 
707     if (iommu_map_sg_atomic(domain, iova, sgt.sgl, sgt.orig_nents, ioprot) < size) {
708         goto out_free_sg;
709     }
710 
711     vaddr = dma_common_pages_remap(pages, size, prot, __builtin_return_address(0));
712     if (!vaddr) {
713         goto out_unmap;
714     }
715 
716     *dma_handle = iova;
717     sg_free_table(&sgt);
718     return vaddr;
719 
720 out_unmap:
721     iommu_dma_unmap_ext(dev, iova, size);
722 out_free_sg:
723     sg_free_table(&sgt);
724 out_free_iova:
725     iommu_dma_free_iova(cookie, iova, size);
726 out_free_pages:
727     iommu_dma_free_pages_ext(pages, count);
728     return NULL;
729 }
730 
731 /**
732  * iommu_dma_mmap_ext - Map a buffer into provided user VMA
733  * @pages: Array representing buffer from __iommu_dma_alloc()
734  * @size: Size of buffer in bytes
735  * @vma: VMA describing requested userspace mapping
736  *
737  * Maps the pages of the buffer in @pages into @vma. The caller is responsible
738  * for verifying the correct size and protection of @vma beforehand.
739  */
iommu_dma_mmap_ext(struct page ** pages,size_t size,struct vm_area_struct * vma)740 static int iommu_dma_mmap_ext(struct page **pages, size_t size, struct vm_area_struct *vma)
741 {
742     return vm_map_pages(vma, pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
743 }
744 
iommu_dma_sync_single_for_cpu(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir)745 static void iommu_dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
746                                           enum dma_data_direction dir)
747 {
748     phys_addr_t phys;
749 
750     if (dev_is_dma_coherent(dev)) {
751         return;
752     }
753 
754     phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
755     arch_sync_dma_for_cpu(phys, size, dir);
756 }
757 
iommu_dma_sync_single_for_device(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir)758 static void iommu_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle, size_t size,
759                                              enum dma_data_direction dir)
760 {
761     phys_addr_t phys;
762 
763     if (dev_is_dma_coherent(dev)) {
764         return;
765     }
766 
767     phys = iommu_iova_to_phys(iommu_get_dma_domain(dev), dma_handle);
768     arch_sync_dma_for_device(phys, size, dir);
769 }
770 
iommu_dma_sync_sg_for_cpu(struct device * dev,struct scatterlist * sgl,int nelems,enum dma_data_direction dir)771 static void iommu_dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, int nelems,
772                                       enum dma_data_direction dir)
773 {
774     struct scatterlist *sg;
775     int i;
776 
777     if (dev_is_dma_coherent(dev)) {
778         return;
779     }
780 
781     for_each_sg(sgl, sg, nelems, i) arch_sync_dma_for_cpu(sg_phys(sg), sg->length, dir);
782 }
783 
iommu_dma_sync_sg_for_device(struct device * dev,struct scatterlist * sgl,int nelems,enum dma_data_direction dir)784 static void iommu_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sgl, int nelems,
785                                          enum dma_data_direction dir)
786 {
787     struct scatterlist *sg;
788     int i;
789 
790     if (dev_is_dma_coherent(dev)) {
791         return;
792     }
793 
794     for_each_sg(sgl, sg, nelems, i) arch_sync_dma_for_device(sg_phys(sg), sg->length, dir);
795 }
796 
iommu_dma_map_page(struct device * dev,struct page * page,unsigned long offset,size_t size,enum dma_data_direction dir,unsigned long attrs)797 static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, unsigned long offset, size_t size,
798                                      enum dma_data_direction dir, unsigned long attrs)
799 {
800     phys_addr_t phys = page_to_phys(page) + offset;
801     bool coherent = dev_is_dma_coherent(dev);
802     int prot = dma_info_to_prot(dir, coherent, attrs);
803     dma_addr_t dma_handle;
804 
805     dma_handle = iommu_dma_map_ext(dev, phys, size, prot, dma_get_mask(dev));
806     if (!coherent && !(attrs & DMA_ATTR_SKIP_CPU_SYNC) && dma_handle != DMA_MAPPING_ERROR) {
807         arch_sync_dma_for_device(phys, size, dir);
808     }
809     return dma_handle;
810 }
811 
iommu_dma_unmap_page(struct device * dev,dma_addr_t dma_handle,size_t size,enum dma_data_direction dir,unsigned long attrs)812 static void iommu_dma_unmap_page(struct device *dev, dma_addr_t dma_handle, size_t size, enum dma_data_direction dir,
813                                  unsigned long attrs)
814 {
815     if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
816         iommu_dma_sync_single_for_cpu(dev, dma_handle, size, dir);
817     }
818     iommu_dma_unmap_ext(dev, dma_handle, size);
819 }
820 
821 /*
822  * Prepare a successfully-mapped scatterlist to give back to the caller.
823  *
824  * At this point the segments are already laid out by iommu_dma_map_sg() to
825  * avoid individually crossing any boundaries, so we merely need to check a
826  * segment's start address to avoid concatenating across one.
827  */
finalise_sg_ext(struct device * dev,struct scatterlist * sg,int nents,dma_addr_t dma_addr)828 static int finalise_sg_ext(struct device *dev, struct scatterlist *sg, int nents, dma_addr_t dma_addr)
829 {
830     struct scatterlist *s, *cur = sg;
831     unsigned long seg_mask = dma_get_seg_boundary(dev);
832     unsigned int cur_len = 0, max_len = dma_get_max_seg_size(dev);
833     int i, count = 0;
834 
835     for_each_sg(sg, s, nents, i)
836     {
837         /* Restore this segment's original unaligned fields first */
838         unsigned int s_iova_off = sg_dma_address(s);
839         unsigned int s_length = sg_dma_len(s);
840         unsigned int s_iova_len = s->length;
841 
842         s->offset += s_iova_off;
843         s->length = s_length;
844         sg_dma_address(s) = DMA_MAPPING_ERROR;
845         sg_dma_len(s) = 0;
846 
847         /*
848          * Now fill in the real DMA data. If...
849          * - there is a valid output segment to append to
850          * - and this segment starts on an IOVA page boundary
851          * - but doesn't fall at a segment boundary
852          * - and wouldn't make the resulting output segment too long
853          */
854         if (cur_len && !s_iova_off && (dma_addr & seg_mask) && (max_len - cur_len >= s_length)) {
855             /* ...then concatenate it with the previous one */
856             cur_len += s_length;
857         } else {
858             /* Otherwise start the next output segment */
859             if (i > 0) {
860                 cur = sg_next(cur);
861             }
862             cur_len = s_length;
863             count++;
864 
865             sg_dma_address(cur) = dma_addr + s_iova_off;
866         }
867 
868         sg_dma_len(cur) = cur_len;
869         dma_addr += s_iova_len;
870 
871         if (s_length + s_iova_off < s_iova_len) {
872             cur_len = 0;
873         }
874     }
875     return count;
876 }
877 
878 /*
879  * If mapping failed, then just restore the original list,
880  * but making sure the DMA fields are invalidated.
881  */
invalidate_sg_ext(struct scatterlist * sg,int nents)882 static void invalidate_sg_ext(struct scatterlist *sg, int nents)
883 {
884     struct scatterlist *s;
885     int i;
886 
887     for_each_sg(sg, s, nents, i)
888     {
889         if (sg_dma_address(s) != DMA_MAPPING_ERROR) {
890             s->offset += sg_dma_address(s);
891         }
892         if (sg_dma_len(s)) {
893             s->length = sg_dma_len(s);
894         }
895         sg_dma_address(s) = DMA_MAPPING_ERROR;
896         sg_dma_len(s) = 0;
897     }
898 }
899 
900 /*
901  * The DMA API client is passing in a scatterlist which could describe
902  * any old buffer layout, but the IOMMU API requires everything to be
903  * aligned to IOMMU pages. Hence the need for this complicated bit of
904  * impedance-matching, to be able to hand off a suitably-aligned list,
905  * but still preserve the original offsets and sizes for the caller.
906  */
iommu_dma_map_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)907 static int iommu_dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir,
908                             unsigned long attrs)
909 {
910     struct iommu_domain *domain = iommu_get_dma_domain(dev);
911     struct iommu_dma_cookie *cookie = domain->iova_cookie;
912     struct iova_domain *iovad = &cookie->iovad;
913     struct scatterlist *s, *prev = NULL;
914     int prot = dma_info_to_prot(dir, dev_is_dma_coherent(dev), attrs);
915     dma_addr_t iova;
916     size_t iova_len = 0;
917     unsigned long mask = dma_get_seg_boundary(dev);
918     int i;
919 
920     if (unlikely(iommu_dma_deferred_attach(dev, domain))) {
921         return 0;
922     }
923 
924     if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
925         iommu_dma_sync_sg_for_device(dev, sg, nents, dir);
926     }
927 
928     /*
929      * Work out how much IOVA space we need, and align the segments to
930      * IOVA granules for the IOMMU driver to handle. With some clever
931      * trickery we can modify the list in-place, but reversibly, by
932      * stashing the unaligned parts in the as-yet-unused DMA fields.
933      */
934     for_each_sg(sg, s, nents, i)
935     {
936         size_t s_iova_off = iova_offset(iovad, s->offset);
937         size_t s_length = s->length;
938         size_t pad_len = (mask - iova_len + 1) & mask;
939 
940         sg_dma_address(s) = s_iova_off;
941         sg_dma_len(s) = s_length;
942         s->offset -= s_iova_off;
943         s_length = iova_align(iovad, s_length + s_iova_off);
944         s->length = s_length;
945 
946         /*
947          * Due to the alignment of our single IOVA allocation, we can
948          * depend on these assumptions about the segment boundary mask:
949          * - If mask size >= IOVA size, then the IOVA range cannot
950          *   possibly fall across a boundary, so we don't care.
951          * - If mask size < IOVA size, then the IOVA range must start
952          *   exactly on a boundary, therefore we can lay things out
953          *   based purely on segment lengths without needing to know
954          *   the actual addresses beforehand.
955          * - The mask must be a power of 2, so pad_len == 0 if
956          *   iova_len == 0, thus we cannot dereference prev the first
957          *   time through here (i.e. before it has a meaningful value).
958          */
959         if (pad_len && pad_len < s_length - 1) {
960             prev->length += pad_len;
961             iova_len += pad_len;
962         }
963 
964         iova_len += s_length;
965         prev = s;
966     }
967 
968     iova = iommu_dma_alloc_iova(domain, iova_len, dma_get_mask(dev), dev);
969     if (!iova) {
970         goto out_restore_sg;
971     }
972 
973     /*
974      * We'll leave any physical concatenation to the IOMMU driver's
975      * implementation - it knows better than we do.
976      */
977     if (iommu_map_sg_atomic(domain, iova, sg, nents, prot) < iova_len) {
978         goto out_free_iova;
979     }
980 
981     return finalise_sg_ext(dev, sg, nents, iova);
982 
983 out_free_iova:
984     iommu_dma_free_iova(cookie, iova, iova_len);
985 out_restore_sg:
986     invalidate_sg_ext(sg, nents);
987     return 0;
988 }
989 
iommu_dma_unmap_sg(struct device * dev,struct scatterlist * sg,int nents,enum dma_data_direction dir,unsigned long attrs)990 static void iommu_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir,
991                                unsigned long attrs)
992 {
993     dma_addr_t start, end;
994     struct scatterlist *tmp;
995     int i;
996 
997     if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
998         iommu_dma_sync_sg_for_cpu(dev, sg, nents, dir);
999     }
1000 
1001     /*
1002      * The scatterlist segments are mapped into a single
1003      * contiguous IOVA allocation, so this is incredibly easy.
1004      */
1005     start = sg_dma_address(sg);
1006     for_each_sg(sg_next(sg), tmp, nents - 1, i)
1007     {
1008         if (sg_dma_len(tmp) == 0) {
1009             break;
1010         }
1011         sg = tmp;
1012     }
1013     end = sg_dma_address(sg) + sg_dma_len(sg);
1014     iommu_dma_unmap_ext(dev, start, end - start);
1015 }
1016 
iommu_dma_map_resource(struct device * dev,phys_addr_t phys,size_t size,enum dma_data_direction dir,unsigned long attrs)1017 static dma_addr_t iommu_dma_map_resource(struct device *dev, phys_addr_t phys, size_t size, enum dma_data_direction dir,
1018                                          unsigned long attrs)
1019 {
1020     return iommu_dma_map_ext(dev, phys, size, dma_info_to_prot(dir, false, attrs) | IOMMU_MMIO, dma_get_mask(dev));
1021 }
1022 
iommu_dma_unmap_resource(struct device * dev,dma_addr_t handle,size_t size,enum dma_data_direction dir,unsigned long attrs)1023 static void iommu_dma_unmap_resource(struct device *dev, dma_addr_t handle, size_t size, enum dma_data_direction dir,
1024                                      unsigned long attrs)
1025 {
1026     iommu_dma_unmap_ext(dev, handle, size);
1027 }
1028 
iommu_dma_free_ext(struct device * dev,size_t size,void * cpu_addr)1029 static void iommu_dma_free_ext(struct device *dev, size_t size, void *cpu_addr)
1030 {
1031     size_t alloc_size = PAGE_ALIGN(size);
1032     int count = alloc_size >> PAGE_SHIFT;
1033     struct page *page = NULL, **pages = NULL;
1034 
1035     /* Non-coherent atomic allocation? Easy */
1036     if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && dma_free_from_pool(dev, cpu_addr, alloc_size)) {
1037         return;
1038     }
1039 
1040     if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
1041         /*
1042          * If it the address is remapped, then it's either non-coherent
1043          * or highmem CMA, or an iommu_dma_alloc_remap() construction.
1044          */
1045         pages = dma_common_find_pages(cpu_addr);
1046         if (!pages) {
1047             page = vmalloc_to_page(cpu_addr);
1048         }
1049         dma_common_free_remap(cpu_addr, alloc_size);
1050     } else {
1051         /* Lowmem means a coherent atomic or CMA allocation */
1052         page = virt_to_page(cpu_addr);
1053     }
1054 
1055     if (pages) {
1056         iommu_dma_free_pages_ext(pages, count);
1057     }
1058     if (page) {
1059         dma_free_contiguous(dev, page, alloc_size);
1060     }
1061 }
1062 
iommu_dma_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle,unsigned long attrs)1063 static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle, unsigned long attrs)
1064 {
1065     iommu_dma_unmap_ext(dev, handle, size);
1066     iommu_dma_free_ext(dev, size, cpu_addr);
1067 }
1068 
iommu_dma_alloc_pages(struct device * dev,size_t size,struct page ** pagep,gfp_t gfp,unsigned long attrs)1069 static void *iommu_dma_alloc_pages(struct device *dev, size_t size, struct page **pagep, gfp_t gfp, unsigned long attrs)
1070 {
1071     bool coherent = dev_is_dma_coherent(dev);
1072     size_t alloc_size = PAGE_ALIGN(size);
1073     int node = dev_to_node(dev);
1074     struct page *page = NULL;
1075     void *cpu_addr;
1076 
1077     page = dma_alloc_contiguous(dev, alloc_size, gfp);
1078     if (!page) {
1079         page = alloc_pages_node(node, gfp, get_order(alloc_size));
1080     }
1081     if (!page) {
1082         return NULL;
1083     }
1084 
1085     if (IS_ENABLED(CONFIG_DMA_REMAP) && (!coherent || PageHighMem(page))) {
1086         pgprot_t prot = dma_pgprot(dev, PAGE_KERNEL, attrs);
1087 
1088         cpu_addr = dma_common_contiguous_remap(page, alloc_size, prot, __builtin_return_address(0));
1089         if (!cpu_addr) {
1090             goto out_free_pages;
1091         }
1092 
1093         if (!coherent) {
1094             arch_dma_prep_coherent(page, size);
1095         }
1096     } else {
1097         cpu_addr = page_address(page);
1098     }
1099 
1100     *pagep = page;
1101     memset(cpu_addr, 0, alloc_size);
1102     return cpu_addr;
1103 out_free_pages:
1104     dma_free_contiguous(dev, page, alloc_size);
1105     return NULL;
1106 }
1107 
iommu_dma_alloc(struct device * dev,size_t size,dma_addr_t * handle,gfp_t gfp,unsigned long attrs)1108 static void *iommu_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp, unsigned long attrs)
1109 {
1110     bool coherent = dev_is_dma_coherent(dev);
1111     int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs);
1112     struct page *page = NULL;
1113     void *cpu_addr;
1114 
1115     gfp |= __GFP_ZERO;
1116 
1117     if (IS_ENABLED(CONFIG_DMA_REMAP) && gfpflags_allow_blocking(gfp) && !(attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
1118         return iommu_dma_alloc_remap(dev, size, handle, gfp, dma_pgprot(dev, PAGE_KERNEL, attrs), attrs);
1119     }
1120 
1121     if (IS_ENABLED(CONFIG_DMA_DIRECT_REMAP) && !gfpflags_allow_blocking(gfp) && !coherent) {
1122         page = dma_alloc_from_pool(dev, PAGE_ALIGN(size), &cpu_addr, gfp, NULL);
1123     } else {
1124         cpu_addr = iommu_dma_alloc_pages(dev, size, &page, gfp, attrs);
1125     }
1126     if (!cpu_addr) {
1127         return NULL;
1128     }
1129 
1130     *handle = iommu_dma_map_ext(dev, page_to_phys(page), size, ioprot, dev->coherent_dma_mask);
1131     if (*handle == DMA_MAPPING_ERROR) {
1132         iommu_dma_free_ext(dev, size, cpu_addr);
1133         return NULL;
1134     }
1135 
1136     return cpu_addr;
1137 }
1138 
1139 #ifdef CONFIG_DMA_REMAP
iommu_dma_alloc_noncoherent(struct device * dev,size_t size,dma_addr_t * handle,enum dma_data_direction dir,gfp_t gfp)1140 static void *iommu_dma_alloc_noncoherent(struct device *dev, size_t size, dma_addr_t *handle,
1141                                          enum dma_data_direction dir, gfp_t gfp)
1142 {
1143     if (!gfpflags_allow_blocking(gfp)) {
1144         struct page *page;
1145 
1146         page = dma_common_alloc_pages(dev, size, handle, dir, gfp);
1147         if (!page) {
1148             return NULL;
1149         }
1150         return page_address(page);
1151     }
1152 
1153     return iommu_dma_alloc_remap(dev, size, handle, gfp | __GFP_ZERO, PAGE_KERNEL, 0);
1154 }
1155 
iommu_dma_free_noncoherent(struct device * dev,size_t size,void * cpu_addr,dma_addr_t handle,enum dma_data_direction dir)1156 static void iommu_dma_free_noncoherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle,
1157                                        enum dma_data_direction dir)
1158 {
1159     iommu_dma_unmap_ext(dev, handle, size);
1160     iommu_dma_free_ext(dev, size, cpu_addr);
1161 }
1162 #else
1163 #define iommu_dma_alloc_noncoherent NULL
1164 #define iommu_dma_free_noncoherent NULL
1165 #endif /* CONFIG_DMA_REMAP */
1166 
iommu_dma_mmap(struct device * dev,struct vm_area_struct * vma,void * cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs)1167 static int iommu_dma_mmap(struct device *dev, struct vm_area_struct *vma, void *cpu_addr, dma_addr_t dma_addr,
1168                           size_t size, unsigned long attrs)
1169 {
1170     unsigned long nr_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
1171     unsigned long pfn, off = vma->vm_pgoff;
1172     int ret;
1173 
1174     vma->vm_page_prot = dma_pgprot(dev, vma->vm_page_prot, attrs);
1175 
1176     if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret)) {
1177         return ret;
1178     }
1179 
1180     if (off >= nr_pages || vma_pages(vma) > nr_pages - off) {
1181         return -ENXIO;
1182     }
1183 
1184     if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
1185         struct page **pages = dma_common_find_pages(cpu_addr);
1186 
1187         if (pages) {
1188             return iommu_dma_mmap_ext(pages, size, vma);
1189         }
1190         pfn = vmalloc_to_pfn(cpu_addr);
1191     } else {
1192         pfn = page_to_pfn(virt_to_page(cpu_addr));
1193     }
1194 
1195     return remap_pfn_range(vma, vma->vm_start, pfn + off, vma->vm_end - vma->vm_start, vma->vm_page_prot);
1196 }
1197 
iommu_dma_get_sgtable(struct device * dev,struct sg_table * sgt,void * cpu_addr,dma_addr_t dma_addr,size_t size,unsigned long attrs)1198 static int iommu_dma_get_sgtable(struct device *dev, struct sg_table *sgt, void *cpu_addr, dma_addr_t dma_addr,
1199                                  size_t size, unsigned long attrs)
1200 {
1201     struct page *page;
1202     int ret;
1203 
1204     if (IS_ENABLED(CONFIG_DMA_REMAP) && is_vmalloc_addr(cpu_addr)) {
1205         struct page **pages = dma_common_find_pages(cpu_addr);
1206 
1207         if (pages) {
1208             return sg_alloc_table_from_pages(sgt, pages, PAGE_ALIGN(size) >> PAGE_SHIFT, 0, size, GFP_KERNEL);
1209         }
1210 
1211         page = vmalloc_to_page(cpu_addr);
1212     } else {
1213         page = virt_to_page(cpu_addr);
1214     }
1215 
1216     ret = sg_alloc_table(sgt, 1, GFP_KERNEL);
1217     if (!ret) {
1218         sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0);
1219     }
1220     return ret;
1221 }
1222 
iommu_dma_get_merge_boundary(struct device * dev)1223 static unsigned long iommu_dma_get_merge_boundary(struct device *dev)
1224 {
1225     struct iommu_domain *domain = iommu_get_dma_domain(dev);
1226 
1227     return (1UL << __ffs(domain->pgsize_bitmap)) - 1;
1228 }
1229 
1230 static const struct dma_map_ops iommu_dma_ops = {
1231     .alloc = iommu_dma_alloc,
1232     .free = iommu_dma_free,
1233     .alloc_pages = dma_common_alloc_pages,
1234     .free_pages = dma_common_free_pages,
1235     .alloc_noncoherent = iommu_dma_alloc_noncoherent,
1236     .free_noncoherent = iommu_dma_free_noncoherent,
1237     .mmap = iommu_dma_mmap,
1238     .get_sgtable = iommu_dma_get_sgtable,
1239     .map_page = iommu_dma_map_page,
1240     .unmap_page = iommu_dma_unmap_page,
1241     .map_sg = iommu_dma_map_sg,
1242     .unmap_sg = iommu_dma_unmap_sg,
1243     .sync_single_for_cpu = iommu_dma_sync_single_for_cpu,
1244     .sync_single_for_device = iommu_dma_sync_single_for_device,
1245     .sync_sg_for_cpu = iommu_dma_sync_sg_for_cpu,
1246     .sync_sg_for_device = iommu_dma_sync_sg_for_device,
1247     .map_resource = iommu_dma_map_resource,
1248     .unmap_resource = iommu_dma_unmap_resource,
1249     .get_merge_boundary = iommu_dma_get_merge_boundary,
1250 };
1251 
1252 /*
1253  * The IOMMU core code allocates the default DMA domain, which the underlying
1254  * IOMMU driver needs to support via the dma-iommu layer.
1255  */
iommu_setup_dma_ops(struct device * dev,u64 dma_base,u64 size)1256 void iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size)
1257 {
1258     struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1259 
1260     if (!domain) {
1261         goto out_err;
1262     }
1263 
1264     /*
1265      * The IOMMU core code allocates the default DMA domain, which the
1266      * underlying IOMMU driver needs to support via the dma-iommu layer.
1267      */
1268     if (domain->type == IOMMU_DOMAIN_DMA) {
1269         if (iommu_dma_init_domain(domain, dma_base, size, dev)) {
1270             goto out_err;
1271         }
1272         dev->dma_ops = &iommu_dma_ops;
1273     }
1274 
1275     return;
1276 out_err:
1277     pr_warn("Failed to set up IOMMU for device %s; retaining platform DMA ops\n", dev_name(dev));
1278 }
1279 
iommu_dma_get_msi_page(struct device * dev,phys_addr_t msi_addr,struct iommu_domain * domain)1280 static struct iommu_dma_msi_page *iommu_dma_get_msi_page(struct device *dev, phys_addr_t msi_addr,
1281                                                          struct iommu_domain *domain)
1282 {
1283     struct iommu_dma_cookie *cookie = domain->iova_cookie;
1284     struct iommu_dma_msi_page *msi_page;
1285     dma_addr_t iova;
1286     int prot = IOMMU_WRITE | IOMMU_NOEXEC | IOMMU_MMIO;
1287     size_t size = cookie_msi_granule(cookie);
1288 
1289     msi_addr &= ~(phys_addr_t)(size - 1);
1290     list_for_each_entry(msi_page, &cookie->msi_page_list, list) if (msi_page->phys == msi_addr) return msi_page;
1291 
1292     msi_page = kzalloc(sizeof(*msi_page), GFP_KERNEL);
1293     if (!msi_page) {
1294         return NULL;
1295     }
1296 
1297     iova = iommu_dma_alloc_iova(domain, size, dma_get_mask(dev), dev);
1298     if (!iova) {
1299         goto out_free_page;
1300     }
1301 
1302     if (iommu_map(domain, iova, msi_addr, size, prot)) {
1303         goto out_free_iova;
1304     }
1305 
1306     INIT_LIST_HEAD(&msi_page->list);
1307     msi_page->phys = msi_addr;
1308     msi_page->iova = iova;
1309     list_add(&msi_page->list, &cookie->msi_page_list);
1310     return msi_page;
1311 
1312 out_free_iova:
1313     iommu_dma_free_iova(cookie, iova, size);
1314 out_free_page:
1315     kfree(msi_page);
1316     return NULL;
1317 }
1318 
iommu_dma_prepare_msi(struct msi_desc * desc,phys_addr_t msi_addr)1319 int iommu_dma_prepare_msi(struct msi_desc *desc, phys_addr_t msi_addr)
1320 {
1321     struct device *dev = msi_desc_to_dev(desc);
1322     struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1323     struct iommu_dma_msi_page *msi_page;
1324     static DEFINE_MUTEX(msi_prepare_lock); /* see below */
1325 
1326     if (!domain || !domain->iova_cookie) {
1327         desc->iommu_cookie = NULL;
1328         return 0;
1329     }
1330 
1331     /*
1332      * In fact the whole prepare operation should already be serialised by
1333      * irq_domain_mutex further up the callchain, but that's pretty subtle
1334      * on its own, so consider this locking as failsafe documentation...
1335      */
1336     mutex_lock(&msi_prepare_lock);
1337     msi_page = iommu_dma_get_msi_page(dev, msi_addr, domain);
1338     mutex_unlock(&msi_prepare_lock);
1339 
1340     msi_desc_set_iommu_cookie(desc, msi_page);
1341 
1342     if (!msi_page) {
1343         return -ENOMEM;
1344     }
1345     return 0;
1346 }
1347 
iommu_dma_compose_msi_msg(struct msi_desc * desc,struct msi_msg * msg)1348 void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
1349 {
1350     struct device *dev = msi_desc_to_dev(desc);
1351     const struct iommu_domain *domain = iommu_get_domain_for_dev(dev);
1352     const struct iommu_dma_msi_page *msi_page;
1353 
1354     msi_page = msi_desc_get_iommu_cookie(desc);
1355     if (!domain || !domain->iova_cookie || WARN_ON(!msi_page)) {
1356         return;
1357     }
1358 
1359     msg->address_hi = upper_32_bits(msi_page->iova);
1360     msg->address_lo &= cookie_msi_granule(domain->iova_cookie) - 1;
1361     msg->address_lo += lower_32_bits(msi_page->iova);
1362 }
1363 
iommu_dma_init(void)1364 static int iommu_dma_init(void)
1365 {
1366     return iova_cache_get();
1367 }
1368 arch_initcall(iommu_dma_init);
1369