Added patch headers.
[linux-flexiantxendom0-3.2.10.git] / lib / swiotlb-xen.c
1 /*
2  * Dynamic DMA mapping support.
3  *
4  * This implementation is a fallback for platforms that do not support
5  * I/O TLBs (aka DMA address translation hardware).
6  * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
7  * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
8  * Copyright (C) 2000, 2003 Hewlett-Packard Co
9  *      David Mosberger-Tang <davidm@hpl.hp.com>
10  * Copyright (C) 2005 Keir Fraser <keir@xensource.com>
11  * 08/12/11 beckyb      Add highmem support
12  */
13
14 #include <linux/cache.h>
15 #include <linux/mm.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/spinlock.h>
19 #include <linux/string.h>
20 #include <linux/swiotlb.h>
21 #include <linux/pfn.h>
22 #include <linux/types.h>
23 #include <linux/ctype.h>
24 #include <linux/init.h>
25 #include <linux/bootmem.h>
26 #include <linux/iommu-helper.h>
27 #include <linux/highmem.h>
28 #include <linux/gfp.h>
29
30 #include <asm/io.h>
31 #include <asm/pci.h>
32 #include <asm/dma.h>
33 #include <asm/uaccess.h>
34 #include <xen/gnttab.h>
35 #include <xen/interface/memory.h>
36 #include <asm/gnttab_dma.h>
37
38 #define OFFSET(val,align) ((unsigned long)((val) & ( (align) - 1)))
39
40 int swiotlb;
41 int swiotlb_force;
42
43 /*
44  * Used to do a quick range check in unmap_single and
45  * sync_single_*, to see if the memory was in fact allocated by this
46  * API.
47  */
48 static char *io_tlb_start, *io_tlb_end;
49
50 /*
51  * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
52  * io_tlb_end.  This is command line adjustable via setup_io_tlb_npages.
53  */
54 static unsigned long io_tlb_nslabs;
55
56 /*
57  * When the IOMMU overflows we return a fallback buffer. This sets the size.
58  */
59 static unsigned long io_tlb_overflow = 32*1024;
60
61 void *io_tlb_overflow_buffer;
62
63 /*
64  * This is a free list describing the number of free entries available from
65  * each index
66  */
67 static unsigned int *io_tlb_list;
68 static unsigned int io_tlb_index;
69
70 /*
71  * We need to save away the original address corresponding to a mapped entry
72  * for the sync operations.
73  */
74 static phys_addr_t *io_tlb_orig_addr;
75
76 /*
77  * Protect the above data structures in the map and unmap calls
78  */
79 static DEFINE_SPINLOCK(io_tlb_lock);
80
81 static unsigned int dma_bits;
82 static unsigned int __initdata max_dma_bits = 32;
83 static int __init
84 setup_dma_bits(char *str)
85 {
86         max_dma_bits = simple_strtoul(str, NULL, 0);
87         return 0;
88 }
89 __setup("dma_bits=", setup_dma_bits);
90
91 static int __init
92 setup_io_tlb_npages(char *str)
93 {
94         /* Unlike ia64, the size is aperture in megabytes, not 'slabs'! */
95         if (isdigit(*str)) {
96                 io_tlb_nslabs = simple_strtoul(str, &str, 0) <<
97                         (20 - IO_TLB_SHIFT);
98                 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
99         }
100         if (*str == ',')
101                 ++str;
102         /*
103          * NB. 'force' enables the swiotlb, but doesn't force its use for
104          * every DMA like it does on native Linux. 'off' forcibly disables
105          * use of the swiotlb.
106          */
107         if (!strcmp(str, "force"))
108                 swiotlb_force = 1;
109         else if (!strcmp(str, "off"))
110                 swiotlb_force = -1;
111
112         return 1;
113 }
114 __setup("swiotlb=", setup_io_tlb_npages);
115 /* make io_tlb_overflow tunable too? */
116
117 /* Note that this doesn't work with highmem page */
118 static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
119                                       volatile void *address)
120 {
121         return phys_to_dma(hwdev, virt_to_phys(address));
122 }
123
124 void swiotlb_print_info(void)
125 {
126         unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
127
128         printk(KERN_INFO "Software IO TLB enabled: \n"
129                " Aperture:     %lu megabytes\n"
130                " Address size: %u bits\n"
131                " Kernel range: %p - %p\n",
132                bytes >> 20, dma_bits,
133                io_tlb_start, io_tlb_end);
134 }
135
136 /*
137  * Statically reserve bounce buffer space and initialize bounce buffer data
138  * structures for the software IO TLB used to implement the PCI DMA API.
139  */
140 void __init
141 swiotlb_init_with_default_size(size_t default_size, int verbose)
142 {
143         unsigned long i, bytes;
144         int rc;
145
146         if (!io_tlb_nslabs) {
147                 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
148                 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
149         }
150
151         bytes = io_tlb_nslabs << IO_TLB_SHIFT;
152
153         /*
154          * Get IO TLB memory from the low pages
155          */
156         io_tlb_start = alloc_bootmem_pages(bytes);
157         if (!io_tlb_start)
158                 panic("Cannot allocate SWIOTLB buffer!\n");
159         dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT;
160         for (i = 0; i < io_tlb_nslabs; i += IO_TLB_SEGSIZE) {
161                 do {
162                         rc = xen_create_contiguous_region(
163                                 (unsigned long)io_tlb_start + (i << IO_TLB_SHIFT),
164                                 get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT),
165                                 dma_bits);
166                 } while (rc && dma_bits++ < max_dma_bits);
167                 if (rc) {
168                         if (i == 0)
169                                 panic("No suitable physical memory available for SWIOTLB buffer!\n"
170                                       "Use dom0_mem Xen boot parameter to reserve\n"
171                                       "some DMA memory (e.g., dom0_mem=-128M).\n");
172                         io_tlb_nslabs = i;
173                         i <<= IO_TLB_SHIFT;
174                         free_bootmem(__pa(io_tlb_start + i), bytes - i);
175                         bytes = i;
176                         for (dma_bits = 0; i > 0; i -= IO_TLB_SEGSIZE << IO_TLB_SHIFT) {
177                                 unsigned int bits = fls64(virt_to_bus(io_tlb_start + i - 1));
178
179                                 if (bits > dma_bits)
180                                         dma_bits = bits;
181                         }
182                         break;
183                 }
184         }
185         io_tlb_end = io_tlb_start + bytes;
186
187         /*
188          * Allocate and initialize the free list array.  This array is used
189          * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE.
190          */
191         io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
192         for (i = 0; i < io_tlb_nslabs; i++)
193                 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
194         io_tlb_index = 0;
195         io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(phys_addr_t));
196
197         /*
198          * Get the overflow emergency buffer
199          */
200         io_tlb_overflow_buffer = alloc_bootmem(io_tlb_overflow);
201         if (!io_tlb_overflow_buffer)
202                 panic("Cannot allocate SWIOTLB overflow buffer!\n");
203
204         do {
205                 rc = xen_create_contiguous_region(
206                         (unsigned long)io_tlb_overflow_buffer,
207                         get_order(io_tlb_overflow),
208                         dma_bits);
209         } while (rc && dma_bits++ < max_dma_bits);
210         if (rc)
211                 panic("No suitable physical memory available for SWIOTLB overflow buffer!\n");
212         if (verbose)
213                 swiotlb_print_info();
214 }
215
216 void __init
217 swiotlb_init(int verbose)
218 {
219         unsigned long ram_end;
220         size_t defsz = 64 << 20; /* 64MB default size */
221
222         if (swiotlb_force == 1) {
223                 swiotlb = 1;
224         } else if ((swiotlb_force != -1) &&
225                    is_running_on_xen() &&
226                    is_initial_xendomain()) {
227                 /* Domain 0 always has a swiotlb. */
228                 ram_end = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
229                 if (ram_end <= 0x1ffff)
230                         defsz = 2 << 20; /* 2MB on <512MB systems. */
231                 else if (ram_end <= 0x3ffff)
232                         defsz = 4 << 20; /* 4MB on <1GB systems. */
233                 else if (ram_end <= 0x7ffff)
234                         defsz = 8 << 20; /* 8MB on <2GB systems. */
235                 swiotlb = 1;
236         }
237
238         if (swiotlb)
239                 swiotlb_init_with_default_size(defsz, verbose);
240         else
241                 printk(KERN_INFO "Software IO TLB disabled\n");
242 }
243
244 static inline int range_needs_mapping(phys_addr_t pa, size_t size)
245 {
246         return range_straddles_page_boundary(pa, size);
247 }
248
249 static int is_swiotlb_buffer(dma_addr_t addr)
250 {
251         unsigned long pfn = mfn_to_local_pfn(PFN_DOWN(addr));
252         phys_addr_t paddr = (phys_addr_t)pfn << PAGE_SHIFT;
253
254         return paddr >= virt_to_phys(io_tlb_start) &&
255                 paddr < virt_to_phys(io_tlb_end);
256 }
257
258 /*
259  * Bounce: copy the swiotlb buffer back to the original dma location
260  *
261  * We use __copy_to_user_inatomic to transfer to the host buffer because the
262  * buffer may be mapped read-only (e.g, in blkback driver) but lower-level
263  * drivers map the buffer for DMA_BIDIRECTIONAL access. This causes an
264  * unnecessary copy from the aperture to the host buffer, and a page fault.
265  */
266 static void swiotlb_bounce(phys_addr_t phys, char *dma_addr, size_t size,
267                            enum dma_data_direction dir)
268 {
269         unsigned long pfn = PFN_DOWN(phys);
270
271         if (PageHighMem(pfn_to_page(pfn))) {
272                 /* The buffer does not have a mapping.  Map it in and copy */
273                 unsigned int offset = phys & ~PAGE_MASK;
274                 char *buffer;
275                 unsigned int sz = 0;
276                 unsigned long flags;
277
278                 while (size) {
279                         sz = min_t(size_t, PAGE_SIZE - offset, size);
280
281                         local_irq_save(flags);
282                         buffer = kmap_atomic(pfn_to_page(pfn),
283                                              KM_BOUNCE_READ);
284                         if (dir == DMA_TO_DEVICE)
285                                 memcpy(dma_addr, buffer + offset, sz);
286                         else if (__copy_to_user_inatomic(buffer + offset,
287                                                          dma_addr, sz))
288                                 /* inaccessible */;
289                         kunmap_atomic(buffer, KM_BOUNCE_READ);
290                         local_irq_restore(flags);
291
292                         size -= sz;
293                         pfn++;
294                         dma_addr += sz;
295                         offset = 0;
296                 }
297         } else {
298                 if (dir == DMA_TO_DEVICE)
299                         memcpy(dma_addr, phys_to_virt(phys), size);
300                 else if (__copy_to_user_inatomic(phys_to_virt(phys),
301                                                  dma_addr, size))
302                         /* inaccessible */;
303         }
304 }
305
306 /*
307  * Allocates bounce buffer and returns its kernel virtual address.
308  */
309 static void *
310 map_single(struct device *hwdev, phys_addr_t phys, size_t size, int dir)
311 {
312         unsigned long flags;
313         char *dma_addr;
314         unsigned int nslots, stride, index, wrap;
315         int i;
316         unsigned long mask;
317         unsigned long offset_slots;
318         unsigned long max_slots;
319
320         mask = dma_get_seg_boundary(hwdev);
321         offset_slots = -IO_TLB_SEGSIZE;
322
323         /*
324          * Carefully handle integer overflow which can occur when mask == ~0UL.
325          */
326         max_slots = mask + 1
327                     ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
328                     : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
329
330         /*
331          * For mappings greater than a page, we limit the stride (and
332          * hence alignment) to a page size.
333          */
334         nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
335         if (size > PAGE_SIZE)
336                 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
337         else
338                 stride = 1;
339
340         BUG_ON(!nslots);
341
342         /*
343          * Find suitable number of IO TLB entries size that will fit this
344          * request and allocate a buffer from that IO TLB pool.
345          */
346         spin_lock_irqsave(&io_tlb_lock, flags);
347         index = ALIGN(io_tlb_index, stride);
348         if (index >= io_tlb_nslabs)
349                 index = 0;
350         wrap = index;
351
352         do {
353                 while (iommu_is_span_boundary(index, nslots, offset_slots,
354                                               max_slots)) {
355                         index += stride;
356                         if (index >= io_tlb_nslabs)
357                                 index = 0;
358                         if (index == wrap)
359                                 goto not_found;
360                 }
361
362                 /*
363                  * If we find a slot that indicates we have 'nslots' number of
364                  * contiguous buffers, we allocate the buffers from that slot
365                  * and mark the entries as '0' indicating unavailable.
366                  */
367                 if (io_tlb_list[index] >= nslots) {
368                         int count = 0;
369
370                         for (i = index; i < (int) (index + nslots); i++)
371                                 io_tlb_list[i] = 0;
372                         for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
373                                 io_tlb_list[i] = ++count;
374                         dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
375
376                         /*
377                          * Update the indices to avoid searching in the next
378                          * round.
379                          */
380                         io_tlb_index = ((index + nslots) < io_tlb_nslabs
381                                         ? (index + nslots) : 0);
382
383                         goto found;
384                 }
385                 index += stride;
386                 if (index >= io_tlb_nslabs)
387                         index = 0;
388         } while (index != wrap);
389
390 not_found:
391         spin_unlock_irqrestore(&io_tlb_lock, flags);
392         return NULL;
393 found:
394         spin_unlock_irqrestore(&io_tlb_lock, flags);
395
396         /*
397          * Save away the mapping from the original address to the DMA address.
398          * This is needed when we sync the memory.  Then we sync the buffer if
399          * needed.
400          */
401         for (i = 0; i < nslots; i++)
402                 io_tlb_orig_addr[index+i] = phys + (i << IO_TLB_SHIFT);
403         if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
404                 swiotlb_bounce(phys, dma_addr, size, DMA_TO_DEVICE);
405
406         return dma_addr;
407 }
408
409 /*
410  * dma_addr is the kernel virtual address of the bounce buffer to unmap.
411  */
412 static void
413 do_unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
414 {
415         unsigned long flags;
416         int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
417         int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
418         phys_addr_t phys = io_tlb_orig_addr[index];
419
420         /*
421          * First, sync the memory before unmapping the entry
422          */
423         if (phys && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
424                 swiotlb_bounce(phys, dma_addr, size, DMA_FROM_DEVICE);
425
426         /*
427          * Return the buffer to the free list by setting the corresponding
428          * entries to indicate the number of contiguous entries available.
429          * While returning the entries to the free list, we merge the entries
430          * with slots below and above the pool being returned.
431          */
432         spin_lock_irqsave(&io_tlb_lock, flags);
433         {
434                 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
435                          io_tlb_list[index + nslots] : 0);
436                 /*
437                  * Step 1: return the slots to the free list, merging the
438                  * slots with superceeding slots
439                  */
440                 for (i = index + nslots - 1; i >= index; i--)
441                         io_tlb_list[i] = ++count;
442                 /*
443                  * Step 2: merge the returned slots with the preceding slots,
444                  * if available (non zero)
445                  */
446                 for (i = index - 1;
447                      (OFFSET(i, IO_TLB_SEGSIZE) !=
448                       IO_TLB_SEGSIZE -1) && io_tlb_list[i];
449                      i--)
450                         io_tlb_list[i] = ++count;
451         }
452         spin_unlock_irqrestore(&io_tlb_lock, flags);
453 }
454
455 static void
456 sync_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
457 {
458         int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
459         phys_addr_t phys = io_tlb_orig_addr[index];
460
461         phys += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
462
463         BUG_ON((dir != DMA_FROM_DEVICE) && (dir != DMA_TO_DEVICE));
464         swiotlb_bounce(phys, dma_addr, size, dir);
465 }
466
467 static void
468 swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
469 {
470         /*
471          * Ran out of IOMMU space for this operation. This is very bad.
472          * Unfortunately the drivers cannot handle this operation properly.
473          * unless they check for pci_dma_mapping_error (most don't)
474          * When the mapping is small enough return a static buffer to limit
475          * the damage, or panic when the transfer is too big.
476          */
477         printk(KERN_ERR "PCI-DMA: Out of SW-IOMMU space for %zu bytes at "
478                "device %s\n", size, dev ? dev_name(dev) : "?");
479
480         if (size <= io_tlb_overflow || !do_panic)
481                 return;
482
483         if (dir == DMA_BIDIRECTIONAL)
484                 panic("DMA: Random memory could be DMA accessed\n");
485         if (dir == DMA_FROM_DEVICE)
486                 panic("DMA: Random memory could be DMA written\n");
487         if (dir == DMA_TO_DEVICE)
488                 panic("DMA: Random memory could be DMA read\n");
489 }
490
491 /*
492  * Map a single buffer of the indicated size for DMA in streaming mode.  The
493  * PCI address to use is returned.
494  *
495  * Once the device is given the dma address, the device owns this memory until
496  * either swiotlb_unmap_page or swiotlb_dma_sync_single is performed.
497  */
498 dma_addr_t swiotlb_map_page(struct device *dev, struct page *page,
499                             unsigned long offset, size_t size,
500                             enum dma_data_direction dir,
501                             struct dma_attrs *attrs)
502 {
503         phys_addr_t phys = page_to_pseudophys(page) + offset;
504         dma_addr_t dev_addr = gnttab_dma_map_page(page) + offset;
505         void *map;
506
507         BUG_ON(dir == DMA_NONE);
508
509         /*
510          * If the address happens to be in the device's DMA window,
511          * we can safely return the device addr and not worry about bounce
512          * buffering it.
513          */
514         if (dma_capable(dev, dev_addr, size) &&
515             !range_needs_mapping(phys, size))
516                 return dev_addr;
517
518         /*
519          * Oh well, have to allocate and map a bounce buffer.
520          */
521         gnttab_dma_unmap_page(dev_addr);
522         map = map_single(dev, phys, size, dir);
523         if (!map) {
524                 swiotlb_full(dev, size, dir, 1);
525                 map = io_tlb_overflow_buffer;
526         }
527
528         dev_addr = swiotlb_virt_to_bus(dev, map);
529         return dev_addr;
530 }
531 EXPORT_SYMBOL_GPL(swiotlb_map_page);
532
533 /*
534  * Unmap a single streaming mode DMA translation.  The dma_addr and size must
535  * match what was provided for in a previous swiotlb_map_page call.  All
536  * other usages are undefined.
537  *
538  * After this call, reads by the cpu to the buffer are guaranteed to see
539  * whatever the device wrote there.
540  */
541 static void unmap_single(struct device *hwdev, dma_addr_t dev_addr,
542                          size_t size, int dir)
543 {
544         phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
545
546         BUG_ON(dir == DMA_NONE);
547
548         if (is_swiotlb_buffer(dev_addr)) {
549                 do_unmap_single(hwdev, phys_to_virt(paddr), size, dir);
550                 return;
551         }
552
553         gnttab_dma_unmap_page(dev_addr);
554 }
555
556 void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,
557                         size_t size, enum dma_data_direction dir,
558                         struct dma_attrs *attrs)
559 {
560         unmap_single(hwdev, dev_addr, size, dir);
561 }
562 EXPORT_SYMBOL_GPL(swiotlb_unmap_page);
563
564 /*
565  * Make physical memory consistent for a single streaming mode DMA translation
566  * after a transfer.
567  *
568  * If you perform a swiotlb_map_page() but wish to interrogate the buffer
569  * using the cpu, yet do not wish to teardown the PCI dma mapping, you must
570  * call this function before doing so.  At the next point you give the PCI dma
571  * address back to the card, you must first perform a
572  * swiotlb_dma_sync_for_device, and then the device again owns the buffer
573  */
574 void
575 swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
576                             size_t size, enum dma_data_direction dir)
577 {
578         phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
579
580         BUG_ON(dir == DMA_NONE);
581
582         if (is_swiotlb_buffer(dev_addr))
583                 sync_single(hwdev, phys_to_virt(paddr), size, dir);
584 }
585 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
586
587 void
588 swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
589                                size_t size, enum dma_data_direction dir)
590 {
591         phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
592
593         BUG_ON(dir == DMA_NONE);
594
595         if (is_swiotlb_buffer(dev_addr))
596                 sync_single(hwdev, phys_to_virt(paddr), size, dir);
597 }
598 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
599
600 void
601 swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
602                                   unsigned long offset, size_t size,
603                                   enum dma_data_direction dir)
604 {
605         swiotlb_sync_single_for_cpu(hwdev, dev_addr + offset, size, dir);
606 }
607 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
608
609 void
610 swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
611                                      unsigned long offset, size_t size,
612                                      enum dma_data_direction dir)
613 {
614         swiotlb_sync_single_for_device(hwdev, dev_addr + offset, size, dir);
615 }
616 EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
617
618 /*
619  * Map a set of buffers described by scatterlist in streaming mode for DMA.
620  * This is the scatter-gather version of the above swiotlb_map_page
621  * interface.  Here the scatter gather list elements are each tagged with the
622  * appropriate dma address and length.  They are obtained via
623  * sg_dma_{address,length}(SG).
624  *
625  * NOTE: An implementation may be able to use a smaller number of
626  *       DMA address/length pairs than there are SG table elements.
627  *       (for example via virtual mapping capabilities)
628  *       The routine returns the number of addr/length pairs actually
629  *       used, at most nents.
630  *
631  * Device ownership issues as mentioned above for swiotlb_map_page are the
632  * same here.
633  */
634 int
635 swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
636                      enum dma_data_direction dir, struct dma_attrs *attrs)
637 {
638         struct scatterlist *sg;
639         int i;
640
641         BUG_ON(dir == DMA_NONE);
642
643         for_each_sg(sgl, sg, nelems, i) {
644                 dma_addr_t dev_addr = gnttab_dma_map_page(sg_page(sg))
645                                       + sg->offset;
646                 phys_addr_t paddr = page_to_pseudophys(sg_page(sg))
647                                    + sg->offset;
648
649                 if (range_needs_mapping(paddr, sg->length) ||
650                     !dma_capable(hwdev, dev_addr, sg->length)) {
651                         void *map;
652
653                         gnttab_dma_unmap_page(dev_addr);
654                         map = map_single(hwdev, paddr,
655                                          sg->length, dir);
656                         if (!map) {
657                                 /* Don't panic here, we expect map_sg users
658                                    to do proper error handling. */
659                                 swiotlb_full(hwdev, sg->length, dir, 0);
660                                 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
661                                                        attrs);
662                                 sgl[0].dma_length = 0;
663                                 return 0;
664                         }
665                         sg->dma_address = swiotlb_virt_to_bus(hwdev, map);
666                 } else
667                         sg->dma_address = dev_addr;
668                 sg->dma_length = sg->length;
669         }
670         return nelems;
671 }
672 EXPORT_SYMBOL(swiotlb_map_sg_attrs);
673
674 int
675 swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
676                int dir)
677 {
678         return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
679 }
680 EXPORT_SYMBOL(swiotlb_map_sg);
681
682 /*
683  * Unmap a set of streaming mode DMA translations.  Again, cpu read rules
684  * concerning calls here are the same as for swiotlb_unmap_page() above.
685  */
686 void
687 swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
688                        int nelems, enum dma_data_direction dir, struct dma_attrs *attrs)
689 {
690         struct scatterlist *sg;
691         int i;
692
693         BUG_ON(dir == DMA_NONE);
694
695         for_each_sg(sgl, sg, nelems, i)
696                 unmap_single(hwdev, sg->dma_address, sg->dma_length, dir);
697
698 }
699 EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
700
701 void
702 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
703                  int dir)
704 {
705         return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
706 }
707 EXPORT_SYMBOL(swiotlb_unmap_sg);
708
709 /*
710  * Make physical memory consistent for a set of streaming mode DMA translations
711  * after a transfer.
712  *
713  * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
714  * and usage.
715  */
716 void
717 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sgl,
718                         int nelems, enum dma_data_direction dir)
719 {
720         struct scatterlist *sg;
721         int i;
722
723         for_each_sg(sgl, sg, nelems, i)
724                 swiotlb_sync_single_for_cpu(hwdev, sg->dma_address,
725                                             sg->dma_length, dir);
726 }
727 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
728
729 void
730 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sgl,
731                            int nelems, enum dma_data_direction dir)
732 {
733         struct scatterlist *sg;
734         int i;
735
736         for_each_sg(sgl, sg, nelems, i)
737                 swiotlb_sync_single_for_device(hwdev, sg->dma_address,
738                                                sg->dma_length, dir);
739 }
740 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
741
742 int
743 swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
744 {
745         return (dma_addr == swiotlb_virt_to_bus(hwdev, io_tlb_overflow_buffer));
746 }
747 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
748
749 /*
750  * Return whether the given PCI device DMA address mask can be supported
751  * properly.  For example, if your device can only drive the low 24-bits
752  * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
753  * this function.
754  */
755 int
756 swiotlb_dma_supported (struct device *hwdev, u64 mask)
757 {
758         return (mask >= ((1UL << dma_bits) - 1));
759 }
760 EXPORT_SYMBOL(swiotlb_dma_supported);