Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.55
      1  1.55  jakllsch /*	$NetBSD: pci_machdep.c,v 1.55 2012/02/28 18:14:47 jakllsch Exp $	*/
      2   1.1      fvdl 
      3   1.1      fvdl /*-
      4   1.1      fvdl  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5   1.1      fvdl  * All rights reserved.
      6   1.1      fvdl  *
      7   1.1      fvdl  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      fvdl  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1      fvdl  * NASA Ames Research Center.
     10   1.1      fvdl  *
     11   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     12   1.1      fvdl  * modification, are permitted provided that the following conditions
     13   1.1      fvdl  * are met:
     14   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     15   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     16   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     18   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     19   1.1      fvdl  *
     20   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1      fvdl  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1      fvdl  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1      fvdl  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1      fvdl  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1      fvdl  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1      fvdl  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1      fvdl  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1      fvdl  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1      fvdl  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1      fvdl  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1      fvdl  */
     32   1.1      fvdl 
     33   1.1      fvdl /*
     34   1.1      fvdl  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     35   1.1      fvdl  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
     36   1.1      fvdl  *
     37   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     38   1.1      fvdl  * modification, are permitted provided that the following conditions
     39   1.1      fvdl  * are met:
     40   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     41   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     42   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     44   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     45   1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     46   1.1      fvdl  *    must display the following acknowledgement:
     47   1.1      fvdl  *	This product includes software developed by Charles M. Hannum.
     48   1.1      fvdl  * 4. The name of the author may not be used to endorse or promote products
     49   1.1      fvdl  *    derived from this software without specific prior written permission.
     50   1.1      fvdl  *
     51   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52   1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53   1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54   1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55   1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56   1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57   1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58   1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59   1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60   1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61   1.1      fvdl  */
     62   1.1      fvdl 
     63   1.1      fvdl /*
     64   1.1      fvdl  * Machine-specific functions for PCI autoconfiguration.
     65   1.1      fvdl  *
     66   1.1      fvdl  * On PCs, there are two methods of generating PCI configuration cycles.
     67   1.1      fvdl  * We try to detect the appropriate mechanism for this machine and set
     68   1.1      fvdl  * up a few function pointers to access the correct method directly.
     69   1.1      fvdl  *
     70   1.1      fvdl  * The configuration method can be hard-coded in the config file by
     71   1.1      fvdl  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
     72  1.55  jakllsch  * as defined in section 3.6.4.1, `Generating Configuration Cycles'.
     73   1.1      fvdl  */
     74   1.1      fvdl 
     75   1.1      fvdl #include <sys/cdefs.h>
     76  1.55  jakllsch __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.55 2012/02/28 18:14:47 jakllsch Exp $");
     77   1.1      fvdl 
     78   1.1      fvdl #include <sys/types.h>
     79   1.1      fvdl #include <sys/param.h>
     80   1.1      fvdl #include <sys/time.h>
     81   1.1      fvdl #include <sys/systm.h>
     82   1.1      fvdl #include <sys/errno.h>
     83   1.1      fvdl #include <sys/device.h>
     84  1.29        ad #include <sys/bus.h>
     85  1.42    dyoung #include <sys/cpu.h>
     86  1.43    dyoung #include <sys/kmem.h>
     87   1.1      fvdl 
     88   1.1      fvdl #include <uvm/uvm_extern.h>
     89   1.1      fvdl 
     90  1.10      yamt #include <machine/bus_private.h>
     91   1.1      fvdl 
     92   1.1      fvdl #include <machine/pio.h>
     93  1.30        ad #include <machine/lock.h>
     94   1.1      fvdl 
     95   1.3      fvdl #include <dev/isa/isareg.h>
     96   1.1      fvdl #include <dev/isa/isavar.h>
     97   1.1      fvdl #include <dev/pci/pcivar.h>
     98   1.1      fvdl #include <dev/pci/pcireg.h>
     99  1.43    dyoung #include <dev/pci/pccbbreg.h>
    100   1.1      fvdl #include <dev/pci/pcidevs.h>
    101  1.52    dyoung #include <dev/pci/genfb_pcivar.h>
    102  1.52    dyoung 
    103  1.52    dyoung #include <dev/wsfb/genfbvar.h>
    104  1.52    dyoung #include <arch/x86/include/genfb_machdep.h>
    105  1.52    dyoung #include <dev/ic/vgareg.h>
    106   1.1      fvdl 
    107  1.37  jmcneill #include "acpica.h"
    108  1.52    dyoung #include "genfb.h"
    109  1.52    dyoung #include "isa.h"
    110  1.52    dyoung #include "opt_acpi.h"
    111  1.52    dyoung #include "opt_ddb.h"
    112  1.14    bouyer #include "opt_mpbios.h"
    113  1.52    dyoung #include "opt_vga.h"
    114  1.52    dyoung #include "pci.h"
    115  1.52    dyoung #include "wsdisplay.h"
    116  1.52    dyoung 
    117  1.52    dyoung #ifdef DDB
    118  1.52    dyoung #include <machine/db_machdep.h>
    119  1.52    dyoung #include <ddb/db_sym.h>
    120  1.52    dyoung #include <ddb/db_extern.h>
    121  1.52    dyoung #endif
    122  1.52    dyoung 
    123  1.52    dyoung #ifdef VGA_POST
    124  1.52    dyoung #include <x86/vga_post.h>
    125  1.52    dyoung #endif
    126  1.52    dyoung 
    127  1.52    dyoung #include <machine/autoconf.h>
    128  1.52    dyoung #include <machine/bootinfo.h>
    129  1.14    bouyer 
    130  1.14    bouyer #ifdef MPBIOS
    131  1.14    bouyer #include <machine/mpbiosvar.h>
    132  1.14    bouyer #endif
    133  1.14    bouyer 
    134  1.37  jmcneill #if NACPICA > 0
    135  1.14    bouyer #include <machine/mpacpi.h>
    136  1.14    bouyer #endif
    137  1.14    bouyer 
    138  1.16  christos #include <machine/mpconfig.h>
    139  1.16  christos 
    140   1.1      fvdl #include "opt_pci_conf_mode.h"
    141   1.1      fvdl 
    142  1.38    dyoung #ifdef PCI_CONF_MODE
    143  1.38    dyoung #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
    144  1.38    dyoung static int pci_mode = PCI_CONF_MODE;
    145  1.38    dyoung #else
    146  1.38    dyoung #error Invalid PCI configuration mode.
    147  1.38    dyoung #endif
    148  1.38    dyoung #else
    149  1.38    dyoung static int pci_mode = -1;
    150  1.38    dyoung #endif
    151   1.1      fvdl 
    152  1.42    dyoung struct pci_conf_lock {
    153  1.42    dyoung 	uint32_t cl_cpuno;	/* 0: unlocked
    154  1.42    dyoung 				 * 1 + n: locked by CPU n (0 <= n)
    155  1.42    dyoung 				 */
    156  1.42    dyoung 	uint32_t cl_sel;	/* the address that's being read. */
    157  1.42    dyoung };
    158  1.42    dyoung 
    159  1.42    dyoung static void pci_conf_unlock(struct pci_conf_lock *);
    160  1.42    dyoung static uint32_t pci_conf_selector(pcitag_t, int);
    161  1.42    dyoung static unsigned int pci_conf_port(pcitag_t, int);
    162  1.42    dyoung static void pci_conf_select(uint32_t);
    163  1.42    dyoung static void pci_conf_lock(struct pci_conf_lock *, uint32_t);
    164  1.11    sekiya static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
    165  1.11    sekiya struct pci_bridge_hook_arg {
    166  1.55  jakllsch 	void (*func)(pci_chipset_tag_t, pcitag_t, void *);
    167  1.55  jakllsch 	void *arg;
    168  1.55  jakllsch };
    169  1.11    sekiya 
    170   1.1      fvdl #define	PCI_MODE1_ENABLE	0x80000000UL
    171   1.1      fvdl #define	PCI_MODE1_ADDRESS_REG	0x0cf8
    172   1.1      fvdl #define	PCI_MODE1_DATA_REG	0x0cfc
    173   1.1      fvdl 
    174   1.1      fvdl #define	PCI_MODE2_ENABLE_REG	0x0cf8
    175   1.1      fvdl #define	PCI_MODE2_FORWARD_REG	0x0cfa
    176   1.1      fvdl 
    177   1.1      fvdl #define _m1tag(b, d, f) \
    178   1.1      fvdl 	(PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8))
    179   1.1      fvdl #define _qe(bus, dev, fcn, vend, prod) \
    180   1.1      fvdl 	{_m1tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)}
    181   1.1      fvdl struct {
    182  1.33    cegger 	uint32_t tag;
    183   1.1      fvdl 	pcireg_t id;
    184   1.1      fvdl } pcim1_quirk_tbl[] = {
    185   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX1),
    186   1.1      fvdl 	/* XXX Triflex2 not tested */
    187   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX2),
    188   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX4),
    189   1.1      fvdl 	/* Triton needed for Connectix Virtual PC */
    190   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX),
    191   1.1      fvdl 	/* Connectix Virtual PC 5 has a 440BX */
    192   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP),
    193  1.15     soren 	/* Parallels Desktop for Mac */
    194  1.15     soren 	_qe(0, 2, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_VIDEO),
    195  1.15     soren 	_qe(0, 3, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_TOOLS),
    196  1.36  drochner 	/* SIS 740 */
    197  1.36  drochner 	_qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_740),
    198  1.12  christos 	/* SIS 741 */
    199  1.12  christos 	_qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_741),
    200  1.54   tsutsui 	/* VIA Technologies VX900 */
    201  1.54   tsutsui 	_qe(0, 0, 0, PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX900_HB),
    202   1.1      fvdl 	{0, 0xffffffff} /* patchable */
    203   1.1      fvdl };
    204   1.1      fvdl #undef _m1tag
    205   1.1      fvdl #undef _id
    206   1.1      fvdl #undef _qe
    207   1.1      fvdl 
    208   1.1      fvdl /*
    209   1.1      fvdl  * PCI doesn't have any special needs; just use the generic versions
    210   1.1      fvdl  * of these functions.
    211   1.1      fvdl  */
    212   1.1      fvdl struct x86_bus_dma_tag pci_bus_dma_tag = {
    213  1.46  christos 	._tag_needs_free	= 0,
    214   1.3      fvdl #if defined(_LP64) || defined(PAE)
    215  1.46  christos 	._bounce_thresh		= PCI32_DMA_BOUNCE_THRESHOLD,
    216  1.46  christos 	._bounce_alloc_lo	= ISA_DMA_BOUNCE_THRESHOLD,
    217  1.46  christos 	._bounce_alloc_hi	= PCI32_DMA_BOUNCE_THRESHOLD,
    218   1.3      fvdl #else
    219  1.46  christos 	._bounce_thresh		= 0,
    220  1.46  christos 	._bounce_alloc_lo	= 0,
    221  1.46  christos 	._bounce_alloc_hi	= 0,
    222  1.46  christos #endif
    223  1.46  christos 	._may_bounce		= NULL,
    224   1.1      fvdl };
    225   1.5      fvdl 
    226   1.5      fvdl #ifdef _LP64
    227   1.5      fvdl struct x86_bus_dma_tag pci_bus_dma64_tag = {
    228  1.46  christos 	._tag_needs_free	= 0,
    229  1.46  christos 	._bounce_thresh		= 0,
    230  1.46  christos 	._bounce_alloc_lo	= 0,
    231  1.46  christos 	._bounce_alloc_hi	= 0,
    232  1.46  christos 	._may_bounce		= NULL,
    233   1.5      fvdl };
    234   1.5      fvdl #endif
    235   1.1      fvdl 
    236  1.42    dyoung static struct pci_conf_lock cl0 = {
    237  1.42    dyoung 	  .cl_cpuno = 0UL
    238  1.42    dyoung 	, .cl_sel = 0UL
    239  1.42    dyoung };
    240  1.42    dyoung 
    241  1.42    dyoung static struct pci_conf_lock * const cl = &cl0;
    242  1.42    dyoung 
    243  1.52    dyoung #if NGENFB > 0 && NACPICA > 0 && defined(VGA_POST)
    244  1.52    dyoung extern int acpi_md_vbios_reset;
    245  1.52    dyoung extern int acpi_md_vesa_modenum;
    246  1.52    dyoung #endif
    247  1.52    dyoung 
    248  1.52    dyoung static struct genfb_colormap_callback gfb_cb;
    249  1.52    dyoung static struct genfb_pmf_callback pmf_cb;
    250  1.52    dyoung static struct genfb_mode_callback mode_cb;
    251  1.52    dyoung #ifdef VGA_POST
    252  1.52    dyoung static struct vga_post *vga_posth = NULL;
    253  1.52    dyoung #endif
    254  1.52    dyoung 
    255  1.42    dyoung static void
    256  1.42    dyoung pci_conf_lock(struct pci_conf_lock *ocl, uint32_t sel)
    257  1.42    dyoung {
    258  1.42    dyoung 	uint32_t cpuno;
    259  1.42    dyoung 
    260  1.42    dyoung 	KASSERT(sel != 0);
    261  1.42    dyoung 
    262  1.42    dyoung 	kpreempt_disable();
    263  1.42    dyoung 	cpuno = cpu_number() + 1;
    264  1.42    dyoung 	/* If the kernel enters pci_conf_lock() through an interrupt
    265  1.42    dyoung 	 * handler, then the CPU may already hold the lock.
    266  1.42    dyoung 	 *
    267  1.42    dyoung 	 * If the CPU does not already hold the lock, spin until
    268  1.42    dyoung 	 * we can acquire it.
    269  1.42    dyoung 	 */
    270  1.42    dyoung 	if (cpuno == cl->cl_cpuno) {
    271  1.42    dyoung 		ocl->cl_cpuno = cpuno;
    272  1.42    dyoung 	} else {
    273  1.44    dyoung 		u_int spins;
    274  1.44    dyoung 
    275  1.42    dyoung 		ocl->cl_cpuno = 0;
    276  1.44    dyoung 
    277  1.44    dyoung 		spins = SPINLOCK_BACKOFF_MIN;
    278  1.44    dyoung 		while (atomic_cas_32(&cl->cl_cpuno, 0, cpuno) != 0) {
    279  1.44    dyoung 			SPINLOCK_BACKOFF(spins);
    280  1.44    dyoung #ifdef LOCKDEBUG
    281  1.44    dyoung 			if (SPINLOCK_SPINOUT(spins)) {
    282  1.44    dyoung 				panic("%s: cpu %" PRId32
    283  1.44    dyoung 				    " spun out waiting for cpu %" PRId32,
    284  1.44    dyoung 				    __func__, cpuno, cl->cl_cpuno);
    285  1.44    dyoung 			}
    286  1.44    dyoung #endif	/* LOCKDEBUG */
    287  1.44    dyoung 		}
    288  1.42    dyoung 	}
    289  1.42    dyoung 
    290  1.42    dyoung 	/* Only one CPU can be here, so an interlocked atomic_swap(3)
    291  1.42    dyoung 	 * is not necessary.
    292  1.42    dyoung 	 *
    293  1.42    dyoung 	 * Evaluating atomic_cas_32_ni()'s argument, cl->cl_sel,
    294  1.42    dyoung 	 * and applying atomic_cas_32_ni() is not an atomic operation,
    295  1.42    dyoung 	 * however, any interrupt that, in the middle of the
    296  1.42    dyoung 	 * operation, modifies cl->cl_sel, will also restore
    297  1.42    dyoung 	 * cl->cl_sel.  So cl->cl_sel will have the same value when
    298  1.42    dyoung 	 * we apply atomic_cas_32_ni() as when we evaluated it,
    299  1.42    dyoung 	 * before.
    300  1.42    dyoung 	 */
    301  1.42    dyoung 	ocl->cl_sel = atomic_cas_32_ni(&cl->cl_sel, cl->cl_sel, sel);
    302  1.42    dyoung 	pci_conf_select(sel);
    303  1.42    dyoung }
    304  1.42    dyoung 
    305  1.42    dyoung static void
    306  1.42    dyoung pci_conf_unlock(struct pci_conf_lock *ocl)
    307  1.42    dyoung {
    308  1.42    dyoung 	uint32_t sel;
    309  1.42    dyoung 
    310  1.42    dyoung 	sel = atomic_cas_32_ni(&cl->cl_sel, cl->cl_sel, ocl->cl_sel);
    311  1.42    dyoung 	pci_conf_select(ocl->cl_sel);
    312  1.42    dyoung 	if (ocl->cl_cpuno != cl->cl_cpuno)
    313  1.42    dyoung 		atomic_cas_32(&cl->cl_cpuno, cl->cl_cpuno, ocl->cl_cpuno);
    314  1.42    dyoung 	kpreempt_enable();
    315  1.42    dyoung }
    316  1.42    dyoung 
    317  1.39    dyoung static uint32_t
    318  1.39    dyoung pci_conf_selector(pcitag_t tag, int reg)
    319  1.39    dyoung {
    320  1.39    dyoung 	static const pcitag_t mode2_mask = {
    321  1.39    dyoung 		.mode2 = {
    322  1.39    dyoung 			  .enable = 0xff
    323  1.39    dyoung 			, .forward = 0xff
    324  1.39    dyoung 		}
    325  1.39    dyoung 	};
    326  1.39    dyoung 
    327  1.39    dyoung 	switch (pci_mode) {
    328  1.39    dyoung 	case 1:
    329  1.39    dyoung 		return tag.mode1 | reg;
    330  1.39    dyoung 	case 2:
    331  1.39    dyoung 		return tag.mode1 & mode2_mask.mode1;
    332  1.39    dyoung 	default:
    333  1.39    dyoung 		panic("%s: mode not configured", __func__);
    334  1.39    dyoung 	}
    335  1.39    dyoung }
    336  1.39    dyoung 
    337  1.39    dyoung static unsigned int
    338  1.39    dyoung pci_conf_port(pcitag_t tag, int reg)
    339  1.39    dyoung {
    340  1.39    dyoung 	switch (pci_mode) {
    341  1.39    dyoung 	case 1:
    342  1.39    dyoung 		return PCI_MODE1_DATA_REG;
    343  1.39    dyoung 	case 2:
    344  1.39    dyoung 		return tag.mode2.port | reg;
    345  1.39    dyoung 	default:
    346  1.39    dyoung 		panic("%s: mode not configured", __func__);
    347  1.39    dyoung 	}
    348  1.39    dyoung }
    349  1.39    dyoung 
    350  1.39    dyoung static void
    351  1.42    dyoung pci_conf_select(uint32_t sel)
    352  1.39    dyoung {
    353  1.39    dyoung 	pcitag_t tag;
    354  1.39    dyoung 
    355  1.39    dyoung 	switch (pci_mode) {
    356  1.39    dyoung 	case 1:
    357  1.42    dyoung 		outl(PCI_MODE1_ADDRESS_REG, sel);
    358  1.39    dyoung 		return;
    359  1.39    dyoung 	case 2:
    360  1.42    dyoung 		tag.mode1 = sel;
    361  1.39    dyoung 		outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
    362  1.39    dyoung 		if (tag.mode2.enable != 0)
    363  1.39    dyoung 			outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
    364  1.39    dyoung 		return;
    365  1.39    dyoung 	default:
    366  1.39    dyoung 		panic("%s: mode not configured", __func__);
    367  1.39    dyoung 	}
    368  1.39    dyoung }
    369  1.39    dyoung 
    370   1.1      fvdl void
    371  1.32    dyoung pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
    372   1.1      fvdl {
    373   1.1      fvdl 
    374   1.1      fvdl 	if (pba->pba_bus == 0)
    375  1.26       mjf 		aprint_normal(": configuration mode %d", pci_mode);
    376   1.4      fvdl #ifdef MPBIOS
    377   1.4      fvdl 	mpbios_pci_attach_hook(parent, self, pba);
    378   1.4      fvdl #endif
    379  1.37  jmcneill #if NACPICA > 0
    380   1.4      fvdl 	mpacpi_pci_attach_hook(parent, self, pba);
    381   1.4      fvdl #endif
    382   1.1      fvdl }
    383   1.1      fvdl 
    384   1.1      fvdl int
    385  1.18  christos pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    386   1.1      fvdl {
    387   1.1      fvdl 	/*
    388   1.1      fvdl 	 * Bus number is irrelevant.  If Configuration Mechanism 2 is in
    389   1.1      fvdl 	 * use, can only have devices 0-15 on any bus.  If Configuration
    390   1.1      fvdl 	 * Mechanism 1 is in use, can have devices 0-32 (i.e. the `normal'
    391   1.1      fvdl 	 * range).
    392   1.1      fvdl 	 */
    393   1.1      fvdl 	if (pci_mode == 2)
    394   1.1      fvdl 		return (16);
    395   1.1      fvdl 	else
    396   1.1      fvdl 		return (32);
    397   1.1      fvdl }
    398   1.1      fvdl 
    399   1.1      fvdl pcitag_t
    400  1.18  christos pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
    401   1.1      fvdl {
    402  1.47    dyoung 	pci_chipset_tag_t ipc;
    403   1.1      fvdl 	pcitag_t tag;
    404   1.1      fvdl 
    405  1.47    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    406  1.47    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_MAKE_TAG) == 0)
    407  1.47    dyoung 			continue;
    408  1.47    dyoung 		return (*ipc->pc_ov->ov_make_tag)(ipc->pc_ctx,
    409  1.47    dyoung 		    pc, bus, device, function);
    410  1.41    dyoung 	}
    411  1.40    dyoung 
    412   1.1      fvdl 	switch (pci_mode) {
    413   1.1      fvdl 	case 1:
    414  1.38    dyoung 		if (bus >= 256 || device >= 32 || function >= 8)
    415  1.39    dyoung 			panic("%s: bad request", __func__);
    416  1.38    dyoung 
    417  1.38    dyoung 		tag.mode1 = PCI_MODE1_ENABLE |
    418  1.38    dyoung 			    (bus << 16) | (device << 11) | (function << 8);
    419  1.38    dyoung 		return tag;
    420   1.1      fvdl 	case 2:
    421  1.38    dyoung 		if (bus >= 256 || device >= 16 || function >= 8)
    422  1.39    dyoung 			panic("%s: bad request", __func__);
    423  1.38    dyoung 
    424  1.38    dyoung 		tag.mode2.port = 0xc000 | (device << 8);
    425  1.38    dyoung 		tag.mode2.enable = 0xf0 | (function << 1);
    426  1.38    dyoung 		tag.mode2.forward = bus;
    427  1.38    dyoung 		return tag;
    428   1.1      fvdl 	default:
    429  1.39    dyoung 		panic("%s: mode not configured", __func__);
    430   1.1      fvdl 	}
    431   1.1      fvdl }
    432   1.1      fvdl 
    433   1.1      fvdl void
    434  1.18  christos pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
    435  1.17  christos     int *bp, int *dp, int *fp)
    436   1.1      fvdl {
    437  1.47    dyoung 	pci_chipset_tag_t ipc;
    438   1.1      fvdl 
    439  1.47    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    440  1.47    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_DECOMPOSE_TAG) == 0)
    441  1.47    dyoung 			continue;
    442  1.47    dyoung 		(*ipc->pc_ov->ov_decompose_tag)(ipc->pc_ctx,
    443  1.47    dyoung 		    pc, tag, bp, dp, fp);
    444  1.47    dyoung 		return;
    445  1.40    dyoung 	}
    446  1.40    dyoung 
    447   1.1      fvdl 	switch (pci_mode) {
    448   1.1      fvdl 	case 1:
    449  1.38    dyoung 		if (bp != NULL)
    450  1.38    dyoung 			*bp = (tag.mode1 >> 16) & 0xff;
    451  1.38    dyoung 		if (dp != NULL)
    452  1.38    dyoung 			*dp = (tag.mode1 >> 11) & 0x1f;
    453  1.38    dyoung 		if (fp != NULL)
    454  1.38    dyoung 			*fp = (tag.mode1 >> 8) & 0x7;
    455  1.38    dyoung 		return;
    456   1.1      fvdl 	case 2:
    457  1.38    dyoung 		if (bp != NULL)
    458  1.38    dyoung 			*bp = tag.mode2.forward & 0xff;
    459  1.38    dyoung 		if (dp != NULL)
    460  1.38    dyoung 			*dp = (tag.mode2.port >> 8) & 0xf;
    461  1.38    dyoung 		if (fp != NULL)
    462  1.38    dyoung 			*fp = (tag.mode2.enable >> 1) & 0x7;
    463  1.38    dyoung 		return;
    464   1.1      fvdl 	default:
    465  1.39    dyoung 		panic("%s: mode not configured", __func__);
    466   1.1      fvdl 	}
    467   1.1      fvdl }
    468   1.1      fvdl 
    469   1.1      fvdl pcireg_t
    470  1.43    dyoung pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
    471   1.1      fvdl {
    472  1.47    dyoung 	pci_chipset_tag_t ipc;
    473   1.1      fvdl 	pcireg_t data;
    474  1.42    dyoung 	struct pci_conf_lock ocl;
    475   1.1      fvdl 
    476  1.31    dyoung 	KASSERT((reg & 0x3) == 0);
    477  1.40    dyoung 
    478  1.47    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    479  1.47    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_CONF_READ) == 0)
    480  1.47    dyoung 			continue;
    481  1.47    dyoung 		return (*ipc->pc_ov->ov_conf_read)(ipc->pc_ctx, pc, tag, reg);
    482  1.41    dyoung 	}
    483  1.40    dyoung 
    484  1.42    dyoung 	pci_conf_lock(&ocl, pci_conf_selector(tag, reg));
    485  1.39    dyoung 	data = inl(pci_conf_port(tag, reg));
    486  1.42    dyoung 	pci_conf_unlock(&ocl);
    487  1.39    dyoung 	return data;
    488   1.1      fvdl }
    489   1.1      fvdl 
    490   1.1      fvdl void
    491  1.43    dyoung pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
    492   1.1      fvdl {
    493  1.47    dyoung 	pci_chipset_tag_t ipc;
    494  1.42    dyoung 	struct pci_conf_lock ocl;
    495   1.1      fvdl 
    496  1.31    dyoung 	KASSERT((reg & 0x3) == 0);
    497  1.40    dyoung 
    498  1.47    dyoung 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
    499  1.47    dyoung 		if ((ipc->pc_present & PCI_OVERRIDE_CONF_WRITE) == 0)
    500  1.47    dyoung 			continue;
    501  1.47    dyoung 		(*ipc->pc_ov->ov_conf_write)(ipc->pc_ctx, pc, tag, reg,
    502  1.47    dyoung 		    data);
    503  1.47    dyoung 		return;
    504  1.40    dyoung 	}
    505  1.40    dyoung 
    506  1.42    dyoung 	pci_conf_lock(&ocl, pci_conf_selector(tag, reg));
    507  1.39    dyoung 	outl(pci_conf_port(tag, reg), data);
    508  1.42    dyoung 	pci_conf_unlock(&ocl);
    509  1.38    dyoung }
    510   1.1      fvdl 
    511  1.38    dyoung void
    512  1.38    dyoung pci_mode_set(int mode)
    513  1.38    dyoung {
    514  1.38    dyoung 	KASSERT(pci_mode == -1 || pci_mode == mode);
    515   1.1      fvdl 
    516  1.38    dyoung 	pci_mode = mode;
    517   1.1      fvdl }
    518   1.1      fvdl 
    519   1.1      fvdl int
    520  1.33    cegger pci_mode_detect(void)
    521   1.1      fvdl {
    522  1.33    cegger 	uint32_t sav, val;
    523   1.1      fvdl 	int i;
    524   1.1      fvdl 	pcireg_t idreg;
    525   1.1      fvdl 
    526   1.1      fvdl 	if (pci_mode != -1)
    527   1.1      fvdl 		return pci_mode;
    528   1.1      fvdl 
    529   1.1      fvdl 	/*
    530   1.1      fvdl 	 * We try to divine which configuration mode the host bridge wants.
    531   1.1      fvdl 	 */
    532   1.1      fvdl 
    533   1.1      fvdl 	sav = inl(PCI_MODE1_ADDRESS_REG);
    534   1.1      fvdl 
    535   1.1      fvdl 	pci_mode = 1; /* assume this for now */
    536   1.1      fvdl 	/*
    537   1.1      fvdl 	 * catch some known buggy implementations of mode 1
    538   1.1      fvdl 	 */
    539  1.27    dyoung 	for (i = 0; i < __arraycount(pcim1_quirk_tbl); i++) {
    540   1.1      fvdl 		pcitag_t t;
    541   1.1      fvdl 
    542   1.1      fvdl 		if (!pcim1_quirk_tbl[i].tag)
    543   1.1      fvdl 			break;
    544   1.1      fvdl 		t.mode1 = pcim1_quirk_tbl[i].tag;
    545   1.1      fvdl 		idreg = pci_conf_read(0, t, PCI_ID_REG); /* needs "pci_mode" */
    546   1.1      fvdl 		if (idreg == pcim1_quirk_tbl[i].id) {
    547   1.1      fvdl #ifdef DEBUG
    548   1.1      fvdl 			printf("known mode 1 PCI chipset (%08x)\n",
    549   1.1      fvdl 			       idreg);
    550   1.1      fvdl #endif
    551   1.1      fvdl 			return (pci_mode);
    552   1.1      fvdl 		}
    553   1.1      fvdl 	}
    554   1.1      fvdl 
    555   1.1      fvdl 	/*
    556   1.1      fvdl 	 * Strong check for standard compliant mode 1:
    557   1.1      fvdl 	 * 1. bit 31 ("enable") can be set
    558   1.1      fvdl 	 * 2. byte/word access does not affect register
    559   1.1      fvdl 	 */
    560   1.1      fvdl 	outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE);
    561   1.1      fvdl 	outb(PCI_MODE1_ADDRESS_REG + 3, 0);
    562   1.1      fvdl 	outw(PCI_MODE1_ADDRESS_REG + 2, 0);
    563   1.1      fvdl 	val = inl(PCI_MODE1_ADDRESS_REG);
    564   1.1      fvdl 	if ((val & 0x80fffffc) != PCI_MODE1_ENABLE) {
    565   1.1      fvdl #ifdef DEBUG
    566   1.1      fvdl 		printf("pci_mode_detect: mode 1 enable failed (%x)\n",
    567   1.1      fvdl 		       val);
    568   1.1      fvdl #endif
    569   1.1      fvdl 		goto not1;
    570   1.1      fvdl 	}
    571   1.1      fvdl 	outl(PCI_MODE1_ADDRESS_REG, 0);
    572   1.1      fvdl 	val = inl(PCI_MODE1_ADDRESS_REG);
    573   1.1      fvdl 	if ((val & 0x80fffffc) != 0)
    574   1.1      fvdl 		goto not1;
    575   1.1      fvdl 	return (pci_mode);
    576   1.1      fvdl not1:
    577   1.1      fvdl 	outl(PCI_MODE1_ADDRESS_REG, sav);
    578   1.1      fvdl 
    579   1.1      fvdl 	/*
    580   1.1      fvdl 	 * This mode 2 check is quite weak (and known to give false
    581   1.1      fvdl 	 * positives on some Compaq machines).
    582   1.1      fvdl 	 * However, this doesn't matter, because this is the
    583   1.1      fvdl 	 * last test, and simply no PCI devices will be found if
    584   1.1      fvdl 	 * this happens.
    585   1.1      fvdl 	 */
    586   1.1      fvdl 	outb(PCI_MODE2_ENABLE_REG, 0);
    587   1.1      fvdl 	outb(PCI_MODE2_FORWARD_REG, 0);
    588   1.1      fvdl 	if (inb(PCI_MODE2_ENABLE_REG) != 0 ||
    589   1.1      fvdl 	    inb(PCI_MODE2_FORWARD_REG) != 0)
    590   1.1      fvdl 		goto not2;
    591   1.1      fvdl 	return (pci_mode = 2);
    592   1.1      fvdl not2:
    593   1.1      fvdl 
    594   1.1      fvdl 	return (pci_mode = 0);
    595   1.1      fvdl }
    596   1.1      fvdl 
    597   1.1      fvdl /*
    598   1.1      fvdl  * Determine which flags should be passed to the primary PCI bus's
    599   1.1      fvdl  * autoconfiguration node.  We use this to detect broken chipsets
    600   1.1      fvdl  * which cannot safely use memory-mapped device access.
    601   1.1      fvdl  */
    602   1.1      fvdl int
    603  1.35    cegger pci_bus_flags(void)
    604   1.1      fvdl {
    605  1.45    dyoung 	int rval = PCI_FLAGS_IO_OKAY | PCI_FLAGS_MEM_OKAY |
    606   1.1      fvdl 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
    607   1.1      fvdl 	int device, maxndevs;
    608   1.1      fvdl 	pcitag_t tag;
    609   1.1      fvdl 	pcireg_t id;
    610   1.1      fvdl 
    611   1.1      fvdl 	maxndevs = pci_bus_maxdevs(NULL, 0);
    612   1.1      fvdl 
    613   1.1      fvdl 	for (device = 0; device < maxndevs; device++) {
    614   1.1      fvdl 		tag = pci_make_tag(NULL, 0, device, 0);
    615   1.1      fvdl 		id = pci_conf_read(NULL, tag, PCI_ID_REG);
    616   1.1      fvdl 
    617   1.1      fvdl 		/* Invalid vendor ID value? */
    618   1.1      fvdl 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    619   1.1      fvdl 			continue;
    620   1.1      fvdl 		/* XXX Not invalid, but we've done this ~forever. */
    621   1.1      fvdl 		if (PCI_VENDOR(id) == 0)
    622   1.1      fvdl 			continue;
    623   1.1      fvdl 
    624   1.1      fvdl 		switch (PCI_VENDOR(id)) {
    625   1.1      fvdl 		case PCI_VENDOR_SIS:
    626   1.1      fvdl 			switch (PCI_PRODUCT(id)) {
    627   1.1      fvdl 			case PCI_PRODUCT_SIS_85C496:
    628   1.1      fvdl 				goto disable_mem;
    629   1.1      fvdl 			}
    630   1.1      fvdl 			break;
    631   1.1      fvdl 		}
    632   1.1      fvdl 	}
    633   1.1      fvdl 
    634   1.1      fvdl 	return (rval);
    635   1.1      fvdl 
    636   1.1      fvdl  disable_mem:
    637   1.1      fvdl 	printf("Warning: broken PCI-Host bridge detected; "
    638   1.1      fvdl 	    "disabling memory-mapped access\n");
    639  1.45    dyoung 	rval &= ~(PCI_FLAGS_MEM_OKAY|PCI_FLAGS_MRL_OKAY|PCI_FLAGS_MRM_OKAY|
    640   1.1      fvdl 	    PCI_FLAGS_MWI_OKAY);
    641   1.1      fvdl 	return (rval);
    642   1.1      fvdl }
    643  1.11    sekiya 
    644  1.11    sekiya void
    645  1.11    sekiya pci_device_foreach(pci_chipset_tag_t pc, int maxbus,
    646  1.11    sekiya 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
    647  1.11    sekiya {
    648  1.11    sekiya 	pci_device_foreach_min(pc, 0, maxbus, func, context);
    649  1.11    sekiya }
    650  1.11    sekiya 
    651  1.11    sekiya void
    652  1.11    sekiya pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus,
    653  1.11    sekiya 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
    654  1.11    sekiya {
    655  1.11    sekiya 	const struct pci_quirkdata *qd;
    656  1.11    sekiya 	int bus, device, function, maxdevs, nfuncs;
    657  1.11    sekiya 	pcireg_t id, bhlcr;
    658  1.11    sekiya 	pcitag_t tag;
    659  1.11    sekiya 
    660  1.11    sekiya 	for (bus = minbus; bus <= maxbus; bus++) {
    661  1.11    sekiya 		maxdevs = pci_bus_maxdevs(pc, bus);
    662  1.11    sekiya 		for (device = 0; device < maxdevs; device++) {
    663  1.11    sekiya 			tag = pci_make_tag(pc, bus, device, 0);
    664  1.11    sekiya 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    665  1.11    sekiya 
    666  1.11    sekiya 			/* Invalid vendor ID value? */
    667  1.11    sekiya 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    668  1.11    sekiya 				continue;
    669  1.11    sekiya 			/* XXX Not invalid, but we've done this ~forever. */
    670  1.11    sekiya 			if (PCI_VENDOR(id) == 0)
    671  1.11    sekiya 				continue;
    672  1.11    sekiya 
    673  1.11    sekiya 			qd = pci_lookup_quirkdata(PCI_VENDOR(id),
    674  1.11    sekiya 				PCI_PRODUCT(id));
    675  1.11    sekiya 
    676  1.11    sekiya 			bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    677  1.11    sekiya 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    678  1.11    sekiya 			     (qd != NULL &&
    679  1.55  jakllsch 			     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    680  1.11    sekiya 				nfuncs = 8;
    681  1.11    sekiya 			else
    682  1.11    sekiya 				nfuncs = 1;
    683  1.11    sekiya 
    684  1.11    sekiya 			for (function = 0; function < nfuncs; function++) {
    685  1.11    sekiya 				tag = pci_make_tag(pc, bus, device, function);
    686  1.11    sekiya 				id = pci_conf_read(pc, tag, PCI_ID_REG);
    687  1.11    sekiya 
    688  1.11    sekiya 				/* Invalid vendor ID value? */
    689  1.11    sekiya 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    690  1.11    sekiya 					continue;
    691  1.11    sekiya 				/*
    692  1.11    sekiya 				 * XXX Not invalid, but we've done this
    693  1.11    sekiya 				 * ~forever.
    694  1.11    sekiya 				 */
    695  1.11    sekiya 				if (PCI_VENDOR(id) == 0)
    696  1.11    sekiya 					continue;
    697  1.11    sekiya 				(*func)(pc, tag, context);
    698  1.11    sekiya 			}
    699  1.11    sekiya 		}
    700  1.11    sekiya 	}
    701  1.11    sekiya }
    702  1.11    sekiya 
    703  1.11    sekiya void
    704  1.11    sekiya pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
    705  1.11    sekiya 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
    706  1.11    sekiya {
    707  1.11    sekiya 	struct pci_bridge_hook_arg bridge_hook;
    708  1.11    sekiya 
    709  1.11    sekiya 	bridge_hook.func = func;
    710  1.55  jakllsch 	bridge_hook.arg = ctx;
    711  1.11    sekiya 
    712  1.11    sekiya 	pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
    713  1.55  jakllsch 		&bridge_hook);
    714  1.11    sekiya }
    715  1.11    sekiya 
    716  1.11    sekiya static void
    717  1.11    sekiya pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
    718  1.11    sekiya {
    719  1.11    sekiya 	struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
    720  1.11    sekiya 	pcireg_t reg;
    721  1.11    sekiya 
    722  1.11    sekiya 	reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
    723  1.11    sekiya 	if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
    724  1.55  jakllsch 	    (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
    725  1.11    sekiya 		PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
    726  1.11    sekiya 		(*bridge_hook->func)(pc, tag, bridge_hook->arg);
    727  1.11    sekiya 	}
    728  1.11    sekiya }
    729  1.43    dyoung 
    730  1.43    dyoung static const void *
    731  1.43    dyoung bit_to_function_pointer(const struct pci_overrides *ov, uint64_t bit)
    732  1.43    dyoung {
    733  1.43    dyoung 	switch (bit) {
    734  1.43    dyoung 	case PCI_OVERRIDE_CONF_READ:
    735  1.43    dyoung 		return ov->ov_conf_read;
    736  1.43    dyoung 	case PCI_OVERRIDE_CONF_WRITE:
    737  1.43    dyoung 		return ov->ov_conf_write;
    738  1.43    dyoung 	case PCI_OVERRIDE_INTR_MAP:
    739  1.43    dyoung 		return ov->ov_intr_map;
    740  1.43    dyoung 	case PCI_OVERRIDE_INTR_STRING:
    741  1.43    dyoung 		return ov->ov_intr_string;
    742  1.43    dyoung 	case PCI_OVERRIDE_INTR_EVCNT:
    743  1.43    dyoung 		return ov->ov_intr_evcnt;
    744  1.43    dyoung 	case PCI_OVERRIDE_INTR_ESTABLISH:
    745  1.43    dyoung 		return ov->ov_intr_establish;
    746  1.43    dyoung 	case PCI_OVERRIDE_INTR_DISESTABLISH:
    747  1.43    dyoung 		return ov->ov_intr_disestablish;
    748  1.43    dyoung 	case PCI_OVERRIDE_MAKE_TAG:
    749  1.43    dyoung 		return ov->ov_make_tag;
    750  1.43    dyoung 	case PCI_OVERRIDE_DECOMPOSE_TAG:
    751  1.43    dyoung 		return ov->ov_decompose_tag;
    752  1.43    dyoung 	default:
    753  1.43    dyoung 		return NULL;
    754  1.43    dyoung 	}
    755  1.43    dyoung }
    756  1.43    dyoung 
    757  1.43    dyoung void
    758  1.43    dyoung pci_chipset_tag_destroy(pci_chipset_tag_t pc)
    759  1.43    dyoung {
    760  1.43    dyoung 	kmem_free(pc, sizeof(struct pci_chipset_tag));
    761  1.43    dyoung }
    762  1.43    dyoung 
    763  1.43    dyoung int
    764  1.43    dyoung pci_chipset_tag_create(pci_chipset_tag_t opc, const uint64_t present,
    765  1.43    dyoung     const struct pci_overrides *ov, void *ctx, pci_chipset_tag_t *pcp)
    766  1.43    dyoung {
    767  1.43    dyoung 	uint64_t bit, bits, nbits;
    768  1.43    dyoung 	pci_chipset_tag_t pc;
    769  1.43    dyoung 	const void *fp;
    770  1.43    dyoung 
    771  1.43    dyoung 	if (ov == NULL || present == 0)
    772  1.43    dyoung 		return EINVAL;
    773  1.43    dyoung 
    774  1.43    dyoung 	pc = kmem_alloc(sizeof(struct pci_chipset_tag), KM_SLEEP);
    775  1.43    dyoung 
    776  1.43    dyoung 	if (pc == NULL)
    777  1.43    dyoung 		return ENOMEM;
    778  1.43    dyoung 
    779  1.43    dyoung 	pc->pc_super = opc;
    780  1.43    dyoung 
    781  1.43    dyoung 	for (bits = present; bits != 0; bits = nbits) {
    782  1.43    dyoung 		nbits = bits & (bits - 1);
    783  1.43    dyoung 		bit = nbits ^ bits;
    784  1.43    dyoung 		if ((fp = bit_to_function_pointer(ov, bit)) == NULL) {
    785  1.51    dyoung #ifdef DEBUG
    786  1.43    dyoung 			printf("%s: missing bit %" PRIx64 "\n", __func__, bit);
    787  1.51    dyoung #endif
    788  1.43    dyoung 			goto einval;
    789  1.43    dyoung 		}
    790  1.43    dyoung 	}
    791  1.43    dyoung 
    792  1.43    dyoung 	pc->pc_ov = ov;
    793  1.43    dyoung 	pc->pc_present = present;
    794  1.43    dyoung 	pc->pc_ctx = ctx;
    795  1.43    dyoung 
    796  1.43    dyoung 	*pcp = pc;
    797  1.43    dyoung 
    798  1.43    dyoung 	return 0;
    799  1.43    dyoung einval:
    800  1.43    dyoung 	kmem_free(pc, sizeof(struct pci_chipset_tag));
    801  1.43    dyoung 	return EINVAL;
    802  1.43    dyoung }
    803  1.52    dyoung 
    804  1.52    dyoung static void
    805  1.52    dyoung x86_genfb_set_mapreg(void *opaque, int index, int r, int g, int b)
    806  1.52    dyoung {
    807  1.52    dyoung 	outb(0x3c0 + VGA_DAC_ADDRW, index);
    808  1.52    dyoung 	outb(0x3c0 + VGA_DAC_PALETTE, (uint8_t)r >> 2);
    809  1.52    dyoung 	outb(0x3c0 + VGA_DAC_PALETTE, (uint8_t)g >> 2);
    810  1.52    dyoung 	outb(0x3c0 + VGA_DAC_PALETTE, (uint8_t)b >> 2);
    811  1.52    dyoung }
    812  1.52    dyoung 
    813  1.52    dyoung static bool
    814  1.52    dyoung x86_genfb_setmode(struct genfb_softc *sc, int newmode)
    815  1.52    dyoung {
    816  1.52    dyoung #if NGENFB > 0
    817  1.52    dyoung 	static int curmode = WSDISPLAYIO_MODE_EMUL;
    818  1.52    dyoung 
    819  1.52    dyoung 	switch (newmode) {
    820  1.52    dyoung 	case WSDISPLAYIO_MODE_EMUL:
    821  1.52    dyoung 		x86_genfb_mtrr_init(sc->sc_fboffset,
    822  1.52    dyoung 		    sc->sc_height * sc->sc_stride);
    823  1.52    dyoung #if NACPICA > 0 && defined(VGA_POST)
    824  1.52    dyoung 		if (curmode != newmode) {
    825  1.52    dyoung 			if (vga_posth != NULL && acpi_md_vesa_modenum != 0) {
    826  1.52    dyoung 				vga_post_set_vbe(vga_posth,
    827  1.52    dyoung 				    acpi_md_vesa_modenum);
    828  1.52    dyoung 			}
    829  1.52    dyoung 		}
    830  1.52    dyoung #endif
    831  1.52    dyoung 		break;
    832  1.52    dyoung 	}
    833  1.52    dyoung 
    834  1.52    dyoung 	curmode = newmode;
    835  1.52    dyoung #endif
    836  1.52    dyoung 	return true;
    837  1.52    dyoung }
    838  1.52    dyoung 
    839  1.52    dyoung static bool
    840  1.52    dyoung x86_genfb_suspend(device_t dev, const pmf_qual_t *qual)
    841  1.52    dyoung {
    842  1.52    dyoung 	return true;
    843  1.52    dyoung }
    844  1.52    dyoung 
    845  1.52    dyoung static bool
    846  1.52    dyoung x86_genfb_resume(device_t dev, const pmf_qual_t *qual)
    847  1.52    dyoung {
    848  1.52    dyoung #if NGENFB > 0
    849  1.52    dyoung 	struct pci_genfb_softc *psc = device_private(dev);
    850  1.52    dyoung 
    851  1.52    dyoung #if NACPICA > 0 && defined(VGA_POST)
    852  1.52    dyoung 	if (vga_posth != NULL && acpi_md_vbios_reset == 2) {
    853  1.52    dyoung 		vga_post_call(vga_posth);
    854  1.52    dyoung 		if (acpi_md_vesa_modenum != 0)
    855  1.52    dyoung 			vga_post_set_vbe(vga_posth, acpi_md_vesa_modenum);
    856  1.52    dyoung 	}
    857  1.52    dyoung #endif
    858  1.52    dyoung 	genfb_restore_palette(&psc->sc_gen);
    859  1.52    dyoung #endif
    860  1.52    dyoung 
    861  1.52    dyoung 	return true;
    862  1.52    dyoung }
    863  1.52    dyoung 
    864  1.52    dyoung device_t
    865  1.52    dyoung device_pci_register(device_t dev, void *aux)
    866  1.52    dyoung {
    867  1.52    dyoung 	static bool found_console = false;
    868  1.52    dyoung 
    869  1.52    dyoung 	device_pci_props_register(dev, aux);
    870  1.52    dyoung 
    871  1.52    dyoung 	/*
    872  1.52    dyoung 	 * Handle network interfaces here, the attachment information is
    873  1.52    dyoung 	 * not available driver-independently later.
    874  1.52    dyoung 	 *
    875  1.52    dyoung 	 * For disks, there is nothing useful available at attach time.
    876  1.52    dyoung 	 */
    877  1.52    dyoung 	if (device_class(dev) == DV_IFNET) {
    878  1.52    dyoung 		struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF);
    879  1.52    dyoung 		if (bin == NULL)
    880  1.52    dyoung 			return NULL;
    881  1.52    dyoung 
    882  1.52    dyoung 		/*
    883  1.52    dyoung 		 * We don't check the driver name against the device name
    884  1.52    dyoung 		 * passed by the boot ROM.  The ROM should stay usable if
    885  1.52    dyoung 		 * the driver becomes obsolete.  The physical attachment
    886  1.52    dyoung 		 * information (checked below) must be sufficient to
    887  1.55  jakllsch 		 * identify the device.
    888  1.52    dyoung 		 */
    889  1.52    dyoung 		if (bin->bus == BI_BUS_PCI &&
    890  1.52    dyoung 		    device_is_a(device_parent(dev), "pci")) {
    891  1.52    dyoung 			struct pci_attach_args *paa = aux;
    892  1.52    dyoung 			int b, d, f;
    893  1.52    dyoung 
    894  1.52    dyoung 			/*
    895  1.52    dyoung 			 * Calculate BIOS representation of:
    896  1.52    dyoung 			 *
    897  1.52    dyoung 			 *	<bus,device,function>
    898  1.52    dyoung 			 *
    899  1.52    dyoung 			 * and compare.
    900  1.52    dyoung 			 */
    901  1.52    dyoung 			pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f);
    902  1.52    dyoung 			if (bin->addr.tag == ((b << 8) | (d << 3) | f))
    903  1.52    dyoung 				return dev;
    904  1.52    dyoung 		}
    905  1.52    dyoung 	}
    906  1.52    dyoung 	if (device_parent(dev) && device_is_a(device_parent(dev), "pci") &&
    907  1.52    dyoung 	    found_console == false) {
    908  1.52    dyoung 		struct btinfo_framebuffer *fbinfo;
    909  1.52    dyoung 		struct pci_attach_args *pa = aux;
    910  1.52    dyoung 		prop_dictionary_t dict;
    911  1.52    dyoung 
    912  1.52    dyoung 		if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY) {
    913  1.52    dyoung #if NWSDISPLAY > 0 && NGENFB > 0
    914  1.52    dyoung 			extern struct vcons_screen x86_genfb_console_screen;
    915  1.52    dyoung 			struct rasops_info *ri;
    916  1.52    dyoung 
    917  1.52    dyoung 			ri = &x86_genfb_console_screen.scr_ri;
    918  1.52    dyoung #endif
    919  1.52    dyoung 
    920  1.52    dyoung 			fbinfo = lookup_bootinfo(BTINFO_FRAMEBUFFER);
    921  1.52    dyoung 			dict = device_properties(dev);
    922  1.52    dyoung 			/*
    923  1.52    dyoung 			 * framebuffer drivers other than genfb can work
    924  1.52    dyoung 			 * without the address property
    925  1.52    dyoung 			 */
    926  1.52    dyoung 			if (fbinfo != NULL) {
    927  1.52    dyoung 				if (fbinfo->physaddr != 0) {
    928  1.52    dyoung 				prop_dictionary_set_uint32(dict, "width",
    929  1.52    dyoung 				    fbinfo->width);
    930  1.52    dyoung 				prop_dictionary_set_uint32(dict, "height",
    931  1.52    dyoung 				    fbinfo->height);
    932  1.52    dyoung 				prop_dictionary_set_uint8(dict, "depth",
    933  1.52    dyoung 				    fbinfo->depth);
    934  1.52    dyoung 				prop_dictionary_set_uint16(dict, "linebytes",
    935  1.52    dyoung 				    fbinfo->stride);
    936  1.52    dyoung 
    937  1.52    dyoung 				prop_dictionary_set_uint64(dict, "address",
    938  1.52    dyoung 				    fbinfo->physaddr);
    939  1.52    dyoung #if NWSDISPLAY > 0 && NGENFB > 0
    940  1.52    dyoung 				if (ri->ri_bits != NULL) {
    941  1.52    dyoung 					prop_dictionary_set_uint64(dict,
    942  1.52    dyoung 					    "virtual_address",
    943  1.52    dyoung 					    (vaddr_t)ri->ri_bits);
    944  1.52    dyoung 				}
    945  1.52    dyoung #endif
    946  1.52    dyoung 				}
    947  1.52    dyoung #if notyet
    948  1.52    dyoung 				prop_dictionary_set_bool(dict, "splash",
    949  1.52    dyoung 				    fbinfo->flags & BI_FB_SPLASH ?
    950  1.52    dyoung 				     true : false);
    951  1.52    dyoung #endif
    952  1.52    dyoung 				if (fbinfo->depth == 8) {
    953  1.52    dyoung 					gfb_cb.gcc_cookie = NULL;
    954  1.55  jakllsch 					gfb_cb.gcc_set_mapreg =
    955  1.52    dyoung 					    x86_genfb_set_mapreg;
    956  1.52    dyoung 					prop_dictionary_set_uint64(dict,
    957  1.52    dyoung 					    "cmap_callback",
    958  1.52    dyoung 					    (uint64_t)(uintptr_t)&gfb_cb);
    959  1.52    dyoung 				}
    960  1.52    dyoung 				if (fbinfo->physaddr != 0) {
    961  1.52    dyoung 					mode_cb.gmc_setmode = x86_genfb_setmode;
    962  1.52    dyoung 					prop_dictionary_set_uint64(dict,
    963  1.52    dyoung 					    "mode_callback",
    964  1.52    dyoung 					    (uint64_t)(uintptr_t)&mode_cb);
    965  1.52    dyoung 				}
    966  1.52    dyoung 
    967  1.52    dyoung #if NWSDISPLAY > 0 && NGENFB > 0
    968  1.52    dyoung 				if (device_is_a(dev, "genfb")) {
    969  1.52    dyoung 					x86_genfb_set_console_dev(dev);
    970  1.52    dyoung #ifdef DDB
    971  1.52    dyoung 					db_trap_callback =
    972  1.52    dyoung 					    x86_genfb_ddb_trap_callback;
    973  1.52    dyoung #endif
    974  1.52    dyoung 				}
    975  1.52    dyoung #endif
    976  1.52    dyoung 			}
    977  1.52    dyoung 			prop_dictionary_set_bool(dict, "is_console", true);
    978  1.52    dyoung 			prop_dictionary_set_bool(dict, "clear-screen", false);
    979  1.52    dyoung #if NWSDISPLAY > 0 && NGENFB > 0
    980  1.52    dyoung 			prop_dictionary_set_uint16(dict, "cursor-row",
    981  1.52    dyoung 			    x86_genfb_console_screen.scr_ri.ri_crow);
    982  1.52    dyoung #endif
    983  1.52    dyoung #if notyet
    984  1.52    dyoung 			prop_dictionary_set_bool(dict, "splash",
    985  1.52    dyoung 			    fbinfo->flags & BI_FB_SPLASH ? true : false);
    986  1.52    dyoung #endif
    987  1.52    dyoung 			pmf_cb.gpc_suspend = x86_genfb_suspend;
    988  1.52    dyoung 			pmf_cb.gpc_resume = x86_genfb_resume;
    989  1.52    dyoung 			prop_dictionary_set_uint64(dict,
    990  1.52    dyoung 			    "pmf_callback", (uint64_t)(uintptr_t)&pmf_cb);
    991  1.52    dyoung #ifdef VGA_POST
    992  1.52    dyoung 			vga_posth = vga_post_init(pa->pa_bus, pa->pa_device,
    993  1.52    dyoung 			    pa->pa_function);
    994  1.52    dyoung #endif
    995  1.52    dyoung 			found_console = true;
    996  1.52    dyoung 			return NULL;
    997  1.52    dyoung 		}
    998  1.52    dyoung 	}
    999  1.52    dyoung 	return NULL;
   1000  1.52    dyoung }
   1001