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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
39 #include <sys/sysctl.h>
41 #include <dev/pci/pcivar.h>
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcib_private.h>
44 #include <isa/isavar.h>
45 #include <machine/legacyvar.h>
46 #include <machine/pci_cfgreg.h>
47 #include <machine/resource.h>
52 legacy_pcib_maxslots(device_t dev)
57 /* read configuration space register */
60 legacy_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
63 return(pci_cfgregread(bus, slot, func, reg, bytes));
66 /* write configuration space register */
69 legacy_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
70 u_int reg, uint32_t data, int bytes)
72 pci_cfgregwrite(bus, slot, func, reg, data, bytes);
78 legacy_pcib_route_interrupt(device_t pcib, device_t dev, int pin)
81 /* No routing possible */
82 return (PCI_INVALID_IRQ);
85 /* Pass MSI requests up to the nexus. */
88 legacy_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
93 bus = device_get_parent(pcib);
94 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
99 legacy_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
103 bus = device_get_parent(pcib);
104 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
108 legacy_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
113 bus = device_get_parent(pcib);
114 return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data));
118 legacy_pcib_is_host_bridge(int bus, int slot, int func,
119 uint32_t id, uint8_t class, uint8_t subclass,
122 const char *s = NULL;
125 if (class == PCIC_BRIDGE && subclass == PCIS_BRIDGE_HOST)
126 s = "Host to PCI bridge";
131 * Scan the first pci bus for host-pci bridges and add pcib instances
132 * to the nexus for each bridge.
135 legacy_pcib_identify(driver_t *driver, device_t parent)
144 devclass_t pci_devclass;
146 if (pci_cfgregopen() == 0)
149 * Check to see if we haven't already had a PCI bus added
150 * via some other means. If we have, bail since otherwise
151 * we're going to end up duplicating it.
153 if ((pci_devclass = devclass_find("pci")) &&
154 devclass_get_device(pci_devclass, 0))
160 for (slot = 0; slot <= PCI_SLOTMAX; slot++) {
162 hdrtype = legacy_pcib_read_config(0, bus, slot, func,
165 * When enumerating bus devices, the standard says that
166 * one should check the header type and ignore the slots whose
167 * header types that the software doesn't know about. We use
168 * this to filter out devices.
170 if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
172 if ((hdrtype & PCIM_MFDEV) &&
173 (!found_orion || hdrtype != 0xff))
174 pcifunchigh = PCI_FUNCMAX;
177 for (func = 0; func <= pcifunchigh; func++) {
179 * Read the IDs and class from the device.
182 u_int8_t class, subclass, busnum;
187 id = legacy_pcib_read_config(0, bus, slot, func,
191 class = legacy_pcib_read_config(0, bus, slot, func,
193 subclass = legacy_pcib_read_config(0, bus, slot, func,
196 s = legacy_pcib_is_host_bridge(bus, slot, func,
203 * Check to see if the physical bus has already
204 * been seen. Eg: hybrid 32 and 64 bit host
205 * bridges to the same logical bus.
207 if (device_get_children(parent, &devs, &ndevs) == 0) {
208 for (i = 0; s != NULL && i < ndevs; i++) {
209 if (strcmp(device_get_name(devs[i]),
212 if (legacy_get_pcibus(devs[i]) == busnum)
221 * Add at priority 100 to make sure we
222 * go after any motherboard resources
224 child = BUS_ADD_CHILD(parent, 100,
226 device_set_desc(child, s);
227 legacy_set_pcibus(child, busnum);
230 if (id == 0x12258086)
232 if (id == 0x84c48086)
236 if (found824xx && bus == 0) {
242 * Make sure we add at least one bridge since some old
243 * hardware doesn't actually have a host-pci bridge device.
244 * Note that pci_cfgregopen() thinks we have PCI devices..
249 "legacy_pcib_identify: no bridge found, adding pcib0 anyway\n");
250 child = BUS_ADD_CHILD(parent, 100, "pcib", 0);
251 legacy_set_pcibus(child, 0);
256 legacy_pcib_probe(device_t dev)
259 if (pci_cfgregopen() == 0)
265 legacy_pcib_attach(device_t dev)
269 bus = pcib_get_bus(dev);
270 device_add_child(dev, "pci", bus);
271 return bus_generic_attach(dev);
275 legacy_pcib_read_ivar(device_t dev, device_t child, int which,
280 case PCIB_IVAR_DOMAIN:
284 *result = legacy_get_pcibus(dev);
291 legacy_pcib_write_ivar(device_t dev, device_t child, int which,
296 case PCIB_IVAR_DOMAIN:
299 legacy_set_pcibus(dev, value);
305 SYSCTL_DECL(_hw_pci);
307 static unsigned long legacy_host_mem_start = 0x80000000;
308 TUNABLE_ULONG("hw.pci.host_mem_start", &legacy_host_mem_start);
309 SYSCTL_ULONG(_hw_pci, OID_AUTO, host_mem_start, CTLFLAG_RDTUN,
310 &legacy_host_mem_start, 0x80000000,
311 "Limit the host bridge memory to being above this address. Must be\n\
312 set at boot via a tunable.");
315 legacy_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
316 u_long start, u_long end, u_long count, u_int flags)
319 * If no memory preference is given, use upper 32MB slot most
320 * bioses use for their memory window. Typically other bridges
321 * before us get in the way to assert their preferences on memory.
322 * Hardcoding like this sucks, so a more MD/MI way needs to be
323 * found to do it. This is typically only used on older laptops
324 * that don't have pci busses behind pci bridge, so assuming > 32MB
327 * However, this can cause problems for other chipsets, so we make
328 * this tunable by hw.pci.host_mem_start.
330 if (type == SYS_RES_MEMORY && start == 0UL && end == ~0UL)
331 start = legacy_host_mem_start;
332 if (type == SYS_RES_IOPORT && start == 0UL && end == ~0UL)
334 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
338 static device_method_t legacy_pcib_methods[] = {
339 /* Device interface */
340 DEVMETHOD(device_identify, legacy_pcib_identify),
341 DEVMETHOD(device_probe, legacy_pcib_probe),
342 DEVMETHOD(device_attach, legacy_pcib_attach),
343 DEVMETHOD(device_shutdown, bus_generic_shutdown),
344 DEVMETHOD(device_suspend, bus_generic_suspend),
345 DEVMETHOD(device_resume, bus_generic_resume),
348 DEVMETHOD(bus_print_child, bus_generic_print_child),
349 DEVMETHOD(bus_read_ivar, legacy_pcib_read_ivar),
350 DEVMETHOD(bus_write_ivar, legacy_pcib_write_ivar),
351 DEVMETHOD(bus_alloc_resource, legacy_pcib_alloc_resource),
352 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
353 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
354 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
355 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
356 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
357 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
360 DEVMETHOD(pcib_maxslots, legacy_pcib_maxslots),
361 DEVMETHOD(pcib_read_config, legacy_pcib_read_config),
362 DEVMETHOD(pcib_write_config, legacy_pcib_write_config),
363 DEVMETHOD(pcib_route_interrupt, legacy_pcib_route_interrupt),
364 DEVMETHOD(pcib_alloc_msi, legacy_pcib_alloc_msi),
365 DEVMETHOD(pcib_release_msi, pcib_release_msi),
366 DEVMETHOD(pcib_alloc_msix, legacy_pcib_alloc_msix),
367 DEVMETHOD(pcib_release_msix, pcib_release_msix),
368 DEVMETHOD(pcib_map_msi, legacy_pcib_map_msi),
373 static devclass_t hostb_devclass;
375 DEFINE_CLASS_0(pcib, legacy_pcib_driver, legacy_pcib_methods, 1);
376 DRIVER_MODULE(pcib, legacy, legacy_pcib_driver, hostb_devclass, 0, 0);
380 * Install placeholder to claim the resources owned by the
381 * PCI bus interface. This could be used to extract the
382 * config space registers in the extreme case where the PnP
383 * ID is available and the PCI BIOS isn't, but for now we just
384 * eat the PnP ID and do nothing else.
386 * XXX we should silence this probe, as it will generally confuse
389 static struct isa_pnp_id pcibus_pnp_ids[] = {
390 { 0x030ad041 /* PNP0A03 */, "PCI Bus" },
391 { 0x080ad041 /* PNP0A08 */, "PCIe Bus" },
396 pcibus_pnp_probe(device_t dev)
400 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, pcibus_pnp_ids)) <= 0)
406 pcibus_pnp_attach(device_t dev)
411 static device_method_t pcibus_pnp_methods[] = {
412 /* Device interface */
413 DEVMETHOD(device_probe, pcibus_pnp_probe),
414 DEVMETHOD(device_attach, pcibus_pnp_attach),
415 DEVMETHOD(device_detach, bus_generic_detach),
416 DEVMETHOD(device_shutdown, bus_generic_shutdown),
417 DEVMETHOD(device_suspend, bus_generic_suspend),
418 DEVMETHOD(device_resume, bus_generic_resume),
422 static devclass_t pcibus_pnp_devclass;
424 DEFINE_CLASS_0(pcibus_pnp, pcibus_pnp_driver, pcibus_pnp_methods, 1);
425 DRIVER_MODULE(pcibus_pnp, isa, pcibus_pnp_driver, pcibus_pnp_devclass, 0, 0);