Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Dynamic DMA mapping support. | |
3 | * | |
563aaf06 | 4 | * This implementation is a fallback for platforms that do not support |
1da177e4 LT |
5 | * 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. | |
569c8bf5 JL |
14 | * 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. | |
fb05a379 | 17 | * 08/12/11 beckyb Add highmem support |
1da177e4 LT |
18 | */ |
19 | ||
20 | #include <linux/cache.h> | |
17e5ad6c | 21 | #include <linux/dma-mapping.h> |
1da177e4 | 22 | #include <linux/mm.h> |
8bc3bcc9 | 23 | #include <linux/export.h> |
1da177e4 LT |
24 | #include <linux/spinlock.h> |
25 | #include <linux/string.h> | |
0016fdee | 26 | #include <linux/swiotlb.h> |
fb05a379 | 27 | #include <linux/pfn.h> |
1da177e4 LT |
28 | #include <linux/types.h> |
29 | #include <linux/ctype.h> | |
ef9b1893 | 30 | #include <linux/highmem.h> |
5a0e3ad6 | 31 | #include <linux/gfp.h> |
1da177e4 LT |
32 | |
33 | #include <asm/io.h> | |
1da177e4 | 34 | #include <asm/dma.h> |
17e5ad6c | 35 | #include <asm/scatterlist.h> |
1da177e4 LT |
36 | |
37 | #include <linux/init.h> | |
38 | #include <linux/bootmem.h> | |
a8522509 | 39 | #include <linux/iommu-helper.h> |
1da177e4 LT |
40 | |
41 | #define OFFSET(val,align) ((unsigned long) \ | |
42 | ( (val) & ( (align) - 1))) | |
43 | ||
0b9afede AW |
44 | #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 | ||
1da177e4 LT |
53 | int swiotlb_force; |
54 | ||
55 | /* | |
bfc5501f KRW |
56 | * Used to do a quick range check in swiotlb_tbl_unmap_single and |
57 | * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this | |
1da177e4 LT |
58 | * API. |
59 | */ | |
ff7204a7 | 60 | static phys_addr_t io_tlb_start, io_tlb_end; |
1da177e4 LT |
61 | |
62 | /* | |
b595076a | 63 | * The number of IO TLB blocks (in groups of 64) between io_tlb_start and |
1da177e4 LT |
64 | * io_tlb_end. This is command line adjustable via setup_io_tlb_npages. |
65 | */ | |
66 | static unsigned long io_tlb_nslabs; | |
67 | ||
68 | /* | |
69 | * When the IOMMU overflows we return a fallback buffer. This sets the size. | |
70 | */ | |
71 | static unsigned long io_tlb_overflow = 32*1024; | |
72 | ||
ee3f6ba8 | 73 | static phys_addr_t io_tlb_overflow_buffer; |
1da177e4 LT |
74 | |
75 | /* | |
76 | * This is a free list describing the number of free entries available from | |
77 | * each index | |
78 | */ | |
79 | static unsigned int *io_tlb_list; | |
80 | static unsigned int io_tlb_index; | |
81 | ||
82 | /* | |
83 | * We need to save away the original address corresponding to a mapped entry | |
84 | * for the sync operations. | |
85 | */ | |
bc40ac66 | 86 | static phys_addr_t *io_tlb_orig_addr; |
1da177e4 LT |
87 | |
88 | /* | |
89 | * Protect the above data structures in the map and unmap calls | |
90 | */ | |
91 | static DEFINE_SPINLOCK(io_tlb_lock); | |
92 | ||
5740afdb FT |
93 | static int late_alloc; |
94 | ||
1da177e4 LT |
95 | static int __init |
96 | setup_io_tlb_npages(char *str) | |
97 | { | |
98 | if (isdigit(*str)) { | |
e8579e72 | 99 | io_tlb_nslabs = simple_strtoul(str, &str, 0); |
1da177e4 LT |
100 | /* avoid tail segment of size < IO_TLB_SEGSIZE */ |
101 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); | |
102 | } | |
103 | if (*str == ',') | |
104 | ++str; | |
b18485e7 | 105 | if (!strcmp(str, "force")) |
1da177e4 | 106 | swiotlb_force = 1; |
b18485e7 | 107 | |
c729de8f | 108 | return 0; |
1da177e4 | 109 | } |
c729de8f | 110 | early_param("swiotlb", setup_io_tlb_npages); |
1da177e4 LT |
111 | /* make io_tlb_overflow tunable too? */ |
112 | ||
f21ffe9f | 113 | unsigned long swiotlb_nr_tbl(void) |
5f98ecdb FT |
114 | { |
115 | return io_tlb_nslabs; | |
116 | } | |
f21ffe9f | 117 | EXPORT_SYMBOL_GPL(swiotlb_nr_tbl); |
c729de8f YL |
118 | |
119 | /* default to 64MB */ | |
6fa3eb70 S |
120 | #ifdef CONFIG_MTK_LM_MODE |
121 | #define IO_TLB_DEFAULT_SIZE (SZ_64M) | |
122 | #else | |
123 | #define IO_TLB_DEFAULT_SIZE ((1 << IO_TLB_SHIFT) * IO_TLB_SEGSIZE) | |
124 | #endif // end of CONFIG_MTK_LM_MODE | |
c729de8f YL |
125 | unsigned long swiotlb_size_or_default(void) |
126 | { | |
127 | unsigned long size; | |
128 | ||
129 | size = io_tlb_nslabs << IO_TLB_SHIFT; | |
130 | ||
131 | return size ? size : (IO_TLB_DEFAULT_SIZE); | |
132 | } | |
133 | ||
02ca646e | 134 | /* Note that this doesn't work with highmem page */ |
70a7d3cc JF |
135 | static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev, |
136 | volatile void *address) | |
e08e1f7a | 137 | { |
862d196b | 138 | return phys_to_dma(hwdev, virt_to_phys(address)); |
e08e1f7a IC |
139 | } |
140 | ||
ac2cbab2 YL |
141 | static bool no_iotlb_memory; |
142 | ||
ad32e8cb | 143 | void swiotlb_print_info(void) |
2e5b2b86 | 144 | { |
ad32e8cb | 145 | unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
ff7204a7 | 146 | unsigned char *vstart, *vend; |
2e5b2b86 | 147 | |
ac2cbab2 YL |
148 | if (no_iotlb_memory) { |
149 | pr_warn("software IO TLB: No low mem\n"); | |
150 | return; | |
151 | } | |
152 | ||
ff7204a7 | 153 | vstart = phys_to_virt(io_tlb_start); |
c40dba06 | 154 | vend = phys_to_virt(io_tlb_end); |
2e5b2b86 | 155 | |
6fa3eb70 | 156 | printk(KERN_ALERT"software IO TLB [mem %#010llx-%#010llx] (%luMB) mapped at [%p-%p]\n", |
ff7204a7 | 157 | (unsigned long long)io_tlb_start, |
c40dba06 | 158 | (unsigned long long)io_tlb_end, |
ff7204a7 | 159 | bytes >> 20, vstart, vend - 1); |
2e5b2b86 IC |
160 | } |
161 | ||
ac2cbab2 | 162 | int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) |
1da177e4 | 163 | { |
ee3f6ba8 | 164 | void *v_overflow_buffer; |
563aaf06 | 165 | unsigned long i, bytes; |
1da177e4 | 166 | |
abbceff7 | 167 | bytes = nslabs << IO_TLB_SHIFT; |
1da177e4 | 168 | |
abbceff7 | 169 | io_tlb_nslabs = nslabs; |
ff7204a7 AD |
170 | io_tlb_start = __pa(tlb); |
171 | io_tlb_end = io_tlb_start + bytes; | |
1da177e4 | 172 | |
ee3f6ba8 AD |
173 | /* |
174 | * Get the overflow emergency buffer | |
175 | */ | |
ac2cbab2 YL |
176 | v_overflow_buffer = alloc_bootmem_low_pages_nopanic( |
177 | PAGE_ALIGN(io_tlb_overflow)); | |
ee3f6ba8 | 178 | if (!v_overflow_buffer) |
ac2cbab2 | 179 | return -ENOMEM; |
ee3f6ba8 AD |
180 | |
181 | io_tlb_overflow_buffer = __pa(v_overflow_buffer); | |
182 | ||
1da177e4 LT |
183 | /* |
184 | * Allocate and initialize the free list array. This array is used | |
185 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE | |
186 | * between io_tlb_start and io_tlb_end. | |
187 | */ | |
e79f86b2 | 188 | io_tlb_list = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_nslabs * sizeof(int))); |
25667d67 | 189 | for (i = 0; i < io_tlb_nslabs; i++) |
1da177e4 LT |
190 | io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE); |
191 | io_tlb_index = 0; | |
e79f86b2 | 192 | io_tlb_orig_addr = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t))); |
1da177e4 | 193 | |
ad32e8cb FT |
194 | if (verbose) |
195 | swiotlb_print_info(); | |
ac2cbab2 YL |
196 | |
197 | return 0; | |
1da177e4 LT |
198 | } |
199 | ||
abbceff7 FT |
200 | /* |
201 | * Statically reserve bounce buffer space and initialize bounce buffer data | |
202 | * structures for the software IO TLB used to implement the DMA API. | |
203 | */ | |
ac2cbab2 YL |
204 | void __init |
205 | swiotlb_init(int verbose) | |
abbceff7 | 206 | { |
c729de8f | 207 | size_t default_size = IO_TLB_DEFAULT_SIZE; |
ff7204a7 | 208 | unsigned char *vstart; |
abbceff7 FT |
209 | unsigned long bytes; |
210 | ||
211 | if (!io_tlb_nslabs) { | |
212 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); | |
213 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); | |
214 | } | |
215 | ||
216 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; | |
217 | ||
ac2cbab2 YL |
218 | /* Get IO TLB memory from the low pages */ |
219 | vstart = alloc_bootmem_low_pages_nopanic(PAGE_ALIGN(bytes)); | |
220 | if (vstart && !swiotlb_init_with_tbl(vstart, io_tlb_nslabs, verbose)) | |
221 | return; | |
abbceff7 | 222 | |
ac2cbab2 YL |
223 | if (io_tlb_start) |
224 | free_bootmem(io_tlb_start, | |
225 | PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); | |
226 | pr_warn("Cannot allocate SWIOTLB buffer"); | |
227 | no_iotlb_memory = true; | |
1da177e4 LT |
228 | } |
229 | ||
0b9afede AW |
230 | /* |
231 | * Systems with larger DMA zones (those that don't support ISA) can | |
232 | * initialize the swiotlb later using the slab allocator if needed. | |
233 | * This should be just like above, but with some error catching. | |
234 | */ | |
235 | int | |
563aaf06 | 236 | swiotlb_late_init_with_default_size(size_t default_size) |
0b9afede | 237 | { |
74838b75 | 238 | unsigned long bytes, req_nslabs = io_tlb_nslabs; |
ff7204a7 | 239 | unsigned char *vstart = NULL; |
0b9afede | 240 | unsigned int order; |
74838b75 | 241 | int rc = 0; |
0b9afede AW |
242 | |
243 | if (!io_tlb_nslabs) { | |
244 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); | |
245 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); | |
246 | } | |
247 | ||
248 | /* | |
249 | * Get IO TLB memory from the low pages | |
250 | */ | |
563aaf06 | 251 | order = get_order(io_tlb_nslabs << IO_TLB_SHIFT); |
0b9afede | 252 | io_tlb_nslabs = SLABS_PER_PAGE << order; |
563aaf06 | 253 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
0b9afede AW |
254 | |
255 | while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { | |
ff7204a7 AD |
256 | vstart = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, |
257 | order); | |
258 | if (vstart) | |
0b9afede AW |
259 | break; |
260 | order--; | |
261 | } | |
262 | ||
ff7204a7 | 263 | if (!vstart) { |
74838b75 KRW |
264 | io_tlb_nslabs = req_nslabs; |
265 | return -ENOMEM; | |
266 | } | |
563aaf06 | 267 | if (order != get_order(bytes)) { |
0b9afede AW |
268 | printk(KERN_WARNING "Warning: only able to allocate %ld MB " |
269 | "for software IO TLB\n", (PAGE_SIZE << order) >> 20); | |
270 | io_tlb_nslabs = SLABS_PER_PAGE << order; | |
271 | } | |
ff7204a7 | 272 | rc = swiotlb_late_init_with_tbl(vstart, io_tlb_nslabs); |
74838b75 | 273 | if (rc) |
ff7204a7 | 274 | free_pages((unsigned long)vstart, order); |
74838b75 KRW |
275 | return rc; |
276 | } | |
277 | ||
278 | int | |
279 | swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs) | |
280 | { | |
281 | unsigned long i, bytes; | |
ee3f6ba8 | 282 | unsigned char *v_overflow_buffer; |
74838b75 KRW |
283 | |
284 | bytes = nslabs << IO_TLB_SHIFT; | |
285 | ||
286 | io_tlb_nslabs = nslabs; | |
ff7204a7 AD |
287 | io_tlb_start = virt_to_phys(tlb); |
288 | io_tlb_end = io_tlb_start + bytes; | |
74838b75 | 289 | |
ff7204a7 | 290 | memset(tlb, 0, bytes); |
0b9afede | 291 | |
ee3f6ba8 AD |
292 | /* |
293 | * Get the overflow emergency buffer | |
294 | */ | |
295 | v_overflow_buffer = (void *)__get_free_pages(GFP_DMA, | |
296 | get_order(io_tlb_overflow)); | |
297 | if (!v_overflow_buffer) | |
298 | goto cleanup2; | |
299 | ||
300 | io_tlb_overflow_buffer = virt_to_phys(v_overflow_buffer); | |
301 | ||
0b9afede AW |
302 | /* |
303 | * Allocate and initialize the free list array. This array is used | |
304 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE | |
305 | * between io_tlb_start and io_tlb_end. | |
306 | */ | |
307 | io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL, | |
308 | get_order(io_tlb_nslabs * sizeof(int))); | |
309 | if (!io_tlb_list) | |
ee3f6ba8 | 310 | goto cleanup3; |
0b9afede AW |
311 | |
312 | for (i = 0; i < io_tlb_nslabs; i++) | |
313 | io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE); | |
314 | io_tlb_index = 0; | |
315 | ||
bc40ac66 BB |
316 | io_tlb_orig_addr = (phys_addr_t *) |
317 | __get_free_pages(GFP_KERNEL, | |
318 | get_order(io_tlb_nslabs * | |
319 | sizeof(phys_addr_t))); | |
0b9afede | 320 | if (!io_tlb_orig_addr) |
ee3f6ba8 | 321 | goto cleanup4; |
0b9afede | 322 | |
bc40ac66 | 323 | memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(phys_addr_t)); |
0b9afede | 324 | |
ad32e8cb | 325 | swiotlb_print_info(); |
0b9afede | 326 | |
5740afdb FT |
327 | late_alloc = 1; |
328 | ||
0b9afede AW |
329 | return 0; |
330 | ||
331 | cleanup4: | |
25667d67 TL |
332 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * |
333 | sizeof(int))); | |
0b9afede | 334 | io_tlb_list = NULL; |
ee3f6ba8 AD |
335 | cleanup3: |
336 | free_pages((unsigned long)v_overflow_buffer, | |
337 | get_order(io_tlb_overflow)); | |
338 | io_tlb_overflow_buffer = 0; | |
0b9afede | 339 | cleanup2: |
c40dba06 | 340 | io_tlb_end = 0; |
ff7204a7 | 341 | io_tlb_start = 0; |
74838b75 | 342 | io_tlb_nslabs = 0; |
0b9afede AW |
343 | return -ENOMEM; |
344 | } | |
345 | ||
5740afdb FT |
346 | void __init swiotlb_free(void) |
347 | { | |
ee3f6ba8 | 348 | if (!io_tlb_orig_addr) |
5740afdb FT |
349 | return; |
350 | ||
351 | if (late_alloc) { | |
ee3f6ba8 | 352 | free_pages((unsigned long)phys_to_virt(io_tlb_overflow_buffer), |
5740afdb FT |
353 | get_order(io_tlb_overflow)); |
354 | free_pages((unsigned long)io_tlb_orig_addr, | |
355 | get_order(io_tlb_nslabs * sizeof(phys_addr_t))); | |
356 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * | |
357 | sizeof(int))); | |
ff7204a7 | 358 | free_pages((unsigned long)phys_to_virt(io_tlb_start), |
5740afdb FT |
359 | get_order(io_tlb_nslabs << IO_TLB_SHIFT)); |
360 | } else { | |
ee3f6ba8 | 361 | free_bootmem_late(io_tlb_overflow_buffer, |
e79f86b2 | 362 | PAGE_ALIGN(io_tlb_overflow)); |
5740afdb | 363 | free_bootmem_late(__pa(io_tlb_orig_addr), |
e79f86b2 | 364 | PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t))); |
5740afdb | 365 | free_bootmem_late(__pa(io_tlb_list), |
e79f86b2 | 366 | PAGE_ALIGN(io_tlb_nslabs * sizeof(int))); |
ff7204a7 | 367 | free_bootmem_late(io_tlb_start, |
e79f86b2 | 368 | PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT)); |
5740afdb | 369 | } |
f21ffe9f | 370 | io_tlb_nslabs = 0; |
5740afdb FT |
371 | } |
372 | ||
02ca646e | 373 | static int is_swiotlb_buffer(phys_addr_t paddr) |
640aebfe | 374 | { |
ff7204a7 | 375 | return paddr >= io_tlb_start && paddr < io_tlb_end; |
640aebfe FT |
376 | } |
377 | ||
fb05a379 BB |
378 | /* |
379 | * Bounce: copy the swiotlb buffer back to the original dma location | |
380 | */ | |
af51a9f1 AD |
381 | static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr, |
382 | size_t size, enum dma_data_direction dir) | |
fb05a379 | 383 | { |
af51a9f1 AD |
384 | unsigned long pfn = PFN_DOWN(orig_addr); |
385 | unsigned char *vaddr = phys_to_virt(tlb_addr); | |
fb05a379 BB |
386 | |
387 | if (PageHighMem(pfn_to_page(pfn))) { | |
388 | /* The buffer does not have a mapping. Map it in and copy */ | |
af51a9f1 | 389 | unsigned int offset = orig_addr & ~PAGE_MASK; |
fb05a379 BB |
390 | char *buffer; |
391 | unsigned int sz = 0; | |
392 | unsigned long flags; | |
393 | ||
394 | while (size) { | |
67131ad0 | 395 | sz = min_t(size_t, PAGE_SIZE - offset, size); |
fb05a379 BB |
396 | |
397 | local_irq_save(flags); | |
c3eede8e | 398 | buffer = kmap_atomic(pfn_to_page(pfn)); |
fb05a379 | 399 | if (dir == DMA_TO_DEVICE) |
af51a9f1 | 400 | memcpy(vaddr, buffer + offset, sz); |
ef9b1893 | 401 | else |
af51a9f1 | 402 | memcpy(buffer + offset, vaddr, sz); |
c3eede8e | 403 | kunmap_atomic(buffer); |
ef9b1893 | 404 | local_irq_restore(flags); |
fb05a379 BB |
405 | |
406 | size -= sz; | |
407 | pfn++; | |
af51a9f1 | 408 | vaddr += sz; |
fb05a379 | 409 | offset = 0; |
ef9b1893 | 410 | } |
af51a9f1 AD |
411 | } else if (dir == DMA_TO_DEVICE) { |
412 | memcpy(vaddr, phys_to_virt(orig_addr), size); | |
ef9b1893 | 413 | } else { |
af51a9f1 | 414 | memcpy(phys_to_virt(orig_addr), vaddr, size); |
ef9b1893 | 415 | } |
1b548f66 JF |
416 | } |
417 | ||
e05ed4d1 AD |
418 | phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, |
419 | dma_addr_t tbl_dma_addr, | |
420 | phys_addr_t orig_addr, size_t size, | |
421 | enum dma_data_direction dir) | |
1da177e4 LT |
422 | { |
423 | unsigned long flags; | |
e05ed4d1 | 424 | phys_addr_t tlb_addr; |
1da177e4 LT |
425 | unsigned int nslots, stride, index, wrap; |
426 | int i; | |
681cc5cd FT |
427 | unsigned long mask; |
428 | unsigned long offset_slots; | |
429 | unsigned long max_slots; | |
430 | ||
ac2cbab2 YL |
431 | if (no_iotlb_memory) |
432 | panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer"); | |
433 | ||
681cc5cd | 434 | mask = dma_get_seg_boundary(hwdev); |
681cc5cd | 435 | |
eb605a57 FT |
436 | tbl_dma_addr &= mask; |
437 | ||
438 | offset_slots = ALIGN(tbl_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; | |
a5ddde4a IC |
439 | |
440 | /* | |
441 | * Carefully handle integer overflow which can occur when mask == ~0UL. | |
442 | */ | |
b15a3891 JB |
443 | max_slots = mask + 1 |
444 | ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT | |
445 | : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT); | |
1da177e4 LT |
446 | |
447 | /* | |
448 | * For mappings greater than a page, we limit the stride (and | |
449 | * hence alignment) to a page size. | |
450 | */ | |
451 | nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; | |
452 | if (size > PAGE_SIZE) | |
453 | stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT)); | |
454 | else | |
455 | stride = 1; | |
456 | ||
34814545 | 457 | BUG_ON(!nslots); |
1da177e4 LT |
458 | |
459 | /* | |
460 | * Find suitable number of IO TLB entries size that will fit this | |
461 | * request and allocate a buffer from that IO TLB pool. | |
462 | */ | |
463 | spin_lock_irqsave(&io_tlb_lock, flags); | |
a7133a15 AM |
464 | index = ALIGN(io_tlb_index, stride); |
465 | if (index >= io_tlb_nslabs) | |
466 | index = 0; | |
467 | wrap = index; | |
468 | ||
469 | do { | |
a8522509 FT |
470 | while (iommu_is_span_boundary(index, nslots, offset_slots, |
471 | max_slots)) { | |
b15a3891 JB |
472 | index += stride; |
473 | if (index >= io_tlb_nslabs) | |
474 | index = 0; | |
a7133a15 AM |
475 | if (index == wrap) |
476 | goto not_found; | |
477 | } | |
478 | ||
479 | /* | |
480 | * If we find a slot that indicates we have 'nslots' number of | |
481 | * contiguous buffers, we allocate the buffers from that slot | |
482 | * and mark the entries as '0' indicating unavailable. | |
483 | */ | |
484 | if (io_tlb_list[index] >= nslots) { | |
485 | int count = 0; | |
486 | ||
487 | for (i = index; i < (int) (index + nslots); i++) | |
488 | io_tlb_list[i] = 0; | |
489 | for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--) | |
490 | io_tlb_list[i] = ++count; | |
e05ed4d1 | 491 | tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT); |
1da177e4 | 492 | |
a7133a15 AM |
493 | /* |
494 | * Update the indices to avoid searching in the next | |
495 | * round. | |
496 | */ | |
497 | io_tlb_index = ((index + nslots) < io_tlb_nslabs | |
498 | ? (index + nslots) : 0); | |
499 | ||
500 | goto found; | |
501 | } | |
502 | index += stride; | |
503 | if (index >= io_tlb_nslabs) | |
504 | index = 0; | |
505 | } while (index != wrap); | |
506 | ||
507 | not_found: | |
508 | spin_unlock_irqrestore(&io_tlb_lock, flags); | |
e05ed4d1 | 509 | return SWIOTLB_MAP_ERROR; |
a7133a15 | 510 | found: |
1da177e4 LT |
511 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
512 | ||
513 | /* | |
514 | * Save away the mapping from the original address to the DMA address. | |
515 | * This is needed when we sync the memory. Then we sync the buffer if | |
516 | * needed. | |
517 | */ | |
bc40ac66 | 518 | for (i = 0; i < nslots; i++) |
e05ed4d1 | 519 | io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT); |
1da177e4 | 520 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) |
af51a9f1 | 521 | swiotlb_bounce(orig_addr, tlb_addr, size, DMA_TO_DEVICE); |
1da177e4 | 522 | |
e05ed4d1 | 523 | return tlb_addr; |
1da177e4 | 524 | } |
d7ef1533 | 525 | EXPORT_SYMBOL_GPL(swiotlb_tbl_map_single); |
1da177e4 | 526 | |
eb605a57 FT |
527 | /* |
528 | * Allocates bounce buffer and returns its kernel virtual address. | |
529 | */ | |
530 | ||
e05ed4d1 AD |
531 | phys_addr_t map_single(struct device *hwdev, phys_addr_t phys, size_t size, |
532 | enum dma_data_direction dir) | |
eb605a57 | 533 | { |
ff7204a7 | 534 | dma_addr_t start_dma_addr = phys_to_dma(hwdev, io_tlb_start); |
eb605a57 FT |
535 | |
536 | return swiotlb_tbl_map_single(hwdev, start_dma_addr, phys, size, dir); | |
537 | } | |
538 | ||
1da177e4 LT |
539 | /* |
540 | * dma_addr is the kernel virtual address of the bounce buffer to unmap. | |
541 | */ | |
61ca08c3 AD |
542 | void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr, |
543 | size_t size, enum dma_data_direction dir) | |
1da177e4 LT |
544 | { |
545 | unsigned long flags; | |
546 | int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; | |
61ca08c3 AD |
547 | int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT; |
548 | phys_addr_t orig_addr = io_tlb_orig_addr[index]; | |
1da177e4 LT |
549 | |
550 | /* | |
551 | * First, sync the memory before unmapping the entry | |
552 | */ | |
af51a9f1 AD |
553 | if (orig_addr && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL))) |
554 | swiotlb_bounce(orig_addr, tlb_addr, size, DMA_FROM_DEVICE); | |
1da177e4 LT |
555 | |
556 | /* | |
557 | * Return the buffer to the free list by setting the corresponding | |
af901ca1 | 558 | * entries to indicate the number of contiguous entries available. |
1da177e4 LT |
559 | * While returning the entries to the free list, we merge the entries |
560 | * with slots below and above the pool being returned. | |
561 | */ | |
562 | spin_lock_irqsave(&io_tlb_lock, flags); | |
563 | { | |
564 | count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ? | |
565 | io_tlb_list[index + nslots] : 0); | |
566 | /* | |
567 | * Step 1: return the slots to the free list, merging the | |
568 | * slots with superceeding slots | |
569 | */ | |
570 | for (i = index + nslots - 1; i >= index; i--) | |
571 | io_tlb_list[i] = ++count; | |
572 | /* | |
573 | * Step 2: merge the returned slots with the preceding slots, | |
574 | * if available (non zero) | |
575 | */ | |
576 | for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--) | |
577 | io_tlb_list[i] = ++count; | |
578 | } | |
579 | spin_unlock_irqrestore(&io_tlb_lock, flags); | |
580 | } | |
d7ef1533 | 581 | EXPORT_SYMBOL_GPL(swiotlb_tbl_unmap_single); |
1da177e4 | 582 | |
fbfda893 AD |
583 | void swiotlb_tbl_sync_single(struct device *hwdev, phys_addr_t tlb_addr, |
584 | size_t size, enum dma_data_direction dir, | |
585 | enum dma_sync_target target) | |
1da177e4 | 586 | { |
fbfda893 AD |
587 | int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT; |
588 | phys_addr_t orig_addr = io_tlb_orig_addr[index]; | |
bc40ac66 | 589 | |
fbfda893 | 590 | orig_addr += (unsigned long)tlb_addr & ((1 << IO_TLB_SHIFT) - 1); |
df336d1c | 591 | |
de69e0f0 JL |
592 | switch (target) { |
593 | case SYNC_FOR_CPU: | |
594 | if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)) | |
af51a9f1 | 595 | swiotlb_bounce(orig_addr, tlb_addr, |
fbfda893 | 596 | size, DMA_FROM_DEVICE); |
34814545 ES |
597 | else |
598 | BUG_ON(dir != DMA_TO_DEVICE); | |
de69e0f0 JL |
599 | break; |
600 | case SYNC_FOR_DEVICE: | |
601 | if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) | |
af51a9f1 | 602 | swiotlb_bounce(orig_addr, tlb_addr, |
fbfda893 | 603 | size, DMA_TO_DEVICE); |
34814545 ES |
604 | else |
605 | BUG_ON(dir != DMA_FROM_DEVICE); | |
de69e0f0 JL |
606 | break; |
607 | default: | |
1da177e4 | 608 | BUG(); |
de69e0f0 | 609 | } |
1da177e4 | 610 | } |
d7ef1533 | 611 | EXPORT_SYMBOL_GPL(swiotlb_tbl_sync_single); |
1da177e4 LT |
612 | |
613 | void * | |
614 | swiotlb_alloc_coherent(struct device *hwdev, size_t size, | |
06a54497 | 615 | dma_addr_t *dma_handle, gfp_t flags) |
1da177e4 | 616 | { |
563aaf06 | 617 | dma_addr_t dev_addr; |
1da177e4 LT |
618 | void *ret; |
619 | int order = get_order(size); | |
284901a9 | 620 | u64 dma_mask = DMA_BIT_MASK(32); |
1e74f300 FT |
621 | |
622 | if (hwdev && hwdev->coherent_dma_mask) | |
623 | dma_mask = hwdev->coherent_dma_mask; | |
1da177e4 | 624 | |
25667d67 | 625 | ret = (void *)__get_free_pages(flags, order); |
e05ed4d1 AD |
626 | if (ret) { |
627 | dev_addr = swiotlb_virt_to_bus(hwdev, ret); | |
628 | if (dev_addr + size - 1 > dma_mask) { | |
629 | /* | |
630 | * The allocated memory isn't reachable by the device. | |
631 | */ | |
632 | free_pages((unsigned long) ret, order); | |
633 | ret = NULL; | |
634 | } | |
1da177e4 LT |
635 | } |
636 | if (!ret) { | |
637 | /* | |
bfc5501f KRW |
638 | * We are either out of memory or the device can't DMA to |
639 | * GFP_DMA memory; fall back on map_single(), which | |
ceb5ac32 | 640 | * will grab memory from the lowest available address range. |
1da177e4 | 641 | */ |
e05ed4d1 AD |
642 | phys_addr_t paddr = map_single(hwdev, 0, size, DMA_FROM_DEVICE); |
643 | if (paddr == SWIOTLB_MAP_ERROR) | |
1da177e4 | 644 | return NULL; |
1da177e4 | 645 | |
e05ed4d1 AD |
646 | ret = phys_to_virt(paddr); |
647 | dev_addr = phys_to_dma(hwdev, paddr); | |
1da177e4 | 648 | |
61ca08c3 AD |
649 | /* Confirm address can be DMA'd by device */ |
650 | if (dev_addr + size - 1 > dma_mask) { | |
651 | printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n", | |
652 | (unsigned long long)dma_mask, | |
653 | (unsigned long long)dev_addr); | |
a2b89b59 | 654 | |
61ca08c3 AD |
655 | /* DMA_TO_DEVICE to avoid memcpy in unmap_single */ |
656 | swiotlb_tbl_unmap_single(hwdev, paddr, | |
657 | size, DMA_TO_DEVICE); | |
658 | return NULL; | |
659 | } | |
1da177e4 | 660 | } |
e05ed4d1 | 661 | |
1da177e4 | 662 | *dma_handle = dev_addr; |
e05ed4d1 AD |
663 | memset(ret, 0, size); |
664 | ||
1da177e4 LT |
665 | return ret; |
666 | } | |
874d6a95 | 667 | EXPORT_SYMBOL(swiotlb_alloc_coherent); |
1da177e4 LT |
668 | |
669 | void | |
670 | swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr, | |
02ca646e | 671 | dma_addr_t dev_addr) |
1da177e4 | 672 | { |
862d196b | 673 | phys_addr_t paddr = dma_to_phys(hwdev, dev_addr); |
02ca646e | 674 | |
aa24886e | 675 | WARN_ON(irqs_disabled()); |
02ca646e FT |
676 | if (!is_swiotlb_buffer(paddr)) |
677 | free_pages((unsigned long)vaddr, get_order(size)); | |
1da177e4 | 678 | else |
bfc5501f | 679 | /* DMA_TO_DEVICE to avoid memcpy in swiotlb_tbl_unmap_single */ |
61ca08c3 | 680 | swiotlb_tbl_unmap_single(hwdev, paddr, size, DMA_TO_DEVICE); |
1da177e4 | 681 | } |
874d6a95 | 682 | EXPORT_SYMBOL(swiotlb_free_coherent); |
1da177e4 LT |
683 | |
684 | static void | |
22d48269 KRW |
685 | swiotlb_full(struct device *dev, size_t size, enum dma_data_direction dir, |
686 | int do_panic) | |
1da177e4 LT |
687 | { |
688 | /* | |
689 | * Ran out of IOMMU space for this operation. This is very bad. | |
690 | * Unfortunately the drivers cannot handle this operation properly. | |
17e5ad6c | 691 | * unless they check for dma_mapping_error (most don't) |
1da177e4 LT |
692 | * When the mapping is small enough return a static buffer to limit |
693 | * the damage, or panic when the transfer is too big. | |
694 | */ | |
563aaf06 | 695 | printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at " |
94b32486 | 696 | "device %s\n", size, dev ? dev_name(dev) : "?"); |
6fa3eb70 | 697 | BUG(); |
1da177e4 | 698 | |
c7084b35 CD |
699 | if (size <= io_tlb_overflow || !do_panic) |
700 | return; | |
701 | ||
702 | if (dir == DMA_BIDIRECTIONAL) | |
703 | panic("DMA: Random memory could be DMA accessed\n"); | |
704 | if (dir == DMA_FROM_DEVICE) | |
705 | panic("DMA: Random memory could be DMA written\n"); | |
706 | if (dir == DMA_TO_DEVICE) | |
707 | panic("DMA: Random memory could be DMA read\n"); | |
1da177e4 LT |
708 | } |
709 | ||
710 | /* | |
711 | * Map a single buffer of the indicated size for DMA in streaming mode. The | |
17e5ad6c | 712 | * physical address to use is returned. |
1da177e4 LT |
713 | * |
714 | * Once the device is given the dma address, the device owns this memory until | |
ceb5ac32 | 715 | * either swiotlb_unmap_page or swiotlb_dma_sync_single is performed. |
1da177e4 | 716 | */ |
f98eee8e FT |
717 | dma_addr_t swiotlb_map_page(struct device *dev, struct page *page, |
718 | unsigned long offset, size_t size, | |
719 | enum dma_data_direction dir, | |
720 | struct dma_attrs *attrs) | |
1da177e4 | 721 | { |
e05ed4d1 | 722 | phys_addr_t map, phys = page_to_phys(page) + offset; |
862d196b | 723 | dma_addr_t dev_addr = phys_to_dma(dev, phys); |
1da177e4 | 724 | |
34814545 | 725 | BUG_ON(dir == DMA_NONE); |
1da177e4 | 726 | /* |
ceb5ac32 | 727 | * If the address happens to be in the device's DMA window, |
1da177e4 LT |
728 | * we can safely return the device addr and not worry about bounce |
729 | * buffering it. | |
730 | */ | |
b9394647 | 731 | if (dma_capable(dev, dev_addr, size) && !swiotlb_force) |
1da177e4 LT |
732 | return dev_addr; |
733 | ||
e05ed4d1 | 734 | /* Oh well, have to allocate and map a bounce buffer. */ |
f98eee8e | 735 | map = map_single(dev, phys, size, dir); |
e05ed4d1 | 736 | if (map == SWIOTLB_MAP_ERROR) { |
f98eee8e | 737 | swiotlb_full(dev, size, dir, 1); |
ee3f6ba8 | 738 | return phys_to_dma(dev, io_tlb_overflow_buffer); |
1da177e4 LT |
739 | } |
740 | ||
e05ed4d1 | 741 | dev_addr = phys_to_dma(dev, map); |
1da177e4 | 742 | |
e05ed4d1 | 743 | /* Ensure that the address returned is DMA'ble */ |
fba99fa3 | 744 | if (!dma_capable(dev, dev_addr, size)) { |
61ca08c3 | 745 | swiotlb_tbl_unmap_single(dev, map, size, dir); |
ee3f6ba8 | 746 | return phys_to_dma(dev, io_tlb_overflow_buffer); |
fba99fa3 | 747 | } |
1da177e4 LT |
748 | |
749 | return dev_addr; | |
750 | } | |
f98eee8e | 751 | EXPORT_SYMBOL_GPL(swiotlb_map_page); |
1da177e4 | 752 | |
1da177e4 LT |
753 | /* |
754 | * Unmap a single streaming mode DMA translation. The dma_addr and size must | |
ceb5ac32 | 755 | * match what was provided for in a previous swiotlb_map_page call. All |
1da177e4 LT |
756 | * other usages are undefined. |
757 | * | |
758 | * After this call, reads by the cpu to the buffer are guaranteed to see | |
759 | * whatever the device wrote there. | |
760 | */ | |
7fcebbd2 | 761 | static void unmap_single(struct device *hwdev, dma_addr_t dev_addr, |
22d48269 | 762 | size_t size, enum dma_data_direction dir) |
1da177e4 | 763 | { |
862d196b | 764 | phys_addr_t paddr = dma_to_phys(hwdev, dev_addr); |
1da177e4 | 765 | |
34814545 | 766 | BUG_ON(dir == DMA_NONE); |
7fcebbd2 | 767 | |
02ca646e | 768 | if (is_swiotlb_buffer(paddr)) { |
61ca08c3 | 769 | swiotlb_tbl_unmap_single(hwdev, paddr, size, dir); |
7fcebbd2 BB |
770 | return; |
771 | } | |
772 | ||
773 | if (dir != DMA_FROM_DEVICE) | |
774 | return; | |
775 | ||
02ca646e FT |
776 | /* |
777 | * phys_to_virt doesn't work with hihgmem page but we could | |
778 | * call dma_mark_clean() with hihgmem page here. However, we | |
779 | * are fine since dma_mark_clean() is null on POWERPC. We can | |
780 | * make dma_mark_clean() take a physical address if necessary. | |
781 | */ | |
782 | dma_mark_clean(phys_to_virt(paddr), size); | |
7fcebbd2 BB |
783 | } |
784 | ||
785 | void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr, | |
786 | size_t size, enum dma_data_direction dir, | |
787 | struct dma_attrs *attrs) | |
788 | { | |
789 | unmap_single(hwdev, dev_addr, size, dir); | |
1da177e4 | 790 | } |
f98eee8e | 791 | EXPORT_SYMBOL_GPL(swiotlb_unmap_page); |
874d6a95 | 792 | |
1da177e4 LT |
793 | /* |
794 | * Make physical memory consistent for a single streaming mode DMA translation | |
795 | * after a transfer. | |
796 | * | |
ceb5ac32 | 797 | * If you perform a swiotlb_map_page() but wish to interrogate the buffer |
17e5ad6c TL |
798 | * using the cpu, yet do not wish to teardown the dma mapping, you must |
799 | * call this function before doing so. At the next point you give the dma | |
1da177e4 LT |
800 | * address back to the card, you must first perform a |
801 | * swiotlb_dma_sync_for_device, and then the device again owns the buffer | |
802 | */ | |
be6b0267 | 803 | static void |
8270f3f1 | 804 | swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr, |
d7ef1533 KRW |
805 | size_t size, enum dma_data_direction dir, |
806 | enum dma_sync_target target) | |
1da177e4 | 807 | { |
862d196b | 808 | phys_addr_t paddr = dma_to_phys(hwdev, dev_addr); |
1da177e4 | 809 | |
34814545 | 810 | BUG_ON(dir == DMA_NONE); |
380d6878 | 811 | |
02ca646e | 812 | if (is_swiotlb_buffer(paddr)) { |
fbfda893 | 813 | swiotlb_tbl_sync_single(hwdev, paddr, size, dir, target); |
380d6878 BB |
814 | return; |
815 | } | |
816 | ||
817 | if (dir != DMA_FROM_DEVICE) | |
818 | return; | |
819 | ||
02ca646e | 820 | dma_mark_clean(phys_to_virt(paddr), size); |
1da177e4 LT |
821 | } |
822 | ||
8270f3f1 JL |
823 | void |
824 | swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr, | |
160c1d8e | 825 | size_t size, enum dma_data_direction dir) |
8270f3f1 | 826 | { |
de69e0f0 | 827 | swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU); |
8270f3f1 | 828 | } |
874d6a95 | 829 | EXPORT_SYMBOL(swiotlb_sync_single_for_cpu); |
8270f3f1 | 830 | |
1da177e4 LT |
831 | void |
832 | swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, | |
160c1d8e | 833 | size_t size, enum dma_data_direction dir) |
1da177e4 | 834 | { |
de69e0f0 | 835 | swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE); |
1da177e4 | 836 | } |
874d6a95 | 837 | EXPORT_SYMBOL(swiotlb_sync_single_for_device); |
1da177e4 LT |
838 | |
839 | /* | |
840 | * Map a set of buffers described by scatterlist in streaming mode for DMA. | |
ceb5ac32 | 841 | * This is the scatter-gather version of the above swiotlb_map_page |
1da177e4 LT |
842 | * interface. Here the scatter gather list elements are each tagged with the |
843 | * appropriate dma address and length. They are obtained via | |
844 | * sg_dma_{address,length}(SG). | |
845 | * | |
846 | * NOTE: An implementation may be able to use a smaller number of | |
847 | * DMA address/length pairs than there are SG table elements. | |
848 | * (for example via virtual mapping capabilities) | |
849 | * The routine returns the number of addr/length pairs actually | |
850 | * used, at most nents. | |
851 | * | |
ceb5ac32 | 852 | * Device ownership issues as mentioned above for swiotlb_map_page are the |
1da177e4 LT |
853 | * same here. |
854 | */ | |
855 | int | |
309df0c5 | 856 | swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems, |
160c1d8e | 857 | enum dma_data_direction dir, struct dma_attrs *attrs) |
1da177e4 | 858 | { |
dbfd49fe | 859 | struct scatterlist *sg; |
1da177e4 LT |
860 | int i; |
861 | ||
34814545 | 862 | BUG_ON(dir == DMA_NONE); |
1da177e4 | 863 | |
dbfd49fe | 864 | for_each_sg(sgl, sg, nelems, i) { |
961d7d0e | 865 | phys_addr_t paddr = sg_phys(sg); |
862d196b | 866 | dma_addr_t dev_addr = phys_to_dma(hwdev, paddr); |
bc40ac66 | 867 | |
cf56e3f2 | 868 | if (swiotlb_force || |
b9394647 | 869 | !dma_capable(hwdev, dev_addr, sg->length)) { |
e05ed4d1 AD |
870 | phys_addr_t map = map_single(hwdev, sg_phys(sg), |
871 | sg->length, dir); | |
872 | if (map == SWIOTLB_MAP_ERROR) { | |
1da177e4 LT |
873 | /* Don't panic here, we expect map_sg users |
874 | to do proper error handling. */ | |
875 | swiotlb_full(hwdev, sg->length, dir, 0); | |
309df0c5 AK |
876 | swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir, |
877 | attrs); | |
dbfd49fe | 878 | sgl[0].dma_length = 0; |
1da177e4 LT |
879 | return 0; |
880 | } | |
e05ed4d1 | 881 | sg->dma_address = phys_to_dma(hwdev, map); |
1da177e4 LT |
882 | } else |
883 | sg->dma_address = dev_addr; | |
884 | sg->dma_length = sg->length; | |
885 | } | |
886 | return nelems; | |
887 | } | |
309df0c5 AK |
888 | EXPORT_SYMBOL(swiotlb_map_sg_attrs); |
889 | ||
890 | int | |
891 | swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems, | |
22d48269 | 892 | enum dma_data_direction dir) |
309df0c5 AK |
893 | { |
894 | return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL); | |
895 | } | |
874d6a95 | 896 | EXPORT_SYMBOL(swiotlb_map_sg); |
1da177e4 LT |
897 | |
898 | /* | |
899 | * Unmap a set of streaming mode DMA translations. Again, cpu read rules | |
ceb5ac32 | 900 | * concerning calls here are the same as for swiotlb_unmap_page() above. |
1da177e4 LT |
901 | */ |
902 | void | |
309df0c5 | 903 | swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl, |
160c1d8e | 904 | int nelems, enum dma_data_direction dir, struct dma_attrs *attrs) |
1da177e4 | 905 | { |
dbfd49fe | 906 | struct scatterlist *sg; |
1da177e4 LT |
907 | int i; |
908 | ||
34814545 | 909 | BUG_ON(dir == DMA_NONE); |
1da177e4 | 910 | |
7fcebbd2 BB |
911 | for_each_sg(sgl, sg, nelems, i) |
912 | unmap_single(hwdev, sg->dma_address, sg->dma_length, dir); | |
913 | ||
1da177e4 | 914 | } |
309df0c5 AK |
915 | EXPORT_SYMBOL(swiotlb_unmap_sg_attrs); |
916 | ||
917 | void | |
918 | swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems, | |
22d48269 | 919 | enum dma_data_direction dir) |
309df0c5 AK |
920 | { |
921 | return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL); | |
922 | } | |
874d6a95 | 923 | EXPORT_SYMBOL(swiotlb_unmap_sg); |
1da177e4 LT |
924 | |
925 | /* | |
926 | * Make physical memory consistent for a set of streaming mode DMA translations | |
927 | * after a transfer. | |
928 | * | |
929 | * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules | |
930 | * and usage. | |
931 | */ | |
be6b0267 | 932 | static void |
dbfd49fe | 933 | swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl, |
d7ef1533 KRW |
934 | int nelems, enum dma_data_direction dir, |
935 | enum dma_sync_target target) | |
1da177e4 | 936 | { |
dbfd49fe | 937 | struct scatterlist *sg; |
1da177e4 LT |
938 | int i; |
939 | ||
380d6878 BB |
940 | for_each_sg(sgl, sg, nelems, i) |
941 | swiotlb_sync_single(hwdev, sg->dma_address, | |
de69e0f0 | 942 | sg->dma_length, dir, target); |
1da177e4 LT |
943 | } |
944 | ||
8270f3f1 JL |
945 | void |
946 | swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg, | |
160c1d8e | 947 | int nelems, enum dma_data_direction dir) |
8270f3f1 | 948 | { |
de69e0f0 | 949 | swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU); |
8270f3f1 | 950 | } |
874d6a95 | 951 | EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu); |
8270f3f1 | 952 | |
1da177e4 LT |
953 | void |
954 | swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg, | |
160c1d8e | 955 | int nelems, enum dma_data_direction dir) |
1da177e4 | 956 | { |
de69e0f0 | 957 | swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE); |
1da177e4 | 958 | } |
874d6a95 | 959 | EXPORT_SYMBOL(swiotlb_sync_sg_for_device); |
1da177e4 LT |
960 | |
961 | int | |
8d8bb39b | 962 | swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr) |
1da177e4 | 963 | { |
ee3f6ba8 | 964 | return (dma_addr == phys_to_dma(hwdev, io_tlb_overflow_buffer)); |
1da177e4 | 965 | } |
874d6a95 | 966 | EXPORT_SYMBOL(swiotlb_dma_mapping_error); |
1da177e4 LT |
967 | |
968 | /* | |
17e5ad6c | 969 | * Return whether the given device DMA address mask can be supported |
1da177e4 | 970 | * properly. For example, if your device can only drive the low 24-bits |
17e5ad6c | 971 | * during bus mastering, then you would pass 0x00ffffff as the mask to |
1da177e4 LT |
972 | * this function. |
973 | */ | |
974 | int | |
563aaf06 | 975 | swiotlb_dma_supported(struct device *hwdev, u64 mask) |
1da177e4 | 976 | { |
c40dba06 | 977 | return phys_to_dma(hwdev, io_tlb_end - 1) <= mask; |
1da177e4 | 978 | } |
1da177e4 | 979 | EXPORT_SYMBOL(swiotlb_dma_supported); |