blob: 8826fdf0f1801029e4b5e43555af44b5da203aae [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>
24#include <linux/string.h>
25#include <linux/types.h>
26#include <linux/ctype.h>
27
28#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/dma.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070030#include <asm/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/init.h>
33#include <linux/bootmem.h>
FUJITA Tomonoria8522502008-04-29 00:59:36 -070034#include <linux/iommu-helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#define OFFSET(val,align) ((unsigned long) \
37 ( (val) & ( (align) - 1)))
38
Jens Axboef9527f12007-10-22 19:44:53 +020039#define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
Jan Beulich93fbff62007-02-05 18:49:45 -080040#define SG_ENT_PHYS_ADDRESS(sg) virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Maximum allowable number of contiguous slabs to map,
44 * must be a power of 2. What is the appropriate value ?
45 * The complexity of {map,unmap}_single is linearly dependent on this value.
46 */
47#define IO_TLB_SEGSIZE 128
48
49/*
50 * log of the size of each IO TLB slab. The number of slabs is command line
51 * controllable.
52 */
53#define IO_TLB_SHIFT 11
54
Alex Williamson0b9afed2005-09-06 11:20:49 -060055#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
56
57/*
58 * Minimum IO TLB size to bother booting with. Systems with mainly
59 * 64bit capable cards will only lightly use the swiotlb. If we can't
60 * allocate a contiguous 1MB, we're probably in trouble anyway.
61 */
62#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
63
John W. Linvillede69e0f2005-09-29 14:44:57 -070064/*
65 * Enumeration for sync targets
66 */
67enum dma_sync_target {
68 SYNC_FOR_CPU = 0,
69 SYNC_FOR_DEVICE = 1,
70};
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072int swiotlb_force;
73
74/*
75 * Used to do a quick range check in swiotlb_unmap_single and
76 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
77 * API.
78 */
79static char *io_tlb_start, *io_tlb_end;
80
81/*
82 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
83 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
84 */
85static unsigned long io_tlb_nslabs;
86
87/*
88 * When the IOMMU overflows we return a fallback buffer. This sets the size.
89 */
90static unsigned long io_tlb_overflow = 32*1024;
91
92void *io_tlb_overflow_buffer;
93
94/*
95 * This is a free list describing the number of free entries available from
96 * each index
97 */
98static unsigned int *io_tlb_list;
99static unsigned int io_tlb_index;
100
101/*
102 * We need to save away the original address corresponding to a mapped entry
103 * for the sync operations.
104 */
Tony Luck25667d62007-03-06 13:31:45 -0800105static unsigned char **io_tlb_orig_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * Protect the above data structures in the map and unmap calls
109 */
110static DEFINE_SPINLOCK(io_tlb_lock);
111
112static int __init
113setup_io_tlb_npages(char *str)
114{
115 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700116 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* avoid tail segment of size < IO_TLB_SEGSIZE */
118 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
119 }
120 if (*str == ',')
121 ++str;
122 if (!strcmp(str, "force"))
123 swiotlb_force = 1;
124 return 1;
125}
126__setup("swiotlb=", setup_io_tlb_npages);
127/* make io_tlb_overflow tunable too? */
128
129/*
130 * Statically reserve bounce buffer space and initialize bounce buffer data
Tony Luck17e5ad62005-09-29 15:52:13 -0700131 * structures for the software IO TLB used to implement the DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800133void __init
134swiotlb_init_with_default_size(size_t default_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Jan Beulich563aaf02007-02-05 18:51:25 -0800136 unsigned long i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 if (!io_tlb_nslabs) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700139 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
141 }
142
Jan Beulich563aaf02007-02-05 18:51:25 -0800143 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /*
146 * Get IO TLB memory from the low pages
147 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800148 io_tlb_start = alloc_bootmem_low_pages(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!io_tlb_start)
150 panic("Cannot allocate SWIOTLB buffer");
Jan Beulich563aaf02007-02-05 18:51:25 -0800151 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /*
154 * Allocate and initialize the free list array. This array is used
155 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
156 * between io_tlb_start and io_tlb_end.
157 */
158 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
Tony Luck25667d62007-03-06 13:31:45 -0800159 for (i = 0; i < io_tlb_nslabs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
161 io_tlb_index = 0;
Tony Luck25667d62007-03-06 13:31:45 -0800162 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /*
165 * Get the overflow emergency buffer
166 */
167 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
Jan Beulich563aaf02007-02-05 18:51:25 -0800168 if (!io_tlb_overflow_buffer)
169 panic("Cannot allocate SWIOTLB overflow buffer!\n");
170
Tony Luck25667d62007-03-06 13:31:45 -0800171 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
172 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Jan Beulich563aaf02007-02-05 18:51:25 -0800175void __init
176swiotlb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Tony Luck25667d62007-03-06 13:31:45 -0800178 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Alex Williamson0b9afed2005-09-06 11:20:49 -0600181/*
182 * Systems with larger DMA zones (those that don't support ISA) can
183 * initialize the swiotlb later using the slab allocator if needed.
184 * This should be just like above, but with some error catching.
185 */
186int
Jan Beulich563aaf02007-02-05 18:51:25 -0800187swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600188{
Jan Beulich563aaf02007-02-05 18:51:25 -0800189 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600190 unsigned int order;
191
192 if (!io_tlb_nslabs) {
193 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
194 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
195 }
196
197 /*
198 * Get IO TLB memory from the low pages
199 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800200 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600201 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800202 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600203
204 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
205 io_tlb_start = (char *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
206 order);
207 if (io_tlb_start)
208 break;
209 order--;
210 }
211
212 if (!io_tlb_start)
213 goto cleanup1;
214
Jan Beulich563aaf02007-02-05 18:51:25 -0800215 if (order != get_order(bytes)) {
Alex Williamson0b9afed2005-09-06 11:20:49 -0600216 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
217 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
218 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800219 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600220 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800221 io_tlb_end = io_tlb_start + bytes;
222 memset(io_tlb_start, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600223
224 /*
225 * Allocate and initialize the free list array. This array is used
226 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
227 * between io_tlb_start and io_tlb_end.
228 */
229 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
230 get_order(io_tlb_nslabs * sizeof(int)));
231 if (!io_tlb_list)
232 goto cleanup2;
233
234 for (i = 0; i < io_tlb_nslabs; i++)
235 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
236 io_tlb_index = 0;
237
Tony Luck25667d62007-03-06 13:31:45 -0800238 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
239 get_order(io_tlb_nslabs * sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600240 if (!io_tlb_orig_addr)
241 goto cleanup3;
242
Tony Luck25667d62007-03-06 13:31:45 -0800243 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600244
245 /*
246 * Get the overflow emergency buffer
247 */
248 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
249 get_order(io_tlb_overflow));
250 if (!io_tlb_overflow_buffer)
251 goto cleanup4;
252
Tony Luck25667d62007-03-06 13:31:45 -0800253 printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - "
254 "0x%lx\n", bytes >> 20,
255 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600256
257 return 0;
258
259cleanup4:
Tony Luck25667d62007-03-06 13:31:45 -0800260 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
261 sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600262 io_tlb_orig_addr = NULL;
263cleanup3:
Tony Luck25667d62007-03-06 13:31:45 -0800264 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
265 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600266 io_tlb_list = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600267cleanup2:
Jan Beulich563aaf02007-02-05 18:51:25 -0800268 io_tlb_end = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600269 free_pages((unsigned long)io_tlb_start, order);
270 io_tlb_start = NULL;
271cleanup1:
272 io_tlb_nslabs = req_nslabs;
273 return -ENOMEM;
274}
275
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800276static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277address_needs_mapping(struct device *hwdev, dma_addr_t addr)
278{
279 dma_addr_t mask = 0xffffffff;
280 /* If the device has a mask, use it, otherwise default to 32 bits */
281 if (hwdev && hwdev->dma_mask)
282 mask = *hwdev->dma_mask;
283 return (addr & ~mask) != 0;
284}
285
286/*
287 * Allocates bounce buffer and returns its kernel virtual address.
288 */
289static void *
Tony Luck25667d62007-03-06 13:31:45 -0800290map_single(struct device *hwdev, char *buffer, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned long flags;
293 char *dma_addr;
294 unsigned int nslots, stride, index, wrap;
295 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800296 unsigned long start_dma_addr;
297 unsigned long mask;
298 unsigned long offset_slots;
299 unsigned long max_slots;
300
301 mask = dma_get_seg_boundary(hwdev);
302 start_dma_addr = virt_to_bus(io_tlb_start) & mask;
303
304 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Jan Beulichb15a38912008-03-13 09:13:30 +0000305 max_slots = mask + 1
306 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
307 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /*
310 * For mappings greater than a page, we limit the stride (and
311 * hence alignment) to a page size.
312 */
313 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
314 if (size > PAGE_SIZE)
315 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
316 else
317 stride = 1;
318
Eric Sesterhenn34814542006-03-24 18:47:11 +0100319 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /*
322 * Find suitable number of IO TLB entries size that will fit this
323 * request and allocate a buffer from that IO TLB pool.
324 */
325 spin_lock_irqsave(&io_tlb_lock, flags);
Andrew Mortona7133a12008-04-29 00:59:36 -0700326 index = ALIGN(io_tlb_index, stride);
327 if (index >= io_tlb_nslabs)
328 index = 0;
329 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Andrew Mortona7133a12008-04-29 00:59:36 -0700331 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700332 while (iommu_is_span_boundary(index, nslots, offset_slots,
333 max_slots)) {
Jan Beulichb15a38912008-03-13 09:13:30 +0000334 index += stride;
335 if (index >= io_tlb_nslabs)
336 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700337 if (index == wrap)
338 goto not_found;
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Andrew Mortona7133a12008-04-29 00:59:36 -0700341 /*
342 * If we find a slot that indicates we have 'nslots' number of
343 * contiguous buffers, we allocate the buffers from that slot
344 * and mark the entries as '0' indicating unavailable.
345 */
346 if (io_tlb_list[index] >= nslots) {
347 int count = 0;
348
349 for (i = index; i < (int) (index + nslots); i++)
350 io_tlb_list[i] = 0;
351 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
352 io_tlb_list[i] = ++count;
353 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
354
355 /*
356 * Update the indices to avoid searching in the next
357 * round.
358 */
359 io_tlb_index = ((index + nslots) < io_tlb_nslabs
360 ? (index + nslots) : 0);
361
362 goto found;
363 }
364 index += stride;
365 if (index >= io_tlb_nslabs)
366 index = 0;
367 } while (index != wrap);
368
369not_found:
370 spin_unlock_irqrestore(&io_tlb_lock, flags);
371 return NULL;
372found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 spin_unlock_irqrestore(&io_tlb_lock, flags);
374
375 /*
376 * Save away the mapping from the original address to the DMA address.
377 * This is needed when we sync the memory. Then we sync the buffer if
378 * needed.
379 */
Keir Fraserdf336d12007-07-21 04:37:24 -0700380 for (i = 0; i < nslots; i++)
381 io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
Tony Luck25667d62007-03-06 13:31:45 -0800383 memcpy(dma_addr, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 return dma_addr;
386}
387
388/*
389 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
390 */
391static void
392unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
393{
394 unsigned long flags;
395 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
396 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800397 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
400 * First, sync the memory before unmapping the entry
401 */
Tony Luck25667d62007-03-06 13:31:45 -0800402 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
404 * bounce... copy the data back into the original buffer * and
405 * delete the bounce buffer.
406 */
Tony Luck25667d62007-03-06 13:31:45 -0800407 memcpy(buffer, dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
410 * Return the buffer to the free list by setting the corresponding
411 * entries to indicate the number of contigous entries available.
412 * While returning the entries to the free list, we merge the entries
413 * with slots below and above the pool being returned.
414 */
415 spin_lock_irqsave(&io_tlb_lock, flags);
416 {
417 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
418 io_tlb_list[index + nslots] : 0);
419 /*
420 * Step 1: return the slots to the free list, merging the
421 * slots with superceeding slots
422 */
423 for (i = index + nslots - 1; i >= index; i--)
424 io_tlb_list[i] = ++count;
425 /*
426 * Step 2: merge the returned slots with the preceding slots,
427 * if available (non zero)
428 */
429 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
430 io_tlb_list[i] = ++count;
431 }
432 spin_unlock_irqrestore(&io_tlb_lock, flags);
433}
434
435static void
John W. Linvillede69e0f2005-09-29 14:44:57 -0700436sync_single(struct device *hwdev, char *dma_addr, size_t size,
437 int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800440 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Keir Fraserdf336d12007-07-21 04:37:24 -0700442 buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
443
John W. Linvillede69e0f2005-09-29 14:44:57 -0700444 switch (target) {
445 case SYNC_FOR_CPU:
446 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Tony Luck25667d62007-03-06 13:31:45 -0800447 memcpy(buffer, dma_addr, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100448 else
449 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700450 break;
451 case SYNC_FOR_DEVICE:
452 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Tony Luck25667d62007-03-06 13:31:45 -0800453 memcpy(dma_addr, buffer, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100454 else
455 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700456 break;
457 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462void *
463swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Al Viro06a54492005-10-21 03:21:03 -0400464 dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Jan Beulich563aaf02007-02-05 18:51:25 -0800466 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 void *ret;
468 int order = get_order(size);
469
470 /*
471 * XXX fix me: the DMA API should pass us an explicit DMA mask
472 * instead, or use ZONE_DMA32 (ia64 overloads ZONE_DMA to be a ~32
473 * bit range instead of a 16MB one).
474 */
475 flags |= GFP_DMA;
476
Tony Luck25667d62007-03-06 13:31:45 -0800477 ret = (void *)__get_free_pages(flags, order);
Jan Beulich93fbff62007-02-05 18:49:45 -0800478 if (ret && address_needs_mapping(hwdev, virt_to_bus(ret))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /*
480 * The allocated memory isn't reachable by the device.
481 * Fall back on swiotlb_map_single().
482 */
483 free_pages((unsigned long) ret, order);
484 ret = NULL;
485 }
486 if (!ret) {
487 /*
488 * We are either out of memory or the device can't DMA
489 * to GFP_DMA memory; fall back on
490 * swiotlb_map_single(), which will grab memory from
491 * the lowest available address range.
492 */
493 dma_addr_t handle;
Daniel J Blueman36223a32008-09-10 21:07:55 +0100494 handle = swiotlb_map_single(hwdev, NULL, size, DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700495 if (swiotlb_dma_mapping_error(hwdev, handle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return NULL;
497
Jan Beulich93fbff62007-02-05 18:49:45 -0800498 ret = bus_to_virt(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
501 memset(ret, 0, size);
Jan Beulich93fbff62007-02-05 18:49:45 -0800502 dev_addr = virt_to_bus(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /* Confirm address can be DMA'd by device */
505 if (address_needs_mapping(hwdev, dev_addr)) {
Jan Beulich563aaf02007-02-05 18:51:25 -0800506 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
507 (unsigned long long)*hwdev->dma_mask,
508 (unsigned long long)dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 panic("swiotlb_alloc_coherent: allocated memory is out of "
510 "range for device");
511 }
512 *dma_handle = dev_addr;
513 return ret;
514}
515
516void
517swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
518 dma_addr_t dma_handle)
519{
David Brownellaa248862007-08-10 13:10:27 -0700520 WARN_ON(irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (!(vaddr >= (void *)io_tlb_start
522 && vaddr < (void *)io_tlb_end))
523 free_pages((unsigned long) vaddr, get_order(size));
524 else
525 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
526 swiotlb_unmap_single (hwdev, dma_handle, size, DMA_TO_DEVICE);
527}
528
529static void
530swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
531{
532 /*
533 * Ran out of IOMMU space for this operation. This is very bad.
534 * Unfortunately the drivers cannot handle this operation properly.
Tony Luck17e5ad62005-09-29 15:52:13 -0700535 * unless they check for dma_mapping_error (most don't)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * When the mapping is small enough return a static buffer to limit
537 * the damage, or panic when the transfer is too big.
538 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800539 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 "device %s\n", size, dev ? dev->bus_id : "?");
541
542 if (size > io_tlb_overflow && do_panic) {
Tony Luck17e5ad62005-09-29 15:52:13 -0700543 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
544 panic("DMA: Memory would be corrupted\n");
545 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
546 panic("DMA: Random memory would be DMAed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548}
549
550/*
551 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700552 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *
554 * Once the device is given the dma address, the device owns this memory until
555 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
556 */
557dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -0700558swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
559 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Jan Beulich563aaf02007-02-05 18:51:25 -0800561 dma_addr_t dev_addr = virt_to_bus(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 void *map;
563
Eric Sesterhenn34814542006-03-24 18:47:11 +0100564 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * If the pointer passed in happens to be in the device's DMA window,
567 * we can safely return the device addr and not worry about bounce
568 * buffering it.
569 */
Tony Luck25667d62007-03-06 13:31:45 -0800570 if (!address_needs_mapping(hwdev, dev_addr) && !swiotlb_force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return dev_addr;
572
573 /*
574 * Oh well, have to allocate and map a bounce buffer.
575 */
Tony Luck25667d62007-03-06 13:31:45 -0800576 map = map_single(hwdev, ptr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (!map) {
578 swiotlb_full(hwdev, size, dir, 1);
579 map = io_tlb_overflow_buffer;
580 }
581
Jan Beulich93fbff62007-02-05 18:49:45 -0800582 dev_addr = virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /*
585 * Ensure that the address returned is DMA'ble
586 */
587 if (address_needs_mapping(hwdev, dev_addr))
588 panic("map_single: bounce buffer is not DMA'ble");
589
590 return dev_addr;
591}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700592EXPORT_SYMBOL(swiotlb_map_single_attrs);
593
594dma_addr_t
595swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
596{
597 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * Unmap a single streaming mode DMA translation. The dma_addr and size must
602 * match what was provided for in a previous swiotlb_map_single call. All
603 * other usages are undefined.
604 *
605 * After this call, reads by the cpu to the buffer are guaranteed to see
606 * whatever the device wrote there.
607 */
608void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700609swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
610 size_t size, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Jan Beulich93fbff62007-02-05 18:49:45 -0800612 char *dma_addr = bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Eric Sesterhenn34814542006-03-24 18:47:11 +0100614 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
616 unmap_single(hwdev, dma_addr, size, dir);
617 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800618 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700620EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Arthur Kepner309df0c2008-04-29 01:00:32 -0700622void
623swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
624 int dir)
625{
626 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
627}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/*
629 * Make physical memory consistent for a single streaming mode DMA translation
630 * after a transfer.
631 *
632 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700633 * using the cpu, yet do not wish to teardown the dma mapping, you must
634 * call this function before doing so. At the next point you give the dma
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * address back to the card, you must first perform a
636 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
637 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800638static void
John W. Linville8270f3f2005-09-29 14:43:32 -0700639swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700640 size_t size, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Jan Beulich93fbff62007-02-05 18:49:45 -0800642 char *dma_addr = bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Eric Sesterhenn34814542006-03-24 18:47:11 +0100644 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
John W. Linvillede69e0f2005-09-29 14:44:57 -0700646 sync_single(hwdev, dma_addr, size, dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800648 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651void
John W. Linville8270f3f2005-09-29 14:43:32 -0700652swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
653 size_t size, int dir)
654{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700655 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700656}
657
658void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
660 size_t size, int dir)
661{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700662 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665/*
John W. Linville878a97c2005-09-29 14:44:23 -0700666 * Same as above, but for a sub-range of the mapping.
667 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800668static void
John W. Linville878a97c2005-09-29 14:44:23 -0700669swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700670 unsigned long offset, size_t size,
671 int dir, int target)
John W. Linville878a97c2005-09-29 14:44:23 -0700672{
Jan Beulich93fbff62007-02-05 18:49:45 -0800673 char *dma_addr = bus_to_virt(dev_addr) + offset;
John W. Linville878a97c2005-09-29 14:44:23 -0700674
Eric Sesterhenn34814542006-03-24 18:47:11 +0100675 BUG_ON(dir == DMA_NONE);
John W. Linville878a97c2005-09-29 14:44:23 -0700676 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
John W. Linvillede69e0f2005-09-29 14:44:57 -0700677 sync_single(hwdev, dma_addr, size, dir, target);
John W. Linville878a97c2005-09-29 14:44:23 -0700678 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800679 dma_mark_clean(dma_addr, size);
John W. Linville878a97c2005-09-29 14:44:23 -0700680}
681
682void
683swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
684 unsigned long offset, size_t size, int dir)
685{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700686 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
687 SYNC_FOR_CPU);
John W. Linville878a97c2005-09-29 14:44:23 -0700688}
689
690void
691swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
692 unsigned long offset, size_t size, int dir)
693{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700694 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
695 SYNC_FOR_DEVICE);
John W. Linville878a97c2005-09-29 14:44:23 -0700696}
697
Arthur Kepner309df0c2008-04-29 01:00:32 -0700698void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
699 struct dma_attrs *);
John W. Linville878a97c2005-09-29 14:44:23 -0700700/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 * Map a set of buffers described by scatterlist in streaming mode for DMA.
702 * This is the scatter-gather version of the above swiotlb_map_single
703 * interface. Here the scatter gather list elements are each tagged with the
704 * appropriate dma address and length. They are obtained via
705 * sg_dma_{address,length}(SG).
706 *
707 * NOTE: An implementation may be able to use a smaller number of
708 * DMA address/length pairs than there are SG table elements.
709 * (for example via virtual mapping capabilities)
710 * The routine returns the number of addr/length pairs actually
711 * used, at most nents.
712 *
713 * Device ownership issues as mentioned above for swiotlb_map_single are the
714 * same here.
715 */
716int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700717swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
718 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200720 struct scatterlist *sg;
Tony Luck25667d62007-03-06 13:31:45 -0800721 void *addr;
Jan Beulich563aaf02007-02-05 18:51:25 -0800722 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int i;
724
Eric Sesterhenn34814542006-03-24 18:47:11 +0100725 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Jens Axboedbfd49f2007-05-11 14:56:18 +0200727 for_each_sg(sgl, sg, nelems, i) {
Tony Luck25667d62007-03-06 13:31:45 -0800728 addr = SG_ENT_VIRT_ADDRESS(sg);
729 dev_addr = virt_to_bus(addr);
730 if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
731 void *map = map_single(hwdev, addr, sg->length, dir);
Andi Kleen7e870232005-12-20 14:45:19 +0100732 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /* Don't panic here, we expect map_sg users
734 to do proper error handling. */
735 swiotlb_full(hwdev, sg->length, dir, 0);
Arthur Kepner309df0c2008-04-29 01:00:32 -0700736 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
737 attrs);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200738 sgl[0].dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return 0;
740 }
Jan Beulichcde14bb2007-02-05 18:46:40 -0800741 sg->dma_address = virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 } else
743 sg->dma_address = dev_addr;
744 sg->dma_length = sg->length;
745 }
746 return nelems;
747}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700748EXPORT_SYMBOL(swiotlb_map_sg_attrs);
749
750int
751swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
752 int dir)
753{
754 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
755}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757/*
758 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
759 * concerning calls here are the same as for swiotlb_unmap_single() above.
760 */
761void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700762swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
763 int nelems, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200765 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int i;
767
Eric Sesterhenn34814542006-03-24 18:47:11 +0100768 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Jens Axboedbfd49f2007-05-11 14:56:18 +0200770 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Jan Beulich93fbff62007-02-05 18:49:45 -0800772 unmap_single(hwdev, bus_to_virt(sg->dma_address),
773 sg->dma_length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800775 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700778EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
779
780void
781swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
782 int dir)
783{
784 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
785}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787/*
788 * Make physical memory consistent for a set of streaming mode DMA translations
789 * after a transfer.
790 *
791 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
792 * and usage.
793 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800794static void
Jens Axboedbfd49f2007-05-11 14:56:18 +0200795swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700796 int nelems, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200798 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int i;
800
Eric Sesterhenn34814542006-03-24 18:47:11 +0100801 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Jens Axboedbfd49f2007-05-11 14:56:18 +0200803 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Jan Beulich93fbff62007-02-05 18:49:45 -0800805 sync_single(hwdev, bus_to_virt(sg->dma_address),
John W. Linvillede69e0f2005-09-29 14:44:57 -0700806 sg->dma_length, dir, target);
Jan Beulichcde14bb2007-02-05 18:46:40 -0800807 else if (dir == DMA_FROM_DEVICE)
808 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812void
John W. Linville8270f3f2005-09-29 14:43:32 -0700813swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
814 int nelems, int dir)
815{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700816 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700817}
818
819void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
821 int nelems, int dir)
822{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700823 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700827swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Jan Beulich93fbff62007-02-05 18:49:45 -0800829 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
832/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700833 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700835 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * this function.
837 */
838int
Jan Beulich563aaf02007-02-05 18:51:25 -0800839swiotlb_dma_supported(struct device *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Tony Luck25667d62007-03-06 13:31:45 -0800841 return virt_to_bus(io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844EXPORT_SYMBOL(swiotlb_map_single);
845EXPORT_SYMBOL(swiotlb_unmap_single);
846EXPORT_SYMBOL(swiotlb_map_sg);
847EXPORT_SYMBOL(swiotlb_unmap_sg);
848EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
849EXPORT_SYMBOL(swiotlb_sync_single_for_device);
John W. Linville878a97c2005-09-29 14:44:23 -0700850EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
851EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
853EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
854EXPORT_SYMBOL(swiotlb_dma_mapping_error);
Tony Luck25667d62007-03-06 13:31:45 -0800855EXPORT_SYMBOL(swiotlb_alloc_coherent);
856EXPORT_SYMBOL(swiotlb_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857EXPORT_SYMBOL(swiotlb_dma_supported);