Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.89
      1 /*	$NetBSD: pci_machdep.c,v 1.89 2021/10/15 18:51:38 jmcneill 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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     35  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by Charles M. Hannum.
     48  * 4. The name of the author may not be used to endorse or promote products
     49  *    derived from this software without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 
     63 /*
     64  * Machine-specific functions for PCI autoconfiguration.
     65  *
     66  * On PCs, there are two methods of generating PCI configuration cycles.
     67  * We try to detect the appropriate mechanism for this machine and set
     68  * up a few function pointers to access the correct method directly.
     69  *
     70  * The configuration method can be hard-coded in the config file by
     71  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
     72  * as defined in section 3.6.4.1, `Generating Configuration Cycles'.
     73  */
     74 
     75 #include <sys/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.89 2021/10/15 18:51:38 jmcneill Exp $");
     77 
     78 #include <sys/types.h>
     79 #include <sys/param.h>
     80 #include <sys/time.h>
     81 #include <sys/systm.h>
     82 #include <sys/errno.h>
     83 #include <sys/device.h>
     84 #include <sys/bus.h>
     85 #include <sys/cpu.h>
     86 #include <sys/kmem.h>
     87 
     88 #include <uvm/uvm_extern.h>
     89 
     90 #include <machine/bus_private.h>
     91 
     92 #include <machine/pio.h>
     93 #include <machine/lock.h>
     94 
     95 #include <dev/isa/isareg.h>
     96 #include <dev/isa/isavar.h>
     97 #include <dev/pci/pcivar.h>
     98 #include <dev/pci/pcireg.h>
     99 #include <dev/pci/pccbbreg.h>
    100 #include <dev/pci/pcidevs.h>
    101 #include <dev/pci/ppbvar.h>
    102 #include <dev/pci/genfb_pcivar.h>
    103 
    104 #include <dev/wsfb/genfbvar.h>
    105 #include <arch/x86/include/genfb_machdep.h>
    106 #include <dev/ic/vgareg.h>
    107 
    108 #include "acpica.h"
    109 #include "genfb.h"
    110 #include "isa.h"
    111 #include "opt_acpi.h"
    112 #include "opt_ddb.h"
    113 #include "opt_mpbios.h"
    114 #include "opt_puc.h"
    115 #include "opt_vga.h"
    116 #include "pci.h"
    117 #include "wsdisplay.h"
    118 #include "com.h"
    119 
    120 #ifdef DDB
    121 #include <machine/db_machdep.h>
    122 #include <ddb/db_sym.h>
    123 #include <ddb/db_extern.h>
    124 #endif
    125 
    126 #ifdef VGA_POST
    127 #include <x86/vga_post.h>
    128 #endif
    129 
    130 #include <x86/cpuvar.h>
    131 
    132 #include <machine/autoconf.h>
    133 #include <machine/bootinfo.h>
    134 
    135 #ifdef MPBIOS
    136 #include <machine/mpbiosvar.h>
    137 #endif
    138 
    139 #if NACPICA > 0
    140 #include <machine/mpacpi.h>
    141 #if !defined(NO_PCI_EXTENDED_CONFIG)
    142 #include <dev/acpi/acpivar.h>
    143 #include <dev/acpi/acpi_mcfg.h>
    144 #endif
    145 #endif
    146 
    147 #include <machine/mpconfig.h>
    148 
    149 #if NCOM > 0
    150 #include <dev/pci/puccn.h>
    151 #endif
    152 
    153 #ifndef XENPV
    154 #include <x86/efi.h>
    155 #endif
    156 
    157 #include "opt_pci_conf_mode.h"
    158 
    159 #ifdef PCI_CONF_MODE
    160 #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
    161 static int pci_mode = PCI_CONF_MODE;
    162 #else
    163 #error Invalid PCI configuration mode.
    164 #endif
    165 #else
    166 static int pci_mode = -1;
    167 #endif
    168 
    169 struct pci_conf_lock {
    170 	uint32_t cl_cpuno;	/* 0: unlocked
    171 				 * 1 + n: locked by CPU n (0 <= n)
    172 				 */
    173 	uint32_t cl_sel;	/* the address that's being read. */
    174 };
    175 
    176 static void pci_conf_unlock(struct pci_conf_lock *);
    177 static uint32_t pci_conf_selector(pcitag_t, int);
    178 static unsigned int pci_conf_port(pcitag_t, int);
    179 static void pci_conf_select(uint32_t);
    180 static void pci_conf_lock(struct pci_conf_lock *, uint32_t);
    181 static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
    182 struct pci_bridge_hook_arg {
    183 	void (*func)(pci_chipset_tag_t, pcitag_t, void *);
    184 	void *arg;
    185 };
    186 
    187 #define	PCI_MODE1_ENABLE	0x80000000UL
    188 #define	PCI_MODE1_ADDRESS_REG	0x0cf8
    189 #define	PCI_MODE1_DATA_REG	0x0cfc
    190 
    191 #define	PCI_MODE2_ENABLE_REG	0x0cf8
    192 #define	PCI_MODE2_FORWARD_REG	0x0cfa
    193 
    194 #define _tag(b, d, f) \
    195 	{.mode1 = PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8)}
    196 #define _qe(bus, dev, fcn, vend, prod) \
    197 	{_tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)}
    198 const struct {
    199 	pcitag_t tag;
    200 	pcireg_t id;
    201 } pcim1_quirk_tbl[] = {
    202 	_qe(0, 0, 0, PCI_VENDOR_INVALID, 0x0000), /* patchable */
    203 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX1),
    204 	/* XXX Triflex2 not tested */
    205 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX2),
    206 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX4),
    207 #if 0
    208 	/* Triton needed for Connectix Virtual PC */
    209 	_qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX),
    210 	/* Connectix Virtual PC 5 has a 440BX */
    211 	_qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP),
    212 	/* Parallels Desktop for Mac */
    213 	_qe(0, 2, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_VIDEO),
    214 	_qe(0, 3, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_TOOLS),
    215 	/* SIS 740 */
    216 	_qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_740),
    217 	/* SIS 741 */
    218 	_qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_741),
    219 	/* VIA Technologies VX900 */
    220 	_qe(0, 0, 0, PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX900_HB)
    221 #endif
    222 };
    223 #undef _tag
    224 #undef _qe
    225 
    226 /* arch/xen does not support MSI/MSI-X yet. */
    227 #ifdef __HAVE_PCI_MSI_MSIX
    228 #define PCI_QUIRK_DISABLE_MSI	1 /* Neigher MSI nor MSI-X work */
    229 #define PCI_QUIRK_DISABLE_MSIX	2 /* MSI-X does not work */
    230 #define PCI_QUIRK_ENABLE_MSI_VM	3 /* Older chipset in VM where MSI and MSI-X works */
    231 
    232 #define _dme(vend, prod) \
    233 	{ PCI_QUIRK_DISABLE_MSI, PCI_ID_CODE(vend, prod) }
    234 #define _dmxe(vend, prod) \
    235 	{ PCI_QUIRK_DISABLE_MSIX, PCI_ID_CODE(vend, prod) }
    236 #define _emve(vend, prod) \
    237 	{ PCI_QUIRK_ENABLE_MSI_VM, PCI_ID_CODE(vend, prod) }
    238 const struct {
    239 	int type;
    240 	pcireg_t id;
    241 } pci_msi_quirk_tbl[] = {
    242 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PCMC),
    243 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX),
    244 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437MX),
    245 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437VX),
    246 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82439HX),
    247 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82439TX),
    248 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443GX),
    249 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443GX_AGP),
    250 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82440MX),
    251 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82441FX),
    252 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX),
    253 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_AGP),
    254 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP),
    255 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443GX_NOAGP),
    256 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443LX),
    257 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443LX_AGP),
    258 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810_MCH),
    259 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810E_MCH),
    260 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82815_FULL_HUB),
    261 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82820_MCH),
    262 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82830MP_IO_1),
    263 	_dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82840_HB),
    264 	_dme(PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE_PCHB),
    265 	_dme(PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_PCHB),
    266 	_dme(PCI_VENDOR_AMD, PCI_PRODUCT_AMD_SC751_SC),
    267 	_dme(PCI_VENDOR_AMD, PCI_PRODUCT_AMD_SC761_SC),
    268 	_dme(PCI_VENDOR_AMD, PCI_PRODUCT_AMD_SC762_NB),
    269 
    270 	_emve(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82441FX), /* QEMU */
    271 	_emve(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX), /* VMWare */
    272 };
    273 #undef _dme
    274 #undef _dmxe
    275 #undef _emve
    276 #endif /* __HAVE_PCI_MSI_MSIX */
    277 
    278 /*
    279  * PCI doesn't have any special needs; just use the generic versions
    280  * of these functions.
    281  */
    282 struct x86_bus_dma_tag pci_bus_dma_tag = {
    283 	._tag_needs_free	= 0,
    284 #if defined(_LP64) || defined(PAE)
    285 	._bounce_thresh		= PCI32_DMA_BOUNCE_THRESHOLD,
    286 	._bounce_alloc_lo	= ISA_DMA_BOUNCE_THRESHOLD,
    287 	._bounce_alloc_hi	= PCI32_DMA_BOUNCE_THRESHOLD,
    288 #else
    289 	._bounce_thresh		= 0,
    290 	._bounce_alloc_lo	= 0,
    291 	._bounce_alloc_hi	= 0,
    292 #endif
    293 	._may_bounce		= NULL,
    294 };
    295 
    296 #ifdef _LP64
    297 struct x86_bus_dma_tag pci_bus_dma64_tag = {
    298 	._tag_needs_free	= 0,
    299 	._bounce_thresh		= 0,
    300 	._bounce_alloc_lo	= 0,
    301 	._bounce_alloc_hi	= 0,
    302 	._may_bounce		= NULL,
    303 };
    304 #endif
    305 
    306 static struct pci_conf_lock cl0 = {
    307 	  .cl_cpuno = 0UL
    308 	, .cl_sel = 0UL
    309 };
    310 
    311 static struct pci_conf_lock * const cl = &cl0;
    312 
    313 #if NGENFB > 0 && NACPICA > 0 && defined(VGA_POST)
    314 extern int acpi_md_vbios_reset;
    315 extern int acpi_md_vesa_modenum;
    316 #endif
    317 
    318 static struct genfb_colormap_callback gfb_cb;
    319 static struct genfb_pmf_callback pmf_cb;
    320 static struct genfb_mode_callback mode_cb;
    321 #ifdef VGA_POST
    322 static struct vga_post *vga_posth = NULL;
    323 #endif
    324 
    325 static void
    326 pci_conf_lock(struct pci_conf_lock *ocl, uint32_t sel)
    327 {
    328 	uint32_t cpuno;
    329 
    330 	KASSERT(sel != 0);
    331 
    332 	kpreempt_disable();
    333 	cpuno = cpu_number() + 1;
    334 	/* If the kernel enters pci_conf_lock() through an interrupt
    335 	 * handler, then the CPU may already hold the lock.
    336 	 *
    337 	 * If the CPU does not already hold the lock, spin until
    338 	 * we can acquire it.
    339 	 */
    340 	if (cpuno == cl->cl_cpuno) {
    341 		ocl->cl_cpuno = cpuno;
    342 	} else {
    343 #ifdef LOCKDEBUG
    344 		u_int spins = 0;
    345 #endif
    346 		u_int count;
    347 		count = SPINLOCK_BACKOFF_MIN;
    348 
    349 		ocl->cl_cpuno = 0;
    350 
    351 		while (atomic_cas_32(&cl->cl_cpuno, 0, cpuno) != 0) {
    352 			SPINLOCK_BACKOFF(count);
    353 #ifdef LOCKDEBUG
    354 			if (SPINLOCK_SPINOUT(spins)) {
    355 				panic("%s: cpu %" PRId32
    356 				    " spun out waiting for cpu %" PRId32,
    357 				    __func__, cpuno, cl->cl_cpuno);
    358 			}
    359 #endif
    360 		}
    361 	}
    362 
    363 	/* Only one CPU can be here, so an interlocked atomic_swap(3)
    364 	 * is not necessary.
    365 	 *
    366 	 * Evaluating atomic_cas_32_ni()'s argument, cl->cl_sel,
    367 	 * and applying atomic_cas_32_ni() is not an atomic operation,
    368 	 * however, any interrupt that, in the middle of the
    369 	 * operation, modifies cl->cl_sel, will also restore
    370 	 * cl->cl_sel.  So cl->cl_sel will have the same value when
    371 	 * we apply atomic_cas_32_ni() as when we evaluated it,
    372 	 * before.
    373 	 */
    374 	ocl->cl_sel = atomic_cas_32_ni(&cl->cl_sel, cl->cl_sel, sel);
    375 	pci_conf_select(sel);
    376 }
    377 
    378 static void
    379 pci_conf_unlock(struct pci_conf_lock *ocl)
    380 {
    381 	atomic_cas_32_ni(&cl->cl_sel, cl->cl_sel, ocl->cl_sel);
    382 	pci_conf_select(ocl->cl_sel);
    383 	if (ocl->cl_cpuno != cl->cl_cpuno)
    384 		atomic_cas_32(&cl->cl_cpuno, cl->cl_cpuno, ocl->cl_cpuno);
    385 	kpreempt_enable();
    386 }
    387 
    388 static uint32_t
    389 pci_conf_selector(pcitag_t tag, int reg)
    390 {
    391 	static const pcitag_t mode2_mask = {
    392 		.mode2 = {
    393 			  .enable = 0xff
    394 			, .forward = 0xff
    395 		}
    396 	};
    397 
    398 	switch (pci_mode) {
    399 	case 1:
    400 		return tag.mode1 | reg;
    401 	case 2:
    402 		return tag.mode1 & mode2_mask.mode1;
    403 	default:
    404 		panic("%s: mode %d not configured", __func__, pci_mode);
    405 	}
    406 }
    407 
    408 static unsigned int
    409 pci_conf_port(pcitag_t tag, int reg)
    410 {
    411 	switch (pci_mode) {
    412 	case 1:
    413 		return PCI_MODE1_DATA_REG;
    414 	case 2:
    415 		return tag.mode2.port | reg;
    416 	default:
    417 		panic("%s: mode %d not configured", __func__, pci_mode);
    418 	}
    419 }
    420 
    421 static void
    422 pci_conf_select(uint32_t sel)
    423 {
    424 	pcitag_t tag;
    425 
    426 	switch (pci_mode) {
    427 	case 1:
    428 		outl(PCI_MODE1_ADDRESS_REG, sel);
    429 		return;
    430 	case 2:
    431 		tag.mode1 = sel;
    432 		outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
    433 		if (tag.mode2.enable != 0)
    434 			outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
    435 		return;
    436 	default:
    437 		panic("%s: mode %d not configured", __func__, pci_mode);
    438 	}
    439 }
    440 
    441 static int
    442 pci_mode_check(void)
    443 {
    444 	pcireg_t x;
    445 	pcitag_t t;
    446 	int device;
    447 	const int maxdev = pci_bus_maxdevs(NULL, 0);
    448 
    449 	for (device = 0; device < maxdev; device++) {
    450 		t = pci_make_tag(NULL, 0, device, 0);
    451 		x = pci_conf_read(NULL, t, PCI_CLASS_REG);
    452 		if (PCI_CLASS(x) == PCI_CLASS_BRIDGE &&
    453 		    PCI_SUBCLASS(x) == PCI_SUBCLASS_BRIDGE_HOST)
    454 			return 0;
    455 		x = pci_conf_read(NULL, t, PCI_ID_REG);
    456 		switch (PCI_VENDOR(x)) {
    457 		case PCI_VENDOR_COMPAQ:
    458 		case PCI_VENDOR_INTEL:
    459 		case PCI_VENDOR_VIATECH:
    460 			return 0;
    461 		}
    462 	}
    463 	return -1;
    464 }
    465 #ifdef __HAVE_PCI_MSI_MSIX
    466 static int
    467 pci_has_msi_quirk(pcireg_t id, int type)
    468 {
    469 	int i;
    470 
    471 	for (i = 0; i < __arraycount(pci_msi_quirk_tbl); i++) {
    472 		if (id == pci_msi_quirk_tbl[i].id &&
    473 		    type == pci_msi_quirk_tbl[i].type)
    474 			return 1;
    475 	}
    476 
    477 	return 0;
    478 }
    479 #endif
    480 
    481 void
    482 pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
    483 {
    484 #ifdef __HAVE_PCI_MSI_MSIX
    485 	pci_chipset_tag_t pc = pba->pba_pc;
    486 	pcitag_t tag;
    487 	pcireg_t id, class;
    488 #endif
    489 
    490 	if (pba->pba_bus == 0)
    491 		aprint_normal(": configuration mode %d", pci_mode);
    492 #ifdef MPBIOS
    493 	mpbios_pci_attach_hook(parent, self, pba);
    494 #endif
    495 #if NACPICA > 0
    496 	mpacpi_pci_attach_hook(parent, self, pba);
    497 #endif
    498 #if NACPICA > 0 && !defined(NO_PCI_EXTENDED_CONFIG)
    499 	acpimcfg_map_bus(self, pba->pba_pc, pba->pba_bus);
    500 #endif
    501 
    502 #ifdef __HAVE_PCI_MSI_MSIX
    503 	/*
    504 	 * In order to decide whether the system supports MSI we look
    505 	 * at the host bridge, which should be device 0 function 0 on
    506 	 * bus 0.  It is better to not enable MSI on systems that
    507 	 * support it than the other way around, so be conservative
    508 	 * here.  So we don't enable MSI if we don't find a host
    509 	 * bridge there.  We also deliberately don't enable MSI on
    510 	 * chipsets from low-end manifacturers like VIA and SiS.
    511 	 */
    512 	tag = pci_make_tag(pc, 0, 0, 0);
    513 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    514 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    515 
    516 	if (PCI_CLASS(class) != PCI_CLASS_BRIDGE ||
    517 	    PCI_SUBCLASS(class) != PCI_SUBCLASS_BRIDGE_HOST)
    518 		return;
    519 
    520 	/* VMware and KVM use old chipset, but they can use MSI/MSI-X */
    521 	if ((cpu_feature[1] & CPUID2_RAZ)
    522 	    && (pci_has_msi_quirk(id, PCI_QUIRK_ENABLE_MSI_VM))) {
    523 			pba->pba_flags |= PCI_FLAGS_MSI_OKAY;
    524 			pba->pba_flags |= PCI_FLAGS_MSIX_OKAY;
    525 	} else if (pci_has_msi_quirk(id, PCI_QUIRK_DISABLE_MSI)) {
    526 		pba->pba_flags &= ~PCI_FLAGS_MSI_OKAY;
    527 		pba->pba_flags &= ~PCI_FLAGS_MSIX_OKAY;
    528 		aprint_verbose("\n");
    529 		aprint_verbose_dev(self,
    530 		    "This pci host supports neither MSI nor MSI-X.");
    531 	} else if (pci_has_msi_quirk(id, PCI_QUIRK_DISABLE_MSIX)) {
    532 		pba->pba_flags |= PCI_FLAGS_MSI_OKAY;
    533 		pba->pba_flags &= ~PCI_FLAGS_MSIX_OKAY;
    534 		aprint_verbose("\n");
    535 		aprint_verbose_dev(self,
    536 		    "This pci host does not support MSI-X.");
    537 #if NACPICA > 0
    538 	} else if (acpi_active &&
    539 		   AcpiGbl_FADT.Header.Revision >= 4 &&
    540 		   (AcpiGbl_FADT.BootFlags & ACPI_FADT_NO_MSI) != 0) {
    541 		pba->pba_flags &= ~PCI_FLAGS_MSI_OKAY;
    542 		pba->pba_flags &= ~PCI_FLAGS_MSIX_OKAY;
    543 		aprint_verbose("\n");
    544 		aprint_verbose_dev(self,
    545 		    "MSI support disabled via ACPI IAPC_BOOT_ARCH flag.\n");
    546 #endif
    547 	} else {
    548 		pba->pba_flags |= PCI_FLAGS_MSI_OKAY;
    549 		pba->pba_flags |= PCI_FLAGS_MSIX_OKAY;
    550 	}
    551 
    552 	/*
    553 	 * Don't enable MSI on a HyperTransport bus.  In order to
    554 	 * determine that bus 0 is a HyperTransport bus, we look at
    555 	 * device 24 function 0, which is the HyperTransport
    556 	 * host/primary interface integrated on most 64-bit AMD CPUs.
    557 	 * If that device has a HyperTransport capability, bus 0 must
    558 	 * be a HyperTransport bus and we disable MSI.
    559 	 */
    560 	if (24 < pci_bus_maxdevs(pc, 0)) {
    561 		tag = pci_make_tag(pc, 0, 24, 0);
    562 		if (pci_get_capability(pc, tag, PCI_CAP_LDT, NULL, NULL)) {
    563 			pba->pba_flags &= ~PCI_FLAGS_MSI_OKAY;
    564 			pba->pba_flags &= ~PCI_FLAGS_MSIX_OKAY;
    565 		}
    566 	}
    567 
    568 #ifdef XENPV
    569 	/*
    570 	 * XXX MSI-X doesn't work for XenPV yet - setup seems to be correct,
    571 	 * XXX but no interrupts are actually delivered.
    572 	 */
    573 	pba->pba_flags &= ~PCI_FLAGS_MSIX_OKAY;
    574 #endif
    575 
    576 #endif /* __HAVE_PCI_MSI_MSIX */
    577 }
    578 
    579 int
    580 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    581 {
    582 	/*
    583 	 * Bus number is irrelevant.  If Configuration Mechanism 2 is in
    584 	 * use, can only have devices 0-15 on any bus.  If Configuration
    585 	 * Mechanism 1 is in use, can have devices 0-32 (i.e. the `normal'
    586 	 * range).
    587 	 */
    588 	if (pci_mode == 2)
    589 		return (16);
    590 	else
    591 		return (32);
    592 }
    593 
    594 pcitag_t
    595 pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
    596 {
    597 	pci_chipset_tag_t ipc;
    598 	pcitag_t tag;
    599 
    600 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    601 		if ((ipc->pc_present & PCI_OVERRIDE_MAKE_TAG) == 0)
    602 			continue;
    603 		return (*ipc->pc_ov->ov_make_tag)(ipc->pc_ctx,
    604 		    pc, bus, device, function);
    605 	}
    606 
    607 	switch (pci_mode) {
    608 	case 1:
    609 		if (bus >= 256 || device >= 32 || function >= 8)
    610 			panic("%s: bad request(%d, %d, %d)", __func__,
    611 			    bus, device, function);
    612 
    613 		tag.mode1 = PCI_MODE1_ENABLE |
    614 			    (bus << 16) | (device << 11) | (function << 8);
    615 		return tag;
    616 	case 2:
    617 		if (bus >= 256 || device >= 16 || function >= 8)
    618 			panic("%s: bad request(%d, %d, %d)", __func__,
    619 			    bus, device, function);
    620 
    621 		tag.mode2.port = 0xc000 | (device << 8);
    622 		tag.mode2.enable = 0xf0 | (function << 1);
    623 		tag.mode2.forward = bus;
    624 		return tag;
    625 	default:
    626 		panic("%s: mode %d not configured", __func__, pci_mode);
    627 	}
    628 }
    629 
    630 void
    631 pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
    632     int *bp, int *dp, int *fp)
    633 {
    634 	pci_chipset_tag_t ipc;
    635 
    636 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    637 		if ((ipc->pc_present & PCI_OVERRIDE_DECOMPOSE_TAG) == 0)
    638 			continue;
    639 		(*ipc->pc_ov->ov_decompose_tag)(ipc->pc_ctx,
    640 		    pc, tag, bp, dp, fp);
    641 		return;
    642 	}
    643 
    644 	switch (pci_mode) {
    645 	case 1:
    646 		if (bp != NULL)
    647 			*bp = (tag.mode1 >> 16) & 0xff;
    648 		if (dp != NULL)
    649 			*dp = (tag.mode1 >> 11) & 0x1f;
    650 		if (fp != NULL)
    651 			*fp = (tag.mode1 >> 8) & 0x7;
    652 		return;
    653 	case 2:
    654 		if (bp != NULL)
    655 			*bp = tag.mode2.forward & 0xff;
    656 		if (dp != NULL)
    657 			*dp = (tag.mode2.port >> 8) & 0xf;
    658 		if (fp != NULL)
    659 			*fp = (tag.mode2.enable >> 1) & 0x7;
    660 		return;
    661 	default:
    662 		panic("%s: mode %d not configured", __func__, pci_mode);
    663 	}
    664 }
    665 
    666 pcireg_t
    667 pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    668 {
    669 	pci_chipset_tag_t ipc;
    670 	pcireg_t data;
    671 	struct pci_conf_lock ocl;
    672 	int dev;
    673 
    674 	KASSERT((reg & 0x3) == 0);
    675 
    676 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    677 		if ((ipc->pc_present & PCI_OVERRIDE_CONF_READ) == 0)
    678 			continue;
    679 		return (*ipc->pc_ov->ov_conf_read)(ipc->pc_ctx, pc, tag, reg);
    680 	}
    681 
    682 	pci_decompose_tag(pc, tag, NULL, &dev, NULL);
    683 	if (__predict_false(pci_mode == 2 && dev >= 16))
    684 		return (pcireg_t) -1;
    685 
    686 	if (reg < 0)
    687 		return (pcireg_t) -1;
    688 	if (reg >= PCI_CONF_SIZE) {
    689 #if NACPICA > 0 && !defined(NO_PCI_EXTENDED_CONFIG)
    690 		if (reg >= PCI_EXTCONF_SIZE)
    691 			return (pcireg_t) -1;
    692 		acpimcfg_conf_read(pc, tag, reg, &data);
    693 		return data;
    694 #else
    695 		return (pcireg_t) -1;
    696 #endif
    697 	}
    698 
    699 	pci_conf_lock(&ocl, pci_conf_selector(tag, reg));
    700 	data = inl(pci_conf_port(tag, reg));
    701 	pci_conf_unlock(&ocl);
    702 	return data;
    703 }
    704 
    705 void
    706 pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    707 {
    708 	pci_chipset_tag_t ipc;
    709 	struct pci_conf_lock ocl;
    710 	int dev;
    711 
    712 	KASSERT((reg & 0x3) == 0);
    713 
    714 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    715 		if ((ipc->pc_present & PCI_OVERRIDE_CONF_WRITE) == 0)
    716 			continue;
    717 		(*ipc->pc_ov->ov_conf_write)(ipc->pc_ctx, pc, tag, reg,
    718 		    data);
    719 		return;
    720 	}
    721 
    722 	pci_decompose_tag(pc, tag, NULL, &dev, NULL);
    723 	if (__predict_false(pci_mode == 2 && dev >= 16)) {
    724 		return;
    725 	}
    726 
    727 	if (reg < 0)
    728 		return;
    729 	if (reg >= PCI_CONF_SIZE) {
    730 #if NACPICA > 0 && !defined(NO_PCI_EXTENDED_CONFIG)
    731 		if (reg >= PCI_EXTCONF_SIZE)
    732 			return;
    733 		acpimcfg_conf_write(pc, tag, reg, data);
    734 #endif
    735 		return;
    736 	}
    737 
    738 	pci_conf_lock(&ocl, pci_conf_selector(tag, reg));
    739 	outl(pci_conf_port(tag, reg), data);
    740 	pci_conf_unlock(&ocl);
    741 }
    742 
    743 void
    744 pci_mode_set(int mode)
    745 {
    746 	KASSERT(pci_mode == -1 || pci_mode == mode);
    747 
    748 	pci_mode = mode;
    749 }
    750 
    751 int
    752 pci_mode_detect(void)
    753 {
    754 	uint32_t sav, val;
    755 	int i;
    756 	pcireg_t idreg;
    757 
    758 	if (pci_mode != -1)
    759 		return pci_mode;
    760 
    761 	/*
    762 	 * We try to divine which configuration mode the host bridge wants.
    763 	 */
    764 
    765 	sav = inl(PCI_MODE1_ADDRESS_REG);
    766 
    767 	pci_mode = 1; /* assume this for now */
    768 	/*
    769 	 * catch some known buggy implementations of mode 1
    770 	 */
    771 	for (i = 0; i < __arraycount(pcim1_quirk_tbl); i++) {
    772 		pcitag_t t;
    773 
    774 		if (PCI_VENDOR(pcim1_quirk_tbl[i].id) == PCI_VENDOR_INVALID)
    775 			continue;
    776 		t.mode1 = pcim1_quirk_tbl[i].tag.mode1;
    777 		idreg = pci_conf_read(NULL, t, PCI_ID_REG); /* needs "pci_mode" */
    778 		if (idreg == pcim1_quirk_tbl[i].id) {
    779 #ifdef DEBUG
    780 			printf("%s: known mode 1 PCI chipset (%08x)\n",
    781 			    __func__, idreg);
    782 #endif
    783 			return (pci_mode);
    784 		}
    785 	}
    786 
    787 #if 0
    788 	extern char cpu_brand_string[];
    789 	const char *reason, *system_vendor, *system_product;
    790 	if (memcmp(cpu_brand_string, "QEMU", 4) == 0)
    791 		/* PR 45671, https://bugs.launchpad.net/qemu/+bug/897771 */
    792 		reason = "QEMU";
    793 	else if ((system_vendor = pmf_get_platform("system-vendor")) != NULL &&
    794 	    strcmp(system_vendor, "Xen") == 0 &&
    795 	    (system_product = pmf_get_platform("system-product")) != NULL &&
    796 	    strcmp(system_product, "HVM domU") == 0)
    797 		reason = "Xen";
    798 	else
    799 		reason = NULL;
    800 
    801 	if (reason) {
    802 #ifdef DEBUG
    803 		printf("%s: forcing PCI mode 1 for %s\n", __func__, reason);
    804 #endif
    805 		return (pci_mode);
    806 	}
    807 #endif
    808 	/*
    809 	 * Strong check for standard compliant mode 1:
    810 	 * 1. bit 31 ("enable") can be set
    811 	 * 2. byte/word access does not affect register
    812 	 */
    813 	outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE);
    814 	outb(PCI_MODE1_ADDRESS_REG + 3, 0);
    815 	outw(PCI_MODE1_ADDRESS_REG + 2, 0);
    816 	val = inl(PCI_MODE1_ADDRESS_REG);
    817 	if ((val & 0x80fffffc) != PCI_MODE1_ENABLE) {
    818 #ifdef DEBUG
    819 		printf("%s: mode 1 enable failed (%x)\n", __func__, val);
    820 #endif
    821 		/* Try out mode 1 to see if we can find a host bridge. */
    822 		if (pci_mode_check() == 0) {
    823 #ifdef DEBUG
    824 			printf("%s: mode 1 functional, using\n", __func__);
    825 #endif
    826 			return (pci_mode);
    827 		}
    828 		goto not1;
    829 	}
    830 	outl(PCI_MODE1_ADDRESS_REG, 0);
    831 	val = inl(PCI_MODE1_ADDRESS_REG);
    832 	if ((val & 0x80fffffc) != 0)
    833 		goto not1;
    834 	return (pci_mode);
    835 not1:
    836 	outl(PCI_MODE1_ADDRESS_REG, sav);
    837 
    838 	/*
    839 	 * This mode 2 check is quite weak (and known to give false
    840 	 * positives on some Compaq machines).
    841 	 * However, this doesn't matter, because this is the
    842 	 * last test, and simply no PCI devices will be found if
    843 	 * this happens.
    844 	 */
    845 	outb(PCI_MODE2_ENABLE_REG, 0);
    846 	outb(PCI_MODE2_FORWARD_REG, 0);
    847 	if (inb(PCI_MODE2_ENABLE_REG) != 0 ||
    848 	    inb(PCI_MODE2_FORWARD_REG) != 0)
    849 		goto not2;
    850 	return (pci_mode = 2);
    851 not2:
    852 
    853 	return (pci_mode = 0);
    854 }
    855 
    856 void
    857 pci_device_foreach(pci_chipset_tag_t pc, int maxbus,
    858 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
    859 {
    860 	pci_device_foreach_min(pc, 0, maxbus, func, context);
    861 }
    862 
    863 void
    864 pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus,
    865 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
    866 {
    867 	const struct pci_quirkdata *qd;
    868 	int bus, device, function, maxdevs, nfuncs;
    869 	pcireg_t id, bhlcr;
    870 	pcitag_t tag;
    871 
    872 	for (bus = minbus; bus <= maxbus; bus++) {
    873 		maxdevs = pci_bus_maxdevs(pc, bus);
    874 		for (device = 0; device < maxdevs; device++) {
    875 			tag = pci_make_tag(pc, bus, device, 0);
    876 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    877 
    878 			/* Invalid vendor ID value? */
    879 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    880 				continue;
    881 			/* XXX Not invalid, but we've done this ~forever. */
    882 			if (PCI_VENDOR(id) == 0)
    883 				continue;
    884 
    885 			qd = pci_lookup_quirkdata(PCI_VENDOR(id),
    886 				PCI_PRODUCT(id));
    887 
    888 			bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    889 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    890 			     (qd != NULL &&
    891 			     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    892 				nfuncs = 8;
    893 			else
    894 				nfuncs = 1;
    895 
    896 			for (function = 0; function < nfuncs; function++) {
    897 				tag = pci_make_tag(pc, bus, device, function);
    898 				id = pci_conf_read(pc, tag, PCI_ID_REG);
    899 
    900 				/* Invalid vendor ID value? */
    901 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    902 					continue;
    903 				/*
    904 				 * XXX Not invalid, but we've done this
    905 				 * ~forever.
    906 				 */
    907 				if (PCI_VENDOR(id) == 0)
    908 					continue;
    909 				(*func)(pc, tag, context);
    910 			}
    911 		}
    912 	}
    913 }
    914 
    915 void
    916 pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
    917 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
    918 {
    919 	struct pci_bridge_hook_arg bridge_hook;
    920 
    921 	bridge_hook.func = func;
    922 	bridge_hook.arg = ctx;
    923 
    924 	pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
    925 		&bridge_hook);
    926 }
    927 
    928 static void
    929 pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
    930 {
    931 	struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
    932 	pcireg_t reg;
    933 
    934 	reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
    935 	if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
    936 	    (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
    937 		PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
    938 		(*bridge_hook->func)(pc, tag, bridge_hook->arg);
    939 	}
    940 }
    941 
    942 static const void *
    943 bit_to_function_pointer(const struct pci_overrides *ov, uint64_t bit)
    944 {
    945 	switch (bit) {
    946 	case PCI_OVERRIDE_CONF_READ:
    947 		return ov->ov_conf_read;
    948 	case PCI_OVERRIDE_CONF_WRITE:
    949 		return ov->ov_conf_write;
    950 	case PCI_OVERRIDE_INTR_MAP:
    951 		return ov->ov_intr_map;
    952 	case PCI_OVERRIDE_INTR_STRING:
    953 		return ov->ov_intr_string;
    954 	case PCI_OVERRIDE_INTR_EVCNT:
    955 		return ov->ov_intr_evcnt;
    956 	case PCI_OVERRIDE_INTR_ESTABLISH:
    957 		return ov->ov_intr_establish;
    958 	case PCI_OVERRIDE_INTR_DISESTABLISH:
    959 		return ov->ov_intr_disestablish;
    960 	case PCI_OVERRIDE_MAKE_TAG:
    961 		return ov->ov_make_tag;
    962 	case PCI_OVERRIDE_DECOMPOSE_TAG:
    963 		return ov->ov_decompose_tag;
    964 	default:
    965 		return NULL;
    966 	}
    967 }
    968 
    969 void
    970 pci_chipset_tag_destroy(pci_chipset_tag_t pc)
    971 {
    972 	kmem_free(pc, sizeof(struct pci_chipset_tag));
    973 }
    974 
    975 int
    976 pci_chipset_tag_create(pci_chipset_tag_t opc, const uint64_t present,
    977     const struct pci_overrides *ov, void *ctx, pci_chipset_tag_t *pcp)
    978 {
    979 	uint64_t bit, bits, nbits;
    980 	pci_chipset_tag_t pc;
    981 	const void *fp;
    982 
    983 	if (ov == NULL || present == 0)
    984 		return EINVAL;
    985 
    986 	pc = kmem_alloc(sizeof(struct pci_chipset_tag), KM_SLEEP);
    987 	pc->pc_super = opc;
    988 
    989 	for (bits = present; bits != 0; bits = nbits) {
    990 		nbits = bits & (bits - 1);
    991 		bit = nbits ^ bits;
    992 		if ((fp = bit_to_function_pointer(ov, bit)) == NULL) {
    993 #ifdef DEBUG
    994 			printf("%s: missing bit %" PRIx64 "\n", __func__, bit);
    995 #endif
    996 			goto einval;
    997 		}
    998 	}
    999 
   1000 	pc->pc_ov = ov;
   1001 	pc->pc_present = present;
   1002 	pc->pc_ctx = ctx;
   1003 
   1004 	*pcp = pc;
   1005 
   1006 	return 0;
   1007 einval:
   1008 	kmem_free(pc, sizeof(struct pci_chipset_tag));
   1009 	return EINVAL;
   1010 }
   1011 
   1012 static void
   1013 x86_genfb_set_mapreg(void *opaque, int index, int r, int g, int b)
   1014 {
   1015 	outb(IO_VGA + VGA_DAC_ADDRW, index);
   1016 	outb(IO_VGA + VGA_DAC_PALETTE, (uint8_t)r >> 2);
   1017 	outb(IO_VGA + VGA_DAC_PALETTE, (uint8_t)g >> 2);
   1018 	outb(IO_VGA + VGA_DAC_PALETTE, (uint8_t)b >> 2);
   1019 }
   1020 
   1021 static bool
   1022 x86_genfb_setmode(struct genfb_softc *sc, int newmode)
   1023 {
   1024 #if NGENFB > 0
   1025 # if NACPICA > 0 && defined(VGA_POST)
   1026 	static int curmode = WSDISPLAYIO_MODE_EMUL;
   1027 # endif
   1028 
   1029 	switch (newmode) {
   1030 	case WSDISPLAYIO_MODE_EMUL:
   1031 # if NACPICA > 0 && defined(VGA_POST)
   1032 		if (curmode != newmode) {
   1033 			if (vga_posth != NULL && acpi_md_vesa_modenum != 0) {
   1034 				vga_post_set_vbe(vga_posth,
   1035 				    acpi_md_vesa_modenum);
   1036 			}
   1037 		}
   1038 # endif
   1039 		break;
   1040 	}
   1041 
   1042 # if NACPICA > 0 && defined(VGA_POST)
   1043 	curmode = newmode;
   1044 # endif
   1045 #endif
   1046 	return true;
   1047 }
   1048 
   1049 static bool
   1050 x86_genfb_suspend(device_t dev, const pmf_qual_t *qual)
   1051 {
   1052 	return true;
   1053 }
   1054 
   1055 static bool
   1056 x86_genfb_resume(device_t dev, const pmf_qual_t *qual)
   1057 {
   1058 #if NGENFB > 0
   1059 	struct pci_genfb_softc *psc = device_private(dev);
   1060 
   1061 #if NACPICA > 0 && defined(VGA_POST)
   1062 	if (vga_posth != NULL && acpi_md_vbios_reset == 2) {
   1063 		vga_post_call(vga_posth);
   1064 		if (acpi_md_vesa_modenum != 0)
   1065 			vga_post_set_vbe(vga_posth, acpi_md_vesa_modenum);
   1066 	}
   1067 #endif
   1068 	genfb_restore_palette(&psc->sc_gen);
   1069 #endif
   1070 
   1071 	return true;
   1072 }
   1073 
   1074 static void
   1075 populate_fbinfo(device_t dev, prop_dictionary_t dict)
   1076 {
   1077 #if NWSDISPLAY > 0 && NGENFB > 0
   1078 	extern struct vcons_screen x86_genfb_console_screen;
   1079 	struct rasops_info *ri = &x86_genfb_console_screen.scr_ri;
   1080 #endif
   1081 	const void *fbptr = lookup_bootinfo(BTINFO_FRAMEBUFFER);
   1082 	struct btinfo_framebuffer fbinfo;
   1083 
   1084 	if (fbptr == NULL)
   1085 		return;
   1086 
   1087 	memcpy(&fbinfo, fbptr, sizeof(fbinfo));
   1088 
   1089 	if (fbinfo.physaddr != 0) {
   1090 		prop_dictionary_set_uint32(dict, "width", fbinfo.width);
   1091 		prop_dictionary_set_uint32(dict, "height", fbinfo.height);
   1092 		prop_dictionary_set_uint8(dict, "depth", fbinfo.depth);
   1093 		prop_dictionary_set_uint16(dict, "linebytes", fbinfo.stride);
   1094 
   1095 		prop_dictionary_set_uint64(dict, "address", fbinfo.physaddr);
   1096 #if NWSDISPLAY > 0 && NGENFB > 0
   1097 		if (ri->ri_bits != NULL) {
   1098 			prop_dictionary_set_uint64(dict, "virtual_address",
   1099 			    ri->ri_hwbits != NULL ?
   1100 			    (vaddr_t)ri->ri_hworigbits :
   1101 			    (vaddr_t)ri->ri_origbits);
   1102 		}
   1103 #endif
   1104 	}
   1105 #if notyet
   1106 	prop_dictionary_set_bool(dict, "splash",
   1107 	    (fbinfo.flags & BI_FB_SPLASH) != 0);
   1108 #endif
   1109 	if (fbinfo.depth == 8) {
   1110 		gfb_cb.gcc_cookie = NULL;
   1111 		gfb_cb.gcc_set_mapreg = x86_genfb_set_mapreg;
   1112 		prop_dictionary_set_uint64(dict, "cmap_callback",
   1113 		    (uint64_t)(uintptr_t)&gfb_cb);
   1114 	}
   1115 	if (fbinfo.physaddr != 0) {
   1116 		mode_cb.gmc_setmode = x86_genfb_setmode;
   1117 		prop_dictionary_set_uint64(dict, "mode_callback",
   1118 		    (uint64_t)(uintptr_t)&mode_cb);
   1119 	}
   1120 
   1121 #if NWSDISPLAY > 0 && NGENFB > 0
   1122 	if (device_is_a(dev, "genfb")) {
   1123 		prop_dictionary_set_bool(dict, "enable_shadowfb",
   1124 		    ri->ri_hwbits != NULL);
   1125 
   1126 		x86_genfb_set_console_dev(dev);
   1127 #ifdef DDB
   1128 		db_trap_callback = x86_genfb_ddb_trap_callback;
   1129 #endif
   1130 	}
   1131 #endif
   1132 }
   1133 
   1134 device_t
   1135 device_pci_register(device_t dev, void *aux)
   1136 {
   1137 	device_t parent = device_parent(dev);
   1138 
   1139 	device_pci_props_register(dev, aux);
   1140 
   1141 	/*
   1142 	 * Handle network interfaces here, the attachment information is
   1143 	 * not available driver-independently later.
   1144 	 *
   1145 	 * For disks, there is nothing useful available at attach time.
   1146 	 */
   1147 	if (device_class(dev) == DV_IFNET) {
   1148 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
   1149 		if (bin == NULL)
   1150 			return NULL;
   1151 
   1152 		/*
   1153 		 * We don't check the driver name against the device name
   1154 		 * passed by the boot ROM.  The ROM should stay usable if
   1155 		 * the driver becomes obsolete.  The physical attachment
   1156 		 * information (checked below) must be sufficient to
   1157 		 * identify the device.
   1158 		 */
   1159 		if (bin->bus == BI_BUS_PCI && device_is_a(parent, "pci")) {
   1160 			struct pci_attach_args *paa = aux;
   1161 			int b, d, f;
   1162 
   1163 			/*
   1164 			 * Calculate BIOS representation of:
   1165 			 *
   1166 			 *	<bus,device,function>
   1167 			 *
   1168 			 * and compare.
   1169 			 */
   1170 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
   1171 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
   1172 				return dev;
   1173 
   1174 #ifndef XENPV
   1175 			/*
   1176 			 * efiboot reports parent ppb bus/device/function.
   1177 			 */
   1178 			device_t grand = device_parent(parent);
   1179 			if (efi_probe() && grand && device_is_a(grand, "ppb")) {
   1180 				struct ppb_softc *ppb_sc = device_private(grand);
   1181 				pci_decompose_tag(ppb_sc->sc_pc, ppb_sc->sc_tag,
   1182 				    &b, &d, &f);
   1183 				if (bin->addr.tag == ((b << 8) | (d << 3) | f))
   1184 					return dev;
   1185 			}
   1186 #endif
   1187 		}
   1188 	}
   1189 	if (parent && device_is_a(parent, "pci") &&
   1190 	    x86_found_console == false) {
   1191 		struct pci_attach_args *pa = aux;
   1192 
   1193 		if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY) {
   1194 			prop_dictionary_t dict = device_properties(dev);
   1195 			/*
   1196 			 * framebuffer drivers other than genfb can work
   1197 			 * without the address property
   1198 			 */
   1199 			populate_fbinfo(dev, dict);
   1200 
   1201 #if 1 && NWSDISPLAY > 0 && NGENFB > 0
   1202 			/* XXX */
   1203 			if (device_is_a(dev, "genfb")) {
   1204 				prop_dictionary_set_bool(dict, "is_console",
   1205 				    genfb_is_console());
   1206 			} else
   1207 #endif
   1208 			prop_dictionary_set_bool(dict, "is_console", true);
   1209 
   1210 			prop_dictionary_set_bool(dict, "clear-screen", false);
   1211 #if NWSDISPLAY > 0 && NGENFB > 0
   1212 			extern struct vcons_screen x86_genfb_console_screen;
   1213 			prop_dictionary_set_uint16(dict, "cursor-row",
   1214 			    x86_genfb_console_screen.scr_ri.ri_crow);
   1215 #endif
   1216 #if notyet
   1217 			prop_dictionary_set_bool(dict, "splash",
   1218 			    (fbinfo->flags & BI_FB_SPLASH) != 0);
   1219 #endif
   1220 			pmf_cb.gpc_suspend = x86_genfb_suspend;
   1221 			pmf_cb.gpc_resume = x86_genfb_resume;
   1222 			prop_dictionary_set_uint64(dict,
   1223 			    "pmf_callback", (uint64_t)(uintptr_t)&pmf_cb);
   1224 #ifdef VGA_POST
   1225 			vga_posth = vga_post_init(pa->pa_bus, pa->pa_device,
   1226 			    pa->pa_function);
   1227 #endif
   1228 			x86_found_console = true;
   1229 			return NULL;
   1230 		}
   1231 	}
   1232 	return NULL;
   1233 }
   1234 
   1235 #ifndef PUC_CNBUS
   1236 #define PUC_CNBUS 0
   1237 #endif
   1238 
   1239 #if NCOM > 0
   1240 int
   1241 cpu_puc_cnprobe(struct consdev *cn, struct pci_attach_args *pa)
   1242 {
   1243 	pci_mode_detect();
   1244 	pa->pa_iot = x86_bus_space_io;
   1245 	pa->pa_memt = x86_bus_space_mem;
   1246 	pa->pa_pc = 0;
   1247 	pa->pa_tag = pci_make_tag(0, PUC_CNBUS, pci_bus_maxdevs(NULL, 0) - 1,
   1248 				  0);
   1249 
   1250 	return 0;
   1251 }
   1252 #endif
   1253