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 */ 211106Smrj 220Sstevel@tonic-gate /* 236996Sgs150176 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/sunndi.h> 311865Sdilpreet #include <sys/sysmacros.h> 320Sstevel@tonic-gate #include <sys/pci.h> 33240Stimh #include <sys/pcie.h> 340Sstevel@tonic-gate #include <sys/pci_impl.h> 350Sstevel@tonic-gate #include <sys/epm.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate int 380Sstevel@tonic-gate pci_config_setup(dev_info_t *dip, ddi_acc_handle_t *handle) 390Sstevel@tonic-gate { 400Sstevel@tonic-gate caddr_t cfgaddr; 410Sstevel@tonic-gate ddi_device_acc_attr_t attr; 420Sstevel@tonic-gate 430Sstevel@tonic-gate attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 440Sstevel@tonic-gate attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 450Sstevel@tonic-gate attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 460Sstevel@tonic-gate 470Sstevel@tonic-gate /* Check for fault management capabilities */ 481865Sdilpreet if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(dip))) { 491865Sdilpreet attr.devacc_attr_version = DDI_DEVICE_ATTR_V1; 500Sstevel@tonic-gate attr.devacc_attr_access = DDI_FLAGERR_ACC; 511865Sdilpreet } 520Sstevel@tonic-gate 530Sstevel@tonic-gate return (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, handle)); 540Sstevel@tonic-gate } 550Sstevel@tonic-gate 560Sstevel@tonic-gate void 570Sstevel@tonic-gate pci_config_teardown(ddi_acc_handle_t *handle) 580Sstevel@tonic-gate { 590Sstevel@tonic-gate ddi_regs_map_free(handle); 600Sstevel@tonic-gate } 610Sstevel@tonic-gate 620Sstevel@tonic-gate uint8_t 630Sstevel@tonic-gate pci_config_get8(ddi_acc_handle_t handle, off_t offset) 640Sstevel@tonic-gate { 650Sstevel@tonic-gate caddr_t cfgaddr; 660Sstevel@tonic-gate ddi_acc_hdl_t *hp; 670Sstevel@tonic-gate 680Sstevel@tonic-gate hp = impl_acc_hdl_get(handle); 690Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset; 700Sstevel@tonic-gate return (ddi_get8(handle, (uint8_t *)cfgaddr)); 710Sstevel@tonic-gate } 720Sstevel@tonic-gate 730Sstevel@tonic-gate uint16_t 740Sstevel@tonic-gate pci_config_get16(ddi_acc_handle_t handle, off_t offset) 750Sstevel@tonic-gate { 760Sstevel@tonic-gate caddr_t cfgaddr; 770Sstevel@tonic-gate ddi_acc_hdl_t *hp; 780Sstevel@tonic-gate 790Sstevel@tonic-gate hp = impl_acc_hdl_get(handle); 800Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset; 810Sstevel@tonic-gate return (ddi_get16(handle, (uint16_t *)cfgaddr)); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate 840Sstevel@tonic-gate uint32_t 850Sstevel@tonic-gate pci_config_get32(ddi_acc_handle_t handle, off_t offset) 860Sstevel@tonic-gate { 870Sstevel@tonic-gate caddr_t cfgaddr; 880Sstevel@tonic-gate ddi_acc_hdl_t *hp; 890Sstevel@tonic-gate 900Sstevel@tonic-gate hp = impl_acc_hdl_get(handle); 910Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset; 920Sstevel@tonic-gate return (ddi_get32(handle, (uint32_t *)cfgaddr)); 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate uint64_t 960Sstevel@tonic-gate pci_config_get64(ddi_acc_handle_t handle, off_t offset) 970Sstevel@tonic-gate { 980Sstevel@tonic-gate caddr_t cfgaddr; 990Sstevel@tonic-gate ddi_acc_hdl_t *hp; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate hp = impl_acc_hdl_get(handle); 1020Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset; 1030Sstevel@tonic-gate return (ddi_get64(handle, (uint64_t *)cfgaddr)); 1040Sstevel@tonic-gate } 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate void 1070Sstevel@tonic-gate pci_config_put8(ddi_acc_handle_t handle, off_t offset, uint8_t value) 1080Sstevel@tonic-gate { 1090Sstevel@tonic-gate caddr_t cfgaddr; 1100Sstevel@tonic-gate ddi_acc_hdl_t *hp; 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate hp = impl_acc_hdl_get(handle); 1130Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset; 1140Sstevel@tonic-gate ddi_put8(handle, (uint8_t *)cfgaddr, value); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate void 1180Sstevel@tonic-gate pci_config_put16(ddi_acc_handle_t handle, off_t offset, uint16_t value) 1190Sstevel@tonic-gate { 1200Sstevel@tonic-gate caddr_t cfgaddr; 1210Sstevel@tonic-gate ddi_acc_hdl_t *hp; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate hp = impl_acc_hdl_get(handle); 1240Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset; 1250Sstevel@tonic-gate ddi_put16(handle, (uint16_t *)cfgaddr, value); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate void 1290Sstevel@tonic-gate pci_config_put32(ddi_acc_handle_t handle, off_t offset, uint32_t value) 1300Sstevel@tonic-gate { 1310Sstevel@tonic-gate caddr_t cfgaddr; 1320Sstevel@tonic-gate ddi_acc_hdl_t *hp; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate hp = impl_acc_hdl_get(handle); 1350Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset; 1360Sstevel@tonic-gate ddi_put32(handle, (uint32_t *)cfgaddr, value); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate void 1400Sstevel@tonic-gate pci_config_put64(ddi_acc_handle_t handle, off_t offset, uint64_t value) 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate caddr_t cfgaddr; 1430Sstevel@tonic-gate ddi_acc_hdl_t *hp; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate hp = impl_acc_hdl_get(handle); 1460Sstevel@tonic-gate cfgaddr = hp->ah_addr + offset; 1470Sstevel@tonic-gate ddi_put64(handle, (uint64_t *)cfgaddr, value); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1501106Smrj /* 1511106Smrj * We need to separate the old interfaces from the new ones and leave them 1521106Smrj * in here for a while. Previous versions of the OS defined the new interfaces 1531106Smrj * to the old interfaces. This way we can fix things up so that we can 1541106Smrj * eventually remove these interfaces. 1551106Smrj * e.g. A 3rd party module/driver using pci_config_get8 and built against S10 1561106Smrj * or earlier will actually have a reference to pci_config_getb in the binary. 1571106Smrj */ 1581106Smrj #ifdef _ILP32 1591106Smrj uint8_t 1601106Smrj pci_config_getb(ddi_acc_handle_t handle, off_t offset) 1611106Smrj { 1621106Smrj caddr_t cfgaddr; 1631106Smrj ddi_acc_hdl_t *hp; 1641106Smrj 1651106Smrj hp = impl_acc_hdl_get(handle); 1661106Smrj cfgaddr = hp->ah_addr + offset; 1671106Smrj return (ddi_get8(handle, (uint8_t *)cfgaddr)); 1681106Smrj } 1691106Smrj 1701106Smrj uint16_t 1711106Smrj pci_config_getw(ddi_acc_handle_t handle, off_t offset) 1721106Smrj { 1731106Smrj caddr_t cfgaddr; 1741106Smrj ddi_acc_hdl_t *hp; 1751106Smrj 1761106Smrj hp = impl_acc_hdl_get(handle); 1771106Smrj cfgaddr = hp->ah_addr + offset; 1781106Smrj return (ddi_get16(handle, (uint16_t *)cfgaddr)); 1791106Smrj } 1801106Smrj 1811106Smrj uint32_t 1821106Smrj pci_config_getl(ddi_acc_handle_t handle, off_t offset) 1831106Smrj { 1841106Smrj caddr_t cfgaddr; 1851106Smrj ddi_acc_hdl_t *hp; 1861106Smrj 1871106Smrj hp = impl_acc_hdl_get(handle); 1881106Smrj cfgaddr = hp->ah_addr + offset; 1891106Smrj return (ddi_get32(handle, (uint32_t *)cfgaddr)); 1901106Smrj } 1911106Smrj 1921106Smrj uint64_t 1931106Smrj pci_config_getll(ddi_acc_handle_t handle, off_t offset) 1941106Smrj { 1951106Smrj caddr_t cfgaddr; 1961106Smrj ddi_acc_hdl_t *hp; 1971106Smrj 1981106Smrj hp = impl_acc_hdl_get(handle); 1991106Smrj cfgaddr = hp->ah_addr + offset; 2001106Smrj return (ddi_get64(handle, (uint64_t *)cfgaddr)); 2011106Smrj } 2021106Smrj 2031106Smrj void 2041106Smrj pci_config_putb(ddi_acc_handle_t handle, off_t offset, uint8_t value) 2051106Smrj { 2061106Smrj caddr_t cfgaddr; 2071106Smrj ddi_acc_hdl_t *hp; 2081106Smrj 2091106Smrj hp = impl_acc_hdl_get(handle); 2101106Smrj cfgaddr = hp->ah_addr + offset; 2111106Smrj ddi_put8(handle, (uint8_t *)cfgaddr, value); 2121106Smrj } 2131106Smrj 2141106Smrj void 2151106Smrj pci_config_putw(ddi_acc_handle_t handle, off_t offset, uint16_t value) 2161106Smrj { 2171106Smrj caddr_t cfgaddr; 2181106Smrj ddi_acc_hdl_t *hp; 2191106Smrj 2201106Smrj hp = impl_acc_hdl_get(handle); 2211106Smrj cfgaddr = hp->ah_addr + offset; 2221106Smrj ddi_put16(handle, (uint16_t *)cfgaddr, value); 2231106Smrj } 2241106Smrj 2251106Smrj void 2261106Smrj pci_config_putl(ddi_acc_handle_t handle, off_t offset, uint32_t value) 2271106Smrj { 2281106Smrj caddr_t cfgaddr; 2291106Smrj ddi_acc_hdl_t *hp; 2301106Smrj 2311106Smrj hp = impl_acc_hdl_get(handle); 2321106Smrj cfgaddr = hp->ah_addr + offset; 2331106Smrj ddi_put32(handle, (uint32_t *)cfgaddr, value); 2341106Smrj } 2351106Smrj 2361106Smrj void 2371106Smrj pci_config_putll(ddi_acc_handle_t handle, off_t offset, uint64_t value) 2381106Smrj { 2391106Smrj caddr_t cfgaddr; 2401106Smrj ddi_acc_hdl_t *hp; 2411106Smrj 2421106Smrj hp = impl_acc_hdl_get(handle); 2431106Smrj cfgaddr = hp->ah_addr + offset; 2441106Smrj ddi_put64(handle, (uint64_t *)cfgaddr, value); 2451106Smrj } 2461106Smrj #endif /* _ILP32 */ 2471106Smrj 2480Sstevel@tonic-gate /*ARGSUSED*/ 2490Sstevel@tonic-gate int 2500Sstevel@tonic-gate pci_report_pmcap(dev_info_t *dip, int cap, void *arg) 2510Sstevel@tonic-gate { 2520Sstevel@tonic-gate return (DDI_SUCCESS); 2530Sstevel@tonic-gate } 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate /* 2560Sstevel@tonic-gate * Note about saving and restoring config space. 2570Sstevel@tonic-gate * PCI devices have only upto 256 bytes of config space while PCI Express 2580Sstevel@tonic-gate * devices can have upto 4k config space. In case of PCI Express device, 2590Sstevel@tonic-gate * we save all 4k config space and restore it even if it doesn't make use 2600Sstevel@tonic-gate * of all 4k. But some devices don't respond to reads to non-existent 2610Sstevel@tonic-gate * registers within the config space. To avoid any panics, we use ddi_peek 2620Sstevel@tonic-gate * to do the reads. A bit mask is used to indicate which words of the 2630Sstevel@tonic-gate * config space are accessible. While restoring the config space, only those 2640Sstevel@tonic-gate * readable words are restored. We do all this in 32 bit size words. 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate #define INDEX_SHIFT 3 2670Sstevel@tonic-gate #define BITMASK 0x7 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate static uint32_t pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 2700Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp); 2710Sstevel@tonic-gate static void pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 2720Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t elements); 2730Sstevel@tonic-gate static uint32_t pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 2740Sstevel@tonic-gate uint32_t *regbuf, uint32_t nwords); 2750Sstevel@tonic-gate static uint32_t pci_msi_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_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 2780Sstevel@tonic-gate uint32_t *regbuf, uint32_t notused); 2790Sstevel@tonic-gate static uint32_t pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 2800Sstevel@tonic-gate uint32_t *regbuf, uint32_t notused); 2810Sstevel@tonic-gate static void pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 2820Sstevel@tonic-gate uint32_t *regbuf, uint32_t nwords); 2830Sstevel@tonic-gate static uint32_t cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf, 2840Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace); 2850Sstevel@tonic-gate static void pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf, 2860Sstevel@tonic-gate uint16_t pmcap_offset); 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* 2890Sstevel@tonic-gate * Table below specifies the number of registers to be saved for each PCI 2900Sstevel@tonic-gate * capability. pci_generic_save saves the number of words specified in the 2910Sstevel@tonic-gate * table. Any special considerations will be taken care by the capability 2920Sstevel@tonic-gate * specific save function e.g. use pci_msi_save to save registers associated 2930Sstevel@tonic-gate * with MSI capability. PCI_UNKNOWN_SIZE indicates that number of registers 2940Sstevel@tonic-gate * to be saved is variable and will be determined by the specific save function. 2950Sstevel@tonic-gate * Currently we save/restore all the registers associated with the capability 2960Sstevel@tonic-gate * including read only registers. Regsiters are saved and restored in 32 bit 2970Sstevel@tonic-gate * size words. 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate static pci_cap_entry_t pci_cap_table[] = { 3000Sstevel@tonic-gate {PCI_CAP_ID_PM, PCI_PMCAP_NDWORDS, pci_generic_save}, 3010Sstevel@tonic-gate {PCI_CAP_ID_AGP, PCI_AGP_NDWORDS, pci_generic_save}, 3020Sstevel@tonic-gate {PCI_CAP_ID_SLOT_ID, PCI_SLOTID_NDWORDS, pci_generic_save}, 3030Sstevel@tonic-gate {PCI_CAP_ID_MSI_X, PCI_MSIX_NDWORDS, pci_generic_save}, 3040Sstevel@tonic-gate {PCI_CAP_ID_MSI, PCI_CAP_SZUNKNOWN, pci_msi_save}, 3050Sstevel@tonic-gate {PCI_CAP_ID_PCIX, PCI_CAP_SZUNKNOWN, pci_pcix_save}, 3060Sstevel@tonic-gate {PCI_CAP_ID_PCI_E, PCI_CAP_SZUNKNOWN, pci_pcie_save}, 3070Sstevel@tonic-gate /* 3080Sstevel@tonic-gate * {PCI_CAP_ID_cPCI_CRC, 0, NULL}, 3090Sstevel@tonic-gate * {PCI_CAP_ID_VPD, 0, NULL}, 3100Sstevel@tonic-gate * {PCI_CAP_ID_cPCI_HS, 0, NULL}, 3110Sstevel@tonic-gate * {PCI_CAP_ID_PCI_HOTPLUG, 0, NULL}, 3120Sstevel@tonic-gate * {PCI_CAP_ID_AGP_8X, 0, NULL}, 3130Sstevel@tonic-gate * {PCI_CAP_ID_SECURE_DEV, 0, NULL}, 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate {PCI_CAP_NEXT_PTR_NULL, 0, NULL} 3160Sstevel@tonic-gate }; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate /* 3190Sstevel@tonic-gate * Save the configuration registers for cdip as a property 3200Sstevel@tonic-gate * so that it persists after detach/uninitchild. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate int 3230Sstevel@tonic-gate pci_save_config_regs(dev_info_t *dip) 3240Sstevel@tonic-gate { 3250Sstevel@tonic-gate ddi_acc_handle_t confhdl; 3260Sstevel@tonic-gate pci_config_header_state_t *chsp; 3270Sstevel@tonic-gate pci_cap_save_desc_t *pci_cap_descp; 3280Sstevel@tonic-gate int ret; 3290Sstevel@tonic-gate uint32_t i, ncaps, nwords; 3300Sstevel@tonic-gate uint32_t *regbuf, *p; 3310Sstevel@tonic-gate uint8_t *maskbuf; 3320Sstevel@tonic-gate size_t maskbufsz, regbufsz, capbufsz; 333*7270Sgs150176 #ifdef __sparc 3340Sstevel@tonic-gate ddi_acc_hdl_t *hp; 335*7270Sgs150176 #else 336*7270Sgs150176 ddi_device_acc_attr_t attr; 337*7270Sgs150176 caddr_t cfgaddr; 338*7270Sgs150176 #endif 3390Sstevel@tonic-gate off_t offset = 0; 3400Sstevel@tonic-gate uint8_t cap_ptr, cap_id; 3410Sstevel@tonic-gate int pcie = 0; 3427168Syf149591 uint16_t status; 3437168Syf149591 3445295Srandyf PMD(PMD_SX, ("pci_save_config_regs %s:%d\n", ddi_driver_name(dip), 3455295Srandyf ddi_get_instance(dip))) 3460Sstevel@tonic-gate 347*7270Sgs150176 #ifdef __sparc 3480Sstevel@tonic-gate if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 3490Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't get config handle", 3505295Srandyf ddi_driver_name(dip), ddi_get_instance(dip)); 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate return (DDI_FAILURE); 3530Sstevel@tonic-gate } 354*7270Sgs150176 #else 355*7270Sgs150176 /* Set up cautious config access handle */ 356*7270Sgs150176 attr.devacc_attr_version = DDI_DEVICE_ATTR_V1; 357*7270Sgs150176 attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 358*7270Sgs150176 attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 359*7270Sgs150176 attr.devacc_attr_access = DDI_CAUTIOUS_ACC; 360*7270Sgs150176 if (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, &confhdl) 361*7270Sgs150176 != DDI_SUCCESS) { 362*7270Sgs150176 cmn_err(CE_WARN, "%s%d can't setup cautious config handle", 363*7270Sgs150176 ddi_driver_name(dip), ddi_get_instance(dip)); 364*7270Sgs150176 365*7270Sgs150176 return (DDI_FAILURE); 366*7270Sgs150176 } 367*7270Sgs150176 #endif 3687168Syf149591 3697168Syf149591 /* 3707168Syf149591 * Determine if it implements capabilities 3717168Syf149591 */ 3727168Syf149591 status = pci_config_get16(confhdl, PCI_CONF_STAT); 3737168Syf149591 if (!(status & 0x10)) { 3747168Syf149591 goto no_cap; 3757168Syf149591 } 3760Sstevel@tonic-gate /* 3770Sstevel@tonic-gate * Determine if it is a pci express device. If it is, save entire 3780Sstevel@tonic-gate * 4k config space treating it as a array of 32 bit integers. 3790Sstevel@tonic-gate * If it is not, do it in a usual PCI way. 3800Sstevel@tonic-gate */ 3810Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 3820Sstevel@tonic-gate /* 3830Sstevel@tonic-gate * Walk the capabilities searching for pci express capability 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 3860Sstevel@tonic-gate cap_id = pci_config_get8(confhdl, 3870Sstevel@tonic-gate cap_ptr + PCI_CAP_ID); 3880Sstevel@tonic-gate if (cap_id == PCI_CAP_ID_PCI_E) { 3890Sstevel@tonic-gate pcie = 1; 3900Sstevel@tonic-gate break; 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, 3930Sstevel@tonic-gate cap_ptr + PCI_CAP_NEXT_PTR); 3940Sstevel@tonic-gate } 3957168Syf149591 no_cap: 3960Sstevel@tonic-gate if (pcie) { 3970Sstevel@tonic-gate /* PCI express device. Can have data in all 4k space */ 3980Sstevel@tonic-gate regbuf = (uint32_t *)kmem_zalloc((size_t)PCIE_CONF_HDR_SIZE, 3995295Srandyf KM_SLEEP); 4000Sstevel@tonic-gate p = regbuf; 4010Sstevel@tonic-gate /* 4020Sstevel@tonic-gate * Allocate space for mask. 4030Sstevel@tonic-gate * mask size is 128 bytes (4096 / 4 / 8 ) 4040Sstevel@tonic-gate */ 4050Sstevel@tonic-gate maskbufsz = (size_t)((PCIE_CONF_HDR_SIZE/ sizeof (uint32_t)) >> 4060Sstevel@tonic-gate INDEX_SHIFT); 4070Sstevel@tonic-gate maskbuf = (uint8_t *)kmem_zalloc(maskbufsz, KM_SLEEP); 408*7270Sgs150176 #ifdef __sparc 4090Sstevel@tonic-gate hp = impl_acc_hdl_get(confhdl); 410*7270Sgs150176 #endif 4110Sstevel@tonic-gate for (i = 0; i < (PCIE_CONF_HDR_SIZE / sizeof (uint32_t)); i++) { 412*7270Sgs150176 #ifdef __sparc 413*7270Sgs150176 ret = ddi_peek32(dip, (int32_t *)(hp->ah_addr + offset), 414*7270Sgs150176 (int32_t *)p); 4156996Sgs150176 if (ret == DDI_SUCCESS) { 416*7270Sgs150176 #else 417*7270Sgs150176 /* 418*7270Sgs150176 * ddi_peek doesn't work on x86, so we use cautious pci 419*7270Sgs150176 * config access instead. 420*7270Sgs150176 */ 421*7270Sgs150176 *p = pci_config_get32(confhdl, offset); 422*7270Sgs150176 if (*p != -1) { 423*7270Sgs150176 #endif 4240Sstevel@tonic-gate /* it is readable register. set the bit */ 4250Sstevel@tonic-gate maskbuf[i >> INDEX_SHIFT] |= 4260Sstevel@tonic-gate (uint8_t)(1 << (i & BITMASK)); 4270Sstevel@tonic-gate } 4280Sstevel@tonic-gate p++; 4290Sstevel@tonic-gate offset += sizeof (uint32_t); 4300Sstevel@tonic-gate } 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 4330Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK, (uchar_t *)maskbuf, 4340Sstevel@tonic-gate maskbufsz)) != DDI_PROP_SUCCESS) { 4350Sstevel@tonic-gate cmn_err(CE_WARN, "couldn't create %s property while" 4360Sstevel@tonic-gate "saving config space for %s@%d\n", 4370Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK, ddi_driver_name(dip), 4380Sstevel@tonic-gate ddi_get_instance(dip)); 4390Sstevel@tonic-gate } else if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, 4400Sstevel@tonic-gate dip, SAVED_CONFIG_REGS, (uchar_t *)regbuf, 4410Sstevel@tonic-gate (size_t)PCIE_CONF_HDR_SIZE)) != DDI_PROP_SUCCESS) { 4420Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 4430Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 4440Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't update prop %s", 4450Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 4460Sstevel@tonic-gate SAVED_CONFIG_REGS); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate kmem_free(maskbuf, (size_t)maskbufsz); 4500Sstevel@tonic-gate kmem_free(regbuf, (size_t)PCIE_CONF_HDR_SIZE); 4510Sstevel@tonic-gate } else { 4520Sstevel@tonic-gate regbuf = (uint32_t *)kmem_zalloc((size_t)PCI_CONF_HDR_SIZE, 4535295Srandyf KM_SLEEP); 4540Sstevel@tonic-gate chsp = (pci_config_header_state_t *)regbuf; 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate chsp->chs_command = pci_config_get16(confhdl, PCI_CONF_COMM); 4570Sstevel@tonic-gate chsp->chs_header_type = pci_config_get8(confhdl, 4585295Srandyf PCI_CONF_HEADER); 4590Sstevel@tonic-gate if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 4600Sstevel@tonic-gate PCI_HEADER_ONE) 4610Sstevel@tonic-gate chsp->chs_bridge_control = 4620Sstevel@tonic-gate pci_config_get16(confhdl, PCI_BCNF_BCNTRL); 4630Sstevel@tonic-gate chsp->chs_cache_line_size = pci_config_get8(confhdl, 4640Sstevel@tonic-gate PCI_CONF_CACHE_LINESZ); 4650Sstevel@tonic-gate chsp->chs_latency_timer = pci_config_get8(confhdl, 4660Sstevel@tonic-gate PCI_CONF_LATENCY_TIMER); 4670Sstevel@tonic-gate if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 4680Sstevel@tonic-gate PCI_HEADER_ONE) { 4690Sstevel@tonic-gate chsp->chs_sec_latency_timer = 4700Sstevel@tonic-gate pci_config_get8(confhdl, PCI_BCNF_LATENCY_TIMER); 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate chsp->chs_base0 = pci_config_get32(confhdl, PCI_CONF_BASE0); 4740Sstevel@tonic-gate chsp->chs_base1 = pci_config_get32(confhdl, PCI_CONF_BASE1); 4750Sstevel@tonic-gate chsp->chs_base2 = pci_config_get32(confhdl, PCI_CONF_BASE2); 4760Sstevel@tonic-gate chsp->chs_base3 = pci_config_get32(confhdl, PCI_CONF_BASE3); 4770Sstevel@tonic-gate chsp->chs_base4 = pci_config_get32(confhdl, PCI_CONF_BASE4); 4780Sstevel@tonic-gate chsp->chs_base5 = pci_config_get32(confhdl, PCI_CONF_BASE5); 4790Sstevel@tonic-gate 4800Sstevel@tonic-gate /* 4810Sstevel@tonic-gate * Allocate maximum space required for capability descriptions. 4820Sstevel@tonic-gate * The maximum number of capabilties saved is the number of 4830Sstevel@tonic-gate * capabilities listed in the pci_cap_table. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate ncaps = (sizeof (pci_cap_table) / sizeof (pci_cap_entry_t)); 4860Sstevel@tonic-gate capbufsz = ncaps * sizeof (pci_cap_save_desc_t); 4870Sstevel@tonic-gate pci_cap_descp = (pci_cap_save_desc_t *)kmem_zalloc( 4880Sstevel@tonic-gate capbufsz, KM_SLEEP); 4890Sstevel@tonic-gate p = (uint32_t *)((caddr_t)regbuf + 4900Sstevel@tonic-gate sizeof (pci_config_header_state_t)); 4910Sstevel@tonic-gate nwords = pci_save_caps(confhdl, p, pci_cap_descp, &ncaps); 4920Sstevel@tonic-gate regbufsz = sizeof (pci_config_header_state_t) + 4930Sstevel@tonic-gate nwords * sizeof (uint32_t); 4940Sstevel@tonic-gate 4950Sstevel@tonic-gate if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 4960Sstevel@tonic-gate SAVED_CONFIG_REGS, (uchar_t *)regbuf, regbufsz)) != 4970Sstevel@tonic-gate DDI_PROP_SUCCESS) { 4980Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't update prop %s", 4990Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 5000Sstevel@tonic-gate SAVED_CONFIG_REGS); 5010Sstevel@tonic-gate } else if (ncaps) { 5020Sstevel@tonic-gate ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 5030Sstevel@tonic-gate SAVED_CONFIG_REGS_CAPINFO, (uchar_t *)pci_cap_descp, 5040Sstevel@tonic-gate ncaps * sizeof (pci_cap_save_desc_t)); 5050Sstevel@tonic-gate if (ret != DDI_PROP_SUCCESS) 5060Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 5070Sstevel@tonic-gate SAVED_CONFIG_REGS); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate kmem_free(regbuf, (size_t)PCI_CONF_HDR_SIZE); 5100Sstevel@tonic-gate kmem_free(pci_cap_descp, capbufsz); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate pci_config_teardown(&confhdl); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate if (ret != DDI_PROP_SUCCESS) 5150Sstevel@tonic-gate return (DDI_FAILURE); 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate return (DDI_SUCCESS); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate /* 5210Sstevel@tonic-gate * Saves registers associated with PCI capabilities. 5220Sstevel@tonic-gate * Returns number of 32 bit words saved. 5230Sstevel@tonic-gate * Number of capabilities saved is returned in ncapsp. 5240Sstevel@tonic-gate */ 5250Sstevel@tonic-gate static uint32_t 5260Sstevel@tonic-gate pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 5270Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp) 5280Sstevel@tonic-gate { 5290Sstevel@tonic-gate return (cap_walk_and_save(confhdl, regbuf, cap_descp, ncapsp, 0)); 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate static uint32_t 5330Sstevel@tonic-gate cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf, 5340Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace) 5350Sstevel@tonic-gate { 5360Sstevel@tonic-gate pci_cap_entry_t *pci_cap_entp; 5377168Syf149591 uint16_t cap_id, offset, status; 5380Sstevel@tonic-gate uint32_t words_saved = 0, nwords = 0; 5390Sstevel@tonic-gate uint16_t cap_ptr = PCI_CAP_NEXT_PTR_NULL; 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate *ncapsp = 0; 5427168Syf149591 5437168Syf149591 /* 5447168Syf149591 * Determine if it implements capabilities 5457168Syf149591 */ 5467168Syf149591 status = pci_config_get16(confhdl, PCI_CONF_STAT); 5477168Syf149591 if (!(status & 0x10)) { 5487168Syf149591 return (words_saved); 5497168Syf149591 } 5507168Syf149591 5510Sstevel@tonic-gate if (!xspace) 5520Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 5530Sstevel@tonic-gate /* 5540Sstevel@tonic-gate * Walk the capabilities 5550Sstevel@tonic-gate */ 5560Sstevel@tonic-gate while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 5570Sstevel@tonic-gate cap_id = CAP_ID(confhdl, cap_ptr, xspace); 5580Sstevel@tonic-gate /* Search for this cap id in our table */ 5590Sstevel@tonic-gate if (!xspace) 5600Sstevel@tonic-gate pci_cap_entp = pci_cap_table; 5610Sstevel@tonic-gate while (pci_cap_entp->cap_id != PCI_CAP_NEXT_PTR_NULL && 5620Sstevel@tonic-gate pci_cap_entp->cap_id != cap_id) 5630Sstevel@tonic-gate pci_cap_entp++; 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate offset = cap_ptr; 5660Sstevel@tonic-gate cap_ptr = NEXT_CAP(confhdl, cap_ptr, xspace); 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * If this cap id is not found in the table, there is nothing 5690Sstevel@tonic-gate * to save. 5700Sstevel@tonic-gate */ 5710Sstevel@tonic-gate if (pci_cap_entp->cap_id == PCI_CAP_NEXT_PTR_NULL) 5720Sstevel@tonic-gate continue; 5730Sstevel@tonic-gate if (pci_cap_entp->cap_save_func) { 5740Sstevel@tonic-gate if ((nwords = pci_cap_entp->cap_save_func(confhdl, 5750Sstevel@tonic-gate offset, regbuf, pci_cap_entp->cap_ndwords))) { 5760Sstevel@tonic-gate cap_descp->cap_nregs = nwords; 5770Sstevel@tonic-gate cap_descp->cap_offset = offset; 5780Sstevel@tonic-gate cap_descp->cap_id = cap_id; 5790Sstevel@tonic-gate regbuf += nwords; 5800Sstevel@tonic-gate cap_descp++; 5810Sstevel@tonic-gate words_saved += nwords; 5820Sstevel@tonic-gate (*ncapsp)++; 5830Sstevel@tonic-gate } 5840Sstevel@tonic-gate } 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate return (words_saved); 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate static void 5910Sstevel@tonic-gate pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 5920Sstevel@tonic-gate uint32_t *regbuf, uint32_t nwords) 5930Sstevel@tonic-gate { 5940Sstevel@tonic-gate int i; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate for (i = 0; i < nwords; i++) { 5970Sstevel@tonic-gate *regbuf = pci_config_get32(confhdl, cap_ptr); 5980Sstevel@tonic-gate regbuf++; 5990Sstevel@tonic-gate cap_ptr += 4; 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate } 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate static uint32_t 6040Sstevel@tonic-gate pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 6050Sstevel@tonic-gate uint32_t nwords) 6060Sstevel@tonic-gate { 6070Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 6080Sstevel@tonic-gate return (nwords); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate /*ARGSUSED*/ 6120Sstevel@tonic-gate static uint32_t 6130Sstevel@tonic-gate pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 6140Sstevel@tonic-gate uint32_t notused) 6150Sstevel@tonic-gate { 6160Sstevel@tonic-gate uint32_t nwords = PCI_MSI_MIN_WORDS; 6170Sstevel@tonic-gate uint16_t msi_ctrl; 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate /* Figure out how many registers to be saved */ 6200Sstevel@tonic-gate msi_ctrl = pci_config_get16(confhdl, cap_ptr + PCI_MSI_CTRL); 6210Sstevel@tonic-gate /* If 64 bit address capable add one word */ 6220Sstevel@tonic-gate if (msi_ctrl & PCI_MSI_64BIT_MASK) 6230Sstevel@tonic-gate nwords++; 6240Sstevel@tonic-gate /* If per vector masking capable, add two more words */ 6250Sstevel@tonic-gate if (msi_ctrl & PCI_MSI_PVM_MASK) 6260Sstevel@tonic-gate nwords += 2; 6270Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate return (nwords); 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate /*ARGSUSED*/ 6330Sstevel@tonic-gate static uint32_t 6340Sstevel@tonic-gate pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 6350Sstevel@tonic-gate uint32_t notused) 6360Sstevel@tonic-gate { 6370Sstevel@tonic-gate uint32_t nwords = PCI_PCIX_MIN_WORDS; 6380Sstevel@tonic-gate uint16_t pcix_command; 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate /* Figure out how many registers to be saved */ 6410Sstevel@tonic-gate pcix_command = pci_config_get16(confhdl, cap_ptr + PCI_PCIX_COMMAND); 6420Sstevel@tonic-gate /* If it is version 1 or version 2, add 4 words */ 6430Sstevel@tonic-gate if (((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_1) || 6440Sstevel@tonic-gate ((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_2)) 6450Sstevel@tonic-gate nwords += 4; 6460Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate return (nwords); 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate /*ARGSUSED*/ 6520Sstevel@tonic-gate static uint32_t 6530Sstevel@tonic-gate pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 6540Sstevel@tonic-gate uint32_t notused) 6550Sstevel@tonic-gate { 6560Sstevel@tonic-gate return (0); 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate 6590Sstevel@tonic-gate static void 6600Sstevel@tonic-gate pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf, 6610Sstevel@tonic-gate uint16_t pmcap_offset) 6620Sstevel@tonic-gate { 6630Sstevel@tonic-gate uint16_t pmcsr; 6640Sstevel@tonic-gate uint16_t pmcsr_offset = pmcap_offset + PCI_PMCSR; 6650Sstevel@tonic-gate uint32_t *saved_pmcsrp = (uint32_t *)((caddr_t)regbuf + PCI_PMCSR); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate /* 6680Sstevel@tonic-gate * Copy the power state bits from the PMCSR to our saved copy. 6690Sstevel@tonic-gate * This is to make sure that we don't change the D state when 6700Sstevel@tonic-gate * we restore config space of the device. 6710Sstevel@tonic-gate */ 6720Sstevel@tonic-gate pmcsr = pci_config_get16(confhdl, pmcsr_offset); 6730Sstevel@tonic-gate (*saved_pmcsrp) &= ~PCI_PMCSR_STATE_MASK; 6740Sstevel@tonic-gate (*saved_pmcsrp) |= (pmcsr & PCI_PMCSR_STATE_MASK); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate static void 6780Sstevel@tonic-gate pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 6790Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t elements) 6800Sstevel@tonic-gate { 6810Sstevel@tonic-gate int i, j; 6820Sstevel@tonic-gate uint16_t offset; 6830Sstevel@tonic-gate 6840Sstevel@tonic-gate for (i = 0; i < (elements / sizeof (pci_cap_save_desc_t)); i++) { 6850Sstevel@tonic-gate offset = cap_descp->cap_offset; 6860Sstevel@tonic-gate if (cap_descp->cap_id == PCI_CAP_ID_PM) 6870Sstevel@tonic-gate pci_pmcap_check(confhdl, regbuf, offset); 6880Sstevel@tonic-gate for (j = 0; j < cap_descp->cap_nregs; j++) { 6890Sstevel@tonic-gate pci_config_put32(confhdl, offset, *regbuf); 6900Sstevel@tonic-gate regbuf++; 6910Sstevel@tonic-gate offset += 4; 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate cap_descp++; 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate /* 6980Sstevel@tonic-gate * Restore config_regs from a single devinfo node. 6990Sstevel@tonic-gate */ 7000Sstevel@tonic-gate int 7010Sstevel@tonic-gate pci_restore_config_regs(dev_info_t *dip) 7020Sstevel@tonic-gate { 7030Sstevel@tonic-gate ddi_acc_handle_t confhdl; 7040Sstevel@tonic-gate pci_config_header_state_t *chs_p; 7050Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp; 7060Sstevel@tonic-gate uint32_t elements, i; 7070Sstevel@tonic-gate uint8_t *maskbuf; 7080Sstevel@tonic-gate uint32_t *regbuf, *p; 7090Sstevel@tonic-gate off_t offset = 0; 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 7120Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't get config handle", 7130Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 7140Sstevel@tonic-gate return (DDI_FAILURE); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 7180Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS_MASK, 7190Sstevel@tonic-gate (uchar_t **)&maskbuf, &elements) == DDI_PROP_SUCCESS) { 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 7220Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 7230Sstevel@tonic-gate (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 7240Sstevel@tonic-gate goto restoreconfig_err; 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate ASSERT(elements == PCIE_CONF_HDR_SIZE); 7270Sstevel@tonic-gate /* pcie device and has 4k config space saved */ 7280Sstevel@tonic-gate p = regbuf; 7290Sstevel@tonic-gate for (i = 0; i < PCIE_CONF_HDR_SIZE / sizeof (uint32_t); i++) { 7300Sstevel@tonic-gate /* If the word is readable then restore it */ 7310Sstevel@tonic-gate if (maskbuf[i >> INDEX_SHIFT] & 7320Sstevel@tonic-gate (uint8_t)(1 << (i & BITMASK))) 7330Sstevel@tonic-gate pci_config_put32(confhdl, offset, *p); 7340Sstevel@tonic-gate p++; 7350Sstevel@tonic-gate offset += sizeof (uint32_t); 7360Sstevel@tonic-gate } 7370Sstevel@tonic-gate ddi_prop_free(regbuf); 7380Sstevel@tonic-gate ddi_prop_free(maskbuf); 7390Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, 7400Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK) != DDI_PROP_SUCCESS) { 7410Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 7420Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 7430Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 7440Sstevel@tonic-gate } 7450Sstevel@tonic-gate } else { 7460Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 7470Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 7480Sstevel@tonic-gate (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate pci_config_teardown(&confhdl); 7516996Sgs150176 return (DDI_SUCCESS); 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate chs_p = (pci_config_header_state_t *)regbuf; 7550Sstevel@tonic-gate pci_config_put16(confhdl, PCI_CONF_COMM, 7560Sstevel@tonic-gate chs_p->chs_command); 7570Sstevel@tonic-gate if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 7580Sstevel@tonic-gate PCI_HEADER_ONE) { 7590Sstevel@tonic-gate pci_config_put16(confhdl, PCI_BCNF_BCNTRL, 7600Sstevel@tonic-gate chs_p->chs_bridge_control); 7610Sstevel@tonic-gate } 7620Sstevel@tonic-gate pci_config_put8(confhdl, PCI_CONF_CACHE_LINESZ, 7630Sstevel@tonic-gate chs_p->chs_cache_line_size); 7640Sstevel@tonic-gate pci_config_put8(confhdl, PCI_CONF_LATENCY_TIMER, 7650Sstevel@tonic-gate chs_p->chs_latency_timer); 7660Sstevel@tonic-gate if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 7670Sstevel@tonic-gate PCI_HEADER_ONE) 7680Sstevel@tonic-gate pci_config_put8(confhdl, PCI_BCNF_LATENCY_TIMER, 7690Sstevel@tonic-gate chs_p->chs_sec_latency_timer); 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE0, chs_p->chs_base0); 7720Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE1, chs_p->chs_base1); 7730Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE2, chs_p->chs_base2); 7740Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE3, chs_p->chs_base3); 7750Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE4, chs_p->chs_base4); 7760Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE5, chs_p->chs_base5); 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 7790Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 7800Sstevel@tonic-gate SAVED_CONFIG_REGS_CAPINFO, 7810Sstevel@tonic-gate (uchar_t **)&cap_descp, &elements) == DDI_PROP_SUCCESS) { 7820Sstevel@tonic-gate /* 7830Sstevel@tonic-gate * PCI capability related regsiters are saved. 7840Sstevel@tonic-gate * Restore them based on the description. 7850Sstevel@tonic-gate */ 7860Sstevel@tonic-gate p = (uint32_t *)((caddr_t)regbuf + 7870Sstevel@tonic-gate sizeof (pci_config_header_state_t)); 7880Sstevel@tonic-gate pci_restore_caps(confhdl, p, cap_descp, elements); 7890Sstevel@tonic-gate ddi_prop_free(cap_descp); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate ddi_prop_free(regbuf); 7930Sstevel@tonic-gate } 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate /* 7960Sstevel@tonic-gate * Make sure registers are flushed 7970Sstevel@tonic-gate */ 7980Sstevel@tonic-gate (void) pci_config_get32(confhdl, PCI_CONF_BASE5); 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS) != 8020Sstevel@tonic-gate DDI_PROP_SUCCESS) { 8030Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 8040Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 8050Sstevel@tonic-gate SAVED_CONFIG_REGS); 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate pci_config_teardown(&confhdl); 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate return (DDI_SUCCESS); 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate restoreconfig_err: 8130Sstevel@tonic-gate ddi_prop_free(maskbuf); 8140Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS_MASK) != 8150Sstevel@tonic-gate DDI_PROP_SUCCESS) { 8160Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 8170Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 8180Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 8190Sstevel@tonic-gate } 8200Sstevel@tonic-gate pci_config_teardown(&confhdl); 8210Sstevel@tonic-gate return (DDI_FAILURE); 8220Sstevel@tonic-gate } 8235295Srandyf 8245295Srandyf /*ARGSUSED*/ 8255295Srandyf static int 8265295Srandyf pci_lookup_pmcap(dev_info_t *dip, ddi_acc_handle_t conf_hdl, 8275295Srandyf uint16_t *pmcap_offsetp) 8285295Srandyf { 8295295Srandyf uint8_t cap_ptr; 8305295Srandyf uint8_t cap_id; 8315295Srandyf uint8_t header_type; 8325295Srandyf uint16_t status; 8335295Srandyf 8345295Srandyf header_type = pci_config_get8(conf_hdl, PCI_CONF_HEADER); 8355295Srandyf header_type &= PCI_HEADER_TYPE_M; 8365295Srandyf 8375295Srandyf /* we don't deal with bridges, etc here */ 8385295Srandyf if (header_type != PCI_HEADER_ZERO) { 8395295Srandyf return (DDI_FAILURE); 8405295Srandyf } 8415295Srandyf 8425295Srandyf status = pci_config_get16(conf_hdl, PCI_CONF_STAT); 8435295Srandyf if ((status & PCI_STAT_CAP) == 0) { 8445295Srandyf return (DDI_FAILURE); 8455295Srandyf } 8465295Srandyf 8475295Srandyf cap_ptr = pci_config_get8(conf_hdl, PCI_CONF_CAP_PTR); 8485295Srandyf 8495295Srandyf /* 8505295Srandyf * Walk the capabilities searching for a PM entry. 8515295Srandyf */ 8525295Srandyf while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 8535295Srandyf cap_id = pci_config_get8(conf_hdl, cap_ptr + PCI_CAP_ID); 8545295Srandyf if (cap_id == PCI_CAP_ID_PM) { 8555295Srandyf break; 8565295Srandyf } 8575295Srandyf cap_ptr = pci_config_get8(conf_hdl, 8585295Srandyf cap_ptr + PCI_CAP_NEXT_PTR); 8595295Srandyf } 8605295Srandyf 8615295Srandyf if (cap_ptr == PCI_CAP_NEXT_PTR_NULL) { 8625295Srandyf return (DDI_FAILURE); 8635295Srandyf } 8645295Srandyf *pmcap_offsetp = cap_ptr; 8655295Srandyf return (DDI_SUCCESS); 8665295Srandyf } 8675295Srandyf 8685295Srandyf /* 8695295Srandyf * Do common pci-specific suspend actions: 8705295Srandyf * - enable wakeup if appropriate for the device 8715295Srandyf * - put device in lowest D-state that supports wakeup, or D3 if none 8725295Srandyf * - turn off bus mastering in control register 8735295Srandyf * For lack of per-dip storage (parent private date is pretty busy) 8745295Srandyf * we use properties to store the necessary context 8755295Srandyf * To avoid grotting through pci config space on every suspend, 8765295Srandyf * we leave the prop in existence after resume, cause we know that 8775295Srandyf * the detach framework code will dispose of it for us. 8785295Srandyf */ 8795295Srandyf 8805295Srandyf typedef struct pci_pm_context { 8815295Srandyf int ppc_flags; 8825295Srandyf uint16_t ppc_cap_offset; /* offset in config space to pm cap */ 8835295Srandyf uint16_t ppc_pmcsr; /* need this too */ 8845295Srandyf uint16_t ppc_suspend_level; 8855295Srandyf } pci_pm_context_t; 8865295Srandyf 8875295Srandyf #define SAVED_PM_CONTEXT "pci-pm-context" 8885295Srandyf 8895295Srandyf /* values for ppc_flags */ 8905295Srandyf #define PPCF_NOPMCAP 1 8915295Srandyf 8925295Srandyf /* 8935295Srandyf * Handle pci-specific suspend processing 8945295Srandyf * PM CSR and PCI CMD are saved by pci_save_config_regs(). 8955295Srandyf * If device can wake up system via PME, enable it to do so 8965295Srandyf * Set device power level to lowest that can generate PME, or D3 if none can 8975295Srandyf * Turn off bus master enable in pci command register 8985295Srandyf */ 8995295Srandyf #if defined(__x86) 9005295Srandyf extern int acpi_ddi_setwake(dev_info_t *dip, int level); 9015295Srandyf #endif 9025295Srandyf 9035295Srandyf int 9045295Srandyf pci_post_suspend(dev_info_t *dip) 9055295Srandyf { 9065295Srandyf pci_pm_context_t *p; 9075295Srandyf uint16_t pmcap, pmcsr, pcicmd; 9085295Srandyf uint_t length; 9095295Srandyf int ret; 9105295Srandyf int fromprop = 1; /* source of memory *p */ 9115295Srandyf ddi_acc_handle_t hdl; 9125295Srandyf 9135295Srandyf PMD(PMD_SX, ("pci_post_suspend %s:%d\n", 9145295Srandyf ddi_driver_name(dip), ddi_get_instance(dip))) 9155295Srandyf 9165295Srandyf if (pci_save_config_regs(dip) != DDI_SUCCESS) { 9175295Srandyf return (DDI_FAILURE); 9185295Srandyf } 9195295Srandyf 9205295Srandyf if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) { 9215295Srandyf return (DDI_FAILURE); 9225295Srandyf } 9235295Srandyf 9245295Srandyf if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 9255295Srandyf DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 9265295Srandyf SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) { 9275295Srandyf p = (pci_pm_context_t *)kmem_zalloc(sizeof (*p), KM_SLEEP); 9285295Srandyf fromprop = 0; 9295295Srandyf if (pci_lookup_pmcap(dip, hdl, 9305295Srandyf &p->ppc_cap_offset) != DDI_SUCCESS) { 9315295Srandyf p->ppc_flags |= PPCF_NOPMCAP; 9325295Srandyf ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 9335295Srandyf SAVED_PM_CONTEXT, (uchar_t *)p, 9345295Srandyf sizeof (pci_pm_context_t)); 9355295Srandyf if (ret != DDI_PROP_SUCCESS) { 9365295Srandyf (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 9375295Srandyf SAVED_PM_CONTEXT); 9385295Srandyf ret = DDI_FAILURE; 9395295Srandyf } else { 9405295Srandyf ret = DDI_SUCCESS; 9415295Srandyf } 9425295Srandyf kmem_free(p, sizeof (*p)); 9435295Srandyf pci_config_teardown(&hdl); 9445295Srandyf return (DDI_SUCCESS); 9455295Srandyf } 9465295Srandyf /* 9475295Srandyf * Upon suspend, set the power level to the lowest that can 9485295Srandyf * wake the system. If none can, then set to lowest. 9495295Srandyf * XXX later we will need to check policy to see if this 9505295Srandyf * XXX device has had wakeup disabled 9515295Srandyf */ 9525295Srandyf pmcap = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCAP); 9535295Srandyf if ((pmcap & PCI_PMCAP_D3COLD_PME) != 0) 9545295Srandyf p->ppc_suspend_level = 9555295Srandyf (PCI_PMCSR_PME_EN | PCI_PMCSR_D3HOT); 9565295Srandyf else if ((pmcap & (PCI_PMCAP_D3HOT_PME | PCI_PMCAP_D2_PME)) != 9575295Srandyf 0) 9585295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D2; 9595295Srandyf else if ((pmcap & PCI_PMCAP_D1_PME) != 0) 9605295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D1; 9615295Srandyf else if ((pmcap & PCI_PMCAP_D0_PME) != 0) 9625295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D0; 9635295Srandyf else 9645295Srandyf p->ppc_suspend_level = PCI_PMCSR_D3HOT; 9655295Srandyf 9665295Srandyf /* 9675295Srandyf * we defer updating the property to catch the saved 9685295Srandyf * register values as well 9695295Srandyf */ 9705295Srandyf } 9715295Srandyf /* If we set this in kmem_zalloc'd memory, we already returned above */ 9725295Srandyf if ((p->ppc_flags & PPCF_NOPMCAP) != 0) { 9735295Srandyf ddi_prop_free(p); 9745295Srandyf pci_config_teardown(&hdl); 9755295Srandyf return (DDI_SUCCESS); 9765295Srandyf } 9775295Srandyf 9785295Srandyf 9795295Srandyf /* 9805295Srandyf * Turn off (Bus) Master Enable, since acpica will be turning off 9815295Srandyf * bus master aribitration 9825295Srandyf */ 9835295Srandyf pcicmd = pci_config_get16(hdl, PCI_CONF_COMM); 9845295Srandyf pcicmd &= ~PCI_COMM_ME; 9855295Srandyf pci_config_put16(hdl, PCI_CONF_COMM, pcicmd); 9865295Srandyf 9875295Srandyf /* 9885295Srandyf * set pm csr 9895295Srandyf */ 9905295Srandyf pmcsr = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCSR); 9915295Srandyf p->ppc_pmcsr = pmcsr; 9925295Srandyf pmcsr &= (PCI_PMCSR_STATE_MASK); 9935295Srandyf pmcsr |= (PCI_PMCSR_PME_STAT | p->ppc_suspend_level); 9945295Srandyf pci_config_put16(hdl, p->ppc_cap_offset + PCI_PMCSR, pmcsr); 9955295Srandyf 9965295Srandyf #if defined(__x86) 9975295Srandyf /* 9985295Srandyf * Arrange for platform wakeup enabling 9995295Srandyf */ 10005295Srandyf if ((p->ppc_suspend_level & PCI_PMCSR_PME_EN) != 0) { 10015295Srandyf int retval; 10025295Srandyf 10035295Srandyf retval = acpi_ddi_setwake(dip, 3); /* XXX 3 for now */ 10045295Srandyf if (retval) { 10055295Srandyf PMD(PMD_SX, ("pci_post_suspend, setwake %s@%s rets " 10065295Srandyf "%x\n", PM_NAME(dip), PM_ADDR(dip), retval)); 10075295Srandyf } 10085295Srandyf } 10095295Srandyf #endif 10105295Srandyf 10115295Srandyf /* 10125295Srandyf * Push out saved register values 10135295Srandyf */ 10145295Srandyf ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT, 10155295Srandyf (uchar_t *)p, sizeof (pci_pm_context_t)); 10165295Srandyf if (ret == DDI_PROP_SUCCESS) { 10175295Srandyf if (fromprop) 10185295Srandyf ddi_prop_free(p); 10195295Srandyf else 10205295Srandyf kmem_free(p, sizeof (*p)); 10215295Srandyf pci_config_teardown(&hdl); 10225295Srandyf return (DDI_SUCCESS); 10235295Srandyf } 10245295Srandyf /* Failed; put things back the way we found them */ 10255295Srandyf (void) pci_restore_config_regs(dip); 10265295Srandyf if (fromprop) 10275295Srandyf ddi_prop_free(p); 10285295Srandyf else 10295295Srandyf kmem_free(p, sizeof (*p)); 10305295Srandyf (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT); 10315295Srandyf pci_config_teardown(&hdl); 10325295Srandyf return (DDI_FAILURE); 10335295Srandyf } 10345295Srandyf 10355295Srandyf /* 10365295Srandyf * The inverse of pci_post_suspend; handle pci-specific resume processing 10375295Srandyf * First, turn device back on, then restore config space. 10385295Srandyf */ 10395295Srandyf 10405295Srandyf int 10415295Srandyf pci_pre_resume(dev_info_t *dip) 10425295Srandyf { 10435295Srandyf ddi_acc_handle_t hdl; 10445295Srandyf pci_pm_context_t *p; 10455295Srandyf /* E_FUNC_SET_NOT_USED */ 10465295Srandyf uint16_t pmcap, pmcsr; 10475295Srandyf int flags; 10485295Srandyf uint_t length; 10495295Srandyf clock_t drv_usectohz(clock_t microsecs); 10505295Srandyf #if defined(__x86) 10515295Srandyf uint16_t suspend_level; 10525295Srandyf #endif 10535295Srandyf 10545295Srandyf PMD(PMD_SX, ("pci_pre_resume %s:%d\n", ddi_driver_name(dip), 10555295Srandyf ddi_get_instance(dip))) 10565295Srandyf if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 10575295Srandyf DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 10585295Srandyf SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) { 10595295Srandyf return (DDI_FAILURE); 10605295Srandyf } 10615295Srandyf flags = p->ppc_flags; 10625295Srandyf pmcap = p->ppc_cap_offset; 10635295Srandyf pmcsr = p->ppc_pmcsr; 10645295Srandyf #if defined(__x86) 10655295Srandyf suspend_level = p->ppc_suspend_level; 10665295Srandyf #endif 10675295Srandyf ddi_prop_free(p); 10686996Sgs150176 if ((flags & PPCF_NOPMCAP) != 0) 10696996Sgs150176 goto done; 10705295Srandyf #if defined(__x86) 10715295Srandyf /* 10725295Srandyf * Turn platform wake enable back off 10735295Srandyf */ 10745295Srandyf if ((suspend_level & PCI_PMCSR_PME_EN) != 0) { 10755295Srandyf int retval; 10765295Srandyf 10775295Srandyf retval = acpi_ddi_setwake(dip, 0); /* 0 for now */ 10785295Srandyf if (retval) { 10795295Srandyf PMD(PMD_SX, ("pci_pre_resume, setwake %s@%s rets " 10805295Srandyf "%x\n", PM_NAME(dip), PM_ADDR(dip), retval)); 10815295Srandyf } 10825295Srandyf } 10835295Srandyf #endif 10845295Srandyf if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) { 10855295Srandyf return (DDI_FAILURE); 10865295Srandyf } 10875295Srandyf pci_config_put16(hdl, pmcap + PCI_PMCSR, pmcsr); 10885295Srandyf delay(drv_usectohz(10000)); /* PCI PM spec D3->D0 (10ms) */ 10895295Srandyf pci_config_teardown(&hdl); 10906996Sgs150176 done: 10915295Srandyf (void) pci_restore_config_regs(dip); /* fudges D-state! */ 10925295Srandyf return (DDI_SUCCESS); 10935295Srandyf } 1094