pci_subr.c revision 129876
169783Smsmith/*-
269783Smsmith * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
369783Smsmith * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
469783Smsmith * Copyright (c) 2000 BSDi
569783Smsmith * All rights reserved.
669783Smsmith *
769783Smsmith * Redistribution and use in source and binary forms, with or without
869783Smsmith * modification, are permitted provided that the following conditions
969783Smsmith * are met:
1069783Smsmith * 1. Redistributions of source code must retain the above copyright
1169783Smsmith *    notice, this list of conditions and the following disclaimer.
1269783Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1369783Smsmith *    notice, this list of conditions and the following disclaimer in the
1469783Smsmith *    documentation and/or other materials provided with the distribution.
1569783Smsmith * 3. The name of the author may not be used to endorse or promote products
1669783Smsmith *    derived from this software without specific prior written permission.
1769783Smsmith *
1869783Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1969783Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2069783Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2169783Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2269783Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2369783Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2469783Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2569783Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2669783Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2769783Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2869783Smsmith * SUCH DAMAGE.
2969783Smsmith */
3069783Smsmith
31119418Sobrien#include <sys/cdefs.h>
32119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/pci/pci_pci.c 129876 2004-05-30 17:57:46Z phk $");
33119418Sobrien
3469783Smsmith/*
3569783Smsmith * PCI:PCI bridge support.
3669783Smsmith */
3769783Smsmith
3869783Smsmith#include <sys/param.h>
3969783Smsmith#include <sys/systm.h>
4069783Smsmith#include <sys/kernel.h>
41129876Sphk#include <sys/module.h>
4269783Smsmith#include <sys/bus.h>
43107546Simp#include <machine/bus.h>
44107546Simp#include <sys/rman.h>
45106844Smdodd#include <sys/sysctl.h>
4669783Smsmith
4769783Smsmith#include <machine/resource.h>
4869783Smsmith
49119285Simp#include <dev/pci/pcivar.h>
50119285Simp#include <dev/pci/pcireg.h>
51119285Simp#include <dev/pci/pcib_private.h>
5269783Smsmith
5369783Smsmith#include "pcib_if.h"
5469783Smsmith
5569783Smsmithstatic int		pcib_probe(device_t dev);
5669783Smsmith
5769783Smsmithstatic device_method_t pcib_methods[] = {
5869783Smsmith    /* Device interface */
5969783Smsmith    DEVMETHOD(device_probe,		pcib_probe),
6069783Smsmith    DEVMETHOD(device_attach,		pcib_attach),
6169783Smsmith    DEVMETHOD(device_shutdown,		bus_generic_shutdown),
6269783Smsmith    DEVMETHOD(device_suspend,		bus_generic_suspend),
6369783Smsmith    DEVMETHOD(device_resume,		bus_generic_resume),
6469783Smsmith
6569783Smsmith    /* Bus interface */
6669783Smsmith    DEVMETHOD(bus_print_child,		bus_generic_print_child),
6769783Smsmith    DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
6869783Smsmith    DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
6969783Smsmith    DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
7069783Smsmith    DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
7169783Smsmith    DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
7269783Smsmith    DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
7369783Smsmith    DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
7469783Smsmith    DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
7569783Smsmith
7669783Smsmith    /* pcib interface */
7769783Smsmith    DEVMETHOD(pcib_maxslots,		pcib_maxslots),
7869783Smsmith    DEVMETHOD(pcib_read_config,		pcib_read_config),
7969783Smsmith    DEVMETHOD(pcib_write_config,	pcib_write_config),
8069783Smsmith    DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
8169783Smsmith
8269783Smsmith    { 0, 0 }
8369783Smsmith};
8469783Smsmith
8569783Smsmithstatic driver_t pcib_driver = {
8669783Smsmith    "pcib",
8769783Smsmith    pcib_methods,
8869783Smsmith    sizeof(struct pcib_softc),
8969783Smsmith};
9069783Smsmith
91102441Sjhbdevclass_t pcib_devclass;
9269783Smsmith
9369783SmsmithDRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
9469783Smsmith
9569783Smsmith/*
9669783Smsmith * Generic device interface
9769783Smsmith */
9869783Smsmithstatic int
9969783Smsmithpcib_probe(device_t dev)
10069783Smsmith{
10169783Smsmith    if ((pci_get_class(dev) == PCIC_BRIDGE) &&
10269783Smsmith	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
10369783Smsmith	device_set_desc(dev, "PCI-PCI bridge");
10469783Smsmith	return(-10000);
10569783Smsmith    }
10669783Smsmith    return(ENXIO);
10769783Smsmith}
10869783Smsmith
109102441Sjhbvoid
110102441Sjhbpcib_attach_common(device_t dev)
11169783Smsmith{
11269783Smsmith    struct pcib_softc	*sc;
113119266Simp    uint8_t		iolow;
11469783Smsmith
11569783Smsmith    sc = device_get_softc(dev);
11669783Smsmith    sc->dev = dev;
11769783Smsmith
11869908Smsmith    /*
11969908Smsmith     * Get current bridge configuration.
12069908Smsmith     */
12169953Smsmith    sc->command   = pci_read_config(dev, PCIR_COMMAND, 1);
12269908Smsmith    sc->secbus    = pci_read_config(dev, PCIR_SECBUS_1, 1);
12369908Smsmith    sc->subbus    = pci_read_config(dev, PCIR_SUBBUS_1, 1);
12469908Smsmith    sc->secstat   = pci_read_config(dev, PCIR_SECSTAT_1, 2);
12569908Smsmith    sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
12669908Smsmith    sc->seclat    = pci_read_config(dev, PCIR_SECLAT_1, 1);
12769783Smsmith
12869908Smsmith    /*
12969908Smsmith     * Determine current I/O decode.
13069908Smsmith     */
13169953Smsmith    if (sc->command & PCIM_CMD_PORTEN) {
13269953Smsmith	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
13369953Smsmith	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
13469953Smsmith	    sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2),
13569953Smsmith				       pci_read_config(dev, PCIR_IOBASEL_1, 1));
13669953Smsmith	} else {
13769953Smsmith	    sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1));
13869953Smsmith	}
13969908Smsmith
14069953Smsmith	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
14169953Smsmith	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
14269953Smsmith	    sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2),
14369953Smsmith					 pci_read_config(dev, PCIR_IOLIMITL_1, 1));
14469953Smsmith	} else {
14569953Smsmith	    sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1));
14669953Smsmith	}
14769908Smsmith    }
14869908Smsmith
14969908Smsmith    /*
15069908Smsmith     * Determine current memory decode.
15169908Smsmith     */
15269953Smsmith    if (sc->command & PCIM_CMD_MEMEN) {
15369953Smsmith	sc->membase   = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2));
15469953Smsmith	sc->memlimit  = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
15569953Smsmith	sc->pmembase  = PCI_PPBMEMBASE((pci_addr_t)pci_read_config(dev, PCIR_PMBASEH_1, 4),
15669953Smsmith				       pci_read_config(dev, PCIR_PMBASEL_1, 2));
15769953Smsmith	sc->pmemlimit = PCI_PPBMEMLIMIT((pci_addr_t)pci_read_config(dev, PCIR_PMLIMITH_1, 4),
15869953Smsmith					pci_read_config(dev, PCIR_PMLIMITL_1, 2));
15969953Smsmith    }
16069908Smsmith
16169908Smsmith    /*
16269908Smsmith     * Quirk handling.
16369908Smsmith     */
16469908Smsmith    switch (pci_get_devid(dev)) {
165124365Simp    case 0x12258086:		/* Intel 82454KX/GX (Orion) */
16669908Smsmith	{
167119266Simp	    uint8_t	supbus;
16869908Smsmith
16969908Smsmith	    supbus = pci_read_config(dev, 0x41, 1);
17069908Smsmith	    if (supbus != 0xff) {
17169908Smsmith		sc->secbus = supbus + 1;
17269908Smsmith		sc->subbus = supbus + 1;
17369908Smsmith	    }
174124365Simp	    break;
17569908Smsmith	}
176124365Simp
177124365Simp    /*
178124365Simp     * The i82380FB mobile docking controller is a PCI-PCI bridge,
179124365Simp     * and it is a subtractive bridge.  However, the ProgIf is wrong
180124365Simp     * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
181124365Simp     * happen.  There's also a Toshiba bridge that behaves this
182124365Simp     * way.
183124365Simp     */
184124365Simp    case 0x124b8086:		/* Intel 82380FB Mobile */
185124365Simp    case 0x060513d7:		/* Toshiba ???? */
186124365Simp	sc->flags |= PCIB_SUBTRACTIVE;
18769908Smsmith	break;
18869908Smsmith    }
18969908Smsmith
190124365Simp    /*
191124365Simp     * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
192124365Simp     * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
193124365Simp     * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
194124365Simp     * This means they act as if they were subtractively decoding
195124365Simp     * bridges and pass all transactions.  Mark them and real ProgIf 1
196124365Simp     * parts as subtractive.
197124365Simp     */
198124365Simp    if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
199124365Simp      pci_read_config(dev, PCIR_PROGIF, 1) == 1)
200124365Simp	sc->flags |= PCIB_SUBTRACTIVE;
201124365Simp
20269783Smsmith    if (bootverbose) {
20369783Smsmith	device_printf(dev, "  secondary bus     %d\n", sc->secbus);
20469783Smsmith	device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
20569783Smsmith	device_printf(dev, "  I/O decode        0x%x-0x%x\n", sc->iobase, sc->iolimit);
20669783Smsmith	device_printf(dev, "  memory decode     0x%x-0x%x\n", sc->membase, sc->memlimit);
20769783Smsmith	device_printf(dev, "  prefetched decode 0x%x-0x%x\n", sc->pmembase, sc->pmemlimit);
208124365Simp	if (sc->flags & PCIB_SUBTRACTIVE)
209124365Simp	    device_printf(dev, "  Subtractively decoded bridge.\n");
21069783Smsmith    }
21169783Smsmith
21269783Smsmith    /*
21369783Smsmith     * XXX If the secondary bus number is zero, we should assign a bus number
21469783Smsmith     *     since the BIOS hasn't, then initialise the bridge.
21569783Smsmith     */
21669783Smsmith
21769783Smsmith    /*
21869783Smsmith     * XXX If the subordinate bus number is less than the secondary bus number,
21969783Smsmith     *     we should pick a better value.  One sensible alternative would be to
22069783Smsmith     *     pick 255; the only tradeoff here is that configuration transactions
22169783Smsmith     *     would be more widely routed than absolutely necessary.
22269783Smsmith     */
223102441Sjhb}
22469783Smsmith
225103042Sjhbint
226102441Sjhbpcib_attach(device_t dev)
227102441Sjhb{
228102441Sjhb    struct pcib_softc	*sc;
229102441Sjhb    device_t		child;
230102441Sjhb
231102441Sjhb    pcib_attach_common(dev);
232102441Sjhb    sc = device_get_softc(dev);
23369783Smsmith    if (sc->secbus != 0) {
234103016Sjhb	child = device_add_child(dev, "pci", sc->secbus);
23569783Smsmith	if (child != NULL)
23669783Smsmith	    return(bus_generic_attach(dev));
23769783Smsmith    }
23869783Smsmith
23969783Smsmith    /* no secondary bus; we should have fixed this */
24069783Smsmith    return(0);
24169783Smsmith}
24269783Smsmith
243102441Sjhbint
24469783Smsmithpcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
24569783Smsmith{
24669783Smsmith    struct pcib_softc	*sc = device_get_softc(dev);
24769783Smsmith
24869783Smsmith    switch (which) {
24969783Smsmith    case PCIB_IVAR_BUS:
25069783Smsmith	*result = sc->secbus;
25169783Smsmith	return(0);
25269783Smsmith    }
25369783Smsmith    return(ENOENT);
25469783Smsmith}
25569783Smsmith
256102441Sjhbint
25769783Smsmithpcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
25869783Smsmith{
25969783Smsmith    struct pcib_softc	*sc = device_get_softc(dev);
26069783Smsmith
26169783Smsmith    switch (which) {
26269783Smsmith    case PCIB_IVAR_BUS:
26369783Smsmith	sc->secbus = value;
26469783Smsmith	break;
26569783Smsmith    }
26669783Smsmith    return(ENOENT);
26769783Smsmith}
26869783Smsmith
26969783Smsmith/*
270107546Simp * Is the prefetch window open (eg, can we allocate memory in it?)
271107546Simp */
272107546Simpstatic int
273107546Simppcib_is_prefetch_open(struct pcib_softc *sc)
274107546Simp{
275124365Simp	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
276107546Simp}
277107546Simp
278107546Simp/*
279107546Simp * Is the nonprefetch window open (eg, can we allocate memory in it?)
280107546Simp */
281107546Simpstatic int
282107546Simppcib_is_nonprefetch_open(struct pcib_softc *sc)
283107546Simp{
284124365Simp	return (sc->membase > 0 && sc->membase < sc->memlimit);
285107546Simp}
286107546Simp
287107546Simp/*
288107546Simp * Is the io window open (eg, can we allocate ports in it?)
289107546Simp */
290107546Simpstatic int
291107546Simppcib_is_io_open(struct pcib_softc *sc)
292107546Simp{
293124365Simp	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
294107546Simp}
295107546Simp
296107546Simp/*
29769783Smsmith * We have to trap resource allocation requests and ensure that the bridge
29869783Smsmith * is set up to, or capable of handling them.
29969783Smsmith */
300102441Sjhbstruct resource *
30169783Smsmithpcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
30269783Smsmith		    u_long start, u_long end, u_long count, u_int flags)
30369783Smsmith{
304124365Simp	struct pcib_softc	*sc = device_get_softc(dev);
305124365Simp	int ok;
30669783Smsmith
30769783Smsmith	/*
30869783Smsmith	 * Fail the allocation for this range if it's not supported.
30969783Smsmith	 */
31069783Smsmith	switch (type) {
31169783Smsmith	case SYS_RES_IOPORT:
312107546Simp		ok = 0;
313124365Simp		if (!pcib_is_io_open(sc))
314124365Simp			break;
315124365Simp		ok = (start >= sc->iobase && end <= sc->iolimit);
316124365Simp		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
317124365Simp			if (!ok) {
318124365Simp				if (start < sc->iobase)
319124365Simp					start = sc->iobase;
320124365Simp				if (end > sc->iolimit)
321124365Simp					end = sc->iolimit;
322124365Simp			}
323106844Smdodd		} else {
324124365Simp			ok = 1;
325128058Simp#if 1
326124365Simp			if (start < sc->iobase && end > sc->iolimit) {
327124365Simp				start = sc->iobase;
328124365Simp				end = sc->iolimit;
329124365Simp			}
330124641Simp#endif
331106844Smdodd		}
332124365Simp		if (end < start) {
333124365Simp			device_printf(dev, "ioport: end (%lx) < start (%lx)\n", end, start);
334124365Simp			start = 0;
335124365Simp			end = 0;
336124365Simp			ok = 0;
337124365Simp		}
338124365Simp		if (!ok) {
339124365Simp			device_printf(dev, "device %s requested unsupported I/O "
340124365Simp			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
341124365Simp			    device_get_nameunit(child), start, end,
342124365Simp			    sc->iobase, sc->iolimit);
343124365Simp			return (NULL);
344124365Simp		}
345124365Simp		if (bootverbose)
346124365Simp			device_printf(dev, "device %s requested decoded I/O range 0x%lx-0x%lx\n",
347124365Simp			    device_get_nameunit(child), start, end);
348124365Simp		break;
34969783Smsmith
35069783Smsmith	case SYS_RES_MEMORY:
351107546Simp		ok = 0;
352107546Simp		if (pcib_is_nonprefetch_open(sc))
353124365Simp			ok = ok || (start >= sc->membase && end <= sc->memlimit);
354107546Simp		if (pcib_is_prefetch_open(sc))
355124365Simp			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
356124365Simp		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
357124365Simp			if (!ok) {
358124365Simp				ok = 1;
359124365Simp				if (flags & RF_PREFETCHABLE) {
360124365Simp					if (pcib_is_prefetch_open(sc)) {
361124365Simp						if (start < sc->pmembase)
362124365Simp							start = sc->pmembase;
363124365Simp						if (end > sc->pmemlimit)
364124365Simp							end = sc->pmemlimit;
365124365Simp					} else {
366124365Simp						ok = 0;
367124365Simp					}
368124365Simp				} else {	/* non-prefetchable */
369124365Simp					if (pcib_is_nonprefetch_open(sc)) {
370124365Simp						if (start < sc->membase)
371124365Simp							start = sc->membase;
372124365Simp						if (end > sc->memlimit)
373124365Simp							end = sc->memlimit;
374124365Simp					} else {
375124365Simp						ok = 0;
376124365Simp					}
377124365Simp				}
378107546Simp			}
379107546Simp		} else if (!ok) {
380124365Simp			ok = 1;	/* subtractive bridge: always ok */
381128058Simp#if 1
382124365Simp			if (pcib_is_nonprefetch_open(sc)) {
383124365Simp				if (start < sc->membase && end > sc->memlimit) {
384124365Simp					start = sc->membase;
385124365Simp					end = sc->memlimit;
386124365Simp				}
387124365Simp			}
388124365Simp			if (pcib_is_prefetch_open(sc)) {
389124365Simp				if (start < sc->pmembase && end > sc->pmemlimit) {
390124365Simp					start = sc->pmembase;
391124365Simp					end = sc->pmemlimit;
392124365Simp				}
393124365Simp			}
394124641Simp#endif
395106844Smdodd		}
396124365Simp		if (end < start) {
397124365Simp			device_printf(dev, "memory: end (%lx) < start (%lx)\n", end, start);
398124365Simp			start = 0;
399124365Simp			end = 0;
400124365Simp			ok = 0;
401124365Simp		}
402124365Simp		if (!ok && bootverbose)
403124365Simp			device_printf(dev,
404124365Simp			    "device %s requested unsupported memory range "
405124365Simp			    "0x%lx-0x%lx (decoding 0x%x-0x%x, 0x%x-0x%x)\n",
406124365Simp			    device_get_nameunit(child), start, end,
407124365Simp			    sc->membase, sc->memlimit, sc->pmembase,
408124365Simp			    sc->pmemlimit);
409124365Simp		if (!ok)
410124365Simp			return (NULL);
411124365Simp		if (bootverbose)
412124365Simp			device_printf(dev,"device %s requested decoded memory range 0x%lx-0x%lx\n",
413124365Simp			    device_get_nameunit(child), start, end);
414124365Simp		break;
41569908Smsmith
41669783Smsmith	default:
417124365Simp		break;
41869783Smsmith	}
419124365Simp	/*
420124365Simp	 * Bridge is OK decoding this resource, so pass it up.
421124365Simp	 */
422124365Simp	return (bus_generic_alloc_resource(dev, child, type, rid, start, end, count, flags));
42369783Smsmith}
42469783Smsmith
42569783Smsmith/*
42669783Smsmith * PCIB interface.
42769783Smsmith */
428102441Sjhbint
42969783Smsmithpcib_maxslots(device_t dev)
43069783Smsmith{
43169908Smsmith    return(PCI_SLOTMAX);
43269783Smsmith}
43369783Smsmith
43469783Smsmith/*
43569783Smsmith * Since we are a child of a PCI bus, its parent must support the pcib interface.
43669783Smsmith */
437119266Simpuint32_t
43869783Smsmithpcib_read_config(device_t dev, int b, int s, int f, int reg, int width)
43969783Smsmith{
44069783Smsmith    return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
44169783Smsmith}
44269783Smsmith
443102441Sjhbvoid
444119266Simppcib_write_config(device_t dev, int b, int s, int f, int reg, uint32_t val, int width)
44569783Smsmith{
44669783Smsmith    PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
44769783Smsmith}
44869783Smsmith
44969783Smsmith/*
45069783Smsmith * Route an interrupt across a PCI bridge.
45169783Smsmith */
452109229Sbennoint
45369783Smsmithpcib_route_interrupt(device_t pcib, device_t dev, int pin)
45469783Smsmith{
45569783Smsmith    device_t	bus;
45669783Smsmith    int		parent_intpin;
45769783Smsmith    int		intnum;
45869783Smsmith
45969783Smsmith    /*
46069783Smsmith     *
46169783Smsmith     * The PCI standard defines a swizzle of the child-side device/intpin to
46269783Smsmith     * the parent-side intpin as follows.
46369783Smsmith     *
46469783Smsmith     * device = device on child bus
46569783Smsmith     * child_intpin = intpin on child bus slot (0-3)
46669783Smsmith     * parent_intpin = intpin on parent bus slot (0-3)
46769783Smsmith     *
46869783Smsmith     * parent_intpin = (device + child_intpin) % 4
46969783Smsmith     */
470115234Sticso    parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
47169783Smsmith
47269783Smsmith    /*
47369783Smsmith     * Our parent is a PCI bus.  Its parent must export the pcib interface
47469783Smsmith     * which includes the ability to route interrupts.
47569783Smsmith     */
47669783Smsmith    bus = device_get_parent(pcib);
47769783Smsmith    intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
47890554Smsmith    if (PCI_INTERRUPT_VALID(intnum)) {
479102977Sjhb	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
480102977Sjhb	    pci_get_slot(dev), 'A' + pin - 1, intnum);
48190554Smsmith    }
48269783Smsmith    return(intnum);
48369783Smsmith}
484107172Sjhb
485107172Sjhb/*
486107172Sjhb * Try to read the bus number of a host-PCI bridge using appropriate config
487107172Sjhb * registers.
488107172Sjhb */
489107172Sjhbint
490107172Sjhbhost_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
491119266Simp    uint8_t *busnum)
492107172Sjhb{
493119266Simp	uint32_t id;
494107172Sjhb
495107172Sjhb	id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
496107248Sjhb	if (id == 0xffffffff)
497107172Sjhb		return (0);
498107172Sjhb
499107172Sjhb	switch (id) {
500107172Sjhb	case 0x12258086:
501107172Sjhb		/* Intel 824?? */
502107172Sjhb		/* XXX This is a guess */
503107172Sjhb		/* *busnum = read_config(bus, slot, func, 0x41, 1); */
504107172Sjhb		*busnum = bus;
505107172Sjhb		break;
506107172Sjhb	case 0x84c48086:
507107172Sjhb		/* Intel 82454KX/GX (Orion) */
508107172Sjhb		*busnum = read_config(bus, slot, func, 0x4a, 1);
509107172Sjhb		break;
510107172Sjhb	case 0x84ca8086:
511107172Sjhb		/*
512107172Sjhb		 * For the 450nx chipset, there is a whole bundle of
513107172Sjhb		 * things pretending to be host bridges. The MIOC will
514107172Sjhb		 * be seen first and isn't really a pci bridge (the
515107172Sjhb		 * actual busses are attached to the PXB's). We need to
516107172Sjhb		 * read the registers of the MIOC to figure out the
517107172Sjhb		 * bus numbers for the PXB channels.
518107172Sjhb		 *
519107172Sjhb		 * Since the MIOC doesn't have a pci bus attached, we
520107172Sjhb		 * pretend it wasn't there.
521107172Sjhb		 */
522107172Sjhb		return (0);
523107172Sjhb	case 0x84cb8086:
524107172Sjhb		switch (slot) {
525107172Sjhb		case 0x12:
526107172Sjhb			/* Intel 82454NX PXB#0, Bus#A */
527107248Sjhb			*busnum = read_config(bus, 0x10, func, 0xd0, 1);
528107172Sjhb			break;
529107172Sjhb		case 0x13:
530107172Sjhb			/* Intel 82454NX PXB#0, Bus#B */
531107248Sjhb			*busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
532107172Sjhb			break;
533107172Sjhb		case 0x14:
534107172Sjhb			/* Intel 82454NX PXB#1, Bus#A */
535107248Sjhb			*busnum = read_config(bus, 0x10, func, 0xd3, 1);
536107172Sjhb			break;
537107172Sjhb		case 0x15:
538107172Sjhb			/* Intel 82454NX PXB#1, Bus#B */
539107248Sjhb			*busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
540107172Sjhb			break;
541107172Sjhb		}
542107172Sjhb		break;
543107172Sjhb
544107172Sjhb		/* ServerWorks -- vendor 0x1166 */
545107172Sjhb	case 0x00051166:
546107172Sjhb	case 0x00061166:
547107172Sjhb	case 0x00081166:
548107172Sjhb	case 0x00091166:
549107172Sjhb	case 0x00101166:
550107172Sjhb	case 0x00111166:
551107172Sjhb	case 0x00171166:
552107172Sjhb	case 0x01011166:
553107172Sjhb	case 0x010f1014:
554107172Sjhb	case 0x02011166:
555107172Sjhb	case 0x03021014:
556107172Sjhb		*busnum = read_config(bus, slot, func, 0x44, 1);
557107172Sjhb		break;
558107172Sjhb	default:
559107172Sjhb		/* Don't know how to read bus number. */
560107172Sjhb		return 0;
561107172Sjhb	}
562107172Sjhb
563107172Sjhb	return 1;
564107172Sjhb}
565