blob: abecb2857556c9790ab23ee9eca3281716c90bca [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>
26#include <linux/types.h>
27#include <linux/ctype.h>
28
29#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/dma.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070031#include <asm/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/init.h>
34#include <linux/bootmem.h>
FUJITA Tomonoria8522502008-04-29 00:59:36 -070035#include <linux/iommu-helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#define OFFSET(val,align) ((unsigned long) \
38 ( (val) & ( (align) - 1)))
39
Jens Axboef9527f12007-10-22 19:44:53 +020040#define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
Jan Beulich93fbff62007-02-05 18:49:45 -080041#define SG_ENT_PHYS_ADDRESS(sg) virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Maximum allowable number of contiguous slabs to map,
45 * must be a power of 2. What is the appropriate value ?
46 * The complexity of {map,unmap}_single is linearly dependent on this value.
47 */
48#define IO_TLB_SEGSIZE 128
49
50/*
51 * log of the size of each IO TLB slab. The number of slabs is command line
52 * controllable.
53 */
54#define IO_TLB_SHIFT 11
55
Alex Williamson0b9afed2005-09-06 11:20:49 -060056#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
57
58/*
59 * Minimum IO TLB size to bother booting with. Systems with mainly
60 * 64bit capable cards will only lightly use the swiotlb. If we can't
61 * allocate a contiguous 1MB, we're probably in trouble anyway.
62 */
63#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
64
John W. Linvillede69e0f2005-09-29 14:44:57 -070065/*
66 * Enumeration for sync targets
67 */
68enum dma_sync_target {
69 SYNC_FOR_CPU = 0,
70 SYNC_FOR_DEVICE = 1,
71};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073int swiotlb_force;
74
75/*
76 * Used to do a quick range check in swiotlb_unmap_single and
77 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
78 * API.
79 */
80static char *io_tlb_start, *io_tlb_end;
81
82/*
83 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
84 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
85 */
86static unsigned long io_tlb_nslabs;
87
88/*
89 * When the IOMMU overflows we return a fallback buffer. This sets the size.
90 */
91static unsigned long io_tlb_overflow = 32*1024;
92
93void *io_tlb_overflow_buffer;
94
95/*
96 * This is a free list describing the number of free entries available from
97 * each index
98 */
99static unsigned int *io_tlb_list;
100static unsigned int io_tlb_index;
101
102/*
103 * We need to save away the original address corresponding to a mapped entry
104 * for the sync operations.
105 */
Tony Luck25667d62007-03-06 13:31:45 -0800106static unsigned char **io_tlb_orig_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * Protect the above data structures in the map and unmap calls
110 */
111static DEFINE_SPINLOCK(io_tlb_lock);
112
113static int __init
114setup_io_tlb_npages(char *str)
115{
116 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700117 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* avoid tail segment of size < IO_TLB_SEGSIZE */
119 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
120 }
121 if (*str == ',')
122 ++str;
123 if (!strcmp(str, "force"))
124 swiotlb_force = 1;
125 return 1;
126}
127__setup("swiotlb=", setup_io_tlb_npages);
128/* make io_tlb_overflow tunable too? */
129
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800130void * __weak swiotlb_alloc_boot(size_t size, unsigned long nslabs)
131{
132 return alloc_bootmem_low_pages(size);
133}
134
135void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs)
136{
137 return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * Statically reserve bounce buffer space and initialize bounce buffer data
Tony Luck17e5ad62005-09-29 15:52:13 -0700142 * structures for the software IO TLB used to implement the DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800144void __init
145swiotlb_init_with_default_size(size_t default_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Jan Beulich563aaf02007-02-05 18:51:25 -0800147 unsigned long i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (!io_tlb_nslabs) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700150 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
152 }
153
Jan Beulich563aaf02007-02-05 18:51:25 -0800154 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /*
157 * Get IO TLB memory from the low pages
158 */
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800159 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!io_tlb_start)
161 panic("Cannot allocate SWIOTLB buffer");
Jan Beulich563aaf02007-02-05 18:51:25 -0800162 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /*
165 * Allocate and initialize the free list array. This array is used
166 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
167 * between io_tlb_start and io_tlb_end.
168 */
169 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
Tony Luck25667d62007-03-06 13:31:45 -0800170 for (i = 0; i < io_tlb_nslabs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
172 io_tlb_index = 0;
Tony Luck25667d62007-03-06 13:31:45 -0800173 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /*
176 * Get the overflow emergency buffer
177 */
178 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
Jan Beulich563aaf02007-02-05 18:51:25 -0800179 if (!io_tlb_overflow_buffer)
180 panic("Cannot allocate SWIOTLB overflow buffer!\n");
181
Tony Luck25667d62007-03-06 13:31:45 -0800182 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
183 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Jan Beulich563aaf02007-02-05 18:51:25 -0800186void __init
187swiotlb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Tony Luck25667d62007-03-06 13:31:45 -0800189 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Alex Williamson0b9afed2005-09-06 11:20:49 -0600192/*
193 * Systems with larger DMA zones (those that don't support ISA) can
194 * initialize the swiotlb later using the slab allocator if needed.
195 * This should be just like above, but with some error catching.
196 */
197int
Jan Beulich563aaf02007-02-05 18:51:25 -0800198swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600199{
Jan Beulich563aaf02007-02-05 18:51:25 -0800200 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600201 unsigned int order;
202
203 if (!io_tlb_nslabs) {
204 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
205 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
206 }
207
208 /*
209 * Get IO TLB memory from the low pages
210 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800211 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600212 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800213 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600214
215 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800216 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600217 if (io_tlb_start)
218 break;
219 order--;
220 }
221
222 if (!io_tlb_start)
223 goto cleanup1;
224
Jan Beulich563aaf02007-02-05 18:51:25 -0800225 if (order != get_order(bytes)) {
Alex Williamson0b9afed2005-09-06 11:20:49 -0600226 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
227 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
228 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800229 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600230 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800231 io_tlb_end = io_tlb_start + bytes;
232 memset(io_tlb_start, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600233
234 /*
235 * Allocate and initialize the free list array. This array is used
236 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
237 * between io_tlb_start and io_tlb_end.
238 */
239 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
240 get_order(io_tlb_nslabs * sizeof(int)));
241 if (!io_tlb_list)
242 goto cleanup2;
243
244 for (i = 0; i < io_tlb_nslabs; i++)
245 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
246 io_tlb_index = 0;
247
Tony Luck25667d62007-03-06 13:31:45 -0800248 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
249 get_order(io_tlb_nslabs * sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600250 if (!io_tlb_orig_addr)
251 goto cleanup3;
252
Tony Luck25667d62007-03-06 13:31:45 -0800253 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600254
255 /*
256 * Get the overflow emergency buffer
257 */
258 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
259 get_order(io_tlb_overflow));
260 if (!io_tlb_overflow_buffer)
261 goto cleanup4;
262
Tony Luck25667d62007-03-06 13:31:45 -0800263 printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - "
264 "0x%lx\n", bytes >> 20,
265 virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600266
267 return 0;
268
269cleanup4:
Tony Luck25667d62007-03-06 13:31:45 -0800270 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
271 sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600272 io_tlb_orig_addr = NULL;
273cleanup3:
Tony Luck25667d62007-03-06 13:31:45 -0800274 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
275 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600276 io_tlb_list = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600277cleanup2:
Jan Beulich563aaf02007-02-05 18:51:25 -0800278 io_tlb_end = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600279 free_pages((unsigned long)io_tlb_start, order);
280 io_tlb_start = NULL;
281cleanup1:
282 io_tlb_nslabs = req_nslabs;
283 return -ENOMEM;
284}
285
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800286static int
FUJITA Tomonori27979822008-09-10 01:06:49 +0900287address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
FUJITA Tomonori07a2c012008-09-19 02:02:05 +0900289 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900292static int is_swiotlb_buffer(char *addr)
293{
294 return addr >= io_tlb_start && addr < io_tlb_end;
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * Allocates bounce buffer and returns its kernel virtual address.
299 */
300static void *
Tony Luck25667d62007-03-06 13:31:45 -0800301map_single(struct device *hwdev, char *buffer, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 unsigned long flags;
304 char *dma_addr;
305 unsigned int nslots, stride, index, wrap;
306 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800307 unsigned long start_dma_addr;
308 unsigned long mask;
309 unsigned long offset_slots;
310 unsigned long max_slots;
311
312 mask = dma_get_seg_boundary(hwdev);
313 start_dma_addr = virt_to_bus(io_tlb_start) & mask;
314
315 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Jan Beulichb15a38912008-03-13 09:13:30 +0000316 max_slots = mask + 1
317 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
318 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /*
321 * For mappings greater than a page, we limit the stride (and
322 * hence alignment) to a page size.
323 */
324 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
325 if (size > PAGE_SIZE)
326 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
327 else
328 stride = 1;
329
Eric Sesterhenn34814542006-03-24 18:47:11 +0100330 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /*
333 * Find suitable number of IO TLB entries size that will fit this
334 * request and allocate a buffer from that IO TLB pool.
335 */
336 spin_lock_irqsave(&io_tlb_lock, flags);
Andrew Mortona7133a12008-04-29 00:59:36 -0700337 index = ALIGN(io_tlb_index, stride);
338 if (index >= io_tlb_nslabs)
339 index = 0;
340 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Andrew Mortona7133a12008-04-29 00:59:36 -0700342 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700343 while (iommu_is_span_boundary(index, nslots, offset_slots,
344 max_slots)) {
Jan Beulichb15a38912008-03-13 09:13:30 +0000345 index += stride;
346 if (index >= io_tlb_nslabs)
347 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700348 if (index == wrap)
349 goto not_found;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Andrew Mortona7133a12008-04-29 00:59:36 -0700352 /*
353 * If we find a slot that indicates we have 'nslots' number of
354 * contiguous buffers, we allocate the buffers from that slot
355 * and mark the entries as '0' indicating unavailable.
356 */
357 if (io_tlb_list[index] >= nslots) {
358 int count = 0;
359
360 for (i = index; i < (int) (index + nslots); i++)
361 io_tlb_list[i] = 0;
362 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
363 io_tlb_list[i] = ++count;
364 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
365
366 /*
367 * Update the indices to avoid searching in the next
368 * round.
369 */
370 io_tlb_index = ((index + nslots) < io_tlb_nslabs
371 ? (index + nslots) : 0);
372
373 goto found;
374 }
375 index += stride;
376 if (index >= io_tlb_nslabs)
377 index = 0;
378 } while (index != wrap);
379
380not_found:
381 spin_unlock_irqrestore(&io_tlb_lock, flags);
382 return NULL;
383found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_unlock_irqrestore(&io_tlb_lock, flags);
385
386 /*
387 * Save away the mapping from the original address to the DMA address.
388 * This is needed when we sync the memory. Then we sync the buffer if
389 * needed.
390 */
Keir Fraserdf336d12007-07-21 04:37:24 -0700391 for (i = 0; i < nslots; i++)
392 io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
Tony Luck25667d62007-03-06 13:31:45 -0800394 memcpy(dma_addr, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 return dma_addr;
397}
398
399/*
400 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
401 */
402static void
403unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
404{
405 unsigned long flags;
406 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
407 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800408 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /*
411 * First, sync the memory before unmapping the entry
412 */
Tony Luck25667d62007-03-06 13:31:45 -0800413 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /*
415 * bounce... copy the data back into the original buffer * and
416 * delete the bounce buffer.
417 */
Tony Luck25667d62007-03-06 13:31:45 -0800418 memcpy(buffer, dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /*
421 * Return the buffer to the free list by setting the corresponding
422 * entries to indicate the number of contigous entries available.
423 * While returning the entries to the free list, we merge the entries
424 * with slots below and above the pool being returned.
425 */
426 spin_lock_irqsave(&io_tlb_lock, flags);
427 {
428 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
429 io_tlb_list[index + nslots] : 0);
430 /*
431 * Step 1: return the slots to the free list, merging the
432 * slots with superceeding slots
433 */
434 for (i = index + nslots - 1; i >= index; i--)
435 io_tlb_list[i] = ++count;
436 /*
437 * Step 2: merge the returned slots with the preceding slots,
438 * if available (non zero)
439 */
440 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
441 io_tlb_list[i] = ++count;
442 }
443 spin_unlock_irqrestore(&io_tlb_lock, flags);
444}
445
446static void
John W. Linvillede69e0f2005-09-29 14:44:57 -0700447sync_single(struct device *hwdev, char *dma_addr, size_t size,
448 int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800451 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Keir Fraserdf336d12007-07-21 04:37:24 -0700453 buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
454
John W. Linvillede69e0f2005-09-29 14:44:57 -0700455 switch (target) {
456 case SYNC_FOR_CPU:
457 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Tony Luck25667d62007-03-06 13:31:45 -0800458 memcpy(buffer, dma_addr, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100459 else
460 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700461 break;
462 case SYNC_FOR_DEVICE:
463 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Tony Luck25667d62007-03-06 13:31:45 -0800464 memcpy(dma_addr, buffer, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100465 else
466 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700467 break;
468 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473void *
474swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Al Viro06a54492005-10-21 03:21:03 -0400475 dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Jan Beulich563aaf02007-02-05 18:51:25 -0800477 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 void *ret;
479 int order = get_order(size);
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900480 u64 dma_mask = DMA_32BIT_MASK;
481
482 if (hwdev && hwdev->coherent_dma_mask)
483 dma_mask = hwdev->coherent_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Tony Luck25667d62007-03-06 13:31:45 -0800485 ret = (void *)__get_free_pages(flags, order);
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900486 if (ret && !is_buffer_dma_capable(dma_mask, virt_to_bus(ret), size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /*
488 * The allocated memory isn't reachable by the device.
489 * Fall back on swiotlb_map_single().
490 */
491 free_pages((unsigned long) ret, order);
492 ret = NULL;
493 }
494 if (!ret) {
495 /*
496 * We are either out of memory or the device can't DMA
497 * to GFP_DMA memory; fall back on
498 * swiotlb_map_single(), which will grab memory from
499 * the lowest available address range.
500 */
FUJITA Tomonori9dfda122008-09-08 18:53:48 +0900501 ret = map_single(hwdev, NULL, size, DMA_FROM_DEVICE);
502 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
506 memset(ret, 0, size);
Jan Beulich93fbff62007-02-05 18:49:45 -0800507 dev_addr = virt_to_bus(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 /* Confirm address can be DMA'd by device */
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900510 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
Jan Beulich563aaf02007-02-05 18:51:25 -0800511 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900512 (unsigned long long)dma_mask,
Jan Beulich563aaf02007-02-05 18:51:25 -0800513 (unsigned long long)dev_addr);
FUJITA Tomonoria2b89b52008-10-23 18:42:03 +0900514
515 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
516 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
517 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 *dma_handle = dev_addr;
520 return ret;
521}
522
523void
524swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
525 dma_addr_t dma_handle)
526{
David Brownellaa248862007-08-10 13:10:27 -0700527 WARN_ON(irqs_disabled());
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900528 if (!is_swiotlb_buffer(vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 free_pages((unsigned long) vaddr, get_order(size));
530 else
531 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
FUJITA Tomonori21f6c4d2008-09-08 18:53:49 +0900532 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535static void
536swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
537{
538 /*
539 * Ran out of IOMMU space for this operation. This is very bad.
540 * Unfortunately the drivers cannot handle this operation properly.
Tony Luck17e5ad62005-09-29 15:52:13 -0700541 * unless they check for dma_mapping_error (most don't)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * When the mapping is small enough return a static buffer to limit
543 * the damage, or panic when the transfer is too big.
544 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800545 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 "device %s\n", size, dev ? dev->bus_id : "?");
547
548 if (size > io_tlb_overflow && do_panic) {
Tony Luck17e5ad62005-09-29 15:52:13 -0700549 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
550 panic("DMA: Memory would be corrupted\n");
551 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
552 panic("DMA: Random memory would be DMAed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554}
555
556/*
557 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700558 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 *
560 * Once the device is given the dma address, the device owns this memory until
561 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
562 */
563dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -0700564swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
565 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Jan Beulich563aaf02007-02-05 18:51:25 -0800567 dma_addr_t dev_addr = virt_to_bus(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 void *map;
569
Eric Sesterhenn34814542006-03-24 18:47:11 +0100570 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /*
572 * If the pointer passed in happens to be in the device's DMA window,
573 * we can safely return the device addr and not worry about bounce
574 * buffering it.
575 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900576 if (!address_needs_mapping(hwdev, dev_addr, size) && !swiotlb_force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return dev_addr;
578
579 /*
580 * Oh well, have to allocate and map a bounce buffer.
581 */
Tony Luck25667d62007-03-06 13:31:45 -0800582 map = map_single(hwdev, ptr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (!map) {
584 swiotlb_full(hwdev, size, dir, 1);
585 map = io_tlb_overflow_buffer;
586 }
587
Jan Beulich93fbff62007-02-05 18:49:45 -0800588 dev_addr = virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /*
591 * Ensure that the address returned is DMA'ble
592 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900593 if (address_needs_mapping(hwdev, dev_addr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 panic("map_single: bounce buffer is not DMA'ble");
595
596 return dev_addr;
597}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700598EXPORT_SYMBOL(swiotlb_map_single_attrs);
599
600dma_addr_t
601swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
602{
603 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * Unmap a single streaming mode DMA translation. The dma_addr and size must
608 * match what was provided for in a previous swiotlb_map_single call. All
609 * other usages are undefined.
610 *
611 * After this call, reads by the cpu to the buffer are guaranteed to see
612 * whatever the device wrote there.
613 */
614void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700615swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
616 size_t size, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Jan Beulich93fbff62007-02-05 18:49:45 -0800618 char *dma_addr = bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Eric Sesterhenn34814542006-03-24 18:47:11 +0100620 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900621 if (is_swiotlb_buffer(dma_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 unmap_single(hwdev, dma_addr, size, dir);
623 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800624 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700626EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Arthur Kepner309df0c2008-04-29 01:00:32 -0700628void
629swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
630 int dir)
631{
632 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
633}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
635 * Make physical memory consistent for a single streaming mode DMA translation
636 * after a transfer.
637 *
638 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700639 * using the cpu, yet do not wish to teardown the dma mapping, you must
640 * call this function before doing so. At the next point you give the dma
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * address back to the card, you must first perform a
642 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
643 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800644static void
John W. Linville8270f3f2005-09-29 14:43:32 -0700645swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700646 size_t size, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Jan Beulich93fbff62007-02-05 18:49:45 -0800648 char *dma_addr = bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric Sesterhenn34814542006-03-24 18:47:11 +0100650 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900651 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700652 sync_single(hwdev, dma_addr, size, dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800654 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657void
John W. Linville8270f3f2005-09-29 14:43:32 -0700658swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
659 size_t size, int dir)
660{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700661 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700662}
663
664void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
666 size_t size, int dir)
667{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700668 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671/*
John W. Linville878a97c2005-09-29 14:44:23 -0700672 * Same as above, but for a sub-range of the mapping.
673 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800674static void
John W. Linville878a97c2005-09-29 14:44:23 -0700675swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700676 unsigned long offset, size_t size,
677 int dir, int target)
John W. Linville878a97c2005-09-29 14:44:23 -0700678{
Jan Beulich93fbff62007-02-05 18:49:45 -0800679 char *dma_addr = bus_to_virt(dev_addr) + offset;
John W. Linville878a97c2005-09-29 14:44:23 -0700680
Eric Sesterhenn34814542006-03-24 18:47:11 +0100681 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900682 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700683 sync_single(hwdev, dma_addr, size, dir, target);
John W. Linville878a97c2005-09-29 14:44:23 -0700684 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800685 dma_mark_clean(dma_addr, size);
John W. Linville878a97c2005-09-29 14:44:23 -0700686}
687
688void
689swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
690 unsigned long offset, size_t size, int dir)
691{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700692 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
693 SYNC_FOR_CPU);
John W. Linville878a97c2005-09-29 14:44:23 -0700694}
695
696void
697swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
698 unsigned long offset, size_t size, int dir)
699{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700700 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
701 SYNC_FOR_DEVICE);
John W. Linville878a97c2005-09-29 14:44:23 -0700702}
703
Arthur Kepner309df0c2008-04-29 01:00:32 -0700704void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
705 struct dma_attrs *);
John W. Linville878a97c2005-09-29 14:44:23 -0700706/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * Map a set of buffers described by scatterlist in streaming mode for DMA.
708 * This is the scatter-gather version of the above swiotlb_map_single
709 * interface. Here the scatter gather list elements are each tagged with the
710 * appropriate dma address and length. They are obtained via
711 * sg_dma_{address,length}(SG).
712 *
713 * NOTE: An implementation may be able to use a smaller number of
714 * DMA address/length pairs than there are SG table elements.
715 * (for example via virtual mapping capabilities)
716 * The routine returns the number of addr/length pairs actually
717 * used, at most nents.
718 *
719 * Device ownership issues as mentioned above for swiotlb_map_single are the
720 * same here.
721 */
722int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700723swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
724 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200726 struct scatterlist *sg;
Tony Luck25667d62007-03-06 13:31:45 -0800727 void *addr;
Jan Beulich563aaf02007-02-05 18:51:25 -0800728 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int i;
730
Eric Sesterhenn34814542006-03-24 18:47:11 +0100731 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Jens Axboedbfd49f2007-05-11 14:56:18 +0200733 for_each_sg(sgl, sg, nelems, i) {
Tony Luck25667d62007-03-06 13:31:45 -0800734 addr = SG_ENT_VIRT_ADDRESS(sg);
735 dev_addr = virt_to_bus(addr);
FUJITA Tomonori27979822008-09-10 01:06:49 +0900736 if (swiotlb_force ||
737 address_needs_mapping(hwdev, dev_addr, sg->length)) {
Tony Luck25667d62007-03-06 13:31:45 -0800738 void *map = map_single(hwdev, addr, sg->length, dir);
Andi Kleen7e870232005-12-20 14:45:19 +0100739 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /* Don't panic here, we expect map_sg users
741 to do proper error handling. */
742 swiotlb_full(hwdev, sg->length, dir, 0);
Arthur Kepner309df0c2008-04-29 01:00:32 -0700743 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
744 attrs);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200745 sgl[0].dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747 }
Jan Beulichcde14bb2007-02-05 18:46:40 -0800748 sg->dma_address = virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 } else
750 sg->dma_address = dev_addr;
751 sg->dma_length = sg->length;
752 }
753 return nelems;
754}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700755EXPORT_SYMBOL(swiotlb_map_sg_attrs);
756
757int
758swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
759 int dir)
760{
761 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
762}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764/*
765 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
766 * concerning calls here are the same as for swiotlb_unmap_single() above.
767 */
768void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700769swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
770 int nelems, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200772 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 int i;
774
Eric Sesterhenn34814542006-03-24 18:47:11 +0100775 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jens Axboedbfd49f2007-05-11 14:56:18 +0200777 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Jan Beulich93fbff62007-02-05 18:49:45 -0800779 unmap_single(hwdev, bus_to_virt(sg->dma_address),
780 sg->dma_length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800782 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700785EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
786
787void
788swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
789 int dir)
790{
791 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
792}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794/*
795 * Make physical memory consistent for a set of streaming mode DMA translations
796 * after a transfer.
797 *
798 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
799 * and usage.
800 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800801static void
Jens Axboedbfd49f2007-05-11 14:56:18 +0200802swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700803 int nelems, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200805 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int i;
807
Eric Sesterhenn34814542006-03-24 18:47:11 +0100808 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Jens Axboedbfd49f2007-05-11 14:56:18 +0200810 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Jan Beulich93fbff62007-02-05 18:49:45 -0800812 sync_single(hwdev, bus_to_virt(sg->dma_address),
John W. Linvillede69e0f2005-09-29 14:44:57 -0700813 sg->dma_length, dir, target);
Jan Beulichcde14bb2007-02-05 18:46:40 -0800814 else if (dir == DMA_FROM_DEVICE)
815 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819void
John W. Linville8270f3f2005-09-29 14:43:32 -0700820swiotlb_sync_sg_for_cpu(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_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700824}
825
826void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
828 int nelems, int dir)
829{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700830 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700834swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Jan Beulich93fbff62007-02-05 18:49:45 -0800836 return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700840 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700842 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 * this function.
844 */
845int
Jan Beulich563aaf02007-02-05 18:51:25 -0800846swiotlb_dma_supported(struct device *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Tony Luck25667d62007-03-06 13:31:45 -0800848 return virt_to_bus(io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851EXPORT_SYMBOL(swiotlb_map_single);
852EXPORT_SYMBOL(swiotlb_unmap_single);
853EXPORT_SYMBOL(swiotlb_map_sg);
854EXPORT_SYMBOL(swiotlb_unmap_sg);
855EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
856EXPORT_SYMBOL(swiotlb_sync_single_for_device);
John W. Linville878a97c2005-09-29 14:44:23 -0700857EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
858EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
860EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
861EXPORT_SYMBOL(swiotlb_dma_mapping_error);
Tony Luck25667d62007-03-06 13:31:45 -0800862EXPORT_SYMBOL(swiotlb_alloc_coherent);
863EXPORT_SYMBOL(swiotlb_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864EXPORT_SYMBOL(swiotlb_dma_supported);