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 { 3256996Sgs150176 peekpoke_ctlops_t cautacc_ctlops_arg; 3260Sstevel@tonic-gate ddi_acc_handle_t confhdl; 3270Sstevel@tonic-gate pci_config_header_state_t *chsp; 3280Sstevel@tonic-gate pci_cap_save_desc_t *pci_cap_descp; 3290Sstevel@tonic-gate int ret; 3300Sstevel@tonic-gate uint32_t i, ncaps, nwords; 3310Sstevel@tonic-gate uint32_t *regbuf, *p; 3320Sstevel@tonic-gate uint8_t *maskbuf; 3330Sstevel@tonic-gate size_t maskbufsz, regbufsz, capbufsz; 3340Sstevel@tonic-gate ddi_acc_hdl_t *hp; 3350Sstevel@tonic-gate off_t offset = 0; 3360Sstevel@tonic-gate uint8_t cap_ptr, cap_id; 3370Sstevel@tonic-gate int pcie = 0; 338*7168Syf149591 uint16_t status; 339*7168Syf149591 3405295Srandyf PMD(PMD_SX, ("pci_save_config_regs %s:%d\n", ddi_driver_name(dip), 3415295Srandyf ddi_get_instance(dip))) 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 3440Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't get config handle", 3455295Srandyf ddi_driver_name(dip), ddi_get_instance(dip)); 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate return (DDI_FAILURE); 3480Sstevel@tonic-gate } 349*7168Syf149591 350*7168Syf149591 /* 351*7168Syf149591 * Determine if it implements capabilities 352*7168Syf149591 */ 353*7168Syf149591 status = pci_config_get16(confhdl, PCI_CONF_STAT); 354*7168Syf149591 if (!(status & 0x10)) { 355*7168Syf149591 goto no_cap; 356*7168Syf149591 } 3570Sstevel@tonic-gate /* 3580Sstevel@tonic-gate * Determine if it is a pci express device. If it is, save entire 3590Sstevel@tonic-gate * 4k config space treating it as a array of 32 bit integers. 3600Sstevel@tonic-gate * If it is not, do it in a usual PCI way. 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 3630Sstevel@tonic-gate /* 3640Sstevel@tonic-gate * Walk the capabilities searching for pci express capability 3650Sstevel@tonic-gate */ 3660Sstevel@tonic-gate while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 3670Sstevel@tonic-gate cap_id = pci_config_get8(confhdl, 3680Sstevel@tonic-gate cap_ptr + PCI_CAP_ID); 3690Sstevel@tonic-gate if (cap_id == PCI_CAP_ID_PCI_E) { 3700Sstevel@tonic-gate pcie = 1; 3710Sstevel@tonic-gate break; 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, 3740Sstevel@tonic-gate cap_ptr + PCI_CAP_NEXT_PTR); 3750Sstevel@tonic-gate } 376*7168Syf149591 no_cap: 3770Sstevel@tonic-gate if (pcie) { 3780Sstevel@tonic-gate /* PCI express device. Can have data in all 4k space */ 3790Sstevel@tonic-gate regbuf = (uint32_t *)kmem_zalloc((size_t)PCIE_CONF_HDR_SIZE, 3805295Srandyf KM_SLEEP); 3810Sstevel@tonic-gate p = regbuf; 3820Sstevel@tonic-gate /* 3830Sstevel@tonic-gate * Allocate space for mask. 3840Sstevel@tonic-gate * mask size is 128 bytes (4096 / 4 / 8 ) 3850Sstevel@tonic-gate */ 3860Sstevel@tonic-gate maskbufsz = (size_t)((PCIE_CONF_HDR_SIZE/ sizeof (uint32_t)) >> 3870Sstevel@tonic-gate INDEX_SHIFT); 3880Sstevel@tonic-gate maskbuf = (uint8_t *)kmem_zalloc(maskbufsz, KM_SLEEP); 3890Sstevel@tonic-gate hp = impl_acc_hdl_get(confhdl); 3906996Sgs150176 cautacc_ctlops_arg.size = sizeof (uint32_t); 3916996Sgs150176 cautacc_ctlops_arg.handle = confhdl; 3926996Sgs150176 cautacc_ctlops_arg.repcount = 1; 3936996Sgs150176 cautacc_ctlops_arg.flags = 0; 3940Sstevel@tonic-gate for (i = 0; i < (PCIE_CONF_HDR_SIZE / sizeof (uint32_t)); i++) { 3956996Sgs150176 cautacc_ctlops_arg.dev_addr = (uintptr_t)(hp->ah_addr + 3966996Sgs150176 offset); 3976996Sgs150176 cautacc_ctlops_arg.host_addr = (uintptr_t)p; 3986996Sgs150176 ret = ddi_ctlops(dip, dip, DDI_CTLOPS_PEEK, 3996996Sgs150176 &cautacc_ctlops_arg, NULL); 4006996Sgs150176 if (ret == DDI_SUCCESS) { 4010Sstevel@tonic-gate /* it is readable register. set the bit */ 4020Sstevel@tonic-gate maskbuf[i >> INDEX_SHIFT] |= 4030Sstevel@tonic-gate (uint8_t)(1 << (i & BITMASK)); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate p++; 4060Sstevel@tonic-gate offset += sizeof (uint32_t); 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 4100Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK, (uchar_t *)maskbuf, 4110Sstevel@tonic-gate maskbufsz)) != DDI_PROP_SUCCESS) { 4120Sstevel@tonic-gate cmn_err(CE_WARN, "couldn't create %s property while" 4130Sstevel@tonic-gate "saving config space for %s@%d\n", 4140Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK, ddi_driver_name(dip), 4150Sstevel@tonic-gate ddi_get_instance(dip)); 4160Sstevel@tonic-gate } else if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, 4170Sstevel@tonic-gate dip, SAVED_CONFIG_REGS, (uchar_t *)regbuf, 4180Sstevel@tonic-gate (size_t)PCIE_CONF_HDR_SIZE)) != DDI_PROP_SUCCESS) { 4190Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 4200Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 4210Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't update prop %s", 4220Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 4230Sstevel@tonic-gate SAVED_CONFIG_REGS); 4240Sstevel@tonic-gate } 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate kmem_free(maskbuf, (size_t)maskbufsz); 4270Sstevel@tonic-gate kmem_free(regbuf, (size_t)PCIE_CONF_HDR_SIZE); 4280Sstevel@tonic-gate } else { 4290Sstevel@tonic-gate regbuf = (uint32_t *)kmem_zalloc((size_t)PCI_CONF_HDR_SIZE, 4305295Srandyf KM_SLEEP); 4310Sstevel@tonic-gate chsp = (pci_config_header_state_t *)regbuf; 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate chsp->chs_command = pci_config_get16(confhdl, PCI_CONF_COMM); 4340Sstevel@tonic-gate chsp->chs_header_type = pci_config_get8(confhdl, 4355295Srandyf PCI_CONF_HEADER); 4360Sstevel@tonic-gate if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 4370Sstevel@tonic-gate PCI_HEADER_ONE) 4380Sstevel@tonic-gate chsp->chs_bridge_control = 4390Sstevel@tonic-gate pci_config_get16(confhdl, PCI_BCNF_BCNTRL); 4400Sstevel@tonic-gate chsp->chs_cache_line_size = pci_config_get8(confhdl, 4410Sstevel@tonic-gate PCI_CONF_CACHE_LINESZ); 4420Sstevel@tonic-gate chsp->chs_latency_timer = pci_config_get8(confhdl, 4430Sstevel@tonic-gate PCI_CONF_LATENCY_TIMER); 4440Sstevel@tonic-gate if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 4450Sstevel@tonic-gate PCI_HEADER_ONE) { 4460Sstevel@tonic-gate chsp->chs_sec_latency_timer = 4470Sstevel@tonic-gate pci_config_get8(confhdl, PCI_BCNF_LATENCY_TIMER); 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate chsp->chs_base0 = pci_config_get32(confhdl, PCI_CONF_BASE0); 4510Sstevel@tonic-gate chsp->chs_base1 = pci_config_get32(confhdl, PCI_CONF_BASE1); 4520Sstevel@tonic-gate chsp->chs_base2 = pci_config_get32(confhdl, PCI_CONF_BASE2); 4530Sstevel@tonic-gate chsp->chs_base3 = pci_config_get32(confhdl, PCI_CONF_BASE3); 4540Sstevel@tonic-gate chsp->chs_base4 = pci_config_get32(confhdl, PCI_CONF_BASE4); 4550Sstevel@tonic-gate chsp->chs_base5 = pci_config_get32(confhdl, PCI_CONF_BASE5); 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate /* 4580Sstevel@tonic-gate * Allocate maximum space required for capability descriptions. 4590Sstevel@tonic-gate * The maximum number of capabilties saved is the number of 4600Sstevel@tonic-gate * capabilities listed in the pci_cap_table. 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate ncaps = (sizeof (pci_cap_table) / sizeof (pci_cap_entry_t)); 4630Sstevel@tonic-gate capbufsz = ncaps * sizeof (pci_cap_save_desc_t); 4640Sstevel@tonic-gate pci_cap_descp = (pci_cap_save_desc_t *)kmem_zalloc( 4650Sstevel@tonic-gate capbufsz, KM_SLEEP); 4660Sstevel@tonic-gate p = (uint32_t *)((caddr_t)regbuf + 4670Sstevel@tonic-gate sizeof (pci_config_header_state_t)); 4680Sstevel@tonic-gate nwords = pci_save_caps(confhdl, p, pci_cap_descp, &ncaps); 4690Sstevel@tonic-gate regbufsz = sizeof (pci_config_header_state_t) + 4700Sstevel@tonic-gate nwords * sizeof (uint32_t); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 4730Sstevel@tonic-gate SAVED_CONFIG_REGS, (uchar_t *)regbuf, regbufsz)) != 4740Sstevel@tonic-gate DDI_PROP_SUCCESS) { 4750Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't update prop %s", 4760Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 4770Sstevel@tonic-gate SAVED_CONFIG_REGS); 4780Sstevel@tonic-gate } else if (ncaps) { 4790Sstevel@tonic-gate ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 4800Sstevel@tonic-gate SAVED_CONFIG_REGS_CAPINFO, (uchar_t *)pci_cap_descp, 4810Sstevel@tonic-gate ncaps * sizeof (pci_cap_save_desc_t)); 4820Sstevel@tonic-gate if (ret != DDI_PROP_SUCCESS) 4830Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 4840Sstevel@tonic-gate SAVED_CONFIG_REGS); 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate kmem_free(regbuf, (size_t)PCI_CONF_HDR_SIZE); 4870Sstevel@tonic-gate kmem_free(pci_cap_descp, capbufsz); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate pci_config_teardown(&confhdl); 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate if (ret != DDI_PROP_SUCCESS) 4920Sstevel@tonic-gate return (DDI_FAILURE); 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate return (DDI_SUCCESS); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate /* 4980Sstevel@tonic-gate * Saves registers associated with PCI capabilities. 4990Sstevel@tonic-gate * Returns number of 32 bit words saved. 5000Sstevel@tonic-gate * Number of capabilities saved is returned in ncapsp. 5010Sstevel@tonic-gate */ 5020Sstevel@tonic-gate static uint32_t 5030Sstevel@tonic-gate pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 5040Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp) 5050Sstevel@tonic-gate { 5060Sstevel@tonic-gate return (cap_walk_and_save(confhdl, regbuf, cap_descp, ncapsp, 0)); 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate static uint32_t 5100Sstevel@tonic-gate cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf, 5110Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace) 5120Sstevel@tonic-gate { 5130Sstevel@tonic-gate pci_cap_entry_t *pci_cap_entp; 514*7168Syf149591 uint16_t cap_id, offset, status; 5150Sstevel@tonic-gate uint32_t words_saved = 0, nwords = 0; 5160Sstevel@tonic-gate uint16_t cap_ptr = PCI_CAP_NEXT_PTR_NULL; 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate *ncapsp = 0; 519*7168Syf149591 520*7168Syf149591 /* 521*7168Syf149591 * Determine if it implements capabilities 522*7168Syf149591 */ 523*7168Syf149591 status = pci_config_get16(confhdl, PCI_CONF_STAT); 524*7168Syf149591 if (!(status & 0x10)) { 525*7168Syf149591 return (words_saved); 526*7168Syf149591 } 527*7168Syf149591 5280Sstevel@tonic-gate if (!xspace) 5290Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 5300Sstevel@tonic-gate /* 5310Sstevel@tonic-gate * Walk the capabilities 5320Sstevel@tonic-gate */ 5330Sstevel@tonic-gate while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 5340Sstevel@tonic-gate cap_id = CAP_ID(confhdl, cap_ptr, xspace); 5350Sstevel@tonic-gate /* Search for this cap id in our table */ 5360Sstevel@tonic-gate if (!xspace) 5370Sstevel@tonic-gate pci_cap_entp = pci_cap_table; 5380Sstevel@tonic-gate while (pci_cap_entp->cap_id != PCI_CAP_NEXT_PTR_NULL && 5390Sstevel@tonic-gate pci_cap_entp->cap_id != cap_id) 5400Sstevel@tonic-gate pci_cap_entp++; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate offset = cap_ptr; 5430Sstevel@tonic-gate cap_ptr = NEXT_CAP(confhdl, cap_ptr, xspace); 5440Sstevel@tonic-gate /* 5450Sstevel@tonic-gate * If this cap id is not found in the table, there is nothing 5460Sstevel@tonic-gate * to save. 5470Sstevel@tonic-gate */ 5480Sstevel@tonic-gate if (pci_cap_entp->cap_id == PCI_CAP_NEXT_PTR_NULL) 5490Sstevel@tonic-gate continue; 5500Sstevel@tonic-gate if (pci_cap_entp->cap_save_func) { 5510Sstevel@tonic-gate if ((nwords = pci_cap_entp->cap_save_func(confhdl, 5520Sstevel@tonic-gate offset, regbuf, pci_cap_entp->cap_ndwords))) { 5530Sstevel@tonic-gate cap_descp->cap_nregs = nwords; 5540Sstevel@tonic-gate cap_descp->cap_offset = offset; 5550Sstevel@tonic-gate cap_descp->cap_id = cap_id; 5560Sstevel@tonic-gate regbuf += nwords; 5570Sstevel@tonic-gate cap_descp++; 5580Sstevel@tonic-gate words_saved += nwords; 5590Sstevel@tonic-gate (*ncapsp)++; 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate } 5640Sstevel@tonic-gate return (words_saved); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate static void 5680Sstevel@tonic-gate pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 5690Sstevel@tonic-gate uint32_t *regbuf, uint32_t nwords) 5700Sstevel@tonic-gate { 5710Sstevel@tonic-gate int i; 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate for (i = 0; i < nwords; i++) { 5740Sstevel@tonic-gate *regbuf = pci_config_get32(confhdl, cap_ptr); 5750Sstevel@tonic-gate regbuf++; 5760Sstevel@tonic-gate cap_ptr += 4; 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate static uint32_t 5810Sstevel@tonic-gate pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 5820Sstevel@tonic-gate uint32_t nwords) 5830Sstevel@tonic-gate { 5840Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 5850Sstevel@tonic-gate return (nwords); 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate 5880Sstevel@tonic-gate /*ARGSUSED*/ 5890Sstevel@tonic-gate static uint32_t 5900Sstevel@tonic-gate pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 5910Sstevel@tonic-gate uint32_t notused) 5920Sstevel@tonic-gate { 5930Sstevel@tonic-gate uint32_t nwords = PCI_MSI_MIN_WORDS; 5940Sstevel@tonic-gate uint16_t msi_ctrl; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate /* Figure out how many registers to be saved */ 5970Sstevel@tonic-gate msi_ctrl = pci_config_get16(confhdl, cap_ptr + PCI_MSI_CTRL); 5980Sstevel@tonic-gate /* If 64 bit address capable add one word */ 5990Sstevel@tonic-gate if (msi_ctrl & PCI_MSI_64BIT_MASK) 6000Sstevel@tonic-gate nwords++; 6010Sstevel@tonic-gate /* If per vector masking capable, add two more words */ 6020Sstevel@tonic-gate if (msi_ctrl & PCI_MSI_PVM_MASK) 6030Sstevel@tonic-gate nwords += 2; 6040Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate return (nwords); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate /*ARGSUSED*/ 6100Sstevel@tonic-gate static uint32_t 6110Sstevel@tonic-gate pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 6120Sstevel@tonic-gate uint32_t notused) 6130Sstevel@tonic-gate { 6140Sstevel@tonic-gate uint32_t nwords = PCI_PCIX_MIN_WORDS; 6150Sstevel@tonic-gate uint16_t pcix_command; 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate /* Figure out how many registers to be saved */ 6180Sstevel@tonic-gate pcix_command = pci_config_get16(confhdl, cap_ptr + PCI_PCIX_COMMAND); 6190Sstevel@tonic-gate /* If it is version 1 or version 2, add 4 words */ 6200Sstevel@tonic-gate if (((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_1) || 6210Sstevel@tonic-gate ((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_2)) 6220Sstevel@tonic-gate nwords += 4; 6230Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate return (nwords); 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate /*ARGSUSED*/ 6290Sstevel@tonic-gate static uint32_t 6300Sstevel@tonic-gate pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 6310Sstevel@tonic-gate uint32_t notused) 6320Sstevel@tonic-gate { 6330Sstevel@tonic-gate return (0); 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate static void 6370Sstevel@tonic-gate pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf, 6380Sstevel@tonic-gate uint16_t pmcap_offset) 6390Sstevel@tonic-gate { 6400Sstevel@tonic-gate uint16_t pmcsr; 6410Sstevel@tonic-gate uint16_t pmcsr_offset = pmcap_offset + PCI_PMCSR; 6420Sstevel@tonic-gate uint32_t *saved_pmcsrp = (uint32_t *)((caddr_t)regbuf + PCI_PMCSR); 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* 6450Sstevel@tonic-gate * Copy the power state bits from the PMCSR to our saved copy. 6460Sstevel@tonic-gate * This is to make sure that we don't change the D state when 6470Sstevel@tonic-gate * we restore config space of the device. 6480Sstevel@tonic-gate */ 6490Sstevel@tonic-gate pmcsr = pci_config_get16(confhdl, pmcsr_offset); 6500Sstevel@tonic-gate (*saved_pmcsrp) &= ~PCI_PMCSR_STATE_MASK; 6510Sstevel@tonic-gate (*saved_pmcsrp) |= (pmcsr & PCI_PMCSR_STATE_MASK); 6520Sstevel@tonic-gate } 6530Sstevel@tonic-gate 6540Sstevel@tonic-gate static void 6550Sstevel@tonic-gate pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 6560Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t elements) 6570Sstevel@tonic-gate { 6580Sstevel@tonic-gate int i, j; 6590Sstevel@tonic-gate uint16_t offset; 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate for (i = 0; i < (elements / sizeof (pci_cap_save_desc_t)); i++) { 6620Sstevel@tonic-gate offset = cap_descp->cap_offset; 6630Sstevel@tonic-gate if (cap_descp->cap_id == PCI_CAP_ID_PM) 6640Sstevel@tonic-gate pci_pmcap_check(confhdl, regbuf, offset); 6650Sstevel@tonic-gate for (j = 0; j < cap_descp->cap_nregs; j++) { 6660Sstevel@tonic-gate pci_config_put32(confhdl, offset, *regbuf); 6670Sstevel@tonic-gate regbuf++; 6680Sstevel@tonic-gate offset += 4; 6690Sstevel@tonic-gate } 6700Sstevel@tonic-gate cap_descp++; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate } 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate /* 6750Sstevel@tonic-gate * Restore config_regs from a single devinfo node. 6760Sstevel@tonic-gate */ 6770Sstevel@tonic-gate int 6780Sstevel@tonic-gate pci_restore_config_regs(dev_info_t *dip) 6790Sstevel@tonic-gate { 6800Sstevel@tonic-gate ddi_acc_handle_t confhdl; 6810Sstevel@tonic-gate pci_config_header_state_t *chs_p; 6820Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp; 6830Sstevel@tonic-gate uint32_t elements, i; 6840Sstevel@tonic-gate uint8_t *maskbuf; 6850Sstevel@tonic-gate uint32_t *regbuf, *p; 6860Sstevel@tonic-gate off_t offset = 0; 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 6890Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't get config handle", 6900Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 6910Sstevel@tonic-gate return (DDI_FAILURE); 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 6950Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS_MASK, 6960Sstevel@tonic-gate (uchar_t **)&maskbuf, &elements) == DDI_PROP_SUCCESS) { 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 6990Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 7000Sstevel@tonic-gate (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 7010Sstevel@tonic-gate goto restoreconfig_err; 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate ASSERT(elements == PCIE_CONF_HDR_SIZE); 7040Sstevel@tonic-gate /* pcie device and has 4k config space saved */ 7050Sstevel@tonic-gate p = regbuf; 7060Sstevel@tonic-gate for (i = 0; i < PCIE_CONF_HDR_SIZE / sizeof (uint32_t); i++) { 7070Sstevel@tonic-gate /* If the word is readable then restore it */ 7080Sstevel@tonic-gate if (maskbuf[i >> INDEX_SHIFT] & 7090Sstevel@tonic-gate (uint8_t)(1 << (i & BITMASK))) 7100Sstevel@tonic-gate pci_config_put32(confhdl, offset, *p); 7110Sstevel@tonic-gate p++; 7120Sstevel@tonic-gate offset += sizeof (uint32_t); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate ddi_prop_free(regbuf); 7150Sstevel@tonic-gate ddi_prop_free(maskbuf); 7160Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, 7170Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK) != DDI_PROP_SUCCESS) { 7180Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 7190Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 7200Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 7210Sstevel@tonic-gate } 7220Sstevel@tonic-gate } else { 7230Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 7240Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 7250Sstevel@tonic-gate (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate pci_config_teardown(&confhdl); 7286996Sgs150176 return (DDI_SUCCESS); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate 7310Sstevel@tonic-gate chs_p = (pci_config_header_state_t *)regbuf; 7320Sstevel@tonic-gate pci_config_put16(confhdl, PCI_CONF_COMM, 7330Sstevel@tonic-gate chs_p->chs_command); 7340Sstevel@tonic-gate if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 7350Sstevel@tonic-gate PCI_HEADER_ONE) { 7360Sstevel@tonic-gate pci_config_put16(confhdl, PCI_BCNF_BCNTRL, 7370Sstevel@tonic-gate chs_p->chs_bridge_control); 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate pci_config_put8(confhdl, PCI_CONF_CACHE_LINESZ, 7400Sstevel@tonic-gate chs_p->chs_cache_line_size); 7410Sstevel@tonic-gate pci_config_put8(confhdl, PCI_CONF_LATENCY_TIMER, 7420Sstevel@tonic-gate chs_p->chs_latency_timer); 7430Sstevel@tonic-gate if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 7440Sstevel@tonic-gate PCI_HEADER_ONE) 7450Sstevel@tonic-gate pci_config_put8(confhdl, PCI_BCNF_LATENCY_TIMER, 7460Sstevel@tonic-gate chs_p->chs_sec_latency_timer); 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE0, chs_p->chs_base0); 7490Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE1, chs_p->chs_base1); 7500Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE2, chs_p->chs_base2); 7510Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE3, chs_p->chs_base3); 7520Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE4, chs_p->chs_base4); 7530Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE5, chs_p->chs_base5); 7540Sstevel@tonic-gate 7550Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 7560Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 7570Sstevel@tonic-gate SAVED_CONFIG_REGS_CAPINFO, 7580Sstevel@tonic-gate (uchar_t **)&cap_descp, &elements) == DDI_PROP_SUCCESS) { 7590Sstevel@tonic-gate /* 7600Sstevel@tonic-gate * PCI capability related regsiters are saved. 7610Sstevel@tonic-gate * Restore them based on the description. 7620Sstevel@tonic-gate */ 7630Sstevel@tonic-gate p = (uint32_t *)((caddr_t)regbuf + 7640Sstevel@tonic-gate sizeof (pci_config_header_state_t)); 7650Sstevel@tonic-gate pci_restore_caps(confhdl, p, cap_descp, elements); 7660Sstevel@tonic-gate ddi_prop_free(cap_descp); 7670Sstevel@tonic-gate } 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate ddi_prop_free(regbuf); 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate /* 7730Sstevel@tonic-gate * Make sure registers are flushed 7740Sstevel@tonic-gate */ 7750Sstevel@tonic-gate (void) pci_config_get32(confhdl, PCI_CONF_BASE5); 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS) != 7790Sstevel@tonic-gate DDI_PROP_SUCCESS) { 7800Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 7810Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 7820Sstevel@tonic-gate SAVED_CONFIG_REGS); 7830Sstevel@tonic-gate } 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate pci_config_teardown(&confhdl); 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate return (DDI_SUCCESS); 7880Sstevel@tonic-gate 7890Sstevel@tonic-gate restoreconfig_err: 7900Sstevel@tonic-gate ddi_prop_free(maskbuf); 7910Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS_MASK) != 7920Sstevel@tonic-gate DDI_PROP_SUCCESS) { 7930Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 7940Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 7950Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate pci_config_teardown(&confhdl); 7980Sstevel@tonic-gate return (DDI_FAILURE); 7990Sstevel@tonic-gate } 8005295Srandyf 8015295Srandyf /*ARGSUSED*/ 8025295Srandyf static int 8035295Srandyf pci_lookup_pmcap(dev_info_t *dip, ddi_acc_handle_t conf_hdl, 8045295Srandyf uint16_t *pmcap_offsetp) 8055295Srandyf { 8065295Srandyf uint8_t cap_ptr; 8075295Srandyf uint8_t cap_id; 8085295Srandyf uint8_t header_type; 8095295Srandyf uint16_t status; 8105295Srandyf 8115295Srandyf header_type = pci_config_get8(conf_hdl, PCI_CONF_HEADER); 8125295Srandyf header_type &= PCI_HEADER_TYPE_M; 8135295Srandyf 8145295Srandyf /* we don't deal with bridges, etc here */ 8155295Srandyf if (header_type != PCI_HEADER_ZERO) { 8165295Srandyf return (DDI_FAILURE); 8175295Srandyf } 8185295Srandyf 8195295Srandyf status = pci_config_get16(conf_hdl, PCI_CONF_STAT); 8205295Srandyf if ((status & PCI_STAT_CAP) == 0) { 8215295Srandyf return (DDI_FAILURE); 8225295Srandyf } 8235295Srandyf 8245295Srandyf cap_ptr = pci_config_get8(conf_hdl, PCI_CONF_CAP_PTR); 8255295Srandyf 8265295Srandyf /* 8275295Srandyf * Walk the capabilities searching for a PM entry. 8285295Srandyf */ 8295295Srandyf while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 8305295Srandyf cap_id = pci_config_get8(conf_hdl, cap_ptr + PCI_CAP_ID); 8315295Srandyf if (cap_id == PCI_CAP_ID_PM) { 8325295Srandyf break; 8335295Srandyf } 8345295Srandyf cap_ptr = pci_config_get8(conf_hdl, 8355295Srandyf cap_ptr + PCI_CAP_NEXT_PTR); 8365295Srandyf } 8375295Srandyf 8385295Srandyf if (cap_ptr == PCI_CAP_NEXT_PTR_NULL) { 8395295Srandyf return (DDI_FAILURE); 8405295Srandyf } 8415295Srandyf *pmcap_offsetp = cap_ptr; 8425295Srandyf return (DDI_SUCCESS); 8435295Srandyf } 8445295Srandyf 8455295Srandyf /* 8465295Srandyf * Do common pci-specific suspend actions: 8475295Srandyf * - enable wakeup if appropriate for the device 8485295Srandyf * - put device in lowest D-state that supports wakeup, or D3 if none 8495295Srandyf * - turn off bus mastering in control register 8505295Srandyf * For lack of per-dip storage (parent private date is pretty busy) 8515295Srandyf * we use properties to store the necessary context 8525295Srandyf * To avoid grotting through pci config space on every suspend, 8535295Srandyf * we leave the prop in existence after resume, cause we know that 8545295Srandyf * the detach framework code will dispose of it for us. 8555295Srandyf */ 8565295Srandyf 8575295Srandyf typedef struct pci_pm_context { 8585295Srandyf int ppc_flags; 8595295Srandyf uint16_t ppc_cap_offset; /* offset in config space to pm cap */ 8605295Srandyf uint16_t ppc_pmcsr; /* need this too */ 8615295Srandyf uint16_t ppc_suspend_level; 8625295Srandyf } pci_pm_context_t; 8635295Srandyf 8645295Srandyf #define SAVED_PM_CONTEXT "pci-pm-context" 8655295Srandyf 8665295Srandyf /* values for ppc_flags */ 8675295Srandyf #define PPCF_NOPMCAP 1 8685295Srandyf 8695295Srandyf /* 8705295Srandyf * Handle pci-specific suspend processing 8715295Srandyf * PM CSR and PCI CMD are saved by pci_save_config_regs(). 8725295Srandyf * If device can wake up system via PME, enable it to do so 8735295Srandyf * Set device power level to lowest that can generate PME, or D3 if none can 8745295Srandyf * Turn off bus master enable in pci command register 8755295Srandyf */ 8765295Srandyf #if defined(__x86) 8775295Srandyf extern int acpi_ddi_setwake(dev_info_t *dip, int level); 8785295Srandyf #endif 8795295Srandyf 8805295Srandyf int 8815295Srandyf pci_post_suspend(dev_info_t *dip) 8825295Srandyf { 8835295Srandyf pci_pm_context_t *p; 8845295Srandyf uint16_t pmcap, pmcsr, pcicmd; 8855295Srandyf uint_t length; 8865295Srandyf int ret; 8875295Srandyf int fromprop = 1; /* source of memory *p */ 8885295Srandyf ddi_acc_handle_t hdl; 8895295Srandyf 8905295Srandyf PMD(PMD_SX, ("pci_post_suspend %s:%d\n", 8915295Srandyf ddi_driver_name(dip), ddi_get_instance(dip))) 8925295Srandyf 8935295Srandyf if (pci_save_config_regs(dip) != DDI_SUCCESS) { 8945295Srandyf return (DDI_FAILURE); 8955295Srandyf } 8965295Srandyf 8975295Srandyf if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) { 8985295Srandyf return (DDI_FAILURE); 8995295Srandyf } 9005295Srandyf 9015295Srandyf if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 9025295Srandyf DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 9035295Srandyf SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) { 9045295Srandyf p = (pci_pm_context_t *)kmem_zalloc(sizeof (*p), KM_SLEEP); 9055295Srandyf fromprop = 0; 9065295Srandyf if (pci_lookup_pmcap(dip, hdl, 9075295Srandyf &p->ppc_cap_offset) != DDI_SUCCESS) { 9085295Srandyf p->ppc_flags |= PPCF_NOPMCAP; 9095295Srandyf ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 9105295Srandyf SAVED_PM_CONTEXT, (uchar_t *)p, 9115295Srandyf sizeof (pci_pm_context_t)); 9125295Srandyf if (ret != DDI_PROP_SUCCESS) { 9135295Srandyf (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 9145295Srandyf SAVED_PM_CONTEXT); 9155295Srandyf ret = DDI_FAILURE; 9165295Srandyf } else { 9175295Srandyf ret = DDI_SUCCESS; 9185295Srandyf } 9195295Srandyf kmem_free(p, sizeof (*p)); 9205295Srandyf pci_config_teardown(&hdl); 9215295Srandyf return (DDI_SUCCESS); 9225295Srandyf } 9235295Srandyf /* 9245295Srandyf * Upon suspend, set the power level to the lowest that can 9255295Srandyf * wake the system. If none can, then set to lowest. 9265295Srandyf * XXX later we will need to check policy to see if this 9275295Srandyf * XXX device has had wakeup disabled 9285295Srandyf */ 9295295Srandyf pmcap = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCAP); 9305295Srandyf if ((pmcap & PCI_PMCAP_D3COLD_PME) != 0) 9315295Srandyf p->ppc_suspend_level = 9325295Srandyf (PCI_PMCSR_PME_EN | PCI_PMCSR_D3HOT); 9335295Srandyf else if ((pmcap & (PCI_PMCAP_D3HOT_PME | PCI_PMCAP_D2_PME)) != 9345295Srandyf 0) 9355295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D2; 9365295Srandyf else if ((pmcap & PCI_PMCAP_D1_PME) != 0) 9375295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D1; 9385295Srandyf else if ((pmcap & PCI_PMCAP_D0_PME) != 0) 9395295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D0; 9405295Srandyf else 9415295Srandyf p->ppc_suspend_level = PCI_PMCSR_D3HOT; 9425295Srandyf 9435295Srandyf /* 9445295Srandyf * we defer updating the property to catch the saved 9455295Srandyf * register values as well 9465295Srandyf */ 9475295Srandyf } 9485295Srandyf /* If we set this in kmem_zalloc'd memory, we already returned above */ 9495295Srandyf if ((p->ppc_flags & PPCF_NOPMCAP) != 0) { 9505295Srandyf ddi_prop_free(p); 9515295Srandyf pci_config_teardown(&hdl); 9525295Srandyf return (DDI_SUCCESS); 9535295Srandyf } 9545295Srandyf 9555295Srandyf 9565295Srandyf /* 9575295Srandyf * Turn off (Bus) Master Enable, since acpica will be turning off 9585295Srandyf * bus master aribitration 9595295Srandyf */ 9605295Srandyf pcicmd = pci_config_get16(hdl, PCI_CONF_COMM); 9615295Srandyf pcicmd &= ~PCI_COMM_ME; 9625295Srandyf pci_config_put16(hdl, PCI_CONF_COMM, pcicmd); 9635295Srandyf 9645295Srandyf /* 9655295Srandyf * set pm csr 9665295Srandyf */ 9675295Srandyf pmcsr = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCSR); 9685295Srandyf p->ppc_pmcsr = pmcsr; 9695295Srandyf pmcsr &= (PCI_PMCSR_STATE_MASK); 9705295Srandyf pmcsr |= (PCI_PMCSR_PME_STAT | p->ppc_suspend_level); 9715295Srandyf pci_config_put16(hdl, p->ppc_cap_offset + PCI_PMCSR, pmcsr); 9725295Srandyf 9735295Srandyf #if defined(__x86) 9745295Srandyf /* 9755295Srandyf * Arrange for platform wakeup enabling 9765295Srandyf */ 9775295Srandyf if ((p->ppc_suspend_level & PCI_PMCSR_PME_EN) != 0) { 9785295Srandyf int retval; 9795295Srandyf 9805295Srandyf retval = acpi_ddi_setwake(dip, 3); /* XXX 3 for now */ 9815295Srandyf if (retval) { 9825295Srandyf PMD(PMD_SX, ("pci_post_suspend, setwake %s@%s rets " 9835295Srandyf "%x\n", PM_NAME(dip), PM_ADDR(dip), retval)); 9845295Srandyf } 9855295Srandyf } 9865295Srandyf #endif 9875295Srandyf 9885295Srandyf /* 9895295Srandyf * Push out saved register values 9905295Srandyf */ 9915295Srandyf ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT, 9925295Srandyf (uchar_t *)p, sizeof (pci_pm_context_t)); 9935295Srandyf if (ret == DDI_PROP_SUCCESS) { 9945295Srandyf if (fromprop) 9955295Srandyf ddi_prop_free(p); 9965295Srandyf else 9975295Srandyf kmem_free(p, sizeof (*p)); 9985295Srandyf pci_config_teardown(&hdl); 9995295Srandyf return (DDI_SUCCESS); 10005295Srandyf } 10015295Srandyf /* Failed; put things back the way we found them */ 10025295Srandyf (void) pci_restore_config_regs(dip); 10035295Srandyf if (fromprop) 10045295Srandyf ddi_prop_free(p); 10055295Srandyf else 10065295Srandyf kmem_free(p, sizeof (*p)); 10075295Srandyf (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT); 10085295Srandyf pci_config_teardown(&hdl); 10095295Srandyf return (DDI_FAILURE); 10105295Srandyf } 10115295Srandyf 10125295Srandyf /* 10135295Srandyf * The inverse of pci_post_suspend; handle pci-specific resume processing 10145295Srandyf * First, turn device back on, then restore config space. 10155295Srandyf */ 10165295Srandyf 10175295Srandyf int 10185295Srandyf pci_pre_resume(dev_info_t *dip) 10195295Srandyf { 10205295Srandyf ddi_acc_handle_t hdl; 10215295Srandyf pci_pm_context_t *p; 10225295Srandyf /* E_FUNC_SET_NOT_USED */ 10235295Srandyf uint16_t pmcap, pmcsr; 10245295Srandyf int flags; 10255295Srandyf uint_t length; 10265295Srandyf clock_t drv_usectohz(clock_t microsecs); 10275295Srandyf #if defined(__x86) 10285295Srandyf uint16_t suspend_level; 10295295Srandyf #endif 10305295Srandyf 10315295Srandyf PMD(PMD_SX, ("pci_pre_resume %s:%d\n", ddi_driver_name(dip), 10325295Srandyf ddi_get_instance(dip))) 10335295Srandyf if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 10345295Srandyf DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 10355295Srandyf SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) { 10365295Srandyf return (DDI_FAILURE); 10375295Srandyf } 10385295Srandyf flags = p->ppc_flags; 10395295Srandyf pmcap = p->ppc_cap_offset; 10405295Srandyf pmcsr = p->ppc_pmcsr; 10415295Srandyf #if defined(__x86) 10425295Srandyf suspend_level = p->ppc_suspend_level; 10435295Srandyf #endif 10445295Srandyf ddi_prop_free(p); 10456996Sgs150176 if ((flags & PPCF_NOPMCAP) != 0) 10466996Sgs150176 goto done; 10475295Srandyf #if defined(__x86) 10485295Srandyf /* 10495295Srandyf * Turn platform wake enable back off 10505295Srandyf */ 10515295Srandyf if ((suspend_level & PCI_PMCSR_PME_EN) != 0) { 10525295Srandyf int retval; 10535295Srandyf 10545295Srandyf retval = acpi_ddi_setwake(dip, 0); /* 0 for now */ 10555295Srandyf if (retval) { 10565295Srandyf PMD(PMD_SX, ("pci_pre_resume, setwake %s@%s rets " 10575295Srandyf "%x\n", PM_NAME(dip), PM_ADDR(dip), retval)); 10585295Srandyf } 10595295Srandyf } 10605295Srandyf #endif 10615295Srandyf if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) { 10625295Srandyf return (DDI_FAILURE); 10635295Srandyf } 10645295Srandyf pci_config_put16(hdl, pmcap + PCI_PMCSR, pmcsr); 10655295Srandyf delay(drv_usectohz(10000)); /* PCI PM spec D3->D0 (10ms) */ 10665295Srandyf pci_config_teardown(&hdl); 10676996Sgs150176 done: 10685295Srandyf (void) pci_restore_config_regs(dip); /* fudges D-state! */ 10695295Srandyf return (DDI_SUCCESS); 10705295Srandyf } 1071