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