ia64/linux-2.6.18-xen.hg

view lib/swiotlb-xen.c @ 647:a5bb490065f6

Fix the build after public header sync.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Aug 13 14:01:49 2008 +0100 (2008-08-13)
parents 3baca673faeb
children 4dc80607377d
line source
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 */
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-i386/mach-xen/asm/gnttab_dma.h>
32 int swiotlb;
33 EXPORT_SYMBOL(swiotlb);
35 #define OFFSET(val,align) ((unsigned long)((val) & ( (align) - 1)))
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
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
50 int swiotlb_force;
52 static char *iotlb_virt_start;
53 static unsigned long iotlb_nslabs;
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;
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 }
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;
76 void *io_tlb_overflow_buffer;
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;
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;
94 /*
95 * Protect the above data structures in the map and unmap calls
96 */
97 static DEFINE_SPINLOCK(io_tlb_lock);
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);
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? */
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
142 swiotlb_init_with_default_size (size_t default_size)
143 {
144 unsigned long i, bytes;
145 int rc;
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 }
155 bytes = iotlb_nslabs * (1UL << IO_TLB_SHIFT);
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");
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));
184 if (bits > dma_bits)
185 dma_bits = bits;
186 }
187 break;
188 }
189 }
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));
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");
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");
218 iotlb_pfn_start = __pa(iotlb_virt_start) >> PAGE_SHIFT;
219 iotlb_pfn_end = iotlb_pfn_start + (bytes >> PAGE_SHIFT);
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 }
230 void
231 swiotlb_init(void)
232 {
233 long ram_end;
234 size_t defsz = 64 * (1 << 20); /* 64MB default size */
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 }
248 if (swiotlb)
249 swiotlb_init_with_default_size(defsz);
250 else
251 printk(KERN_INFO "Software IO TLB disabled\n");
252 }
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;
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 }
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;
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;
320 BUG_ON(!nslots);
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);
330 if (index >= iotlb_nslabs)
331 wrap = index = 0;
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;
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);
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);
361 goto found;
362 }
363 index += stride;
364 if (index >= iotlb_nslabs)
365 index = 0;
366 } while (index != wrap);
368 spin_unlock_irqrestore(&io_tlb_lock, flags);
369 return NULL;
370 }
371 found:
372 spin_unlock_irqrestore(&io_tlb_lock, flags);
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);
389 return dma_addr;
390 }
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 }
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);
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);
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 }
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 }
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 %lu bytes at "
467 "device %s\n", (unsigned long)size, dev ? dev->bus_id : "?");
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 }
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;
492 BUG_ON(dir == DMA_NONE);
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;
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 }
515 dev_addr = virt_to_bus(map);
516 return dev_addr;
517 }
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 }
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 }
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 }
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 *sg, int nelems,
584 int dir)
585 {
586 struct phys_addr buffer;
587 dma_addr_t dev_addr;
588 char *map;
589 int i;
591 BUG_ON(dir == DMA_NONE);
593 for (i = 0; i < nelems; i++, sg++) {
594 dev_addr = gnttab_dma_map_page(sg->page) + sg->offset;
596 if (range_straddles_page_boundary(page_to_pseudophys(sg->page)
597 + sg->offset, sg->length)
598 || address_needs_mapping(hwdev, dev_addr)) {
599 gnttab_dma_unmap_page(dev_addr);
600 buffer.page = sg->page;
601 buffer.offset = sg->offset;
602 map = map_single(hwdev, buffer, sg->length, dir);
603 if (!map) {
604 /* Don't panic here, we expect map_sg users
605 to do proper error handling. */
606 swiotlb_full(hwdev, sg->length, dir, 0);
607 swiotlb_unmap_sg(hwdev, sg - i, i, dir);
608 sg[0].dma_length = 0;
609 return 0;
610 }
611 sg->dma_address = (dma_addr_t)virt_to_bus(map);
612 } else
613 sg->dma_address = dev_addr;
614 sg->dma_length = sg->length;
615 }
616 return nelems;
617 }
619 /*
620 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
621 * concerning calls here are the same as for swiotlb_unmap_single() above.
622 */
623 void
624 swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg, int nelems,
625 int dir)
626 {
627 int i;
629 BUG_ON(dir == DMA_NONE);
631 for (i = 0; i < nelems; i++, sg++)
632 if (in_swiotlb_aperture(sg->dma_address))
633 unmap_single(hwdev,
634 (void *)bus_to_virt(sg->dma_address),
635 sg->dma_length, dir);
636 else
637 gnttab_dma_unmap_page(sg->dma_address);
638 }
640 /*
641 * Make physical memory consistent for a set of streaming mode DMA translations
642 * after a transfer.
643 *
644 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
645 * and usage.
646 */
647 void
648 swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
649 int nelems, int dir)
650 {
651 int i;
653 BUG_ON(dir == DMA_NONE);
655 for (i = 0; i < nelems; i++, sg++)
656 if (in_swiotlb_aperture(sg->dma_address))
657 sync_single(hwdev,
658 (void *)bus_to_virt(sg->dma_address),
659 sg->dma_length, dir);
660 }
662 void
663 swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
664 int nelems, int dir)
665 {
666 int i;
668 BUG_ON(dir == DMA_NONE);
670 for (i = 0; i < nelems; i++, sg++)
671 if (in_swiotlb_aperture(sg->dma_address))
672 sync_single(hwdev,
673 (void *)bus_to_virt(sg->dma_address),
674 sg->dma_length, dir);
675 }
677 #ifdef CONFIG_HIGHMEM
679 dma_addr_t
680 swiotlb_map_page(struct device *hwdev, struct page *page,
681 unsigned long offset, size_t size,
682 enum dma_data_direction direction)
683 {
684 struct phys_addr buffer;
685 dma_addr_t dev_addr;
686 char *map;
688 dev_addr = gnttab_dma_map_page(page) + offset;
689 if (address_needs_mapping(hwdev, dev_addr)) {
690 gnttab_dma_unmap_page(dev_addr);
691 buffer.page = page;
692 buffer.offset = offset;
693 map = map_single(hwdev, buffer, size, direction);
694 if (!map) {
695 swiotlb_full(hwdev, size, direction, 1);
696 map = io_tlb_overflow_buffer;
697 }
698 dev_addr = (dma_addr_t)virt_to_bus(map);
699 }
701 return dev_addr;
702 }
704 void
705 swiotlb_unmap_page(struct device *hwdev, dma_addr_t dma_address,
706 size_t size, enum dma_data_direction direction)
707 {
708 BUG_ON(direction == DMA_NONE);
709 if (in_swiotlb_aperture(dma_address))
710 unmap_single(hwdev, bus_to_virt(dma_address), size, direction);
711 else
712 gnttab_dma_unmap_page(dma_address);
713 }
715 #endif
717 int
718 swiotlb_dma_mapping_error(dma_addr_t dma_addr)
719 {
720 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
721 }
723 /*
724 * Return whether the given PCI device DMA address mask can be supported
725 * properly. For example, if your device can only drive the low 24-bits
726 * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
727 * this function.
728 */
729 int
730 swiotlb_dma_supported (struct device *hwdev, u64 mask)
731 {
732 return (mask >= ((1UL << dma_bits) - 1));
733 }
735 EXPORT_SYMBOL(swiotlb_init);
736 EXPORT_SYMBOL(swiotlb_map_single);
737 EXPORT_SYMBOL(swiotlb_unmap_single);
738 EXPORT_SYMBOL(swiotlb_map_sg);
739 EXPORT_SYMBOL(swiotlb_unmap_sg);
740 EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
741 EXPORT_SYMBOL(swiotlb_sync_single_for_device);
742 EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
743 EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
744 EXPORT_SYMBOL(swiotlb_dma_mapping_error);
745 EXPORT_SYMBOL(swiotlb_dma_supported);