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