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
34 * PCI:PCI bridge support.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
42 #include <machine/resource.h>
44 #include <pci/pcivar.h>
45 #include <pci/pcireg.h>
46 #include <pci/pcib_private.h>
51 static int pcib_probe(device_t dev);
52 static int pcib_attach(device_t dev);
53 static int pcib_route_interrupt(device_t pcib, device_t dev, int pin);
55 static device_method_t pcib_methods[] = {
56 /* Device interface */
57 DEVMETHOD(device_probe, pcib_probe),
58 DEVMETHOD(device_attach, pcib_attach),
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),
83 static driver_t pcib_driver = {
86 sizeof(struct pcib_softc),
89 devclass_t pcib_devclass;
91 DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
94 * Generic device interface
97 pcib_probe(device_t dev)
99 if ((pci_get_class(dev) == PCIC_BRIDGE) &&
100 (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
101 device_set_desc(dev, "PCI-PCI bridge");
108 pcib_attach_common(device_t dev)
110 struct pcib_softc *sc;
113 sc = device_get_softc(dev);
117 * Get current bridge configuration.
119 sc->command = pci_read_config(dev, PCIR_COMMAND, 1);
120 sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1);
121 sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
122 sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2);
123 sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
124 sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1);
127 * Determine current I/O decode.
129 if (sc->command & PCIM_CMD_PORTEN) {
130 iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
131 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
132 sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2),
133 pci_read_config(dev, PCIR_IOBASEL_1, 1));
135 sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1));
138 iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
139 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
140 sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2),
141 pci_read_config(dev, PCIR_IOLIMITL_1, 1));
143 sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1));
148 * Determine current memory decode.
150 if (sc->command & PCIM_CMD_MEMEN) {
151 sc->membase = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2));
152 sc->memlimit = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
153 sc->pmembase = PCI_PPBMEMBASE((pci_addr_t)pci_read_config(dev, PCIR_PMBASEH_1, 4),
154 pci_read_config(dev, PCIR_PMBASEL_1, 2));
155 sc->pmemlimit = PCI_PPBMEMLIMIT((pci_addr_t)pci_read_config(dev, PCIR_PMLIMITH_1, 4),
156 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
162 switch (pci_get_devid(dev)) {
163 case 0x12258086: /* Intel 82454KX/GX (Orion) */
167 supbus = pci_read_config(dev, 0x41, 1);
168 if (supbus != 0xff) {
169 sc->secbus = supbus + 1;
170 sc->subbus = supbus + 1;
177 device_printf(dev, " secondary bus %d\n", sc->secbus);
178 device_printf(dev, " subordinate bus %d\n", sc->subbus);
179 device_printf(dev, " I/O decode 0x%x-0x%x\n", sc->iobase, sc->iolimit);
180 device_printf(dev, " memory decode 0x%x-0x%x\n", sc->membase, sc->memlimit);
181 device_printf(dev, " prefetched decode 0x%x-0x%x\n", sc->pmembase, sc->pmemlimit);
185 * XXX If the secondary bus number is zero, we should assign a bus number
186 * since the BIOS hasn't, then initialise the bridge.
190 * XXX If the subordinate bus number is less than the secondary bus number,
191 * we should pick a better value. One sensible alternative would be to
192 * pick 255; the only tradeoff here is that configuration transactions
193 * would be more widely routed than absolutely necessary.
198 pcib_attach(device_t dev)
200 struct pcib_softc *sc;
203 pcib_attach_common(dev);
204 sc = device_get_softc(dev);
205 if (sc->secbus != 0) {
206 child = device_add_child(dev, "pci", -1);
208 return(bus_generic_attach(dev));
211 /* no secondary bus; we should have fixed this */
216 pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
218 struct pcib_softc *sc = device_get_softc(dev);
222 *result = sc->secbus;
229 pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
231 struct pcib_softc *sc = device_get_softc(dev);
242 * Is this a decoded ISA I/O port address? Note, we need to do the mask that
243 * we do below because of the ISA alias addresses. I'm not 100% sure that
247 pcib_is_isa_io(u_long start)
249 if ((start & 0xfffUL) > 0x3ffUL || start == 0)
255 * Is this a decoded ISA memory address?
258 pcib_is_isa_mem(u_long start)
260 if (start > 0xfffffUL || start == 0)
266 * We have to trap resource allocation requests and ensure that the bridge
267 * is set up to, or capable of handling them.
270 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
271 u_long start, u_long end, u_long count, u_int flags)
273 struct pcib_softc *sc = device_get_softc(dev);
276 * If this is a "default" allocation against this rid, we can't work
277 * out where it's coming from (we should actually never see these) so we
280 if ((start == 0) && (end == ~0)) {
281 device_printf(dev, "can't decode default resource id %d for %s%d, bypassing\n",
282 *rid, device_get_name(child), device_get_unit(child));
285 * Fail the allocation for this range if it's not supported.
287 * XXX we should probably just fix up the bridge decode and soldier on.
291 if (!pcib_is_isa_io(start)) {
292 #ifndef PCI_ALLOW_UNSUPPORTED_IO_RANGE
293 if (start < sc->iobase)
295 if (end > sc->iolimit)
300 if (start < sc->iobase)
301 printf("start (%lx) < sc->iobase (%x)\n", start, sc->iobase);
302 if (end > sc->iolimit)
303 printf("end (%lx) > sc->iolimit (%x)\n", end, sc->iolimit);
305 printf("end (%lx) < start (%lx)\n", end, start);
308 if (!pcib_is_isa_io(start) &&
309 ((start < sc->iobase) || (end > sc->iolimit))) {
310 device_printf(dev, "device %s%d requested unsupported I/O range 0x%lx-0x%lx"
311 " (decoding 0x%x-0x%x)\n",
312 device_get_name(child), device_get_unit(child), start, end,
313 sc->iobase, sc->iolimit);
317 device_printf(sc->dev, "device %s%d requested decoded I/O range 0x%lx-0x%lx\n",
318 device_get_name(child), device_get_unit(child), start, end);
322 * XXX will have to decide whether the device making the request is asking
323 * for prefetchable memory or not. If it's coming from another bridge
324 * down the line, do we assume not, or ask the bridge to pass in another
325 * flag as the request bubbles up?
328 if (!pcib_is_isa_mem(start)) {
329 #ifndef PCI_ALLOW_UNSUPPORTED_IO_RANGE
330 if (start < sc->membase && end >= sc->membase)
332 if (end > sc->memlimit)
337 if (start < sc->membase && end > sc->membase)
338 printf("start (%lx) < sc->membase (%x)\n", start, sc->membase);
339 if (end > sc->memlimit)
340 printf("end (%lx) > sc->memlimit (%x)\n", end, sc->memlimit);
342 printf("end (%lx) < start (%lx)\n", end, start);
345 if (!pcib_is_isa_mem(start) &&
346 (((start < sc->membase) || (end > sc->memlimit)) &&
347 ((start < sc->pmembase) || (end > sc->pmemlimit)))) {
350 "device %s%d requested unsupported memory range "
351 "0x%lx-0x%lx (decoding 0x%x-0x%x, 0x%x-0x%x)\n",
352 device_get_name(child), device_get_unit(child), start,
353 end, sc->membase, sc->memlimit, sc->pmembase,
355 #ifndef PCI_ALLOW_UNSUPPORTED_IO_RANGE
360 device_printf(sc->dev, "device %s%d requested decoded memory range 0x%lx-0x%lx\n",
361 device_get_name(child), device_get_unit(child), start, end);
370 * Bridge is OK decoding this resource, so pass it up.
372 return(bus_generic_alloc_resource(dev, child, type, rid, start, end, count, flags));
379 pcib_maxslots(device_t dev)
385 * Since we are a child of a PCI bus, its parent must support the pcib interface.
388 pcib_read_config(device_t dev, int b, int s, int f, int reg, int width)
390 return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
394 pcib_write_config(device_t dev, int b, int s, int f, int reg, u_int32_t val, int width)
396 PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
400 * Route an interrupt across a PCI bridge.
403 pcib_route_interrupt(device_t pcib, device_t dev, int pin)
411 * The PCI standard defines a swizzle of the child-side device/intpin to
412 * the parent-side intpin as follows.
414 * device = device on child bus
415 * child_intpin = intpin on child bus slot (0-3)
416 * parent_intpin = intpin on parent bus slot (0-3)
418 * parent_intpin = (device + child_intpin) % 4
420 parent_intpin = (pci_get_slot(pcib) + (pin - 1)) % 4;
423 * Our parent is a PCI bus. Its parent must export the pcib interface
424 * which includes the ability to route interrupts.
426 bus = device_get_parent(pcib);
427 intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
428 if (PCI_INTERRUPT_VALID(intnum)) {
429 device_printf(pcib, "routed slot %d INT%c to irq %d\n", pci_get_slot(dev),
430 'A' + pin - 1, intnum);