pci_subr.c revision 221393
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 221393 2011-05-03 17:37:24Z jhb $");
33119418Sobrien
3469783Smsmith/*
3569783Smsmith * PCI:PCI bridge support.
3669783Smsmith */
3769783Smsmith
3869783Smsmith#include <sys/param.h>
39221393Sjhb#include <sys/bus.h>
4069783Smsmith#include <sys/kernel.h>
41221393Sjhb#include <sys/libkern.h>
42221393Sjhb#include <sys/malloc.h>
43129876Sphk#include <sys/module.h>
44107546Simp#include <sys/rman.h>
45106844Smdodd#include <sys/sysctl.h>
46221393Sjhb#include <sys/systm.h>
4769783Smsmith
48221393Sjhb#include <machine/bus.h>
4969783Smsmith#include <machine/resource.h>
5069783Smsmith
51119285Simp#include <dev/pci/pcivar.h>
52119285Simp#include <dev/pci/pcireg.h>
53211430Sjhb#include <dev/pci/pci_private.h>
54119285Simp#include <dev/pci/pcib_private.h>
5569783Smsmith
5669783Smsmith#include "pcib_if.h"
5769783Smsmith
5869783Smsmithstatic int		pcib_probe(device_t dev);
59200341Sjkimstatic int		pcib_suspend(device_t dev);
60200341Sjkimstatic int		pcib_resume(device_t dev);
61211430Sjhbstatic int		pcib_power_for_sleep(device_t pcib, device_t dev,
62211430Sjhb			    int *pstate);
6369783Smsmith
6469783Smsmithstatic device_method_t pcib_methods[] = {
6569783Smsmith    /* Device interface */
6669783Smsmith    DEVMETHOD(device_probe,		pcib_probe),
6769783Smsmith    DEVMETHOD(device_attach,		pcib_attach),
68145661Simp    DEVMETHOD(device_detach,		bus_generic_detach),
6969783Smsmith    DEVMETHOD(device_shutdown,		bus_generic_shutdown),
70200341Sjkim    DEVMETHOD(device_suspend,		pcib_suspend),
71200341Sjkim    DEVMETHOD(device_resume,		pcib_resume),
7269783Smsmith
7369783Smsmith    /* Bus interface */
7469783Smsmith    DEVMETHOD(bus_print_child,		bus_generic_print_child),
7569783Smsmith    DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
7669783Smsmith    DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
7769783Smsmith    DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
78221393Sjhb#ifdef NEW_PCIB
79221393Sjhb    DEVMETHOD(bus_adjust_resource,	pcib_adjust_resource),
80221393Sjhb    DEVMETHOD(bus_release_resource,	pcib_release_resource),
81221393Sjhb#else
82221324Sjhb    DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
8369783Smsmith    DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
84221393Sjhb#endif
8569783Smsmith    DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
8669783Smsmith    DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
8769783Smsmith    DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
8869783Smsmith    DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
8969783Smsmith
9069783Smsmith    /* pcib interface */
9169783Smsmith    DEVMETHOD(pcib_maxslots,		pcib_maxslots),
9269783Smsmith    DEVMETHOD(pcib_read_config,		pcib_read_config),
9369783Smsmith    DEVMETHOD(pcib_write_config,	pcib_write_config),
9469783Smsmith    DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
95164264Sjhb    DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
96164264Sjhb    DEVMETHOD(pcib_release_msi,		pcib_release_msi),
97164264Sjhb    DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
98164264Sjhb    DEVMETHOD(pcib_release_msix,	pcib_release_msix),
99169221Sjhb    DEVMETHOD(pcib_map_msi,		pcib_map_msi),
100211430Sjhb    DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
10169783Smsmith
10269783Smsmith    { 0, 0 }
10369783Smsmith};
10469783Smsmith
105154079Sjhbstatic devclass_t pcib_devclass;
10669783Smsmith
107154079SjhbDEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
10869783SmsmithDRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
10969783Smsmith
110221393Sjhb#ifdef NEW_PCIB
11169783Smsmith/*
112221393Sjhb * XXX Todo:
113221393Sjhb * - properly handle the ISA enable bit.  If it is set, we should change
114221393Sjhb *   the behavior of the I/O window resource and rman to not allocate the
115221393Sjhb *   blocked ranges (upper 768 bytes of each 1K in the first 64k of the
116221393Sjhb *   I/O port address space).
117221393Sjhb */
118221393Sjhb
119221393Sjhb/*
120221393Sjhb * Is a resource from a child device sub-allocated from one of our
121221393Sjhb * resource managers?
122221393Sjhb */
123221393Sjhbstatic int
124221393Sjhbpcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
125221393Sjhb{
126221393Sjhb
127221393Sjhb	switch (type) {
128221393Sjhb	case SYS_RES_IOPORT:
129221393Sjhb		return (rman_is_region_manager(r, &sc->io.rman));
130221393Sjhb	case SYS_RES_MEMORY:
131221393Sjhb		/* Prefetchable resources may live in either memory rman. */
132221393Sjhb		if (rman_get_flags(r) & RF_PREFETCHABLE &&
133221393Sjhb		    rman_is_region_manager(r, &sc->pmem.rman))
134221393Sjhb			return (1);
135221393Sjhb		return (rman_is_region_manager(r, &sc->mem.rman));
136221393Sjhb	}
137221393Sjhb	return (0);
138221393Sjhb}
139221393Sjhb
140221393Sjhbstatic int
141221393Sjhbpcib_is_window_open(struct pcib_window *pw)
142221393Sjhb{
143221393Sjhb
144221393Sjhb	return (pw->valid && pw->base < pw->limit);
145221393Sjhb}
146221393Sjhb
147221393Sjhb/*
148221393Sjhb * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
149221393Sjhb * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
150221393Sjhb * when allocating the resource windows and rely on the PCI bus driver
151221393Sjhb * to do this for us.
152221393Sjhb */
153221393Sjhbstatic void
154221393Sjhbpcib_activate_window(struct pcib_softc *sc, int type)
155221393Sjhb{
156221393Sjhb
157221393Sjhb	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
158221393Sjhb}
159221393Sjhb
160221393Sjhbstatic void
161221393Sjhbpcib_write_windows(struct pcib_softc *sc, int mask)
162221393Sjhb{
163221393Sjhb	device_t dev;
164221393Sjhb	uint32_t val;
165221393Sjhb
166221393Sjhb	dev = sc->dev;
167221393Sjhb	if (sc->io.valid && mask & WIN_IO) {
168221393Sjhb		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
169221393Sjhb		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
170221393Sjhb			pci_write_config(dev, PCIR_IOBASEH_1,
171221393Sjhb			    sc->io.base >> 16, 2);
172221393Sjhb			pci_write_config(dev, PCIR_IOLIMITH_1,
173221393Sjhb			    sc->io.limit >> 16, 2);
174221393Sjhb		}
175221393Sjhb		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
176221393Sjhb		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
177221393Sjhb	}
178221393Sjhb
179221393Sjhb	if (mask & WIN_MEM) {
180221393Sjhb		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
181221393Sjhb		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
182221393Sjhb	}
183221393Sjhb
184221393Sjhb	if (sc->pmem.valid && mask & WIN_PMEM) {
185221393Sjhb		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
186221393Sjhb		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
187221393Sjhb			pci_write_config(dev, PCIR_PMBASEH_1,
188221393Sjhb			    sc->pmem.base >> 32, 4);
189221393Sjhb			pci_write_config(dev, PCIR_PMLIMITH_1,
190221393Sjhb			    sc->pmem.limit >> 32, 4);
191221393Sjhb		}
192221393Sjhb		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
193221393Sjhb		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
194221393Sjhb	}
195221393Sjhb}
196221393Sjhb
197221393Sjhbstatic void
198221393Sjhbpcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
199221393Sjhb    int flags, pci_addr_t max_address)
200221393Sjhb{
201221393Sjhb	char buf[64];
202221393Sjhb	int error, rid;
203221393Sjhb
204221393Sjhb	if (max_address != (u_long)max_address)
205221393Sjhb		max_address = ~0ul;
206221393Sjhb	w->rman.rm_start = 0;
207221393Sjhb	w->rman.rm_end = max_address;
208221393Sjhb	w->rman.rm_type = RMAN_ARRAY;
209221393Sjhb	snprintf(buf, sizeof(buf), "%s %s window",
210221393Sjhb	    device_get_nameunit(sc->dev), w->name);
211221393Sjhb	w->rman.rm_descr = strdup(buf, M_DEVBUF);
212221393Sjhb	error = rman_init(&w->rman);
213221393Sjhb	if (error)
214221393Sjhb		panic("Failed to initialize %s %s rman",
215221393Sjhb		    device_get_nameunit(sc->dev), w->name);
216221393Sjhb
217221393Sjhb	if (!pcib_is_window_open(w))
218221393Sjhb		return;
219221393Sjhb
220221393Sjhb	if (w->base > max_address || w->limit > max_address) {
221221393Sjhb		device_printf(sc->dev,
222221393Sjhb		    "initial %s window has too many bits, ignoring\n", w->name);
223221393Sjhb		return;
224221393Sjhb	}
225221393Sjhb	rid = w->reg;
226221393Sjhb	w->res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
227221393Sjhb	    w->limit - w->base + 1, flags);
228221393Sjhb	if (w->res == NULL) {
229221393Sjhb		device_printf(sc->dev,
230221393Sjhb		    "failed to allocate initial %s window: %#jx-%#jx\n",
231221393Sjhb		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
232221393Sjhb		w->base = max_address;
233221393Sjhb		w->limit = 0;
234221393Sjhb		pcib_write_windows(sc, w->mask);
235221393Sjhb		return;
236221393Sjhb	}
237221393Sjhb	pcib_activate_window(sc, type);
238221393Sjhb
239221393Sjhb	error = rman_manage_region(&w->rman, rman_get_start(w->res),
240221393Sjhb	    rman_get_end(w->res));
241221393Sjhb	if (error)
242221393Sjhb		panic("Failed to initialize rman with resource");
243221393Sjhb}
244221393Sjhb
245221393Sjhb/*
246221393Sjhb * Initialize I/O windows.
247221393Sjhb */
248221393Sjhbstatic void
249221393Sjhbpcib_probe_windows(struct pcib_softc *sc)
250221393Sjhb{
251221393Sjhb	pci_addr_t max;
252221393Sjhb	device_t dev;
253221393Sjhb	uint32_t val;
254221393Sjhb
255221393Sjhb	dev = sc->dev;
256221393Sjhb
257221393Sjhb	/* Determine if the I/O port window is implemented. */
258221393Sjhb	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
259221393Sjhb	if (val == 0) {
260221393Sjhb		/*
261221393Sjhb		 * If 'val' is zero, then only 16-bits of I/O space
262221393Sjhb		 * are supported.
263221393Sjhb		 */
264221393Sjhb		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
265221393Sjhb		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
266221393Sjhb			sc->io.valid = 1;
267221393Sjhb			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
268221393Sjhb		}
269221393Sjhb	} else
270221393Sjhb		sc->io.valid = 1;
271221393Sjhb
272221393Sjhb	/* Read the existing I/O port window. */
273221393Sjhb	if (sc->io.valid) {
274221393Sjhb		sc->io.reg = PCIR_IOBASEL_1;
275221393Sjhb		sc->io.step = 12;
276221393Sjhb		sc->io.mask = WIN_IO;
277221393Sjhb		sc->io.name = "I/O port";
278221393Sjhb		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
279221393Sjhb			sc->io.base = PCI_PPBIOBASE(
280221393Sjhb			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
281221393Sjhb			sc->io.limit = PCI_PPBIOLIMIT(
282221393Sjhb			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
283221393Sjhb			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
284221393Sjhb			max = 0xffffffff;
285221393Sjhb		} else {
286221393Sjhb			sc->io.base = PCI_PPBIOBASE(0, val);
287221393Sjhb			sc->io.limit = PCI_PPBIOLIMIT(0,
288221393Sjhb			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
289221393Sjhb			max = 0xffff;
290221393Sjhb		}
291221393Sjhb		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
292221393Sjhb	}
293221393Sjhb
294221393Sjhb	/* Read the existing memory window. */
295221393Sjhb	sc->mem.valid = 1;
296221393Sjhb	sc->mem.reg = PCIR_MEMBASE_1;
297221393Sjhb	sc->mem.step = 20;
298221393Sjhb	sc->mem.mask = WIN_MEM;
299221393Sjhb	sc->mem.name = "memory";
300221393Sjhb	sc->mem.base = PCI_PPBMEMBASE(0,
301221393Sjhb	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
302221393Sjhb	sc->mem.limit = PCI_PPBMEMLIMIT(0,
303221393Sjhb	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
304221393Sjhb	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
305221393Sjhb
306221393Sjhb	/* Determine if the prefetchable memory window is implemented. */
307221393Sjhb	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
308221393Sjhb	if (val == 0) {
309221393Sjhb		/*
310221393Sjhb		 * If 'val' is zero, then only 32-bits of memory space
311221393Sjhb		 * are supported.
312221393Sjhb		 */
313221393Sjhb		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
314221393Sjhb		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
315221393Sjhb			sc->pmem.valid = 1;
316221393Sjhb			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
317221393Sjhb		}
318221393Sjhb	} else
319221393Sjhb		sc->pmem.valid = 1;
320221393Sjhb
321221393Sjhb	/* Read the existing prefetchable memory window. */
322221393Sjhb	if (sc->pmem.valid) {
323221393Sjhb		sc->pmem.reg = PCIR_PMBASEL_1;
324221393Sjhb		sc->pmem.step = 20;
325221393Sjhb		sc->pmem.mask = WIN_PMEM;
326221393Sjhb		sc->pmem.name = "prefetch";
327221393Sjhb		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
328221393Sjhb			sc->pmem.base = PCI_PPBMEMBASE(
329221393Sjhb			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
330221393Sjhb			sc->pmem.limit = PCI_PPBMEMLIMIT(
331221393Sjhb			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
332221393Sjhb			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
333221393Sjhb			max = 0xffffffffffffffff;
334221393Sjhb		} else {
335221393Sjhb			sc->pmem.base = PCI_PPBMEMBASE(0, val);
336221393Sjhb			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
337221393Sjhb			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
338221393Sjhb			max = 0xffffffff;
339221393Sjhb		}
340221393Sjhb		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
341221393Sjhb		    RF_PREFETCHABLE, max);
342221393Sjhb	}
343221393Sjhb}
344221393Sjhb
345221393Sjhb#else
346221393Sjhb
347221393Sjhb/*
348163805Simp * Is the prefetch window open (eg, can we allocate memory in it?)
349163805Simp */
350163805Simpstatic int
351163805Simppcib_is_prefetch_open(struct pcib_softc *sc)
352163805Simp{
353163805Simp	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
354163805Simp}
355163805Simp
356163805Simp/*
357163805Simp * Is the nonprefetch window open (eg, can we allocate memory in it?)
358163805Simp */
359163805Simpstatic int
360163805Simppcib_is_nonprefetch_open(struct pcib_softc *sc)
361163805Simp{
362163805Simp	return (sc->membase > 0 && sc->membase < sc->memlimit);
363163805Simp}
364163805Simp
365163805Simp/*
366163805Simp * Is the io window open (eg, can we allocate ports in it?)
367163805Simp */
368163805Simpstatic int
369163805Simppcib_is_io_open(struct pcib_softc *sc)
370163805Simp{
371163805Simp	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
372163805Simp}
373163805Simp
374163805Simp/*
375200341Sjkim * Get current I/O decode.
376200341Sjkim */
377200341Sjkimstatic void
378200341Sjkimpcib_get_io_decode(struct pcib_softc *sc)
379200341Sjkim{
380200341Sjkim	device_t	dev;
381200341Sjkim	uint32_t	iolow;
382200341Sjkim
383200341Sjkim	dev = sc->dev;
384200341Sjkim
385200341Sjkim	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
386200341Sjkim	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
387200341Sjkim		sc->iobase = PCI_PPBIOBASE(
388200341Sjkim		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
389200341Sjkim	else
390200341Sjkim		sc->iobase = PCI_PPBIOBASE(0, iolow);
391200341Sjkim
392200341Sjkim	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
393200341Sjkim	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
394200341Sjkim		sc->iolimit = PCI_PPBIOLIMIT(
395200341Sjkim		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
396200341Sjkim	else
397200341Sjkim		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
398200341Sjkim}
399200341Sjkim
400200341Sjkim/*
401200341Sjkim * Get current memory decode.
402200341Sjkim */
403200341Sjkimstatic void
404200341Sjkimpcib_get_mem_decode(struct pcib_softc *sc)
405200341Sjkim{
406200341Sjkim	device_t	dev;
407200341Sjkim	pci_addr_t	pmemlow;
408200341Sjkim
409200341Sjkim	dev = sc->dev;
410200341Sjkim
411200341Sjkim	sc->membase = PCI_PPBMEMBASE(0,
412200341Sjkim	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
413200341Sjkim	sc->memlimit = PCI_PPBMEMLIMIT(0,
414200341Sjkim	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
415200341Sjkim
416200341Sjkim	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
417200341Sjkim	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
418200341Sjkim		sc->pmembase = PCI_PPBMEMBASE(
419200341Sjkim		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
420200341Sjkim	else
421200341Sjkim		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
422200341Sjkim
423200341Sjkim	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
424200341Sjkim	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
425200341Sjkim		sc->pmemlimit = PCI_PPBMEMLIMIT(
426200341Sjkim		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
427200341Sjkim	else
428200341Sjkim		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
429200341Sjkim}
430200341Sjkim
431200341Sjkim/*
432200341Sjkim * Restore previous I/O decode.
433200341Sjkim */
434200341Sjkimstatic void
435200341Sjkimpcib_set_io_decode(struct pcib_softc *sc)
436200341Sjkim{
437200341Sjkim	device_t	dev;
438200341Sjkim	uint32_t	iohi;
439200341Sjkim
440200341Sjkim	dev = sc->dev;
441200341Sjkim
442200341Sjkim	iohi = sc->iobase >> 16;
443200341Sjkim	if (iohi > 0)
444200341Sjkim		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
445200341Sjkim	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
446200341Sjkim
447200341Sjkim	iohi = sc->iolimit >> 16;
448200341Sjkim	if (iohi > 0)
449200341Sjkim		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
450200341Sjkim	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
451200341Sjkim}
452200341Sjkim
453200341Sjkim/*
454200341Sjkim * Restore previous memory decode.
455200341Sjkim */
456200341Sjkimstatic void
457200341Sjkimpcib_set_mem_decode(struct pcib_softc *sc)
458200341Sjkim{
459200341Sjkim	device_t	dev;
460200341Sjkim	pci_addr_t	pmemhi;
461200341Sjkim
462200341Sjkim	dev = sc->dev;
463200341Sjkim
464200341Sjkim	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
465200341Sjkim	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
466200341Sjkim
467200341Sjkim	pmemhi = sc->pmembase >> 32;
468200341Sjkim	if (pmemhi > 0)
469200341Sjkim		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
470200341Sjkim	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
471200341Sjkim
472200341Sjkim	pmemhi = sc->pmemlimit >> 32;
473200341Sjkim	if (pmemhi > 0)
474200341Sjkim		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
475200341Sjkim	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
476200341Sjkim}
477221393Sjhb#endif
478200341Sjkim
479200341Sjkim/*
480200341Sjkim * Get current bridge configuration.
481200341Sjkim */
482200341Sjkimstatic void
483200341Sjkimpcib_cfg_save(struct pcib_softc *sc)
484200341Sjkim{
485200341Sjkim	device_t	dev;
486200341Sjkim
487200341Sjkim	dev = sc->dev;
488200341Sjkim
489200341Sjkim	sc->command = pci_read_config(dev, PCIR_COMMAND, 2);
490200341Sjkim	sc->pribus = pci_read_config(dev, PCIR_PRIBUS_1, 1);
491200341Sjkim	sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1);
492200341Sjkim	sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
493200341Sjkim	sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
494200341Sjkim	sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1);
495221393Sjhb#ifndef NEW_PCIB
496200341Sjkim	if (sc->command & PCIM_CMD_PORTEN)
497200341Sjkim		pcib_get_io_decode(sc);
498200341Sjkim	if (sc->command & PCIM_CMD_MEMEN)
499200341Sjkim		pcib_get_mem_decode(sc);
500221393Sjhb#endif
501200341Sjkim}
502200341Sjkim
503200341Sjkim/*
504200341Sjkim * Restore previous bridge configuration.
505200341Sjkim */
506200341Sjkimstatic void
507200341Sjkimpcib_cfg_restore(struct pcib_softc *sc)
508200341Sjkim{
509200341Sjkim	device_t	dev;
510200341Sjkim
511200341Sjkim	dev = sc->dev;
512200341Sjkim
513200341Sjkim	pci_write_config(dev, PCIR_COMMAND, sc->command, 2);
514200341Sjkim	pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
515200341Sjkim	pci_write_config(dev, PCIR_SECBUS_1, sc->secbus, 1);
516200341Sjkim	pci_write_config(dev, PCIR_SUBBUS_1, sc->subbus, 1);
517200341Sjkim	pci_write_config(dev, PCIR_BRIDGECTL_1, sc->bridgectl, 2);
518200341Sjkim	pci_write_config(dev, PCIR_SECLAT_1, sc->seclat, 1);
519221393Sjhb#ifdef NEW_PCIB
520221393Sjhb	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
521221393Sjhb#else
522200341Sjkim	if (sc->command & PCIM_CMD_PORTEN)
523200341Sjkim		pcib_set_io_decode(sc);
524200341Sjkim	if (sc->command & PCIM_CMD_MEMEN)
525200341Sjkim		pcib_set_mem_decode(sc);
526221393Sjhb#endif
527200341Sjkim}
528200341Sjkim
529200341Sjkim/*
53069783Smsmith * Generic device interface
53169783Smsmith */
53269783Smsmithstatic int
53369783Smsmithpcib_probe(device_t dev)
53469783Smsmith{
53569783Smsmith    if ((pci_get_class(dev) == PCIC_BRIDGE) &&
53669783Smsmith	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
53769783Smsmith	device_set_desc(dev, "PCI-PCI bridge");
53869783Smsmith	return(-10000);
53969783Smsmith    }
54069783Smsmith    return(ENXIO);
54169783Smsmith}
54269783Smsmith
543102441Sjhbvoid
544102441Sjhbpcib_attach_common(device_t dev)
54569783Smsmith{
54669783Smsmith    struct pcib_softc	*sc;
547181789Simp    struct sysctl_ctx_list *sctx;
548181789Simp    struct sysctl_oid	*soid;
54969783Smsmith
55069783Smsmith    sc = device_get_softc(dev);
55169783Smsmith    sc->dev = dev;
55269783Smsmith
55369908Smsmith    /*
55469908Smsmith     * Get current bridge configuration.
55569908Smsmith     */
556200341Sjkim    sc->domain = pci_get_domain(dev);
557200341Sjkim    sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2);
558200341Sjkim    pcib_cfg_save(sc);
55969783Smsmith
56069908Smsmith    /*
561181789Simp     * Setup sysctl reporting nodes
562181789Simp     */
563181789Simp    sctx = device_get_sysctl_ctx(dev);
564181789Simp    soid = device_get_sysctl_tree(dev);
565181789Simp    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
566182706Simp      CTLFLAG_RD, &sc->domain, 0, "Domain number");
567181789Simp    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
568182706Simp      CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
569181789Simp    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
570182706Simp      CTLFLAG_RD, &sc->secbus, 0, "Secondary bus number");
571181789Simp    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
572182706Simp      CTLFLAG_RD, &sc->subbus, 0, "Subordinate bus number");
573181789Simp
574181789Simp    /*
57569908Smsmith     * Quirk handling.
57669908Smsmith     */
57769908Smsmith    switch (pci_get_devid(dev)) {
578124365Simp    case 0x12258086:		/* Intel 82454KX/GX (Orion) */
57969908Smsmith	{
580119266Simp	    uint8_t	supbus;
58169908Smsmith
58269908Smsmith	    supbus = pci_read_config(dev, 0x41, 1);
58369908Smsmith	    if (supbus != 0xff) {
58469908Smsmith		sc->secbus = supbus + 1;
58569908Smsmith		sc->subbus = supbus + 1;
58669908Smsmith	    }
587124365Simp	    break;
58869908Smsmith	}
589124365Simp
590124365Simp    /*
591124365Simp     * The i82380FB mobile docking controller is a PCI-PCI bridge,
592124365Simp     * and it is a subtractive bridge.  However, the ProgIf is wrong
593124365Simp     * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
594124365Simp     * happen.  There's also a Toshiba bridge that behaves this
595124365Simp     * way.
596124365Simp     */
597124365Simp    case 0x124b8086:		/* Intel 82380FB Mobile */
598124365Simp    case 0x060513d7:		/* Toshiba ???? */
599124365Simp	sc->flags |= PCIB_SUBTRACTIVE;
60069908Smsmith	break;
601149521Sjkim
602149521Sjkim    /* Compaq R3000 BIOS sets wrong subordinate bus number. */
603149521Sjkim    case 0x00dd10de:
604149521Sjkim	{
605149521Sjkim	    char *cp;
606149521Sjkim
607157949Sjkim	    if ((cp = getenv("smbios.planar.maker")) == NULL)
608149521Sjkim		break;
609157949Sjkim	    if (strncmp(cp, "Compal", 6) != 0) {
610157949Sjkim		freeenv(cp);
611149521Sjkim		break;
612157949Sjkim	    }
613157949Sjkim	    freeenv(cp);
614157949Sjkim	    if ((cp = getenv("smbios.planar.product")) == NULL)
615157949Sjkim		break;
616157949Sjkim	    if (strncmp(cp, "08A0", 4) != 0) {
617157949Sjkim		freeenv(cp);
618157949Sjkim		break;
619157949Sjkim	    }
620157949Sjkim	    freeenv(cp);
621149521Sjkim	    if (sc->subbus < 0xa) {
622149521Sjkim		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
623149521Sjkim		sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
624149521Sjkim	    }
625149521Sjkim	    break;
626149521Sjkim	}
62769908Smsmith    }
62869908Smsmith
629165995Sjhb    if (pci_msi_device_blacklisted(dev))
630165995Sjhb	sc->flags |= PCIB_DISABLE_MSI;
631165995Sjhb
632124365Simp    /*
633124365Simp     * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
634124365Simp     * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
635124365Simp     * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
636124365Simp     * This means they act as if they were subtractively decoding
637124365Simp     * bridges and pass all transactions.  Mark them and real ProgIf 1
638124365Simp     * parts as subtractive.
639124365Simp     */
640124365Simp    if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
641168157Sjhb      pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
642124365Simp	sc->flags |= PCIB_SUBTRACTIVE;
643221393Sjhb
644221393Sjhb#ifdef NEW_PCIB
645221393Sjhb    pcib_probe_windows(sc);
646221393Sjhb#endif
64769783Smsmith    if (bootverbose) {
648172394Smarius	device_printf(dev, "  domain            %d\n", sc->domain);
64969783Smsmith	device_printf(dev, "  secondary bus     %d\n", sc->secbus);
65069783Smsmith	device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
651221393Sjhb#ifdef NEW_PCIB
652221393Sjhb	if (pcib_is_window_open(&sc->io))
653221393Sjhb	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
654221393Sjhb	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
655221393Sjhb	if (pcib_is_window_open(&sc->mem))
656221393Sjhb	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
657221393Sjhb	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
658221393Sjhb	if (pcib_is_window_open(&sc->pmem))
659221393Sjhb	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
660221393Sjhb	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
661221393Sjhb#else
662221393Sjhb	if (pcib_is_io_open(sc))
663221393Sjhb	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
664221393Sjhb	      sc->iobase, sc->iolimit);
665163805Simp	if (pcib_is_nonprefetch_open(sc))
666163805Simp	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
667163805Simp	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
668163805Simp	if (pcib_is_prefetch_open(sc))
669163805Simp	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
670163805Simp	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
671221393Sjhb#endif
672163805Simp	else
673163805Simp	    device_printf(dev, "  no prefetched decode\n");
674124365Simp	if (sc->flags & PCIB_SUBTRACTIVE)
675124365Simp	    device_printf(dev, "  Subtractively decoded bridge.\n");
67669783Smsmith    }
67769783Smsmith
67869783Smsmith    /*
67969783Smsmith     * XXX If the secondary bus number is zero, we should assign a bus number
680181798Simp     *     since the BIOS hasn't, then initialise the bridge.  A simple
681181798Simp     *     bus_alloc_resource with the a couple of busses seems like the right
682181798Simp     *     approach, but we don't know what busses the BIOS might have already
683181798Simp     *     assigned to other bridges on this bus that probe later than we do.
684181798Simp     *
685181798Simp     *     If the subordinate bus number is less than the secondary bus number,
68669783Smsmith     *     we should pick a better value.  One sensible alternative would be to
68769783Smsmith     *     pick 255; the only tradeoff here is that configuration transactions
688181798Simp     *     would be more widely routed than absolutely necessary.  We could
689181798Simp     *     then do a walk of the tree later and fix it.
69069783Smsmith     */
691102441Sjhb}
69269783Smsmith
693103042Sjhbint
694102441Sjhbpcib_attach(device_t dev)
695102441Sjhb{
696102441Sjhb    struct pcib_softc	*sc;
697102441Sjhb    device_t		child;
698102441Sjhb
699102441Sjhb    pcib_attach_common(dev);
700102441Sjhb    sc = device_get_softc(dev);
70169783Smsmith    if (sc->secbus != 0) {
702103016Sjhb	child = device_add_child(dev, "pci", sc->secbus);
70369783Smsmith	if (child != NULL)
70469783Smsmith	    return(bus_generic_attach(dev));
705181798Simp    }
70669783Smsmith
70769783Smsmith    /* no secondary bus; we should have fixed this */
70869783Smsmith    return(0);
70969783Smsmith}
71069783Smsmith
711102441Sjhbint
712200341Sjkimpcib_suspend(device_t dev)
713200341Sjkim{
714211430Sjhb	device_t	pcib;
715200341Sjkim	int		dstate, error;
716200341Sjkim
717200341Sjkim	pcib_cfg_save(device_get_softc(dev));
718200341Sjkim	error = bus_generic_suspend(dev);
719214110Sjkim	if (error == 0 && pci_do_power_suspend) {
720211430Sjhb		dstate = PCI_POWERSTATE_D3;
721211430Sjhb		pcib = device_get_parent(device_get_parent(dev));
722211430Sjhb		if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0)
723200341Sjkim			pci_set_powerstate(dev, dstate);
724200341Sjkim	}
725200341Sjkim	return (error);
726200341Sjkim}
727200341Sjkim
728200341Sjkimint
729200341Sjkimpcib_resume(device_t dev)
730200341Sjkim{
731211430Sjhb	device_t	pcib;
732200341Sjkim
733200341Sjkim	if (pci_do_power_resume) {
734211430Sjhb		pcib = device_get_parent(device_get_parent(dev));
735211430Sjhb		if (PCIB_POWER_FOR_SLEEP(pcib, dev, NULL) == 0)
736200341Sjkim			pci_set_powerstate(dev, PCI_POWERSTATE_D0);
737200341Sjkim	}
738200341Sjkim	pcib_cfg_restore(device_get_softc(dev));
739200341Sjkim	return (bus_generic_resume(dev));
740200341Sjkim}
741200341Sjkim
742200341Sjkimint
74369783Smsmithpcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
74469783Smsmith{
74569783Smsmith    struct pcib_softc	*sc = device_get_softc(dev);
74669783Smsmith
74769783Smsmith    switch (which) {
748172394Smarius    case PCIB_IVAR_DOMAIN:
749172394Smarius	*result = sc->domain;
750172394Smarius	return(0);
75169783Smsmith    case PCIB_IVAR_BUS:
75269783Smsmith	*result = sc->secbus;
75369783Smsmith	return(0);
75469783Smsmith    }
75569783Smsmith    return(ENOENT);
75669783Smsmith}
75769783Smsmith
758102441Sjhbint
75969783Smsmithpcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
76069783Smsmith{
76169783Smsmith    struct pcib_softc	*sc = device_get_softc(dev);
76269783Smsmith
76369783Smsmith    switch (which) {
764172394Smarius    case PCIB_IVAR_DOMAIN:
765172394Smarius	return(EINVAL);
76669783Smsmith    case PCIB_IVAR_BUS:
76769783Smsmith	sc->secbus = value;
768172394Smarius	return(0);
76969783Smsmith    }
77069783Smsmith    return(ENOENT);
77169783Smsmith}
77269783Smsmith
773221393Sjhb#ifdef NEW_PCIB
774221393Sjhbstatic const char *
775221393Sjhbpcib_child_name(device_t child)
776221393Sjhb{
777221393Sjhb	static char buf[64];
778221393Sjhb
779221393Sjhb	if (device_get_nameunit(child) != NULL)
780221393Sjhb		return (device_get_nameunit(child));
781221393Sjhb	snprintf(buf, sizeof(buf), "pci%d:%d:%d:%d", pci_get_domain(child),
782221393Sjhb	    pci_get_bus(child), pci_get_slot(child), pci_get_function(child));
783221393Sjhb	return (buf);
784221393Sjhb}
785221393Sjhb
78669783Smsmith/*
787221393Sjhb * Attempt to allocate a resource from the existing resources assigned
788221393Sjhb * to a window.
789221393Sjhb */
790221393Sjhbstatic struct resource *
791221393Sjhbpcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
792221393Sjhb    device_t child, int type, int *rid, u_long start, u_long end, u_long count,
793221393Sjhb    u_int flags)
794221393Sjhb{
795221393Sjhb	struct resource *res;
796221393Sjhb
797221393Sjhb	if (!pcib_is_window_open(w))
798221393Sjhb		return (NULL);
799221393Sjhb
800221393Sjhb	res = rman_reserve_resource(&w->rman, start, end, count,
801221393Sjhb	    flags & ~RF_ACTIVE, child);
802221393Sjhb	if (res == NULL)
803221393Sjhb		return (NULL);
804221393Sjhb
805221393Sjhb	if (bootverbose)
806221393Sjhb		device_printf(sc->dev,
807221393Sjhb		    "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
808221393Sjhb		    w->name, rman_get_start(res), rman_get_end(res), *rid,
809221393Sjhb		    pcib_child_name(child));
810221393Sjhb	rman_set_rid(res, *rid);
811221393Sjhb
812221393Sjhb	/*
813221393Sjhb	 * If the resource should be active, pass that request up the
814221393Sjhb	 * tree.  This assumes the parent drivers can handle
815221393Sjhb	 * activating sub-allocated resources.
816221393Sjhb	 */
817221393Sjhb	if (flags & RF_ACTIVE) {
818221393Sjhb		if (bus_activate_resource(child, type, *rid, res) != 0) {
819221393Sjhb			rman_release_resource(res);
820221393Sjhb			return (NULL);
821221393Sjhb		}
822221393Sjhb	}
823221393Sjhb
824221393Sjhb	return (res);
825221393Sjhb}
826221393Sjhb
827221393Sjhb/*
828221393Sjhb * Attempt to grow a window to make room for a given resource request.
829221393Sjhb * The 'step' parameter is log_2 of the desired I/O window's alignment.
830221393Sjhb */
831221393Sjhbstatic int
832221393Sjhbpcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
833221393Sjhb    u_long start, u_long end, u_long count, u_int flags)
834221393Sjhb{
835221393Sjhb	u_long align, start_free, end_free, front, back;
836221393Sjhb	int error, rid;
837221393Sjhb
838221393Sjhb	/*
839221393Sjhb	 * Clamp the desired resource range to the maximum address
840221393Sjhb	 * this window supports.  Reject impossible requests.
841221393Sjhb	 */
842221393Sjhb	if (!w->valid)
843221393Sjhb		return (EINVAL);
844221393Sjhb	if (end > w->rman.rm_end)
845221393Sjhb		end = w->rman.rm_end;
846221393Sjhb	if (start + count - 1 > end || start + count < start)
847221393Sjhb		return (EINVAL);
848221393Sjhb
849221393Sjhb	/*
850221393Sjhb	 * If there is no resource at all, just try to allocate enough
851221393Sjhb	 * aligned space for this resource.
852221393Sjhb	 */
853221393Sjhb	if (w->res == NULL) {
854221393Sjhb		if (RF_ALIGNMENT(flags) < w->step) {
855221393Sjhb			flags &= ~RF_ALIGNMENT_MASK;
856221393Sjhb			flags |= RF_ALIGNMENT_LOG2(w->step);
857221393Sjhb		}
858221393Sjhb		start &= ~((1ul << w->step) - 1);
859221393Sjhb		end |= ((1ul << w->step) - 1);
860221393Sjhb		count = roundup2(count, 1ul << w->step);
861221393Sjhb		rid = w->reg;
862221393Sjhb		w->res = bus_alloc_resource(sc->dev, type, &rid, start, end,
863221393Sjhb		    count, flags & ~RF_ACTIVE);
864221393Sjhb		if (w->res == NULL) {
865221393Sjhb			if (bootverbose)
866221393Sjhb				device_printf(sc->dev,
867221393Sjhb		    "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
868221393Sjhb				    w->name, start, end, count);
869221393Sjhb			return (ENXIO);
870221393Sjhb		}
871221393Sjhb		if (bootverbose)
872221393Sjhb			device_printf(sc->dev,
873221393Sjhb			    "allocated initial %s window of %#lx-%#lx\n",
874221393Sjhb			    w->name, rman_get_start(w->res),
875221393Sjhb			    rman_get_end(w->res));
876221393Sjhb		error = rman_manage_region(&w->rman, rman_get_start(w->res),
877221393Sjhb		    rman_get_end(w->res));
878221393Sjhb		if (error) {
879221393Sjhb			if (bootverbose)
880221393Sjhb				device_printf(sc->dev,
881221393Sjhb				    "failed to add initial %s window to rman\n",
882221393Sjhb				    w->name);
883221393Sjhb			bus_release_resource(sc->dev, type, w->reg, w->res);
884221393Sjhb			w->res = NULL;
885221393Sjhb			return (error);
886221393Sjhb		}
887221393Sjhb		pcib_activate_window(sc, type);
888221393Sjhb		goto updatewin;
889221393Sjhb	}
890221393Sjhb
891221393Sjhb	/*
892221393Sjhb	 * See if growing the window would help.  Compute the minimum
893221393Sjhb	 * amount of address space needed on both the front and back
894221393Sjhb	 * ends of the existing window to satisfy the allocation.
895221393Sjhb	 *
896221393Sjhb	 * For each end, build a candidate region adjusting for the
897221393Sjhb	 * required alignment, etc.  If there is a free region at the
898221393Sjhb	 * edge of the window, grow from the inner edge of the free
899221393Sjhb	 * region.  Otherwise grow from the window boundary.
900221393Sjhb	 *
901221393Sjhb	 * XXX: Special case: if w->res is completely empty and the
902221393Sjhb	 * request size is larger than w->res, we should find the
903221393Sjhb	 * optimal aligned buffer containing w->res and allocate that.
904221393Sjhb	 */
905221393Sjhb	if (bootverbose)
906221393Sjhb		device_printf(sc->dev,
907221393Sjhb		    "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
908221393Sjhb		    w->name, start, end, count);
909221393Sjhb	align = 1ul << RF_ALIGNMENT(flags);
910221393Sjhb	if (start < rman_get_start(w->res)) {
911221393Sjhb		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
912221393Sjhb		    0 || start_free != rman_get_start(w->res))
913221393Sjhb			end_free = rman_get_start(w->res) - 1;
914221393Sjhb		if (end_free > end)
915221393Sjhb			end_free = end;
916221393Sjhb
917221393Sjhb		/* Move end_free down until it is properly aligned. */
918221393Sjhb		end_free &= ~(align - 1);
919221393Sjhb		front = end_free - count;
920221393Sjhb
921221393Sjhb		/*
922221393Sjhb		 * The resource would now be allocated at (front,
923221393Sjhb		 * end_free).  Ensure that fits in the (start, end)
924221393Sjhb		 * bounds.  end_free is checked above.  If 'front' is
925221393Sjhb		 * ok, ensure it is properly aligned for this window.
926221393Sjhb		 * Also check for underflow.
927221393Sjhb		 */
928221393Sjhb		if (front >= start && front <= end_free) {
929221393Sjhb			if (bootverbose)
930221393Sjhb				printf("\tfront candidate range: %#lx-%#lx\n",
931221393Sjhb				    front, end_free);
932221393Sjhb			front &= (1ul << w->step) - 1;
933221393Sjhb			front = rman_get_start(w->res) - front;
934221393Sjhb		} else
935221393Sjhb			front = 0;
936221393Sjhb	} else
937221393Sjhb		front = 0;
938221393Sjhb	if (end > rman_get_end(w->res)) {
939221393Sjhb		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
940221393Sjhb		    0 || end_free != rman_get_end(w->res))
941221393Sjhb			start_free = rman_get_end(w->res) + 1;
942221393Sjhb		if (start_free < start)
943221393Sjhb			start_free = start;
944221393Sjhb
945221393Sjhb		/* Move start_free up until it is properly aligned. */
946221393Sjhb		start_free = roundup2(start_free, align);
947221393Sjhb		back = start_free + count;
948221393Sjhb
949221393Sjhb		/*
950221393Sjhb		 * The resource would now be allocated at (start_free,
951221393Sjhb		 * back).  Ensure that fits in the (start, end)
952221393Sjhb		 * bounds.  start_free is checked above.  If 'back' is
953221393Sjhb		 * ok, ensure it is properly aligned for this window.
954221393Sjhb		 * Also check for overflow.
955221393Sjhb		 */
956221393Sjhb		if (back <= end && start_free <= back) {
957221393Sjhb			if (bootverbose)
958221393Sjhb				printf("\tback candidate range: %#lx-%#lx\n",
959221393Sjhb				    start_free, back);
960221393Sjhb			back = roundup2(back, w->step) - 1;
961221393Sjhb			back -= rman_get_end(w->res);
962221393Sjhb		} else
963221393Sjhb			back = 0;
964221393Sjhb	} else
965221393Sjhb		back = 0;
966221393Sjhb
967221393Sjhb	/*
968221393Sjhb	 * Try to allocate the smallest needed region first.
969221393Sjhb	 * If that fails, fall back to the other region.
970221393Sjhb	 */
971221393Sjhb	error = ENOSPC;
972221393Sjhb	while (front != 0 || back != 0) {
973221393Sjhb		if (front != 0 && (front <= back || back == 0)) {
974221393Sjhb			error = bus_adjust_resource(sc->dev, type, w->res,
975221393Sjhb			    rman_get_start(w->res) - front,
976221393Sjhb			    rman_get_end(w->res));
977221393Sjhb			if (error == 0)
978221393Sjhb				break;
979221393Sjhb			front = 0;
980221393Sjhb		} else {
981221393Sjhb			error = bus_adjust_resource(sc->dev, type, w->res,
982221393Sjhb			    rman_get_start(w->res),
983221393Sjhb			    rman_get_end(w->res) + back);
984221393Sjhb			if (error == 0)
985221393Sjhb				break;
986221393Sjhb			back = 0;
987221393Sjhb		}
988221393Sjhb	}
989221393Sjhb
990221393Sjhb	if (error)
991221393Sjhb		return (error);
992221393Sjhb	if (bootverbose)
993221393Sjhb		device_printf(sc->dev, "grew %s window to %#lx-%#lx\n",
994221393Sjhb		    w->name, rman_get_start(w->res), rman_get_end(w->res));
995221393Sjhb
996221393Sjhb	/* Add the newly allocated region to the resource manager. */
997221393Sjhb	if (w->base != rman_get_start(w->res)) {
998221393Sjhb		KASSERT(w->limit == rman_get_end(w->res), ("both ends moved"));
999221393Sjhb		error = rman_manage_region(&w->rman, rman_get_start(w->res),
1000221393Sjhb		    w->base - 1);
1001221393Sjhb	} else {
1002221393Sjhb		KASSERT(w->limit != rman_get_end(w->res),
1003221393Sjhb		    ("neither end moved"));
1004221393Sjhb		error = rman_manage_region(&w->rman, w->limit + 1,
1005221393Sjhb		    rman_get_end(w->res));
1006221393Sjhb	}
1007221393Sjhb	if (error) {
1008221393Sjhb		if (bootverbose)
1009221393Sjhb			device_printf(sc->dev,
1010221393Sjhb			    "failed to expand %s resource manager\n", w->name);
1011221393Sjhb		bus_adjust_resource(sc->dev, type, w->res, w->base, w->limit);
1012221393Sjhb		return (error);
1013221393Sjhb	}
1014221393Sjhb
1015221393Sjhbupdatewin:
1016221393Sjhb	/* Save the new window. */
1017221393Sjhb	w->base = rman_get_start(w->res);
1018221393Sjhb	w->limit = rman_get_end(w->res);
1019221393Sjhb	KASSERT((w->base & ((1ul << w->step) - 1)) == 0,
1020221393Sjhb	    ("start address is not aligned"));
1021221393Sjhb	KASSERT((w->limit & ((1ul << w->step) - 1)) == (1ul << w->step) - 1,
1022221393Sjhb	    ("end address is not aligned"));
1023221393Sjhb	pcib_write_windows(sc, w->mask);
1024221393Sjhb	return (0);
1025221393Sjhb}
1026221393Sjhb
1027221393Sjhb/*
102869783Smsmith * We have to trap resource allocation requests and ensure that the bridge
102969783Smsmith * is set up to, or capable of handling them.
103069783Smsmith */
1031102441Sjhbstruct resource *
1032221393Sjhbpcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1033221393Sjhb    u_long start, u_long end, u_long count, u_int flags)
1034221393Sjhb{
1035221393Sjhb	struct pcib_softc *sc;
1036221393Sjhb	struct resource *r;
1037221393Sjhb
1038221393Sjhb	sc = device_get_softc(dev);
1039221393Sjhb
1040221393Sjhb	/*
1041221393Sjhb	 * VGA resources are decoded iff the VGA enable bit is set in
1042221393Sjhb	 * the bridge control register.  VGA resources do not fall into
1043221393Sjhb	 * the resource windows and are passed up to the parent.
1044221393Sjhb	 */
1045221393Sjhb	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
1046221393Sjhb	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
1047221393Sjhb		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
1048221393Sjhb			return (bus_generic_alloc_resource(dev, child, type,
1049221393Sjhb			    rid, start, end, count, flags));
1050221393Sjhb		else
1051221393Sjhb			return (NULL);
1052221393Sjhb	}
1053221393Sjhb
1054221393Sjhb	switch (type) {
1055221393Sjhb	case SYS_RES_IOPORT:
1056221393Sjhb		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
1057221393Sjhb		    end, count, flags);
1058221393Sjhb		if (r != NULL)
1059221393Sjhb			break;
1060221393Sjhb		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
1061221393Sjhb		    flags) == 0)
1062221393Sjhb			r = pcib_suballoc_resource(sc, &sc->io, child, type,
1063221393Sjhb			    rid, start, end, count, flags);
1064221393Sjhb		break;
1065221393Sjhb	case SYS_RES_MEMORY:
1066221393Sjhb		/*
1067221393Sjhb		 * For prefetchable resources, prefer the prefetchable
1068221393Sjhb		 * memory window, but fall back to the regular memory
1069221393Sjhb		 * window if that fails.  Try both windows before
1070221393Sjhb		 * attempting to grow a window in case the firmware
1071221393Sjhb		 * has used a range in the regular memory window to
1072221393Sjhb		 * map a prefetchable BAR.
1073221393Sjhb		 */
1074221393Sjhb		if (flags & RF_PREFETCHABLE) {
1075221393Sjhb			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
1076221393Sjhb			    rid, start, end, count, flags);
1077221393Sjhb			if (r != NULL)
1078221393Sjhb				break;
1079221393Sjhb		}
1080221393Sjhb		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
1081221393Sjhb		    start, end, count, flags);
1082221393Sjhb		if (r != NULL)
1083221393Sjhb			break;
1084221393Sjhb		if (flags & RF_PREFETCHABLE) {
1085221393Sjhb			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
1086221393Sjhb			    count, flags) == 0) {
1087221393Sjhb				r = pcib_suballoc_resource(sc, &sc->pmem, child,
1088221393Sjhb				    type, rid, start, end, count, flags);
1089221393Sjhb				if (r != NULL)
1090221393Sjhb					break;
1091221393Sjhb			}
1092221393Sjhb		}
1093221393Sjhb		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
1094221393Sjhb		    flags & ~RF_PREFETCHABLE) == 0)
1095221393Sjhb			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
1096221393Sjhb			    rid, start, end, count, flags);
1097221393Sjhb		break;
1098221393Sjhb	default:
1099221393Sjhb		return (bus_generic_alloc_resource(dev, child, type, rid,
1100221393Sjhb		    start, end, count, flags));
1101221393Sjhb	}
1102221393Sjhb
1103221393Sjhb	/*
1104221393Sjhb	 * If attempts to suballocate from the window fail but this is a
1105221393Sjhb	 * subtractive bridge, pass the request up the tree.
1106221393Sjhb	 */
1107221393Sjhb	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
1108221393Sjhb		return (bus_generic_alloc_resource(dev, child, type, rid,
1109221393Sjhb		    start, end, count, flags));
1110221393Sjhb	return (r);
1111221393Sjhb}
1112221393Sjhb
1113221393Sjhbint
1114221393Sjhbpcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
1115221393Sjhb    u_long start, u_long end)
1116221393Sjhb{
1117221393Sjhb	struct pcib_softc *sc;
1118221393Sjhb
1119221393Sjhb	sc = device_get_softc(bus);
1120221393Sjhb	if (pcib_is_resource_managed(sc, type, r))
1121221393Sjhb		return (rman_adjust_resource(r, start, end));
1122221393Sjhb	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
1123221393Sjhb}
1124221393Sjhb
1125221393Sjhbint
1126221393Sjhbpcib_release_resource(device_t dev, device_t child, int type, int rid,
1127221393Sjhb    struct resource *r)
1128221393Sjhb{
1129221393Sjhb	struct pcib_softc *sc;
1130221393Sjhb	int error;
1131221393Sjhb
1132221393Sjhb	sc = device_get_softc(dev);
1133221393Sjhb	if (pcib_is_resource_managed(sc, type, r)) {
1134221393Sjhb		if (rman_get_flags(r) & RF_ACTIVE) {
1135221393Sjhb			error = bus_deactivate_resource(child, type, rid, r);
1136221393Sjhb			if (error)
1137221393Sjhb				return (error);
1138221393Sjhb		}
1139221393Sjhb		return (rman_release_resource(r));
1140221393Sjhb	}
1141221393Sjhb	return (bus_generic_release_resource(dev, child, type, rid, r));
1142221393Sjhb}
1143221393Sjhb#else
1144221393Sjhb/*
1145221393Sjhb * We have to trap resource allocation requests and ensure that the bridge
1146221393Sjhb * is set up to, or capable of handling them.
1147221393Sjhb */
1148221393Sjhbstruct resource *
114969783Smsmithpcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1150142051Simp    u_long start, u_long end, u_long count, u_int flags)
115169783Smsmith{
1152124365Simp	struct pcib_softc	*sc = device_get_softc(dev);
1153164130Sjhb	const char *name, *suffix;
1154124365Simp	int ok;
115569783Smsmith
115669783Smsmith	/*
115769783Smsmith	 * Fail the allocation for this range if it's not supported.
115869783Smsmith	 */
1159164130Sjhb	name = device_get_nameunit(child);
1160164130Sjhb	if (name == NULL) {
1161164130Sjhb		name = "";
1162164130Sjhb		suffix = "";
1163164130Sjhb	} else
1164164130Sjhb		suffix = " ";
116569783Smsmith	switch (type) {
116669783Smsmith	case SYS_RES_IOPORT:
1167107546Simp		ok = 0;
1168124365Simp		if (!pcib_is_io_open(sc))
1169124365Simp			break;
1170124365Simp		ok = (start >= sc->iobase && end <= sc->iolimit);
1171145652Smarcel
1172145652Smarcel		/*
1173145652Smarcel		 * Make sure we allow access to VGA I/O addresses when the
1174145652Smarcel		 * bridge has the "VGA Enable" bit set.
1175145652Smarcel		 */
1176145652Smarcel		if (!ok && pci_is_vga_ioport_range(start, end))
1177145652Smarcel			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1178145652Smarcel
1179124365Simp		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1180124365Simp			if (!ok) {
1181124365Simp				if (start < sc->iobase)
1182124365Simp					start = sc->iobase;
1183124365Simp				if (end > sc->iolimit)
1184124365Simp					end = sc->iolimit;
1185142051Simp				if (start < end)
1186142051Simp					ok = 1;
1187124365Simp			}
1188106844Smdodd		} else {
1189124365Simp			ok = 1;
1190189844Simp#if 0
1191189792Simp			/*
1192189792Simp			 * If we overlap with the subtractive range, then
1193189792Simp			 * pick the upper range to use.
1194189792Simp			 */
1195189792Simp			if (start < sc->iolimit && end > sc->iobase)
1196189792Simp				start = sc->iolimit + 1;
1197189844Simp#endif
1198106844Smdodd		}
1199124365Simp		if (end < start) {
1200142051Simp			device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
1201142051Simp			    end, start);
1202124365Simp			start = 0;
1203124365Simp			end = 0;
1204124365Simp			ok = 0;
1205124365Simp		}
1206124365Simp		if (!ok) {
1207164130Sjhb			device_printf(dev, "%s%srequested unsupported I/O "
1208124365Simp			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
1209164130Sjhb			    name, suffix, start, end, sc->iobase, sc->iolimit);
1210124365Simp			return (NULL);
1211124365Simp		}
1212124365Simp		if (bootverbose)
1213142051Simp			device_printf(dev,
1214164130Sjhb			    "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
1215164130Sjhb			    name, suffix, start, end);
1216124365Simp		break;
121769783Smsmith
121869783Smsmith	case SYS_RES_MEMORY:
1219107546Simp		ok = 0;
1220107546Simp		if (pcib_is_nonprefetch_open(sc))
1221124365Simp			ok = ok || (start >= sc->membase && end <= sc->memlimit);
1222107546Simp		if (pcib_is_prefetch_open(sc))
1223124365Simp			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1224145652Smarcel
1225145652Smarcel		/*
1226145652Smarcel		 * Make sure we allow access to VGA memory addresses when the
1227145652Smarcel		 * bridge has the "VGA Enable" bit set.
1228145652Smarcel		 */
1229145652Smarcel		if (!ok && pci_is_vga_memory_range(start, end))
1230145652Smarcel			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1231145652Smarcel
1232124365Simp		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1233124365Simp			if (!ok) {
1234124365Simp				ok = 1;
1235124365Simp				if (flags & RF_PREFETCHABLE) {
1236124365Simp					if (pcib_is_prefetch_open(sc)) {
1237124365Simp						if (start < sc->pmembase)
1238124365Simp							start = sc->pmembase;
1239124365Simp						if (end > sc->pmemlimit)
1240124365Simp							end = sc->pmemlimit;
1241124365Simp					} else {
1242124365Simp						ok = 0;
1243124365Simp					}
1244124365Simp				} else {	/* non-prefetchable */
1245124365Simp					if (pcib_is_nonprefetch_open(sc)) {
1246124365Simp						if (start < sc->membase)
1247124365Simp							start = sc->membase;
1248124365Simp						if (end > sc->memlimit)
1249124365Simp							end = sc->memlimit;
1250124365Simp					} else {
1251124365Simp						ok = 0;
1252124365Simp					}
1253124365Simp				}
1254107546Simp			}
1255107546Simp		} else if (!ok) {
1256124365Simp			ok = 1;	/* subtractive bridge: always ok */
1257189844Simp#if 0
1258124365Simp			if (pcib_is_nonprefetch_open(sc)) {
1259189792Simp				if (start < sc->memlimit && end > sc->membase)
1260189792Simp					start = sc->memlimit + 1;
1261124365Simp			}
1262124365Simp			if (pcib_is_prefetch_open(sc)) {
1263189792Simp				if (start < sc->pmemlimit && end > sc->pmembase)
1264189792Simp					start = sc->pmemlimit + 1;
1265124365Simp			}
1266189844Simp#endif
1267106844Smdodd		}
1268124365Simp		if (end < start) {
1269142051Simp			device_printf(dev, "memory: end (%lx) < start (%lx)\n",
1270142051Simp			    end, start);
1271124365Simp			start = 0;
1272124365Simp			end = 0;
1273124365Simp			ok = 0;
1274124365Simp		}
1275124365Simp		if (!ok && bootverbose)
1276124365Simp			device_printf(dev,
1277164130Sjhb			    "%s%srequested unsupported memory range %#lx-%#lx "
1278163805Simp			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
1279164130Sjhb			    name, suffix, start, end,
1280163805Simp			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1281163805Simp			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1282124365Simp		if (!ok)
1283124365Simp			return (NULL);
1284124365Simp		if (bootverbose)
1285164130Sjhb			device_printf(dev,"%s%srequested memory range "
1286142051Simp			    "0x%lx-0x%lx: good\n",
1287164130Sjhb			    name, suffix, start, end);
1288124365Simp		break;
128969908Smsmith
129069783Smsmith	default:
1291124365Simp		break;
129269783Smsmith	}
1293124365Simp	/*
1294124365Simp	 * Bridge is OK decoding this resource, so pass it up.
1295124365Simp	 */
1296142051Simp	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
1297142051Simp	    count, flags));
129869783Smsmith}
1299221393Sjhb#endif
130069783Smsmith
130169783Smsmith/*
130269783Smsmith * PCIB interface.
130369783Smsmith */
1304102441Sjhbint
130569783Smsmithpcib_maxslots(device_t dev)
130669783Smsmith{
130769908Smsmith    return(PCI_SLOTMAX);
130869783Smsmith}
130969783Smsmith
131069783Smsmith/*
131169783Smsmith * Since we are a child of a PCI bus, its parent must support the pcib interface.
131269783Smsmith */
1313119266Simpuint32_t
1314189792Simppcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
131569783Smsmith{
131669783Smsmith    return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
131769783Smsmith}
131869783Smsmith
1319102441Sjhbvoid
1320189792Simppcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
132169783Smsmith{
132269783Smsmith    PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
132369783Smsmith}
132469783Smsmith
132569783Smsmith/*
132669783Smsmith * Route an interrupt across a PCI bridge.
132769783Smsmith */
1328109229Sbennoint
132969783Smsmithpcib_route_interrupt(device_t pcib, device_t dev, int pin)
133069783Smsmith{
133169783Smsmith    device_t	bus;
133269783Smsmith    int		parent_intpin;
133369783Smsmith    int		intnum;
133469783Smsmith
133569783Smsmith    /*
133669783Smsmith     *
133769783Smsmith     * The PCI standard defines a swizzle of the child-side device/intpin to
133869783Smsmith     * the parent-side intpin as follows.
133969783Smsmith     *
134069783Smsmith     * device = device on child bus
134169783Smsmith     * child_intpin = intpin on child bus slot (0-3)
134269783Smsmith     * parent_intpin = intpin on parent bus slot (0-3)
134369783Smsmith     *
134469783Smsmith     * parent_intpin = (device + child_intpin) % 4
134569783Smsmith     */
1346115234Sticso    parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
134769783Smsmith
134869783Smsmith    /*
134969783Smsmith     * Our parent is a PCI bus.  Its parent must export the pcib interface
135069783Smsmith     * which includes the ability to route interrupts.
135169783Smsmith     */
135269783Smsmith    bus = device_get_parent(pcib);
135369783Smsmith    intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
1354131398Sjhb    if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1355102977Sjhb	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1356102977Sjhb	    pci_get_slot(dev), 'A' + pin - 1, intnum);
135790554Smsmith    }
135869783Smsmith    return(intnum);
135969783Smsmith}
1360107172Sjhb
1361169221Sjhb/* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
1362164264Sjhbint
1363164264Sjhbpcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
1364164264Sjhb{
1365169902Sgallatin	struct pcib_softc *sc = device_get_softc(pcib);
1366164264Sjhb	device_t bus;
1367164264Sjhb
1368165995Sjhb	if (sc->flags & PCIB_DISABLE_MSI)
1369165995Sjhb		return (ENXIO);
1370164264Sjhb	bus = device_get_parent(pcib);
1371164264Sjhb	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
1372164264Sjhb	    irqs));
1373164264Sjhb}
1374164264Sjhb
1375169221Sjhb/* Pass request to release MSI/MSI-X messages up to the parent bridge. */
1376164264Sjhbint
1377164264Sjhbpcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
1378164264Sjhb{
1379164264Sjhb	device_t bus;
1380164264Sjhb
1381164264Sjhb	bus = device_get_parent(pcib);
1382164264Sjhb	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
1383164264Sjhb}
1384164264Sjhb
1385164264Sjhb/* Pass request to alloc an MSI-X message up to the parent bridge. */
1386164264Sjhbint
1387169221Sjhbpcib_alloc_msix(device_t pcib, device_t dev, int *irq)
1388164264Sjhb{
1389169902Sgallatin	struct pcib_softc *sc = device_get_softc(pcib);
1390164264Sjhb	device_t bus;
1391164264Sjhb
1392165995Sjhb	if (sc->flags & PCIB_DISABLE_MSI)
1393165995Sjhb		return (ENXIO);
1394164264Sjhb	bus = device_get_parent(pcib);
1395169221Sjhb	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
1396164264Sjhb}
1397164264Sjhb
1398169221Sjhb/* Pass request to release an MSI-X message up to the parent bridge. */
1399166176Sjhbint
1400169221Sjhbpcib_release_msix(device_t pcib, device_t dev, int irq)
1401166176Sjhb{
1402166176Sjhb	device_t bus;
1403166176Sjhb
1404166176Sjhb	bus = device_get_parent(pcib);
1405169221Sjhb	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
1406166176Sjhb}
1407166176Sjhb
1408169221Sjhb/* Pass request to map MSI/MSI-X message up to parent bridge. */
1409164264Sjhbint
1410169221Sjhbpcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
1411169221Sjhb    uint32_t *data)
1412164264Sjhb{
1413164264Sjhb	device_t bus;
1414180753Sluoqi	int error;
1415164264Sjhb
1416164264Sjhb	bus = device_get_parent(pcib);
1417180753Sluoqi	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
1418180753Sluoqi	if (error)
1419180753Sluoqi		return (error);
1420180753Sluoqi
1421180753Sluoqi	pci_ht_map_msi(pcib, *addr);
1422180753Sluoqi	return (0);
1423164264Sjhb}
1424164264Sjhb
1425211430Sjhb/* Pass request for device power state up to parent bridge. */
1426211430Sjhbint
1427211430Sjhbpcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
1428211430Sjhb{
1429211430Sjhb	device_t bus;
1430211430Sjhb
1431211430Sjhb	bus = device_get_parent(pcib);
1432211430Sjhb	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
1433211430Sjhb}
1434211430Sjhb
1435107172Sjhb/*
1436107172Sjhb * Try to read the bus number of a host-PCI bridge using appropriate config
1437107172Sjhb * registers.
1438107172Sjhb */
1439107172Sjhbint
1440107172Sjhbhost_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
1441119266Simp    uint8_t *busnum)
1442107172Sjhb{
1443119266Simp	uint32_t id;
1444107172Sjhb
1445107172Sjhb	id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
1446107248Sjhb	if (id == 0xffffffff)
1447107172Sjhb		return (0);
1448107172Sjhb
1449107172Sjhb	switch (id) {
1450107172Sjhb	case 0x12258086:
1451107172Sjhb		/* Intel 824?? */
1452107172Sjhb		/* XXX This is a guess */
1453107172Sjhb		/* *busnum = read_config(bus, slot, func, 0x41, 1); */
1454107172Sjhb		*busnum = bus;
1455107172Sjhb		break;
1456107172Sjhb	case 0x84c48086:
1457107172Sjhb		/* Intel 82454KX/GX (Orion) */
1458107172Sjhb		*busnum = read_config(bus, slot, func, 0x4a, 1);
1459107172Sjhb		break;
1460107172Sjhb	case 0x84ca8086:
1461107172Sjhb		/*
1462107172Sjhb		 * For the 450nx chipset, there is a whole bundle of
1463107172Sjhb		 * things pretending to be host bridges. The MIOC will
1464107172Sjhb		 * be seen first and isn't really a pci bridge (the
1465107172Sjhb		 * actual busses are attached to the PXB's). We need to
1466107172Sjhb		 * read the registers of the MIOC to figure out the
1467107172Sjhb		 * bus numbers for the PXB channels.
1468107172Sjhb		 *
1469107172Sjhb		 * Since the MIOC doesn't have a pci bus attached, we
1470107172Sjhb		 * pretend it wasn't there.
1471107172Sjhb		 */
1472107172Sjhb		return (0);
1473107172Sjhb	case 0x84cb8086:
1474107172Sjhb		switch (slot) {
1475107172Sjhb		case 0x12:
1476107172Sjhb			/* Intel 82454NX PXB#0, Bus#A */
1477107248Sjhb			*busnum = read_config(bus, 0x10, func, 0xd0, 1);
1478107172Sjhb			break;
1479107172Sjhb		case 0x13:
1480107172Sjhb			/* Intel 82454NX PXB#0, Bus#B */
1481107248Sjhb			*busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
1482107172Sjhb			break;
1483107172Sjhb		case 0x14:
1484107172Sjhb			/* Intel 82454NX PXB#1, Bus#A */
1485107248Sjhb			*busnum = read_config(bus, 0x10, func, 0xd3, 1);
1486107172Sjhb			break;
1487107172Sjhb		case 0x15:
1488107172Sjhb			/* Intel 82454NX PXB#1, Bus#B */
1489107248Sjhb			*busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
1490107172Sjhb			break;
1491107172Sjhb		}
1492107172Sjhb		break;
1493107172Sjhb
1494107172Sjhb		/* ServerWorks -- vendor 0x1166 */
1495107172Sjhb	case 0x00051166:
1496107172Sjhb	case 0x00061166:
1497107172Sjhb	case 0x00081166:
1498107172Sjhb	case 0x00091166:
1499107172Sjhb	case 0x00101166:
1500107172Sjhb	case 0x00111166:
1501107172Sjhb	case 0x00171166:
1502107172Sjhb	case 0x01011166:
1503107172Sjhb	case 0x010f1014:
1504215820Sjhb	case 0x01101166:
1505107172Sjhb	case 0x02011166:
1506215820Sjhb	case 0x02251166:
1507107172Sjhb	case 0x03021014:
1508107172Sjhb		*busnum = read_config(bus, slot, func, 0x44, 1);
1509107172Sjhb		break;
1510144110Sjhb
1511144110Sjhb		/* Compaq/HP -- vendor 0x0e11 */
1512144110Sjhb	case 0x60100e11:
1513144110Sjhb		*busnum = read_config(bus, slot, func, 0xc8, 1);
1514144110Sjhb		break;
1515107172Sjhb	default:
1516107172Sjhb		/* Don't know how to read bus number. */
1517107172Sjhb		return 0;
1518107172Sjhb	}
1519107172Sjhb
1520107172Sjhb	return 1;
1521107172Sjhb}
1522