xref: /onnv-gate/usr/src/uts/common/io/cpudrv.c (revision 8409:ea07f5941a68)
14667Smh27603 /*
24667Smh27603  * CDDL HEADER START
34667Smh27603  *
44667Smh27603  * The contents of this file are subject to the terms of the
54667Smh27603  * Common Development and Distribution License (the "License").
64667Smh27603  * You may not use this file except in compliance with the License.
74667Smh27603  *
84667Smh27603  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94667Smh27603  * or http://www.opensolaris.org/os/licensing.
104667Smh27603  * See the License for the specific language governing permissions
114667Smh27603  * and limitations under the License.
124667Smh27603  *
134667Smh27603  * When distributing Covered Code, include this CDDL HEADER in each
144667Smh27603  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154667Smh27603  * If applicable, add the following below this CDDL HEADER, with the
164667Smh27603  * fields enclosed by brackets "[]" replaced with your own identifying
174667Smh27603  * information: Portions Copyright [yyyy] [name of copyright owner]
184667Smh27603  *
194667Smh27603  * CDDL HEADER END
204667Smh27603  */
214667Smh27603 /*
225864Sesaxe  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
234667Smh27603  * Use is subject to license terms.
244667Smh27603  */
254667Smh27603 
264667Smh27603 /*
274667Smh27603  * CPU Device driver. The driver is not DDI-compliant.
284667Smh27603  *
294667Smh27603  * The driver supports following features:
304667Smh27603  *	- Power management.
314667Smh27603  */
324667Smh27603 
334667Smh27603 #include <sys/types.h>
344667Smh27603 #include <sys/param.h>
354667Smh27603 #include <sys/errno.h>
364667Smh27603 #include <sys/modctl.h>
374667Smh27603 #include <sys/kmem.h>
384667Smh27603 #include <sys/conf.h>
394667Smh27603 #include <sys/cmn_err.h>
404667Smh27603 #include <sys/stat.h>
414667Smh27603 #include <sys/debug.h>
424667Smh27603 #include <sys/systm.h>
434667Smh27603 #include <sys/ddi.h>
444667Smh27603 #include <sys/sunddi.h>
455864Sesaxe #include <sys/sdt.h>
464667Smh27603 
474667Smh27603 #include <sys/machsystm.h>
484667Smh27603 #include <sys/x_call.h>
497319SMark.Haywood@Sun.COM #include <sys/cpudrv_mach.h>
504667Smh27603 #include <sys/msacct.h>
514667Smh27603 
524667Smh27603 /*
534667Smh27603  * CPU power management
544667Smh27603  *
554667Smh27603  * The supported power saving model is to slow down the CPU (on SPARC by
564667Smh27603  * dividing the CPU clock and on x86 by dropping down a P-state).
574667Smh27603  * Periodically we determine the amount of time the CPU is running
584667Smh27603  * idle thread and threads in user mode during the last quantum.  If the idle
594667Smh27603  * thread was running less than its low water mark for current speed for
604667Smh27603  * number of consecutive sampling periods, or number of running threads in
614667Smh27603  * user mode are above its high water mark, we arrange to go to the higher
624667Smh27603  * speed.  If the idle thread was running more than its high water mark without
634667Smh27603  * dropping a number of consecutive times below the mark, and number of threads
644667Smh27603  * running in user mode are below its low water mark, we arrange to go to the
654667Smh27603  * next lower speed.  While going down, we go through all the speeds.  While
664667Smh27603  * going up we go to the maximum speed to minimize impact on the user, but have
674667Smh27603  * provisions in the driver to go to other speeds.
684667Smh27603  *
694667Smh27603  * The driver does not have knowledge of a particular implementation of this
704667Smh27603  * scheme and will work with all CPUs supporting this model. On SPARC, the
714667Smh27603  * driver determines supported speeds by looking at 'clock-divisors' property
724667Smh27603  * created by OBP. On x86, the driver retrieves the supported speeds from
734667Smh27603  * ACPI.
744667Smh27603  */
754667Smh27603 
764667Smh27603 /*
774667Smh27603  * Configuration function prototypes and data structures
784667Smh27603  */
794667Smh27603 static int cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
804667Smh27603 static int cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
814667Smh27603 static int cpudrv_power(dev_info_t *dip, int comp, int level);
824667Smh27603 
834667Smh27603 struct dev_ops cpudrv_ops = {
844667Smh27603 	DEVO_REV,		/* rev */
854667Smh27603 	0,			/* refcnt */
864667Smh27603 	nodev,			/* getinfo */
874667Smh27603 	nulldev,		/* identify */
884667Smh27603 	nulldev,		/* probe */
894667Smh27603 	cpudrv_attach,		/* attach */
904667Smh27603 	cpudrv_detach,		/* detach */
914667Smh27603 	nodev,			/* reset */
924667Smh27603 	(struct cb_ops *)NULL,	/* cb_ops */
934667Smh27603 	(struct bus_ops *)NULL,	/* bus_ops */
947656SSherry.Moore@Sun.COM 	cpudrv_power,		/* power */
957656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
964667Smh27603 };
974667Smh27603 
984667Smh27603 static struct modldrv modldrv = {
994667Smh27603 	&mod_driverops,			/* modops */
1007319SMark.Haywood@Sun.COM 	"CPU Driver",			/* linkinfo */
1014667Smh27603 	&cpudrv_ops,			/* dev_ops */
1024667Smh27603 };
1034667Smh27603 
1044667Smh27603 static struct modlinkage modlinkage = {
1054667Smh27603 	MODREV_1,		/* rev */
1064667Smh27603 	&modldrv,		/* linkage */
1074667Smh27603 	NULL
1084667Smh27603 };
1094667Smh27603 
1104667Smh27603 /*
1114667Smh27603  * Function prototypes
1124667Smh27603  */
1137319SMark.Haywood@Sun.COM static int cpudrv_pm_init_power(cpudrv_devstate_t *cpudsp);
1144667Smh27603 static void cpudrv_pm_free(cpudrv_devstate_t *cpudsp);
1154667Smh27603 static int cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp);
1164667Smh27603 static void cpudrv_pm_monitor_disp(void *arg);
1174667Smh27603 static void cpudrv_pm_monitor(void *arg);
1184667Smh27603 
1194667Smh27603 /*
1204667Smh27603  * Driver global variables
1214667Smh27603  */
1224667Smh27603 uint_t cpudrv_debug = 0;
1234667Smh27603 void *cpudrv_state;
1244667Smh27603 static uint_t cpudrv_pm_idle_hwm = CPUDRV_PM_IDLE_HWM;
1254667Smh27603 static uint_t cpudrv_pm_idle_lwm = CPUDRV_PM_IDLE_LWM;
1264667Smh27603 static uint_t cpudrv_pm_idle_buf_zone = CPUDRV_PM_IDLE_BUF_ZONE;
1274667Smh27603 static uint_t cpudrv_pm_idle_bhwm_cnt_max = CPUDRV_PM_IDLE_BHWM_CNT_MAX;
1284667Smh27603 static uint_t cpudrv_pm_idle_blwm_cnt_max = CPUDRV_PM_IDLE_BLWM_CNT_MAX;
1294667Smh27603 static uint_t cpudrv_pm_user_hwm = CPUDRV_PM_USER_HWM;
1304667Smh27603 
1314667Smh27603 /*
1324667Smh27603  * cpudrv_direct_pm allows user applications to directly control the
1334667Smh27603  * power state transitions (direct pm) without following the normal
1344667Smh27603  * direct pm protocol. This is needed because the normal protocol
1354667Smh27603  * requires that a device only be lowered when it is idle, and be
1364667Smh27603  * brought up when it request to do so by calling pm_raise_power().
1374667Smh27603  * Ignoring this protocol is harmless for CPU (other than speed).
1384667Smh27603  * Moreover it might be the case that CPU is never idle or wants
1394667Smh27603  * to be at higher speed because of the addition CPU cycles required
1404667Smh27603  * to run the user application.
1414667Smh27603  *
1424667Smh27603  * The driver will still report idle/busy status to the framework. Although
1434667Smh27603  * framework will ignore this information for direct pm devices and not
1444667Smh27603  * try to bring them down when idle, user applications can still use this
1454667Smh27603  * information if they wants.
1464667Smh27603  *
1474667Smh27603  * In the future, provide an ioctl to control setting of this mode. In
1484667Smh27603  * that case, this variable should move to the state structure and
1494667Smh27603  * be protected by the lock in the state structure.
1504667Smh27603  */
1514667Smh27603 int cpudrv_direct_pm = 0;
1524667Smh27603 
1534667Smh27603 /*
1544667Smh27603  * Arranges for the handler function to be called at the interval suitable
1554667Smh27603  * for current speed.
1564667Smh27603  */
1574667Smh27603 #define	CPUDRV_PM_MONITOR_INIT(cpudsp) { \
1587319SMark.Haywood@Sun.COM 	if (CPUDRV_PM_POWER_ENABLED(cpudsp)) { \
1597319SMark.Haywood@Sun.COM 		ASSERT(mutex_owned(&(cpudsp)->lock)); \
1607319SMark.Haywood@Sun.COM 		(cpudsp)->cpudrv_pm.timeout_id = \
1617319SMark.Haywood@Sun.COM 		    timeout(cpudrv_pm_monitor_disp, \
1627319SMark.Haywood@Sun.COM 		    (cpudsp), (((cpudsp)->cpudrv_pm.cur_spd == NULL) ? \
1637319SMark.Haywood@Sun.COM 		    CPUDRV_PM_QUANT_CNT_OTHR : \
1647319SMark.Haywood@Sun.COM 		    (cpudsp)->cpudrv_pm.cur_spd->quant_cnt)); \
1657319SMark.Haywood@Sun.COM 	} \
1664667Smh27603 }
1674667Smh27603 
1684667Smh27603 /*
1694667Smh27603  * Arranges for the handler function not to be called back.
1704667Smh27603  */
1714667Smh27603 #define	CPUDRV_PM_MONITOR_FINI(cpudsp) { \
1724667Smh27603 	timeout_id_t tmp_tid; \
1734667Smh27603 	ASSERT(mutex_owned(&(cpudsp)->lock)); \
1744667Smh27603 	tmp_tid = (cpudsp)->cpudrv_pm.timeout_id; \
1754667Smh27603 	(cpudsp)->cpudrv_pm.timeout_id = 0; \
1764667Smh27603 	mutex_exit(&(cpudsp)->lock); \
1777319SMark.Haywood@Sun.COM 	if (tmp_tid != 0) { \
1787319SMark.Haywood@Sun.COM 		(void) untimeout(tmp_tid); \
1797319SMark.Haywood@Sun.COM 		mutex_enter(&(cpudsp)->cpudrv_pm.timeout_lock); \
1807319SMark.Haywood@Sun.COM 		while ((cpudsp)->cpudrv_pm.timeout_count != 0) \
1817319SMark.Haywood@Sun.COM 			cv_wait(&(cpudsp)->cpudrv_pm.timeout_cv, \
1827319SMark.Haywood@Sun.COM 			    &(cpudsp)->cpudrv_pm.timeout_lock); \
1837319SMark.Haywood@Sun.COM 		mutex_exit(&(cpudsp)->cpudrv_pm.timeout_lock); \
1847319SMark.Haywood@Sun.COM 	} \
1854667Smh27603 	mutex_enter(&(cpudsp)->lock); \
1864667Smh27603 }
1874667Smh27603 
1884667Smh27603 int
1894667Smh27603 _init(void)
1904667Smh27603 {
1914667Smh27603 	int	error;
1924667Smh27603 
1934667Smh27603 	DPRINTF(D_INIT, (" _init: function called\n"));
1944667Smh27603 	if ((error = ddi_soft_state_init(&cpudrv_state,
1954667Smh27603 	    sizeof (cpudrv_devstate_t), 0)) != 0) {
1964667Smh27603 		return (error);
1974667Smh27603 	}
1984667Smh27603 
1994667Smh27603 	if ((error = mod_install(&modlinkage)) != 0)  {
2004667Smh27603 		ddi_soft_state_fini(&cpudrv_state);
2014667Smh27603 	}
2024667Smh27603 
2034667Smh27603 	/*
2044667Smh27603 	 * Callbacks used by the PPM driver.
2054667Smh27603 	 */
2064667Smh27603 	CPUDRV_PM_SET_PPM_CALLBACKS();
2074667Smh27603 	return (error);
2084667Smh27603 }
2094667Smh27603 
2104667Smh27603 int
2114667Smh27603 _fini(void)
2124667Smh27603 {
2134667Smh27603 	int	error;
2144667Smh27603 
2154667Smh27603 	DPRINTF(D_FINI, (" _fini: function called\n"));
2164667Smh27603 	if ((error = mod_remove(&modlinkage)) == 0) {
2174667Smh27603 		ddi_soft_state_fini(&cpudrv_state);
2184667Smh27603 	}
2194667Smh27603 
2204667Smh27603 	return (error);
2214667Smh27603 }
2224667Smh27603 
2234667Smh27603 int
2244667Smh27603 _info(struct modinfo *modinfop)
2254667Smh27603 {
2264667Smh27603 	return (mod_info(&modlinkage, modinfop));
2274667Smh27603 }
2284667Smh27603 
2294667Smh27603 /*
2304667Smh27603  * Driver attach(9e) entry point.
2314667Smh27603  */
2324667Smh27603 static int
2334667Smh27603 cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2344667Smh27603 {
2354667Smh27603 	int			instance;
2364667Smh27603 	cpudrv_devstate_t	*cpudsp;
2374667Smh27603 	extern pri_t		maxclsyspri;
2384667Smh27603 
2394667Smh27603 	instance = ddi_get_instance(dip);
2404667Smh27603 
2414667Smh27603 	switch (cmd) {
2424667Smh27603 	case DDI_ATTACH:
2434667Smh27603 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
2444667Smh27603 		    "DDI_ATTACH called\n", instance));
2457319SMark.Haywood@Sun.COM 		if (CPUDRV_PM_DISABLED())
2467319SMark.Haywood@Sun.COM 			return (DDI_FAILURE);
2474667Smh27603 		if (ddi_soft_state_zalloc(cpudrv_state, instance) !=
2484667Smh27603 		    DDI_SUCCESS) {
2494667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2504667Smh27603 			    "can't allocate state", instance);
2514667Smh27603 			CPUDRV_PM_DISABLE();
2524667Smh27603 			return (DDI_FAILURE);
2534667Smh27603 		}
2544667Smh27603 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
2554667Smh27603 		    NULL) {
2564667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2574667Smh27603 			    "can't get state", instance);
2584667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2594667Smh27603 			CPUDRV_PM_DISABLE();
2604667Smh27603 			return (DDI_FAILURE);
2614667Smh27603 		}
2624667Smh27603 		cpudsp->dip = dip;
2634667Smh27603 
2644667Smh27603 		/*
2654667Smh27603 		 * Find CPU number for this dev_info node.
2664667Smh27603 		 */
2674667Smh27603 		if (!cpudrv_pm_get_cpu_id(dip, &(cpudsp->cpu_id))) {
2684667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2694667Smh27603 			    "can't convert dip to cpu_id", instance);
2704667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2714667Smh27603 			CPUDRV_PM_DISABLE();
2724667Smh27603 			return (DDI_FAILURE);
2734667Smh27603 		}
2747319SMark.Haywood@Sun.COM 		if (!cpudrv_mach_pm_init(cpudsp)) {
2754667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2764667Smh27603 			CPUDRV_PM_DISABLE();
2774667Smh27603 			return (DDI_FAILURE);
2784667Smh27603 		}
2797319SMark.Haywood@Sun.COM 		mutex_init(&cpudsp->lock, NULL, MUTEX_DRIVER, NULL);
2807319SMark.Haywood@Sun.COM 		if (CPUDRV_PM_POWER_ENABLED(cpudsp)) {
2817319SMark.Haywood@Sun.COM 			if (cpudrv_pm_init_power(cpudsp) != DDI_SUCCESS) {
2827319SMark.Haywood@Sun.COM 				CPUDRV_PM_DISABLE();
2837319SMark.Haywood@Sun.COM 				cpudrv_pm_free(cpudsp);
2847319SMark.Haywood@Sun.COM 				ddi_soft_state_free(cpudrv_state, instance);
2857319SMark.Haywood@Sun.COM 				return (DDI_FAILURE);
2867319SMark.Haywood@Sun.COM 			}
2877319SMark.Haywood@Sun.COM 			if (cpudrv_pm_comp_create(cpudsp) != DDI_SUCCESS) {
2887319SMark.Haywood@Sun.COM 				CPUDRV_PM_DISABLE();
2897319SMark.Haywood@Sun.COM 				cpudrv_pm_free(cpudsp);
2907319SMark.Haywood@Sun.COM 				ddi_soft_state_free(cpudrv_state, instance);
2917319SMark.Haywood@Sun.COM 				return (DDI_FAILURE);
2927319SMark.Haywood@Sun.COM 			}
2937319SMark.Haywood@Sun.COM 			if (ddi_prop_update_string(DDI_DEV_T_NONE,
2947319SMark.Haywood@Sun.COM 			    dip, "pm-class", "CPU") != DDI_PROP_SUCCESS) {
2957319SMark.Haywood@Sun.COM 				CPUDRV_PM_DISABLE();
2967319SMark.Haywood@Sun.COM 				cpudrv_pm_free(cpudsp);
2977319SMark.Haywood@Sun.COM 				ddi_soft_state_free(cpudrv_state, instance);
2987319SMark.Haywood@Sun.COM 				return (DDI_FAILURE);
2997319SMark.Haywood@Sun.COM 			}
3007319SMark.Haywood@Sun.COM 
3017319SMark.Haywood@Sun.COM 			/*
3027319SMark.Haywood@Sun.COM 			 * Taskq is used to dispatch routine to monitor CPU
3037319SMark.Haywood@Sun.COM 			 * activities.
3047319SMark.Haywood@Sun.COM 			 */
3057319SMark.Haywood@Sun.COM 			cpudsp->cpudrv_pm.tq = taskq_create_instance(
3067319SMark.Haywood@Sun.COM 			    "cpudrv_pm_monitor",
3077319SMark.Haywood@Sun.COM 			    ddi_get_instance(dip), CPUDRV_PM_TASKQ_THREADS,
3087319SMark.Haywood@Sun.COM 			    (maxclsyspri - 1), CPUDRV_PM_TASKQ_MIN,
3097319SMark.Haywood@Sun.COM 			    CPUDRV_PM_TASKQ_MAX,
3107319SMark.Haywood@Sun.COM 			    TASKQ_PREPOPULATE|TASKQ_CPR_SAFE);
3117319SMark.Haywood@Sun.COM 
3127319SMark.Haywood@Sun.COM 			mutex_init(&cpudsp->cpudrv_pm.timeout_lock, NULL,
3137319SMark.Haywood@Sun.COM 			    MUTEX_DRIVER, NULL);
3147319SMark.Haywood@Sun.COM 			cv_init(&cpudsp->cpudrv_pm.timeout_cv, NULL,
3157319SMark.Haywood@Sun.COM 			    CV_DEFAULT, NULL);
3167319SMark.Haywood@Sun.COM 
3177319SMark.Haywood@Sun.COM 			/*
3187319SMark.Haywood@Sun.COM 			 * Driver needs to assume that CPU is running at
3197319SMark.Haywood@Sun.COM 			 * unknown speed at DDI_ATTACH and switch it to the
3207319SMark.Haywood@Sun.COM 			 * needed speed. We assume that initial needed speed
3217319SMark.Haywood@Sun.COM 			 * is full speed for us.
3227319SMark.Haywood@Sun.COM 			 */
3237319SMark.Haywood@Sun.COM 			/*
3247319SMark.Haywood@Sun.COM 			 * We need to take the lock because cpudrv_pm_monitor()
3257319SMark.Haywood@Sun.COM 			 * will start running in parallel with attach().
3267319SMark.Haywood@Sun.COM 			 */
3277319SMark.Haywood@Sun.COM 			mutex_enter(&cpudsp->lock);
3287319SMark.Haywood@Sun.COM 			cpudsp->cpudrv_pm.cur_spd = NULL;
3297319SMark.Haywood@Sun.COM 			cpudsp->cpudrv_pm.pm_started = B_FALSE;
3307319SMark.Haywood@Sun.COM 			/*
3317319SMark.Haywood@Sun.COM 			 * We don't call pm_raise_power() directly from attach
3327319SMark.Haywood@Sun.COM 			 * because driver attach for a slave CPU node can
3337319SMark.Haywood@Sun.COM 			 * happen before the CPU is even initialized. We just
3347319SMark.Haywood@Sun.COM 			 * start the monitoring system which understands
335*8409SMark.Haywood@Sun.COM 			 * unknown speed and moves CPU to top speed when it
336*8409SMark.Haywood@Sun.COM 			 * has been initialized.
3377319SMark.Haywood@Sun.COM 			 */
3387319SMark.Haywood@Sun.COM 			CPUDRV_PM_MONITOR_INIT(cpudsp);
3397319SMark.Haywood@Sun.COM 			mutex_exit(&cpudsp->lock);
3407319SMark.Haywood@Sun.COM 
3414667Smh27603 		}
3424667Smh27603 
3437319SMark.Haywood@Sun.COM 		CPUDRV_PM_INSTALL_MAX_CHANGE_HANDLER(cpudsp, dip);
3444667Smh27603 
3454667Smh27603 		ddi_report_dev(dip);
3464667Smh27603 		return (DDI_SUCCESS);
3474667Smh27603 
3484667Smh27603 	case DDI_RESUME:
3494667Smh27603 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
3504667Smh27603 		    "DDI_RESUME called\n", instance));
3517319SMark.Haywood@Sun.COM 
3527319SMark.Haywood@Sun.COM 		cpudsp = ddi_get_soft_state(cpudrv_state, instance);
3537319SMark.Haywood@Sun.COM 		ASSERT(cpudsp != NULL);
3547319SMark.Haywood@Sun.COM 
3557319SMark.Haywood@Sun.COM 		/*
3567319SMark.Haywood@Sun.COM 		 * Nothing to do for resume, if not doing active PM.
3577319SMark.Haywood@Sun.COM 		 */
3587319SMark.Haywood@Sun.COM 		if (!CPUDRV_PM_POWER_ENABLED(cpudsp))
3597319SMark.Haywood@Sun.COM 			return (DDI_SUCCESS);
3607319SMark.Haywood@Sun.COM 
3614667Smh27603 		mutex_enter(&cpudsp->lock);
3624667Smh27603 		/*
3634667Smh27603 		 * Driver needs to assume that CPU is running at unknown speed
3644667Smh27603 		 * at DDI_RESUME and switch it to the needed speed. We assume
3654667Smh27603 		 * that the needed speed is full speed for us.
3664667Smh27603 		 */
3674667Smh27603 		cpudsp->cpudrv_pm.cur_spd = NULL;
3684667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
3694667Smh27603 		mutex_exit(&cpudsp->lock);
3704667Smh27603 		CPUDRV_PM_REDEFINE_TOPSPEED(dip);
3714667Smh27603 		return (DDI_SUCCESS);
3724667Smh27603 
3734667Smh27603 	default:
3744667Smh27603 		return (DDI_FAILURE);
3754667Smh27603 	}
3764667Smh27603 }
3774667Smh27603 
3784667Smh27603 /*
3794667Smh27603  * Driver detach(9e) entry point.
3804667Smh27603  */
3814667Smh27603 static int
3824667Smh27603 cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3834667Smh27603 {
3844667Smh27603 	int			instance;
3854667Smh27603 	cpudrv_devstate_t	*cpudsp;
3864667Smh27603 	cpudrv_pm_t		*cpupm;
3874667Smh27603 
3884667Smh27603 	instance = ddi_get_instance(dip);
3894667Smh27603 
3904667Smh27603 	switch (cmd) {
3914667Smh27603 	case DDI_DETACH:
3924667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
3934667Smh27603 		    "DDI_DETACH called\n", instance));
3944667Smh27603 		/*
3954667Smh27603 		 * If the only thing supported by the driver is power
3964667Smh27603 		 * management, we can in future enhance the driver and
3974667Smh27603 		 * framework that loads it to unload the driver when
3984667Smh27603 		 * user has disabled CPU power management.
3994667Smh27603 		 */
4004667Smh27603 		return (DDI_FAILURE);
4014667Smh27603 
4024667Smh27603 	case DDI_SUSPEND:
4034667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
4044667Smh27603 		    "DDI_SUSPEND called\n", instance));
4057319SMark.Haywood@Sun.COM 
4067319SMark.Haywood@Sun.COM 		cpudsp = ddi_get_soft_state(cpudrv_state, instance);
4077319SMark.Haywood@Sun.COM 		ASSERT(cpudsp != NULL);
4087319SMark.Haywood@Sun.COM 
4097319SMark.Haywood@Sun.COM 		/*
4107319SMark.Haywood@Sun.COM 		 * Nothing to do for suspend, if not doing active PM.
4117319SMark.Haywood@Sun.COM 		 */
4127319SMark.Haywood@Sun.COM 		if (!CPUDRV_PM_POWER_ENABLED(cpudsp))
4137319SMark.Haywood@Sun.COM 			return (DDI_SUCCESS);
4147319SMark.Haywood@Sun.COM 
4154667Smh27603 		/*
4164667Smh27603 		 * During a checkpoint-resume sequence, framework will
4174667Smh27603 		 * stop interrupts to quiesce kernel activity. This will
4184667Smh27603 		 * leave our monitoring system ineffective. Handle this
4194667Smh27603 		 * by stopping our monitoring system and bringing CPU
4204667Smh27603 		 * to full speed. In case we are in special direct pm
4214667Smh27603 		 * mode, we leave the CPU at whatever speed it is. This
4224667Smh27603 		 * is harmless other than speed.
4234667Smh27603 		 */
4244667Smh27603 		mutex_enter(&cpudsp->lock);
4254667Smh27603 		cpupm = &(cpudsp->cpudrv_pm);
4264667Smh27603 
4274667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: DDI_SUSPEND - "
428*8409SMark.Haywood@Sun.COM 		    "cur_spd %d, topspeed %d\n", instance,
429*8409SMark.Haywood@Sun.COM 		    cpupm->cur_spd->pm_level,
430*8409SMark.Haywood@Sun.COM 		    CPUDRV_PM_TOPSPEED(cpupm)->pm_level));
4314667Smh27603 
4324667Smh27603 		CPUDRV_PM_MONITOR_FINI(cpudsp);
4334667Smh27603 
434*8409SMark.Haywood@Sun.COM 		if (!cpudrv_direct_pm && (cpupm->cur_spd !=
435*8409SMark.Haywood@Sun.COM 		    CPUDRV_PM_TOPSPEED(cpupm))) {
4364667Smh27603 			if (cpupm->pm_busycnt < 1) {
4374667Smh27603 				if ((pm_busy_component(dip, CPUDRV_PM_COMP_NUM)
4384667Smh27603 				    == DDI_SUCCESS)) {
4394667Smh27603 					cpupm->pm_busycnt++;
4404667Smh27603 				} else {
4414667Smh27603 					CPUDRV_PM_MONITOR_INIT(cpudsp);
4424667Smh27603 					mutex_exit(&cpudsp->lock);
4434667Smh27603 					cmn_err(CE_WARN, "cpudrv_detach: "
4444667Smh27603 					    "instance %d: can't busy CPU "
4454667Smh27603 					    "component", instance);
4464667Smh27603 					return (DDI_FAILURE);
4474667Smh27603 				}
4484667Smh27603 			}
4494667Smh27603 			mutex_exit(&cpudsp->lock);
4504667Smh27603 			if (pm_raise_power(dip, CPUDRV_PM_COMP_NUM,
451*8409SMark.Haywood@Sun.COM 			    CPUDRV_PM_TOPSPEED(cpupm)->pm_level) !=
452*8409SMark.Haywood@Sun.COM 			    DDI_SUCCESS) {
4534667Smh27603 				mutex_enter(&cpudsp->lock);
4544667Smh27603 				CPUDRV_PM_MONITOR_INIT(cpudsp);
4554667Smh27603 				mutex_exit(&cpudsp->lock);
4564667Smh27603 				cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
457*8409SMark.Haywood@Sun.COM 				    "can't raise CPU power level to %d",
458*8409SMark.Haywood@Sun.COM 				    instance,
459*8409SMark.Haywood@Sun.COM 				    CPUDRV_PM_TOPSPEED(cpupm)->pm_level);
4604667Smh27603 				return (DDI_FAILURE);
4614667Smh27603 			} else {
4624667Smh27603 				return (DDI_SUCCESS);
4634667Smh27603 			}
4644667Smh27603 		} else {
4654667Smh27603 			mutex_exit(&cpudsp->lock);
4664667Smh27603 			return (DDI_SUCCESS);
4674667Smh27603 		}
4684667Smh27603 
4694667Smh27603 	default:
4704667Smh27603 		return (DDI_FAILURE);
4714667Smh27603 	}
4724667Smh27603 }
4734667Smh27603 
4744667Smh27603 /*
4754667Smh27603  * Driver power(9e) entry point.
4764667Smh27603  *
4774667Smh27603  * Driver's notion of current power is set *only* in power(9e) entry point
4784667Smh27603  * after actual power change operation has been successfully completed.
4794667Smh27603  */
4804667Smh27603 /* ARGSUSED */
4814667Smh27603 static int
4824667Smh27603 cpudrv_power(dev_info_t *dip, int comp, int level)
4834667Smh27603 {
4844667Smh27603 	int			instance;
4854667Smh27603 	cpudrv_devstate_t	*cpudsp;
4864667Smh27603 	cpudrv_pm_t 		*cpupm;
4874667Smh27603 	cpudrv_pm_spd_t		*new_spd;
4884667Smh27603 	boolean_t		is_ready;
4894667Smh27603 	int			ret;
4904667Smh27603 
4914667Smh27603 	instance = ddi_get_instance(dip);
4924667Smh27603 
4934667Smh27603 	DPRINTF(D_POWER, ("cpudrv_power: instance %d: level %d\n",
4944667Smh27603 	    instance, level));
4954667Smh27603 	if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) == NULL) {
4964667Smh27603 		cmn_err(CE_WARN, "cpudrv_power: instance %d: can't get state",
4974667Smh27603 		    instance);
4984667Smh27603 		return (DDI_FAILURE);
4994667Smh27603 	}
5004667Smh27603 
5014667Smh27603 	mutex_enter(&cpudsp->lock);
5024667Smh27603 	cpupm = &(cpudsp->cpudrv_pm);
5034667Smh27603 
5044667Smh27603 	/*
5054667Smh27603 	 * In normal operation, we fail if we are busy and request is
5064667Smh27603 	 * to lower the power level. We let this go through if the driver
5074667Smh27603 	 * is in special direct pm mode. On x86, we also let this through
5087319SMark.Haywood@Sun.COM 	 * if the change is due to a request to govern the max speed.
5094667Smh27603 	 */
5104667Smh27603 	if (!cpudrv_direct_pm && (cpupm->pm_busycnt >= 1) &&
5117319SMark.Haywood@Sun.COM 	    !cpudrv_pm_is_governor_thread(cpupm)) {
5124667Smh27603 		if ((cpupm->cur_spd != NULL) &&
5134667Smh27603 		    (level < cpupm->cur_spd->pm_level)) {
5144667Smh27603 			mutex_exit(&cpudsp->lock);
5154667Smh27603 			return (DDI_FAILURE);
5164667Smh27603 		}
5174667Smh27603 	}
5184667Smh27603 
5194667Smh27603 	for (new_spd = cpupm->head_spd; new_spd; new_spd = new_spd->down_spd) {
5204667Smh27603 		if (new_spd->pm_level == level)
5214667Smh27603 			break;
5224667Smh27603 	}
5234667Smh27603 	if (!new_spd) {
5247319SMark.Haywood@Sun.COM 		CPUDRV_PM_RESET_GOVERNOR_THREAD(cpupm);
5254667Smh27603 		mutex_exit(&cpudsp->lock);
5264667Smh27603 		cmn_err(CE_WARN, "cpudrv_power: instance %d: "
5274667Smh27603 		    "can't locate new CPU speed", instance);
5284667Smh27603 		return (DDI_FAILURE);
5294667Smh27603 	}
5304667Smh27603 
5314667Smh27603 	/*
5324667Smh27603 	 * We currently refuse to power manage if the CPU is not ready to
5334667Smh27603 	 * take cross calls (cross calls fail silently if CPU is not ready
5344667Smh27603 	 * for it).
5354667Smh27603 	 *
5364667Smh27603 	 * Additionally, for x86 platforms we cannot power manage
5374667Smh27603 	 * any one instance, until all instances have been initialized.
5384667Smh27603 	 * That's because we don't know what the CPU domains look like
5394667Smh27603 	 * until all instances have been initialized.
5404667Smh27603 	 */
5414667Smh27603 	is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
5424667Smh27603 	if (!is_ready) {
5434667Smh27603 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
5444667Smh27603 		    "CPU not ready for x-calls\n", instance));
5457319SMark.Haywood@Sun.COM 	} else if (!(is_ready = cpudrv_pm_power_ready())) {
5464667Smh27603 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
5477319SMark.Haywood@Sun.COM 		    "waiting for all CPUs to be power manageable\n", instance));
5484667Smh27603 	}
5494667Smh27603 	if (!is_ready) {
5507319SMark.Haywood@Sun.COM 		CPUDRV_PM_RESET_GOVERNOR_THREAD(cpupm);
5514667Smh27603 		mutex_exit(&cpudsp->lock);
5524667Smh27603 		return (DDI_FAILURE);
5534667Smh27603 	}
5544667Smh27603 
5554667Smh27603 	/*
5564667Smh27603 	 * Execute CPU specific routine on the requested CPU to change its
5574667Smh27603 	 * speed to normal-speed/divisor.
5584667Smh27603 	 */
5594667Smh27603 	if ((ret = cpudrv_pm_change_speed(cpudsp, new_spd)) != DDI_SUCCESS) {
5604667Smh27603 		cmn_err(CE_WARN, "cpudrv_power: cpudrv_pm_change_speed() "
5614667Smh27603 		    "return = %d", ret);
5624667Smh27603 		mutex_exit(&cpudsp->lock);
5634667Smh27603 		return (DDI_FAILURE);
5644667Smh27603 	}
5654667Smh27603 
5664667Smh27603 	/*
5675864Sesaxe 	 * DTrace probe point for CPU speed change transition
5685864Sesaxe 	 */
5695864Sesaxe 	DTRACE_PROBE3(cpu__change__speed, cpudrv_devstate_t *, cpudsp,
5705864Sesaxe 	    cpudrv_pm_t *, cpupm, cpudrv_pm_spd_t *, new_spd);
5715864Sesaxe 
5725864Sesaxe 	/*
5734667Smh27603 	 * Reset idle threshold time for the new power level.
5744667Smh27603 	 */
5754667Smh27603 	if ((cpupm->cur_spd != NULL) && (level < cpupm->cur_spd->pm_level)) {
5764667Smh27603 		if (pm_idle_component(dip, CPUDRV_PM_COMP_NUM) ==
5774667Smh27603 		    DDI_SUCCESS) {
5784667Smh27603 			if (cpupm->pm_busycnt >= 1)
5794667Smh27603 				cpupm->pm_busycnt--;
5804667Smh27603 		} else
5814667Smh27603 			cmn_err(CE_WARN, "cpudrv_power: instance %d: can't "
5824667Smh27603 			    "idle CPU component", ddi_get_instance(dip));
5834667Smh27603 	}
5844667Smh27603 	/*
5854667Smh27603 	 * Reset various parameters because we are now running at new speed.
5864667Smh27603 	 */
5874667Smh27603 	cpupm->lastquan_mstate[CMS_IDLE] = 0;
5884667Smh27603 	cpupm->lastquan_mstate[CMS_SYSTEM] = 0;
5894667Smh27603 	cpupm->lastquan_mstate[CMS_USER] = 0;
5904667Smh27603 	cpupm->lastquan_lbolt = 0;
5914667Smh27603 	cpupm->cur_spd = new_spd;
5927319SMark.Haywood@Sun.COM 	CPUDRV_PM_RESET_GOVERNOR_THREAD(cpupm);
5934667Smh27603 	mutex_exit(&cpudsp->lock);
5944667Smh27603 
5954667Smh27603 	return (DDI_SUCCESS);
5964667Smh27603 }
5974667Smh27603 
5984667Smh27603 /*
5994667Smh27603  * Initialize the field that will be used for reporting
6004667Smh27603  * the supported_frequencies_Hz cpu_info kstat.
6014667Smh27603  */
6024667Smh27603 static void
6034667Smh27603 set_supp_freqs(cpu_t *cp, cpudrv_pm_t *cpupm)
6044667Smh27603 {
6054667Smh27603 	char		*supp_freqs;
6064667Smh27603 	char		*sfptr;
6074667Smh27603 	uint64_t	*speeds;
6084667Smh27603 	cpudrv_pm_spd_t	*spd;
6094667Smh27603 	int		i;
6104667Smh27603 #define	UINT64_MAX_STRING (sizeof ("18446744073709551615"))
6114667Smh27603 
6124667Smh27603 	speeds = kmem_zalloc(cpupm->num_spd * sizeof (uint64_t), KM_SLEEP);
6134667Smh27603 	for (i = cpupm->num_spd - 1, spd = cpupm->head_spd; spd;
6144667Smh27603 	    i--, spd = spd->down_spd) {
6154667Smh27603 		speeds[i] =
6164667Smh27603 		    CPUDRV_PM_SPEED_HZ(cp->cpu_type_info.pi_clock, spd->speed);
6174667Smh27603 	}
6184667Smh27603 
6194667Smh27603 	supp_freqs = kmem_zalloc((UINT64_MAX_STRING * cpupm->num_spd),
6204667Smh27603 	    KM_SLEEP);
6214667Smh27603 	sfptr = supp_freqs;
6224667Smh27603 	for (i = 0; i < cpupm->num_spd; i++) {
6234667Smh27603 		if (i == cpupm->num_spd - 1) {
6244667Smh27603 			(void) sprintf(sfptr, "%"PRIu64, speeds[i]);
6254667Smh27603 		} else {
6264667Smh27603 			(void) sprintf(sfptr, "%"PRIu64":", speeds[i]);
6274667Smh27603 			sfptr = supp_freqs + strlen(supp_freqs);
6284667Smh27603 		}
6294667Smh27603 	}
6304877Smh27603 	cpu_set_supp_freqs(cp, supp_freqs);
6314877Smh27603 	kmem_free(supp_freqs, (UINT64_MAX_STRING * cpupm->num_spd));
6324667Smh27603 	kmem_free(speeds, cpupm->num_spd * sizeof (uint64_t));
6334667Smh27603 }
6344667Smh27603 
6354667Smh27603 /*
6364667Smh27603  * Initialize power management data.
6374667Smh27603  */
6384667Smh27603 static int
6397319SMark.Haywood@Sun.COM cpudrv_pm_init_power(cpudrv_devstate_t *cpudsp)
6404667Smh27603 {
6414667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
6424667Smh27603 	cpudrv_pm_spd_t	*cur_spd;
6434667Smh27603 	cpudrv_pm_spd_t	*prev_spd = NULL;
6444667Smh27603 	int		*speeds;
6454667Smh27603 	uint_t		nspeeds;
6464667Smh27603 	int		idle_cnt_percent;
6474667Smh27603 	int		user_cnt_percent;
6484667Smh27603 	int		i;
6494667Smh27603 
6504667Smh27603 	CPUDRV_PM_GET_SPEEDS(cpudsp, speeds, nspeeds);
6514667Smh27603 	if (nspeeds < 2) {
6524667Smh27603 		/* Need at least two speeds to power manage */
6534667Smh27603 		CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
6544667Smh27603 		return (DDI_FAILURE);
6554667Smh27603 	}
6564667Smh27603 	cpupm->num_spd = nspeeds;
6574667Smh27603 
6584667Smh27603 	/*
6594667Smh27603 	 * Calculate the watermarks and other parameters based on the
6604667Smh27603 	 * supplied speeds.
6614667Smh27603 	 *
6624667Smh27603 	 * One of the basic assumption is that for X amount of CPU work,
6634667Smh27603 	 * if CPU is slowed down by a factor of N, the time it takes to
6644667Smh27603 	 * do the same work will be N * X.
6654667Smh27603 	 *
6664667Smh27603 	 * The driver declares that a CPU is idle and ready for slowed down,
6674667Smh27603 	 * if amount of idle thread is more than the current speed idle_hwm
6684667Smh27603 	 * without dropping below idle_hwm a number of consecutive sampling
6694667Smh27603 	 * intervals and number of running threads in user mode are below
6704667Smh27603 	 * user_lwm.  We want to set the current user_lwm such that if we
6714667Smh27603 	 * just switched to the next slower speed with no change in real work
6724667Smh27603 	 * load, the amount of user threads at the slower speed will be such
6734667Smh27603 	 * that it falls below the slower speed's user_hwm.  If we didn't do
6744667Smh27603 	 * that then we will just come back to the higher speed as soon as we
6754667Smh27603 	 * go down even with no change in work load.
6764667Smh27603 	 * The user_hwm is a fixed precentage and not calculated dynamically.
6774667Smh27603 	 *
6784667Smh27603 	 * We bring the CPU up if idle thread at current speed is less than
6794667Smh27603 	 * the current speed idle_lwm for a number of consecutive sampling
6804667Smh27603 	 * intervals or user threads are above the user_hwm for the current
6814667Smh27603 	 * speed.
6824667Smh27603 	 */
6834667Smh27603 	for (i = 0; i < nspeeds; i++) {
6844667Smh27603 		cur_spd = kmem_zalloc(sizeof (cpudrv_pm_spd_t), KM_SLEEP);
6854667Smh27603 		cur_spd->speed = speeds[i];
6864667Smh27603 		if (i == 0) {	/* normal speed */
6874667Smh27603 			cpupm->head_spd = cur_spd;
688*8409SMark.Haywood@Sun.COM 			CPUDRV_PM_TOPSPEED(cpupm) = cur_spd;
6894667Smh27603 			cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_NORMAL;
6904667Smh27603 			cur_spd->idle_hwm =
6914667Smh27603 			    (cpudrv_pm_idle_hwm * cur_spd->quant_cnt) / 100;
6924667Smh27603 			/* can't speed anymore */
6934667Smh27603 			cur_spd->idle_lwm = 0;
6944667Smh27603 			cur_spd->user_hwm = UINT_MAX;
6954667Smh27603 		} else {
6964667Smh27603 			cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_OTHR;
6974667Smh27603 			ASSERT(prev_spd != NULL);
6984667Smh27603 			prev_spd->down_spd = cur_spd;
6994667Smh27603 			cur_spd->up_spd = cpupm->head_spd;
7004667Smh27603 
7014667Smh27603 			/*
7024667Smh27603 			 * Let's assume CPU is considered idle at full speed
7034667Smh27603 			 * when it is spending I% of time in running the idle
7044667Smh27603 			 * thread.  At full speed, CPU will be busy (100 - I) %
7054667Smh27603 			 * of times.  This % of busyness increases by factor of
7064667Smh27603 			 * N as CPU slows down.  CPU that is idle I% of times
7074667Smh27603 			 * in full speed, it is idle (100 - ((100 - I) * N)) %
7084667Smh27603 			 * of times in N speed.  The idle_lwm is a fixed
7094667Smh27603 			 * percentage.  A large value of N may result in
7104667Smh27603 			 * idle_hwm to go below idle_lwm.  We need to make sure
7114667Smh27603 			 * that there is at least a buffer zone seperation
7124667Smh27603 			 * between the idle_lwm and idle_hwm values.
7134667Smh27603 			 */
7144667Smh27603 			idle_cnt_percent = CPUDRV_PM_IDLE_CNT_PERCENT(
7154667Smh27603 			    cpudrv_pm_idle_hwm, speeds, i);
7164667Smh27603 			idle_cnt_percent = max(idle_cnt_percent,
7174667Smh27603 			    (cpudrv_pm_idle_lwm + cpudrv_pm_idle_buf_zone));
7184667Smh27603 			cur_spd->idle_hwm =
7194667Smh27603 			    (idle_cnt_percent * cur_spd->quant_cnt) / 100;
7204667Smh27603 			cur_spd->idle_lwm =
7214667Smh27603 			    (cpudrv_pm_idle_lwm * cur_spd->quant_cnt) / 100;
7224667Smh27603 
7234667Smh27603 			/*
7244667Smh27603 			 * The lwm for user threads are determined such that
7254667Smh27603 			 * if CPU slows down, the load of work in the
7264667Smh27603 			 * new speed would still keep the CPU at or below the
7274667Smh27603 			 * user_hwm in the new speed.  This is to prevent
7284667Smh27603 			 * the quick jump back up to higher speed.
7294667Smh27603 			 */
7304667Smh27603 			cur_spd->user_hwm = (cpudrv_pm_user_hwm *
7314667Smh27603 			    cur_spd->quant_cnt) / 100;
7324667Smh27603 			user_cnt_percent = CPUDRV_PM_USER_CNT_PERCENT(
7334667Smh27603 			    cpudrv_pm_user_hwm, speeds, i);
7344667Smh27603 			prev_spd->user_lwm =
7354667Smh27603 			    (user_cnt_percent * prev_spd->quant_cnt) / 100;
7364667Smh27603 		}
7374667Smh27603 		prev_spd = cur_spd;
7384667Smh27603 	}
7394667Smh27603 	/* Slowest speed. Can't slow down anymore */
7404667Smh27603 	cur_spd->idle_hwm = UINT_MAX;
7414667Smh27603 	cur_spd->user_lwm = -1;
7424667Smh27603 #ifdef	DEBUG
7434667Smh27603 	DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: head_spd spd %d, "
7444667Smh27603 	    "num_spd %d\n", ddi_get_instance(cpudsp->dip),
7454667Smh27603 	    cpupm->head_spd->speed, cpupm->num_spd));
7464667Smh27603 	for (cur_spd = cpupm->head_spd; cur_spd; cur_spd = cur_spd->down_spd) {
7474667Smh27603 		DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: speed %d, "
7484667Smh27603 		    "down_spd spd %d, idle_hwm %d, user_lwm %d, "
7494667Smh27603 		    "up_spd spd %d, idle_lwm %d, user_hwm %d, "
7504667Smh27603 		    "quant_cnt %d\n", ddi_get_instance(cpudsp->dip),
7514667Smh27603 		    cur_spd->speed,
7524667Smh27603 		    (cur_spd->down_spd ? cur_spd->down_spd->speed : 0),
7534667Smh27603 		    cur_spd->idle_hwm, cur_spd->user_lwm,
7544667Smh27603 		    (cur_spd->up_spd ? cur_spd->up_spd->speed : 0),
7554667Smh27603 		    cur_spd->idle_lwm, cur_spd->user_hwm,
7564667Smh27603 		    cur_spd->quant_cnt));
7574667Smh27603 	}
7584667Smh27603 #endif	/* DEBUG */
7594667Smh27603 	CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
7604667Smh27603 	return (DDI_SUCCESS);
7614667Smh27603 }
7624667Smh27603 
7634667Smh27603 /*
7644667Smh27603  * Free CPU power management data.
7654667Smh27603  */
7664667Smh27603 static void
7674667Smh27603 cpudrv_pm_free(cpudrv_devstate_t *cpudsp)
7684667Smh27603 {
7694667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
7704667Smh27603 	cpudrv_pm_spd_t	*cur_spd, *next_spd;
7714667Smh27603 
7724667Smh27603 	cur_spd = cpupm->head_spd;
7734667Smh27603 	while (cur_spd) {
7744667Smh27603 		next_spd = cur_spd->down_spd;
7754667Smh27603 		kmem_free(cur_spd, sizeof (cpudrv_pm_spd_t));
7764667Smh27603 		cur_spd = next_spd;
7774667Smh27603 	}
7784667Smh27603 	bzero(cpupm, sizeof (cpudrv_pm_t));
7797319SMark.Haywood@Sun.COM 	cpudrv_mach_pm_free(cpudsp);
7804667Smh27603 }
7814667Smh27603 
7824667Smh27603 /*
7834667Smh27603  * Create pm-components property.
7844667Smh27603  */
7854667Smh27603 static int
7864667Smh27603 cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp)
7874667Smh27603 {
7884667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
7894667Smh27603 	cpudrv_pm_spd_t	*cur_spd;
7904667Smh27603 	char		**pmc;
7914667Smh27603 	int		size;
7924667Smh27603 	char		name[] = "NAME=CPU Speed";
7934667Smh27603 	int		i, j;
7944667Smh27603 	uint_t		comp_spd;
7954667Smh27603 	int		result = DDI_FAILURE;
7964667Smh27603 
7974667Smh27603 	pmc = kmem_zalloc((cpupm->num_spd + 1) * sizeof (char *), KM_SLEEP);
7984667Smh27603 	size = CPUDRV_PM_COMP_SIZE();
7994667Smh27603 	if (cpupm->num_spd > CPUDRV_PM_COMP_MAX_VAL) {
8004667Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
8014667Smh27603 		    "number of speeds exceeded limits",
8024667Smh27603 		    ddi_get_instance(cpudsp->dip));
8034667Smh27603 		kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
8044667Smh27603 		return (result);
8054667Smh27603 	}
8064667Smh27603 
8074667Smh27603 	for (i = cpupm->num_spd, cur_spd = cpupm->head_spd; i > 0;
8084667Smh27603 	    i--, cur_spd = cur_spd->down_spd) {
8094667Smh27603 		cur_spd->pm_level = i;
8104667Smh27603 		pmc[i] = kmem_zalloc((size * sizeof (char)), KM_SLEEP);
8114667Smh27603 		comp_spd = CPUDRV_PM_COMP_SPEED(cpupm, cur_spd);
8124667Smh27603 		if (comp_spd > CPUDRV_PM_COMP_MAX_VAL) {
8134667Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_comp_create: "
8144667Smh27603 			    "instance %d: speed exceeded limits",
8154667Smh27603 			    ddi_get_instance(cpudsp->dip));
8164667Smh27603 			for (j = cpupm->num_spd; j >= i; j--) {
8174667Smh27603 				kmem_free(pmc[j], size * sizeof (char));
8184667Smh27603 			}
8194667Smh27603 			kmem_free(pmc, (cpupm->num_spd + 1) *
8204667Smh27603 			    sizeof (char *));
8214667Smh27603 			return (result);
8224667Smh27603 		}
8234667Smh27603 		CPUDRV_PM_COMP_SPRINT(pmc[i], cpupm, cur_spd, comp_spd)
8244667Smh27603 		DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: "
8254667Smh27603 		    "instance %d: pm-components power level %d string '%s'\n",
8264667Smh27603 		    ddi_get_instance(cpudsp->dip), i, pmc[i]));
8274667Smh27603 	}
8284667Smh27603 	pmc[0] = kmem_zalloc(sizeof (name), KM_SLEEP);
8294667Smh27603 	(void) strcat(pmc[0], name);
8304667Smh27603 	DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: instance %d: "
8314667Smh27603 	    "pm-components component name '%s'\n",
8324667Smh27603 	    ddi_get_instance(cpudsp->dip), pmc[0]));
8334667Smh27603 
8344667Smh27603 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, cpudsp->dip,
8354667Smh27603 	    "pm-components", pmc, cpupm->num_spd + 1) == DDI_PROP_SUCCESS) {
8364667Smh27603 		result = DDI_SUCCESS;
8374667Smh27603 	} else {
8384667Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
8394667Smh27603 		    "can't create pm-components property",
8404667Smh27603 		    ddi_get_instance(cpudsp->dip));
8414667Smh27603 	}
8424667Smh27603 
8434667Smh27603 	for (i = cpupm->num_spd; i > 0; i--) {
8444667Smh27603 		kmem_free(pmc[i], size * sizeof (char));
8454667Smh27603 	}
8464667Smh27603 	kmem_free(pmc[0], sizeof (name));
8474667Smh27603 	kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
8484667Smh27603 	return (result);
8494667Smh27603 }
8504667Smh27603 
8514667Smh27603 /*
8524667Smh27603  * Mark a component idle.
8534667Smh27603  */
8544667Smh27603 #define	CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm) { \
8554667Smh27603 	if ((cpupm)->pm_busycnt >= 1) { \
8564667Smh27603 		if (pm_idle_component((dip), CPUDRV_PM_COMP_NUM) == \
8574667Smh27603 		    DDI_SUCCESS) { \
8584667Smh27603 			DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
8594667Smh27603 			    "instance %d: pm_idle_component called\n", \
8604667Smh27603 			    ddi_get_instance((dip)))); \
8614667Smh27603 			(cpupm)->pm_busycnt--; \
8624667Smh27603 		} else { \
8634667Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
8644667Smh27603 			    "can't idle CPU component", \
8654667Smh27603 			    ddi_get_instance((dip))); \
8664667Smh27603 		} \
8674667Smh27603 	} \
8684667Smh27603 }
8694667Smh27603 
8704667Smh27603 /*
8714667Smh27603  * Marks a component busy in both PM framework and driver state structure.
8724667Smh27603  */
8734667Smh27603 #define	CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm) { \
8744667Smh27603 	if ((cpupm)->pm_busycnt < 1) { \
8754667Smh27603 		if (pm_busy_component((dip), CPUDRV_PM_COMP_NUM) == \
8764667Smh27603 		    DDI_SUCCESS) { \
8774667Smh27603 			DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
8784667Smh27603 			    "instance %d: pm_busy_component called\n", \
8794667Smh27603 			    ddi_get_instance((dip)))); \
8804667Smh27603 			(cpupm)->pm_busycnt++; \
8814667Smh27603 		} else { \
8824667Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
8834667Smh27603 			    "can't busy CPU component", \
8844667Smh27603 			    ddi_get_instance((dip))); \
8854667Smh27603 		} \
8864667Smh27603 	} \
8874667Smh27603 }
8884667Smh27603 
8894667Smh27603 /*
8904667Smh27603  * Marks a component busy and calls pm_raise_power().
8914667Smh27603  */
8924667Smh27603 #define	CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm, new_level) { \
8934667Smh27603 	/* \
8944667Smh27603 	 * Mark driver and PM framework busy first so framework doesn't try \
8954667Smh27603 	 * to bring CPU to lower speed when we need to be at higher speed. \
8964667Smh27603 	 */ \
8974667Smh27603 	CPUDRV_PM_MONITOR_PM_BUSY_COMP((dip), (cpupm)); \
8984667Smh27603 	mutex_exit(&(cpudsp)->lock); \
8994667Smh27603 	DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: " \
9004667Smh27603 	    "pm_raise_power called to %d\n", ddi_get_instance((dip)), \
9014667Smh27603 		(new_level))); \
9024667Smh27603 	if (pm_raise_power((dip), CPUDRV_PM_COMP_NUM, (new_level)) != \
9034667Smh27603 	    DDI_SUCCESS) { \
9044667Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't " \
9054667Smh27603 		    "raise CPU power level", ddi_get_instance((dip))); \
9064667Smh27603 	} \
9074667Smh27603 	mutex_enter(&(cpudsp)->lock); \
9084667Smh27603 }
9094667Smh27603 
9104667Smh27603 /*
9114667Smh27603  * In order to monitor a CPU, we need to hold cpu_lock to access CPU
9124667Smh27603  * statistics. Holding cpu_lock is not allowed from a callout routine.
9134667Smh27603  * We dispatch a taskq to do that job.
9144667Smh27603  */
9154667Smh27603 static void
9164667Smh27603 cpudrv_pm_monitor_disp(void *arg)
9174667Smh27603 {
9184667Smh27603 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
9194667Smh27603 
9204667Smh27603 	/*
9214667Smh27603 	 * We are here because the last task has scheduled a timeout.
9224667Smh27603 	 * The queue should be empty at this time.
9234667Smh27603 	 */
9244667Smh27603 	mutex_enter(&cpudsp->cpudrv_pm.timeout_lock);
9254667Smh27603 	if (!taskq_dispatch(cpudsp->cpudrv_pm.tq, cpudrv_pm_monitor, arg,
9264667Smh27603 	    TQ_NOSLEEP)) {
9274667Smh27603 		mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
9284667Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor_disp: failed to "
9294667Smh27603 		    "dispatch the cpudrv_pm_monitor taskq\n"));
9304667Smh27603 		mutex_enter(&cpudsp->lock);
9314667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
9324667Smh27603 		mutex_exit(&cpudsp->lock);
9334667Smh27603 		return;
9344667Smh27603 	}
9354667Smh27603 	cpudsp->cpudrv_pm.timeout_count++;
9364667Smh27603 	mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
9374667Smh27603 }
9384667Smh27603 
9394667Smh27603 /*
9404667Smh27603  * Monitors each CPU for the amount of time idle thread was running in the
9414667Smh27603  * last quantum and arranges for the CPU to go to the lower or higher speed.
9424667Smh27603  * Called at the time interval appropriate for the current speed. The
9434667Smh27603  * time interval for normal speed is CPUDRV_PM_QUANT_CNT_NORMAL. The time
9444667Smh27603  * interval for other speeds (including unknown speed) is
9454667Smh27603  * CPUDRV_PM_QUANT_CNT_OTHR.
9464667Smh27603  */
9474667Smh27603 static void
9484667Smh27603 cpudrv_pm_monitor(void *arg)
9494667Smh27603 {
9504667Smh27603 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
9514667Smh27603 	cpudrv_pm_t		*cpupm;
9524667Smh27603 	cpudrv_pm_spd_t		*cur_spd, *new_spd;
9534667Smh27603 	cpu_t			*cp;
9544667Smh27603 	dev_info_t		*dip;
9554667Smh27603 	uint_t			idle_cnt, user_cnt, system_cnt;
9564667Smh27603 	clock_t			lbolt_cnt;
9574667Smh27603 	hrtime_t		msnsecs[NCMSTATES];
9584667Smh27603 	boolean_t		is_ready;
9594667Smh27603 
9604667Smh27603 #define	GET_CPU_MSTATE_CNT(state, cnt) \
9614667Smh27603 	msnsecs[state] = NSEC_TO_TICK(msnsecs[state]); \
9624667Smh27603 	if (cpupm->lastquan_mstate[state] > msnsecs[state]) \
9634667Smh27603 		msnsecs[state] = cpupm->lastquan_mstate[state]; \
9644667Smh27603 	cnt = msnsecs[state] - cpupm->lastquan_mstate[state]; \
9654667Smh27603 	cpupm->lastquan_mstate[state] = msnsecs[state]
9664667Smh27603 
9674667Smh27603 	mutex_enter(&cpudsp->lock);
9684667Smh27603 	cpupm = &(cpudsp->cpudrv_pm);
9694667Smh27603 	if (cpupm->timeout_id == 0) {
9704667Smh27603 		mutex_exit(&cpudsp->lock);
9714667Smh27603 		goto do_return;
9724667Smh27603 	}
9734667Smh27603 	cur_spd = cpupm->cur_spd;
9744667Smh27603 	dip = cpudsp->dip;
9754667Smh27603 
9764667Smh27603 	/*
9774667Smh27603 	 * We assume that a CPU is initialized and has a valid cpu_t
9784667Smh27603 	 * structure, if it is ready for cross calls. If this changes,
9794667Smh27603 	 * additional checks might be needed.
9804667Smh27603 	 *
9814667Smh27603 	 * Additionally, for x86 platforms we cannot power manage
9824667Smh27603 	 * any one instance, until all instances have been initialized.
9834667Smh27603 	 * That's because we don't know what the CPU domains look like
9844667Smh27603 	 * until all instances have been initialized.
9854667Smh27603 	 */
9864667Smh27603 	is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
9874667Smh27603 	if (!is_ready) {
9884667Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
9894667Smh27603 		    "CPU not ready for x-calls\n", ddi_get_instance(dip)));
9907319SMark.Haywood@Sun.COM 	} else if (!(is_ready = cpudrv_pm_power_ready())) {
9914667Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
9927319SMark.Haywood@Sun.COM 		    "waiting for all CPUs to be power manageable\n",
9934667Smh27603 		    ddi_get_instance(dip)));
9944667Smh27603 	}
9954667Smh27603 	if (!is_ready) {
9964667Smh27603 		/*
9974667Smh27603 		 * Make sure that we are busy so that framework doesn't
9984667Smh27603 		 * try to bring us down in this situation.
9994667Smh27603 		 */
10004667Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
10014667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10024667Smh27603 		mutex_exit(&cpudsp->lock);
10034667Smh27603 		goto do_return;
10044667Smh27603 	}
10054667Smh27603 
10064667Smh27603 	/*
10074667Smh27603 	 * Make sure that we are still not at unknown power level.
10084667Smh27603 	 */
10094667Smh27603 	if (cur_spd == NULL) {
10104667Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
10114667Smh27603 		    "cur_spd is unknown\n", ddi_get_instance(dip)));
10124667Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
1013*8409SMark.Haywood@Sun.COM 		    CPUDRV_PM_TOPSPEED(cpupm)->pm_level);
10144667Smh27603 		/*
10154667Smh27603 		 * We just changed the speed. Wait till at least next
10164667Smh27603 		 * call to this routine before proceeding ahead.
10174667Smh27603 		 */
10184667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10194667Smh27603 		mutex_exit(&cpudsp->lock);
10204667Smh27603 		goto do_return;
10214667Smh27603 	}
10224667Smh27603 
10234667Smh27603 	mutex_enter(&cpu_lock);
10244667Smh27603 	if ((cp = cpu_get(cpudsp->cpu_id)) == NULL) {
10254667Smh27603 		mutex_exit(&cpu_lock);
10264667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10274667Smh27603 		mutex_exit(&cpudsp->lock);
10284667Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't get "
10294667Smh27603 		    "cpu_t", ddi_get_instance(dip));
10304667Smh27603 		goto do_return;
10314667Smh27603 	}
10324877Smh27603 
10334877Smh27603 	if (!cpupm->pm_started) {
10344877Smh27603 		cpupm->pm_started = B_TRUE;
10354667Smh27603 		set_supp_freqs(cp, cpupm);
10364877Smh27603 	}
10374667Smh27603 
10385611Smh27603 	get_cpu_mstate(cp, msnsecs);
10394667Smh27603 	GET_CPU_MSTATE_CNT(CMS_IDLE, idle_cnt);
10404667Smh27603 	GET_CPU_MSTATE_CNT(CMS_USER, user_cnt);
10414667Smh27603 	GET_CPU_MSTATE_CNT(CMS_SYSTEM, system_cnt);
10424667Smh27603 
10434667Smh27603 	/*
10444667Smh27603 	 * We can't do anything when we have just switched to a state
10454667Smh27603 	 * because there is no valid timestamp.
10464667Smh27603 	 */
10474667Smh27603 	if (cpupm->lastquan_lbolt == 0) {
10484667Smh27603 		cpupm->lastquan_lbolt = lbolt;
10494667Smh27603 		mutex_exit(&cpu_lock);
10504667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10514667Smh27603 		mutex_exit(&cpudsp->lock);
10524667Smh27603 		goto do_return;
10534667Smh27603 	}
10544667Smh27603 
10554667Smh27603 	/*
10564667Smh27603 	 * Various watermarks are based on this routine being called back
10574667Smh27603 	 * exactly at the requested period. This is not guaranteed
10584667Smh27603 	 * because this routine is called from a taskq that is dispatched
10594667Smh27603 	 * from a timeout routine.  Handle this by finding out how many
10604667Smh27603 	 * ticks have elapsed since the last call (lbolt_cnt) and adjusting
10614667Smh27603 	 * the idle_cnt based on the delay added to the requested period
10624667Smh27603 	 * by timeout and taskq.
10634667Smh27603 	 */
10644667Smh27603 	lbolt_cnt = lbolt - cpupm->lastquan_lbolt;
10654667Smh27603 	cpupm->lastquan_lbolt = lbolt;
10664667Smh27603 	mutex_exit(&cpu_lock);
10674667Smh27603 	/*
10684667Smh27603 	 * Time taken between recording the current counts and
10694667Smh27603 	 * arranging the next call of this routine is an error in our
10704667Smh27603 	 * calculation. We minimize the error by calling
10714667Smh27603 	 * CPUDRV_PM_MONITOR_INIT() here instead of end of this routine.
10724667Smh27603 	 */
10734667Smh27603 	CPUDRV_PM_MONITOR_INIT(cpudsp);
10744667Smh27603 	DPRINTF(D_PM_MONITOR_VERBOSE, ("cpudrv_pm_monitor: instance %d: "
10754667Smh27603 	    "idle count %d, user count %d, system count %d, pm_level %d, "
10764667Smh27603 	    "pm_busycnt %d\n", ddi_get_instance(dip), idle_cnt, user_cnt,
10774667Smh27603 	    system_cnt, cur_spd->pm_level, cpupm->pm_busycnt));
10784667Smh27603 
10794667Smh27603 #ifdef	DEBUG
10804667Smh27603 	/*
10814667Smh27603 	 * Notify that timeout and taskq has caused delays and we need to
10824667Smh27603 	 * scale our parameters accordingly.
10834667Smh27603 	 *
10844667Smh27603 	 * To get accurate result, don't turn on other DPRINTFs with
10854667Smh27603 	 * the following DPRINTF. PROM calls generated by other
10864667Smh27603 	 * DPRINTFs changes the timing.
10874667Smh27603 	 */
10884667Smh27603 	if (lbolt_cnt > cur_spd->quant_cnt) {
10894667Smh27603 		DPRINTF(D_PM_MONITOR_DELAY, ("cpudrv_pm_monitor: instance %d: "
10904667Smh27603 		    "lbolt count %ld > quantum_count %u\n",
10914667Smh27603 		    ddi_get_instance(dip), lbolt_cnt, cur_spd->quant_cnt));
10924667Smh27603 	}
10934667Smh27603 #endif	/* DEBUG */
10944667Smh27603 
10954667Smh27603 	/*
10964667Smh27603 	 * Adjust counts based on the delay added by timeout and taskq.
10974667Smh27603 	 */
10984667Smh27603 	idle_cnt = (idle_cnt * cur_spd->quant_cnt) / lbolt_cnt;
10994667Smh27603 	user_cnt = (user_cnt * cur_spd->quant_cnt) / lbolt_cnt;
11004667Smh27603 	if ((user_cnt > cur_spd->user_hwm) || (idle_cnt < cur_spd->idle_lwm &&
11014667Smh27603 	    cur_spd->idle_blwm_cnt >= cpudrv_pm_idle_blwm_cnt_max)) {
11024667Smh27603 		cur_spd->idle_blwm_cnt = 0;
11034667Smh27603 		cur_spd->idle_bhwm_cnt = 0;
11044667Smh27603 		/*
11054667Smh27603 		 * In normal situation, arrange to go to next higher speed.
11064667Smh27603 		 * If we are running in special direct pm mode, we just stay
11074667Smh27603 		 * at the current speed.
11084667Smh27603 		 */
11094667Smh27603 		if (cur_spd == cur_spd->up_spd || cpudrv_direct_pm) {
11104667Smh27603 			CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
11114667Smh27603 		} else {
11124667Smh27603 			new_spd = cur_spd->up_spd;
11134667Smh27603 			CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
11144667Smh27603 			    new_spd->pm_level);
11154667Smh27603 		}
11164667Smh27603 	} else if ((user_cnt <= cur_spd->user_lwm) &&
11174667Smh27603 	    (idle_cnt >= cur_spd->idle_hwm) || !CPU_ACTIVE(cp)) {
11184667Smh27603 		cur_spd->idle_blwm_cnt = 0;
11194667Smh27603 		cur_spd->idle_bhwm_cnt = 0;
11204667Smh27603 		/*
11214667Smh27603 		 * Arrange to go to next lower speed by informing our idle
11224667Smh27603 		 * status to the power management framework.
11234667Smh27603 		 */
11244667Smh27603 		CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm);
11254667Smh27603 	} else {
11264667Smh27603 		/*
11274667Smh27603 		 * If we are between the idle water marks and have not
11284667Smh27603 		 * been here enough consecutive times to be considered
11294667Smh27603 		 * busy, just increment the count and return.
11304667Smh27603 		 */
11314667Smh27603 		if ((idle_cnt < cur_spd->idle_hwm) &&
11324667Smh27603 		    (idle_cnt >= cur_spd->idle_lwm) &&
11334667Smh27603 		    (cur_spd->idle_bhwm_cnt < cpudrv_pm_idle_bhwm_cnt_max)) {
11344667Smh27603 			cur_spd->idle_blwm_cnt = 0;
11354667Smh27603 			cur_spd->idle_bhwm_cnt++;
11364667Smh27603 			mutex_exit(&cpudsp->lock);
11374667Smh27603 			goto do_return;
11384667Smh27603 		}
11394667Smh27603 		if (idle_cnt < cur_spd->idle_lwm) {
11404667Smh27603 			cur_spd->idle_blwm_cnt++;
11414667Smh27603 			cur_spd->idle_bhwm_cnt = 0;
11424667Smh27603 		}
11434667Smh27603 		/*
11444667Smh27603 		 * Arranges to stay at the current speed.
11454667Smh27603 		 */
11464667Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
11474667Smh27603 	}
11484667Smh27603 	mutex_exit(&cpudsp->lock);
11494667Smh27603 do_return:
11504667Smh27603 	mutex_enter(&cpupm->timeout_lock);
11514667Smh27603 	ASSERT(cpupm->timeout_count > 0);
11524667Smh27603 	cpupm->timeout_count--;
11534667Smh27603 	cv_signal(&cpupm->timeout_cv);
11544667Smh27603 	mutex_exit(&cpupm->timeout_lock);
11554667Smh27603 }
1156