blob: 98a7a4450e022c1e613adeae6d523d5b64c952a4 [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>
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -080029#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/dma.h>
Tony Luck17e5ad62005-09-29 15:52:13 -070033#include <asm/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <linux/init.h>
36#include <linux/bootmem.h>
FUJITA Tomonoria8522502008-04-29 00:59:36 -070037#include <linux/iommu-helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define OFFSET(val,align) ((unsigned long) \
40 ( (val) & ( (align) - 1)))
41
Alex Williamson0b9afed2005-09-06 11:20:49 -060042#define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
43
44/*
45 * Minimum IO TLB size to bother booting with. Systems with mainly
46 * 64bit capable cards will only lightly use the swiotlb. If we can't
47 * allocate a contiguous 1MB, we're probably in trouble anyway.
48 */
49#define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
50
John W. Linvillede69e0f2005-09-29 14:44:57 -070051/*
52 * Enumeration for sync targets
53 */
54enum dma_sync_target {
55 SYNC_FOR_CPU = 0,
56 SYNC_FOR_DEVICE = 1,
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059int swiotlb_force;
60
61/*
62 * Used to do a quick range check in swiotlb_unmap_single and
63 * swiotlb_sync_single_*, to see if the memory was in fact allocated by this
64 * API.
65 */
66static char *io_tlb_start, *io_tlb_end;
67
68/*
69 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and
70 * io_tlb_end. This is command line adjustable via setup_io_tlb_npages.
71 */
72static unsigned long io_tlb_nslabs;
73
74/*
75 * When the IOMMU overflows we return a fallback buffer. This sets the size.
76 */
77static unsigned long io_tlb_overflow = 32*1024;
78
79void *io_tlb_overflow_buffer;
80
81/*
82 * This is a free list describing the number of free entries available from
83 * each index
84 */
85static unsigned int *io_tlb_list;
86static unsigned int io_tlb_index;
87
88/*
89 * We need to save away the original address corresponding to a mapped entry
90 * for the sync operations.
91 */
Becky Brucebc40ac62008-12-22 10:26:08 -080092static phys_addr_t *io_tlb_orig_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * Protect the above data structures in the map and unmap calls
96 */
97static DEFINE_SPINLOCK(io_tlb_lock);
98
99static int __init
100setup_io_tlb_npages(char *str)
101{
102 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700103 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* avoid tail segment of size < IO_TLB_SEGSIZE */
105 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
106 }
107 if (*str == ',')
108 ++str;
109 if (!strcmp(str, "force"))
110 swiotlb_force = 1;
111 return 1;
112}
113__setup("swiotlb=", setup_io_tlb_npages);
114/* make io_tlb_overflow tunable too? */
115
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800116void * __weak swiotlb_alloc_boot(size_t size, unsigned long nslabs)
117{
118 return alloc_bootmem_low_pages(size);
119}
120
121void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs)
122{
123 return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
124}
125
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800126dma_addr_t __weak swiotlb_phys_to_bus(struct device *hwdev, phys_addr_t paddr)
Ian Campbelle08e1f72008-12-16 12:17:30 -0800127{
128 return paddr;
129}
130
131phys_addr_t __weak swiotlb_bus_to_phys(dma_addr_t baddr)
132{
133 return baddr;
134}
135
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800136static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
137 volatile void *address)
Ian Campbelle08e1f72008-12-16 12:17:30 -0800138{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800139 return swiotlb_phys_to_bus(hwdev, virt_to_phys(address));
Ian Campbelle08e1f72008-12-16 12:17:30 -0800140}
141
142static void *swiotlb_bus_to_virt(dma_addr_t address)
143{
144 return phys_to_virt(swiotlb_bus_to_phys(address));
145}
146
Ian Campbellb81ea272008-12-16 12:17:31 -0800147int __weak swiotlb_arch_range_needs_mapping(void *ptr, size_t size)
148{
149 return 0;
150}
151
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800152static dma_addr_t swiotlb_sg_to_bus(struct device *hwdev, struct scatterlist *sg)
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800153{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800154 return swiotlb_phys_to_bus(hwdev, page_to_phys(sg_page(sg)) + sg->offset);
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800155}
156
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800157static void swiotlb_print_info(unsigned long bytes)
158{
159 phys_addr_t pstart, pend;
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800160
161 pstart = virt_to_phys(io_tlb_start);
162 pend = virt_to_phys(io_tlb_end);
163
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800164 printk(KERN_INFO "Placing %luMB software IO TLB between %p - %p\n",
165 bytes >> 20, io_tlb_start, io_tlb_end);
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800166 printk(KERN_INFO "software IO TLB at phys %#llx - %#llx\n",
167 (unsigned long long)pstart,
168 (unsigned long long)pend);
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
172 * Statically reserve bounce buffer space and initialize bounce buffer data
Tony Luck17e5ad62005-09-29 15:52:13 -0700173 * structures for the software IO TLB used to implement the DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800175void __init
176swiotlb_init_with_default_size(size_t default_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Jan Beulich563aaf02007-02-05 18:51:25 -0800178 unsigned long i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (!io_tlb_nslabs) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700181 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
183 }
184
Jan Beulich563aaf02007-02-05 18:51:25 -0800185 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * Get IO TLB memory from the low pages
189 */
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800190 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!io_tlb_start)
192 panic("Cannot allocate SWIOTLB buffer");
Jan Beulich563aaf02007-02-05 18:51:25 -0800193 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 /*
196 * Allocate and initialize the free list array. This array is used
197 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
198 * between io_tlb_start and io_tlb_end.
199 */
200 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
Tony Luck25667d62007-03-06 13:31:45 -0800201 for (i = 0; i < io_tlb_nslabs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
203 io_tlb_index = 0;
Becky Brucebc40ac62008-12-22 10:26:08 -0800204 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(phys_addr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /*
207 * Get the overflow emergency buffer
208 */
209 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
Jan Beulich563aaf02007-02-05 18:51:25 -0800210 if (!io_tlb_overflow_buffer)
211 panic("Cannot allocate SWIOTLB overflow buffer!\n");
212
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800213 swiotlb_print_info(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Jan Beulich563aaf02007-02-05 18:51:25 -0800216void __init
217swiotlb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Tony Luck25667d62007-03-06 13:31:45 -0800219 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Alex Williamson0b9afed2005-09-06 11:20:49 -0600222/*
223 * Systems with larger DMA zones (those that don't support ISA) can
224 * initialize the swiotlb later using the slab allocator if needed.
225 * This should be just like above, but with some error catching.
226 */
227int
Jan Beulich563aaf02007-02-05 18:51:25 -0800228swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600229{
Jan Beulich563aaf02007-02-05 18:51:25 -0800230 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600231 unsigned int order;
232
233 if (!io_tlb_nslabs) {
234 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
235 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
236 }
237
238 /*
239 * Get IO TLB memory from the low pages
240 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800241 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600242 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800243 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600244
245 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800246 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600247 if (io_tlb_start)
248 break;
249 order--;
250 }
251
252 if (!io_tlb_start)
253 goto cleanup1;
254
Jan Beulich563aaf02007-02-05 18:51:25 -0800255 if (order != get_order(bytes)) {
Alex Williamson0b9afed2005-09-06 11:20:49 -0600256 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
257 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
258 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800259 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600260 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800261 io_tlb_end = io_tlb_start + bytes;
262 memset(io_tlb_start, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600263
264 /*
265 * Allocate and initialize the free list array. This array is used
266 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
267 * between io_tlb_start and io_tlb_end.
268 */
269 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
270 get_order(io_tlb_nslabs * sizeof(int)));
271 if (!io_tlb_list)
272 goto cleanup2;
273
274 for (i = 0; i < io_tlb_nslabs; i++)
275 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
276 io_tlb_index = 0;
277
Becky Brucebc40ac62008-12-22 10:26:08 -0800278 io_tlb_orig_addr = (phys_addr_t *)
279 __get_free_pages(GFP_KERNEL,
280 get_order(io_tlb_nslabs *
281 sizeof(phys_addr_t)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600282 if (!io_tlb_orig_addr)
283 goto cleanup3;
284
Becky Brucebc40ac62008-12-22 10:26:08 -0800285 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(phys_addr_t));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600286
287 /*
288 * Get the overflow emergency buffer
289 */
290 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
291 get_order(io_tlb_overflow));
292 if (!io_tlb_overflow_buffer)
293 goto cleanup4;
294
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800295 swiotlb_print_info(bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600296
297 return 0;
298
299cleanup4:
Becky Brucebc40ac62008-12-22 10:26:08 -0800300 free_pages((unsigned long)io_tlb_orig_addr,
301 get_order(io_tlb_nslabs * sizeof(phys_addr_t)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600302 io_tlb_orig_addr = NULL;
303cleanup3:
Tony Luck25667d62007-03-06 13:31:45 -0800304 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
305 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600306 io_tlb_list = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600307cleanup2:
Jan Beulich563aaf02007-02-05 18:51:25 -0800308 io_tlb_end = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600309 free_pages((unsigned long)io_tlb_start, order);
310 io_tlb_start = NULL;
311cleanup1:
312 io_tlb_nslabs = req_nslabs;
313 return -ENOMEM;
314}
315
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800316static int
FUJITA Tomonori27979822008-09-10 01:06:49 +0900317address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
FUJITA Tomonori07a2c012008-09-19 02:02:05 +0900319 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Ian Campbellb81ea272008-12-16 12:17:31 -0800322static inline int range_needs_mapping(void *ptr, size_t size)
323{
324 return swiotlb_force || swiotlb_arch_range_needs_mapping(ptr, size);
325}
326
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900327static int is_swiotlb_buffer(char *addr)
328{
329 return addr >= io_tlb_start && addr < io_tlb_end;
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * Allocates bounce buffer and returns its kernel virtual address.
334 */
335static void *
Becky Brucebc40ac62008-12-22 10:26:08 -0800336map_single(struct device *hwdev, phys_addr_t phys, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 unsigned long flags;
339 char *dma_addr;
340 unsigned int nslots, stride, index, wrap;
341 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800342 unsigned long start_dma_addr;
343 unsigned long mask;
344 unsigned long offset_slots;
345 unsigned long max_slots;
346
347 mask = dma_get_seg_boundary(hwdev);
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800348 start_dma_addr = swiotlb_virt_to_bus(hwdev, io_tlb_start) & mask;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800349
350 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Ian Campbella5ddde4a2008-12-16 12:17:29 -0800351
352 /*
353 * Carefully handle integer overflow which can occur when mask == ~0UL.
354 */
Jan Beulichb15a38912008-03-13 09:13:30 +0000355 max_slots = mask + 1
356 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
357 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /*
360 * For mappings greater than a page, we limit the stride (and
361 * hence alignment) to a page size.
362 */
363 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
364 if (size > PAGE_SIZE)
365 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
366 else
367 stride = 1;
368
Eric Sesterhenn34814542006-03-24 18:47:11 +0100369 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /*
372 * Find suitable number of IO TLB entries size that will fit this
373 * request and allocate a buffer from that IO TLB pool.
374 */
375 spin_lock_irqsave(&io_tlb_lock, flags);
Andrew Mortona7133a12008-04-29 00:59:36 -0700376 index = ALIGN(io_tlb_index, stride);
377 if (index >= io_tlb_nslabs)
378 index = 0;
379 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Andrew Mortona7133a12008-04-29 00:59:36 -0700381 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700382 while (iommu_is_span_boundary(index, nslots, offset_slots,
383 max_slots)) {
Jan Beulichb15a38912008-03-13 09:13:30 +0000384 index += stride;
385 if (index >= io_tlb_nslabs)
386 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700387 if (index == wrap)
388 goto not_found;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Andrew Mortona7133a12008-04-29 00:59:36 -0700391 /*
392 * If we find a slot that indicates we have 'nslots' number of
393 * contiguous buffers, we allocate the buffers from that slot
394 * and mark the entries as '0' indicating unavailable.
395 */
396 if (io_tlb_list[index] >= nslots) {
397 int count = 0;
398
399 for (i = index; i < (int) (index + nslots); i++)
400 io_tlb_list[i] = 0;
401 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
402 io_tlb_list[i] = ++count;
403 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
404
405 /*
406 * Update the indices to avoid searching in the next
407 * round.
408 */
409 io_tlb_index = ((index + nslots) < io_tlb_nslabs
410 ? (index + nslots) : 0);
411
412 goto found;
413 }
414 index += stride;
415 if (index >= io_tlb_nslabs)
416 index = 0;
417 } while (index != wrap);
418
419not_found:
420 spin_unlock_irqrestore(&io_tlb_lock, flags);
421 return NULL;
422found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 spin_unlock_irqrestore(&io_tlb_lock, flags);
424
425 /*
426 * Save away the mapping from the original address to the DMA address.
427 * This is needed when we sync the memory. Then we sync the buffer if
428 * needed.
429 */
Becky Brucebc40ac62008-12-22 10:26:08 -0800430 for (i = 0; i < nslots; i++)
431 io_tlb_orig_addr[index+i] = phys + (i << IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
Becky Brucebc40ac62008-12-22 10:26:08 -0800433 memcpy(dma_addr, phys_to_virt(phys), size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 return dma_addr;
436}
437
438/*
439 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
440 */
441static void
442unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
443{
444 unsigned long flags;
445 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
446 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Becky Brucebc40ac62008-12-22 10:26:08 -0800447 phys_addr_t phys = io_tlb_orig_addr[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /*
450 * First, sync the memory before unmapping the entry
451 */
Becky Brucebc40ac62008-12-22 10:26:08 -0800452 if (phys && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
454 * bounce... copy the data back into the original buffer * and
455 * delete the bounce buffer.
456 */
Becky Brucebc40ac62008-12-22 10:26:08 -0800457 memcpy(phys_to_virt(phys), dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /*
460 * Return the buffer to the free list by setting the corresponding
461 * entries to indicate the number of contigous entries available.
462 * While returning the entries to the free list, we merge the entries
463 * with slots below and above the pool being returned.
464 */
465 spin_lock_irqsave(&io_tlb_lock, flags);
466 {
467 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
468 io_tlb_list[index + nslots] : 0);
469 /*
470 * Step 1: return the slots to the free list, merging the
471 * slots with superceeding slots
472 */
473 for (i = index + nslots - 1; i >= index; i--)
474 io_tlb_list[i] = ++count;
475 /*
476 * Step 2: merge the returned slots with the preceding slots,
477 * if available (non zero)
478 */
479 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
480 io_tlb_list[i] = ++count;
481 }
482 spin_unlock_irqrestore(&io_tlb_lock, flags);
483}
484
485static void
John W. Linvillede69e0f2005-09-29 14:44:57 -0700486sync_single(struct device *hwdev, char *dma_addr, size_t size,
487 int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Becky Brucebc40ac62008-12-22 10:26:08 -0800489 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
490 phys_addr_t phys = io_tlb_orig_addr[index];
491
492 phys += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
Keir Fraserdf336d12007-07-21 04:37:24 -0700493
John W. Linvillede69e0f2005-09-29 14:44:57 -0700494 switch (target) {
495 case SYNC_FOR_CPU:
496 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Becky Brucebc40ac62008-12-22 10:26:08 -0800497 memcpy(phys_to_virt(phys), dma_addr, size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100498 else
499 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700500 break;
501 case SYNC_FOR_DEVICE:
502 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Becky Brucebc40ac62008-12-22 10:26:08 -0800503 memcpy(dma_addr, phys_to_virt(phys), size);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100504 else
505 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700506 break;
507 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512void *
513swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Al Viro06a54492005-10-21 03:21:03 -0400514 dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jan Beulich563aaf02007-02-05 18:51:25 -0800516 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 void *ret;
518 int order = get_order(size);
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900519 u64 dma_mask = DMA_32BIT_MASK;
520
521 if (hwdev && hwdev->coherent_dma_mask)
522 dma_mask = hwdev->coherent_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Tony Luck25667d62007-03-06 13:31:45 -0800524 ret = (void *)__get_free_pages(flags, order);
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800525 if (ret &&
526 !is_buffer_dma_capable(dma_mask, swiotlb_virt_to_bus(hwdev, ret),
527 size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /*
529 * The allocated memory isn't reachable by the device.
530 * Fall back on swiotlb_map_single().
531 */
532 free_pages((unsigned long) ret, order);
533 ret = NULL;
534 }
535 if (!ret) {
536 /*
537 * We are either out of memory or the device can't DMA
538 * to GFP_DMA memory; fall back on
539 * swiotlb_map_single(), which will grab memory from
540 * the lowest available address range.
541 */
Becky Brucebc40ac62008-12-22 10:26:08 -0800542 ret = map_single(hwdev, 0, size, DMA_FROM_DEVICE);
FUJITA Tomonori9dfda122008-09-08 18:53:48 +0900543 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
547 memset(ret, 0, size);
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800548 dev_addr = swiotlb_virt_to_bus(hwdev, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Confirm address can be DMA'd by device */
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900551 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
Jan Beulich563aaf02007-02-05 18:51:25 -0800552 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900553 (unsigned long long)dma_mask,
Jan Beulich563aaf02007-02-05 18:51:25 -0800554 (unsigned long long)dev_addr);
FUJITA Tomonoria2b89b52008-10-23 18:42:03 +0900555
556 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
557 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
558 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 *dma_handle = dev_addr;
561 return ret;
562}
563
564void
565swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
566 dma_addr_t dma_handle)
567{
David Brownellaa248862007-08-10 13:10:27 -0700568 WARN_ON(irqs_disabled());
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900569 if (!is_swiotlb_buffer(vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 free_pages((unsigned long) vaddr, get_order(size));
571 else
572 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
FUJITA Tomonori21f6c4d2008-09-08 18:53:49 +0900573 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static void
577swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
578{
579 /*
580 * Ran out of IOMMU space for this operation. This is very bad.
581 * Unfortunately the drivers cannot handle this operation properly.
Tony Luck17e5ad62005-09-29 15:52:13 -0700582 * unless they check for dma_mapping_error (most don't)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * When the mapping is small enough return a static buffer to limit
584 * the damage, or panic when the transfer is too big.
585 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800586 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 "device %s\n", size, dev ? dev->bus_id : "?");
588
589 if (size > io_tlb_overflow && do_panic) {
Tony Luck17e5ad62005-09-29 15:52:13 -0700590 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
591 panic("DMA: Memory would be corrupted\n");
592 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
593 panic("DMA: Random memory would be DMAed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595}
596
597/*
598 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700599 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
601 * Once the device is given the dma address, the device owns this memory until
602 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
603 */
604dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -0700605swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
606 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800608 dma_addr_t dev_addr = swiotlb_virt_to_bus(hwdev, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 void *map;
610
Eric Sesterhenn34814542006-03-24 18:47:11 +0100611 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * If the pointer passed in happens to be in the device's DMA window,
614 * we can safely return the device addr and not worry about bounce
615 * buffering it.
616 */
Ian Campbellb81ea272008-12-16 12:17:31 -0800617 if (!address_needs_mapping(hwdev, dev_addr, size) &&
618 !range_needs_mapping(ptr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return dev_addr;
620
621 /*
622 * Oh well, have to allocate and map a bounce buffer.
623 */
Becky Brucebc40ac62008-12-22 10:26:08 -0800624 map = map_single(hwdev, virt_to_phys(ptr), size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!map) {
626 swiotlb_full(hwdev, size, dir, 1);
627 map = io_tlb_overflow_buffer;
628 }
629
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800630 dev_addr = swiotlb_virt_to_bus(hwdev, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 /*
633 * Ensure that the address returned is DMA'ble
634 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900635 if (address_needs_mapping(hwdev, dev_addr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 panic("map_single: bounce buffer is not DMA'ble");
637
638 return dev_addr;
639}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700640EXPORT_SYMBOL(swiotlb_map_single_attrs);
641
642dma_addr_t
643swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
644{
645 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * Unmap a single streaming mode DMA translation. The dma_addr and size must
650 * match what was provided for in a previous swiotlb_map_single call. All
651 * other usages are undefined.
652 *
653 * After this call, reads by the cpu to the buffer are guaranteed to see
654 * whatever the device wrote there.
655 */
656void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700657swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
658 size_t size, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800660 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Eric Sesterhenn34814542006-03-24 18:47:11 +0100662 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900663 if (is_swiotlb_buffer(dma_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 unmap_single(hwdev, dma_addr, size, dir);
665 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800666 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700668EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Arthur Kepner309df0c2008-04-29 01:00:32 -0700670void
671swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
672 int dir)
673{
674 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
675}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/*
677 * Make physical memory consistent for a single streaming mode DMA translation
678 * after a transfer.
679 *
680 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700681 * using the cpu, yet do not wish to teardown the dma mapping, you must
682 * call this function before doing so. At the next point you give the dma
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 * address back to the card, you must first perform a
684 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
685 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800686static void
John W. Linville8270f3f2005-09-29 14:43:32 -0700687swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700688 size_t size, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800690 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Eric Sesterhenn34814542006-03-24 18:47:11 +0100692 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900693 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700694 sync_single(hwdev, dma_addr, size, dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800696 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
699void
John W. Linville8270f3f2005-09-29 14:43:32 -0700700swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
701 size_t size, int dir)
702{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700703 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700704}
705
706void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
708 size_t size, int dir)
709{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700710 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713/*
John W. Linville878a97c2005-09-29 14:44:23 -0700714 * Same as above, but for a sub-range of the mapping.
715 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800716static void
John W. Linville878a97c2005-09-29 14:44:23 -0700717swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700718 unsigned long offset, size_t size,
719 int dir, int target)
John W. Linville878a97c2005-09-29 14:44:23 -0700720{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800721 char *dma_addr = swiotlb_bus_to_virt(dev_addr) + offset;
John W. Linville878a97c2005-09-29 14:44:23 -0700722
Eric Sesterhenn34814542006-03-24 18:47:11 +0100723 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900724 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700725 sync_single(hwdev, dma_addr, size, dir, target);
John W. Linville878a97c2005-09-29 14:44:23 -0700726 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800727 dma_mark_clean(dma_addr, size);
John W. Linville878a97c2005-09-29 14:44:23 -0700728}
729
730void
731swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
732 unsigned long offset, size_t size, int dir)
733{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700734 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
735 SYNC_FOR_CPU);
John W. Linville878a97c2005-09-29 14:44:23 -0700736}
737
738void
739swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
740 unsigned long offset, size_t size, int dir)
741{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700742 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
743 SYNC_FOR_DEVICE);
John W. Linville878a97c2005-09-29 14:44:23 -0700744}
745
Arthur Kepner309df0c2008-04-29 01:00:32 -0700746void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
747 struct dma_attrs *);
John W. Linville878a97c2005-09-29 14:44:23 -0700748/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 * Map a set of buffers described by scatterlist in streaming mode for DMA.
750 * This is the scatter-gather version of the above swiotlb_map_single
751 * interface. Here the scatter gather list elements are each tagged with the
752 * appropriate dma address and length. They are obtained via
753 * sg_dma_{address,length}(SG).
754 *
755 * NOTE: An implementation may be able to use a smaller number of
756 * DMA address/length pairs than there are SG table elements.
757 * (for example via virtual mapping capabilities)
758 * The routine returns the number of addr/length pairs actually
759 * used, at most nents.
760 *
761 * Device ownership issues as mentioned above for swiotlb_map_single are the
762 * same here.
763 */
764int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700765swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
766 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200768 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 int i;
770
Eric Sesterhenn34814542006-03-24 18:47:11 +0100771 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jens Axboedbfd49f2007-05-11 14:56:18 +0200773 for_each_sg(sgl, sg, nelems, i) {
Becky Brucebc40ac62008-12-22 10:26:08 -0800774 void *addr = sg_virt(sg);
775 dma_addr_t dev_addr = swiotlb_virt_to_bus(hwdev, addr);
776
777 if (range_needs_mapping(addr, sg->length) ||
FUJITA Tomonori27979822008-09-10 01:06:49 +0900778 address_needs_mapping(hwdev, dev_addr, sg->length)) {
Becky Brucebc40ac62008-12-22 10:26:08 -0800779 void *map = map_single(hwdev, sg_phys(sg),
780 sg->length, dir);
Andi Kleen7e870232005-12-20 14:45:19 +0100781 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* Don't panic here, we expect map_sg users
783 to do proper error handling. */
784 swiotlb_full(hwdev, sg->length, dir, 0);
Arthur Kepner309df0c2008-04-29 01:00:32 -0700785 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
786 attrs);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200787 sgl[0].dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 0;
789 }
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800790 sg->dma_address = swiotlb_virt_to_bus(hwdev, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 } else
792 sg->dma_address = dev_addr;
793 sg->dma_length = sg->length;
794 }
795 return nelems;
796}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700797EXPORT_SYMBOL(swiotlb_map_sg_attrs);
798
799int
800swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
801 int dir)
802{
803 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
804}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806/*
807 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
808 * concerning calls here are the same as for swiotlb_unmap_single() above.
809 */
810void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700811swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
812 int nelems, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200814 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 int i;
816
Eric Sesterhenn34814542006-03-24 18:47:11 +0100817 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Jens Axboedbfd49f2007-05-11 14:56:18 +0200819 for_each_sg(sgl, sg, nelems, i) {
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800820 if (sg->dma_address != swiotlb_sg_to_bus(hwdev, sg))
Ian Campbelle08e1f72008-12-16 12:17:30 -0800821 unmap_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
Jan Beulich93fbff62007-02-05 18:49:45 -0800822 sg->dma_length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 else if (dir == DMA_FROM_DEVICE)
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800824 dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700827EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
828
829void
830swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
831 int dir)
832{
833 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
834}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836/*
837 * Make physical memory consistent for a set of streaming mode DMA translations
838 * after a transfer.
839 *
840 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
841 * and usage.
842 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800843static void
Jens Axboedbfd49f2007-05-11 14:56:18 +0200844swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700845 int nelems, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200847 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int i;
849
Eric Sesterhenn34814542006-03-24 18:47:11 +0100850 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Jens Axboedbfd49f2007-05-11 14:56:18 +0200852 for_each_sg(sgl, sg, nelems, i) {
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800853 if (sg->dma_address != swiotlb_sg_to_bus(hwdev, sg))
Ian Campbelle08e1f72008-12-16 12:17:30 -0800854 sync_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
John W. Linvillede69e0f2005-09-29 14:44:57 -0700855 sg->dma_length, dir, target);
Jan Beulichcde14bb2007-02-05 18:46:40 -0800856 else if (dir == DMA_FROM_DEVICE)
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800857 dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861void
John W. Linville8270f3f2005-09-29 14:43:32 -0700862swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
863 int nelems, int dir)
864{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700865 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700866}
867
868void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
870 int nelems, int dir)
871{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700872 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700876swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800878 return (dma_addr == swiotlb_virt_to_bus(hwdev, io_tlb_overflow_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700882 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700884 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * this function.
886 */
887int
Jan Beulich563aaf02007-02-05 18:51:25 -0800888swiotlb_dma_supported(struct device *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800890 return swiotlb_virt_to_bus(hwdev, io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893EXPORT_SYMBOL(swiotlb_map_single);
894EXPORT_SYMBOL(swiotlb_unmap_single);
895EXPORT_SYMBOL(swiotlb_map_sg);
896EXPORT_SYMBOL(swiotlb_unmap_sg);
897EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
898EXPORT_SYMBOL(swiotlb_sync_single_for_device);
John W. Linville878a97c2005-09-29 14:44:23 -0700899EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
900EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
902EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
903EXPORT_SYMBOL(swiotlb_dma_mapping_error);
Tony Luck25667d62007-03-06 13:31:45 -0800904EXPORT_SYMBOL(swiotlb_alloc_coherent);
905EXPORT_SYMBOL(swiotlb_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906EXPORT_SYMBOL(swiotlb_dma_supported);