pci_subr.c revision 221393
1270096Strasz/*-
2270096Strasz * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3270096Strasz * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4270096Strasz * Copyright (c) 2000 BSDi
5270096Strasz * All rights reserved.
6270096Strasz *
7270096Strasz * Redistribution and use in source and binary forms, with or without
8270096Strasz * modification, are permitted provided that the following conditions
9270096Strasz * are met:
10270096Strasz * 1. Redistributions of source code must retain the above copyright
11270096Strasz *    notice, this list of conditions and the following disclaimer.
12270096Strasz * 2. Redistributions in binary form must reproduce the above copyright
13270096Strasz *    notice, this list of conditions and the following disclaimer in the
14270096Strasz *    documentation and/or other materials provided with the distribution.
15270096Strasz * 3. The name of the author may not be used to endorse or promote products
16270096Strasz *    derived from this software without specific prior written permission.
17270096Strasz *
18270096Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19270096Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20270096Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21270096Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22270096Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23270096Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24270096Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25270096Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26270096Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27270096Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28270096Strasz * SUCH DAMAGE.
29270096Strasz */
30270096Strasz
31270096Strasz#include <sys/cdefs.h>
32270096Strasz__FBSDID("$FreeBSD: head/sys/dev/pci/pci_pci.c 221393 2011-05-03 17:37:24Z jhb $");
33270096Strasz
34270096Strasz/*
35270096Strasz * PCI:PCI bridge support.
36270096Strasz */
37270096Strasz
38270096Strasz#include <sys/param.h>
39270096Strasz#include <sys/bus.h>
40270096Strasz#include <sys/kernel.h>
41270096Strasz#include <sys/libkern.h>
42270096Strasz#include <sys/malloc.h>
43270096Strasz#include <sys/module.h>
44270096Strasz#include <sys/rman.h>
45270096Strasz#include <sys/sysctl.h>
46270096Strasz#include <sys/systm.h>
47270096Strasz
48270096Strasz#include <machine/bus.h>
49270096Strasz#include <machine/resource.h>
50270096Strasz
51270096Strasz#include <dev/pci/pcivar.h>
52270096Strasz#include <dev/pci/pcireg.h>
53270096Strasz#include <dev/pci/pci_private.h>
54270096Strasz#include <dev/pci/pcib_private.h>
55270096Strasz
56270096Strasz#include "pcib_if.h"
57270096Strasz
58270096Straszstatic int		pcib_probe(device_t dev);
59270096Straszstatic int		pcib_suspend(device_t dev);
60270096Straszstatic int		pcib_resume(device_t dev);
61270096Straszstatic int		pcib_power_for_sleep(device_t pcib, device_t dev,
62270096Strasz			    int *pstate);
63270096Strasz
64270096Straszstatic device_method_t pcib_methods[] = {
65270096Strasz    /* Device interface */
66270096Strasz    DEVMETHOD(device_probe,		pcib_probe),
67270096Strasz    DEVMETHOD(device_attach,		pcib_attach),
68270096Strasz    DEVMETHOD(device_detach,		bus_generic_detach),
69270096Strasz    DEVMETHOD(device_shutdown,		bus_generic_shutdown),
70270096Strasz    DEVMETHOD(device_suspend,		pcib_suspend),
71270096Strasz    DEVMETHOD(device_resume,		pcib_resume),
72270096Strasz
73270096Strasz    /* Bus interface */
74270096Strasz    DEVMETHOD(bus_print_child,		bus_generic_print_child),
75270096Strasz    DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
76270096Strasz    DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
77270096Strasz    DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
78270096Strasz#ifdef NEW_PCIB
79270096Strasz    DEVMETHOD(bus_adjust_resource,	pcib_adjust_resource),
80270096Strasz    DEVMETHOD(bus_release_resource,	pcib_release_resource),
81270096Strasz#else
82270096Strasz    DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
83270096Strasz    DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
84270096Strasz#endif
85270096Strasz    DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
86270096Strasz    DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
87270096Strasz    DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
88270096Strasz    DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
89270096Strasz
90270096Strasz    /* pcib interface */
91270096Strasz    DEVMETHOD(pcib_maxslots,		pcib_maxslots),
92270096Strasz    DEVMETHOD(pcib_read_config,		pcib_read_config),
93270096Strasz    DEVMETHOD(pcib_write_config,	pcib_write_config),
94270096Strasz    DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
95270096Strasz    DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
96270096Strasz    DEVMETHOD(pcib_release_msi,		pcib_release_msi),
97270096Strasz    DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
98270096Strasz    DEVMETHOD(pcib_release_msix,	pcib_release_msix),
99270096Strasz    DEVMETHOD(pcib_map_msi,		pcib_map_msi),
100270096Strasz    DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
101270096Strasz
102270096Strasz    { 0, 0 }
103270096Strasz};
104270096Strasz
105270096Straszstatic devclass_t pcib_devclass;
106270096Strasz
107270096StraszDEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
108270096StraszDRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
109270096Strasz
110270096Strasz#ifdef NEW_PCIB
111270096Strasz/*
112270096Strasz * XXX Todo:
113270096Strasz * - properly handle the ISA enable bit.  If it is set, we should change
114270096Strasz *   the behavior of the I/O window resource and rman to not allocate the
115270096Strasz *   blocked ranges (upper 768 bytes of each 1K in the first 64k of the
116270096Strasz *   I/O port address space).
117270096Strasz */
118270096Strasz
119270096Strasz/*
120270096Strasz * Is a resource from a child device sub-allocated from one of our
121270096Strasz * resource managers?
122270096Strasz */
123270096Straszstatic int
124270096Straszpcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
125270096Strasz{
126270096Strasz
127270096Strasz	switch (type) {
128270096Strasz	case SYS_RES_IOPORT:
129270096Strasz		return (rman_is_region_manager(r, &sc->io.rman));
130270096Strasz	case SYS_RES_MEMORY:
131270096Strasz		/* Prefetchable resources may live in either memory rman. */
132270096Strasz		if (rman_get_flags(r) & RF_PREFETCHABLE &&
133270096Strasz		    rman_is_region_manager(r, &sc->pmem.rman))
134270096Strasz			return (1);
135270096Strasz		return (rman_is_region_manager(r, &sc->mem.rman));
136270096Strasz	}
137270096Strasz	return (0);
138270096Strasz}
139270096Strasz
140270096Straszstatic int
141270096Straszpcib_is_window_open(struct pcib_window *pw)
142270096Strasz{
143270096Strasz
144270096Strasz	return (pw->valid && pw->base < pw->limit);
145270096Strasz}
146270096Strasz
147270096Strasz/*
148270096Strasz * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
149270096Strasz * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
150270096Strasz * when allocating the resource windows and rely on the PCI bus driver
151270096Strasz * to do this for us.
152270096Strasz */
153270096Straszstatic void
154270096Straszpcib_activate_window(struct pcib_softc *sc, int type)
155270096Strasz{
156270096Strasz
157270096Strasz	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
158270096Strasz}
159270096Strasz
160270096Straszstatic void
161270096Straszpcib_write_windows(struct pcib_softc *sc, int mask)
162270096Strasz{
163270096Strasz	device_t dev;
164270096Strasz	uint32_t val;
165270096Strasz
166270096Strasz	dev = sc->dev;
167270096Strasz	if (sc->io.valid && mask & WIN_IO) {
168270096Strasz		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
169270096Strasz		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
170270096Strasz			pci_write_config(dev, PCIR_IOBASEH_1,
171270096Strasz			    sc->io.base >> 16, 2);
172270096Strasz			pci_write_config(dev, PCIR_IOLIMITH_1,
173270096Strasz			    sc->io.limit >> 16, 2);
174270096Strasz		}
175270096Strasz		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
176270096Strasz		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
177270096Strasz	}
178270096Strasz
179270096Strasz	if (mask & WIN_MEM) {
180270096Strasz		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
181270096Strasz		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
182270096Strasz	}
183270096Strasz
184270096Strasz	if (sc->pmem.valid && mask & WIN_PMEM) {
185270096Strasz		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
186270096Strasz		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
187270096Strasz			pci_write_config(dev, PCIR_PMBASEH_1,
188270096Strasz			    sc->pmem.base >> 32, 4);
189270096Strasz			pci_write_config(dev, PCIR_PMLIMITH_1,
190270096Strasz			    sc->pmem.limit >> 32, 4);
191270096Strasz		}
192270096Strasz		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
193270096Strasz		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
194270096Strasz	}
195270096Strasz}
196270096Strasz
197270096Straszstatic void
198270096Straszpcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
199270096Strasz    int flags, pci_addr_t max_address)
200270096Strasz{
201270096Strasz	char buf[64];
202270096Strasz	int error, rid;
203270096Strasz
204270096Strasz	if (max_address != (u_long)max_address)
205270096Strasz		max_address = ~0ul;
206270096Strasz	w->rman.rm_start = 0;
207270096Strasz	w->rman.rm_end = max_address;
208270096Strasz	w->rman.rm_type = RMAN_ARRAY;
209270096Strasz	snprintf(buf, sizeof(buf), "%s %s window",
210270096Strasz	    device_get_nameunit(sc->dev), w->name);
211270096Strasz	w->rman.rm_descr = strdup(buf, M_DEVBUF);
212270096Strasz	error = rman_init(&w->rman);
213270096Strasz	if (error)
214270096Strasz		panic("Failed to initialize %s %s rman",
215270096Strasz		    device_get_nameunit(sc->dev), w->name);
216270096Strasz
217270096Strasz	if (!pcib_is_window_open(w))
218270096Strasz		return;
219270096Strasz
220270096Strasz	if (w->base > max_address || w->limit > max_address) {
221270096Strasz		device_printf(sc->dev,
222270096Strasz		    "initial %s window has too many bits, ignoring\n", w->name);
223270096Strasz		return;
224270096Strasz	}
225270096Strasz	rid = w->reg;
226270096Strasz	w->res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
227270096Strasz	    w->limit - w->base + 1, flags);
228270096Strasz	if (w->res == NULL) {
229270096Strasz		device_printf(sc->dev,
230270096Strasz		    "failed to allocate initial %s window: %#jx-%#jx\n",
231270096Strasz		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
232270096Strasz		w->base = max_address;
233270096Strasz		w->limit = 0;
234270096Strasz		pcib_write_windows(sc, w->mask);
235270096Strasz		return;
236270096Strasz	}
237270096Strasz	pcib_activate_window(sc, type);
238270096Strasz
239270096Strasz	error = rman_manage_region(&w->rman, rman_get_start(w->res),
240270096Strasz	    rman_get_end(w->res));
241270096Strasz	if (error)
242270096Strasz		panic("Failed to initialize rman with resource");
243270096Strasz}
244270096Strasz
245270096Strasz/*
246270096Strasz * Initialize I/O windows.
247270096Strasz */
248270096Straszstatic void
249270096Straszpcib_probe_windows(struct pcib_softc *sc)
250270096Strasz{
251270096Strasz	pci_addr_t max;
252270096Strasz	device_t dev;
253270096Strasz	uint32_t val;
254270096Strasz
255270096Strasz	dev = sc->dev;
256270096Strasz
257270096Strasz	/* Determine if the I/O port window is implemented. */
258270096Strasz	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
259270096Strasz	if (val == 0) {
260270096Strasz		/*
261270096Strasz		 * If 'val' is zero, then only 16-bits of I/O space
262270096Strasz		 * are supported.
263270096Strasz		 */
264270096Strasz		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
265270096Strasz		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
266270096Strasz			sc->io.valid = 1;
267270096Strasz			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
268270096Strasz		}
269270096Strasz	} else
270270096Strasz		sc->io.valid = 1;
271
272	/* Read the existing I/O port window. */
273	if (sc->io.valid) {
274		sc->io.reg = PCIR_IOBASEL_1;
275		sc->io.step = 12;
276		sc->io.mask = WIN_IO;
277		sc->io.name = "I/O port";
278		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
279			sc->io.base = PCI_PPBIOBASE(
280			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
281			sc->io.limit = PCI_PPBIOLIMIT(
282			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
283			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
284			max = 0xffffffff;
285		} else {
286			sc->io.base = PCI_PPBIOBASE(0, val);
287			sc->io.limit = PCI_PPBIOLIMIT(0,
288			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
289			max = 0xffff;
290		}
291		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
292	}
293
294	/* Read the existing memory window. */
295	sc->mem.valid = 1;
296	sc->mem.reg = PCIR_MEMBASE_1;
297	sc->mem.step = 20;
298	sc->mem.mask = WIN_MEM;
299	sc->mem.name = "memory";
300	sc->mem.base = PCI_PPBMEMBASE(0,
301	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
302	sc->mem.limit = PCI_PPBMEMLIMIT(0,
303	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
304	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
305
306	/* Determine if the prefetchable memory window is implemented. */
307	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
308	if (val == 0) {
309		/*
310		 * If 'val' is zero, then only 32-bits of memory space
311		 * are supported.
312		 */
313		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
314		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
315			sc->pmem.valid = 1;
316			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
317		}
318	} else
319		sc->pmem.valid = 1;
320
321	/* Read the existing prefetchable memory window. */
322	if (sc->pmem.valid) {
323		sc->pmem.reg = PCIR_PMBASEL_1;
324		sc->pmem.step = 20;
325		sc->pmem.mask = WIN_PMEM;
326		sc->pmem.name = "prefetch";
327		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
328			sc->pmem.base = PCI_PPBMEMBASE(
329			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
330			sc->pmem.limit = PCI_PPBMEMLIMIT(
331			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
332			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
333			max = 0xffffffffffffffff;
334		} else {
335			sc->pmem.base = PCI_PPBMEMBASE(0, val);
336			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
337			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
338			max = 0xffffffff;
339		}
340		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
341		    RF_PREFETCHABLE, max);
342	}
343}
344
345#else
346
347/*
348 * Is the prefetch window open (eg, can we allocate memory in it?)
349 */
350static int
351pcib_is_prefetch_open(struct pcib_softc *sc)
352{
353	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
354}
355
356/*
357 * Is the nonprefetch window open (eg, can we allocate memory in it?)
358 */
359static int
360pcib_is_nonprefetch_open(struct pcib_softc *sc)
361{
362	return (sc->membase > 0 && sc->membase < sc->memlimit);
363}
364
365/*
366 * Is the io window open (eg, can we allocate ports in it?)
367 */
368static int
369pcib_is_io_open(struct pcib_softc *sc)
370{
371	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
372}
373
374/*
375 * Get current I/O decode.
376 */
377static void
378pcib_get_io_decode(struct pcib_softc *sc)
379{
380	device_t	dev;
381	uint32_t	iolow;
382
383	dev = sc->dev;
384
385	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
386	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
387		sc->iobase = PCI_PPBIOBASE(
388		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
389	else
390		sc->iobase = PCI_PPBIOBASE(0, iolow);
391
392	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
393	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
394		sc->iolimit = PCI_PPBIOLIMIT(
395		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
396	else
397		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
398}
399
400/*
401 * Get current memory decode.
402 */
403static void
404pcib_get_mem_decode(struct pcib_softc *sc)
405{
406	device_t	dev;
407	pci_addr_t	pmemlow;
408
409	dev = sc->dev;
410
411	sc->membase = PCI_PPBMEMBASE(0,
412	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
413	sc->memlimit = PCI_PPBMEMLIMIT(0,
414	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
415
416	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
417	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
418		sc->pmembase = PCI_PPBMEMBASE(
419		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
420	else
421		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
422
423	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
424	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
425		sc->pmemlimit = PCI_PPBMEMLIMIT(
426		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
427	else
428		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
429}
430
431/*
432 * Restore previous I/O decode.
433 */
434static void
435pcib_set_io_decode(struct pcib_softc *sc)
436{
437	device_t	dev;
438	uint32_t	iohi;
439
440	dev = sc->dev;
441
442	iohi = sc->iobase >> 16;
443	if (iohi > 0)
444		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
445	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
446
447	iohi = sc->iolimit >> 16;
448	if (iohi > 0)
449		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
450	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
451}
452
453/*
454 * Restore previous memory decode.
455 */
456static void
457pcib_set_mem_decode(struct pcib_softc *sc)
458{
459	device_t	dev;
460	pci_addr_t	pmemhi;
461
462	dev = sc->dev;
463
464	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
465	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
466
467	pmemhi = sc->pmembase >> 32;
468	if (pmemhi > 0)
469		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
470	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
471
472	pmemhi = sc->pmemlimit >> 32;
473	if (pmemhi > 0)
474		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
475	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
476}
477#endif
478
479/*
480 * Get current bridge configuration.
481 */
482static void
483pcib_cfg_save(struct pcib_softc *sc)
484{
485	device_t	dev;
486
487	dev = sc->dev;
488
489	sc->command = pci_read_config(dev, PCIR_COMMAND, 2);
490	sc->pribus = pci_read_config(dev, PCIR_PRIBUS_1, 1);
491	sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1);
492	sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
493	sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
494	sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1);
495#ifndef NEW_PCIB
496	if (sc->command & PCIM_CMD_PORTEN)
497		pcib_get_io_decode(sc);
498	if (sc->command & PCIM_CMD_MEMEN)
499		pcib_get_mem_decode(sc);
500#endif
501}
502
503/*
504 * Restore previous bridge configuration.
505 */
506static void
507pcib_cfg_restore(struct pcib_softc *sc)
508{
509	device_t	dev;
510
511	dev = sc->dev;
512
513	pci_write_config(dev, PCIR_COMMAND, sc->command, 2);
514	pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
515	pci_write_config(dev, PCIR_SECBUS_1, sc->secbus, 1);
516	pci_write_config(dev, PCIR_SUBBUS_1, sc->subbus, 1);
517	pci_write_config(dev, PCIR_BRIDGECTL_1, sc->bridgectl, 2);
518	pci_write_config(dev, PCIR_SECLAT_1, sc->seclat, 1);
519#ifdef NEW_PCIB
520	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
521#else
522	if (sc->command & PCIM_CMD_PORTEN)
523		pcib_set_io_decode(sc);
524	if (sc->command & PCIM_CMD_MEMEN)
525		pcib_set_mem_decode(sc);
526#endif
527}
528
529/*
530 * Generic device interface
531 */
532static int
533pcib_probe(device_t dev)
534{
535    if ((pci_get_class(dev) == PCIC_BRIDGE) &&
536	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
537	device_set_desc(dev, "PCI-PCI bridge");
538	return(-10000);
539    }
540    return(ENXIO);
541}
542
543void
544pcib_attach_common(device_t dev)
545{
546    struct pcib_softc	*sc;
547    struct sysctl_ctx_list *sctx;
548    struct sysctl_oid	*soid;
549
550    sc = device_get_softc(dev);
551    sc->dev = dev;
552
553    /*
554     * Get current bridge configuration.
555     */
556    sc->domain = pci_get_domain(dev);
557    sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2);
558    pcib_cfg_save(sc);
559
560    /*
561     * Setup sysctl reporting nodes
562     */
563    sctx = device_get_sysctl_ctx(dev);
564    soid = device_get_sysctl_tree(dev);
565    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
566      CTLFLAG_RD, &sc->domain, 0, "Domain number");
567    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
568      CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
569    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
570      CTLFLAG_RD, &sc->secbus, 0, "Secondary bus number");
571    SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
572      CTLFLAG_RD, &sc->subbus, 0, "Subordinate bus number");
573
574    /*
575     * Quirk handling.
576     */
577    switch (pci_get_devid(dev)) {
578    case 0x12258086:		/* Intel 82454KX/GX (Orion) */
579	{
580	    uint8_t	supbus;
581
582	    supbus = pci_read_config(dev, 0x41, 1);
583	    if (supbus != 0xff) {
584		sc->secbus = supbus + 1;
585		sc->subbus = supbus + 1;
586	    }
587	    break;
588	}
589
590    /*
591     * The i82380FB mobile docking controller is a PCI-PCI bridge,
592     * and it is a subtractive bridge.  However, the ProgIf is wrong
593     * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
594     * happen.  There's also a Toshiba bridge that behaves this
595     * way.
596     */
597    case 0x124b8086:		/* Intel 82380FB Mobile */
598    case 0x060513d7:		/* Toshiba ???? */
599	sc->flags |= PCIB_SUBTRACTIVE;
600	break;
601
602    /* Compaq R3000 BIOS sets wrong subordinate bus number. */
603    case 0x00dd10de:
604	{
605	    char *cp;
606
607	    if ((cp = getenv("smbios.planar.maker")) == NULL)
608		break;
609	    if (strncmp(cp, "Compal", 6) != 0) {
610		freeenv(cp);
611		break;
612	    }
613	    freeenv(cp);
614	    if ((cp = getenv("smbios.planar.product")) == NULL)
615		break;
616	    if (strncmp(cp, "08A0", 4) != 0) {
617		freeenv(cp);
618		break;
619	    }
620	    freeenv(cp);
621	    if (sc->subbus < 0xa) {
622		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
623		sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
624	    }
625	    break;
626	}
627    }
628
629    if (pci_msi_device_blacklisted(dev))
630	sc->flags |= PCIB_DISABLE_MSI;
631
632    /*
633     * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
634     * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
635     * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
636     * This means they act as if they were subtractively decoding
637     * bridges and pass all transactions.  Mark them and real ProgIf 1
638     * parts as subtractive.
639     */
640    if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
641      pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
642	sc->flags |= PCIB_SUBTRACTIVE;
643
644#ifdef NEW_PCIB
645    pcib_probe_windows(sc);
646#endif
647    if (bootverbose) {
648	device_printf(dev, "  domain            %d\n", sc->domain);
649	device_printf(dev, "  secondary bus     %d\n", sc->secbus);
650	device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
651#ifdef NEW_PCIB
652	if (pcib_is_window_open(&sc->io))
653	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
654	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
655	if (pcib_is_window_open(&sc->mem))
656	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
657	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
658	if (pcib_is_window_open(&sc->pmem))
659	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
660	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
661#else
662	if (pcib_is_io_open(sc))
663	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
664	      sc->iobase, sc->iolimit);
665	if (pcib_is_nonprefetch_open(sc))
666	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
667	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
668	if (pcib_is_prefetch_open(sc))
669	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
670	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
671#endif
672	else
673	    device_printf(dev, "  no prefetched decode\n");
674	if (sc->flags & PCIB_SUBTRACTIVE)
675	    device_printf(dev, "  Subtractively decoded bridge.\n");
676    }
677
678    /*
679     * XXX If the secondary bus number is zero, we should assign a bus number
680     *     since the BIOS hasn't, then initialise the bridge.  A simple
681     *     bus_alloc_resource with the a couple of busses seems like the right
682     *     approach, but we don't know what busses the BIOS might have already
683     *     assigned to other bridges on this bus that probe later than we do.
684     *
685     *     If the subordinate bus number is less than the secondary bus number,
686     *     we should pick a better value.  One sensible alternative would be to
687     *     pick 255; the only tradeoff here is that configuration transactions
688     *     would be more widely routed than absolutely necessary.  We could
689     *     then do a walk of the tree later and fix it.
690     */
691}
692
693int
694pcib_attach(device_t dev)
695{
696    struct pcib_softc	*sc;
697    device_t		child;
698
699    pcib_attach_common(dev);
700    sc = device_get_softc(dev);
701    if (sc->secbus != 0) {
702	child = device_add_child(dev, "pci", sc->secbus);
703	if (child != NULL)
704	    return(bus_generic_attach(dev));
705    }
706
707    /* no secondary bus; we should have fixed this */
708    return(0);
709}
710
711int
712pcib_suspend(device_t dev)
713{
714	device_t	pcib;
715	int		dstate, error;
716
717	pcib_cfg_save(device_get_softc(dev));
718	error = bus_generic_suspend(dev);
719	if (error == 0 && pci_do_power_suspend) {
720		dstate = PCI_POWERSTATE_D3;
721		pcib = device_get_parent(device_get_parent(dev));
722		if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0)
723			pci_set_powerstate(dev, dstate);
724	}
725	return (error);
726}
727
728int
729pcib_resume(device_t dev)
730{
731	device_t	pcib;
732
733	if (pci_do_power_resume) {
734		pcib = device_get_parent(device_get_parent(dev));
735		if (PCIB_POWER_FOR_SLEEP(pcib, dev, NULL) == 0)
736			pci_set_powerstate(dev, PCI_POWERSTATE_D0);
737	}
738	pcib_cfg_restore(device_get_softc(dev));
739	return (bus_generic_resume(dev));
740}
741
742int
743pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
744{
745    struct pcib_softc	*sc = device_get_softc(dev);
746
747    switch (which) {
748    case PCIB_IVAR_DOMAIN:
749	*result = sc->domain;
750	return(0);
751    case PCIB_IVAR_BUS:
752	*result = sc->secbus;
753	return(0);
754    }
755    return(ENOENT);
756}
757
758int
759pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
760{
761    struct pcib_softc	*sc = device_get_softc(dev);
762
763    switch (which) {
764    case PCIB_IVAR_DOMAIN:
765	return(EINVAL);
766    case PCIB_IVAR_BUS:
767	sc->secbus = value;
768	return(0);
769    }
770    return(ENOENT);
771}
772
773#ifdef NEW_PCIB
774static const char *
775pcib_child_name(device_t child)
776{
777	static char buf[64];
778
779	if (device_get_nameunit(child) != NULL)
780		return (device_get_nameunit(child));
781	snprintf(buf, sizeof(buf), "pci%d:%d:%d:%d", pci_get_domain(child),
782	    pci_get_bus(child), pci_get_slot(child), pci_get_function(child));
783	return (buf);
784}
785
786/*
787 * Attempt to allocate a resource from the existing resources assigned
788 * to a window.
789 */
790static struct resource *
791pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
792    device_t child, int type, int *rid, u_long start, u_long end, u_long count,
793    u_int flags)
794{
795	struct resource *res;
796
797	if (!pcib_is_window_open(w))
798		return (NULL);
799
800	res = rman_reserve_resource(&w->rman, start, end, count,
801	    flags & ~RF_ACTIVE, child);
802	if (res == NULL)
803		return (NULL);
804
805	if (bootverbose)
806		device_printf(sc->dev,
807		    "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
808		    w->name, rman_get_start(res), rman_get_end(res), *rid,
809		    pcib_child_name(child));
810	rman_set_rid(res, *rid);
811
812	/*
813	 * If the resource should be active, pass that request up the
814	 * tree.  This assumes the parent drivers can handle
815	 * activating sub-allocated resources.
816	 */
817	if (flags & RF_ACTIVE) {
818		if (bus_activate_resource(child, type, *rid, res) != 0) {
819			rman_release_resource(res);
820			return (NULL);
821		}
822	}
823
824	return (res);
825}
826
827/*
828 * Attempt to grow a window to make room for a given resource request.
829 * The 'step' parameter is log_2 of the desired I/O window's alignment.
830 */
831static int
832pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
833    u_long start, u_long end, u_long count, u_int flags)
834{
835	u_long align, start_free, end_free, front, back;
836	int error, rid;
837
838	/*
839	 * Clamp the desired resource range to the maximum address
840	 * this window supports.  Reject impossible requests.
841	 */
842	if (!w->valid)
843		return (EINVAL);
844	if (end > w->rman.rm_end)
845		end = w->rman.rm_end;
846	if (start + count - 1 > end || start + count < start)
847		return (EINVAL);
848
849	/*
850	 * If there is no resource at all, just try to allocate enough
851	 * aligned space for this resource.
852	 */
853	if (w->res == NULL) {
854		if (RF_ALIGNMENT(flags) < w->step) {
855			flags &= ~RF_ALIGNMENT_MASK;
856			flags |= RF_ALIGNMENT_LOG2(w->step);
857		}
858		start &= ~((1ul << w->step) - 1);
859		end |= ((1ul << w->step) - 1);
860		count = roundup2(count, 1ul << w->step);
861		rid = w->reg;
862		w->res = bus_alloc_resource(sc->dev, type, &rid, start, end,
863		    count, flags & ~RF_ACTIVE);
864		if (w->res == NULL) {
865			if (bootverbose)
866				device_printf(sc->dev,
867		    "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
868				    w->name, start, end, count);
869			return (ENXIO);
870		}
871		if (bootverbose)
872			device_printf(sc->dev,
873			    "allocated initial %s window of %#lx-%#lx\n",
874			    w->name, rman_get_start(w->res),
875			    rman_get_end(w->res));
876		error = rman_manage_region(&w->rman, rman_get_start(w->res),
877		    rman_get_end(w->res));
878		if (error) {
879			if (bootverbose)
880				device_printf(sc->dev,
881				    "failed to add initial %s window to rman\n",
882				    w->name);
883			bus_release_resource(sc->dev, type, w->reg, w->res);
884			w->res = NULL;
885			return (error);
886		}
887		pcib_activate_window(sc, type);
888		goto updatewin;
889	}
890
891	/*
892	 * See if growing the window would help.  Compute the minimum
893	 * amount of address space needed on both the front and back
894	 * ends of the existing window to satisfy the allocation.
895	 *
896	 * For each end, build a candidate region adjusting for the
897	 * required alignment, etc.  If there is a free region at the
898	 * edge of the window, grow from the inner edge of the free
899	 * region.  Otherwise grow from the window boundary.
900	 *
901	 * XXX: Special case: if w->res is completely empty and the
902	 * request size is larger than w->res, we should find the
903	 * optimal aligned buffer containing w->res and allocate that.
904	 */
905	if (bootverbose)
906		device_printf(sc->dev,
907		    "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
908		    w->name, start, end, count);
909	align = 1ul << RF_ALIGNMENT(flags);
910	if (start < rman_get_start(w->res)) {
911		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
912		    0 || start_free != rman_get_start(w->res))
913			end_free = rman_get_start(w->res) - 1;
914		if (end_free > end)
915			end_free = end;
916
917		/* Move end_free down until it is properly aligned. */
918		end_free &= ~(align - 1);
919		front = end_free - count;
920
921		/*
922		 * The resource would now be allocated at (front,
923		 * end_free).  Ensure that fits in the (start, end)
924		 * bounds.  end_free is checked above.  If 'front' is
925		 * ok, ensure it is properly aligned for this window.
926		 * Also check for underflow.
927		 */
928		if (front >= start && front <= end_free) {
929			if (bootverbose)
930				printf("\tfront candidate range: %#lx-%#lx\n",
931				    front, end_free);
932			front &= (1ul << w->step) - 1;
933			front = rman_get_start(w->res) - front;
934		} else
935			front = 0;
936	} else
937		front = 0;
938	if (end > rman_get_end(w->res)) {
939		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
940		    0 || end_free != rman_get_end(w->res))
941			start_free = rman_get_end(w->res) + 1;
942		if (start_free < start)
943			start_free = start;
944
945		/* Move start_free up until it is properly aligned. */
946		start_free = roundup2(start_free, align);
947		back = start_free + count;
948
949		/*
950		 * The resource would now be allocated at (start_free,
951		 * back).  Ensure that fits in the (start, end)
952		 * bounds.  start_free is checked above.  If 'back' is
953		 * ok, ensure it is properly aligned for this window.
954		 * Also check for overflow.
955		 */
956		if (back <= end && start_free <= back) {
957			if (bootverbose)
958				printf("\tback candidate range: %#lx-%#lx\n",
959				    start_free, back);
960			back = roundup2(back, w->step) - 1;
961			back -= rman_get_end(w->res);
962		} else
963			back = 0;
964	} else
965		back = 0;
966
967	/*
968	 * Try to allocate the smallest needed region first.
969	 * If that fails, fall back to the other region.
970	 */
971	error = ENOSPC;
972	while (front != 0 || back != 0) {
973		if (front != 0 && (front <= back || back == 0)) {
974			error = bus_adjust_resource(sc->dev, type, w->res,
975			    rman_get_start(w->res) - front,
976			    rman_get_end(w->res));
977			if (error == 0)
978				break;
979			front = 0;
980		} else {
981			error = bus_adjust_resource(sc->dev, type, w->res,
982			    rman_get_start(w->res),
983			    rman_get_end(w->res) + back);
984			if (error == 0)
985				break;
986			back = 0;
987		}
988	}
989
990	if (error)
991		return (error);
992	if (bootverbose)
993		device_printf(sc->dev, "grew %s window to %#lx-%#lx\n",
994		    w->name, rman_get_start(w->res), rman_get_end(w->res));
995
996	/* Add the newly allocated region to the resource manager. */
997	if (w->base != rman_get_start(w->res)) {
998		KASSERT(w->limit == rman_get_end(w->res), ("both ends moved"));
999		error = rman_manage_region(&w->rman, rman_get_start(w->res),
1000		    w->base - 1);
1001	} else {
1002		KASSERT(w->limit != rman_get_end(w->res),
1003		    ("neither end moved"));
1004		error = rman_manage_region(&w->rman, w->limit + 1,
1005		    rman_get_end(w->res));
1006	}
1007	if (error) {
1008		if (bootverbose)
1009			device_printf(sc->dev,
1010			    "failed to expand %s resource manager\n", w->name);
1011		bus_adjust_resource(sc->dev, type, w->res, w->base, w->limit);
1012		return (error);
1013	}
1014
1015updatewin:
1016	/* Save the new window. */
1017	w->base = rman_get_start(w->res);
1018	w->limit = rman_get_end(w->res);
1019	KASSERT((w->base & ((1ul << w->step) - 1)) == 0,
1020	    ("start address is not aligned"));
1021	KASSERT((w->limit & ((1ul << w->step) - 1)) == (1ul << w->step) - 1,
1022	    ("end address is not aligned"));
1023	pcib_write_windows(sc, w->mask);
1024	return (0);
1025}
1026
1027/*
1028 * We have to trap resource allocation requests and ensure that the bridge
1029 * is set up to, or capable of handling them.
1030 */
1031struct resource *
1032pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1033    u_long start, u_long end, u_long count, u_int flags)
1034{
1035	struct pcib_softc *sc;
1036	struct resource *r;
1037
1038	sc = device_get_softc(dev);
1039
1040	/*
1041	 * VGA resources are decoded iff the VGA enable bit is set in
1042	 * the bridge control register.  VGA resources do not fall into
1043	 * the resource windows and are passed up to the parent.
1044	 */
1045	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
1046	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
1047		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
1048			return (bus_generic_alloc_resource(dev, child, type,
1049			    rid, start, end, count, flags));
1050		else
1051			return (NULL);
1052	}
1053
1054	switch (type) {
1055	case SYS_RES_IOPORT:
1056		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
1057		    end, count, flags);
1058		if (r != NULL)
1059			break;
1060		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
1061		    flags) == 0)
1062			r = pcib_suballoc_resource(sc, &sc->io, child, type,
1063			    rid, start, end, count, flags);
1064		break;
1065	case SYS_RES_MEMORY:
1066		/*
1067		 * For prefetchable resources, prefer the prefetchable
1068		 * memory window, but fall back to the regular memory
1069		 * window if that fails.  Try both windows before
1070		 * attempting to grow a window in case the firmware
1071		 * has used a range in the regular memory window to
1072		 * map a prefetchable BAR.
1073		 */
1074		if (flags & RF_PREFETCHABLE) {
1075			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
1076			    rid, start, end, count, flags);
1077			if (r != NULL)
1078				break;
1079		}
1080		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
1081		    start, end, count, flags);
1082		if (r != NULL)
1083			break;
1084		if (flags & RF_PREFETCHABLE) {
1085			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
1086			    count, flags) == 0) {
1087				r = pcib_suballoc_resource(sc, &sc->pmem, child,
1088				    type, rid, start, end, count, flags);
1089				if (r != NULL)
1090					break;
1091			}
1092		}
1093		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
1094		    flags & ~RF_PREFETCHABLE) == 0)
1095			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
1096			    rid, start, end, count, flags);
1097		break;
1098	default:
1099		return (bus_generic_alloc_resource(dev, child, type, rid,
1100		    start, end, count, flags));
1101	}
1102
1103	/*
1104	 * If attempts to suballocate from the window fail but this is a
1105	 * subtractive bridge, pass the request up the tree.
1106	 */
1107	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
1108		return (bus_generic_alloc_resource(dev, child, type, rid,
1109		    start, end, count, flags));
1110	return (r);
1111}
1112
1113int
1114pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
1115    u_long start, u_long end)
1116{
1117	struct pcib_softc *sc;
1118
1119	sc = device_get_softc(bus);
1120	if (pcib_is_resource_managed(sc, type, r))
1121		return (rman_adjust_resource(r, start, end));
1122	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
1123}
1124
1125int
1126pcib_release_resource(device_t dev, device_t child, int type, int rid,
1127    struct resource *r)
1128{
1129	struct pcib_softc *sc;
1130	int error;
1131
1132	sc = device_get_softc(dev);
1133	if (pcib_is_resource_managed(sc, type, r)) {
1134		if (rman_get_flags(r) & RF_ACTIVE) {
1135			error = bus_deactivate_resource(child, type, rid, r);
1136			if (error)
1137				return (error);
1138		}
1139		return (rman_release_resource(r));
1140	}
1141	return (bus_generic_release_resource(dev, child, type, rid, r));
1142}
1143#else
1144/*
1145 * We have to trap resource allocation requests and ensure that the bridge
1146 * is set up to, or capable of handling them.
1147 */
1148struct resource *
1149pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1150    u_long start, u_long end, u_long count, u_int flags)
1151{
1152	struct pcib_softc	*sc = device_get_softc(dev);
1153	const char *name, *suffix;
1154	int ok;
1155
1156	/*
1157	 * Fail the allocation for this range if it's not supported.
1158	 */
1159	name = device_get_nameunit(child);
1160	if (name == NULL) {
1161		name = "";
1162		suffix = "";
1163	} else
1164		suffix = " ";
1165	switch (type) {
1166	case SYS_RES_IOPORT:
1167		ok = 0;
1168		if (!pcib_is_io_open(sc))
1169			break;
1170		ok = (start >= sc->iobase && end <= sc->iolimit);
1171
1172		/*
1173		 * Make sure we allow access to VGA I/O addresses when the
1174		 * bridge has the "VGA Enable" bit set.
1175		 */
1176		if (!ok && pci_is_vga_ioport_range(start, end))
1177			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1178
1179		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1180			if (!ok) {
1181				if (start < sc->iobase)
1182					start = sc->iobase;
1183				if (end > sc->iolimit)
1184					end = sc->iolimit;
1185				if (start < end)
1186					ok = 1;
1187			}
1188		} else {
1189			ok = 1;
1190#if 0
1191			/*
1192			 * If we overlap with the subtractive range, then
1193			 * pick the upper range to use.
1194			 */
1195			if (start < sc->iolimit && end > sc->iobase)
1196				start = sc->iolimit + 1;
1197#endif
1198		}
1199		if (end < start) {
1200			device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
1201			    end, start);
1202			start = 0;
1203			end = 0;
1204			ok = 0;
1205		}
1206		if (!ok) {
1207			device_printf(dev, "%s%srequested unsupported I/O "
1208			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
1209			    name, suffix, start, end, sc->iobase, sc->iolimit);
1210			return (NULL);
1211		}
1212		if (bootverbose)
1213			device_printf(dev,
1214			    "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
1215			    name, suffix, start, end);
1216		break;
1217
1218	case SYS_RES_MEMORY:
1219		ok = 0;
1220		if (pcib_is_nonprefetch_open(sc))
1221			ok = ok || (start >= sc->membase && end <= sc->memlimit);
1222		if (pcib_is_prefetch_open(sc))
1223			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1224
1225		/*
1226		 * Make sure we allow access to VGA memory addresses when the
1227		 * bridge has the "VGA Enable" bit set.
1228		 */
1229		if (!ok && pci_is_vga_memory_range(start, end))
1230			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1231
1232		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1233			if (!ok) {
1234				ok = 1;
1235				if (flags & RF_PREFETCHABLE) {
1236					if (pcib_is_prefetch_open(sc)) {
1237						if (start < sc->pmembase)
1238							start = sc->pmembase;
1239						if (end > sc->pmemlimit)
1240							end = sc->pmemlimit;
1241					} else {
1242						ok = 0;
1243					}
1244				} else {	/* non-prefetchable */
1245					if (pcib_is_nonprefetch_open(sc)) {
1246						if (start < sc->membase)
1247							start = sc->membase;
1248						if (end > sc->memlimit)
1249							end = sc->memlimit;
1250					} else {
1251						ok = 0;
1252					}
1253				}
1254			}
1255		} else if (!ok) {
1256			ok = 1;	/* subtractive bridge: always ok */
1257#if 0
1258			if (pcib_is_nonprefetch_open(sc)) {
1259				if (start < sc->memlimit && end > sc->membase)
1260					start = sc->memlimit + 1;
1261			}
1262			if (pcib_is_prefetch_open(sc)) {
1263				if (start < sc->pmemlimit && end > sc->pmembase)
1264					start = sc->pmemlimit + 1;
1265			}
1266#endif
1267		}
1268		if (end < start) {
1269			device_printf(dev, "memory: end (%lx) < start (%lx)\n",
1270			    end, start);
1271			start = 0;
1272			end = 0;
1273			ok = 0;
1274		}
1275		if (!ok && bootverbose)
1276			device_printf(dev,
1277			    "%s%srequested unsupported memory range %#lx-%#lx "
1278			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
1279			    name, suffix, start, end,
1280			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1281			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1282		if (!ok)
1283			return (NULL);
1284		if (bootverbose)
1285			device_printf(dev,"%s%srequested memory range "
1286			    "0x%lx-0x%lx: good\n",
1287			    name, suffix, start, end);
1288		break;
1289
1290	default:
1291		break;
1292	}
1293	/*
1294	 * Bridge is OK decoding this resource, so pass it up.
1295	 */
1296	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
1297	    count, flags));
1298}
1299#endif
1300
1301/*
1302 * PCIB interface.
1303 */
1304int
1305pcib_maxslots(device_t dev)
1306{
1307    return(PCI_SLOTMAX);
1308}
1309
1310/*
1311 * Since we are a child of a PCI bus, its parent must support the pcib interface.
1312 */
1313uint32_t
1314pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
1315{
1316    return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
1317}
1318
1319void
1320pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
1321{
1322    PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
1323}
1324
1325/*
1326 * Route an interrupt across a PCI bridge.
1327 */
1328int
1329pcib_route_interrupt(device_t pcib, device_t dev, int pin)
1330{
1331    device_t	bus;
1332    int		parent_intpin;
1333    int		intnum;
1334
1335    /*
1336     *
1337     * The PCI standard defines a swizzle of the child-side device/intpin to
1338     * the parent-side intpin as follows.
1339     *
1340     * device = device on child bus
1341     * child_intpin = intpin on child bus slot (0-3)
1342     * parent_intpin = intpin on parent bus slot (0-3)
1343     *
1344     * parent_intpin = (device + child_intpin) % 4
1345     */
1346    parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
1347
1348    /*
1349     * Our parent is a PCI bus.  Its parent must export the pcib interface
1350     * which includes the ability to route interrupts.
1351     */
1352    bus = device_get_parent(pcib);
1353    intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
1354    if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1355	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1356	    pci_get_slot(dev), 'A' + pin - 1, intnum);
1357    }
1358    return(intnum);
1359}
1360
1361/* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
1362int
1363pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
1364{
1365	struct pcib_softc *sc = device_get_softc(pcib);
1366	device_t bus;
1367
1368	if (sc->flags & PCIB_DISABLE_MSI)
1369		return (ENXIO);
1370	bus = device_get_parent(pcib);
1371	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
1372	    irqs));
1373}
1374
1375/* Pass request to release MSI/MSI-X messages up to the parent bridge. */
1376int
1377pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
1378{
1379	device_t bus;
1380
1381	bus = device_get_parent(pcib);
1382	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
1383}
1384
1385/* Pass request to alloc an MSI-X message up to the parent bridge. */
1386int
1387pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
1388{
1389	struct pcib_softc *sc = device_get_softc(pcib);
1390	device_t bus;
1391
1392	if (sc->flags & PCIB_DISABLE_MSI)
1393		return (ENXIO);
1394	bus = device_get_parent(pcib);
1395	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
1396}
1397
1398/* Pass request to release an MSI-X message up to the parent bridge. */
1399int
1400pcib_release_msix(device_t pcib, device_t dev, int irq)
1401{
1402	device_t bus;
1403
1404	bus = device_get_parent(pcib);
1405	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
1406}
1407
1408/* Pass request to map MSI/MSI-X message up to parent bridge. */
1409int
1410pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
1411    uint32_t *data)
1412{
1413	device_t bus;
1414	int error;
1415
1416	bus = device_get_parent(pcib);
1417	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
1418	if (error)
1419		return (error);
1420
1421	pci_ht_map_msi(pcib, *addr);
1422	return (0);
1423}
1424
1425/* Pass request for device power state up to parent bridge. */
1426int
1427pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
1428{
1429	device_t bus;
1430
1431	bus = device_get_parent(pcib);
1432	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
1433}
1434
1435/*
1436 * Try to read the bus number of a host-PCI bridge using appropriate config
1437 * registers.
1438 */
1439int
1440host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
1441    uint8_t *busnum)
1442{
1443	uint32_t id;
1444
1445	id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
1446	if (id == 0xffffffff)
1447		return (0);
1448
1449	switch (id) {
1450	case 0x12258086:
1451		/* Intel 824?? */
1452		/* XXX This is a guess */
1453		/* *busnum = read_config(bus, slot, func, 0x41, 1); */
1454		*busnum = bus;
1455		break;
1456	case 0x84c48086:
1457		/* Intel 82454KX/GX (Orion) */
1458		*busnum = read_config(bus, slot, func, 0x4a, 1);
1459		break;
1460	case 0x84ca8086:
1461		/*
1462		 * For the 450nx chipset, there is a whole bundle of
1463		 * things pretending to be host bridges. The MIOC will
1464		 * be seen first and isn't really a pci bridge (the
1465		 * actual busses are attached to the PXB's). We need to
1466		 * read the registers of the MIOC to figure out the
1467		 * bus numbers for the PXB channels.
1468		 *
1469		 * Since the MIOC doesn't have a pci bus attached, we
1470		 * pretend it wasn't there.
1471		 */
1472		return (0);
1473	case 0x84cb8086:
1474		switch (slot) {
1475		case 0x12:
1476			/* Intel 82454NX PXB#0, Bus#A */
1477			*busnum = read_config(bus, 0x10, func, 0xd0, 1);
1478			break;
1479		case 0x13:
1480			/* Intel 82454NX PXB#0, Bus#B */
1481			*busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
1482			break;
1483		case 0x14:
1484			/* Intel 82454NX PXB#1, Bus#A */
1485			*busnum = read_config(bus, 0x10, func, 0xd3, 1);
1486			break;
1487		case 0x15:
1488			/* Intel 82454NX PXB#1, Bus#B */
1489			*busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
1490			break;
1491		}
1492		break;
1493
1494		/* ServerWorks -- vendor 0x1166 */
1495	case 0x00051166:
1496	case 0x00061166:
1497	case 0x00081166:
1498	case 0x00091166:
1499	case 0x00101166:
1500	case 0x00111166:
1501	case 0x00171166:
1502	case 0x01011166:
1503	case 0x010f1014:
1504	case 0x01101166:
1505	case 0x02011166:
1506	case 0x02251166:
1507	case 0x03021014:
1508		*busnum = read_config(bus, slot, func, 0x44, 1);
1509		break;
1510
1511		/* Compaq/HP -- vendor 0x0e11 */
1512	case 0x60100e11:
1513		*busnum = read_config(bus, slot, func, 0xc8, 1);
1514		break;
1515	default:
1516		/* Don't know how to read bus number. */
1517		return 0;
1518	}
1519
1520	return 1;
1521}
1522