memblock: replace free_bootmem{_node} with memblock_free
[linux-2.6-block.git] / drivers / xen / swiotlb-xen.c
1 /*
2  *  Copyright 2010
3  *  by Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
4  *
5  * This code provides a IOMMU for Xen PV guests with PCI passthrough.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License v2.0 as published by
9  * the Free Software Foundation
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * PV guests under Xen are running in an non-contiguous memory architecture.
17  *
18  * When PCI pass-through is utilized, this necessitates an IOMMU for
19  * translating bus (DMA) to virtual and vice-versa and also providing a
20  * mechanism to have contiguous pages for device drivers operations (say DMA
21  * operations).
22  *
23  * Specifically, under Xen the Linux idea of pages is an illusion. It
24  * assumes that pages start at zero and go up to the available memory. To
25  * help with that, the Linux Xen MMU provides a lookup mechanism to
26  * translate the page frame numbers (PFN) to machine frame numbers (MFN)
27  * and vice-versa. The MFN are the "real" frame numbers. Furthermore
28  * memory is not contiguous. Xen hypervisor stitches memory for guests
29  * from different pools, which means there is no guarantee that PFN==MFN
30  * and PFN+1==MFN+1. Lastly with Xen 4.0, pages (in debug mode) are
31  * allocated in descending order (high to low), meaning the guest might
32  * never get any MFN's under the 4GB mark.
33  *
34  */
35
36 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
37
38 #include <linux/bootmem.h>
39 #include <linux/memblock.h>
40 #include <linux/dma-direct.h>
41 #include <linux/export.h>
42 #include <xen/swiotlb-xen.h>
43 #include <xen/page.h>
44 #include <xen/xen-ops.h>
45 #include <xen/hvc-console.h>
46
47 #include <asm/dma-mapping.h>
48 #include <asm/xen/page-coherent.h>
49
50 #include <trace/events/swiotlb.h>
51 /*
52  * Used to do a quick range check in swiotlb_tbl_unmap_single and
53  * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this
54  * API.
55  */
56
57 #define XEN_SWIOTLB_ERROR_CODE  (~(dma_addr_t)0x0)
58
59 static char *xen_io_tlb_start, *xen_io_tlb_end;
60 static unsigned long xen_io_tlb_nslabs;
61 /*
62  * Quick lookup value of the bus address of the IOTLB.
63  */
64
65 static u64 start_dma_addr;
66
67 /*
68  * Both of these functions should avoid XEN_PFN_PHYS because phys_addr_t
69  * can be 32bit when dma_addr_t is 64bit leading to a loss in
70  * information if the shift is done before casting to 64bit.
71  */
72 static inline dma_addr_t xen_phys_to_bus(phys_addr_t paddr)
73 {
74         unsigned long bfn = pfn_to_bfn(XEN_PFN_DOWN(paddr));
75         dma_addr_t dma = (dma_addr_t)bfn << XEN_PAGE_SHIFT;
76
77         dma |= paddr & ~XEN_PAGE_MASK;
78
79         return dma;
80 }
81
82 static inline phys_addr_t xen_bus_to_phys(dma_addr_t baddr)
83 {
84         unsigned long xen_pfn = bfn_to_pfn(XEN_PFN_DOWN(baddr));
85         dma_addr_t dma = (dma_addr_t)xen_pfn << XEN_PAGE_SHIFT;
86         phys_addr_t paddr = dma;
87
88         paddr |= baddr & ~XEN_PAGE_MASK;
89
90         return paddr;
91 }
92
93 static inline dma_addr_t xen_virt_to_bus(void *address)
94 {
95         return xen_phys_to_bus(virt_to_phys(address));
96 }
97
98 static int check_pages_physically_contiguous(unsigned long xen_pfn,
99                                              unsigned int offset,
100                                              size_t length)
101 {
102         unsigned long next_bfn;
103         int i;
104         int nr_pages;
105
106         next_bfn = pfn_to_bfn(xen_pfn);
107         nr_pages = (offset + length + XEN_PAGE_SIZE-1) >> XEN_PAGE_SHIFT;
108
109         for (i = 1; i < nr_pages; i++) {
110                 if (pfn_to_bfn(++xen_pfn) != ++next_bfn)
111                         return 0;
112         }
113         return 1;
114 }
115
116 static inline int range_straddles_page_boundary(phys_addr_t p, size_t size)
117 {
118         unsigned long xen_pfn = XEN_PFN_DOWN(p);
119         unsigned int offset = p & ~XEN_PAGE_MASK;
120
121         if (offset + size <= XEN_PAGE_SIZE)
122                 return 0;
123         if (check_pages_physically_contiguous(xen_pfn, offset, size))
124                 return 0;
125         return 1;
126 }
127
128 static int is_xen_swiotlb_buffer(dma_addr_t dma_addr)
129 {
130         unsigned long bfn = XEN_PFN_DOWN(dma_addr);
131         unsigned long xen_pfn = bfn_to_local_pfn(bfn);
132         phys_addr_t paddr = XEN_PFN_PHYS(xen_pfn);
133
134         /* If the address is outside our domain, it CAN
135          * have the same virtual address as another address
136          * in our domain. Therefore _only_ check address within our domain.
137          */
138         if (pfn_valid(PFN_DOWN(paddr))) {
139                 return paddr >= virt_to_phys(xen_io_tlb_start) &&
140                        paddr < virt_to_phys(xen_io_tlb_end);
141         }
142         return 0;
143 }
144
145 static int max_dma_bits = 32;
146
147 static int
148 xen_swiotlb_fixup(void *buf, size_t size, unsigned long nslabs)
149 {
150         int i, rc;
151         int dma_bits;
152         dma_addr_t dma_handle;
153         phys_addr_t p = virt_to_phys(buf);
154
155         dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT;
156
157         i = 0;
158         do {
159                 int slabs = min(nslabs - i, (unsigned long)IO_TLB_SEGSIZE);
160
161                 do {
162                         rc = xen_create_contiguous_region(
163                                 p + (i << IO_TLB_SHIFT),
164                                 get_order(slabs << IO_TLB_SHIFT),
165                                 dma_bits, &dma_handle);
166                 } while (rc && dma_bits++ < max_dma_bits);
167                 if (rc)
168                         return rc;
169
170                 i += slabs;
171         } while (i < nslabs);
172         return 0;
173 }
174 static unsigned long xen_set_nslabs(unsigned long nr_tbl)
175 {
176         if (!nr_tbl) {
177                 xen_io_tlb_nslabs = (64 * 1024 * 1024 >> IO_TLB_SHIFT);
178                 xen_io_tlb_nslabs = ALIGN(xen_io_tlb_nslabs, IO_TLB_SEGSIZE);
179         } else
180                 xen_io_tlb_nslabs = nr_tbl;
181
182         return xen_io_tlb_nslabs << IO_TLB_SHIFT;
183 }
184
185 enum xen_swiotlb_err {
186         XEN_SWIOTLB_UNKNOWN = 0,
187         XEN_SWIOTLB_ENOMEM,
188         XEN_SWIOTLB_EFIXUP
189 };
190
191 static const char *xen_swiotlb_error(enum xen_swiotlb_err err)
192 {
193         switch (err) {
194         case XEN_SWIOTLB_ENOMEM:
195                 return "Cannot allocate Xen-SWIOTLB buffer\n";
196         case XEN_SWIOTLB_EFIXUP:
197                 return "Failed to get contiguous memory for DMA from Xen!\n"\
198                     "You either: don't have the permissions, do not have"\
199                     " enough free memory under 4GB, or the hypervisor memory"\
200                     " is too fragmented!";
201         default:
202                 break;
203         }
204         return "";
205 }
206 int __ref xen_swiotlb_init(int verbose, bool early)
207 {
208         unsigned long bytes, order;
209         int rc = -ENOMEM;
210         enum xen_swiotlb_err m_ret = XEN_SWIOTLB_UNKNOWN;
211         unsigned int repeat = 3;
212
213         xen_io_tlb_nslabs = swiotlb_nr_tbl();
214 retry:
215         bytes = xen_set_nslabs(xen_io_tlb_nslabs);
216         order = get_order(xen_io_tlb_nslabs << IO_TLB_SHIFT);
217         /*
218          * Get IO TLB memory from any location.
219          */
220         if (early)
221                 xen_io_tlb_start = memblock_alloc(PAGE_ALIGN(bytes),
222                                                   PAGE_SIZE);
223         else {
224 #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
225 #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
226                 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
227                         xen_io_tlb_start = (void *)xen_get_swiotlb_free_pages(order);
228                         if (xen_io_tlb_start)
229                                 break;
230                         order--;
231                 }
232                 if (order != get_order(bytes)) {
233                         pr_warn("Warning: only able to allocate %ld MB for software IO TLB\n",
234                                 (PAGE_SIZE << order) >> 20);
235                         xen_io_tlb_nslabs = SLABS_PER_PAGE << order;
236                         bytes = xen_io_tlb_nslabs << IO_TLB_SHIFT;
237                 }
238         }
239         if (!xen_io_tlb_start) {
240                 m_ret = XEN_SWIOTLB_ENOMEM;
241                 goto error;
242         }
243         xen_io_tlb_end = xen_io_tlb_start + bytes;
244         /*
245          * And replace that memory with pages under 4GB.
246          */
247         rc = xen_swiotlb_fixup(xen_io_tlb_start,
248                                bytes,
249                                xen_io_tlb_nslabs);
250         if (rc) {
251                 if (early)
252                         memblock_free(__pa(xen_io_tlb_start),
253                                       PAGE_ALIGN(bytes));
254                 else {
255                         free_pages((unsigned long)xen_io_tlb_start, order);
256                         xen_io_tlb_start = NULL;
257                 }
258                 m_ret = XEN_SWIOTLB_EFIXUP;
259                 goto error;
260         }
261         start_dma_addr = xen_virt_to_bus(xen_io_tlb_start);
262         if (early) {
263                 if (swiotlb_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs,
264                          verbose))
265                         panic("Cannot allocate SWIOTLB buffer");
266                 rc = 0;
267         } else
268                 rc = swiotlb_late_init_with_tbl(xen_io_tlb_start, xen_io_tlb_nslabs);
269
270         if (!rc)
271                 swiotlb_set_max_segment(PAGE_SIZE);
272
273         return rc;
274 error:
275         if (repeat--) {
276                 xen_io_tlb_nslabs = max(1024UL, /* Min is 2MB */
277                                         (xen_io_tlb_nslabs >> 1));
278                 pr_info("Lowering to %luMB\n",
279                         (xen_io_tlb_nslabs << IO_TLB_SHIFT) >> 20);
280                 goto retry;
281         }
282         pr_err("%s (rc:%d)\n", xen_swiotlb_error(m_ret), rc);
283         if (early)
284                 panic("%s (rc:%d)", xen_swiotlb_error(m_ret), rc);
285         else
286                 free_pages((unsigned long)xen_io_tlb_start, order);
287         return rc;
288 }
289
290 static void *
291 xen_swiotlb_alloc_coherent(struct device *hwdev, size_t size,
292                            dma_addr_t *dma_handle, gfp_t flags,
293                            unsigned long attrs)
294 {
295         void *ret;
296         int order = get_order(size);
297         u64 dma_mask = DMA_BIT_MASK(32);
298         phys_addr_t phys;
299         dma_addr_t dev_addr;
300
301         /*
302         * Ignore region specifiers - the kernel's ideas of
303         * pseudo-phys memory layout has nothing to do with the
304         * machine physical layout.  We can't allocate highmem
305         * because we can't return a pointer to it.
306         */
307         flags &= ~(__GFP_DMA | __GFP_HIGHMEM);
308
309         /* Convert the size to actually allocated. */
310         size = 1UL << (order + XEN_PAGE_SHIFT);
311
312         /* On ARM this function returns an ioremap'ped virtual address for
313          * which virt_to_phys doesn't return the corresponding physical
314          * address. In fact on ARM virt_to_phys only works for kernel direct
315          * mapped RAM memory. Also see comment below.
316          */
317         ret = xen_alloc_coherent_pages(hwdev, size, dma_handle, flags, attrs);
318
319         if (!ret)
320                 return ret;
321
322         if (hwdev && hwdev->coherent_dma_mask)
323                 dma_mask = hwdev->coherent_dma_mask;
324
325         /* At this point dma_handle is the physical address, next we are
326          * going to set it to the machine address.
327          * Do not use virt_to_phys(ret) because on ARM it doesn't correspond
328          * to *dma_handle. */
329         phys = *dma_handle;
330         dev_addr = xen_phys_to_bus(phys);
331         if (((dev_addr + size - 1 <= dma_mask)) &&
332             !range_straddles_page_boundary(phys, size))
333                 *dma_handle = dev_addr;
334         else {
335                 if (xen_create_contiguous_region(phys, order,
336                                                  fls64(dma_mask), dma_handle) != 0) {
337                         xen_free_coherent_pages(hwdev, size, ret, (dma_addr_t)phys, attrs);
338                         return NULL;
339                 }
340         }
341         memset(ret, 0, size);
342         return ret;
343 }
344
345 static void
346 xen_swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
347                           dma_addr_t dev_addr, unsigned long attrs)
348 {
349         int order = get_order(size);
350         phys_addr_t phys;
351         u64 dma_mask = DMA_BIT_MASK(32);
352
353         if (hwdev && hwdev->coherent_dma_mask)
354                 dma_mask = hwdev->coherent_dma_mask;
355
356         /* do not use virt_to_phys because on ARM it doesn't return you the
357          * physical address */
358         phys = xen_bus_to_phys(dev_addr);
359
360         /* Convert the size to actually allocated. */
361         size = 1UL << (order + XEN_PAGE_SHIFT);
362
363         if (((dev_addr + size - 1 <= dma_mask)) ||
364             range_straddles_page_boundary(phys, size))
365                 xen_destroy_contiguous_region(phys, order);
366
367         xen_free_coherent_pages(hwdev, size, vaddr, (dma_addr_t)phys, attrs);
368 }
369
370 /*
371  * Map a single buffer of the indicated size for DMA in streaming mode.  The
372  * physical address to use is returned.
373  *
374  * Once the device is given the dma address, the device owns this memory until
375  * either xen_swiotlb_unmap_page or xen_swiotlb_dma_sync_single is performed.
376  */
377 static dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page,
378                                 unsigned long offset, size_t size,
379                                 enum dma_data_direction dir,
380                                 unsigned long attrs)
381 {
382         phys_addr_t map, phys = page_to_phys(page) + offset;
383         dma_addr_t dev_addr = xen_phys_to_bus(phys);
384
385         BUG_ON(dir == DMA_NONE);
386         /*
387          * If the address happens to be in the device's DMA window,
388          * we can safely return the device addr and not worry about bounce
389          * buffering it.
390          */
391         if (dma_capable(dev, dev_addr, size) &&
392             !range_straddles_page_boundary(phys, size) &&
393                 !xen_arch_need_swiotlb(dev, phys, dev_addr) &&
394                 (swiotlb_force != SWIOTLB_FORCE)) {
395                 /* we are not interested in the dma_addr returned by
396                  * xen_dma_map_page, only in the potential cache flushes executed
397                  * by the function. */
398                 xen_dma_map_page(dev, page, dev_addr, offset, size, dir, attrs);
399                 return dev_addr;
400         }
401
402         /*
403          * Oh well, have to allocate and map a bounce buffer.
404          */
405         trace_swiotlb_bounced(dev, dev_addr, size, swiotlb_force);
406
407         map = swiotlb_tbl_map_single(dev, start_dma_addr, phys, size, dir,
408                                      attrs);
409         if (map == SWIOTLB_MAP_ERROR)
410                 return XEN_SWIOTLB_ERROR_CODE;
411
412         dev_addr = xen_phys_to_bus(map);
413         xen_dma_map_page(dev, pfn_to_page(map >> PAGE_SHIFT),
414                                         dev_addr, map & ~PAGE_MASK, size, dir, attrs);
415
416         /*
417          * Ensure that the address returned is DMA'ble
418          */
419         if (dma_capable(dev, dev_addr, size))
420                 return dev_addr;
421
422         attrs |= DMA_ATTR_SKIP_CPU_SYNC;
423         swiotlb_tbl_unmap_single(dev, map, size, dir, attrs);
424
425         return XEN_SWIOTLB_ERROR_CODE;
426 }
427
428 /*
429  * Unmap a single streaming mode DMA translation.  The dma_addr and size must
430  * match what was provided for in a previous xen_swiotlb_map_page call.  All
431  * other usages are undefined.
432  *
433  * After this call, reads by the cpu to the buffer are guaranteed to see
434  * whatever the device wrote there.
435  */
436 static void xen_unmap_single(struct device *hwdev, dma_addr_t dev_addr,
437                              size_t size, enum dma_data_direction dir,
438                              unsigned long attrs)
439 {
440         phys_addr_t paddr = xen_bus_to_phys(dev_addr);
441
442         BUG_ON(dir == DMA_NONE);
443
444         xen_dma_unmap_page(hwdev, dev_addr, size, dir, attrs);
445
446         /* NOTE: We use dev_addr here, not paddr! */
447         if (is_xen_swiotlb_buffer(dev_addr)) {
448                 swiotlb_tbl_unmap_single(hwdev, paddr, size, dir, attrs);
449                 return;
450         }
451
452         if (dir != DMA_FROM_DEVICE)
453                 return;
454
455         /*
456          * phys_to_virt doesn't work with hihgmem page but we could
457          * call dma_mark_clean() with hihgmem page here. However, we
458          * are fine since dma_mark_clean() is null on POWERPC. We can
459          * make dma_mark_clean() take a physical address if necessary.
460          */
461         dma_mark_clean(phys_to_virt(paddr), size);
462 }
463
464 static void xen_swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,
465                             size_t size, enum dma_data_direction dir,
466                             unsigned long attrs)
467 {
468         xen_unmap_single(hwdev, dev_addr, size, dir, attrs);
469 }
470
471 /*
472  * Make physical memory consistent for a single streaming mode DMA translation
473  * after a transfer.
474  *
475  * If you perform a xen_swiotlb_map_page() but wish to interrogate the buffer
476  * using the cpu, yet do not wish to teardown the dma mapping, you must
477  * call this function before doing so.  At the next point you give the dma
478  * address back to the card, you must first perform a
479  * xen_swiotlb_dma_sync_for_device, and then the device again owns the buffer
480  */
481 static void
482 xen_swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
483                         size_t size, enum dma_data_direction dir,
484                         enum dma_sync_target target)
485 {
486         phys_addr_t paddr = xen_bus_to_phys(dev_addr);
487
488         BUG_ON(dir == DMA_NONE);
489
490         if (target == SYNC_FOR_CPU)
491                 xen_dma_sync_single_for_cpu(hwdev, dev_addr, size, dir);
492
493         /* NOTE: We use dev_addr here, not paddr! */
494         if (is_xen_swiotlb_buffer(dev_addr))
495                 swiotlb_tbl_sync_single(hwdev, paddr, size, dir, target);
496
497         if (target == SYNC_FOR_DEVICE)
498                 xen_dma_sync_single_for_device(hwdev, dev_addr, size, dir);
499
500         if (dir != DMA_FROM_DEVICE)
501                 return;
502
503         dma_mark_clean(phys_to_virt(paddr), size);
504 }
505
506 void
507 xen_swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
508                                 size_t size, enum dma_data_direction dir)
509 {
510         xen_swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
511 }
512
513 void
514 xen_swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
515                                    size_t size, enum dma_data_direction dir)
516 {
517         xen_swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
518 }
519
520 /*
521  * Unmap a set of streaming mode DMA translations.  Again, cpu read rules
522  * concerning calls here are the same as for swiotlb_unmap_page() above.
523  */
524 static void
525 xen_swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
526                            int nelems, enum dma_data_direction dir,
527                            unsigned long attrs)
528 {
529         struct scatterlist *sg;
530         int i;
531
532         BUG_ON(dir == DMA_NONE);
533
534         for_each_sg(sgl, sg, nelems, i)
535                 xen_unmap_single(hwdev, sg->dma_address, sg_dma_len(sg), dir, attrs);
536
537 }
538
539 /*
540  * Map a set of buffers described by scatterlist in streaming mode for DMA.
541  * This is the scatter-gather version of the above xen_swiotlb_map_page
542  * interface.  Here the scatter gather list elements are each tagged with the
543  * appropriate dma address and length.  They are obtained via
544  * sg_dma_{address,length}(SG).
545  *
546  * NOTE: An implementation may be able to use a smaller number of
547  *       DMA address/length pairs than there are SG table elements.
548  *       (for example via virtual mapping capabilities)
549  *       The routine returns the number of addr/length pairs actually
550  *       used, at most nents.
551  *
552  * Device ownership issues as mentioned above for xen_swiotlb_map_page are the
553  * same here.
554  */
555 static int
556 xen_swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
557                          int nelems, enum dma_data_direction dir,
558                          unsigned long attrs)
559 {
560         struct scatterlist *sg;
561         int i;
562
563         BUG_ON(dir == DMA_NONE);
564
565         for_each_sg(sgl, sg, nelems, i) {
566                 phys_addr_t paddr = sg_phys(sg);
567                 dma_addr_t dev_addr = xen_phys_to_bus(paddr);
568
569                 if (swiotlb_force == SWIOTLB_FORCE ||
570                     xen_arch_need_swiotlb(hwdev, paddr, dev_addr) ||
571                     !dma_capable(hwdev, dev_addr, sg->length) ||
572                     range_straddles_page_boundary(paddr, sg->length)) {
573                         phys_addr_t map = swiotlb_tbl_map_single(hwdev,
574                                                                  start_dma_addr,
575                                                                  sg_phys(sg),
576                                                                  sg->length,
577                                                                  dir, attrs);
578                         if (map == SWIOTLB_MAP_ERROR) {
579                                 dev_warn(hwdev, "swiotlb buffer is full\n");
580                                 /* Don't panic here, we expect map_sg users
581                                    to do proper error handling. */
582                                 attrs |= DMA_ATTR_SKIP_CPU_SYNC;
583                                 xen_swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
584                                                            attrs);
585                                 sg_dma_len(sgl) = 0;
586                                 return 0;
587                         }
588                         dev_addr = xen_phys_to_bus(map);
589                         xen_dma_map_page(hwdev, pfn_to_page(map >> PAGE_SHIFT),
590                                                 dev_addr,
591                                                 map & ~PAGE_MASK,
592                                                 sg->length,
593                                                 dir,
594                                                 attrs);
595                         sg->dma_address = dev_addr;
596                 } else {
597                         /* we are not interested in the dma_addr returned by
598                          * xen_dma_map_page, only in the potential cache flushes executed
599                          * by the function. */
600                         xen_dma_map_page(hwdev, pfn_to_page(paddr >> PAGE_SHIFT),
601                                                 dev_addr,
602                                                 paddr & ~PAGE_MASK,
603                                                 sg->length,
604                                                 dir,
605                                                 attrs);
606                         sg->dma_address = dev_addr;
607                 }
608                 sg_dma_len(sg) = sg->length;
609         }
610         return nelems;
611 }
612
613 /*
614  * Make physical memory consistent for a set of streaming mode DMA translations
615  * after a transfer.
616  *
617  * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
618  * and usage.
619  */
620 static void
621 xen_swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
622                     int nelems, enum dma_data_direction dir,
623                     enum dma_sync_target target)
624 {
625         struct scatterlist *sg;
626         int i;
627
628         for_each_sg(sgl, sg, nelems, i)
629                 xen_swiotlb_sync_single(hwdev, sg->dma_address,
630                                         sg_dma_len(sg), dir, target);
631 }
632
633 static void
634 xen_swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
635                             int nelems, enum dma_data_direction dir)
636 {
637         xen_swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
638 }
639
640 static void
641 xen_swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
642                                int nelems, enum dma_data_direction dir)
643 {
644         xen_swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
645 }
646
647 /*
648  * Return whether the given device DMA address mask can be supported
649  * properly.  For example, if your device can only drive the low 24-bits
650  * during bus mastering, then you would pass 0x00ffffff as the mask to
651  * this function.
652  */
653 static int
654 xen_swiotlb_dma_supported(struct device *hwdev, u64 mask)
655 {
656         return xen_virt_to_bus(xen_io_tlb_end - 1) <= mask;
657 }
658
659 /*
660  * Create userspace mapping for the DMA-coherent memory.
661  * This function should be called with the pages from the current domain only,
662  * passing pages mapped from other domains would lead to memory corruption.
663  */
664 static int
665 xen_swiotlb_dma_mmap(struct device *dev, struct vm_area_struct *vma,
666                      void *cpu_addr, dma_addr_t dma_addr, size_t size,
667                      unsigned long attrs)
668 {
669 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
670         if (xen_get_dma_ops(dev)->mmap)
671                 return xen_get_dma_ops(dev)->mmap(dev, vma, cpu_addr,
672                                                     dma_addr, size, attrs);
673 #endif
674         return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
675 }
676
677 /*
678  * This function should be called with the pages from the current domain only,
679  * passing pages mapped from other domains would lead to memory corruption.
680  */
681 static int
682 xen_swiotlb_get_sgtable(struct device *dev, struct sg_table *sgt,
683                         void *cpu_addr, dma_addr_t handle, size_t size,
684                         unsigned long attrs)
685 {
686 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
687         if (xen_get_dma_ops(dev)->get_sgtable) {
688 #if 0
689         /*
690          * This check verifies that the page belongs to the current domain and
691          * is not one mapped from another domain.
692          * This check is for debug only, and should not go to production build
693          */
694                 unsigned long bfn = PHYS_PFN(dma_to_phys(dev, handle));
695                 BUG_ON (!page_is_ram(bfn));
696 #endif
697                 return xen_get_dma_ops(dev)->get_sgtable(dev, sgt, cpu_addr,
698                                                            handle, size, attrs);
699         }
700 #endif
701         return dma_common_get_sgtable(dev, sgt, cpu_addr, handle, size, attrs);
702 }
703
704 static int xen_swiotlb_mapping_error(struct device *dev, dma_addr_t dma_addr)
705 {
706         return dma_addr == XEN_SWIOTLB_ERROR_CODE;
707 }
708
709 const struct dma_map_ops xen_swiotlb_dma_ops = {
710         .alloc = xen_swiotlb_alloc_coherent,
711         .free = xen_swiotlb_free_coherent,
712         .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu,
713         .sync_single_for_device = xen_swiotlb_sync_single_for_device,
714         .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu,
715         .sync_sg_for_device = xen_swiotlb_sync_sg_for_device,
716         .map_sg = xen_swiotlb_map_sg_attrs,
717         .unmap_sg = xen_swiotlb_unmap_sg_attrs,
718         .map_page = xen_swiotlb_map_page,
719         .unmap_page = xen_swiotlb_unmap_page,
720         .dma_supported = xen_swiotlb_dma_supported,
721         .mmap = xen_swiotlb_dma_mmap,
722         .get_sgtable = xen_swiotlb_get_sgtable,
723         .mapping_error  = xen_swiotlb_mapping_error,
724 };