1 /* $NetBSD: pci.c,v 1.124 2009/08/19 21:36:47 dyoung Exp $ */
4 * Copyright (c) 1995, 1996, 1997, 1998
5 * Christopher G. Demetriou. All rights reserved.
6 * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
35 * PCI bus autoconfiguration.
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: pci.c,v 1.124 2009/08/19 21:36:47 dyoung Exp $");
43 #include <sys/param.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
48 #include <dev/pci/pcireg.h>
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcidevs.h>
52 #include <uvm/uvm_extern.h>
58 static bool pci_child_register(device_t
);
60 #ifdef PCI_CONFIG_DUMP
61 int pci_config_dump
= 1;
63 int pci_config_dump
= 0;
66 int pciprint(void *, const char *);
68 #ifdef PCI_MACHDEP_ENUMERATE_BUS
69 #define pci_enumerate_bus PCI_MACHDEP_ENUMERATE_BUS
71 int pci_enumerate_bus(struct pci_softc
*, const int *,
72 int (*)(struct pci_attach_args
*), struct pci_attach_args
*);
76 * Important note about PCI-ISA bridges:
78 * Callbacks are used to configure these devices so that ISA/EISA bridges
79 * can attach their child busses after PCI configuration is done.
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).
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.
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.
98 * We use the generic config_defer() facility to achieve this.
102 pcirescan(device_t self
, const char *ifattr
, const int *locators
)
104 struct pci_softc
*sc
= device_private(self
);
106 KASSERT(ifattr
&& !strcmp(ifattr
, "pci"));
109 pci_enumerate_bus(sc
, locators
, NULL
, NULL
);
114 pcimatch(device_t parent
, cfdata_t cf
, void *aux
)
116 struct pcibus_attach_args
*pba
= aux
;
118 /* Check the locators */
119 if (cf
->cf_loc
[PCIBUSCF_BUS
] != PCIBUSCF_BUS_DEFAULT
&&
120 cf
->cf_loc
[PCIBUSCF_BUS
] != pba
->pba_bus
)
124 if (pba
->pba_bus
< 0 || pba
->pba_bus
> 255)
128 * XXX check other (hardware?) indicators
135 pciattach(device_t parent
, device_t self
, void *aux
)
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
147 pci_attach_hook(parent
, self
, pba
);
152 io_enabled
= (pba
->pba_flags
& PCI_FLAGS_IO_ENABLED
);
153 mem_enabled
= (pba
->pba_flags
& PCI_FLAGS_MEM_ENABLED
);
154 mrl_enabled
= (pba
->pba_flags
& PCI_FLAGS_MRL_OKAY
);
155 mrm_enabled
= (pba
->pba_flags
& PCI_FLAGS_MRM_OKAY
);
156 mwi_enabled
= (pba
->pba_flags
& PCI_FLAGS_MWI_OKAY
);
158 if (io_enabled
== 0 && mem_enabled
== 0) {
159 aprint_error_dev(self
, "no spaces enabled!\n");
165 aprint_verbose("%s%s", sep, str); \
167 } while (/*CONSTCOND*/0)
169 aprint_verbose_dev(self
, "");
174 PRINT("memory space");
175 aprint_verbose(" enabled");
177 if (mrl_enabled
|| mrm_enabled
|| mwi_enabled
) {
184 aprint_verbose(" ok");
187 aprint_verbose("\n");
191 sc
->sc_iot
= pba
->pba_iot
;
192 sc
->sc_memt
= pba
->pba_memt
;
193 sc
->sc_dmat
= pba
->pba_dmat
;
194 sc
->sc_dmat64
= pba
->pba_dmat64
;
195 sc
->sc_pc
= pba
->pba_pc
;
196 sc
->sc_bus
= pba
->pba_bus
;
197 sc
->sc_bridgetag
= pba
->pba_bridgetag
;
198 sc
->sc_maxndevs
= pci_bus_maxdevs(pba
->pba_pc
, pba
->pba_bus
);
199 sc
->sc_intrswiz
= pba
->pba_intrswiz
;
200 sc
->sc_intrtag
= pba
->pba_intrtag
;
201 sc
->sc_flags
= pba
->pba_flags
;
203 device_pmf_driver_set_child_register(sc
->sc_dev
, pci_child_register
);
205 pcirescan(sc
->sc_dev
, "pci", wildcard
);
208 if (!pmf_device_register(self
, NULL
, NULL
))
209 aprint_error_dev(self
, "couldn't establish power handler\n");
213 pcidetach(device_t self
, int flags
)
217 if ((rc
= config_detach_children(self
, flags
)) != 0)
219 pmf_device_deregister(self
);
224 pciprint(void *aux
, const char *pnp
)
226 struct pci_attach_args
*pa
= aux
;
228 const struct pci_quirkdata
*qd
;
231 pci_devinfo(pa
->pa_id
, pa
->pa_class
, 1, devinfo
, sizeof(devinfo
));
232 aprint_normal("%s at %s", devinfo
, pnp
);
234 aprint_normal(" dev %d function %d", pa
->pa_device
, pa
->pa_function
);
235 if (pci_config_dump
) {
237 pci_conf_print(pa
->pa_pc
, pa
->pa_tag
, NULL
);
239 pci_devinfo(pa
->pa_id
, pa
->pa_class
, 1, devinfo
, sizeof(devinfo
));
240 printf("%s at %s", devinfo
, pnp
? pnp
: "?");
241 printf(" dev %d function %d (", pa
->pa_device
, pa
->pa_function
);
243 printf("tag %#lx, intrtag %#lx, intrswiz %#lx, intrpin %#lx",
244 *(long *)&pa
->pa_tag
, *(long *)&pa
->pa_intrtag
,
245 (long)pa
->pa_intrswiz
, (long)pa
->pa_intrpin
);
247 printf("intrswiz %#lx, intrpin %#lx",
248 (long)pa
->pa_intrswiz
, (long)pa
->pa_intrpin
);
250 printf(", i/o %s, mem %s,",
251 pa
->pa_flags
& PCI_FLAGS_IO_ENABLED
? "on" : "off",
252 pa
->pa_flags
& PCI_FLAGS_MEM_ENABLED
? "on" : "off");
253 qd
= pci_lookup_quirkdata(PCI_VENDOR(pa
->pa_id
),
254 PCI_PRODUCT(pa
->pa_id
));
256 printf(" no quirks");
258 snprintb(devinfo
, sizeof (devinfo
),
259 "\002\001multifn\002singlefn\003skipfunc0"
260 "\004skipfunc1\005skipfunc2\006skipfunc3"
261 "\007skipfunc4\010skipfunc5\011skipfunc6"
262 "\012skipfunc7", qd
->quirks
);
263 printf(" quirks %s", devinfo
);
271 pci_probe_device(struct pci_softc
*sc
, pcitag_t tag
,
272 int (*match
)(struct pci_attach_args
*), struct pci_attach_args
*pap
)
274 pci_chipset_tag_t pc
= sc
->sc_pc
;
275 struct pci_attach_args pa
;
276 pcireg_t id
, csr
, class, intr
, bhlcr
;
277 int ret
, pin
, bus
, device
, function
;
278 int locs
[PCICF_NLOCS
];
280 pci_decompose_tag(pc
, tag
, &bus
, &device
, &function
);
282 /* a driver already attached? */
283 if (sc
->PCI_SC_DEVICESC(device
, function
).c_dev
!= NULL
&& !match
)
286 bhlcr
= pci_conf_read(pc
, tag
, PCI_BHLC_REG
);
287 if (PCI_HDRTYPE_TYPE(bhlcr
) > 2)
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
);
294 /* Invalid vendor ID value? */
295 if (PCI_VENDOR(id
) == PCI_VENDOR_INVALID
)
297 /* XXX Not invalid, but we've done this ~forever. */
298 if (PCI_VENDOR(id
) == 0)
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
;
307 pa
.pa_device
= device
;
308 pa
.pa_function
= function
;
314 * Set up memory, I/O enable, and PCI command flags
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
;
324 * If the cache line size is not configured, then
325 * clear the MRL/MRM/MWI command-ok flags.
327 if (PCI_CACHELINE(bhlcr
) == 0)
328 pa
.pa_flags
&= ~(PCI_FLAGS_MRL_OKAY
|
329 PCI_FLAGS_MRM_OKAY
|PCI_FLAGS_MWI_OKAY
);
331 if (sc
->sc_bridgetag
== NULL
) {
335 pa
.pa_intrswiz
= sc
->sc_intrswiz
+ device
;
336 pa
.pa_intrtag
= sc
->sc_intrtag
;
339 intr
= pci_conf_read(pc
, tag
, PCI_INTERRUPT_REG
);
341 pin
= PCI_INTERRUPT_PIN(intr
);
342 pa
.pa_rawintrpin
= pin
;
343 if (pin
== PCI_INTERRUPT_PIN_NONE
) {
348 * swizzle it based on the number of busses we're
349 * behind and our device number.
351 pa
.pa_intrpin
= /* XXX */
352 ((pin
+ pa
.pa_intrswiz
- 1) % 4) + 1;
354 pa
.pa_intrline
= PCI_INTERRUPT_LINE(intr
);
358 if (ret
!= 0 && pap
!= NULL
)
362 locs
[PCICF_DEV
] = device
;
363 locs
[PCICF_FUNCTION
] = function
;
365 c
= &sc
->PCI_SC_DEVICESC(device
, function
);
366 pci_conf_capture(pc
, tag
, &c
->c_conf
);
367 if (pci_get_powerstate(pc
, tag
, &c
->c_powerstate
) == 0)
372 c
->c_dev
= config_found_sm_loc(sc
->sc_dev
, "pci", locs
, &pa
,
373 pciprint
, config_stdsubmatch
);
375 ret
= (c
->c_dev
!= NULL
);
382 pcidevdetached(device_t self
, device_t child
)
384 struct pci_softc
*sc
= device_private(self
);
389 d
= device_locator(child
, PCICF_DEV
);
390 f
= device_locator(child
, PCICF_FUNCTION
);
392 c
= &sc
->PCI_SC_DEVICESC(d
, f
);
394 KASSERT(c
->c_dev
== child
);
396 tag
= pci_make_tag(sc
->sc_pc
, sc
->sc_bus
, d
, f
);
398 pci_set_powerstate(sc
->sc_pc
, tag
, c
->c_powerstate
);
399 pci_conf_restore(sc
->sc_pc
, tag
, &c
->c_conf
);
403 CFATTACH_DECL3_NEW(pci
, sizeof(struct pci_softc
),
404 pcimatch
, pciattach
, pcidetach
, NULL
, pcirescan
, pcidevdetached
,
405 DVF_DETACH_SHUTDOWN
);
408 pci_get_capability(pci_chipset_tag_t pc
, pcitag_t tag
, int capid
,
409 int *offset
, pcireg_t
*value
)
414 reg
= pci_conf_read(pc
, tag
, PCI_COMMAND_STATUS_REG
);
415 if (!(reg
& PCI_STATUS_CAPLIST_SUPPORT
))
418 /* Determine the Capability List Pointer register to start with. */
419 reg
= pci_conf_read(pc
, tag
, PCI_BHLC_REG
);
420 switch (PCI_HDRTYPE_TYPE(reg
)) {
421 case 0: /* standard device header */
422 case 1: /* PCI-PCI bridge header */
423 ofs
= PCI_CAPLISTPTR_REG
;
425 case 2: /* PCI-CardBus Bridge header */
426 ofs
= PCI_CARDBUS_CAPLISTPTR_REG
;
432 ofs
= PCI_CAPLIST_PTR(pci_conf_read(pc
, tag
, ofs
));
434 if ((ofs
& 3) || (ofs
< 0x40)) {
435 int bus
, device
, function
;
437 pci_decompose_tag(pc
, tag
, &bus
, &device
, &function
);
439 printf("Skipping broken PCI header on %d:%d:%d\n",
440 bus
, device
, function
);
443 reg
= pci_conf_read(pc
, tag
, ofs
);
444 if (PCI_CAPLIST_CAP(reg
) == capid
) {
451 ofs
= PCI_CAPLIST_NEXT(reg
);
458 pci_find_device(struct pci_attach_args
*pa
,
459 int (*match
)(struct pci_attach_args
*))
461 extern struct cfdriver pci_cd
;
464 static const int wildcard
[2] = {
466 PCICF_FUNCTION_DEFAULT
469 for (i
= 0; i
< pci_cd
.cd_ndevs
; i
++) {
470 pcidev
= device_lookup(&pci_cd
, i
);
471 if (pcidev
!= NULL
&&
472 pci_enumerate_bus(device_private(pcidev
), wildcard
,
479 #ifndef PCI_MACHDEP_ENUMERATE_BUS
481 * Generic PCI bus enumeration routine. Used unless machine-dependent
482 * code needs to provide something else.
485 pci_enumerate_bus(struct pci_softc
*sc
, const int *locators
,
486 int (*match
)(struct pci_attach_args
*), struct pci_attach_args
*pap
)
488 pci_chipset_tag_t pc
= sc
->sc_pc
;
489 int device
, function
, nfunctions
, ret
;
490 const struct pci_quirkdata
*qd
;
493 #ifdef __PCI_BUS_DEVORDER
498 #ifdef __PCI_BUS_DEVORDER
499 pci_bus_devorder(sc
->sc_pc
, sc
->sc_bus
, devs
);
500 for (i
= 0; (device
= devs
[i
]) < 32 && device
>= 0; i
++)
502 for (device
= 0; device
< sc
->sc_maxndevs
; device
++)
505 if ((locators
[PCICF_DEV
] != PCICF_DEV_DEFAULT
) &&
506 (locators
[PCICF_DEV
] != device
))
509 tag
= pci_make_tag(pc
, sc
->sc_bus
, device
, 0);
511 bhlcr
= pci_conf_read(pc
, tag
, PCI_BHLC_REG
);
512 if (PCI_HDRTYPE_TYPE(bhlcr
) > 2)
515 id
= pci_conf_read(pc
, tag
, PCI_ID_REG
);
517 /* Invalid vendor ID value? */
518 if (PCI_VENDOR(id
) == PCI_VENDOR_INVALID
)
520 /* XXX Not invalid, but we've done this ~forever. */
521 if (PCI_VENDOR(id
) == 0)
524 qd
= pci_lookup_quirkdata(PCI_VENDOR(id
), PCI_PRODUCT(id
));
527 (qd
->quirks
& PCI_QUIRK_MULTIFUNCTION
) != 0)
529 else if (qd
!= NULL
&&
530 (qd
->quirks
& PCI_QUIRK_MONOFUNCTION
) != 0)
533 nfunctions
= PCI_HDRTYPE_MULTIFN(bhlcr
) ? 8 : 1;
535 for (function
= 0; function
< nfunctions
; function
++) {
536 if ((locators
[PCICF_FUNCTION
] != PCICF_FUNCTION_DEFAULT
)
537 && (locators
[PCICF_FUNCTION
] != function
))
541 (qd
->quirks
& PCI_QUIRK_SKIP_FUNC(function
)) != 0)
543 tag
= pci_make_tag(pc
, sc
->sc_bus
, device
, function
);
544 ret
= pci_probe_device(sc
, tag
, match
, pap
);
545 if (match
!= NULL
&& ret
!= 0)
551 #endif /* PCI_MACHDEP_ENUMERATE_BUS */
555 * Vital Product Data (PCI 2.2)
559 pci_vpd_read(pci_chipset_tag_t pc
, pcitag_t tag
, int offset
, int count
,
565 KASSERT(data
!= NULL
);
566 KASSERT((offset
+ count
) < 0x7fff);
568 if (pci_get_capability(pc
, tag
, PCI_CAP_VPD
, &ofs
, ®
) == 0)
571 for (i
= 0; i
< count
; offset
+= sizeof(*data
), i
++) {
573 reg
&= ~PCI_VPD_OPFLAG
;
574 reg
|= PCI_VPD_ADDRESS(offset
);
575 pci_conf_write(pc
, tag
, ofs
, reg
);
578 * PCI 2.2 does not specify how long we should poll
579 * for completion nor whether the operation can fail.
586 reg
= pci_conf_read(pc
, tag
, ofs
);
587 } while ((reg
& PCI_VPD_OPFLAG
) == 0);
588 data
[i
] = pci_conf_read(pc
, tag
, PCI_VPD_DATAREG(ofs
));
595 pci_vpd_write(pci_chipset_tag_t pc
, pcitag_t tag
, int offset
, int count
,
601 KASSERT(data
!= NULL
);
602 KASSERT((offset
+ count
) < 0x7fff);
604 if (pci_get_capability(pc
, tag
, PCI_CAP_VPD
, &ofs
, ®
) == 0)
607 for (i
= 0; i
< count
; offset
+= sizeof(*data
), i
++) {
608 pci_conf_write(pc
, tag
, PCI_VPD_DATAREG(ofs
), data
[i
]);
611 reg
|= PCI_VPD_OPFLAG
;
612 reg
|= PCI_VPD_ADDRESS(offset
);
613 pci_conf_write(pc
, tag
, ofs
, reg
);
616 * PCI 2.2 does not specify how long we should poll
617 * for completion nor whether the operation can fail.
624 reg
= pci_conf_read(pc
, tag
, ofs
);
625 } while (reg
& PCI_VPD_OPFLAG
);
632 pci_dma64_available(struct pci_attach_args
*pa
)
634 #ifdef _PCI_HAVE_DMA64
635 if (BUS_DMA_TAG_VALID(pa
->pa_dmat64
))
642 pci_conf_capture(pci_chipset_tag_t pc
, pcitag_t tag
,
643 struct pci_conf_state
*pcs
)
647 for (off
= 0; off
< 16; off
++)
648 pcs
->reg
[off
] = pci_conf_read(pc
, tag
, (off
* 4));
654 pci_conf_restore(pci_chipset_tag_t pc
, pcitag_t tag
,
655 struct pci_conf_state
*pcs
)
660 for (off
= 15; off
>= 0; off
--) {
661 val
= pci_conf_read(pc
, tag
, (off
* 4));
662 if (val
!= pcs
->reg
[off
])
663 pci_conf_write(pc
, tag
, (off
* 4), pcs
->reg
[off
]);
670 * Power Management Capability (Rev 2.2)
673 pci_get_powerstate_int(pci_chipset_tag_t pc
, pcitag_t tag
, pcireg_t
*state
,
678 value
= pci_conf_read(pc
, tag
, offset
+ PCI_PMCSR
);
679 now
= value
& PCI_PMCSR_STATE_MASK
;
681 case PCI_PMCSR_STATE_D0
:
682 case PCI_PMCSR_STATE_D1
:
683 case PCI_PMCSR_STATE_D2
:
684 case PCI_PMCSR_STATE_D3
:
693 pci_get_powerstate(pci_chipset_tag_t pc
, pcitag_t tag
, pcireg_t
*state
)
698 if (!pci_get_capability(pc
, tag
, PCI_CAP_PWRMGMT
, &offset
, &value
))
701 return pci_get_powerstate_int(pc
, tag
, state
, offset
);
705 pci_set_powerstate_int(pci_chipset_tag_t pc
, pcitag_t tag
, pcireg_t state
,
706 int offset
, pcireg_t cap_reg
)
708 pcireg_t value
, cap
, now
;
710 cap
= cap_reg
>> PCI_PMCR_SHIFT
;
711 value
= pci_conf_read(pc
, tag
, offset
+ PCI_PMCSR
);
712 now
= value
& PCI_PMCSR_STATE_MASK
;
713 value
&= ~PCI_PMCSR_STATE_MASK
;
718 case PCI_PMCSR_STATE_D0
:
720 case PCI_PMCSR_STATE_D1
:
721 if (now
== PCI_PMCSR_STATE_D2
|| now
== PCI_PMCSR_STATE_D3
) {
722 printf("invalid transition from %d to D1\n", (int)now
);
725 if (!(cap
& PCI_PMCR_D1SUPP
)) {
726 printf("D1 not supported\n");
730 case PCI_PMCSR_STATE_D2
:
731 if (now
== PCI_PMCSR_STATE_D3
) {
732 printf("invalid transition from %d to D2\n", (int)now
);
735 if (!(cap
& PCI_PMCR_D2SUPP
)) {
736 printf("D2 not supported\n");
740 case PCI_PMCSR_STATE_D3
:
746 pci_conf_write(pc
, tag
, offset
+ PCI_PMCSR
, value
);
747 /* delay according to pcipm1.2, ch. 5.6.1 */
748 if (state
== PCI_PMCSR_STATE_D3
|| now
== PCI_PMCSR_STATE_D3
)
750 else if (state
== PCI_PMCSR_STATE_D2
|| now
== PCI_PMCSR_STATE_D2
)
757 pci_set_powerstate(pci_chipset_tag_t pc
, pcitag_t tag
, pcireg_t state
)
762 if (!pci_get_capability(pc
, tag
, PCI_CAP_PWRMGMT
, &offset
, &value
)) {
763 printf("pci_set_powerstate not supported\n");
767 return pci_set_powerstate_int(pc
, tag
, state
, offset
, value
);
771 pci_activate(pci_chipset_tag_t pc
, pcitag_t tag
, device_t dev
,
772 int (*wakefun
)(pci_chipset_tag_t
, pcitag_t
, device_t
, pcireg_t
))
777 if ((error
= pci_get_powerstate(pc
, tag
, &pmode
)))
781 case PCI_PMCSR_STATE_D0
:
783 case PCI_PMCSR_STATE_D3
:
784 if (wakefun
== NULL
) {
786 * The card has lost all configuration data in
787 * this state, so punt.
789 aprint_error_dev(dev
,
790 "unable to wake up from power state D3\n");
796 error
= (*wakefun
)(pc
, tag
, dev
, pmode
);
800 aprint_normal_dev(dev
, "waking up from power state D%d\n",
802 if ((error
= pci_set_powerstate(pc
, tag
, PCI_PMCSR_STATE_D0
)))
809 pci_activate_null(pci_chipset_tag_t pc
, pcitag_t tag
,
810 device_t dev
, pcireg_t state
)
815 /* I have disabled this code for now. --dyoung
817 * Insofar as I understand what the PCI retry timeout is [1],
818 * I see no justification for any driver to disable when it
819 * attaches/resumes a device.
821 * A PCI bus bridge may tell a bus master to retry its transaction
822 * at a later time if the resources to complete the transaction
823 * are not immediately available. Taking a guess, PCI bus masters
824 * that implement a PCI retry timeout register count down from the
825 * retry timeout to 0 while it retries a delayed PCI transaction.
826 * When it reaches 0, it stops retrying. A PCI master is *never*
827 * supposed to stop retrying a delayed transaction, though.
829 * Incidentally, I initially suspected that writing 0 to the register
830 * would not disable *retries*, but would disable the timeout.
831 * That is, any device whose retry timeout was set to 0 would
832 * *never* timeout. However, I found out, by using PCI debug
833 * facilities on the AMD Elan SC520, that if I write 0 to the retry
834 * timeout register on an ath(4) MiniPCI card, the card really does
835 * not retry transactions.
837 * Some uses of this register have mentioned "interference" with
838 * a CPU's "C3 sleep state." It seems to me that if a bus master
839 * is properly put to sleep, it will neither initiate new transactions,
840 * nor retry delayed transactions, so disabling retries should not
843 * [1] The timeout does not appear to be documented in any PCI
844 * standard, and we have no documentation of it for the devices by
845 * Atheros, and others, that supposedly implement it.
848 pci_disable_retry(pci_chipset_tag_t pc
, pcitag_t tag
)
854 * Disable retry timeout to keep PCI Tx retries from
855 * interfering with ACPI C3 CPU state.
857 retry
= pci_conf_read(pc
, tag
, PCI_RETRY_TIMEOUT_REG
);
858 retry
&= ~PCI_RETRY_TIMEOUT_REG_MASK
;
859 pci_conf_write(pc
, tag
, PCI_RETRY_TIMEOUT_REG
, retry
);
863 struct pci_child_power
{
864 struct pci_conf_state p_pciconf
;
865 pci_chipset_tag_t p_pc
;
874 pci_child_suspend(device_t dv
, pmf_qual_t qual
)
876 struct pci_child_power
*priv
= device_pmf_bus_private(dv
);
879 pci_conf_capture(priv
->p_pc
, priv
->p_tag
, &priv
->p_pciconf
);
882 return true; /* ??? hopefully handled by ACPI */
883 if (PCI_CLASS(priv
->p_class
) == PCI_CLASS_DISPLAY
)
884 return true; /* XXX */
886 /* disable decoding and busmastering, see pcipm1.2 ch. 8.2.1 */
887 ocsr
= pci_conf_read(priv
->p_pc
, priv
->p_tag
, PCI_COMMAND_STATUS_REG
);
888 csr
= ocsr
& ~(PCI_COMMAND_IO_ENABLE
| PCI_COMMAND_MEM_ENABLE
889 | PCI_COMMAND_MASTER_ENABLE
);
890 pci_conf_write(priv
->p_pc
, priv
->p_tag
, PCI_COMMAND_STATUS_REG
, csr
);
891 if (pci_set_powerstate_int(priv
->p_pc
, priv
->p_tag
,
892 PCI_PMCSR_STATE_D3
, priv
->p_pm_offset
, priv
->p_pm_cap
)) {
893 pci_conf_write(priv
->p_pc
, priv
->p_tag
,
894 PCI_COMMAND_STATUS_REG
, ocsr
);
895 aprint_error_dev(dv
, "unsupported state, continuing.\n");
902 pci_child_resume(device_t dv
, pmf_qual_t qual
)
904 struct pci_child_power
*priv
= device_pmf_bus_private(dv
);
906 if (priv
->p_has_pm
&&
907 pci_set_powerstate_int(priv
->p_pc
, priv
->p_tag
,
908 PCI_PMCSR_STATE_D0
, priv
->p_pm_offset
, priv
->p_pm_cap
)) {
909 aprint_error_dev(dv
, "unsupported state, continuing.\n");
913 pci_conf_restore(priv
->p_pc
, priv
->p_tag
, &priv
->p_pciconf
);
919 pci_child_shutdown(device_t dv
, int how
)
921 struct pci_child_power
*priv
= device_pmf_bus_private(dv
);
924 /* disable busmastering */
925 csr
= pci_conf_read(priv
->p_pc
, priv
->p_tag
, PCI_COMMAND_STATUS_REG
);
926 csr
&= ~PCI_COMMAND_MASTER_ENABLE
;
927 pci_conf_write(priv
->p_pc
, priv
->p_tag
, PCI_COMMAND_STATUS_REG
, csr
);
932 pci_child_deregister(device_t dv
)
934 struct pci_child_power
*priv
= device_pmf_bus_private(dv
);
936 free(priv
, M_DEVBUF
);
940 pci_child_register(device_t child
)
942 device_t self
= device_parent(child
);
943 struct pci_softc
*sc
= device_private(self
);
944 struct pci_child_power
*priv
;
945 int device
, function
, off
;
948 priv
= malloc(sizeof(*priv
), M_DEVBUF
, M_WAITOK
);
950 device
= device_locator(child
, PCICF_DEV
);
951 function
= device_locator(child
, PCICF_FUNCTION
);
953 priv
->p_pc
= sc
->sc_pc
;
954 priv
->p_tag
= pci_make_tag(priv
->p_pc
, sc
->sc_bus
, device
,
956 priv
->p_class
= pci_conf_read(priv
->p_pc
, priv
->p_tag
, PCI_CLASS_REG
);
958 if (pci_get_capability(priv
->p_pc
, priv
->p_tag
,
959 PCI_CAP_PWRMGMT
, &off
, ®
)) {
960 priv
->p_has_pm
= true;
961 priv
->p_pm_offset
= off
;
962 priv
->p_pm_cap
= reg
;
964 priv
->p_has_pm
= false;
965 priv
->p_pm_offset
= -1;
968 device_pmf_bus_register(child
, priv
, pci_child_suspend
,
969 pci_child_resume
, pci_child_shutdown
, pci_child_deregister
);