xref: /freebsd/sys/dev/pci/pci_pci.c (revision bb0d0a8efc748ae3a7a6f639d373bac067cf8ba1)
1bb0d0a8eSMike Smith /*-
2bb0d0a8eSMike Smith  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3bb0d0a8eSMike Smith  * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4bb0d0a8eSMike Smith  * Copyright (c) 2000 BSDi
5bb0d0a8eSMike Smith  * All rights reserved.
6bb0d0a8eSMike Smith  *
7bb0d0a8eSMike Smith  * Redistribution and use in source and binary forms, with or without
8bb0d0a8eSMike Smith  * modification, are permitted provided that the following conditions
9bb0d0a8eSMike Smith  * are met:
10bb0d0a8eSMike Smith  * 1. Redistributions of source code must retain the above copyright
11bb0d0a8eSMike Smith  *    notice, this list of conditions and the following disclaimer.
12bb0d0a8eSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
13bb0d0a8eSMike Smith  *    notice, this list of conditions and the following disclaimer in the
14bb0d0a8eSMike Smith  *    documentation and/or other materials provided with the distribution.
15bb0d0a8eSMike Smith  * 3. The name of the author may not be used to endorse or promote products
16bb0d0a8eSMike Smith  *    derived from this software without specific prior written permission.
17bb0d0a8eSMike Smith  *
18bb0d0a8eSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19bb0d0a8eSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20bb0d0a8eSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21bb0d0a8eSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22bb0d0a8eSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23bb0d0a8eSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24bb0d0a8eSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25bb0d0a8eSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26bb0d0a8eSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27bb0d0a8eSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28bb0d0a8eSMike Smith  * SUCH DAMAGE.
29bb0d0a8eSMike Smith  *
30bb0d0a8eSMike Smith  *	$FreeBSD$
31bb0d0a8eSMike Smith  */
32bb0d0a8eSMike Smith 
33bb0d0a8eSMike Smith /*
34bb0d0a8eSMike Smith  * PCI:PCI bridge support.
35bb0d0a8eSMike Smith  */
36bb0d0a8eSMike Smith 
37bb0d0a8eSMike Smith #include <sys/param.h>
38bb0d0a8eSMike Smith #include <sys/systm.h>
39bb0d0a8eSMike Smith #include <sys/kernel.h>
40bb0d0a8eSMike Smith #include <sys/bus.h>
41bb0d0a8eSMike Smith 
42bb0d0a8eSMike Smith #include <machine/resource.h>
43bb0d0a8eSMike Smith 
44bb0d0a8eSMike Smith #include <pci/pcivar.h>
45bb0d0a8eSMike Smith #include <pci/pcireg.h>
46bb0d0a8eSMike Smith 
47bb0d0a8eSMike Smith #include "pcib_if.h"
48bb0d0a8eSMike Smith 
49bb0d0a8eSMike Smith /*
50bb0d0a8eSMike Smith  * Bridge-specific data.
51bb0d0a8eSMike Smith  */
52bb0d0a8eSMike Smith struct pcib_softc
53bb0d0a8eSMike Smith {
54bb0d0a8eSMike Smith     device_t	dev;
55bb0d0a8eSMike Smith     u_int8_t	secbus;		/* secondary bus number */
56bb0d0a8eSMike Smith     u_int8_t	subbus;		/* subordinate bus number */
57bb0d0a8eSMike Smith     pci_addr_t	pmembase;	/* base address of prefetchable memory */
58bb0d0a8eSMike Smith     pci_addr_t	pmemlimit;	/* topmost address of prefetchable memory */
59bb0d0a8eSMike Smith     u_int32_t	membase;	/* base address of memory window */
60bb0d0a8eSMike Smith     u_int32_t	memlimit;	/* topmost address of memory window */
61bb0d0a8eSMike Smith     u_int32_t	iobase;		/* base address of port window */
62bb0d0a8eSMike Smith     u_int32_t	iolimit;	/* topmost address of port window */
63bb0d0a8eSMike Smith     u_int16_t	secstat;	/* secondary bus status register */
64bb0d0a8eSMike Smith     u_int16_t	bridgectl;	/* bridge control register */
65bb0d0a8eSMike Smith     u_int8_t	seclat;		/* secondary bus latency timer */
66bb0d0a8eSMike Smith };
67bb0d0a8eSMike Smith 
68bb0d0a8eSMike Smith static int		pcib_probe(device_t dev);
69bb0d0a8eSMike Smith static int		pcib_attach(device_t dev);
70bb0d0a8eSMike Smith static int		pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result);
71bb0d0a8eSMike Smith static int		pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value);
72bb0d0a8eSMike Smith static struct resource *pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
73bb0d0a8eSMike Smith 					    u_long start, u_long end, u_long count, u_int flags);
74bb0d0a8eSMike Smith static int		pcib_maxslots(device_t dev);
75bb0d0a8eSMike Smith static u_int32_t	pcib_read_config(device_t dev, int b, int s, int f, int reg, int width);
76bb0d0a8eSMike Smith static void		pcib_write_config(device_t dev, int b, int s, int f, int reg, u_int32_t val, int width);
77bb0d0a8eSMike Smith static int		pcib_route_interrupt(device_t pcib, device_t dev, int pin);
78bb0d0a8eSMike Smith 
79bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
80bb0d0a8eSMike Smith     /* Device interface */
81bb0d0a8eSMike Smith     DEVMETHOD(device_probe,		pcib_probe),
82bb0d0a8eSMike Smith     DEVMETHOD(device_attach,		pcib_attach),
83bb0d0a8eSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
84bb0d0a8eSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
85bb0d0a8eSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
86bb0d0a8eSMike Smith 
87bb0d0a8eSMike Smith     /* Bus interface */
88bb0d0a8eSMike Smith     DEVMETHOD(bus_print_child,		bus_generic_print_child),
89bb0d0a8eSMike Smith     DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
90bb0d0a8eSMike Smith     DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
91bb0d0a8eSMike Smith     DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
92bb0d0a8eSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
93bb0d0a8eSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
94bb0d0a8eSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
95bb0d0a8eSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
96bb0d0a8eSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
97bb0d0a8eSMike Smith 
98bb0d0a8eSMike Smith     /* pcib interface */
99bb0d0a8eSMike Smith     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
100bb0d0a8eSMike Smith     DEVMETHOD(pcib_read_config,		pcib_read_config),
101bb0d0a8eSMike Smith     DEVMETHOD(pcib_write_config,	pcib_write_config),
102bb0d0a8eSMike Smith     DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
103bb0d0a8eSMike Smith 
104bb0d0a8eSMike Smith     { 0, 0 }
105bb0d0a8eSMike Smith };
106bb0d0a8eSMike Smith 
107bb0d0a8eSMike Smith static driver_t pcib_driver = {
108bb0d0a8eSMike Smith     "pcib",
109bb0d0a8eSMike Smith     pcib_methods,
110bb0d0a8eSMike Smith     sizeof(struct pcib_softc),
111bb0d0a8eSMike Smith };
112bb0d0a8eSMike Smith 
113bb0d0a8eSMike Smith static devclass_t pcib_devclass;
114bb0d0a8eSMike Smith 
115bb0d0a8eSMike Smith DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
116bb0d0a8eSMike Smith 
117bb0d0a8eSMike Smith /*
118bb0d0a8eSMike Smith  * Generic device interface
119bb0d0a8eSMike Smith  */
120bb0d0a8eSMike Smith static int
121bb0d0a8eSMike Smith pcib_probe(device_t dev)
122bb0d0a8eSMike Smith {
123bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
124bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
125bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
126bb0d0a8eSMike Smith 	return(-10000);
127bb0d0a8eSMike Smith     }
128bb0d0a8eSMike Smith     return(ENXIO);
129bb0d0a8eSMike Smith }
130bb0d0a8eSMike Smith 
131bb0d0a8eSMike Smith static int
132bb0d0a8eSMike Smith pcib_attach(device_t dev)
133bb0d0a8eSMike Smith {
134bb0d0a8eSMike Smith     struct pcib_softc	*sc;
135bb0d0a8eSMike Smith     device_t		pcib, child;
136bb0d0a8eSMike Smith     int			b, s, f;
137bb0d0a8eSMike Smith 
138bb0d0a8eSMike Smith     sc = device_get_softc(dev);
139bb0d0a8eSMike Smith     sc->dev = dev;
140bb0d0a8eSMike Smith     pcib = device_get_parent(dev);
141bb0d0a8eSMike Smith     b = pci_get_bus(dev);
142bb0d0a8eSMike Smith     s = pci_get_slot(dev);
143bb0d0a8eSMike Smith     f = pci_get_function(dev);
144bb0d0a8eSMike Smith 
145bb0d0a8eSMike Smith     sc->secbus    = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_SECBUS_1, 1);
146bb0d0a8eSMike Smith     sc->subbus    = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_SUBBUS_1, 1);
147bb0d0a8eSMike Smith     sc->secstat   = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_SECSTAT_1, 2);
148bb0d0a8eSMike Smith     sc->bridgectl = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_BRIDGECTL_1, 2);
149bb0d0a8eSMike Smith     sc->seclat    = PCIB_READ_CONFIG(pcib, b, s, f, PCIR_SECLAT_1, 1);
150bb0d0a8eSMike Smith     sc->iobase    = PCI_PPBIOBASE(PCIB_READ_CONFIG(pcib, b, s, f, PCIR_IOBASEH_1, 2),
151bb0d0a8eSMike Smith 				  PCIB_READ_CONFIG(pcib, b, s, f, PCIR_IOBASEL_1, 1));
152bb0d0a8eSMike Smith     sc->iolimit   = PCI_PPBIOLIMIT(PCIB_READ_CONFIG(pcib, b, s, f, PCIR_IOLIMITH_1, 2),
153bb0d0a8eSMike Smith 				   PCIB_READ_CONFIG(pcib, b, s, f, PCIR_IOLIMITL_1, 1));
154bb0d0a8eSMike Smith     sc->membase   = PCI_PPBMEMBASE(0, PCIB_READ_CONFIG(pcib, b, s, f, PCIR_MEMBASE_1, 2));
155bb0d0a8eSMike Smith     sc->memlimit  = PCI_PPBMEMLIMIT(0, PCIB_READ_CONFIG(pcib, b, s, f, PCIR_MEMLIMIT_1, 2));
156bb0d0a8eSMike Smith     sc->pmembase  = PCI_PPBMEMBASE((pci_addr_t)PCIB_READ_CONFIG(pcib, b, s, f, PCIR_PMBASEH_1, 4),
157bb0d0a8eSMike Smith 				   PCIB_READ_CONFIG(pcib, b, s, f, PCIR_PMBASEL_1, 2));
158bb0d0a8eSMike Smith     sc->pmemlimit = PCI_PPBMEMLIMIT((pci_addr_t)PCIB_READ_CONFIG(pcib, b, s, f,PCIR_PMLIMITH_1, 4),
159bb0d0a8eSMike Smith 				    PCIB_READ_CONFIG(pcib, b, s, f, PCIR_PMLIMITL_1, 2));
160bb0d0a8eSMike Smith 
161bb0d0a8eSMike Smith     if (bootverbose) {
162bb0d0a8eSMike Smith 	device_printf(dev, "  secondary bus     %d\n", sc->secbus);
163bb0d0a8eSMike Smith 	device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
164bb0d0a8eSMike Smith 	device_printf(dev, "  I/O decode        0x%x-0x%x\n", sc->iobase, sc->iolimit);
165bb0d0a8eSMike Smith 	device_printf(dev, "  memory decode     0x%x-0x%x\n", sc->membase, sc->memlimit);
166bb0d0a8eSMike Smith 	device_printf(dev, "  prefetched decode 0x%x-0x%x\n", sc->pmembase, sc->pmemlimit);
167bb0d0a8eSMike Smith     }
168bb0d0a8eSMike Smith 
169bb0d0a8eSMike Smith     /*
170bb0d0a8eSMike Smith      * XXX If the secondary bus number is zero, we should assign a bus number
171bb0d0a8eSMike Smith      *     since the BIOS hasn't, then initialise the bridge.
172bb0d0a8eSMike Smith      */
173bb0d0a8eSMike Smith 
174bb0d0a8eSMike Smith     /*
175bb0d0a8eSMike Smith      * XXX If the subordinate bus number is less than the secondary bus number,
176bb0d0a8eSMike Smith      *     we should pick a better value.  One sensible alternative would be to
177bb0d0a8eSMike Smith      *     pick 255; the only tradeoff here is that configuration transactions
178bb0d0a8eSMike Smith      *     would be more widely routed than absolutely necessary.
179bb0d0a8eSMike Smith      */
180bb0d0a8eSMike Smith 
181bb0d0a8eSMike Smith     if (sc->secbus != 0) {
182bb0d0a8eSMike Smith 	child = device_add_child(dev, "pci", -1);
183bb0d0a8eSMike Smith 	if (child != NULL)
184bb0d0a8eSMike Smith 	    return(bus_generic_attach(dev));
185bb0d0a8eSMike Smith     }
186bb0d0a8eSMike Smith 
187bb0d0a8eSMike Smith     /* no secondary bus; we should have fixed this */
188bb0d0a8eSMike Smith     return(0);
189bb0d0a8eSMike Smith }
190bb0d0a8eSMike Smith 
191bb0d0a8eSMike Smith static int
192bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
193bb0d0a8eSMike Smith {
194bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
195bb0d0a8eSMike Smith 
196bb0d0a8eSMike Smith     switch (which) {
197bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
198bb0d0a8eSMike Smith 	*result = sc->secbus;
199bb0d0a8eSMike Smith 	return(0);
200bb0d0a8eSMike Smith     }
201bb0d0a8eSMike Smith     return(ENOENT);
202bb0d0a8eSMike Smith }
203bb0d0a8eSMike Smith 
204bb0d0a8eSMike Smith static int
205bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
206bb0d0a8eSMike Smith {
207bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
208bb0d0a8eSMike Smith 
209bb0d0a8eSMike Smith     switch (which) {
210bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
211bb0d0a8eSMike Smith 	sc->secbus = value;
212bb0d0a8eSMike Smith 	break;
213bb0d0a8eSMike Smith     }
214bb0d0a8eSMike Smith     return(ENOENT);
215bb0d0a8eSMike Smith }
216bb0d0a8eSMike Smith 
217bb0d0a8eSMike Smith /*
218bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
219bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
220bb0d0a8eSMike Smith  */
221bb0d0a8eSMike Smith static struct resource *
222bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
223bb0d0a8eSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
224bb0d0a8eSMike Smith {
225bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
226bb0d0a8eSMike Smith 
227bb0d0a8eSMike Smith     /*
228bb0d0a8eSMike Smith      * If this is a "default" allocation against this rid, we can't work
229bb0d0a8eSMike Smith      * out where it's coming from (we should actually never see these) so we
230bb0d0a8eSMike Smith      * just have to punt.
231bb0d0a8eSMike Smith      */
232bb0d0a8eSMike Smith     if ((start == 0) && (end == ~0)) {
233bb0d0a8eSMike Smith 	device_printf(dev, "can't decode default resource id %d for %s%d, bypassing\n",
234bb0d0a8eSMike Smith 		      *rid, device_get_name(child), device_get_unit(child));
235bb0d0a8eSMike Smith     } else {
236bb0d0a8eSMike Smith 	/*
237bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
238bb0d0a8eSMike Smith 	 *
239bb0d0a8eSMike Smith 	 * XXX we should probably just fix up the bridge decode and soldier on.
240bb0d0a8eSMike Smith 	 */
241bb0d0a8eSMike Smith 	switch (type) {
242bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
243bb0d0a8eSMike Smith 	    if ((start < sc->iobase) || (end > sc->iolimit)) {
244bb0d0a8eSMike Smith 		device_printf(dev, "device %s%d requested unsupported I/O range 0x%lx-0x%lx"
245bb0d0a8eSMike Smith 			      " (decoding 0x%x-0x%x)\n",
246bb0d0a8eSMike Smith 			      device_get_name(child), device_get_unit(child), start, end,
247bb0d0a8eSMike Smith 			      sc->iobase, sc->iolimit);
248bb0d0a8eSMike Smith 		return(NULL);
249bb0d0a8eSMike Smith 	    }
250bb0d0a8eSMike Smith 	    break;
251bb0d0a8eSMike Smith 
252bb0d0a8eSMike Smith 	    /*
253bb0d0a8eSMike Smith 	     * XXX will have to decide whether the device making the request is asking
254bb0d0a8eSMike Smith 	     *     for prefetchable memory or not.  If it's coming from another bridge
255bb0d0a8eSMike Smith 	     *     down the line, do we assume not, or ask the bridge to pass in another
256bb0d0a8eSMike Smith 	     *     flag as the request bubbles up?
257bb0d0a8eSMike Smith 	     */
258bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
259bb0d0a8eSMike Smith 	    if (((start < sc->membase) || (end > sc->memlimit)) &&
260bb0d0a8eSMike Smith 		((start < sc->pmembase) || (end > sc->pmemlimit))) {
261bb0d0a8eSMike Smith 		device_printf(dev, "device %s%d requested unsupported memory range 0x%lx-0x%lx"
262bb0d0a8eSMike Smith 			      " (decoding 0x%x-0x%x, 0x%x-0x%x)\n",
263bb0d0a8eSMike Smith 			      device_get_name(child), device_get_unit(child), start, end,
264bb0d0a8eSMike Smith 			      sc->membase, sc->memlimit, sc->pmembase, sc->pmemlimit);
265bb0d0a8eSMike Smith 		return(NULL);
266bb0d0a8eSMike Smith 	    }
267bb0d0a8eSMike Smith 	default:
268bb0d0a8eSMike Smith 	}
269bb0d0a8eSMike Smith     }
270bb0d0a8eSMike Smith     device_printf(sc->dev, "resource request type %d 0x%lx-0x%lx decodes OK\n",
271bb0d0a8eSMike Smith 		  type, start, end);
272bb0d0a8eSMike Smith     /*
273bb0d0a8eSMike Smith      * Bridge is OK decoding this resource, so pass it up.
274bb0d0a8eSMike Smith      */
275bb0d0a8eSMike Smith     return(bus_generic_alloc_resource(dev, child, type, rid, start, end, count, flags));
276bb0d0a8eSMike Smith }
277bb0d0a8eSMike Smith 
278bb0d0a8eSMike Smith /*
279bb0d0a8eSMike Smith  * PCIB interface.
280bb0d0a8eSMike Smith  */
281bb0d0a8eSMike Smith static int
282bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
283bb0d0a8eSMike Smith {
284bb0d0a8eSMike Smith     return(31);
285bb0d0a8eSMike Smith }
286bb0d0a8eSMike Smith 
287bb0d0a8eSMike Smith /*
288bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
289bb0d0a8eSMike Smith  */
290bb0d0a8eSMike Smith static u_int32_t
291bb0d0a8eSMike Smith pcib_read_config(device_t dev, int b, int s, int f, int reg, int width)
292bb0d0a8eSMike Smith {
293bb0d0a8eSMike Smith     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
294bb0d0a8eSMike Smith }
295bb0d0a8eSMike Smith 
296bb0d0a8eSMike Smith static void
297bb0d0a8eSMike Smith pcib_write_config(device_t dev, int b, int s, int f, int reg, u_int32_t val, int width)
298bb0d0a8eSMike Smith {
299bb0d0a8eSMike Smith     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
300bb0d0a8eSMike Smith }
301bb0d0a8eSMike Smith 
302bb0d0a8eSMike Smith /*
303bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
304bb0d0a8eSMike Smith  */
305bb0d0a8eSMike Smith static int
306bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
307bb0d0a8eSMike Smith {
308bb0d0a8eSMike Smith     device_t	bus;
309bb0d0a8eSMike Smith     int		parent_intpin;
310bb0d0a8eSMike Smith     int		intnum;
311bb0d0a8eSMike Smith 
312bb0d0a8eSMike Smith     /*
313bb0d0a8eSMike Smith      *
314bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
315bb0d0a8eSMike Smith      * the parent-side intpin as follows.
316bb0d0a8eSMike Smith      *
317bb0d0a8eSMike Smith      * device = device on child bus
318bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
319bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
320bb0d0a8eSMike Smith      *
321bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
322bb0d0a8eSMike Smith      */
323bb0d0a8eSMike Smith     parent_intpin = (pci_get_slot(pcib) + (pin - 1)) % 4;
324bb0d0a8eSMike Smith 
325bb0d0a8eSMike Smith     /*
326bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
327bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
328bb0d0a8eSMike Smith      */
329bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
330bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
331bb0d0a8eSMike Smith     device_printf(pcib, "routed slot %d INT%c to irq %d\n", pci_get_slot(dev),
332bb0d0a8eSMike Smith 		  'A' + pin - 1, intnum);
333bb0d0a8eSMike Smith     return(intnum);
334bb0d0a8eSMike Smith }
335