2 * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4 * Copyright (c) 2000 BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * __FBSDID("$FreeBSD: src/sys/dev/pci/pci_pci.c,v 1.50.2.2.4.1 2009/04/15 03:14:26 kensmith Exp $");
32 #include <sys/cdefs.h>
35 * PCI:PCI bridge support.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
44 #include <sys/sysctl.h>
46 #include <bus/pci/pcivar.h>
47 #include <bus/pci/pcireg.h>
48 #include <bus/pci/pcib_private.h>
52 static int pcib_probe(device_t dev);
54 static device_method_t pcib_methods[] = {
55 /* Device interface */
56 DEVMETHOD(device_probe, pcib_probe),
57 DEVMETHOD(device_attach, pcib_attach),
58 DEVMETHOD(device_detach, bus_generic_detach),
59 DEVMETHOD(device_shutdown, bus_generic_shutdown),
60 DEVMETHOD(device_suspend, bus_generic_suspend),
61 DEVMETHOD(device_resume, bus_generic_resume),
64 DEVMETHOD(bus_print_child, bus_generic_print_child),
65 DEVMETHOD(bus_read_ivar, pcib_read_ivar),
66 DEVMETHOD(bus_write_ivar, pcib_write_ivar),
67 DEVMETHOD(bus_alloc_resource, pcib_alloc_resource),
68 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
69 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
70 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
71 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
72 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
75 DEVMETHOD(pcib_maxslots, pcib_maxslots),
76 DEVMETHOD(pcib_read_config, pcib_read_config),
77 DEVMETHOD(pcib_write_config, pcib_write_config),
78 DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt),
79 DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi),
80 DEVMETHOD(pcib_release_msi, pcib_release_msi),
81 DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix),
82 DEVMETHOD(pcib_release_msix, pcib_release_msix),
83 DEVMETHOD(pcib_map_msi, pcib_map_msi),
88 static devclass_t pcib_devclass;
90 DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
91 DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
94 * Is the prefetch window open (eg, can we allocate memory in it?)
97 pcib_is_prefetch_open(struct pcib_softc *sc)
99 return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
103 * Is the nonprefetch window open (eg, can we allocate memory in it?)
106 pcib_is_nonprefetch_open(struct pcib_softc *sc)
108 return (sc->membase > 0 && sc->membase < sc->memlimit);
112 * Is the io window open (eg, can we allocate ports in it?)
115 pcib_is_io_open(struct pcib_softc *sc)
117 return (sc->iobase > 0 && sc->iobase < sc->iolimit);
121 * Generic device interface
124 pcib_probe(device_t dev)
126 if ((pci_get_class(dev) == PCIC_BRIDGE) &&
127 (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
128 device_set_desc(dev, "PCI-PCI bridge");
135 pcib_attach_common(device_t dev)
137 struct pcib_softc *sc;
140 sc = device_get_softc(dev);
144 * Get current bridge configuration.
146 sc->command = pci_read_config(dev, PCIR_COMMAND, 1);
147 sc->domain = pci_get_domain(dev);
148 sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1);
149 sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
150 sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2);
151 sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
152 sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1);
155 * Determine current I/O decode.
157 if (sc->command & PCIM_CMD_PORTEN) {
158 iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
159 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
160 sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2),
161 pci_read_config(dev, PCIR_IOBASEL_1, 1));
163 sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1));
166 iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
167 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
168 sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2),
169 pci_read_config(dev, PCIR_IOLIMITL_1, 1));
171 sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1));
176 * Determine current memory decode.
178 if (sc->command & PCIM_CMD_MEMEN) {
179 sc->membase = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2));
180 sc->memlimit = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
181 iolow = pci_read_config(dev, PCIR_PMBASEL_1, 1);
182 if ((iolow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
183 sc->pmembase = PCI_PPBMEMBASE(
184 pci_read_config(dev, PCIR_PMBASEH_1, 4),
185 pci_read_config(dev, PCIR_PMBASEL_1, 2));
187 sc->pmembase = PCI_PPBMEMBASE(0,
188 pci_read_config(dev, PCIR_PMBASEL_1, 2));
189 iolow = pci_read_config(dev, PCIR_PMLIMITL_1, 1);
190 if ((iolow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
191 sc->pmemlimit = PCI_PPBMEMLIMIT(
192 pci_read_config(dev, PCIR_PMLIMITH_1, 4),
193 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
195 sc->pmemlimit = PCI_PPBMEMLIMIT(0,
196 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
202 switch (pci_get_devid(dev)) {
203 case 0x12258086: /* Intel 82454KX/GX (Orion) */
207 supbus = pci_read_config(dev, 0x41, 1);
208 if (supbus != 0xff) {
209 sc->secbus = supbus + 1;
210 sc->subbus = supbus + 1;
216 * The i82380FB mobile docking controller is a PCI-PCI bridge,
217 * and it is a subtractive bridge. However, the ProgIf is wrong
218 * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
219 * happen. There's also a Toshiba bridge that behaves this
222 case 0x124b8086: /* Intel 82380FB Mobile */
223 case 0x060513d7: /* Toshiba ???? */
224 sc->flags |= PCIB_SUBTRACTIVE;
227 /* Compaq R3000 BIOS sets wrong subordinate bus number. */
232 if ((cp = kgetenv("smbios.planar.maker")) == NULL)
234 if (strncmp(cp, "Compal", 6) != 0) {
239 if ((cp = kgetenv("smbios.planar.product")) == NULL)
241 if (strncmp(cp, "08A0", 4) != 0) {
246 if (sc->subbus < 0xa) {
247 pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
248 sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
254 if (pci_msi_device_blacklisted(dev))
255 sc->flags |= PCIB_DISABLE_MSI;
258 * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
259 * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM,
260 * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
261 * This means they act as if they were subtractively decoding
262 * bridges and pass all transactions. Mark them and real ProgIf 1
263 * parts as subtractive.
265 if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
266 pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
267 sc->flags |= PCIB_SUBTRACTIVE;
270 device_printf(dev, " domain %d\n", sc->domain);
271 device_printf(dev, " secondary bus %d\n", sc->secbus);
272 device_printf(dev, " subordinate bus %d\n", sc->subbus);
273 device_printf(dev, " I/O decode 0x%x-0x%x\n", sc->iobase, sc->iolimit);
274 if (pcib_is_nonprefetch_open(sc))
275 device_printf(dev, " memory decode 0x%jx-0x%jx\n",
276 (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
277 if (pcib_is_prefetch_open(sc))
278 device_printf(dev, " prefetched decode 0x%jx-0x%jx\n",
279 (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
281 device_printf(dev, " no prefetched decode\n");
282 if (sc->flags & PCIB_SUBTRACTIVE)
283 device_printf(dev, " Subtractively decoded bridge.\n");
286 if (pci_is_pcie(dev) && pcie_slot_implemented(dev)) {
292 * Before proper PCI Express hot-plug support is in place,
293 * disable all hot-plug interrupts on the PCI Express root
294 * port or down stream port for now.
296 #define HPINTRS (PCIEM_SLTCTL_HPINTR_MASK | PCIEM_SLTCTL_HPINTR_EN)
298 ptr = pci_get_pciecap_ptr(dev);
299 slot_ctrl = pci_read_config(dev, ptr + PCIER_SLOTCTRL, 2);
300 if (slot_ctrl & HPINTRS) {
301 device_printf(dev, "Disable PCI Express hot-plug "
302 "interrupts(0x%04x)\n", slot_ctrl & HPINTRS);
303 slot_ctrl &= ~HPINTRS;
304 pci_write_config(dev, ptr + PCIER_SLOTCTRL, slot_ctrl, 2);
311 * XXX If the secondary bus number is zero, we should assign a bus number
312 * since the BIOS hasn't, then initialise the bridge.
316 * XXX If the subordinate bus number is less than the secondary bus number,
317 * we should pick a better value. One sensible alternative would be to
318 * pick 255; the only tradeoff here is that configuration transactions
319 * would be more widely routed than absolutely necessary.
324 pcib_attach(device_t dev)
326 struct pcib_softc *sc;
329 pcib_attach_common(dev);
330 sc = device_get_softc(dev);
331 if (sc->secbus != 0) {
332 child = device_add_child(dev, "pci", sc->secbus);
334 return(bus_generic_attach(dev));
337 /* no secondary bus; we should have fixed this */
342 pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
344 struct pcib_softc *sc = device_get_softc(dev);
347 case PCIB_IVAR_DOMAIN:
348 *result = sc->domain;
351 *result = sc->secbus;
358 pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
360 struct pcib_softc *sc = device_get_softc(dev);
363 case PCIB_IVAR_DOMAIN:
373 * We have to trap resource allocation requests and ensure that the bridge
374 * is set up to, or capable of handling them.
377 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
378 u_long start, u_long end, u_long count, u_int flags)
380 struct pcib_softc *sc = device_get_softc(dev);
381 const char *name, *suffix;
385 * Fail the allocation for this range if it's not supported.
387 name = device_get_nameunit(child);
396 if (!pcib_is_io_open(sc))
398 ok = (start >= sc->iobase && end <= sc->iolimit);
401 * Make sure we allow access to VGA I/O addresses when the
402 * bridge has the "VGA Enable" bit set.
404 if (!ok && pci_is_vga_ioport_range(start, end))
405 ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
407 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
409 if (start < sc->iobase)
411 if (end > sc->iolimit)
419 if (start < sc->iobase && end > sc->iolimit) {
426 device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
433 device_printf(dev, "%s%srequested unsupported I/O "
434 "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
435 name, suffix, start, end, sc->iobase, sc->iolimit);
440 "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
441 name, suffix, start, end);
446 if (pcib_is_nonprefetch_open(sc))
447 ok = ok || (start >= sc->membase && end <= sc->memlimit);
448 if (pcib_is_prefetch_open(sc))
449 ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
452 * Make sure we allow access to VGA memory addresses when the
453 * bridge has the "VGA Enable" bit set.
455 if (!ok && pci_is_vga_memory_range(start, end))
456 ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
458 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
461 if (flags & RF_PREFETCHABLE) {
462 if (pcib_is_prefetch_open(sc)) {
463 if (start < sc->pmembase)
464 start = sc->pmembase;
465 if (end > sc->pmemlimit)
470 } else { /* non-prefetchable */
471 if (pcib_is_nonprefetch_open(sc)) {
472 if (start < sc->membase)
474 if (end > sc->memlimit)
482 ok = 1; /* subtractive bridge: always ok */
484 if (pcib_is_nonprefetch_open(sc)) {
485 if (start < sc->membase && end > sc->memlimit) {
490 if (pcib_is_prefetch_open(sc)) {
491 if (start < sc->pmembase && end > sc->pmemlimit) {
492 start = sc->pmembase;
499 device_printf(dev, "memory: end (%lx) < start (%lx)\n",
505 if (!ok && bootverbose)
507 "%s%srequested unsupported memory range %#lx-%#lx "
508 "(decoding %#jx-%#jx, %#jx-%#jx)\n",
509 name, suffix, start, end,
510 (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
511 (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
515 device_printf(dev,"%s%srequested memory range "
516 "0x%lx-0x%lx: good\n",
517 name, suffix, start, end);
524 * Bridge is OK decoding this resource, so pass it up.
526 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
534 pcib_maxslots(device_t dev)
540 * Since we are a child of a PCI bus, its parent must support the pcib interface.
543 pcib_read_config(device_t dev, int b, int s, int f, int reg, int width)
545 return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
549 pcib_write_config(device_t dev, int b, int s, int f, int reg, uint32_t val, int width)
551 PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
555 * Route an interrupt across a PCI bridge.
558 pcib_route_interrupt(device_t pcib, device_t dev, int pin)
566 * The PCI standard defines a swizzle of the child-side device/intpin to
567 * the parent-side intpin as follows.
569 * device = device on child bus
570 * child_intpin = intpin on child bus slot (0-3)
571 * parent_intpin = intpin on parent bus slot (0-3)
573 * parent_intpin = (device + child_intpin) % 4
575 parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
578 * Our parent is a PCI bus. Its parent must export the pcib interface
579 * which includes the ability to route interrupts.
581 bus = device_get_parent(pcib);
582 intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
583 if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
584 device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
585 pci_get_slot(dev), 'A' + pin - 1, intnum);
590 /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
592 pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
594 struct pcib_softc *sc = device_get_softc(pcib);
597 if (sc->flags & PCIB_DISABLE_MSI)
599 bus = device_get_parent(pcib);
600 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
604 /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
606 pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
610 bus = device_get_parent(pcib);
611 return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
614 /* Pass request to alloc an MSI-X message up to the parent bridge. */
616 pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
618 struct pcib_softc *sc = device_get_softc(pcib);
621 if (sc->flags & PCIB_DISABLE_MSI)
623 bus = device_get_parent(pcib);
624 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
627 /* Pass request to release an MSI-X message up to the parent bridge. */
629 pcib_release_msix(device_t pcib, device_t dev, int irq)
633 bus = device_get_parent(pcib);
634 return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
637 /* Pass request to map MSI/MSI-X message up to parent bridge. */
639 pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
645 bus = device_get_parent(pcib);
646 error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
650 pci_ht_map_msi(pcib, *addr);
655 * Try to read the bus number of a host-PCI bridge using appropriate config
659 host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
664 id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
665 if (id == 0xffffffff)
671 /* XXX This is a guess */
672 /* *busnum = read_config(bus, slot, func, 0x41, 1); */
676 /* Intel 82454KX/GX (Orion) */
677 *busnum = read_config(bus, slot, func, 0x4a, 1);
681 * For the 450nx chipset, there is a whole bundle of
682 * things pretending to be host bridges. The MIOC will
683 * be seen first and isn't really a pci bridge (the
684 * actual busses are attached to the PXB's). We need to
685 * read the registers of the MIOC to figure out the
686 * bus numbers for the PXB channels.
688 * Since the MIOC doesn't have a pci bus attached, we
689 * pretend it wasn't there.
695 /* Intel 82454NX PXB#0, Bus#A */
696 *busnum = read_config(bus, 0x10, func, 0xd0, 1);
699 /* Intel 82454NX PXB#0, Bus#B */
700 *busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
703 /* Intel 82454NX PXB#1, Bus#A */
704 *busnum = read_config(bus, 0x10, func, 0xd3, 1);
707 /* Intel 82454NX PXB#1, Bus#B */
708 *busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
713 /* ServerWorks -- vendor 0x1166 */
725 *busnum = read_config(bus, slot, func, 0x44, 1);
728 /* Compaq/HP -- vendor 0x0e11 */
730 *busnum = read_config(bus, slot, func, 0xc8, 1);
733 /* Don't know how to read bus number. */