Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.14
      1 /*	$NetBSD: pci_machdep.c,v 1.14 2006/02/07 20:38:43 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     42  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Charles M. Hannum.
     55  * 4. The name of the author may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     68  */
     69 
     70 /*
     71  * Machine-specific functions for PCI autoconfiguration.
     72  *
     73  * On PCs, there are two methods of generating PCI configuration cycles.
     74  * We try to detect the appropriate mechanism for this machine and set
     75  * up a few function pointers to access the correct method directly.
     76  *
     77  * The configuration method can be hard-coded in the config file by
     78  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
     79  * as defined section 3.6.4.1, `Generating Configuration Cycles'.
     80  */
     81 
     82 #include <sys/cdefs.h>
     83 __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.14 2006/02/07 20:38:43 bouyer Exp $");
     84 
     85 #include <sys/types.h>
     86 #include <sys/param.h>
     87 #include <sys/time.h>
     88 #include <sys/systm.h>
     89 #include <sys/errno.h>
     90 #include <sys/device.h>
     91 #include <sys/lock.h>
     92 
     93 #include <uvm/uvm_extern.h>
     94 
     95 #include <machine/bus.h>
     96 #include <machine/bus_private.h>
     97 
     98 #include <machine/pio.h>
     99 
    100 #include <dev/isa/isareg.h>
    101 #include <dev/isa/isavar.h>
    102 #include <dev/pci/pcivar.h>
    103 #include <dev/pci/pcireg.h>
    104 #include <dev/pci/pcidevs.h>
    105 
    106 #include "opt_mpbios.h"
    107 #include "opt_mpacpi.h"
    108 
    109 #ifdef MPBIOS
    110 #include <machine/mpbiosvar.h>
    111 #endif
    112 
    113 #ifdef MPACPI
    114 #include <machine/mpacpi.h>
    115 #endif
    116 
    117 #include "opt_pci_conf_mode.h"
    118 
    119 int pci_mode = -1;
    120 
    121 static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
    122 struct pci_bridge_hook_arg {
    123 	void (*func)(pci_chipset_tag_t, pcitag_t, void *);
    124 	void *arg;
    125 };
    126 
    127 
    128 struct simplelock pci_conf_slock = SIMPLELOCK_INITIALIZER;
    129 
    130 #define	PCI_CONF_LOCK(s)						\
    131 do {									\
    132 	(s) = splhigh();						\
    133 	simple_lock(&pci_conf_slock);					\
    134 } while (0)
    135 
    136 #define	PCI_CONF_UNLOCK(s)						\
    137 do {									\
    138 	simple_unlock(&pci_conf_slock);					\
    139 	splx((s));							\
    140 } while (0)
    141 
    142 #define	PCI_MODE1_ENABLE	0x80000000UL
    143 #define	PCI_MODE1_ADDRESS_REG	0x0cf8
    144 #define	PCI_MODE1_DATA_REG	0x0cfc
    145 
    146 #define	PCI_MODE2_ENABLE_REG	0x0cf8
    147 #define	PCI_MODE2_FORWARD_REG	0x0cfa
    148 
    149 #define _m1tag(b, d, f) \
    150 	(PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8))
    151 #define _qe(bus, dev, fcn, vend, prod) \
    152 	{_m1tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)}
    153 struct {
    154 	u_int32_t tag;
    155 	pcireg_t id;
    156 } pcim1_quirk_tbl[] = {
    157 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX1),
    158 	/* XXX Triflex2 not tested */
    159 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX2),
    160 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX4),
    161 	/* Triton needed for Connectix Virtual PC */
    162 	_qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX),
    163 	/* Connectix Virtual PC 5 has a 440BX */
    164 	_qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP),
    165 	/* SIS 741 */
    166 	_qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_741),
    167 	{0, 0xffffffff} /* patchable */
    168 };
    169 #undef _m1tag
    170 #undef _id
    171 #undef _qe
    172 
    173 /*
    174  * PCI doesn't have any special needs; just use the generic versions
    175  * of these functions.
    176  */
    177 struct x86_bus_dma_tag pci_bus_dma_tag = {
    178 #if defined(_LP64) || defined(PAE)
    179 	PCI32_DMA_BOUNCE_THRESHOLD,	/* bounce_thresh */
    180 	ISA_DMA_BOUNCE_THRESHOLD,	/* bounce_alloclo */
    181 	PCI32_DMA_BOUNCE_THRESHOLD,	/* bounce_allochi */
    182 #else
    183 	0,
    184 	0,
    185 	0,
    186 #endif
    187 	NULL,			/* _may_bounce */
    188 	_bus_dmamap_create,
    189 	_bus_dmamap_destroy,
    190 	_bus_dmamap_load,
    191 	_bus_dmamap_load_mbuf,
    192 	_bus_dmamap_load_uio,
    193 	_bus_dmamap_load_raw,
    194 	_bus_dmamap_unload,
    195 #if defined(_LP64) || defined(PAE)
    196 	_bus_dmamap_sync,
    197 #else
    198 	NULL,
    199 #endif
    200 	_bus_dmamem_alloc,
    201 	_bus_dmamem_free,
    202 	_bus_dmamem_map,
    203 	_bus_dmamem_unmap,
    204 	_bus_dmamem_mmap,
    205 };
    206 
    207 #ifdef _LP64
    208 struct x86_bus_dma_tag pci_bus_dma64_tag = {
    209 	0,
    210 	0,
    211 	0,
    212 	NULL,			/* _may_bounce */
    213 	_bus_dmamap_create,
    214 	_bus_dmamap_destroy,
    215 	_bus_dmamap_load,
    216 	_bus_dmamap_load_mbuf,
    217 	_bus_dmamap_load_uio,
    218 	_bus_dmamap_load_raw,
    219 	_bus_dmamap_unload,
    220 	NULL,
    221 	_bus_dmamem_alloc,
    222 	_bus_dmamem_free,
    223 	_bus_dmamem_map,
    224 	_bus_dmamem_unmap,
    225 	_bus_dmamem_mmap,
    226 };
    227 #endif
    228 
    229 void
    230 pci_attach_hook(parent, self, pba)
    231 	struct device *parent, *self;
    232 	struct pcibus_attach_args *pba;
    233 {
    234 
    235 	if (pba->pba_bus == 0)
    236 		printf(": configuration mode %d", pci_mode);
    237 #ifdef MPBIOS
    238 	mpbios_pci_attach_hook(parent, self, pba);
    239 #endif
    240 #ifdef MPACPI
    241 	mpacpi_pci_attach_hook(parent, self, pba);
    242 #endif
    243 }
    244 
    245 int
    246 pci_bus_maxdevs(pc, busno)
    247 	pci_chipset_tag_t pc;
    248 	int busno;
    249 {
    250 
    251 	/*
    252 	 * Bus number is irrelevant.  If Configuration Mechanism 2 is in
    253 	 * use, can only have devices 0-15 on any bus.  If Configuration
    254 	 * Mechanism 1 is in use, can have devices 0-32 (i.e. the `normal'
    255 	 * range).
    256 	 */
    257 	if (pci_mode == 2)
    258 		return (16);
    259 	else
    260 		return (32);
    261 }
    262 
    263 pcitag_t
    264 pci_make_tag(pc, bus, device, function)
    265 	pci_chipset_tag_t pc;
    266 	int bus, device, function;
    267 {
    268 	pcitag_t tag;
    269 
    270 #ifndef PCI_CONF_MODE
    271 	switch (pci_mode) {
    272 	case 1:
    273 		goto mode1;
    274 	case 2:
    275 		goto mode2;
    276 	default:
    277 		panic("pci_make_tag: mode not configured");
    278 	}
    279 #endif
    280 
    281 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
    282 #ifndef PCI_CONF_MODE
    283 mode1:
    284 #endif
    285 	if (bus >= 256 || device >= 32 || function >= 8)
    286 		panic("pci_make_tag: bad request");
    287 
    288 	tag.mode1 = PCI_MODE1_ENABLE |
    289 		    (bus << 16) | (device << 11) | (function << 8);
    290 	return tag;
    291 #endif
    292 
    293 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
    294 #ifndef PCI_CONF_MODE
    295 mode2:
    296 #endif
    297 	if (bus >= 256 || device >= 16 || function >= 8)
    298 		panic("pci_make_tag: bad request");
    299 
    300 	tag.mode2.port = 0xc000 | (device << 8);
    301 	tag.mode2.enable = 0xf0 | (function << 1);
    302 	tag.mode2.forward = bus;
    303 	return tag;
    304 #endif
    305 }
    306 
    307 void
    308 pci_decompose_tag(pc, tag, bp, dp, fp)
    309 	pci_chipset_tag_t pc;
    310 	pcitag_t tag;
    311 	int *bp, *dp, *fp;
    312 {
    313 
    314 #ifndef PCI_CONF_MODE
    315 	switch (pci_mode) {
    316 	case 1:
    317 		goto mode1;
    318 	case 2:
    319 		goto mode2;
    320 	default:
    321 		panic("pci_decompose_tag: mode not configured");
    322 	}
    323 #endif
    324 
    325 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
    326 #ifndef PCI_CONF_MODE
    327 mode1:
    328 #endif
    329 	if (bp != NULL)
    330 		*bp = (tag.mode1 >> 16) & 0xff;
    331 	if (dp != NULL)
    332 		*dp = (tag.mode1 >> 11) & 0x1f;
    333 	if (fp != NULL)
    334 		*fp = (tag.mode1 >> 8) & 0x7;
    335 	return;
    336 #endif
    337 
    338 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
    339 #ifndef PCI_CONF_MODE
    340 mode2:
    341 #endif
    342 	if (bp != NULL)
    343 		*bp = tag.mode2.forward & 0xff;
    344 	if (dp != NULL)
    345 		*dp = (tag.mode2.port >> 8) & 0xf;
    346 	if (fp != NULL)
    347 		*fp = (tag.mode2.enable >> 1) & 0x7;
    348 #endif
    349 }
    350 
    351 pcireg_t
    352 pci_conf_read(pc, tag, reg)
    353 	pci_chipset_tag_t pc;
    354 	pcitag_t tag;
    355 	int reg;
    356 {
    357 	pcireg_t data;
    358 	int s;
    359 
    360 #ifndef PCI_CONF_MODE
    361 	switch (pci_mode) {
    362 	case 1:
    363 		goto mode1;
    364 	case 2:
    365 		goto mode2;
    366 	default:
    367 		panic("pci_conf_read: mode not configured");
    368 	}
    369 #endif
    370 
    371 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
    372 #ifndef PCI_CONF_MODE
    373 mode1:
    374 #endif
    375 	PCI_CONF_LOCK(s);
    376 	outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
    377 	data = inl(PCI_MODE1_DATA_REG);
    378 	outl(PCI_MODE1_ADDRESS_REG, 0);
    379 	PCI_CONF_UNLOCK(s);
    380 	return data;
    381 #endif
    382 
    383 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
    384 #ifndef PCI_CONF_MODE
    385 mode2:
    386 #endif
    387 	PCI_CONF_LOCK(s);
    388 	outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
    389 	outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
    390 	data = inl(tag.mode2.port | reg);
    391 	outb(PCI_MODE2_ENABLE_REG, 0);
    392 	PCI_CONF_UNLOCK(s);
    393 	return data;
    394 #endif
    395 }
    396 
    397 void
    398 pci_conf_write(pc, tag, reg, data)
    399 	pci_chipset_tag_t pc;
    400 	pcitag_t tag;
    401 	int reg;
    402 	pcireg_t data;
    403 {
    404 	int s;
    405 
    406 #ifndef PCI_CONF_MODE
    407 	switch (pci_mode) {
    408 	case 1:
    409 		goto mode1;
    410 	case 2:
    411 		goto mode2;
    412 	default:
    413 		panic("pci_conf_write: mode not configured");
    414 	}
    415 #endif
    416 
    417 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
    418 #ifndef PCI_CONF_MODE
    419 mode1:
    420 #endif
    421 	PCI_CONF_LOCK(s);
    422 	outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
    423 	outl(PCI_MODE1_DATA_REG, data);
    424 	outl(PCI_MODE1_ADDRESS_REG, 0);
    425 	PCI_CONF_UNLOCK(s);
    426 	return;
    427 #endif
    428 
    429 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
    430 #ifndef PCI_CONF_MODE
    431 mode2:
    432 #endif
    433 	PCI_CONF_LOCK(s);
    434 	outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
    435 	outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
    436 	outl(tag.mode2.port | reg, data);
    437 	outb(PCI_MODE2_ENABLE_REG, 0);
    438 	PCI_CONF_UNLOCK(s);
    439 #endif
    440 }
    441 
    442 int
    443 pci_mode_detect()
    444 {
    445 
    446 #ifdef PCI_CONF_MODE
    447 #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
    448 	return (pci_mode = PCI_CONF_MODE);
    449 #else
    450 #error Invalid PCI configuration mode.
    451 #endif
    452 #else
    453 	u_int32_t sav, val;
    454 	int i;
    455 	pcireg_t idreg;
    456 
    457 	if (pci_mode != -1)
    458 		return pci_mode;
    459 
    460 	/*
    461 	 * We try to divine which configuration mode the host bridge wants.
    462 	 */
    463 
    464 	sav = inl(PCI_MODE1_ADDRESS_REG);
    465 
    466 	pci_mode = 1; /* assume this for now */
    467 	/*
    468 	 * catch some known buggy implementations of mode 1
    469 	 */
    470 	for (i = 0; i < sizeof(pcim1_quirk_tbl) / sizeof(pcim1_quirk_tbl[0]);
    471 	     i++) {
    472 		pcitag_t t;
    473 
    474 		if (!pcim1_quirk_tbl[i].tag)
    475 			break;
    476 		t.mode1 = pcim1_quirk_tbl[i].tag;
    477 		idreg = pci_conf_read(0, t, PCI_ID_REG); /* needs "pci_mode" */
    478 		if (idreg == pcim1_quirk_tbl[i].id) {
    479 #ifdef DEBUG
    480 			printf("known mode 1 PCI chipset (%08x)\n",
    481 			       idreg);
    482 #endif
    483 			return (pci_mode);
    484 		}
    485 	}
    486 
    487 	/*
    488 	 * Strong check for standard compliant mode 1:
    489 	 * 1. bit 31 ("enable") can be set
    490 	 * 2. byte/word access does not affect register
    491 	 */
    492 	outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE);
    493 	outb(PCI_MODE1_ADDRESS_REG + 3, 0);
    494 	outw(PCI_MODE1_ADDRESS_REG + 2, 0);
    495 	val = inl(PCI_MODE1_ADDRESS_REG);
    496 	if ((val & 0x80fffffc) != PCI_MODE1_ENABLE) {
    497 #ifdef DEBUG
    498 		printf("pci_mode_detect: mode 1 enable failed (%x)\n",
    499 		       val);
    500 #endif
    501 		goto not1;
    502 	}
    503 	outl(PCI_MODE1_ADDRESS_REG, 0);
    504 	val = inl(PCI_MODE1_ADDRESS_REG);
    505 	if ((val & 0x80fffffc) != 0)
    506 		goto not1;
    507 	return (pci_mode);
    508 not1:
    509 	outl(PCI_MODE1_ADDRESS_REG, sav);
    510 
    511 	/*
    512 	 * This mode 2 check is quite weak (and known to give false
    513 	 * positives on some Compaq machines).
    514 	 * However, this doesn't matter, because this is the
    515 	 * last test, and simply no PCI devices will be found if
    516 	 * this happens.
    517 	 */
    518 	outb(PCI_MODE2_ENABLE_REG, 0);
    519 	outb(PCI_MODE2_FORWARD_REG, 0);
    520 	if (inb(PCI_MODE2_ENABLE_REG) != 0 ||
    521 	    inb(PCI_MODE2_FORWARD_REG) != 0)
    522 		goto not2;
    523 	return (pci_mode = 2);
    524 not2:
    525 
    526 	return (pci_mode = 0);
    527 #endif
    528 }
    529 
    530 /*
    531  * Determine which flags should be passed to the primary PCI bus's
    532  * autoconfiguration node.  We use this to detect broken chipsets
    533  * which cannot safely use memory-mapped device access.
    534  */
    535 int
    536 pci_bus_flags()
    537 {
    538 	int rval = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
    539 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
    540 	int device, maxndevs;
    541 	pcitag_t tag;
    542 	pcireg_t id;
    543 
    544 	maxndevs = pci_bus_maxdevs(NULL, 0);
    545 
    546 	for (device = 0; device < maxndevs; device++) {
    547 		tag = pci_make_tag(NULL, 0, device, 0);
    548 		id = pci_conf_read(NULL, tag, PCI_ID_REG);
    549 
    550 		/* Invalid vendor ID value? */
    551 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    552 			continue;
    553 		/* XXX Not invalid, but we've done this ~forever. */
    554 		if (PCI_VENDOR(id) == 0)
    555 			continue;
    556 
    557 		switch (PCI_VENDOR(id)) {
    558 		case PCI_VENDOR_SIS:
    559 			switch (PCI_PRODUCT(id)) {
    560 			case PCI_PRODUCT_SIS_85C496:
    561 				goto disable_mem;
    562 			}
    563 			break;
    564 		}
    565 	}
    566 
    567 	return (rval);
    568 
    569  disable_mem:
    570 	printf("Warning: broken PCI-Host bridge detected; "
    571 	    "disabling memory-mapped access\n");
    572 	rval &= ~(PCI_FLAGS_MEM_ENABLED|PCI_FLAGS_MRL_OKAY|PCI_FLAGS_MRM_OKAY|
    573 	    PCI_FLAGS_MWI_OKAY);
    574 	return (rval);
    575 }
    576 
    577 void
    578 pci_device_foreach(pci_chipset_tag_t pc, int maxbus,
    579 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
    580 {
    581 	pci_device_foreach_min(pc, 0, maxbus, func, context);
    582 }
    583 
    584 void
    585 pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus,
    586 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
    587 {
    588 	const struct pci_quirkdata *qd;
    589 	int bus, device, function, maxdevs, nfuncs;
    590 	pcireg_t id, bhlcr;
    591 	pcitag_t tag;
    592 
    593 	for (bus = minbus; bus <= maxbus; bus++) {
    594 		maxdevs = pci_bus_maxdevs(pc, bus);
    595 		for (device = 0; device < maxdevs; device++) {
    596 			tag = pci_make_tag(pc, bus, device, 0);
    597 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    598 
    599 			/* Invalid vendor ID value? */
    600 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    601 				continue;
    602 			/* XXX Not invalid, but we've done this ~forever. */
    603 			if (PCI_VENDOR(id) == 0)
    604 				continue;
    605 
    606 			qd = pci_lookup_quirkdata(PCI_VENDOR(id),
    607 				PCI_PRODUCT(id));
    608 
    609 			bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    610 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    611 			     (qd != NULL &&
    612 		  	     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    613 				nfuncs = 8;
    614 			else
    615 				nfuncs = 1;
    616 
    617 			for (function = 0; function < nfuncs; function++) {
    618 				tag = pci_make_tag(pc, bus, device, function);
    619 				id = pci_conf_read(pc, tag, PCI_ID_REG);
    620 
    621 				/* Invalid vendor ID value? */
    622 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    623 					continue;
    624 				/*
    625 				 * XXX Not invalid, but we've done this
    626 				 * ~forever.
    627 				 */
    628 				if (PCI_VENDOR(id) == 0)
    629 					continue;
    630 				(*func)(pc, tag, context);
    631 			}
    632 		}
    633 	}
    634 }
    635 
    636 void
    637 pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
    638 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
    639 {
    640 	struct pci_bridge_hook_arg bridge_hook;
    641 
    642 	bridge_hook.func = func;
    643 	bridge_hook.arg = ctx;
    644 
    645 	pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
    646 		&bridge_hook);
    647 }
    648 
    649 static void
    650 pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
    651 {
    652 	struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
    653 	pcireg_t reg;
    654 
    655 	reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
    656 	if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
    657  	     (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
    658 		PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
    659 		(*bridge_hook->func)(pc, tag, bridge_hook->arg);
    660 	}
    661 }
    662