xref: /illumos-gate/usr/src/uts/common/io/pciex/pcie_pwr.c (revision 86ef0a63)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
501689544Sjchu  * Common Development and Distribution License (the "License").
601689544Sjchu  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22d4bc0535SKrishna Elango  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24b3d69c05SRobert Mustacchi  * Copyright 2019 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
297c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
327c478bd9Sstevel@tonic-gate #include <sys/sunpm.h>
337c478bd9Sstevel@tonic-gate #include <sys/epm.h>
347c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
367c478bd9Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
37bf8fc234Set #include <sys/pcie.h>
388bc7d88aSet #include <sys/pcie_impl.h>
39bf8fc234Set #include <sys/promif.h>		/* prom_printf */
40d4bc0535SKrishna Elango #include <sys/pcie_pwr.h>
41bf8fc234Set 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * This file implements the power management functionality for
447c478bd9Sstevel@tonic-gate  * pci express switch and pci express-to-pci/pci-x bridge. All the
457c478bd9Sstevel@tonic-gate  * code in this file is generic and is not specific to a particular chip.
467c478bd9Sstevel@tonic-gate  * The algorithm, which decides when to go to a lower power is explained
477c478bd9Sstevel@tonic-gate  * below:
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *	1. Initially when no children are attached, the driver is idle from
507c478bd9Sstevel@tonic-gate  *	PM framework point of view ( PM idle/PM busy).
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *	2. Driver is PM busy if either a reference count called pwr_hold is
537c478bd9Sstevel@tonic-gate  *	greater than zero or driver is already at the lowest possible power
547c478bd9Sstevel@tonic-gate  *	level. The lowest possible power level for the driver is equal to the
557c478bd9Sstevel@tonic-gate  *	highest power level among its children. The PM busy condition is
567c478bd9Sstevel@tonic-gate  *	indicated by PCIE_PM_BUSY bit. At any point, only one pm_busy_component
577c478bd9Sstevel@tonic-gate  *	call is made for a nexus driver instance.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *	3. Driver is PM idle if the pwr_hold is zero and the lowest
607c478bd9Sstevel@tonic-gate  *	possible power level is less than the driver's current power level.
617c478bd9Sstevel@tonic-gate  *	At any point, only one pm_idle_component call is made for a nexus
627c478bd9Sstevel@tonic-gate  *	driver instance.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *	4. For any events like child attach, it increments pwr_hold and marks
657c478bd9Sstevel@tonic-gate  *	itslef busy, if it is not already done so. This temporary hold is
667c478bd9Sstevel@tonic-gate  *	removed when the event is complete.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *	5. Any child's power change requires the parent (this driver) to be
697c478bd9Sstevel@tonic-gate  *	full power. So it raises its power and increments pwr_hold. It also
707c478bd9Sstevel@tonic-gate  *	marks itself temporarily busy, if it is not already done. This hold
717c478bd9Sstevel@tonic-gate  *	is removed when the child power change is complete.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  *	6. After each child power change, it evaluates what is the lowest
747c478bd9Sstevel@tonic-gate  *	possible power level. If the lowest possible power level is less than
757c478bd9Sstevel@tonic-gate  *	the current power level and pwr_hold is zero, then it marks itself
767c478bd9Sstevel@tonic-gate  *	idle. The lowest power level is equal or greater than the highest level
777c478bd9Sstevel@tonic-gate  *	among the children. It keeps track of children's power level by
787c478bd9Sstevel@tonic-gate  *	using counters.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  *	7. Any code e.g., which is accessing the driver's own registers should
817c478bd9Sstevel@tonic-gate  *	place a temporary hold using pcie_pm_hold.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static int pcie_pwr_change(dev_info_t *dip, pcie_pwr_t *pwr_p, int new);
857c478bd9Sstevel@tonic-gate static void pwr_update_counters(int *countersp, int olevel, int nlevel);
867c478bd9Sstevel@tonic-gate static int pwr_level_allowed(pcie_pwr_t *pwr_p);
877c478bd9Sstevel@tonic-gate static void pcie_add_comps(dev_info_t *dip, dev_info_t *cdip,
887c478bd9Sstevel@tonic-gate     pcie_pwr_t *pwr_p);
897c478bd9Sstevel@tonic-gate static void pcie_remove_comps(dev_info_t *dip, dev_info_t *cdip,
907c478bd9Sstevel@tonic-gate     pcie_pwr_t *pwr_p);
917c478bd9Sstevel@tonic-gate static void pcie_pm_subrelease(dev_info_t *dip, pcie_pwr_t *pwr_p);
92eae2e508Skrishnae static boolean_t pcie_is_pcie(dev_info_t *dip);
937c478bd9Sstevel@tonic-gate #ifdef DEBUG
947c478bd9Sstevel@tonic-gate static char *pcie_decode_pwr_op(pm_bus_power_op_t op);
957c478bd9Sstevel@tonic-gate #else
967c478bd9Sstevel@tonic-gate #define	pcie_decode_pwr_op
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * power entry point.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  * This function decides whether the PM request is honorable.
1037c478bd9Sstevel@tonic-gate  * If yes, it then does what's necessary for switch or
1047c478bd9Sstevel@tonic-gate  *    bridge to change its power.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate /* ARGSUSED */
1077c478bd9Sstevel@tonic-gate int
pcie_power(dev_info_t * dip,int component,int level)1087c478bd9Sstevel@tonic-gate pcie_power(dev_info_t *dip, int component, int level)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p = PCIE_NEXUS_PMINFO(dip);
1117c478bd9Sstevel@tonic-gate 	int *counters = pwr_p->pwr_counters;
1127c478bd9Sstevel@tonic-gate 	int pmcaps = pwr_p->pwr_pmcaps;
1137c478bd9Sstevel@tonic-gate 	int ret = DDI_FAILURE;
1147c478bd9Sstevel@tonic-gate 
115*86ef0a63SRichard Lowe #if defined(__x86)
116d4bc0535SKrishna Elango 	if (dip)
117d4bc0535SKrishna Elango 		return (DDI_SUCCESS);
118*86ef0a63SRichard Lowe #endif /* defined(__x86) */
119d4bc0535SKrishna Elango 
1207c478bd9Sstevel@tonic-gate 	ASSERT(level != PM_LEVEL_UNKNOWN);
1217c478bd9Sstevel@tonic-gate 	/* PM should not asking for a level, which is unsupported */
1227c478bd9Sstevel@tonic-gate 	ASSERT(level == PM_LEVEL_D0 || level == PM_LEVEL_D3 ||
1237c478bd9Sstevel@tonic-gate 	    (level == PM_LEVEL_D1 && (pmcaps & PCIE_SUPPORTS_D1)) ||
1247c478bd9Sstevel@tonic-gate 	    (level == PM_LEVEL_D2 && (pmcaps & PCIE_SUPPORTS_D2)));
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
127e762302fSShesha Sreenivasamurthy 	PCIE_DBG("%s(%d): pcie_power: change from %d to %d\n",
128e762302fSShesha Sreenivasamurthy 	    ddi_driver_name(dip), ddi_get_instance(dip), pwr_p->pwr_func_lvl,
129e762302fSShesha Sreenivasamurthy 	    level);
1307c478bd9Sstevel@tonic-gate 	if (pwr_p->pwr_func_lvl == level) {
131e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pcie_power: already at %d\n",
132e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(dip), ddi_get_instance(dip), level);
1337c478bd9Sstevel@tonic-gate 		ret = DDI_SUCCESS;
1347c478bd9Sstevel@tonic-gate 		goto pcie_pwr_done;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (level < pwr_p->pwr_func_lvl) {
1387c478bd9Sstevel@tonic-gate 		/*
1397c478bd9Sstevel@tonic-gate 		 * Going to lower power. Reject this if we are either busy
1407c478bd9Sstevel@tonic-gate 		 * or there is a hold.
1417c478bd9Sstevel@tonic-gate 		 */
1427c478bd9Sstevel@tonic-gate 		if (pwr_p->pwr_flags & PCIE_PM_BUSY) {
143e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): pcie_power: rejecting change to %d "
144e762302fSShesha Sreenivasamurthy 			    "as busy\n", ddi_driver_name(dip),
145e762302fSShesha Sreenivasamurthy 			    ddi_get_instance(dip), level);
1467c478bd9Sstevel@tonic-gate 			goto pcie_pwr_done;
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		/*
1507c478bd9Sstevel@tonic-gate 		 * Now we know that we are neither busy nor there is a hold.
1517c478bd9Sstevel@tonic-gate 		 * At this point none of the children should be at full power.
1527c478bd9Sstevel@tonic-gate 		 * Reject the request if level reqested is lower than the level
1537c478bd9Sstevel@tonic-gate 		 * possible.
1547c478bd9Sstevel@tonic-gate 		 */
1557c478bd9Sstevel@tonic-gate 		ASSERT(!counters[PCIE_D0_INDEX] &&
1567c478bd9Sstevel@tonic-gate 		    !counters[PCIE_UNKNOWN_INDEX]);
1577c478bd9Sstevel@tonic-gate 		if (level < pwr_level_allowed(pwr_p)) {
158e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): pcie_power: rejecting level %d as"
159e762302fSShesha Sreenivasamurthy 			    " %d is the lowest possible\n",
160e762302fSShesha Sreenivasamurthy 			    ddi_driver_name(dip), ddi_get_instance(dip), level,
1617c478bd9Sstevel@tonic-gate 			    pwr_level_allowed(pwr_p));
1627c478bd9Sstevel@tonic-gate 			goto pcie_pwr_done;
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	if (pcie_pwr_change(dip, pwr_p, level) != DDI_SUCCESS) {
167e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pcie_power: attempt to change to %d "
168e762302fSShesha Sreenivasamurthy 		    " failed \n", ddi_driver_name(dip), ddi_get_instance(dip),
169e762302fSShesha Sreenivasamurthy 		    level);
1707c478bd9Sstevel@tonic-gate 		goto pcie_pwr_done;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	pwr_p->pwr_func_lvl = level;
173e762302fSShesha Sreenivasamurthy 	PCIE_DBG("%s(%d): pcie_power: level changed to %d \n",
174e762302fSShesha Sreenivasamurthy 	    ddi_driver_name(dip), ddi_get_instance(dip), level);
1757c478bd9Sstevel@tonic-gate 	ret = DDI_SUCCESS;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate pcie_pwr_done:
1787c478bd9Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
1797c478bd9Sstevel@tonic-gate 	return (ret);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Called by pcie_power() only. Caller holds the pwr_lock.
1847c478bd9Sstevel@tonic-gate  *
1857c478bd9Sstevel@tonic-gate  * dip - dev_info pointer
1867c478bd9Sstevel@tonic-gate  * pwr_p - pm info for the node.
1877c478bd9Sstevel@tonic-gate  * new     - new level
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate static int
pcie_pwr_change(dev_info_t * dip,pcie_pwr_t * pwr_p,int new)1907c478bd9Sstevel@tonic-gate pcie_pwr_change(dev_info_t *dip, pcie_pwr_t *pwr_p, int new)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	uint16_t pmcsr;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pwr_p->pwr_lock));
1957c478bd9Sstevel@tonic-gate 	ASSERT(new != pwr_p->pwr_func_lvl);
1967c478bd9Sstevel@tonic-gate 	pmcsr = pci_config_get16(pwr_p->pwr_conf_hdl, pwr_p->pwr_pmcsr_offset);
1977c478bd9Sstevel@tonic-gate 	pmcsr &= ~PCI_PMCSR_STATE_MASK;
1987c478bd9Sstevel@tonic-gate 	switch (new) {
1997c478bd9Sstevel@tonic-gate 	case PM_LEVEL_D0:
2007c478bd9Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D0;
2017c478bd9Sstevel@tonic-gate 		break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	case PM_LEVEL_D1:
2047c478bd9Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D1;
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	case PM_LEVEL_D2:
2087c478bd9Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D2;
2097c478bd9Sstevel@tonic-gate 		break;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	case PM_LEVEL_D3:
2127c478bd9Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D3HOT;
2137c478bd9Sstevel@tonic-gate 		break;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	default:
2167c478bd9Sstevel@tonic-gate 		ASSERT(0);
2177c478bd9Sstevel@tonic-gate 		break;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	/* Save config space, if going to D3 */
2207c478bd9Sstevel@tonic-gate 	if (new == PM_LEVEL_D3) {
221e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pwr_change: saving config space regs\n",
222e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(dip), ddi_get_instance(dip));
2237c478bd9Sstevel@tonic-gate 		if (pci_save_config_regs(dip) != DDI_SUCCESS) {
224e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): pcie_pwr_change: failed to save "
225e762302fSShesha Sreenivasamurthy 			    "config space regs\n", ddi_driver_name(dip),
226e762302fSShesha Sreenivasamurthy 			    ddi_get_instance(dip));
2277c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	pci_config_put16(pwr_p->pwr_conf_hdl, pwr_p->pwr_pmcsr_offset, pmcsr);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * TBD: Taken from pci_pci driver. Is this required?
2357c478bd9Sstevel@tonic-gate 	 * No bus transactions should occur without waiting for
2367c478bd9Sstevel@tonic-gate 	 * settle time specified in PCI PM spec rev 2.1 sec 5.6.1
2377c478bd9Sstevel@tonic-gate 	 * To make things simple, just use the max time specified for
2387c478bd9Sstevel@tonic-gate 	 * all state transitions.
2397c478bd9Sstevel@tonic-gate 	 */
2407c478bd9Sstevel@tonic-gate 	delay(drv_usectohz(PCI_CLK_SETTLE_TIME));
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * Restore config space if coming out of D3
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	if (pwr_p->pwr_func_lvl == PM_LEVEL_D3) {
246e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pcie_pwr_change: restoring config space\n",
247e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(dip), ddi_get_instance(dip));
2487c478bd9Sstevel@tonic-gate 		if (pci_restore_config_regs(dip) != DDI_SUCCESS) {
249e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): pcie_pwr_change: failed to restore "
250e762302fSShesha Sreenivasamurthy 			    "config space regs\n", ddi_driver_name(dip),
251e762302fSShesha Sreenivasamurthy 			    ddi_get_instance(dip));
2527c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * bus_ctlops.bus_power function.
2607c478bd9Sstevel@tonic-gate  *
2617c478bd9Sstevel@tonic-gate  * This function handles PRE_ POST_ change notifications, sent by
2627c478bd9Sstevel@tonic-gate  * PM framework related to child's power level change. It marks itself
2637c478bd9Sstevel@tonic-gate  * idle or busy based on the children's power level.
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate int
pcie_bus_power(dev_info_t * dip,void * impl_arg,pm_bus_power_op_t op,void * arg,void * result)2667c478bd9Sstevel@tonic-gate pcie_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
2677c478bd9Sstevel@tonic-gate     void *arg, void *result)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p = PCIE_NEXUS_PMINFO(dip);
2707c478bd9Sstevel@tonic-gate 	int *counters = pwr_p->pwr_counters; /* nexus counters */
2717c478bd9Sstevel@tonic-gate 	int *child_counters; /* per child dip counters */
2727c478bd9Sstevel@tonic-gate 	pm_bp_child_pwrchg_t *bpc;
2737c478bd9Sstevel@tonic-gate 	pm_bp_has_changed_t *bphc;
2747c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
2757c478bd9Sstevel@tonic-gate 	int new_level;
2767c478bd9Sstevel@tonic-gate 	int old_level;
2777c478bd9Sstevel@tonic-gate 	int rv = DDI_SUCCESS;
278672793b2Sjchu 	int level_allowed, comp;
2797c478bd9Sstevel@tonic-gate 
280*86ef0a63SRichard Lowe #if defined(__x86)
281d4bc0535SKrishna Elango 	if (dip)
282d4bc0535SKrishna Elango 		return (DDI_SUCCESS);
283*86ef0a63SRichard Lowe #endif /* defined(__x86) */
284d4bc0535SKrishna Elango 
2857c478bd9Sstevel@tonic-gate 	switch (op) {
2867c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
2877c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
2887c478bd9Sstevel@tonic-gate 		bpc = (pm_bp_child_pwrchg_t *)arg;
2897c478bd9Sstevel@tonic-gate 		cdip = bpc->bpc_dip;
2907c478bd9Sstevel@tonic-gate 		new_level = bpc->bpc_nlevel;
2917c478bd9Sstevel@tonic-gate 		old_level = bpc->bpc_olevel;
292672793b2Sjchu 		comp = bpc->bpc_comp;
2937c478bd9Sstevel@tonic-gate 		break;
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
2967c478bd9Sstevel@tonic-gate 		bphc = (pm_bp_has_changed_t *)arg;
2977c478bd9Sstevel@tonic-gate 		cdip = bphc->bphc_dip;
2987c478bd9Sstevel@tonic-gate 		new_level = bphc->bphc_nlevel;
2997c478bd9Sstevel@tonic-gate 		old_level = bphc->bphc_olevel;
300672793b2Sjchu 		comp = bphc->bphc_comp;
3017c478bd9Sstevel@tonic-gate 		break;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	default:
3047c478bd9Sstevel@tonic-gate 		break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	ASSERT(pwr_p);
3097c478bd9Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
3107c478bd9Sstevel@tonic-gate 	switch (op) {
3117c478bd9Sstevel@tonic-gate 	case BUS_POWER_PRE_NOTIFICATION:
312e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pcie_bus_power: %s@%d op %s %d->%d\n",
313e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(dip), ddi_get_instance(dip),
3147c478bd9Sstevel@tonic-gate 		    ddi_driver_name(cdip), ddi_get_instance(cdip),
3157c478bd9Sstevel@tonic-gate 		    pcie_decode_pwr_op(op), old_level, new_level);
3168a1b8da3Smg 		/*
3178a1b8da3Smg 		 * If the nexus doesn't want the child to go into
3188a1b8da3Smg 		 * non-D0 state, mark the child busy. This way PM
3198a1b8da3Smg 		 * framework will never try to lower the child's power.
3208a1b8da3Smg 		 * In case of pm_lower_power, marking busy won't help.
3218a1b8da3Smg 		 * So we need to specifically reject the attempt to
3228a1b8da3Smg 		 * go to non-D0 state.
3238a1b8da3Smg 		 */
3248a1b8da3Smg 		if (pwr_p->pwr_flags & PCIE_NO_CHILD_PM) {
3258a1b8da3Smg 			if (!PCIE_IS_COMPS_COUNTED(cdip)) {
326e762302fSShesha Sreenivasamurthy 				PCIE_DBG("%s(%d): pcie_bus_power: marking "
327e762302fSShesha Sreenivasamurthy 				    "child busy to disable pm \n",
328e762302fSShesha Sreenivasamurthy 				    ddi_driver_name(dip),
329e762302fSShesha Sreenivasamurthy 				    ddi_get_instance(dip));
3308a1b8da3Smg 				(void) pm_busy_component(cdip, 0);
3318a1b8da3Smg 			}
332672793b2Sjchu 			if (new_level < PM_LEVEL_D0 && !comp) {
333e762302fSShesha Sreenivasamurthy 				PCIE_DBG("%s(%d): pcie_bus_power: rejecting "
334e762302fSShesha Sreenivasamurthy 				    "child's attempt to go to %d\n",
335e762302fSShesha Sreenivasamurthy 				    ddi_driver_name(dip), ddi_get_instance(dip),
336e762302fSShesha Sreenivasamurthy 				    new_level);
3378a1b8da3Smg 				rv = DDI_FAILURE;
3388a1b8da3Smg 			}
3398a1b8da3Smg 		}
3407c478bd9Sstevel@tonic-gate 		mutex_exit(&pwr_p->pwr_lock);
3418a1b8da3Smg 		if (rv == DDI_SUCCESS)
3428a1b8da3Smg 			rv = pcie_pm_hold(dip);
3438a1b8da3Smg 		return (rv);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	case BUS_POWER_HAS_CHANGED:
3467c478bd9Sstevel@tonic-gate 	case BUS_POWER_POST_NOTIFICATION:
347e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pcie_bus_power: %s@%d op %s %d->%d\n",
348e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(dip), ddi_get_instance(dip),
3497c478bd9Sstevel@tonic-gate 		    ddi_driver_name(cdip), ddi_get_instance(cdip),
3507c478bd9Sstevel@tonic-gate 		    pcie_decode_pwr_op(op), old_level, new_level);
3517c478bd9Sstevel@tonic-gate 		/*
3527c478bd9Sstevel@tonic-gate 		 * Child device power changed
3537c478bd9Sstevel@tonic-gate 		 * If pm components of this child aren't accounted for
3547c478bd9Sstevel@tonic-gate 		 * then add the components to the counters. This can't
3557c478bd9Sstevel@tonic-gate 		 * be done in POST_ATTACH ctlop as pm info isn't created
3567c478bd9Sstevel@tonic-gate 		 * by then. Also because a driver can make a pm call during
3577c478bd9Sstevel@tonic-gate 		 * the attach.
3587c478bd9Sstevel@tonic-gate 		 */
3598a1b8da3Smg 		if (!PCIE_IS_COMPS_COUNTED(cdip)) {
3607c478bd9Sstevel@tonic-gate 			(void) pcie_pm_add_child(dip, cdip);
3618a1b8da3Smg 			if ((pwr_p->pwr_flags & PCIE_NO_CHILD_PM) &&
3628a1b8da3Smg 			    (op == BUS_POWER_HAS_CHANGED)) {
363e762302fSShesha Sreenivasamurthy 				PCIE_DBG("%s(%d): pcie_bus_power: marking "
364e762302fSShesha Sreenivasamurthy 				    "child busy to disable pm \n",
365e762302fSShesha Sreenivasamurthy 				    ddi_driver_name(dip),
366e762302fSShesha Sreenivasamurthy 				    ddi_get_instance(dip));
3678a1b8da3Smg 				(void) pm_busy_component(cdip, 0);
3688a1b8da3Smg 				/*
3698a1b8da3Smg 				 * If the driver has already changed to lower
3708a1b8da3Smg 				 * power(pm_power_has_changed) on its own,
3718a1b8da3Smg 				 * there is nothing we can do other than
3728a1b8da3Smg 				 * logging the warning message on the console.
3738a1b8da3Smg 				 */
3748a1b8da3Smg 				if (new_level < PM_LEVEL_D0)
3758a1b8da3Smg 					cmn_err(CE_WARN, "!Downstream device "
3768a1b8da3Smg 					    "%s@%d went to non-D0 state: "
3778a1b8da3Smg 					    "possible loss of link\n",
3788a1b8da3Smg 					    ddi_driver_name(cdip),
3798a1b8da3Smg 					    ddi_get_instance(cdip));
3808a1b8da3Smg 			}
3818a1b8da3Smg 		}
3828a1b8da3Smg 
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		/*
3857c478bd9Sstevel@tonic-gate 		 * If it is POST and device PM is supported, release the
3867c478bd9Sstevel@tonic-gate 		 * hold done in PRE.
3877c478bd9Sstevel@tonic-gate 		 */
3887c478bd9Sstevel@tonic-gate 		if (op == BUS_POWER_POST_NOTIFICATION &&
3897c478bd9Sstevel@tonic-gate 		    PCIE_SUPPORTS_DEVICE_PM(dip)) {
3907c478bd9Sstevel@tonic-gate 			pcie_pm_subrelease(dip, pwr_p);
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 		if (*((int *)result) == DDI_FAILURE) {
394e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): pcie_bus_power: change for %s%d "
395e762302fSShesha Sreenivasamurthy 			    "failed\n", ddi_driver_name(dip),
396e762302fSShesha Sreenivasamurthy 			    ddi_get_instance(dip), ddi_driver_name(cdip),
397e762302fSShesha Sreenivasamurthy 			    ddi_get_instance(cdip));
3987c478bd9Sstevel@tonic-gate 			break;
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 		/* Modify counters appropriately */
4017c478bd9Sstevel@tonic-gate 		pwr_update_counters(counters, old_level, new_level);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		child_counters = PCIE_CHILD_COUNTERS(cdip);
4047c478bd9Sstevel@tonic-gate 		pwr_update_counters(child_counters, old_level, new_level);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		/* If no device PM, return */
4077c478bd9Sstevel@tonic-gate 		if (!PCIE_SUPPORTS_DEVICE_PM(dip))
4087c478bd9Sstevel@tonic-gate 			break;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		level_allowed = pwr_level_allowed(pwr_p);
4117c478bd9Sstevel@tonic-gate 		/*
4127c478bd9Sstevel@tonic-gate 		 * Check conditions for marking busy
4137c478bd9Sstevel@tonic-gate 		 * Check the flag to set this busy only once for multiple
4147c478bd9Sstevel@tonic-gate 		 * busy conditions. Mark busy if our current lowest possible
4157c478bd9Sstevel@tonic-gate 		 * is equal or greater to the current level.
4167c478bd9Sstevel@tonic-gate 		 */
4177c478bd9Sstevel@tonic-gate 		if (level_allowed >= pwr_p->pwr_func_lvl &&
4187c478bd9Sstevel@tonic-gate 		    !(pwr_p->pwr_flags & PCIE_PM_BUSY)) {
419e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): pcie_bus_power: marking busy\n",
420e762302fSShesha Sreenivasamurthy 			    ddi_driver_name(dip), ddi_get_instance(dip));
4217c478bd9Sstevel@tonic-gate 			(void) pm_busy_component(dip, 0);
4227c478bd9Sstevel@tonic-gate 			pwr_p->pwr_flags |= PCIE_PM_BUSY;
4237c478bd9Sstevel@tonic-gate 			break;
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 		/*
4267c478bd9Sstevel@tonic-gate 		 * Check conditions for marking idle.
4277c478bd9Sstevel@tonic-gate 		 * If our lowest possible level is less than our current
4287c478bd9Sstevel@tonic-gate 		 * level mark idle. Mark idle only if it is not already done.
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		if ((level_allowed < pwr_p->pwr_func_lvl) &&
4317c478bd9Sstevel@tonic-gate 		    (pwr_p->pwr_hold == 0) &&
4327c478bd9Sstevel@tonic-gate 		    (pwr_p->pwr_flags & PCIE_PM_BUSY)) {
4337c478bd9Sstevel@tonic-gate 			/*
4347c478bd9Sstevel@tonic-gate 			 * For pci express, we should check here whether
4357c478bd9Sstevel@tonic-gate 			 * the link is in L1 state or not.
4367c478bd9Sstevel@tonic-gate 			 */
437e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): pcie_bus_power: marking idle\n",
438e762302fSShesha Sreenivasamurthy 			    ddi_driver_name(dip), ddi_get_instance(dip));
4397c478bd9Sstevel@tonic-gate 			(void) pm_idle_component(dip, 0);
4407c478bd9Sstevel@tonic-gate 			pwr_p->pwr_flags &= ~PCIE_PM_BUSY;
4417c478bd9Sstevel@tonic-gate 			break;
4427c478bd9Sstevel@tonic-gate 		}
4437c478bd9Sstevel@tonic-gate 		break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	default:
4467c478bd9Sstevel@tonic-gate 		mutex_exit(&pwr_p->pwr_lock);
4477c478bd9Sstevel@tonic-gate 		return (pm_busop_bus_power(dip, impl_arg, op, arg, result));
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
4507c478bd9Sstevel@tonic-gate 	return (rv);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate  * Decrement the count of children at olevel by one and increment
4557c478bd9Sstevel@tonic-gate  * count of children at nlevel by one.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate static void
pwr_update_counters(int * countersp,int olevel,int nlevel)4587c478bd9Sstevel@tonic-gate pwr_update_counters(int *countersp, int olevel, int nlevel)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	uint32_t	index;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	ASSERT(olevel >= PM_LEVEL_UNKNOWN && olevel <= PM_LEVEL_D0);
4637c478bd9Sstevel@tonic-gate 	ASSERT(nlevel >= PM_LEVEL_UNKNOWN && nlevel <= PM_LEVEL_D0);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	index = (olevel == PM_LEVEL_UNKNOWN ? PCIE_UNKNOWN_INDEX : olevel);
4667c478bd9Sstevel@tonic-gate 	countersp[index]--;
4677c478bd9Sstevel@tonic-gate 	index = (nlevel == PM_LEVEL_UNKNOWN ? PCIE_UNKNOWN_INDEX : nlevel);
4687c478bd9Sstevel@tonic-gate 	countersp[index]++;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * Returns the lowest possible power level allowed for nexus
4737c478bd9Sstevel@tonic-gate  * based on children's power level. Lowest possible level is
4747c478bd9Sstevel@tonic-gate  * equal to the highest level among the children. It also checks
4757c478bd9Sstevel@tonic-gate  * for the supported level
4767c478bd9Sstevel@tonic-gate  * UNKNOWN = D0 > D1 > D2 > D3
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate static int
pwr_level_allowed(pcie_pwr_t * pwr_p)4797c478bd9Sstevel@tonic-gate pwr_level_allowed(pcie_pwr_t *pwr_p)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	int *counters = pwr_p->pwr_counters;
4827c478bd9Sstevel@tonic-gate 	int i, j;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pwr_p->pwr_lock));
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * Search from UNKNOWN to D2. unknown is same as D0.
4877c478bd9Sstevel@tonic-gate 	 * find the highest level among the children. If that
4887c478bd9Sstevel@tonic-gate 	 * level is supported, return that level. If not,
4897c478bd9Sstevel@tonic-gate 	 * find the next higher supported level and return that
4907c478bd9Sstevel@tonic-gate 	 * level. For example, if the D1 is the highest among
4917c478bd9Sstevel@tonic-gate 	 * children and if D1 isn't supported return D0 as the
4927c478bd9Sstevel@tonic-gate 	 * lowest possible level. We don't need to look at D3
4937c478bd9Sstevel@tonic-gate 	 * as that is the default lowest level and it is always
4947c478bd9Sstevel@tonic-gate 	 * supported.
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	for (i = PCIE_UNKNOWN_INDEX; i > 0; i--) {
4977c478bd9Sstevel@tonic-gate 		if (counters[i]) {
4987c478bd9Sstevel@tonic-gate 			if (i == PCIE_UNKNOWN_INDEX)
4997c478bd9Sstevel@tonic-gate 				return (PM_LEVEL_D0);
5007c478bd9Sstevel@tonic-gate 			/*
5017c478bd9Sstevel@tonic-gate 			 * i is the highest level among children. If this is
5027c478bd9Sstevel@tonic-gate 			 * supported, return i.
5037c478bd9Sstevel@tonic-gate 			 */
5047c478bd9Sstevel@tonic-gate 			if (PCIE_LEVEL_SUPPORTED(pwr_p->pwr_pmcaps, i))
5057c478bd9Sstevel@tonic-gate 				return (i);
5067c478bd9Sstevel@tonic-gate 			/* find the next higher supported level */
5077c478bd9Sstevel@tonic-gate 			for (j = i + 1; j <= PCIE_D0_INDEX; j++) {
5087c478bd9Sstevel@tonic-gate 				if (PCIE_LEVEL_SUPPORTED(pwr_p->pwr_pmcaps, j))
5097c478bd9Sstevel@tonic-gate 					return (j);
5107c478bd9Sstevel@tonic-gate 			}
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	return (PM_LEVEL_D3);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate /*
5187c478bd9Sstevel@tonic-gate  * Update the counters with number pm components of the child
5197c478bd9Sstevel@tonic-gate  * all components are assumed to be at UNKNOWN level.
5207c478bd9Sstevel@tonic-gate  */
5217c478bd9Sstevel@tonic-gate static void
pcie_add_comps(dev_info_t * dip,dev_info_t * cdip,pcie_pwr_t * pwr_p)5227c478bd9Sstevel@tonic-gate pcie_add_comps(dev_info_t *dip, dev_info_t *cdip, pcie_pwr_t *pwr_p)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	int comps = PM_NUMCMPTS(cdip);
5257c478bd9Sstevel@tonic-gate 	pcie_pm_t *pcie_pm_p;
5267c478bd9Sstevel@tonic-gate 	pcie_pwr_child_t *cpwr_p;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pwr_p->pwr_lock));
5297c478bd9Sstevel@tonic-gate 	if (!comps)
5307c478bd9Sstevel@tonic-gate 		return;
5317c478bd9Sstevel@tonic-gate 
532e762302fSShesha Sreenivasamurthy 	PCIE_DBG("%s(%d): pcie_add_comps: unknown level counter incremented "
5337c478bd9Sstevel@tonic-gate 	    "from %d by %d because of %s@%d\n",
534e762302fSShesha Sreenivasamurthy 	    ddi_driver_name(dip), ddi_get_instance(dip),
5357c478bd9Sstevel@tonic-gate 	    (pwr_p->pwr_counters)[PCIE_UNKNOWN_INDEX], comps,
5367c478bd9Sstevel@tonic-gate 	    ddi_driver_name(cdip), ddi_get_instance(cdip));
5377c478bd9Sstevel@tonic-gate 	(pwr_p->pwr_counters)[PCIE_UNKNOWN_INDEX] += comps;
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * Allocate counters per child. This is a part of pcie
5407c478bd9Sstevel@tonic-gate 	 * pm info. If there is no pcie pm info, allocate it here.
5417c478bd9Sstevel@tonic-gate 	 * pcie pm info might already be there for pci express nexus
542d4bc0535SKrishna Elango 	 * driver e.g. pcieb. For all leaf nodes, it is allocated here.
5437c478bd9Sstevel@tonic-gate 	 */
5447c478bd9Sstevel@tonic-gate 	if ((pcie_pm_p = PCIE_PMINFO(cdip)) == NULL) {
5457c478bd9Sstevel@tonic-gate 		pcie_pm_p = (pcie_pm_t *)kmem_zalloc(
5467c478bd9Sstevel@tonic-gate 		    sizeof (pcie_pm_t), KM_SLEEP);
5477c478bd9Sstevel@tonic-gate 		PCIE_SET_PMINFO(cdip, pcie_pm_p);
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 	cpwr_p = (pcie_pwr_child_t *)kmem_zalloc(sizeof (pcie_pwr_child_t),
550eae2e508Skrishnae 	    KM_SLEEP);
5517c478bd9Sstevel@tonic-gate 	pcie_pm_p->pcie_par_pminfo = cpwr_p;
5527c478bd9Sstevel@tonic-gate 	(cpwr_p->pwr_child_counters)[PCIE_UNKNOWN_INDEX] += comps;
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * Remove the pm components of a child from our counters.
5577c478bd9Sstevel@tonic-gate  */
5587c478bd9Sstevel@tonic-gate static void
pcie_remove_comps(dev_info_t * dip,dev_info_t * cdip,pcie_pwr_t * pwr_p)5597c478bd9Sstevel@tonic-gate pcie_remove_comps(dev_info_t *dip, dev_info_t *cdip, pcie_pwr_t *pwr_p)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	int i;
5627c478bd9Sstevel@tonic-gate 	int *child_counters;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pwr_p->pwr_lock));
5657c478bd9Sstevel@tonic-gate 	if (!(PCIE_PMINFO(cdip)) || !PCIE_PAR_PMINFO(cdip)) {
5667c478bd9Sstevel@tonic-gate 		if (PCIE_SUPPORTS_DEVICE_PM(dip)) {
5677c478bd9Sstevel@tonic-gate 			/*
5687c478bd9Sstevel@tonic-gate 			 * Driver never made a PM call and we didn't create
5697c478bd9Sstevel@tonic-gate 			 * any counters for this device. This also means that
5707c478bd9Sstevel@tonic-gate 			 * hold made at the PRE_ATTACH time, still remains.
5717c478bd9Sstevel@tonic-gate 			 * Remove the hold now. The correct thing to do is to
5727c478bd9Sstevel@tonic-gate 			 * stay at full power when a child is at full power
5737c478bd9Sstevel@tonic-gate 			 * whether a driver is there or not. This will be
5747c478bd9Sstevel@tonic-gate 			 * implemented in the future.
5757c478bd9Sstevel@tonic-gate 			 */
5767c478bd9Sstevel@tonic-gate 			pcie_pm_subrelease(dip, pwr_p);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 		return;
5797c478bd9Sstevel@tonic-gate 	}
580e762302fSShesha Sreenivasamurthy 	PCIE_DBG("%s(%d): pcie_remove_comps:counters decremented because of "
581e762302fSShesha Sreenivasamurthy 	    "%s@%d\n", ddi_driver_name(dip), ddi_get_instance(dip),
582e762302fSShesha Sreenivasamurthy 	    ddi_driver_name(cdip), ddi_get_instance(cdip));
5837c478bd9Sstevel@tonic-gate 	child_counters = PCIE_CHILD_COUNTERS(cdip);
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * Adjust the nexus counters. No need to adjust per child dip
5867c478bd9Sstevel@tonic-gate 	 * counters as we are freeing the per child dip info.
5877c478bd9Sstevel@tonic-gate 	 */
5887c478bd9Sstevel@tonic-gate 	for (i = 0; i < PCIE_MAX_PWR_LEVELS; i++) {
5897c478bd9Sstevel@tonic-gate 		ASSERT((pwr_p->pwr_counters)[i] >= child_counters[i]);
5907c478bd9Sstevel@tonic-gate 		(pwr_p->pwr_counters)[i] -= child_counters[i];
5917c478bd9Sstevel@tonic-gate 	}
5927c478bd9Sstevel@tonic-gate 	/* remove both parent pm info and pcie pminfo itself */
5937c478bd9Sstevel@tonic-gate 	kmem_free(PCIE_PAR_PMINFO(cdip), sizeof (pcie_pwr_child_t));
5947c478bd9Sstevel@tonic-gate 	kmem_free(PCIE_PMINFO(cdip), sizeof (pcie_pm_t));
5957c478bd9Sstevel@tonic-gate 	PCIE_RESET_PMINFO(cdip);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
599d4bc0535SKrishna Elango  * Power management related initialization common to px and pcieb
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate int
pwr_common_setup(dev_info_t * dip)6027c478bd9Sstevel@tonic-gate pwr_common_setup(dev_info_t *dip)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	pcie_pm_t		*pcie_pm_p;
6057c478bd9Sstevel@tonic-gate 	pcie_pwr_t		*pwr_p;
6067c478bd9Sstevel@tonic-gate 	int			pminfo_created = 0;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* Create pminfo, if it doesn't exist already */
6097c478bd9Sstevel@tonic-gate 	if ((pcie_pm_p = PCIE_PMINFO(dip)) == NULL) {
6107c478bd9Sstevel@tonic-gate 		pcie_pm_p = (pcie_pm_t *)kmem_zalloc(
6117c478bd9Sstevel@tonic-gate 		    sizeof (pcie_pm_t), KM_SLEEP);
6127c478bd9Sstevel@tonic-gate 		PCIE_SET_PMINFO(dip, pcie_pm_p);
6137c478bd9Sstevel@tonic-gate 		pminfo_created = 1;
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	pwr_p = (pcie_pwr_t *)kmem_zalloc(sizeof (pcie_pwr_t), KM_SLEEP);
6167c478bd9Sstevel@tonic-gate 	mutex_init(&pwr_p->pwr_lock, NULL, MUTEX_DRIVER, NULL);
6177c478bd9Sstevel@tonic-gate 	/* Initialize the power level and default level support */
6187c478bd9Sstevel@tonic-gate 	pwr_p->pwr_func_lvl = PM_LEVEL_UNKNOWN;
6197c478bd9Sstevel@tonic-gate 	pwr_p->pwr_pmcaps = PCIE_DEFAULT_LEVEL_SUPPORTED;
6207c478bd9Sstevel@tonic-gate 
621e762302fSShesha Sreenivasamurthy 	if (pcie_plat_pwr_setup(dip) != DDI_SUCCESS)
6227c478bd9Sstevel@tonic-gate 		goto pwr_common_err;
6237c478bd9Sstevel@tonic-gate 
624e762302fSShesha Sreenivasamurthy 	pcie_pm_p->pcie_pwr_p = pwr_p;
6257c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate pwr_common_err:
6287c478bd9Sstevel@tonic-gate 	mutex_destroy(&pwr_p->pwr_lock);
6297c478bd9Sstevel@tonic-gate 	kmem_free(pwr_p, sizeof (pcie_pwr_t));
6307c478bd9Sstevel@tonic-gate 	if (pminfo_created) {
6317c478bd9Sstevel@tonic-gate 		PCIE_RESET_PMINFO(dip);
6327c478bd9Sstevel@tonic-gate 		kmem_free(pcie_pm_p, sizeof (pcie_pm_t));
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * Undo whatever is done in pwr_common_setup. Called by px_detach or pxb_detach
6407c478bd9Sstevel@tonic-gate  */
6417c478bd9Sstevel@tonic-gate void
pwr_common_teardown(dev_info_t * dip)6427c478bd9Sstevel@tonic-gate pwr_common_teardown(dev_info_t *dip)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	pcie_pm_t *pcie_pm_p = PCIE_PMINFO(dip);
6457c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	if (!pcie_pm_p || !(pwr_p = PCIE_NEXUS_PMINFO(dip)))
6487c478bd9Sstevel@tonic-gate 		return;
6497c478bd9Sstevel@tonic-gate 
650e762302fSShesha Sreenivasamurthy 	pcie_plat_pwr_teardown(dip);
6517c478bd9Sstevel@tonic-gate 	mutex_destroy(&pwr_p->pwr_lock);
6527c478bd9Sstevel@tonic-gate 	pcie_pm_p->pcie_pwr_p = NULL;
6537c478bd9Sstevel@tonic-gate 	kmem_free(pwr_p, sizeof (pcie_pwr_t));
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * If the parent didn't store have any pm info about
6567c478bd9Sstevel@tonic-gate 	 * this node, that means parent doesn't need pminfo when it handles
6577c478bd9Sstevel@tonic-gate 	 * POST_DETACH for this node. For example, if dip is the dip of
6587c478bd9Sstevel@tonic-gate 	 * root complex, then there is no parent pm info.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	if (!PCIE_PAR_PMINFO(dip)) {
6617c478bd9Sstevel@tonic-gate 		kmem_free(pcie_pm_p, sizeof (pcie_pm_t));
6627c478bd9Sstevel@tonic-gate 		PCIE_RESET_PMINFO(dip);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * Raises the power and marks itself busy.
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate int
pcie_pm_hold(dev_info_t * dip)6707c478bd9Sstevel@tonic-gate pcie_pm_hold(dev_info_t *dip)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/* If no PM info or no device PM, return */
6757c478bd9Sstevel@tonic-gate 	if (!PCIE_PMINFO(dip) || !(pwr_p = PCIE_NEXUS_PMINFO(dip)) ||
6767c478bd9Sstevel@tonic-gate 	    !(PCIE_SUPPORTS_DEVICE_PM(dip)))
6777c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * If we are not at full power, then powerup.
6817c478bd9Sstevel@tonic-gate 	 * Need to be at full power so that link can be
6827c478bd9Sstevel@tonic-gate 	 * at L0. Similarly for PCI/PCI-X bus, it should be
6837c478bd9Sstevel@tonic-gate 	 * at full power.
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
6867c478bd9Sstevel@tonic-gate 	ASSERT(pwr_p->pwr_hold >= 0);
687e762302fSShesha Sreenivasamurthy 	PCIE_DBG("%s(%d): pm_hold: incrementing hold \n",
688e762302fSShesha Sreenivasamurthy 	    ddi_driver_name(dip), ddi_get_instance(dip));
6897c478bd9Sstevel@tonic-gate 	pwr_p->pwr_hold++;
6907c478bd9Sstevel@tonic-gate 	/* Mark itself busy, if it is not done already */
6917c478bd9Sstevel@tonic-gate 	if (!(pwr_p->pwr_flags & PCIE_PM_BUSY)) {
692e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pm_hold: marking busy\n",
693e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(dip), ddi_get_instance(dip));
6947c478bd9Sstevel@tonic-gate 		pwr_p->pwr_flags |= PCIE_PM_BUSY;
6957c478bd9Sstevel@tonic-gate 		(void) pm_busy_component(dip, 0);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	if (pwr_p->pwr_func_lvl == PM_LEVEL_D0) {
6987c478bd9Sstevel@tonic-gate 		mutex_exit(&pwr_p->pwr_lock);
6997c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
7027c478bd9Sstevel@tonic-gate 	if (pm_raise_power(dip, 0, PM_LEVEL_D0) != DDI_SUCCESS) {
703e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pm_hold: attempt to raise power "
704e762302fSShesha Sreenivasamurthy 		    "from %d to %d failed\n", ddi_driver_name(dip),
705e762302fSShesha Sreenivasamurthy 		    ddi_get_instance(dip), pwr_p->pwr_func_lvl,
7067c478bd9Sstevel@tonic-gate 		    PM_LEVEL_D0);
7077c478bd9Sstevel@tonic-gate 		pcie_pm_release(dip);
7087c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate /*
7147c478bd9Sstevel@tonic-gate  * Reverse the things done in pcie_pm_hold
7157c478bd9Sstevel@tonic-gate  */
7167c478bd9Sstevel@tonic-gate void
pcie_pm_release(dev_info_t * dip)7177c478bd9Sstevel@tonic-gate pcie_pm_release(dev_info_t *dip)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	/* If no PM info or no device PM, return */
7227c478bd9Sstevel@tonic-gate 	if (!PCIE_PMINFO(dip) || !(pwr_p = PCIE_NEXUS_PMINFO(dip)) ||
7237c478bd9Sstevel@tonic-gate 	    !(PCIE_SUPPORTS_DEVICE_PM(dip)))
7247c478bd9Sstevel@tonic-gate 		return;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
7277c478bd9Sstevel@tonic-gate 	pcie_pm_subrelease(dip, pwr_p);
7287c478bd9Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate static void
pcie_pm_subrelease(dev_info_t * dip,pcie_pwr_t * pwr_p)7327c478bd9Sstevel@tonic-gate pcie_pm_subrelease(dev_info_t *dip, pcie_pwr_t *pwr_p)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	int level;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pwr_p->pwr_lock));
7377c478bd9Sstevel@tonic-gate 	ASSERT(pwr_p->pwr_hold > 0);
738e762302fSShesha Sreenivasamurthy 	PCIE_DBG("%s(%d): pm_subrelease: decrementing hold \n",
739e762302fSShesha Sreenivasamurthy 	    ddi_driver_name(dip), ddi_get_instance(dip));
7407c478bd9Sstevel@tonic-gate 	pwr_p->pwr_hold--;
7417c478bd9Sstevel@tonic-gate 	ASSERT(pwr_p->pwr_hold >= 0);
7427c478bd9Sstevel@tonic-gate 	ASSERT(pwr_p->pwr_flags & PCIE_PM_BUSY);
7437c478bd9Sstevel@tonic-gate 	level = pwr_level_allowed(pwr_p);
7447c478bd9Sstevel@tonic-gate 	if (pwr_p->pwr_hold == 0 && level < pwr_p->pwr_func_lvl) {
745e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): pm_subrelease: marking idle \n",
746e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(dip), ddi_get_instance(dip));
7477c478bd9Sstevel@tonic-gate 		(void) pm_idle_component(dip, 0);
7487c478bd9Sstevel@tonic-gate 		pwr_p->pwr_flags &= ~PCIE_PM_BUSY;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * Called when the child makes the first power management call.
7547c478bd9Sstevel@tonic-gate  * sets up the counters. All the components of the child device are
7557c478bd9Sstevel@tonic-gate  * assumed to be at unknown level. It also releases the power hold
756b3d69c05SRobert Mustacchi  *	pwr_p - parent's pwr_t
7577c478bd9Sstevel@tonic-gate  *	cdip   - child's dip
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate int
pcie_pm_add_child(dev_info_t * dip,dev_info_t * cdip)7607c478bd9Sstevel@tonic-gate pcie_pm_add_child(dev_info_t *dip, dev_info_t *cdip)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/* If no PM info, return */
7657c478bd9Sstevel@tonic-gate 	if (!PCIE_PMINFO(dip) || !(pwr_p = PCIE_NEXUS_PMINFO(dip)))
7667c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pwr_p->pwr_lock));
7697c478bd9Sstevel@tonic-gate 	ASSERT(pwr_p->pwr_func_lvl == PM_LEVEL_D0);
7707c478bd9Sstevel@tonic-gate 	pcie_add_comps(dip, cdip, pwr_p);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	/* If no device power management then return */
7737c478bd9Sstevel@tonic-gate 	if (!PCIE_SUPPORTS_DEVICE_PM(dip))
7747c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * We have informed PM that we are busy at PRE_ATTACH time for
7787c478bd9Sstevel@tonic-gate 	 * this child. Release the hold and but don't clear the busy bit.
7797c478bd9Sstevel@tonic-gate 	 * If a device never changes power, hold will not be released
7807c478bd9Sstevel@tonic-gate 	 * and we stay at full power.
7817c478bd9Sstevel@tonic-gate 	 */
7827c478bd9Sstevel@tonic-gate 	ASSERT(pwr_p->pwr_hold > 0);
783e762302fSShesha Sreenivasamurthy 	PCIE_DBG("%s(%d): pm_add_child: decrementing hold \n",
784e762302fSShesha Sreenivasamurthy 	    ddi_driver_name(dip), ddi_get_instance(dip));
7857c478bd9Sstevel@tonic-gate 	pwr_p->pwr_hold--;
7867c478bd9Sstevel@tonic-gate 	/*
7877c478bd9Sstevel@tonic-gate 	 * We must have made sure that busy bit
7887c478bd9Sstevel@tonic-gate 	 * is set when we put the hold
7897c478bd9Sstevel@tonic-gate 	 */
7907c478bd9Sstevel@tonic-gate 	ASSERT(pwr_p->pwr_flags & PCIE_PM_BUSY);
7917c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate /*
7957c478bd9Sstevel@tonic-gate  * Adjust the counters when a child detaches
7967c478bd9Sstevel@tonic-gate  * Marks itself idle if the idle conditions are met.
7977c478bd9Sstevel@tonic-gate  * Called at POST_DETACH time
7987c478bd9Sstevel@tonic-gate  */
7997c478bd9Sstevel@tonic-gate int
pcie_pm_remove_child(dev_info_t * dip,dev_info_t * cdip)8007c478bd9Sstevel@tonic-gate pcie_pm_remove_child(dev_info_t *dip, dev_info_t *cdip)
8017c478bd9Sstevel@tonic-gate {
8027c478bd9Sstevel@tonic-gate 	int *counters;
8037c478bd9Sstevel@tonic-gate 	int total;
8047c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* If no PM info, return */
8077c478bd9Sstevel@tonic-gate 	if (!PCIE_PMINFO(dip) || !(pwr_p = PCIE_NEXUS_PMINFO(dip)))
8087c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	counters = pwr_p->pwr_counters;
8117c478bd9Sstevel@tonic-gate 	mutex_enter(&pwr_p->pwr_lock);
8127c478bd9Sstevel@tonic-gate 	pcie_remove_comps(dip, cdip, pwr_p);
8137c478bd9Sstevel@tonic-gate 	/* If no device power management then return */
8147c478bd9Sstevel@tonic-gate 	if (!PCIE_SUPPORTS_DEVICE_PM(dip)) {
8157c478bd9Sstevel@tonic-gate 		mutex_exit(&pwr_p->pwr_lock);
8167c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 	total = (counters[PCIE_D0_INDEX] + counters[PCIE_UNKNOWN_INDEX] +
819eae2e508Skrishnae 	    counters[PCIE_D1_INDEX] + counters[PCIE_D2_INDEX] +
820eae2e508Skrishnae 	    counters[PCIE_D3_INDEX]);
8217c478bd9Sstevel@tonic-gate 	/*
8227c478bd9Sstevel@tonic-gate 	 * Mark idle if either there are no children or our lowest
8237c478bd9Sstevel@tonic-gate 	 * possible level is less than the current level. Mark idle
8247c478bd9Sstevel@tonic-gate 	 * only if it is not already done.
8257c478bd9Sstevel@tonic-gate 	 */
8267c478bd9Sstevel@tonic-gate 	if ((pwr_p->pwr_hold == 0) &&
8277c478bd9Sstevel@tonic-gate 	    (!total || (pwr_level_allowed(pwr_p) < pwr_p->pwr_func_lvl))) {
8287c478bd9Sstevel@tonic-gate 		if (pwr_p->pwr_flags & PCIE_PM_BUSY) {
829e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): pcie_bus_power: marking idle\n",
830e762302fSShesha Sreenivasamurthy 			    ddi_driver_name(dip), ddi_get_instance(dip));
8317c478bd9Sstevel@tonic-gate 			(void) pm_idle_component(dip, 0);
8327c478bd9Sstevel@tonic-gate 			pwr_p->pwr_flags &= ~PCIE_PM_BUSY;
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 	mutex_exit(&pwr_p->pwr_lock);
8367c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
839c73ac1a6Sjchu boolean_t
pcie_is_pcie(dev_info_t * dip)840eae2e508Skrishnae pcie_is_pcie(dev_info_t *dip)
841c73ac1a6Sjchu {
842eae2e508Skrishnae 	pcie_bus_t *bus_p = PCIE_DIP2BUS(dip);
843eae2e508Skrishnae 	ASSERT(bus_p);
844eae2e508Skrishnae 	return (bus_p->bus_pcie_off != 0);
845c73ac1a6Sjchu }
846c73ac1a6Sjchu 
8477c478bd9Sstevel@tonic-gate /*
848d4bc0535SKrishna Elango  * Called by px_attach or pcieb_attach:: DDI_RESUME
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate int
pcie_pwr_resume(dev_info_t * dip)8517c478bd9Sstevel@tonic-gate pcie_pwr_resume(dev_info_t *dip)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
8547c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p = NULL;
8557c478bd9Sstevel@tonic-gate 
856*86ef0a63SRichard Lowe #if defined(__x86)
857d4bc0535SKrishna Elango 	if (dip)
858d4bc0535SKrishna Elango 		return (DDI_SUCCESS);
859*86ef0a63SRichard Lowe #endif /* defined(__x86) */
860d4bc0535SKrishna Elango 
8617c478bd9Sstevel@tonic-gate 	if (PCIE_PMINFO(dip))
8627c478bd9Sstevel@tonic-gate 		pwr_p = PCIE_NEXUS_PMINFO(dip);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (pwr_p) {
8657c478bd9Sstevel@tonic-gate 		/* Inform the PM framework that dip is at full power */
8667c478bd9Sstevel@tonic-gate 		if (PCIE_SUPPORTS_DEVICE_PM(dip)) {
8677c478bd9Sstevel@tonic-gate 			ASSERT(pwr_p->pwr_func_lvl == PM_LEVEL_D0);
8687c478bd9Sstevel@tonic-gate 			(void) pm_raise_power(dip, 0,
8697c478bd9Sstevel@tonic-gate 			    pwr_p->pwr_func_lvl);
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/*
8747c478bd9Sstevel@tonic-gate 	 * Code taken from pci driver.
8757c478bd9Sstevel@tonic-gate 	 * Restore config registers for children that did not save
8767c478bd9Sstevel@tonic-gate 	 * their own registers.  Children pwr states are UNKNOWN after
8777c478bd9Sstevel@tonic-gate 	 * a resume since it is possible for the PM framework to call
8787c478bd9Sstevel@tonic-gate 	 * resume without an actual power cycle. (ie if suspend fails).
8797c478bd9Sstevel@tonic-gate 	 */
8807c478bd9Sstevel@tonic-gate 	for (cdip = ddi_get_child(dip); cdip != NULL;
8817c478bd9Sstevel@tonic-gate 	    cdip = ddi_get_next_sibling(cdip)) {
882c73ac1a6Sjchu 		boolean_t	is_pcie;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		/*
8857c478bd9Sstevel@tonic-gate 		 * Not interested in children who are not already
8867c478bd9Sstevel@tonic-gate 		 * init'ed.  They will be set up by init_child().
8877c478bd9Sstevel@tonic-gate 		 */
8887c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(cdip) < DS_INITIALIZED) {
889e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): "
8907c478bd9Sstevel@tonic-gate 			    "DDI_RESUME: skipping %s%d not in CF1\n",
891e762302fSShesha Sreenivasamurthy 			    ddi_driver_name(dip), ddi_get_instance(dip),
8927c478bd9Sstevel@tonic-gate 			    ddi_driver_name(cdip), ddi_get_instance(cdip));
8937c478bd9Sstevel@tonic-gate 			continue;
8947c478bd9Sstevel@tonic-gate 		}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		/*
8977c478bd9Sstevel@tonic-gate 		 * Only restore config registers if saved by nexus.
8987c478bd9Sstevel@tonic-gate 		 */
8997c478bd9Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
900c73ac1a6Sjchu 		    "nexus-saved-config-regs") != 1)
901c73ac1a6Sjchu 			continue;
902c73ac1a6Sjchu 
903e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): "
904c73ac1a6Sjchu 		    "DDI_RESUME: nexus restoring %s%d config regs\n",
905e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(dip), ddi_get_instance(dip),
906c73ac1a6Sjchu 		    ddi_driver_name(cdip), ddi_get_instance(cdip));
907c73ac1a6Sjchu 
908c73ac1a6Sjchu 		/* clear errors left by OBP scrubbing */
909eae2e508Skrishnae 		pcie_clear_errors(cdip);
9108bc7d88aSet 
911c73ac1a6Sjchu 		/* PCIe workaround: disable errors during 4K config resore */
912b3d69c05SRobert Mustacchi 		is_pcie = pcie_is_pcie(cdip);
913b3d69c05SRobert Mustacchi 		if (is_pcie)
914eae2e508Skrishnae 			pcie_disable_errors(cdip);
915c73ac1a6Sjchu 		(void) pci_restore_config_regs(cdip);
91614ea4bb7Ssd 		if (is_pcie) {
917eae2e508Skrishnae 			pcie_enable_errors(cdip);
918eae2e508Skrishnae 			(void) pcie_enable_ce(cdip);
91914ea4bb7Ssd 		}
9207c478bd9Sstevel@tonic-gate 
921c73ac1a6Sjchu 		if (ndi_prop_remove(DDI_DEV_T_NONE, cdip,
922c73ac1a6Sjchu 		    "nexus-saved-config-regs") != DDI_PROP_SUCCESS) {
923e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): %s%d can't remove prop %s",
924e762302fSShesha Sreenivasamurthy 			    ddi_driver_name(dip), ddi_get_instance(dip),
925c73ac1a6Sjchu 			    ddi_driver_name(cdip), ddi_get_instance(cdip),
926c73ac1a6Sjchu 			    "nexus-saved-config-regs");
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate  * Called by pcie_detach or pcieb_detach:: DDI_SUSPEND
9347c478bd9Sstevel@tonic-gate  */
9357c478bd9Sstevel@tonic-gate int
pcie_pwr_suspend(dev_info_t * dip)9367c478bd9Sstevel@tonic-gate pcie_pwr_suspend(dev_info_t *dip)
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
9397c478bd9Sstevel@tonic-gate 	int i, *counters; /* per nexus counters */
9407c478bd9Sstevel@tonic-gate 	int *child_counters = NULL; /* per child dip counters */
9417c478bd9Sstevel@tonic-gate 	pcie_pwr_t *pwr_p = NULL;
9427c478bd9Sstevel@tonic-gate 
943*86ef0a63SRichard Lowe #if defined(__x86)
944d4bc0535SKrishna Elango 	if (dip)
945d4bc0535SKrishna Elango 		return (DDI_SUCCESS);
946*86ef0a63SRichard Lowe #endif /* defined(__x86) */
947d4bc0535SKrishna Elango 
9487c478bd9Sstevel@tonic-gate 	if (PCIE_PMINFO(dip))
9497c478bd9Sstevel@tonic-gate 		pwr_p = PCIE_NEXUS_PMINFO(dip);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * Mark all children to be unknown and bring our power level
9537c478bd9Sstevel@tonic-gate 	 * to full, if required. This is to avoid any panics while
9547c478bd9Sstevel@tonic-gate 	 * accessing the child's config space.
9557c478bd9Sstevel@tonic-gate 	 */
9567c478bd9Sstevel@tonic-gate 	if (pwr_p) {
9577c478bd9Sstevel@tonic-gate 		mutex_enter(&pwr_p->pwr_lock);
9587c478bd9Sstevel@tonic-gate 		if (PCIE_SUPPORTS_DEVICE_PM(dip) &&
9597c478bd9Sstevel@tonic-gate 		    pwr_p->pwr_func_lvl != PM_LEVEL_D0) {
9607c478bd9Sstevel@tonic-gate 			mutex_exit(&pwr_p->pwr_lock);
9617c478bd9Sstevel@tonic-gate 			if (pm_raise_power(dip, 0, PM_LEVEL_D0) !=
9627c478bd9Sstevel@tonic-gate 			    DDI_SUCCESS) {
963e762302fSShesha Sreenivasamurthy 				PCIE_DBG("%s(%d): pwr_suspend: attempt "
9647c478bd9Sstevel@tonic-gate 				    "to raise power from %d to %d "
965e762302fSShesha Sreenivasamurthy 				    "failed\n", ddi_driver_name(dip),
966e762302fSShesha Sreenivasamurthy 				    ddi_get_instance(dip), pwr_p->pwr_func_lvl,
9677c478bd9Sstevel@tonic-gate 				    PM_LEVEL_D0);
9687c478bd9Sstevel@tonic-gate 				return (DDI_FAILURE);
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 			mutex_enter(&pwr_p->pwr_lock);
9717c478bd9Sstevel@tonic-gate 		}
9727c478bd9Sstevel@tonic-gate 		counters = pwr_p->pwr_counters;
9737c478bd9Sstevel@tonic-gate 		/*
9747c478bd9Sstevel@tonic-gate 		 * Update the nexus counters. At the resume time all
9757c478bd9Sstevel@tonic-gate 		 * components are considered to be at unknown level. Use the
9767c478bd9Sstevel@tonic-gate 		 * fact that counters for unknown level are at the end.
9777c478bd9Sstevel@tonic-gate 		 */
9787c478bd9Sstevel@tonic-gate 		for (i = 0; i < PCIE_UNKNOWN_INDEX; i++) {
9797c478bd9Sstevel@tonic-gate 			counters[PCIE_UNKNOWN_INDEX] += counters[i];
9807c478bd9Sstevel@tonic-gate 			counters[i] = 0;
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 		mutex_exit(&pwr_p->pwr_lock);
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/*
9867c478bd9Sstevel@tonic-gate 	 * Code taken from pci driver.
9877c478bd9Sstevel@tonic-gate 	 * Save the state of the configuration headers of child
9887c478bd9Sstevel@tonic-gate 	 * nodes.
9897c478bd9Sstevel@tonic-gate 	 */
9907c478bd9Sstevel@tonic-gate 	for (cdip = ddi_get_child(dip); cdip != NULL;
9917c478bd9Sstevel@tonic-gate 	    cdip = ddi_get_next_sibling(cdip)) {
992c73ac1a6Sjchu 		boolean_t	is_pcie;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 		/*
9957c478bd9Sstevel@tonic-gate 		 * Not interested in children who are not already
9967c478bd9Sstevel@tonic-gate 		 * init'ed.  They will be set up in init_child().
9977c478bd9Sstevel@tonic-gate 		 */
9987c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(cdip) < DS_INITIALIZED) {
999e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): DDI_SUSPEND: skipping "
1000e762302fSShesha Sreenivasamurthy 			    "%s%d not in CF1\n", ddi_driver_name(dip),
1001e762302fSShesha Sreenivasamurthy 			    ddi_get_instance(dip), ddi_driver_name(cdip),
10027c478bd9Sstevel@tonic-gate 			    ddi_get_instance(cdip));
10037c478bd9Sstevel@tonic-gate 			continue;
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 		/*
10067c478bd9Sstevel@tonic-gate 		 * Update per child dip counters, if any. Counters
10077c478bd9Sstevel@tonic-gate 		 * will not exist if the child is not power manageable
10087c478bd9Sstevel@tonic-gate 		 * or if its power entry is never invoked.
10097c478bd9Sstevel@tonic-gate 		 */
10107c478bd9Sstevel@tonic-gate 		if (PCIE_PMINFO(cdip) && PCIE_PAR_PMINFO(cdip))
10117c478bd9Sstevel@tonic-gate 			child_counters = PCIE_CHILD_COUNTERS(cdip);
10127c478bd9Sstevel@tonic-gate 		if (child_counters && pwr_p) {
10137c478bd9Sstevel@tonic-gate 			mutex_enter(&pwr_p->pwr_lock);
10147c478bd9Sstevel@tonic-gate 			for (i = 0; i < PCIE_UNKNOWN_INDEX; i++) {
10157c478bd9Sstevel@tonic-gate 				child_counters[PCIE_UNKNOWN_INDEX] +=
10167c478bd9Sstevel@tonic-gate 				    child_counters[i];
10177c478bd9Sstevel@tonic-gate 				child_counters[i] = 0;
10187c478bd9Sstevel@tonic-gate 			}
10197c478bd9Sstevel@tonic-gate 			mutex_exit(&pwr_p->pwr_lock);
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 		/*
10237c478bd9Sstevel@tonic-gate 		 * Only save config registers if not already saved by child.
10247c478bd9Sstevel@tonic-gate 		 */
10257c478bd9Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
10267c478bd9Sstevel@tonic-gate 		    SAVED_CONFIG_REGS) == 1) {
10277c478bd9Sstevel@tonic-gate 			continue;
10287c478bd9Sstevel@tonic-gate 		}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		/*
10317c478bd9Sstevel@tonic-gate 		 * The nexus needs to save config registers.  Create a property
10327c478bd9Sstevel@tonic-gate 		 * so it knows to restore on resume.
10337c478bd9Sstevel@tonic-gate 		 */
10347c478bd9Sstevel@tonic-gate 		if (ndi_prop_create_boolean(DDI_DEV_T_NONE, cdip,
10357c478bd9Sstevel@tonic-gate 		    "nexus-saved-config-regs") != DDI_PROP_SUCCESS) {
1036e762302fSShesha Sreenivasamurthy 			PCIE_DBG("%s(%d): %s%d can't update prop %s",
1037e762302fSShesha Sreenivasamurthy 			    ddi_driver_name(dip), ddi_get_instance(dip),
10387c478bd9Sstevel@tonic-gate 			    ddi_driver_name(cdip), ddi_get_instance(cdip),
10397c478bd9Sstevel@tonic-gate 			    "nexus-saved-config-regs");
10407c478bd9Sstevel@tonic-gate 		}
1041e762302fSShesha Sreenivasamurthy 		PCIE_DBG("%s(%d): DDI_SUSPEND: saving config space for"
1042e762302fSShesha Sreenivasamurthy 		    " %s%d\n", ddi_driver_name(dip), ddi_get_instance(dip),
1043e762302fSShesha Sreenivasamurthy 		    ddi_driver_name(cdip), ddi_get_instance(cdip));
1044c73ac1a6Sjchu 
1045c73ac1a6Sjchu 		/* PCIe workaround: disable errors during 4K config save */
1046b3d69c05SRobert Mustacchi 		is_pcie = pcie_is_pcie(cdip);
1047b3d69c05SRobert Mustacchi 		if (is_pcie)
1048eae2e508Skrishnae 			pcie_disable_errors(cdip);
10497c478bd9Sstevel@tonic-gate 		(void) pci_save_config_regs(cdip);
105014ea4bb7Ssd 		if (is_pcie) {
1051eae2e508Skrishnae 			pcie_enable_errors(cdip);
1052eae2e508Skrishnae 			(void) pcie_enable_ce(cdip);
105314ea4bb7Ssd 		}
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate #ifdef DEBUG
10597c478bd9Sstevel@tonic-gate /*
10607c478bd9Sstevel@tonic-gate  * Description of bus_power_op.
10617c478bd9Sstevel@tonic-gate  */
10627c478bd9Sstevel@tonic-gate typedef struct pcie_buspwr_desc {
10637c478bd9Sstevel@tonic-gate 	pm_bus_power_op_t pwr_op;
10647c478bd9Sstevel@tonic-gate 	char *pwr_desc;
10657c478bd9Sstevel@tonic-gate } pcie_buspwr_desc_t;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate static pcie_buspwr_desc_t pcie_buspwr_desc[] = {
10687c478bd9Sstevel@tonic-gate 	{BUS_POWER_CHILD_PWRCHG, "CHILD_PWRCHG"},
10697c478bd9Sstevel@tonic-gate 	{BUS_POWER_NEXUS_PWRUP, "NEXUS_PWRUP"},
10707c478bd9Sstevel@tonic-gate 	{BUS_POWER_PRE_NOTIFICATION, "PRE_NOTIFICATION"},
10717c478bd9Sstevel@tonic-gate 	{BUS_POWER_POST_NOTIFICATION, "POST_NOTIFICATION"},
10727c478bd9Sstevel@tonic-gate 	{BUS_POWER_HAS_CHANGED, "HAS_CHANGED"},
10737c478bd9Sstevel@tonic-gate 	{BUS_POWER_NOINVOL, "NOINVOL"},
10747c478bd9Sstevel@tonic-gate 	{-1, NULL}
10757c478bd9Sstevel@tonic-gate };
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate /*
10787c478bd9Sstevel@tonic-gate  * Returns description of the bus_power_op.
10797c478bd9Sstevel@tonic-gate  */
10807c478bd9Sstevel@tonic-gate static char *
pcie_decode_pwr_op(pm_bus_power_op_t op)10817c478bd9Sstevel@tonic-gate pcie_decode_pwr_op(pm_bus_power_op_t op)
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 	pcie_buspwr_desc_t *descp = pcie_buspwr_desc;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	for (; descp->pwr_desc; descp++) {
10867c478bd9Sstevel@tonic-gate 		if (op == descp->pwr_op)
10877c478bd9Sstevel@tonic-gate 			return (descp->pwr_desc);
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 	return ("UNKNOWN OP");
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate #endif
1092