Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Dynamic DMA mapping support. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/string.h> |
| 8 | #include <linux/pci.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <asm/io.h> |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 11 | #include <asm/proto.h> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 12 | #include <asm/calgary.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 14 | int iommu_merge __read_mostly = 0; |
| 15 | EXPORT_SYMBOL(iommu_merge); |
| 16 | |
| 17 | dma_addr_t bad_dma_address __read_mostly; |
| 18 | EXPORT_SYMBOL(bad_dma_address); |
| 19 | |
| 20 | /* This tells the BIO block layer to assume merging. Default to off |
| 21 | because we cannot guarantee merging later. */ |
| 22 | int iommu_bio_merge __read_mostly = 0; |
| 23 | EXPORT_SYMBOL(iommu_bio_merge); |
| 24 | |
| 25 | int iommu_sac_force __read_mostly = 0; |
| 26 | EXPORT_SYMBOL(iommu_sac_force); |
| 27 | |
| 28 | int no_iommu __read_mostly; |
| 29 | #ifdef CONFIG_IOMMU_DEBUG |
| 30 | int panic_on_overflow __read_mostly = 1; |
| 31 | int force_iommu __read_mostly = 1; |
| 32 | #else |
| 33 | int panic_on_overflow __read_mostly = 0; |
| 34 | int force_iommu __read_mostly= 0; |
| 35 | #endif |
| 36 | |
Jon Mason | 8d4f6b9 | 2006-06-26 13:58:05 +0200 | [diff] [blame] | 37 | /* Set this to 1 if there is a HW IOMMU in the system */ |
| 38 | int iommu_detected __read_mostly = 0; |
| 39 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 40 | /* Dummy device used for NULL arguments (normally ISA). Better would |
| 41 | be probably a smaller DMA mask, but this is bug-to-bug compatible |
| 42 | to i386. */ |
| 43 | struct device fallback_dev = { |
| 44 | .bus_id = "fallback device", |
Jon Mason | 9f2036f | 2006-06-26 13:56:19 +0200 | [diff] [blame] | 45 | .coherent_dma_mask = DMA_32BIT_MASK, |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 46 | .dma_mask = &fallback_dev.coherent_dma_mask, |
| 47 | }; |
| 48 | |
| 49 | /* Allocate DMA memory on node near device */ |
| 50 | noinline static void * |
| 51 | dma_alloc_pages(struct device *dev, gfp_t gfp, unsigned order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 53 | struct page *page; |
| 54 | int node; |
Andi Kleen | fa47dd0 | 2006-04-07 19:49:33 +0200 | [diff] [blame] | 55 | #ifdef CONFIG_PCI |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 56 | if (dev->bus == &pci_bus_type) |
| 57 | node = pcibus_to_node(to_pci_dev(dev)->bus); |
| 58 | else |
Andi Kleen | fa47dd0 | 2006-04-07 19:49:33 +0200 | [diff] [blame] | 59 | #endif |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 60 | node = numa_node_id(); |
Daniel Yeisley | 0d01532 | 2006-05-30 22:47:57 +0200 | [diff] [blame] | 61 | |
| 62 | if (node < first_node(node_online_map)) |
| 63 | node = first_node(node_online_map); |
| 64 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 65 | page = alloc_pages_node(node, gfp, order); |
| 66 | return page ? page_address(page) : NULL; |
| 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 69 | /* |
| 70 | * Allocate memory for a coherent mapping. |
| 71 | */ |
| 72 | void * |
| 73 | dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, |
| 74 | gfp_t gfp) |
| 75 | { |
| 76 | void *memory; |
| 77 | unsigned long dma_mask = 0; |
| 78 | u64 bus; |
| 79 | |
| 80 | if (!dev) |
| 81 | dev = &fallback_dev; |
| 82 | dma_mask = dev->coherent_dma_mask; |
| 83 | if (dma_mask == 0) |
Jon Mason | 9f2036f | 2006-06-26 13:56:19 +0200 | [diff] [blame] | 84 | dma_mask = DMA_32BIT_MASK; |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 85 | |
Andi Kleen | 3056d6b | 2006-03-25 16:30:43 +0100 | [diff] [blame] | 86 | /* Don't invoke OOM killer */ |
| 87 | gfp |= __GFP_NORETRY; |
| 88 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 89 | /* Kludge to make it bug-to-bug compatible with i386. i386 |
| 90 | uses the normal dma_mask for alloc_coherent. */ |
| 91 | dma_mask &= *dev->dma_mask; |
| 92 | |
| 93 | /* Why <=? Even when the mask is smaller than 4GB it is often |
| 94 | larger than 16MB and in this case we have a chance of |
| 95 | finding fitting memory in the next higher zone first. If |
| 96 | not retry with true GFP_DMA. -AK */ |
Jon Mason | 9f2036f | 2006-06-26 13:56:19 +0200 | [diff] [blame] | 97 | if (dma_mask <= DMA_32BIT_MASK) |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 98 | gfp |= GFP_DMA32; |
| 99 | |
| 100 | again: |
| 101 | memory = dma_alloc_pages(dev, gfp, get_order(size)); |
| 102 | if (memory == NULL) |
| 103 | return NULL; |
| 104 | |
| 105 | { |
| 106 | int high, mmu; |
| 107 | bus = virt_to_bus(memory); |
| 108 | high = (bus + size) >= dma_mask; |
| 109 | mmu = high; |
| 110 | if (force_iommu && !(gfp & GFP_DMA)) |
| 111 | mmu = 1; |
| 112 | else if (high) { |
| 113 | free_pages((unsigned long)memory, |
| 114 | get_order(size)); |
| 115 | |
| 116 | /* Don't use the 16MB ZONE_DMA unless absolutely |
| 117 | needed. It's better to use remapping first. */ |
Jon Mason | 9f2036f | 2006-06-26 13:56:19 +0200 | [diff] [blame] | 118 | if (dma_mask < DMA_32BIT_MASK && !(gfp & GFP_DMA)) { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 119 | gfp = (gfp & ~GFP_DMA32) | GFP_DMA; |
| 120 | goto again; |
| 121 | } |
| 122 | |
Andi Kleen | 6bca52b | 2006-02-03 21:50:59 +0100 | [diff] [blame] | 123 | /* Let low level make its own zone decisions */ |
| 124 | gfp &= ~(GFP_DMA32|GFP_DMA); |
| 125 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 126 | if (dma_ops->alloc_coherent) |
| 127 | return dma_ops->alloc_coherent(dev, size, |
| 128 | dma_handle, gfp); |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | memset(memory, 0, size); |
| 133 | if (!mmu) { |
| 134 | *dma_handle = virt_to_bus(memory); |
| 135 | return memory; |
| 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 138 | |
| 139 | if (dma_ops->alloc_coherent) { |
| 140 | free_pages((unsigned long)memory, get_order(size)); |
| 141 | gfp &= ~(GFP_DMA|GFP_DMA32); |
| 142 | return dma_ops->alloc_coherent(dev, size, dma_handle, gfp); |
| 143 | } |
| 144 | |
| 145 | if (dma_ops->map_simple) { |
| 146 | *dma_handle = dma_ops->map_simple(dev, memory, |
| 147 | size, |
| 148 | PCI_DMA_BIDIRECTIONAL); |
| 149 | if (*dma_handle != bad_dma_address) |
| 150 | return memory; |
| 151 | } |
| 152 | |
| 153 | if (panic_on_overflow) |
| 154 | panic("dma_alloc_coherent: IOMMU overflow by %lu bytes\n",size); |
| 155 | free_pages((unsigned long)memory, get_order(size)); |
| 156 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 158 | EXPORT_SYMBOL(dma_alloc_coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 160 | /* |
| 161 | * Unmap coherent memory. |
| 162 | * The caller must ensure that the device has finished accessing the mapping. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | */ |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 164 | void dma_free_coherent(struct device *dev, size_t size, |
| 165 | void *vaddr, dma_addr_t bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 167 | if (dma_ops->unmap_single) |
| 168 | dma_ops->unmap_single(dev, bus, size, 0); |
| 169 | free_pages((unsigned long)vaddr, get_order(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 171 | EXPORT_SYMBOL(dma_free_coherent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Andi Kleen | ece6684 | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 173 | static int forbid_dac __read_mostly; |
| 174 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 175 | int dma_supported(struct device *dev, u64 mask) |
| 176 | { |
Andi Kleen | ece6684 | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 177 | #ifdef CONFIG_PCI |
| 178 | if (mask > 0xffffffff && forbid_dac > 0) { |
| 179 | |
| 180 | |
| 181 | |
| 182 | printk(KERN_INFO "PCI: Disallowing DAC for device %s\n", dev->bus_id); |
| 183 | return 0; |
| 184 | } |
| 185 | #endif |
| 186 | |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 187 | if (dma_ops->dma_supported) |
| 188 | return dma_ops->dma_supported(dev, mask); |
| 189 | |
| 190 | /* Copied from i386. Doesn't make much sense, because it will |
| 191 | only work for pci_alloc_coherent. |
| 192 | The caller just has to use GFP_DMA in this case. */ |
Jon Mason | 9f2036f | 2006-06-26 13:56:19 +0200 | [diff] [blame] | 193 | if (mask < DMA_24BIT_MASK) |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 194 | return 0; |
| 195 | |
| 196 | /* Tell the device to use SAC when IOMMU force is on. This |
| 197 | allows the driver to use cheaper accesses in some cases. |
| 198 | |
| 199 | Problem with this is that if we overflow the IOMMU area and |
| 200 | return DAC as fallback address the device may not handle it |
| 201 | correctly. |
| 202 | |
| 203 | As a special case some controllers have a 39bit address |
| 204 | mode that is as efficient as 32bit (aic79xx). Don't force |
| 205 | SAC for these. Assume all masks <= 40 bits are of this |
| 206 | type. Normally this doesn't make any difference, but gives |
| 207 | more gentle handling of IOMMU overflow. */ |
Jon Mason | 9f2036f | 2006-06-26 13:56:19 +0200 | [diff] [blame] | 208 | if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) { |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 209 | printk(KERN_INFO "%s: Force SAC with mask %Lx\n", dev->bus_id,mask); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | return 1; |
| 214 | } |
| 215 | EXPORT_SYMBOL(dma_supported); |
| 216 | |
| 217 | int dma_set_mask(struct device *dev, u64 mask) |
| 218 | { |
| 219 | if (!dev->dma_mask || !dma_supported(dev, mask)) |
| 220 | return -EIO; |
| 221 | *dev->dma_mask = mask; |
| 222 | return 0; |
| 223 | } |
| 224 | EXPORT_SYMBOL(dma_set_mask); |
| 225 | |
Karsten Weiss | 5558870 | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 226 | /* |
| 227 | * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter |
| 228 | * documentation. |
| 229 | */ |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 230 | __init int iommu_setup(char *p) |
| 231 | { |
Andi Kleen | ded318e | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 232 | iommu_merge = 1; |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 233 | |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 234 | if (!p) |
| 235 | return -EINVAL; |
| 236 | |
Andi Kleen | ded318e | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 237 | while (*p) { |
| 238 | if (!strncmp(p,"off",3)) |
| 239 | no_iommu = 1; |
| 240 | /* gart_parse_options has more force support */ |
| 241 | if (!strncmp(p,"force",5)) |
| 242 | force_iommu = 1; |
| 243 | if (!strncmp(p,"noforce",7)) { |
| 244 | iommu_merge = 0; |
| 245 | force_iommu = 0; |
| 246 | } |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 247 | |
Andi Kleen | ded318e | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 248 | if (!strncmp(p, "biomerge",8)) { |
| 249 | iommu_bio_merge = 4096; |
| 250 | iommu_merge = 1; |
| 251 | force_iommu = 1; |
| 252 | } |
| 253 | if (!strncmp(p, "panic",5)) |
| 254 | panic_on_overflow = 1; |
| 255 | if (!strncmp(p, "nopanic",7)) |
| 256 | panic_on_overflow = 0; |
| 257 | if (!strncmp(p, "merge",5)) { |
| 258 | iommu_merge = 1; |
| 259 | force_iommu = 1; |
| 260 | } |
| 261 | if (!strncmp(p, "nomerge",7)) |
| 262 | iommu_merge = 0; |
| 263 | if (!strncmp(p, "forcesac",8)) |
| 264 | iommu_sac_force = 1; |
| 265 | if (!strncmp(p, "allowdac", 8)) |
| 266 | forbid_dac = 0; |
| 267 | if (!strncmp(p, "nodac", 5)) |
| 268 | forbid_dac = -1; |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 269 | |
| 270 | #ifdef CONFIG_SWIOTLB |
Andi Kleen | ded318e | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 271 | if (!strncmp(p, "soft",4)) |
| 272 | swiotlb = 1; |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 273 | #endif |
| 274 | |
Andi Kleen | a813ce4 | 2006-06-26 13:57:22 +0200 | [diff] [blame] | 275 | #ifdef CONFIG_IOMMU |
Andi Kleen | ded318e | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 276 | gart_parse_options(p); |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 277 | #endif |
| 278 | |
Muli Ben-Yehuda | bff6547 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 279 | #ifdef CONFIG_CALGARY_IOMMU |
| 280 | if (!strncmp(p, "calgary", 7)) |
| 281 | use_calgary = 1; |
| 282 | #endif /* CONFIG_CALGARY_IOMMU */ |
| 283 | |
Andi Kleen | ded318e | 2006-09-30 01:47:55 +0200 | [diff] [blame] | 284 | p += strcspn(p, ","); |
| 285 | if (*p == ',') |
| 286 | ++p; |
| 287 | } |
| 288 | return 0; |
Muli Ben-Yehuda | 17a941d | 2006-01-11 22:44:42 +0100 | [diff] [blame] | 289 | } |
Andi Kleen | 2c8c0e6 | 2006-09-26 10:52:32 +0200 | [diff] [blame] | 290 | early_param("iommu", iommu_setup); |
Jon Mason | 0dc243a | 2006-06-26 13:58:11 +0200 | [diff] [blame] | 291 | |
| 292 | void __init pci_iommu_alloc(void) |
| 293 | { |
| 294 | /* |
| 295 | * The order of these functions is important for |
| 296 | * fall-back/fail-over reasons |
| 297 | */ |
| 298 | #ifdef CONFIG_IOMMU |
| 299 | iommu_hole_init(); |
| 300 | #endif |
| 301 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 302 | #ifdef CONFIG_CALGARY_IOMMU |
| 303 | detect_calgary(); |
| 304 | #endif |
| 305 | |
Jon Mason | 0dc243a | 2006-06-26 13:58:11 +0200 | [diff] [blame] | 306 | #ifdef CONFIG_SWIOTLB |
| 307 | pci_swiotlb_init(); |
| 308 | #endif |
| 309 | } |
| 310 | |
| 311 | static int __init pci_iommu_init(void) |
| 312 | { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 313 | #ifdef CONFIG_CALGARY_IOMMU |
| 314 | calgary_iommu_init(); |
| 315 | #endif |
| 316 | |
Jon Mason | 0dc243a | 2006-06-26 13:58:11 +0200 | [diff] [blame] | 317 | #ifdef CONFIG_IOMMU |
| 318 | gart_iommu_init(); |
| 319 | #endif |
| 320 | |
| 321 | no_iommu_init(); |
| 322 | return 0; |
| 323 | } |
| 324 | |
Andi Kleen | 388c19e | 2007-06-20 12:23:32 +0200 | [diff] [blame^] | 325 | #ifdef CONFIG_PCI |
| 326 | /* Many VIA bridges seem to corrupt data for DAC. Disable it here */ |
| 327 | |
| 328 | static __devinit void via_no_dac(struct pci_dev *dev) |
| 329 | { |
| 330 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) { |
| 331 | printk(KERN_INFO "PCI: VIA PCI bridge detected. Disabling DAC.\n"); |
| 332 | forbid_dac = 1; |
| 333 | } |
| 334 | } |
| 335 | DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac); |
| 336 | #endif |
Jon Mason | 0dc243a | 2006-06-26 13:58:11 +0200 | [diff] [blame] | 337 | /* Must execute after PCI subsystem */ |
| 338 | fs_initcall(pci_iommu_init); |