Home | History | Annotate | Line # | Download | only in pci
pci_machdep.c revision 1.40
      1  1.40    dyoung /*	$NetBSD: pci_machdep.c,v 1.40 2010/02/25 20:51:10 dyoung 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.1      fvdl  * as defined 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.40    dyoung __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.40 2010/02/25 20:51:10 dyoung 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.1      fvdl 
     86   1.1      fvdl #include <uvm/uvm_extern.h>
     87   1.1      fvdl 
     88  1.10      yamt #include <machine/bus_private.h>
     89   1.1      fvdl 
     90   1.1      fvdl #include <machine/pio.h>
     91  1.30        ad #include <machine/lock.h>
     92   1.1      fvdl 
     93   1.3      fvdl #include <dev/isa/isareg.h>
     94   1.1      fvdl #include <dev/isa/isavar.h>
     95   1.1      fvdl #include <dev/pci/pcivar.h>
     96   1.1      fvdl #include <dev/pci/pcireg.h>
     97   1.1      fvdl #include <dev/pci/pcidevs.h>
     98   1.1      fvdl 
     99  1.37  jmcneill #include "acpica.h"
    100  1.14    bouyer #include "opt_mpbios.h"
    101  1.16  christos #include "opt_acpi.h"
    102  1.14    bouyer 
    103  1.14    bouyer #ifdef MPBIOS
    104  1.14    bouyer #include <machine/mpbiosvar.h>
    105  1.14    bouyer #endif
    106  1.14    bouyer 
    107  1.37  jmcneill #if NACPICA > 0
    108  1.14    bouyer #include <machine/mpacpi.h>
    109  1.14    bouyer #endif
    110  1.14    bouyer 
    111  1.16  christos #include <machine/mpconfig.h>
    112  1.16  christos 
    113   1.1      fvdl #include "opt_pci_conf_mode.h"
    114   1.1      fvdl 
    115  1.19  jmcneill #ifdef __i386__
    116  1.19  jmcneill #include "opt_xbox.h"
    117  1.19  jmcneill #ifdef XBOX
    118  1.19  jmcneill #include <machine/xbox.h>
    119  1.19  jmcneill #endif
    120  1.19  jmcneill #endif
    121  1.19  jmcneill 
    122  1.38    dyoung #ifdef PCI_CONF_MODE
    123  1.38    dyoung #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
    124  1.38    dyoung static int pci_mode = PCI_CONF_MODE;
    125  1.38    dyoung #else
    126  1.38    dyoung #error Invalid PCI configuration mode.
    127  1.38    dyoung #endif
    128  1.38    dyoung #else
    129  1.38    dyoung static int pci_mode = -1;
    130  1.38    dyoung #endif
    131   1.1      fvdl 
    132  1.11    sekiya static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
    133  1.11    sekiya struct pci_bridge_hook_arg {
    134  1.11    sekiya 	void (*func)(pci_chipset_tag_t, pcitag_t, void *);
    135  1.11    sekiya 	void *arg;
    136  1.11    sekiya };
    137  1.11    sekiya 
    138  1.23        ad __cpu_simple_lock_t pci_conf_lock = __SIMPLELOCK_UNLOCKED;
    139   1.1      fvdl 
    140   1.1      fvdl #define	PCI_CONF_LOCK(s)						\
    141   1.1      fvdl do {									\
    142   1.1      fvdl 	(s) = splhigh();						\
    143  1.23        ad 	__cpu_simple_lock(&pci_conf_lock);				\
    144   1.1      fvdl } while (0)
    145   1.1      fvdl 
    146   1.1      fvdl #define	PCI_CONF_UNLOCK(s)						\
    147   1.1      fvdl do {									\
    148  1.23        ad 	__cpu_simple_unlock(&pci_conf_lock);				\
    149   1.1      fvdl 	splx((s));							\
    150   1.1      fvdl } while (0)
    151   1.1      fvdl 
    152   1.1      fvdl #define	PCI_MODE1_ENABLE	0x80000000UL
    153   1.1      fvdl #define	PCI_MODE1_ADDRESS_REG	0x0cf8
    154   1.1      fvdl #define	PCI_MODE1_DATA_REG	0x0cfc
    155   1.1      fvdl 
    156   1.1      fvdl #define	PCI_MODE2_ENABLE_REG	0x0cf8
    157   1.1      fvdl #define	PCI_MODE2_FORWARD_REG	0x0cfa
    158   1.1      fvdl 
    159   1.1      fvdl #define _m1tag(b, d, f) \
    160   1.1      fvdl 	(PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8))
    161   1.1      fvdl #define _qe(bus, dev, fcn, vend, prod) \
    162   1.1      fvdl 	{_m1tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)}
    163   1.1      fvdl struct {
    164  1.33    cegger 	uint32_t tag;
    165   1.1      fvdl 	pcireg_t id;
    166   1.1      fvdl } pcim1_quirk_tbl[] = {
    167   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX1),
    168   1.1      fvdl 	/* XXX Triflex2 not tested */
    169   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX2),
    170   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX4),
    171   1.1      fvdl 	/* Triton needed for Connectix Virtual PC */
    172   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX),
    173   1.1      fvdl 	/* Connectix Virtual PC 5 has a 440BX */
    174   1.1      fvdl 	_qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP),
    175  1.15     soren 	/* Parallels Desktop for Mac */
    176  1.15     soren 	_qe(0, 2, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_VIDEO),
    177  1.15     soren 	_qe(0, 3, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_TOOLS),
    178  1.36  drochner 	/* SIS 740 */
    179  1.36  drochner 	_qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_740),
    180  1.12  christos 	/* SIS 741 */
    181  1.12  christos 	_qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_741),
    182   1.1      fvdl 	{0, 0xffffffff} /* patchable */
    183   1.1      fvdl };
    184   1.1      fvdl #undef _m1tag
    185   1.1      fvdl #undef _id
    186   1.1      fvdl #undef _qe
    187   1.1      fvdl 
    188   1.1      fvdl /*
    189   1.1      fvdl  * PCI doesn't have any special needs; just use the generic versions
    190   1.1      fvdl  * of these functions.
    191   1.1      fvdl  */
    192   1.1      fvdl struct x86_bus_dma_tag pci_bus_dma_tag = {
    193  1.21       mrg 	0,				/* tag_needs_free */
    194   1.3      fvdl #if defined(_LP64) || defined(PAE)
    195   1.3      fvdl 	PCI32_DMA_BOUNCE_THRESHOLD,	/* bounce_thresh */
    196   1.3      fvdl 	ISA_DMA_BOUNCE_THRESHOLD,	/* bounce_alloclo */
    197   1.3      fvdl 	PCI32_DMA_BOUNCE_THRESHOLD,	/* bounce_allochi */
    198   1.3      fvdl #else
    199   1.3      fvdl 	0,
    200   1.3      fvdl 	0,
    201   1.3      fvdl 	0,
    202   1.3      fvdl #endif
    203   1.3      fvdl 	NULL,			/* _may_bounce */
    204   1.1      fvdl 	_bus_dmamap_create,
    205   1.1      fvdl 	_bus_dmamap_destroy,
    206   1.1      fvdl 	_bus_dmamap_load,
    207   1.1      fvdl 	_bus_dmamap_load_mbuf,
    208   1.1      fvdl 	_bus_dmamap_load_uio,
    209   1.1      fvdl 	_bus_dmamap_load_raw,
    210   1.1      fvdl 	_bus_dmamap_unload,
    211   1.3      fvdl 	_bus_dmamap_sync,
    212   1.1      fvdl 	_bus_dmamem_alloc,
    213   1.1      fvdl 	_bus_dmamem_free,
    214   1.1      fvdl 	_bus_dmamem_map,
    215   1.1      fvdl 	_bus_dmamem_unmap,
    216   1.1      fvdl 	_bus_dmamem_mmap,
    217  1.21       mrg 	_bus_dmatag_subregion,
    218  1.21       mrg 	_bus_dmatag_destroy,
    219   1.1      fvdl };
    220   1.5      fvdl 
    221   1.5      fvdl #ifdef _LP64
    222   1.5      fvdl struct x86_bus_dma_tag pci_bus_dma64_tag = {
    223  1.22      matt 	0,				/* tag_needs_free */
    224   1.5      fvdl 	0,
    225   1.5      fvdl 	0,
    226   1.5      fvdl 	0,
    227   1.5      fvdl 	NULL,			/* _may_bounce */
    228   1.5      fvdl 	_bus_dmamap_create,
    229   1.5      fvdl 	_bus_dmamap_destroy,
    230   1.5      fvdl 	_bus_dmamap_load,
    231   1.5      fvdl 	_bus_dmamap_load_mbuf,
    232   1.5      fvdl 	_bus_dmamap_load_uio,
    233   1.5      fvdl 	_bus_dmamap_load_raw,
    234   1.5      fvdl 	_bus_dmamap_unload,
    235   1.5      fvdl 	NULL,
    236   1.5      fvdl 	_bus_dmamem_alloc,
    237   1.5      fvdl 	_bus_dmamem_free,
    238   1.5      fvdl 	_bus_dmamem_map,
    239   1.5      fvdl 	_bus_dmamem_unmap,
    240   1.5      fvdl 	_bus_dmamem_mmap,
    241  1.21       mrg 	_bus_dmatag_subregion,
    242  1.21       mrg 	_bus_dmatag_destroy,
    243   1.5      fvdl };
    244   1.5      fvdl #endif
    245   1.1      fvdl 
    246  1.39    dyoung static uint32_t
    247  1.39    dyoung pci_conf_selector(pcitag_t tag, int reg)
    248  1.39    dyoung {
    249  1.39    dyoung 	static const pcitag_t mode2_mask = {
    250  1.39    dyoung 		.mode2 = {
    251  1.39    dyoung 			  .enable = 0xff
    252  1.39    dyoung 			, .forward = 0xff
    253  1.39    dyoung 		}
    254  1.39    dyoung 	};
    255  1.39    dyoung 
    256  1.39    dyoung 	switch (pci_mode) {
    257  1.39    dyoung 	case 1:
    258  1.39    dyoung 		return tag.mode1 | reg;
    259  1.39    dyoung 	case 2:
    260  1.39    dyoung 		return tag.mode1 & mode2_mask.mode1;
    261  1.39    dyoung 	default:
    262  1.39    dyoung 		panic("%s: mode not configured", __func__);
    263  1.39    dyoung 	}
    264  1.39    dyoung }
    265  1.39    dyoung 
    266  1.39    dyoung static unsigned int
    267  1.39    dyoung pci_conf_port(pcitag_t tag, int reg)
    268  1.39    dyoung {
    269  1.39    dyoung 	switch (pci_mode) {
    270  1.39    dyoung 	case 1:
    271  1.39    dyoung 		return PCI_MODE1_DATA_REG;
    272  1.39    dyoung 	case 2:
    273  1.39    dyoung 		return tag.mode2.port | reg;
    274  1.39    dyoung 	default:
    275  1.39    dyoung 		panic("%s: mode not configured", __func__);
    276  1.39    dyoung 	}
    277  1.39    dyoung }
    278  1.39    dyoung 
    279  1.39    dyoung static void
    280  1.39    dyoung pci_conf_select(uint32_t addr)
    281  1.39    dyoung {
    282  1.39    dyoung 	pcitag_t tag;
    283  1.39    dyoung 
    284  1.39    dyoung 	switch (pci_mode) {
    285  1.39    dyoung 	case 1:
    286  1.39    dyoung 		outl(PCI_MODE1_ADDRESS_REG, addr);
    287  1.39    dyoung 		return;
    288  1.39    dyoung 	case 2:
    289  1.39    dyoung 		tag.mode1 = addr;
    290  1.39    dyoung 		outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
    291  1.39    dyoung 		if (tag.mode2.enable != 0)
    292  1.39    dyoung 			outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
    293  1.39    dyoung 		return;
    294  1.39    dyoung 	default:
    295  1.39    dyoung 		panic("%s: mode not configured", __func__);
    296  1.39    dyoung 	}
    297  1.39    dyoung }
    298  1.39    dyoung 
    299   1.1      fvdl void
    300  1.32    dyoung pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
    301   1.1      fvdl {
    302   1.1      fvdl 
    303   1.1      fvdl 	if (pba->pba_bus == 0)
    304  1.26       mjf 		aprint_normal(": configuration mode %d", pci_mode);
    305   1.4      fvdl #ifdef MPBIOS
    306   1.4      fvdl 	mpbios_pci_attach_hook(parent, self, pba);
    307   1.4      fvdl #endif
    308  1.37  jmcneill #if NACPICA > 0
    309   1.4      fvdl 	mpacpi_pci_attach_hook(parent, self, pba);
    310   1.4      fvdl #endif
    311   1.1      fvdl }
    312   1.1      fvdl 
    313   1.1      fvdl int
    314  1.18  christos pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
    315   1.1      fvdl {
    316   1.1      fvdl 
    317  1.19  jmcneill #if defined(__i386__) && defined(XBOX)
    318  1.19  jmcneill 	/*
    319  1.19  jmcneill 	 * Scanning above the first device is fatal on the Microsoft Xbox.
    320  1.19  jmcneill 	 * If busno=1, only allow for one device.
    321  1.19  jmcneill 	 */
    322  1.19  jmcneill 	if (arch_i386_is_xbox) {
    323  1.19  jmcneill 		if (busno == 1)
    324  1.19  jmcneill 			return 1;
    325  1.19  jmcneill 		else if (busno > 1)
    326  1.19  jmcneill 			return 0;
    327  1.19  jmcneill 	}
    328  1.19  jmcneill #endif
    329  1.19  jmcneill 
    330   1.1      fvdl 	/*
    331   1.1      fvdl 	 * Bus number is irrelevant.  If Configuration Mechanism 2 is in
    332   1.1      fvdl 	 * use, can only have devices 0-15 on any bus.  If Configuration
    333   1.1      fvdl 	 * Mechanism 1 is in use, can have devices 0-32 (i.e. the `normal'
    334   1.1      fvdl 	 * range).
    335   1.1      fvdl 	 */
    336   1.1      fvdl 	if (pci_mode == 2)
    337   1.1      fvdl 		return (16);
    338   1.1      fvdl 	else
    339   1.1      fvdl 		return (32);
    340   1.1      fvdl }
    341   1.1      fvdl 
    342   1.1      fvdl pcitag_t
    343  1.18  christos pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
    344   1.1      fvdl {
    345   1.1      fvdl 	pcitag_t tag;
    346   1.1      fvdl 
    347  1.40    dyoung 	if (pc != NULL && pc->pc_make_tag != NULL)
    348  1.40    dyoung 		return (*pc->pc_make_tag)(pc, bus, device, function);
    349  1.40    dyoung 
    350   1.1      fvdl 	switch (pci_mode) {
    351   1.1      fvdl 	case 1:
    352  1.38    dyoung 		if (bus >= 256 || device >= 32 || function >= 8)
    353  1.39    dyoung 			panic("%s: bad request", __func__);
    354  1.38    dyoung 
    355  1.38    dyoung 		tag.mode1 = PCI_MODE1_ENABLE |
    356  1.38    dyoung 			    (bus << 16) | (device << 11) | (function << 8);
    357  1.38    dyoung 		return tag;
    358   1.1      fvdl 	case 2:
    359  1.38    dyoung 		if (bus >= 256 || device >= 16 || function >= 8)
    360  1.39    dyoung 			panic("%s: bad request", __func__);
    361  1.38    dyoung 
    362  1.38    dyoung 		tag.mode2.port = 0xc000 | (device << 8);
    363  1.38    dyoung 		tag.mode2.enable = 0xf0 | (function << 1);
    364  1.38    dyoung 		tag.mode2.forward = bus;
    365  1.38    dyoung 		return tag;
    366   1.1      fvdl 	default:
    367  1.39    dyoung 		panic("%s: mode not configured", __func__);
    368   1.1      fvdl 	}
    369   1.1      fvdl }
    370   1.1      fvdl 
    371   1.1      fvdl void
    372  1.18  christos pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag,
    373  1.17  christos     int *bp, int *dp, int *fp)
    374   1.1      fvdl {
    375   1.1      fvdl 
    376  1.40    dyoung 	if (pc != NULL && pc->pc_decompose_tag != NULL) {
    377  1.40    dyoung 		(*pc->pc_decompose_tag)(pc, tag, bp, dp, fp);
    378  1.40    dyoung 		return;
    379  1.40    dyoung 	}
    380  1.40    dyoung 
    381   1.1      fvdl 	switch (pci_mode) {
    382   1.1      fvdl 	case 1:
    383  1.38    dyoung 		if (bp != NULL)
    384  1.38    dyoung 			*bp = (tag.mode1 >> 16) & 0xff;
    385  1.38    dyoung 		if (dp != NULL)
    386  1.38    dyoung 			*dp = (tag.mode1 >> 11) & 0x1f;
    387  1.38    dyoung 		if (fp != NULL)
    388  1.38    dyoung 			*fp = (tag.mode1 >> 8) & 0x7;
    389  1.38    dyoung 		return;
    390   1.1      fvdl 	case 2:
    391  1.38    dyoung 		if (bp != NULL)
    392  1.38    dyoung 			*bp = tag.mode2.forward & 0xff;
    393  1.38    dyoung 		if (dp != NULL)
    394  1.38    dyoung 			*dp = (tag.mode2.port >> 8) & 0xf;
    395  1.38    dyoung 		if (fp != NULL)
    396  1.38    dyoung 			*fp = (tag.mode2.enable >> 1) & 0x7;
    397  1.38    dyoung 		return;
    398   1.1      fvdl 	default:
    399  1.39    dyoung 		panic("%s: mode not configured", __func__);
    400   1.1      fvdl 	}
    401   1.1      fvdl }
    402   1.1      fvdl 
    403   1.1      fvdl pcireg_t
    404  1.18  christos pci_conf_read( pci_chipset_tag_t pc, pcitag_t tag,
    405  1.18  christos     int reg)
    406   1.1      fvdl {
    407   1.1      fvdl 	pcireg_t data;
    408   1.1      fvdl 	int s;
    409   1.1      fvdl 
    410  1.31    dyoung 	KASSERT((reg & 0x3) == 0);
    411  1.40    dyoung 
    412  1.40    dyoung 	if (pc != NULL && pc->pc_conf_read != NULL)
    413  1.40    dyoung 		return (*pc->pc_conf_read)(pc, tag, reg);
    414  1.40    dyoung 
    415  1.20  jmcneill #if defined(__i386__) && defined(XBOX)
    416  1.20  jmcneill 	if (arch_i386_is_xbox) {
    417  1.20  jmcneill 		int bus, dev, fn;
    418  1.20  jmcneill 		pci_decompose_tag(pc, tag, &bus, &dev, &fn);
    419  1.20  jmcneill 		if (bus == 0 && dev == 0 && (fn == 1 || fn == 2))
    420  1.20  jmcneill 			return (pcireg_t)-1;
    421  1.20  jmcneill 	}
    422  1.20  jmcneill #endif
    423  1.20  jmcneill 
    424  1.39    dyoung 	PCI_CONF_LOCK(s);
    425  1.39    dyoung 	pci_conf_select(pci_conf_selector(tag, reg));
    426  1.39    dyoung 	data = inl(pci_conf_port(tag, reg));
    427  1.39    dyoung 	pci_conf_select(0);
    428  1.39    dyoung 	PCI_CONF_UNLOCK(s);
    429  1.39    dyoung 	return data;
    430   1.1      fvdl }
    431   1.1      fvdl 
    432   1.1      fvdl void
    433  1.18  christos pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg,
    434  1.17  christos     pcireg_t data)
    435   1.1      fvdl {
    436   1.1      fvdl 	int s;
    437   1.1      fvdl 
    438  1.31    dyoung 	KASSERT((reg & 0x3) == 0);
    439  1.40    dyoung 
    440  1.40    dyoung 	if (pc != NULL && pc->pc_conf_write != NULL) {
    441  1.40    dyoung 		(*pc->pc_conf_write)(pc, tag, reg, data);
    442  1.40    dyoung 		return;
    443  1.40    dyoung 	}
    444  1.40    dyoung 
    445  1.20  jmcneill #if defined(__i386__) && defined(XBOX)
    446  1.20  jmcneill 	if (arch_i386_is_xbox) {
    447  1.20  jmcneill 		int bus, dev, fn;
    448  1.20  jmcneill 		pci_decompose_tag(pc, tag, &bus, &dev, &fn);
    449  1.20  jmcneill 		if (bus == 0 && dev == 0 && (fn == 1 || fn == 2))
    450  1.20  jmcneill 			return;
    451  1.20  jmcneill 	}
    452  1.20  jmcneill #endif
    453  1.20  jmcneill 
    454  1.39    dyoung 	PCI_CONF_LOCK(s);
    455  1.39    dyoung 	pci_conf_select(pci_conf_selector(tag, reg));
    456  1.39    dyoung 	outl(pci_conf_port(tag, reg), data);
    457  1.39    dyoung 	pci_conf_select(0);
    458  1.39    dyoung 	PCI_CONF_UNLOCK(s);
    459  1.38    dyoung }
    460   1.1      fvdl 
    461  1.38    dyoung void
    462  1.38    dyoung pci_mode_set(int mode)
    463  1.38    dyoung {
    464  1.38    dyoung 	KASSERT(pci_mode == -1 || pci_mode == mode);
    465   1.1      fvdl 
    466  1.38    dyoung 	pci_mode = mode;
    467   1.1      fvdl }
    468   1.1      fvdl 
    469   1.1      fvdl int
    470  1.33    cegger pci_mode_detect(void)
    471   1.1      fvdl {
    472  1.33    cegger 	uint32_t sav, val;
    473   1.1      fvdl 	int i;
    474   1.1      fvdl 	pcireg_t idreg;
    475   1.1      fvdl 
    476   1.1      fvdl 	if (pci_mode != -1)
    477   1.1      fvdl 		return pci_mode;
    478   1.1      fvdl 
    479   1.1      fvdl 	/*
    480   1.1      fvdl 	 * We try to divine which configuration mode the host bridge wants.
    481   1.1      fvdl 	 */
    482   1.1      fvdl 
    483   1.1      fvdl 	sav = inl(PCI_MODE1_ADDRESS_REG);
    484   1.1      fvdl 
    485   1.1      fvdl 	pci_mode = 1; /* assume this for now */
    486   1.1      fvdl 	/*
    487   1.1      fvdl 	 * catch some known buggy implementations of mode 1
    488   1.1      fvdl 	 */
    489  1.27    dyoung 	for (i = 0; i < __arraycount(pcim1_quirk_tbl); i++) {
    490   1.1      fvdl 		pcitag_t t;
    491   1.1      fvdl 
    492   1.1      fvdl 		if (!pcim1_quirk_tbl[i].tag)
    493   1.1      fvdl 			break;
    494   1.1      fvdl 		t.mode1 = pcim1_quirk_tbl[i].tag;
    495   1.1      fvdl 		idreg = pci_conf_read(0, t, PCI_ID_REG); /* needs "pci_mode" */
    496   1.1      fvdl 		if (idreg == pcim1_quirk_tbl[i].id) {
    497   1.1      fvdl #ifdef DEBUG
    498   1.1      fvdl 			printf("known mode 1 PCI chipset (%08x)\n",
    499   1.1      fvdl 			       idreg);
    500   1.1      fvdl #endif
    501   1.1      fvdl 			return (pci_mode);
    502   1.1      fvdl 		}
    503   1.1      fvdl 	}
    504   1.1      fvdl 
    505   1.1      fvdl 	/*
    506   1.1      fvdl 	 * Strong check for standard compliant mode 1:
    507   1.1      fvdl 	 * 1. bit 31 ("enable") can be set
    508   1.1      fvdl 	 * 2. byte/word access does not affect register
    509   1.1      fvdl 	 */
    510   1.1      fvdl 	outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE);
    511   1.1      fvdl 	outb(PCI_MODE1_ADDRESS_REG + 3, 0);
    512   1.1      fvdl 	outw(PCI_MODE1_ADDRESS_REG + 2, 0);
    513   1.1      fvdl 	val = inl(PCI_MODE1_ADDRESS_REG);
    514   1.1      fvdl 	if ((val & 0x80fffffc) != PCI_MODE1_ENABLE) {
    515   1.1      fvdl #ifdef DEBUG
    516   1.1      fvdl 		printf("pci_mode_detect: mode 1 enable failed (%x)\n",
    517   1.1      fvdl 		       val);
    518   1.1      fvdl #endif
    519   1.1      fvdl 		goto not1;
    520   1.1      fvdl 	}
    521   1.1      fvdl 	outl(PCI_MODE1_ADDRESS_REG, 0);
    522   1.1      fvdl 	val = inl(PCI_MODE1_ADDRESS_REG);
    523   1.1      fvdl 	if ((val & 0x80fffffc) != 0)
    524   1.1      fvdl 		goto not1;
    525   1.1      fvdl 	return (pci_mode);
    526   1.1      fvdl not1:
    527   1.1      fvdl 	outl(PCI_MODE1_ADDRESS_REG, sav);
    528   1.1      fvdl 
    529   1.1      fvdl 	/*
    530   1.1      fvdl 	 * This mode 2 check is quite weak (and known to give false
    531   1.1      fvdl 	 * positives on some Compaq machines).
    532   1.1      fvdl 	 * However, this doesn't matter, because this is the
    533   1.1      fvdl 	 * last test, and simply no PCI devices will be found if
    534   1.1      fvdl 	 * this happens.
    535   1.1      fvdl 	 */
    536   1.1      fvdl 	outb(PCI_MODE2_ENABLE_REG, 0);
    537   1.1      fvdl 	outb(PCI_MODE2_FORWARD_REG, 0);
    538   1.1      fvdl 	if (inb(PCI_MODE2_ENABLE_REG) != 0 ||
    539   1.1      fvdl 	    inb(PCI_MODE2_FORWARD_REG) != 0)
    540   1.1      fvdl 		goto not2;
    541   1.1      fvdl 	return (pci_mode = 2);
    542   1.1      fvdl not2:
    543   1.1      fvdl 
    544   1.1      fvdl 	return (pci_mode = 0);
    545   1.1      fvdl }
    546   1.1      fvdl 
    547   1.1      fvdl /*
    548   1.1      fvdl  * Determine which flags should be passed to the primary PCI bus's
    549   1.1      fvdl  * autoconfiguration node.  We use this to detect broken chipsets
    550   1.1      fvdl  * which cannot safely use memory-mapped device access.
    551   1.1      fvdl  */
    552   1.1      fvdl int
    553  1.35    cegger pci_bus_flags(void)
    554   1.1      fvdl {
    555   1.1      fvdl 	int rval = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
    556   1.1      fvdl 	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
    557   1.1      fvdl 	int device, maxndevs;
    558   1.1      fvdl 	pcitag_t tag;
    559   1.1      fvdl 	pcireg_t id;
    560   1.1      fvdl 
    561   1.1      fvdl 	maxndevs = pci_bus_maxdevs(NULL, 0);
    562   1.1      fvdl 
    563   1.1      fvdl 	for (device = 0; device < maxndevs; device++) {
    564   1.1      fvdl 		tag = pci_make_tag(NULL, 0, device, 0);
    565   1.1      fvdl 		id = pci_conf_read(NULL, tag, PCI_ID_REG);
    566   1.1      fvdl 
    567   1.1      fvdl 		/* Invalid vendor ID value? */
    568   1.1      fvdl 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    569   1.1      fvdl 			continue;
    570   1.1      fvdl 		/* XXX Not invalid, but we've done this ~forever. */
    571   1.1      fvdl 		if (PCI_VENDOR(id) == 0)
    572   1.1      fvdl 			continue;
    573   1.1      fvdl 
    574   1.1      fvdl 		switch (PCI_VENDOR(id)) {
    575   1.1      fvdl 		case PCI_VENDOR_SIS:
    576   1.1      fvdl 			switch (PCI_PRODUCT(id)) {
    577   1.1      fvdl 			case PCI_PRODUCT_SIS_85C496:
    578   1.1      fvdl 				goto disable_mem;
    579   1.1      fvdl 			}
    580   1.1      fvdl 			break;
    581   1.1      fvdl 		}
    582   1.1      fvdl 	}
    583   1.1      fvdl 
    584   1.1      fvdl 	return (rval);
    585   1.1      fvdl 
    586   1.1      fvdl  disable_mem:
    587   1.1      fvdl 	printf("Warning: broken PCI-Host bridge detected; "
    588   1.1      fvdl 	    "disabling memory-mapped access\n");
    589   1.1      fvdl 	rval &= ~(PCI_FLAGS_MEM_ENABLED|PCI_FLAGS_MRL_OKAY|PCI_FLAGS_MRM_OKAY|
    590   1.1      fvdl 	    PCI_FLAGS_MWI_OKAY);
    591   1.1      fvdl 	return (rval);
    592   1.1      fvdl }
    593  1.11    sekiya 
    594  1.11    sekiya void
    595  1.11    sekiya pci_device_foreach(pci_chipset_tag_t pc, int maxbus,
    596  1.11    sekiya 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
    597  1.11    sekiya {
    598  1.11    sekiya 	pci_device_foreach_min(pc, 0, maxbus, func, context);
    599  1.11    sekiya }
    600  1.11    sekiya 
    601  1.11    sekiya void
    602  1.11    sekiya pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus,
    603  1.11    sekiya 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
    604  1.11    sekiya {
    605  1.11    sekiya 	const struct pci_quirkdata *qd;
    606  1.11    sekiya 	int bus, device, function, maxdevs, nfuncs;
    607  1.11    sekiya 	pcireg_t id, bhlcr;
    608  1.11    sekiya 	pcitag_t tag;
    609  1.11    sekiya 
    610  1.11    sekiya 	for (bus = minbus; bus <= maxbus; bus++) {
    611  1.11    sekiya 		maxdevs = pci_bus_maxdevs(pc, bus);
    612  1.11    sekiya 		for (device = 0; device < maxdevs; device++) {
    613  1.11    sekiya 			tag = pci_make_tag(pc, bus, device, 0);
    614  1.11    sekiya 			id = pci_conf_read(pc, tag, PCI_ID_REG);
    615  1.11    sekiya 
    616  1.11    sekiya 			/* Invalid vendor ID value? */
    617  1.11    sekiya 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    618  1.11    sekiya 				continue;
    619  1.11    sekiya 			/* XXX Not invalid, but we've done this ~forever. */
    620  1.11    sekiya 			if (PCI_VENDOR(id) == 0)
    621  1.11    sekiya 				continue;
    622  1.11    sekiya 
    623  1.11    sekiya 			qd = pci_lookup_quirkdata(PCI_VENDOR(id),
    624  1.11    sekiya 				PCI_PRODUCT(id));
    625  1.11    sekiya 
    626  1.11    sekiya 			bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    627  1.11    sekiya 			if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
    628  1.11    sekiya 			     (qd != NULL &&
    629  1.11    sekiya 		  	     (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
    630  1.11    sekiya 				nfuncs = 8;
    631  1.11    sekiya 			else
    632  1.11    sekiya 				nfuncs = 1;
    633  1.11    sekiya 
    634  1.11    sekiya 			for (function = 0; function < nfuncs; function++) {
    635  1.11    sekiya 				tag = pci_make_tag(pc, bus, device, function);
    636  1.11    sekiya 				id = pci_conf_read(pc, tag, PCI_ID_REG);
    637  1.11    sekiya 
    638  1.11    sekiya 				/* Invalid vendor ID value? */
    639  1.11    sekiya 				if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    640  1.11    sekiya 					continue;
    641  1.11    sekiya 				/*
    642  1.11    sekiya 				 * XXX Not invalid, but we've done this
    643  1.11    sekiya 				 * ~forever.
    644  1.11    sekiya 				 */
    645  1.11    sekiya 				if (PCI_VENDOR(id) == 0)
    646  1.11    sekiya 					continue;
    647  1.11    sekiya 				(*func)(pc, tag, context);
    648  1.11    sekiya 			}
    649  1.11    sekiya 		}
    650  1.11    sekiya 	}
    651  1.11    sekiya }
    652  1.11    sekiya 
    653  1.11    sekiya void
    654  1.11    sekiya pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
    655  1.11    sekiya 	void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
    656  1.11    sekiya {
    657  1.11    sekiya 	struct pci_bridge_hook_arg bridge_hook;
    658  1.11    sekiya 
    659  1.11    sekiya 	bridge_hook.func = func;
    660  1.11    sekiya 	bridge_hook.arg = ctx;
    661  1.11    sekiya 
    662  1.11    sekiya 	pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
    663  1.11    sekiya 		&bridge_hook);
    664  1.11    sekiya }
    665  1.11    sekiya 
    666  1.11    sekiya static void
    667  1.11    sekiya pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
    668  1.11    sekiya {
    669  1.11    sekiya 	struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
    670  1.11    sekiya 	pcireg_t reg;
    671  1.11    sekiya 
    672  1.11    sekiya 	reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
    673  1.11    sekiya 	if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
    674  1.11    sekiya  	     (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
    675  1.11    sekiya 		PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
    676  1.11    sekiya 		(*bridge_hook->func)(pc, tag, bridge_hook->arg);
    677  1.11    sekiya 	}
    678  1.11    sekiya }
    679