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