10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51106Smrj * Common Development and Distribution License (the "License").
61106Smrj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12819SVincent.Wang@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/sunndi.h>
271865Sdilpreet #include <sys/sysmacros.h>
280Sstevel@tonic-gate #include <sys/pci.h>
29240Stimh #include <sys/pcie.h>
300Sstevel@tonic-gate #include <sys/pci_impl.h>
310Sstevel@tonic-gate #include <sys/epm.h>
320Sstevel@tonic-gate
33*12819SVincent.Wang@Sun.COM int pci_enable_wakeup = 1;
34*12819SVincent.Wang@Sun.COM
350Sstevel@tonic-gate int
pci_config_setup(dev_info_t * dip,ddi_acc_handle_t * handle)360Sstevel@tonic-gate pci_config_setup(dev_info_t *dip, ddi_acc_handle_t *handle)
370Sstevel@tonic-gate {
380Sstevel@tonic-gate caddr_t cfgaddr;
390Sstevel@tonic-gate ddi_device_acc_attr_t attr;
400Sstevel@tonic-gate
410Sstevel@tonic-gate attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
420Sstevel@tonic-gate attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
430Sstevel@tonic-gate attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
440Sstevel@tonic-gate
450Sstevel@tonic-gate /* Check for fault management capabilities */
461865Sdilpreet if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(dip))) {
471865Sdilpreet attr.devacc_attr_version = DDI_DEVICE_ATTR_V1;
480Sstevel@tonic-gate attr.devacc_attr_access = DDI_FLAGERR_ACC;
491865Sdilpreet }
500Sstevel@tonic-gate
510Sstevel@tonic-gate return (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, handle));
520Sstevel@tonic-gate }
530Sstevel@tonic-gate
540Sstevel@tonic-gate void
pci_config_teardown(ddi_acc_handle_t * handle)550Sstevel@tonic-gate pci_config_teardown(ddi_acc_handle_t *handle)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate ddi_regs_map_free(handle);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate uint8_t
pci_config_get8(ddi_acc_handle_t handle,off_t offset)610Sstevel@tonic-gate pci_config_get8(ddi_acc_handle_t handle, off_t offset)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate caddr_t cfgaddr;
640Sstevel@tonic-gate ddi_acc_hdl_t *hp;
650Sstevel@tonic-gate
660Sstevel@tonic-gate hp = impl_acc_hdl_get(handle);
670Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset;
680Sstevel@tonic-gate return (ddi_get8(handle, (uint8_t *)cfgaddr));
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate uint16_t
pci_config_get16(ddi_acc_handle_t handle,off_t offset)720Sstevel@tonic-gate pci_config_get16(ddi_acc_handle_t handle, off_t offset)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate caddr_t cfgaddr;
750Sstevel@tonic-gate ddi_acc_hdl_t *hp;
760Sstevel@tonic-gate
770Sstevel@tonic-gate hp = impl_acc_hdl_get(handle);
780Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset;
790Sstevel@tonic-gate return (ddi_get16(handle, (uint16_t *)cfgaddr));
800Sstevel@tonic-gate }
810Sstevel@tonic-gate
820Sstevel@tonic-gate uint32_t
pci_config_get32(ddi_acc_handle_t handle,off_t offset)830Sstevel@tonic-gate pci_config_get32(ddi_acc_handle_t handle, off_t offset)
840Sstevel@tonic-gate {
850Sstevel@tonic-gate caddr_t cfgaddr;
860Sstevel@tonic-gate ddi_acc_hdl_t *hp;
870Sstevel@tonic-gate
880Sstevel@tonic-gate hp = impl_acc_hdl_get(handle);
890Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset;
900Sstevel@tonic-gate return (ddi_get32(handle, (uint32_t *)cfgaddr));
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate uint64_t
pci_config_get64(ddi_acc_handle_t handle,off_t offset)940Sstevel@tonic-gate pci_config_get64(ddi_acc_handle_t handle, off_t offset)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate caddr_t cfgaddr;
970Sstevel@tonic-gate ddi_acc_hdl_t *hp;
980Sstevel@tonic-gate
990Sstevel@tonic-gate hp = impl_acc_hdl_get(handle);
1000Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset;
1010Sstevel@tonic-gate return (ddi_get64(handle, (uint64_t *)cfgaddr));
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate void
pci_config_put8(ddi_acc_handle_t handle,off_t offset,uint8_t value)1050Sstevel@tonic-gate pci_config_put8(ddi_acc_handle_t handle, off_t offset, uint8_t value)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate caddr_t cfgaddr;
1080Sstevel@tonic-gate ddi_acc_hdl_t *hp;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate hp = impl_acc_hdl_get(handle);
1110Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset;
1120Sstevel@tonic-gate ddi_put8(handle, (uint8_t *)cfgaddr, value);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate void
pci_config_put16(ddi_acc_handle_t handle,off_t offset,uint16_t value)1160Sstevel@tonic-gate pci_config_put16(ddi_acc_handle_t handle, off_t offset, uint16_t value)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate caddr_t cfgaddr;
1190Sstevel@tonic-gate ddi_acc_hdl_t *hp;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate hp = impl_acc_hdl_get(handle);
1220Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset;
1230Sstevel@tonic-gate ddi_put16(handle, (uint16_t *)cfgaddr, value);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate void
pci_config_put32(ddi_acc_handle_t handle,off_t offset,uint32_t value)1270Sstevel@tonic-gate pci_config_put32(ddi_acc_handle_t handle, off_t offset, uint32_t value)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate caddr_t cfgaddr;
1300Sstevel@tonic-gate ddi_acc_hdl_t *hp;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate hp = impl_acc_hdl_get(handle);
1330Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset;
1340Sstevel@tonic-gate ddi_put32(handle, (uint32_t *)cfgaddr, value);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate void
pci_config_put64(ddi_acc_handle_t handle,off_t offset,uint64_t value)1380Sstevel@tonic-gate pci_config_put64(ddi_acc_handle_t handle, off_t offset, uint64_t value)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate caddr_t cfgaddr;
1410Sstevel@tonic-gate ddi_acc_hdl_t *hp;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate hp = impl_acc_hdl_get(handle);
1440Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset;
1450Sstevel@tonic-gate ddi_put64(handle, (uint64_t *)cfgaddr, value);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1481106Smrj /*
1491106Smrj * We need to separate the old interfaces from the new ones and leave them
1501106Smrj * in here for a while. Previous versions of the OS defined the new interfaces
1511106Smrj * to the old interfaces. This way we can fix things up so that we can
1521106Smrj * eventually remove these interfaces.
1531106Smrj * e.g. A 3rd party module/driver using pci_config_get8 and built against S10
1541106Smrj * or earlier will actually have a reference to pci_config_getb in the binary.
1551106Smrj */
1561106Smrj #ifdef _ILP32
1571106Smrj uint8_t
pci_config_getb(ddi_acc_handle_t handle,off_t offset)1581106Smrj pci_config_getb(ddi_acc_handle_t handle, off_t offset)
1591106Smrj {
1601106Smrj caddr_t cfgaddr;
1611106Smrj ddi_acc_hdl_t *hp;
1621106Smrj
1631106Smrj hp = impl_acc_hdl_get(handle);
1641106Smrj cfgaddr = hp->ah_addr + offset;
1651106Smrj return (ddi_get8(handle, (uint8_t *)cfgaddr));
1661106Smrj }
1671106Smrj
1681106Smrj uint16_t
pci_config_getw(ddi_acc_handle_t handle,off_t offset)1691106Smrj pci_config_getw(ddi_acc_handle_t handle, off_t offset)
1701106Smrj {
1711106Smrj caddr_t cfgaddr;
1721106Smrj ddi_acc_hdl_t *hp;
1731106Smrj
1741106Smrj hp = impl_acc_hdl_get(handle);
1751106Smrj cfgaddr = hp->ah_addr + offset;
1761106Smrj return (ddi_get16(handle, (uint16_t *)cfgaddr));
1771106Smrj }
1781106Smrj
1791106Smrj uint32_t
pci_config_getl(ddi_acc_handle_t handle,off_t offset)1801106Smrj pci_config_getl(ddi_acc_handle_t handle, off_t offset)
1811106Smrj {
1821106Smrj caddr_t cfgaddr;
1831106Smrj ddi_acc_hdl_t *hp;
1841106Smrj
1851106Smrj hp = impl_acc_hdl_get(handle);
1861106Smrj cfgaddr = hp->ah_addr + offset;
1871106Smrj return (ddi_get32(handle, (uint32_t *)cfgaddr));
1881106Smrj }
1891106Smrj
1901106Smrj uint64_t
pci_config_getll(ddi_acc_handle_t handle,off_t offset)1911106Smrj pci_config_getll(ddi_acc_handle_t handle, off_t offset)
1921106Smrj {
1931106Smrj caddr_t cfgaddr;
1941106Smrj ddi_acc_hdl_t *hp;
1951106Smrj
1961106Smrj hp = impl_acc_hdl_get(handle);
1971106Smrj cfgaddr = hp->ah_addr + offset;
1981106Smrj return (ddi_get64(handle, (uint64_t *)cfgaddr));
1991106Smrj }
2001106Smrj
2011106Smrj void
pci_config_putb(ddi_acc_handle_t handle,off_t offset,uint8_t value)2021106Smrj pci_config_putb(ddi_acc_handle_t handle, off_t offset, uint8_t value)
2031106Smrj {
2041106Smrj caddr_t cfgaddr;
2051106Smrj ddi_acc_hdl_t *hp;
2061106Smrj
2071106Smrj hp = impl_acc_hdl_get(handle);
2081106Smrj cfgaddr = hp->ah_addr + offset;
2091106Smrj ddi_put8(handle, (uint8_t *)cfgaddr, value);
2101106Smrj }
2111106Smrj
2121106Smrj void
pci_config_putw(ddi_acc_handle_t handle,off_t offset,uint16_t value)2131106Smrj pci_config_putw(ddi_acc_handle_t handle, off_t offset, uint16_t value)
2141106Smrj {
2151106Smrj caddr_t cfgaddr;
2161106Smrj ddi_acc_hdl_t *hp;
2171106Smrj
2181106Smrj hp = impl_acc_hdl_get(handle);
2191106Smrj cfgaddr = hp->ah_addr + offset;
2201106Smrj ddi_put16(handle, (uint16_t *)cfgaddr, value);
2211106Smrj }
2221106Smrj
2231106Smrj void
pci_config_putl(ddi_acc_handle_t handle,off_t offset,uint32_t value)2241106Smrj pci_config_putl(ddi_acc_handle_t handle, off_t offset, uint32_t value)
2251106Smrj {
2261106Smrj caddr_t cfgaddr;
2271106Smrj ddi_acc_hdl_t *hp;
2281106Smrj
2291106Smrj hp = impl_acc_hdl_get(handle);
2301106Smrj cfgaddr = hp->ah_addr + offset;
2311106Smrj ddi_put32(handle, (uint32_t *)cfgaddr, value);
2321106Smrj }
2331106Smrj
2341106Smrj void
pci_config_putll(ddi_acc_handle_t handle,off_t offset,uint64_t value)2351106Smrj pci_config_putll(ddi_acc_handle_t handle, off_t offset, uint64_t value)
2361106Smrj {
2371106Smrj caddr_t cfgaddr;
2381106Smrj ddi_acc_hdl_t *hp;
2391106Smrj
2401106Smrj hp = impl_acc_hdl_get(handle);
2411106Smrj cfgaddr = hp->ah_addr + offset;
2421106Smrj ddi_put64(handle, (uint64_t *)cfgaddr, value);
2431106Smrj }
2441106Smrj #endif /* _ILP32 */
2451106Smrj
2460Sstevel@tonic-gate /*ARGSUSED*/
2470Sstevel@tonic-gate int
pci_report_pmcap(dev_info_t * dip,int cap,void * arg)2480Sstevel@tonic-gate pci_report_pmcap(dev_info_t *dip, int cap, void *arg)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate return (DDI_SUCCESS);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate * Note about saving and restoring config space.
2550Sstevel@tonic-gate * PCI devices have only upto 256 bytes of config space while PCI Express
2560Sstevel@tonic-gate * devices can have upto 4k config space. In case of PCI Express device,
2570Sstevel@tonic-gate * we save all 4k config space and restore it even if it doesn't make use
2580Sstevel@tonic-gate * of all 4k. But some devices don't respond to reads to non-existent
2590Sstevel@tonic-gate * registers within the config space. To avoid any panics, we use ddi_peek
2600Sstevel@tonic-gate * to do the reads. A bit mask is used to indicate which words of the
2610Sstevel@tonic-gate * config space are accessible. While restoring the config space, only those
2620Sstevel@tonic-gate * readable words are restored. We do all this in 32 bit size words.
2630Sstevel@tonic-gate */
2640Sstevel@tonic-gate #define INDEX_SHIFT 3
2650Sstevel@tonic-gate #define BITMASK 0x7
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate static uint32_t pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf,
2680Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp);
2690Sstevel@tonic-gate static void pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf,
2700Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t elements);
2710Sstevel@tonic-gate static uint32_t pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
2720Sstevel@tonic-gate uint32_t *regbuf, uint32_t nwords);
2730Sstevel@tonic-gate static uint32_t pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
2740Sstevel@tonic-gate uint32_t *regbuf, uint32_t notused);
2750Sstevel@tonic-gate static uint32_t pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
2760Sstevel@tonic-gate uint32_t *regbuf, uint32_t notused);
2770Sstevel@tonic-gate static uint32_t pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
2780Sstevel@tonic-gate uint32_t *regbuf, uint32_t notused);
2799970SJimmy.Vetayases@Sun.COM static uint32_t pci_ht_addrmap_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
2809970SJimmy.Vetayases@Sun.COM uint32_t *regbuf, uint32_t notused);
2819970SJimmy.Vetayases@Sun.COM static uint32_t pci_ht_funcext_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
2829970SJimmy.Vetayases@Sun.COM uint32_t *regbuf, uint32_t notused);
2830Sstevel@tonic-gate static void pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
2840Sstevel@tonic-gate uint32_t *regbuf, uint32_t nwords);
2850Sstevel@tonic-gate static uint32_t cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf,
2860Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace);
2870Sstevel@tonic-gate static void pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf,
2880Sstevel@tonic-gate uint16_t pmcap_offset);
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * Table below specifies the number of registers to be saved for each PCI
2920Sstevel@tonic-gate * capability. pci_generic_save saves the number of words specified in the
2930Sstevel@tonic-gate * table. Any special considerations will be taken care by the capability
2940Sstevel@tonic-gate * specific save function e.g. use pci_msi_save to save registers associated
2950Sstevel@tonic-gate * with MSI capability. PCI_UNKNOWN_SIZE indicates that number of registers
2960Sstevel@tonic-gate * to be saved is variable and will be determined by the specific save function.
2970Sstevel@tonic-gate * Currently we save/restore all the registers associated with the capability
2980Sstevel@tonic-gate * including read only registers. Regsiters are saved and restored in 32 bit
2990Sstevel@tonic-gate * size words.
3000Sstevel@tonic-gate */
3010Sstevel@tonic-gate static pci_cap_entry_t pci_cap_table[] = {
3029970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_PM, 0, 0, PCI_PMCAP_NDWORDS, pci_generic_save},
3039970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_AGP, 0, 0, PCI_AGP_NDWORDS, pci_generic_save},
3049970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_SLOT_ID, 0, 0, PCI_SLOTID_NDWORDS, pci_generic_save},
3059970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_MSI_X, 0, 0, PCI_MSIX_NDWORDS, pci_generic_save},
3069970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_MSI, 0, 0, PCI_CAP_SZUNKNOWN, pci_msi_save},
3079970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_PCIX, 0, 0, PCI_CAP_SZUNKNOWN, pci_pcix_save},
3089970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_PCI_E, 0, 0, PCI_CAP_SZUNKNOWN, pci_pcie_save},
3099970SJimmy.Vetayases@Sun.COM
3109970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_SLPRI_TYPE, PCI_HTCAP_TYPE_SLHOST_MASK,
3119970SJimmy.Vetayases@Sun.COM PCI_HTCAP_SLPRI_NDWORDS, pci_generic_save},
3129970SJimmy.Vetayases@Sun.COM
3139970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_HOSTSEC_TYPE, PCI_HTCAP_TYPE_SLHOST_MASK,
3149970SJimmy.Vetayases@Sun.COM PCI_HTCAP_HOSTSEC_NDWORDS, pci_generic_save},
3159970SJimmy.Vetayases@Sun.COM
3169970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_INTCONF_TYPE, PCI_HTCAP_TYPE_MASK,
3179970SJimmy.Vetayases@Sun.COM PCI_HTCAP_INTCONF_NDWORDS, pci_generic_save},
3189970SJimmy.Vetayases@Sun.COM
3199970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_REVID_TYPE, PCI_HTCAP_TYPE_MASK,
3209970SJimmy.Vetayases@Sun.COM PCI_HTCAP_REVID_NDWORDS, pci_generic_save},
3219970SJimmy.Vetayases@Sun.COM
3229970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_UNITID_CLUMP_TYPE, PCI_HTCAP_TYPE_MASK,
3239970SJimmy.Vetayases@Sun.COM PCI_HTCAP_UNITID_CLUMP_NDWORDS, pci_generic_save},
3249970SJimmy.Vetayases@Sun.COM
3259970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_ECFG_TYPE, PCI_HTCAP_TYPE_MASK,
3269970SJimmy.Vetayases@Sun.COM PCI_HTCAP_ECFG_NDWORDS, pci_generic_save},
3279970SJimmy.Vetayases@Sun.COM
3289970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_ADDRMAP_TYPE, PCI_HTCAP_TYPE_MASK,
3299970SJimmy.Vetayases@Sun.COM PCI_CAP_SZUNKNOWN, pci_ht_addrmap_save},
3309970SJimmy.Vetayases@Sun.COM
3319970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_MSIMAP_TYPE, PCI_HTCAP_TYPE_MASK,
3329970SJimmy.Vetayases@Sun.COM PCI_HTCAP_MSIMAP_NDWORDS, pci_generic_save},
3339970SJimmy.Vetayases@Sun.COM
3349970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_DIRROUTE_TYPE, PCI_HTCAP_TYPE_MASK,
3359970SJimmy.Vetayases@Sun.COM PCI_HTCAP_DIRROUTE_NDWORDS, pci_generic_save},
3369970SJimmy.Vetayases@Sun.COM
3379970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_VCSET_TYPE, PCI_HTCAP_TYPE_MASK,
3389970SJimmy.Vetayases@Sun.COM PCI_HTCAP_VCSET_NDWORDS, pci_generic_save},
3399970SJimmy.Vetayases@Sun.COM
3409970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_RETRYMODE_TYPE, PCI_HTCAP_TYPE_MASK,
3419970SJimmy.Vetayases@Sun.COM PCI_HTCAP_RETRYMODE_NDWORDS, pci_generic_save},
3429970SJimmy.Vetayases@Sun.COM
3439970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_GEN3_TYPE, PCI_HTCAP_TYPE_MASK,
3449970SJimmy.Vetayases@Sun.COM PCI_HTCAP_GEN3_NDWORDS, pci_generic_save},
3459970SJimmy.Vetayases@Sun.COM
3469970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_FUNCEXT_TYPE, PCI_HTCAP_TYPE_MASK,
3479970SJimmy.Vetayases@Sun.COM PCI_CAP_SZUNKNOWN, pci_ht_funcext_save},
3489970SJimmy.Vetayases@Sun.COM
3499970SJimmy.Vetayases@Sun.COM {PCI_CAP_ID_HT, PCI_HTCAP_PM_TYPE, PCI_HTCAP_TYPE_MASK,
3509970SJimmy.Vetayases@Sun.COM PCI_HTCAP_PM_NDWORDS, pci_generic_save},
3519970SJimmy.Vetayases@Sun.COM
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate * {PCI_CAP_ID_cPCI_CRC, 0, NULL},
3540Sstevel@tonic-gate * {PCI_CAP_ID_VPD, 0, NULL},
3550Sstevel@tonic-gate * {PCI_CAP_ID_cPCI_HS, 0, NULL},
3560Sstevel@tonic-gate * {PCI_CAP_ID_PCI_HOTPLUG, 0, NULL},
3570Sstevel@tonic-gate * {PCI_CAP_ID_AGP_8X, 0, NULL},
3580Sstevel@tonic-gate * {PCI_CAP_ID_SECURE_DEV, 0, NULL},
3590Sstevel@tonic-gate */
3600Sstevel@tonic-gate {PCI_CAP_NEXT_PTR_NULL, 0, NULL}
3610Sstevel@tonic-gate };
3620Sstevel@tonic-gate
3639970SJimmy.Vetayases@Sun.COM
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate * Save the configuration registers for cdip as a property
3660Sstevel@tonic-gate * so that it persists after detach/uninitchild.
3670Sstevel@tonic-gate */
3680Sstevel@tonic-gate int
pci_save_config_regs(dev_info_t * dip)3690Sstevel@tonic-gate pci_save_config_regs(dev_info_t *dip)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate ddi_acc_handle_t confhdl;
3720Sstevel@tonic-gate pci_config_header_state_t *chsp;
3730Sstevel@tonic-gate pci_cap_save_desc_t *pci_cap_descp;
3740Sstevel@tonic-gate int ret;
3750Sstevel@tonic-gate uint32_t i, ncaps, nwords;
3760Sstevel@tonic-gate uint32_t *regbuf, *p;
3770Sstevel@tonic-gate uint8_t *maskbuf;
3780Sstevel@tonic-gate size_t maskbufsz, regbufsz, capbufsz;
3797270Sgs150176 #ifdef __sparc
3800Sstevel@tonic-gate ddi_acc_hdl_t *hp;
3817270Sgs150176 #else
3827270Sgs150176 ddi_device_acc_attr_t attr;
3837270Sgs150176 caddr_t cfgaddr;
3847270Sgs150176 #endif
3850Sstevel@tonic-gate off_t offset = 0;
3860Sstevel@tonic-gate uint8_t cap_ptr, cap_id;
3870Sstevel@tonic-gate int pcie = 0;
3887168Syf149591 uint16_t status;
3897168Syf149591
3905295Srandyf PMD(PMD_SX, ("pci_save_config_regs %s:%d\n", ddi_driver_name(dip),
3915295Srandyf ddi_get_instance(dip)))
3920Sstevel@tonic-gate
3937270Sgs150176 #ifdef __sparc
3940Sstevel@tonic-gate if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) {
3950Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't get config handle",
3965295Srandyf ddi_driver_name(dip), ddi_get_instance(dip));
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate return (DDI_FAILURE);
3990Sstevel@tonic-gate }
4007270Sgs150176 #else
4017270Sgs150176 /* Set up cautious config access handle */
4027270Sgs150176 attr.devacc_attr_version = DDI_DEVICE_ATTR_V1;
4037270Sgs150176 attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
4047270Sgs150176 attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
4057270Sgs150176 attr.devacc_attr_access = DDI_CAUTIOUS_ACC;
4067270Sgs150176 if (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, &confhdl)
4077270Sgs150176 != DDI_SUCCESS) {
4087270Sgs150176 cmn_err(CE_WARN, "%s%d can't setup cautious config handle",
4097270Sgs150176 ddi_driver_name(dip), ddi_get_instance(dip));
4107270Sgs150176
4117270Sgs150176 return (DDI_FAILURE);
4127270Sgs150176 }
4137270Sgs150176 #endif
4147168Syf149591
4157168Syf149591 /*
4167168Syf149591 * Determine if it implements capabilities
4177168Syf149591 */
4187168Syf149591 status = pci_config_get16(confhdl, PCI_CONF_STAT);
4197168Syf149591 if (!(status & 0x10)) {
4207168Syf149591 goto no_cap;
4217168Syf149591 }
4220Sstevel@tonic-gate /*
4230Sstevel@tonic-gate * Determine if it is a pci express device. If it is, save entire
4240Sstevel@tonic-gate * 4k config space treating it as a array of 32 bit integers.
4250Sstevel@tonic-gate * If it is not, do it in a usual PCI way.
4260Sstevel@tonic-gate */
4270Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR);
4280Sstevel@tonic-gate /*
4290Sstevel@tonic-gate * Walk the capabilities searching for pci express capability
4300Sstevel@tonic-gate */
4310Sstevel@tonic-gate while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) {
4320Sstevel@tonic-gate cap_id = pci_config_get8(confhdl,
4330Sstevel@tonic-gate cap_ptr + PCI_CAP_ID);
4340Sstevel@tonic-gate if (cap_id == PCI_CAP_ID_PCI_E) {
4350Sstevel@tonic-gate pcie = 1;
4360Sstevel@tonic-gate break;
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl,
4390Sstevel@tonic-gate cap_ptr + PCI_CAP_NEXT_PTR);
4400Sstevel@tonic-gate }
4417168Syf149591 no_cap:
4420Sstevel@tonic-gate if (pcie) {
4430Sstevel@tonic-gate /* PCI express device. Can have data in all 4k space */
4440Sstevel@tonic-gate regbuf = (uint32_t *)kmem_zalloc((size_t)PCIE_CONF_HDR_SIZE,
4455295Srandyf KM_SLEEP);
4460Sstevel@tonic-gate p = regbuf;
4470Sstevel@tonic-gate /*
4480Sstevel@tonic-gate * Allocate space for mask.
4490Sstevel@tonic-gate * mask size is 128 bytes (4096 / 4 / 8 )
4500Sstevel@tonic-gate */
4510Sstevel@tonic-gate maskbufsz = (size_t)((PCIE_CONF_HDR_SIZE/ sizeof (uint32_t)) >>
4520Sstevel@tonic-gate INDEX_SHIFT);
4530Sstevel@tonic-gate maskbuf = (uint8_t *)kmem_zalloc(maskbufsz, KM_SLEEP);
4547270Sgs150176 #ifdef __sparc
4550Sstevel@tonic-gate hp = impl_acc_hdl_get(confhdl);
4567270Sgs150176 #endif
4570Sstevel@tonic-gate for (i = 0; i < (PCIE_CONF_HDR_SIZE / sizeof (uint32_t)); i++) {
4587270Sgs150176 #ifdef __sparc
4597270Sgs150176 ret = ddi_peek32(dip, (int32_t *)(hp->ah_addr + offset),
4607270Sgs150176 (int32_t *)p);
4616996Sgs150176 if (ret == DDI_SUCCESS) {
4627270Sgs150176 #else
4637270Sgs150176 /*
4647270Sgs150176 * ddi_peek doesn't work on x86, so we use cautious pci
4657270Sgs150176 * config access instead.
4667270Sgs150176 */
4677270Sgs150176 *p = pci_config_get32(confhdl, offset);
4687270Sgs150176 if (*p != -1) {
4697270Sgs150176 #endif
4700Sstevel@tonic-gate /* it is readable register. set the bit */
4710Sstevel@tonic-gate maskbuf[i >> INDEX_SHIFT] |=
4720Sstevel@tonic-gate (uint8_t)(1 << (i & BITMASK));
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate p++;
4750Sstevel@tonic-gate offset += sizeof (uint32_t);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip,
4790Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK, (uchar_t *)maskbuf,
4800Sstevel@tonic-gate maskbufsz)) != DDI_PROP_SUCCESS) {
4810Sstevel@tonic-gate cmn_err(CE_WARN, "couldn't create %s property while"
4820Sstevel@tonic-gate "saving config space for %s@%d\n",
4830Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK, ddi_driver_name(dip),
4840Sstevel@tonic-gate ddi_get_instance(dip));
4850Sstevel@tonic-gate } else if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE,
4860Sstevel@tonic-gate dip, SAVED_CONFIG_REGS, (uchar_t *)regbuf,
4870Sstevel@tonic-gate (size_t)PCIE_CONF_HDR_SIZE)) != DDI_PROP_SUCCESS) {
4880Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
4890Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK);
4900Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't update prop %s",
4910Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip),
4920Sstevel@tonic-gate SAVED_CONFIG_REGS);
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate kmem_free(maskbuf, (size_t)maskbufsz);
4960Sstevel@tonic-gate kmem_free(regbuf, (size_t)PCIE_CONF_HDR_SIZE);
4970Sstevel@tonic-gate } else {
4980Sstevel@tonic-gate regbuf = (uint32_t *)kmem_zalloc((size_t)PCI_CONF_HDR_SIZE,
4995295Srandyf KM_SLEEP);
5000Sstevel@tonic-gate chsp = (pci_config_header_state_t *)regbuf;
5010Sstevel@tonic-gate
5020Sstevel@tonic-gate chsp->chs_command = pci_config_get16(confhdl, PCI_CONF_COMM);
5030Sstevel@tonic-gate chsp->chs_header_type = pci_config_get8(confhdl,
5045295Srandyf PCI_CONF_HEADER);
5050Sstevel@tonic-gate if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) ==
5060Sstevel@tonic-gate PCI_HEADER_ONE)
5070Sstevel@tonic-gate chsp->chs_bridge_control =
5080Sstevel@tonic-gate pci_config_get16(confhdl, PCI_BCNF_BCNTRL);
5090Sstevel@tonic-gate chsp->chs_cache_line_size = pci_config_get8(confhdl,
5100Sstevel@tonic-gate PCI_CONF_CACHE_LINESZ);
5110Sstevel@tonic-gate chsp->chs_latency_timer = pci_config_get8(confhdl,
5120Sstevel@tonic-gate PCI_CONF_LATENCY_TIMER);
5130Sstevel@tonic-gate if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) ==
5140Sstevel@tonic-gate PCI_HEADER_ONE) {
5150Sstevel@tonic-gate chsp->chs_sec_latency_timer =
5160Sstevel@tonic-gate pci_config_get8(confhdl, PCI_BCNF_LATENCY_TIMER);
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate chsp->chs_base0 = pci_config_get32(confhdl, PCI_CONF_BASE0);
5200Sstevel@tonic-gate chsp->chs_base1 = pci_config_get32(confhdl, PCI_CONF_BASE1);
5210Sstevel@tonic-gate chsp->chs_base2 = pci_config_get32(confhdl, PCI_CONF_BASE2);
5220Sstevel@tonic-gate chsp->chs_base3 = pci_config_get32(confhdl, PCI_CONF_BASE3);
5230Sstevel@tonic-gate chsp->chs_base4 = pci_config_get32(confhdl, PCI_CONF_BASE4);
5240Sstevel@tonic-gate chsp->chs_base5 = pci_config_get32(confhdl, PCI_CONF_BASE5);
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate * Allocate maximum space required for capability descriptions.
5280Sstevel@tonic-gate * The maximum number of capabilties saved is the number of
5290Sstevel@tonic-gate * capabilities listed in the pci_cap_table.
5300Sstevel@tonic-gate */
5310Sstevel@tonic-gate ncaps = (sizeof (pci_cap_table) / sizeof (pci_cap_entry_t));
5320Sstevel@tonic-gate capbufsz = ncaps * sizeof (pci_cap_save_desc_t);
5330Sstevel@tonic-gate pci_cap_descp = (pci_cap_save_desc_t *)kmem_zalloc(
5340Sstevel@tonic-gate capbufsz, KM_SLEEP);
5350Sstevel@tonic-gate p = (uint32_t *)((caddr_t)regbuf +
5360Sstevel@tonic-gate sizeof (pci_config_header_state_t));
5370Sstevel@tonic-gate nwords = pci_save_caps(confhdl, p, pci_cap_descp, &ncaps);
5380Sstevel@tonic-gate regbufsz = sizeof (pci_config_header_state_t) +
5390Sstevel@tonic-gate nwords * sizeof (uint32_t);
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip,
5420Sstevel@tonic-gate SAVED_CONFIG_REGS, (uchar_t *)regbuf, regbufsz)) !=
5430Sstevel@tonic-gate DDI_PROP_SUCCESS) {
5440Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't update prop %s",
5450Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip),
5460Sstevel@tonic-gate SAVED_CONFIG_REGS);
5470Sstevel@tonic-gate } else if (ncaps) {
5480Sstevel@tonic-gate ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip,
5490Sstevel@tonic-gate SAVED_CONFIG_REGS_CAPINFO, (uchar_t *)pci_cap_descp,
5500Sstevel@tonic-gate ncaps * sizeof (pci_cap_save_desc_t));
5510Sstevel@tonic-gate if (ret != DDI_PROP_SUCCESS)
5520Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
5530Sstevel@tonic-gate SAVED_CONFIG_REGS);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate kmem_free(regbuf, (size_t)PCI_CONF_HDR_SIZE);
5560Sstevel@tonic-gate kmem_free(pci_cap_descp, capbufsz);
5570Sstevel@tonic-gate }
5580Sstevel@tonic-gate pci_config_teardown(&confhdl);
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate if (ret != DDI_PROP_SUCCESS)
5610Sstevel@tonic-gate return (DDI_FAILURE);
5620Sstevel@tonic-gate
5630Sstevel@tonic-gate return (DDI_SUCCESS);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate * Saves registers associated with PCI capabilities.
5680Sstevel@tonic-gate * Returns number of 32 bit words saved.
5690Sstevel@tonic-gate * Number of capabilities saved is returned in ncapsp.
5700Sstevel@tonic-gate */
5710Sstevel@tonic-gate static uint32_t
5720Sstevel@tonic-gate pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf,
5730Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp)
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate return (cap_walk_and_save(confhdl, regbuf, cap_descp, ncapsp, 0));
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate
5780Sstevel@tonic-gate static uint32_t
5790Sstevel@tonic-gate cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf,
5800Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate pci_cap_entry_t *pci_cap_entp;
5837168Syf149591 uint16_t cap_id, offset, status;
5840Sstevel@tonic-gate uint32_t words_saved = 0, nwords = 0;
5850Sstevel@tonic-gate uint16_t cap_ptr = PCI_CAP_NEXT_PTR_NULL;
5869970SJimmy.Vetayases@Sun.COM uint16_t cap_reg;
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate *ncapsp = 0;
5897168Syf149591
5907168Syf149591 /*
5917168Syf149591 * Determine if it implements capabilities
5927168Syf149591 */
5937168Syf149591 status = pci_config_get16(confhdl, PCI_CONF_STAT);
5947168Syf149591 if (!(status & 0x10)) {
5957168Syf149591 return (words_saved);
5967168Syf149591 }
5977168Syf149591
5980Sstevel@tonic-gate if (!xspace)
5990Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR);
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate * Walk the capabilities
6020Sstevel@tonic-gate */
6030Sstevel@tonic-gate while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) {
6040Sstevel@tonic-gate cap_id = CAP_ID(confhdl, cap_ptr, xspace);
6059970SJimmy.Vetayases@Sun.COM
6060Sstevel@tonic-gate /* Search for this cap id in our table */
6079970SJimmy.Vetayases@Sun.COM if (!xspace) {
6080Sstevel@tonic-gate pci_cap_entp = pci_cap_table;
6099970SJimmy.Vetayases@Sun.COM cap_reg = pci_config_get16(confhdl,
6109970SJimmy.Vetayases@Sun.COM cap_ptr + PCI_CAP_ID_REGS_OFF);
6119970SJimmy.Vetayases@Sun.COM }
6129970SJimmy.Vetayases@Sun.COM
6139970SJimmy.Vetayases@Sun.COM while (pci_cap_entp->cap_id != PCI_CAP_NEXT_PTR_NULL) {
6149970SJimmy.Vetayases@Sun.COM if (pci_cap_entp->cap_id == cap_id &&
6159970SJimmy.Vetayases@Sun.COM (cap_reg & pci_cap_entp->cap_mask) ==
6169970SJimmy.Vetayases@Sun.COM pci_cap_entp->cap_reg)
6179970SJimmy.Vetayases@Sun.COM break;
6189970SJimmy.Vetayases@Sun.COM
6190Sstevel@tonic-gate pci_cap_entp++;
6209970SJimmy.Vetayases@Sun.COM }
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate offset = cap_ptr;
6230Sstevel@tonic-gate cap_ptr = NEXT_CAP(confhdl, cap_ptr, xspace);
6240Sstevel@tonic-gate /*
6250Sstevel@tonic-gate * If this cap id is not found in the table, there is nothing
6260Sstevel@tonic-gate * to save.
6270Sstevel@tonic-gate */
6280Sstevel@tonic-gate if (pci_cap_entp->cap_id == PCI_CAP_NEXT_PTR_NULL)
6290Sstevel@tonic-gate continue;
6300Sstevel@tonic-gate if (pci_cap_entp->cap_save_func) {
6310Sstevel@tonic-gate if ((nwords = pci_cap_entp->cap_save_func(confhdl,
6320Sstevel@tonic-gate offset, regbuf, pci_cap_entp->cap_ndwords))) {
6330Sstevel@tonic-gate cap_descp->cap_nregs = nwords;
6340Sstevel@tonic-gate cap_descp->cap_offset = offset;
6350Sstevel@tonic-gate cap_descp->cap_id = cap_id;
6360Sstevel@tonic-gate regbuf += nwords;
6370Sstevel@tonic-gate cap_descp++;
6380Sstevel@tonic-gate words_saved += nwords;
6390Sstevel@tonic-gate (*ncapsp)++;
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate return (words_saved);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate static void
6480Sstevel@tonic-gate pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
6490Sstevel@tonic-gate uint32_t *regbuf, uint32_t nwords)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate int i;
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate for (i = 0; i < nwords; i++) {
6540Sstevel@tonic-gate *regbuf = pci_config_get32(confhdl, cap_ptr);
6550Sstevel@tonic-gate regbuf++;
6560Sstevel@tonic-gate cap_ptr += 4;
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate static uint32_t
6610Sstevel@tonic-gate pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf,
6620Sstevel@tonic-gate uint32_t nwords)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords);
6650Sstevel@tonic-gate return (nwords);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate
6680Sstevel@tonic-gate /*ARGSUSED*/
6690Sstevel@tonic-gate static uint32_t
6700Sstevel@tonic-gate pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf,
6710Sstevel@tonic-gate uint32_t notused)
6720Sstevel@tonic-gate {
6730Sstevel@tonic-gate uint32_t nwords = PCI_MSI_MIN_WORDS;
6740Sstevel@tonic-gate uint16_t msi_ctrl;
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate /* Figure out how many registers to be saved */
6770Sstevel@tonic-gate msi_ctrl = pci_config_get16(confhdl, cap_ptr + PCI_MSI_CTRL);
6780Sstevel@tonic-gate /* If 64 bit address capable add one word */
6790Sstevel@tonic-gate if (msi_ctrl & PCI_MSI_64BIT_MASK)
6800Sstevel@tonic-gate nwords++;
6810Sstevel@tonic-gate /* If per vector masking capable, add two more words */
6820Sstevel@tonic-gate if (msi_ctrl & PCI_MSI_PVM_MASK)
6830Sstevel@tonic-gate nwords += 2;
6840Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords);
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate return (nwords);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate /*ARGSUSED*/
6900Sstevel@tonic-gate static uint32_t
6910Sstevel@tonic-gate pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf,
6920Sstevel@tonic-gate uint32_t notused)
6930Sstevel@tonic-gate {
6940Sstevel@tonic-gate uint32_t nwords = PCI_PCIX_MIN_WORDS;
6950Sstevel@tonic-gate uint16_t pcix_command;
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate /* Figure out how many registers to be saved */
6980Sstevel@tonic-gate pcix_command = pci_config_get16(confhdl, cap_ptr + PCI_PCIX_COMMAND);
6990Sstevel@tonic-gate /* If it is version 1 or version 2, add 4 words */
7000Sstevel@tonic-gate if (((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_1) ||
7010Sstevel@tonic-gate ((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_2))
7020Sstevel@tonic-gate nwords += 4;
7030Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords);
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate return (nwords);
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate /*ARGSUSED*/
7090Sstevel@tonic-gate static uint32_t
7100Sstevel@tonic-gate pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf,
7110Sstevel@tonic-gate uint32_t notused)
7120Sstevel@tonic-gate {
7130Sstevel@tonic-gate return (0);
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate
7169970SJimmy.Vetayases@Sun.COM /*ARGSUSED*/
7179970SJimmy.Vetayases@Sun.COM static uint32_t
7189970SJimmy.Vetayases@Sun.COM pci_ht_addrmap_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
7199970SJimmy.Vetayases@Sun.COM uint32_t *regbuf, uint32_t notused)
7209970SJimmy.Vetayases@Sun.COM {
7219970SJimmy.Vetayases@Sun.COM uint32_t nwords = 0;
7229970SJimmy.Vetayases@Sun.COM uint16_t reg;
7239970SJimmy.Vetayases@Sun.COM
7249970SJimmy.Vetayases@Sun.COM reg = pci_config_get16(confhdl, cap_ptr + PCI_CAP_ID_REGS_OFF);
7259970SJimmy.Vetayases@Sun.COM
7269970SJimmy.Vetayases@Sun.COM switch ((reg & PCI_HTCAP_ADDRMAP_MAPTYPE_MASK) >>
7279970SJimmy.Vetayases@Sun.COM PCI_HTCAP_ADDRMAP_MAPTYPE_SHIFT) {
7289970SJimmy.Vetayases@Sun.COM case PCI_HTCAP_ADDRMAP_40BIT_ID:
7299970SJimmy.Vetayases@Sun.COM /* HT3.1 spec, ch 7.7, 40-bit dma */
7309970SJimmy.Vetayases@Sun.COM nwords = 3 + ((reg & PCI_HTCAP_ADDRMAP_NUMMAP_MASK) * 2);
7319970SJimmy.Vetayases@Sun.COM break;
7329970SJimmy.Vetayases@Sun.COM case PCI_HTCAP_ADDRMAP_64BIT_ID:
7339970SJimmy.Vetayases@Sun.COM /* HT3.1 spec, ch 7.8, 64-bit dma */
7349970SJimmy.Vetayases@Sun.COM nwords = 4;
7359970SJimmy.Vetayases@Sun.COM break;
7369970SJimmy.Vetayases@Sun.COM default:
7379970SJimmy.Vetayases@Sun.COM nwords = 0;
7389970SJimmy.Vetayases@Sun.COM }
7399970SJimmy.Vetayases@Sun.COM
7409970SJimmy.Vetayases@Sun.COM pci_fill_buf(confhdl, cap_ptr, regbuf, nwords);
7419970SJimmy.Vetayases@Sun.COM return (nwords);
7429970SJimmy.Vetayases@Sun.COM }
7439970SJimmy.Vetayases@Sun.COM
7449970SJimmy.Vetayases@Sun.COM /*ARGSUSED*/
7459970SJimmy.Vetayases@Sun.COM static uint32_t
7469970SJimmy.Vetayases@Sun.COM pci_ht_funcext_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr,
7479970SJimmy.Vetayases@Sun.COM uint32_t *regbuf, uint32_t notused)
7489970SJimmy.Vetayases@Sun.COM {
7499970SJimmy.Vetayases@Sun.COM uint32_t nwords;
7509970SJimmy.Vetayases@Sun.COM uint16_t reg;
7519970SJimmy.Vetayases@Sun.COM
7529970SJimmy.Vetayases@Sun.COM reg = pci_config_get16(confhdl, cap_ptr + PCI_CAP_ID_REGS_OFF);
7539970SJimmy.Vetayases@Sun.COM
7549970SJimmy.Vetayases@Sun.COM /* HT3.1 spec, ch 7.17 */
7559970SJimmy.Vetayases@Sun.COM nwords = 1 + (reg & PCI_HTCAP_FUNCEXT_LEN_MASK);
7569970SJimmy.Vetayases@Sun.COM
7579970SJimmy.Vetayases@Sun.COM pci_fill_buf(confhdl, cap_ptr, regbuf, nwords);
7589970SJimmy.Vetayases@Sun.COM return (nwords);
7599970SJimmy.Vetayases@Sun.COM }
7609970SJimmy.Vetayases@Sun.COM
7610Sstevel@tonic-gate static void
7620Sstevel@tonic-gate pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf,
7630Sstevel@tonic-gate uint16_t pmcap_offset)
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate uint16_t pmcsr;
7660Sstevel@tonic-gate uint16_t pmcsr_offset = pmcap_offset + PCI_PMCSR;
7670Sstevel@tonic-gate uint32_t *saved_pmcsrp = (uint32_t *)((caddr_t)regbuf + PCI_PMCSR);
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate * Copy the power state bits from the PMCSR to our saved copy.
7710Sstevel@tonic-gate * This is to make sure that we don't change the D state when
7720Sstevel@tonic-gate * we restore config space of the device.
7730Sstevel@tonic-gate */
7740Sstevel@tonic-gate pmcsr = pci_config_get16(confhdl, pmcsr_offset);
7750Sstevel@tonic-gate (*saved_pmcsrp) &= ~PCI_PMCSR_STATE_MASK;
7760Sstevel@tonic-gate (*saved_pmcsrp) |= (pmcsr & PCI_PMCSR_STATE_MASK);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate static void
7800Sstevel@tonic-gate pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf,
7810Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t elements)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate int i, j;
7840Sstevel@tonic-gate uint16_t offset;
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate for (i = 0; i < (elements / sizeof (pci_cap_save_desc_t)); i++) {
7870Sstevel@tonic-gate offset = cap_descp->cap_offset;
7880Sstevel@tonic-gate if (cap_descp->cap_id == PCI_CAP_ID_PM)
7890Sstevel@tonic-gate pci_pmcap_check(confhdl, regbuf, offset);
7900Sstevel@tonic-gate for (j = 0; j < cap_descp->cap_nregs; j++) {
7910Sstevel@tonic-gate pci_config_put32(confhdl, offset, *regbuf);
7920Sstevel@tonic-gate regbuf++;
7930Sstevel@tonic-gate offset += 4;
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate cap_descp++;
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate /*
8000Sstevel@tonic-gate * Restore config_regs from a single devinfo node.
8010Sstevel@tonic-gate */
8020Sstevel@tonic-gate int
8030Sstevel@tonic-gate pci_restore_config_regs(dev_info_t *dip)
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate ddi_acc_handle_t confhdl;
8060Sstevel@tonic-gate pci_config_header_state_t *chs_p;
8070Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp;
8080Sstevel@tonic-gate uint32_t elements, i;
8090Sstevel@tonic-gate uint8_t *maskbuf;
8100Sstevel@tonic-gate uint32_t *regbuf, *p;
8110Sstevel@tonic-gate off_t offset = 0;
8120Sstevel@tonic-gate
8130Sstevel@tonic-gate if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) {
8140Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't get config handle",
8150Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip));
8160Sstevel@tonic-gate return (DDI_FAILURE);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
8200Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS_MASK,
8210Sstevel@tonic-gate (uchar_t **)&maskbuf, &elements) == DDI_PROP_SUCCESS) {
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
8240Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS,
8250Sstevel@tonic-gate (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) {
8260Sstevel@tonic-gate goto restoreconfig_err;
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate ASSERT(elements == PCIE_CONF_HDR_SIZE);
8290Sstevel@tonic-gate /* pcie device and has 4k config space saved */
8300Sstevel@tonic-gate p = regbuf;
8310Sstevel@tonic-gate for (i = 0; i < PCIE_CONF_HDR_SIZE / sizeof (uint32_t); i++) {
8320Sstevel@tonic-gate /* If the word is readable then restore it */
8330Sstevel@tonic-gate if (maskbuf[i >> INDEX_SHIFT] &
8340Sstevel@tonic-gate (uint8_t)(1 << (i & BITMASK)))
8350Sstevel@tonic-gate pci_config_put32(confhdl, offset, *p);
8360Sstevel@tonic-gate p++;
8370Sstevel@tonic-gate offset += sizeof (uint32_t);
8380Sstevel@tonic-gate }
8390Sstevel@tonic-gate ddi_prop_free(regbuf);
8400Sstevel@tonic-gate ddi_prop_free(maskbuf);
8410Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip,
8420Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK) != DDI_PROP_SUCCESS) {
8430Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s",
8440Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip),
8450Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate } else {
8480Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
8490Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS,
8500Sstevel@tonic-gate (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) {
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate pci_config_teardown(&confhdl);
8536996Sgs150176 return (DDI_SUCCESS);
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate
8560Sstevel@tonic-gate chs_p = (pci_config_header_state_t *)regbuf;
8570Sstevel@tonic-gate pci_config_put16(confhdl, PCI_CONF_COMM,
8580Sstevel@tonic-gate chs_p->chs_command);
8590Sstevel@tonic-gate if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) ==
8600Sstevel@tonic-gate PCI_HEADER_ONE) {
8610Sstevel@tonic-gate pci_config_put16(confhdl, PCI_BCNF_BCNTRL,
8620Sstevel@tonic-gate chs_p->chs_bridge_control);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate pci_config_put8(confhdl, PCI_CONF_CACHE_LINESZ,
8650Sstevel@tonic-gate chs_p->chs_cache_line_size);
8660Sstevel@tonic-gate pci_config_put8(confhdl, PCI_CONF_LATENCY_TIMER,
8670Sstevel@tonic-gate chs_p->chs_latency_timer);
8680Sstevel@tonic-gate if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) ==
8690Sstevel@tonic-gate PCI_HEADER_ONE)
8700Sstevel@tonic-gate pci_config_put8(confhdl, PCI_BCNF_LATENCY_TIMER,
8710Sstevel@tonic-gate chs_p->chs_sec_latency_timer);
8720Sstevel@tonic-gate
8730Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE0, chs_p->chs_base0);
8740Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE1, chs_p->chs_base1);
8750Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE2, chs_p->chs_base2);
8760Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE3, chs_p->chs_base3);
8770Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE4, chs_p->chs_base4);
8780Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE5, chs_p->chs_base5);
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
8810Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
8820Sstevel@tonic-gate SAVED_CONFIG_REGS_CAPINFO,
8830Sstevel@tonic-gate (uchar_t **)&cap_descp, &elements) == DDI_PROP_SUCCESS) {
8840Sstevel@tonic-gate /*
8850Sstevel@tonic-gate * PCI capability related regsiters are saved.
8860Sstevel@tonic-gate * Restore them based on the description.
8870Sstevel@tonic-gate */
8880Sstevel@tonic-gate p = (uint32_t *)((caddr_t)regbuf +
8890Sstevel@tonic-gate sizeof (pci_config_header_state_t));
8900Sstevel@tonic-gate pci_restore_caps(confhdl, p, cap_descp, elements);
8910Sstevel@tonic-gate ddi_prop_free(cap_descp);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate ddi_prop_free(regbuf);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate * Make sure registers are flushed
8990Sstevel@tonic-gate */
9000Sstevel@tonic-gate (void) pci_config_get32(confhdl, PCI_CONF_BASE5);
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS) !=
9040Sstevel@tonic-gate DDI_PROP_SUCCESS) {
9050Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s",
9060Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip),
9070Sstevel@tonic-gate SAVED_CONFIG_REGS);
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate
9100Sstevel@tonic-gate pci_config_teardown(&confhdl);
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate return (DDI_SUCCESS);
9130Sstevel@tonic-gate
9140Sstevel@tonic-gate restoreconfig_err:
9150Sstevel@tonic-gate ddi_prop_free(maskbuf);
9160Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS_MASK) !=
9170Sstevel@tonic-gate DDI_PROP_SUCCESS) {
9180Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s",
9190Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip),
9200Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK);
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate pci_config_teardown(&confhdl);
9230Sstevel@tonic-gate return (DDI_FAILURE);
9240Sstevel@tonic-gate }
9255295Srandyf
9265295Srandyf /*ARGSUSED*/
9275295Srandyf static int
9285295Srandyf pci_lookup_pmcap(dev_info_t *dip, ddi_acc_handle_t conf_hdl,
9295295Srandyf uint16_t *pmcap_offsetp)
9305295Srandyf {
9315295Srandyf uint8_t cap_ptr;
9325295Srandyf uint8_t cap_id;
9335295Srandyf uint8_t header_type;
9345295Srandyf uint16_t status;
9355295Srandyf
9365295Srandyf header_type = pci_config_get8(conf_hdl, PCI_CONF_HEADER);
9375295Srandyf header_type &= PCI_HEADER_TYPE_M;
9385295Srandyf
9395295Srandyf /* we don't deal with bridges, etc here */
9405295Srandyf if (header_type != PCI_HEADER_ZERO) {
9415295Srandyf return (DDI_FAILURE);
9425295Srandyf }
9435295Srandyf
9445295Srandyf status = pci_config_get16(conf_hdl, PCI_CONF_STAT);
9455295Srandyf if ((status & PCI_STAT_CAP) == 0) {
9465295Srandyf return (DDI_FAILURE);
9475295Srandyf }
9485295Srandyf
9495295Srandyf cap_ptr = pci_config_get8(conf_hdl, PCI_CONF_CAP_PTR);
9505295Srandyf
9515295Srandyf /*
9525295Srandyf * Walk the capabilities searching for a PM entry.
9535295Srandyf */
9545295Srandyf while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) {
9555295Srandyf cap_id = pci_config_get8(conf_hdl, cap_ptr + PCI_CAP_ID);
9565295Srandyf if (cap_id == PCI_CAP_ID_PM) {
9575295Srandyf break;
9585295Srandyf }
9595295Srandyf cap_ptr = pci_config_get8(conf_hdl,
9605295Srandyf cap_ptr + PCI_CAP_NEXT_PTR);
9615295Srandyf }
9625295Srandyf
9635295Srandyf if (cap_ptr == PCI_CAP_NEXT_PTR_NULL) {
9645295Srandyf return (DDI_FAILURE);
9655295Srandyf }
9665295Srandyf *pmcap_offsetp = cap_ptr;
9675295Srandyf return (DDI_SUCCESS);
9685295Srandyf }
9695295Srandyf
9705295Srandyf /*
9715295Srandyf * Do common pci-specific suspend actions:
9725295Srandyf * - enable wakeup if appropriate for the device
9735295Srandyf * - put device in lowest D-state that supports wakeup, or D3 if none
9745295Srandyf * - turn off bus mastering in control register
9755295Srandyf * For lack of per-dip storage (parent private date is pretty busy)
9765295Srandyf * we use properties to store the necessary context
9775295Srandyf * To avoid grotting through pci config space on every suspend,
9785295Srandyf * we leave the prop in existence after resume, cause we know that
9795295Srandyf * the detach framework code will dispose of it for us.
9805295Srandyf */
9815295Srandyf
9825295Srandyf typedef struct pci_pm_context {
9835295Srandyf int ppc_flags;
9845295Srandyf uint16_t ppc_cap_offset; /* offset in config space to pm cap */
9855295Srandyf uint16_t ppc_pmcsr; /* need this too */
9865295Srandyf uint16_t ppc_suspend_level;
9875295Srandyf } pci_pm_context_t;
9885295Srandyf
9895295Srandyf #define SAVED_PM_CONTEXT "pci-pm-context"
9905295Srandyf
9915295Srandyf /* values for ppc_flags */
9925295Srandyf #define PPCF_NOPMCAP 1
9935295Srandyf
9945295Srandyf /*
9955295Srandyf * Handle pci-specific suspend processing
9965295Srandyf * PM CSR and PCI CMD are saved by pci_save_config_regs().
9975295Srandyf * If device can wake up system via PME, enable it to do so
9985295Srandyf * Set device power level to lowest that can generate PME, or D3 if none can
9995295Srandyf * Turn off bus master enable in pci command register
10005295Srandyf */
10015295Srandyf #if defined(__x86)
10025295Srandyf extern int acpi_ddi_setwake(dev_info_t *dip, int level);
10035295Srandyf #endif
10045295Srandyf
10055295Srandyf int
10065295Srandyf pci_post_suspend(dev_info_t *dip)
10075295Srandyf {
10085295Srandyf pci_pm_context_t *p;
10095295Srandyf uint16_t pmcap, pmcsr, pcicmd;
10105295Srandyf uint_t length;
10115295Srandyf int ret;
10125295Srandyf int fromprop = 1; /* source of memory *p */
10135295Srandyf ddi_acc_handle_t hdl;
10145295Srandyf
10155295Srandyf PMD(PMD_SX, ("pci_post_suspend %s:%d\n",
10165295Srandyf ddi_driver_name(dip), ddi_get_instance(dip)))
10175295Srandyf
10185295Srandyf if (pci_save_config_regs(dip) != DDI_SUCCESS) {
10195295Srandyf return (DDI_FAILURE);
10205295Srandyf }
10215295Srandyf
10225295Srandyf if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) {
10235295Srandyf return (DDI_FAILURE);
10245295Srandyf }
10255295Srandyf
10265295Srandyf if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
10275295Srandyf DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
10285295Srandyf SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) {
10295295Srandyf p = (pci_pm_context_t *)kmem_zalloc(sizeof (*p), KM_SLEEP);
10305295Srandyf fromprop = 0;
10315295Srandyf if (pci_lookup_pmcap(dip, hdl,
10325295Srandyf &p->ppc_cap_offset) != DDI_SUCCESS) {
10335295Srandyf p->ppc_flags |= PPCF_NOPMCAP;
10345295Srandyf ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip,
10355295Srandyf SAVED_PM_CONTEXT, (uchar_t *)p,
10365295Srandyf sizeof (pci_pm_context_t));
10375295Srandyf if (ret != DDI_PROP_SUCCESS) {
10385295Srandyf (void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
10395295Srandyf SAVED_PM_CONTEXT);
10405295Srandyf ret = DDI_FAILURE;
10415295Srandyf } else {
10425295Srandyf ret = DDI_SUCCESS;
10435295Srandyf }
1044*12819SVincent.Wang@Sun.COM goto done;
10455295Srandyf }
10465295Srandyf /*
10475295Srandyf * Upon suspend, set the power level to the lowest that can
10485295Srandyf * wake the system. If none can, then set to lowest.
10495295Srandyf * XXX later we will need to check policy to see if this
10505295Srandyf * XXX device has had wakeup disabled
10515295Srandyf */
10525295Srandyf pmcap = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCAP);
1053*12819SVincent.Wang@Sun.COM if ((pmcap & (PCI_PMCAP_D3COLD_PME | PCI_PMCAP_D3HOT_PME)) != 0)
10545295Srandyf p->ppc_suspend_level =
10555295Srandyf (PCI_PMCSR_PME_EN | PCI_PMCSR_D3HOT);
1056*12819SVincent.Wang@Sun.COM else if ((pmcap & PCI_PMCAP_D2_PME) !=
10575295Srandyf 0)
10585295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D2;
10595295Srandyf else if ((pmcap & PCI_PMCAP_D1_PME) != 0)
10605295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D1;
10615295Srandyf else if ((pmcap & PCI_PMCAP_D0_PME) != 0)
10625295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D0;
10635295Srandyf else
10645295Srandyf p->ppc_suspend_level = PCI_PMCSR_D3HOT;
10655295Srandyf
10665295Srandyf /*
10675295Srandyf * we defer updating the property to catch the saved
10685295Srandyf * register values as well
10695295Srandyf */
10705295Srandyf }
10715295Srandyf /* If we set this in kmem_zalloc'd memory, we already returned above */
10725295Srandyf if ((p->ppc_flags & PPCF_NOPMCAP) != 0) {
1073*12819SVincent.Wang@Sun.COM goto done;
10745295Srandyf }
10755295Srandyf
10765295Srandyf pmcsr = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCSR);
10775295Srandyf p->ppc_pmcsr = pmcsr;
10785295Srandyf pmcsr &= (PCI_PMCSR_STATE_MASK);
10795295Srandyf pmcsr |= (PCI_PMCSR_PME_STAT | p->ppc_suspend_level);
10805295Srandyf
10815295Srandyf /*
10825295Srandyf * Push out saved register values
10835295Srandyf */
10845295Srandyf ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT,
10855295Srandyf (uchar_t *)p, sizeof (pci_pm_context_t));
10865295Srandyf if (ret == DDI_PROP_SUCCESS) {
1087*12819SVincent.Wang@Sun.COM goto done;
10885295Srandyf }
10895295Srandyf /* Failed; put things back the way we found them */
10905295Srandyf (void) pci_restore_config_regs(dip);
10915295Srandyf if (fromprop)
10925295Srandyf ddi_prop_free(p);
10935295Srandyf else
10945295Srandyf kmem_free(p, sizeof (*p));
10955295Srandyf (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT);
10965295Srandyf pci_config_teardown(&hdl);
10975295Srandyf return (DDI_FAILURE);
1098*12819SVincent.Wang@Sun.COM
1099*12819SVincent.Wang@Sun.COM done:
1100*12819SVincent.Wang@Sun.COM
1101*12819SVincent.Wang@Sun.COM /*
1102*12819SVincent.Wang@Sun.COM * According to 8.2.2 of "PCI Bus Power Management Interface
1103*12819SVincent.Wang@Sun.COM * Specification Revision 1.2":
1104*12819SVincent.Wang@Sun.COM * "When placing a function into D3, the operating system software is
1105*12819SVincent.Wang@Sun.COM * required to disable I/O and memory space as well as bus mastering via
1106*12819SVincent.Wang@Sun.COM * the PCI Command register."
1107*12819SVincent.Wang@Sun.COM */
1108*12819SVincent.Wang@Sun.COM
1109*12819SVincent.Wang@Sun.COM pcicmd = pci_config_get16(hdl, PCI_CONF_COMM);
1110*12819SVincent.Wang@Sun.COM pcicmd &= ~(PCI_COMM_ME|PCI_COMM_MAE|PCI_COMM_IO);
1111*12819SVincent.Wang@Sun.COM pci_config_put16(hdl, PCI_CONF_COMM, pcicmd);
1112*12819SVincent.Wang@Sun.COM
1113*12819SVincent.Wang@Sun.COM
1114*12819SVincent.Wang@Sun.COM #if defined(__x86)
1115*12819SVincent.Wang@Sun.COM if (pci_enable_wakeup) {
1116*12819SVincent.Wang@Sun.COM
1117*12819SVincent.Wang@Sun.COM ret = acpi_ddi_setwake(dip, 3);
1118*12819SVincent.Wang@Sun.COM
1119*12819SVincent.Wang@Sun.COM if (ret) {
1120*12819SVincent.Wang@Sun.COM PMD(PMD_SX, ("pci_post_suspend, setwake %s@%s rets "
1121*12819SVincent.Wang@Sun.COM "%x\n", PM_NAME(dip), PM_ADDR(dip), ret));
1122*12819SVincent.Wang@Sun.COM }
1123*12819SVincent.Wang@Sun.COM }
1124*12819SVincent.Wang@Sun.COM #endif
1125*12819SVincent.Wang@Sun.COM
1126*12819SVincent.Wang@Sun.COM if (p) {
1127*12819SVincent.Wang@Sun.COM
1128*12819SVincent.Wang@Sun.COM /*
1129*12819SVincent.Wang@Sun.COM * Some BIOS (e.g. Toshiba M10) expects pci-ide to be in D0
1130*12819SVincent.Wang@Sun.COM * state when we set SLP_EN, otherwise it takes 5 minutes for
1131*12819SVincent.Wang@Sun.COM * the BIOS to put the system into S3.
1132*12819SVincent.Wang@Sun.COM */
1133*12819SVincent.Wang@Sun.COM if (strcmp(ddi_node_name(dip), "pci-ide") == 0) {
1134*12819SVincent.Wang@Sun.COM pmcsr = 0;
1135*12819SVincent.Wang@Sun.COM }
1136*12819SVincent.Wang@Sun.COM
1137*12819SVincent.Wang@Sun.COM /*
1138*12819SVincent.Wang@Sun.COM * pmcsr is the last write-operation to the device's PCI
1139*12819SVincent.Wang@Sun.COM * config space, because we found that there are
1140*12819SVincent.Wang@Sun.COM * some faulty devices whose PCI config space may not
1141*12819SVincent.Wang@Sun.COM * respond correctly once in D3 state.
1142*12819SVincent.Wang@Sun.COM */
1143*12819SVincent.Wang@Sun.COM if ((p->ppc_flags & PPCF_NOPMCAP) == 0 && pci_enable_wakeup) {
1144*12819SVincent.Wang@Sun.COM pci_config_put16(hdl, p->ppc_cap_offset + PCI_PMCSR,
1145*12819SVincent.Wang@Sun.COM PCI_PMCSR_PME_STAT);
1146*12819SVincent.Wang@Sun.COM pci_config_put16(hdl, p->ppc_cap_offset + PCI_PMCSR,
1147*12819SVincent.Wang@Sun.COM pmcsr);
1148*12819SVincent.Wang@Sun.COM }
1149*12819SVincent.Wang@Sun.COM
1150*12819SVincent.Wang@Sun.COM if (fromprop)
1151*12819SVincent.Wang@Sun.COM ddi_prop_free(p);
1152*12819SVincent.Wang@Sun.COM else
1153*12819SVincent.Wang@Sun.COM kmem_free(p, sizeof (*p));
1154*12819SVincent.Wang@Sun.COM }
1155*12819SVincent.Wang@Sun.COM
1156*12819SVincent.Wang@Sun.COM pci_config_teardown(&hdl);
1157*12819SVincent.Wang@Sun.COM
1158*12819SVincent.Wang@Sun.COM return (DDI_SUCCESS);
11595295Srandyf }
11605295Srandyf
11615295Srandyf /*
11625295Srandyf * The inverse of pci_post_suspend; handle pci-specific resume processing
11635295Srandyf * First, turn device back on, then restore config space.
11645295Srandyf */
11655295Srandyf
11665295Srandyf int
11675295Srandyf pci_pre_resume(dev_info_t *dip)
11685295Srandyf {
11695295Srandyf ddi_acc_handle_t hdl;
11705295Srandyf pci_pm_context_t *p;
11715295Srandyf /* E_FUNC_SET_NOT_USED */
11725295Srandyf uint16_t pmcap, pmcsr;
11735295Srandyf int flags;
11745295Srandyf uint_t length;
11755295Srandyf clock_t drv_usectohz(clock_t microsecs);
11765295Srandyf #if defined(__x86)
1177*12819SVincent.Wang@Sun.COM int retval;
11785295Srandyf #endif
11795295Srandyf
11805295Srandyf PMD(PMD_SX, ("pci_pre_resume %s:%d\n", ddi_driver_name(dip),
11815295Srandyf ddi_get_instance(dip)))
11825295Srandyf if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip,
11835295Srandyf DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
11845295Srandyf SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) {
11855295Srandyf return (DDI_FAILURE);
11865295Srandyf }
11875295Srandyf flags = p->ppc_flags;
11885295Srandyf pmcap = p->ppc_cap_offset;
11895295Srandyf pmcsr = p->ppc_pmcsr;
11905295Srandyf ddi_prop_free(p);
1191*12819SVincent.Wang@Sun.COM
11925295Srandyf #if defined(__x86)
11935295Srandyf /*
11945295Srandyf * Turn platform wake enable back off
11955295Srandyf */
11965295Srandyf
1197*12819SVincent.Wang@Sun.COM if (pci_enable_wakeup) {
11985295Srandyf retval = acpi_ddi_setwake(dip, 0); /* 0 for now */
11995295Srandyf if (retval) {
12005295Srandyf PMD(PMD_SX, ("pci_pre_resume, setwake %s@%s rets "
12015295Srandyf "%x\n", PM_NAME(dip), PM_ADDR(dip), retval));
12025295Srandyf }
12035295Srandyf }
12045295Srandyf #endif
1205*12819SVincent.Wang@Sun.COM if ((flags & PPCF_NOPMCAP) != 0)
1206*12819SVincent.Wang@Sun.COM goto done;
1207*12819SVincent.Wang@Sun.COM
12085295Srandyf if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) {
12095295Srandyf return (DDI_FAILURE);
12105295Srandyf }
12115295Srandyf pci_config_put16(hdl, pmcap + PCI_PMCSR, pmcsr);
12125295Srandyf delay(drv_usectohz(10000)); /* PCI PM spec D3->D0 (10ms) */
12135295Srandyf pci_config_teardown(&hdl);
12146996Sgs150176 done:
12155295Srandyf (void) pci_restore_config_regs(dip); /* fudges D-state! */
12165295Srandyf return (DDI_SUCCESS);
12175295Srandyf }
1218