blob: db724ba7ebf6d5fddccd711eaeff6ad164d16817 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/*
129 * Statically reserve bounce buffer space and initialize bounce buffer data
Tony Luck17e5ad62005-09-29 15:52:13 -0700130 * structures for the software IO TLB used to implement the DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800132void __init
133swiotlb_init_with_default_size(size_t default_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Jan Beulich563aaf02007-02-05 18:51:25 -0800135 unsigned long i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 if (!io_tlb_nslabs) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700138 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
140 }
141
Jan Beulich563aaf02007-02-05 18:51:25 -0800142 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /*
145 * Get IO TLB memory from the low pages
146 */
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800147 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!io_tlb_start)
149 panic("Cannot allocate SWIOTLB buffer");
Jan Beulich563aaf02007-02-05 18:51:25 -0800150 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /*
153 * Allocate and initialize the free list array. This array is used
154 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
155 * between io_tlb_start and io_tlb_end.
156 */
157 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
Tony Luck25667d62007-03-06 13:31:45 -0800158 for (i = 0; i < io_tlb_nslabs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
160 io_tlb_index = 0;
Tony Luck25667d62007-03-06 13:31:45 -0800161 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /*
164 * Get the overflow emergency buffer
165 */
166 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
Jan Beulich563aaf02007-02-05 18:51:25 -0800167 if (!io_tlb_overflow_buffer)
168 panic("Cannot allocate SWIOTLB overflow buffer!\n");
169
Tony Luck25667d62007-03-06 13:31:45 -0800170 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
171 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Jan Beulich563aaf02007-02-05 18:51:25 -0800174void __init
175swiotlb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Tony Luck25667d62007-03-06 13:31:45 -0800177 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Alex Williamson0b9afed2005-09-06 11:20:49 -0600180/*
181 * Systems with larger DMA zones (those that don't support ISA) can
182 * initialize the swiotlb later using the slab allocator if needed.
183 * This should be just like above, but with some error catching.
184 */
185int
Jan Beulich563aaf02007-02-05 18:51:25 -0800186swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600187{
Jan Beulich563aaf02007-02-05 18:51:25 -0800188 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600189 unsigned int order;
190
191 if (!io_tlb_nslabs) {
192 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
193 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
194 }
195
196 /*
197 * Get IO TLB memory from the low pages
198 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800199 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600200 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800201 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600202
203 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800204 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600205 if (io_tlb_start)
206 break;
207 order--;
208 }
209
210 if (!io_tlb_start)
211 goto cleanup1;
212
Jan Beulich563aaf02007-02-05 18:51:25 -0800213 if (order != get_order(bytes)) {
Alex Williamson0b9afed2005-09-06 11:20:49 -0600214 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
215 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
216 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800217 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600218 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800219 io_tlb_end = io_tlb_start + bytes;
220 memset(io_tlb_start, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600221
222 /*
223 * Allocate and initialize the free list array. This array is used
224 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
225 * between io_tlb_start and io_tlb_end.
226 */
227 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
228 get_order(io_tlb_nslabs * sizeof(int)));
229 if (!io_tlb_list)
230 goto cleanup2;
231
232 for (i = 0; i < io_tlb_nslabs; i++)
233 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
234 io_tlb_index = 0;
235
Tony Luck25667d62007-03-06 13:31:45 -0800236 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
237 get_order(io_tlb_nslabs * sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600238 if (!io_tlb_orig_addr)
239 goto cleanup3;
240
Tony Luck25667d62007-03-06 13:31:45 -0800241 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600242
243 /*
244 * Get the overflow emergency buffer
245 */
246 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
247 get_order(io_tlb_overflow));
248 if (!io_tlb_overflow_buffer)
249 goto cleanup4;
250
Tony Luck25667d62007-03-06 13:31:45 -0800251 printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - "
252 "0x%lx\n", bytes >> 20,
253 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600254
255 return 0;
256
257cleanup4:
Tony Luck25667d62007-03-06 13:31:45 -0800258 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
259 sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600260 io_tlb_orig_addr = NULL;
261cleanup3:
Tony Luck25667d62007-03-06 13:31:45 -0800262 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
263 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600264 io_tlb_list = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600265cleanup2:
Jan Beulich563aaf02007-02-05 18:51:25 -0800266 io_tlb_end = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600267 free_pages((unsigned long)io_tlb_start, order);
268 io_tlb_start = NULL;
269cleanup1:
270 io_tlb_nslabs = req_nslabs;
271 return -ENOMEM;
272}
273
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800274static int
FUJITA Tomonori27979822008-09-10 01:06:49 +0900275address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
FUJITA Tomonori07a2c012008-09-19 02:02:05 +0900277 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900280static int is_swiotlb_buffer(char *addr)
281{
282 return addr >= io_tlb_start && addr < io_tlb_end;
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*
286 * Allocates bounce buffer and returns its kernel virtual address.
287 */
288static void *
Tony Luck25667d62007-03-06 13:31:45 -0800289map_single(struct device *hwdev, char *buffer, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 unsigned long flags;
292 char *dma_addr;
293 unsigned int nslots, stride, index, wrap;
294 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800295 unsigned long start_dma_addr;
296 unsigned long mask;
297 unsigned long offset_slots;
298 unsigned long max_slots;
299
300 mask = dma_get_seg_boundary(hwdev);
301 start_dma_addr = virt_to_bus(io_tlb_start) & mask;
302
303 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Jan Beulichb15a38912008-03-13 09:13:30 +0000304 max_slots = mask + 1
305 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
306 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /*
309 * For mappings greater than a page, we limit the stride (and
310 * hence alignment) to a page size.
311 */
312 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
313 if (size > PAGE_SIZE)
314 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
315 else
316 stride = 1;
317
Eric Sesterhenn34814542006-03-24 18:47:11 +0100318 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /*
321 * Find suitable number of IO TLB entries size that will fit this
322 * request and allocate a buffer from that IO TLB pool.
323 */
324 spin_lock_irqsave(&io_tlb_lock, flags);
Andrew Mortona7133a12008-04-29 00:59:36 -0700325 index = ALIGN(io_tlb_index, stride);
326 if (index >= io_tlb_nslabs)
327 index = 0;
328 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Andrew Mortona7133a12008-04-29 00:59:36 -0700330 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700331 while (iommu_is_span_boundary(index, nslots, offset_slots,
332 max_slots)) {
Jan Beulichb15a38912008-03-13 09:13:30 +0000333 index += stride;
334 if (index >= io_tlb_nslabs)
335 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700336 if (index == wrap)
337 goto not_found;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Andrew Mortona7133a12008-04-29 00:59:36 -0700340 /*
341 * If we find a slot that indicates we have 'nslots' number of
342 * contiguous buffers, we allocate the buffers from that slot
343 * and mark the entries as '0' indicating unavailable.
344 */
345 if (io_tlb_list[index] >= nslots) {
346 int count = 0;
347
348 for (i = index; i < (int) (index + nslots); i++)
349 io_tlb_list[i] = 0;
350 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
351 io_tlb_list[i] = ++count;
352 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
353
354 /*
355 * Update the indices to avoid searching in the next
356 * round.
357 */
358 io_tlb_index = ((index + nslots) < io_tlb_nslabs
359 ? (index + nslots) : 0);
360
361 goto found;
362 }
363 index += stride;
364 if (index >= io_tlb_nslabs)
365 index = 0;
366 } while (index != wrap);
367
368not_found:
369 spin_unlock_irqrestore(&io_tlb_lock, flags);
370 return NULL;
371found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_unlock_irqrestore(&io_tlb_lock, flags);
373
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 */
Keir Fraserdf336d12007-07-21 04:37:24 -0700379 for (i = 0; i < nslots; i++)
380 io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
Tony Luck25667d62007-03-06 13:31:45 -0800382 memcpy(dma_addr, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 return dma_addr;
385}
386
387/*
388 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
389 */
390static void
391unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
392{
393 unsigned long flags;
394 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
395 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800396 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /*
399 * First, sync the memory before unmapping the entry
400 */
Tony Luck25667d62007-03-06 13:31:45 -0800401 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /*
403 * bounce... copy the data back into the original buffer * and
404 * delete the bounce buffer.
405 */
Tony Luck25667d62007-03-06 13:31:45 -0800406 memcpy(buffer, dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /*
409 * Return the buffer to the free list by setting the corresponding
410 * entries to indicate the number of contigous entries available.
411 * While returning the entries to the free list, we merge the entries
412 * with slots below and above the pool being returned.
413 */
414 spin_lock_irqsave(&io_tlb_lock, flags);
415 {
416 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
417 io_tlb_list[index + nslots] : 0);
418 /*
419 * Step 1: return the slots to the free list, merging the
420 * slots with superceeding slots
421 */
422 for (i = index + nslots - 1; i >= index; i--)
423 io_tlb_list[i] = ++count;
424 /*
425 * Step 2: merge the returned slots with the preceding slots,
426 * if available (non zero)
427 */
428 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
429 io_tlb_list[i] = ++count;
430 }
431 spin_unlock_irqrestore(&io_tlb_lock, flags);
432}
433
434static void
John W. Linvillede69e0f2005-09-29 14:44:57 -0700435sync_single(struct device *hwdev, char *dma_addr, size_t size,
436 int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800439 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Keir Fraserdf336d12007-07-21 04:37:24 -0700441 buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
442
John W. Linvillede69e0f2005-09-29 14:44:57 -0700443 switch (target) {
444 case SYNC_FOR_CPU:
445 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Tony Luck25667d62007-03-06 13:31:45 -0800446 memcpy(buffer, dma_addr, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100447 else
448 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700449 break;
450 case SYNC_FOR_DEVICE:
451 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Tony Luck25667d62007-03-06 13:31:45 -0800452 memcpy(dma_addr, buffer, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100453 else
454 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700455 break;
456 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461void *
462swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Al Viro06a54492005-10-21 03:21:03 -0400463 dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Jan Beulich563aaf02007-02-05 18:51:25 -0800465 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 void *ret;
467 int order = get_order(size);
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900468 u64 dma_mask = DMA_32BIT_MASK;
469
470 if (hwdev && hwdev->coherent_dma_mask)
471 dma_mask = hwdev->coherent_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Tony Luck25667d62007-03-06 13:31:45 -0800473 ret = (void *)__get_free_pages(flags, order);
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900474 if (ret && !is_buffer_dma_capable(dma_mask, virt_to_bus(ret), size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * The allocated memory isn't reachable by the device.
477 * Fall back on swiotlb_map_single().
478 */
479 free_pages((unsigned long) ret, order);
480 ret = NULL;
481 }
482 if (!ret) {
483 /*
484 * We are either out of memory or the device can't DMA
485 * to GFP_DMA memory; fall back on
486 * swiotlb_map_single(), which will grab memory from
487 * the lowest available address range.
488 */
FUJITA Tomonori9dfda122008-09-08 18:53:48 +0900489 ret = map_single(hwdev, NULL, size, DMA_FROM_DEVICE);
490 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
494 memset(ret, 0, size);
Jan Beulich93fbff62007-02-05 18:49:45 -0800495 dev_addr = virt_to_bus(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* Confirm address can be DMA'd by device */
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900498 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
Jan Beulich563aaf02007-02-05 18:51:25 -0800499 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900500 (unsigned long long)dma_mask,
Jan Beulich563aaf02007-02-05 18:51:25 -0800501 (unsigned long long)dev_addr);
FUJITA Tomonoria2b89b52008-10-23 18:42:03 +0900502
503 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
504 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
505 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 *dma_handle = dev_addr;
508 return ret;
509}
510
511void
512swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
513 dma_addr_t dma_handle)
514{
David Brownellaa248862007-08-10 13:10:27 -0700515 WARN_ON(irqs_disabled());
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900516 if (!is_swiotlb_buffer(vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 free_pages((unsigned long) vaddr, get_order(size));
518 else
519 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
FUJITA Tomonori21f6c4d2008-09-08 18:53:49 +0900520 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523static void
524swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
525{
526 /*
527 * Ran out of IOMMU space for this operation. This is very bad.
528 * Unfortunately the drivers cannot handle this operation properly.
Tony Luck17e5ad62005-09-29 15:52:13 -0700529 * unless they check for dma_mapping_error (most don't)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 * When the mapping is small enough return a static buffer to limit
531 * the damage, or panic when the transfer is too big.
532 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800533 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 "device %s\n", size, dev ? dev->bus_id : "?");
535
536 if (size > io_tlb_overflow && do_panic) {
Tony Luck17e5ad62005-09-29 15:52:13 -0700537 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
538 panic("DMA: Memory would be corrupted\n");
539 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
540 panic("DMA: Random memory would be DMAed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
542}
543
544/*
545 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700546 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *
548 * Once the device is given the dma address, the device owns this memory until
549 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
550 */
551dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -0700552swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
553 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Jan Beulich563aaf02007-02-05 18:51:25 -0800555 dma_addr_t dev_addr = virt_to_bus(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 void *map;
557
Eric Sesterhenn34814542006-03-24 18:47:11 +0100558 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /*
560 * If the pointer passed in happens to be in the device's DMA window,
561 * we can safely return the device addr and not worry about bounce
562 * buffering it.
563 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900564 if (!address_needs_mapping(hwdev, dev_addr, size) && !swiotlb_force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return dev_addr;
566
567 /*
568 * Oh well, have to allocate and map a bounce buffer.
569 */
Tony Luck25667d62007-03-06 13:31:45 -0800570 map = map_single(hwdev, ptr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!map) {
572 swiotlb_full(hwdev, size, dir, 1);
573 map = io_tlb_overflow_buffer;
574 }
575
Jan Beulich93fbff62007-02-05 18:49:45 -0800576 dev_addr = virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /*
579 * Ensure that the address returned is DMA'ble
580 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900581 if (address_needs_mapping(hwdev, dev_addr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 panic("map_single: bounce buffer is not DMA'ble");
583
584 return dev_addr;
585}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700586EXPORT_SYMBOL(swiotlb_map_single_attrs);
587
588dma_addr_t
589swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
590{
591 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
592}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Unmap a single streaming mode DMA translation. The dma_addr and size must
596 * match what was provided for in a previous swiotlb_map_single call. All
597 * other usages are undefined.
598 *
599 * After this call, reads by the cpu to the buffer are guaranteed to see
600 * whatever the device wrote there.
601 */
602void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700603swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
604 size_t size, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Jan Beulich93fbff62007-02-05 18:49:45 -0800606 char *dma_addr = bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Eric Sesterhenn34814542006-03-24 18:47:11 +0100608 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900609 if (is_swiotlb_buffer(dma_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 unmap_single(hwdev, dma_addr, size, dir);
611 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800612 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700614EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Arthur Kepner309df0c2008-04-29 01:00:32 -0700616void
617swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
618 int dir)
619{
620 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
621}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/*
623 * Make physical memory consistent for a single streaming mode DMA translation
624 * after a transfer.
625 *
626 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700627 * using the cpu, yet do not wish to teardown the dma mapping, you must
628 * call this function before doing so. At the next point you give the dma
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * address back to the card, you must first perform a
630 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
631 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800632static void
John W. Linville8270f3f2005-09-29 14:43:32 -0700633swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700634 size_t size, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Jan Beulich93fbff62007-02-05 18:49:45 -0800636 char *dma_addr = bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Eric Sesterhenn34814542006-03-24 18:47:11 +0100638 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900639 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700640 sync_single(hwdev, dma_addr, size, dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800642 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645void
John W. Linville8270f3f2005-09-29 14:43:32 -0700646swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
647 size_t size, int dir)
648{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700649 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700650}
651
652void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
654 size_t size, int dir)
655{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700656 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659/*
John W. Linville878a97c2005-09-29 14:44:23 -0700660 * Same as above, but for a sub-range of the mapping.
661 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800662static void
John W. Linville878a97c2005-09-29 14:44:23 -0700663swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700664 unsigned long offset, size_t size,
665 int dir, int target)
John W. Linville878a97c2005-09-29 14:44:23 -0700666{
Jan Beulich93fbff62007-02-05 18:49:45 -0800667 char *dma_addr = bus_to_virt(dev_addr) + offset;
John W. Linville878a97c2005-09-29 14:44:23 -0700668
Eric Sesterhenn34814542006-03-24 18:47:11 +0100669 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900670 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700671 sync_single(hwdev, dma_addr, size, dir, target);
John W. Linville878a97c2005-09-29 14:44:23 -0700672 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800673 dma_mark_clean(dma_addr, size);
John W. Linville878a97c2005-09-29 14:44:23 -0700674}
675
676void
677swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
678 unsigned long offset, size_t size, int dir)
679{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700680 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
681 SYNC_FOR_CPU);
John W. Linville878a97c2005-09-29 14:44:23 -0700682}
683
684void
685swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
686 unsigned long offset, size_t size, int dir)
687{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700688 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
689 SYNC_FOR_DEVICE);
John W. Linville878a97c2005-09-29 14:44:23 -0700690}
691
Arthur Kepner309df0c2008-04-29 01:00:32 -0700692void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
693 struct dma_attrs *);
John W. Linville878a97c2005-09-29 14:44:23 -0700694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 * Map a set of buffers described by scatterlist in streaming mode for DMA.
696 * This is the scatter-gather version of the above swiotlb_map_single
697 * interface. Here the scatter gather list elements are each tagged with the
698 * appropriate dma address and length. They are obtained via
699 * sg_dma_{address,length}(SG).
700 *
701 * NOTE: An implementation may be able to use a smaller number of
702 * DMA address/length pairs than there are SG table elements.
703 * (for example via virtual mapping capabilities)
704 * The routine returns the number of addr/length pairs actually
705 * used, at most nents.
706 *
707 * Device ownership issues as mentioned above for swiotlb_map_single are the
708 * same here.
709 */
710int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700711swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
712 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200714 struct scatterlist *sg;
Tony Luck25667d62007-03-06 13:31:45 -0800715 void *addr;
Jan Beulich563aaf02007-02-05 18:51:25 -0800716 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int i;
718
Eric Sesterhenn34814542006-03-24 18:47:11 +0100719 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Jens Axboedbfd49f2007-05-11 14:56:18 +0200721 for_each_sg(sgl, sg, nelems, i) {
Tony Luck25667d62007-03-06 13:31:45 -0800722 addr = SG_ENT_VIRT_ADDRESS(sg);
723 dev_addr = virt_to_bus(addr);
FUJITA Tomonori27979822008-09-10 01:06:49 +0900724 if (swiotlb_force ||
725 address_needs_mapping(hwdev, dev_addr, sg->length)) {
Tony Luck25667d62007-03-06 13:31:45 -0800726 void *map = map_single(hwdev, addr, sg->length, dir);
Andi Kleen7e870232005-12-20 14:45:19 +0100727 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* Don't panic here, we expect map_sg users
729 to do proper error handling. */
730 swiotlb_full(hwdev, sg->length, dir, 0);
Arthur Kepner309df0c2008-04-29 01:00:32 -0700731 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
732 attrs);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200733 sgl[0].dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735 }
Jan Beulichcde14bb2007-02-05 18:46:40 -0800736 sg->dma_address = virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 } else
738 sg->dma_address = dev_addr;
739 sg->dma_length = sg->length;
740 }
741 return nelems;
742}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700743EXPORT_SYMBOL(swiotlb_map_sg_attrs);
744
745int
746swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
747 int dir)
748{
749 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
750}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752/*
753 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
754 * concerning calls here are the same as for swiotlb_unmap_single() above.
755 */
756void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700757swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
758 int nelems, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200760 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 int i;
762
Eric Sesterhenn34814542006-03-24 18:47:11 +0100763 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Jens Axboedbfd49f2007-05-11 14:56:18 +0200765 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Jan Beulich93fbff62007-02-05 18:49:45 -0800767 unmap_single(hwdev, bus_to_virt(sg->dma_address),
768 sg->dma_length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800770 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700773EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
774
775void
776swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
777 int dir)
778{
779 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
780}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782/*
783 * Make physical memory consistent for a set of streaming mode DMA translations
784 * after a transfer.
785 *
786 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
787 * and usage.
788 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800789static void
Jens Axboedbfd49f2007-05-11 14:56:18 +0200790swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700791 int nelems, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200793 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int i;
795
Eric Sesterhenn34814542006-03-24 18:47:11 +0100796 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Jens Axboedbfd49f2007-05-11 14:56:18 +0200798 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Jan Beulich93fbff62007-02-05 18:49:45 -0800800 sync_single(hwdev, bus_to_virt(sg->dma_address),
John W. Linvillede69e0f2005-09-29 14:44:57 -0700801 sg->dma_length, dir, target);
Jan Beulichcde14bb2007-02-05 18:46:40 -0800802 else if (dir == DMA_FROM_DEVICE)
803 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807void
John W. Linville8270f3f2005-09-29 14:43:32 -0700808swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
809 int nelems, int dir)
810{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700811 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700812}
813
814void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
816 int nelems, int dir)
817{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700818 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
821int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700822swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Jan Beulich93fbff62007-02-05 18:49:45 -0800824 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700828 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700830 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 * this function.
832 */
833int
Jan Beulich563aaf02007-02-05 18:51:25 -0800834swiotlb_dma_supported(struct device *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Tony Luck25667d62007-03-06 13:31:45 -0800836 return virt_to_bus(io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839EXPORT_SYMBOL(swiotlb_map_single);
840EXPORT_SYMBOL(swiotlb_unmap_single);
841EXPORT_SYMBOL(swiotlb_map_sg);
842EXPORT_SYMBOL(swiotlb_unmap_sg);
843EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
844EXPORT_SYMBOL(swiotlb_sync_single_for_device);
John W. Linville878a97c2005-09-29 14:44:23 -0700845EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
846EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
848EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
849EXPORT_SYMBOL(swiotlb_dma_mapping_error);
Tony Luck25667d62007-03-06 13:31:45 -0800850EXPORT_SYMBOL(swiotlb_alloc_coherent);
851EXPORT_SYMBOL(swiotlb_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852EXPORT_SYMBOL(swiotlb_dma_supported);