1ae115bc7Smrj /* 2ae115bc7Smrj * CDDL HEADER START 3ae115bc7Smrj * 4ae115bc7Smrj * The contents of this file are subject to the terms of the 5ae115bc7Smrj * Common Development and Distribution License (the "License"). 6ae115bc7Smrj * You may not use this file except in compliance with the License. 7ae115bc7Smrj * 8ae115bc7Smrj * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9ae115bc7Smrj * or http://www.opensolaris.org/os/licensing. 10ae115bc7Smrj * See the License for the specific language governing permissions 11ae115bc7Smrj * and limitations under the License. 12ae115bc7Smrj * 13ae115bc7Smrj * When distributing Covered Code, include this CDDL HEADER in each 14ae115bc7Smrj * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15ae115bc7Smrj * If applicable, add the following below this CDDL HEADER, with the 16ae115bc7Smrj * fields enclosed by brackets "[]" replaced with your own identifying 17ae115bc7Smrj * information: Portions Copyright [yyyy] [name of copyright owner] 18ae115bc7Smrj * 19ae115bc7Smrj * CDDL HEADER END 20ae115bc7Smrj */ 21ae115bc7Smrj 22ae115bc7Smrj /* 23*0db3240dSStephen Hanson * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24ae115bc7Smrj */ 25ae115bc7Smrj 26ae115bc7Smrj /* 27ae115bc7Smrj * Library file that has code for PCIe booting 28ae115bc7Smrj */ 29ae115bc7Smrj 30ae115bc7Smrj #include <sys/conf.h> 31ae115bc7Smrj #include <sys/pci.h> 32ae115bc7Smrj #include <sys/sunndi.h> 33ae115bc7Smrj #include <sys/pcie.h> 34c0da6274SZhi-Jun Robin Fu #include <sys/pcie_impl.h> 35ae115bc7Smrj #include <sys/pci_cfgspace.h> 36ae115bc7Smrj #include <io/pciex/pcie_nvidia.h> 37ae115bc7Smrj 38ae115bc7Smrj /* 39ae115bc7Smrj * PCI Configuration (Nvidia chipsets, PCIe) related library functions 40ae115bc7Smrj */ 41ae115bc7Smrj 42ae115bc7Smrj /* Globals */ 43ae115bc7Smrj extern int pci_boot_debug; 44ae115bc7Smrj 45c0da6274SZhi-Jun Robin Fu extern uint64_t mcfg_mem_base; 46c0da6274SZhi-Jun Robin Fu 47ae115bc7Smrj boolean_t 48ae115bc7Smrj check_if_device_is_pciex(dev_info_t *cdip, uchar_t bus, uchar_t dev, 49*0db3240dSStephen Hanson uchar_t func, boolean_t *slot_valid, ushort_t *slot_number, 50*0db3240dSStephen Hanson ushort_t *is_pci_bridge) 51ae115bc7Smrj { 52ae115bc7Smrj boolean_t found_pciex = B_FALSE; 53ae115bc7Smrj ushort_t cap; 54ae115bc7Smrj ushort_t capsp; 55ae115bc7Smrj ushort_t cap_count = PCI_CAP_MAX_PTR; 56ae115bc7Smrj ushort_t status; 57ae115bc7Smrj uint32_t slot_cap; 58ae115bc7Smrj 59*0db3240dSStephen Hanson *slot_valid = B_FALSE; 60ae115bc7Smrj 61ae115bc7Smrj status = (*pci_getw_func)(bus, dev, func, PCI_CONF_STAT); 62ae115bc7Smrj if (!(status & PCI_STAT_CAP)) 63ae115bc7Smrj return (B_FALSE); 64ae115bc7Smrj 65ae115bc7Smrj capsp = (*pci_getb_func)(bus, dev, func, PCI_CONF_CAP_PTR); 66ae115bc7Smrj while (cap_count-- && capsp >= PCI_CAP_PTR_OFF) { 67ae115bc7Smrj capsp &= PCI_CAP_PTR_MASK; 68ae115bc7Smrj cap = (*pci_getb_func)(bus, dev, func, capsp); 69ae115bc7Smrj 70ae115bc7Smrj if (cap == PCI_CAP_ID_PCI_E) { 71ae115bc7Smrj #ifdef DEBUG 72ae115bc7Smrj if (pci_boot_debug) 73ae115bc7Smrj cmn_err(CE_CONT, "PCI-Express (%x,%x,%x) " 74ae115bc7Smrj "capability found\n", bus, dev, func); 75ae115bc7Smrj #endif /* DEBUG */ 76ae115bc7Smrj 77ae115bc7Smrj status = (*pci_getw_func)(bus, dev, func, capsp + 2); 78ae115bc7Smrj /* 79ae115bc7Smrj * See section 7.8.2 of PCI-Express Base Spec v1.0a 80ae115bc7Smrj * for Device/Port Type. 81ae115bc7Smrj * PCIE_PCIECAP_DEV_TYPE_PCIE2PCI implies that the 82ae115bc7Smrj * device is a PCIe2PCI bridge 83ae115bc7Smrj */ 84ae115bc7Smrj *is_pci_bridge = 8549fbdd30SErwin T Tsaur ((status & PCIE_PCIECAP_DEV_TYPE_MASK) == 86ae115bc7Smrj PCIE_PCIECAP_DEV_TYPE_PCIE2PCI) ? 1 : 0; 87ae115bc7Smrj 88ae115bc7Smrj /* 89ae115bc7Smrj * Check for "Slot Implemented" bit 90ae115bc7Smrj * PCIE_PCIECAP_SLOT_IMPL implies that. 91ae115bc7Smrj */ 92ae115bc7Smrj if (status & PCIE_PCIECAP_SLOT_IMPL) { 93ae115bc7Smrj /* offset 14h is Slot Cap Register */ 94ae115bc7Smrj slot_cap = (*pci_getl_func)(bus, dev, func, 95ae115bc7Smrj capsp + PCIE_SLOTCAP); 96*0db3240dSStephen Hanson *slot_valid = B_TRUE; 97ae115bc7Smrj *slot_number = 98ae115bc7Smrj PCIE_SLOTCAP_PHY_SLOT_NUM(slot_cap); 99ae115bc7Smrj 100ae115bc7Smrj /* Is PCI Express HotPlug capability set? */ 101ae115bc7Smrj if (cdip && 102ae115bc7Smrj (slot_cap & PCIE_SLOTCAP_HP_CAPABLE)) { 103ae115bc7Smrj (void) ndi_prop_update_int( 104ae115bc7Smrj DDI_DEV_T_NONE, cdip, 105ae115bc7Smrj "pci-hotplug-type", 106ae115bc7Smrj INBAND_HPC_PCIE); 107ae115bc7Smrj } 108ae115bc7Smrj } 109ae115bc7Smrj 110ae115bc7Smrj found_pciex = B_TRUE; 111ae115bc7Smrj } 112ae115bc7Smrj 113ae115bc7Smrj if (cdip && (cap == PCI_CAP_ID_PCI_HOTPLUG)) { 114ae115bc7Smrj (void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, 115ae115bc7Smrj "pci-hotplug-type", INBAND_HPC_SHPC); 116ae115bc7Smrj } 117ae115bc7Smrj 118ae115bc7Smrj capsp = (*pci_getb_func)(bus, dev, func, 119ae115bc7Smrj capsp + PCI_CAP_NEXT_PTR); 120ae115bc7Smrj } 121ae115bc7Smrj 122ae115bc7Smrj return (found_pciex); 123ae115bc7Smrj } 124ae115bc7Smrj 125ae115bc7Smrj 126ae115bc7Smrj /* 127ae115bc7Smrj * scan all buses, devices, functions to look for any 128ae115bc7Smrj * PCI-Express device in the system. 129ae115bc7Smrj * If found, return B_TRUE else B_FALSE 130ae115bc7Smrj */ 131c0da6274SZhi-Jun Robin Fu boolean_t 132ae115bc7Smrj look_for_any_pciex_device(uchar_t bus) 133ae115bc7Smrj { 134ae115bc7Smrj uchar_t dev, func; 135ae115bc7Smrj uchar_t nfunc, header; 136ae115bc7Smrj ushort_t venid, slot_num, is_pci_bridge = 0; 137*0db3240dSStephen Hanson boolean_t slot_valid; 138ae115bc7Smrj 139ae115bc7Smrj for (dev = 0; dev < 32; dev++) { 140ae115bc7Smrj nfunc = 1; 141ae115bc7Smrj for (func = 0; func < nfunc; func++) { 142ae115bc7Smrj #ifdef DEBUG 143ae115bc7Smrj if (pci_boot_debug) 144ae115bc7Smrj cmn_err(CE_NOTE, "pciex dev 0x%x, func 0x%x", 145ae115bc7Smrj dev, func); 146ae115bc7Smrj #endif /* DEBUG */ 147ae115bc7Smrj 148ae115bc7Smrj venid = (*pci_getw_func)(bus, dev, func, 149ae115bc7Smrj PCI_CONF_VENID); 150ae115bc7Smrj /* no function at this address */ 151ae115bc7Smrj if ((venid == 0xffff) || (venid == 0)) 152ae115bc7Smrj continue; 153ae115bc7Smrj 154ae115bc7Smrj header = (*pci_getb_func)(bus, dev, func, 155ae115bc7Smrj PCI_CONF_HEADER); 156ae115bc7Smrj if (header == 0xff) 157ae115bc7Smrj continue; /* illegal value */ 158ae115bc7Smrj 159ae115bc7Smrj /* 160ae115bc7Smrj * according to some mail from Microsoft posted to 161ae115bc7Smrj * the pci-drivers alias, their only requirement for 162ae115bc7Smrj * a multifunction device is for the 1st function to 163ae115bc7Smrj * have to PCI_HEADER_MULTI bit set. 164ae115bc7Smrj */ 165ae115bc7Smrj if ((func == 0) && (header & PCI_HEADER_MULTI)) 166ae115bc7Smrj nfunc = 8; 167ae115bc7Smrj 168ae115bc7Smrj if (check_if_device_is_pciex(NULL, bus, dev, func, 169*0db3240dSStephen Hanson &slot_valid, &slot_num, &is_pci_bridge) == B_TRUE) 170ae115bc7Smrj return (B_TRUE); 171ae115bc7Smrj } /* end of func */ 172ae115bc7Smrj } /* end of dev */ 173ae115bc7Smrj 174ae115bc7Smrj return (B_FALSE); 175ae115bc7Smrj } 176ae115bc7Smrj 177ae115bc7Smrj boolean_t 178ae115bc7Smrj create_pcie_root_bus(uchar_t bus, dev_info_t *dip) 179ae115bc7Smrj { 180c0da6274SZhi-Jun Robin Fu pcie_bus_t *bus_p; 181c0da6274SZhi-Jun Robin Fu 182ae115bc7Smrj /* 183ae115bc7Smrj * Currently this is being hard-coded. 184ae115bc7Smrj * We need to figure out if the root bus does indeed 185ae115bc7Smrj * have PCI-Ex in the path by looking for MCFG in 186ae115bc7Smrj * the ACPI tables 187ae115bc7Smrj */ 188ae115bc7Smrj if (look_for_any_pciex_device(bus) == B_FALSE) 189ae115bc7Smrj return (B_FALSE); 190ae115bc7Smrj 191ae115bc7Smrj #ifdef DEBUG 192ae115bc7Smrj if (pci_boot_debug) 193ae115bc7Smrj cmn_err(CE_CONT, "Found PCI-Ex in the system\n"); 194ae115bc7Smrj #endif /* DEBUG */ 195ae115bc7Smrj (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 196ae115bc7Smrj "device_type", "pciex"); 197ae115bc7Smrj (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 198ae115bc7Smrj "compatible", "pciex_root_complex"); 199ae115bc7Smrj 200c0da6274SZhi-Jun Robin Fu pcie_rc_init_bus(dip); 201c0da6274SZhi-Jun Robin Fu 202c0da6274SZhi-Jun Robin Fu /* save base addr in bus_t for pci_cfgacc_xxx() */ 203c0da6274SZhi-Jun Robin Fu bus_p = PCIE_DIP2BUS(dip); 204c0da6274SZhi-Jun Robin Fu bus_p->bus_cfgacc_base = mcfg_mem_base; 205c0da6274SZhi-Jun Robin Fu 206ae115bc7Smrj return (B_TRUE); 207ae115bc7Smrj } 208ae115bc7Smrj 209ae115bc7Smrj 210ae115bc7Smrj /* 211ae115bc7Smrj * add_nvidia_isa_bridge_props(): 212ae115bc7Smrj * To enable native hotplug; we need to map in two I/O BARs 213ae115bc7Smrj * from ISA bridge's config space 214ae115bc7Smrj * 215ae115bc7Smrj * NOTE: For now, this function is only used for Nvidia's CrushK 8-04 chipsets. 216ae115bc7Smrj */ 217ae115bc7Smrj void 218ae115bc7Smrj add_nvidia_isa_bridge_props(dev_info_t *dip, uchar_t bus, uchar_t dev, 219ae115bc7Smrj uchar_t func) 220ae115bc7Smrj { 221ae115bc7Smrj uint_t devloc, base; 222ae115bc7Smrj pci_regspec_t regs[2] = {{0}}; 223ae115bc7Smrj pci_regspec_t assigned[2] = {{0}}; 224ae115bc7Smrj 225ae115bc7Smrj devloc = (uint_t)bus << PCI_REG_BUS_SHIFT | 226ae115bc7Smrj (uint_t)dev << PCI_REG_DEV_SHIFT | 227ae115bc7Smrj (uint_t)func << PCI_REG_FUNC_SHIFT; 228ae115bc7Smrj regs[0].pci_phys_hi = devloc; 229ae115bc7Smrj 230ae115bc7Smrj /* System Control BAR i/o space */ 231ae115bc7Smrj base = (*pci_getl_func)(bus, dev, func, 232ae115bc7Smrj NVIDIA_CK804_ISA_SYSCTRL_BAR_OFF); 233ae115bc7Smrj regs[0].pci_size_low = assigned[0].pci_size_low = PCI_CONF_HDR_SIZE; 234ae115bc7Smrj assigned[0].pci_phys_hi = regs[0].pci_phys_hi = (PCI_RELOCAT_B | 235ae115bc7Smrj PCI_ADDR_IO | devloc | NVIDIA_CK804_ISA_SYSCTRL_BAR_OFF); 236ae115bc7Smrj assigned[0].pci_phys_low = regs[0].pci_phys_low = 237ae115bc7Smrj base & PCI_BASE_IO_ADDR_M; 238ae115bc7Smrj 239ae115bc7Smrj /* Analog BAR i/o space */ 240ae115bc7Smrj base = (*pci_getl_func)(bus, dev, func, 241ae115bc7Smrj NVIDIA_CK804_ISA_ANALOG_BAR_OFF); 242ae115bc7Smrj regs[1].pci_size_low = assigned[1].pci_size_low = PCI_CONF_HDR_SIZE; 243ae115bc7Smrj assigned[1].pci_phys_hi = regs[1].pci_phys_hi = (PCI_RELOCAT_B | 244ae115bc7Smrj PCI_ADDR_IO | devloc | NVIDIA_CK804_ISA_ANALOG_BAR_OFF); 245ae115bc7Smrj assigned[1].pci_phys_low = regs[1].pci_phys_low = 246ae115bc7Smrj base & PCI_BASE_IO_ADDR_M; 247ae115bc7Smrj 248ae115bc7Smrj (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg", 249ae115bc7Smrj (int *)regs, 2 * sizeof (pci_regspec_t) / sizeof (int)); 250ae115bc7Smrj (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 251ae115bc7Smrj "assigned-addresses", 252ae115bc7Smrj (int *)assigned, 2 * sizeof (pci_regspec_t) / sizeof (int)); 253ae115bc7Smrj } 254