xref: /freebsd/sys/dev/pci/pci_pci.c (revision 62508c531e0f19a9154670a3cd820164c486131f)
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 
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
33aad970f1SDavid E. O'Brien 
34bb0d0a8eSMike Smith /*
35bb0d0a8eSMike Smith  * PCI:PCI bridge support.
36bb0d0a8eSMike Smith  */
37bb0d0a8eSMike Smith 
38bb0d0a8eSMike Smith #include <sys/param.h>
39bb0d0a8eSMike Smith #include <sys/systm.h>
40bb0d0a8eSMike Smith #include <sys/kernel.h>
4141ee9f1cSPoul-Henning Kamp #include <sys/module.h>
42bb0d0a8eSMike Smith #include <sys/bus.h>
43a8b354a8SWarner Losh #include <machine/bus.h>
44a8b354a8SWarner Losh #include <sys/rman.h>
451c54ff33SMatthew N. Dodd #include <sys/sysctl.h>
46bb0d0a8eSMike Smith 
47bb0d0a8eSMike Smith #include <machine/resource.h>
48bb0d0a8eSMike Smith 
4938d8c994SWarner Losh #include <dev/pci/pcivar.h>
5038d8c994SWarner Losh #include <dev/pci/pcireg.h>
51*62508c53SJohn Baldwin #include <dev/pci/pci_private.h>
5238d8c994SWarner Losh #include <dev/pci/pcib_private.h>
53bb0d0a8eSMike Smith 
54bb0d0a8eSMike Smith #include "pcib_if.h"
55bb0d0a8eSMike Smith 
56bb0d0a8eSMike Smith static int		pcib_probe(device_t dev);
57e36af292SJung-uk Kim static int		pcib_suspend(device_t dev);
58e36af292SJung-uk Kim static int		pcib_resume(device_t dev);
59*62508c53SJohn Baldwin static int		pcib_power_for_sleep(device_t pcib, device_t dev,
60*62508c53SJohn Baldwin 			    int *pstate);
61bb0d0a8eSMike Smith 
62bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
63bb0d0a8eSMike Smith     /* Device interface */
64bb0d0a8eSMike Smith     DEVMETHOD(device_probe,		pcib_probe),
65bb0d0a8eSMike Smith     DEVMETHOD(device_attach,		pcib_attach),
664e30440dSWarner Losh     DEVMETHOD(device_detach,		bus_generic_detach),
67bb0d0a8eSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
68e36af292SJung-uk Kim     DEVMETHOD(device_suspend,		pcib_suspend),
69e36af292SJung-uk Kim     DEVMETHOD(device_resume,		pcib_resume),
70bb0d0a8eSMike Smith 
71bb0d0a8eSMike Smith     /* Bus interface */
72bb0d0a8eSMike Smith     DEVMETHOD(bus_print_child,		bus_generic_print_child),
73bb0d0a8eSMike Smith     DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
74bb0d0a8eSMike Smith     DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
75bb0d0a8eSMike Smith     DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
76bb0d0a8eSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
77bb0d0a8eSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
78bb0d0a8eSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
79bb0d0a8eSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
80bb0d0a8eSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
81bb0d0a8eSMike Smith 
82bb0d0a8eSMike Smith     /* pcib interface */
83bb0d0a8eSMike Smith     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
84bb0d0a8eSMike Smith     DEVMETHOD(pcib_read_config,		pcib_read_config),
85bb0d0a8eSMike Smith     DEVMETHOD(pcib_write_config,	pcib_write_config),
86bb0d0a8eSMike Smith     DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
879bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
889bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
899bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
909bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
91e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		pcib_map_msi),
92*62508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
93bb0d0a8eSMike Smith 
94bb0d0a8eSMike Smith     { 0, 0 }
95bb0d0a8eSMike Smith };
96bb0d0a8eSMike Smith 
9704dda605SJohn Baldwin static devclass_t pcib_devclass;
98bb0d0a8eSMike Smith 
9904dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
100bb0d0a8eSMike Smith DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
101bb0d0a8eSMike Smith 
102bb0d0a8eSMike Smith /*
103b0a2d4b8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
104b0a2d4b8SWarner Losh  */
105b0a2d4b8SWarner Losh static int
106b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
107b0a2d4b8SWarner Losh {
108b0a2d4b8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
109b0a2d4b8SWarner Losh }
110b0a2d4b8SWarner Losh 
111b0a2d4b8SWarner Losh /*
112b0a2d4b8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
113b0a2d4b8SWarner Losh  */
114b0a2d4b8SWarner Losh static int
115b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
116b0a2d4b8SWarner Losh {
117b0a2d4b8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
118b0a2d4b8SWarner Losh }
119b0a2d4b8SWarner Losh 
120b0a2d4b8SWarner Losh /*
121b0a2d4b8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
122b0a2d4b8SWarner Losh  */
123b0a2d4b8SWarner Losh static int
124b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
125b0a2d4b8SWarner Losh {
126b0a2d4b8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
127b0a2d4b8SWarner Losh }
128b0a2d4b8SWarner Losh 
129b0a2d4b8SWarner Losh /*
130e36af292SJung-uk Kim  * Get current I/O decode.
131e36af292SJung-uk Kim  */
132e36af292SJung-uk Kim static void
133e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc)
134e36af292SJung-uk Kim {
135e36af292SJung-uk Kim 	device_t	dev;
136e36af292SJung-uk Kim 	uint32_t	iolow;
137e36af292SJung-uk Kim 
138e36af292SJung-uk Kim 	dev = sc->dev;
139e36af292SJung-uk Kim 
140e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
141e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
142e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(
143e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
144e36af292SJung-uk Kim 	else
145e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(0, iolow);
146e36af292SJung-uk Kim 
147e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
148e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
149e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(
150e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
151e36af292SJung-uk Kim 	else
152e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
153e36af292SJung-uk Kim }
154e36af292SJung-uk Kim 
155e36af292SJung-uk Kim /*
156e36af292SJung-uk Kim  * Get current memory decode.
157e36af292SJung-uk Kim  */
158e36af292SJung-uk Kim static void
159e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc)
160e36af292SJung-uk Kim {
161e36af292SJung-uk Kim 	device_t	dev;
162e36af292SJung-uk Kim 	pci_addr_t	pmemlow;
163e36af292SJung-uk Kim 
164e36af292SJung-uk Kim 	dev = sc->dev;
165e36af292SJung-uk Kim 
166e36af292SJung-uk Kim 	sc->membase = PCI_PPBMEMBASE(0,
167e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
168e36af292SJung-uk Kim 	sc->memlimit = PCI_PPBMEMLIMIT(0,
169e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
170e36af292SJung-uk Kim 
171e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
172e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
173e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(
174e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
175e36af292SJung-uk Kim 	else
176e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
177e36af292SJung-uk Kim 
178e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
179e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
180e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(
181e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
182e36af292SJung-uk Kim 	else
183e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
184e36af292SJung-uk Kim }
185e36af292SJung-uk Kim 
186e36af292SJung-uk Kim /*
187e36af292SJung-uk Kim  * Restore previous I/O decode.
188e36af292SJung-uk Kim  */
189e36af292SJung-uk Kim static void
190e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc)
191e36af292SJung-uk Kim {
192e36af292SJung-uk Kim 	device_t	dev;
193e36af292SJung-uk Kim 	uint32_t	iohi;
194e36af292SJung-uk Kim 
195e36af292SJung-uk Kim 	dev = sc->dev;
196e36af292SJung-uk Kim 
197e36af292SJung-uk Kim 	iohi = sc->iobase >> 16;
198e36af292SJung-uk Kim 	if (iohi > 0)
199e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
200e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
201e36af292SJung-uk Kim 
202e36af292SJung-uk Kim 	iohi = sc->iolimit >> 16;
203e36af292SJung-uk Kim 	if (iohi > 0)
204e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
205e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
206e36af292SJung-uk Kim }
207e36af292SJung-uk Kim 
208e36af292SJung-uk Kim /*
209e36af292SJung-uk Kim  * Restore previous memory decode.
210e36af292SJung-uk Kim  */
211e36af292SJung-uk Kim static void
212e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc)
213e36af292SJung-uk Kim {
214e36af292SJung-uk Kim 	device_t	dev;
215e36af292SJung-uk Kim 	pci_addr_t	pmemhi;
216e36af292SJung-uk Kim 
217e36af292SJung-uk Kim 	dev = sc->dev;
218e36af292SJung-uk Kim 
219e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
220e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
221e36af292SJung-uk Kim 
222e36af292SJung-uk Kim 	pmemhi = sc->pmembase >> 32;
223e36af292SJung-uk Kim 	if (pmemhi > 0)
224e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
225e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
226e36af292SJung-uk Kim 
227e36af292SJung-uk Kim 	pmemhi = sc->pmemlimit >> 32;
228e36af292SJung-uk Kim 	if (pmemhi > 0)
229e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
230e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
231e36af292SJung-uk Kim }
232e36af292SJung-uk Kim 
233e36af292SJung-uk Kim /*
234e36af292SJung-uk Kim  * Get current bridge configuration.
235e36af292SJung-uk Kim  */
236e36af292SJung-uk Kim static void
237e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc)
238e36af292SJung-uk Kim {
239e36af292SJung-uk Kim 	device_t	dev;
240e36af292SJung-uk Kim 
241e36af292SJung-uk Kim 	dev = sc->dev;
242e36af292SJung-uk Kim 
243e36af292SJung-uk Kim 	sc->command = pci_read_config(dev, PCIR_COMMAND, 2);
244e36af292SJung-uk Kim 	sc->pribus = pci_read_config(dev, PCIR_PRIBUS_1, 1);
245e36af292SJung-uk Kim 	sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1);
246e36af292SJung-uk Kim 	sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
247e36af292SJung-uk Kim 	sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
248e36af292SJung-uk Kim 	sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1);
249e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_PORTEN)
250e36af292SJung-uk Kim 		pcib_get_io_decode(sc);
251e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_MEMEN)
252e36af292SJung-uk Kim 		pcib_get_mem_decode(sc);
253e36af292SJung-uk Kim }
254e36af292SJung-uk Kim 
255e36af292SJung-uk Kim /*
256e36af292SJung-uk Kim  * Restore previous bridge configuration.
257e36af292SJung-uk Kim  */
258e36af292SJung-uk Kim static void
259e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
260e36af292SJung-uk Kim {
261e36af292SJung-uk Kim 	device_t	dev;
262e36af292SJung-uk Kim 
263e36af292SJung-uk Kim 	dev = sc->dev;
264e36af292SJung-uk Kim 
265e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_COMMAND, sc->command, 2);
266e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
267e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_SECBUS_1, sc->secbus, 1);
268e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_SUBBUS_1, sc->subbus, 1);
269e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_BRIDGECTL_1, sc->bridgectl, 2);
270e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_SECLAT_1, sc->seclat, 1);
271e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_PORTEN)
272e36af292SJung-uk Kim 		pcib_set_io_decode(sc);
273e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_MEMEN)
274e36af292SJung-uk Kim 		pcib_set_mem_decode(sc);
275e36af292SJung-uk Kim }
276e36af292SJung-uk Kim 
277e36af292SJung-uk Kim /*
278bb0d0a8eSMike Smith  * Generic device interface
279bb0d0a8eSMike Smith  */
280bb0d0a8eSMike Smith static int
281bb0d0a8eSMike Smith pcib_probe(device_t dev)
282bb0d0a8eSMike Smith {
283bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
284bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
285bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
286bb0d0a8eSMike Smith 	return(-10000);
287bb0d0a8eSMike Smith     }
288bb0d0a8eSMike Smith     return(ENXIO);
289bb0d0a8eSMike Smith }
290bb0d0a8eSMike Smith 
2916f0d5884SJohn Baldwin void
2926f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
293bb0d0a8eSMike Smith {
294bb0d0a8eSMike Smith     struct pcib_softc	*sc;
295abf07f13SWarner Losh     struct sysctl_ctx_list *sctx;
296abf07f13SWarner Losh     struct sysctl_oid	*soid;
297bb0d0a8eSMike Smith 
298bb0d0a8eSMike Smith     sc = device_get_softc(dev);
299bb0d0a8eSMike Smith     sc->dev = dev;
300bb0d0a8eSMike Smith 
3014fa59183SMike Smith     /*
3024fa59183SMike Smith      * Get current bridge configuration.
3034fa59183SMike Smith      */
30455aaf894SMarius Strobl     sc->domain = pci_get_domain(dev);
3054fa59183SMike Smith     sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2);
306e36af292SJung-uk Kim     pcib_cfg_save(sc);
3074fa59183SMike Smith 
3084fa59183SMike Smith     /*
309abf07f13SWarner Losh      * Setup sysctl reporting nodes
310abf07f13SWarner Losh      */
311abf07f13SWarner Losh     sctx = device_get_sysctl_ctx(dev);
312abf07f13SWarner Losh     soid = device_get_sysctl_tree(dev);
313abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
314abf07f13SWarner Losh       CTLFLAG_RD, &sc->domain, 0, "Domain number");
315abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
316abf07f13SWarner Losh       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
317abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
318abf07f13SWarner Losh       CTLFLAG_RD, &sc->secbus, 0, "Secondary bus number");
319abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
320abf07f13SWarner Losh       CTLFLAG_RD, &sc->subbus, 0, "Subordinate bus number");
321abf07f13SWarner Losh 
322abf07f13SWarner Losh     /*
3234fa59183SMike Smith      * Quirk handling.
3244fa59183SMike Smith      */
3254fa59183SMike Smith     switch (pci_get_devid(dev)) {
3264fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
3274fa59183SMike Smith 	{
328b0cb115fSWarner Losh 	    uint8_t	supbus;
3294fa59183SMike Smith 
3304fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
3314fa59183SMike Smith 	    if (supbus != 0xff) {
3324fa59183SMike Smith 		sc->secbus = supbus + 1;
3334fa59183SMike Smith 		sc->subbus = supbus + 1;
3344fa59183SMike Smith 	    }
3354fa59183SMike Smith 	    break;
3364fa59183SMike Smith 	}
3374fa59183SMike Smith 
338e4b59fc5SWarner Losh     /*
339e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
340e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
341e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
342e4b59fc5SWarner Losh      * happen.  There's also a Toshiba bridge that behaves this
343e4b59fc5SWarner Losh      * way.
344e4b59fc5SWarner Losh      */
345e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
346e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
347e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
348e4b59fc5SWarner Losh 	break;
349c94d6dbeSJung-uk Kim 
350c94d6dbeSJung-uk Kim     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
351c94d6dbeSJung-uk Kim     case 0x00dd10de:
352c94d6dbeSJung-uk Kim 	{
353c94d6dbeSJung-uk Kim 	    char *cp;
354c94d6dbeSJung-uk Kim 
3551def0ca6SJung-uk Kim 	    if ((cp = getenv("smbios.planar.maker")) == NULL)
356c94d6dbeSJung-uk Kim 		break;
3571def0ca6SJung-uk Kim 	    if (strncmp(cp, "Compal", 6) != 0) {
3581def0ca6SJung-uk Kim 		freeenv(cp);
359c94d6dbeSJung-uk Kim 		break;
3601def0ca6SJung-uk Kim 	    }
3611def0ca6SJung-uk Kim 	    freeenv(cp);
3621def0ca6SJung-uk Kim 	    if ((cp = getenv("smbios.planar.product")) == NULL)
3631def0ca6SJung-uk Kim 		break;
3641def0ca6SJung-uk Kim 	    if (strncmp(cp, "08A0", 4) != 0) {
3651def0ca6SJung-uk Kim 		freeenv(cp);
3661def0ca6SJung-uk Kim 		break;
3671def0ca6SJung-uk Kim 	    }
3681def0ca6SJung-uk Kim 	    freeenv(cp);
369c94d6dbeSJung-uk Kim 	    if (sc->subbus < 0xa) {
370c94d6dbeSJung-uk Kim 		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
371c94d6dbeSJung-uk Kim 		sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
372c94d6dbeSJung-uk Kim 	    }
373c94d6dbeSJung-uk Kim 	    break;
374c94d6dbeSJung-uk Kim 	}
375e4b59fc5SWarner Losh     }
376e4b59fc5SWarner Losh 
37722bf1c7fSJohn Baldwin     if (pci_msi_device_blacklisted(dev))
37822bf1c7fSJohn Baldwin 	sc->flags |= PCIB_DISABLE_MSI;
37922bf1c7fSJohn Baldwin 
380e4b59fc5SWarner Losh     /*
381e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
382e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
383e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
384e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
385e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
386e4b59fc5SWarner Losh      * parts as subtractive.
387e4b59fc5SWarner Losh      */
388e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
389657d9f9fSJohn Baldwin       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
390e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
391e4b59fc5SWarner Losh 
392bb0d0a8eSMike Smith     if (bootverbose) {
39355aaf894SMarius Strobl 	device_printf(dev, "  domain            %d\n", sc->domain);
394bb0d0a8eSMike Smith 	device_printf(dev, "  secondary bus     %d\n", sc->secbus);
395bb0d0a8eSMike Smith 	device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
396bb0d0a8eSMike Smith 	device_printf(dev, "  I/O decode        0x%x-0x%x\n", sc->iobase, sc->iolimit);
397b0a2d4b8SWarner Losh 	if (pcib_is_nonprefetch_open(sc))
398b0a2d4b8SWarner Losh 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
399b0a2d4b8SWarner Losh 	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
400b0a2d4b8SWarner Losh 	if (pcib_is_prefetch_open(sc))
401b0a2d4b8SWarner Losh 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
402b0a2d4b8SWarner Losh 	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
403b0a2d4b8SWarner Losh 	else
404b0a2d4b8SWarner Losh 	    device_printf(dev, "  no prefetched decode\n");
405e4b59fc5SWarner Losh 	if (sc->flags & PCIB_SUBTRACTIVE)
406e4b59fc5SWarner Losh 	    device_printf(dev, "  Subtractively decoded bridge.\n");
407bb0d0a8eSMike Smith     }
408bb0d0a8eSMike Smith 
409bb0d0a8eSMike Smith     /*
410bb0d0a8eSMike Smith      * XXX If the secondary bus number is zero, we should assign a bus number
4117e178674SWarner Losh      *     since the BIOS hasn't, then initialise the bridge.  A simple
4127e178674SWarner Losh      *     bus_alloc_resource with the a couple of busses seems like the right
4137e178674SWarner Losh      *     approach, but we don't know what busses the BIOS might have already
4147e178674SWarner Losh      *     assigned to other bridges on this bus that probe later than we do.
4157e178674SWarner Losh      *
4167e178674SWarner Losh      *     If the subordinate bus number is less than the secondary bus number,
417bb0d0a8eSMike Smith      *     we should pick a better value.  One sensible alternative would be to
418bb0d0a8eSMike Smith      *     pick 255; the only tradeoff here is that configuration transactions
4197e178674SWarner Losh      *     would be more widely routed than absolutely necessary.  We could
4207e178674SWarner Losh      *     then do a walk of the tree later and fix it.
421bb0d0a8eSMike Smith      */
4226f0d5884SJohn Baldwin }
423bb0d0a8eSMike Smith 
42438906aedSJohn Baldwin int
4256f0d5884SJohn Baldwin pcib_attach(device_t dev)
4266f0d5884SJohn Baldwin {
4276f0d5884SJohn Baldwin     struct pcib_softc	*sc;
4286f0d5884SJohn Baldwin     device_t		child;
4296f0d5884SJohn Baldwin 
4306f0d5884SJohn Baldwin     pcib_attach_common(dev);
4316f0d5884SJohn Baldwin     sc = device_get_softc(dev);
432bb0d0a8eSMike Smith     if (sc->secbus != 0) {
433cea0a895SJohn Baldwin 	child = device_add_child(dev, "pci", sc->secbus);
434bb0d0a8eSMike Smith 	if (child != NULL)
435bb0d0a8eSMike Smith 	    return(bus_generic_attach(dev));
436bb0d0a8eSMike Smith     }
437bb0d0a8eSMike Smith 
438bb0d0a8eSMike Smith     /* no secondary bus; we should have fixed this */
439bb0d0a8eSMike Smith     return(0);
440bb0d0a8eSMike Smith }
441bb0d0a8eSMike Smith 
4426f0d5884SJohn Baldwin int
443e36af292SJung-uk Kim pcib_suspend(device_t dev)
444e36af292SJung-uk Kim {
445*62508c53SJohn Baldwin 	device_t	pcib;
446e36af292SJung-uk Kim 	int		dstate, error;
447e36af292SJung-uk Kim 
448e36af292SJung-uk Kim 	pcib_cfg_save(device_get_softc(dev));
449e36af292SJung-uk Kim 	error = bus_generic_suspend(dev);
450e36af292SJung-uk Kim 	if (error == 0 && pci_do_power_resume) {
451e36af292SJung-uk Kim 		dstate = PCI_POWERSTATE_D3;
452*62508c53SJohn Baldwin 		pcib = device_get_parent(device_get_parent(dev));
453*62508c53SJohn Baldwin 		if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0)
454e36af292SJung-uk Kim 			pci_set_powerstate(dev, dstate);
455e36af292SJung-uk Kim 	}
456e36af292SJung-uk Kim 	return (error);
457e36af292SJung-uk Kim }
458e36af292SJung-uk Kim 
459e36af292SJung-uk Kim int
460e36af292SJung-uk Kim pcib_resume(device_t dev)
461e36af292SJung-uk Kim {
462*62508c53SJohn Baldwin 	device_t	pcib;
463e36af292SJung-uk Kim 
464e36af292SJung-uk Kim 	if (pci_do_power_resume) {
465*62508c53SJohn Baldwin 		pcib = device_get_parent(device_get_parent(dev));
466*62508c53SJohn Baldwin 		if (PCIB_POWER_FOR_SLEEP(pcib, dev, NULL) == 0)
467e36af292SJung-uk Kim 			pci_set_powerstate(dev, PCI_POWERSTATE_D0);
468e36af292SJung-uk Kim 	}
469e36af292SJung-uk Kim 	pcib_cfg_restore(device_get_softc(dev));
470e36af292SJung-uk Kim 	return (bus_generic_resume(dev));
471e36af292SJung-uk Kim }
472e36af292SJung-uk Kim 
473e36af292SJung-uk Kim int
474bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
475bb0d0a8eSMike Smith {
476bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
477bb0d0a8eSMike Smith 
478bb0d0a8eSMike Smith     switch (which) {
47955aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
48055aaf894SMarius Strobl 	*result = sc->domain;
48155aaf894SMarius Strobl 	return(0);
482bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
483bb0d0a8eSMike Smith 	*result = sc->secbus;
484bb0d0a8eSMike Smith 	return(0);
485bb0d0a8eSMike Smith     }
486bb0d0a8eSMike Smith     return(ENOENT);
487bb0d0a8eSMike Smith }
488bb0d0a8eSMike Smith 
4896f0d5884SJohn Baldwin int
490bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
491bb0d0a8eSMike Smith {
492bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
493bb0d0a8eSMike Smith 
494bb0d0a8eSMike Smith     switch (which) {
49555aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
49655aaf894SMarius Strobl 	return(EINVAL);
497bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
498bb0d0a8eSMike Smith 	sc->secbus = value;
49955aaf894SMarius Strobl 	return(0);
500bb0d0a8eSMike Smith     }
501bb0d0a8eSMike Smith     return(ENOENT);
502bb0d0a8eSMike Smith }
503bb0d0a8eSMike Smith 
504bb0d0a8eSMike Smith /*
505bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
506bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
507bb0d0a8eSMike Smith  */
5086f0d5884SJohn Baldwin struct resource *
509bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
510bb0d0a8eSMike Smith     u_long start, u_long end, u_long count, u_int flags)
511bb0d0a8eSMike Smith {
512bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
51326043836SJohn Baldwin 	const char *name, *suffix;
514a8b354a8SWarner Losh 	int ok;
515bb0d0a8eSMike Smith 
516bb0d0a8eSMike Smith 	/*
517bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
518bb0d0a8eSMike Smith 	 */
51926043836SJohn Baldwin 	name = device_get_nameunit(child);
52026043836SJohn Baldwin 	if (name == NULL) {
52126043836SJohn Baldwin 		name = "";
52226043836SJohn Baldwin 		suffix = "";
52326043836SJohn Baldwin 	} else
52426043836SJohn Baldwin 		suffix = " ";
525bb0d0a8eSMike Smith 	switch (type) {
526bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
527a8b354a8SWarner Losh 		ok = 0;
528e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
529e4b59fc5SWarner Losh 			break;
530a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
531d98d9b12SMarcel Moolenaar 
532d98d9b12SMarcel Moolenaar 		/*
533d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA I/O addresses when the
534d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
535d98d9b12SMarcel Moolenaar 		 */
536d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_ioport_range(start, end))
537d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
538d98d9b12SMarcel Moolenaar 
539e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
540a8b354a8SWarner Losh 			if (!ok) {
54112b8c86eSWarner Losh 				if (start < sc->iobase)
54212b8c86eSWarner Losh 					start = sc->iobase;
54312b8c86eSWarner Losh 				if (end > sc->iolimit)
54412b8c86eSWarner Losh 					end = sc->iolimit;
5452daa7a07SWarner Losh 				if (start < end)
5462daa7a07SWarner Losh 					ok = 1;
547a8b354a8SWarner Losh 			}
5481c54ff33SMatthew N. Dodd 		} else {
549e4b59fc5SWarner Losh 			ok = 1;
5509dffe835SWarner Losh #if 0
551795dceffSWarner Losh 			/*
552795dceffSWarner Losh 			 * If we overlap with the subtractive range, then
553795dceffSWarner Losh 			 * pick the upper range to use.
554795dceffSWarner Losh 			 */
555795dceffSWarner Losh 			if (start < sc->iolimit && end > sc->iobase)
556795dceffSWarner Losh 				start = sc->iolimit + 1;
5579dffe835SWarner Losh #endif
55812b8c86eSWarner Losh 		}
559a8b354a8SWarner Losh 		if (end < start) {
5602daa7a07SWarner Losh 			device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
5612daa7a07SWarner Losh 			    end, start);
562a8b354a8SWarner Losh 			start = 0;
563a8b354a8SWarner Losh 			end = 0;
564a8b354a8SWarner Losh 			ok = 0;
565a8b354a8SWarner Losh 		}
566a8b354a8SWarner Losh 		if (!ok) {
56726043836SJohn Baldwin 			device_printf(dev, "%s%srequested unsupported I/O "
568a8b354a8SWarner Losh 			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
56926043836SJohn Baldwin 			    name, suffix, start, end, sc->iobase, sc->iolimit);
570bb0d0a8eSMike Smith 			return (NULL);
571bb0d0a8eSMike Smith 		}
5724fa59183SMike Smith 		if (bootverbose)
5732daa7a07SWarner Losh 			device_printf(dev,
57426043836SJohn Baldwin 			    "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
57526043836SJohn Baldwin 			    name, suffix, start, end);
576bb0d0a8eSMike Smith 		break;
577bb0d0a8eSMike Smith 
578bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
579a8b354a8SWarner Losh 		ok = 0;
580a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
581a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
582a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
583a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
584d98d9b12SMarcel Moolenaar 
585d98d9b12SMarcel Moolenaar 		/*
586d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA memory addresses when the
587d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
588d98d9b12SMarcel Moolenaar 		 */
589d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_memory_range(start, end))
590d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
591d98d9b12SMarcel Moolenaar 
592e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
593a8b354a8SWarner Losh 			if (!ok) {
594a8b354a8SWarner Losh 				ok = 1;
595a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
596a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
597a8b354a8SWarner Losh 						if (start < sc->pmembase)
598a8b354a8SWarner Losh 							start = sc->pmembase;
599a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
600a8b354a8SWarner Losh 							end = sc->pmemlimit;
601a8b354a8SWarner Losh 					} else {
602a8b354a8SWarner Losh 						ok = 0;
603a8b354a8SWarner Losh 					}
604a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
605a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
606a8b354a8SWarner Losh 						if (start < sc->membase)
60712b8c86eSWarner Losh 							start = sc->membase;
60812b8c86eSWarner Losh 						if (end > sc->memlimit)
60912b8c86eSWarner Losh 							end = sc->memlimit;
6101c54ff33SMatthew N. Dodd 					} else {
611a8b354a8SWarner Losh 						ok = 0;
612a8b354a8SWarner Losh 					}
613a8b354a8SWarner Losh 				}
614a8b354a8SWarner Losh 			}
615a8b354a8SWarner Losh 		} else if (!ok) {
616e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
6179dffe835SWarner Losh #if 0
618a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
619795dceffSWarner Losh 				if (start < sc->memlimit && end > sc->membase)
620795dceffSWarner Losh 					start = sc->memlimit + 1;
621a8b354a8SWarner Losh 			}
622a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
623795dceffSWarner Losh 				if (start < sc->pmemlimit && end > sc->pmembase)
624795dceffSWarner Losh 					start = sc->pmemlimit + 1;
6251c54ff33SMatthew N. Dodd 			}
6269dffe835SWarner Losh #endif
62712b8c86eSWarner Losh 		}
628a8b354a8SWarner Losh 		if (end < start) {
6292daa7a07SWarner Losh 			device_printf(dev, "memory: end (%lx) < start (%lx)\n",
6302daa7a07SWarner Losh 			    end, start);
631a8b354a8SWarner Losh 			start = 0;
632a8b354a8SWarner Losh 			end = 0;
633a8b354a8SWarner Losh 			ok = 0;
634a8b354a8SWarner Losh 		}
635a8b354a8SWarner Losh 		if (!ok && bootverbose)
63634428485SWarner Losh 			device_printf(dev,
63726043836SJohn Baldwin 			    "%s%srequested unsupported memory range %#lx-%#lx "
638b0a2d4b8SWarner Losh 			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
63926043836SJohn Baldwin 			    name, suffix, start, end,
640b0a2d4b8SWarner Losh 			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
641b0a2d4b8SWarner Losh 			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
642a8b354a8SWarner Losh 		if (!ok)
643bb0d0a8eSMike Smith 			return (NULL);
6444fa59183SMike Smith 		if (bootverbose)
64526043836SJohn Baldwin 			device_printf(dev,"%s%srequested memory range "
6462daa7a07SWarner Losh 			    "0x%lx-0x%lx: good\n",
64726043836SJohn Baldwin 			    name, suffix, start, end);
6484fa59183SMike Smith 		break;
6494fa59183SMike Smith 
650bb0d0a8eSMike Smith 	default:
6514fa59183SMike Smith 		break;
652bb0d0a8eSMike Smith 	}
653bb0d0a8eSMike Smith 	/*
654bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
655bb0d0a8eSMike Smith 	 */
6562daa7a07SWarner Losh 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
6572daa7a07SWarner Losh 	    count, flags));
658bb0d0a8eSMike Smith }
659bb0d0a8eSMike Smith 
660bb0d0a8eSMike Smith /*
661bb0d0a8eSMike Smith  * PCIB interface.
662bb0d0a8eSMike Smith  */
6636f0d5884SJohn Baldwin int
664bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
665bb0d0a8eSMike Smith {
6664fa59183SMike Smith     return(PCI_SLOTMAX);
667bb0d0a8eSMike Smith }
668bb0d0a8eSMike Smith 
669bb0d0a8eSMike Smith /*
670bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
671bb0d0a8eSMike Smith  */
672b0cb115fSWarner Losh uint32_t
673795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
674bb0d0a8eSMike Smith {
675bb0d0a8eSMike Smith     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
676bb0d0a8eSMike Smith }
677bb0d0a8eSMike Smith 
6786f0d5884SJohn Baldwin void
679795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
680bb0d0a8eSMike Smith {
681bb0d0a8eSMike Smith     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
682bb0d0a8eSMike Smith }
683bb0d0a8eSMike Smith 
684bb0d0a8eSMike Smith /*
685bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
686bb0d0a8eSMike Smith  */
6872c2d1d07SBenno Rice int
688bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
689bb0d0a8eSMike Smith {
690bb0d0a8eSMike Smith     device_t	bus;
691bb0d0a8eSMike Smith     int		parent_intpin;
692bb0d0a8eSMike Smith     int		intnum;
693bb0d0a8eSMike Smith 
694bb0d0a8eSMike Smith     /*
695bb0d0a8eSMike Smith      *
696bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
697bb0d0a8eSMike Smith      * the parent-side intpin as follows.
698bb0d0a8eSMike Smith      *
699bb0d0a8eSMike Smith      * device = device on child bus
700bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
701bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
702bb0d0a8eSMike Smith      *
703bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
704bb0d0a8eSMike Smith      */
705cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
706bb0d0a8eSMike Smith 
707bb0d0a8eSMike Smith     /*
708bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
709bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
710bb0d0a8eSMike Smith      */
711bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
712bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
71339981fedSJohn Baldwin     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
714c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
715c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
7168046c4b9SMike Smith     }
717bb0d0a8eSMike Smith     return(intnum);
718bb0d0a8eSMike Smith }
719b173edafSJohn Baldwin 
720e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
7219bf4c9c1SJohn Baldwin int
7229bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
7239bf4c9c1SJohn Baldwin {
724bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
7259bf4c9c1SJohn Baldwin 	device_t bus;
7269bf4c9c1SJohn Baldwin 
72722bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
72822bf1c7fSJohn Baldwin 		return (ENXIO);
7299bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
7309bf4c9c1SJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
7319bf4c9c1SJohn Baldwin 	    irqs));
7329bf4c9c1SJohn Baldwin }
7339bf4c9c1SJohn Baldwin 
734e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
7359bf4c9c1SJohn Baldwin int
7369bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
7379bf4c9c1SJohn Baldwin {
7389bf4c9c1SJohn Baldwin 	device_t bus;
7399bf4c9c1SJohn Baldwin 
7409bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
7419bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
7429bf4c9c1SJohn Baldwin }
7439bf4c9c1SJohn Baldwin 
7449bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
7459bf4c9c1SJohn Baldwin int
746e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
7479bf4c9c1SJohn Baldwin {
748bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
7499bf4c9c1SJohn Baldwin 	device_t bus;
7509bf4c9c1SJohn Baldwin 
75122bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
75222bf1c7fSJohn Baldwin 		return (ENXIO);
7539bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
754e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
7555fe82bcaSJohn Baldwin }
7565fe82bcaSJohn Baldwin 
7579bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
7589bf4c9c1SJohn Baldwin int
7599bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
7609bf4c9c1SJohn Baldwin {
7619bf4c9c1SJohn Baldwin 	device_t bus;
7629bf4c9c1SJohn Baldwin 
7639bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
7649bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
7659bf4c9c1SJohn Baldwin }
7669bf4c9c1SJohn Baldwin 
767e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
768e706f7f0SJohn Baldwin int
769e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
770e706f7f0SJohn Baldwin     uint32_t *data)
771e706f7f0SJohn Baldwin {
772e706f7f0SJohn Baldwin 	device_t bus;
7734522ac77SLuoqi Chen 	int error;
774e706f7f0SJohn Baldwin 
775e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
7764522ac77SLuoqi Chen 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
7774522ac77SLuoqi Chen 	if (error)
7784522ac77SLuoqi Chen 		return (error);
7794522ac77SLuoqi Chen 
7804522ac77SLuoqi Chen 	pci_ht_map_msi(pcib, *addr);
7814522ac77SLuoqi Chen 	return (0);
782e706f7f0SJohn Baldwin }
783e706f7f0SJohn Baldwin 
784*62508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
785*62508c53SJohn Baldwin int
786*62508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
787*62508c53SJohn Baldwin {
788*62508c53SJohn Baldwin 	device_t bus;
789*62508c53SJohn Baldwin 
790*62508c53SJohn Baldwin 	bus = device_get_parent(pcib);
791*62508c53SJohn Baldwin 	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
792*62508c53SJohn Baldwin }
793*62508c53SJohn Baldwin 
794b173edafSJohn Baldwin /*
795b173edafSJohn Baldwin  * Try to read the bus number of a host-PCI bridge using appropriate config
796b173edafSJohn Baldwin  * registers.
797b173edafSJohn Baldwin  */
798b173edafSJohn Baldwin int
799b173edafSJohn Baldwin host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
800b0cb115fSWarner Losh     uint8_t *busnum)
801b173edafSJohn Baldwin {
802b0cb115fSWarner Losh 	uint32_t id;
803b173edafSJohn Baldwin 
804b173edafSJohn Baldwin 	id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
8051bbf2464SJohn Baldwin 	if (id == 0xffffffff)
806b173edafSJohn Baldwin 		return (0);
807b173edafSJohn Baldwin 
808b173edafSJohn Baldwin 	switch (id) {
809b173edafSJohn Baldwin 	case 0x12258086:
810b173edafSJohn Baldwin 		/* Intel 824?? */
811b173edafSJohn Baldwin 		/* XXX This is a guess */
812b173edafSJohn Baldwin 		/* *busnum = read_config(bus, slot, func, 0x41, 1); */
813b173edafSJohn Baldwin 		*busnum = bus;
814b173edafSJohn Baldwin 		break;
815b173edafSJohn Baldwin 	case 0x84c48086:
816b173edafSJohn Baldwin 		/* Intel 82454KX/GX (Orion) */
817b173edafSJohn Baldwin 		*busnum = read_config(bus, slot, func, 0x4a, 1);
818b173edafSJohn Baldwin 		break;
819b173edafSJohn Baldwin 	case 0x84ca8086:
820b173edafSJohn Baldwin 		/*
821b173edafSJohn Baldwin 		 * For the 450nx chipset, there is a whole bundle of
822b173edafSJohn Baldwin 		 * things pretending to be host bridges. The MIOC will
823b173edafSJohn Baldwin 		 * be seen first and isn't really a pci bridge (the
824b173edafSJohn Baldwin 		 * actual busses are attached to the PXB's). We need to
825b173edafSJohn Baldwin 		 * read the registers of the MIOC to figure out the
826b173edafSJohn Baldwin 		 * bus numbers for the PXB channels.
827b173edafSJohn Baldwin 		 *
828b173edafSJohn Baldwin 		 * Since the MIOC doesn't have a pci bus attached, we
829b173edafSJohn Baldwin 		 * pretend it wasn't there.
830b173edafSJohn Baldwin 		 */
831b173edafSJohn Baldwin 		return (0);
832b173edafSJohn Baldwin 	case 0x84cb8086:
833b173edafSJohn Baldwin 		switch (slot) {
834b173edafSJohn Baldwin 		case 0x12:
835b173edafSJohn Baldwin 			/* Intel 82454NX PXB#0, Bus#A */
8361bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd0, 1);
837b173edafSJohn Baldwin 			break;
838b173edafSJohn Baldwin 		case 0x13:
839b173edafSJohn Baldwin 			/* Intel 82454NX PXB#0, Bus#B */
8401bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
841b173edafSJohn Baldwin 			break;
842b173edafSJohn Baldwin 		case 0x14:
843b173edafSJohn Baldwin 			/* Intel 82454NX PXB#1, Bus#A */
8441bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd3, 1);
845b173edafSJohn Baldwin 			break;
846b173edafSJohn Baldwin 		case 0x15:
847b173edafSJohn Baldwin 			/* Intel 82454NX PXB#1, Bus#B */
8481bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
849b173edafSJohn Baldwin 			break;
850b173edafSJohn Baldwin 		}
851b173edafSJohn Baldwin 		break;
852b173edafSJohn Baldwin 
853b173edafSJohn Baldwin 		/* ServerWorks -- vendor 0x1166 */
854b173edafSJohn Baldwin 	case 0x00051166:
855b173edafSJohn Baldwin 	case 0x00061166:
856b173edafSJohn Baldwin 	case 0x00081166:
857b173edafSJohn Baldwin 	case 0x00091166:
858b173edafSJohn Baldwin 	case 0x00101166:
859b173edafSJohn Baldwin 	case 0x00111166:
860b173edafSJohn Baldwin 	case 0x00171166:
861b173edafSJohn Baldwin 	case 0x01011166:
862b173edafSJohn Baldwin 	case 0x010f1014:
863b173edafSJohn Baldwin 	case 0x02011166:
864b173edafSJohn Baldwin 	case 0x03021014:
865b173edafSJohn Baldwin 		*busnum = read_config(bus, slot, func, 0x44, 1);
866b173edafSJohn Baldwin 		break;
8675165a17dSJohn Baldwin 
8685165a17dSJohn Baldwin 		/* Compaq/HP -- vendor 0x0e11 */
8695165a17dSJohn Baldwin 	case 0x60100e11:
8705165a17dSJohn Baldwin 		*busnum = read_config(bus, slot, func, 0xc8, 1);
8715165a17dSJohn Baldwin 		break;
872b173edafSJohn Baldwin 	default:
873b173edafSJohn Baldwin 		/* Don't know how to read bus number. */
874b173edafSJohn Baldwin 		return 0;
875b173edafSJohn Baldwin 	}
876b173edafSJohn Baldwin 
877b173edafSJohn Baldwin 	return 1;
878b173edafSJohn Baldwin }
879