Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * direct.c - Low-level direct PCI config space access |
| 3 | */ |
| 4 | |
| 5 | #include <linux/pci.h> |
| 6 | #include <linux/init.h> |
Andi Kleen | ec0f08e | 2006-04-07 19:49:36 +0200 | [diff] [blame] | 7 | #include <linux/dmi.h> |
Jaswinder Singh Rajput | 8248771 | 2008-12-27 18:32:28 +0530 | [diff] [blame] | 8 | #include <asm/pci_x86.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
| 10 | /* |
Robert Richter | 831d991 | 2007-09-03 10:17:39 +0200 | [diff] [blame] | 11 | * Functions for accessing PCI base (first 256 bytes) and extended |
| 12 | * (4096 bytes per PCI function) configuration space with type 1 |
| 13 | * accesses. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #define PCI_CONF1_ADDRESS(bus, devfn, reg) \ |
Robert Richter | 831d991 | 2007-09-03 10:17:39 +0200 | [diff] [blame] | 17 | (0x80000000 | ((reg & 0xF00) << 16) | (bus << 16) \ |
| 18 | | (devfn << 8) | (reg & 0xFC)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Matthew Wilcox | b6ce068 | 2008-02-10 09:45:28 -0500 | [diff] [blame] | 20 | static int pci_conf1_read(unsigned int seg, unsigned int bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | unsigned int devfn, int reg, int len, u32 *value) |
| 22 | { |
| 23 | unsigned long flags; |
| 24 | |
Robert Richter | 831d991 | 2007-09-03 10:17:39 +0200 | [diff] [blame] | 25 | if ((bus > 255) || (devfn > 255) || (reg > 4095)) { |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 26 | *value = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | return -EINVAL; |
Andi Kleen | 49c93e8 | 2006-04-07 19:50:15 +0200 | [diff] [blame] | 28 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Thomas Gleixner | d19f61f | 2010-02-17 14:35:25 +0000 | [diff] [blame] | 30 | raw_spin_lock_irqsave(&pci_config_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8); |
| 33 | |
| 34 | switch (len) { |
| 35 | case 1: |
| 36 | *value = inb(0xCFC + (reg & 3)); |
| 37 | break; |
| 38 | case 2: |
| 39 | *value = inw(0xCFC + (reg & 2)); |
| 40 | break; |
| 41 | case 4: |
| 42 | *value = inl(0xCFC); |
| 43 | break; |
| 44 | } |
| 45 | |
Thomas Gleixner | d19f61f | 2010-02-17 14:35:25 +0000 | [diff] [blame] | 46 | raw_spin_unlock_irqrestore(&pci_config_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
Matthew Wilcox | b6ce068 | 2008-02-10 09:45:28 -0500 | [diff] [blame] | 51 | static int pci_conf1_write(unsigned int seg, unsigned int bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | unsigned int devfn, int reg, int len, u32 value) |
| 53 | { |
| 54 | unsigned long flags; |
| 55 | |
Robert Richter | 831d991 | 2007-09-03 10:17:39 +0200 | [diff] [blame] | 56 | if ((bus > 255) || (devfn > 255) || (reg > 4095)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | return -EINVAL; |
| 58 | |
Thomas Gleixner | d19f61f | 2010-02-17 14:35:25 +0000 | [diff] [blame] | 59 | raw_spin_lock_irqsave(&pci_config_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | outl(PCI_CONF1_ADDRESS(bus, devfn, reg), 0xCF8); |
| 62 | |
| 63 | switch (len) { |
| 64 | case 1: |
| 65 | outb((u8)value, 0xCFC + (reg & 3)); |
| 66 | break; |
| 67 | case 2: |
| 68 | outw((u16)value, 0xCFC + (reg & 2)); |
| 69 | break; |
| 70 | case 4: |
| 71 | outl((u32)value, 0xCFC); |
| 72 | break; |
| 73 | } |
| 74 | |
Thomas Gleixner | d19f61f | 2010-02-17 14:35:25 +0000 | [diff] [blame] | 75 | raw_spin_unlock_irqrestore(&pci_config_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | #undef PCI_CONF1_ADDRESS |
| 81 | |
| 82 | struct pci_raw_ops pci_direct_conf1 = { |
| 83 | .read = pci_conf1_read, |
| 84 | .write = pci_conf1_write, |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | /* |
| 89 | * Functions for accessing PCI configuration space with type 2 accesses |
| 90 | */ |
| 91 | |
| 92 | #define PCI_CONF2_ADDRESS(dev, reg) (u16)(0xC000 | (dev << 8) | reg) |
| 93 | |
| 94 | static int pci_conf2_read(unsigned int seg, unsigned int bus, |
| 95 | unsigned int devfn, int reg, int len, u32 *value) |
| 96 | { |
| 97 | unsigned long flags; |
| 98 | int dev, fn; |
| 99 | |
Andi Kleen | ecc16ba | 2006-04-11 12:54:48 +0200 | [diff] [blame] | 100 | if ((bus > 255) || (devfn > 255) || (reg > 255)) { |
| 101 | *value = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return -EINVAL; |
Andi Kleen | ecc16ba | 2006-04-11 12:54:48 +0200 | [diff] [blame] | 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | dev = PCI_SLOT(devfn); |
| 106 | fn = PCI_FUNC(devfn); |
| 107 | |
| 108 | if (dev & 0x10) |
| 109 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 110 | |
Thomas Gleixner | d19f61f | 2010-02-17 14:35:25 +0000 | [diff] [blame] | 111 | raw_spin_lock_irqsave(&pci_config_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | outb((u8)(0xF0 | (fn << 1)), 0xCF8); |
| 114 | outb((u8)bus, 0xCFA); |
| 115 | |
| 116 | switch (len) { |
| 117 | case 1: |
| 118 | *value = inb(PCI_CONF2_ADDRESS(dev, reg)); |
| 119 | break; |
| 120 | case 2: |
| 121 | *value = inw(PCI_CONF2_ADDRESS(dev, reg)); |
| 122 | break; |
| 123 | case 4: |
| 124 | *value = inl(PCI_CONF2_ADDRESS(dev, reg)); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | outb(0, 0xCF8); |
| 129 | |
Thomas Gleixner | d19f61f | 2010-02-17 14:35:25 +0000 | [diff] [blame] | 130 | raw_spin_unlock_irqrestore(&pci_config_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int pci_conf2_write(unsigned int seg, unsigned int bus, |
| 136 | unsigned int devfn, int reg, int len, u32 value) |
| 137 | { |
| 138 | unsigned long flags; |
| 139 | int dev, fn; |
| 140 | |
| 141 | if ((bus > 255) || (devfn > 255) || (reg > 255)) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | dev = PCI_SLOT(devfn); |
| 145 | fn = PCI_FUNC(devfn); |
| 146 | |
| 147 | if (dev & 0x10) |
| 148 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 149 | |
Thomas Gleixner | d19f61f | 2010-02-17 14:35:25 +0000 | [diff] [blame] | 150 | raw_spin_lock_irqsave(&pci_config_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
| 152 | outb((u8)(0xF0 | (fn << 1)), 0xCF8); |
| 153 | outb((u8)bus, 0xCFA); |
| 154 | |
| 155 | switch (len) { |
| 156 | case 1: |
| 157 | outb((u8)value, PCI_CONF2_ADDRESS(dev, reg)); |
| 158 | break; |
| 159 | case 2: |
| 160 | outw((u16)value, PCI_CONF2_ADDRESS(dev, reg)); |
| 161 | break; |
| 162 | case 4: |
| 163 | outl((u32)value, PCI_CONF2_ADDRESS(dev, reg)); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | outb(0, 0xCF8); |
| 168 | |
Thomas Gleixner | d19f61f | 2010-02-17 14:35:25 +0000 | [diff] [blame] | 169 | raw_spin_unlock_irqrestore(&pci_config_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | #undef PCI_CONF2_ADDRESS |
| 175 | |
H. Peter Anvin | 14d7ca5 | 2008-11-11 16:19:48 -0800 | [diff] [blame] | 176 | struct pci_raw_ops pci_direct_conf2 = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | .read = pci_conf2_read, |
| 178 | .write = pci_conf2_write, |
| 179 | }; |
| 180 | |
| 181 | |
| 182 | /* |
| 183 | * Before we decide to use direct hardware access mechanisms, we try to do some |
| 184 | * trivial checks to ensure it at least _seems_ to be working -- we just test |
| 185 | * whether bus 00 contains a host bridge (this is similar to checking |
| 186 | * techniques used in XFree86, but ours should be more reliable since we |
| 187 | * attempt to make use of direct access hints provided by the PCI BIOS). |
| 188 | * |
| 189 | * This should be close to trivial, but it isn't, because there are buggy |
| 190 | * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID. |
| 191 | */ |
| 192 | static int __init pci_sanity_check(struct pci_raw_ops *o) |
| 193 | { |
| 194 | u32 x = 0; |
Tejun Heo | 3e5cd1f | 2009-08-16 21:02:36 +0900 | [diff] [blame] | 195 | int year, devfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | if (pci_probe & PCI_NO_CHECKS) |
| 198 | return 1; |
Andi Kleen | ec0f08e | 2006-04-07 19:49:36 +0200 | [diff] [blame] | 199 | /* Assume Type 1 works for newer systems. |
| 200 | This handles machines that don't have anything on PCI Bus 0. */ |
Tejun Heo | 3e5cd1f | 2009-08-16 21:02:36 +0900 | [diff] [blame] | 201 | dmi_get_date(DMI_BIOS_DATE, &year, NULL, NULL); |
| 202 | if (year >= 2001) |
Andi Kleen | ec0f08e | 2006-04-07 19:49:36 +0200 | [diff] [blame] | 203 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | for (devfn = 0; devfn < 0x100; devfn++) { |
| 206 | if (o->read(0, 0, devfn, PCI_CLASS_DEVICE, 2, &x)) |
| 207 | continue; |
| 208 | if (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA) |
| 209 | return 1; |
| 210 | |
| 211 | if (o->read(0, 0, devfn, PCI_VENDOR_ID, 2, &x)) |
| 212 | continue; |
| 213 | if (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ) |
| 214 | return 1; |
| 215 | } |
| 216 | |
Daniel Marjamäki | cac1a29 | 2005-11-23 15:45:09 -0800 | [diff] [blame] | 217 | DBG(KERN_WARNING "PCI: Sanity check failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int __init pci_check_type1(void) |
| 222 | { |
| 223 | unsigned long flags; |
| 224 | unsigned int tmp; |
| 225 | int works = 0; |
| 226 | |
| 227 | local_irq_save(flags); |
| 228 | |
| 229 | outb(0x01, 0xCFB); |
| 230 | tmp = inl(0xCF8); |
| 231 | outl(0x80000000, 0xCF8); |
| 232 | if (inl(0xCF8) == 0x80000000 && pci_sanity_check(&pci_direct_conf1)) { |
| 233 | works = 1; |
| 234 | } |
| 235 | outl(tmp, 0xCF8); |
| 236 | local_irq_restore(flags); |
| 237 | |
| 238 | return works; |
| 239 | } |
| 240 | |
| 241 | static int __init pci_check_type2(void) |
| 242 | { |
| 243 | unsigned long flags; |
| 244 | int works = 0; |
| 245 | |
| 246 | local_irq_save(flags); |
| 247 | |
| 248 | outb(0x00, 0xCFB); |
| 249 | outb(0x00, 0xCF8); |
| 250 | outb(0x00, 0xCFA); |
| 251 | if (inb(0xCF8) == 0x00 && inb(0xCFA) == 0x00 && |
| 252 | pci_sanity_check(&pci_direct_conf2)) { |
| 253 | works = 1; |
| 254 | } |
| 255 | |
| 256 | local_irq_restore(flags); |
| 257 | |
| 258 | return works; |
| 259 | } |
| 260 | |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 261 | void __init pci_direct_init(int type) |
| 262 | { |
Andi Kleen | f015c6c | 2006-10-05 18:47:22 +0200 | [diff] [blame] | 263 | if (type == 0) |
| 264 | return; |
Yinghai Lu | bb63b42 | 2008-02-28 23:56:50 -0800 | [diff] [blame] | 265 | printk(KERN_INFO "PCI: Using configuration type %d for base access\n", |
| 266 | type); |
Robert Richter | 831d991 | 2007-09-03 10:17:39 +0200 | [diff] [blame] | 267 | if (type == 1) { |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 268 | raw_pci_ops = &pci_direct_conf1; |
Robert Richter | 3a27dd1 | 2008-06-12 20:19:23 +0200 | [diff] [blame] | 269 | if (raw_pci_ext_ops) |
| 270 | return; |
| 271 | if (!(pci_probe & PCI_HAS_IO_ECS)) |
| 272 | return; |
| 273 | printk(KERN_INFO "PCI: Using configuration type 1 " |
| 274 | "for extended access\n"); |
| 275 | raw_pci_ext_ops = &pci_direct_conf1; |
| 276 | return; |
Robert Richter | 831d991 | 2007-09-03 10:17:39 +0200 | [diff] [blame] | 277 | } |
Robert Richter | 3a27dd1 | 2008-06-12 20:19:23 +0200 | [diff] [blame] | 278 | raw_pci_ops = &pci_direct_conf2; |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | int __init pci_direct_probe(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
| 283 | struct resource *region, *region2; |
| 284 | |
| 285 | if ((pci_probe & PCI_PROBE_CONF1) == 0) |
| 286 | goto type2; |
| 287 | region = request_region(0xCF8, 8, "PCI conf1"); |
| 288 | if (!region) |
| 289 | goto type2; |
| 290 | |
Yinghai Lu | bb63b42 | 2008-02-28 23:56:50 -0800 | [diff] [blame] | 291 | if (pci_check_type1()) { |
| 292 | raw_pci_ops = &pci_direct_conf1; |
H. Peter Anvin | 14d7ca5 | 2008-11-11 16:19:48 -0800 | [diff] [blame] | 293 | port_cf9_safe = true; |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 294 | return 1; |
Yinghai Lu | bb63b42 | 2008-02-28 23:56:50 -0800 | [diff] [blame] | 295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | release_resource(region); |
| 297 | |
| 298 | type2: |
| 299 | if ((pci_probe & PCI_PROBE_CONF2) == 0) |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 300 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | region = request_region(0xCF8, 4, "PCI conf2"); |
| 302 | if (!region) |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 303 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | region2 = request_region(0xC000, 0x1000, "PCI conf2"); |
| 305 | if (!region2) |
| 306 | goto fail2; |
| 307 | |
| 308 | if (pci_check_type2()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | raw_pci_ops = &pci_direct_conf2; |
H. Peter Anvin | 14d7ca5 | 2008-11-11 16:19:48 -0800 | [diff] [blame] | 310 | port_cf9_safe = true; |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 311 | return 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | release_resource(region2); |
| 315 | fail2: |
| 316 | release_resource(region); |
Andi Kleen | 5e544d6 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 317 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |