blob: 3494263cdd9a0c01e046427a474798147123e6b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dynamic DMA mapping support.
3 *
Jan Beulich563aaf02007-02-05 18:51:25 -08004 * This implementation is a fallback for platforms that do not support
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 *
11 * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API.
12 * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
13 * unnecessary i-cache flushing.
John W. Linville569c8bf2005-09-29 14:45:24 -070014 * 04/07/.. ak Better overflow handling. Assorted fixes.
15 * 05/09/10 linville Add support for syncing ranges, support syncing for
16 * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/cache.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070020#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/spinlock.h>
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -080024#include <linux/swiotlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Ian Campbell0016fde2008-12-16 12:17:27 -080026#include <linux/swiotlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/types.h>
28#include <linux/ctype.h>
29
30#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/dma.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070032#include <asm/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/init.h>
35#include <linux/bootmem.h>
FUJITA Tomonoria8522502008-04-29 00:59:36 -070036#include <linux/iommu-helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define OFFSET(val,align) ((unsigned long) \
39 ( (val) & ( (align) - 1)))
40
Jens Axboef9527f12007-10-22 19:44:53 +020041#define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
Jan Beulich93fbff62007-02-05 18:49:45 -080042#define SG_ENT_PHYS_ADDRESS(sg) virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Alex Williamson0b9afed2005-09-06 11:20:49 -060044#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
45
46/*
47 * Minimum IO TLB size to bother booting with. Systems with mainly
48 * 64bit capable cards will only lightly use the swiotlb. If we can't
49 * allocate a contiguous 1MB, we're probably in trouble anyway.
50 */
51#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
52
John W. Linvillede69e0f2005-09-29 14:44:57 -070053/*
54 * Enumeration for sync targets
55 */
56enum dma_sync_target {
57 SYNC_FOR_CPU = 0,
58 SYNC_FOR_DEVICE = 1,
59};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061int swiotlb_force;
62
63/*
64 * Used to do a quick range check in swiotlb_unmap_single and
65 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
66 * API.
67 */
68static char *io_tlb_start, *io_tlb_end;
69
70/*
71 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
72 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
73 */
74static unsigned long io_tlb_nslabs;
75
76/*
77 * When the IOMMU overflows we return a fallback buffer. This sets the size.
78 */
79static unsigned long io_tlb_overflow = 32*1024;
80
81void *io_tlb_overflow_buffer;
82
83/*
84 * This is a free list describing the number of free entries available from
85 * each index
86 */
87static unsigned int *io_tlb_list;
88static unsigned int io_tlb_index;
89
90/*
91 * We need to save away the original address corresponding to a mapped entry
92 * for the sync operations.
93 */
Tony Luck25667d62007-03-06 13:31:45 -080094static unsigned char **io_tlb_orig_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
97 * Protect the above data structures in the map and unmap calls
98 */
99static DEFINE_SPINLOCK(io_tlb_lock);
100
101static int __init
102setup_io_tlb_npages(char *str)
103{
104 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700105 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /* avoid tail segment of size < IO_TLB_SEGSIZE */
107 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
108 }
109 if (*str == ',')
110 ++str;
111 if (!strcmp(str, "force"))
112 swiotlb_force = 1;
113 return 1;
114}
115__setup("swiotlb=", setup_io_tlb_npages);
116/* make io_tlb_overflow tunable too? */
117
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800118void * __weak swiotlb_alloc_boot(size_t size, unsigned long nslabs)
119{
120 return alloc_bootmem_low_pages(size);
121}
122
123void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs)
124{
125 return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
126}
127
Ian Campbelle08e1f72008-12-16 12:17:30 -0800128dma_addr_t __weak swiotlb_phys_to_bus(phys_addr_t paddr)
129{
130 return paddr;
131}
132
133phys_addr_t __weak swiotlb_bus_to_phys(dma_addr_t baddr)
134{
135 return baddr;
136}
137
138static dma_addr_t swiotlb_virt_to_bus(volatile void *address)
139{
140 return swiotlb_phys_to_bus(virt_to_phys(address));
141}
142
143static void *swiotlb_bus_to_virt(dma_addr_t address)
144{
145 return phys_to_virt(swiotlb_bus_to_phys(address));
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/*
149 * Statically reserve bounce buffer space and initialize bounce buffer data
Tony Luck17e5ad62005-09-29 15:52:13 -0700150 * structures for the software IO TLB used to implement the DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800152void __init
153swiotlb_init_with_default_size(size_t default_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Jan Beulich563aaf02007-02-05 18:51:25 -0800155 unsigned long i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (!io_tlb_nslabs) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700158 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
160 }
161
Jan Beulich563aaf02007-02-05 18:51:25 -0800162 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * Get IO TLB memory from the low pages
166 */
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800167 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (!io_tlb_start)
169 panic("Cannot allocate SWIOTLB buffer");
Jan Beulich563aaf02007-02-05 18:51:25 -0800170 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /*
173 * Allocate and initialize the free list array. This array is used
174 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
175 * between io_tlb_start and io_tlb_end.
176 */
177 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
Tony Luck25667d62007-03-06 13:31:45 -0800178 for (i = 0; i < io_tlb_nslabs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
180 io_tlb_index = 0;
Tony Luck25667d62007-03-06 13:31:45 -0800181 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /*
184 * Get the overflow emergency buffer
185 */
186 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
Jan Beulich563aaf02007-02-05 18:51:25 -0800187 if (!io_tlb_overflow_buffer)
188 panic("Cannot allocate SWIOTLB overflow buffer!\n");
189
Tony Luck25667d62007-03-06 13:31:45 -0800190 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
Ian Campbelle08e1f72008-12-16 12:17:30 -0800191 swiotlb_virt_to_bus(io_tlb_start), swiotlb_virt_to_bus(io_tlb_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Jan Beulich563aaf02007-02-05 18:51:25 -0800194void __init
195swiotlb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Tony Luck25667d62007-03-06 13:31:45 -0800197 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Alex Williamson0b9afed2005-09-06 11:20:49 -0600200/*
201 * Systems with larger DMA zones (those that don't support ISA) can
202 * initialize the swiotlb later using the slab allocator if needed.
203 * This should be just like above, but with some error catching.
204 */
205int
Jan Beulich563aaf02007-02-05 18:51:25 -0800206swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600207{
Jan Beulich563aaf02007-02-05 18:51:25 -0800208 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600209 unsigned int order;
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 /*
217 * Get IO TLB memory from the low pages
218 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800219 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600220 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800221 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600222
223 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800224 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600225 if (io_tlb_start)
226 break;
227 order--;
228 }
229
230 if (!io_tlb_start)
231 goto cleanup1;
232
Jan Beulich563aaf02007-02-05 18:51:25 -0800233 if (order != get_order(bytes)) {
Alex Williamson0b9afed2005-09-06 11:20:49 -0600234 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
235 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
236 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800237 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600238 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800239 io_tlb_end = io_tlb_start + bytes;
240 memset(io_tlb_start, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600241
242 /*
243 * Allocate and initialize the free list array. This array is used
244 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
245 * between io_tlb_start and io_tlb_end.
246 */
247 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
248 get_order(io_tlb_nslabs * sizeof(int)));
249 if (!io_tlb_list)
250 goto cleanup2;
251
252 for (i = 0; i < io_tlb_nslabs; i++)
253 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
254 io_tlb_index = 0;
255
Tony Luck25667d62007-03-06 13:31:45 -0800256 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
257 get_order(io_tlb_nslabs * sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600258 if (!io_tlb_orig_addr)
259 goto cleanup3;
260
Tony Luck25667d62007-03-06 13:31:45 -0800261 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600262
263 /*
264 * Get the overflow emergency buffer
265 */
266 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
267 get_order(io_tlb_overflow));
268 if (!io_tlb_overflow_buffer)
269 goto cleanup4;
270
Tony Luck25667d62007-03-06 13:31:45 -0800271 printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - "
272 "0x%lx\n", bytes >> 20,
Ian Campbelle08e1f72008-12-16 12:17:30 -0800273 swiotlb_virt_to_bus(io_tlb_start), swiotlb_virt_to_bus(io_tlb_end));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600274
275 return 0;
276
277cleanup4:
Tony Luck25667d62007-03-06 13:31:45 -0800278 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
279 sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600280 io_tlb_orig_addr = NULL;
281cleanup3:
Tony Luck25667d62007-03-06 13:31:45 -0800282 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
283 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600284 io_tlb_list = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600285cleanup2:
Jan Beulich563aaf02007-02-05 18:51:25 -0800286 io_tlb_end = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600287 free_pages((unsigned long)io_tlb_start, order);
288 io_tlb_start = NULL;
289cleanup1:
290 io_tlb_nslabs = req_nslabs;
291 return -ENOMEM;
292}
293
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800294static int
FUJITA Tomonori27979822008-09-10 01:06:49 +0900295address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
FUJITA Tomonori07a2c012008-09-19 02:02:05 +0900297 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900300static int is_swiotlb_buffer(char *addr)
301{
302 return addr >= io_tlb_start && addr < io_tlb_end;
303}
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305/*
306 * Allocates bounce buffer and returns its kernel virtual address.
307 */
308static void *
Tony Luck25667d62007-03-06 13:31:45 -0800309map_single(struct device *hwdev, char *buffer, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 unsigned long flags;
312 char *dma_addr;
313 unsigned int nslots, stride, index, wrap;
314 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800315 unsigned long start_dma_addr;
316 unsigned long mask;
317 unsigned long offset_slots;
318 unsigned long max_slots;
319
320 mask = dma_get_seg_boundary(hwdev);
Ian Campbelle08e1f72008-12-16 12:17:30 -0800321 start_dma_addr = swiotlb_virt_to_bus(io_tlb_start) & mask;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800322
323 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Ian Campbella5ddde42008-12-16 12:17:29 -0800324
325 /*
326 * Carefully handle integer overflow which can occur when mask == ~0UL.
327 */
Jan Beulichb15a3892008-03-13 09:13:30 +0000328 max_slots = mask + 1
329 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
330 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /*
333 * For mappings greater than a page, we limit the stride (and
334 * hence alignment) to a page size.
335 */
336 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
337 if (size > PAGE_SIZE)
338 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
339 else
340 stride = 1;
341
Eric Sesterhenn34814542006-03-24 18:47:11 +0100342 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /*
345 * Find suitable number of IO TLB entries size that will fit this
346 * request and allocate a buffer from that IO TLB pool.
347 */
348 spin_lock_irqsave(&io_tlb_lock, flags);
Andrew Mortona7133a12008-04-29 00:59:36 -0700349 index = ALIGN(io_tlb_index, stride);
350 if (index >= io_tlb_nslabs)
351 index = 0;
352 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Andrew Mortona7133a12008-04-29 00:59:36 -0700354 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700355 while (iommu_is_span_boundary(index, nslots, offset_slots,
356 max_slots)) {
Jan Beulichb15a3892008-03-13 09:13:30 +0000357 index += stride;
358 if (index >= io_tlb_nslabs)
359 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700360 if (index == wrap)
361 goto not_found;
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Andrew Mortona7133a12008-04-29 00:59:36 -0700364 /*
365 * If we find a slot that indicates we have 'nslots' number of
366 * contiguous buffers, we allocate the buffers from that slot
367 * and mark the entries as '0' indicating unavailable.
368 */
369 if (io_tlb_list[index] >= nslots) {
370 int count = 0;
371
372 for (i = index; i < (int) (index + nslots); i++)
373 io_tlb_list[i] = 0;
374 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
375 io_tlb_list[i] = ++count;
376 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
377
378 /*
379 * Update the indices to avoid searching in the next
380 * round.
381 */
382 io_tlb_index = ((index + nslots) < io_tlb_nslabs
383 ? (index + nslots) : 0);
384
385 goto found;
386 }
387 index += stride;
388 if (index >= io_tlb_nslabs)
389 index = 0;
390 } while (index != wrap);
391
392not_found:
393 spin_unlock_irqrestore(&io_tlb_lock, flags);
394 return NULL;
395found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_unlock_irqrestore(&io_tlb_lock, flags);
397
398 /*
399 * Save away the mapping from the original address to the DMA address.
400 * This is needed when we sync the memory. Then we sync the buffer if
401 * needed.
402 */
Keir Fraserdf336d12007-07-21 04:37:24 -0700403 for (i = 0; i < nslots; i++)
404 io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
Tony Luck25667d62007-03-06 13:31:45 -0800406 memcpy(dma_addr, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 return dma_addr;
409}
410
411/*
412 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
413 */
414static void
415unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
416{
417 unsigned long flags;
418 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
419 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800420 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /*
423 * First, sync the memory before unmapping the entry
424 */
Tony Luck25667d62007-03-06 13:31:45 -0800425 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /*
427 * bounce... copy the data back into the original buffer * and
428 * delete the bounce buffer.
429 */
Tony Luck25667d62007-03-06 13:31:45 -0800430 memcpy(buffer, dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
433 * Return the buffer to the free list by setting the corresponding
434 * entries to indicate the number of contigous entries available.
435 * While returning the entries to the free list, we merge the entries
436 * with slots below and above the pool being returned.
437 */
438 spin_lock_irqsave(&io_tlb_lock, flags);
439 {
440 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
441 io_tlb_list[index + nslots] : 0);
442 /*
443 * Step 1: return the slots to the free list, merging the
444 * slots with superceeding slots
445 */
446 for (i = index + nslots - 1; i >= index; i--)
447 io_tlb_list[i] = ++count;
448 /*
449 * Step 2: merge the returned slots with the preceding slots,
450 * if available (non zero)
451 */
452 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
453 io_tlb_list[i] = ++count;
454 }
455 spin_unlock_irqrestore(&io_tlb_lock, flags);
456}
457
458static void
John W. Linvillede69e0f2005-09-29 14:44:57 -0700459sync_single(struct device *hwdev, char *dma_addr, size_t size,
460 int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800463 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Keir Fraserdf336d12007-07-21 04:37:24 -0700465 buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
466
John W. Linvillede69e0f2005-09-29 14:44:57 -0700467 switch (target) {
468 case SYNC_FOR_CPU:
469 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Tony Luck25667d62007-03-06 13:31:45 -0800470 memcpy(buffer, dma_addr, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100471 else
472 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700473 break;
474 case SYNC_FOR_DEVICE:
475 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Tony Luck25667d62007-03-06 13:31:45 -0800476 memcpy(dma_addr, buffer, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100477 else
478 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700479 break;
480 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485void *
486swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Al Viro06a54492005-10-21 03:21:03 -0400487 dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Jan Beulich563aaf02007-02-05 18:51:25 -0800489 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 void *ret;
491 int order = get_order(size);
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900492 u64 dma_mask = DMA_32BIT_MASK;
493
494 if (hwdev && hwdev->coherent_dma_mask)
495 dma_mask = hwdev->coherent_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Tony Luck25667d62007-03-06 13:31:45 -0800497 ret = (void *)__get_free_pages(flags, order);
Ian Campbelle08e1f72008-12-16 12:17:30 -0800498 if (ret && !is_buffer_dma_capable(dma_mask, swiotlb_virt_to_bus(ret), size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * The allocated memory isn't reachable by the device.
501 * Fall back on swiotlb_map_single().
502 */
503 free_pages((unsigned long) ret, order);
504 ret = NULL;
505 }
506 if (!ret) {
507 /*
508 * We are either out of memory or the device can't DMA
509 * to GFP_DMA memory; fall back on
510 * swiotlb_map_single(), which will grab memory from
511 * the lowest available address range.
512 */
FUJITA Tomonori9dfda122008-09-08 18:53:48 +0900513 ret = map_single(hwdev, NULL, size, DMA_FROM_DEVICE);
514 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
518 memset(ret, 0, size);
Ian Campbelle08e1f72008-12-16 12:17:30 -0800519 dev_addr = swiotlb_virt_to_bus(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /* Confirm address can be DMA'd by device */
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900522 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
Jan Beulich563aaf02007-02-05 18:51:25 -0800523 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900524 (unsigned long long)dma_mask,
Jan Beulich563aaf02007-02-05 18:51:25 -0800525 (unsigned long long)dev_addr);
FUJITA Tomonoria2b89b52008-10-23 18:42:03 +0900526
527 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
528 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
529 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 *dma_handle = dev_addr;
532 return ret;
533}
534
535void
536swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
537 dma_addr_t dma_handle)
538{
David Brownellaa248862007-08-10 13:10:27 -0700539 WARN_ON(irqs_disabled());
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900540 if (!is_swiotlb_buffer(vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 free_pages((unsigned long) vaddr, get_order(size));
542 else
543 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
FUJITA Tomonori21f6c4d2008-09-08 18:53:49 +0900544 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547static void
548swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
549{
550 /*
551 * Ran out of IOMMU space for this operation. This is very bad.
552 * Unfortunately the drivers cannot handle this operation properly.
Tony Luck17e5ad62005-09-29 15:52:13 -0700553 * unless they check for dma_mapping_error (most don't)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 * When the mapping is small enough return a static buffer to limit
555 * the damage, or panic when the transfer is too big.
556 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800557 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 "device %s\n", size, dev ? dev->bus_id : "?");
559
560 if (size > io_tlb_overflow && do_panic) {
Tony Luck17e5ad62005-09-29 15:52:13 -0700561 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
562 panic("DMA: Memory would be corrupted\n");
563 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
564 panic("DMA: Random memory would be DMAed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566}
567
568/*
569 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700570 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 *
572 * Once the device is given the dma address, the device owns this memory until
573 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
574 */
575dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -0700576swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
577 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800579 dma_addr_t dev_addr = swiotlb_virt_to_bus(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 void *map;
581
Eric Sesterhenn34814542006-03-24 18:47:11 +0100582 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /*
584 * If the pointer passed in happens to be in the device's DMA window,
585 * we can safely return the device addr and not worry about bounce
586 * buffering it.
587 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900588 if (!address_needs_mapping(hwdev, dev_addr, size) && !swiotlb_force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return dev_addr;
590
591 /*
592 * Oh well, have to allocate and map a bounce buffer.
593 */
Tony Luck25667d62007-03-06 13:31:45 -0800594 map = map_single(hwdev, ptr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!map) {
596 swiotlb_full(hwdev, size, dir, 1);
597 map = io_tlb_overflow_buffer;
598 }
599
Ian Campbelle08e1f72008-12-16 12:17:30 -0800600 dev_addr = swiotlb_virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /*
603 * Ensure that the address returned is DMA'ble
604 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900605 if (address_needs_mapping(hwdev, dev_addr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 panic("map_single: bounce buffer is not DMA'ble");
607
608 return dev_addr;
609}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700610EXPORT_SYMBOL(swiotlb_map_single_attrs);
611
612dma_addr_t
613swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
614{
615 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
616}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * Unmap a single streaming mode DMA translation. The dma_addr and size must
620 * match what was provided for in a previous swiotlb_map_single call. All
621 * other usages are undefined.
622 *
623 * After this call, reads by the cpu to the buffer are guaranteed to see
624 * whatever the device wrote there.
625 */
626void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700627swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
628 size_t size, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800630 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Eric Sesterhenn34814542006-03-24 18:47:11 +0100632 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900633 if (is_swiotlb_buffer(dma_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 unmap_single(hwdev, dma_addr, size, dir);
635 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800636 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700638EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Arthur Kepner309df0c2008-04-29 01:00:32 -0700640void
641swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
642 int dir)
643{
644 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
645}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/*
647 * Make physical memory consistent for a single streaming mode DMA translation
648 * after a transfer.
649 *
650 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700651 * using the cpu, yet do not wish to teardown the dma mapping, you must
652 * call this function before doing so. At the next point you give the dma
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 * address back to the card, you must first perform a
654 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
655 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800656static void
John W. Linville8270f3f2005-09-29 14:43:32 -0700657swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700658 size_t size, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800660 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Eric Sesterhenn34814542006-03-24 18:47:11 +0100662 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900663 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700664 sync_single(hwdev, dma_addr, size, dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800666 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669void
John W. Linville8270f3f2005-09-29 14:43:32 -0700670swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
671 size_t size, int dir)
672{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700673 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700674}
675
676void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
678 size_t size, int dir)
679{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700680 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/*
John W. Linville878a97c2005-09-29 14:44:23 -0700684 * Same as above, but for a sub-range of the mapping.
685 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800686static void
John W. Linville878a97c2005-09-29 14:44:23 -0700687swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700688 unsigned long offset, size_t size,
689 int dir, int target)
John W. Linville878a97c2005-09-29 14:44:23 -0700690{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800691 char *dma_addr = swiotlb_bus_to_virt(dev_addr) + offset;
John W. Linville878a97c2005-09-29 14:44:23 -0700692
Eric Sesterhenn34814542006-03-24 18:47:11 +0100693 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900694 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700695 sync_single(hwdev, dma_addr, size, dir, target);
John W. Linville878a97c2005-09-29 14:44:23 -0700696 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800697 dma_mark_clean(dma_addr, size);
John W. Linville878a97c2005-09-29 14:44:23 -0700698}
699
700void
701swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
702 unsigned long offset, size_t size, int dir)
703{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700704 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
705 SYNC_FOR_CPU);
John W. Linville878a97c2005-09-29 14:44:23 -0700706}
707
708void
709swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
710 unsigned long offset, size_t size, int dir)
711{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700712 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
713 SYNC_FOR_DEVICE);
John W. Linville878a97c2005-09-29 14:44:23 -0700714}
715
Arthur Kepner309df0c2008-04-29 01:00:32 -0700716void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
717 struct dma_attrs *);
John W. Linville878a97c2005-09-29 14:44:23 -0700718/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * Map a set of buffers described by scatterlist in streaming mode for DMA.
720 * This is the scatter-gather version of the above swiotlb_map_single
721 * interface. Here the scatter gather list elements are each tagged with the
722 * appropriate dma address and length. They are obtained via
723 * sg_dma_{address,length}(SG).
724 *
725 * NOTE: An implementation may be able to use a smaller number of
726 * DMA address/length pairs than there are SG table elements.
727 * (for example via virtual mapping capabilities)
728 * The routine returns the number of addr/length pairs actually
729 * used, at most nents.
730 *
731 * Device ownership issues as mentioned above for swiotlb_map_single are the
732 * same here.
733 */
734int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700735swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
736 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200738 struct scatterlist *sg;
Tony Luck25667d62007-03-06 13:31:45 -0800739 void *addr;
Jan Beulich563aaf02007-02-05 18:51:25 -0800740 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int i;
742
Eric Sesterhenn34814542006-03-24 18:47:11 +0100743 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Jens Axboedbfd49f2007-05-11 14:56:18 +0200745 for_each_sg(sgl, sg, nelems, i) {
Tony Luck25667d62007-03-06 13:31:45 -0800746 addr = SG_ENT_VIRT_ADDRESS(sg);
Ian Campbelle08e1f72008-12-16 12:17:30 -0800747 dev_addr = swiotlb_virt_to_bus(addr);
FUJITA Tomonori27979822008-09-10 01:06:49 +0900748 if (swiotlb_force ||
749 address_needs_mapping(hwdev, dev_addr, sg->length)) {
Tony Luck25667d62007-03-06 13:31:45 -0800750 void *map = map_single(hwdev, addr, sg->length, dir);
Andi Kleen7e870232005-12-20 14:45:19 +0100751 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* Don't panic here, we expect map_sg users
753 to do proper error handling. */
754 swiotlb_full(hwdev, sg->length, dir, 0);
Arthur Kepner309df0c2008-04-29 01:00:32 -0700755 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
756 attrs);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200757 sgl[0].dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759 }
Ian Campbelle08e1f72008-12-16 12:17:30 -0800760 sg->dma_address = swiotlb_virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 } else
762 sg->dma_address = dev_addr;
763 sg->dma_length = sg->length;
764 }
765 return nelems;
766}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700767EXPORT_SYMBOL(swiotlb_map_sg_attrs);
768
769int
770swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
771 int dir)
772{
773 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
774}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776/*
777 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
778 * concerning calls here are the same as for swiotlb_unmap_single() above.
779 */
780void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700781swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
782 int nelems, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200784 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int i;
786
Eric Sesterhenn34814542006-03-24 18:47:11 +0100787 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Jens Axboedbfd49f2007-05-11 14:56:18 +0200789 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Ian Campbelle08e1f72008-12-16 12:17:30 -0800791 unmap_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
Jan Beulich93fbff62007-02-05 18:49:45 -0800792 sg->dma_length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800794 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700797EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
798
799void
800swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
801 int dir)
802{
803 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
804}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806/*
807 * Make physical memory consistent for a set of streaming mode DMA translations
808 * after a transfer.
809 *
810 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
811 * and usage.
812 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800813static void
Jens Axboedbfd49f2007-05-11 14:56:18 +0200814swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700815 int nelems, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200817 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int i;
819
Eric Sesterhenn34814542006-03-24 18:47:11 +0100820 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Jens Axboedbfd49f2007-05-11 14:56:18 +0200822 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Ian Campbelle08e1f72008-12-16 12:17:30 -0800824 sync_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
John W. Linvillede69e0f2005-09-29 14:44:57 -0700825 sg->dma_length, dir, target);
Jan Beulichcde14bb2007-02-05 18:46:40 -0800826 else if (dir == DMA_FROM_DEVICE)
827 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831void
John W. Linville8270f3f2005-09-29 14:43:32 -0700832swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
833 int nelems, int dir)
834{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700835 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700836}
837
838void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
840 int nelems, int dir)
841{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700842 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700846swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800848 return (dma_addr == swiotlb_virt_to_bus(io_tlb_overflow_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700852 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700854 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * this function.
856 */
857int
Jan Beulich563aaf02007-02-05 18:51:25 -0800858swiotlb_dma_supported(struct device *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800860 return swiotlb_virt_to_bus(io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863EXPORT_SYMBOL(swiotlb_map_single);
864EXPORT_SYMBOL(swiotlb_unmap_single);
865EXPORT_SYMBOL(swiotlb_map_sg);
866EXPORT_SYMBOL(swiotlb_unmap_sg);
867EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
868EXPORT_SYMBOL(swiotlb_sync_single_for_device);
John W. Linville878a97c2005-09-29 14:44:23 -0700869EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
870EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
872EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
873EXPORT_SYMBOL(swiotlb_dma_mapping_error);
Tony Luck25667d62007-03-06 13:31:45 -0800874EXPORT_SYMBOL(swiotlb_alloc_coherent);
875EXPORT_SYMBOL(swiotlb_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876EXPORT_SYMBOL(swiotlb_dma_supported);