xref: /onnv-gate/usr/src/uts/common/io/cpudrv.c (revision 10488:296c315b92df)
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 /*
228627SMark.Haywood@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
234667Smh27603  * Use is subject to license terms.
244667Smh27603  */
25*10488SMark.Haywood@Sun.COM /*
26*10488SMark.Haywood@Sun.COM  * Copyright (c) 2009,  Intel Corporation.
27*10488SMark.Haywood@Sun.COM  * All Rights Reserved.
28*10488SMark.Haywood@Sun.COM  */
294667Smh27603 
304667Smh27603 /*
314667Smh27603  * CPU Device driver. The driver is not DDI-compliant.
324667Smh27603  *
334667Smh27603  * The driver supports following features:
344667Smh27603  *	- Power management.
354667Smh27603  */
364667Smh27603 
374667Smh27603 #include <sys/types.h>
384667Smh27603 #include <sys/param.h>
394667Smh27603 #include <sys/errno.h>
404667Smh27603 #include <sys/modctl.h>
414667Smh27603 #include <sys/kmem.h>
424667Smh27603 #include <sys/conf.h>
434667Smh27603 #include <sys/cmn_err.h>
444667Smh27603 #include <sys/stat.h>
454667Smh27603 #include <sys/debug.h>
464667Smh27603 #include <sys/systm.h>
474667Smh27603 #include <sys/ddi.h>
484667Smh27603 #include <sys/sunddi.h>
495864Sesaxe #include <sys/sdt.h>
508906SEric.Saxe@Sun.COM #include <sys/epm.h>
514667Smh27603 #include <sys/machsystm.h>
524667Smh27603 #include <sys/x_call.h>
537319SMark.Haywood@Sun.COM #include <sys/cpudrv_mach.h>
544667Smh27603 #include <sys/msacct.h>
554667Smh27603 
564667Smh27603 /*
574667Smh27603  * CPU power management
584667Smh27603  *
594667Smh27603  * The supported power saving model is to slow down the CPU (on SPARC by
604667Smh27603  * dividing the CPU clock and on x86 by dropping down a P-state).
614667Smh27603  * Periodically we determine the amount of time the CPU is running
624667Smh27603  * idle thread and threads in user mode during the last quantum.  If the idle
634667Smh27603  * thread was running less than its low water mark for current speed for
644667Smh27603  * number of consecutive sampling periods, or number of running threads in
654667Smh27603  * user mode are above its high water mark, we arrange to go to the higher
664667Smh27603  * speed.  If the idle thread was running more than its high water mark without
674667Smh27603  * dropping a number of consecutive times below the mark, and number of threads
684667Smh27603  * running in user mode are below its low water mark, we arrange to go to the
694667Smh27603  * next lower speed.  While going down, we go through all the speeds.  While
704667Smh27603  * going up we go to the maximum speed to minimize impact on the user, but have
714667Smh27603  * provisions in the driver to go to other speeds.
724667Smh27603  *
734667Smh27603  * The driver does not have knowledge of a particular implementation of this
744667Smh27603  * scheme and will work with all CPUs supporting this model. On SPARC, the
754667Smh27603  * driver determines supported speeds by looking at 'clock-divisors' property
764667Smh27603  * created by OBP. On x86, the driver retrieves the supported speeds from
774667Smh27603  * ACPI.
784667Smh27603  */
794667Smh27603 
804667Smh27603 /*
814667Smh27603  * Configuration function prototypes and data structures
824667Smh27603  */
834667Smh27603 static int cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
844667Smh27603 static int cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
854667Smh27603 static int cpudrv_power(dev_info_t *dip, int comp, int level);
864667Smh27603 
874667Smh27603 struct dev_ops cpudrv_ops = {
884667Smh27603 	DEVO_REV,		/* rev */
894667Smh27603 	0,			/* refcnt */
904667Smh27603 	nodev,			/* getinfo */
914667Smh27603 	nulldev,		/* identify */
924667Smh27603 	nulldev,		/* probe */
934667Smh27603 	cpudrv_attach,		/* attach */
944667Smh27603 	cpudrv_detach,		/* detach */
954667Smh27603 	nodev,			/* reset */
964667Smh27603 	(struct cb_ops *)NULL,	/* cb_ops */
974667Smh27603 	(struct bus_ops *)NULL,	/* bus_ops */
987656SSherry.Moore@Sun.COM 	cpudrv_power,		/* power */
997656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1004667Smh27603 };
1014667Smh27603 
1024667Smh27603 static struct modldrv modldrv = {
1034667Smh27603 	&mod_driverops,			/* modops */
1047319SMark.Haywood@Sun.COM 	"CPU Driver",			/* linkinfo */
1054667Smh27603 	&cpudrv_ops,			/* dev_ops */
1064667Smh27603 };
1074667Smh27603 
1084667Smh27603 static struct modlinkage modlinkage = {
1094667Smh27603 	MODREV_1,		/* rev */
1104667Smh27603 	&modldrv,		/* linkage */
1114667Smh27603 	NULL
1124667Smh27603 };
1134667Smh27603 
1144667Smh27603 /*
1154667Smh27603  * Function prototypes
1164667Smh27603  */
1178906SEric.Saxe@Sun.COM static int cpudrv_init(cpudrv_devstate_t *cpudsp);
1188906SEric.Saxe@Sun.COM static void cpudrv_free(cpudrv_devstate_t *cpudsp);
1198906SEric.Saxe@Sun.COM static int cpudrv_comp_create(cpudrv_devstate_t *cpudsp);
1208906SEric.Saxe@Sun.COM static void cpudrv_monitor_disp(void *arg);
1218906SEric.Saxe@Sun.COM static void cpudrv_monitor(void *arg);
1224667Smh27603 
1234667Smh27603 /*
1244667Smh27603  * Driver global variables
1254667Smh27603  */
1264667Smh27603 uint_t cpudrv_debug = 0;
1274667Smh27603 void *cpudrv_state;
1288906SEric.Saxe@Sun.COM static uint_t cpudrv_idle_hwm = CPUDRV_IDLE_HWM;
1298906SEric.Saxe@Sun.COM static uint_t cpudrv_idle_lwm = CPUDRV_IDLE_LWM;
1308906SEric.Saxe@Sun.COM static uint_t cpudrv_idle_buf_zone = CPUDRV_IDLE_BUF_ZONE;
1318906SEric.Saxe@Sun.COM static uint_t cpudrv_idle_bhwm_cnt_max = CPUDRV_IDLE_BHWM_CNT_MAX;
1328906SEric.Saxe@Sun.COM static uint_t cpudrv_idle_blwm_cnt_max = CPUDRV_IDLE_BLWM_CNT_MAX;
1338906SEric.Saxe@Sun.COM static uint_t cpudrv_user_hwm = CPUDRV_USER_HWM;
1348906SEric.Saxe@Sun.COM 
1358906SEric.Saxe@Sun.COM boolean_t cpudrv_enabled = B_TRUE;
1364667Smh27603 
1374667Smh27603 /*
1384667Smh27603  * cpudrv_direct_pm allows user applications to directly control the
1394667Smh27603  * power state transitions (direct pm) without following the normal
1404667Smh27603  * direct pm protocol. This is needed because the normal protocol
1414667Smh27603  * requires that a device only be lowered when it is idle, and be
1424667Smh27603  * brought up when it request to do so by calling pm_raise_power().
1434667Smh27603  * Ignoring this protocol is harmless for CPU (other than speed).
1444667Smh27603  * Moreover it might be the case that CPU is never idle or wants
1454667Smh27603  * to be at higher speed because of the addition CPU cycles required
1464667Smh27603  * to run the user application.
1474667Smh27603  *
1484667Smh27603  * The driver will still report idle/busy status to the framework. Although
1494667Smh27603  * framework will ignore this information for direct pm devices and not
1504667Smh27603  * try to bring them down when idle, user applications can still use this
1514667Smh27603  * information if they wants.
1524667Smh27603  *
1534667Smh27603  * In the future, provide an ioctl to control setting of this mode. In
1544667Smh27603  * that case, this variable should move to the state structure and
1554667Smh27603  * be protected by the lock in the state structure.
1564667Smh27603  */
1574667Smh27603 int cpudrv_direct_pm = 0;
1584667Smh27603 
1594667Smh27603 /*
1604667Smh27603  * Arranges for the handler function to be called at the interval suitable
1614667Smh27603  * for current speed.
1624667Smh27603  */
1638906SEric.Saxe@Sun.COM #define	CPUDRV_MONITOR_INIT(cpudsp) { \
1648906SEric.Saxe@Sun.COM     if (cpudrv_is_enabled(cpudsp)) {	      \
1657319SMark.Haywood@Sun.COM 		ASSERT(mutex_owned(&(cpudsp)->lock)); \
1667319SMark.Haywood@Sun.COM 		(cpudsp)->cpudrv_pm.timeout_id = \
1678906SEric.Saxe@Sun.COM 		    timeout(cpudrv_monitor_disp, \
1687319SMark.Haywood@Sun.COM 		    (cpudsp), (((cpudsp)->cpudrv_pm.cur_spd == NULL) ? \
1698906SEric.Saxe@Sun.COM 		    CPUDRV_QUANT_CNT_OTHR : \
1707319SMark.Haywood@Sun.COM 		    (cpudsp)->cpudrv_pm.cur_spd->quant_cnt)); \
1717319SMark.Haywood@Sun.COM 	} \
1724667Smh27603 }
1734667Smh27603 
1744667Smh27603 /*
1754667Smh27603  * Arranges for the handler function not to be called back.
1764667Smh27603  */
1778906SEric.Saxe@Sun.COM #define	CPUDRV_MONITOR_FINI(cpudsp) { \
1784667Smh27603 	timeout_id_t tmp_tid; \
1794667Smh27603 	ASSERT(mutex_owned(&(cpudsp)->lock)); \
1804667Smh27603 	tmp_tid = (cpudsp)->cpudrv_pm.timeout_id; \
1814667Smh27603 	(cpudsp)->cpudrv_pm.timeout_id = 0; \
1824667Smh27603 	mutex_exit(&(cpudsp)->lock); \
1837319SMark.Haywood@Sun.COM 	if (tmp_tid != 0) { \
1847319SMark.Haywood@Sun.COM 		(void) untimeout(tmp_tid); \
1857319SMark.Haywood@Sun.COM 		mutex_enter(&(cpudsp)->cpudrv_pm.timeout_lock); \
1867319SMark.Haywood@Sun.COM 		while ((cpudsp)->cpudrv_pm.timeout_count != 0) \
1877319SMark.Haywood@Sun.COM 			cv_wait(&(cpudsp)->cpudrv_pm.timeout_cv, \
1887319SMark.Haywood@Sun.COM 			    &(cpudsp)->cpudrv_pm.timeout_lock); \
1897319SMark.Haywood@Sun.COM 		mutex_exit(&(cpudsp)->cpudrv_pm.timeout_lock); \
1907319SMark.Haywood@Sun.COM 	} \
1914667Smh27603 	mutex_enter(&(cpudsp)->lock); \
1924667Smh27603 }
1934667Smh27603 
1944667Smh27603 int
1954667Smh27603 _init(void)
1964667Smh27603 {
1974667Smh27603 	int	error;
1984667Smh27603 
1994667Smh27603 	DPRINTF(D_INIT, (" _init: function called\n"));
2004667Smh27603 	if ((error = ddi_soft_state_init(&cpudrv_state,
2014667Smh27603 	    sizeof (cpudrv_devstate_t), 0)) != 0) {
2024667Smh27603 		return (error);
2034667Smh27603 	}
2044667Smh27603 
2054667Smh27603 	if ((error = mod_install(&modlinkage)) != 0)  {
2064667Smh27603 		ddi_soft_state_fini(&cpudrv_state);
2074667Smh27603 	}
2084667Smh27603 
2094667Smh27603 	/*
2104667Smh27603 	 * Callbacks used by the PPM driver.
2114667Smh27603 	 */
2128906SEric.Saxe@Sun.COM 	CPUDRV_SET_PPM_CALLBACKS();
2134667Smh27603 	return (error);
2144667Smh27603 }
2154667Smh27603 
2164667Smh27603 int
2174667Smh27603 _fini(void)
2184667Smh27603 {
2194667Smh27603 	int	error;
2204667Smh27603 
2214667Smh27603 	DPRINTF(D_FINI, (" _fini: function called\n"));
2224667Smh27603 	if ((error = mod_remove(&modlinkage)) == 0) {
2234667Smh27603 		ddi_soft_state_fini(&cpudrv_state);
2244667Smh27603 	}
2254667Smh27603 
2264667Smh27603 	return (error);
2274667Smh27603 }
2284667Smh27603 
2294667Smh27603 int
2304667Smh27603 _info(struct modinfo *modinfop)
2314667Smh27603 {
2324667Smh27603 	return (mod_info(&modlinkage, modinfop));
2334667Smh27603 }
2344667Smh27603 
2354667Smh27603 /*
2364667Smh27603  * Driver attach(9e) entry point.
2374667Smh27603  */
2384667Smh27603 static int
2394667Smh27603 cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2404667Smh27603 {
2414667Smh27603 	int			instance;
2424667Smh27603 	cpudrv_devstate_t	*cpudsp;
2434667Smh27603 
2444667Smh27603 	instance = ddi_get_instance(dip);
2454667Smh27603 
2464667Smh27603 	switch (cmd) {
2474667Smh27603 	case DDI_ATTACH:
2484667Smh27603 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
2494667Smh27603 		    "DDI_ATTACH called\n", instance));
2508906SEric.Saxe@Sun.COM 		if (!cpudrv_is_enabled(NULL))
2517319SMark.Haywood@Sun.COM 			return (DDI_FAILURE);
2524667Smh27603 		if (ddi_soft_state_zalloc(cpudrv_state, instance) !=
2534667Smh27603 		    DDI_SUCCESS) {
2544667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2554667Smh27603 			    "can't allocate state", instance);
2568906SEric.Saxe@Sun.COM 			cpudrv_enabled = B_FALSE;
2574667Smh27603 			return (DDI_FAILURE);
2584667Smh27603 		}
2594667Smh27603 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
2604667Smh27603 		    NULL) {
2614667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2624667Smh27603 			    "can't get state", instance);
2634667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2648906SEric.Saxe@Sun.COM 			cpudrv_enabled = B_FALSE;
2654667Smh27603 			return (DDI_FAILURE);
2664667Smh27603 		}
2674667Smh27603 		cpudsp->dip = dip;
2684667Smh27603 
2694667Smh27603 		/*
2704667Smh27603 		 * Find CPU number for this dev_info node.
2714667Smh27603 		 */
2728906SEric.Saxe@Sun.COM 		if (!cpudrv_get_cpu_id(dip, &(cpudsp->cpu_id))) {
2734667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2744667Smh27603 			    "can't convert dip to cpu_id", instance);
2754667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2768906SEric.Saxe@Sun.COM 			cpudrv_enabled = B_FALSE;
2778906SEric.Saxe@Sun.COM 			return (DDI_FAILURE);
2788906SEric.Saxe@Sun.COM 		}
279*10488SMark.Haywood@Sun.COM 
280*10488SMark.Haywood@Sun.COM 		mutex_enter(&cpu_lock);
281*10488SMark.Haywood@Sun.COM 		cpudsp->cp = cpu_get(cpudsp->cpu_id);
282*10488SMark.Haywood@Sun.COM 		mutex_exit(&cpu_lock);
283*10488SMark.Haywood@Sun.COM 		if (cpudsp->cp == NULL) {
284*10488SMark.Haywood@Sun.COM 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
285*10488SMark.Haywood@Sun.COM 			    "can't get cpu_t", ddi_get_instance(cpudsp->dip));
286*10488SMark.Haywood@Sun.COM 			ddi_soft_state_free(cpudrv_state, instance);
2878906SEric.Saxe@Sun.COM 			cpudrv_enabled = B_FALSE;
2884667Smh27603 			return (DDI_FAILURE);
2894667Smh27603 		}
2908906SEric.Saxe@Sun.COM 
2917319SMark.Haywood@Sun.COM 		mutex_init(&cpudsp->lock, NULL, MUTEX_DRIVER, NULL);
2928906SEric.Saxe@Sun.COM 		if (cpudrv_is_enabled(cpudsp)) {
2938906SEric.Saxe@Sun.COM 			if (cpudrv_init(cpudsp) != DDI_SUCCESS) {
2948906SEric.Saxe@Sun.COM 				cpudrv_enabled = B_FALSE;
2958906SEric.Saxe@Sun.COM 				cpudrv_free(cpudsp);
2967319SMark.Haywood@Sun.COM 				ddi_soft_state_free(cpudrv_state, instance);
2977319SMark.Haywood@Sun.COM 				return (DDI_FAILURE);
2987319SMark.Haywood@Sun.COM 			}
2998906SEric.Saxe@Sun.COM 			if (cpudrv_comp_create(cpudsp) != DDI_SUCCESS) {
3008906SEric.Saxe@Sun.COM 				cpudrv_enabled = B_FALSE;
3018906SEric.Saxe@Sun.COM 				cpudrv_free(cpudsp);
3027319SMark.Haywood@Sun.COM 				ddi_soft_state_free(cpudrv_state, instance);
3037319SMark.Haywood@Sun.COM 				return (DDI_FAILURE);
3047319SMark.Haywood@Sun.COM 			}
3057319SMark.Haywood@Sun.COM 			if (ddi_prop_update_string(DDI_DEV_T_NONE,
3067319SMark.Haywood@Sun.COM 			    dip, "pm-class", "CPU") != DDI_PROP_SUCCESS) {
3078906SEric.Saxe@Sun.COM 				cpudrv_enabled = B_FALSE;
3088906SEric.Saxe@Sun.COM 				cpudrv_free(cpudsp);
3097319SMark.Haywood@Sun.COM 				ddi_soft_state_free(cpudrv_state, instance);
3107319SMark.Haywood@Sun.COM 				return (DDI_FAILURE);
3117319SMark.Haywood@Sun.COM 			}
3127319SMark.Haywood@Sun.COM 
3137319SMark.Haywood@Sun.COM 			/*
3147319SMark.Haywood@Sun.COM 			 * Taskq is used to dispatch routine to monitor CPU
3157319SMark.Haywood@Sun.COM 			 * activities.
3167319SMark.Haywood@Sun.COM 			 */
317*10488SMark.Haywood@Sun.COM 			cpudsp->cpudrv_pm.tq = ddi_taskq_create(dip,
318*10488SMark.Haywood@Sun.COM 			    "cpudrv_monitor", CPUDRV_TASKQ_THREADS,
319*10488SMark.Haywood@Sun.COM 			    TASKQ_DEFAULTPRI, 0);
3207319SMark.Haywood@Sun.COM 
3217319SMark.Haywood@Sun.COM 			mutex_init(&cpudsp->cpudrv_pm.timeout_lock, NULL,
3227319SMark.Haywood@Sun.COM 			    MUTEX_DRIVER, NULL);
3237319SMark.Haywood@Sun.COM 			cv_init(&cpudsp->cpudrv_pm.timeout_cv, NULL,
3247319SMark.Haywood@Sun.COM 			    CV_DEFAULT, NULL);
3257319SMark.Haywood@Sun.COM 
3267319SMark.Haywood@Sun.COM 			/*
3277319SMark.Haywood@Sun.COM 			 * Driver needs to assume that CPU is running at
3287319SMark.Haywood@Sun.COM 			 * unknown speed at DDI_ATTACH and switch it to the
3297319SMark.Haywood@Sun.COM 			 * needed speed. We assume that initial needed speed
3307319SMark.Haywood@Sun.COM 			 * is full speed for us.
3317319SMark.Haywood@Sun.COM 			 */
3327319SMark.Haywood@Sun.COM 			/*
3338906SEric.Saxe@Sun.COM 			 * We need to take the lock because cpudrv_monitor()
3347319SMark.Haywood@Sun.COM 			 * will start running in parallel with attach().
3357319SMark.Haywood@Sun.COM 			 */
3367319SMark.Haywood@Sun.COM 			mutex_enter(&cpudsp->lock);
3377319SMark.Haywood@Sun.COM 			cpudsp->cpudrv_pm.cur_spd = NULL;
3387319SMark.Haywood@Sun.COM 			cpudsp->cpudrv_pm.pm_started = B_FALSE;
3397319SMark.Haywood@Sun.COM 			/*
3407319SMark.Haywood@Sun.COM 			 * We don't call pm_raise_power() directly from attach
3417319SMark.Haywood@Sun.COM 			 * because driver attach for a slave CPU node can
3427319SMark.Haywood@Sun.COM 			 * happen before the CPU is even initialized. We just
3437319SMark.Haywood@Sun.COM 			 * start the monitoring system which understands
3448409SMark.Haywood@Sun.COM 			 * unknown speed and moves CPU to top speed when it
3458409SMark.Haywood@Sun.COM 			 * has been initialized.
3467319SMark.Haywood@Sun.COM 			 */
3478906SEric.Saxe@Sun.COM 			CPUDRV_MONITOR_INIT(cpudsp);
3487319SMark.Haywood@Sun.COM 			mutex_exit(&cpudsp->lock);
3497319SMark.Haywood@Sun.COM 
3504667Smh27603 		}
3514667Smh27603 
352*10488SMark.Haywood@Sun.COM 		if (!cpudrv_mach_init(cpudsp)) {
353*10488SMark.Haywood@Sun.COM 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
354*10488SMark.Haywood@Sun.COM 			    "cpudrv_mach_init failed", instance);
355*10488SMark.Haywood@Sun.COM 			cpudrv_enabled = B_FALSE;
356*10488SMark.Haywood@Sun.COM 			cpudrv_free(cpudsp);
357*10488SMark.Haywood@Sun.COM 			ddi_soft_state_free(cpudrv_state, instance);
358*10488SMark.Haywood@Sun.COM 			return (DDI_FAILURE);
359*10488SMark.Haywood@Sun.COM 		}
360*10488SMark.Haywood@Sun.COM 
3618906SEric.Saxe@Sun.COM 		CPUDRV_INSTALL_MAX_CHANGE_HANDLER(cpudsp);
3624667Smh27603 
363*10488SMark.Haywood@Sun.COM 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
364*10488SMark.Haywood@Sun.COM 		    DDI_NO_AUTODETACH, 1);
3654667Smh27603 		ddi_report_dev(dip);
3664667Smh27603 		return (DDI_SUCCESS);
3674667Smh27603 
3684667Smh27603 	case DDI_RESUME:
3694667Smh27603 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
3704667Smh27603 		    "DDI_RESUME called\n", instance));
3717319SMark.Haywood@Sun.COM 
3727319SMark.Haywood@Sun.COM 		cpudsp = ddi_get_soft_state(cpudrv_state, instance);
3737319SMark.Haywood@Sun.COM 		ASSERT(cpudsp != NULL);
3747319SMark.Haywood@Sun.COM 
3757319SMark.Haywood@Sun.COM 		/*
3767319SMark.Haywood@Sun.COM 		 * Nothing to do for resume, if not doing active PM.
3777319SMark.Haywood@Sun.COM 		 */
3788906SEric.Saxe@Sun.COM 		if (!cpudrv_is_enabled(cpudsp))
3797319SMark.Haywood@Sun.COM 			return (DDI_SUCCESS);
3807319SMark.Haywood@Sun.COM 
3814667Smh27603 		mutex_enter(&cpudsp->lock);
3824667Smh27603 		/*
3834667Smh27603 		 * Driver needs to assume that CPU is running at unknown speed
3844667Smh27603 		 * at DDI_RESUME and switch it to the needed speed. We assume
3854667Smh27603 		 * that the needed speed is full speed for us.
3864667Smh27603 		 */
3874667Smh27603 		cpudsp->cpudrv_pm.cur_spd = NULL;
3888906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_INIT(cpudsp);
3894667Smh27603 		mutex_exit(&cpudsp->lock);
3908906SEric.Saxe@Sun.COM 		CPUDRV_REDEFINE_TOPSPEED(dip);
3914667Smh27603 		return (DDI_SUCCESS);
3924667Smh27603 
3934667Smh27603 	default:
3944667Smh27603 		return (DDI_FAILURE);
3954667Smh27603 	}
3964667Smh27603 }
3974667Smh27603 
3984667Smh27603 /*
3994667Smh27603  * Driver detach(9e) entry point.
4004667Smh27603  */
4014667Smh27603 static int
4024667Smh27603 cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4034667Smh27603 {
4044667Smh27603 	int			instance;
4054667Smh27603 	cpudrv_devstate_t	*cpudsp;
4064667Smh27603 	cpudrv_pm_t		*cpupm;
4074667Smh27603 
4084667Smh27603 	instance = ddi_get_instance(dip);
4094667Smh27603 
4104667Smh27603 	switch (cmd) {
4114667Smh27603 	case DDI_DETACH:
4124667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
4134667Smh27603 		    "DDI_DETACH called\n", instance));
414*10488SMark.Haywood@Sun.COM 
415*10488SMark.Haywood@Sun.COM #if defined(__x86)
416*10488SMark.Haywood@Sun.COM 		cpudsp = ddi_get_soft_state(cpudrv_state, instance);
417*10488SMark.Haywood@Sun.COM 		ASSERT(cpudsp != NULL);
418*10488SMark.Haywood@Sun.COM 
419*10488SMark.Haywood@Sun.COM 		/*
420*10488SMark.Haywood@Sun.COM 		 * Nothing to do for detach, if no doing active PM.
421*10488SMark.Haywood@Sun.COM 		 */
422*10488SMark.Haywood@Sun.COM 		if (!cpudrv_is_enabled(cpudsp))
423*10488SMark.Haywood@Sun.COM 			return (DDI_SUCCESS);
424*10488SMark.Haywood@Sun.COM 
425*10488SMark.Haywood@Sun.COM 		/*
426*10488SMark.Haywood@Sun.COM 		 * uninstall PPC/_TPC change notification handler
427*10488SMark.Haywood@Sun.COM 		 */
428*10488SMark.Haywood@Sun.COM 		CPUDRV_UNINSTALL_MAX_CHANGE_HANDLER(cpudsp);
429*10488SMark.Haywood@Sun.COM 
430*10488SMark.Haywood@Sun.COM 		/*
431*10488SMark.Haywood@Sun.COM 		 * destruct platform specific resource
432*10488SMark.Haywood@Sun.COM 		 */
433*10488SMark.Haywood@Sun.COM 		if (!cpudrv_mach_fini(cpudsp))
434*10488SMark.Haywood@Sun.COM 			return (DDI_FAILURE);
435*10488SMark.Haywood@Sun.COM 
436*10488SMark.Haywood@Sun.COM 		mutex_enter(&cpudsp->lock);
437*10488SMark.Haywood@Sun.COM 		CPUDRV_MONITOR_FINI(cpudsp);
438*10488SMark.Haywood@Sun.COM 		cv_destroy(&cpudsp->cpudrv_pm.timeout_cv);
439*10488SMark.Haywood@Sun.COM 		mutex_destroy(&cpudsp->cpudrv_pm.timeout_lock);
440*10488SMark.Haywood@Sun.COM 		ddi_taskq_destroy(cpudsp->cpudrv_pm.tq);
441*10488SMark.Haywood@Sun.COM 		cpudrv_free(cpudsp);
442*10488SMark.Haywood@Sun.COM 		mutex_exit(&cpudsp->lock);
443*10488SMark.Haywood@Sun.COM 		mutex_destroy(&cpudsp->lock);
444*10488SMark.Haywood@Sun.COM 		ddi_soft_state_free(cpudrv_state, instance);
445*10488SMark.Haywood@Sun.COM 		(void) ddi_prop_update_int(DDI_DEV_T_NONE, dip,
446*10488SMark.Haywood@Sun.COM 		    DDI_NO_AUTODETACH, 0);
447*10488SMark.Haywood@Sun.COM 		return (DDI_SUCCESS);
448*10488SMark.Haywood@Sun.COM 
449*10488SMark.Haywood@Sun.COM #else
4504667Smh27603 		/*
4514667Smh27603 		 * If the only thing supported by the driver is power
4524667Smh27603 		 * management, we can in future enhance the driver and
4534667Smh27603 		 * framework that loads it to unload the driver when
4544667Smh27603 		 * user has disabled CPU power management.
4554667Smh27603 		 */
4564667Smh27603 		return (DDI_FAILURE);
457*10488SMark.Haywood@Sun.COM #endif
4584667Smh27603 
4594667Smh27603 	case DDI_SUSPEND:
4604667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
4614667Smh27603 		    "DDI_SUSPEND called\n", instance));
4627319SMark.Haywood@Sun.COM 
4637319SMark.Haywood@Sun.COM 		cpudsp = ddi_get_soft_state(cpudrv_state, instance);
4647319SMark.Haywood@Sun.COM 		ASSERT(cpudsp != NULL);
4657319SMark.Haywood@Sun.COM 
4667319SMark.Haywood@Sun.COM 		/*
4677319SMark.Haywood@Sun.COM 		 * Nothing to do for suspend, if not doing active PM.
4687319SMark.Haywood@Sun.COM 		 */
4698906SEric.Saxe@Sun.COM 		if (!cpudrv_is_enabled(cpudsp))
4707319SMark.Haywood@Sun.COM 			return (DDI_SUCCESS);
4717319SMark.Haywood@Sun.COM 
4724667Smh27603 		/*
4734667Smh27603 		 * During a checkpoint-resume sequence, framework will
4744667Smh27603 		 * stop interrupts to quiesce kernel activity. This will
4754667Smh27603 		 * leave our monitoring system ineffective. Handle this
4764667Smh27603 		 * by stopping our monitoring system and bringing CPU
4774667Smh27603 		 * to full speed. In case we are in special direct pm
4784667Smh27603 		 * mode, we leave the CPU at whatever speed it is. This
4794667Smh27603 		 * is harmless other than speed.
4804667Smh27603 		 */
4814667Smh27603 		mutex_enter(&cpudsp->lock);
4824667Smh27603 		cpupm = &(cpudsp->cpudrv_pm);
4834667Smh27603 
4844667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: DDI_SUSPEND - "
4858409SMark.Haywood@Sun.COM 		    "cur_spd %d, topspeed %d\n", instance,
4868409SMark.Haywood@Sun.COM 		    cpupm->cur_spd->pm_level,
4878906SEric.Saxe@Sun.COM 		    CPUDRV_TOPSPEED(cpupm)->pm_level));
4884667Smh27603 
4898906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_FINI(cpudsp);
4904667Smh27603 
4918409SMark.Haywood@Sun.COM 		if (!cpudrv_direct_pm && (cpupm->cur_spd !=
4928906SEric.Saxe@Sun.COM 		    CPUDRV_TOPSPEED(cpupm))) {
4934667Smh27603 			if (cpupm->pm_busycnt < 1) {
4948906SEric.Saxe@Sun.COM 				if ((pm_busy_component(dip, CPUDRV_COMP_NUM)
4954667Smh27603 				    == DDI_SUCCESS)) {
4964667Smh27603 					cpupm->pm_busycnt++;
4974667Smh27603 				} else {
4988906SEric.Saxe@Sun.COM 					CPUDRV_MONITOR_INIT(cpudsp);
4994667Smh27603 					mutex_exit(&cpudsp->lock);
5004667Smh27603 					cmn_err(CE_WARN, "cpudrv_detach: "
5014667Smh27603 					    "instance %d: can't busy CPU "
5024667Smh27603 					    "component", instance);
5034667Smh27603 					return (DDI_FAILURE);
5044667Smh27603 				}
5054667Smh27603 			}
5064667Smh27603 			mutex_exit(&cpudsp->lock);
5078906SEric.Saxe@Sun.COM 			if (pm_raise_power(dip, CPUDRV_COMP_NUM,
5088906SEric.Saxe@Sun.COM 			    CPUDRV_TOPSPEED(cpupm)->pm_level) !=
5098409SMark.Haywood@Sun.COM 			    DDI_SUCCESS) {
5104667Smh27603 				mutex_enter(&cpudsp->lock);
5118906SEric.Saxe@Sun.COM 				CPUDRV_MONITOR_INIT(cpudsp);
5124667Smh27603 				mutex_exit(&cpudsp->lock);
5134667Smh27603 				cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
5148409SMark.Haywood@Sun.COM 				    "can't raise CPU power level to %d",
5158409SMark.Haywood@Sun.COM 				    instance,
5168906SEric.Saxe@Sun.COM 				    CPUDRV_TOPSPEED(cpupm)->pm_level);
5174667Smh27603 				return (DDI_FAILURE);
5184667Smh27603 			} else {
5194667Smh27603 				return (DDI_SUCCESS);
5204667Smh27603 			}
5214667Smh27603 		} else {
5224667Smh27603 			mutex_exit(&cpudsp->lock);
5234667Smh27603 			return (DDI_SUCCESS);
5244667Smh27603 		}
5254667Smh27603 
5264667Smh27603 	default:
5274667Smh27603 		return (DDI_FAILURE);
5284667Smh27603 	}
5294667Smh27603 }
5304667Smh27603 
5314667Smh27603 /*
5324667Smh27603  * Driver power(9e) entry point.
5334667Smh27603  *
5344667Smh27603  * Driver's notion of current power is set *only* in power(9e) entry point
5354667Smh27603  * after actual power change operation has been successfully completed.
5364667Smh27603  */
5374667Smh27603 /* ARGSUSED */
5384667Smh27603 static int
5394667Smh27603 cpudrv_power(dev_info_t *dip, int comp, int level)
5404667Smh27603 {
5414667Smh27603 	int			instance;
5424667Smh27603 	cpudrv_devstate_t	*cpudsp;
5438906SEric.Saxe@Sun.COM 	cpudrv_pm_t 		*cpudrvpm;
5444667Smh27603 	cpudrv_pm_spd_t		*new_spd;
5454667Smh27603 	boolean_t		is_ready;
5464667Smh27603 	int			ret;
5474667Smh27603 
5484667Smh27603 	instance = ddi_get_instance(dip);
5494667Smh27603 
5504667Smh27603 	DPRINTF(D_POWER, ("cpudrv_power: instance %d: level %d\n",
5514667Smh27603 	    instance, level));
5528906SEric.Saxe@Sun.COM 
5534667Smh27603 	if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) == NULL) {
5548906SEric.Saxe@Sun.COM 		cmn_err(CE_WARN, "cpudrv_power: instance %d: can't "
5558906SEric.Saxe@Sun.COM 		    "get state", instance);
5564667Smh27603 		return (DDI_FAILURE);
5574667Smh27603 	}
5584667Smh27603 
5594667Smh27603 	mutex_enter(&cpudsp->lock);
5608906SEric.Saxe@Sun.COM 	cpudrvpm = &(cpudsp->cpudrv_pm);
5614667Smh27603 
5624667Smh27603 	/*
5634667Smh27603 	 * In normal operation, we fail if we are busy and request is
5644667Smh27603 	 * to lower the power level. We let this go through if the driver
5654667Smh27603 	 * is in special direct pm mode. On x86, we also let this through
5667319SMark.Haywood@Sun.COM 	 * if the change is due to a request to govern the max speed.
5674667Smh27603 	 */
5688906SEric.Saxe@Sun.COM 	if (!cpudrv_direct_pm && (cpudrvpm->pm_busycnt >= 1) &&
5698906SEric.Saxe@Sun.COM 	    !cpudrv_is_governor_thread(cpudrvpm)) {
5708906SEric.Saxe@Sun.COM 		if ((cpudrvpm->cur_spd != NULL) &&
5718906SEric.Saxe@Sun.COM 		    (level < cpudrvpm->cur_spd->pm_level)) {
5724667Smh27603 			mutex_exit(&cpudsp->lock);
5734667Smh27603 			return (DDI_FAILURE);
5744667Smh27603 		}
5754667Smh27603 	}
5764667Smh27603 
5778906SEric.Saxe@Sun.COM 	for (new_spd = cpudrvpm->head_spd; new_spd; new_spd =
5788906SEric.Saxe@Sun.COM 	    new_spd->down_spd) {
5794667Smh27603 		if (new_spd->pm_level == level)
5804667Smh27603 			break;
5814667Smh27603 	}
5824667Smh27603 	if (!new_spd) {
5838906SEric.Saxe@Sun.COM 		CPUDRV_RESET_GOVERNOR_THREAD(cpudrvpm);
5844667Smh27603 		mutex_exit(&cpudsp->lock);
5854667Smh27603 		cmn_err(CE_WARN, "cpudrv_power: instance %d: "
5864667Smh27603 		    "can't locate new CPU speed", instance);
5874667Smh27603 		return (DDI_FAILURE);
5884667Smh27603 	}
5894667Smh27603 
5904667Smh27603 	/*
5914667Smh27603 	 * We currently refuse to power manage if the CPU is not ready to
5924667Smh27603 	 * take cross calls (cross calls fail silently if CPU is not ready
5934667Smh27603 	 * for it).
5944667Smh27603 	 *
595*10488SMark.Haywood@Sun.COM 	 * Additionally, for x86 platforms we cannot power manage an instance,
596*10488SMark.Haywood@Sun.COM 	 * until it has been initialized.
5974667Smh27603 	 */
598*10488SMark.Haywood@Sun.COM 	ASSERT(cpudsp->cp);
5998906SEric.Saxe@Sun.COM 	is_ready = CPUDRV_XCALL_IS_READY(cpudsp->cpu_id);
6004667Smh27603 	if (!is_ready) {
6014667Smh27603 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
6024667Smh27603 		    "CPU not ready for x-calls\n", instance));
603*10488SMark.Haywood@Sun.COM 	} else if (!(is_ready = cpudrv_power_ready(cpudsp->cp))) {
6044667Smh27603 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
6058906SEric.Saxe@Sun.COM 		    "waiting for all CPUs to be power manageable\n",
6068906SEric.Saxe@Sun.COM 		    instance));
6074667Smh27603 	}
6084667Smh27603 	if (!is_ready) {
6098906SEric.Saxe@Sun.COM 		CPUDRV_RESET_GOVERNOR_THREAD(cpudrvpm);
6104667Smh27603 		mutex_exit(&cpudsp->lock);
6114667Smh27603 		return (DDI_FAILURE);
6124667Smh27603 	}
6134667Smh27603 
6144667Smh27603 	/*
6158906SEric.Saxe@Sun.COM 	 * Execute CPU specific routine on the requested CPU to
6168906SEric.Saxe@Sun.COM 	 * change its speed to normal-speed/divisor.
6175864Sesaxe 	 */
6188906SEric.Saxe@Sun.COM 	if ((ret = cpudrv_change_speed(cpudsp, new_spd)) != DDI_SUCCESS) {
6198906SEric.Saxe@Sun.COM 		cmn_err(CE_WARN, "cpudrv_power: "
6208906SEric.Saxe@Sun.COM 		    "cpudrv_change_speed() return = %d", ret);
6218906SEric.Saxe@Sun.COM 		mutex_exit(&cpudsp->lock);
6228906SEric.Saxe@Sun.COM 		return (DDI_FAILURE);
6238906SEric.Saxe@Sun.COM 	}
6245864Sesaxe 
6255864Sesaxe 	/*
6264667Smh27603 	 * Reset idle threshold time for the new power level.
6274667Smh27603 	 */
6288906SEric.Saxe@Sun.COM 	if ((cpudrvpm->cur_spd != NULL) && (level <
6298906SEric.Saxe@Sun.COM 	    cpudrvpm->cur_spd->pm_level)) {
6308906SEric.Saxe@Sun.COM 		if (pm_idle_component(dip, CPUDRV_COMP_NUM) ==
6314667Smh27603 		    DDI_SUCCESS) {
6328906SEric.Saxe@Sun.COM 			if (cpudrvpm->pm_busycnt >= 1)
6338906SEric.Saxe@Sun.COM 				cpudrvpm->pm_busycnt--;
6348906SEric.Saxe@Sun.COM 		} else {
6358906SEric.Saxe@Sun.COM 			cmn_err(CE_WARN, "cpudrv_power: instance %d: "
6368906SEric.Saxe@Sun.COM 			    "can't idle CPU component",
6378906SEric.Saxe@Sun.COM 			    ddi_get_instance(dip));
6388906SEric.Saxe@Sun.COM 		}
6394667Smh27603 	}
6404667Smh27603 	/*
6414667Smh27603 	 * Reset various parameters because we are now running at new speed.
6424667Smh27603 	 */
6438906SEric.Saxe@Sun.COM 	cpudrvpm->lastquan_mstate[CMS_IDLE] = 0;
6448906SEric.Saxe@Sun.COM 	cpudrvpm->lastquan_mstate[CMS_SYSTEM] = 0;
6458906SEric.Saxe@Sun.COM 	cpudrvpm->lastquan_mstate[CMS_USER] = 0;
6468906SEric.Saxe@Sun.COM 	cpudrvpm->lastquan_ticks = 0;
6478906SEric.Saxe@Sun.COM 	cpudrvpm->cur_spd = new_spd;
6488906SEric.Saxe@Sun.COM 	CPUDRV_RESET_GOVERNOR_THREAD(cpudrvpm);
6494667Smh27603 	mutex_exit(&cpudsp->lock);
6504667Smh27603 
6514667Smh27603 	return (DDI_SUCCESS);
6524667Smh27603 }
6534667Smh27603 
6544667Smh27603 /*
6554667Smh27603  * Initialize power management data.
6564667Smh27603  */
6574667Smh27603 static int
6588906SEric.Saxe@Sun.COM cpudrv_init(cpudrv_devstate_t *cpudsp)
6594667Smh27603 {
6604667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
6614667Smh27603 	cpudrv_pm_spd_t	*cur_spd;
6624667Smh27603 	cpudrv_pm_spd_t	*prev_spd = NULL;
6634667Smh27603 	int		*speeds;
6644667Smh27603 	uint_t		nspeeds;
6654667Smh27603 	int		idle_cnt_percent;
6664667Smh27603 	int		user_cnt_percent;
6674667Smh27603 	int		i;
6684667Smh27603 
6698906SEric.Saxe@Sun.COM 	CPUDRV_GET_SPEEDS(cpudsp, speeds, nspeeds);
6704667Smh27603 	if (nspeeds < 2) {
6714667Smh27603 		/* Need at least two speeds to power manage */
6728906SEric.Saxe@Sun.COM 		CPUDRV_FREE_SPEEDS(speeds, nspeeds);
6734667Smh27603 		return (DDI_FAILURE);
6744667Smh27603 	}
6754667Smh27603 	cpupm->num_spd = nspeeds;
6764667Smh27603 
6774667Smh27603 	/*
6784667Smh27603 	 * Calculate the watermarks and other parameters based on the
6794667Smh27603 	 * supplied speeds.
6804667Smh27603 	 *
6814667Smh27603 	 * One of the basic assumption is that for X amount of CPU work,
6824667Smh27603 	 * if CPU is slowed down by a factor of N, the time it takes to
6834667Smh27603 	 * do the same work will be N * X.
6844667Smh27603 	 *
6854667Smh27603 	 * The driver declares that a CPU is idle and ready for slowed down,
6864667Smh27603 	 * if amount of idle thread is more than the current speed idle_hwm
6874667Smh27603 	 * without dropping below idle_hwm a number of consecutive sampling
6884667Smh27603 	 * intervals and number of running threads in user mode are below
6894667Smh27603 	 * user_lwm.  We want to set the current user_lwm such that if we
6904667Smh27603 	 * just switched to the next slower speed with no change in real work
6914667Smh27603 	 * load, the amount of user threads at the slower speed will be such
6924667Smh27603 	 * that it falls below the slower speed's user_hwm.  If we didn't do
6934667Smh27603 	 * that then we will just come back to the higher speed as soon as we
6944667Smh27603 	 * go down even with no change in work load.
6954667Smh27603 	 * The user_hwm is a fixed precentage and not calculated dynamically.
6964667Smh27603 	 *
6974667Smh27603 	 * We bring the CPU up if idle thread at current speed is less than
6984667Smh27603 	 * the current speed idle_lwm for a number of consecutive sampling
6994667Smh27603 	 * intervals or user threads are above the user_hwm for the current
7004667Smh27603 	 * speed.
7014667Smh27603 	 */
7024667Smh27603 	for (i = 0; i < nspeeds; i++) {
7034667Smh27603 		cur_spd = kmem_zalloc(sizeof (cpudrv_pm_spd_t), KM_SLEEP);
7044667Smh27603 		cur_spd->speed = speeds[i];
7054667Smh27603 		if (i == 0) {	/* normal speed */
7064667Smh27603 			cpupm->head_spd = cur_spd;
7078906SEric.Saxe@Sun.COM 			CPUDRV_TOPSPEED(cpupm) = cur_spd;
7088906SEric.Saxe@Sun.COM 			cur_spd->quant_cnt = CPUDRV_QUANT_CNT_NORMAL;
7094667Smh27603 			cur_spd->idle_hwm =
7108906SEric.Saxe@Sun.COM 			    (cpudrv_idle_hwm * cur_spd->quant_cnt) / 100;
7114667Smh27603 			/* can't speed anymore */
7124667Smh27603 			cur_spd->idle_lwm = 0;
7134667Smh27603 			cur_spd->user_hwm = UINT_MAX;
7144667Smh27603 		} else {
7158906SEric.Saxe@Sun.COM 			cur_spd->quant_cnt = CPUDRV_QUANT_CNT_OTHR;
7164667Smh27603 			ASSERT(prev_spd != NULL);
7174667Smh27603 			prev_spd->down_spd = cur_spd;
7184667Smh27603 			cur_spd->up_spd = cpupm->head_spd;
7194667Smh27603 
7204667Smh27603 			/*
7214667Smh27603 			 * Let's assume CPU is considered idle at full speed
7224667Smh27603 			 * when it is spending I% of time in running the idle
7234667Smh27603 			 * thread.  At full speed, CPU will be busy (100 - I) %
7244667Smh27603 			 * of times.  This % of busyness increases by factor of
7254667Smh27603 			 * N as CPU slows down.  CPU that is idle I% of times
7264667Smh27603 			 * in full speed, it is idle (100 - ((100 - I) * N)) %
7274667Smh27603 			 * of times in N speed.  The idle_lwm is a fixed
7284667Smh27603 			 * percentage.  A large value of N may result in
7294667Smh27603 			 * idle_hwm to go below idle_lwm.  We need to make sure
7304667Smh27603 			 * that there is at least a buffer zone seperation
7314667Smh27603 			 * between the idle_lwm and idle_hwm values.
7324667Smh27603 			 */
7338906SEric.Saxe@Sun.COM 			idle_cnt_percent = CPUDRV_IDLE_CNT_PERCENT(
7348906SEric.Saxe@Sun.COM 			    cpudrv_idle_hwm, speeds, i);
7354667Smh27603 			idle_cnt_percent = max(idle_cnt_percent,
7368906SEric.Saxe@Sun.COM 			    (cpudrv_idle_lwm + cpudrv_idle_buf_zone));
7374667Smh27603 			cur_spd->idle_hwm =
7384667Smh27603 			    (idle_cnt_percent * cur_spd->quant_cnt) / 100;
7394667Smh27603 			cur_spd->idle_lwm =
7408906SEric.Saxe@Sun.COM 			    (cpudrv_idle_lwm * cur_spd->quant_cnt) / 100;
7414667Smh27603 
7424667Smh27603 			/*
7434667Smh27603 			 * The lwm for user threads are determined such that
7444667Smh27603 			 * if CPU slows down, the load of work in the
7454667Smh27603 			 * new speed would still keep the CPU at or below the
7464667Smh27603 			 * user_hwm in the new speed.  This is to prevent
7474667Smh27603 			 * the quick jump back up to higher speed.
7484667Smh27603 			 */
7498906SEric.Saxe@Sun.COM 			cur_spd->user_hwm = (cpudrv_user_hwm *
7504667Smh27603 			    cur_spd->quant_cnt) / 100;
7518906SEric.Saxe@Sun.COM 			user_cnt_percent = CPUDRV_USER_CNT_PERCENT(
7528906SEric.Saxe@Sun.COM 			    cpudrv_user_hwm, speeds, i);
7534667Smh27603 			prev_spd->user_lwm =
7544667Smh27603 			    (user_cnt_percent * prev_spd->quant_cnt) / 100;
7554667Smh27603 		}
7564667Smh27603 		prev_spd = cur_spd;
7574667Smh27603 	}
7584667Smh27603 	/* Slowest speed. Can't slow down anymore */
7594667Smh27603 	cur_spd->idle_hwm = UINT_MAX;
7604667Smh27603 	cur_spd->user_lwm = -1;
7614667Smh27603 #ifdef	DEBUG
7628906SEric.Saxe@Sun.COM 	DPRINTF(D_PM_INIT, ("cpudrv_init: instance %d: head_spd spd %d, "
7634667Smh27603 	    "num_spd %d\n", ddi_get_instance(cpudsp->dip),
7644667Smh27603 	    cpupm->head_spd->speed, cpupm->num_spd));
7654667Smh27603 	for (cur_spd = cpupm->head_spd; cur_spd; cur_spd = cur_spd->down_spd) {
7668906SEric.Saxe@Sun.COM 		DPRINTF(D_PM_INIT, ("cpudrv_init: instance %d: speed %d, "
7674667Smh27603 		    "down_spd spd %d, idle_hwm %d, user_lwm %d, "
7684667Smh27603 		    "up_spd spd %d, idle_lwm %d, user_hwm %d, "
7694667Smh27603 		    "quant_cnt %d\n", ddi_get_instance(cpudsp->dip),
7704667Smh27603 		    cur_spd->speed,
7714667Smh27603 		    (cur_spd->down_spd ? cur_spd->down_spd->speed : 0),
7724667Smh27603 		    cur_spd->idle_hwm, cur_spd->user_lwm,
7734667Smh27603 		    (cur_spd->up_spd ? cur_spd->up_spd->speed : 0),
7744667Smh27603 		    cur_spd->idle_lwm, cur_spd->user_hwm,
7754667Smh27603 		    cur_spd->quant_cnt));
7764667Smh27603 	}
7774667Smh27603 #endif	/* DEBUG */
7788906SEric.Saxe@Sun.COM 	CPUDRV_FREE_SPEEDS(speeds, nspeeds);
7794667Smh27603 	return (DDI_SUCCESS);
7804667Smh27603 }
7814667Smh27603 
7824667Smh27603 /*
7834667Smh27603  * Free CPU power management data.
7844667Smh27603  */
7854667Smh27603 static void
7868906SEric.Saxe@Sun.COM cpudrv_free(cpudrv_devstate_t *cpudsp)
7874667Smh27603 {
7884667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
7894667Smh27603 	cpudrv_pm_spd_t	*cur_spd, *next_spd;
7904667Smh27603 
7914667Smh27603 	cur_spd = cpupm->head_spd;
7924667Smh27603 	while (cur_spd) {
7934667Smh27603 		next_spd = cur_spd->down_spd;
7944667Smh27603 		kmem_free(cur_spd, sizeof (cpudrv_pm_spd_t));
7954667Smh27603 		cur_spd = next_spd;
7964667Smh27603 	}
7974667Smh27603 	bzero(cpupm, sizeof (cpudrv_pm_t));
7984667Smh27603 }
7994667Smh27603 
8004667Smh27603 /*
8014667Smh27603  * Create pm-components property.
8024667Smh27603  */
8034667Smh27603 static int
8048906SEric.Saxe@Sun.COM cpudrv_comp_create(cpudrv_devstate_t *cpudsp)
8054667Smh27603 {
8064667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
8074667Smh27603 	cpudrv_pm_spd_t	*cur_spd;
8084667Smh27603 	char		**pmc;
8094667Smh27603 	int		size;
8104667Smh27603 	char		name[] = "NAME=CPU Speed";
8114667Smh27603 	int		i, j;
8124667Smh27603 	uint_t		comp_spd;
8134667Smh27603 	int		result = DDI_FAILURE;
8144667Smh27603 
8154667Smh27603 	pmc = kmem_zalloc((cpupm->num_spd + 1) * sizeof (char *), KM_SLEEP);
8168906SEric.Saxe@Sun.COM 	size = CPUDRV_COMP_SIZE();
8178906SEric.Saxe@Sun.COM 	if (cpupm->num_spd > CPUDRV_COMP_MAX_VAL) {
8188906SEric.Saxe@Sun.COM 		cmn_err(CE_WARN, "cpudrv_comp_create: instance %d: "
8194667Smh27603 		    "number of speeds exceeded limits",
8204667Smh27603 		    ddi_get_instance(cpudsp->dip));
8214667Smh27603 		kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
8224667Smh27603 		return (result);
8234667Smh27603 	}
8244667Smh27603 
8254667Smh27603 	for (i = cpupm->num_spd, cur_spd = cpupm->head_spd; i > 0;
8264667Smh27603 	    i--, cur_spd = cur_spd->down_spd) {
8274667Smh27603 		cur_spd->pm_level = i;
8284667Smh27603 		pmc[i] = kmem_zalloc((size * sizeof (char)), KM_SLEEP);
8298906SEric.Saxe@Sun.COM 		comp_spd = CPUDRV_COMP_SPEED(cpupm, cur_spd);
8308906SEric.Saxe@Sun.COM 		if (comp_spd > CPUDRV_COMP_MAX_VAL) {
8318906SEric.Saxe@Sun.COM 			cmn_err(CE_WARN, "cpudrv_comp_create: "
8324667Smh27603 			    "instance %d: speed exceeded limits",
8334667Smh27603 			    ddi_get_instance(cpudsp->dip));
8344667Smh27603 			for (j = cpupm->num_spd; j >= i; j--) {
8354667Smh27603 				kmem_free(pmc[j], size * sizeof (char));
8364667Smh27603 			}
8374667Smh27603 			kmem_free(pmc, (cpupm->num_spd + 1) *
8384667Smh27603 			    sizeof (char *));
8394667Smh27603 			return (result);
8404667Smh27603 		}
8418906SEric.Saxe@Sun.COM 		CPUDRV_COMP_SPRINT(pmc[i], cpupm, cur_spd, comp_spd)
8428906SEric.Saxe@Sun.COM 		DPRINTF(D_PM_COMP_CREATE, ("cpudrv_comp_create: "
8434667Smh27603 		    "instance %d: pm-components power level %d string '%s'\n",
8444667Smh27603 		    ddi_get_instance(cpudsp->dip), i, pmc[i]));
8454667Smh27603 	}
8464667Smh27603 	pmc[0] = kmem_zalloc(sizeof (name), KM_SLEEP);
8474667Smh27603 	(void) strcat(pmc[0], name);
8488906SEric.Saxe@Sun.COM 	DPRINTF(D_PM_COMP_CREATE, ("cpudrv_comp_create: instance %d: "
8494667Smh27603 	    "pm-components component name '%s'\n",
8504667Smh27603 	    ddi_get_instance(cpudsp->dip), pmc[0]));
8514667Smh27603 
8524667Smh27603 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, cpudsp->dip,
8534667Smh27603 	    "pm-components", pmc, cpupm->num_spd + 1) == DDI_PROP_SUCCESS) {
8544667Smh27603 		result = DDI_SUCCESS;
8554667Smh27603 	} else {
8568906SEric.Saxe@Sun.COM 		cmn_err(CE_WARN, "cpudrv_comp_create: instance %d: "
8574667Smh27603 		    "can't create pm-components property",
8584667Smh27603 		    ddi_get_instance(cpudsp->dip));
8594667Smh27603 	}
8604667Smh27603 
8614667Smh27603 	for (i = cpupm->num_spd; i > 0; i--) {
8624667Smh27603 		kmem_free(pmc[i], size * sizeof (char));
8634667Smh27603 	}
8644667Smh27603 	kmem_free(pmc[0], sizeof (name));
8654667Smh27603 	kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
8664667Smh27603 	return (result);
8674667Smh27603 }
8684667Smh27603 
8694667Smh27603 /*
8704667Smh27603  * Mark a component idle.
8714667Smh27603  */
8728906SEric.Saxe@Sun.COM #define	CPUDRV_MONITOR_PM_IDLE_COMP(dip, cpupm) { \
8734667Smh27603 	if ((cpupm)->pm_busycnt >= 1) { \
8748906SEric.Saxe@Sun.COM 		if (pm_idle_component((dip), CPUDRV_COMP_NUM) == \
8754667Smh27603 		    DDI_SUCCESS) { \
8768906SEric.Saxe@Sun.COM 			DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: " \
8774667Smh27603 			    "instance %d: pm_idle_component called\n", \
8784667Smh27603 			    ddi_get_instance((dip)))); \
8794667Smh27603 			(cpupm)->pm_busycnt--; \
8804667Smh27603 		} else { \
8818906SEric.Saxe@Sun.COM 			cmn_err(CE_WARN, "cpudrv_monitor: instance %d: " \
8824667Smh27603 			    "can't idle CPU component", \
8834667Smh27603 			    ddi_get_instance((dip))); \
8844667Smh27603 		} \
8854667Smh27603 	} \
8864667Smh27603 }
8874667Smh27603 
8884667Smh27603 /*
8894667Smh27603  * Marks a component busy in both PM framework and driver state structure.
8904667Smh27603  */
8918906SEric.Saxe@Sun.COM #define	CPUDRV_MONITOR_PM_BUSY_COMP(dip, cpupm) { \
8924667Smh27603 	if ((cpupm)->pm_busycnt < 1) { \
8938906SEric.Saxe@Sun.COM 		if (pm_busy_component((dip), CPUDRV_COMP_NUM) == \
8944667Smh27603 		    DDI_SUCCESS) { \
8958906SEric.Saxe@Sun.COM 			DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: " \
8964667Smh27603 			    "instance %d: pm_busy_component called\n", \
8974667Smh27603 			    ddi_get_instance((dip)))); \
8984667Smh27603 			(cpupm)->pm_busycnt++; \
8994667Smh27603 		} else { \
9008906SEric.Saxe@Sun.COM 			cmn_err(CE_WARN, "cpudrv_monitor: instance %d: " \
9014667Smh27603 			    "can't busy CPU component", \
9024667Smh27603 			    ddi_get_instance((dip))); \
9034667Smh27603 		} \
9044667Smh27603 	} \
9054667Smh27603 }
9064667Smh27603 
9074667Smh27603 /*
9084667Smh27603  * Marks a component busy and calls pm_raise_power().
9094667Smh27603  */
9109766SMark.Haywood@Sun.COM #define	CPUDRV_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm, new_spd) { \
9119766SMark.Haywood@Sun.COM 	int ret; \
9124667Smh27603 	/* \
9134667Smh27603 	 * Mark driver and PM framework busy first so framework doesn't try \
9144667Smh27603 	 * to bring CPU to lower speed when we need to be at higher speed. \
9154667Smh27603 	 */ \
9168906SEric.Saxe@Sun.COM 	CPUDRV_MONITOR_PM_BUSY_COMP((dip), (cpupm)); \
9174667Smh27603 	mutex_exit(&(cpudsp)->lock); \
9188906SEric.Saxe@Sun.COM 	DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: instance %d: " \
9194667Smh27603 	    "pm_raise_power called to %d\n", ddi_get_instance((dip)), \
9209766SMark.Haywood@Sun.COM 		(new_spd->pm_level))); \
9219766SMark.Haywood@Sun.COM 	ret = pm_raise_power((dip), CPUDRV_COMP_NUM, (new_spd->pm_level)); \
9229766SMark.Haywood@Sun.COM 	if (ret != DDI_SUCCESS) { \
9238906SEric.Saxe@Sun.COM 		cmn_err(CE_WARN, "cpudrv_monitor: instance %d: can't " \
9244667Smh27603 		    "raise CPU power level", ddi_get_instance((dip))); \
9254667Smh27603 	} \
9264667Smh27603 	mutex_enter(&(cpudsp)->lock); \
9279766SMark.Haywood@Sun.COM 	if (ret == DDI_SUCCESS && cpudsp->cpudrv_pm.cur_spd == NULL) { \
9289766SMark.Haywood@Sun.COM 		cpudsp->cpudrv_pm.cur_spd = new_spd; \
9299766SMark.Haywood@Sun.COM 	} \
9304667Smh27603 }
9314667Smh27603 
9324667Smh27603 /*
9334667Smh27603  * In order to monitor a CPU, we need to hold cpu_lock to access CPU
9344667Smh27603  * statistics. Holding cpu_lock is not allowed from a callout routine.
9354667Smh27603  * We dispatch a taskq to do that job.
9364667Smh27603  */
9374667Smh27603 static void
9388906SEric.Saxe@Sun.COM cpudrv_monitor_disp(void *arg)
9394667Smh27603 {
9404667Smh27603 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
9414667Smh27603 
9424667Smh27603 	/*
9434667Smh27603 	 * We are here because the last task has scheduled a timeout.
9444667Smh27603 	 * The queue should be empty at this time.
9454667Smh27603 	 */
9464667Smh27603 	mutex_enter(&cpudsp->cpudrv_pm.timeout_lock);
947*10488SMark.Haywood@Sun.COM 	if ((ddi_taskq_dispatch(cpudsp->cpudrv_pm.tq, cpudrv_monitor, arg,
948*10488SMark.Haywood@Sun.COM 	    DDI_NOSLEEP)) != DDI_SUCCESS) {
9494667Smh27603 		mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
9508906SEric.Saxe@Sun.COM 		DPRINTF(D_PM_MONITOR, ("cpudrv_monitor_disp: failed to "
9518906SEric.Saxe@Sun.COM 		    "dispatch the cpudrv_monitor taskq\n"));
9524667Smh27603 		mutex_enter(&cpudsp->lock);
9538906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_INIT(cpudsp);
9544667Smh27603 		mutex_exit(&cpudsp->lock);
9554667Smh27603 		return;
9564667Smh27603 	}
9574667Smh27603 	cpudsp->cpudrv_pm.timeout_count++;
9584667Smh27603 	mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
9594667Smh27603 }
9604667Smh27603 
9614667Smh27603 /*
9624667Smh27603  * Monitors each CPU for the amount of time idle thread was running in the
9634667Smh27603  * last quantum and arranges for the CPU to go to the lower or higher speed.
9644667Smh27603  * Called at the time interval appropriate for the current speed. The
9658906SEric.Saxe@Sun.COM  * time interval for normal speed is CPUDRV_QUANT_CNT_NORMAL. The time
9664667Smh27603  * interval for other speeds (including unknown speed) is
9678906SEric.Saxe@Sun.COM  * CPUDRV_QUANT_CNT_OTHR.
9684667Smh27603  */
9694667Smh27603 static void
9708906SEric.Saxe@Sun.COM cpudrv_monitor(void *arg)
9714667Smh27603 {
9724667Smh27603 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
9734667Smh27603 	cpudrv_pm_t		*cpupm;
9744667Smh27603 	cpudrv_pm_spd_t		*cur_spd, *new_spd;
9754667Smh27603 	dev_info_t		*dip;
9764667Smh27603 	uint_t			idle_cnt, user_cnt, system_cnt;
9778627SMark.Haywood@Sun.COM 	clock_t			ticks;
9788627SMark.Haywood@Sun.COM 	uint_t			tick_cnt;
9794667Smh27603 	hrtime_t		msnsecs[NCMSTATES];
9804667Smh27603 	boolean_t		is_ready;
9814667Smh27603 
9824667Smh27603 #define	GET_CPU_MSTATE_CNT(state, cnt) \
9834667Smh27603 	msnsecs[state] = NSEC_TO_TICK(msnsecs[state]); \
9844667Smh27603 	if (cpupm->lastquan_mstate[state] > msnsecs[state]) \
9854667Smh27603 		msnsecs[state] = cpupm->lastquan_mstate[state]; \
9864667Smh27603 	cnt = msnsecs[state] - cpupm->lastquan_mstate[state]; \
9874667Smh27603 	cpupm->lastquan_mstate[state] = msnsecs[state]
9884667Smh27603 
9894667Smh27603 	mutex_enter(&cpudsp->lock);
9904667Smh27603 	cpupm = &(cpudsp->cpudrv_pm);
9914667Smh27603 	if (cpupm->timeout_id == 0) {
9924667Smh27603 		mutex_exit(&cpudsp->lock);
9934667Smh27603 		goto do_return;
9944667Smh27603 	}
9954667Smh27603 	cur_spd = cpupm->cur_spd;
9964667Smh27603 	dip = cpudsp->dip;
9974667Smh27603 
9984667Smh27603 	/*
9994667Smh27603 	 * We assume that a CPU is initialized and has a valid cpu_t
10004667Smh27603 	 * structure, if it is ready for cross calls. If this changes,
10014667Smh27603 	 * additional checks might be needed.
10024667Smh27603 	 *
1003*10488SMark.Haywood@Sun.COM 	 * Additionally, for x86 platforms we cannot power manage an
1004*10488SMark.Haywood@Sun.COM 	 * instance, until it has been initialized.
10054667Smh27603 	 */
1006*10488SMark.Haywood@Sun.COM 	ASSERT(cpudsp->cp);
10078906SEric.Saxe@Sun.COM 	is_ready = CPUDRV_XCALL_IS_READY(cpudsp->cpu_id);
10084667Smh27603 	if (!is_ready) {
10098906SEric.Saxe@Sun.COM 		DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: instance %d: "
10104667Smh27603 		    "CPU not ready for x-calls\n", ddi_get_instance(dip)));
1011*10488SMark.Haywood@Sun.COM 	} else if (!(is_ready = cpudrv_power_ready(cpudsp->cp))) {
10128906SEric.Saxe@Sun.COM 		DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: instance %d: "
10137319SMark.Haywood@Sun.COM 		    "waiting for all CPUs to be power manageable\n",
10144667Smh27603 		    ddi_get_instance(dip)));
10154667Smh27603 	}
10164667Smh27603 	if (!is_ready) {
10174667Smh27603 		/*
10184667Smh27603 		 * Make sure that we are busy so that framework doesn't
10194667Smh27603 		 * try to bring us down in this situation.
10204667Smh27603 		 */
10218906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_PM_BUSY_COMP(dip, cpupm);
10228906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_INIT(cpudsp);
10234667Smh27603 		mutex_exit(&cpudsp->lock);
10244667Smh27603 		goto do_return;
10254667Smh27603 	}
10264667Smh27603 
10274667Smh27603 	/*
10284667Smh27603 	 * Make sure that we are still not at unknown power level.
10294667Smh27603 	 */
10304667Smh27603 	if (cur_spd == NULL) {
10318906SEric.Saxe@Sun.COM 		DPRINTF(D_PM_MONITOR, ("cpudrv_monitor: instance %d: "
10324667Smh27603 		    "cur_spd is unknown\n", ddi_get_instance(dip)));
10338906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
10349766SMark.Haywood@Sun.COM 		    CPUDRV_TOPSPEED(cpupm));
10354667Smh27603 		/*
10364667Smh27603 		 * We just changed the speed. Wait till at least next
10374667Smh27603 		 * call to this routine before proceeding ahead.
10384667Smh27603 		 */
10398906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_INIT(cpudsp);
10404667Smh27603 		mutex_exit(&cpudsp->lock);
10414667Smh27603 		goto do_return;
10424667Smh27603 	}
10434667Smh27603 
10444667Smh27603 	mutex_enter(&cpu_lock);
10458906SEric.Saxe@Sun.COM 	if (cpudsp->cp == NULL &&
10468906SEric.Saxe@Sun.COM 	    (cpudsp->cp = cpu_get(cpudsp->cpu_id)) == NULL) {
10474667Smh27603 		mutex_exit(&cpu_lock);
10488906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_INIT(cpudsp);
10494667Smh27603 		mutex_exit(&cpudsp->lock);
10508906SEric.Saxe@Sun.COM 		cmn_err(CE_WARN, "cpudrv_monitor: instance %d: can't get "
10514667Smh27603 		    "cpu_t", ddi_get_instance(dip));
10524667Smh27603 		goto do_return;
10534667Smh27603 	}
10544877Smh27603 
10554877Smh27603 	if (!cpupm->pm_started) {
10564877Smh27603 		cpupm->pm_started = B_TRUE;
10578906SEric.Saxe@Sun.COM 		cpudrv_set_supp_freqs(cpudsp);
10584877Smh27603 	}
10594667Smh27603 
10608906SEric.Saxe@Sun.COM 	get_cpu_mstate(cpudsp->cp, msnsecs);
10614667Smh27603 	GET_CPU_MSTATE_CNT(CMS_IDLE, idle_cnt);
10624667Smh27603 	GET_CPU_MSTATE_CNT(CMS_USER, user_cnt);
10634667Smh27603 	GET_CPU_MSTATE_CNT(CMS_SYSTEM, system_cnt);
10644667Smh27603 
10654667Smh27603 	/*
10664667Smh27603 	 * We can't do anything when we have just switched to a state
10674667Smh27603 	 * because there is no valid timestamp.
10684667Smh27603 	 */
10698627SMark.Haywood@Sun.COM 	if (cpupm->lastquan_ticks == 0) {
10708627SMark.Haywood@Sun.COM 		cpupm->lastquan_ticks = NSEC_TO_TICK(gethrtime());
10714667Smh27603 		mutex_exit(&cpu_lock);
10728906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_INIT(cpudsp);
10734667Smh27603 		mutex_exit(&cpudsp->lock);
10744667Smh27603 		goto do_return;
10754667Smh27603 	}
10764667Smh27603 
10774667Smh27603 	/*
10784667Smh27603 	 * Various watermarks are based on this routine being called back
10794667Smh27603 	 * exactly at the requested period. This is not guaranteed
10804667Smh27603 	 * because this routine is called from a taskq that is dispatched
10814667Smh27603 	 * from a timeout routine.  Handle this by finding out how many
10828627SMark.Haywood@Sun.COM 	 * ticks have elapsed since the last call and adjusting
10834667Smh27603 	 * the idle_cnt based on the delay added to the requested period
10844667Smh27603 	 * by timeout and taskq.
10854667Smh27603 	 */
10868627SMark.Haywood@Sun.COM 	ticks = NSEC_TO_TICK(gethrtime());
10878627SMark.Haywood@Sun.COM 	tick_cnt = ticks - cpupm->lastquan_ticks;
10888627SMark.Haywood@Sun.COM 	ASSERT(tick_cnt != 0);
10898627SMark.Haywood@Sun.COM 	cpupm->lastquan_ticks = ticks;
10904667Smh27603 	mutex_exit(&cpu_lock);
10914667Smh27603 	/*
10924667Smh27603 	 * Time taken between recording the current counts and
10934667Smh27603 	 * arranging the next call of this routine is an error in our
10944667Smh27603 	 * calculation. We minimize the error by calling
10958906SEric.Saxe@Sun.COM 	 * CPUDRV_MONITOR_INIT() here instead of end of this routine.
10964667Smh27603 	 */
10978906SEric.Saxe@Sun.COM 	CPUDRV_MONITOR_INIT(cpudsp);
10988906SEric.Saxe@Sun.COM 	DPRINTF(D_PM_MONITOR_VERBOSE, ("cpudrv_monitor: instance %d: "
10994667Smh27603 	    "idle count %d, user count %d, system count %d, pm_level %d, "
11004667Smh27603 	    "pm_busycnt %d\n", ddi_get_instance(dip), idle_cnt, user_cnt,
11014667Smh27603 	    system_cnt, cur_spd->pm_level, cpupm->pm_busycnt));
11024667Smh27603 
11034667Smh27603 #ifdef	DEBUG
11044667Smh27603 	/*
11054667Smh27603 	 * Notify that timeout and taskq has caused delays and we need to
11064667Smh27603 	 * scale our parameters accordingly.
11074667Smh27603 	 *
11084667Smh27603 	 * To get accurate result, don't turn on other DPRINTFs with
11094667Smh27603 	 * the following DPRINTF. PROM calls generated by other
11104667Smh27603 	 * DPRINTFs changes the timing.
11114667Smh27603 	 */
11128627SMark.Haywood@Sun.COM 	if (tick_cnt > cur_spd->quant_cnt) {
11138906SEric.Saxe@Sun.COM 		DPRINTF(D_PM_MONITOR_DELAY, ("cpudrv_monitor: instance %d: "
11148627SMark.Haywood@Sun.COM 		    "tick count %d > quantum_count %u\n",
11158627SMark.Haywood@Sun.COM 		    ddi_get_instance(dip), tick_cnt, cur_spd->quant_cnt));
11164667Smh27603 	}
11174667Smh27603 #endif	/* DEBUG */
11184667Smh27603 
11194667Smh27603 	/*
11204667Smh27603 	 * Adjust counts based on the delay added by timeout and taskq.
11214667Smh27603 	 */
11228627SMark.Haywood@Sun.COM 	idle_cnt = (idle_cnt * cur_spd->quant_cnt) / tick_cnt;
11238627SMark.Haywood@Sun.COM 	user_cnt = (user_cnt * cur_spd->quant_cnt) / tick_cnt;
11248627SMark.Haywood@Sun.COM 
11254667Smh27603 	if ((user_cnt > cur_spd->user_hwm) || (idle_cnt < cur_spd->idle_lwm &&
11268906SEric.Saxe@Sun.COM 	    cur_spd->idle_blwm_cnt >= cpudrv_idle_blwm_cnt_max)) {
11274667Smh27603 		cur_spd->idle_blwm_cnt = 0;
11284667Smh27603 		cur_spd->idle_bhwm_cnt = 0;
11294667Smh27603 		/*
11304667Smh27603 		 * In normal situation, arrange to go to next higher speed.
11314667Smh27603 		 * If we are running in special direct pm mode, we just stay
11324667Smh27603 		 * at the current speed.
11334667Smh27603 		 */
11344667Smh27603 		if (cur_spd == cur_spd->up_spd || cpudrv_direct_pm) {
11358906SEric.Saxe@Sun.COM 			CPUDRV_MONITOR_PM_BUSY_COMP(dip, cpupm);
11364667Smh27603 		} else {
11374667Smh27603 			new_spd = cur_spd->up_spd;
11388906SEric.Saxe@Sun.COM 			CPUDRV_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
11399766SMark.Haywood@Sun.COM 			    new_spd);
11404667Smh27603 		}
11414667Smh27603 	} else if ((user_cnt <= cur_spd->user_lwm) &&
11428906SEric.Saxe@Sun.COM 	    (idle_cnt >= cur_spd->idle_hwm) || !CPU_ACTIVE(cpudsp->cp)) {
11434667Smh27603 		cur_spd->idle_blwm_cnt = 0;
11444667Smh27603 		cur_spd->idle_bhwm_cnt = 0;
11454667Smh27603 		/*
11464667Smh27603 		 * Arrange to go to next lower speed by informing our idle
11474667Smh27603 		 * status to the power management framework.
11484667Smh27603 		 */
11498906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_PM_IDLE_COMP(dip, cpupm);
11504667Smh27603 	} else {
11514667Smh27603 		/*
11524667Smh27603 		 * If we are between the idle water marks and have not
11534667Smh27603 		 * been here enough consecutive times to be considered
11544667Smh27603 		 * busy, just increment the count and return.
11554667Smh27603 		 */
11564667Smh27603 		if ((idle_cnt < cur_spd->idle_hwm) &&
11574667Smh27603 		    (idle_cnt >= cur_spd->idle_lwm) &&
11588906SEric.Saxe@Sun.COM 		    (cur_spd->idle_bhwm_cnt < cpudrv_idle_bhwm_cnt_max)) {
11594667Smh27603 			cur_spd->idle_blwm_cnt = 0;
11604667Smh27603 			cur_spd->idle_bhwm_cnt++;
11614667Smh27603 			mutex_exit(&cpudsp->lock);
11624667Smh27603 			goto do_return;
11634667Smh27603 		}
11644667Smh27603 		if (idle_cnt < cur_spd->idle_lwm) {
11654667Smh27603 			cur_spd->idle_blwm_cnt++;
11664667Smh27603 			cur_spd->idle_bhwm_cnt = 0;
11674667Smh27603 		}
11684667Smh27603 		/*
11694667Smh27603 		 * Arranges to stay at the current speed.
11704667Smh27603 		 */
11718906SEric.Saxe@Sun.COM 		CPUDRV_MONITOR_PM_BUSY_COMP(dip, cpupm);
11724667Smh27603 	}
11734667Smh27603 	mutex_exit(&cpudsp->lock);
11744667Smh27603 do_return:
11754667Smh27603 	mutex_enter(&cpupm->timeout_lock);
11764667Smh27603 	ASSERT(cpupm->timeout_count > 0);
11774667Smh27603 	cpupm->timeout_count--;
11784667Smh27603 	cv_signal(&cpupm->timeout_cv);
11794667Smh27603 	mutex_exit(&cpupm->timeout_lock);
11804667Smh27603 }
1181