Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Dynamic DMA mapping support. |
| 3 | * |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 4 | * This implementation is a fallback for platforms that do not support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
John W. Linville | 569c8bf | 2005-09-29 14:45:24 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/cache.h> |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 20 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/mm.h> |
| 22 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/spinlock.h> |
Jeremy Fitzhardinge | 8c5df16 | 2008-12-16 12:17:26 -0800 | [diff] [blame] | 24 | #include <linux/swiotlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/string.h> |
Ian Campbell | 0016fde | 2008-12-16 12:17:27 -0800 | [diff] [blame] | 26 | #include <linux/swiotlb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/types.h> |
| 28 | #include <linux/ctype.h> |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 29 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/dma.h> |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 33 | #include <asm/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/bootmem.h> |
FUJITA Tomonori | a852250 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 37 | #include <linux/iommu-helper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #define OFFSET(val,align) ((unsigned long) \ |
| 40 | ( (val) & ( (align) - 1))) |
| 41 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 42 | #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. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Enumeration for sync targets |
| 53 | */ |
| 54 | enum dma_sync_target { |
| 55 | SYNC_FOR_CPU = 0, |
| 56 | SYNC_FOR_DEVICE = 1, |
| 57 | }; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | int 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 | */ |
| 66 | static 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 | */ |
| 72 | static unsigned long io_tlb_nslabs; |
| 73 | |
| 74 | /* |
| 75 | * When the IOMMU overflows we return a fallback buffer. This sets the size. |
| 76 | */ |
| 77 | static unsigned long io_tlb_overflow = 32*1024; |
| 78 | |
| 79 | void *io_tlb_overflow_buffer; |
| 80 | |
| 81 | /* |
| 82 | * This is a free list describing the number of free entries available from |
| 83 | * each index |
| 84 | */ |
| 85 | static unsigned int *io_tlb_list; |
| 86 | static 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 Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 92 | static struct swiotlb_phys_addr { |
| 93 | struct page *page; |
| 94 | unsigned int offset; |
| 95 | } *io_tlb_orig_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Protect the above data structures in the map and unmap calls |
| 99 | */ |
| 100 | static DEFINE_SPINLOCK(io_tlb_lock); |
| 101 | |
| 102 | static int __init |
| 103 | setup_io_tlb_npages(char *str) |
| 104 | { |
| 105 | if (isdigit(*str)) { |
Alex Williamson | e8579e7 | 2005-08-04 13:06:00 -0700 | [diff] [blame] | 106 | io_tlb_nslabs = simple_strtoul(str, &str, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* 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 Fitzhardinge | 8c5df16 | 2008-12-16 12:17:26 -0800 | [diff] [blame] | 119 | void * __weak swiotlb_alloc_boot(size_t size, unsigned long nslabs) |
| 120 | { |
| 121 | return alloc_bootmem_low_pages(size); |
| 122 | } |
| 123 | |
| 124 | void * __weak swiotlb_alloc(unsigned order, unsigned long nslabs) |
| 125 | { |
| 126 | return (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN, order); |
| 127 | } |
| 128 | |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 129 | dma_addr_t __weak swiotlb_phys_to_bus(phys_addr_t paddr) |
| 130 | { |
| 131 | return paddr; |
| 132 | } |
| 133 | |
| 134 | phys_addr_t __weak swiotlb_bus_to_phys(dma_addr_t baddr) |
| 135 | { |
| 136 | return baddr; |
| 137 | } |
| 138 | |
| 139 | static dma_addr_t swiotlb_virt_to_bus(volatile void *address) |
| 140 | { |
| 141 | return swiotlb_phys_to_bus(virt_to_phys(address)); |
| 142 | } |
| 143 | |
| 144 | static void *swiotlb_bus_to_virt(dma_addr_t address) |
| 145 | { |
| 146 | return phys_to_virt(swiotlb_bus_to_phys(address)); |
| 147 | } |
| 148 | |
Ian Campbell | b81ea27 | 2008-12-16 12:17:31 -0800 | [diff] [blame] | 149 | int __weak swiotlb_arch_range_needs_mapping(void *ptr, size_t size) |
| 150 | { |
| 151 | return 0; |
| 152 | } |
| 153 | |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 154 | static dma_addr_t swiotlb_sg_to_bus(struct scatterlist *sg) |
| 155 | { |
| 156 | return swiotlb_phys_to_bus(page_to_phys(sg_page(sg)) + sg->offset); |
| 157 | } |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* |
| 160 | * Statically reserve bounce buffer space and initialize bounce buffer data |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 161 | * structures for the software IO TLB used to implement the DMA API. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | */ |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 163 | void __init |
| 164 | swiotlb_init_with_default_size(size_t default_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 166 | unsigned long i, bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | if (!io_tlb_nslabs) { |
Alex Williamson | e8579e7 | 2005-08-04 13:06:00 -0700 | [diff] [blame] | 169 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); |
| 171 | } |
| 172 | |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 173 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | /* |
| 176 | * Get IO TLB memory from the low pages |
| 177 | */ |
Jeremy Fitzhardinge | 8c5df16 | 2008-12-16 12:17:26 -0800 | [diff] [blame] | 178 | io_tlb_start = swiotlb_alloc_boot(bytes, io_tlb_nslabs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | if (!io_tlb_start) |
| 180 | panic("Cannot allocate SWIOTLB buffer"); |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 181 | io_tlb_end = io_tlb_start + bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 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 | */ |
| 188 | io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int)); |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 189 | for (i = 0; i < io_tlb_nslabs; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE); |
| 191 | io_tlb_index = 0; |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 192 | io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(struct swiotlb_phys_addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Get the overflow emergency buffer |
| 196 | */ |
| 197 | io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow); |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 198 | if (!io_tlb_overflow_buffer) |
| 199 | panic("Cannot allocate SWIOTLB overflow buffer!\n"); |
| 200 | |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 201 | printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n", |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 202 | swiotlb_virt_to_bus(io_tlb_start), swiotlb_virt_to_bus(io_tlb_end)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 205 | void __init |
| 206 | swiotlb_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 208 | swiotlb_init_with_default_size(64 * (1<<20)); /* default to 64MB */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 211 | /* |
| 212 | * Systems with larger DMA zones (those that don't support ISA) can |
| 213 | * initialize the swiotlb later using the slab allocator if needed. |
| 214 | * This should be just like above, but with some error catching. |
| 215 | */ |
| 216 | int |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 217 | swiotlb_late_init_with_default_size(size_t default_size) |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 218 | { |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 219 | unsigned long i, bytes, req_nslabs = io_tlb_nslabs; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 220 | unsigned int order; |
| 221 | |
| 222 | if (!io_tlb_nslabs) { |
| 223 | io_tlb_nslabs = (default_size >> IO_TLB_SHIFT); |
| 224 | io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * Get IO TLB memory from the low pages |
| 229 | */ |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 230 | order = get_order(io_tlb_nslabs << IO_TLB_SHIFT); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 231 | io_tlb_nslabs = SLABS_PER_PAGE << order; |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 232 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 233 | |
| 234 | while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) { |
Jeremy Fitzhardinge | 8c5df16 | 2008-12-16 12:17:26 -0800 | [diff] [blame] | 235 | io_tlb_start = swiotlb_alloc(order, io_tlb_nslabs); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 236 | if (io_tlb_start) |
| 237 | break; |
| 238 | order--; |
| 239 | } |
| 240 | |
| 241 | if (!io_tlb_start) |
| 242 | goto cleanup1; |
| 243 | |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 244 | if (order != get_order(bytes)) { |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 245 | printk(KERN_WARNING "Warning: only able to allocate %ld MB " |
| 246 | "for software IO TLB\n", (PAGE_SIZE << order) >> 20); |
| 247 | io_tlb_nslabs = SLABS_PER_PAGE << order; |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 248 | bytes = io_tlb_nslabs << IO_TLB_SHIFT; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 249 | } |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 250 | io_tlb_end = io_tlb_start + bytes; |
| 251 | memset(io_tlb_start, 0, bytes); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Allocate and initialize the free list array. This array is used |
| 255 | * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE |
| 256 | * between io_tlb_start and io_tlb_end. |
| 257 | */ |
| 258 | io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL, |
| 259 | get_order(io_tlb_nslabs * sizeof(int))); |
| 260 | if (!io_tlb_list) |
| 261 | goto cleanup2; |
| 262 | |
| 263 | for (i = 0; i < io_tlb_nslabs; i++) |
| 264 | io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE); |
| 265 | io_tlb_index = 0; |
| 266 | |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 267 | io_tlb_orig_addr = (struct swiotlb_phys_addr *)__get_free_pages(GFP_KERNEL, |
| 268 | get_order(io_tlb_nslabs * sizeof(struct swiotlb_phys_addr))); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 269 | if (!io_tlb_orig_addr) |
| 270 | goto cleanup3; |
| 271 | |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 272 | memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(struct swiotlb_phys_addr)); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * Get the overflow emergency buffer |
| 276 | */ |
| 277 | io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA, |
| 278 | get_order(io_tlb_overflow)); |
| 279 | if (!io_tlb_overflow_buffer) |
| 280 | goto cleanup4; |
| 281 | |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 282 | printk(KERN_INFO "Placing %luMB software IO TLB between 0x%lx - " |
| 283 | "0x%lx\n", bytes >> 20, |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 284 | swiotlb_virt_to_bus(io_tlb_start), swiotlb_virt_to_bus(io_tlb_end)); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 285 | |
| 286 | return 0; |
| 287 | |
| 288 | cleanup4: |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 289 | free_pages((unsigned long)io_tlb_orig_addr, get_order(io_tlb_nslabs * |
| 290 | sizeof(char *))); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 291 | io_tlb_orig_addr = NULL; |
| 292 | cleanup3: |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 293 | free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs * |
| 294 | sizeof(int))); |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 295 | io_tlb_list = NULL; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 296 | cleanup2: |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 297 | io_tlb_end = NULL; |
Alex Williamson | 0b9afed | 2005-09-06 11:20:49 -0600 | [diff] [blame] | 298 | free_pages((unsigned long)io_tlb_start, order); |
| 299 | io_tlb_start = NULL; |
| 300 | cleanup1: |
| 301 | io_tlb_nslabs = req_nslabs; |
| 302 | return -ENOMEM; |
| 303 | } |
| 304 | |
Andrew Morton | be6b026 | 2007-02-12 00:52:17 -0800 | [diff] [blame] | 305 | static int |
FUJITA Tomonori | 2797982 | 2008-09-10 01:06:49 +0900 | [diff] [blame] | 306 | address_needs_mapping(struct device *hwdev, dma_addr_t addr, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
FUJITA Tomonori | 07a2c01 | 2008-09-19 02:02:05 +0900 | [diff] [blame] | 308 | return !is_buffer_dma_capable(dma_get_mask(hwdev), addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Ian Campbell | b81ea27 | 2008-12-16 12:17:31 -0800 | [diff] [blame] | 311 | static inline int range_needs_mapping(void *ptr, size_t size) |
| 312 | { |
| 313 | return swiotlb_force || swiotlb_arch_range_needs_mapping(ptr, size); |
| 314 | } |
| 315 | |
FUJITA Tomonori | 640aebf | 2008-09-08 18:53:50 +0900 | [diff] [blame] | 316 | static int is_swiotlb_buffer(char *addr) |
| 317 | { |
| 318 | return addr >= io_tlb_start && addr < io_tlb_end; |
| 319 | } |
| 320 | |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 321 | static struct swiotlb_phys_addr swiotlb_bus_to_phys_addr(char *dma_addr) |
Jeremy Fitzhardinge | 1b548f6 | 2008-12-16 12:17:32 -0800 | [diff] [blame] | 322 | { |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 323 | int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT; |
| 324 | struct swiotlb_phys_addr buffer = io_tlb_orig_addr[index]; |
| 325 | buffer.offset += (long)dma_addr & ((1 << IO_TLB_SHIFT) - 1); |
| 326 | buffer.page += buffer.offset >> PAGE_SHIFT; |
| 327 | buffer.offset &= PAGE_SIZE - 1; |
| 328 | return buffer; |
| 329 | } |
| 330 | |
| 331 | static void |
| 332 | __sync_single(struct swiotlb_phys_addr buffer, char *dma_addr, size_t size, int dir) |
| 333 | { |
| 334 | if (PageHighMem(buffer.page)) { |
| 335 | size_t len, bytes; |
| 336 | char *dev, *host, *kmp; |
| 337 | |
| 338 | len = size; |
| 339 | while (len != 0) { |
| 340 | unsigned long flags; |
| 341 | |
| 342 | bytes = len; |
| 343 | if ((bytes + buffer.offset) > PAGE_SIZE) |
| 344 | bytes = PAGE_SIZE - buffer.offset; |
| 345 | local_irq_save(flags); /* protects KM_BOUNCE_READ */ |
| 346 | kmp = kmap_atomic(buffer.page, KM_BOUNCE_READ); |
| 347 | dev = dma_addr + size - len; |
| 348 | host = kmp + buffer.offset; |
| 349 | if (dir == DMA_FROM_DEVICE) |
| 350 | memcpy(host, dev, bytes); |
| 351 | else |
| 352 | memcpy(dev, host, bytes); |
| 353 | kunmap_atomic(kmp, KM_BOUNCE_READ); |
| 354 | local_irq_restore(flags); |
| 355 | len -= bytes; |
| 356 | buffer.page++; |
| 357 | buffer.offset = 0; |
| 358 | } |
| 359 | } else { |
| 360 | void *v = page_address(buffer.page) + buffer.offset; |
| 361 | |
| 362 | if (dir == DMA_TO_DEVICE) |
| 363 | memcpy(dma_addr, v, size); |
| 364 | else |
| 365 | memcpy(v, dma_addr, size); |
| 366 | } |
Jeremy Fitzhardinge | 1b548f6 | 2008-12-16 12:17:32 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* |
| 370 | * Allocates bounce buffer and returns its kernel virtual address. |
| 371 | */ |
| 372 | static void * |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 373 | map_single(struct device *hwdev, struct swiotlb_phys_addr buffer, size_t size, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | unsigned long flags; |
| 376 | char *dma_addr; |
| 377 | unsigned int nslots, stride, index, wrap; |
| 378 | int i; |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 379 | unsigned long start_dma_addr; |
| 380 | unsigned long mask; |
| 381 | unsigned long offset_slots; |
| 382 | unsigned long max_slots; |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 383 | struct swiotlb_phys_addr slot_buf; |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 384 | |
| 385 | mask = dma_get_seg_boundary(hwdev); |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 386 | start_dma_addr = swiotlb_virt_to_bus(io_tlb_start) & mask; |
FUJITA Tomonori | 681cc5c | 2008-02-04 22:28:16 -0800 | [diff] [blame] | 387 | |
| 388 | offset_slots = ALIGN(start_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; |
Ian Campbell | a5ddde4a | 2008-12-16 12:17:29 -0800 | [diff] [blame] | 389 | |
| 390 | /* |
| 391 | * Carefully handle integer overflow which can occur when mask == ~0UL. |
| 392 | */ |
Jan Beulich | b15a3891 | 2008-03-13 09:13:30 +0000 | [diff] [blame] | 393 | max_slots = mask + 1 |
| 394 | ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT |
| 395 | : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
| 398 | * For mappings greater than a page, we limit the stride (and |
| 399 | * hence alignment) to a page size. |
| 400 | */ |
| 401 | nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; |
| 402 | if (size > PAGE_SIZE) |
| 403 | stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT)); |
| 404 | else |
| 405 | stride = 1; |
| 406 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 407 | BUG_ON(!nslots); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * Find suitable number of IO TLB entries size that will fit this |
| 411 | * request and allocate a buffer from that IO TLB pool. |
| 412 | */ |
| 413 | spin_lock_irqsave(&io_tlb_lock, flags); |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 414 | index = ALIGN(io_tlb_index, stride); |
| 415 | if (index >= io_tlb_nslabs) |
| 416 | index = 0; |
| 417 | wrap = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 419 | do { |
FUJITA Tomonori | a852250 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 420 | while (iommu_is_span_boundary(index, nslots, offset_slots, |
| 421 | max_slots)) { |
Jan Beulich | b15a3891 | 2008-03-13 09:13:30 +0000 | [diff] [blame] | 422 | index += stride; |
| 423 | if (index >= io_tlb_nslabs) |
| 424 | index = 0; |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 425 | if (index == wrap) |
| 426 | goto not_found; |
| 427 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
Andrew Morton | a7133a1 | 2008-04-29 00:59:36 -0700 | [diff] [blame] | 429 | /* |
| 430 | * If we find a slot that indicates we have 'nslots' number of |
| 431 | * contiguous buffers, we allocate the buffers from that slot |
| 432 | * and mark the entries as '0' indicating unavailable. |
| 433 | */ |
| 434 | if (io_tlb_list[index] >= nslots) { |
| 435 | int count = 0; |
| 436 | |
| 437 | for (i = index; i < (int) (index + nslots); i++) |
| 438 | io_tlb_list[i] = 0; |
| 439 | for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--) |
| 440 | io_tlb_list[i] = ++count; |
| 441 | dma_addr = io_tlb_start + (index << IO_TLB_SHIFT); |
| 442 | |
| 443 | /* |
| 444 | * Update the indices to avoid searching in the next |
| 445 | * round. |
| 446 | */ |
| 447 | io_tlb_index = ((index + nslots) < io_tlb_nslabs |
| 448 | ? (index + nslots) : 0); |
| 449 | |
| 450 | goto found; |
| 451 | } |
| 452 | index += stride; |
| 453 | if (index >= io_tlb_nslabs) |
| 454 | index = 0; |
| 455 | } while (index != wrap); |
| 456 | |
| 457 | not_found: |
| 458 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
| 459 | return NULL; |
| 460 | found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
| 462 | |
| 463 | /* |
| 464 | * Save away the mapping from the original address to the DMA address. |
| 465 | * This is needed when we sync the memory. Then we sync the buffer if |
| 466 | * needed. |
| 467 | */ |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 468 | slot_buf = buffer; |
| 469 | for (i = 0; i < nslots; i++) { |
| 470 | slot_buf.page += slot_buf.offset >> PAGE_SHIFT; |
| 471 | slot_buf.offset &= PAGE_SIZE - 1; |
| 472 | io_tlb_orig_addr[index+i] = slot_buf; |
| 473 | slot_buf.offset += 1 << IO_TLB_SHIFT; |
| 474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) |
Jeremy Fitzhardinge | 1b548f6 | 2008-12-16 12:17:32 -0800 | [diff] [blame] | 476 | __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | return dma_addr; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * dma_addr is the kernel virtual address of the bounce buffer to unmap. |
| 483 | */ |
| 484 | static void |
| 485 | unmap_single(struct device *hwdev, char *dma_addr, size_t size, int dir) |
| 486 | { |
| 487 | unsigned long flags; |
| 488 | int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT; |
| 489 | int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT; |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 490 | struct swiotlb_phys_addr buffer = swiotlb_bus_to_phys_addr(dma_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * First, sync the memory before unmapping the entry |
| 494 | */ |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 495 | if ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | /* |
| 497 | * bounce... copy the data back into the original buffer * and |
| 498 | * delete the bounce buffer. |
| 499 | */ |
Jeremy Fitzhardinge | 1b548f6 | 2008-12-16 12:17:32 -0800 | [diff] [blame] | 500 | __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * Return the buffer to the free list by setting the corresponding |
| 504 | * entries to indicate the number of contigous entries available. |
| 505 | * While returning the entries to the free list, we merge the entries |
| 506 | * with slots below and above the pool being returned. |
| 507 | */ |
| 508 | spin_lock_irqsave(&io_tlb_lock, flags); |
| 509 | { |
| 510 | count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ? |
| 511 | io_tlb_list[index + nslots] : 0); |
| 512 | /* |
| 513 | * Step 1: return the slots to the free list, merging the |
| 514 | * slots with superceeding slots |
| 515 | */ |
| 516 | for (i = index + nslots - 1; i >= index; i--) |
| 517 | io_tlb_list[i] = ++count; |
| 518 | /* |
| 519 | * Step 2: merge the returned slots with the preceding slots, |
| 520 | * if available (non zero) |
| 521 | */ |
| 522 | for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--) |
| 523 | io_tlb_list[i] = ++count; |
| 524 | } |
| 525 | spin_unlock_irqrestore(&io_tlb_lock, flags); |
| 526 | } |
| 527 | |
| 528 | static void |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 529 | sync_single(struct device *hwdev, char *dma_addr, size_t size, |
| 530 | int dir, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 532 | struct swiotlb_phys_addr buffer = swiotlb_bus_to_phys_addr(dma_addr); |
Keir Fraser | df336d1 | 2007-07-21 04:37:24 -0700 | [diff] [blame] | 533 | |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 534 | switch (target) { |
| 535 | case SYNC_FOR_CPU: |
| 536 | if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Jeremy Fitzhardinge | 1b548f6 | 2008-12-16 12:17:32 -0800 | [diff] [blame] | 537 | __sync_single(buffer, dma_addr, size, DMA_FROM_DEVICE); |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 538 | else |
| 539 | BUG_ON(dir != DMA_TO_DEVICE); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 540 | break; |
| 541 | case SYNC_FOR_DEVICE: |
| 542 | if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)) |
Jeremy Fitzhardinge | 1b548f6 | 2008-12-16 12:17:32 -0800 | [diff] [blame] | 543 | __sync_single(buffer, dma_addr, size, DMA_TO_DEVICE); |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 544 | else |
| 545 | BUG_ON(dir != DMA_FROM_DEVICE); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 546 | break; |
| 547 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | BUG(); |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void * |
| 553 | swiotlb_alloc_coherent(struct device *hwdev, size_t size, |
Al Viro | 06a5449 | 2005-10-21 03:21:03 -0400 | [diff] [blame] | 554 | dma_addr_t *dma_handle, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 556 | dma_addr_t dev_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | void *ret; |
| 558 | int order = get_order(size); |
FUJITA Tomonori | 1e74f30 | 2008-11-17 16:24:34 +0900 | [diff] [blame] | 559 | u64 dma_mask = DMA_32BIT_MASK; |
| 560 | |
| 561 | if (hwdev && hwdev->coherent_dma_mask) |
| 562 | dma_mask = hwdev->coherent_dma_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 564 | ret = (void *)__get_free_pages(flags, order); |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 565 | if (ret && !is_buffer_dma_capable(dma_mask, swiotlb_virt_to_bus(ret), size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | /* |
| 567 | * The allocated memory isn't reachable by the device. |
| 568 | * Fall back on swiotlb_map_single(). |
| 569 | */ |
| 570 | free_pages((unsigned long) ret, order); |
| 571 | ret = NULL; |
| 572 | } |
| 573 | if (!ret) { |
| 574 | /* |
| 575 | * We are either out of memory or the device can't DMA |
| 576 | * to GFP_DMA memory; fall back on |
| 577 | * swiotlb_map_single(), which will grab memory from |
| 578 | * the lowest available address range. |
| 579 | */ |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 580 | struct swiotlb_phys_addr buffer; |
| 581 | buffer.page = virt_to_page(NULL); |
| 582 | buffer.offset = 0; |
| 583 | ret = map_single(hwdev, buffer, size, DMA_FROM_DEVICE); |
FUJITA Tomonori | 9dfda12 | 2008-09-08 18:53:48 +0900 | [diff] [blame] | 584 | if (!ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | memset(ret, 0, size); |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 589 | dev_addr = swiotlb_virt_to_bus(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | /* Confirm address can be DMA'd by device */ |
FUJITA Tomonori | 1e74f30 | 2008-11-17 16:24:34 +0900 | [diff] [blame] | 592 | if (!is_buffer_dma_capable(dma_mask, dev_addr, size)) { |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 593 | printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx\n", |
FUJITA Tomonori | 1e74f30 | 2008-11-17 16:24:34 +0900 | [diff] [blame] | 594 | (unsigned long long)dma_mask, |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 595 | (unsigned long long)dev_addr); |
FUJITA Tomonori | a2b89b5 | 2008-10-23 18:42:03 +0900 | [diff] [blame] | 596 | |
| 597 | /* DMA_TO_DEVICE to avoid memcpy in unmap_single */ |
| 598 | unmap_single(hwdev, ret, size, DMA_TO_DEVICE); |
| 599 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | } |
| 601 | *dma_handle = dev_addr; |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | void |
| 606 | swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr, |
| 607 | dma_addr_t dma_handle) |
| 608 | { |
David Brownell | aa24886 | 2007-08-10 13:10:27 -0700 | [diff] [blame] | 609 | WARN_ON(irqs_disabled()); |
FUJITA Tomonori | 640aebf | 2008-09-08 18:53:50 +0900 | [diff] [blame] | 610 | if (!is_swiotlb_buffer(vaddr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | free_pages((unsigned long) vaddr, get_order(size)); |
| 612 | else |
| 613 | /* DMA_TO_DEVICE to avoid memcpy in unmap_single */ |
FUJITA Tomonori | 21f6c4d | 2008-09-08 18:53:49 +0900 | [diff] [blame] | 614 | unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void |
| 618 | swiotlb_full(struct device *dev, size_t size, int dir, int do_panic) |
| 619 | { |
| 620 | /* |
| 621 | * Ran out of IOMMU space for this operation. This is very bad. |
| 622 | * Unfortunately the drivers cannot handle this operation properly. |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 623 | * unless they check for dma_mapping_error (most don't) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | * When the mapping is small enough return a static buffer to limit |
| 625 | * the damage, or panic when the transfer is too big. |
| 626 | */ |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 627 | printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | "device %s\n", size, dev ? dev->bus_id : "?"); |
| 629 | |
| 630 | if (size > io_tlb_overflow && do_panic) { |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 631 | if (dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 632 | panic("DMA: Memory would be corrupted\n"); |
| 633 | if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL) |
| 634 | panic("DMA: Random memory would be DMAed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Map a single buffer of the indicated size for DMA in streaming mode. The |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 640 | * physical address to use is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | * |
| 642 | * Once the device is given the dma address, the device owns this memory until |
| 643 | * either swiotlb_unmap_single or swiotlb_dma_sync_single is performed. |
| 644 | */ |
| 645 | dma_addr_t |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 646 | swiotlb_map_single_attrs(struct device *hwdev, void *ptr, size_t size, |
| 647 | int dir, struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | { |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 649 | dma_addr_t dev_addr = swiotlb_virt_to_bus(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | void *map; |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 651 | struct swiotlb_phys_addr buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 653 | BUG_ON(dir == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /* |
| 655 | * If the pointer passed in happens to be in the device's DMA window, |
| 656 | * we can safely return the device addr and not worry about bounce |
| 657 | * buffering it. |
| 658 | */ |
Ian Campbell | b81ea27 | 2008-12-16 12:17:31 -0800 | [diff] [blame] | 659 | if (!address_needs_mapping(hwdev, dev_addr, size) && |
| 660 | !range_needs_mapping(ptr, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | return dev_addr; |
| 662 | |
| 663 | /* |
| 664 | * Oh well, have to allocate and map a bounce buffer. |
| 665 | */ |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 666 | buffer.page = virt_to_page(ptr); |
| 667 | buffer.offset = (unsigned long)ptr & ~PAGE_MASK; |
| 668 | map = map_single(hwdev, buffer, size, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | if (!map) { |
| 670 | swiotlb_full(hwdev, size, dir, 1); |
| 671 | map = io_tlb_overflow_buffer; |
| 672 | } |
| 673 | |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 674 | dev_addr = swiotlb_virt_to_bus(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
| 676 | /* |
| 677 | * Ensure that the address returned is DMA'ble |
| 678 | */ |
FUJITA Tomonori | 2797982 | 2008-09-10 01:06:49 +0900 | [diff] [blame] | 679 | if (address_needs_mapping(hwdev, dev_addr, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | panic("map_single: bounce buffer is not DMA'ble"); |
| 681 | |
| 682 | return dev_addr; |
| 683 | } |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 684 | EXPORT_SYMBOL(swiotlb_map_single_attrs); |
| 685 | |
| 686 | dma_addr_t |
| 687 | swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir) |
| 688 | { |
| 689 | return swiotlb_map_single_attrs(hwdev, ptr, size, dir, NULL); |
| 690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
| 692 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | * Unmap a single streaming mode DMA translation. The dma_addr and size must |
| 694 | * match what was provided for in a previous swiotlb_map_single call. All |
| 695 | * other usages are undefined. |
| 696 | * |
| 697 | * After this call, reads by the cpu to the buffer are guaranteed to see |
| 698 | * whatever the device wrote there. |
| 699 | */ |
| 700 | void |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 701 | swiotlb_unmap_single_attrs(struct device *hwdev, dma_addr_t dev_addr, |
| 702 | size_t size, int dir, struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | { |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 704 | char *dma_addr = swiotlb_bus_to_virt(dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 706 | BUG_ON(dir == DMA_NONE); |
FUJITA Tomonori | 640aebf | 2008-09-08 18:53:50 +0900 | [diff] [blame] | 707 | if (is_swiotlb_buffer(dma_addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | unmap_single(hwdev, dma_addr, size, dir); |
| 709 | else if (dir == DMA_FROM_DEVICE) |
Jan Beulich | cde14bb | 2007-02-05 18:46:40 -0800 | [diff] [blame] | 710 | dma_mark_clean(dma_addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 712 | EXPORT_SYMBOL(swiotlb_unmap_single_attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 714 | void |
| 715 | swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size, |
| 716 | int dir) |
| 717 | { |
| 718 | return swiotlb_unmap_single_attrs(hwdev, dev_addr, size, dir, NULL); |
| 719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | /* |
| 721 | * Make physical memory consistent for a single streaming mode DMA translation |
| 722 | * after a transfer. |
| 723 | * |
| 724 | * If you perform a swiotlb_map_single() but wish to interrogate the buffer |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 725 | * using the cpu, yet do not wish to teardown the dma mapping, you must |
| 726 | * call this function before doing so. At the next point you give the dma |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | * address back to the card, you must first perform a |
| 728 | * swiotlb_dma_sync_for_device, and then the device again owns the buffer |
| 729 | */ |
Andrew Morton | be6b026 | 2007-02-12 00:52:17 -0800 | [diff] [blame] | 730 | static void |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 731 | swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr, |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 732 | size_t size, int dir, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | { |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 734 | char *dma_addr = swiotlb_bus_to_virt(dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 736 | BUG_ON(dir == DMA_NONE); |
FUJITA Tomonori | 640aebf | 2008-09-08 18:53:50 +0900 | [diff] [blame] | 737 | if (is_swiotlb_buffer(dma_addr)) |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 738 | sync_single(hwdev, dma_addr, size, dir, target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | else if (dir == DMA_FROM_DEVICE) |
Jan Beulich | cde14bb | 2007-02-05 18:46:40 -0800 | [diff] [blame] | 740 | dma_mark_clean(dma_addr, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | void |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 744 | swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr, |
| 745 | size_t size, int dir) |
| 746 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 747 | swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU); |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr, |
| 752 | size_t size, int dir) |
| 753 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 754 | swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 758 | * Same as above, but for a sub-range of the mapping. |
| 759 | */ |
Andrew Morton | be6b026 | 2007-02-12 00:52:17 -0800 | [diff] [blame] | 760 | static void |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 761 | swiotlb_sync_single_range(struct device *hwdev, dma_addr_t dev_addr, |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 762 | unsigned long offset, size_t size, |
| 763 | int dir, int target) |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 764 | { |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 765 | char *dma_addr = swiotlb_bus_to_virt(dev_addr) + offset; |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 766 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 767 | BUG_ON(dir == DMA_NONE); |
FUJITA Tomonori | 640aebf | 2008-09-08 18:53:50 +0900 | [diff] [blame] | 768 | if (is_swiotlb_buffer(dma_addr)) |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 769 | sync_single(hwdev, dma_addr, size, dir, target); |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 770 | else if (dir == DMA_FROM_DEVICE) |
Jan Beulich | cde14bb | 2007-02-05 18:46:40 -0800 | [diff] [blame] | 771 | dma_mark_clean(dma_addr, size); |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | void |
| 775 | swiotlb_sync_single_range_for_cpu(struct device *hwdev, dma_addr_t dev_addr, |
| 776 | unsigned long offset, size_t size, int dir) |
| 777 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 778 | swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir, |
| 779 | SYNC_FOR_CPU); |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | void |
| 783 | swiotlb_sync_single_range_for_device(struct device *hwdev, dma_addr_t dev_addr, |
| 784 | unsigned long offset, size_t size, int dir) |
| 785 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 786 | swiotlb_sync_single_range(hwdev, dev_addr, offset, size, dir, |
| 787 | SYNC_FOR_DEVICE); |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 790 | void swiotlb_unmap_sg_attrs(struct device *, struct scatterlist *, int, int, |
| 791 | struct dma_attrs *); |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 792 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | * Map a set of buffers described by scatterlist in streaming mode for DMA. |
| 794 | * This is the scatter-gather version of the above swiotlb_map_single |
| 795 | * interface. Here the scatter gather list elements are each tagged with the |
| 796 | * appropriate dma address and length. They are obtained via |
| 797 | * sg_dma_{address,length}(SG). |
| 798 | * |
| 799 | * NOTE: An implementation may be able to use a smaller number of |
| 800 | * DMA address/length pairs than there are SG table elements. |
| 801 | * (for example via virtual mapping capabilities) |
| 802 | * The routine returns the number of addr/length pairs actually |
| 803 | * used, at most nents. |
| 804 | * |
| 805 | * Device ownership issues as mentioned above for swiotlb_map_single are the |
| 806 | * same here. |
| 807 | */ |
| 808 | int |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 809 | swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems, |
| 810 | int dir, struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | { |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 812 | struct scatterlist *sg; |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 813 | struct swiotlb_phys_addr buffer; |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 814 | dma_addr_t dev_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | int i; |
| 816 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 817 | BUG_ON(dir == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 819 | for_each_sg(sgl, sg, nelems, i) { |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 820 | dev_addr = swiotlb_sg_to_bus(sg); |
Ian Campbell | b81ea27 | 2008-12-16 12:17:31 -0800 | [diff] [blame] | 821 | if (range_needs_mapping(sg_virt(sg), sg->length) || |
FUJITA Tomonori | 2797982 | 2008-09-10 01:06:49 +0900 | [diff] [blame] | 822 | address_needs_mapping(hwdev, dev_addr, sg->length)) { |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 823 | void *map; |
| 824 | buffer.page = sg_page(sg); |
| 825 | buffer.offset = sg->offset; |
| 826 | map = map_single(hwdev, buffer, sg->length, dir); |
Andi Kleen | 7e87023 | 2005-12-20 14:45:19 +0100 | [diff] [blame] | 827 | if (!map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | /* Don't panic here, we expect map_sg users |
| 829 | to do proper error handling. */ |
| 830 | swiotlb_full(hwdev, sg->length, dir, 0); |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 831 | swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir, |
| 832 | attrs); |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 833 | sgl[0].dma_length = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | return 0; |
| 835 | } |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 836 | sg->dma_address = swiotlb_virt_to_bus(map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } else |
| 838 | sg->dma_address = dev_addr; |
| 839 | sg->dma_length = sg->length; |
| 840 | } |
| 841 | return nelems; |
| 842 | } |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 843 | EXPORT_SYMBOL(swiotlb_map_sg_attrs); |
| 844 | |
| 845 | int |
| 846 | swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems, |
| 847 | int dir) |
| 848 | { |
| 849 | return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL); |
| 850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
| 852 | /* |
| 853 | * Unmap a set of streaming mode DMA translations. Again, cpu read rules |
| 854 | * concerning calls here are the same as for swiotlb_unmap_single() above. |
| 855 | */ |
| 856 | void |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 857 | swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl, |
| 858 | int nelems, int dir, struct dma_attrs *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | { |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 860 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | int i; |
| 862 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 863 | BUG_ON(dir == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 865 | for_each_sg(sgl, sg, nelems, i) { |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 866 | if (sg->dma_address != swiotlb_sg_to_bus(sg)) |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 867 | unmap_single(hwdev, swiotlb_bus_to_virt(sg->dma_address), |
Jan Beulich | 93fbff6 | 2007-02-05 18:49:45 -0800 | [diff] [blame] | 868 | sg->dma_length, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | else if (dir == DMA_FROM_DEVICE) |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 870 | dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length); |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 871 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | } |
Arthur Kepner | 309df0c | 2008-04-29 01:00:32 -0700 | [diff] [blame] | 873 | EXPORT_SYMBOL(swiotlb_unmap_sg_attrs); |
| 874 | |
| 875 | void |
| 876 | swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems, |
| 877 | int dir) |
| 878 | { |
| 879 | return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL); |
| 880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
| 882 | /* |
| 883 | * Make physical memory consistent for a set of streaming mode DMA translations |
| 884 | * after a transfer. |
| 885 | * |
| 886 | * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules |
| 887 | * and usage. |
| 888 | */ |
Andrew Morton | be6b026 | 2007-02-12 00:52:17 -0800 | [diff] [blame] | 889 | static void |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 890 | swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl, |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 891 | int nelems, int dir, int target) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | { |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 893 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | int i; |
| 895 | |
Eric Sesterhenn | 3481454 | 2006-03-24 18:47:11 +0100 | [diff] [blame] | 896 | BUG_ON(dir == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 898 | for_each_sg(sgl, sg, nelems, i) { |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 899 | if (sg->dma_address != swiotlb_sg_to_bus(sg)) |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 900 | sync_single(hwdev, swiotlb_bus_to_virt(sg->dma_address), |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 901 | sg->dma_length, dir, target); |
Jan Beulich | cde14bb | 2007-02-05 18:46:40 -0800 | [diff] [blame] | 902 | else if (dir == DMA_FROM_DEVICE) |
Jeremy Fitzhardinge | ef9b189 | 2008-12-16 12:17:33 -0800 | [diff] [blame^] | 903 | dma_mark_clean(swiotlb_bus_to_virt(sg->dma_address), sg->dma_length); |
Jens Axboe | dbfd49f | 2007-05-11 14:56:18 +0200 | [diff] [blame] | 904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | void |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 908 | swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg, |
| 909 | int nelems, int dir) |
| 910 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 911 | swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU); |
John W. Linville | 8270f3f | 2005-09-29 14:43:32 -0700 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg, |
| 916 | int nelems, int dir) |
| 917 | { |
John W. Linville | de69e0f | 2005-09-29 14:44:57 -0700 | [diff] [blame] | 918 | swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | int |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 922 | swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | { |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 924 | return (dma_addr == swiotlb_virt_to_bus(io_tlb_overflow_buffer)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /* |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 928 | * Return whether the given device DMA address mask can be supported |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | * properly. For example, if your device can only drive the low 24-bits |
Tony Luck | 17e5ad6 | 2005-09-29 15:52:13 -0700 | [diff] [blame] | 930 | * during bus mastering, then you would pass 0x00ffffff as the mask to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | * this function. |
| 932 | */ |
| 933 | int |
Jan Beulich | 563aaf0 | 2007-02-05 18:51:25 -0800 | [diff] [blame] | 934 | swiotlb_dma_supported(struct device *hwdev, u64 mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
Ian Campbell | e08e1f7 | 2008-12-16 12:17:30 -0800 | [diff] [blame] | 936 | return swiotlb_virt_to_bus(io_tlb_end - 1) <= mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | EXPORT_SYMBOL(swiotlb_map_single); |
| 940 | EXPORT_SYMBOL(swiotlb_unmap_single); |
| 941 | EXPORT_SYMBOL(swiotlb_map_sg); |
| 942 | EXPORT_SYMBOL(swiotlb_unmap_sg); |
| 943 | EXPORT_SYMBOL(swiotlb_sync_single_for_cpu); |
| 944 | EXPORT_SYMBOL(swiotlb_sync_single_for_device); |
John W. Linville | 878a97c | 2005-09-29 14:44:23 -0700 | [diff] [blame] | 945 | EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_cpu); |
| 946 | EXPORT_SYMBOL_GPL(swiotlb_sync_single_range_for_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu); |
| 948 | EXPORT_SYMBOL(swiotlb_sync_sg_for_device); |
| 949 | EXPORT_SYMBOL(swiotlb_dma_mapping_error); |
Tony Luck | 25667d6 | 2007-03-06 13:31:45 -0800 | [diff] [blame] | 950 | EXPORT_SYMBOL(swiotlb_alloc_coherent); |
| 951 | EXPORT_SYMBOL(swiotlb_free_coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | EXPORT_SYMBOL(swiotlb_dma_supported); |