xref: /netbsd-src/sys/dev/pci/pci.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: pci.c,v 1.99 2006/06/17 23:34:27 christos 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.99 2006/06/17 23:34:27 christos Exp $");
40 
41 #include "opt_pci.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcidevs.h>
50 
51 #include <uvm/uvm_extern.h>
52 
53 #include "locators.h"
54 
55 #ifdef PCI_CONFIG_DUMP
56 int pci_config_dump = 1;
57 #else
58 int pci_config_dump = 0;
59 #endif
60 
61 int	pciprint(void *, const char *);
62 
63 #ifdef PCI_MACHDEP_ENUMERATE_BUS
64 #define pci_enumerate_bus PCI_MACHDEP_ENUMERATE_BUS
65 #else
66 int pci_enumerate_bus(struct pci_softc *, const int *,
67     int (*)(struct pci_attach_args *), struct pci_attach_args *);
68 #endif
69 
70 /*
71  * Important note about PCI-ISA bridges:
72  *
73  * Callbacks are used to configure these devices so that ISA/EISA bridges
74  * can attach their child busses after PCI configuration is done.
75  *
76  * This works because:
77  *	(1) there can be at most one ISA/EISA bridge per PCI bus, and
78  *	(2) any ISA/EISA bridges must be attached to primary PCI
79  *	    busses (i.e. bus zero).
80  *
81  * That boils down to: there can only be one of these outstanding
82  * at a time, it is cleared when configuring PCI bus 0 before any
83  * subdevices have been found, and it is run after all subdevices
84  * of PCI bus 0 have been found.
85  *
86  * This is needed because there are some (legacy) PCI devices which
87  * can show up as ISA/EISA devices as well (the prime example of which
88  * are VGA controllers).  If you attach ISA from a PCI-ISA/EISA bridge,
89  * and the bridge is seen before the video board is, the board can show
90  * up as an ISA device, and that can (bogusly) complicate the PCI device's
91  * attach code, or make the PCI device not be properly attached at all.
92  *
93  * We use the generic config_defer() facility to achieve this.
94  */
95 
96 static int
97 pcirescan(struct device *sc, const char *ifattr, const int *locators)
98 {
99 
100 	KASSERT(ifattr && !strcmp(ifattr, "pci"));
101 	KASSERT(locators);
102 
103 	pci_enumerate_bus((struct pci_softc *)sc, locators, NULL, NULL);
104 	return (0);
105 }
106 
107 static int
108 pcimatch(struct device *parent, struct cfdata *cf, void *aux)
109 {
110 	struct pcibus_attach_args *pba = aux;
111 
112 	/* Check the locators */
113 	if (cf->cf_loc[PCIBUSCF_BUS] != PCIBUSCF_BUS_DEFAULT &&
114 	    cf->cf_loc[PCIBUSCF_BUS] != pba->pba_bus)
115 		return (0);
116 
117 	/* sanity */
118 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
119 		return (0);
120 
121 	/*
122 	 * XXX check other (hardware?) indicators
123 	 */
124 
125 	return (1);
126 }
127 
128 static void
129 pciattach(struct device *parent, struct device *self, void *aux)
130 {
131 	struct pcibus_attach_args *pba = aux;
132 	struct pci_softc *sc = (struct pci_softc *)self;
133 	int io_enabled, mem_enabled, mrl_enabled, mrm_enabled, mwi_enabled;
134 	const char *sep = "";
135 	static const int wildcard[PCICF_NLOCS] = {
136 		PCICF_DEV_DEFAULT, PCICF_FUNCTION_DEFAULT
137 	};
138 
139 	pci_attach_hook(parent, self, pba);
140 
141 	aprint_naive("\n");
142 	aprint_normal("\n");
143 
144 	io_enabled = (pba->pba_flags & PCI_FLAGS_IO_ENABLED);
145 	mem_enabled = (pba->pba_flags & PCI_FLAGS_MEM_ENABLED);
146 	mrl_enabled = (pba->pba_flags & PCI_FLAGS_MRL_OKAY);
147 	mrm_enabled = (pba->pba_flags & PCI_FLAGS_MRM_OKAY);
148 	mwi_enabled = (pba->pba_flags & PCI_FLAGS_MWI_OKAY);
149 
150 	if (io_enabled == 0 && mem_enabled == 0) {
151 		aprint_error("%s: no spaces enabled!\n", self->dv_xname);
152 		return;
153 	}
154 
155 #define	PRINT(str)							\
156 do {									\
157 	aprint_normal("%s%s", sep, str);				\
158 	sep = ", ";							\
159 } while (/*CONSTCOND*/0)
160 
161 	aprint_normal("%s: ", self->dv_xname);
162 
163 	if (io_enabled)
164 		PRINT("i/o space");
165 	if (mem_enabled)
166 		PRINT("memory space");
167 	aprint_normal(" enabled");
168 
169 	if (mrl_enabled || mrm_enabled || mwi_enabled) {
170 		if (mrl_enabled)
171 			PRINT("rd/line");
172 		if (mrm_enabled)
173 			PRINT("rd/mult");
174 		if (mwi_enabled)
175 			PRINT("wr/inv");
176 		aprint_normal(" ok");
177 	}
178 
179 	aprint_normal("\n");
180 
181 #undef PRINT
182 
183 	sc->sc_iot = pba->pba_iot;
184 	sc->sc_memt = pba->pba_memt;
185 	sc->sc_dmat = pba->pba_dmat;
186 	sc->sc_dmat64 = pba->pba_dmat64;
187 	sc->sc_pc = pba->pba_pc;
188 	sc->sc_bus = pba->pba_bus;
189 	sc->sc_bridgetag = pba->pba_bridgetag;
190 	sc->sc_maxndevs = pci_bus_maxdevs(pba->pba_pc, pba->pba_bus);
191 	sc->sc_intrswiz = pba->pba_intrswiz;
192 	sc->sc_intrtag = pba->pba_intrtag;
193 	sc->sc_flags = pba->pba_flags;
194 	pcirescan(&sc->sc_dev, "pci", wildcard);
195 }
196 
197 int
198 pciprint(void *aux, const char *pnp)
199 {
200 	struct pci_attach_args *pa = aux;
201 	char devinfo[256];
202 	const struct pci_quirkdata *qd;
203 
204 	if (pnp) {
205 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
206 		aprint_normal("%s at %s", devinfo, pnp);
207 	}
208 	aprint_normal(" dev %d function %d", pa->pa_device, pa->pa_function);
209 	if (pci_config_dump) {
210 		printf(": ");
211 		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
212 		if (!pnp)
213 			pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
214 		printf("%s at %s", devinfo, pnp ? pnp : "?");
215 		printf(" dev %d function %d (", pa->pa_device, pa->pa_function);
216 #ifdef __i386__
217 		printf("tag %#lx, intrtag %#lx, intrswiz %#lx, intrpin %#lx",
218 		    *(long *)&pa->pa_tag, *(long *)&pa->pa_intrtag,
219 		    (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
220 #else
221 		printf("intrswiz %#lx, intrpin %#lx",
222 		    (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
223 #endif
224 		printf(", i/o %s, mem %s,",
225 		    pa->pa_flags & PCI_FLAGS_IO_ENABLED ? "on" : "off",
226 		    pa->pa_flags & PCI_FLAGS_MEM_ENABLED ? "on" : "off");
227 		qd = pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
228 		    PCI_PRODUCT(pa->pa_id));
229 		if (qd == NULL) {
230 			printf(" no quirks");
231 		} else {
232 			bitmask_snprintf(qd->quirks,
233 			    "\002\001multifn\002singlefn\003skipfunc0"
234 			    "\004skipfunc1\005skipfunc2\006skipfunc3"
235 			    "\007skipfunc4\010skipfunc5\011skipfunc6"
236 			    "\012skipfunc7",
237 			    devinfo, sizeof (devinfo));
238 			printf(" quirks %s", devinfo);
239 		}
240 		printf(")");
241 	}
242 	return (UNCONF);
243 }
244 
245 int
246 pci_probe_device(struct pci_softc *sc, pcitag_t tag,
247     int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
248 {
249 	pci_chipset_tag_t pc = sc->sc_pc;
250 	struct pci_attach_args pa;
251 	pcireg_t id, csr, class, intr, bhlcr;
252 	int ret, pin, bus, device, function;
253 	int locs[PCICF_NLOCS];
254 	struct device *subdev;
255 
256 	pci_decompose_tag(pc, tag, &bus, &device, &function);
257 
258 	/* a driver already attached? */
259 	if (sc->PCI_SC_DEVICESC(device, function) && !match)
260 		return (0);
261 
262 	bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
263 	if (PCI_HDRTYPE_TYPE(bhlcr) > 2)
264 		return (0);
265 
266 	id = pci_conf_read(pc, tag, PCI_ID_REG);
267 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
268 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
269 
270 	/* Invalid vendor ID value? */
271 	if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
272 		return (0);
273 	/* XXX Not invalid, but we've done this ~forever. */
274 	if (PCI_VENDOR(id) == 0)
275 		return (0);
276 
277 	pa.pa_iot = sc->sc_iot;
278 	pa.pa_memt = sc->sc_memt;
279 	pa.pa_dmat = sc->sc_dmat;
280 	pa.pa_dmat64 = sc->sc_dmat64;
281 	pa.pa_pc = pc;
282 	pa.pa_bus = bus;
283 	pa.pa_device = device;
284 	pa.pa_function = function;
285 	pa.pa_tag = tag;
286 	pa.pa_id = id;
287 	pa.pa_class = class;
288 
289 	/*
290 	 * Set up memory, I/O enable, and PCI command flags
291 	 * as appropriate.
292 	 */
293 	pa.pa_flags = sc->sc_flags;
294 	if ((csr & PCI_COMMAND_IO_ENABLE) == 0)
295 		pa.pa_flags &= ~PCI_FLAGS_IO_ENABLED;
296 	if ((csr & PCI_COMMAND_MEM_ENABLE) == 0)
297 		pa.pa_flags &= ~PCI_FLAGS_MEM_ENABLED;
298 
299 	/*
300 	 * If the cache line size is not configured, then
301 	 * clear the MRL/MRM/MWI command-ok flags.
302 	 */
303 	if (PCI_CACHELINE(bhlcr) == 0)
304 		pa.pa_flags &= ~(PCI_FLAGS_MRL_OKAY|
305 		    PCI_FLAGS_MRM_OKAY|PCI_FLAGS_MWI_OKAY);
306 
307 	if (sc->sc_bridgetag == NULL) {
308 		pa.pa_intrswiz = 0;
309 		pa.pa_intrtag = tag;
310 	} else {
311 		pa.pa_intrswiz = sc->sc_intrswiz + device;
312 		pa.pa_intrtag = sc->sc_intrtag;
313 	}
314 
315 	intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
316 
317 	pin = PCI_INTERRUPT_PIN(intr);
318 	pa.pa_rawintrpin = pin;
319 	if (pin == PCI_INTERRUPT_PIN_NONE) {
320 		/* no interrupt */
321 		pa.pa_intrpin = 0;
322 	} else {
323 		/*
324 		 * swizzle it based on the number of busses we're
325 		 * behind and our device number.
326 		 */
327 		pa.pa_intrpin = 	/* XXX */
328 		    ((pin + pa.pa_intrswiz - 1) % 4) + 1;
329 	}
330 	pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
331 
332 	if (match != NULL) {
333 		ret = (*match)(&pa);
334 		if (ret != 0 && pap != NULL)
335 			*pap = pa;
336 	} else {
337 		locs[PCICF_DEV] = device;
338 		locs[PCICF_FUNCTION] = function;
339 
340 		subdev = config_found_sm_loc(&sc->sc_dev, "pci", locs, &pa,
341 					     pciprint, config_stdsubmatch);
342 		sc->PCI_SC_DEVICESC(device, function) = subdev;
343 		ret = (subdev != NULL);
344 	}
345 
346 	return (ret);
347 }
348 
349 static void
350 pcidevdetached(struct device *sc, struct device *dev)
351 {
352 	struct pci_softc *psc = (struct pci_softc *)sc;
353 	int d, f;
354 
355 	d = device_locator(dev, PCICF_DEV);
356 	f = device_locator(dev, PCICF_FUNCTION);
357 
358 	KASSERT(psc->PCI_SC_DEVICESC(d, f) == dev);
359 
360 	psc->PCI_SC_DEVICESC(d, f) = 0;
361 }
362 
363 int
364 pci_get_capability(pci_chipset_tag_t pc, pcitag_t tag, int capid,
365     int *offset, pcireg_t *value)
366 {
367 	pcireg_t reg;
368 	unsigned int ofs;
369 
370 	reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
371 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
372 		return (0);
373 
374 	/* Determine the Capability List Pointer register to start with. */
375 	reg = pci_conf_read(pc, tag, PCI_BHLC_REG);
376 	switch (PCI_HDRTYPE_TYPE(reg)) {
377 	case 0:	/* standard device header */
378 		ofs = PCI_CAPLISTPTR_REG;
379 		break;
380 	case 2:	/* PCI-CardBus Bridge header */
381 		ofs = PCI_CARDBUS_CAPLISTPTR_REG;
382 		break;
383 	default:
384 		return (0);
385 	}
386 
387 	ofs = PCI_CAPLIST_PTR(pci_conf_read(pc, tag, ofs));
388 	while (ofs != 0) {
389 #ifdef DIAGNOSTIC
390 		if ((ofs & 3) || (ofs < 0x40))
391 			panic("pci_get_capability");
392 #endif
393 		reg = pci_conf_read(pc, tag, ofs);
394 		if (PCI_CAPLIST_CAP(reg) == capid) {
395 			if (offset)
396 				*offset = ofs;
397 			if (value)
398 				*value = reg;
399 			return (1);
400 		}
401 		ofs = PCI_CAPLIST_NEXT(reg);
402 	}
403 
404 	return (0);
405 }
406 
407 int
408 pci_find_device(struct pci_attach_args *pa,
409 		int (*match)(struct pci_attach_args *))
410 {
411 	extern struct cfdriver pci_cd;
412 	struct device *pcidev;
413 	int i;
414 	static const int wildcard[2] = {
415 		PCICF_DEV_DEFAULT,
416 		PCICF_FUNCTION_DEFAULT
417 	};
418 
419 	for (i = 0; i < pci_cd.cd_ndevs; i++) {
420 		pcidev = pci_cd.cd_devs[i];
421 		if (pcidev != NULL &&
422 		    pci_enumerate_bus((struct pci_softc *)pcidev, wildcard,
423 		    		      match, pa) != 0)
424 			return (1);
425 	}
426 	return (0);
427 }
428 
429 #ifndef PCI_MACHDEP_ENUMERATE_BUS
430 /*
431  * Generic PCI bus enumeration routine.  Used unless machine-dependent
432  * code needs to provide something else.
433  */
434 int
435 pci_enumerate_bus(struct pci_softc *sc, const int *locators,
436     int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
437 {
438 	pci_chipset_tag_t pc = sc->sc_pc;
439 	int device, function, nfunctions, ret;
440 	const struct pci_quirkdata *qd;
441 	pcireg_t id, bhlcr;
442 	pcitag_t tag;
443 #ifdef __PCI_BUS_DEVORDER
444 	char devs[32];
445 	int i;
446 #endif
447 
448 #ifdef __PCI_BUS_DEVORDER
449 	pci_bus_devorder(sc->sc_pc, sc->sc_bus, devs);
450 	for (i = 0; (device = devs[i]) < 32 && device >= 0; i++)
451 #else
452 	for (device = 0; device < sc->sc_maxndevs; device++)
453 #endif
454 	{
455 		if ((locators[PCICF_DEV] != PCICF_DEV_DEFAULT) &&
456 		    (locators[PCICF_DEV] != device))
457 			continue;
458 
459 		tag = pci_make_tag(pc, sc->sc_bus, device, 0);
460 
461 		bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
462 		if (PCI_HDRTYPE_TYPE(bhlcr) > 2)
463 			continue;
464 
465 		id = pci_conf_read(pc, tag, PCI_ID_REG);
466 
467 		/* Invalid vendor ID value? */
468 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
469 			continue;
470 		/* XXX Not invalid, but we've done this ~forever. */
471 		if (PCI_VENDOR(id) == 0)
472 			continue;
473 
474 		qd = pci_lookup_quirkdata(PCI_VENDOR(id), PCI_PRODUCT(id));
475 
476 		if (qd != NULL &&
477 		      (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0)
478 			nfunctions = 8;
479 		else if (qd != NULL &&
480 		      (qd->quirks & PCI_QUIRK_MONOFUNCTION) != 0)
481 			nfunctions = 1;
482 		else
483 			nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
484 
485 		for (function = 0; function < nfunctions; function++) {
486 			if ((locators[PCICF_FUNCTION] != PCICF_FUNCTION_DEFAULT)
487 			    && (locators[PCICF_FUNCTION] != function))
488 				continue;
489 
490 			if (qd != NULL &&
491 			    (qd->quirks & PCI_QUIRK_SKIP_FUNC(function)) != 0)
492 				continue;
493 			tag = pci_make_tag(pc, sc->sc_bus, device, function);
494 			ret = pci_probe_device(sc, tag, match, pap);
495 			if (match != NULL && ret != 0)
496 				return (ret);
497 		}
498 	}
499 	return (0);
500 }
501 #endif /* PCI_MACHDEP_ENUMERATE_BUS */
502 
503 
504 /*
505  * Vital Product Data (PCI 2.2)
506  */
507 
508 int
509 pci_vpd_read(pci_chipset_tag_t pc, pcitag_t tag, int offset, int count,
510     pcireg_t *data)
511 {
512 	uint32_t reg;
513 	int ofs, i, j;
514 
515 	KASSERT(data != NULL);
516 	KASSERT((offset + count) < 0x7fff);
517 
518 	if (pci_get_capability(pc, tag, PCI_CAP_VPD, &ofs, &reg) == 0)
519 		return (1);
520 
521 	for (i = 0; i < count; offset += sizeof(*data), i++) {
522 		reg &= 0x0000ffff;
523 		reg &= ~PCI_VPD_OPFLAG;
524 		reg |= PCI_VPD_ADDRESS(offset);
525 		pci_conf_write(pc, tag, ofs, reg);
526 
527 		/*
528 		 * PCI 2.2 does not specify how long we should poll
529 		 * for completion nor whether the operation can fail.
530 		 */
531 		j = 0;
532 		do {
533 			if (j++ == 20)
534 				return (1);
535 			delay(4);
536 			reg = pci_conf_read(pc, tag, ofs);
537 		} while ((reg & PCI_VPD_OPFLAG) == 0);
538 		data[i] = pci_conf_read(pc, tag, PCI_VPD_DATAREG(ofs));
539 	}
540 
541 	return (0);
542 }
543 
544 int
545 pci_vpd_write(pci_chipset_tag_t pc, pcitag_t tag, int offset, int count,
546     pcireg_t *data)
547 {
548 	pcireg_t reg;
549 	int ofs, i, j;
550 
551 	KASSERT(data != NULL);
552 	KASSERT((offset + count) < 0x7fff);
553 
554 	if (pci_get_capability(pc, tag, PCI_CAP_VPD, &ofs, &reg) == 0)
555 		return (1);
556 
557 	for (i = 0; i < count; offset += sizeof(*data), i++) {
558 		pci_conf_write(pc, tag, PCI_VPD_DATAREG(ofs), data[i]);
559 
560 		reg &= 0x0000ffff;
561 		reg |= PCI_VPD_OPFLAG;
562 		reg |= PCI_VPD_ADDRESS(offset);
563 		pci_conf_write(pc, tag, ofs, reg);
564 
565 		/*
566 		 * PCI 2.2 does not specify how long we should poll
567 		 * for completion nor whether the operation can fail.
568 		 */
569 		j = 0;
570 		do {
571 			if (j++ == 20)
572 				return (1);
573 			delay(1);
574 			reg = pci_conf_read(pc, tag, ofs);
575 		} while (reg & PCI_VPD_OPFLAG);
576 	}
577 
578 	return (0);
579 }
580 
581 int
582 pci_dma64_available(struct pci_attach_args *pa)
583 {
584 #ifdef _PCI_HAVE_DMA64
585 	if (BUS_DMA_TAG_VALID(pa->pa_dmat64) &&
586 		((uint64_t)physmem << PAGE_SHIFT) > 0xffffffffULL)
587                         return 1;
588 #endif
589         return 0;
590 }
591 
592 void
593 pci_conf_capture(pci_chipset_tag_t pc, pcitag_t tag,
594 		  struct pci_conf_state *pcs)
595 {
596 	int off;
597 
598 	for (off = 0; off < 16; off++)
599 		pcs->reg[off] = pci_conf_read(pc, tag, (off * 4));
600 
601 	return;
602 }
603 
604 void
605 pci_conf_restore(pci_chipset_tag_t pc, pcitag_t tag,
606 		  struct pci_conf_state *pcs)
607 {
608 	int off;
609 
610 	for (off = 0; off < 16; off++)
611 		pci_conf_write(pc, tag, (off * 4), pcs->reg[off]);
612 
613 	return;
614 }
615 
616 /*
617  * Power Management Capability (Rev 2.2)
618  */
619 int
620 pci_get_powerstate(pci_chipset_tag_t pc, pcitag_t tag , pcireg_t *state)
621 {
622 	int offset;
623 	pcireg_t value, cap, now;
624 
625 	if (!pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, &value))
626 		return EOPNOTSUPP;
627 
628 	cap = value >> PCI_PMCR_SHIFT;
629 	value = pci_conf_read(pc, tag, offset + PCI_PMCSR);
630 	now = value & PCI_PMCSR_STATE_MASK;
631 	switch (now) {
632 	case PCI_PMCSR_STATE_D0:
633 	case PCI_PMCSR_STATE_D1:
634 	case PCI_PMCSR_STATE_D2:
635 	case PCI_PMCSR_STATE_D3:
636 		*state = now;
637 		return 0;
638 	default:
639 		return EINVAL;
640 	}
641 }
642 
643 int
644 pci_set_powerstate(pci_chipset_tag_t pc, pcitag_t tag, pcireg_t state)
645 {
646 	int offset;
647 	pcireg_t value, cap, now;
648 
649 	if (!pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, &value))
650 		return EOPNOTSUPP;
651 
652 	cap = value >> PCI_PMCR_SHIFT;
653 	value = pci_conf_read(pc, tag, offset + PCI_PMCSR);
654 	now = value & PCI_PMCSR_STATE_MASK;
655 	value &= ~PCI_PMCSR_STATE_MASK;
656 
657 	if (now == state)
658 		return 0;
659 	switch (state) {
660 	case PCI_PMCSR_STATE_D0:
661 		value |= PCI_PMCSR_STATE_D0;
662 		break;
663 	case PCI_PMCSR_STATE_D1:
664 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3)
665 			return EINVAL;
666 		if (!(cap & PCI_PMCR_D1SUPP))
667 			return EOPNOTSUPP;
668 		value |= PCI_PMCSR_STATE_D1;
669 		break;
670 	case PCI_PMCSR_STATE_D2:
671 		if (now == PCI_PMCSR_STATE_D3)
672 			return EINVAL;
673 		if (!(cap & PCI_PMCR_D2SUPP))
674 			return EOPNOTSUPP;
675 		value |= PCI_PMCSR_STATE_D2;
676 		break;
677 	case PCI_PMCSR_STATE_D3:
678 		if (now == PCI_PMCSR_STATE_D3)
679 			return 0;
680 		value |= PCI_PMCSR_STATE_D3;
681 		break;
682 	default:
683 		return EINVAL;
684 	}
685 	pci_conf_write(pc, tag, offset + PCI_PMCSR, value);
686 	DELAY(1000);
687 	return 0;
688 }
689 
690 int
691 pci_activate(pci_chipset_tag_t pc, pcitag_t tag, void *sc,
692     int (*wakefun)(pci_chipset_tag_t, pcitag_t, void *, pcireg_t))
693 {
694 	struct device *dv = sc;
695 	pcireg_t pmode;
696 	int error;
697 
698 	if ((error = pci_get_powerstate(pc, tag, &pmode)))
699 		return error;
700 
701 	switch (pmode) {
702 	case PCI_PMCSR_STATE_D0:
703 		break;
704 	case PCI_PMCSR_STATE_D3:
705 		if (wakefun == NULL) {
706 			/*
707 			 * The card has lost all configuration data in
708 			 * this state, so punt.
709 			 */
710 			aprint_error(
711 			    "%s: unable to wake up from power state D3\n",
712 			    dv->dv_xname);
713 			return EOPNOTSUPP;
714 		}
715 		/*FALLTHROUGH*/
716 	default:
717 		if (wakefun) {
718 			error = (*wakefun)(pc, tag, sc, pmode);
719 			if (error)
720 				return error;
721 		}
722 		aprint_normal("%s: waking up from power state D%d\n",
723 		    dv->dv_xname, pmode);
724 		if ((error = pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0)))
725 			return error;
726 	}
727 	return 0;
728 }
729 
730 int
731 pci_activate_null(pci_chipset_tag_t pc, pcitag_t tag, void *sc, pcireg_t state)
732 {
733 	return 0;
734 }
735 
736 CFATTACH_DECL2(pci, sizeof(struct pci_softc),
737     pcimatch, pciattach, NULL, NULL, pcirescan, pcidevdetached);
738