xref: /onnv-gate/usr/src/uts/intel/io/pciex/pcie_nvidia.c (revision 12054:bcacc803343d)
13446Smrj /*
23446Smrj  * CDDL HEADER START
33446Smrj  *
43446Smrj  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
73446Smrj  *
83446Smrj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93446Smrj  * or http://www.opensolaris.org/os/licensing.
103446Smrj  * See the License for the specific language governing permissions
113446Smrj  * and limitations under the License.
123446Smrj  *
133446Smrj  * When distributing Covered Code, include this CDDL HEADER in each
143446Smrj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153446Smrj  * If applicable, add the following below this CDDL HEADER, with the
163446Smrj  * fields enclosed by brackets "[]" replaced with your own identifying
173446Smrj  * information: Portions Copyright [yyyy] [name of copyright owner]
183446Smrj  *
193446Smrj  * CDDL HEADER END
203446Smrj  */
213446Smrj 
223446Smrj /*
23*12054SStephen.Hanson@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
243446Smrj  */
253446Smrj 
263446Smrj /*
273446Smrj  *	Library file that has code for PCIe booting
283446Smrj  */
293446Smrj 
303446Smrj #include <sys/conf.h>
313446Smrj #include <sys/pci.h>
323446Smrj #include <sys/sunndi.h>
333446Smrj #include <sys/pcie.h>
3411245SZhijun.Fu@Sun.COM #include <sys/pcie_impl.h>
353446Smrj #include <sys/pci_cfgspace.h>
363446Smrj #include <io/pciex/pcie_nvidia.h>
373446Smrj 
383446Smrj /*
393446Smrj  * PCI Configuration (Nvidia chipsets, PCIe) related library functions
403446Smrj  */
413446Smrj 
423446Smrj /* Globals */
433446Smrj extern int pci_boot_debug;
443446Smrj 
4511245SZhijun.Fu@Sun.COM extern uint64_t mcfg_mem_base;
4611245SZhijun.Fu@Sun.COM 
473446Smrj boolean_t
check_if_device_is_pciex(dev_info_t * cdip,uchar_t bus,uchar_t dev,uchar_t func,boolean_t * slot_valid,ushort_t * slot_number,ushort_t * is_pci_bridge)483446Smrj check_if_device_is_pciex(dev_info_t *cdip, uchar_t bus, uchar_t dev,
49*12054SStephen.Hanson@Sun.COM     uchar_t func, boolean_t *slot_valid, ushort_t *slot_number,
50*12054SStephen.Hanson@Sun.COM     ushort_t *is_pci_bridge)
513446Smrj {
523446Smrj 	boolean_t found_pciex = B_FALSE;
533446Smrj 	ushort_t cap;
543446Smrj 	ushort_t capsp;
553446Smrj 	ushort_t cap_count = PCI_CAP_MAX_PTR;
563446Smrj 	ushort_t status;
573446Smrj 	uint32_t slot_cap;
583446Smrj 
59*12054SStephen.Hanson@Sun.COM 	*slot_valid = B_FALSE;
603446Smrj 
613446Smrj 	status = (*pci_getw_func)(bus, dev, func, PCI_CONF_STAT);
623446Smrj 	if (!(status & PCI_STAT_CAP))
633446Smrj 		return (B_FALSE);
643446Smrj 
653446Smrj 	capsp = (*pci_getb_func)(bus, dev, func, PCI_CONF_CAP_PTR);
663446Smrj 	while (cap_count-- && capsp >= PCI_CAP_PTR_OFF) {
673446Smrj 		capsp &= PCI_CAP_PTR_MASK;
683446Smrj 		cap = (*pci_getb_func)(bus, dev, func, capsp);
693446Smrj 
703446Smrj 		if (cap == PCI_CAP_ID_PCI_E) {
713446Smrj #ifdef	DEBUG
723446Smrj 			if (pci_boot_debug)
733446Smrj 				cmn_err(CE_CONT, "PCI-Express (%x,%x,%x) "
743446Smrj 				    "capability found\n", bus, dev, func);
753446Smrj #endif	/* DEBUG */
763446Smrj 
773446Smrj 			status = (*pci_getw_func)(bus, dev, func, capsp + 2);
783446Smrj 			/*
793446Smrj 			 * See section 7.8.2 of PCI-Express Base Spec v1.0a
803446Smrj 			 * for Device/Port Type.
813446Smrj 			 * PCIE_PCIECAP_DEV_TYPE_PCIE2PCI implies that the
823446Smrj 			 * device is a PCIe2PCI bridge
833446Smrj 			 */
843446Smrj 			*is_pci_bridge =
857987SErwin.Tsaur@Sun.COM 			    ((status & PCIE_PCIECAP_DEV_TYPE_MASK) ==
863446Smrj 			    PCIE_PCIECAP_DEV_TYPE_PCIE2PCI) ? 1 : 0;
873446Smrj 
883446Smrj 			/*
893446Smrj 			 * Check for "Slot  Implemented" bit
903446Smrj 			 * PCIE_PCIECAP_SLOT_IMPL implies that.
913446Smrj 			 */
923446Smrj 			if (status & PCIE_PCIECAP_SLOT_IMPL) {
933446Smrj 				/* offset 14h is Slot Cap Register */
943446Smrj 				slot_cap = (*pci_getl_func)(bus, dev, func,
953446Smrj 				    capsp + PCIE_SLOTCAP);
96*12054SStephen.Hanson@Sun.COM 				*slot_valid = B_TRUE;
973446Smrj 				*slot_number =
983446Smrj 				    PCIE_SLOTCAP_PHY_SLOT_NUM(slot_cap);
993446Smrj 
1003446Smrj 				/* Is PCI Express HotPlug capability set? */
1013446Smrj 				if (cdip &&
1023446Smrj 				    (slot_cap & PCIE_SLOTCAP_HP_CAPABLE)) {
1033446Smrj 					(void) ndi_prop_update_int(
1043446Smrj 					    DDI_DEV_T_NONE, cdip,
1053446Smrj 					    "pci-hotplug-type",
1063446Smrj 					    INBAND_HPC_PCIE);
1073446Smrj 				}
1083446Smrj 			}
1093446Smrj 
1103446Smrj 			found_pciex = B_TRUE;
1113446Smrj 		}
1123446Smrj 
1133446Smrj 		if (cdip && (cap == PCI_CAP_ID_PCI_HOTPLUG)) {
1143446Smrj 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip,
1153446Smrj 			    "pci-hotplug-type", INBAND_HPC_SHPC);
1163446Smrj 		}
1173446Smrj 
1183446Smrj 		capsp = (*pci_getb_func)(bus, dev, func,
1193446Smrj 		    capsp + PCI_CAP_NEXT_PTR);
1203446Smrj 	}
1213446Smrj 
1223446Smrj 	return (found_pciex);
1233446Smrj }
1243446Smrj 
1253446Smrj 
1263446Smrj /*
1273446Smrj  * scan all buses, devices, functions to look for any
1283446Smrj  * PCI-Express device in the system.
1293446Smrj  * If found, return B_TRUE else B_FALSE
1303446Smrj  */
13111245SZhijun.Fu@Sun.COM boolean_t
look_for_any_pciex_device(uchar_t bus)1323446Smrj look_for_any_pciex_device(uchar_t bus)
1333446Smrj {
1343446Smrj 	uchar_t dev, func;
1353446Smrj 	uchar_t nfunc, header;
1363446Smrj 	ushort_t venid, slot_num, is_pci_bridge = 0;
137*12054SStephen.Hanson@Sun.COM 	boolean_t slot_valid;
1383446Smrj 
1393446Smrj 	for (dev = 0; dev < 32; dev++) {
1403446Smrj 		nfunc = 1;
1413446Smrj 		for (func = 0; func < nfunc; func++) {
1423446Smrj #ifdef	DEBUG
1433446Smrj 			if (pci_boot_debug)
1443446Smrj 				cmn_err(CE_NOTE, "pciex dev 0x%x, func 0x%x",
1453446Smrj 				    dev, func);
1463446Smrj #endif	/* DEBUG */
1473446Smrj 
1483446Smrj 			venid = (*pci_getw_func)(bus, dev, func,
1493446Smrj 			    PCI_CONF_VENID);
1503446Smrj 			/* no function at this address */
1513446Smrj 			if ((venid == 0xffff) || (venid == 0))
1523446Smrj 				continue;
1533446Smrj 
1543446Smrj 			header = (*pci_getb_func)(bus, dev, func,
1553446Smrj 			    PCI_CONF_HEADER);
1563446Smrj 			if (header == 0xff)
1573446Smrj 				continue; /* illegal value */
1583446Smrj 
1593446Smrj 			/*
1603446Smrj 			 * according to some mail from Microsoft posted to
1613446Smrj 			 * the pci-drivers alias, their only requirement for
1623446Smrj 			 * a multifunction device is for the 1st function to
1633446Smrj 			 * have to PCI_HEADER_MULTI bit set.
1643446Smrj 			 */
1653446Smrj 			if ((func == 0) && (header & PCI_HEADER_MULTI))
1663446Smrj 				nfunc = 8;
1673446Smrj 
1683446Smrj 			if (check_if_device_is_pciex(NULL, bus, dev, func,
169*12054SStephen.Hanson@Sun.COM 			    &slot_valid, &slot_num, &is_pci_bridge) == B_TRUE)
1703446Smrj 				return (B_TRUE);
1713446Smrj 		} /* end of func */
1723446Smrj 	} /* end of dev */
1733446Smrj 
1743446Smrj 	return (B_FALSE);
1753446Smrj }
1763446Smrj 
1773446Smrj boolean_t
create_pcie_root_bus(uchar_t bus,dev_info_t * dip)1783446Smrj create_pcie_root_bus(uchar_t bus, dev_info_t *dip)
1793446Smrj {
18011245SZhijun.Fu@Sun.COM 	pcie_bus_t *bus_p;
18111245SZhijun.Fu@Sun.COM 
1823446Smrj 	/*
1833446Smrj 	 * Currently this is being hard-coded.
1843446Smrj 	 * We need to figure out if the root bus does indeed
1853446Smrj 	 * have PCI-Ex in the path by looking for MCFG in
1863446Smrj 	 * the ACPI tables
1873446Smrj 	 */
1883446Smrj 	if (look_for_any_pciex_device(bus) == B_FALSE)
1893446Smrj 		return (B_FALSE);
1903446Smrj 
1913446Smrj #ifdef	DEBUG
1923446Smrj 	if (pci_boot_debug)
1933446Smrj 		cmn_err(CE_CONT, "Found PCI-Ex in the system\n");
1943446Smrj #endif	/* DEBUG */
1953446Smrj 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1963446Smrj 	    "device_type", "pciex");
1973446Smrj 	(void) ndi_prop_update_string(DDI_DEV_T_NONE, dip,
1983446Smrj 	    "compatible", "pciex_root_complex");
1993446Smrj 
20011245SZhijun.Fu@Sun.COM 	pcie_rc_init_bus(dip);
20111245SZhijun.Fu@Sun.COM 
20211245SZhijun.Fu@Sun.COM 	/* save base addr in bus_t for pci_cfgacc_xxx() */
20311245SZhijun.Fu@Sun.COM 	bus_p = PCIE_DIP2BUS(dip);
20411245SZhijun.Fu@Sun.COM 	bus_p->bus_cfgacc_base = mcfg_mem_base;
20511245SZhijun.Fu@Sun.COM 
2063446Smrj 	return (B_TRUE);
2073446Smrj }
2083446Smrj 
2093446Smrj 
2103446Smrj /*
2113446Smrj  * add_nvidia_isa_bridge_props():
2123446Smrj  *	To enable native hotplug; we need to map in two I/O BARs
2133446Smrj  *	from ISA bridge's config space
2143446Smrj  *
2153446Smrj  * NOTE: For now, this function is only used for Nvidia's CrushK 8-04 chipsets.
2163446Smrj  */
2173446Smrj void
add_nvidia_isa_bridge_props(dev_info_t * dip,uchar_t bus,uchar_t dev,uchar_t func)2183446Smrj add_nvidia_isa_bridge_props(dev_info_t *dip, uchar_t bus, uchar_t dev,
2193446Smrj     uchar_t func)
2203446Smrj {
2213446Smrj 	uint_t devloc, base;
2223446Smrj 	pci_regspec_t regs[2] = {{0}};
2233446Smrj 	pci_regspec_t assigned[2] = {{0}};
2243446Smrj 
2253446Smrj 	devloc = (uint_t)bus << PCI_REG_BUS_SHIFT |
2263446Smrj 	    (uint_t)dev << PCI_REG_DEV_SHIFT |
2273446Smrj 	    (uint_t)func << PCI_REG_FUNC_SHIFT;
2283446Smrj 	regs[0].pci_phys_hi = devloc;
2293446Smrj 
2303446Smrj 	/* System Control BAR i/o space */
2313446Smrj 	base = (*pci_getl_func)(bus, dev, func,
2323446Smrj 	    NVIDIA_CK804_ISA_SYSCTRL_BAR_OFF);
2333446Smrj 	regs[0].pci_size_low = assigned[0].pci_size_low = PCI_CONF_HDR_SIZE;
2343446Smrj 	assigned[0].pci_phys_hi = regs[0].pci_phys_hi = (PCI_RELOCAT_B |
2353446Smrj 	    PCI_ADDR_IO | devloc | NVIDIA_CK804_ISA_SYSCTRL_BAR_OFF);
2363446Smrj 	assigned[0].pci_phys_low = regs[0].pci_phys_low =
2373446Smrj 	    base & PCI_BASE_IO_ADDR_M;
2383446Smrj 
2393446Smrj 	/* Analog BAR i/o space */
2403446Smrj 	base = (*pci_getl_func)(bus, dev, func,
2413446Smrj 	    NVIDIA_CK804_ISA_ANALOG_BAR_OFF);
2423446Smrj 	regs[1].pci_size_low = assigned[1].pci_size_low = PCI_CONF_HDR_SIZE;
2433446Smrj 	assigned[1].pci_phys_hi = regs[1].pci_phys_hi = (PCI_RELOCAT_B |
2443446Smrj 	    PCI_ADDR_IO | devloc | NVIDIA_CK804_ISA_ANALOG_BAR_OFF);
2453446Smrj 	assigned[1].pci_phys_low = regs[1].pci_phys_low =
2463446Smrj 	    base & PCI_BASE_IO_ADDR_M;
2473446Smrj 
2483446Smrj 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg",
2493446Smrj 	    (int *)regs, 2 * sizeof (pci_regspec_t) / sizeof (int));
2503446Smrj 	(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip,
2513446Smrj 	    "assigned-addresses",
2523446Smrj 	    (int *)assigned, 2 * sizeof (pci_regspec_t) / sizeof (int));
2533446Smrj }
254