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