Glauber Costa | 459121c9 | 2008-04-08 13:20:43 -0300 | [diff] [blame] | 1 | #include <linux/dma-mapping.h> |
Glauber Costa | cb5867a | 2008-04-08 13:20:51 -0300 | [diff] [blame] | 2 | #include <linux/dmar.h> |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 3 | #include <linux/bootmem.h> |
Glauber Costa | bca5c09 | 2008-04-08 13:20:53 -0300 | [diff] [blame] | 4 | #include <linux/pci.h> |
Glauber Costa | cb5867a | 2008-04-08 13:20:51 -0300 | [diff] [blame] | 5 | |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 6 | #include <asm/proto.h> |
| 7 | #include <asm/dma.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 8 | #include <asm/iommu.h> |
Joerg Roedel | 1d9b16d | 2008-11-27 18:39:15 +0100 | [diff] [blame^] | 9 | #include <asm/gart.h> |
Glauber Costa | cb5867a | 2008-04-08 13:20:51 -0300 | [diff] [blame] | 10 | #include <asm/calgary.h> |
Joerg Roedel | a69ca34 | 2008-06-26 21:28:08 +0200 | [diff] [blame] | 11 | #include <asm/amd_iommu.h> |
Glauber Costa | 459121c9 | 2008-04-08 13:20:43 -0300 | [diff] [blame] | 12 | |
Fenghua Yu | 3b15e58 | 2008-10-23 16:51:00 -0700 | [diff] [blame] | 13 | static int forbid_dac __read_mostly; |
| 14 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 15 | struct dma_mapping_ops *dma_ops; |
Glauber Costa | 85c246e | 2008-04-08 13:20:50 -0300 | [diff] [blame] | 16 | EXPORT_SYMBOL(dma_ops); |
| 17 | |
Dmitri Vorobiev | b4cdc43 | 2008-04-28 03:15:58 +0400 | [diff] [blame] | 18 | static int iommu_sac_force __read_mostly; |
Glauber Costa | 8e0c379 | 2008-04-08 13:20:55 -0300 | [diff] [blame] | 19 | |
Glauber Costa | f9c258d | 2008-04-08 13:20:52 -0300 | [diff] [blame] | 20 | #ifdef CONFIG_IOMMU_DEBUG |
| 21 | int panic_on_overflow __read_mostly = 1; |
| 22 | int force_iommu __read_mostly = 1; |
| 23 | #else |
| 24 | int panic_on_overflow __read_mostly = 0; |
| 25 | int force_iommu __read_mostly = 0; |
| 26 | #endif |
| 27 | |
Glauber Costa | fae9a0d | 2008-04-08 13:20:56 -0300 | [diff] [blame] | 28 | int iommu_merge __read_mostly = 0; |
| 29 | |
| 30 | int no_iommu __read_mostly; |
| 31 | /* Set this to 1 if there is a HW IOMMU in the system */ |
| 32 | int iommu_detected __read_mostly = 0; |
| 33 | |
| 34 | /* This tells the BIO block layer to assume merging. Default to off |
| 35 | because we cannot guarantee merging later. */ |
| 36 | int iommu_bio_merge __read_mostly = 0; |
| 37 | EXPORT_SYMBOL(iommu_bio_merge); |
| 38 | |
Glauber Costa | cac6787 | 2008-04-08 13:21:00 -0300 | [diff] [blame] | 39 | dma_addr_t bad_dma_address __read_mostly = 0; |
| 40 | EXPORT_SYMBOL(bad_dma_address); |
Glauber Costa | fae9a0d | 2008-04-08 13:20:56 -0300 | [diff] [blame] | 41 | |
Glauber Costa | 098cb7f | 2008-04-09 13:18:10 -0300 | [diff] [blame] | 42 | /* Dummy device used for NULL arguments (normally ISA). Better would |
| 43 | be probably a smaller DMA mask, but this is bug-to-bug compatible |
| 44 | to older i386. */ |
Joerg Roedel | 6c505ce | 2008-08-19 16:32:45 +0200 | [diff] [blame] | 45 | struct device x86_dma_fallback_dev = { |
Glauber Costa | 098cb7f | 2008-04-09 13:18:10 -0300 | [diff] [blame] | 46 | .bus_id = "fallback device", |
| 47 | .coherent_dma_mask = DMA_32BIT_MASK, |
Joerg Roedel | 6c505ce | 2008-08-19 16:32:45 +0200 | [diff] [blame] | 48 | .dma_mask = &x86_dma_fallback_dev.coherent_dma_mask, |
Glauber Costa | 098cb7f | 2008-04-09 13:18:10 -0300 | [diff] [blame] | 49 | }; |
Joerg Roedel | 6c505ce | 2008-08-19 16:32:45 +0200 | [diff] [blame] | 50 | EXPORT_SYMBOL(x86_dma_fallback_dev); |
Glauber Costa | 098cb7f | 2008-04-09 13:18:10 -0300 | [diff] [blame] | 51 | |
Glauber Costa | 459121c9 | 2008-04-08 13:20:43 -0300 | [diff] [blame] | 52 | int dma_set_mask(struct device *dev, u64 mask) |
| 53 | { |
| 54 | if (!dev->dma_mask || !dma_supported(dev, mask)) |
| 55 | return -EIO; |
| 56 | |
| 57 | *dev->dma_mask = mask; |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | EXPORT_SYMBOL(dma_set_mask); |
| 62 | |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 63 | #ifdef CONFIG_X86_64 |
| 64 | static __initdata void *dma32_bootmem_ptr; |
| 65 | static unsigned long dma32_bootmem_size __initdata = (128ULL<<20); |
| 66 | |
| 67 | static int __init parse_dma32_size_opt(char *p) |
| 68 | { |
| 69 | if (!p) |
| 70 | return -EINVAL; |
| 71 | dma32_bootmem_size = memparse(p, &p); |
| 72 | return 0; |
| 73 | } |
| 74 | early_param("dma32_size", parse_dma32_size_opt); |
| 75 | |
| 76 | void __init dma32_reserve_bootmem(void) |
| 77 | { |
| 78 | unsigned long size, align; |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 79 | if (max_pfn <= MAX_DMA32_PFN) |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 80 | return; |
| 81 | |
Yinghai Lu | 7677b2e | 2008-04-14 20:40:37 -0700 | [diff] [blame] | 82 | /* |
| 83 | * check aperture_64.c allocate_aperture() for reason about |
| 84 | * using 512M as goal |
| 85 | */ |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 86 | align = 64ULL<<20; |
Joerg Roedel | 1ddb551 | 2008-07-25 16:48:55 +0200 | [diff] [blame] | 87 | size = roundup(dma32_bootmem_size, align); |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 88 | dma32_bootmem_ptr = __alloc_bootmem_nopanic(size, align, |
Yinghai Lu | 7677b2e | 2008-04-14 20:40:37 -0700 | [diff] [blame] | 89 | 512ULL<<20); |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 90 | if (dma32_bootmem_ptr) |
| 91 | dma32_bootmem_size = size; |
| 92 | else |
| 93 | dma32_bootmem_size = 0; |
| 94 | } |
| 95 | static void __init dma32_free_bootmem(void) |
| 96 | { |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 97 | |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 98 | if (max_pfn <= MAX_DMA32_PFN) |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 99 | return; |
| 100 | |
| 101 | if (!dma32_bootmem_ptr) |
| 102 | return; |
| 103 | |
Yinghai Lu | 330fce2 | 2008-04-19 01:31:45 -0700 | [diff] [blame] | 104 | free_bootmem(__pa(dma32_bootmem_ptr), dma32_bootmem_size); |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 105 | |
| 106 | dma32_bootmem_ptr = NULL; |
| 107 | dma32_bootmem_size = 0; |
| 108 | } |
| 109 | |
| 110 | void __init pci_iommu_alloc(void) |
| 111 | { |
| 112 | /* free the range so iommu could get some range less than 4G */ |
| 113 | dma32_free_bootmem(); |
| 114 | /* |
| 115 | * The order of these functions is important for |
| 116 | * fall-back/fail-over reasons |
| 117 | */ |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 118 | gart_iommu_hole_init(); |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 119 | |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 120 | detect_calgary(); |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 121 | |
| 122 | detect_intel_iommu(); |
| 123 | |
Joerg Roedel | a69ca34 | 2008-06-26 21:28:08 +0200 | [diff] [blame] | 124 | amd_iommu_detect(); |
| 125 | |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 126 | pci_swiotlb_init(); |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 127 | } |
FUJITA Tomonori | 8978b74 | 2008-07-29 13:38:53 +0900 | [diff] [blame] | 128 | |
Joerg Roedel | bdab0ba | 2008-10-15 22:02:07 -0700 | [diff] [blame] | 129 | unsigned long iommu_nr_pages(unsigned long addr, unsigned long len) |
FUJITA Tomonori | 8978b74 | 2008-07-29 13:38:53 +0900 | [diff] [blame] | 130 | { |
| 131 | unsigned long size = roundup((addr & ~PAGE_MASK) + len, PAGE_SIZE); |
| 132 | |
| 133 | return size >> PAGE_SHIFT; |
| 134 | } |
Joerg Roedel | bdab0ba | 2008-10-15 22:02:07 -0700 | [diff] [blame] | 135 | EXPORT_SYMBOL(iommu_nr_pages); |
Glauber Costa | 116890d | 2008-04-08 13:20:54 -0300 | [diff] [blame] | 136 | #endif |
| 137 | |
FUJITA Tomonori | 9f6ac57 | 2008-09-24 20:48:35 +0900 | [diff] [blame] | 138 | void *dma_generic_alloc_coherent(struct device *dev, size_t size, |
| 139 | dma_addr_t *dma_addr, gfp_t flag) |
| 140 | { |
| 141 | unsigned long dma_mask; |
| 142 | struct page *page; |
| 143 | dma_addr_t addr; |
| 144 | |
| 145 | dma_mask = dma_alloc_coherent_mask(dev, flag); |
| 146 | |
| 147 | flag |= __GFP_ZERO; |
| 148 | again: |
| 149 | page = alloc_pages_node(dev_to_node(dev), flag, get_order(size)); |
| 150 | if (!page) |
| 151 | return NULL; |
| 152 | |
| 153 | addr = page_to_phys(page); |
| 154 | if (!is_buffer_dma_capable(dma_mask, addr, size)) { |
| 155 | __free_pages(page, get_order(size)); |
| 156 | |
| 157 | if (dma_mask < DMA_32BIT_MASK && !(flag & GFP_DMA)) { |
| 158 | flag = (flag & ~GFP_DMA32) | GFP_DMA; |
| 159 | goto again; |
| 160 | } |
| 161 | |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | *dma_addr = addr; |
| 166 | return page_address(page); |
| 167 | } |
| 168 | |
Glauber Costa | fae9a0d | 2008-04-08 13:20:56 -0300 | [diff] [blame] | 169 | /* |
| 170 | * See <Documentation/x86_64/boot-options.txt> for the iommu kernel parameter |
| 171 | * documentation. |
| 172 | */ |
| 173 | static __init int iommu_setup(char *p) |
| 174 | { |
| 175 | iommu_merge = 1; |
| 176 | |
| 177 | if (!p) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | while (*p) { |
| 181 | if (!strncmp(p, "off", 3)) |
| 182 | no_iommu = 1; |
| 183 | /* gart_parse_options has more force support */ |
| 184 | if (!strncmp(p, "force", 5)) |
| 185 | force_iommu = 1; |
| 186 | if (!strncmp(p, "noforce", 7)) { |
| 187 | iommu_merge = 0; |
| 188 | force_iommu = 0; |
| 189 | } |
| 190 | |
| 191 | if (!strncmp(p, "biomerge", 8)) { |
| 192 | iommu_bio_merge = 4096; |
| 193 | iommu_merge = 1; |
| 194 | force_iommu = 1; |
| 195 | } |
| 196 | if (!strncmp(p, "panic", 5)) |
| 197 | panic_on_overflow = 1; |
| 198 | if (!strncmp(p, "nopanic", 7)) |
| 199 | panic_on_overflow = 0; |
| 200 | if (!strncmp(p, "merge", 5)) { |
| 201 | iommu_merge = 1; |
| 202 | force_iommu = 1; |
| 203 | } |
| 204 | if (!strncmp(p, "nomerge", 7)) |
| 205 | iommu_merge = 0; |
| 206 | if (!strncmp(p, "forcesac", 8)) |
| 207 | iommu_sac_force = 1; |
| 208 | if (!strncmp(p, "allowdac", 8)) |
| 209 | forbid_dac = 0; |
| 210 | if (!strncmp(p, "nodac", 5)) |
| 211 | forbid_dac = -1; |
| 212 | if (!strncmp(p, "usedac", 6)) { |
| 213 | forbid_dac = -1; |
| 214 | return 1; |
| 215 | } |
| 216 | #ifdef CONFIG_SWIOTLB |
| 217 | if (!strncmp(p, "soft", 4)) |
| 218 | swiotlb = 1; |
| 219 | #endif |
| 220 | |
Glauber Costa | fae9a0d | 2008-04-08 13:20:56 -0300 | [diff] [blame] | 221 | gart_parse_options(p); |
Glauber Costa | fae9a0d | 2008-04-08 13:20:56 -0300 | [diff] [blame] | 222 | |
| 223 | #ifdef CONFIG_CALGARY_IOMMU |
| 224 | if (!strncmp(p, "calgary", 7)) |
| 225 | use_calgary = 1; |
| 226 | #endif /* CONFIG_CALGARY_IOMMU */ |
| 227 | |
| 228 | p += strcspn(p, ","); |
| 229 | if (*p == ',') |
| 230 | ++p; |
| 231 | } |
| 232 | return 0; |
| 233 | } |
| 234 | early_param("iommu", iommu_setup); |
| 235 | |
Glauber Costa | 8e0c379 | 2008-04-08 13:20:55 -0300 | [diff] [blame] | 236 | int dma_supported(struct device *dev, u64 mask) |
| 237 | { |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 238 | struct dma_mapping_ops *ops = get_dma_ops(dev); |
| 239 | |
Glauber Costa | 8e0c379 | 2008-04-08 13:20:55 -0300 | [diff] [blame] | 240 | #ifdef CONFIG_PCI |
| 241 | if (mask > 0xffffffff && forbid_dac > 0) { |
Greg Kroah-Hartman | fc3a882 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 242 | dev_info(dev, "PCI: Disallowing DAC for device\n"); |
Glauber Costa | 8e0c379 | 2008-04-08 13:20:55 -0300 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | #endif |
| 246 | |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 247 | if (ops->dma_supported) |
| 248 | return ops->dma_supported(dev, mask); |
Glauber Costa | 8e0c379 | 2008-04-08 13:20:55 -0300 | [diff] [blame] | 249 | |
| 250 | /* Copied from i386. Doesn't make much sense, because it will |
| 251 | only work for pci_alloc_coherent. |
| 252 | The caller just has to use GFP_DMA in this case. */ |
| 253 | if (mask < DMA_24BIT_MASK) |
| 254 | return 0; |
| 255 | |
| 256 | /* Tell the device to use SAC when IOMMU force is on. This |
| 257 | allows the driver to use cheaper accesses in some cases. |
| 258 | |
| 259 | Problem with this is that if we overflow the IOMMU area and |
| 260 | return DAC as fallback address the device may not handle it |
| 261 | correctly. |
| 262 | |
| 263 | As a special case some controllers have a 39bit address |
| 264 | mode that is as efficient as 32bit (aic79xx). Don't force |
| 265 | SAC for these. Assume all masks <= 40 bits are of this |
| 266 | type. Normally this doesn't make any difference, but gives |
| 267 | more gentle handling of IOMMU overflow. */ |
| 268 | if (iommu_sac_force && (mask >= DMA_40BIT_MASK)) { |
Greg Kroah-Hartman | fc3a882 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 269 | dev_info(dev, "Force SAC with mask %Lx\n", mask); |
Glauber Costa | 8e0c379 | 2008-04-08 13:20:55 -0300 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | return 1; |
| 274 | } |
| 275 | EXPORT_SYMBOL(dma_supported); |
| 276 | |
Glauber Costa | cb5867a | 2008-04-08 13:20:51 -0300 | [diff] [blame] | 277 | static int __init pci_iommu_init(void) |
| 278 | { |
Glauber Costa | cb5867a | 2008-04-08 13:20:51 -0300 | [diff] [blame] | 279 | calgary_iommu_init(); |
Glauber Costa | 459121c9 | 2008-04-08 13:20:43 -0300 | [diff] [blame] | 280 | |
Glauber Costa | cb5867a | 2008-04-08 13:20:51 -0300 | [diff] [blame] | 281 | intel_iommu_init(); |
| 282 | |
Joerg Roedel | a69ca34 | 2008-06-26 21:28:08 +0200 | [diff] [blame] | 283 | amd_iommu_init(); |
| 284 | |
Glauber Costa | cb5867a | 2008-04-08 13:20:51 -0300 | [diff] [blame] | 285 | gart_iommu_init(); |
Glauber Costa | cb5867a | 2008-04-08 13:20:51 -0300 | [diff] [blame] | 286 | |
| 287 | no_iommu_init(); |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | void pci_iommu_shutdown(void) |
| 292 | { |
| 293 | gart_iommu_shutdown(); |
| 294 | } |
| 295 | /* Must execute after PCI subsystem */ |
| 296 | fs_initcall(pci_iommu_init); |
Fenghua Yu | 3b15e58 | 2008-10-23 16:51:00 -0700 | [diff] [blame] | 297 | |
| 298 | #ifdef CONFIG_PCI |
| 299 | /* Many VIA bridges seem to corrupt data for DAC. Disable it here */ |
| 300 | |
| 301 | static __devinit void via_no_dac(struct pci_dev *dev) |
| 302 | { |
| 303 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI && forbid_dac == 0) { |
| 304 | printk(KERN_INFO "PCI: VIA PCI bridge detected." |
| 305 | "Disabling DAC.\n"); |
| 306 | forbid_dac = 1; |
| 307 | } |
| 308 | } |
| 309 | DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_VIA, PCI_ANY_ID, via_no_dac); |
| 310 | #endif |