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