blob: 862746390666daa6da34942889321d6eebcbe9b2 [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{
Jeff Garzika79e4192007-10-11 16:58:30 -040032 return raw_pci_ops->read(pci_domain_nr(bus), bus->number,
33 devfn, where, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
36static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
37{
Jeff Garzika79e4192007-10-11 16:58:30 -040038 return raw_pci_ops->write(pci_domain_nr(bus), bus->number,
39 devfn, where, size, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42struct pci_ops pci_root_ops = {
43 .read = pci_read,
44 .write = pci_write,
45};
46
47/*
48 * legacy, numa, and acpi all want to call pcibios_scan_root
49 * from their initcalls. This flag prevents that.
50 */
51int pcibios_scanned;
52
53/*
54 * This interrupt-safe spinlock protects all accesses to PCI
55 * configuration space.
56 */
57DEFINE_SPINLOCK(pci_config_lock);
58
59/*
60 * Several buggy motherboards address only 16 devices and mirror
61 * them to next 16 IDs. We try to detect this `feature' on all
62 * primary buses (those containing host bridges as they are
63 * expected to be unique) and remove the ghost devices.
64 */
65
66static void __devinit pcibios_fixup_ghosts(struct pci_bus *b)
67{
68 struct list_head *ln, *mn;
69 struct pci_dev *d, *e;
70 int mirror = PCI_DEVFN(16,0);
71 int seen_host_bridge = 0;
72 int i;
73
74 DBG("PCI: Scanning for ghost devices on bus %d\n", b->number);
75 list_for_each(ln, &b->devices) {
76 d = pci_dev_b(ln);
77 if ((d->class >> 8) == PCI_CLASS_BRIDGE_HOST)
78 seen_host_bridge++;
79 for (mn=ln->next; mn != &b->devices; mn=mn->next) {
80 e = pci_dev_b(mn);
81 if (e->devfn != d->devfn + mirror ||
82 e->vendor != d->vendor ||
83 e->device != d->device ||
84 e->class != d->class)
85 continue;
86 for(i=0; i<PCI_NUM_RESOURCES; i++)
87 if (e->resource[i].start != d->resource[i].start ||
88 e->resource[i].end != d->resource[i].end ||
89 e->resource[i].flags != d->resource[i].flags)
90 continue;
91 break;
92 }
93 if (mn == &b->devices)
94 return;
95 }
96 if (!seen_host_bridge)
97 return;
98 printk(KERN_WARNING "PCI: Ignoring ghost devices on bus %02x\n", b->number);
99
100 ln = &b->devices;
101 while (ln->next != &b->devices) {
102 d = pci_dev_b(ln->next);
103 if (d->devfn >= mirror) {
104 list_del(&d->global_list);
105 list_del(&d->bus_list);
106 kfree(d);
107 } else
108 ln = ln->next;
109 }
110}
111
112/*
113 * Called after each bus is probed, but before its children
114 * are examined.
115 */
116
117void __devinit pcibios_fixup_bus(struct pci_bus *b)
118{
119 pcibios_fixup_ghosts(b);
120 pci_read_bridge_bases(b);
121}
122
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800123/*
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500124 * Only use DMI information to set this if nothing was passed
125 * on the kernel command line (which was parsed earlier).
126 */
127
Jeff Garzik18552562007-10-03 15:15:40 -0400128static int __devinit set_bf_sort(const struct dmi_system_id *d)
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500129{
130 if (pci_bf_sort == pci_bf_sort_default) {
131 pci_bf_sort = pci_dmi_bf;
132 printk(KERN_INFO "PCI: %s detected, enabling pci=bfsort.\n", d->ident);
133 }
134 return 0;
135}
136
137/*
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800138 * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
139 */
140#ifdef __i386__
Jeff Garzik18552562007-10-03 15:15:40 -0400141static int __devinit assign_all_busses(const struct dmi_system_id *d)
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800142{
143 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
144 printk(KERN_INFO "%s detected: enabling PCI bus# renumbering"
145 " (pci=assign-busses)\n", d->ident);
146 return 0;
147}
148#endif
149
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500150static struct dmi_system_id __devinitdata pciprobe_dmi_table[] = {
151#ifdef __i386__
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800152/*
153 * Laptops which need pci=assign-busses to see Cardbus cards
154 */
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800155 {
156 .callback = assign_all_busses,
157 .ident = "Samsung X20 Laptop",
158 .matches = {
159 DMI_MATCH(DMI_SYS_VENDOR, "Samsung Electronics"),
160 DMI_MATCH(DMI_PRODUCT_NAME, "SX20S"),
161 },
162 },
163#endif /* __i386__ */
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500164 {
165 .callback = set_bf_sort,
166 .ident = "Dell PowerEdge 1950",
167 .matches = {
168 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
169 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1950"),
170 },
171 },
172 {
173 .callback = set_bf_sort,
174 .ident = "Dell PowerEdge 1955",
175 .matches = {
176 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
177 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1955"),
178 },
179 },
180 {
181 .callback = set_bf_sort,
182 .ident = "Dell PowerEdge 2900",
183 .matches = {
184 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
185 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2900"),
186 },
187 },
188 {
189 .callback = set_bf_sort,
190 .ident = "Dell PowerEdge 2950",
191 .matches = {
192 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
193 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2950"),
194 },
195 },
Andy Gospodarekf52383d2007-02-05 16:36:10 -0800196 {
197 .callback = set_bf_sort,
Matt Domschf7a9dae2007-03-23 23:58:07 -0500198 .ident = "Dell PowerEdge R900",
199 .matches = {
200 DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
201 DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge R900"),
202 },
203 },
204 {
205 .callback = set_bf_sort,
Andy Gospodarekf52383d2007-02-05 16:36:10 -0800206 .ident = "HP ProLiant BL20p G3",
207 .matches = {
208 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
209 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G3"),
210 },
211 },
212 {
213 .callback = set_bf_sort,
214 .ident = "HP ProLiant BL20p G4",
215 .matches = {
216 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
217 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G4"),
218 },
219 },
220 {
221 .callback = set_bf_sort,
222 .ident = "HP ProLiant BL30p G1",
223 .matches = {
224 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
225 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL30p G1"),
226 },
227 },
228 {
229 .callback = set_bf_sort,
230 .ident = "HP ProLiant BL25p G1",
231 .matches = {
232 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
233 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL25p G1"),
234 },
235 },
236 {
237 .callback = set_bf_sort,
238 .ident = "HP ProLiant BL35p G1",
239 .matches = {
240 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
241 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL35p G1"),
242 },
243 },
244 {
245 .callback = set_bf_sort,
246 .ident = "HP ProLiant BL45p G1",
247 .matches = {
248 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
249 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G1"),
250 },
251 },
252 {
253 .callback = set_bf_sort,
254 .ident = "HP ProLiant BL45p G2",
255 .matches = {
256 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
257 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G2"),
258 },
259 },
260 {
261 .callback = set_bf_sort,
262 .ident = "HP ProLiant BL460c G1",
263 .matches = {
264 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
265 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL460c G1"),
266 },
267 },
268 {
269 .callback = set_bf_sort,
270 .ident = "HP ProLiant BL465c G1",
271 .matches = {
272 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
273 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL465c G1"),
274 },
275 },
276 {
277 .callback = set_bf_sort,
278 .ident = "HP ProLiant BL480c G1",
279 .matches = {
280 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
281 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL480c G1"),
282 },
283 },
284 {
285 .callback = set_bf_sort,
286 .ident = "HP ProLiant BL685c G1",
287 .matches = {
288 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
289 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL685c G1"),
290 },
291 },
Michal Schmidt8f8ae1a2007-10-17 18:04:35 +0200292 {
293 .callback = set_bf_sort,
294 .ident = "HP ProLiant DL385 G2",
295 .matches = {
296 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
297 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
298 },
299 },
300 {
301 .callback = set_bf_sort,
302 .ident = "HP ProLiant DL585 G2",
303 .matches = {
304 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
305 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
306 },
307 },
Juha Laiho5b1ea822007-09-13 21:21:34 +0300308#ifdef __i386__
309 {
310 .callback = assign_all_busses,
311 .ident = "Compaq EVO N800c",
312 .matches = {
313 DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
314 DMI_MATCH(DMI_PRODUCT_NAME, "EVO N800c"),
315 },
316 },
317#endif
Michal Schmidtc82bc5a2007-11-26 20:42:19 +0100318 {
319 .callback = set_bf_sort,
320 .ident = "HP ProLiant DL385 G2",
321 .matches = {
322 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
323 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
324 },
325 },
326 {
327 .callback = set_bf_sort,
328 .ident = "HP ProLiant DL585 G2",
329 .matches = {
330 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
331 DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
332 },
333 },
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800334 {}
335};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337struct pci_bus * __devinit pcibios_scan_root(int busnum)
338{
339 struct pci_bus *bus = NULL;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300340 struct pci_sysdata *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Bernhard Kaindl8c4b2cf2006-02-18 01:36:55 -0800342 dmi_check_system(pciprobe_dmi_table);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 while ((bus = pci_find_next_bus(bus)) != NULL) {
345 if (bus->number == busnum) {
346 /* Already scanned */
347 return bus;
348 }
349 }
350
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300351 /* Allocate per-root-bus (not per bus) arch-specific data.
352 * TODO: leak; this memory is never freed.
353 * It's arguable whether it's worth the trouble to care.
354 */
355 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
356 if (!sd) {
357 printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
358 return NULL;
359 }
360
Daniel Marjamäkiadcb89072005-11-23 15:44:49 -0800361 printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300363 return pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366extern u8 pci_cache_line_size;
367
368static int __init pcibios_init(void)
369{
370 struct cpuinfo_x86 *c = &boot_cpu_data;
371
372 if (!raw_pci_ops) {
Daniel Marjamäkiadcb89072005-11-23 15:44:49 -0800373 printk(KERN_WARNING "PCI: System does not support PCI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 0;
375 }
376
377 /*
378 * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
379 * and P4. It's also good for 386/486s (which actually have 16)
380 * as quite a few PCI devices do not support smaller values.
381 */
382 pci_cache_line_size = 32 >> 2;
383 if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
384 pci_cache_line_size = 64 >> 2; /* K7 & K8 */
385 else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
386 pci_cache_line_size = 128 >> 2; /* P4 */
387
388 pcibios_resource_survey();
389
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500390 if (pci_bf_sort >= pci_force_bf)
391 pci_sort_breadthfirst();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#ifdef CONFIG_PCI_BIOS
393 if ((pci_probe & PCI_BIOS_SORT) && !(pci_probe & PCI_NO_SORT))
394 pcibios_sort();
395#endif
396 return 0;
397}
398
399subsys_initcall(pcibios_init);
400
401char * __devinit pcibios_setup(char *str)
402{
403 if (!strcmp(str, "off")) {
404 pci_probe = 0;
405 return NULL;
Matt Domsch6b4b78f2006-09-29 15:23:23 -0500406 } else if (!strcmp(str, "bfsort")) {
407 pci_bf_sort = pci_force_bf;
408 return NULL;
409 } else if (!strcmp(str, "nobfsort")) {
410 pci_bf_sort = pci_force_nobf;
411 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413#ifdef CONFIG_PCI_BIOS
414 else if (!strcmp(str, "bios")) {
415 pci_probe = PCI_PROBE_BIOS;
416 return NULL;
417 } else if (!strcmp(str, "nobios")) {
418 pci_probe &= ~PCI_PROBE_BIOS;
419 return NULL;
420 } else if (!strcmp(str, "nosort")) {
421 pci_probe |= PCI_NO_SORT;
422 return NULL;
423 } else if (!strcmp(str, "biosirq")) {
424 pci_probe |= PCI_BIOS_IRQ_SCAN;
425 return NULL;
jayalk@intworks.biz120bb422005-03-21 20:20:42 -0800426 } else if (!strncmp(str, "pirqaddr=", 9)) {
427 pirq_table_addr = simple_strtoul(str+9, NULL, 0);
428 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430#endif
431#ifdef CONFIG_PCI_DIRECT
432 else if (!strcmp(str, "conf1")) {
433 pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
434 return NULL;
435 }
436 else if (!strcmp(str, "conf2")) {
437 pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
438 return NULL;
439 }
440#endif
441#ifdef CONFIG_PCI_MMCONFIG
442 else if (!strcmp(str, "nommconf")) {
443 pci_probe &= ~PCI_PROBE_MMCONF;
444 return NULL;
445 }
446#endif
447 else if (!strcmp(str, "noacpi")) {
448 acpi_noirq_set();
449 return NULL;
450 }
Andi Kleen0637a702006-09-26 10:52:41 +0200451 else if (!strcmp(str, "noearly")) {
452 pci_probe |= PCI_PROBE_NOEARLY;
453 return NULL;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#ifndef CONFIG_X86_VISWS
456 else if (!strcmp(str, "usepirqmask")) {
457 pci_probe |= PCI_USE_PIRQ_MASK;
458 return NULL;
459 } else if (!strncmp(str, "irqmask=", 8)) {
460 pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
461 return NULL;
462 } else if (!strncmp(str, "lastbus=", 8)) {
463 pcibios_last_bus = simple_strtol(str+8, NULL, 0);
464 return NULL;
465 }
466#endif
467 else if (!strcmp(str, "rom")) {
468 pci_probe |= PCI_ASSIGN_ROMS;
469 return NULL;
470 } else if (!strcmp(str, "assign-busses")) {
471 pci_probe |= PCI_ASSIGN_ALL_BUSSES;
472 return NULL;
Gary Hade62f420f2007-10-03 15:56:51 -0700473 } else if (!strcmp(str, "use_crs")) {
474 pci_probe |= PCI_USE__CRS;
475 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 } else if (!strcmp(str, "routeirq")) {
477 pci_routeirq = 1;
478 return NULL;
479 }
480 return str;
481}
482
483unsigned int pcibios_assign_all_busses(void)
484{
485 return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
486}
487
488int pcibios_enable_device(struct pci_dev *dev, int mask)
489{
490 int err;
491
492 if ((err = pcibios_enable_resources(dev, mask)) < 0)
493 return err;
494
Eric W. Biedermanbba6f6f2007-03-28 15:36:09 +0200495 if (!dev->msi_enabled)
496 return pcibios_enable_irq(dev);
497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
David Shaohua Li87bec662005-07-27 23:02:00 -0400499
500void pcibios_disable_device (struct pci_dev *dev)
501{
Eric W. Biedermanbba6f6f2007-03-28 15:36:09 +0200502 if (!dev->msi_enabled && pcibios_disable_irq)
David Shaohua Li87bec662005-07-27 23:02:00 -0400503 pcibios_disable_irq(dev);
504}
Muli Ben-Yehuda73c59af2007-08-10 13:01:19 -0700505
506struct pci_bus *pci_scan_bus_with_sysdata(int busno)
507{
508 struct pci_bus *bus = NULL;
509 struct pci_sysdata *sd;
510
511 /*
512 * Allocate per-root-bus (not per bus) arch-specific data.
513 * TODO: leak; this memory is never freed.
514 * It's arguable whether it's worth the trouble to care.
515 */
516 sd = kzalloc(sizeof(*sd), GFP_KERNEL);
517 if (!sd) {
518 printk(KERN_ERR "PCI: OOM, skipping PCI bus %02x\n", busno);
519 return NULL;
520 }
521 sd->node = -1;
522 bus = pci_scan_bus(busno, &pci_root_ops, sd);
523 if (!bus)
524 kfree(sd);
525
526 return bus;
527}