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