Home | History | Annotate | Line # | Download | only in pci
pci.c revision 1.149
      1  1.149   msaitoh /*	$NetBSD: pci.c,v 1.149 2015/10/02 05:22:53 msaitoh Exp $	*/
      2    1.3       cgd 
      3    1.1   mycroft /*
      4   1.37       cgd  * Copyright (c) 1995, 1996, 1997, 1998
      5   1.27       cgd  *     Christopher G. Demetriou.  All rights reserved.
      6   1.39   mycroft  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      7    1.1   mycroft  *
      8    1.1   mycroft  * Redistribution and use in source and binary forms, with or without
      9    1.1   mycroft  * modification, are permitted provided that the following conditions
     10    1.1   mycroft  * are met:
     11    1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     12    1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     13    1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     15    1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     16    1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     17    1.1   mycroft  *    must display the following acknowledgement:
     18   1.39   mycroft  *	This product includes software developed by Charles M. Hannum.
     19    1.1   mycroft  * 4. The name of the author may not be used to endorse or promote products
     20    1.1   mycroft  *    derived from this software without specific prior written permission.
     21    1.1   mycroft  *
     22    1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23    1.1   mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24    1.1   mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25    1.1   mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26    1.1   mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27    1.1   mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28    1.1   mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29    1.1   mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30    1.1   mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31    1.1   mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32    1.1   mycroft  */
     33    1.1   mycroft 
     34    1.1   mycroft /*
     35   1.10       cgd  * PCI bus autoconfiguration.
     36    1.1   mycroft  */
     37   1.58     lukem 
     38   1.58     lukem #include <sys/cdefs.h>
     39  1.149   msaitoh __KERNEL_RCSID(0, "$NetBSD: pci.c,v 1.149 2015/10/02 05:22:53 msaitoh Exp $");
     40    1.1   mycroft 
     41  1.148     pooka #ifdef _KERNEL_OPT
     42   1.45       cgd #include "opt_pci.h"
     43  1.148     pooka #endif
     44   1.45       cgd 
     45    1.1   mycroft #include <sys/param.h>
     46  1.107  jmcneill #include <sys/malloc.h>
     47   1.10       cgd #include <sys/systm.h>
     48    1.1   mycroft #include <sys/device.h>
     49  1.142  jmcneill #include <sys/module.h>
     50    1.1   mycroft 
     51   1.10       cgd #include <dev/pci/pcireg.h>
     52    1.7       cgd #include <dev/pci/pcivar.h>
     53   1.33       cgd #include <dev/pci/pcidevs.h>
     54   1.76  christos 
     55  1.107  jmcneill #include <net/if.h>
     56  1.107  jmcneill 
     57   1.76  christos #include "locators.h"
     58   1.10       cgd 
     59  1.107  jmcneill static bool pci_child_register(device_t);
     60  1.107  jmcneill 
     61   1.45       cgd #ifdef PCI_CONFIG_DUMP
     62   1.45       cgd int pci_config_dump = 1;
     63   1.45       cgd #else
     64   1.45       cgd int pci_config_dump = 0;
     65   1.45       cgd #endif
     66   1.45       cgd 
     67   1.91     perry int	pciprint(void *, const char *);
     68   1.10       cgd 
     69   1.86  drochner #ifdef PCI_MACHDEP_ENUMERATE_BUS
     70   1.86  drochner #define pci_enumerate_bus PCI_MACHDEP_ENUMERATE_BUS
     71   1.86  drochner #endif
     72   1.86  drochner 
     73   1.25       cgd /*
     74   1.38   thorpej  * Important note about PCI-ISA bridges:
     75   1.38   thorpej  *
     76   1.38   thorpej  * Callbacks are used to configure these devices so that ISA/EISA bridges
     77   1.38   thorpej  * can attach their child busses after PCI configuration is done.
     78   1.25       cgd  *
     79   1.25       cgd  * This works because:
     80   1.25       cgd  *	(1) there can be at most one ISA/EISA bridge per PCI bus, and
     81   1.25       cgd  *	(2) any ISA/EISA bridges must be attached to primary PCI
     82   1.25       cgd  *	    busses (i.e. bus zero).
     83   1.25       cgd  *
     84   1.25       cgd  * That boils down to: there can only be one of these outstanding
     85   1.25       cgd  * at a time, it is cleared when configuring PCI bus 0 before any
     86   1.25       cgd  * subdevices have been found, and it is run after all subdevices
     87   1.25       cgd  * of PCI bus 0 have been found.
     88   1.25       cgd  *
     89   1.25       cgd  * This is needed because there are some (legacy) PCI devices which
     90   1.25       cgd  * can show up as ISA/EISA devices as well (the prime example of which
     91   1.25       cgd  * are VGA controllers).  If you attach ISA from a PCI-ISA/EISA bridge,
     92   1.25       cgd  * and the bridge is seen before the video board is, the board can show
     93   1.25       cgd  * up as an ISA device, and that can (bogusly) complicate the PCI device's
     94   1.25       cgd  * attach code, or make the PCI device not be properly attached at all.
     95   1.38   thorpej  *
     96   1.38   thorpej  * We use the generic config_defer() facility to achieve this.
     97   1.25       cgd  */
     98   1.25       cgd 
     99  1.116    dyoung int
    100  1.114    dyoung pcirescan(device_t self, const char *ifattr, const int *locators)
    101   1.93   thorpej {
    102  1.114    dyoung 	struct pci_softc *sc = device_private(self);
    103   1.93   thorpej 
    104   1.93   thorpej 	KASSERT(ifattr && !strcmp(ifattr, "pci"));
    105   1.93   thorpej 	KASSERT(locators);
    106   1.93   thorpej 
    107  1.114    dyoung 	pci_enumerate_bus(sc, locators, NULL, NULL);
    108  1.128  pgoyette 
    109  1.114    dyoung 	return 0;
    110   1.93   thorpej }
    111   1.93   thorpej 
    112  1.116    dyoung int
    113  1.115      cube pcimatch(device_t parent, cfdata_t cf, void *aux)
    114   1.10       cgd {
    115   1.10       cgd 	struct pcibus_attach_args *pba = aux;
    116   1.10       cgd 
    117   1.10       cgd 	/* Check the locators */
    118   1.89  drochner 	if (cf->cf_loc[PCIBUSCF_BUS] != PCIBUSCF_BUS_DEFAULT &&
    119   1.89  drochner 	    cf->cf_loc[PCIBUSCF_BUS] != pba->pba_bus)
    120  1.123    cegger 		return 0;
    121   1.10       cgd 
    122   1.10       cgd 	/* sanity */
    123   1.10       cgd 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
    124  1.123    cegger 		return 0;
    125   1.10       cgd 
    126   1.10       cgd 	/*
    127   1.10       cgd 	 * XXX check other (hardware?) indicators
    128   1.10       cgd 	 */
    129   1.10       cgd 
    130  1.123    cegger 	return 1;
    131   1.10       cgd }
    132    1.1   mycroft 
    133  1.116    dyoung void
    134  1.114    dyoung pciattach(device_t parent, device_t self, void *aux)
    135   1.34  drochner {
    136   1.34  drochner 	struct pcibus_attach_args *pba = aux;
    137  1.114    dyoung 	struct pci_softc *sc = device_private(self);
    138   1.43   thorpej 	int io_enabled, mem_enabled, mrl_enabled, mrm_enabled, mwi_enabled;
    139   1.43   thorpej 	const char *sep = "";
    140   1.96  drochner 	static const int wildcard[PCICF_NLOCS] = {
    141   1.96  drochner 		PCICF_DEV_DEFAULT, PCICF_FUNCTION_DEFAULT
    142   1.96  drochner 	};
    143   1.34  drochner 
    144  1.115      cube 	sc->sc_dev = self;
    145  1.115      cube 
    146   1.34  drochner 	pci_attach_hook(parent, self, pba);
    147   1.78   thorpej 
    148   1.78   thorpej 	aprint_naive("\n");
    149   1.78   thorpej 	aprint_normal("\n");
    150   1.34  drochner 
    151  1.140    dyoung 	io_enabled = (pba->pba_flags & PCI_FLAGS_IO_OKAY);
    152  1.140    dyoung 	mem_enabled = (pba->pba_flags & PCI_FLAGS_MEM_OKAY);
    153   1.43   thorpej 	mrl_enabled = (pba->pba_flags & PCI_FLAGS_MRL_OKAY);
    154   1.43   thorpej 	mrm_enabled = (pba->pba_flags & PCI_FLAGS_MRM_OKAY);
    155   1.43   thorpej 	mwi_enabled = (pba->pba_flags & PCI_FLAGS_MWI_OKAY);
    156   1.34  drochner 
    157   1.34  drochner 	if (io_enabled == 0 && mem_enabled == 0) {
    158  1.114    dyoung 		aprint_error_dev(self, "no spaces enabled!\n");
    159  1.107  jmcneill 		goto fail;
    160   1.34  drochner 	}
    161   1.34  drochner 
    162   1.78   thorpej #define	PRINT(str)							\
    163   1.78   thorpej do {									\
    164  1.106        ad 	aprint_verbose("%s%s", sep, str);				\
    165   1.78   thorpej 	sep = ", ";							\
    166   1.78   thorpej } while (/*CONSTCOND*/0)
    167   1.43   thorpej 
    168  1.115      cube 	aprint_verbose_dev(self, "");
    169   1.43   thorpej 
    170   1.34  drochner 	if (io_enabled)
    171   1.43   thorpej 		PRINT("i/o space");
    172   1.43   thorpej 	if (mem_enabled)
    173   1.43   thorpej 		PRINT("memory space");
    174  1.106        ad 	aprint_verbose(" enabled");
    175   1.43   thorpej 
    176   1.43   thorpej 	if (mrl_enabled || mrm_enabled || mwi_enabled) {
    177   1.43   thorpej 		if (mrl_enabled)
    178   1.43   thorpej 			PRINT("rd/line");
    179   1.43   thorpej 		if (mrm_enabled)
    180   1.43   thorpej 			PRINT("rd/mult");
    181   1.43   thorpej 		if (mwi_enabled)
    182   1.43   thorpej 			PRINT("wr/inv");
    183  1.106        ad 		aprint_verbose(" ok");
    184   1.34  drochner 	}
    185   1.43   thorpej 
    186  1.106        ad 	aprint_verbose("\n");
    187   1.43   thorpej 
    188   1.43   thorpej #undef PRINT
    189   1.34  drochner 
    190   1.34  drochner 	sc->sc_iot = pba->pba_iot;
    191   1.34  drochner 	sc->sc_memt = pba->pba_memt;
    192   1.34  drochner 	sc->sc_dmat = pba->pba_dmat;
    193   1.80      fvdl 	sc->sc_dmat64 = pba->pba_dmat64;
    194   1.34  drochner 	sc->sc_pc = pba->pba_pc;
    195   1.34  drochner 	sc->sc_bus = pba->pba_bus;
    196   1.62   thorpej 	sc->sc_bridgetag = pba->pba_bridgetag;
    197   1.34  drochner 	sc->sc_maxndevs = pci_bus_maxdevs(pba->pba_pc, pba->pba_bus);
    198   1.34  drochner 	sc->sc_intrswiz = pba->pba_intrswiz;
    199   1.34  drochner 	sc->sc_intrtag = pba->pba_intrtag;
    200   1.34  drochner 	sc->sc_flags = pba->pba_flags;
    201  1.100  jmcneill 
    202  1.115      cube 	device_pmf_driver_set_child_register(sc->sc_dev, pci_child_register);
    203  1.100  jmcneill 
    204  1.115      cube 	pcirescan(sc->sc_dev, "pci", wildcard);
    205  1.107  jmcneill 
    206  1.107  jmcneill fail:
    207  1.107  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    208  1.107  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    209  1.107  jmcneill }
    210  1.107  jmcneill 
    211  1.116    dyoung int
    212  1.114    dyoung pcidetach(device_t self, int flags)
    213  1.107  jmcneill {
    214  1.108    dyoung 	int rc;
    215  1.108    dyoung 
    216  1.108    dyoung 	if ((rc = config_detach_children(self, flags)) != 0)
    217  1.108    dyoung 		return rc;
    218  1.107  jmcneill 	pmf_device_deregister(self);
    219  1.107  jmcneill 	return 0;
    220   1.87  drochner }
    221   1.87  drochner 
    222   1.87  drochner int
    223   1.93   thorpej pciprint(void *aux, const char *pnp)
    224    1.1   mycroft {
    225   1.46  augustss 	struct pci_attach_args *pa = aux;
    226   1.10       cgd 	char devinfo[256];
    227   1.37       cgd 	const struct pci_quirkdata *qd;
    228    1.1   mycroft 
    229   1.10       cgd 	if (pnp) {
    230   1.83    itojun 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
    231   1.75   thorpej 		aprint_normal("%s at %s", devinfo, pnp);
    232   1.10       cgd 	}
    233   1.75   thorpej 	aprint_normal(" dev %d function %d", pa->pa_device, pa->pa_function);
    234   1.45       cgd 	if (pci_config_dump) {
    235   1.45       cgd 		printf(": ");
    236   1.45       cgd 		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    237   1.45       cgd 		if (!pnp)
    238   1.83    itojun 			pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
    239   1.45       cgd 		printf("%s at %s", devinfo, pnp ? pnp : "?");
    240   1.45       cgd 		printf(" dev %d function %d (", pa->pa_device, pa->pa_function);
    241   1.37       cgd #ifdef __i386__
    242   1.45       cgd 		printf("tag %#lx, intrtag %#lx, intrswiz %#lx, intrpin %#lx",
    243   1.45       cgd 		    *(long *)&pa->pa_tag, *(long *)&pa->pa_intrtag,
    244   1.45       cgd 		    (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
    245   1.37       cgd #else
    246   1.54       mrg 		printf("intrswiz %#lx, intrpin %#lx",
    247   1.54       mrg 		    (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
    248   1.36       cgd #endif
    249   1.45       cgd 		printf(", i/o %s, mem %s,",
    250  1.140    dyoung 		    pa->pa_flags & PCI_FLAGS_IO_OKAY ? "on" : "off",
    251  1.140    dyoung 		    pa->pa_flags & PCI_FLAGS_MEM_OKAY ? "on" : "off");
    252   1.45       cgd 		qd = pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
    253   1.45       cgd 		    PCI_PRODUCT(pa->pa_id));
    254   1.45       cgd 		if (qd == NULL) {
    255   1.45       cgd 			printf(" no quirks");
    256   1.45       cgd 		} else {
    257  1.121  christos 			snprintb(devinfo, sizeof (devinfo),
    258   1.82    itojun 			    "\002\001multifn\002singlefn\003skipfunc0"
    259   1.82    itojun 			    "\004skipfunc1\005skipfunc2\006skipfunc3"
    260   1.82    itojun 			    "\007skipfunc4\010skipfunc5\011skipfunc6"
    261  1.121  christos 			    "\012skipfunc7", qd->quirks);
    262   1.45       cgd 			printf(" quirks %s", devinfo);
    263   1.45       cgd 		}
    264   1.45       cgd 		printf(")");
    265   1.37       cgd 	}
    266  1.123    cegger 	return UNCONF;
    267    1.6   mycroft }
    268    1.6   mycroft 
    269    1.6   mycroft int
    270   1.59   thorpej pci_probe_device(struct pci_softc *sc, pcitag_t tag,
    271  1.136    dyoung     int (*match)(const struct pci_attach_args *),
    272  1.136    dyoung     struct pci_attach_args *pap)
    273   1.59   thorpej {
    274   1.59   thorpej 	pci_chipset_tag_t pc = sc->sc_pc;
    275   1.59   thorpej 	struct pci_attach_args pa;
    276  1.145      matt 	pcireg_t id, /* csr, */ pciclass, intr, bhlcr, bar, endbar;
    277  1.146  knakahar #ifdef __HAVE_PCI_MSI_MSIX
    278  1.146  knakahar 	pcireg_t cap;
    279  1.146  knakahar 	int off;
    280  1.146  knakahar #endif
    281  1.132  jmcneill 	int ret, pin, bus, device, function, i, width;
    282   1.94  drochner 	int locs[PCICF_NLOCS];
    283   1.59   thorpej 
    284   1.59   thorpej 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    285   1.59   thorpej 
    286   1.87  drochner 	/* a driver already attached? */
    287  1.117    dyoung 	if (sc->PCI_SC_DEVICESC(device, function).c_dev != NULL && !match)
    288  1.123    cegger 		return 0;
    289   1.87  drochner 
    290   1.81    itojun 	bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    291   1.81    itojun 	if (PCI_HDRTYPE_TYPE(bhlcr) > 2)
    292  1.123    cegger 		return 0;
    293   1.81    itojun 
    294   1.59   thorpej 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    295  1.144    martin 	/* csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG); */
    296  1.145      matt 	pciclass = pci_conf_read(pc, tag, PCI_CLASS_REG);
    297   1.59   thorpej 
    298   1.59   thorpej 	/* Invalid vendor ID value? */
    299   1.59   thorpej 	if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    300  1.123    cegger 		return 0;
    301   1.59   thorpej 	/* XXX Not invalid, but we've done this ~forever. */
    302   1.59   thorpej 	if (PCI_VENDOR(id) == 0)
    303  1.123    cegger 		return 0;
    304   1.59   thorpej 
    305  1.132  jmcneill 	/* Collect memory range info */
    306  1.132  jmcneill 	memset(sc->PCI_SC_DEVICESC(device, function).c_range, 0,
    307  1.132  jmcneill 	    sizeof(sc->PCI_SC_DEVICESC(device, function).c_range));
    308  1.132  jmcneill 	i = 0;
    309  1.135      matt 	switch (PCI_HDRTYPE_TYPE(bhlcr)) {
    310  1.138    dyoung 	case PCI_HDRTYPE_PPB:
    311  1.138    dyoung 		endbar = PCI_MAPREG_PPB_END;
    312  1.138    dyoung 		break;
    313  1.138    dyoung 	case PCI_HDRTYPE_PCB:
    314  1.138    dyoung 		endbar = PCI_MAPREG_PCB_END;
    315  1.138    dyoung 		break;
    316  1.138    dyoung 	default:
    317  1.138    dyoung 		endbar = PCI_MAPREG_END;
    318  1.138    dyoung 		break;
    319  1.135      matt 	}
    320  1.135      matt 	for (bar = PCI_MAPREG_START; bar < endbar; bar += width) {
    321  1.132  jmcneill 		struct pci_range *r;
    322  1.134  jmcneill 		pcireg_t type;
    323  1.132  jmcneill 
    324  1.132  jmcneill 		width = 4;
    325  1.134  jmcneill 		if (pci_mapreg_probe(pc, tag, bar, &type) == 0)
    326  1.134  jmcneill 			continue;
    327  1.134  jmcneill 
    328  1.132  jmcneill 		if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_MEM) {
    329  1.132  jmcneill 			if (PCI_MAPREG_MEM_TYPE(type) ==
    330  1.132  jmcneill 			    PCI_MAPREG_MEM_TYPE_64BIT)
    331  1.132  jmcneill 				width = 8;
    332  1.132  jmcneill 
    333  1.132  jmcneill 			r = &sc->PCI_SC_DEVICESC(device, function).c_range[i++];
    334  1.132  jmcneill 			if (pci_mapreg_info(pc, tag, bar, type,
    335  1.132  jmcneill 			    &r->r_offset, &r->r_size, &r->r_flags) != 0)
    336  1.132  jmcneill 				break;
    337  1.133  macallan 			if ((PCI_VENDOR(id) == PCI_VENDOR_ATI) && (bar == 0x10)
    338  1.137  macallan 			    && (r->r_size == 0x1000000)) {
    339  1.133  macallan 				struct pci_range *nr;
    340  1.133  macallan 				/*
    341  1.133  macallan 				 * this has to be a mach64
    342  1.133  macallan 				 * split things up so each half-aperture can
    343  1.133  macallan 				 * be mapped PREFETCHABLE except the last page
    344  1.133  macallan 				 * which may contain registers
    345  1.133  macallan 				 */
    346  1.133  macallan 				r->r_size = 0x7ff000;
    347  1.133  macallan 				r->r_flags = BUS_SPACE_MAP_LINEAR |
    348  1.133  macallan 					     BUS_SPACE_MAP_PREFETCHABLE;
    349  1.133  macallan 				nr = &sc->PCI_SC_DEVICESC(device,
    350  1.133  macallan 				    function).c_range[i++];
    351  1.133  macallan 				nr->r_offset = r->r_offset + 0x800000;
    352  1.133  macallan 				nr->r_size = 0x7ff000;
    353  1.133  macallan 				nr->r_flags = BUS_SPACE_MAP_LINEAR |
    354  1.133  macallan 					      BUS_SPACE_MAP_PREFETCHABLE;
    355  1.133  macallan 			}
    356  1.133  macallan 
    357  1.132  jmcneill 		}
    358  1.132  jmcneill 	}
    359  1.132  jmcneill 
    360   1.59   thorpej 	pa.pa_iot = sc->sc_iot;
    361   1.59   thorpej 	pa.pa_memt = sc->sc_memt;
    362   1.59   thorpej 	pa.pa_dmat = sc->sc_dmat;
    363   1.80      fvdl 	pa.pa_dmat64 = sc->sc_dmat64;
    364   1.59   thorpej 	pa.pa_pc = pc;
    365   1.63   thorpej 	pa.pa_bus = bus;
    366   1.59   thorpej 	pa.pa_device = device;
    367   1.59   thorpej 	pa.pa_function = function;
    368   1.59   thorpej 	pa.pa_tag = tag;
    369   1.59   thorpej 	pa.pa_id = id;
    370  1.145      matt 	pa.pa_class = pciclass;
    371   1.59   thorpej 
    372   1.59   thorpej 	/*
    373   1.59   thorpej 	 * Set up memory, I/O enable, and PCI command flags
    374   1.59   thorpej 	 * as appropriate.
    375   1.59   thorpej 	 */
    376   1.59   thorpej 	pa.pa_flags = sc->sc_flags;
    377   1.59   thorpej 
    378   1.59   thorpej 	/*
    379   1.59   thorpej 	 * If the cache line size is not configured, then
    380   1.59   thorpej 	 * clear the MRL/MRM/MWI command-ok flags.
    381   1.59   thorpej 	 */
    382  1.138    dyoung 	if (PCI_CACHELINE(bhlcr) == 0) {
    383   1.59   thorpej 		pa.pa_flags &= ~(PCI_FLAGS_MRL_OKAY|
    384   1.59   thorpej 		    PCI_FLAGS_MRM_OKAY|PCI_FLAGS_MWI_OKAY);
    385  1.138    dyoung 	}
    386   1.59   thorpej 
    387   1.64  sommerfe 	if (sc->sc_bridgetag == NULL) {
    388   1.59   thorpej 		pa.pa_intrswiz = 0;
    389   1.59   thorpej 		pa.pa_intrtag = tag;
    390   1.59   thorpej 	} else {
    391   1.59   thorpej 		pa.pa_intrswiz = sc->sc_intrswiz + device;
    392   1.59   thorpej 		pa.pa_intrtag = sc->sc_intrtag;
    393   1.59   thorpej 	}
    394   1.81    itojun 
    395   1.81    itojun 	intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    396   1.81    itojun 
    397   1.59   thorpej 	pin = PCI_INTERRUPT_PIN(intr);
    398   1.65  sommerfe 	pa.pa_rawintrpin = pin;
    399   1.59   thorpej 	if (pin == PCI_INTERRUPT_PIN_NONE) {
    400   1.59   thorpej 		/* no interrupt */
    401   1.59   thorpej 		pa.pa_intrpin = 0;
    402   1.59   thorpej 	} else {
    403   1.59   thorpej 		/*
    404   1.59   thorpej 		 * swizzle it based on the number of busses we're
    405   1.59   thorpej 		 * behind and our device number.
    406   1.59   thorpej 		 */
    407   1.59   thorpej 		pa.pa_intrpin = 	/* XXX */
    408   1.59   thorpej 		    ((pin + pa.pa_intrswiz - 1) % 4) + 1;
    409   1.59   thorpej 	}
    410   1.59   thorpej 	pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
    411   1.59   thorpej 
    412  1.146  knakahar #ifdef __HAVE_PCI_MSI_MSIX
    413  1.146  knakahar 	if (pci_get_ht_capability(pc, tag, PCI_HT_CAP_MSIMAP, &off, &cap)) {
    414  1.146  knakahar 		/*
    415  1.146  knakahar 		 * XXX Should we enable MSI mapping ourselves on
    416  1.146  knakahar 		 * systems that have it disabled?
    417  1.146  knakahar 		 */
    418  1.146  knakahar 		if (cap & PCI_HT_MSI_ENABLED) {
    419  1.146  knakahar 			uint64_t addr;
    420  1.146  knakahar 			if ((cap & PCI_HT_MSI_FIXED) == 0) {
    421  1.146  knakahar 				addr = pci_conf_read(pc, tag,
    422  1.146  knakahar 				    off + PCI_HT_MSI_ADDR_LO);
    423  1.146  knakahar 				addr |= (uint64_t)pci_conf_read(pc, tag,
    424  1.146  knakahar 				    off + PCI_HT_MSI_ADDR_HI) << 32;
    425  1.146  knakahar 			} else
    426  1.146  knakahar 				addr = PCI_HT_MSI_FIXED_ADDR;
    427  1.146  knakahar 
    428  1.146  knakahar 			/*
    429  1.146  knakahar 			 * XXX This will fail to enable MSI on systems
    430  1.146  knakahar 			 * that don't use the canonical address.
    431  1.146  knakahar 			 */
    432  1.146  knakahar 			if (addr == PCI_HT_MSI_FIXED_ADDR) {
    433  1.146  knakahar 				pa.pa_flags |= PCI_FLAGS_MSI_OKAY;
    434  1.146  knakahar 				pa.pa_flags |= PCI_FLAGS_MSIX_OKAY;
    435  1.146  knakahar 			}
    436  1.146  knakahar 		}
    437  1.146  knakahar 	}
    438  1.146  knakahar #endif
    439  1.146  knakahar 
    440   1.59   thorpej 	if (match != NULL) {
    441   1.59   thorpej 		ret = (*match)(&pa);
    442   1.59   thorpej 		if (ret != 0 && pap != NULL)
    443   1.59   thorpej 			*pap = pa;
    444   1.59   thorpej 	} else {
    445  1.117    dyoung 		struct pci_child *c;
    446   1.94  drochner 		locs[PCICF_DEV] = device;
    447   1.94  drochner 		locs[PCICF_FUNCTION] = function;
    448   1.87  drochner 
    449  1.117    dyoung 		c = &sc->PCI_SC_DEVICESC(device, function);
    450  1.117    dyoung 		pci_conf_capture(pc, tag, &c->c_conf);
    451  1.117    dyoung 		if (pci_get_powerstate(pc, tag, &c->c_powerstate) == 0)
    452  1.117    dyoung 			c->c_psok = true;
    453  1.117    dyoung 		else
    454  1.117    dyoung 			c->c_psok = false;
    455  1.124    dyoung 
    456  1.124    dyoung 		c->c_dev = config_found_sm_loc(sc->sc_dev, "pci", locs, &pa,
    457  1.124    dyoung 					     pciprint, config_stdsubmatch);
    458  1.124    dyoung 
    459  1.124    dyoung 		ret = (c->c_dev != NULL);
    460   1.59   thorpej 	}
    461   1.59   thorpej 
    462  1.123    cegger 	return ret;
    463   1.59   thorpej }
    464   1.59   thorpej 
    465  1.116    dyoung void
    466  1.114    dyoung pcidevdetached(device_t self, device_t child)
    467   1.87  drochner {
    468  1.117    dyoung 	struct pci_softc *sc = device_private(self);
    469   1.87  drochner 	int d, f;
    470  1.117    dyoung 	pcitag_t tag;
    471  1.117    dyoung 	struct pci_child *c;
    472   1.87  drochner 
    473  1.114    dyoung 	d = device_locator(child, PCICF_DEV);
    474  1.114    dyoung 	f = device_locator(child, PCICF_FUNCTION);
    475   1.87  drochner 
    476  1.117    dyoung 	c = &sc->PCI_SC_DEVICESC(d, f);
    477  1.117    dyoung 
    478  1.117    dyoung 	KASSERT(c->c_dev == child);
    479   1.87  drochner 
    480  1.117    dyoung 	tag = pci_make_tag(sc->sc_pc, sc->sc_bus, d, f);
    481  1.117    dyoung 	if (c->c_psok)
    482  1.117    dyoung 		pci_set_powerstate(sc->sc_pc, tag, c->c_powerstate);
    483  1.117    dyoung 	pci_conf_restore(sc->sc_pc, tag, &c->c_conf);
    484  1.117    dyoung 	c->c_dev = NULL;
    485   1.87  drochner }
    486   1.87  drochner 
    487  1.122    dyoung CFATTACH_DECL3_NEW(pci, sizeof(struct pci_softc),
    488  1.122    dyoung     pcimatch, pciattach, pcidetach, NULL, pcirescan, pcidevdetached,
    489  1.122    dyoung     DVF_DETACH_SHUTDOWN);
    490  1.107  jmcneill 
    491   1.59   thorpej int
    492   1.93   thorpej pci_get_capability(pci_chipset_tag_t pc, pcitag_t tag, int capid,
    493   1.93   thorpej     int *offset, pcireg_t *value)
    494   1.40  drochner {
    495   1.40  drochner 	pcireg_t reg;
    496   1.40  drochner 	unsigned int ofs;
    497   1.40  drochner 
    498   1.40  drochner 	reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    499   1.40  drochner 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    500  1.123    cegger 		return 0;
    501   1.40  drochner 
    502   1.48    kleink 	/* Determine the Capability List Pointer register to start with. */
    503   1.47    kleink 	reg = pci_conf_read(pc, tag, PCI_BHLC_REG);
    504   1.47    kleink 	switch (PCI_HDRTYPE_TYPE(reg)) {
    505   1.47    kleink 	case 0:	/* standard device header */
    506  1.104     joerg 	case 1: /* PCI-PCI bridge header */
    507   1.47    kleink 		ofs = PCI_CAPLISTPTR_REG;
    508   1.47    kleink 		break;
    509   1.47    kleink 	case 2:	/* PCI-CardBus Bridge header */
    510   1.47    kleink 		ofs = PCI_CARDBUS_CAPLISTPTR_REG;
    511   1.47    kleink 		break;
    512   1.47    kleink 	default:
    513  1.123    cegger 		return 0;
    514   1.47    kleink 	}
    515   1.47    kleink 
    516   1.47    kleink 	ofs = PCI_CAPLIST_PTR(pci_conf_read(pc, tag, ofs));
    517   1.40  drochner 	while (ofs != 0) {
    518  1.119     joerg 		if ((ofs & 3) || (ofs < 0x40)) {
    519  1.119     joerg 			int bus, device, function;
    520  1.119     joerg 
    521  1.119     joerg 			pci_decompose_tag(pc, tag, &bus, &device, &function);
    522  1.119     joerg 
    523  1.119     joerg 			printf("Skipping broken PCI header on %d:%d:%d\n",
    524  1.119     joerg 			    bus, device, function);
    525  1.119     joerg 			break;
    526  1.119     joerg 		}
    527   1.40  drochner 		reg = pci_conf_read(pc, tag, ofs);
    528   1.40  drochner 		if (PCI_CAPLIST_CAP(reg) == capid) {
    529   1.40  drochner 			if (offset)
    530   1.40  drochner 				*offset = ofs;
    531   1.40  drochner 			if (value)
    532   1.40  drochner 				*value = reg;
    533  1.123    cegger 			return 1;
    534   1.40  drochner 		}
    535   1.40  drochner 		ofs = PCI_CAPLIST_NEXT(reg);
    536   1.40  drochner 	}
    537   1.40  drochner 
    538  1.123    cegger 	return 0;
    539   1.55      fvdl }
    540   1.55      fvdl 
    541   1.55      fvdl int
    542  1.146  knakahar pci_get_ht_capability(pci_chipset_tag_t pc, pcitag_t tag, int capid,
    543  1.146  knakahar     int *offset, pcireg_t *value)
    544  1.146  knakahar {
    545  1.146  knakahar 	pcireg_t reg;
    546  1.146  knakahar 	unsigned int ofs;
    547  1.146  knakahar 
    548  1.146  knakahar 	if (pci_get_capability(pc, tag, PCI_CAP_LDT, &ofs, NULL) == 0)
    549  1.146  knakahar 		return 0;
    550  1.146  knakahar 
    551  1.146  knakahar 	while (ofs != 0) {
    552  1.146  knakahar #ifdef DIAGNOSTIC
    553  1.146  knakahar 		if ((ofs & 3) || (ofs < 0x40))
    554  1.146  knakahar 			panic("pci_get_ht_capability");
    555  1.146  knakahar #endif
    556  1.146  knakahar 		reg = pci_conf_read(pc, tag, ofs);
    557  1.146  knakahar 		if (PCI_HT_CAP(reg) == capid) {
    558  1.146  knakahar 			if (offset)
    559  1.146  knakahar 				*offset = ofs;
    560  1.146  knakahar 			if (value)
    561  1.146  knakahar 				*value = reg;
    562  1.146  knakahar 			return 1;
    563  1.146  knakahar 		}
    564  1.146  knakahar 		ofs = PCI_CAPLIST_NEXT(reg);
    565  1.146  knakahar 	}
    566  1.146  knakahar 
    567  1.146  knakahar 	return 0;
    568  1.146  knakahar }
    569  1.146  knakahar 
    570  1.147   msaitoh /*
    571  1.147   msaitoh  * return number of the devices's MSI vectors
    572  1.147   msaitoh  * return 0 if the device does not support MSI
    573  1.147   msaitoh  */
    574  1.147   msaitoh int
    575  1.147   msaitoh pci_msi_count(pci_chipset_tag_t pc, pcitag_t tag)
    576  1.147   msaitoh {
    577  1.147   msaitoh 	pcireg_t reg;
    578  1.147   msaitoh 	uint32_t mmc;
    579  1.147   msaitoh 	int count, offset;
    580  1.147   msaitoh 
    581  1.147   msaitoh 	if (pci_get_capability(pc, tag, PCI_CAP_MSI, &offset, NULL) == 0)
    582  1.147   msaitoh 		return 0;
    583  1.147   msaitoh 
    584  1.147   msaitoh 	reg = pci_conf_read(pc, tag, offset + PCI_MSI_CTL);
    585  1.147   msaitoh 	mmc = PCI_MSI_CTL_MMC(reg);
    586  1.147   msaitoh 	count = 1 << mmc;
    587  1.147   msaitoh 	if (count > PCI_MSI_MAX_VECTORS) {
    588  1.147   msaitoh 		aprint_error("detect an illegal device! The device use reserved MMC values.\n");
    589  1.147   msaitoh 		return 0;
    590  1.147   msaitoh 	}
    591  1.147   msaitoh 
    592  1.147   msaitoh 	return count;
    593  1.147   msaitoh }
    594  1.147   msaitoh 
    595  1.147   msaitoh /*
    596  1.147   msaitoh  * return number of the devices's MSI-X vectors
    597  1.147   msaitoh  * return 0 if the device does not support MSI-X
    598  1.147   msaitoh  */
    599  1.147   msaitoh int
    600  1.147   msaitoh pci_msix_count(pci_chipset_tag_t pc, pcitag_t tag)
    601  1.147   msaitoh {
    602  1.147   msaitoh 	pcireg_t reg;
    603  1.147   msaitoh 	int offset;
    604  1.147   msaitoh 
    605  1.147   msaitoh 	if (pci_get_capability(pc, tag, PCI_CAP_MSIX, &offset, NULL) == 0)
    606  1.147   msaitoh 		return 0;
    607  1.147   msaitoh 
    608  1.147   msaitoh 	reg = pci_conf_read(pc, tag, offset + PCI_MSIX_CTL);
    609  1.147   msaitoh 
    610  1.147   msaitoh 	return PCI_MSIX_CTL_TBLSIZE(reg);
    611  1.147   msaitoh }
    612  1.147   msaitoh 
    613  1.146  knakahar int
    614  1.149   msaitoh pci_get_ext_capability(pci_chipset_tag_t pc, pcitag_t tag, int capid,
    615  1.149   msaitoh     int *offset, pcireg_t *value)
    616  1.149   msaitoh {
    617  1.149   msaitoh 	pcireg_t reg;
    618  1.149   msaitoh 	unsigned int ofs;
    619  1.149   msaitoh 
    620  1.149   msaitoh 	/* Only supported for PCI-express devices */
    621  1.149   msaitoh 	if (!pci_get_capability(pc, tag, PCI_CAP_PCIEXPRESS, NULL, NULL))
    622  1.149   msaitoh 		return 0;
    623  1.149   msaitoh 
    624  1.149   msaitoh 	ofs = PCI_EXTCAPLIST_BASE;
    625  1.149   msaitoh 	reg = pci_conf_read(pc, tag, ofs);
    626  1.149   msaitoh 	if (reg == 0xffffffff || reg == 0)
    627  1.149   msaitoh 		return 0;
    628  1.149   msaitoh 
    629  1.149   msaitoh 	for (;;) {
    630  1.149   msaitoh #ifdef DIAGNOSTIC
    631  1.149   msaitoh 		if ((ofs & 3) || ofs < PCI_EXTCAPLIST_BASE)
    632  1.149   msaitoh 			panic("%s: invalid offset %u", __func__, ofs);
    633  1.149   msaitoh #endif
    634  1.149   msaitoh 		if (PCI_EXTCAPLIST_CAP(reg) == capid) {
    635  1.149   msaitoh 			if (offset != NULL)
    636  1.149   msaitoh 				*offset = ofs;
    637  1.149   msaitoh 			if (value != NULL)
    638  1.149   msaitoh 				*value = reg;
    639  1.149   msaitoh 			return 1;
    640  1.149   msaitoh 		}
    641  1.149   msaitoh 		ofs = PCI_EXTCAPLIST_NEXT(reg);
    642  1.149   msaitoh 		if (ofs == 0)
    643  1.149   msaitoh 			break;
    644  1.149   msaitoh 		reg = pci_conf_read(pc, tag, ofs);
    645  1.149   msaitoh 	}
    646  1.149   msaitoh 
    647  1.149   msaitoh 	return 0;
    648  1.149   msaitoh }
    649  1.149   msaitoh 
    650  1.149   msaitoh int
    651   1.55      fvdl pci_find_device(struct pci_attach_args *pa,
    652  1.136    dyoung 		int (*match)(const struct pci_attach_args *))
    653   1.55      fvdl {
    654   1.59   thorpej 	extern struct cfdriver pci_cd;
    655  1.114    dyoung 	device_t pcidev;
    656   1.55      fvdl 	int i;
    657   1.87  drochner 	static const int wildcard[2] = {
    658   1.87  drochner 		PCICF_DEV_DEFAULT,
    659   1.87  drochner 		PCICF_FUNCTION_DEFAULT
    660   1.87  drochner 	};
    661   1.55      fvdl 
    662   1.55      fvdl 	for (i = 0; i < pci_cd.cd_ndevs; i++) {
    663  1.118    cegger 		pcidev = device_lookup(&pci_cd, i);
    664   1.59   thorpej 		if (pcidev != NULL &&
    665  1.115      cube 		    pci_enumerate_bus(device_private(pcidev), wildcard,
    666   1.59   thorpej 		    		      match, pa) != 0)
    667  1.123    cegger 			return 1;
    668   1.59   thorpej 	}
    669  1.123    cegger 	return 0;
    670   1.59   thorpej }
    671   1.59   thorpej 
    672   1.86  drochner #ifndef PCI_MACHDEP_ENUMERATE_BUS
    673   1.59   thorpej /*
    674   1.59   thorpej  * Generic PCI bus enumeration routine.  Used unless machine-dependent
    675   1.59   thorpej  * code needs to provide something else.
    676   1.59   thorpej  */
    677   1.59   thorpej int
    678   1.87  drochner pci_enumerate_bus(struct pci_softc *sc, const int *locators,
    679  1.136    dyoung     int (*match)(const struct pci_attach_args *), struct pci_attach_args *pap)
    680   1.59   thorpej {
    681   1.59   thorpej 	pci_chipset_tag_t pc = sc->sc_pc;
    682   1.59   thorpej 	int device, function, nfunctions, ret;
    683   1.59   thorpej 	const struct pci_quirkdata *qd;
    684   1.59   thorpej 	pcireg_t id, bhlcr;
    685   1.59   thorpej 	pcitag_t tag;
    686  1.141    dyoung 	uint8_t devs[32];
    687  1.141    dyoung 	int i, n;
    688  1.141    dyoung 
    689  1.141    dyoung 	n = pci_bus_devorder(sc->sc_pc, sc->sc_bus, devs, __arraycount(devs));
    690  1.141    dyoung 	for (i = 0; i < n; i++) {
    691  1.141    dyoung 		device = devs[i];
    692   1.59   thorpej 
    693   1.87  drochner 		if ((locators[PCICF_DEV] != PCICF_DEV_DEFAULT) &&
    694   1.87  drochner 		    (locators[PCICF_DEV] != device))
    695   1.87  drochner 			continue;
    696   1.87  drochner 
    697   1.59   thorpej 		tag = pci_make_tag(pc, sc->sc_bus, device, 0);
    698   1.81    itojun 
    699   1.81    itojun 		bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    700   1.81    itojun 		if (PCI_HDRTYPE_TYPE(bhlcr) > 2)
    701   1.81    itojun 			continue;
    702   1.81    itojun 
    703   1.59   thorpej 		id = pci_conf_read(pc, tag, PCI_ID_REG);
    704   1.59   thorpej 
    705   1.59   thorpej 		/* Invalid vendor ID value? */
    706   1.59   thorpej 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    707   1.59   thorpej 			continue;
    708   1.59   thorpej 		/* XXX Not invalid, but we've done this ~forever. */
    709   1.59   thorpej 		if (PCI_VENDOR(id) == 0)
    710   1.59   thorpej 			continue;
    711   1.59   thorpej 
    712   1.59   thorpej 		qd = pci_lookup_quirkdata(PCI_VENDOR(id), PCI_PRODUCT(id));
    713   1.59   thorpej 
    714   1.81    itojun 		if (qd != NULL &&
    715   1.81    itojun 		      (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0)
    716   1.59   thorpej 			nfunctions = 8;
    717   1.81    itojun 		else if (qd != NULL &&
    718   1.81    itojun 		      (qd->quirks & PCI_QUIRK_MONOFUNCTION) != 0)
    719   1.81    itojun 			nfunctions = 1;
    720   1.59   thorpej 		else
    721   1.81    itojun 			nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    722   1.59   thorpej 
    723  1.143      matt #ifdef __PCI_DEV_FUNCORDER
    724  1.143      matt 		char funcs[8];
    725  1.143      matt 		int j;
    726  1.143      matt 		for (j = 0; j < nfunctions; j++) {
    727  1.143      matt 			funcs[j] = j;
    728  1.143      matt 		}
    729  1.143      matt 		if (j < __arraycount(funcs))
    730  1.143      matt 			funcs[j] = -1;
    731  1.143      matt 		if (nfunctions > 1) {
    732  1.143      matt 			pci_dev_funcorder(sc->sc_pc, sc->sc_bus, device,
    733  1.143      matt 			    nfunctions, funcs);
    734  1.143      matt 		}
    735  1.143      matt 		for (j = 0;
    736  1.143      matt 		     j < 8 && (function = funcs[j]) < 8 && function >= 0;
    737  1.143      matt 		     j++) {
    738  1.143      matt #else
    739   1.59   thorpej 		for (function = 0; function < nfunctions; function++) {
    740  1.143      matt #endif
    741   1.87  drochner 			if ((locators[PCICF_FUNCTION] != PCICF_FUNCTION_DEFAULT)
    742   1.87  drochner 			    && (locators[PCICF_FUNCTION] != function))
    743   1.87  drochner 				continue;
    744   1.87  drochner 
    745   1.81    itojun 			if (qd != NULL &&
    746   1.81    itojun 			    (qd->quirks & PCI_QUIRK_SKIP_FUNC(function)) != 0)
    747   1.81    itojun 				continue;
    748   1.59   thorpej 			tag = pci_make_tag(pc, sc->sc_bus, device, function);
    749   1.59   thorpej 			ret = pci_probe_device(sc, tag, match, pap);
    750   1.59   thorpej 			if (match != NULL && ret != 0)
    751  1.123    cegger 				return ret;
    752   1.59   thorpej 		}
    753   1.55      fvdl 	}
    754  1.123    cegger 	return 0;
    755   1.66  tshiozak }
    756   1.86  drochner #endif /* PCI_MACHDEP_ENUMERATE_BUS */
    757   1.66  tshiozak 
    758   1.77   thorpej 
    759   1.77   thorpej /*
    760   1.77   thorpej  * Vital Product Data (PCI 2.2)
    761   1.77   thorpej  */
    762   1.77   thorpej 
    763   1.77   thorpej int
    764   1.77   thorpej pci_vpd_read(pci_chipset_tag_t pc, pcitag_t tag, int offset, int count,
    765   1.77   thorpej     pcireg_t *data)
    766   1.77   thorpej {
    767   1.77   thorpej 	uint32_t reg;
    768   1.77   thorpej 	int ofs, i, j;
    769   1.77   thorpej 
    770   1.77   thorpej 	KASSERT(data != NULL);
    771   1.77   thorpej 	KASSERT((offset + count) < 0x7fff);
    772   1.77   thorpej 
    773   1.77   thorpej 	if (pci_get_capability(pc, tag, PCI_CAP_VPD, &ofs, &reg) == 0)
    774  1.123    cegger 		return 1;
    775   1.77   thorpej 
    776   1.77   thorpej 	for (i = 0; i < count; offset += sizeof(*data), i++) {
    777   1.77   thorpej 		reg &= 0x0000ffff;
    778   1.77   thorpej 		reg &= ~PCI_VPD_OPFLAG;
    779   1.77   thorpej 		reg |= PCI_VPD_ADDRESS(offset);
    780   1.77   thorpej 		pci_conf_write(pc, tag, ofs, reg);
    781   1.77   thorpej 
    782   1.77   thorpej 		/*
    783   1.77   thorpej 		 * PCI 2.2 does not specify how long we should poll
    784   1.77   thorpej 		 * for completion nor whether the operation can fail.
    785   1.77   thorpej 		 */
    786   1.77   thorpej 		j = 0;
    787   1.77   thorpej 		do {
    788   1.77   thorpej 			if (j++ == 20)
    789  1.123    cegger 				return 1;
    790   1.77   thorpej 			delay(4);
    791   1.77   thorpej 			reg = pci_conf_read(pc, tag, ofs);
    792   1.77   thorpej 		} while ((reg & PCI_VPD_OPFLAG) == 0);
    793   1.77   thorpej 		data[i] = pci_conf_read(pc, tag, PCI_VPD_DATAREG(ofs));
    794   1.77   thorpej 	}
    795   1.77   thorpej 
    796  1.123    cegger 	return 0;
    797   1.77   thorpej }
    798   1.77   thorpej 
    799   1.77   thorpej int
    800   1.77   thorpej pci_vpd_write(pci_chipset_tag_t pc, pcitag_t tag, int offset, int count,
    801   1.77   thorpej     pcireg_t *data)
    802   1.77   thorpej {
    803   1.77   thorpej 	pcireg_t reg;
    804   1.77   thorpej 	int ofs, i, j;
    805   1.77   thorpej 
    806   1.77   thorpej 	KASSERT(data != NULL);
    807   1.77   thorpej 	KASSERT((offset + count) < 0x7fff);
    808   1.77   thorpej 
    809   1.77   thorpej 	if (pci_get_capability(pc, tag, PCI_CAP_VPD, &ofs, &reg) == 0)
    810  1.123    cegger 		return 1;
    811   1.77   thorpej 
    812   1.77   thorpej 	for (i = 0; i < count; offset += sizeof(*data), i++) {
    813   1.77   thorpej 		pci_conf_write(pc, tag, PCI_VPD_DATAREG(ofs), data[i]);
    814   1.77   thorpej 
    815   1.77   thorpej 		reg &= 0x0000ffff;
    816   1.79   thorpej 		reg |= PCI_VPD_OPFLAG;
    817   1.77   thorpej 		reg |= PCI_VPD_ADDRESS(offset);
    818   1.77   thorpej 		pci_conf_write(pc, tag, ofs, reg);
    819   1.77   thorpej 
    820   1.77   thorpej 		/*
    821   1.77   thorpej 		 * PCI 2.2 does not specify how long we should poll
    822   1.77   thorpej 		 * for completion nor whether the operation can fail.
    823   1.77   thorpej 		 */
    824   1.77   thorpej 		j = 0;
    825   1.77   thorpej 		do {
    826   1.77   thorpej 			if (j++ == 20)
    827  1.123    cegger 				return 1;
    828   1.77   thorpej 			delay(1);
    829   1.77   thorpej 			reg = pci_conf_read(pc, tag, ofs);
    830   1.79   thorpej 		} while (reg & PCI_VPD_OPFLAG);
    831   1.77   thorpej 	}
    832   1.77   thorpej 
    833  1.123    cegger 	return 0;
    834   1.80      fvdl }
    835   1.80      fvdl 
    836   1.80      fvdl int
    837  1.136    dyoung pci_dma64_available(const struct pci_attach_args *pa)
    838   1.92     perry {
    839   1.80      fvdl #ifdef _PCI_HAVE_DMA64
    840  1.120    bouyer 	if (BUS_DMA_TAG_VALID(pa->pa_dmat64))
    841   1.80      fvdl                         return 1;
    842   1.80      fvdl #endif
    843   1.80      fvdl         return 0;
    844    1.1   mycroft }
    845   1.90  jmcneill 
    846   1.90  jmcneill void
    847   1.90  jmcneill pci_conf_capture(pci_chipset_tag_t pc, pcitag_t tag,
    848   1.90  jmcneill 		  struct pci_conf_state *pcs)
    849   1.90  jmcneill {
    850   1.90  jmcneill 	int off;
    851   1.90  jmcneill 
    852   1.90  jmcneill 	for (off = 0; off < 16; off++)
    853   1.90  jmcneill 		pcs->reg[off] = pci_conf_read(pc, tag, (off * 4));
    854   1.90  jmcneill 
    855   1.90  jmcneill 	return;
    856   1.90  jmcneill }
    857   1.90  jmcneill 
    858   1.90  jmcneill void
    859   1.90  jmcneill pci_conf_restore(pci_chipset_tag_t pc, pcitag_t tag,
    860   1.90  jmcneill 		  struct pci_conf_state *pcs)
    861   1.90  jmcneill {
    862   1.90  jmcneill 	int off;
    863  1.107  jmcneill 	pcireg_t val;
    864   1.90  jmcneill 
    865  1.107  jmcneill 	for (off = 15; off >= 0; off--) {
    866  1.107  jmcneill 		val = pci_conf_read(pc, tag, (off * 4));
    867  1.107  jmcneill 		if (val != pcs->reg[off])
    868  1.107  jmcneill 			pci_conf_write(pc, tag, (off * 4), pcs->reg[off]);
    869  1.107  jmcneill 	}
    870   1.90  jmcneill 
    871   1.90  jmcneill 	return;
    872   1.90  jmcneill }
    873   1.93   thorpej 
    874   1.99  christos /*
    875   1.99  christos  * Power Management Capability (Rev 2.2)
    876   1.99  christos  */
    877  1.107  jmcneill static int
    878  1.107  jmcneill pci_get_powerstate_int(pci_chipset_tag_t pc, pcitag_t tag , pcireg_t *state,
    879  1.107  jmcneill     int offset)
    880   1.99  christos {
    881  1.107  jmcneill 	pcireg_t value, now;
    882   1.99  christos 
    883   1.99  christos 	value = pci_conf_read(pc, tag, offset + PCI_PMCSR);
    884   1.99  christos 	now = value & PCI_PMCSR_STATE_MASK;
    885   1.99  christos 	switch (now) {
    886   1.99  christos 	case PCI_PMCSR_STATE_D0:
    887   1.99  christos 	case PCI_PMCSR_STATE_D1:
    888   1.99  christos 	case PCI_PMCSR_STATE_D2:
    889   1.99  christos 	case PCI_PMCSR_STATE_D3:
    890   1.99  christos 		*state = now;
    891   1.99  christos 		return 0;
    892   1.99  christos 	default:
    893   1.99  christos 		return EINVAL;
    894   1.99  christos 	}
    895   1.99  christos }
    896   1.99  christos 
    897   1.99  christos int
    898  1.107  jmcneill pci_get_powerstate(pci_chipset_tag_t pc, pcitag_t tag , pcireg_t *state)
    899   1.99  christos {
    900   1.99  christos 	int offset;
    901  1.107  jmcneill 	pcireg_t value;
    902   1.99  christos 
    903   1.99  christos 	if (!pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, &value))
    904   1.99  christos 		return EOPNOTSUPP;
    905   1.99  christos 
    906  1.107  jmcneill 	return pci_get_powerstate_int(pc, tag, state, offset);
    907  1.107  jmcneill }
    908  1.107  jmcneill 
    909  1.107  jmcneill static int
    910  1.107  jmcneill pci_set_powerstate_int(pci_chipset_tag_t pc, pcitag_t tag, pcireg_t state,
    911  1.107  jmcneill     int offset, pcireg_t cap_reg)
    912  1.107  jmcneill {
    913  1.107  jmcneill 	pcireg_t value, cap, now;
    914  1.107  jmcneill 
    915  1.107  jmcneill 	cap = cap_reg >> PCI_PMCR_SHIFT;
    916   1.99  christos 	value = pci_conf_read(pc, tag, offset + PCI_PMCSR);
    917   1.99  christos 	now = value & PCI_PMCSR_STATE_MASK;
    918   1.99  christos 	value &= ~PCI_PMCSR_STATE_MASK;
    919   1.99  christos 
    920   1.99  christos 	if (now == state)
    921   1.99  christos 		return 0;
    922   1.99  christos 	switch (state) {
    923   1.99  christos 	case PCI_PMCSR_STATE_D0:
    924   1.99  christos 		break;
    925   1.99  christos 	case PCI_PMCSR_STATE_D1:
    926  1.107  jmcneill 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3) {
    927  1.107  jmcneill 			printf("invalid transition from %d to D1\n", (int)now);
    928   1.99  christos 			return EINVAL;
    929  1.107  jmcneill 		}
    930  1.107  jmcneill 		if (!(cap & PCI_PMCR_D1SUPP)) {
    931  1.107  jmcneill 			printf("D1 not supported\n");
    932   1.99  christos 			return EOPNOTSUPP;
    933  1.107  jmcneill 		}
    934   1.99  christos 		break;
    935   1.99  christos 	case PCI_PMCSR_STATE_D2:
    936  1.107  jmcneill 		if (now == PCI_PMCSR_STATE_D3) {
    937  1.107  jmcneill 			printf("invalid transition from %d to D2\n", (int)now);
    938   1.99  christos 			return EINVAL;
    939  1.107  jmcneill 		}
    940  1.107  jmcneill 		if (!(cap & PCI_PMCR_D2SUPP)) {
    941  1.107  jmcneill 			printf("D2 not supported\n");
    942   1.99  christos 			return EOPNOTSUPP;
    943  1.107  jmcneill 		}
    944   1.99  christos 		break;
    945   1.99  christos 	case PCI_PMCSR_STATE_D3:
    946   1.99  christos 		break;
    947   1.99  christos 	default:
    948   1.99  christos 		return EINVAL;
    949   1.99  christos 	}
    950  1.112    dyoung 	value |= state;
    951   1.99  christos 	pci_conf_write(pc, tag, offset + PCI_PMCSR, value);
    952  1.111  drochner 	/* delay according to pcipm1.2, ch. 5.6.1 */
    953  1.112    dyoung 	if (state == PCI_PMCSR_STATE_D3 || now == PCI_PMCSR_STATE_D3)
    954  1.110  jmcneill 		DELAY(10000);
    955  1.112    dyoung 	else if (state == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D2)
    956  1.110  jmcneill 		DELAY(200);
    957  1.110  jmcneill 
    958   1.99  christos 	return 0;
    959   1.99  christos }
    960   1.99  christos 
    961   1.99  christos int
    962  1.107  jmcneill pci_set_powerstate(pci_chipset_tag_t pc, pcitag_t tag, pcireg_t state)
    963  1.107  jmcneill {
    964  1.107  jmcneill 	int offset;
    965  1.107  jmcneill 	pcireg_t value;
    966  1.107  jmcneill 
    967  1.107  jmcneill 	if (!pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, &value)) {
    968  1.107  jmcneill 		printf("pci_set_powerstate not supported\n");
    969  1.107  jmcneill 		return EOPNOTSUPP;
    970  1.107  jmcneill 	}
    971  1.107  jmcneill 
    972  1.107  jmcneill 	return pci_set_powerstate_int(pc, tag, state, offset, value);
    973  1.107  jmcneill }
    974  1.107  jmcneill 
    975  1.107  jmcneill int
    976  1.114    dyoung pci_activate(pci_chipset_tag_t pc, pcitag_t tag, device_t dev,
    977  1.114    dyoung     int (*wakefun)(pci_chipset_tag_t, pcitag_t, device_t, pcireg_t))
    978   1.99  christos {
    979   1.99  christos 	pcireg_t pmode;
    980   1.99  christos 	int error;
    981   1.99  christos 
    982   1.99  christos 	if ((error = pci_get_powerstate(pc, tag, &pmode)))
    983   1.99  christos 		return error;
    984   1.99  christos 
    985   1.99  christos 	switch (pmode) {
    986   1.99  christos 	case PCI_PMCSR_STATE_D0:
    987   1.99  christos 		break;
    988   1.99  christos 	case PCI_PMCSR_STATE_D3:
    989   1.99  christos 		if (wakefun == NULL) {
    990   1.99  christos 			/*
    991   1.99  christos 			 * The card has lost all configuration data in
    992   1.99  christos 			 * this state, so punt.
    993   1.99  christos 			 */
    994  1.114    dyoung 			aprint_error_dev(dev,
    995  1.114    dyoung 			    "unable to wake up from power state D3\n");
    996   1.99  christos 			return EOPNOTSUPP;
    997   1.99  christos 		}
    998   1.99  christos 		/*FALLTHROUGH*/
    999   1.99  christos 	default:
   1000   1.99  christos 		if (wakefun) {
   1001  1.114    dyoung 			error = (*wakefun)(pc, tag, dev, pmode);
   1002   1.99  christos 			if (error)
   1003   1.99  christos 				return error;
   1004   1.99  christos 		}
   1005  1.114    dyoung 		aprint_normal_dev(dev, "waking up from power state D%d\n",
   1006  1.114    dyoung 		    pmode);
   1007   1.99  christos 		if ((error = pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0)))
   1008   1.99  christos 			return error;
   1009   1.99  christos 	}
   1010   1.99  christos 	return 0;
   1011   1.99  christos }
   1012   1.99  christos 
   1013   1.99  christos int
   1014  1.103  christos pci_activate_null(pci_chipset_tag_t pc, pcitag_t tag,
   1015  1.114    dyoung     device_t dev, pcireg_t state)
   1016   1.99  christos {
   1017   1.99  christos 	return 0;
   1018   1.99  christos }
   1019   1.99  christos 
   1020  1.107  jmcneill struct pci_child_power {
   1021  1.107  jmcneill 	struct pci_conf_state p_pciconf;
   1022  1.107  jmcneill 	pci_chipset_tag_t p_pc;
   1023  1.107  jmcneill 	pcitag_t p_tag;
   1024  1.107  jmcneill 	bool p_has_pm;
   1025  1.107  jmcneill 	int p_pm_offset;
   1026  1.107  jmcneill 	pcireg_t p_pm_cap;
   1027  1.107  jmcneill 	pcireg_t p_class;
   1028  1.131    dyoung 	pcireg_t p_csr;
   1029  1.107  jmcneill };
   1030  1.107  jmcneill 
   1031  1.107  jmcneill static bool
   1032  1.127    dyoung pci_child_suspend(device_t dv, const pmf_qual_t *qual)
   1033  1.107  jmcneill {
   1034  1.107  jmcneill 	struct pci_child_power *priv = device_pmf_bus_private(dv);
   1035  1.111  drochner 	pcireg_t ocsr, csr;
   1036  1.107  jmcneill 
   1037  1.107  jmcneill 	pci_conf_capture(priv->p_pc, priv->p_tag, &priv->p_pciconf);
   1038  1.107  jmcneill 
   1039  1.111  drochner 	if (!priv->p_has_pm)
   1040  1.111  drochner 		return true; /* ??? hopefully handled by ACPI */
   1041  1.111  drochner 	if (PCI_CLASS(priv->p_class) == PCI_CLASS_DISPLAY)
   1042  1.111  drochner 		return true; /* XXX */
   1043  1.111  drochner 
   1044  1.111  drochner 	/* disable decoding and busmastering, see pcipm1.2 ch. 8.2.1 */
   1045  1.111  drochner 	ocsr = pci_conf_read(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG);
   1046  1.111  drochner 	csr = ocsr & ~(PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE
   1047  1.111  drochner 		       | PCI_COMMAND_MASTER_ENABLE);
   1048  1.111  drochner 	pci_conf_write(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG, csr);
   1049  1.111  drochner 	if (pci_set_powerstate_int(priv->p_pc, priv->p_tag,
   1050  1.107  jmcneill 	    PCI_PMCSR_STATE_D3, priv->p_pm_offset, priv->p_pm_cap)) {
   1051  1.111  drochner 		pci_conf_write(priv->p_pc, priv->p_tag,
   1052  1.111  drochner 			       PCI_COMMAND_STATUS_REG, ocsr);
   1053  1.107  jmcneill 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1054  1.107  jmcneill 		return false;
   1055  1.107  jmcneill 	}
   1056  1.107  jmcneill 	return true;
   1057  1.107  jmcneill }
   1058  1.107  jmcneill 
   1059  1.107  jmcneill static bool
   1060  1.127    dyoung pci_child_resume(device_t dv, const pmf_qual_t *qual)
   1061  1.107  jmcneill {
   1062  1.107  jmcneill 	struct pci_child_power *priv = device_pmf_bus_private(dv);
   1063  1.107  jmcneill 
   1064  1.107  jmcneill 	if (priv->p_has_pm &&
   1065  1.107  jmcneill 	    pci_set_powerstate_int(priv->p_pc, priv->p_tag,
   1066  1.107  jmcneill 	    PCI_PMCSR_STATE_D0, priv->p_pm_offset, priv->p_pm_cap)) {
   1067  1.107  jmcneill 		aprint_error_dev(dv, "unsupported state, continuing.\n");
   1068  1.107  jmcneill 		return false;
   1069  1.107  jmcneill 	}
   1070  1.107  jmcneill 
   1071  1.107  jmcneill 	pci_conf_restore(priv->p_pc, priv->p_tag, &priv->p_pciconf);
   1072  1.107  jmcneill 
   1073  1.107  jmcneill 	return true;
   1074  1.107  jmcneill }
   1075  1.107  jmcneill 
   1076  1.113  drochner static bool
   1077  1.113  drochner pci_child_shutdown(device_t dv, int how)
   1078  1.113  drochner {
   1079  1.113  drochner 	struct pci_child_power *priv = device_pmf_bus_private(dv);
   1080  1.113  drochner 	pcireg_t csr;
   1081  1.113  drochner 
   1082  1.131    dyoung 	/* restore original bus-mastering state */
   1083  1.113  drochner 	csr = pci_conf_read(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG);
   1084  1.113  drochner 	csr &= ~PCI_COMMAND_MASTER_ENABLE;
   1085  1.131    dyoung 	csr |= priv->p_csr & PCI_COMMAND_MASTER_ENABLE;
   1086  1.113  drochner 	pci_conf_write(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG, csr);
   1087  1.113  drochner 	return true;
   1088  1.113  drochner }
   1089  1.113  drochner 
   1090  1.107  jmcneill static void
   1091  1.107  jmcneill pci_child_deregister(device_t dv)
   1092  1.107  jmcneill {
   1093  1.107  jmcneill 	struct pci_child_power *priv = device_pmf_bus_private(dv);
   1094  1.107  jmcneill 
   1095  1.107  jmcneill 	free(priv, M_DEVBUF);
   1096  1.107  jmcneill }
   1097  1.107  jmcneill 
   1098  1.107  jmcneill static bool
   1099  1.107  jmcneill pci_child_register(device_t child)
   1100  1.107  jmcneill {
   1101  1.107  jmcneill 	device_t self = device_parent(child);
   1102  1.107  jmcneill 	struct pci_softc *sc = device_private(self);
   1103  1.107  jmcneill 	struct pci_child_power *priv;
   1104  1.107  jmcneill 	int device, function, off;
   1105  1.107  jmcneill 	pcireg_t reg;
   1106  1.107  jmcneill 
   1107  1.107  jmcneill 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK);
   1108  1.107  jmcneill 
   1109  1.107  jmcneill 	device = device_locator(child, PCICF_DEV);
   1110  1.107  jmcneill 	function = device_locator(child, PCICF_FUNCTION);
   1111  1.107  jmcneill 
   1112  1.107  jmcneill 	priv->p_pc = sc->sc_pc;
   1113  1.107  jmcneill 	priv->p_tag = pci_make_tag(priv->p_pc, sc->sc_bus, device,
   1114  1.107  jmcneill 	    function);
   1115  1.107  jmcneill 	priv->p_class = pci_conf_read(priv->p_pc, priv->p_tag, PCI_CLASS_REG);
   1116  1.131    dyoung 	priv->p_csr = pci_conf_read(priv->p_pc, priv->p_tag,
   1117  1.131    dyoung 	    PCI_COMMAND_STATUS_REG);
   1118  1.107  jmcneill 
   1119  1.107  jmcneill 	if (pci_get_capability(priv->p_pc, priv->p_tag,
   1120  1.107  jmcneill 			       PCI_CAP_PWRMGMT, &off, &reg)) {
   1121  1.107  jmcneill 		priv->p_has_pm = true;
   1122  1.107  jmcneill 		priv->p_pm_offset = off;
   1123  1.107  jmcneill 		priv->p_pm_cap = reg;
   1124  1.107  jmcneill 	} else {
   1125  1.107  jmcneill 		priv->p_has_pm = false;
   1126  1.107  jmcneill 		priv->p_pm_offset = -1;
   1127  1.107  jmcneill 	}
   1128  1.107  jmcneill 
   1129  1.107  jmcneill 	device_pmf_bus_register(child, priv, pci_child_suspend,
   1130  1.113  drochner 	    pci_child_resume, pci_child_shutdown, pci_child_deregister);
   1131  1.107  jmcneill 
   1132  1.107  jmcneill 	return true;
   1133  1.107  jmcneill }
   1134  1.142  jmcneill 
   1135  1.142  jmcneill MODULE(MODULE_CLASS_DRIVER, pci, NULL);
   1136  1.142  jmcneill 
   1137  1.142  jmcneill static int
   1138  1.142  jmcneill pci_modcmd(modcmd_t cmd, void *priv)
   1139  1.142  jmcneill {
   1140  1.142  jmcneill 	if (cmd == MODULE_CMD_INIT || cmd == MODULE_CMD_FINI)
   1141  1.142  jmcneill 		return 0;
   1142  1.142  jmcneill 	return ENOTTY;
   1143  1.142  jmcneill }
   1144