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