blob: 7c4d75e3c5f3dc4e207af3ee5c7c30ddda72c022 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Low-Level PCI Support for PC
3 *
4 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
5 */
6
7#include <linux/sched.h>
8#include <linux/pci.h>
9#include <linux/ioport.h>
10#include <linux/init.h>
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -080011#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include <asm/acpi.h>
14#include <asm/segment.h>
15#include <asm/io.h>
16#include <asm/smp.h>
17
18#include "pci.h"
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |
21 PCI_PROBE_MMCONF;
22
Adrian Bunk2b290da2006-11-16 13:16:23 +010023static int pci_bf_sort;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024int pci_routeirq;
25int pcibios_last_bus = -1;
jayalk@intworks.biz120bb422005-03-21 20:20:42 -080026unsigned long pirq_table_addr;
27struct pci_bus *pci_root_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028struct pci_raw_ops *raw_pci_ops;
29
30static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
31{
32 return raw_pci_ops->read(0, bus->number, devfn, where, size, value);
33}
34
35static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
36{
37 return raw_pci_ops->write(0, bus->number, devfn, where, size, value);
38}
39
40struct pci_ops pci_root_ops = {
41 .read = pci_read,
42 .write = pci_write,
43};
44
45/*
46 * legacy, numa, and acpi all want to call pcibios_scan_root
47 * from their initcalls. This flag prevents that.
48 */
49int pcibios_scanned;
50
51/*
52 * This interrupt-safe spinlock protects all accesses to PCI
53 * configuration space.
54 */
55DEFINE_SPINLOCK(pci_config_lock);
56
57/*
58 * Several buggy motherboards address only 16 devices and mirror
59 * them to next 16 IDs. We try to detect this `feature' on all
60 * primary buses (those containing host bridges as they are
61 * expected to be unique) and remove the ghost devices.
62 */
63
64static void __devinit pcibios_fixup_ghosts(struct pci_bus *b)
65{
66 struct list_head *ln, *mn;
67 struct pci_dev *d, *e;
68 int mirror = PCI_DEVFN(16,0);
69 int seen_host_bridge = 0;
70 int i;
71
72 DBG("PCI: Scanning for ghost devices on bus %d\n", b->number);
73 list_for_each(ln, &b->devices) {
74 d = pci_dev_b(ln);
75 if ((d->class >> 8) == PCI_CLASS_BRIDGE_HOST)
76 seen_host_bridge++;
77 for (mn=ln->next; mn != &b->devices; mn=mn->next) {
78 e = pci_dev_b(mn);
79 if (e->devfn != d->devfn + mirror ||
80 e->vendor != d->vendor ||
81 e->device != d->device ||
82 e->class != d->class)
83 continue;
84 for(i=0; i<PCI_NUM_RESOURCES; i++)
85 if (e->resource[i].start != d->resource[i].start ||
86 e->resource[i].end != d->resource[i].end ||
87 e->resource[i].flags != d->resource[i].flags)
88 continue;
89 break;
90 }
91 if (mn == &b->devices)
92 return;
93 }
94 if (!seen_host_bridge)
95 return;
96 printk(KERN_WARNING "PCI: Ignoring ghost devices on bus %02x\n", b->number);
97
98 ln = &b->devices;
99 while (ln->next != &b->devices) {
100 d = pci_dev_b(ln->next);
101 if (d->devfn >= mirror) {
102 list_del(&d->global_list);
103 list_del(&d->bus_list);
104 kfree(d);
105 } else
106 ln = ln->next;
107 }
108}
109
110/*
111 * Called after each bus is probed, but before its children
112 * are examined.
113 */
114
115void __devinit pcibios_fixup_bus(struct pci_bus *b)
116{
117 pcibios_fixup_ghosts(b);
118 pci_read_bridge_bases(b);
119}
120
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800121/*
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500122 * Only use DMI information to set this if nothing was passed
123 * on the kernel command line (which was parsed earlier).
124 */
125
Jeff Garzik18552562007-10-03 15:15:40 -0400126static int __devinit set_bf_sort(const struct dmi_system_id *d)
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500127{
128 if (pci_bf_sort == pci_bf_sort_default) {
129 pci_bf_sort = pci_dmi_bf;
130 printk(KERN_INFO "PCI: %s detected, enabling pci=bfsort.\n", d->ident);
131 }
132 return 0;
133}
134
135/*
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800136 * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
137 */
138#ifdef __i386__
Jeff Garzik18552562007-10-03 15:15:40 -0400139static int __devinit assign_all_busses(const struct dmi_system_id *d)
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800140{
141 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
142 printk(KERN_INFO "%s detected: enabling PCI bus# renumbering"
143 " (pci=assign-busses)\n", d->ident);
144 return 0;
145}
146#endif
147
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500148static struct dmi_system_id __devinitdata pciprobe_dmi_table[] = {
149#ifdef __i386__
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800150/*
151 * Laptops which need pci=assign-busses to see Cardbus cards
152 */
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800153 {
154 .callback = assign_all_busses,
155 .ident = "Samsung X20 Laptop",
156 .matches = {
157 DMI_MATCH(DMI_SYS_VENDOR, "Samsung Electronics"),
158 DMI_MATCH(DMI_PRODUCT_NAME, "SX20S"),
159 },
160 },
161#endif /* __i386__ */
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500162 {
163 .callback = set_bf_sort,
164 .ident = "Dell PowerEdge 1950",
165 .matches = {
166 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
167 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1950"),
168 },
169 },
170 {
171 .callback = set_bf_sort,
172 .ident = "Dell PowerEdge 1955",
173 .matches = {
174 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
175 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1955"),
176 },
177 },
178 {
179 .callback = set_bf_sort,
180 .ident = "Dell PowerEdge 2900",
181 .matches = {
182 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
183 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2900"),
184 },
185 },
186 {
187 .callback = set_bf_sort,
188 .ident = "Dell PowerEdge 2950",
189 .matches = {
190 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
191 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2950"),
192 },
193 },
Andy Gospodarekf52383d2007-02-05 16:36:10 -0800194 {
195 .callback = set_bf_sort,
Matt Domschf7a9dae2007-03-23 23:58:07 -0500196 .ident = "Dell PowerEdge R900",
197 .matches = {
198 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
199 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge R900"),
200 },
201 },
202 {
203 .callback = set_bf_sort,
Andy Gospodarekf52383d2007-02-05 16:36:10 -0800204 .ident = "HP ProLiant BL20p G3",
205 .matches = {
206 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
207 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G3"),
208 },
209 },
210 {
211 .callback = set_bf_sort,
212 .ident = "HP ProLiant BL20p G4",
213 .matches = {
214 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
215 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G4"),
216 },
217 },
218 {
219 .callback = set_bf_sort,
220 .ident = "HP ProLiant BL30p G1",
221 .matches = {
222 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
223 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL30p G1"),
224 },
225 },
226 {
227 .callback = set_bf_sort,
228 .ident = "HP ProLiant BL25p G1",
229 .matches = {
230 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
231 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL25p G1"),
232 },
233 },
234 {
235 .callback = set_bf_sort,
236 .ident = "HP ProLiant BL35p G1",
237 .matches = {
238 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
239 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL35p G1"),
240 },
241 },
242 {
243 .callback = set_bf_sort,
244 .ident = "HP ProLiant BL45p G1",
245 .matches = {
246 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
247 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G1"),
248 },
249 },
250 {
251 .callback = set_bf_sort,
252 .ident = "HP ProLiant BL45p G2",
253 .matches = {
254 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
255 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G2"),
256 },
257 },
258 {
259 .callback = set_bf_sort,
260 .ident = "HP ProLiant BL460c G1",
261 .matches = {
262 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
263 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL460c G1"),
264 },
265 },
266 {
267 .callback = set_bf_sort,
268 .ident = "HP ProLiant BL465c G1",
269 .matches = {
270 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
271 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL465c G1"),
272 },
273 },
274 {
275 .callback = set_bf_sort,
276 .ident = "HP ProLiant BL480c G1",
277 .matches = {
278 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
279 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL480c G1"),
280 },
281 },
282 {
283 .callback = set_bf_sort,
284 .ident = "HP ProLiant BL685c G1",
285 .matches = {
286 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
287 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL685c G1"),
288 },
289 },
Juha Laiho5b1ea822007-09-13 21:21:34 +0300290#ifdef __i386__
291 {
292 .callback = assign_all_busses,
293 .ident = "Compaq EVO N800c",
294 .matches = {
295 DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
296 DMI_MATCH(DMI_PRODUCT_NAME, "EVO N800c"),
297 },
298 },
299#endif
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800300 {}
301};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303struct pci_bus * __devinit pcibios_scan_root(int busnum)
304{
305 struct pci_bus *bus = NULL;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300306 struct pci_sysdata *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800308 dmi_check_system(pciprobe_dmi_table);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 while ((bus = pci_find_next_bus(bus)) != NULL) {
311 if (bus->number == busnum) {
312 /* Already scanned */
313 return bus;
314 }
315 }
316
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300317 /* Allocate per-root-bus (not per bus) arch-specific data.
318 * TODO: leak; this memory is never freed.
319 * It's arguable whether it's worth the trouble to care.
320 */
321 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
322 if (!sd) {
323 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
324 return NULL;
325 }
326
Daniel Marjamäkiadcb89072005-11-23 15:44:49 -0800327 printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300329 return pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332extern u8 pci_cache_line_size;
333
334static int __init pcibios_init(void)
335{
336 struct cpuinfo_x86 *c = &boot_cpu_data;
337
338 if (!raw_pci_ops) {
Daniel Marjamäkiadcb89072005-11-23 15:44:49 -0800339 printk(KERN_WARNING "PCI: System does not support PCI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341 }
342
343 /*
344 * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
345 * and P4. It's also good for 386/486s (which actually have 16)
346 * as quite a few PCI devices do not support smaller values.
347 */
348 pci_cache_line_size = 32 >> 2;
349 if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
350 pci_cache_line_size = 64 >> 2; /* K7 & K8 */
351 else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
352 pci_cache_line_size = 128 >> 2; /* P4 */
353
354 pcibios_resource_survey();
355
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500356 if (pci_bf_sort >= pci_force_bf)
357 pci_sort_breadthfirst();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#ifdef CONFIG_PCI_BIOS
359 if ((pci_probe & PCI_BIOS_SORT) && !(pci_probe & PCI_NO_SORT))
360 pcibios_sort();
361#endif
362 return 0;
363}
364
365subsys_initcall(pcibios_init);
366
367char * __devinit pcibios_setup(char *str)
368{
369 if (!strcmp(str, "off")) {
370 pci_probe = 0;
371 return NULL;
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500372 } else if (!strcmp(str, "bfsort")) {
373 pci_bf_sort = pci_force_bf;
374 return NULL;
375 } else if (!strcmp(str, "nobfsort")) {
376 pci_bf_sort = pci_force_nobf;
377 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379#ifdef CONFIG_PCI_BIOS
380 else if (!strcmp(str, "bios")) {
381 pci_probe = PCI_PROBE_BIOS;
382 return NULL;
383 } else if (!strcmp(str, "nobios")) {
384 pci_probe &= ~PCI_PROBE_BIOS;
385 return NULL;
386 } else if (!strcmp(str, "nosort")) {
387 pci_probe |= PCI_NO_SORT;
388 return NULL;
389 } else if (!strcmp(str, "biosirq")) {
390 pci_probe |= PCI_BIOS_IRQ_SCAN;
391 return NULL;
jayalk@intworks.biz120bb422005-03-21 20:20:42 -0800392 } else if (!strncmp(str, "pirqaddr=", 9)) {
393 pirq_table_addr = simple_strtoul(str+9, NULL, 0);
394 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396#endif
397#ifdef CONFIG_PCI_DIRECT
398 else if (!strcmp(str, "conf1")) {
399 pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
400 return NULL;
401 }
402 else if (!strcmp(str, "conf2")) {
403 pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
404 return NULL;
405 }
406#endif
407#ifdef CONFIG_PCI_MMCONFIG
408 else if (!strcmp(str, "nommconf")) {
409 pci_probe &= ~PCI_PROBE_MMCONF;
410 return NULL;
411 }
412#endif
413 else if (!strcmp(str, "noacpi")) {
414 acpi_noirq_set();
415 return NULL;
416 }
Andi Kleen0637a702006-09-26 10:52:41 +0200417 else if (!strcmp(str, "noearly")) {
418 pci_probe |= PCI_PROBE_NOEARLY;
419 return NULL;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#ifndef CONFIG_X86_VISWS
422 else if (!strcmp(str, "usepirqmask")) {
423 pci_probe |= PCI_USE_PIRQ_MASK;
424 return NULL;
425 } else if (!strncmp(str, "irqmask=", 8)) {
426 pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
427 return NULL;
428 } else if (!strncmp(str, "lastbus=", 8)) {
429 pcibios_last_bus = simple_strtol(str+8, NULL, 0);
430 return NULL;
431 }
432#endif
433 else if (!strcmp(str, "rom")) {
434 pci_probe |= PCI_ASSIGN_ROMS;
435 return NULL;
436 } else if (!strcmp(str, "assign-busses")) {
437 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
438 return NULL;
439 } else if (!strcmp(str, "routeirq")) {
440 pci_routeirq = 1;
441 return NULL;
442 }
443 return str;
444}
445
446unsigned int pcibios_assign_all_busses(void)
447{
448 return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
449}
450
451int pcibios_enable_device(struct pci_dev *dev, int mask)
452{
453 int err;
454
455 if ((err = pcibios_enable_resources(dev, mask)) < 0)
456 return err;
457
Eric W. Biedermanbba6f6f2007-03-28 15:36:09 +0200458 if (!dev->msi_enabled)
459 return pcibios_enable_irq(dev);
460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
David Shaohua Li87bec662005-07-27 23:02:00 -0400462
463void pcibios_disable_device (struct pci_dev *dev)
464{
Eric W. Biedermanbba6f6f2007-03-28 15:36:09 +0200465 if (!dev->msi_enabled && pcibios_disable_irq)
David Shaohua Li87bec662005-07-27 23:02:00 -0400466 pcibios_disable_irq(dev);
467}
Muli Ben-Yehuda73c59af2007-08-10 13:01:19 -0700468
469struct pci_bus *pci_scan_bus_with_sysdata(int busno)
470{
471 struct pci_bus *bus = NULL;
472 struct pci_sysdata *sd;
473
474 /*
475 * Allocate per-root-bus (not per bus) arch-specific data.
476 * TODO: leak; this memory is never freed.
477 * It's arguable whether it's worth the trouble to care.
478 */
479 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
480 if (!sd) {
481 printk(KERN_ERR "PCI: OOM, skipping PCI bus %02x\n", busno);
482 return NULL;
483 }
484 sd->node = -1;
485 bus = pci_scan_bus(busno, &pci_root_ops, sd);
486 if (!bus)
487 kfree(sd);
488
489 return bus;
490}