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 /* 23*5295Srandyf * Copyright 2007 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; 3330Sstevel@tonic-gate ddi_acc_hdl_t *hp; 3340Sstevel@tonic-gate off_t offset = 0; 3350Sstevel@tonic-gate uint8_t cap_ptr, cap_id; 3360Sstevel@tonic-gate int pcie = 0; 337*5295Srandyf PMD(PMD_SX, ("pci_save_config_regs %s:%d\n", ddi_driver_name(dip), 338*5295Srandyf ddi_get_instance(dip))) 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 3410Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't get config handle", 342*5295Srandyf ddi_driver_name(dip), ddi_get_instance(dip)); 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate return (DDI_FAILURE); 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate /* 3470Sstevel@tonic-gate * Determine if it is a pci express device. If it is, save entire 3480Sstevel@tonic-gate * 4k config space treating it as a array of 32 bit integers. 3490Sstevel@tonic-gate * If it is not, do it in a usual PCI way. 3500Sstevel@tonic-gate */ 3510Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 3520Sstevel@tonic-gate /* 3530Sstevel@tonic-gate * Walk the capabilities searching for pci express capability 3540Sstevel@tonic-gate */ 3550Sstevel@tonic-gate while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 3560Sstevel@tonic-gate cap_id = pci_config_get8(confhdl, 3570Sstevel@tonic-gate cap_ptr + PCI_CAP_ID); 3580Sstevel@tonic-gate if (cap_id == PCI_CAP_ID_PCI_E) { 3590Sstevel@tonic-gate pcie = 1; 3600Sstevel@tonic-gate break; 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, 3630Sstevel@tonic-gate cap_ptr + PCI_CAP_NEXT_PTR); 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate if (pcie) { 3670Sstevel@tonic-gate /* PCI express device. Can have data in all 4k space */ 3680Sstevel@tonic-gate regbuf = (uint32_t *)kmem_zalloc((size_t)PCIE_CONF_HDR_SIZE, 369*5295Srandyf KM_SLEEP); 3700Sstevel@tonic-gate p = regbuf; 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Allocate space for mask. 3730Sstevel@tonic-gate * mask size is 128 bytes (4096 / 4 / 8 ) 3740Sstevel@tonic-gate */ 3750Sstevel@tonic-gate maskbufsz = (size_t)((PCIE_CONF_HDR_SIZE/ sizeof (uint32_t)) >> 3760Sstevel@tonic-gate INDEX_SHIFT); 3770Sstevel@tonic-gate maskbuf = (uint8_t *)kmem_zalloc(maskbufsz, KM_SLEEP); 3780Sstevel@tonic-gate hp = impl_acc_hdl_get(confhdl); 3790Sstevel@tonic-gate for (i = 0; i < (PCIE_CONF_HDR_SIZE / sizeof (uint32_t)); i++) { 3800Sstevel@tonic-gate if (ddi_peek32(dip, (int32_t *)(hp->ah_addr + offset), 3810Sstevel@tonic-gate (int32_t *)p) == DDI_SUCCESS) { 3820Sstevel@tonic-gate /* it is readable register. set the bit */ 3830Sstevel@tonic-gate maskbuf[i >> INDEX_SHIFT] |= 3840Sstevel@tonic-gate (uint8_t)(1 << (i & BITMASK)); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate p++; 3870Sstevel@tonic-gate offset += sizeof (uint32_t); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 3910Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK, (uchar_t *)maskbuf, 3920Sstevel@tonic-gate maskbufsz)) != DDI_PROP_SUCCESS) { 3930Sstevel@tonic-gate cmn_err(CE_WARN, "couldn't create %s property while" 3940Sstevel@tonic-gate "saving config space for %s@%d\n", 3950Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK, ddi_driver_name(dip), 3960Sstevel@tonic-gate ddi_get_instance(dip)); 3970Sstevel@tonic-gate } else if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, 3980Sstevel@tonic-gate dip, SAVED_CONFIG_REGS, (uchar_t *)regbuf, 3990Sstevel@tonic-gate (size_t)PCIE_CONF_HDR_SIZE)) != DDI_PROP_SUCCESS) { 4000Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 4010Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 4020Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't update prop %s", 4030Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 4040Sstevel@tonic-gate SAVED_CONFIG_REGS); 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate kmem_free(maskbuf, (size_t)maskbufsz); 4080Sstevel@tonic-gate kmem_free(regbuf, (size_t)PCIE_CONF_HDR_SIZE); 4090Sstevel@tonic-gate } else { 4100Sstevel@tonic-gate regbuf = (uint32_t *)kmem_zalloc((size_t)PCI_CONF_HDR_SIZE, 411*5295Srandyf KM_SLEEP); 4120Sstevel@tonic-gate chsp = (pci_config_header_state_t *)regbuf; 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate chsp->chs_command = pci_config_get16(confhdl, PCI_CONF_COMM); 4150Sstevel@tonic-gate chsp->chs_header_type = pci_config_get8(confhdl, 416*5295Srandyf PCI_CONF_HEADER); 4170Sstevel@tonic-gate if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 4180Sstevel@tonic-gate PCI_HEADER_ONE) 4190Sstevel@tonic-gate chsp->chs_bridge_control = 4200Sstevel@tonic-gate pci_config_get16(confhdl, PCI_BCNF_BCNTRL); 4210Sstevel@tonic-gate chsp->chs_cache_line_size = pci_config_get8(confhdl, 4220Sstevel@tonic-gate PCI_CONF_CACHE_LINESZ); 4230Sstevel@tonic-gate chsp->chs_latency_timer = pci_config_get8(confhdl, 4240Sstevel@tonic-gate PCI_CONF_LATENCY_TIMER); 4250Sstevel@tonic-gate if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 4260Sstevel@tonic-gate PCI_HEADER_ONE) { 4270Sstevel@tonic-gate chsp->chs_sec_latency_timer = 4280Sstevel@tonic-gate pci_config_get8(confhdl, PCI_BCNF_LATENCY_TIMER); 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate chsp->chs_base0 = pci_config_get32(confhdl, PCI_CONF_BASE0); 4320Sstevel@tonic-gate chsp->chs_base1 = pci_config_get32(confhdl, PCI_CONF_BASE1); 4330Sstevel@tonic-gate chsp->chs_base2 = pci_config_get32(confhdl, PCI_CONF_BASE2); 4340Sstevel@tonic-gate chsp->chs_base3 = pci_config_get32(confhdl, PCI_CONF_BASE3); 4350Sstevel@tonic-gate chsp->chs_base4 = pci_config_get32(confhdl, PCI_CONF_BASE4); 4360Sstevel@tonic-gate chsp->chs_base5 = pci_config_get32(confhdl, PCI_CONF_BASE5); 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* 4390Sstevel@tonic-gate * Allocate maximum space required for capability descriptions. 4400Sstevel@tonic-gate * The maximum number of capabilties saved is the number of 4410Sstevel@tonic-gate * capabilities listed in the pci_cap_table. 4420Sstevel@tonic-gate */ 4430Sstevel@tonic-gate ncaps = (sizeof (pci_cap_table) / sizeof (pci_cap_entry_t)); 4440Sstevel@tonic-gate capbufsz = ncaps * sizeof (pci_cap_save_desc_t); 4450Sstevel@tonic-gate pci_cap_descp = (pci_cap_save_desc_t *)kmem_zalloc( 4460Sstevel@tonic-gate capbufsz, KM_SLEEP); 4470Sstevel@tonic-gate p = (uint32_t *)((caddr_t)regbuf + 4480Sstevel@tonic-gate sizeof (pci_config_header_state_t)); 4490Sstevel@tonic-gate nwords = pci_save_caps(confhdl, p, pci_cap_descp, &ncaps); 4500Sstevel@tonic-gate regbufsz = sizeof (pci_config_header_state_t) + 4510Sstevel@tonic-gate nwords * sizeof (uint32_t); 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 4540Sstevel@tonic-gate SAVED_CONFIG_REGS, (uchar_t *)regbuf, regbufsz)) != 4550Sstevel@tonic-gate DDI_PROP_SUCCESS) { 4560Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't update prop %s", 4570Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 4580Sstevel@tonic-gate SAVED_CONFIG_REGS); 4590Sstevel@tonic-gate } else if (ncaps) { 4600Sstevel@tonic-gate ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 4610Sstevel@tonic-gate SAVED_CONFIG_REGS_CAPINFO, (uchar_t *)pci_cap_descp, 4620Sstevel@tonic-gate ncaps * sizeof (pci_cap_save_desc_t)); 4630Sstevel@tonic-gate if (ret != DDI_PROP_SUCCESS) 4640Sstevel@tonic-gate (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 4650Sstevel@tonic-gate SAVED_CONFIG_REGS); 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate kmem_free(regbuf, (size_t)PCI_CONF_HDR_SIZE); 4680Sstevel@tonic-gate kmem_free(pci_cap_descp, capbufsz); 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate pci_config_teardown(&confhdl); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate if (ret != DDI_PROP_SUCCESS) 4730Sstevel@tonic-gate return (DDI_FAILURE); 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate return (DDI_SUCCESS); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * Saves registers associated with PCI capabilities. 4800Sstevel@tonic-gate * Returns number of 32 bit words saved. 4810Sstevel@tonic-gate * Number of capabilities saved is returned in ncapsp. 4820Sstevel@tonic-gate */ 4830Sstevel@tonic-gate static uint32_t 4840Sstevel@tonic-gate pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 4850Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp) 4860Sstevel@tonic-gate { 4870Sstevel@tonic-gate return (cap_walk_and_save(confhdl, regbuf, cap_descp, ncapsp, 0)); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate 4900Sstevel@tonic-gate static uint32_t 4910Sstevel@tonic-gate cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf, 4920Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace) 4930Sstevel@tonic-gate { 4940Sstevel@tonic-gate pci_cap_entry_t *pci_cap_entp; 4950Sstevel@tonic-gate uint16_t cap_id, offset; 4960Sstevel@tonic-gate uint32_t words_saved = 0, nwords = 0; 4970Sstevel@tonic-gate uint16_t cap_ptr = PCI_CAP_NEXT_PTR_NULL; 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate *ncapsp = 0; 5000Sstevel@tonic-gate if (!xspace) 5010Sstevel@tonic-gate cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 5020Sstevel@tonic-gate /* 5030Sstevel@tonic-gate * Walk the capabilities 5040Sstevel@tonic-gate */ 5050Sstevel@tonic-gate while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 5060Sstevel@tonic-gate cap_id = CAP_ID(confhdl, cap_ptr, xspace); 5070Sstevel@tonic-gate /* Search for this cap id in our table */ 5080Sstevel@tonic-gate if (!xspace) 5090Sstevel@tonic-gate pci_cap_entp = pci_cap_table; 5100Sstevel@tonic-gate while (pci_cap_entp->cap_id != PCI_CAP_NEXT_PTR_NULL && 5110Sstevel@tonic-gate pci_cap_entp->cap_id != cap_id) 5120Sstevel@tonic-gate pci_cap_entp++; 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate offset = cap_ptr; 5150Sstevel@tonic-gate cap_ptr = NEXT_CAP(confhdl, cap_ptr, xspace); 5160Sstevel@tonic-gate /* 5170Sstevel@tonic-gate * If this cap id is not found in the table, there is nothing 5180Sstevel@tonic-gate * to save. 5190Sstevel@tonic-gate */ 5200Sstevel@tonic-gate if (pci_cap_entp->cap_id == PCI_CAP_NEXT_PTR_NULL) 5210Sstevel@tonic-gate continue; 5220Sstevel@tonic-gate if (pci_cap_entp->cap_save_func) { 5230Sstevel@tonic-gate if ((nwords = pci_cap_entp->cap_save_func(confhdl, 5240Sstevel@tonic-gate offset, regbuf, pci_cap_entp->cap_ndwords))) { 5250Sstevel@tonic-gate cap_descp->cap_nregs = nwords; 5260Sstevel@tonic-gate cap_descp->cap_offset = offset; 5270Sstevel@tonic-gate cap_descp->cap_id = cap_id; 5280Sstevel@tonic-gate regbuf += nwords; 5290Sstevel@tonic-gate cap_descp++; 5300Sstevel@tonic-gate words_saved += nwords; 5310Sstevel@tonic-gate (*ncapsp)++; 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate return (words_saved); 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate static void 5400Sstevel@tonic-gate pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 5410Sstevel@tonic-gate uint32_t *regbuf, uint32_t nwords) 5420Sstevel@tonic-gate { 5430Sstevel@tonic-gate int i; 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate for (i = 0; i < nwords; i++) { 5460Sstevel@tonic-gate *regbuf = pci_config_get32(confhdl, cap_ptr); 5470Sstevel@tonic-gate regbuf++; 5480Sstevel@tonic-gate cap_ptr += 4; 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate static uint32_t 5530Sstevel@tonic-gate pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 5540Sstevel@tonic-gate uint32_t nwords) 5550Sstevel@tonic-gate { 5560Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 5570Sstevel@tonic-gate return (nwords); 5580Sstevel@tonic-gate } 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate /*ARGSUSED*/ 5610Sstevel@tonic-gate static uint32_t 5620Sstevel@tonic-gate pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 5630Sstevel@tonic-gate uint32_t notused) 5640Sstevel@tonic-gate { 5650Sstevel@tonic-gate uint32_t nwords = PCI_MSI_MIN_WORDS; 5660Sstevel@tonic-gate uint16_t msi_ctrl; 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate /* Figure out how many registers to be saved */ 5690Sstevel@tonic-gate msi_ctrl = pci_config_get16(confhdl, cap_ptr + PCI_MSI_CTRL); 5700Sstevel@tonic-gate /* If 64 bit address capable add one word */ 5710Sstevel@tonic-gate if (msi_ctrl & PCI_MSI_64BIT_MASK) 5720Sstevel@tonic-gate nwords++; 5730Sstevel@tonic-gate /* If per vector masking capable, add two more words */ 5740Sstevel@tonic-gate if (msi_ctrl & PCI_MSI_PVM_MASK) 5750Sstevel@tonic-gate nwords += 2; 5760Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate return (nwords); 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate /*ARGSUSED*/ 5820Sstevel@tonic-gate static uint32_t 5830Sstevel@tonic-gate pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 5840Sstevel@tonic-gate uint32_t notused) 5850Sstevel@tonic-gate { 5860Sstevel@tonic-gate uint32_t nwords = PCI_PCIX_MIN_WORDS; 5870Sstevel@tonic-gate uint16_t pcix_command; 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate /* Figure out how many registers to be saved */ 5900Sstevel@tonic-gate pcix_command = pci_config_get16(confhdl, cap_ptr + PCI_PCIX_COMMAND); 5910Sstevel@tonic-gate /* If it is version 1 or version 2, add 4 words */ 5920Sstevel@tonic-gate if (((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_1) || 5930Sstevel@tonic-gate ((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_2)) 5940Sstevel@tonic-gate nwords += 4; 5950Sstevel@tonic-gate pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate return (nwords); 5980Sstevel@tonic-gate } 5990Sstevel@tonic-gate 6000Sstevel@tonic-gate /*ARGSUSED*/ 6010Sstevel@tonic-gate static uint32_t 6020Sstevel@tonic-gate pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 6030Sstevel@tonic-gate uint32_t notused) 6040Sstevel@tonic-gate { 6050Sstevel@tonic-gate return (0); 6060Sstevel@tonic-gate } 6070Sstevel@tonic-gate 6080Sstevel@tonic-gate static void 6090Sstevel@tonic-gate pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf, 6100Sstevel@tonic-gate uint16_t pmcap_offset) 6110Sstevel@tonic-gate { 6120Sstevel@tonic-gate uint16_t pmcsr; 6130Sstevel@tonic-gate uint16_t pmcsr_offset = pmcap_offset + PCI_PMCSR; 6140Sstevel@tonic-gate uint32_t *saved_pmcsrp = (uint32_t *)((caddr_t)regbuf + PCI_PMCSR); 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate /* 6170Sstevel@tonic-gate * Copy the power state bits from the PMCSR to our saved copy. 6180Sstevel@tonic-gate * This is to make sure that we don't change the D state when 6190Sstevel@tonic-gate * we restore config space of the device. 6200Sstevel@tonic-gate */ 6210Sstevel@tonic-gate pmcsr = pci_config_get16(confhdl, pmcsr_offset); 6220Sstevel@tonic-gate (*saved_pmcsrp) &= ~PCI_PMCSR_STATE_MASK; 6230Sstevel@tonic-gate (*saved_pmcsrp) |= (pmcsr & PCI_PMCSR_STATE_MASK); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate static void 6270Sstevel@tonic-gate pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 6280Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp, uint32_t elements) 6290Sstevel@tonic-gate { 6300Sstevel@tonic-gate int i, j; 6310Sstevel@tonic-gate uint16_t offset; 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate for (i = 0; i < (elements / sizeof (pci_cap_save_desc_t)); i++) { 6340Sstevel@tonic-gate offset = cap_descp->cap_offset; 6350Sstevel@tonic-gate if (cap_descp->cap_id == PCI_CAP_ID_PM) 6360Sstevel@tonic-gate pci_pmcap_check(confhdl, regbuf, offset); 6370Sstevel@tonic-gate for (j = 0; j < cap_descp->cap_nregs; j++) { 6380Sstevel@tonic-gate pci_config_put32(confhdl, offset, *regbuf); 6390Sstevel@tonic-gate regbuf++; 6400Sstevel@tonic-gate offset += 4; 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate cap_descp++; 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate /* 6470Sstevel@tonic-gate * Restore config_regs from a single devinfo node. 6480Sstevel@tonic-gate */ 6490Sstevel@tonic-gate int 6500Sstevel@tonic-gate pci_restore_config_regs(dev_info_t *dip) 6510Sstevel@tonic-gate { 6520Sstevel@tonic-gate ddi_acc_handle_t confhdl; 6530Sstevel@tonic-gate pci_config_header_state_t *chs_p; 6540Sstevel@tonic-gate pci_cap_save_desc_t *cap_descp; 6550Sstevel@tonic-gate uint32_t elements, i; 6560Sstevel@tonic-gate uint8_t *maskbuf; 6570Sstevel@tonic-gate uint32_t *regbuf, *p; 6580Sstevel@tonic-gate off_t offset = 0; 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 6610Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't get config handle", 6620Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 6630Sstevel@tonic-gate return (DDI_FAILURE); 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 6670Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS_MASK, 6680Sstevel@tonic-gate (uchar_t **)&maskbuf, &elements) == DDI_PROP_SUCCESS) { 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 6710Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 6720Sstevel@tonic-gate (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 6730Sstevel@tonic-gate goto restoreconfig_err; 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate ASSERT(elements == PCIE_CONF_HDR_SIZE); 6760Sstevel@tonic-gate /* pcie device and has 4k config space saved */ 6770Sstevel@tonic-gate p = regbuf; 6780Sstevel@tonic-gate for (i = 0; i < PCIE_CONF_HDR_SIZE / sizeof (uint32_t); i++) { 6790Sstevel@tonic-gate /* If the word is readable then restore it */ 6800Sstevel@tonic-gate if (maskbuf[i >> INDEX_SHIFT] & 6810Sstevel@tonic-gate (uint8_t)(1 << (i & BITMASK))) 6820Sstevel@tonic-gate pci_config_put32(confhdl, offset, *p); 6830Sstevel@tonic-gate p++; 6840Sstevel@tonic-gate offset += sizeof (uint32_t); 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate ddi_prop_free(regbuf); 6870Sstevel@tonic-gate ddi_prop_free(maskbuf); 6880Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, 6890Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK) != DDI_PROP_SUCCESS) { 6900Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 6910Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 6920Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate } else { 6950Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 6960Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 6970Sstevel@tonic-gate (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate pci_config_teardown(&confhdl); 7000Sstevel@tonic-gate return (DDI_FAILURE); 7010Sstevel@tonic-gate } 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate chs_p = (pci_config_header_state_t *)regbuf; 7040Sstevel@tonic-gate pci_config_put16(confhdl, PCI_CONF_COMM, 7050Sstevel@tonic-gate chs_p->chs_command); 7060Sstevel@tonic-gate if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 7070Sstevel@tonic-gate PCI_HEADER_ONE) { 7080Sstevel@tonic-gate pci_config_put16(confhdl, PCI_BCNF_BCNTRL, 7090Sstevel@tonic-gate chs_p->chs_bridge_control); 7100Sstevel@tonic-gate } 7110Sstevel@tonic-gate pci_config_put8(confhdl, PCI_CONF_CACHE_LINESZ, 7120Sstevel@tonic-gate chs_p->chs_cache_line_size); 7130Sstevel@tonic-gate pci_config_put8(confhdl, PCI_CONF_LATENCY_TIMER, 7140Sstevel@tonic-gate chs_p->chs_latency_timer); 7150Sstevel@tonic-gate if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 7160Sstevel@tonic-gate PCI_HEADER_ONE) 7170Sstevel@tonic-gate pci_config_put8(confhdl, PCI_BCNF_LATENCY_TIMER, 7180Sstevel@tonic-gate chs_p->chs_sec_latency_timer); 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE0, chs_p->chs_base0); 7210Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE1, chs_p->chs_base1); 7220Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE2, chs_p->chs_base2); 7230Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE3, chs_p->chs_base3); 7240Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE4, chs_p->chs_base4); 7250Sstevel@tonic-gate pci_config_put32(confhdl, PCI_CONF_BASE5, chs_p->chs_base5); 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 7280Sstevel@tonic-gate DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 7290Sstevel@tonic-gate SAVED_CONFIG_REGS_CAPINFO, 7300Sstevel@tonic-gate (uchar_t **)&cap_descp, &elements) == DDI_PROP_SUCCESS) { 7310Sstevel@tonic-gate /* 7320Sstevel@tonic-gate * PCI capability related regsiters are saved. 7330Sstevel@tonic-gate * Restore them based on the description. 7340Sstevel@tonic-gate */ 7350Sstevel@tonic-gate p = (uint32_t *)((caddr_t)regbuf + 7360Sstevel@tonic-gate sizeof (pci_config_header_state_t)); 7370Sstevel@tonic-gate pci_restore_caps(confhdl, p, cap_descp, elements); 7380Sstevel@tonic-gate ddi_prop_free(cap_descp); 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate ddi_prop_free(regbuf); 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate /* 7450Sstevel@tonic-gate * Make sure registers are flushed 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate (void) pci_config_get32(confhdl, PCI_CONF_BASE5); 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS) != 7510Sstevel@tonic-gate DDI_PROP_SUCCESS) { 7520Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 7530Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 7540Sstevel@tonic-gate SAVED_CONFIG_REGS); 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate pci_config_teardown(&confhdl); 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate return (DDI_SUCCESS); 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate restoreconfig_err: 7620Sstevel@tonic-gate ddi_prop_free(maskbuf); 7630Sstevel@tonic-gate if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS_MASK) != 7640Sstevel@tonic-gate DDI_PROP_SUCCESS) { 7650Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d can't remove prop %s", 7660Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 7670Sstevel@tonic-gate SAVED_CONFIG_REGS_MASK); 7680Sstevel@tonic-gate } 7690Sstevel@tonic-gate pci_config_teardown(&confhdl); 7700Sstevel@tonic-gate return (DDI_FAILURE); 7710Sstevel@tonic-gate } 772*5295Srandyf 773*5295Srandyf /*ARGSUSED*/ 774*5295Srandyf static int 775*5295Srandyf pci_lookup_pmcap(dev_info_t *dip, ddi_acc_handle_t conf_hdl, 776*5295Srandyf uint16_t *pmcap_offsetp) 777*5295Srandyf { 778*5295Srandyf uint8_t cap_ptr; 779*5295Srandyf uint8_t cap_id; 780*5295Srandyf uint8_t header_type; 781*5295Srandyf uint16_t status; 782*5295Srandyf 783*5295Srandyf header_type = pci_config_get8(conf_hdl, PCI_CONF_HEADER); 784*5295Srandyf header_type &= PCI_HEADER_TYPE_M; 785*5295Srandyf 786*5295Srandyf /* we don't deal with bridges, etc here */ 787*5295Srandyf if (header_type != PCI_HEADER_ZERO) { 788*5295Srandyf return (DDI_FAILURE); 789*5295Srandyf } 790*5295Srandyf 791*5295Srandyf status = pci_config_get16(conf_hdl, PCI_CONF_STAT); 792*5295Srandyf if ((status & PCI_STAT_CAP) == 0) { 793*5295Srandyf return (DDI_FAILURE); 794*5295Srandyf } 795*5295Srandyf 796*5295Srandyf cap_ptr = pci_config_get8(conf_hdl, PCI_CONF_CAP_PTR); 797*5295Srandyf 798*5295Srandyf /* 799*5295Srandyf * Walk the capabilities searching for a PM entry. 800*5295Srandyf */ 801*5295Srandyf while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 802*5295Srandyf cap_id = pci_config_get8(conf_hdl, cap_ptr + PCI_CAP_ID); 803*5295Srandyf if (cap_id == PCI_CAP_ID_PM) { 804*5295Srandyf break; 805*5295Srandyf } 806*5295Srandyf cap_ptr = pci_config_get8(conf_hdl, 807*5295Srandyf cap_ptr + PCI_CAP_NEXT_PTR); 808*5295Srandyf } 809*5295Srandyf 810*5295Srandyf if (cap_ptr == PCI_CAP_NEXT_PTR_NULL) { 811*5295Srandyf return (DDI_FAILURE); 812*5295Srandyf } 813*5295Srandyf *pmcap_offsetp = cap_ptr; 814*5295Srandyf return (DDI_SUCCESS); 815*5295Srandyf } 816*5295Srandyf 817*5295Srandyf /* 818*5295Srandyf * Do common pci-specific suspend actions: 819*5295Srandyf * - enable wakeup if appropriate for the device 820*5295Srandyf * - put device in lowest D-state that supports wakeup, or D3 if none 821*5295Srandyf * - turn off bus mastering in control register 822*5295Srandyf * For lack of per-dip storage (parent private date is pretty busy) 823*5295Srandyf * we use properties to store the necessary context 824*5295Srandyf * To avoid grotting through pci config space on every suspend, 825*5295Srandyf * we leave the prop in existence after resume, cause we know that 826*5295Srandyf * the detach framework code will dispose of it for us. 827*5295Srandyf */ 828*5295Srandyf 829*5295Srandyf typedef struct pci_pm_context { 830*5295Srandyf int ppc_flags; 831*5295Srandyf uint16_t ppc_cap_offset; /* offset in config space to pm cap */ 832*5295Srandyf uint16_t ppc_pmcsr; /* need this too */ 833*5295Srandyf uint16_t ppc_suspend_level; 834*5295Srandyf } pci_pm_context_t; 835*5295Srandyf 836*5295Srandyf #define SAVED_PM_CONTEXT "pci-pm-context" 837*5295Srandyf 838*5295Srandyf /* values for ppc_flags */ 839*5295Srandyf #define PPCF_NOPMCAP 1 840*5295Srandyf 841*5295Srandyf /* 842*5295Srandyf * Handle pci-specific suspend processing 843*5295Srandyf * PM CSR and PCI CMD are saved by pci_save_config_regs(). 844*5295Srandyf * If device can wake up system via PME, enable it to do so 845*5295Srandyf * Set device power level to lowest that can generate PME, or D3 if none can 846*5295Srandyf * Turn off bus master enable in pci command register 847*5295Srandyf */ 848*5295Srandyf #if defined(__x86) 849*5295Srandyf extern int acpi_ddi_setwake(dev_info_t *dip, int level); 850*5295Srandyf #endif 851*5295Srandyf 852*5295Srandyf int 853*5295Srandyf pci_post_suspend(dev_info_t *dip) 854*5295Srandyf { 855*5295Srandyf pci_pm_context_t *p; 856*5295Srandyf uint16_t pmcap, pmcsr, pcicmd; 857*5295Srandyf uint_t length; 858*5295Srandyf int ret; 859*5295Srandyf int fromprop = 1; /* source of memory *p */ 860*5295Srandyf ddi_acc_handle_t hdl; 861*5295Srandyf 862*5295Srandyf PMD(PMD_SX, ("pci_post_suspend %s:%d\n", 863*5295Srandyf ddi_driver_name(dip), ddi_get_instance(dip))) 864*5295Srandyf 865*5295Srandyf if (pci_save_config_regs(dip) != DDI_SUCCESS) { 866*5295Srandyf return (DDI_FAILURE); 867*5295Srandyf } 868*5295Srandyf 869*5295Srandyf if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) { 870*5295Srandyf return (DDI_FAILURE); 871*5295Srandyf } 872*5295Srandyf 873*5295Srandyf if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 874*5295Srandyf DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 875*5295Srandyf SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) { 876*5295Srandyf p = (pci_pm_context_t *)kmem_zalloc(sizeof (*p), KM_SLEEP); 877*5295Srandyf fromprop = 0; 878*5295Srandyf if (pci_lookup_pmcap(dip, hdl, 879*5295Srandyf &p->ppc_cap_offset) != DDI_SUCCESS) { 880*5295Srandyf p->ppc_flags |= PPCF_NOPMCAP; 881*5295Srandyf ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 882*5295Srandyf SAVED_PM_CONTEXT, (uchar_t *)p, 883*5295Srandyf sizeof (pci_pm_context_t)); 884*5295Srandyf if (ret != DDI_PROP_SUCCESS) { 885*5295Srandyf (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 886*5295Srandyf SAVED_PM_CONTEXT); 887*5295Srandyf ret = DDI_FAILURE; 888*5295Srandyf } else { 889*5295Srandyf ret = DDI_SUCCESS; 890*5295Srandyf } 891*5295Srandyf kmem_free(p, sizeof (*p)); 892*5295Srandyf pci_config_teardown(&hdl); 893*5295Srandyf return (DDI_SUCCESS); 894*5295Srandyf } 895*5295Srandyf /* 896*5295Srandyf * Upon suspend, set the power level to the lowest that can 897*5295Srandyf * wake the system. If none can, then set to lowest. 898*5295Srandyf * XXX later we will need to check policy to see if this 899*5295Srandyf * XXX device has had wakeup disabled 900*5295Srandyf */ 901*5295Srandyf pmcap = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCAP); 902*5295Srandyf if ((pmcap & PCI_PMCAP_D3COLD_PME) != 0) 903*5295Srandyf p->ppc_suspend_level = 904*5295Srandyf (PCI_PMCSR_PME_EN | PCI_PMCSR_D3HOT); 905*5295Srandyf else if ((pmcap & (PCI_PMCAP_D3HOT_PME | PCI_PMCAP_D2_PME)) != 906*5295Srandyf 0) 907*5295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D2; 908*5295Srandyf else if ((pmcap & PCI_PMCAP_D1_PME) != 0) 909*5295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D1; 910*5295Srandyf else if ((pmcap & PCI_PMCAP_D0_PME) != 0) 911*5295Srandyf p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D0; 912*5295Srandyf else 913*5295Srandyf p->ppc_suspend_level = PCI_PMCSR_D3HOT; 914*5295Srandyf 915*5295Srandyf /* 916*5295Srandyf * we defer updating the property to catch the saved 917*5295Srandyf * register values as well 918*5295Srandyf */ 919*5295Srandyf } 920*5295Srandyf /* If we set this in kmem_zalloc'd memory, we already returned above */ 921*5295Srandyf if ((p->ppc_flags & PPCF_NOPMCAP) != 0) { 922*5295Srandyf ddi_prop_free(p); 923*5295Srandyf pci_config_teardown(&hdl); 924*5295Srandyf return (DDI_SUCCESS); 925*5295Srandyf } 926*5295Srandyf 927*5295Srandyf 928*5295Srandyf /* 929*5295Srandyf * Turn off (Bus) Master Enable, since acpica will be turning off 930*5295Srandyf * bus master aribitration 931*5295Srandyf */ 932*5295Srandyf pcicmd = pci_config_get16(hdl, PCI_CONF_COMM); 933*5295Srandyf pcicmd &= ~PCI_COMM_ME; 934*5295Srandyf pci_config_put16(hdl, PCI_CONF_COMM, pcicmd); 935*5295Srandyf 936*5295Srandyf /* 937*5295Srandyf * set pm csr 938*5295Srandyf */ 939*5295Srandyf pmcsr = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCSR); 940*5295Srandyf p->ppc_pmcsr = pmcsr; 941*5295Srandyf pmcsr &= (PCI_PMCSR_STATE_MASK); 942*5295Srandyf pmcsr |= (PCI_PMCSR_PME_STAT | p->ppc_suspend_level); 943*5295Srandyf pci_config_put16(hdl, p->ppc_cap_offset + PCI_PMCSR, pmcsr); 944*5295Srandyf 945*5295Srandyf #if defined(__x86) 946*5295Srandyf /* 947*5295Srandyf * Arrange for platform wakeup enabling 948*5295Srandyf */ 949*5295Srandyf if ((p->ppc_suspend_level & PCI_PMCSR_PME_EN) != 0) { 950*5295Srandyf int retval; 951*5295Srandyf 952*5295Srandyf retval = acpi_ddi_setwake(dip, 3); /* XXX 3 for now */ 953*5295Srandyf if (retval) { 954*5295Srandyf PMD(PMD_SX, ("pci_post_suspend, setwake %s@%s rets " 955*5295Srandyf "%x\n", PM_NAME(dip), PM_ADDR(dip), retval)); 956*5295Srandyf } 957*5295Srandyf } 958*5295Srandyf #endif 959*5295Srandyf 960*5295Srandyf /* 961*5295Srandyf * Push out saved register values 962*5295Srandyf */ 963*5295Srandyf ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT, 964*5295Srandyf (uchar_t *)p, sizeof (pci_pm_context_t)); 965*5295Srandyf if (ret == DDI_PROP_SUCCESS) { 966*5295Srandyf if (fromprop) 967*5295Srandyf ddi_prop_free(p); 968*5295Srandyf else 969*5295Srandyf kmem_free(p, sizeof (*p)); 970*5295Srandyf pci_config_teardown(&hdl); 971*5295Srandyf return (DDI_SUCCESS); 972*5295Srandyf } 973*5295Srandyf /* Failed; put things back the way we found them */ 974*5295Srandyf (void) pci_restore_config_regs(dip); 975*5295Srandyf if (fromprop) 976*5295Srandyf ddi_prop_free(p); 977*5295Srandyf else 978*5295Srandyf kmem_free(p, sizeof (*p)); 979*5295Srandyf (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT); 980*5295Srandyf pci_config_teardown(&hdl); 981*5295Srandyf return (DDI_FAILURE); 982*5295Srandyf } 983*5295Srandyf 984*5295Srandyf /* 985*5295Srandyf * The inverse of pci_post_suspend; handle pci-specific resume processing 986*5295Srandyf * First, turn device back on, then restore config space. 987*5295Srandyf */ 988*5295Srandyf 989*5295Srandyf int 990*5295Srandyf pci_pre_resume(dev_info_t *dip) 991*5295Srandyf { 992*5295Srandyf ddi_acc_handle_t hdl; 993*5295Srandyf pci_pm_context_t *p; 994*5295Srandyf /* E_FUNC_SET_NOT_USED */ 995*5295Srandyf uint16_t pmcap, pmcsr; 996*5295Srandyf int flags; 997*5295Srandyf uint_t length; 998*5295Srandyf clock_t drv_usectohz(clock_t microsecs); 999*5295Srandyf #if defined(__x86) 1000*5295Srandyf uint16_t suspend_level; 1001*5295Srandyf #endif 1002*5295Srandyf 1003*5295Srandyf PMD(PMD_SX, ("pci_pre_resume %s:%d\n", ddi_driver_name(dip), 1004*5295Srandyf ddi_get_instance(dip))) 1005*5295Srandyf if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 1006*5295Srandyf DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 1007*5295Srandyf SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) { 1008*5295Srandyf return (DDI_FAILURE); 1009*5295Srandyf } 1010*5295Srandyf flags = p->ppc_flags; 1011*5295Srandyf pmcap = p->ppc_cap_offset; 1012*5295Srandyf pmcsr = p->ppc_pmcsr; 1013*5295Srandyf #if defined(__x86) 1014*5295Srandyf suspend_level = p->ppc_suspend_level; 1015*5295Srandyf #endif 1016*5295Srandyf ddi_prop_free(p); 1017*5295Srandyf if ((flags & PPCF_NOPMCAP) != 0) { 1018*5295Srandyf return (DDI_SUCCESS); 1019*5295Srandyf } 1020*5295Srandyf #if defined(__x86) 1021*5295Srandyf /* 1022*5295Srandyf * Turn platform wake enable back off 1023*5295Srandyf */ 1024*5295Srandyf if ((suspend_level & PCI_PMCSR_PME_EN) != 0) { 1025*5295Srandyf int retval; 1026*5295Srandyf 1027*5295Srandyf retval = acpi_ddi_setwake(dip, 0); /* 0 for now */ 1028*5295Srandyf if (retval) { 1029*5295Srandyf PMD(PMD_SX, ("pci_pre_resume, setwake %s@%s rets " 1030*5295Srandyf "%x\n", PM_NAME(dip), PM_ADDR(dip), retval)); 1031*5295Srandyf } 1032*5295Srandyf } 1033*5295Srandyf #endif 1034*5295Srandyf if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) { 1035*5295Srandyf return (DDI_FAILURE); 1036*5295Srandyf } 1037*5295Srandyf pci_config_put16(hdl, pmcap + PCI_PMCSR, pmcsr); 1038*5295Srandyf delay(drv_usectohz(10000)); /* PCI PM spec D3->D0 (10ms) */ 1039*5295Srandyf pci_config_teardown(&hdl); 1040*5295Srandyf (void) pci_restore_config_regs(dip); /* fudges D-state! */ 1041*5295Srandyf return (DDI_SUCCESS); 1042*5295Srandyf } 1043