2 * Low-Level PCI Support for PC
4 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
7 #include <linux/sched.h>
9 #include <linux/ioport.h>
10 #include <linux/init.h>
11 #include <linux/dmi.h>
14 #include <asm/segment.h>
20 unsigned int pci_probe
= PCI_PROBE_BIOS
| PCI_PROBE_CONF1
| PCI_PROBE_CONF2
|
23 static int pci_bf_sort
;
25 int pcibios_last_bus
= -1;
26 unsigned long pirq_table_addr
;
27 struct pci_bus
*pci_root_bus
;
28 struct pci_raw_ops
*raw_pci_ops
;
29 struct pci_raw_ops
*raw_pci_ext_ops
;
31 int raw_pci_read(unsigned int domain
, unsigned int bus
, unsigned int devfn
,
32 int reg
, int len
, u32
*val
)
34 if (reg
< 256 && raw_pci_ops
)
35 return raw_pci_ops
->read(domain
, bus
, devfn
, reg
, len
, val
);
37 return raw_pci_ext_ops
->read(domain
, bus
, devfn
, reg
, len
, val
);
41 int raw_pci_write(unsigned int domain
, unsigned int bus
, unsigned int devfn
,
42 int reg
, int len
, u32 val
)
44 if (reg
< 256 && raw_pci_ops
)
45 return raw_pci_ops
->write(domain
, bus
, devfn
, reg
, len
, val
);
47 return raw_pci_ext_ops
->write(domain
, bus
, devfn
, reg
, len
, val
);
51 static int pci_read(struct pci_bus
*bus
, unsigned int devfn
, int where
, int size
, u32
*value
)
53 return raw_pci_read(pci_domain_nr(bus
), bus
->number
,
54 devfn
, where
, size
, value
);
57 static int pci_write(struct pci_bus
*bus
, unsigned int devfn
, int where
, int size
, u32 value
)
59 return raw_pci_write(pci_domain_nr(bus
), bus
->number
,
60 devfn
, where
, size
, value
);
63 struct pci_ops pci_root_ops
= {
69 * legacy, numa, and acpi all want to call pcibios_scan_root
70 * from their initcalls. This flag prevents that.
75 * This interrupt-safe spinlock protects all accesses to PCI
76 * configuration space.
78 DEFINE_SPINLOCK(pci_config_lock
);
81 * Several buggy motherboards address only 16 devices and mirror
82 * them to next 16 IDs. We try to detect this `feature' on all
83 * primary buses (those containing host bridges as they are
84 * expected to be unique) and remove the ghost devices.
87 static void __devinit
pcibios_fixup_ghosts(struct pci_bus
*b
)
89 struct list_head
*ln
, *mn
;
90 struct pci_dev
*d
, *e
;
91 int mirror
= PCI_DEVFN(16,0);
92 int seen_host_bridge
= 0;
95 DBG("PCI: Scanning for ghost devices on bus %d\n", b
->number
);
96 list_for_each(ln
, &b
->devices
) {
98 if ((d
->class >> 8) == PCI_CLASS_BRIDGE_HOST
)
100 for (mn
=ln
->next
; mn
!= &b
->devices
; mn
=mn
->next
) {
102 if (e
->devfn
!= d
->devfn
+ mirror
||
103 e
->vendor
!= d
->vendor
||
104 e
->device
!= d
->device
||
105 e
->class != d
->class)
107 for(i
=0; i
<PCI_NUM_RESOURCES
; i
++)
108 if (e
->resource
[i
].start
!= d
->resource
[i
].start
||
109 e
->resource
[i
].end
!= d
->resource
[i
].end
||
110 e
->resource
[i
].flags
!= d
->resource
[i
].flags
)
114 if (mn
== &b
->devices
)
117 if (!seen_host_bridge
)
119 printk(KERN_WARNING
"PCI: Ignoring ghost devices on bus %02x\n", b
->number
);
122 while (ln
->next
!= &b
->devices
) {
123 d
= pci_dev_b(ln
->next
);
124 if (d
->devfn
>= mirror
) {
125 list_del(&d
->global_list
);
126 list_del(&d
->bus_list
);
133 static void __devinit
pcibios_fixup_device_resources(struct pci_dev
*dev
)
135 struct resource
*rom_r
= &dev
->resource
[PCI_ROM_RESOURCE
];
140 /* we deal with BIOS assigned ROM later */
142 if (!(pci_probe
& PCI_ASSIGN_ROMS
))
143 rom_r
->start
= rom_r
->end
= rom_r
->flags
= 0;
147 * Called after each bus is probed, but before its children
151 void __devinit
pcibios_fixup_bus(struct pci_bus
*b
)
155 pcibios_fixup_ghosts(b
);
156 pci_read_bridge_bases(b
);
157 list_for_each_entry(dev
, &b
->devices
, bus_list
)
158 pcibios_fixup_device_resources(dev
);
162 * Only use DMI information to set this if nothing was passed
163 * on the kernel command line (which was parsed earlier).
166 static int __devinit
set_bf_sort(const struct dmi_system_id
*d
)
168 if (pci_bf_sort
== pci_bf_sort_default
) {
169 pci_bf_sort
= pci_dmi_bf
;
170 printk(KERN_INFO
"PCI: %s detected, enabling pci=bfsort.\n", d
->ident
);
176 * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
179 static int __devinit
assign_all_busses(const struct dmi_system_id
*d
)
181 pci_probe
|= PCI_ASSIGN_ALL_BUSSES
;
182 printk(KERN_INFO
"%s detected: enabling PCI bus# renumbering"
183 " (pci=assign-busses)\n", d
->ident
);
188 static struct dmi_system_id __devinitdata pciprobe_dmi_table
[] = {
191 * Laptops which need pci=assign-busses to see Cardbus cards
194 .callback
= assign_all_busses
,
195 .ident
= "Samsung X20 Laptop",
197 DMI_MATCH(DMI_SYS_VENDOR
, "Samsung Electronics"),
198 DMI_MATCH(DMI_PRODUCT_NAME
, "SX20S"),
201 #endif /* __i386__ */
203 .callback
= set_bf_sort
,
204 .ident
= "Dell PowerEdge 1950",
206 DMI_MATCH(DMI_SYS_VENDOR
, "Dell"),
207 DMI_MATCH(DMI_PRODUCT_NAME
, "PowerEdge 1950"),
211 .callback
= set_bf_sort
,
212 .ident
= "Dell PowerEdge 1955",
214 DMI_MATCH(DMI_SYS_VENDOR
, "Dell"),
215 DMI_MATCH(DMI_PRODUCT_NAME
, "PowerEdge 1955"),
219 .callback
= set_bf_sort
,
220 .ident
= "Dell PowerEdge 2900",
222 DMI_MATCH(DMI_SYS_VENDOR
, "Dell"),
223 DMI_MATCH(DMI_PRODUCT_NAME
, "PowerEdge 2900"),
227 .callback
= set_bf_sort
,
228 .ident
= "Dell PowerEdge 2950",
230 DMI_MATCH(DMI_SYS_VENDOR
, "Dell"),
231 DMI_MATCH(DMI_PRODUCT_NAME
, "PowerEdge 2950"),
235 .callback
= set_bf_sort
,
236 .ident
= "Dell PowerEdge R900",
238 DMI_MATCH(DMI_SYS_VENDOR
, "Dell"),
239 DMI_MATCH(DMI_PRODUCT_NAME
, "PowerEdge R900"),
243 .callback
= set_bf_sort
,
244 .ident
= "HP ProLiant BL20p G3",
246 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
247 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL20p G3"),
251 .callback
= set_bf_sort
,
252 .ident
= "HP ProLiant BL20p G4",
254 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
255 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL20p G4"),
259 .callback
= set_bf_sort
,
260 .ident
= "HP ProLiant BL30p G1",
262 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
263 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL30p G1"),
267 .callback
= set_bf_sort
,
268 .ident
= "HP ProLiant BL25p G1",
270 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
271 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL25p G1"),
275 .callback
= set_bf_sort
,
276 .ident
= "HP ProLiant BL35p G1",
278 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
279 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL35p G1"),
283 .callback
= set_bf_sort
,
284 .ident
= "HP ProLiant BL45p G1",
286 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
287 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL45p G1"),
291 .callback
= set_bf_sort
,
292 .ident
= "HP ProLiant BL45p G2",
294 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
295 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL45p G2"),
299 .callback
= set_bf_sort
,
300 .ident
= "HP ProLiant BL460c G1",
302 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
303 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL460c G1"),
307 .callback
= set_bf_sort
,
308 .ident
= "HP ProLiant BL465c G1",
310 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
311 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL465c G1"),
315 .callback
= set_bf_sort
,
316 .ident
= "HP ProLiant BL480c G1",
318 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
319 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL480c G1"),
323 .callback
= set_bf_sort
,
324 .ident
= "HP ProLiant BL685c G1",
326 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
327 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant BL685c G1"),
331 .callback
= set_bf_sort
,
332 .ident
= "HP ProLiant DL385 G2",
334 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
335 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant DL385 G2"),
339 .callback
= set_bf_sort
,
340 .ident
= "HP ProLiant DL585 G2",
342 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
343 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant DL585 G2"),
348 .callback
= assign_all_busses
,
349 .ident
= "Compaq EVO N800c",
351 DMI_MATCH(DMI_SYS_VENDOR
, "Compaq"),
352 DMI_MATCH(DMI_PRODUCT_NAME
, "EVO N800c"),
357 .callback
= set_bf_sort
,
358 .ident
= "HP ProLiant DL385 G2",
360 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
361 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant DL385 G2"),
365 .callback
= set_bf_sort
,
366 .ident
= "HP ProLiant DL585 G2",
368 DMI_MATCH(DMI_SYS_VENDOR
, "HP"),
369 DMI_MATCH(DMI_PRODUCT_NAME
, "ProLiant DL585 G2"),
375 void __init
dmi_check_pciprobe(void)
377 dmi_check_system(pciprobe_dmi_table
);
380 struct pci_bus
* __devinit
pcibios_scan_root(int busnum
)
382 struct pci_bus
*bus
= NULL
;
383 struct pci_sysdata
*sd
;
385 while ((bus
= pci_find_next_bus(bus
)) != NULL
) {
386 if (bus
->number
== busnum
) {
387 /* Already scanned */
392 /* Allocate per-root-bus (not per bus) arch-specific data.
393 * TODO: leak; this memory is never freed.
394 * It's arguable whether it's worth the trouble to care.
396 sd
= kzalloc(sizeof(*sd
), GFP_KERNEL
);
398 printk(KERN_ERR
"PCI: OOM, not probing PCI bus %02x\n", busnum
);
402 printk(KERN_DEBUG
"PCI: Probing PCI hardware (bus %02x)\n", busnum
);
404 return pci_scan_bus_parented(NULL
, busnum
, &pci_root_ops
, sd
);
407 extern u8 pci_cache_line_size
;
409 static int __init
pcibios_init(void)
411 struct cpuinfo_x86
*c
= &boot_cpu_data
;
414 printk(KERN_WARNING
"PCI: System does not support PCI\n");
419 * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
420 * and P4. It's also good for 386/486s (which actually have 16)
421 * as quite a few PCI devices do not support smaller values.
423 pci_cache_line_size
= 32 >> 2;
424 if (c
->x86
>= 6 && c
->x86_vendor
== X86_VENDOR_AMD
)
425 pci_cache_line_size
= 64 >> 2; /* K7 & K8 */
426 else if (c
->x86
> 6 && c
->x86_vendor
== X86_VENDOR_INTEL
)
427 pci_cache_line_size
= 128 >> 2; /* P4 */
429 pcibios_resource_survey();
431 if (pci_bf_sort
>= pci_force_bf
)
432 pci_sort_breadthfirst();
433 #ifdef CONFIG_PCI_BIOS
434 if ((pci_probe
& PCI_BIOS_SORT
) && !(pci_probe
& PCI_NO_SORT
))
440 subsys_initcall(pcibios_init
);
442 char * __devinit
pcibios_setup(char *str
)
444 if (!strcmp(str
, "off")) {
447 } else if (!strcmp(str
, "bfsort")) {
448 pci_bf_sort
= pci_force_bf
;
450 } else if (!strcmp(str
, "nobfsort")) {
451 pci_bf_sort
= pci_force_nobf
;
454 #ifdef CONFIG_PCI_BIOS
455 else if (!strcmp(str
, "bios")) {
456 pci_probe
= PCI_PROBE_BIOS
;
458 } else if (!strcmp(str
, "nobios")) {
459 pci_probe
&= ~PCI_PROBE_BIOS
;
461 } else if (!strcmp(str
, "nosort")) {
462 pci_probe
|= PCI_NO_SORT
;
464 } else if (!strcmp(str
, "biosirq")) {
465 pci_probe
|= PCI_BIOS_IRQ_SCAN
;
467 } else if (!strncmp(str
, "pirqaddr=", 9)) {
468 pirq_table_addr
= simple_strtoul(str
+9, NULL
, 0);
472 #ifdef CONFIG_PCI_DIRECT
473 else if (!strcmp(str
, "conf1")) {
474 pci_probe
= PCI_PROBE_CONF1
| PCI_NO_CHECKS
;
477 else if (!strcmp(str
, "conf2")) {
478 pci_probe
= PCI_PROBE_CONF2
| PCI_NO_CHECKS
;
482 #ifdef CONFIG_PCI_MMCONFIG
483 else if (!strcmp(str
, "nommconf")) {
484 pci_probe
&= ~PCI_PROBE_MMCONF
;
488 else if (!strcmp(str
, "noacpi")) {
492 else if (!strcmp(str
, "noearly")) {
493 pci_probe
|= PCI_PROBE_NOEARLY
;
496 #ifndef CONFIG_X86_VISWS
497 else if (!strcmp(str
, "usepirqmask")) {
498 pci_probe
|= PCI_USE_PIRQ_MASK
;
500 } else if (!strncmp(str
, "irqmask=", 8)) {
501 pcibios_irq_mask
= simple_strtol(str
+8, NULL
, 0);
503 } else if (!strncmp(str
, "lastbus=", 8)) {
504 pcibios_last_bus
= simple_strtol(str
+8, NULL
, 0);
508 else if (!strcmp(str
, "rom")) {
509 pci_probe
|= PCI_ASSIGN_ROMS
;
511 } else if (!strcmp(str
, "assign-busses")) {
512 pci_probe
|= PCI_ASSIGN_ALL_BUSSES
;
514 } else if (!strcmp(str
, "use_crs")) {
515 pci_probe
|= PCI_USE__CRS
;
517 } else if (!strcmp(str
, "routeirq")) {
524 unsigned int pcibios_assign_all_busses(void)
526 return (pci_probe
& PCI_ASSIGN_ALL_BUSSES
) ? 1 : 0;
529 int pcibios_enable_device(struct pci_dev
*dev
, int mask
)
533 if ((err
= pcibios_enable_resources(dev
, mask
)) < 0)
536 if (!dev
->msi_enabled
)
537 return pcibios_enable_irq(dev
);
541 void pcibios_disable_device (struct pci_dev
*dev
)
543 if (!dev
->msi_enabled
&& pcibios_disable_irq
)
544 pcibios_disable_irq(dev
);
547 struct pci_bus
*__devinit
pci_scan_bus_with_sysdata(int busno
)
549 struct pci_bus
*bus
= NULL
;
550 struct pci_sysdata
*sd
;
553 * Allocate per-root-bus (not per bus) arch-specific data.
554 * TODO: leak; this memory is never freed.
555 * It's arguable whether it's worth the trouble to care.
557 sd
= kzalloc(sizeof(*sd
), GFP_KERNEL
);
559 printk(KERN_ERR
"PCI: OOM, skipping PCI bus %02x\n", busno
);
563 bus
= pci_scan_bus(busno
, &pci_root_ops
, sd
);