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