2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/i386/isa/pcibus.c,v 1.57.2.12 2003/08/07 06:19:26 imp Exp $
27 * $DragonFly: src/sys/bus/pci/i386/pcibus.c,v 1.16 2006/12/22 23:12:17 swildner Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <bus/pci/pcivar.h>
38 #include <bus/pci/pcireg.h>
39 #include <bus/pci/i386/pcibus.h>
40 #include <bus/isa/isavar.h>
41 #include <bus/pci/i386/pci_cfgreg.h>
42 #include <machine/md_var.h>
43 #include <machine/nexusvar.h>
47 static u_int32_t nexus_pcib_read_config(device_t, int, int, int, int, int);
50 * Figure out if a PCI entity is a host bridge, return its name or NULL.
53 nexus_legacypci_is_host_bridge(int bus, int slot, int func,
54 u_int32_t id, u_int8_t class, u_int8_t subclass,
58 static u_int8_t pxb[4]; /* hack for 450nx */
64 s = "Intel 824?? host to PCI bridge";
65 /* XXX This is a guess */
66 /* *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x41, 1); */
70 s = "Intel 82810 (i810 GMCH) Host To Hub bridge";
73 s = "Intel 82810-DC100 (i810-DC100 GMCH) Host To Hub bridge";
76 s = "Intel 82810E (i810E GMCH) Host To Hub bridge";
79 s = "Intel 82443LX (440 LX) host to PCI bridge";
82 s = "Intel 82443BX (440 BX) host to PCI bridge";
85 s = "Intel 82443BX host to PCI bridge (AGP disabled)";
88 s = "Intel 82443MX host to PCI bridge";
91 s = "Intel 82443GX host to PCI bridge";
94 s = "Intel 82443GX host to AGP bridge";
97 s = "Intel 82443GX host to PCI bridge (AGP disabled)";
100 s = "Intel 82454KX/GX (Orion) host to PCI bridge";
101 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x4a, 1);
105 * For the 450nx chipset, there is a whole bundle of
106 * things pretending to be host bridges. The MIOC will
107 * be seen first and isn't really a pci bridge (the
108 * actual busses are attached to the PXB's). We need to
109 * read the registers of the MIOC to figure out the
110 * bus numbers for the PXB channels.
112 * Since the MIOC doesn't have a pci bus attached, we
113 * pretend it wasn't there.
115 pxb[0] = nexus_pcib_read_config(0, bus, slot, func,
116 0xd0, 1); /* BUSNO[0] */
117 pxb[1] = nexus_pcib_read_config(0, bus, slot, func,
118 0xd1, 1) + 1; /* SUBA[0]+1 */
119 pxb[2] = nexus_pcib_read_config(0, bus, slot, func,
120 0xd3, 1); /* BUSNO[1] */
121 pxb[3] = nexus_pcib_read_config(0, bus, slot, func,
122 0xd4, 1) + 1; /* SUBA[1]+1 */
127 s = "Intel 82454NX PXB#0, Bus#A";
131 s = "Intel 82454NX PXB#0, Bus#B";
135 s = "Intel 82454NX PXB#1, Bus#A";
139 s = "Intel 82454NX PXB#1, Bus#B";
145 s = "Intel 82845 Host to PCI bridge";
148 /* AMD -- vendor 0x1022 */
150 s = "AMD Elan SC520 host to PCI bridge";
152 init_AMD_Elan_sc520();
154 kprintf("*** WARNING: kernel option CPU_ELAN missing");
155 kprintf("-- timekeeping may be wrong\n");
159 s = "AMD-751 host to PCI bridge";
162 s = "AMD-761 host to PCI bridge";
165 /* SiS -- vendor 0x1039 */
176 s = "SiS 5591 host to PCI bridge";
179 s = "SiS 5591 host to AGP bridge";
182 /* VLSI -- vendor 0x1004 */
184 s = "VLSI 82C592 Host to PCI bridge";
187 /* XXX Here is MVP3, I got the datasheet but NO M/B to test it */
188 /* totally. Please let me know if anything wrong. -F */
189 /* XXX need info on the MVP3 -- any takers? */
191 s = "VIA 82C598MVP (Apollo MVP3) host bridge";
194 /* AcerLabs -- vendor 0x10b9 */
195 /* Funny : The datasheet told me vendor id is "10b8",sub-vendor */
196 /* id is '10b9" but the register always shows "10b9". -Foxfair */
198 s = "AcerLabs M1541 (Aladdin-V) PCI host bridge";
201 /* OPTi -- vendor 0x1045 */
203 s = "OPTi 82C700 host to PCI bridge";
206 s = "OPTi 82C822 host to PCI Bridge";
209 /* ServerWorks -- vendor 0x1166 */
211 s = "ServerWorks NB6536 2.0HE host to PCI bridge";
212 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
221 case 0x010f1014: /* IBM re-badged ServerWorks chipset */
223 s = "ServerWorks host to PCI bridge";
224 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
228 s = "ServerWorks NB6635 3.0LE host to PCI bridge";
229 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
233 s = "ServerWorks CIOB30 host to PCI bridge";
234 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
239 case 0x03021014: /* IBM re-badged ServerWorks chipset */
240 s = "ServerWorks CMIC-HE host to PCI-X bridge";
241 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
244 /* XXX unknown chipset, but working */
248 s = "ServerWorks host to PCI bridge(unknown chipset)";
249 *busnum = nexus_pcib_read_config(0, bus, slot, func, 0x44, 1);
252 /* Integrated Micro Solutions -- vendor 0x10e0 */
254 s = "Integrated Micro Solutions VL Bridge";
258 if (class == PCIC_BRIDGE && subclass == PCIS_BRIDGE_HOST)
259 s = "Host to PCI bridge";
267 * Identify the existance of the first pci bus and install a child to
268 * nexus if we find it. Use an order of 1 so it gets probed after
269 * any ACPI device installed under nexus. To avoid boot-time confusion,
270 * we do not install any 'pcib' devices at this time.
272 * The identify method coupled with the driver spec of the same name
273 * automatically installs it under the nexus.
276 nexus_legacypci_identify(driver_t *driver, device_t parent)
279 * Basically a static device, there's no point reinstalling it
282 if (device_get_state(parent) == DS_ATTACHED)
285 if (pci_cfgregopen() == 0)
288 BUS_ADD_CHILD(parent, parent, 100, "legacypci", 0);
293 * Scan the first pci bus for host-pci bridges and add pcib instances
294 * to the nexus for each bridge.
297 nexus_legacypci_probe(device_t dev)
307 * Do not install any pci busses ('pcib' devices) if the PCI
308 * subsystem has already been claimed by someone else.
310 if (pcib_owner != NULL) {
311 device_printf(dev, "PCI subsystem owned by %s, skipping scan\n",
315 pcib_owner = "legacypci";
317 if (pci_cfgregopen() == 0)
325 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
327 hdrtype = nexus_pcib_read_config(0, bus, slot, func,
329 if ((hdrtype & ~PCIM_MFDEV) > 2)
331 if (hdrtype & PCIM_MFDEV)
335 for (func = 0; func <= pcifunchigh; func++) {
337 * Read the IDs and class from the device.
340 u_int8_t class, subclass, busnum;
345 id = nexus_pcib_read_config(0, bus, slot, func,
349 class = nexus_pcib_read_config(0, bus, slot, func,
351 subclass = nexus_pcib_read_config(0, bus, slot, func,
354 s = nexus_legacypci_is_host_bridge(bus, slot, func,
361 * Check to see if the physical bus has already
362 * been seen. Eg: hybrid 32 and 64 bit host
363 * bridges to the same logical bus.
365 if (device_get_children(dev, &devs, &ndevs) == 0) {
366 for (i = 0; s != NULL && i < ndevs; i++) {
367 if (strcmp(device_get_name(devs[i]),
370 kprintf("compare %d\n", nexus_get_pcibus(devs[i]));
371 if (nexus_get_pcibus(devs[i]) == busnum)
380 * Add at priority 100+busnum to keep the scanning
381 * order sane in the boot dmesg output.
383 child = BUS_ADD_CHILD(dev, dev, 100 + busnum,
385 device_set_desc(child, s);
386 nexus_set_pcibus(child, busnum);
389 if (id == 0x12258086)
393 if (found824xx && bus == 0) {
400 * Now that we have installed the main PCI bridges, go
401 * probe and attach each one.
403 bus_generic_attach(dev);
407 * Make sure we add at least one bridge since some old
408 * hardware doesn't actually have a host-pci bridge device.
409 * Note that pci_cfgregopen() thinks we have PCI devices..
413 kprintf("nexus_pcib_identify: no bridge found, "
414 "adding pcib0 anyway\n");
416 child = BUS_ADD_CHILD(dev, dev, 100, "pcib", 0);
417 nexus_set_pcibus(child, 0);
423 nexus_legacypci_attach(device_t dev)
425 bus_generic_attach(dev);
429 static device_method_t legacypci_methods[] = {
430 /* Device interface */
431 DEVMETHOD(device_identify, nexus_legacypci_identify),
432 DEVMETHOD(device_probe, nexus_legacypci_probe),
433 DEVMETHOD(device_attach, nexus_legacypci_attach),
434 DEVMETHOD(device_shutdown, bus_generic_shutdown),
435 DEVMETHOD(device_suspend, bus_generic_suspend),
436 DEVMETHOD(device_resume, bus_generic_resume),
439 * Bus interface - propogate through to the nexus. Note that
440 * this means devices under us will have nexus ivars.
442 DEVMETHOD(bus_add_child, bus_generic_add_child),
443 DEVMETHOD(bus_print_child, bus_generic_print_child),
444 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
445 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
446 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
447 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
448 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
449 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
450 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
451 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
452 DEVMETHOD(bus_set_resource, bus_generic_set_resource),
453 DEVMETHOD(bus_get_resource, bus_generic_get_resource),
454 DEVMETHOD(bus_delete_resource, bus_generic_delete_resource),
458 static driver_t legacypci_driver = {
464 static devclass_t legacypci_devclass;
466 DRIVER_MODULE(legacypci, nexus, legacypci_driver, legacypci_devclass, 0, 0);
469 * Legacypci Host-Bridge PCI BUS support. The underlying pcib devices
470 * will only exist if we actually control the PCI bus. The actual PCI
471 * bus driver is attached in our attach routine.
473 * There is no identify function because the legacypci placeholder will
474 * have already scanned and added PCIB devices for the host-bridges found.
477 nexus_pcib_maxslots(device_t dev)
483 * Read configuration space register.
486 nexus_pcib_read_config(device_t dev, int bus, int slot, int func,
489 return (pci_cfgregread(bus, slot, func, reg, bytes));
493 * Write configuration space register.
496 nexus_pcib_write_config(device_t dev, int bus, int slot, int func,
497 int reg, u_int32_t data, int bytes)
499 pci_cfgregwrite(bus, slot, func, reg, data, bytes);
503 * Stack a pci device on top of the pci bridge bus device.
506 nexus_pcib_probe(device_t dev)
508 BUS_ADD_CHILD(dev, dev, 0, "pci", device_get_unit(dev));
513 nexus_pcib_attach(device_t dev)
517 error = bus_generic_attach(dev);
521 /* route interrupt */
524 nexus_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
526 return(pci_cfgintr(pci_get_bus(dev), pci_get_slot(dev), pin,
530 static device_method_t nexus_pcib_methods[] = {
531 /* Device interface */
532 DEVMETHOD(device_probe, nexus_pcib_probe),
533 DEVMETHOD(device_attach, nexus_pcib_attach),
534 DEVMETHOD(device_shutdown, bus_generic_shutdown),
535 DEVMETHOD(device_suspend, bus_generic_suspend),
536 DEVMETHOD(device_resume, bus_generic_resume),
539 * Bus interface - propogate through to the nexus. Note
540 * that this means we will get nexus-managed ivars.
542 DEVMETHOD(bus_add_child, bus_generic_add_child),
543 DEVMETHOD(bus_print_child, bus_generic_print_child),
544 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
545 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
546 DEVMETHOD(bus_alloc_resource, bus_generic_alloc_resource),
547 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
548 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
549 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
550 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
551 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
554 DEVMETHOD(pcib_maxslots, nexus_pcib_maxslots),
555 DEVMETHOD(pcib_read_config, nexus_pcib_read_config),
556 DEVMETHOD(pcib_write_config, nexus_pcib_write_config),
557 DEVMETHOD(pcib_route_interrupt, nexus_pcib_route_interrupt),
562 static driver_t nexus_pcib_driver = {
568 static devclass_t pcib_devclass;
570 DRIVER_MODULE(pcib, legacypci, nexus_pcib_driver, pcib_devclass, 0, 0);
574 * Provide a device to "eat" the host->pci bridges that we dug up above
575 * and stop them showing up twice on the probes. This also stops them
576 * showing up as 'none' in pciconf -l.
578 * Return an ultra-low priority so other devices can attach the bus before
581 * XXX may have to disable the registration entirely to support module-loaded
582 * bridges such as agp.ko.
585 pci_hostb_probe(device_t dev)
587 if (pci_get_class(dev) == PCIC_BRIDGE &&
588 pci_get_subclass(dev) == PCIS_BRIDGE_HOST) {
589 device_set_desc(dev, "Host to PCI bridge");
597 pci_hostb_attach(device_t dev)
602 static device_method_t pci_hostb_methods[] = {
603 /* Device interface */
604 DEVMETHOD(device_probe, pci_hostb_probe),
605 DEVMETHOD(device_attach, pci_hostb_attach),
606 DEVMETHOD(device_shutdown, bus_generic_shutdown),
607 DEVMETHOD(device_suspend, bus_generic_suspend),
608 DEVMETHOD(device_resume, bus_generic_resume),
612 static driver_t pci_hostb_driver = {
617 static devclass_t pci_hostb_devclass;
619 DRIVER_MODULE(hostb, pci, pci_hostb_driver, pci_hostb_devclass, 0, 0);
623 * Install placeholder to claim the resources owned by the
624 * PCI bus interface. This could be used to extract the
625 * config space registers in the extreme case where the PnP
626 * ID is available and the PCI BIOS isn't, but for now we just
627 * eat the PnP ID and do nothing else.
629 * XXX we should silence this probe, as it will generally confuse
632 static struct isa_pnp_id pcibus_pnp_ids[] = {
633 { 0x030ad041 /* PNP030A */, "PCI Bus" },
638 pcibus_pnp_probe(device_t dev)
642 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, pcibus_pnp_ids)) <= 0)
648 pcibus_pnp_attach(device_t dev)
653 static device_method_t pcibus_pnp_methods[] = {
654 /* Device interface */
655 DEVMETHOD(device_probe, pcibus_pnp_probe),
656 DEVMETHOD(device_attach, pcibus_pnp_attach),
657 DEVMETHOD(device_detach, bus_generic_detach),
658 DEVMETHOD(device_shutdown, bus_generic_shutdown),
659 DEVMETHOD(device_suspend, bus_generic_suspend),
660 DEVMETHOD(device_resume, bus_generic_resume),
664 static driver_t pcibus_pnp_driver = {
670 static devclass_t pcibus_pnp_devclass;
672 DRIVER_MODULE(pcibus_pnp, isa, pcibus_pnp_driver, pcibus_pnp_devclass, 0, 0);