Home | History | Annotate | Line # | Download | only in pci
pci.c revision 1.114
      1 /*	$NetBSD: pci.c,v 1.114 2008/03/21 07:47:43 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996, 1997, 1998
      5  *     Christopher G. Demetriou.  All rights reserved.
      6  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Charles M. Hannum.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * PCI bus autoconfiguration.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: pci.c,v 1.114 2008/03/21 07:47:43 dyoung Exp $");
     40 
     41 #include "opt_pci.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/malloc.h>
     45 #include <sys/systm.h>
     46 #include <sys/device.h>
     47 
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcivar.h>
     50 #include <dev/pci/pcidevs.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include <net/if.h>
     55 
     56 #include "locators.h"
     57 
     58 static bool pci_child_register(device_t);
     59 
     60 #ifdef PCI_CONFIG_DUMP
     61 int pci_config_dump = 1;
     62 #else
     63 int pci_config_dump = 0;
     64 #endif
     65 
     66 int	pciprint(void *, const char *);
     67 
     68 #ifdef PCI_MACHDEP_ENUMERATE_BUS
     69 #define pci_enumerate_bus PCI_MACHDEP_ENUMERATE_BUS
     70 #else
     71 int pci_enumerate_bus(struct pci_softc *, const int *,
     72     int (*)(struct pci_attach_args *), struct pci_attach_args *);
     73 #endif
     74 
     75 /*
     76  * Important note about PCI-ISA bridges:
     77  *
     78  * Callbacks are used to configure these devices so that ISA/EISA bridges
     79  * can attach their child busses after PCI configuration is done.
     80  *
     81  * This works because:
     82  *	(1) there can be at most one ISA/EISA bridge per PCI bus, and
     83  *	(2) any ISA/EISA bridges must be attached to primary PCI
     84  *	    busses (i.e. bus zero).
     85  *
     86  * That boils down to: there can only be one of these outstanding
     87  * at a time, it is cleared when configuring PCI bus 0 before any
     88  * subdevices have been found, and it is run after all subdevices
     89  * of PCI bus 0 have been found.
     90  *
     91  * This is needed because there are some (legacy) PCI devices which
     92  * can show up as ISA/EISA devices as well (the prime example of which
     93  * are VGA controllers).  If you attach ISA from a PCI-ISA/EISA bridge,
     94  * and the bridge is seen before the video board is, the board can show
     95  * up as an ISA device, and that can (bogusly) complicate the PCI device's
     96  * attach code, or make the PCI device not be properly attached at all.
     97  *
     98  * We use the generic config_defer() facility to achieve this.
     99  */
    100 
    101 static int
    102 pcirescan(device_t self, const char *ifattr, const int *locators)
    103 {
    104 	struct pci_softc *sc = device_private(self);
    105 
    106 	KASSERT(ifattr && !strcmp(ifattr, "pci"));
    107 	KASSERT(locators);
    108 
    109 	pci_enumerate_bus(sc, locators, NULL, NULL);
    110 	return 0;
    111 }
    112 
    113 static int
    114 pcimatch(device_t parent, struct cfdata *cf, void *aux)
    115 {
    116 	struct pcibus_attach_args *pba = aux;
    117 
    118 	/* Check the locators */
    119 	if (cf->cf_loc[PCIBUSCF_BUS] != PCIBUSCF_BUS_DEFAULT &&
    120 	    cf->cf_loc[PCIBUSCF_BUS] != pba->pba_bus)
    121 		return (0);
    122 
    123 	/* sanity */
    124 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
    125 		return (0);
    126 
    127 	/*
    128 	 * XXX check other (hardware?) indicators
    129 	 */
    130 
    131 	return (1);
    132 }
    133 
    134 static void
    135 pciattach(device_t parent, device_t self, void *aux)
    136 {
    137 	struct pcibus_attach_args *pba = aux;
    138 	struct pci_softc *sc = device_private(self);
    139 	int io_enabled, mem_enabled, mrl_enabled, mrm_enabled, mwi_enabled;
    140 	const char *sep = "";
    141 	static const int wildcard[PCICF_NLOCS] = {
    142 		PCICF_DEV_DEFAULT, PCICF_FUNCTION_DEFAULT
    143 	};
    144 
    145 	pci_attach_hook(parent, self, pba);
    146 
    147 	aprint_naive("\n");
    148 	aprint_normal("\n");
    149 
    150 	io_enabled = (pba->pba_flags & PCI_FLAGS_IO_ENABLED);
    151 	mem_enabled = (pba->pba_flags & PCI_FLAGS_MEM_ENABLED);
    152 	mrl_enabled = (pba->pba_flags & PCI_FLAGS_MRL_OKAY);
    153 	mrm_enabled = (pba->pba_flags & PCI_FLAGS_MRM_OKAY);
    154 	mwi_enabled = (pba->pba_flags & PCI_FLAGS_MWI_OKAY);
    155 
    156 	if (io_enabled == 0 && mem_enabled == 0) {
    157 		aprint_error_dev(self, "no spaces enabled!\n");
    158 		goto fail;
    159 	}
    160 
    161 #define	PRINT(str)							\
    162 do {									\
    163 	aprint_verbose("%s%s", sep, str);				\
    164 	sep = ", ";							\
    165 } while (/*CONSTCOND*/0)
    166 
    167 	aprint_verbose_dev(self, " ");
    168 
    169 	if (io_enabled)
    170 		PRINT("i/o space");
    171 	if (mem_enabled)
    172 		PRINT("memory space");
    173 	aprint_verbose(" enabled");
    174 
    175 	if (mrl_enabled || mrm_enabled || mwi_enabled) {
    176 		if (mrl_enabled)
    177 			PRINT("rd/line");
    178 		if (mrm_enabled)
    179 			PRINT("rd/mult");
    180 		if (mwi_enabled)
    181 			PRINT("wr/inv");
    182 		aprint_verbose(" ok");
    183 	}
    184 
    185 	aprint_verbose("\n");
    186 
    187 #undef PRINT
    188 
    189 	sc->sc_iot = pba->pba_iot;
    190 	sc->sc_memt = pba->pba_memt;
    191 	sc->sc_dmat = pba->pba_dmat;
    192 	sc->sc_dmat64 = pba->pba_dmat64;
    193 	sc->sc_pc = pba->pba_pc;
    194 	sc->sc_bus = pba->pba_bus;
    195 	sc->sc_bridgetag = pba->pba_bridgetag;
    196 	sc->sc_maxndevs = pci_bus_maxdevs(pba->pba_pc, pba->pba_bus);
    197 	sc->sc_intrswiz = pba->pba_intrswiz;
    198 	sc->sc_intrtag = pba->pba_intrtag;
    199 	sc->sc_flags = pba->pba_flags;
    200 
    201 	device_pmf_driver_set_child_register(&sc->sc_dev, pci_child_register);
    202 
    203 	pcirescan(&sc->sc_dev, "pci", wildcard);
    204 
    205 fail:
    206 	if (!pmf_device_register(self, NULL, NULL))
    207 		aprint_error_dev(self, "couldn't establish power handler\n");
    208 }
    209 
    210 static int
    211 pcidetach(device_t self, int flags)
    212 {
    213 	int rc;
    214 
    215 	if ((rc = config_detach_children(self, flags)) != 0)
    216 		return rc;
    217 	pmf_device_deregister(self);
    218 	return 0;
    219 }
    220 
    221 int
    222 pciprint(void *aux, const char *pnp)
    223 {
    224 	struct pci_attach_args *pa = aux;
    225 	char devinfo[256];
    226 	const struct pci_quirkdata *qd;
    227 
    228 	if (pnp) {
    229 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
    230 		aprint_normal("%s at %s", devinfo, pnp);
    231 	}
    232 	aprint_normal(" dev %d function %d", pa->pa_device, pa->pa_function);
    233 	if (pci_config_dump) {
    234 		printf(": ");
    235 		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    236 		if (!pnp)
    237 			pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
    238 		printf("%s at %s", devinfo, pnp ? pnp : "?");
    239 		printf(" dev %d function %d (", pa->pa_device, pa->pa_function);
    240 #ifdef __i386__
    241 		printf("tag %#lx, intrtag %#lx, intrswiz %#lx, intrpin %#lx",
    242 		    *(long *)&pa->pa_tag, *(long *)&pa->pa_intrtag,
    243 		    (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
    244 #else
    245 		printf("intrswiz %#lx, intrpin %#lx",
    246 		    (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
    247 #endif
    248 		printf(", i/o %s, mem %s,",
    249 		    pa->pa_flags & PCI_FLAGS_IO_ENABLED ? "on" : "off",
    250 		    pa->pa_flags & PCI_FLAGS_MEM_ENABLED ? "on" : "off");
    251 		qd = pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
    252 		    PCI_PRODUCT(pa->pa_id));
    253 		if (qd == NULL) {
    254 			printf(" no quirks");
    255 		} else {
    256 			bitmask_snprintf(qd->quirks,
    257 			    "\002\001multifn\002singlefn\003skipfunc0"
    258 			    "\004skipfunc1\005skipfunc2\006skipfunc3"
    259 			    "\007skipfunc4\010skipfunc5\011skipfunc6"
    260 			    "\012skipfunc7",
    261 			    devinfo, sizeof (devinfo));
    262 			printf(" quirks %s", devinfo);
    263 		}
    264 		printf(")");
    265 	}
    266 	return (UNCONF);
    267 }
    268 
    269 int
    270 pci_probe_device(struct pci_softc *sc, pcitag_t tag,
    271     int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
    272 {
    273 	pci_chipset_tag_t pc = sc->sc_pc;
    274 	struct pci_attach_args pa;
    275 	pcireg_t id, csr, class, intr, bhlcr;
    276 	int ret, pin, bus, device, function;
    277 	int locs[PCICF_NLOCS];
    278 	device_t subdev;
    279 
    280 	pci_decompose_tag(pc, tag, &bus, &device, &function);
    281 
    282 	/* a driver already attached? */
    283 	if (sc->PCI_SC_DEVICESC(device, function) && !match)
    284 		return (0);
    285 
    286 	bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    287 	if (PCI_HDRTYPE_TYPE(bhlcr) > 2)
    288 		return (0);
    289 
    290 	id = pci_conf_read(pc, tag, PCI_ID_REG);
    291 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    292 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
    293 
    294 	/* Invalid vendor ID value? */
    295 	if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    296 		return (0);
    297 	/* XXX Not invalid, but we've done this ~forever. */
    298 	if (PCI_VENDOR(id) == 0)
    299 		return (0);
    300 
    301 	pa.pa_iot = sc->sc_iot;
    302 	pa.pa_memt = sc->sc_memt;
    303 	pa.pa_dmat = sc->sc_dmat;
    304 	pa.pa_dmat64 = sc->sc_dmat64;
    305 	pa.pa_pc = pc;
    306 	pa.pa_bus = bus;
    307 	pa.pa_device = device;
    308 	pa.pa_function = function;
    309 	pa.pa_tag = tag;
    310 	pa.pa_id = id;
    311 	pa.pa_class = class;
    312 
    313 	/*
    314 	 * Set up memory, I/O enable, and PCI command flags
    315 	 * as appropriate.
    316 	 */
    317 	pa.pa_flags = sc->sc_flags;
    318 	if ((csr & PCI_COMMAND_IO_ENABLE) == 0)
    319 		pa.pa_flags &= ~PCI_FLAGS_IO_ENABLED;
    320 	if ((csr & PCI_COMMAND_MEM_ENABLE) == 0)
    321 		pa.pa_flags &= ~PCI_FLAGS_MEM_ENABLED;
    322 
    323 	/*
    324 	 * If the cache line size is not configured, then
    325 	 * clear the MRL/MRM/MWI command-ok flags.
    326 	 */
    327 	if (PCI_CACHELINE(bhlcr) == 0)
    328 		pa.pa_flags &= ~(PCI_FLAGS_MRL_OKAY|
    329 		    PCI_FLAGS_MRM_OKAY|PCI_FLAGS_MWI_OKAY);
    330 
    331 	if (sc->sc_bridgetag == NULL) {
    332 		pa.pa_intrswiz = 0;
    333 		pa.pa_intrtag = tag;
    334 	} else {
    335 		pa.pa_intrswiz = sc->sc_intrswiz + device;
    336 		pa.pa_intrtag = sc->sc_intrtag;
    337 	}
    338 
    339 	intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
    340 
    341 	pin = PCI_INTERRUPT_PIN(intr);
    342 	pa.pa_rawintrpin = pin;
    343 	if (pin == PCI_INTERRUPT_PIN_NONE) {
    344 		/* no interrupt */
    345 		pa.pa_intrpin = 0;
    346 	} else {
    347 		/*
    348 		 * swizzle it based on the number of busses we're
    349 		 * behind and our device number.
    350 		 */
    351 		pa.pa_intrpin = 	/* XXX */
    352 		    ((pin + pa.pa_intrswiz - 1) % 4) + 1;
    353 	}
    354 	pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
    355 
    356 	if (match != NULL) {
    357 		ret = (*match)(&pa);
    358 		if (ret != 0 && pap != NULL)
    359 			*pap = pa;
    360 	} else {
    361 		locs[PCICF_DEV] = device;
    362 		locs[PCICF_FUNCTION] = function;
    363 
    364 		subdev = config_found_sm_loc(&sc->sc_dev, "pci", locs, &pa,
    365 					     pciprint, config_stdsubmatch);
    366 		sc->PCI_SC_DEVICESC(device, function) = subdev;
    367 		ret = (subdev != NULL);
    368 	}
    369 
    370 	return (ret);
    371 }
    372 
    373 static void
    374 pcidevdetached(device_t self, device_t child)
    375 {
    376 	struct pci_softc *psc = device_private(self);
    377 	int d, f;
    378 
    379 	d = device_locator(child, PCICF_DEV);
    380 	f = device_locator(child, PCICF_FUNCTION);
    381 
    382 	KASSERT(psc->PCI_SC_DEVICESC(d, f) == child);
    383 
    384 	psc->PCI_SC_DEVICESC(d, f) = 0;
    385 }
    386 
    387 CFATTACH_DECL2(pci, sizeof(struct pci_softc),
    388     pcimatch, pciattach, pcidetach, NULL, pcirescan, pcidevdetached);
    389 
    390 int
    391 pci_get_capability(pci_chipset_tag_t pc, pcitag_t tag, int capid,
    392     int *offset, pcireg_t *value)
    393 {
    394 	pcireg_t reg;
    395 	unsigned int ofs;
    396 
    397 	reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    398 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
    399 		return (0);
    400 
    401 	/* Determine the Capability List Pointer register to start with. */
    402 	reg = pci_conf_read(pc, tag, PCI_BHLC_REG);
    403 	switch (PCI_HDRTYPE_TYPE(reg)) {
    404 	case 0:	/* standard device header */
    405 	case 1: /* PCI-PCI bridge header */
    406 		ofs = PCI_CAPLISTPTR_REG;
    407 		break;
    408 	case 2:	/* PCI-CardBus Bridge header */
    409 		ofs = PCI_CARDBUS_CAPLISTPTR_REG;
    410 		break;
    411 	default:
    412 		return (0);
    413 	}
    414 
    415 	ofs = PCI_CAPLIST_PTR(pci_conf_read(pc, tag, ofs));
    416 	while (ofs != 0) {
    417 #ifdef DIAGNOSTIC
    418 		if ((ofs & 3) || (ofs < 0x40))
    419 			panic("pci_get_capability");
    420 #endif
    421 		reg = pci_conf_read(pc, tag, ofs);
    422 		if (PCI_CAPLIST_CAP(reg) == capid) {
    423 			if (offset)
    424 				*offset = ofs;
    425 			if (value)
    426 				*value = reg;
    427 			return (1);
    428 		}
    429 		ofs = PCI_CAPLIST_NEXT(reg);
    430 	}
    431 
    432 	return (0);
    433 }
    434 
    435 int
    436 pci_find_device(struct pci_attach_args *pa,
    437 		int (*match)(struct pci_attach_args *))
    438 {
    439 	extern struct cfdriver pci_cd;
    440 	device_t pcidev;
    441 	int i;
    442 	static const int wildcard[2] = {
    443 		PCICF_DEV_DEFAULT,
    444 		PCICF_FUNCTION_DEFAULT
    445 	};
    446 
    447 	for (i = 0; i < pci_cd.cd_ndevs; i++) {
    448 		pcidev = pci_cd.cd_devs[i];
    449 		if (pcidev != NULL &&
    450 		    pci_enumerate_bus((struct pci_softc *)pcidev, wildcard,
    451 		    		      match, pa) != 0)
    452 			return (1);
    453 	}
    454 	return (0);
    455 }
    456 
    457 #ifndef PCI_MACHDEP_ENUMERATE_BUS
    458 /*
    459  * Generic PCI bus enumeration routine.  Used unless machine-dependent
    460  * code needs to provide something else.
    461  */
    462 int
    463 pci_enumerate_bus(struct pci_softc *sc, const int *locators,
    464     int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
    465 {
    466 	pci_chipset_tag_t pc = sc->sc_pc;
    467 	int device, function, nfunctions, ret;
    468 	const struct pci_quirkdata *qd;
    469 	pcireg_t id, bhlcr;
    470 	pcitag_t tag;
    471 #ifdef __PCI_BUS_DEVORDER
    472 	char devs[32];
    473 	int i;
    474 #endif
    475 
    476 #ifdef __PCI_BUS_DEVORDER
    477 	pci_bus_devorder(sc->sc_pc, sc->sc_bus, devs);
    478 	for (i = 0; (device = devs[i]) < 32 && device >= 0; i++)
    479 #else
    480 	for (device = 0; device < sc->sc_maxndevs; device++)
    481 #endif
    482 	{
    483 		if ((locators[PCICF_DEV] != PCICF_DEV_DEFAULT) &&
    484 		    (locators[PCICF_DEV] != device))
    485 			continue;
    486 
    487 		tag = pci_make_tag(pc, sc->sc_bus, device, 0);
    488 
    489 		bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
    490 		if (PCI_HDRTYPE_TYPE(bhlcr) > 2)
    491 			continue;
    492 
    493 		id = pci_conf_read(pc, tag, PCI_ID_REG);
    494 
    495 		/* Invalid vendor ID value? */
    496 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    497 			continue;
    498 		/* XXX Not invalid, but we've done this ~forever. */
    499 		if (PCI_VENDOR(id) == 0)
    500 			continue;
    501 
    502 		qd = pci_lookup_quirkdata(PCI_VENDOR(id), PCI_PRODUCT(id));
    503 
    504 		if (qd != NULL &&
    505 		      (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0)
    506 			nfunctions = 8;
    507 		else if (qd != NULL &&
    508 		      (qd->quirks & PCI_QUIRK_MONOFUNCTION) != 0)
    509 			nfunctions = 1;
    510 		else
    511 			nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    512 
    513 		for (function = 0; function < nfunctions; function++) {
    514 			if ((locators[PCICF_FUNCTION] != PCICF_FUNCTION_DEFAULT)
    515 			    && (locators[PCICF_FUNCTION] != function))
    516 				continue;
    517 
    518 			if (qd != NULL &&
    519 			    (qd->quirks & PCI_QUIRK_SKIP_FUNC(function)) != 0)
    520 				continue;
    521 			tag = pci_make_tag(pc, sc->sc_bus, device, function);
    522 			ret = pci_probe_device(sc, tag, match, pap);
    523 			if (match != NULL && ret != 0)
    524 				return (ret);
    525 		}
    526 	}
    527 	return (0);
    528 }
    529 #endif /* PCI_MACHDEP_ENUMERATE_BUS */
    530 
    531 
    532 /*
    533  * Vital Product Data (PCI 2.2)
    534  */
    535 
    536 int
    537 pci_vpd_read(pci_chipset_tag_t pc, pcitag_t tag, int offset, int count,
    538     pcireg_t *data)
    539 {
    540 	uint32_t reg;
    541 	int ofs, i, j;
    542 
    543 	KASSERT(data != NULL);
    544 	KASSERT((offset + count) < 0x7fff);
    545 
    546 	if (pci_get_capability(pc, tag, PCI_CAP_VPD, &ofs, &reg) == 0)
    547 		return (1);
    548 
    549 	for (i = 0; i < count; offset += sizeof(*data), i++) {
    550 		reg &= 0x0000ffff;
    551 		reg &= ~PCI_VPD_OPFLAG;
    552 		reg |= PCI_VPD_ADDRESS(offset);
    553 		pci_conf_write(pc, tag, ofs, reg);
    554 
    555 		/*
    556 		 * PCI 2.2 does not specify how long we should poll
    557 		 * for completion nor whether the operation can fail.
    558 		 */
    559 		j = 0;
    560 		do {
    561 			if (j++ == 20)
    562 				return (1);
    563 			delay(4);
    564 			reg = pci_conf_read(pc, tag, ofs);
    565 		} while ((reg & PCI_VPD_OPFLAG) == 0);
    566 		data[i] = pci_conf_read(pc, tag, PCI_VPD_DATAREG(ofs));
    567 	}
    568 
    569 	return (0);
    570 }
    571 
    572 int
    573 pci_vpd_write(pci_chipset_tag_t pc, pcitag_t tag, int offset, int count,
    574     pcireg_t *data)
    575 {
    576 	pcireg_t reg;
    577 	int ofs, i, j;
    578 
    579 	KASSERT(data != NULL);
    580 	KASSERT((offset + count) < 0x7fff);
    581 
    582 	if (pci_get_capability(pc, tag, PCI_CAP_VPD, &ofs, &reg) == 0)
    583 		return (1);
    584 
    585 	for (i = 0; i < count; offset += sizeof(*data), i++) {
    586 		pci_conf_write(pc, tag, PCI_VPD_DATAREG(ofs), data[i]);
    587 
    588 		reg &= 0x0000ffff;
    589 		reg |= PCI_VPD_OPFLAG;
    590 		reg |= PCI_VPD_ADDRESS(offset);
    591 		pci_conf_write(pc, tag, ofs, reg);
    592 
    593 		/*
    594 		 * PCI 2.2 does not specify how long we should poll
    595 		 * for completion nor whether the operation can fail.
    596 		 */
    597 		j = 0;
    598 		do {
    599 			if (j++ == 20)
    600 				return (1);
    601 			delay(1);
    602 			reg = pci_conf_read(pc, tag, ofs);
    603 		} while (reg & PCI_VPD_OPFLAG);
    604 	}
    605 
    606 	return (0);
    607 }
    608 
    609 int
    610 pci_dma64_available(struct pci_attach_args *pa)
    611 {
    612 #ifdef _PCI_HAVE_DMA64
    613 	if (BUS_DMA_TAG_VALID(pa->pa_dmat64) &&
    614 		((uint64_t)physmem << PAGE_SHIFT) > 0xffffffffULL)
    615                         return 1;
    616 #endif
    617         return 0;
    618 }
    619 
    620 void
    621 pci_conf_capture(pci_chipset_tag_t pc, pcitag_t tag,
    622 		  struct pci_conf_state *pcs)
    623 {
    624 	int off;
    625 
    626 	for (off = 0; off < 16; off++)
    627 		pcs->reg[off] = pci_conf_read(pc, tag, (off * 4));
    628 
    629 	return;
    630 }
    631 
    632 void
    633 pci_conf_restore(pci_chipset_tag_t pc, pcitag_t tag,
    634 		  struct pci_conf_state *pcs)
    635 {
    636 	int off;
    637 	pcireg_t val;
    638 
    639 	for (off = 15; off >= 0; off--) {
    640 		val = pci_conf_read(pc, tag, (off * 4));
    641 		if (val != pcs->reg[off])
    642 			pci_conf_write(pc, tag, (off * 4), pcs->reg[off]);
    643 	}
    644 
    645 	return;
    646 }
    647 
    648 /*
    649  * Power Management Capability (Rev 2.2)
    650  */
    651 static int
    652 pci_get_powerstate_int(pci_chipset_tag_t pc, pcitag_t tag , pcireg_t *state,
    653     int offset)
    654 {
    655 	pcireg_t value, now;
    656 
    657 	value = pci_conf_read(pc, tag, offset + PCI_PMCSR);
    658 	now = value & PCI_PMCSR_STATE_MASK;
    659 	switch (now) {
    660 	case PCI_PMCSR_STATE_D0:
    661 	case PCI_PMCSR_STATE_D1:
    662 	case PCI_PMCSR_STATE_D2:
    663 	case PCI_PMCSR_STATE_D3:
    664 		*state = now;
    665 		return 0;
    666 	default:
    667 		return EINVAL;
    668 	}
    669 }
    670 
    671 int
    672 pci_get_powerstate(pci_chipset_tag_t pc, pcitag_t tag , pcireg_t *state)
    673 {
    674 	int offset;
    675 	pcireg_t value;
    676 
    677 	if (!pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, &value))
    678 		return EOPNOTSUPP;
    679 
    680 	return pci_get_powerstate_int(pc, tag, state, offset);
    681 }
    682 
    683 static int
    684 pci_set_powerstate_int(pci_chipset_tag_t pc, pcitag_t tag, pcireg_t state,
    685     int offset, pcireg_t cap_reg)
    686 {
    687 	pcireg_t value, cap, now;
    688 
    689 	cap = cap_reg >> PCI_PMCR_SHIFT;
    690 	value = pci_conf_read(pc, tag, offset + PCI_PMCSR);
    691 	now = value & PCI_PMCSR_STATE_MASK;
    692 	value &= ~PCI_PMCSR_STATE_MASK;
    693 
    694 	if (now == state)
    695 		return 0;
    696 	switch (state) {
    697 	case PCI_PMCSR_STATE_D0:
    698 		break;
    699 	case PCI_PMCSR_STATE_D1:
    700 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3) {
    701 			printf("invalid transition from %d to D1\n", (int)now);
    702 			return EINVAL;
    703 		}
    704 		if (!(cap & PCI_PMCR_D1SUPP)) {
    705 			printf("D1 not supported\n");
    706 			return EOPNOTSUPP;
    707 		}
    708 		break;
    709 	case PCI_PMCSR_STATE_D2:
    710 		if (now == PCI_PMCSR_STATE_D3) {
    711 			printf("invalid transition from %d to D2\n", (int)now);
    712 			return EINVAL;
    713 		}
    714 		if (!(cap & PCI_PMCR_D2SUPP)) {
    715 			printf("D2 not supported\n");
    716 			return EOPNOTSUPP;
    717 		}
    718 		break;
    719 	case PCI_PMCSR_STATE_D3:
    720 		break;
    721 	default:
    722 		return EINVAL;
    723 	}
    724 	value |= state;
    725 	pci_conf_write(pc, tag, offset + PCI_PMCSR, value);
    726 	/* delay according to pcipm1.2, ch. 5.6.1 */
    727 	if (state == PCI_PMCSR_STATE_D3 || now == PCI_PMCSR_STATE_D3)
    728 		DELAY(10000);
    729 	else if (state == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D2)
    730 		DELAY(200);
    731 
    732 	return 0;
    733 }
    734 
    735 int
    736 pci_set_powerstate(pci_chipset_tag_t pc, pcitag_t tag, pcireg_t state)
    737 {
    738 	int offset;
    739 	pcireg_t value;
    740 
    741 	if (!pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, &value)) {
    742 		printf("pci_set_powerstate not supported\n");
    743 		return EOPNOTSUPP;
    744 	}
    745 
    746 	return pci_set_powerstate_int(pc, tag, state, offset, value);
    747 }
    748 
    749 int
    750 pci_activate(pci_chipset_tag_t pc, pcitag_t tag, device_t dev,
    751     int (*wakefun)(pci_chipset_tag_t, pcitag_t, device_t, pcireg_t))
    752 {
    753 	pcireg_t pmode;
    754 	int error;
    755 
    756 	if ((error = pci_get_powerstate(pc, tag, &pmode)))
    757 		return error;
    758 
    759 	switch (pmode) {
    760 	case PCI_PMCSR_STATE_D0:
    761 		break;
    762 	case PCI_PMCSR_STATE_D3:
    763 		if (wakefun == NULL) {
    764 			/*
    765 			 * The card has lost all configuration data in
    766 			 * this state, so punt.
    767 			 */
    768 			aprint_error_dev(dev,
    769 			    "unable to wake up from power state D3\n");
    770 			return EOPNOTSUPP;
    771 		}
    772 		/*FALLTHROUGH*/
    773 	default:
    774 		if (wakefun) {
    775 			error = (*wakefun)(pc, tag, dev, pmode);
    776 			if (error)
    777 				return error;
    778 		}
    779 		aprint_normal_dev(dev, "waking up from power state D%d\n",
    780 		    pmode);
    781 		if ((error = pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0)))
    782 			return error;
    783 	}
    784 	return 0;
    785 }
    786 
    787 int
    788 pci_activate_null(pci_chipset_tag_t pc, pcitag_t tag,
    789     device_t dev, pcireg_t state)
    790 {
    791 	return 0;
    792 }
    793 
    794 /* I have disabled this code for now. --dyoung
    795  *
    796  * Insofar as I understand what the PCI retry timeout is [1],
    797  * I see no justification for any driver to disable when it
    798  * attaches/resumes a device.
    799  *
    800  * A PCI bus bridge may tell a bus master to retry its transaction
    801  * at a later time if the resources to complete the transaction
    802  * are not immediately available.  Taking a guess, PCI bus masters
    803  * that implement a PCI retry timeout register count down from the
    804  * retry timeout to 0 while it retries a delayed PCI transaction.
    805  * When it reaches 0, it stops retrying.  A PCI master is *never*
    806  * supposed to stop retrying a delayed transaction, though.
    807  *
    808  * Incidentally, I initially suspected that writing 0 to the register
    809  * would not disable *retries*, but would disable the timeout.
    810  * That is, any device whose retry timeout was set to 0 would
    811  * *never* timeout.  However, I found out, by using PCI debug
    812  * facilities on the AMD Elan SC520, that if I write 0 to the retry
    813  * timeout register on an ath(4) MiniPCI card, the card really does
    814  * not retry transactions.
    815  *
    816  * Some uses of this register have mentioned "interference" with
    817  * a CPU's "C3 sleep state."  It seems to me that if a bus master
    818  * is properly put to sleep, it will neither initiate new transactions,
    819  * nor retry delayed transactions, so disabling retries should not
    820  * be necessary.
    821  *
    822  * [1] The timeout does not appear to be documented in any PCI
    823  * standard, and we have no documentation of it for the devices by
    824  * Atheros, and others, that supposedly implement it.
    825  */
    826 void
    827 pci_disable_retry(pci_chipset_tag_t pc, pcitag_t tag)
    828 {
    829 #if 0
    830 	pcireg_t retry;
    831 
    832 	/*
    833 	 * Disable retry timeout to keep PCI Tx retries from
    834 	 * interfering with ACPI C3 CPU state.
    835 	 */
    836 	retry = pci_conf_read(pc, tag, PCI_RETRY_TIMEOUT_REG);
    837 	retry &= ~PCI_RETRY_TIMEOUT_REG_MASK;
    838 	pci_conf_write(pc, tag, PCI_RETRY_TIMEOUT_REG, retry);
    839 #endif
    840 }
    841 
    842 struct pci_child_power {
    843 	struct pci_conf_state p_pciconf;
    844 	pci_chipset_tag_t p_pc;
    845 	pcitag_t p_tag;
    846 	bool p_has_pm;
    847 	int p_pm_offset;
    848 	pcireg_t p_pm_cap;
    849 	pcireg_t p_class;
    850 };
    851 
    852 static bool
    853 pci_child_suspend(device_t dv PMF_FN_ARGS)
    854 {
    855 	struct pci_child_power *priv = device_pmf_bus_private(dv);
    856 	pcireg_t ocsr, csr;
    857 
    858 	pci_conf_capture(priv->p_pc, priv->p_tag, &priv->p_pciconf);
    859 
    860 	if (!priv->p_has_pm)
    861 		return true; /* ??? hopefully handled by ACPI */
    862 	if (PCI_CLASS(priv->p_class) == PCI_CLASS_DISPLAY)
    863 		return true; /* XXX */
    864 
    865 	/* disable decoding and busmastering, see pcipm1.2 ch. 8.2.1 */
    866 	ocsr = pci_conf_read(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG);
    867 	csr = ocsr & ~(PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE
    868 		       | PCI_COMMAND_MASTER_ENABLE);
    869 	pci_conf_write(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG, csr);
    870 	if (pci_set_powerstate_int(priv->p_pc, priv->p_tag,
    871 	    PCI_PMCSR_STATE_D3, priv->p_pm_offset, priv->p_pm_cap)) {
    872 		pci_conf_write(priv->p_pc, priv->p_tag,
    873 			       PCI_COMMAND_STATUS_REG, ocsr);
    874 		aprint_error_dev(dv, "unsupported state, continuing.\n");
    875 		return false;
    876 	}
    877 	return true;
    878 }
    879 
    880 static bool
    881 pci_child_resume(device_t dv PMF_FN_ARGS)
    882 {
    883 	struct pci_child_power *priv = device_pmf_bus_private(dv);
    884 
    885 	if (priv->p_has_pm &&
    886 	    pci_set_powerstate_int(priv->p_pc, priv->p_tag,
    887 	    PCI_PMCSR_STATE_D0, priv->p_pm_offset, priv->p_pm_cap)) {
    888 		aprint_error_dev(dv, "unsupported state, continuing.\n");
    889 		return false;
    890 	}
    891 
    892 	pci_conf_restore(priv->p_pc, priv->p_tag, &priv->p_pciconf);
    893 
    894 	return true;
    895 }
    896 
    897 static bool
    898 pci_child_shutdown(device_t dv, int how)
    899 {
    900 	struct pci_child_power *priv = device_pmf_bus_private(dv);
    901 	pcireg_t csr;
    902 
    903 	/* disable busmastering */
    904 	csr = pci_conf_read(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG);
    905 	csr &= ~PCI_COMMAND_MASTER_ENABLE;
    906 	pci_conf_write(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG, csr);
    907 	return true;
    908 }
    909 
    910 static void
    911 pci_child_deregister(device_t dv)
    912 {
    913 	struct pci_child_power *priv = device_pmf_bus_private(dv);
    914 
    915 	free(priv, M_DEVBUF);
    916 }
    917 
    918 static bool
    919 pci_child_register(device_t child)
    920 {
    921 	device_t self = device_parent(child);
    922 	struct pci_softc *sc = device_private(self);
    923 	struct pci_child_power *priv;
    924 	int device, function, off;
    925 	pcireg_t reg;
    926 
    927 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK);
    928 
    929 	device = device_locator(child, PCICF_DEV);
    930 	function = device_locator(child, PCICF_FUNCTION);
    931 
    932 	priv->p_pc = sc->sc_pc;
    933 	priv->p_tag = pci_make_tag(priv->p_pc, sc->sc_bus, device,
    934 	    function);
    935 	priv->p_class = pci_conf_read(priv->p_pc, priv->p_tag, PCI_CLASS_REG);
    936 
    937 	if (pci_get_capability(priv->p_pc, priv->p_tag,
    938 			       PCI_CAP_PWRMGMT, &off, &reg)) {
    939 		priv->p_has_pm = true;
    940 		priv->p_pm_offset = off;
    941 		priv->p_pm_cap = reg;
    942 	} else {
    943 		priv->p_has_pm = false;
    944 		priv->p_pm_offset = -1;
    945 	}
    946 
    947 	device_pmf_bus_register(child, priv, pci_child_suspend,
    948 	    pci_child_resume, pci_child_shutdown, pci_child_deregister);
    949 
    950 	return true;
    951 }
    952