- Update Xen patches to 2.6.38-rc4 and c/s 1066.
[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 swiotlb_tbl_unmap_single and
45  * swiotlb_tbl_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) between 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 static 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 void __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
137 {
138         unsigned long i, bytes;
139         int rc;
140
141         bytes = nslabs << IO_TLB_SHIFT;
142
143         io_tlb_nslabs = nslabs;
144         io_tlb_start = tlb;
145         dma_bits = get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT) + PAGE_SHIFT;
146         for (nslabs = 0; nslabs < io_tlb_nslabs; nslabs += IO_TLB_SEGSIZE) {
147                 do {
148                         rc = xen_create_contiguous_region(
149                                 (unsigned long)io_tlb_start + (nslabs << IO_TLB_SHIFT),
150                                 get_order(IO_TLB_SEGSIZE << IO_TLB_SHIFT),
151                                 dma_bits);
152                 } while (rc && dma_bits++ < max_dma_bits);
153                 if (rc) {
154                         if (nslabs == 0)
155                                 panic("No suitable physical memory available for SWIOTLB buffer!\n"
156                                       "Use dom0_mem Xen boot parameter to reserve\n"
157                                       "some DMA memory (e.g., dom0_mem=-128M).\n");
158                         io_tlb_nslabs = nslabs;
159                         i = nslabs << IO_TLB_SHIFT;
160                         free_bootmem(__pa(io_tlb_start + i), bytes - i);
161                         bytes = i;
162                         for (dma_bits = 0; i > 0; i -= IO_TLB_SEGSIZE << IO_TLB_SHIFT) {
163                                 unsigned int bits = fls64(virt_to_bus(io_tlb_start + i - 1));
164
165                                 if (bits > dma_bits)
166                                         dma_bits = bits;
167                         }
168                         break;
169                 }
170         }
171         io_tlb_end = io_tlb_start + bytes;
172
173         /*
174          * Allocate and initialize the free list array.  This array is used
175          * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE.
176          */
177         io_tlb_list = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_nslabs * sizeof(int)));
178         for (i = 0; i < io_tlb_nslabs; i++)
179                 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
180         io_tlb_index = 0;
181         io_tlb_orig_addr = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)));
182
183         /*
184          * Get the overflow emergency buffer
185          */
186         io_tlb_overflow_buffer = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_overflow));
187         if (!io_tlb_overflow_buffer)
188                 panic("Cannot allocate SWIOTLB overflow buffer!\n");
189
190         do {
191                 rc = xen_create_contiguous_region(
192                         (unsigned long)io_tlb_overflow_buffer,
193                         get_order(io_tlb_overflow),
194                         dma_bits);
195         } while (rc && dma_bits++ < max_dma_bits);
196         if (rc)
197                 panic("No suitable physical memory available for SWIOTLB overflow buffer!\n");
198         if (verbose)
199                 swiotlb_print_info();
200 }
201
202 /*
203  * Statically reserve bounce buffer space and initialize bounce buffer data
204  * structures for the software IO TLB used to implement the DMA API.
205  */
206 void __init
207 swiotlb_init_with_default_size(size_t default_size, int verbose)
208 {
209         unsigned long bytes;
210
211         if (!io_tlb_nslabs) {
212                 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
213                 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
214         }
215
216         bytes = io_tlb_nslabs << IO_TLB_SHIFT;
217
218         /*
219          * Get IO TLB memory from the low pages
220          */
221         io_tlb_start = alloc_bootmem_pages(PAGE_ALIGN(bytes));
222         if (!io_tlb_start)
223                 panic("Cannot allocate SWIOTLB buffer");
224
225         swiotlb_init_with_tbl(io_tlb_start, io_tlb_nslabs, verbose);
226 }
227
228 void __init
229 swiotlb_init(int verbose)
230 {
231         unsigned long ram_end;
232         size_t defsz = 64 << 20; /* 64MB default size */
233
234         if (swiotlb_force == 1) {
235                 swiotlb = 1;
236         } else if ((swiotlb_force != -1) &&
237                    is_running_on_xen() &&
238                    is_initial_xendomain()) {
239                 /* Domain 0 always has a swiotlb. */
240                 ram_end = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
241                 if (ram_end <= 0x1ffff)
242                         defsz = 2 << 20; /* 2MB on <512MB systems. */
243                 else if (ram_end <= 0x3ffff)
244                         defsz = 4 << 20; /* 4MB on <1GB systems. */
245                 else if (ram_end <= 0x7ffff)
246                         defsz = 8 << 20; /* 8MB on <2GB systems. */
247                 swiotlb = 1;
248         }
249
250         if (swiotlb)
251                 swiotlb_init_with_default_size(defsz, verbose);
252         else
253                 printk(KERN_INFO "Software IO TLB disabled\n");
254 }
255
256 static inline int range_needs_mapping(phys_addr_t pa, size_t size)
257 {
258         return range_straddles_page_boundary(pa, size);
259 }
260
261 static int is_swiotlb_buffer(dma_addr_t addr)
262 {
263         unsigned long pfn = mfn_to_local_pfn(PFN_DOWN(addr));
264         phys_addr_t paddr = (phys_addr_t)pfn << PAGE_SHIFT;
265
266         return paddr >= virt_to_phys(io_tlb_start) &&
267                 paddr < virt_to_phys(io_tlb_end);
268 }
269
270 /*
271  * Bounce: copy the swiotlb buffer back to the original dma location
272  *
273  * We use __copy_to_user_inatomic to transfer to the host buffer because the
274  * buffer may be mapped read-only (e.g, in blkback driver) but lower-level
275  * drivers map the buffer for DMA_BIDIRECTIONAL access. This causes an
276  * unnecessary copy from the aperture to the host buffer, and a page fault.
277  */
278 void swiotlb_bounce(phys_addr_t phys, char *dma_addr, size_t size,
279                     enum dma_data_direction dir)
280 {
281         unsigned long pfn = PFN_DOWN(phys);
282
283         if (PageHighMem(pfn_to_page(pfn))) {
284                 /* The buffer does not have a mapping.  Map it in and copy */
285                 unsigned int offset = phys & ~PAGE_MASK;
286                 char *buffer;
287                 unsigned int sz = 0;
288                 unsigned long flags;
289
290                 while (size) {
291                         sz = min_t(size_t, PAGE_SIZE - offset, size);
292
293                         local_irq_save(flags);
294                         buffer = kmap_atomic(pfn_to_page(pfn),
295                                              KM_BOUNCE_READ);
296                         if (dir == DMA_TO_DEVICE)
297                                 memcpy(dma_addr, buffer + offset, sz);
298                         else if (__copy_to_user_inatomic(buffer + offset,
299                                                          dma_addr, sz))
300                                 /* inaccessible */;
301                         kunmap_atomic(buffer, KM_BOUNCE_READ);
302                         local_irq_restore(flags);
303
304                         size -= sz;
305                         pfn++;
306                         dma_addr += sz;
307                         offset = 0;
308                 }
309         } else {
310                 if (dir == DMA_TO_DEVICE)
311                         memcpy(dma_addr, phys_to_virt(phys), size);
312                 else if (__copy_to_user_inatomic(phys_to_virt(phys),
313                                                  dma_addr, size))
314                         /* inaccessible */;
315         }
316 }
317 EXPORT_SYMBOL_GPL(swiotlb_bounce);
318
319 void *swiotlb_tbl_map_single(struct device *hwdev, dma_addr_t tbl_dma_addr,
320                              phys_addr_t phys, size_t size,
321                              enum dma_data_direction dir)
322 {
323         unsigned long flags;
324         char *dma_addr;
325         unsigned int nslots, stride, index, wrap;
326         int i;
327         unsigned long mask;
328         unsigned long offset_slots;
329         unsigned long max_slots;
330
331         mask = dma_get_seg_boundary(hwdev);
332         offset_slots = -IO_TLB_SEGSIZE;
333
334         /*
335          * Carefully handle integer overflow which can occur when mask == ~0UL.
336          */
337         max_slots = mask + 1
338                     ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
339                     : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
340
341         /*
342          * For mappings greater than a page, we limit the stride (and
343          * hence alignment) to a page size.
344          */
345         nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
346         if (size > PAGE_SIZE)
347                 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
348         else
349                 stride = 1;
350
351         BUG_ON(!nslots);
352
353         /*
354          * Find suitable number of IO TLB entries size that will fit this
355          * request and allocate a buffer from that IO TLB pool.
356          */
357         spin_lock_irqsave(&io_tlb_lock, flags);
358         index = ALIGN(io_tlb_index, stride);
359         if (index >= io_tlb_nslabs)
360                 index = 0;
361         wrap = index;
362
363         do {
364                 while (iommu_is_span_boundary(index, nslots, offset_slots,
365                                               max_slots)) {
366                         index += stride;
367                         if (index >= io_tlb_nslabs)
368                                 index = 0;
369                         if (index == wrap)
370                                 goto not_found;
371                 }
372
373                 /*
374                  * If we find a slot that indicates we have 'nslots' number of
375                  * contiguous buffers, we allocate the buffers from that slot
376                  * and mark the entries as '0' indicating unavailable.
377                  */
378                 if (io_tlb_list[index] >= nslots) {
379                         int count = 0;
380
381                         for (i = index; i < (int) (index + nslots); i++)
382                                 io_tlb_list[i] = 0;
383                         for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
384                                 io_tlb_list[i] = ++count;
385                         dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
386
387                         /*
388                          * Update the indices to avoid searching in the next
389                          * round.
390                          */
391                         io_tlb_index = ((index + nslots) < io_tlb_nslabs
392                                         ? (index + nslots) : 0);
393
394                         goto found;
395                 }
396                 index += stride;
397                 if (index >= io_tlb_nslabs)
398                         index = 0;
399         } while (index != wrap);
400
401 not_found:
402         spin_unlock_irqrestore(&io_tlb_lock, flags);
403         return NULL;
404 found:
405         spin_unlock_irqrestore(&io_tlb_lock, flags);
406
407         /*
408          * Save away the mapping from the original address to the DMA address.
409          * This is needed when we sync the memory.  Then we sync the buffer if
410          * needed.
411          */
412         for (i = 0; i < nslots; i++)
413                 io_tlb_orig_addr[index+i] = phys + (i << IO_TLB_SHIFT);
414         if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
415                 swiotlb_bounce(phys, dma_addr, size, DMA_TO_DEVICE);
416
417         return dma_addr;
418 }
419 EXPORT_SYMBOL_GPL(swiotlb_tbl_map_single);
420
421 /*
422  * Allocates bounce buffer and returns its kernel virtual address.
423  */
424
425 static void *
426 map_single(struct device *hwdev, phys_addr_t phys, size_t size,
427            enum dma_data_direction dir)
428 {
429         dma_addr_t start_dma_addr = swiotlb_virt_to_bus(hwdev, io_tlb_start);
430
431         return swiotlb_tbl_map_single(hwdev, start_dma_addr, phys, size, dir);
432 }
433
434 /*
435  * dma_addr is the kernel virtual address of the bounce buffer to unmap.
436  */
437 void
438 swiotlb_tbl_unmap_single(struct device *hwdev, char *dma_addr, size_t size,
439                         enum dma_data_direction dir)
440 {
441         unsigned long flags;
442         int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
443         int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
444         phys_addr_t phys = io_tlb_orig_addr[index];
445
446         /*
447          * First, sync the memory before unmapping the entry
448          */
449         if (phys && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
450                 swiotlb_bounce(phys, dma_addr, size, DMA_FROM_DEVICE);
451
452         /*
453          * Return the buffer to the free list by setting the corresponding
454          * entries to indicate the number of contiguous entries available.
455          * While returning the entries to the free list, we merge the entries
456          * with slots below and above the pool being returned.
457          */
458         spin_lock_irqsave(&io_tlb_lock, flags);
459         {
460                 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
461                          io_tlb_list[index + nslots] : 0);
462                 /*
463                  * Step 1: return the slots to the free list, merging the
464                  * slots with superceeding slots
465                  */
466                 for (i = index + nslots - 1; i >= index; i--)
467                         io_tlb_list[i] = ++count;
468                 /*
469                  * Step 2: merge the returned slots with the preceding slots,
470                  * if available (non zero)
471                  */
472                 for (i = index - 1;
473                      (OFFSET(i, IO_TLB_SEGSIZE) !=
474                       IO_TLB_SEGSIZE -1) && io_tlb_list[i];
475                      i--)
476                         io_tlb_list[i] = ++count;
477         }
478         spin_unlock_irqrestore(&io_tlb_lock, flags);
479 }
480 EXPORT_SYMBOL_GPL(swiotlb_tbl_unmap_single);
481
482 void
483 swiotlb_tbl_sync_single(struct device *hwdev, char *dma_addr, size_t size,
484                         enum dma_data_direction dir,
485                         enum dma_sync_target target)
486 {
487         int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
488         phys_addr_t phys = io_tlb_orig_addr[index];
489
490         phys += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
491
492         switch (target) {
493         case SYNC_FOR_CPU:
494                 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
495                         swiotlb_bounce(phys, dma_addr, size, DMA_FROM_DEVICE);
496                 else
497                         BUG_ON(dir != DMA_TO_DEVICE);
498                 break;
499         case SYNC_FOR_DEVICE:
500                 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
501                         swiotlb_bounce(phys, dma_addr, size, DMA_TO_DEVICE);
502                 else
503                         BUG_ON(dir != DMA_FROM_DEVICE);
504                 break;
505         default:
506                 BUG();
507         }
508 }
509 EXPORT_SYMBOL_GPL(swiotlb_tbl_sync_single);
510
511 static void
512 swiotlb_full(struct device *dev, size_t size, enum dma_data_direction dir,
513              int do_panic)
514 {
515         /*
516          * Ran out of IOMMU space for this operation. This is very bad.
517          * Unfortunately the drivers cannot handle this operation properly.
518          * unless they check for pci_dma_mapping_error (most don't)
519          * When the mapping is small enough return a static buffer to limit
520          * the damage, or panic when the transfer is too big.
521          */
522         printk(KERN_ERR "PCI-DMA: Out of SW-IOMMU space for %zu bytes at "
523                "device %s\n", size, dev ? dev_name(dev) : "?");
524
525         if (size <= io_tlb_overflow || !do_panic)
526                 return;
527
528         if (dir == DMA_BIDIRECTIONAL)
529                 panic("DMA: Random memory could be DMA accessed\n");
530         if (dir == DMA_FROM_DEVICE)
531                 panic("DMA: Random memory could be DMA written\n");
532         if (dir == DMA_TO_DEVICE)
533                 panic("DMA: Random memory could be DMA read\n");
534 }
535
536 /*
537  * Map a single buffer of the indicated size for DMA in streaming mode.  The
538  * PCI address to use is returned.
539  *
540  * Once the device is given the dma address, the device owns this memory until
541  * either swiotlb_unmap_page or swiotlb_dma_sync_single is performed.
542  */
543 dma_addr_t swiotlb_map_page(struct device *dev, struct page *page,
544                             unsigned long offset, size_t size,
545                             enum dma_data_direction dir,
546                             struct dma_attrs *attrs)
547 {
548         phys_addr_t phys = page_to_pseudophys(page) + offset;
549         dma_addr_t dev_addr = gnttab_dma_map_page(page) + offset;
550         void *map;
551
552         BUG_ON(dir == DMA_NONE);
553
554         /*
555          * If the address happens to be in the device's DMA window,
556          * we can safely return the device addr and not worry about bounce
557          * buffering it.
558          */
559         if (dma_capable(dev, dev_addr, size) &&
560             !range_needs_mapping(phys, size))
561                 return dev_addr;
562
563         /*
564          * Oh well, have to allocate and map a bounce buffer.
565          */
566         gnttab_dma_unmap_page(dev_addr);
567         map = map_single(dev, phys, size, dir);
568         if (!map) {
569                 swiotlb_full(dev, size, dir, 1);
570                 map = io_tlb_overflow_buffer;
571         }
572
573         dev_addr = swiotlb_virt_to_bus(dev, map);
574         return dev_addr;
575 }
576 EXPORT_SYMBOL_GPL(swiotlb_map_page);
577
578 /*
579  * Unmap a single streaming mode DMA translation.  The dma_addr and size must
580  * match what was provided for in a previous swiotlb_map_page call.  All
581  * other usages are undefined.
582  *
583  * After this call, reads by the cpu to the buffer are guaranteed to see
584  * whatever the device wrote there.
585  */
586 static void unmap_single(struct device *hwdev, dma_addr_t dev_addr,
587                          size_t size, enum dma_data_direction dir)
588 {
589         phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
590
591         BUG_ON(dir == DMA_NONE);
592
593         if (is_swiotlb_buffer(dev_addr)) {
594                 swiotlb_tbl_unmap_single(hwdev, phys_to_virt(paddr), size, dir);
595                 return;
596         }
597
598         gnttab_dma_unmap_page(dev_addr);
599 }
600
601 void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,
602                         size_t size, enum dma_data_direction dir,
603                         struct dma_attrs *attrs)
604 {
605         unmap_single(hwdev, dev_addr, size, dir);
606 }
607 EXPORT_SYMBOL_GPL(swiotlb_unmap_page);
608
609 /*
610  * Make physical memory consistent for a single streaming mode DMA translation
611  * after a transfer.
612  *
613  * If you perform a swiotlb_map_page() but wish to interrogate the buffer
614  * using the cpu, yet do not wish to teardown the PCI dma mapping, you must
615  * call this function before doing so.  At the next point you give the PCI dma
616  * address back to the card, you must first perform a
617  * swiotlb_dma_sync_for_device, and then the device again owns the buffer
618  */
619 static void
620 swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
621                     size_t size, enum dma_data_direction dir,
622                     enum dma_sync_target target)
623 {
624         phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
625
626         BUG_ON(dir == DMA_NONE);
627
628         if (is_swiotlb_buffer(dev_addr))
629                 swiotlb_tbl_sync_single(hwdev, phys_to_virt(paddr), size, dir,
630                                        target);
631 }
632
633 void
634 swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
635                             size_t size, enum dma_data_direction dir)
636 {
637         swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
638 }
639 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
640
641 void
642 swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
643                                size_t size, enum dma_data_direction dir)
644 {
645         swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
646 }
647 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
648
649 /*
650  * Map a set of buffers described by scatterlist in streaming mode for DMA.
651  * This is the scatter-gather version of the above swiotlb_map_page
652  * interface.  Here the scatter gather list elements are each tagged with the
653  * appropriate dma address and length.  They are obtained via
654  * sg_dma_{address,length}(SG).
655  *
656  * NOTE: An implementation may be able to use a smaller number of
657  *       DMA address/length pairs than there are SG table elements.
658  *       (for example via virtual mapping capabilities)
659  *       The routine returns the number of addr/length pairs actually
660  *       used, at most nents.
661  *
662  * Device ownership issues as mentioned above for swiotlb_map_page are the
663  * same here.
664  */
665 int
666 swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
667                      enum dma_data_direction dir, struct dma_attrs *attrs)
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                 dma_addr_t dev_addr = gnttab_dma_map_page(sg_page(sg))
676                                       + sg->offset;
677                 phys_addr_t paddr = page_to_pseudophys(sg_page(sg))
678                                    + sg->offset;
679
680                 if (range_needs_mapping(paddr, sg->length) ||
681                     !dma_capable(hwdev, dev_addr, sg->length)) {
682                         void *map;
683
684                         gnttab_dma_unmap_page(dev_addr);
685                         map = map_single(hwdev, paddr,
686                                          sg->length, dir);
687                         if (!map) {
688                                 /* Don't panic here, we expect map_sg users
689                                    to do proper error handling. */
690                                 swiotlb_full(hwdev, sg->length, dir, 0);
691                                 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
692                                                        attrs);
693                                 sgl[0].dma_length = 0;
694                                 return 0;
695                         }
696                         sg->dma_address = swiotlb_virt_to_bus(hwdev, map);
697                 } else
698                         sg->dma_address = dev_addr;
699                 sg->dma_length = sg->length;
700         }
701         return nelems;
702 }
703 EXPORT_SYMBOL(swiotlb_map_sg_attrs);
704
705 int
706 swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
707                enum dma_data_direction dir)
708 {
709         return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
710 }
711 EXPORT_SYMBOL(swiotlb_map_sg);
712
713 /*
714  * Unmap a set of streaming mode DMA translations.  Again, cpu read rules
715  * concerning calls here are the same as for swiotlb_unmap_page() above.
716  */
717 void
718 swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
719                        int nelems, enum dma_data_direction dir, struct dma_attrs *attrs)
720 {
721         struct scatterlist *sg;
722         int i;
723
724         BUG_ON(dir == DMA_NONE);
725
726         for_each_sg(sgl, sg, nelems, i)
727                 unmap_single(hwdev, sg->dma_address, sg->dma_length, dir);
728
729 }
730 EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
731
732 void
733 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
734                  enum dma_data_direction dir)
735 {
736         return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
737 }
738 EXPORT_SYMBOL(swiotlb_unmap_sg);
739
740 /*
741  * Make physical memory consistent for a set of streaming mode DMA translations
742  * after a transfer.
743  *
744  * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
745  * and usage.
746  */
747 static void
748 swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
749                 int nelems, enum dma_data_direction dir,
750                 enum dma_sync_target target)
751 {
752         struct scatterlist *sg;
753         int i;
754
755         for_each_sg(sgl, sg, nelems, i)
756                 swiotlb_sync_single(hwdev, sg->dma_address,
757                                     sg->dma_length, dir, target);
758 }
759
760 void
761 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
762                         int nelems, enum dma_data_direction dir)
763 {
764         swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
765 }
766 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
767
768 void
769 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
770                            int nelems, enum dma_data_direction dir)
771 {
772         swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
773 }
774 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
775
776 int
777 swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
778 {
779         return (dma_addr == swiotlb_virt_to_bus(hwdev, io_tlb_overflow_buffer));
780 }
781 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
782
783 /*
784  * Return whether the given PCI device DMA address mask can be supported
785  * properly.  For example, if your device can only drive the low 24-bits
786  * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
787  * this function.
788  */
789 int
790 swiotlb_dma_supported (struct device *hwdev, u64 mask)
791 {
792         return (mask >= ((1UL << dma_bits) - 1));
793 }
794 EXPORT_SYMBOL(swiotlb_dma_supported);