blob: 3657da8ebbc3694b8b7fb39ca40662d38ab4e292 [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 */
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -080092static struct swiotlb_phys_addr {
93 struct page *page;
94 unsigned int offset;
95} *io_tlb_orig_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*
98 * Protect the above data structures in the map and unmap calls
99 */
100static DEFINE_SPINLOCK(io_tlb_lock);
101
102static int __init
103setup_io_tlb_npages(char *str)
104{
105 if (isdigit(*str)) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700106 io_tlb_nslabs = simple_strtoul(str, &str, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* avoid tail segment of size < IO_TLB_SEGSIZE */
108 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
109 }
110 if (*str == ',')
111 ++str;
112 if (!strcmp(str, "force"))
113 swiotlb_force = 1;
114 return 1;
115}
116__setup("swiotlb=", setup_io_tlb_npages);
117/* make io_tlb_overflow tunable too? */
118
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800119void * __weak swiotlb_alloc_boot(size_t size, unsigned long nslabs)
120{
121 return alloc_bootmem_low_pages(size);
122}
123
124void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs)
125{
126 return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order);
127}
128
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800129dma_addr_t __weak swiotlb_phys_to_bus(struct device *hwdev, phys_addr_t paddr)
Ian Campbelle08e1f72008-12-16 12:17:30 -0800130{
131 return paddr;
132}
133
134phys_addr_t __weak swiotlb_bus_to_phys(dma_addr_t baddr)
135{
136 return baddr;
137}
138
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800139static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
140 volatile void *address)
Ian Campbelle08e1f72008-12-16 12:17:30 -0800141{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800142 return swiotlb_phys_to_bus(hwdev, virt_to_phys(address));
Ian Campbelle08e1f72008-12-16 12:17:30 -0800143}
144
145static void *swiotlb_bus_to_virt(dma_addr_t address)
146{
147 return phys_to_virt(swiotlb_bus_to_phys(address));
148}
149
Ian Campbellb81ea272008-12-16 12:17:31 -0800150int __weak swiotlb_arch_range_needs_mapping(void *ptr, size_t size)
151{
152 return 0;
153}
154
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800155static dma_addr_t swiotlb_sg_to_bus(struct device *hwdev, struct scatterlist *sg)
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800156{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800157 return swiotlb_phys_to_bus(hwdev, page_to_phys(sg_page(sg)) + sg->offset);
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800158}
159
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800160static void swiotlb_print_info(unsigned long bytes)
161{
162 phys_addr_t pstart, pend;
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800163
164 pstart = virt_to_phys(io_tlb_start);
165 pend = virt_to_phys(io_tlb_end);
166
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800167 printk(KERN_INFO "Placing %luMB software IO TLB between %p - %p\n",
168 bytes >> 20, io_tlb_start, io_tlb_end);
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800169 printk(KERN_INFO "software IO TLB at phys %#llx - %#llx\n",
170 (unsigned long long)pstart,
171 (unsigned long long)pend);
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
175 * Statically reserve bounce buffer space and initialize bounce buffer data
Tony Luck17e5ad62005-09-29 15:52:13 -0700176 * structures for the software IO TLB used to implement the DMA API.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800178void __init
179swiotlb_init_with_default_size(size_t default_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Jan Beulich563aaf02007-02-05 18:51:25 -0800181 unsigned long i, bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (!io_tlb_nslabs) {
Alex Williamsone8579e72005-08-04 13:06:00 -0700184 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
186 }
187
Jan Beulich563aaf02007-02-05 18:51:25 -0800188 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /*
191 * Get IO TLB memory from the low pages
192 */
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800193 io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (!io_tlb_start)
195 panic("Cannot allocate SWIOTLB buffer");
Jan Beulich563aaf02007-02-05 18:51:25 -0800196 io_tlb_end = io_tlb_start + bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /*
199 * Allocate and initialize the free list array. This array is used
200 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
201 * between io_tlb_start and io_tlb_end.
202 */
203 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
Tony Luck25667d62007-03-06 13:31:45 -0800204 for (i = 0; i < io_tlb_nslabs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
206 io_tlb_index = 0;
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800207 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(struct swiotlb_phys_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /*
210 * Get the overflow emergency buffer
211 */
212 io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
Jan Beulich563aaf02007-02-05 18:51:25 -0800213 if (!io_tlb_overflow_buffer)
214 panic("Cannot allocate SWIOTLB overflow buffer!\n");
215
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800216 swiotlb_print_info(bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Jan Beulich563aaf02007-02-05 18:51:25 -0800219void __init
220swiotlb_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Tony Luck25667d62007-03-06 13:31:45 -0800222 swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Alex Williamson0b9afed2005-09-06 11:20:49 -0600225/*
226 * Systems with larger DMA zones (those that don't support ISA) can
227 * initialize the swiotlb later using the slab allocator if needed.
228 * This should be just like above, but with some error catching.
229 */
230int
Jan Beulich563aaf02007-02-05 18:51:25 -0800231swiotlb_late_init_with_default_size(size_t default_size)
Alex Williamson0b9afed2005-09-06 11:20:49 -0600232{
Jan Beulich563aaf02007-02-05 18:51:25 -0800233 unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600234 unsigned int order;
235
236 if (!io_tlb_nslabs) {
237 io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
238 io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
239 }
240
241 /*
242 * Get IO TLB memory from the low pages
243 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800244 order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600245 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800246 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600247
248 while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
Jeremy Fitzhardinge8c5df162008-12-16 12:17:26 -0800249 io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600250 if (io_tlb_start)
251 break;
252 order--;
253 }
254
255 if (!io_tlb_start)
256 goto cleanup1;
257
Jan Beulich563aaf02007-02-05 18:51:25 -0800258 if (order != get_order(bytes)) {
Alex Williamson0b9afed2005-09-06 11:20:49 -0600259 printk(KERN_WARNING "Warning: only able to allocate %ld MB "
260 "for software IO TLB\n", (PAGE_SIZE << order) >> 20);
261 io_tlb_nslabs = SLABS_PER_PAGE << order;
Jan Beulich563aaf02007-02-05 18:51:25 -0800262 bytes = io_tlb_nslabs << IO_TLB_SHIFT;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600263 }
Jan Beulich563aaf02007-02-05 18:51:25 -0800264 io_tlb_end = io_tlb_start + bytes;
265 memset(io_tlb_start, 0, bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600266
267 /*
268 * Allocate and initialize the free list array. This array is used
269 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
270 * between io_tlb_start and io_tlb_end.
271 */
272 io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
273 get_order(io_tlb_nslabs * sizeof(int)));
274 if (!io_tlb_list)
275 goto cleanup2;
276
277 for (i = 0; i < io_tlb_nslabs; i++)
278 io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
279 io_tlb_index = 0;
280
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800281 io_tlb_orig_addr = (struct swiotlb_phys_addr *)__get_free_pages(GFP_KERNEL,
282 get_order(io_tlb_nslabs * sizeof(struct swiotlb_phys_addr)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600283 if (!io_tlb_orig_addr)
284 goto cleanup3;
285
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800286 memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(struct swiotlb_phys_addr));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600287
288 /*
289 * Get the overflow emergency buffer
290 */
291 io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
292 get_order(io_tlb_overflow));
293 if (!io_tlb_overflow_buffer)
294 goto cleanup4;
295
Ian Campbell2e5b2b82008-12-16 12:17:34 -0800296 swiotlb_print_info(bytes);
Alex Williamson0b9afed2005-09-06 11:20:49 -0600297
298 return 0;
299
300cleanup4:
Tony Luck25667d62007-03-06 13:31:45 -0800301 free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs *
302 sizeof(char *)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600303 io_tlb_orig_addr = NULL;
304cleanup3:
Tony Luck25667d62007-03-06 13:31:45 -0800305 free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
306 sizeof(int)));
Alex Williamson0b9afed2005-09-06 11:20:49 -0600307 io_tlb_list = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600308cleanup2:
Jan Beulich563aaf02007-02-05 18:51:25 -0800309 io_tlb_end = NULL;
Alex Williamson0b9afed2005-09-06 11:20:49 -0600310 free_pages((unsigned long)io_tlb_start, order);
311 io_tlb_start = NULL;
312cleanup1:
313 io_tlb_nslabs = req_nslabs;
314 return -ENOMEM;
315}
316
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800317static int
FUJITA Tomonori27979822008-09-10 01:06:49 +0900318address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
FUJITA Tomonori07a2c012008-09-19 02:02:05 +0900320 return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Ian Campbellb81ea272008-12-16 12:17:31 -0800323static inline int range_needs_mapping(void *ptr, size_t size)
324{
325 return swiotlb_force || swiotlb_arch_range_needs_mapping(ptr, size);
326}
327
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900328static int is_swiotlb_buffer(char *addr)
329{
330 return addr >= io_tlb_start && addr < io_tlb_end;
331}
332
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800333static struct swiotlb_phys_addr swiotlb_bus_to_phys_addr(char *dma_addr)
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800334{
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800335 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
336 struct swiotlb_phys_addr buffer = io_tlb_orig_addr[index];
337 buffer.offset += (long)dma_addr & ((1 << IO_TLB_SHIFT) - 1);
338 buffer.page += buffer.offset >> PAGE_SHIFT;
339 buffer.offset &= PAGE_SIZE - 1;
340 return buffer;
341}
342
343static void
344__sync_single(struct swiotlb_phys_addr buffer, char *dma_addr, size_t size, int dir)
345{
346 if (PageHighMem(buffer.page)) {
347 size_t len, bytes;
348 char *dev, *host, *kmp;
349
350 len = size;
351 while (len != 0) {
352 unsigned long flags;
353
354 bytes = len;
355 if ((bytes + buffer.offset) > PAGE_SIZE)
356 bytes = PAGE_SIZE - buffer.offset;
357 local_irq_save(flags); /* protects KM_BOUNCE_READ */
358 kmp = kmap_atomic(buffer.page, KM_BOUNCE_READ);
359 dev = dma_addr + size - len;
360 host = kmp + buffer.offset;
361 if (dir == DMA_FROM_DEVICE)
362 memcpy(host, dev, bytes);
363 else
364 memcpy(dev, host, bytes);
365 kunmap_atomic(kmp, KM_BOUNCE_READ);
366 local_irq_restore(flags);
367 len -= bytes;
368 buffer.page++;
369 buffer.offset = 0;
370 }
371 } else {
372 void *v = page_address(buffer.page) + buffer.offset;
373
374 if (dir == DMA_TO_DEVICE)
375 memcpy(dma_addr, v, size);
376 else
377 memcpy(v, dma_addr, size);
378 }
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
382 * Allocates bounce buffer and returns its kernel virtual address.
383 */
384static void *
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800385map_single(struct device *hwdev, struct swiotlb_phys_addr buffer, size_t size, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 unsigned long flags;
388 char *dma_addr;
389 unsigned int nslots, stride, index, wrap;
390 int i;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800391 unsigned long start_dma_addr;
392 unsigned long mask;
393 unsigned long offset_slots;
394 unsigned long max_slots;
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800395 struct swiotlb_phys_addr slot_buf;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800396
397 mask = dma_get_seg_boundary(hwdev);
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800398 start_dma_addr = swiotlb_virt_to_bus(hwdev, io_tlb_start) & mask;
FUJITA Tomonori681cc5c2008-02-04 22:28:16 -0800399
400 offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
Ian Campbella5ddde42008-12-16 12:17:29 -0800401
402 /*
403 * Carefully handle integer overflow which can occur when mask == ~0UL.
404 */
Jan Beulichb15a3892008-03-13 09:13:30 +0000405 max_slots = mask + 1
406 ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
407 : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
410 * For mappings greater than a page, we limit the stride (and
411 * hence alignment) to a page size.
412 */
413 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
414 if (size > PAGE_SIZE)
415 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
416 else
417 stride = 1;
418
Eric Sesterhenn34814542006-03-24 18:47:11 +0100419 BUG_ON(!nslots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /*
422 * Find suitable number of IO TLB entries size that will fit this
423 * request and allocate a buffer from that IO TLB pool.
424 */
425 spin_lock_irqsave(&io_tlb_lock, flags);
Andrew Mortona7133a12008-04-29 00:59:36 -0700426 index = ALIGN(io_tlb_index, stride);
427 if (index >= io_tlb_nslabs)
428 index = 0;
429 wrap = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Andrew Mortona7133a12008-04-29 00:59:36 -0700431 do {
FUJITA Tomonoria8522502008-04-29 00:59:36 -0700432 while (iommu_is_span_boundary(index, nslots, offset_slots,
433 max_slots)) {
Jan Beulichb15a3892008-03-13 09:13:30 +0000434 index += stride;
435 if (index >= io_tlb_nslabs)
436 index = 0;
Andrew Mortona7133a12008-04-29 00:59:36 -0700437 if (index == wrap)
438 goto not_found;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Andrew Mortona7133a12008-04-29 00:59:36 -0700441 /*
442 * If we find a slot that indicates we have 'nslots' number of
443 * contiguous buffers, we allocate the buffers from that slot
444 * and mark the entries as '0' indicating unavailable.
445 */
446 if (io_tlb_list[index] >= nslots) {
447 int count = 0;
448
449 for (i = index; i < (int) (index + nslots); i++)
450 io_tlb_list[i] = 0;
451 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
452 io_tlb_list[i] = ++count;
453 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
454
455 /*
456 * Update the indices to avoid searching in the next
457 * round.
458 */
459 io_tlb_index = ((index + nslots) < io_tlb_nslabs
460 ? (index + nslots) : 0);
461
462 goto found;
463 }
464 index += stride;
465 if (index >= io_tlb_nslabs)
466 index = 0;
467 } while (index != wrap);
468
469not_found:
470 spin_unlock_irqrestore(&io_tlb_lock, flags);
471 return NULL;
472found:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 spin_unlock_irqrestore(&io_tlb_lock, flags);
474
475 /*
476 * Save away the mapping from the original address to the DMA address.
477 * This is needed when we sync the memory. Then we sync the buffer if
478 * needed.
479 */
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800480 slot_buf = buffer;
481 for (i = 0; i < nslots; i++) {
482 slot_buf.page += slot_buf.offset >> PAGE_SHIFT;
483 slot_buf.offset &= PAGE_SIZE - 1;
484 io_tlb_orig_addr[index+i] = slot_buf;
485 slot_buf.offset += 1 << IO_TLB_SHIFT;
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800488 __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 return dma_addr;
491}
492
493/*
494 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
495 */
496static void
497unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir)
498{
499 unsigned long flags;
500 int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
501 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800502 struct swiotlb_phys_addr buffer = swiotlb_bus_to_phys_addr(dma_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /*
505 * First, sync the memory before unmapping the entry
506 */
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800507 if ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /*
509 * bounce... copy the data back into the original buffer * and
510 * delete the bounce buffer.
511 */
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800512 __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /*
515 * Return the buffer to the free list by setting the corresponding
516 * entries to indicate the number of contigous entries available.
517 * While returning the entries to the free list, we merge the entries
518 * with slots below and above the pool being returned.
519 */
520 spin_lock_irqsave(&io_tlb_lock, flags);
521 {
522 count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
523 io_tlb_list[index + nslots] : 0);
524 /*
525 * Step 1: return the slots to the free list, merging the
526 * slots with superceeding slots
527 */
528 for (i = index + nslots - 1; i >= index; i--)
529 io_tlb_list[i] = ++count;
530 /*
531 * Step 2: merge the returned slots with the preceding slots,
532 * if available (non zero)
533 */
534 for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
535 io_tlb_list[i] = ++count;
536 }
537 spin_unlock_irqrestore(&io_tlb_lock, flags);
538}
539
540static void
John W. Linvillede69e0f2005-09-29 14:44:57 -0700541sync_single(struct device *hwdev, char *dma_addr, size_t size,
542 int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800544 struct swiotlb_phys_addr buffer = swiotlb_bus_to_phys_addr(dma_addr);
Keir Fraserdf336d12007-07-21 04:37:24 -0700545
John W. Linvillede69e0f2005-09-29 14:44:57 -0700546 switch (target) {
547 case SYNC_FOR_CPU:
548 if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800549 __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100550 else
551 BUG_ON(dir != DMA_TO_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700552 break;
553 case SYNC_FOR_DEVICE:
554 if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
Jeremy Fitzhardinge1b548f62008-12-16 12:17:32 -0800555 __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE);
Eric Sesterhenn34814542006-03-24 18:47:11 +0100556 else
557 BUG_ON(dir != DMA_FROM_DEVICE);
John W. Linvillede69e0f2005-09-29 14:44:57 -0700558 break;
559 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 BUG();
John W. Linvillede69e0f2005-09-29 14:44:57 -0700561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564void *
565swiotlb_alloc_coherent(struct device *hwdev, size_t size,
Al Viro06a54492005-10-21 03:21:03 -0400566 dma_addr_t *dma_handle, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Jan Beulich563aaf02007-02-05 18:51:25 -0800568 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 void *ret;
570 int order = get_order(size);
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900571 u64 dma_mask = DMA_32BIT_MASK;
572
573 if (hwdev && hwdev->coherent_dma_mask)
574 dma_mask = hwdev->coherent_dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Tony Luck25667d62007-03-06 13:31:45 -0800576 ret = (void *)__get_free_pages(flags, order);
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800577 if (ret &&
578 !is_buffer_dma_capable(dma_mask, swiotlb_virt_to_bus(hwdev, ret),
579 size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /*
581 * The allocated memory isn't reachable by the device.
582 * Fall back on swiotlb_map_single().
583 */
584 free_pages((unsigned long) ret, order);
585 ret = NULL;
586 }
587 if (!ret) {
588 /*
589 * We are either out of memory or the device can't DMA
590 * to GFP_DMA memory; fall back on
591 * swiotlb_map_single(), which will grab memory from
592 * the lowest available address range.
593 */
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800594 struct swiotlb_phys_addr buffer;
595 buffer.page = virt_to_page(NULL);
596 buffer.offset = 0;
597 ret = map_single(hwdev, buffer, size, DMA_FROM_DEVICE);
FUJITA Tomonori9dfda122008-09-08 18:53:48 +0900598 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 memset(ret, 0, size);
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800603 dev_addr = swiotlb_virt_to_bus(hwdev, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* Confirm address can be DMA'd by device */
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900606 if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) {
Jan Beulich563aaf02007-02-05 18:51:25 -0800607 printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n",
FUJITA Tomonori1e74f302008-11-17 16:24:34 +0900608 (unsigned long long)dma_mask,
Jan Beulich563aaf02007-02-05 18:51:25 -0800609 (unsigned long long)dev_addr);
FUJITA Tomonoria2b89b52008-10-23 18:42:03 +0900610
611 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
612 unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
613 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 *dma_handle = dev_addr;
616 return ret;
617}
618
619void
620swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
621 dma_addr_t dma_handle)
622{
David Brownellaa248862007-08-10 13:10:27 -0700623 WARN_ON(irqs_disabled());
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900624 if (!is_swiotlb_buffer(vaddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 free_pages((unsigned long) vaddr, get_order(size));
626 else
627 /* DMA_TO_DEVICE to avoid memcpy in unmap_single */
FUJITA Tomonori21f6c4d2008-09-08 18:53:49 +0900628 unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631static void
632swiotlb_full(struct device *dev, size_t size, int dir, int do_panic)
633{
634 /*
635 * Ran out of IOMMU space for this operation. This is very bad.
636 * Unfortunately the drivers cannot handle this operation properly.
Tony Luck17e5ad62005-09-29 15:52:13 -0700637 * unless they check for dma_mapping_error (most don't)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * When the mapping is small enough return a static buffer to limit
639 * the damage, or panic when the transfer is too big.
640 */
Jan Beulich563aaf02007-02-05 18:51:25 -0800641 printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 "device %s\n", size, dev ? dev->bus_id : "?");
643
644 if (size > io_tlb_overflow && do_panic) {
Tony Luck17e5ad62005-09-29 15:52:13 -0700645 if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)
646 panic("DMA: Memory would be corrupted\n");
647 if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
648 panic("DMA: Random memory would be DMAed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650}
651
652/*
653 * Map a single buffer of the indicated size for DMA in streaming mode. The
Tony Luck17e5ad62005-09-29 15:52:13 -0700654 * physical address to use is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *
656 * Once the device is given the dma address, the device owns this memory until
657 * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
658 */
659dma_addr_t
Arthur Kepner309df0c2008-04-29 01:00:32 -0700660swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size,
661 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800663 dma_addr_t dev_addr = swiotlb_virt_to_bus(hwdev, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 void *map;
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800665 struct swiotlb_phys_addr buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Eric Sesterhenn34814542006-03-24 18:47:11 +0100667 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
669 * If the pointer passed in happens to be in the device's DMA window,
670 * we can safely return the device addr and not worry about bounce
671 * buffering it.
672 */
Ian Campbellb81ea272008-12-16 12:17:31 -0800673 if (!address_needs_mapping(hwdev, dev_addr, size) &&
674 !range_needs_mapping(ptr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return dev_addr;
676
677 /*
678 * Oh well, have to allocate and map a bounce buffer.
679 */
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800680 buffer.page = virt_to_page(ptr);
681 buffer.offset = (unsigned long)ptr & ~PAGE_MASK;
682 map = map_single(hwdev, buffer, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (!map) {
684 swiotlb_full(hwdev, size, dir, 1);
685 map = io_tlb_overflow_buffer;
686 }
687
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800688 dev_addr = swiotlb_virt_to_bus(hwdev, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /*
691 * Ensure that the address returned is DMA'ble
692 */
FUJITA Tomonori27979822008-09-10 01:06:49 +0900693 if (address_needs_mapping(hwdev, dev_addr, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 panic("map_single: bounce buffer is not DMA'ble");
695
696 return dev_addr;
697}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700698EXPORT_SYMBOL(swiotlb_map_single_attrs);
699
700dma_addr_t
701swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
702{
703 return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL);
704}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * Unmap a single streaming mode DMA translation. The dma_addr and size must
708 * match what was provided for in a previous swiotlb_map_single call. All
709 * other usages are undefined.
710 *
711 * After this call, reads by the cpu to the buffer are guaranteed to see
712 * whatever the device wrote there.
713 */
714void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700715swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr,
716 size_t size, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800718 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Eric Sesterhenn34814542006-03-24 18:47:11 +0100720 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900721 if (is_swiotlb_buffer(dma_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unmap_single(hwdev, dma_addr, size, dir);
723 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800724 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700726EXPORT_SYMBOL(swiotlb_unmap_single_attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Arthur Kepner309df0c2008-04-29 01:00:32 -0700728void
729swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
730 int dir)
731{
732 return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL);
733}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/*
735 * Make physical memory consistent for a single streaming mode DMA translation
736 * after a transfer.
737 *
738 * If you perform a swiotlb_map_single() but wish to interrogate the buffer
Tony Luck17e5ad62005-09-29 15:52:13 -0700739 * using the cpu, yet do not wish to teardown the dma mapping, you must
740 * call this function before doing so. At the next point you give the dma
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * address back to the card, you must first perform a
742 * swiotlb_dma_sync_for_device, and then the device again owns the buffer
743 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800744static void
John W. Linville8270f3f2005-09-29 14:43:32 -0700745swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700746 size_t size, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800748 char *dma_addr = swiotlb_bus_to_virt(dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Eric Sesterhenn34814542006-03-24 18:47:11 +0100750 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900751 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700752 sync_single(hwdev, dma_addr, size, dir, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800754 dma_mark_clean(dma_addr, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757void
John W. Linville8270f3f2005-09-29 14:43:32 -0700758swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
759 size_t size, int dir)
760{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700761 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700762}
763
764void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
766 size_t size, int dir)
767{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700768 swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/*
John W. Linville878a97c2005-09-29 14:44:23 -0700772 * Same as above, but for a sub-range of the mapping.
773 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800774static void
John W. Linville878a97c2005-09-29 14:44:23 -0700775swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700776 unsigned long offset, size_t size,
777 int dir, int target)
John W. Linville878a97c2005-09-29 14:44:23 -0700778{
Ian Campbelle08e1f72008-12-16 12:17:30 -0800779 char *dma_addr = swiotlb_bus_to_virt(dev_addr) + offset;
John W. Linville878a97c2005-09-29 14:44:23 -0700780
Eric Sesterhenn34814542006-03-24 18:47:11 +0100781 BUG_ON(dir == DMA_NONE);
FUJITA Tomonori640aebf2008-09-08 18:53:50 +0900782 if (is_swiotlb_buffer(dma_addr))
John W. Linvillede69e0f2005-09-29 14:44:57 -0700783 sync_single(hwdev, dma_addr, size, dir, target);
John W. Linville878a97c2005-09-29 14:44:23 -0700784 else if (dir == DMA_FROM_DEVICE)
Jan Beulichcde14bb2007-02-05 18:46:40 -0800785 dma_mark_clean(dma_addr, size);
John W. Linville878a97c2005-09-29 14:44:23 -0700786}
787
788void
789swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
790 unsigned long offset, size_t size, int dir)
791{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700792 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
793 SYNC_FOR_CPU);
John W. Linville878a97c2005-09-29 14:44:23 -0700794}
795
796void
797swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr,
798 unsigned long offset, size_t size, int dir)
799{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700800 swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir,
801 SYNC_FOR_DEVICE);
John W. Linville878a97c2005-09-29 14:44:23 -0700802}
803
Arthur Kepner309df0c2008-04-29 01:00:32 -0700804void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int,
805 struct dma_attrs *);
John W. Linville878a97c2005-09-29 14:44:23 -0700806/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * Map a set of buffers described by scatterlist in streaming mode for DMA.
808 * This is the scatter-gather version of the above swiotlb_map_single
809 * interface. Here the scatter gather list elements are each tagged with the
810 * appropriate dma address and length. They are obtained via
811 * sg_dma_{address,length}(SG).
812 *
813 * NOTE: An implementation may be able to use a smaller number of
814 * DMA address/length pairs than there are SG table elements.
815 * (for example via virtual mapping capabilities)
816 * The routine returns the number of addr/length pairs actually
817 * used, at most nents.
818 *
819 * Device ownership issues as mentioned above for swiotlb_map_single are the
820 * same here.
821 */
822int
Arthur Kepner309df0c2008-04-29 01:00:32 -0700823swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
824 int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200826 struct scatterlist *sg;
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800827 struct swiotlb_phys_addr buffer;
Jan Beulich563aaf02007-02-05 18:51:25 -0800828 dma_addr_t dev_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int i;
830
Eric Sesterhenn34814542006-03-24 18:47:11 +0100831 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Jens Axboedbfd49f2007-05-11 14:56:18 +0200833 for_each_sg(sgl, sg, nelems, i) {
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800834 dev_addr = swiotlb_sg_to_bus(hwdev, sg);
Ian Campbellb81ea272008-12-16 12:17:31 -0800835 if (range_needs_mapping(sg_virt(sg), sg->length) ||
FUJITA Tomonori27979822008-09-10 01:06:49 +0900836 address_needs_mapping(hwdev, dev_addr, sg->length)) {
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800837 void *map;
838 buffer.page = sg_page(sg);
839 buffer.offset = sg->offset;
840 map = map_single(hwdev, buffer, sg->length, dir);
Andi Kleen7e870232005-12-20 14:45:19 +0100841 if (!map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* Don't panic here, we expect map_sg users
843 to do proper error handling. */
844 swiotlb_full(hwdev, sg->length, dir, 0);
Arthur Kepner309df0c2008-04-29 01:00:32 -0700845 swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
846 attrs);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200847 sgl[0].dma_length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return 0;
849 }
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800850 sg->dma_address = swiotlb_virt_to_bus(hwdev, map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 } else
852 sg->dma_address = dev_addr;
853 sg->dma_length = sg->length;
854 }
855 return nelems;
856}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700857EXPORT_SYMBOL(swiotlb_map_sg_attrs);
858
859int
860swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
861 int dir)
862{
863 return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
864}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866/*
867 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
868 * concerning calls here are the same as for swiotlb_unmap_single() above.
869 */
870void
Arthur Kepner309df0c2008-04-29 01:00:32 -0700871swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
872 int nelems, int dir, struct dma_attrs *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200874 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int i;
876
Eric Sesterhenn34814542006-03-24 18:47:11 +0100877 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Jens Axboedbfd49f2007-05-11 14:56:18 +0200879 for_each_sg(sgl, sg, nelems, i) {
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800880 if (sg->dma_address != swiotlb_sg_to_bus(hwdev, sg))
Ian Campbelle08e1f72008-12-16 12:17:30 -0800881 unmap_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
Jan Beulich93fbff62007-02-05 18:49:45 -0800882 sg->dma_length, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 else if (dir == DMA_FROM_DEVICE)
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800884 dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
Arthur Kepner309df0c2008-04-29 01:00:32 -0700887EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
888
889void
890swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
891 int dir)
892{
893 return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
894}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896/*
897 * Make physical memory consistent for a set of streaming mode DMA translations
898 * after a transfer.
899 *
900 * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
901 * and usage.
902 */
Andrew Mortonbe6b0262007-02-12 00:52:17 -0800903static void
Jens Axboedbfd49f2007-05-11 14:56:18 +0200904swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
John W. Linvillede69e0f2005-09-29 14:44:57 -0700905 int nelems, int dir, int target)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Jens Axboedbfd49f2007-05-11 14:56:18 +0200907 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 int i;
909
Eric Sesterhenn34814542006-03-24 18:47:11 +0100910 BUG_ON(dir == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Jens Axboedbfd49f2007-05-11 14:56:18 +0200912 for_each_sg(sgl, sg, nelems, i) {
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800913 if (sg->dma_address != swiotlb_sg_to_bus(hwdev, sg))
Ian Campbelle08e1f72008-12-16 12:17:30 -0800914 sync_single(hwdev, swiotlb_bus_to_virt(sg->dma_address),
John W. Linvillede69e0f2005-09-29 14:44:57 -0700915 sg->dma_length, dir, target);
Jan Beulichcde14bb2007-02-05 18:46:40 -0800916 else if (dir == DMA_FROM_DEVICE)
Jeremy Fitzhardingeef9b1892008-12-16 12:17:33 -0800917 dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length);
Jens Axboedbfd49f2007-05-11 14:56:18 +0200918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921void
John W. Linville8270f3f2005-09-29 14:43:32 -0700922swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
923 int nelems, int dir)
924{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700925 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
John W. Linville8270f3f2005-09-29 14:43:32 -0700926}
927
928void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
930 int nelems, int dir)
931{
John W. Linvillede69e0f2005-09-29 14:44:57 -0700932 swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935int
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700936swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800938 return (dma_addr == swiotlb_virt_to_bus(hwdev, io_tlb_overflow_buffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941/*
Tony Luck17e5ad62005-09-29 15:52:13 -0700942 * Return whether the given device DMA address mask can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * properly. For example, if your device can only drive the low 24-bits
Tony Luck17e5ad62005-09-29 15:52:13 -0700944 * during bus mastering, then you would pass 0x00ffffff as the mask to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * this function.
946 */
947int
Jan Beulich563aaf02007-02-05 18:51:25 -0800948swiotlb_dma_supported(struct device *hwdev, u64 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Jeremy Fitzhardinge70a7d3c2008-12-22 10:26:05 -0800950 return swiotlb_virt_to_bus(hwdev, io_tlb_end - 1) <= mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953EXPORT_SYMBOL(swiotlb_map_single);
954EXPORT_SYMBOL(swiotlb_unmap_single);
955EXPORT_SYMBOL(swiotlb_map_sg);
956EXPORT_SYMBOL(swiotlb_unmap_sg);
957EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
958EXPORT_SYMBOL(swiotlb_sync_single_for_device);
John W. Linville878a97c2005-09-29 14:44:23 -0700959EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu);
960EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
962EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
963EXPORT_SYMBOL(swiotlb_dma_mapping_error);
Tony Luck25667d62007-03-06 13:31:45 -0800964EXPORT_SYMBOL(swiotlb_alloc_coherent);
965EXPORT_SYMBOL(swiotlb_free_coherent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966EXPORT_SYMBOL(swiotlb_dma_supported);