xref: /netbsd-src/sys/dev/pci/pci.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: pci.c,v 1.129 2010/06/06 18:58:23 pgoyette 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.129 2010/06/06 18:58:23 pgoyette 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 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 
111 	return 0;
112 }
113 
114 int
115 pcimatch(device_t parent, cfdata_t cf, void *aux)
116 {
117 	struct pcibus_attach_args *pba = aux;
118 
119 	/* Check the locators */
120 	if (cf->cf_loc[PCIBUSCF_BUS] != PCIBUSCF_BUS_DEFAULT &&
121 	    cf->cf_loc[PCIBUSCF_BUS] != pba->pba_bus)
122 		return 0;
123 
124 	/* sanity */
125 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
126 		return 0;
127 
128 	/*
129 	 * XXX check other (hardware?) indicators
130 	 */
131 
132 	return 1;
133 }
134 
135 void
136 pciattach(device_t parent, device_t self, void *aux)
137 {
138 	struct pcibus_attach_args *pba = aux;
139 	struct pci_softc *sc = device_private(self);
140 	int io_enabled, mem_enabled, mrl_enabled, mrm_enabled, mwi_enabled;
141 	const char *sep = "";
142 	static const int wildcard[PCICF_NLOCS] = {
143 		PCICF_DEV_DEFAULT, PCICF_FUNCTION_DEFAULT
144 	};
145 
146 	sc->sc_dev = self;
147 
148 	pci_attach_hook(parent, self, pba);
149 
150 	aprint_naive("\n");
151 	aprint_normal("\n");
152 
153 	io_enabled = (pba->pba_flags & PCI_FLAGS_IO_ENABLED);
154 	mem_enabled = (pba->pba_flags & PCI_FLAGS_MEM_ENABLED);
155 	mrl_enabled = (pba->pba_flags & PCI_FLAGS_MRL_OKAY);
156 	mrm_enabled = (pba->pba_flags & PCI_FLAGS_MRM_OKAY);
157 	mwi_enabled = (pba->pba_flags & PCI_FLAGS_MWI_OKAY);
158 
159 	if (io_enabled == 0 && mem_enabled == 0) {
160 		aprint_error_dev(self, "no spaces enabled!\n");
161 		goto fail;
162 	}
163 
164 #define	PRINT(str)							\
165 do {									\
166 	aprint_verbose("%s%s", sep, str);				\
167 	sep = ", ";							\
168 } while (/*CONSTCOND*/0)
169 
170 	aprint_verbose_dev(self, "");
171 
172 	if (io_enabled)
173 		PRINT("i/o space");
174 	if (mem_enabled)
175 		PRINT("memory space");
176 	aprint_verbose(" enabled");
177 
178 	if (mrl_enabled || mrm_enabled || mwi_enabled) {
179 		if (mrl_enabled)
180 			PRINT("rd/line");
181 		if (mrm_enabled)
182 			PRINT("rd/mult");
183 		if (mwi_enabled)
184 			PRINT("wr/inv");
185 		aprint_verbose(" ok");
186 	}
187 
188 	aprint_verbose("\n");
189 
190 #undef PRINT
191 
192 	sc->sc_iot = pba->pba_iot;
193 	sc->sc_memt = pba->pba_memt;
194 	sc->sc_dmat = pba->pba_dmat;
195 	sc->sc_dmat64 = pba->pba_dmat64;
196 	sc->sc_pc = pba->pba_pc;
197 	sc->sc_bus = pba->pba_bus;
198 	sc->sc_bridgetag = pba->pba_bridgetag;
199 	sc->sc_maxndevs = pci_bus_maxdevs(pba->pba_pc, pba->pba_bus);
200 	sc->sc_intrswiz = pba->pba_intrswiz;
201 	sc->sc_intrtag = pba->pba_intrtag;
202 	sc->sc_flags = pba->pba_flags;
203 
204 	device_pmf_driver_set_child_register(sc->sc_dev, pci_child_register);
205 
206 	pcirescan(sc->sc_dev, "pci", wildcard);
207 
208 fail:
209 	if (!pmf_device_register(self, NULL, NULL))
210 		aprint_error_dev(self, "couldn't establish power handler\n");
211 }
212 
213 int
214 pcidetach(device_t self, int flags)
215 {
216 	int rc;
217 
218 	if ((rc = config_detach_children(self, flags)) != 0)
219 		return rc;
220 	pmf_device_deregister(self);
221 	return 0;
222 }
223 
224 int
225 pciprint(void *aux, const char *pnp)
226 {
227 	struct pci_attach_args *pa = aux;
228 	char devinfo[256];
229 	const struct pci_quirkdata *qd;
230 
231 	if (pnp) {
232 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
233 		aprint_normal("%s at %s", devinfo, pnp);
234 	}
235 	aprint_normal(" dev %d function %d", pa->pa_device, pa->pa_function);
236 	if (pci_config_dump) {
237 		printf(": ");
238 		pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
239 		if (!pnp)
240 			pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo, sizeof(devinfo));
241 		printf("%s at %s", devinfo, pnp ? pnp : "?");
242 		printf(" dev %d function %d (", pa->pa_device, pa->pa_function);
243 #ifdef __i386__
244 		printf("tag %#lx, intrtag %#lx, intrswiz %#lx, intrpin %#lx",
245 		    *(long *)&pa->pa_tag, *(long *)&pa->pa_intrtag,
246 		    (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
247 #else
248 		printf("intrswiz %#lx, intrpin %#lx",
249 		    (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
250 #endif
251 		printf(", i/o %s, mem %s,",
252 		    pa->pa_flags & PCI_FLAGS_IO_ENABLED ? "on" : "off",
253 		    pa->pa_flags & PCI_FLAGS_MEM_ENABLED ? "on" : "off");
254 		qd = pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
255 		    PCI_PRODUCT(pa->pa_id));
256 		if (qd == NULL) {
257 			printf(" no quirks");
258 		} else {
259 			snprintb(devinfo, sizeof (devinfo),
260 			    "\002\001multifn\002singlefn\003skipfunc0"
261 			    "\004skipfunc1\005skipfunc2\006skipfunc3"
262 			    "\007skipfunc4\010skipfunc5\011skipfunc6"
263 			    "\012skipfunc7", qd->quirks);
264 			printf(" quirks %s", devinfo);
265 		}
266 		printf(")");
267 	}
268 	return UNCONF;
269 }
270 
271 int
272 pci_probe_device(struct pci_softc *sc, pcitag_t tag,
273     int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
274 {
275 	pci_chipset_tag_t pc = sc->sc_pc;
276 	struct pci_attach_args pa;
277 	pcireg_t id, csr, class, intr, bhlcr;
278 	int ret, pin, bus, device, function;
279 	int locs[PCICF_NLOCS];
280 
281 	pci_decompose_tag(pc, tag, &bus, &device, &function);
282 
283 	/* a driver already attached? */
284 	if (sc->PCI_SC_DEVICESC(device, function).c_dev != NULL && !match)
285 		return 0;
286 
287 	bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
288 	if (PCI_HDRTYPE_TYPE(bhlcr) > 2)
289 		return 0;
290 
291 	id = pci_conf_read(pc, tag, PCI_ID_REG);
292 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
293 	class = pci_conf_read(pc, tag, PCI_CLASS_REG);
294 
295 	/* Invalid vendor ID value? */
296 	if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
297 		return 0;
298 	/* XXX Not invalid, but we've done this ~forever. */
299 	if (PCI_VENDOR(id) == 0)
300 		return 0;
301 
302 	pa.pa_iot = sc->sc_iot;
303 	pa.pa_memt = sc->sc_memt;
304 	pa.pa_dmat = sc->sc_dmat;
305 	pa.pa_dmat64 = sc->sc_dmat64;
306 	pa.pa_pc = pc;
307 	pa.pa_bus = bus;
308 	pa.pa_device = device;
309 	pa.pa_function = function;
310 	pa.pa_tag = tag;
311 	pa.pa_id = id;
312 	pa.pa_class = class;
313 
314 	/*
315 	 * Set up memory, I/O enable, and PCI command flags
316 	 * as appropriate.
317 	 */
318 	pa.pa_flags = sc->sc_flags;
319 	if ((csr & PCI_COMMAND_IO_ENABLE) == 0)
320 		pa.pa_flags &= ~PCI_FLAGS_IO_ENABLED;
321 	if ((csr & PCI_COMMAND_MEM_ENABLE) == 0)
322 		pa.pa_flags &= ~PCI_FLAGS_MEM_ENABLED;
323 
324 	/*
325 	 * If the cache line size is not configured, then
326 	 * clear the MRL/MRM/MWI command-ok flags.
327 	 */
328 	if (PCI_CACHELINE(bhlcr) == 0)
329 		pa.pa_flags &= ~(PCI_FLAGS_MRL_OKAY|
330 		    PCI_FLAGS_MRM_OKAY|PCI_FLAGS_MWI_OKAY);
331 
332 	if (sc->sc_bridgetag == NULL) {
333 		pa.pa_intrswiz = 0;
334 		pa.pa_intrtag = tag;
335 	} else {
336 		pa.pa_intrswiz = sc->sc_intrswiz + device;
337 		pa.pa_intrtag = sc->sc_intrtag;
338 	}
339 
340 	intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
341 
342 	pin = PCI_INTERRUPT_PIN(intr);
343 	pa.pa_rawintrpin = pin;
344 	if (pin == PCI_INTERRUPT_PIN_NONE) {
345 		/* no interrupt */
346 		pa.pa_intrpin = 0;
347 	} else {
348 		/*
349 		 * swizzle it based on the number of busses we're
350 		 * behind and our device number.
351 		 */
352 		pa.pa_intrpin = 	/* XXX */
353 		    ((pin + pa.pa_intrswiz - 1) % 4) + 1;
354 	}
355 	pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
356 
357 	if (match != NULL) {
358 		ret = (*match)(&pa);
359 		if (ret != 0 && pap != NULL)
360 			*pap = pa;
361 	} else {
362 		struct pci_child *c;
363 		locs[PCICF_DEV] = device;
364 		locs[PCICF_FUNCTION] = function;
365 
366 		c = &sc->PCI_SC_DEVICESC(device, function);
367 		pci_conf_capture(pc, tag, &c->c_conf);
368 		if (pci_get_powerstate(pc, tag, &c->c_powerstate) == 0)
369 			c->c_psok = true;
370 		else
371 			c->c_psok = false;
372 
373 		c->c_dev = config_found_sm_loc(sc->sc_dev, "pci", locs, &pa,
374 					     pciprint, config_stdsubmatch);
375 
376 		ret = (c->c_dev != NULL);
377 	}
378 
379 	return ret;
380 }
381 
382 void
383 pcidevdetached(device_t self, device_t child)
384 {
385 	struct pci_softc *sc = device_private(self);
386 	int d, f;
387 	pcitag_t tag;
388 	struct pci_child *c;
389 
390 	d = device_locator(child, PCICF_DEV);
391 	f = device_locator(child, PCICF_FUNCTION);
392 
393 	c = &sc->PCI_SC_DEVICESC(d, f);
394 
395 	KASSERT(c->c_dev == child);
396 
397 	tag = pci_make_tag(sc->sc_pc, sc->sc_bus, d, f);
398 	if (c->c_psok)
399 		pci_set_powerstate(sc->sc_pc, tag, c->c_powerstate);
400 	pci_conf_restore(sc->sc_pc, tag, &c->c_conf);
401 	c->c_dev = NULL;
402 }
403 
404 CFATTACH_DECL3_NEW(pci, sizeof(struct pci_softc),
405     pcimatch, pciattach, pcidetach, NULL, pcirescan, pcidevdetached,
406     DVF_DETACH_SHUTDOWN);
407 
408 int
409 pci_get_capability(pci_chipset_tag_t pc, pcitag_t tag, int capid,
410     int *offset, pcireg_t *value)
411 {
412 	pcireg_t reg;
413 	unsigned int ofs;
414 
415 	reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
416 	if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
417 		return 0;
418 
419 	/* Determine the Capability List Pointer register to start with. */
420 	reg = pci_conf_read(pc, tag, PCI_BHLC_REG);
421 	switch (PCI_HDRTYPE_TYPE(reg)) {
422 	case 0:	/* standard device header */
423 	case 1: /* PCI-PCI bridge header */
424 		ofs = PCI_CAPLISTPTR_REG;
425 		break;
426 	case 2:	/* PCI-CardBus Bridge header */
427 		ofs = PCI_CARDBUS_CAPLISTPTR_REG;
428 		break;
429 	default:
430 		return 0;
431 	}
432 
433 	ofs = PCI_CAPLIST_PTR(pci_conf_read(pc, tag, ofs));
434 	while (ofs != 0) {
435 		if ((ofs & 3) || (ofs < 0x40)) {
436 			int bus, device, function;
437 
438 			pci_decompose_tag(pc, tag, &bus, &device, &function);
439 
440 			printf("Skipping broken PCI header on %d:%d:%d\n",
441 			    bus, device, function);
442 			break;
443 		}
444 		reg = pci_conf_read(pc, tag, ofs);
445 		if (PCI_CAPLIST_CAP(reg) == capid) {
446 			if (offset)
447 				*offset = ofs;
448 			if (value)
449 				*value = reg;
450 			return 1;
451 		}
452 		ofs = PCI_CAPLIST_NEXT(reg);
453 	}
454 
455 	return 0;
456 }
457 
458 int
459 pci_find_device(struct pci_attach_args *pa,
460 		int (*match)(struct pci_attach_args *))
461 {
462 	extern struct cfdriver pci_cd;
463 	device_t pcidev;
464 	int i;
465 	static const int wildcard[2] = {
466 		PCICF_DEV_DEFAULT,
467 		PCICF_FUNCTION_DEFAULT
468 	};
469 
470 	for (i = 0; i < pci_cd.cd_ndevs; i++) {
471 		pcidev = device_lookup(&pci_cd, i);
472 		if (pcidev != NULL &&
473 		    pci_enumerate_bus(device_private(pcidev), wildcard,
474 		    		      match, pa) != 0)
475 			return 1;
476 	}
477 	return 0;
478 }
479 
480 #ifndef PCI_MACHDEP_ENUMERATE_BUS
481 /*
482  * Generic PCI bus enumeration routine.  Used unless machine-dependent
483  * code needs to provide something else.
484  */
485 int
486 pci_enumerate_bus(struct pci_softc *sc, const int *locators,
487     int (*match)(struct pci_attach_args *), struct pci_attach_args *pap)
488 {
489 	pci_chipset_tag_t pc = sc->sc_pc;
490 	int device, function, nfunctions, ret;
491 	const struct pci_quirkdata *qd;
492 	pcireg_t id, bhlcr;
493 	pcitag_t tag;
494 #ifdef __PCI_BUS_DEVORDER
495 	char devs[32];
496 	int i;
497 #endif
498 
499 #ifdef __PCI_BUS_DEVORDER
500 	pci_bus_devorder(sc->sc_pc, sc->sc_bus, devs);
501 	for (i = 0; (device = devs[i]) < 32 && device >= 0; i++)
502 #else
503 	for (device = 0; device < sc->sc_maxndevs; device++)
504 #endif
505 	{
506 		if ((locators[PCICF_DEV] != PCICF_DEV_DEFAULT) &&
507 		    (locators[PCICF_DEV] != device))
508 			continue;
509 
510 		tag = pci_make_tag(pc, sc->sc_bus, device, 0);
511 
512 		bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
513 		if (PCI_HDRTYPE_TYPE(bhlcr) > 2)
514 			continue;
515 
516 		id = pci_conf_read(pc, tag, PCI_ID_REG);
517 
518 		/* Invalid vendor ID value? */
519 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
520 			continue;
521 		/* XXX Not invalid, but we've done this ~forever. */
522 		if (PCI_VENDOR(id) == 0)
523 			continue;
524 
525 		qd = pci_lookup_quirkdata(PCI_VENDOR(id), PCI_PRODUCT(id));
526 
527 		if (qd != NULL &&
528 		      (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0)
529 			nfunctions = 8;
530 		else if (qd != NULL &&
531 		      (qd->quirks & PCI_QUIRK_MONOFUNCTION) != 0)
532 			nfunctions = 1;
533 		else
534 			nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
535 
536 		for (function = 0; function < nfunctions; function++) {
537 			if ((locators[PCICF_FUNCTION] != PCICF_FUNCTION_DEFAULT)
538 			    && (locators[PCICF_FUNCTION] != function))
539 				continue;
540 
541 			if (qd != NULL &&
542 			    (qd->quirks & PCI_QUIRK_SKIP_FUNC(function)) != 0)
543 				continue;
544 			tag = pci_make_tag(pc, sc->sc_bus, device, function);
545 			ret = pci_probe_device(sc, tag, match, pap);
546 			if (match != NULL && ret != 0)
547 				return ret;
548 		}
549 	}
550 	return 0;
551 }
552 #endif /* PCI_MACHDEP_ENUMERATE_BUS */
553 
554 
555 /*
556  * Vital Product Data (PCI 2.2)
557  */
558 
559 int
560 pci_vpd_read(pci_chipset_tag_t pc, pcitag_t tag, int offset, int count,
561     pcireg_t *data)
562 {
563 	uint32_t reg;
564 	int ofs, i, j;
565 
566 	KASSERT(data != NULL);
567 	KASSERT((offset + count) < 0x7fff);
568 
569 	if (pci_get_capability(pc, tag, PCI_CAP_VPD, &ofs, &reg) == 0)
570 		return 1;
571 
572 	for (i = 0; i < count; offset += sizeof(*data), i++) {
573 		reg &= 0x0000ffff;
574 		reg &= ~PCI_VPD_OPFLAG;
575 		reg |= PCI_VPD_ADDRESS(offset);
576 		pci_conf_write(pc, tag, ofs, reg);
577 
578 		/*
579 		 * PCI 2.2 does not specify how long we should poll
580 		 * for completion nor whether the operation can fail.
581 		 */
582 		j = 0;
583 		do {
584 			if (j++ == 20)
585 				return 1;
586 			delay(4);
587 			reg = pci_conf_read(pc, tag, ofs);
588 		} while ((reg & PCI_VPD_OPFLAG) == 0);
589 		data[i] = pci_conf_read(pc, tag, PCI_VPD_DATAREG(ofs));
590 	}
591 
592 	return 0;
593 }
594 
595 int
596 pci_vpd_write(pci_chipset_tag_t pc, pcitag_t tag, int offset, int count,
597     pcireg_t *data)
598 {
599 	pcireg_t reg;
600 	int ofs, i, j;
601 
602 	KASSERT(data != NULL);
603 	KASSERT((offset + count) < 0x7fff);
604 
605 	if (pci_get_capability(pc, tag, PCI_CAP_VPD, &ofs, &reg) == 0)
606 		return 1;
607 
608 	for (i = 0; i < count; offset += sizeof(*data), i++) {
609 		pci_conf_write(pc, tag, PCI_VPD_DATAREG(ofs), data[i]);
610 
611 		reg &= 0x0000ffff;
612 		reg |= PCI_VPD_OPFLAG;
613 		reg |= PCI_VPD_ADDRESS(offset);
614 		pci_conf_write(pc, tag, ofs, reg);
615 
616 		/*
617 		 * PCI 2.2 does not specify how long we should poll
618 		 * for completion nor whether the operation can fail.
619 		 */
620 		j = 0;
621 		do {
622 			if (j++ == 20)
623 				return 1;
624 			delay(1);
625 			reg = pci_conf_read(pc, tag, ofs);
626 		} while (reg & PCI_VPD_OPFLAG);
627 	}
628 
629 	return 0;
630 }
631 
632 int
633 pci_dma64_available(struct pci_attach_args *pa)
634 {
635 #ifdef _PCI_HAVE_DMA64
636 	if (BUS_DMA_TAG_VALID(pa->pa_dmat64))
637                         return 1;
638 #endif
639         return 0;
640 }
641 
642 void
643 pci_conf_capture(pci_chipset_tag_t pc, pcitag_t tag,
644 		  struct pci_conf_state *pcs)
645 {
646 	int off;
647 
648 	for (off = 0; off < 16; off++)
649 		pcs->reg[off] = pci_conf_read(pc, tag, (off * 4));
650 
651 	return;
652 }
653 
654 void
655 pci_conf_restore(pci_chipset_tag_t pc, pcitag_t tag,
656 		  struct pci_conf_state *pcs)
657 {
658 	int off;
659 	pcireg_t val;
660 
661 	for (off = 15; off >= 0; off--) {
662 		val = pci_conf_read(pc, tag, (off * 4));
663 		if (val != pcs->reg[off])
664 			pci_conf_write(pc, tag, (off * 4), pcs->reg[off]);
665 	}
666 
667 	return;
668 }
669 
670 /*
671  * Power Management Capability (Rev 2.2)
672  */
673 static int
674 pci_get_powerstate_int(pci_chipset_tag_t pc, pcitag_t tag , pcireg_t *state,
675     int offset)
676 {
677 	pcireg_t value, now;
678 
679 	value = pci_conf_read(pc, tag, offset + PCI_PMCSR);
680 	now = value & PCI_PMCSR_STATE_MASK;
681 	switch (now) {
682 	case PCI_PMCSR_STATE_D0:
683 	case PCI_PMCSR_STATE_D1:
684 	case PCI_PMCSR_STATE_D2:
685 	case PCI_PMCSR_STATE_D3:
686 		*state = now;
687 		return 0;
688 	default:
689 		return EINVAL;
690 	}
691 }
692 
693 int
694 pci_get_powerstate(pci_chipset_tag_t pc, pcitag_t tag , pcireg_t *state)
695 {
696 	int offset;
697 	pcireg_t value;
698 
699 	if (!pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, &value))
700 		return EOPNOTSUPP;
701 
702 	return pci_get_powerstate_int(pc, tag, state, offset);
703 }
704 
705 static int
706 pci_set_powerstate_int(pci_chipset_tag_t pc, pcitag_t tag, pcireg_t state,
707     int offset, pcireg_t cap_reg)
708 {
709 	pcireg_t value, cap, now;
710 
711 	cap = cap_reg >> PCI_PMCR_SHIFT;
712 	value = pci_conf_read(pc, tag, offset + PCI_PMCSR);
713 	now = value & PCI_PMCSR_STATE_MASK;
714 	value &= ~PCI_PMCSR_STATE_MASK;
715 
716 	if (now == state)
717 		return 0;
718 	switch (state) {
719 	case PCI_PMCSR_STATE_D0:
720 		break;
721 	case PCI_PMCSR_STATE_D1:
722 		if (now == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D3) {
723 			printf("invalid transition from %d to D1\n", (int)now);
724 			return EINVAL;
725 		}
726 		if (!(cap & PCI_PMCR_D1SUPP)) {
727 			printf("D1 not supported\n");
728 			return EOPNOTSUPP;
729 		}
730 		break;
731 	case PCI_PMCSR_STATE_D2:
732 		if (now == PCI_PMCSR_STATE_D3) {
733 			printf("invalid transition from %d to D2\n", (int)now);
734 			return EINVAL;
735 		}
736 		if (!(cap & PCI_PMCR_D2SUPP)) {
737 			printf("D2 not supported\n");
738 			return EOPNOTSUPP;
739 		}
740 		break;
741 	case PCI_PMCSR_STATE_D3:
742 		break;
743 	default:
744 		return EINVAL;
745 	}
746 	value |= state;
747 	pci_conf_write(pc, tag, offset + PCI_PMCSR, value);
748 	/* delay according to pcipm1.2, ch. 5.6.1 */
749 	if (state == PCI_PMCSR_STATE_D3 || now == PCI_PMCSR_STATE_D3)
750 		DELAY(10000);
751 	else if (state == PCI_PMCSR_STATE_D2 || now == PCI_PMCSR_STATE_D2)
752 		DELAY(200);
753 
754 	return 0;
755 }
756 
757 int
758 pci_set_powerstate(pci_chipset_tag_t pc, pcitag_t tag, pcireg_t state)
759 {
760 	int offset;
761 	pcireg_t value;
762 
763 	if (!pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &offset, &value)) {
764 		printf("pci_set_powerstate not supported\n");
765 		return EOPNOTSUPP;
766 	}
767 
768 	return pci_set_powerstate_int(pc, tag, state, offset, value);
769 }
770 
771 int
772 pci_activate(pci_chipset_tag_t pc, pcitag_t tag, device_t dev,
773     int (*wakefun)(pci_chipset_tag_t, pcitag_t, device_t, pcireg_t))
774 {
775 	pcireg_t pmode;
776 	int error;
777 
778 	if ((error = pci_get_powerstate(pc, tag, &pmode)))
779 		return error;
780 
781 	switch (pmode) {
782 	case PCI_PMCSR_STATE_D0:
783 		break;
784 	case PCI_PMCSR_STATE_D3:
785 		if (wakefun == NULL) {
786 			/*
787 			 * The card has lost all configuration data in
788 			 * this state, so punt.
789 			 */
790 			aprint_error_dev(dev,
791 			    "unable to wake up from power state D3\n");
792 			return EOPNOTSUPP;
793 		}
794 		/*FALLTHROUGH*/
795 	default:
796 		if (wakefun) {
797 			error = (*wakefun)(pc, tag, dev, pmode);
798 			if (error)
799 				return error;
800 		}
801 		aprint_normal_dev(dev, "waking up from power state D%d\n",
802 		    pmode);
803 		if ((error = pci_set_powerstate(pc, tag, PCI_PMCSR_STATE_D0)))
804 			return error;
805 	}
806 	return 0;
807 }
808 
809 int
810 pci_activate_null(pci_chipset_tag_t pc, pcitag_t tag,
811     device_t dev, pcireg_t state)
812 {
813 	return 0;
814 }
815 
816 struct pci_child_power {
817 	struct pci_conf_state p_pciconf;
818 	pci_chipset_tag_t p_pc;
819 	pcitag_t p_tag;
820 	bool p_has_pm;
821 	int p_pm_offset;
822 	pcireg_t p_pm_cap;
823 	pcireg_t p_class;
824 };
825 
826 static bool
827 pci_child_suspend(device_t dv, const pmf_qual_t *qual)
828 {
829 	struct pci_child_power *priv = device_pmf_bus_private(dv);
830 	pcireg_t ocsr, csr;
831 
832 	pci_conf_capture(priv->p_pc, priv->p_tag, &priv->p_pciconf);
833 
834 	if (!priv->p_has_pm)
835 		return true; /* ??? hopefully handled by ACPI */
836 	if (PCI_CLASS(priv->p_class) == PCI_CLASS_DISPLAY)
837 		return true; /* XXX */
838 
839 	/* disable decoding and busmastering, see pcipm1.2 ch. 8.2.1 */
840 	ocsr = pci_conf_read(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG);
841 	csr = ocsr & ~(PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE
842 		       | PCI_COMMAND_MASTER_ENABLE);
843 	pci_conf_write(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG, csr);
844 	if (pci_set_powerstate_int(priv->p_pc, priv->p_tag,
845 	    PCI_PMCSR_STATE_D3, priv->p_pm_offset, priv->p_pm_cap)) {
846 		pci_conf_write(priv->p_pc, priv->p_tag,
847 			       PCI_COMMAND_STATUS_REG, ocsr);
848 		aprint_error_dev(dv, "unsupported state, continuing.\n");
849 		return false;
850 	}
851 	return true;
852 }
853 
854 static bool
855 pci_child_resume(device_t dv, const pmf_qual_t *qual)
856 {
857 	struct pci_child_power *priv = device_pmf_bus_private(dv);
858 
859 	if (priv->p_has_pm &&
860 	    pci_set_powerstate_int(priv->p_pc, priv->p_tag,
861 	    PCI_PMCSR_STATE_D0, priv->p_pm_offset, priv->p_pm_cap)) {
862 		aprint_error_dev(dv, "unsupported state, continuing.\n");
863 		return false;
864 	}
865 
866 	pci_conf_restore(priv->p_pc, priv->p_tag, &priv->p_pciconf);
867 
868 	return true;
869 }
870 
871 static bool
872 pci_child_shutdown(device_t dv, int how)
873 {
874 	struct pci_child_power *priv = device_pmf_bus_private(dv);
875 	pcireg_t csr;
876 
877 	/* disable busmastering */
878 	csr = pci_conf_read(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG);
879 	csr &= ~PCI_COMMAND_MASTER_ENABLE;
880 	pci_conf_write(priv->p_pc, priv->p_tag, PCI_COMMAND_STATUS_REG, csr);
881 	return true;
882 }
883 
884 static void
885 pci_child_deregister(device_t dv)
886 {
887 	struct pci_child_power *priv = device_pmf_bus_private(dv);
888 
889 	free(priv, M_DEVBUF);
890 }
891 
892 static bool
893 pci_child_register(device_t child)
894 {
895 	device_t self = device_parent(child);
896 	struct pci_softc *sc = device_private(self);
897 	struct pci_child_power *priv;
898 	int device, function, off;
899 	pcireg_t reg;
900 
901 	priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK);
902 
903 	device = device_locator(child, PCICF_DEV);
904 	function = device_locator(child, PCICF_FUNCTION);
905 
906 	priv->p_pc = sc->sc_pc;
907 	priv->p_tag = pci_make_tag(priv->p_pc, sc->sc_bus, device,
908 	    function);
909 	priv->p_class = pci_conf_read(priv->p_pc, priv->p_tag, PCI_CLASS_REG);
910 
911 	if (pci_get_capability(priv->p_pc, priv->p_tag,
912 			       PCI_CAP_PWRMGMT, &off, &reg)) {
913 		priv->p_has_pm = true;
914 		priv->p_pm_offset = off;
915 		priv->p_pm_cap = reg;
916 	} else {
917 		priv->p_has_pm = false;
918 		priv->p_pm_offset = -1;
919 	}
920 
921 	device_pmf_bus_register(child, priv, pci_child_suspend,
922 	    pci_child_resume, pci_child_shutdown, pci_child_deregister);
923 
924 	return true;
925 }
926