blob: a0b4039e2880fc69f61742c93220f0883cc8ac99 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Dynamic DMA mapping support.
3 *
Jan Beulich563aaf02007-02-05 18:51:25 -08004 * This implementation is a fallback for platforms that do not support
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * I/O TLBs (aka DMA address translation hardware).
6 * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
7 * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
8 * Copyright (C) 2000, 2003 Hewlett-Packard Co
9 * David Mosberger-Tang <davidm@hpl.hp.com>
10 *
11 * 03/05/07 davidm Switch from PCI-DMA to generic device DMA API.
12 * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
13 * unnecessary i-cache flushing.
John W. Linville569c8bf2005-09-29 14:45:24 -070014 * 04/07/.. ak Better overflow handling. Assorted fixes.
15 * 05/09/10 linville Add support for syncing ranges, support syncing for
16 * DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
18
19#include <linux/cache.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070020#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
22#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/spinlock.h>
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -080024#include <linux/swiotlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
Ian Campbell0016fde2008-12-16 12:17:27 -080026#include <linux/swiotlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/types.h>
28#include <linux/ctype.h>
29
30#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/dma.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070032#include <asm/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/init.h>
35#include <linux/bootmem.h>
FUJITA Tomonoria8522502008-04-29 00:59:36 -070036#include <linux/iommu-helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define OFFSET(val,align) ((unsigned long) \
39 ( (val) & ( (align) - 1)))
40
Jens Axboef9527f12007-10-22 19:44:53 +020041#define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
Jan Beulich93fbff62007-02-05 18:49:45 -080042#define SG_ENT_PHYS_ADDRESS(sg) virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Alex Williamson0b9afed2005-09-06 11:20:49 -060044#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
45
46/*
47 * Minimum IO TLB size to bother booting with. Systems with mainly
48 * 64bit capable cards will only lightly use the swiotlb. If we can't
49 * allocate a contiguous 1MB, we're probably in trouble anyway.
50 */
51#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
52
John W. Linvillede69e0f2005-09-29 14:44:57 -070053/*
54 * Enumeration for sync targets
55 */
56enum dma_sync_target {
57 SYNC_FOR_CPU = 0,
58 SYNC_FOR_DEVICE = 1,
59};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061int swiotlb_force;
62
63/*
64 * Used to do a quick range check in swiotlb_unmap_single and
65 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
66 * API.
67 */
68static char *io_tlb_start, *io_tlb_end;
69
70/*
71 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
72 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
73 */
74static unsigned long io_tlb_nslabs;
75
76/*
77 * When the IOMMU overflows we return a fallback buffer. This sets the size.
78 */
79static unsigned long io_tlb_overflow = 32*1024;
80
81void *io_tlb_overflow_buffer;
82
83/*
84 * This is a free list describing the number of free entries available from
85 * each index
86 */
87static unsigned int *io_tlb_list;
88static unsigned int io_tlb_index;
89
90/*
91 * We need to save away the original address corresponding to a mapped entry
92 * for the sync operations.
93 */
Tony Luck25667d62007-03-06 13:31:45 -080094static unsigned char **io_tlb_orig_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
97 * Protect the above data structures in the map and unmap calls
98 */
99static DEFINE_SPINLOCK(io_tlb_lock);
100
101static int __init
102setup_io_tlb_npages(char *str)
103{
104 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700105 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /* avoid tail segment of size < IO_TLB_SEGSIZE */
107 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
108 }
109 if (*str == ',')
110 ++str;
111 if (!strcmp(str, "force"))
112 swiotlb_force = 1;
113 return 1;
114}
115__setup("swiotlb=", setup_io_tlb_npages);
116/* make io_tlb_overflow tunable too? */
117
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800118void * __weak swiotlb_alloc_boot(size_t size, unsigned long nslabs)
119{
120 return alloc_bootmem_low_pages(size);
121}
122
123void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs)
124{
125 return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
126}
127
Ian Campbelle08e1f72008-12-16 12:17:30 -0800128dma_addr_t __weak swiotlb_phys_to_bus(phys_addr_t paddr)
129{
130 return paddr;
131}
132
133phys_addr_t __weak swiotlb_bus_to_phys(dma_addr_t baddr)
134{
135 return baddr;
136}
137
138static dma_addr_t swiotlb_virt_to_bus(volatile void *address)
139{
140 return swiotlb_phys_to_bus(virt_to_phys(address));
141}
142
143static void *swiotlb_bus_to_virt(dma_addr_t address)
144{
145 return phys_to_virt(swiotlb_bus_to_phys(address));
146}
147
Ian Campbellb81ea272008-12-16 12:17:31 -0800148int __weak swiotlb_arch_range_needs_mapping(void *ptr, size_t size)
149{
150 return 0;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * Statically reserve bounce buffer space and initialize bounce buffer data
Tony Luck17e5ad62005-09-29 15:52:13 -0700155 * structures for the software IO TLB used to implement the DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800157void __init
158swiotlb_init_with_default_size(size_t default_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Jan Beulich563aaf02007-02-05 18:51:25 -0800160 unsigned long i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (!io_tlb_nslabs) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700163 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
165 }
166
Jan Beulich563aaf02007-02-05 18:51:25 -0800167 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
170 * Get IO TLB memory from the low pages
171 */
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800172 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (!io_tlb_start)
174 panic("Cannot allocate SWIOTLB buffer");
Jan Beulich563aaf02007-02-05 18:51:25 -0800175 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /*
178 * Allocate and initialize the free list array. This array is used
179 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
180 * between io_tlb_start and io_tlb_end.
181 */
182 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
Tony Luck25667d62007-03-06 13:31:45 -0800183 for (i = 0; i < io_tlb_nslabs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
185 io_tlb_index = 0;
Tony Luck25667d62007-03-06 13:31:45 -0800186 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /*
189 * Get the overflow emergency buffer
190 */
191 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
Jan Beulich563aaf02007-02-05 18:51:25 -0800192 if (!io_tlb_overflow_buffer)
193 panic("Cannot allocate SWIOTLB overflow buffer!\n");
194
Tony Luck25667d62007-03-06 13:31:45 -0800195 printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
Ian Campbelle08e1f72008-12-16 12:17:30 -0800196 swiotlb_virt_to_bus(io_tlb_start), swiotlb_virt_to_bus(io_tlb_end));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Jan Beulich563aaf02007-02-05 18:51:25 -0800199void __init
200swiotlb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Tony Luck25667d62007-03-06 13:31:45 -0800202 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Alex Williamson0b9afed2005-09-06 11:20:49 -0600205/*
206 * Systems with larger DMA zones (those that don't support ISA) can
207 * initialize the swiotlb later using the slab allocator if needed.
208 * This should be just like above, but with some error catching.
209 */
210int
Jan Beulich563aaf02007-02-05 18:51:25 -0800211swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600212{
Jan Beulich563aaf02007-02-05 18:51:25 -0800213 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600214 unsigned int order;
215
216 if (!io_tlb_nslabs) {
217 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
218 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
219 }
220
221 /*
222 * Get IO TLB memory from the low pages
223 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800224 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600225 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800226 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600227
228 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800229 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600230 if (io_tlb_start)
231 break;
232 order--;
233 }
234
235 if (!io_tlb_start)
236 goto cleanup1;
237
Jan Beulich563aaf02007-02-05 18:51:25 -0800238 if (order != get_order(bytes)) {
Alex Williamson0b9afed2005-09-06 11:20:49 -0600239 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
240 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
241 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800242 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600243 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800244 io_tlb_end = io_tlb_start + bytes;
245 memset(io_tlb_start, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600246
247 /*
248 * Allocate and initialize the free list array. This array is used
249 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
250 * between io_tlb_start and io_tlb_end.
251 */
252 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
253 get_order(io_tlb_nslabs * sizeof(int)));
254 if (!io_tlb_list)
255 goto cleanup2;
256
257 for (i = 0; i < io_tlb_nslabs; i++)
258 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
259 io_tlb_index = 0;
260
Tony Luck25667d62007-03-06 13:31:45 -0800261 io_tlb_orig_addr = (unsigned char **)__get_free_pages(GFP_KERNEL,
262 get_order(io_tlb_nslabs * sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600263 if (!io_tlb_orig_addr)
264 goto cleanup3;
265
Tony Luck25667d62007-03-06 13:31:45 -0800266 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(char *));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600267
268 /*
269 * Get the overflow emergency buffer
270 */
271 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
272 get_order(io_tlb_overflow));
273 if (!io_tlb_overflow_buffer)
274 goto cleanup4;
275
Tony Luck25667d62007-03-06 13:31:45 -0800276 printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - "
277 "0x%lx\n", bytes >> 20,
Ian Campbelle08e1f72008-12-16 12:17:30 -0800278 swiotlb_virt_to_bus(io_tlb_start), swiotlb_virt_to_bus(io_tlb_end));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600279
280 return 0;
281
282cleanup4:
Tony Luck25667d62007-03-06 13:31:45 -0800283 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
284 sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600285 io_tlb_orig_addr = NULL;
286cleanup3:
Tony Luck25667d62007-03-06 13:31:45 -0800287 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
288 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600289 io_tlb_list = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600290cleanup2:
Jan Beulich563aaf02007-02-05 18:51:25 -0800291 io_tlb_end = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600292 free_pages((unsigned long)io_tlb_start, order);
293 io_tlb_start = NULL;
294cleanup1:
295 io_tlb_nslabs = req_nslabs;
296 return -ENOMEM;
297}
298
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800299static int
FUJITA Tomonori27979822008-09-10 01:06:49 +0900300address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
FUJITA Tomonori07a2c012008-09-19 02:02:05 +0900302 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Ian Campbellb81ea272008-12-16 12:17:31 -0800305static inline int range_needs_mapping(void *ptr, size_t size)
306{
307 return swiotlb_force || swiotlb_arch_range_needs_mapping(ptr, size);
308}
309
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900310static int is_swiotlb_buffer(char *addr)
311{
312 return addr >= io_tlb_start && addr < io_tlb_end;
313}
314
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800315static void
316__sync_single(char *buffer, char *dma_addr, size_t size, int dir)
317{
318 if (dir == DMA_TO_DEVICE)
319 memcpy(dma_addr, buffer, size);
320 else
321 memcpy(buffer, dma_addr, size);
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * Allocates bounce buffer and returns its kernel virtual address.
326 */
327static void *
Tony Luck25667d62007-03-06 13:31:45 -0800328map_single(struct device *hwdev, char *buffer, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned long flags;
331 char *dma_addr;
332 unsigned int nslots, stride, index, wrap;
333 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800334 unsigned long start_dma_addr;
335 unsigned long mask;
336 unsigned long offset_slots;
337 unsigned long max_slots;
338
339 mask = dma_get_seg_boundary(hwdev);
Ian Campbelle08e1f72008-12-16 12:17:30 -0800340 start_dma_addr = swiotlb_virt_to_bus(io_tlb_start) & mask;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800341
342 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Ian Campbella5ddde4a2008-12-16 12:17:29 -0800343
344 /*
345 * Carefully handle integer overflow which can occur when mask == ~0UL.
346 */
Jan Beulichb15a38912008-03-13 09:13:30 +0000347 max_slots = mask + 1
348 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
349 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /*
352 * For mappings greater than a page, we limit the stride (and
353 * hence alignment) to a page size.
354 */
355 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
356 if (size > PAGE_SIZE)
357 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
358 else
359 stride = 1;
360
Eric Sesterhenn34814542006-03-24 18:47:11 +0100361 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /*
364 * Find suitable number of IO TLB entries size that will fit this
365 * request and allocate a buffer from that IO TLB pool.
366 */
367 spin_lock_irqsave(&io_tlb_lock, flags);
Andrew Mortona7133a12008-04-29 00:59:36 -0700368 index = ALIGN(io_tlb_index, stride);
369 if (index >= io_tlb_nslabs)
370 index = 0;
371 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Andrew Mortona7133a12008-04-29 00:59:36 -0700373 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700374 while (iommu_is_span_boundary(index, nslots, offset_slots,
375 max_slots)) {
Jan Beulichb15a38912008-03-13 09:13:30 +0000376 index += stride;
377 if (index >= io_tlb_nslabs)
378 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700379 if (index == wrap)
380 goto not_found;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Andrew Mortona7133a12008-04-29 00:59:36 -0700383 /*
384 * If we find a slot that indicates we have 'nslots' number of
385 * contiguous buffers, we allocate the buffers from that slot
386 * and mark the entries as '0' indicating unavailable.
387 */
388 if (io_tlb_list[index] >= nslots) {
389 int count = 0;
390
391 for (i = index; i < (int) (index + nslots); i++)
392 io_tlb_list[i] = 0;
393 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
394 io_tlb_list[i] = ++count;
395 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
396
397 /*
398 * Update the indices to avoid searching in the next
399 * round.
400 */
401 io_tlb_index = ((index + nslots) < io_tlb_nslabs
402 ? (index + nslots) : 0);
403
404 goto found;
405 }
406 index += stride;
407 if (index >= io_tlb_nslabs)
408 index = 0;
409 } while (index != wrap);
410
411not_found:
412 spin_unlock_irqrestore(&io_tlb_lock, flags);
413 return NULL;
414found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 spin_unlock_irqrestore(&io_tlb_lock, flags);
416
417 /*
418 * Save away the mapping from the original address to the DMA address.
419 * This is needed when we sync the memory. Then we sync the buffer if
420 * needed.
421 */
Keir Fraserdf336d12007-07-21 04:37:24 -0700422 for (i = 0; i < nslots; i++)
423 io_tlb_orig_addr[index+i] = buffer + (i << IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800425 __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return dma_addr;
428}
429
430/*
431 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
432 */
433static void
434unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
435{
436 unsigned long flags;
437 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
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
441 /*
442 * First, sync the memory before unmapping the entry
443 */
Tony Luck25667d62007-03-06 13:31:45 -0800444 if (buffer && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * bounce... copy the data back into the original buffer * and
447 * delete the bounce buffer.
448 */
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800449 __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /*
452 * Return the buffer to the free list by setting the corresponding
453 * entries to indicate the number of contigous entries available.
454 * While returning the entries to the free list, we merge the entries
455 * with slots below and above the pool being returned.
456 */
457 spin_lock_irqsave(&io_tlb_lock, flags);
458 {
459 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
460 io_tlb_list[index + nslots] : 0);
461 /*
462 * Step 1: return the slots to the free list, merging the
463 * slots with superceeding slots
464 */
465 for (i = index + nslots - 1; i >= index; i--)
466 io_tlb_list[i] = ++count;
467 /*
468 * Step 2: merge the returned slots with the preceding slots,
469 * if available (non zero)
470 */
471 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
472 io_tlb_list[i] = ++count;
473 }
474 spin_unlock_irqrestore(&io_tlb_lock, flags);
475}
476
477static void
John W. Linvillede69e0f2005-09-29 14:44:57 -0700478sync_single(struct device *hwdev, char *dma_addr, size_t size,
479 int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Tony Luck25667d62007-03-06 13:31:45 -0800482 char *buffer = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Keir Fraserdf336d12007-07-21 04:37:24 -0700484 buffer += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
485
John W. Linvillede69e0f2005-09-29 14:44:57 -0700486 switch (target) {
487 case SYNC_FOR_CPU:
488 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800489 __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100490 else
491 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700492 break;
493 case SYNC_FOR_DEVICE:
494 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800495 __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100496 else
497 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700498 break;
499 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504void *
505swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Al Viro06a54492005-10-21 03:21:03 -0400506 dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Jan Beulich563aaf02007-02-05 18:51:25 -0800508 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 void *ret;
510 int order = get_order(size);
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900511 u64 dma_mask = DMA_32BIT_MASK;
512
513 if (hwdev && hwdev->coherent_dma_mask)
514 dma_mask = hwdev->coherent_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Tony Luck25667d62007-03-06 13:31:45 -0800516 ret = (void *)__get_free_pages(flags, order);
Ian Campbelle08e1f72008-12-16 12:17:30 -0800517 if (ret && !is_buffer_dma_capable(dma_mask, swiotlb_virt_to_bus(ret), size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
519 * The allocated memory isn't reachable by the device.
520 * Fall back on swiotlb_map_single().
521 */
522 free_pages((unsigned long) ret, order);
523 ret = NULL;
524 }
525 if (!ret) {
526 /*
527 * We are either out of memory or the device can't DMA
528 * to GFP_DMA memory; fall back on
529 * swiotlb_map_single(), which will grab memory from
530 * the lowest available address range.
531 */
FUJITA Tomonori9dfda122008-09-08 18:53:48 +0900532 ret = map_single(hwdev, NULL, size, DMA_FROM_DEVICE);
533 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536
537 memset(ret, 0, size);
Ian Campbelle08e1f72008-12-16 12:17:30 -0800538 dev_addr = swiotlb_virt_to_bus(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* Confirm address can be DMA'd by device */
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900541 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
Jan Beulich563aaf02007-02-05 18:51:25 -0800542 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900543 (unsigned long long)dma_mask,
Jan Beulich563aaf02007-02-05 18:51:25 -0800544 (unsigned long long)dev_addr);
FUJITA Tomonoria2b89b52008-10-23 18:42:03 +0900545
546 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
547 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
548 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 *dma_handle = dev_addr;
551 return ret;
552}
553
554void
555swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
556 dma_addr_t dma_handle)
557{
David Brownellaa248862007-08-10 13:10:27 -0700558 WARN_ON(irqs_disabled());
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900559 if (!is_swiotlb_buffer(vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 free_pages((unsigned long) vaddr, get_order(size));
561 else
562 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
FUJITA Tomonori21f6c4d2008-09-08 18:53:49 +0900563 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566static void
567swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
568{
569 /*
570 * Ran out of IOMMU space for this operation. This is very bad.
571 * Unfortunately the drivers cannot handle this operation properly.
Tony Luck17e5ad62005-09-29 15:52:13 -0700572 * unless they check for dma_mapping_error (most don't)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 * When the mapping is small enough return a static buffer to limit
574 * the damage, or panic when the transfer is too big.
575 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800576 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 "device %s\n", size, dev ? dev->bus_id : "?");
578
579 if (size > io_tlb_overflow && do_panic) {
Tony Luck17e5ad62005-09-29 15:52:13 -0700580 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
581 panic("DMA: Memory would be corrupted\n");
582 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
583 panic("DMA: Random memory would be DMAed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585}
586
587/*
588 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700589 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *
591 * Once the device is given the dma address, the device owns this memory until
592 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
593 */
594dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -0700595swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
596 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800598 dma_addr_t dev_addr = swiotlb_virt_to_bus(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 void *map;
600
Eric Sesterhenn34814542006-03-24 18:47:11 +0100601 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
603 * If the pointer passed in happens to be in the device's DMA window,
604 * we can safely return the device addr and not worry about bounce
605 * buffering it.
606 */
Ian Campbellb81ea272008-12-16 12:17:31 -0800607 if (!address_needs_mapping(hwdev, dev_addr, size) &&
608 !range_needs_mapping(ptr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return dev_addr;
610
611 /*
612 * Oh well, have to allocate and map a bounce buffer.
613 */
Tony Luck25667d62007-03-06 13:31:45 -0800614 map = map_single(hwdev, ptr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!map) {
616 swiotlb_full(hwdev, size, dir, 1);
617 map = io_tlb_overflow_buffer;
618 }
619
Ian Campbelle08e1f72008-12-16 12:17:30 -0800620 dev_addr = swiotlb_virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /*
623 * Ensure that the address returned is DMA'ble
624 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900625 if (address_needs_mapping(hwdev, dev_addr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 panic("map_single: bounce buffer is not DMA'ble");
627
628 return dev_addr;
629}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700630EXPORT_SYMBOL(swiotlb_map_single_attrs);
631
632dma_addr_t
633swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
634{
635 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
636}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 * Unmap a single streaming mode DMA translation. The dma_addr and size must
640 * match what was provided for in a previous swiotlb_map_single call. All
641 * other usages are undefined.
642 *
643 * After this call, reads by the cpu to the buffer are guaranteed to see
644 * whatever the device wrote there.
645 */
646void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700647swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
648 size_t size, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800650 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric Sesterhenn34814542006-03-24 18:47:11 +0100652 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900653 if (is_swiotlb_buffer(dma_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 unmap_single(hwdev, dma_addr, size, dir);
655 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800656 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700658EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Arthur Kepner309df0c2008-04-29 01:00:32 -0700660void
661swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
662 int dir)
663{
664 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
665}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/*
667 * Make physical memory consistent for a single streaming mode DMA translation
668 * after a transfer.
669 *
670 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700671 * using the cpu, yet do not wish to teardown the dma mapping, you must
672 * call this function before doing so. At the next point you give the dma
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 * address back to the card, you must first perform a
674 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
675 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800676static void
John W. Linville8270f3f2005-09-29 14:43:32 -0700677swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700678 size_t size, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800680 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Eric Sesterhenn34814542006-03-24 18:47:11 +0100682 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900683 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700684 sync_single(hwdev, dma_addr, size, dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800686 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689void
John W. Linville8270f3f2005-09-29 14:43:32 -0700690swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
691 size_t size, int dir)
692{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700693 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700694}
695
696void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
698 size_t size, int dir)
699{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700700 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703/*
John W. Linville878a97c2005-09-29 14:44:23 -0700704 * Same as above, but for a sub-range of the mapping.
705 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800706static void
John W. Linville878a97c2005-09-29 14:44:23 -0700707swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700708 unsigned long offset, size_t size,
709 int dir, int target)
John W. Linville878a97c2005-09-29 14:44:23 -0700710{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800711 char *dma_addr = swiotlb_bus_to_virt(dev_addr) + offset;
John W. Linville878a97c2005-09-29 14:44:23 -0700712
Eric Sesterhenn34814542006-03-24 18:47:11 +0100713 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900714 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700715 sync_single(hwdev, dma_addr, size, dir, target);
John W. Linville878a97c2005-09-29 14:44:23 -0700716 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800717 dma_mark_clean(dma_addr, size);
John W. Linville878a97c2005-09-29 14:44:23 -0700718}
719
720void
721swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
722 unsigned long offset, size_t size, int dir)
723{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700724 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
725 SYNC_FOR_CPU);
John W. Linville878a97c2005-09-29 14:44:23 -0700726}
727
728void
729swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
730 unsigned long offset, size_t size, int dir)
731{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700732 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
733 SYNC_FOR_DEVICE);
John W. Linville878a97c2005-09-29 14:44:23 -0700734}
735
Arthur Kepner309df0c2008-04-29 01:00:32 -0700736void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
737 struct dma_attrs *);
John W. Linville878a97c2005-09-29 14:44:23 -0700738/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * Map a set of buffers described by scatterlist in streaming mode for DMA.
740 * This is the scatter-gather version of the above swiotlb_map_single
741 * interface. Here the scatter gather list elements are each tagged with the
742 * appropriate dma address and length. They are obtained via
743 * sg_dma_{address,length}(SG).
744 *
745 * NOTE: An implementation may be able to use a smaller number of
746 * DMA address/length pairs than there are SG table elements.
747 * (for example via virtual mapping capabilities)
748 * The routine returns the number of addr/length pairs actually
749 * used, at most nents.
750 *
751 * Device ownership issues as mentioned above for swiotlb_map_single are the
752 * same here.
753 */
754int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700755swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
756 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200758 struct scatterlist *sg;
Tony Luck25667d62007-03-06 13:31:45 -0800759 void *addr;
Jan Beulich563aaf02007-02-05 18:51:25 -0800760 dma_addr_t dev_addr;
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) {
Tony Luck25667d62007-03-06 13:31:45 -0800766 addr = SG_ENT_VIRT_ADDRESS(sg);
Ian Campbelle08e1f72008-12-16 12:17:30 -0800767 dev_addr = swiotlb_virt_to_bus(addr);
Ian Campbellb81ea272008-12-16 12:17:31 -0800768 if (range_needs_mapping(sg_virt(sg), sg->length) ||
FUJITA Tomonori27979822008-09-10 01:06:49 +0900769 address_needs_mapping(hwdev, dev_addr, sg->length)) {
Tony Luck25667d62007-03-06 13:31:45 -0800770 void *map = map_single(hwdev, addr, sg->length, dir);
Andi Kleen7e870232005-12-20 14:45:19 +0100771 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 /* Don't panic here, we expect map_sg users
773 to do proper error handling. */
774 swiotlb_full(hwdev, sg->length, dir, 0);
Arthur Kepner309df0c2008-04-29 01:00:32 -0700775 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
776 attrs);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200777 sgl[0].dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return 0;
779 }
Ian Campbelle08e1f72008-12-16 12:17:30 -0800780 sg->dma_address = swiotlb_virt_to_bus(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 } else
782 sg->dma_address = dev_addr;
783 sg->dma_length = sg->length;
784 }
785 return nelems;
786}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700787EXPORT_SYMBOL(swiotlb_map_sg_attrs);
788
789int
790swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
791 int dir)
792{
793 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796/*
797 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
798 * concerning calls here are the same as for swiotlb_unmap_single() above.
799 */
800void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700801swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
802 int nelems, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200804 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int i;
806
Eric Sesterhenn34814542006-03-24 18:47:11 +0100807 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Jens Axboedbfd49f2007-05-11 14:56:18 +0200809 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Ian Campbelle08e1f72008-12-16 12:17:30 -0800811 unmap_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
Jan Beulich93fbff62007-02-05 18:49:45 -0800812 sg->dma_length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800814 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700817EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
818
819void
820swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
821 int dir)
822{
823 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
824}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826/*
827 * Make physical memory consistent for a set of streaming mode DMA translations
828 * after a transfer.
829 *
830 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
831 * and usage.
832 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800833static void
Jens Axboedbfd49f2007-05-11 14:56:18 +0200834swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700835 int nelems, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200837 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 int i;
839
Eric Sesterhenn34814542006-03-24 18:47:11 +0100840 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Jens Axboedbfd49f2007-05-11 14:56:18 +0200842 for_each_sg(sgl, sg, nelems, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
Ian Campbelle08e1f72008-12-16 12:17:30 -0800844 sync_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
John W. Linvillede69e0f2005-09-29 14:44:57 -0700845 sg->dma_length, dir, target);
Jan Beulichcde14bb2007-02-05 18:46:40 -0800846 else if (dir == DMA_FROM_DEVICE)
847 dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
851void
John W. Linville8270f3f2005-09-29 14:43:32 -0700852swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
853 int nelems, int dir)
854{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700855 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700856}
857
858void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
860 int nelems, int dir)
861{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700862 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700866swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800868 return (dma_addr == swiotlb_virt_to_bus(io_tlb_overflow_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700872 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700874 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * this function.
876 */
877int
Jan Beulich563aaf02007-02-05 18:51:25 -0800878swiotlb_dma_supported(struct device *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800880 return swiotlb_virt_to_bus(io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883EXPORT_SYMBOL(swiotlb_map_single);
884EXPORT_SYMBOL(swiotlb_unmap_single);
885EXPORT_SYMBOL(swiotlb_map_sg);
886EXPORT_SYMBOL(swiotlb_unmap_sg);
887EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
888EXPORT_SYMBOL(swiotlb_sync_single_for_device);
John W. Linville878a97c2005-09-29 14:44:23 -0700889EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
890EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
892EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
893EXPORT_SYMBOL(swiotlb_dma_mapping_error);
Tony Luck25667d62007-03-06 13:31:45 -0800894EXPORT_SYMBOL(swiotlb_alloc_coherent);
895EXPORT_SYMBOL(swiotlb_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896EXPORT_SYMBOL(swiotlb_dma_supported);