xref: /titanic_44/usr/src/uts/common/io/cpudrv.c (revision 68afbec1fabe0d352bb5ab4ed82c44b58ec651fb)
15cff7825Smh27603 /*
25cff7825Smh27603  * CDDL HEADER START
35cff7825Smh27603  *
45cff7825Smh27603  * The contents of this file are subject to the terms of the
55cff7825Smh27603  * Common Development and Distribution License (the "License").
65cff7825Smh27603  * You may not use this file except in compliance with the License.
75cff7825Smh27603  *
85cff7825Smh27603  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95cff7825Smh27603  * or http://www.opensolaris.org/os/licensing.
105cff7825Smh27603  * See the License for the specific language governing permissions
115cff7825Smh27603  * and limitations under the License.
125cff7825Smh27603  *
135cff7825Smh27603  * When distributing Covered Code, include this CDDL HEADER in each
145cff7825Smh27603  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155cff7825Smh27603  * If applicable, add the following below this CDDL HEADER, with the
165cff7825Smh27603  * fields enclosed by brackets "[]" replaced with your own identifying
175cff7825Smh27603  * information: Portions Copyright [yyyy] [name of copyright owner]
185cff7825Smh27603  *
195cff7825Smh27603  * CDDL HEADER END
205cff7825Smh27603  */
215cff7825Smh27603 /*
225cff7825Smh27603  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
235cff7825Smh27603  * Use is subject to license terms.
245cff7825Smh27603  */
255cff7825Smh27603 
265cff7825Smh27603 #pragma ident	"%Z%%M%	%I%	%E% SMI"
275cff7825Smh27603 
285cff7825Smh27603 /*
295cff7825Smh27603  * CPU Device driver. The driver is not DDI-compliant.
305cff7825Smh27603  *
315cff7825Smh27603  * The driver supports following features:
325cff7825Smh27603  *	- Power management.
335cff7825Smh27603  */
345cff7825Smh27603 
355cff7825Smh27603 #include <sys/types.h>
365cff7825Smh27603 #include <sys/param.h>
375cff7825Smh27603 #include <sys/errno.h>
385cff7825Smh27603 #include <sys/modctl.h>
395cff7825Smh27603 #include <sys/kmem.h>
405cff7825Smh27603 #include <sys/conf.h>
415cff7825Smh27603 #include <sys/cmn_err.h>
425cff7825Smh27603 #include <sys/stat.h>
435cff7825Smh27603 #include <sys/debug.h>
445cff7825Smh27603 #include <sys/systm.h>
455cff7825Smh27603 #include <sys/ddi.h>
465cff7825Smh27603 #include <sys/sunddi.h>
475cff7825Smh27603 
485cff7825Smh27603 #include <sys/machsystm.h>
495cff7825Smh27603 #include <sys/x_call.h>
505cff7825Smh27603 #include <sys/cpudrv.h>
515cff7825Smh27603 #include <sys/cpudrv_plat.h>
525cff7825Smh27603 #include <sys/msacct.h>
535cff7825Smh27603 
545cff7825Smh27603 /*
555cff7825Smh27603  * CPU power management
565cff7825Smh27603  *
575cff7825Smh27603  * The supported power saving model is to slow down the CPU (on SPARC by
585cff7825Smh27603  * dividing the CPU clock and on x86 by dropping down a P-state).
595cff7825Smh27603  * Periodically we determine the amount of time the CPU is running
605cff7825Smh27603  * idle thread and threads in user mode during the last quantum.  If the idle
615cff7825Smh27603  * thread was running less than its low water mark for current speed for
625cff7825Smh27603  * number of consecutive sampling periods, or number of running threads in
635cff7825Smh27603  * user mode are above its high water mark, we arrange to go to the higher
645cff7825Smh27603  * speed.  If the idle thread was running more than its high water mark without
655cff7825Smh27603  * dropping a number of consecutive times below the mark, and number of threads
665cff7825Smh27603  * running in user mode are below its low water mark, we arrange to go to the
675cff7825Smh27603  * next lower speed.  While going down, we go through all the speeds.  While
685cff7825Smh27603  * going up we go to the maximum speed to minimize impact on the user, but have
695cff7825Smh27603  * provisions in the driver to go to other speeds.
705cff7825Smh27603  *
715cff7825Smh27603  * The driver does not have knowledge of a particular implementation of this
725cff7825Smh27603  * scheme and will work with all CPUs supporting this model. On SPARC, the
735cff7825Smh27603  * driver determines supported speeds by looking at 'clock-divisors' property
745cff7825Smh27603  * created by OBP. On x86, the driver retrieves the supported speeds from
755cff7825Smh27603  * ACPI.
765cff7825Smh27603  */
775cff7825Smh27603 
785cff7825Smh27603 /*
795cff7825Smh27603  * Configuration function prototypes and data structures
805cff7825Smh27603  */
815cff7825Smh27603 static int cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
825cff7825Smh27603 static int cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
835cff7825Smh27603 static int cpudrv_power(dev_info_t *dip, int comp, int level);
845cff7825Smh27603 
855cff7825Smh27603 struct dev_ops cpudrv_ops = {
865cff7825Smh27603 	DEVO_REV,		/* rev */
875cff7825Smh27603 	0,			/* refcnt */
885cff7825Smh27603 	nodev,			/* getinfo */
895cff7825Smh27603 	nulldev,		/* identify */
905cff7825Smh27603 	nulldev,		/* probe */
915cff7825Smh27603 	cpudrv_attach,		/* attach */
925cff7825Smh27603 	cpudrv_detach,		/* detach */
935cff7825Smh27603 	nodev,			/* reset */
945cff7825Smh27603 	(struct cb_ops *)NULL,	/* cb_ops */
955cff7825Smh27603 	(struct bus_ops *)NULL,	/* bus_ops */
965cff7825Smh27603 	cpudrv_power		/* power */
975cff7825Smh27603 };
985cff7825Smh27603 
995cff7825Smh27603 static struct modldrv modldrv = {
1005cff7825Smh27603 	&mod_driverops,			/* modops */
1015cff7825Smh27603 	"CPU Driver %I%",		/* linkinfo */
1025cff7825Smh27603 	&cpudrv_ops,			/* dev_ops */
1035cff7825Smh27603 };
1045cff7825Smh27603 
1055cff7825Smh27603 static struct modlinkage modlinkage = {
1065cff7825Smh27603 	MODREV_1,		/* rev */
1075cff7825Smh27603 	&modldrv,		/* linkage */
1085cff7825Smh27603 	NULL
1095cff7825Smh27603 };
1105cff7825Smh27603 
1115cff7825Smh27603 /*
1125cff7825Smh27603  * Function prototypes
1135cff7825Smh27603  */
1145cff7825Smh27603 static int cpudrv_pm_init(cpudrv_devstate_t *cpudsp);
1155cff7825Smh27603 static void cpudrv_pm_free(cpudrv_devstate_t *cpudsp);
1165cff7825Smh27603 static int cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp);
1175cff7825Smh27603 static void cpudrv_pm_monitor_disp(void *arg);
1185cff7825Smh27603 static void cpudrv_pm_monitor(void *arg);
1195cff7825Smh27603 
1205cff7825Smh27603 /*
1215cff7825Smh27603  * Driver global variables
1225cff7825Smh27603  */
1235cff7825Smh27603 uint_t cpudrv_debug = 0;
1245cff7825Smh27603 void *cpudrv_state;
1255cff7825Smh27603 static uint_t cpudrv_pm_idle_hwm = CPUDRV_PM_IDLE_HWM;
1265cff7825Smh27603 static uint_t cpudrv_pm_idle_lwm = CPUDRV_PM_IDLE_LWM;
1275cff7825Smh27603 static uint_t cpudrv_pm_idle_buf_zone = CPUDRV_PM_IDLE_BUF_ZONE;
1285cff7825Smh27603 static uint_t cpudrv_pm_idle_bhwm_cnt_max = CPUDRV_PM_IDLE_BHWM_CNT_MAX;
1295cff7825Smh27603 static uint_t cpudrv_pm_idle_blwm_cnt_max = CPUDRV_PM_IDLE_BLWM_CNT_MAX;
1305cff7825Smh27603 static uint_t cpudrv_pm_user_hwm = CPUDRV_PM_USER_HWM;
1315cff7825Smh27603 
1325cff7825Smh27603 /*
1335cff7825Smh27603  * cpudrv_direct_pm allows user applications to directly control the
1345cff7825Smh27603  * power state transitions (direct pm) without following the normal
1355cff7825Smh27603  * direct pm protocol. This is needed because the normal protocol
1365cff7825Smh27603  * requires that a device only be lowered when it is idle, and be
1375cff7825Smh27603  * brought up when it request to do so by calling pm_raise_power().
1385cff7825Smh27603  * Ignoring this protocol is harmless for CPU (other than speed).
1395cff7825Smh27603  * Moreover it might be the case that CPU is never idle or wants
1405cff7825Smh27603  * to be at higher speed because of the addition CPU cycles required
1415cff7825Smh27603  * to run the user application.
1425cff7825Smh27603  *
1435cff7825Smh27603  * The driver will still report idle/busy status to the framework. Although
1445cff7825Smh27603  * framework will ignore this information for direct pm devices and not
1455cff7825Smh27603  * try to bring them down when idle, user applications can still use this
1465cff7825Smh27603  * information if they wants.
1475cff7825Smh27603  *
1485cff7825Smh27603  * In the future, provide an ioctl to control setting of this mode. In
1495cff7825Smh27603  * that case, this variable should move to the state structure and
1505cff7825Smh27603  * be protected by the lock in the state structure.
1515cff7825Smh27603  */
1525cff7825Smh27603 int cpudrv_direct_pm = 0;
1535cff7825Smh27603 
1545cff7825Smh27603 /*
1555cff7825Smh27603  * Arranges for the handler function to be called at the interval suitable
1565cff7825Smh27603  * for current speed.
1575cff7825Smh27603  */
1585cff7825Smh27603 #define	CPUDRV_PM_MONITOR_INIT(cpudsp) { \
1595cff7825Smh27603 	ASSERT(mutex_owned(&(cpudsp)->lock)); \
1605cff7825Smh27603 	(cpudsp)->cpudrv_pm.timeout_id = timeout(cpudrv_pm_monitor_disp, \
1615cff7825Smh27603 	    (cpudsp), (((cpudsp)->cpudrv_pm.cur_spd == NULL) ? \
1625cff7825Smh27603 	    CPUDRV_PM_QUANT_CNT_OTHR : \
1635cff7825Smh27603 	    (cpudsp)->cpudrv_pm.cur_spd->quant_cnt)); \
1645cff7825Smh27603 }
1655cff7825Smh27603 
1665cff7825Smh27603 /*
1675cff7825Smh27603  * Arranges for the handler function not to be called back.
1685cff7825Smh27603  */
1695cff7825Smh27603 #define	CPUDRV_PM_MONITOR_FINI(cpudsp) { \
1705cff7825Smh27603 	timeout_id_t tmp_tid; \
1715cff7825Smh27603 	ASSERT(mutex_owned(&(cpudsp)->lock)); \
1725cff7825Smh27603 	ASSERT((cpudsp)->cpudrv_pm.timeout_id); \
1735cff7825Smh27603 	tmp_tid = (cpudsp)->cpudrv_pm.timeout_id; \
1745cff7825Smh27603 	(cpudsp)->cpudrv_pm.timeout_id = 0; \
1755cff7825Smh27603 	mutex_exit(&(cpudsp)->lock); \
1765cff7825Smh27603 	(void) untimeout(tmp_tid); \
1775cff7825Smh27603 	mutex_enter(&(cpudsp)->cpudrv_pm.timeout_lock); \
1785cff7825Smh27603 	while ((cpudsp)->cpudrv_pm.timeout_count != 0) \
1795cff7825Smh27603 		cv_wait(&(cpudsp)->cpudrv_pm.timeout_cv, \
1805cff7825Smh27603 		    &(cpudsp)->cpudrv_pm.timeout_lock); \
1815cff7825Smh27603 	mutex_exit(&(cpudsp)->cpudrv_pm.timeout_lock); \
1825cff7825Smh27603 	mutex_enter(&(cpudsp)->lock); \
1835cff7825Smh27603 }
1845cff7825Smh27603 
1855cff7825Smh27603 int
1865cff7825Smh27603 _init(void)
1875cff7825Smh27603 {
1885cff7825Smh27603 	int	error;
1895cff7825Smh27603 
1905cff7825Smh27603 	DPRINTF(D_INIT, (" _init: function called\n"));
1915cff7825Smh27603 	if ((error = ddi_soft_state_init(&cpudrv_state,
1925cff7825Smh27603 	    sizeof (cpudrv_devstate_t), 0)) != 0) {
1935cff7825Smh27603 		return (error);
1945cff7825Smh27603 	}
1955cff7825Smh27603 
1965cff7825Smh27603 	if ((error = mod_install(&modlinkage)) != 0)  {
1975cff7825Smh27603 		ddi_soft_state_fini(&cpudrv_state);
1985cff7825Smh27603 	}
1995cff7825Smh27603 
2005cff7825Smh27603 	/*
2015cff7825Smh27603 	 * Callbacks used by the PPM driver.
2025cff7825Smh27603 	 */
2035cff7825Smh27603 	CPUDRV_PM_SET_PPM_CALLBACKS();
2045cff7825Smh27603 	return (error);
2055cff7825Smh27603 }
2065cff7825Smh27603 
2075cff7825Smh27603 int
2085cff7825Smh27603 _fini(void)
2095cff7825Smh27603 {
2105cff7825Smh27603 	int	error;
2115cff7825Smh27603 
2125cff7825Smh27603 	DPRINTF(D_FINI, (" _fini: function called\n"));
2135cff7825Smh27603 	if ((error = mod_remove(&modlinkage)) == 0) {
2145cff7825Smh27603 		ddi_soft_state_fini(&cpudrv_state);
2155cff7825Smh27603 	}
2165cff7825Smh27603 
2175cff7825Smh27603 	return (error);
2185cff7825Smh27603 }
2195cff7825Smh27603 
2205cff7825Smh27603 int
2215cff7825Smh27603 _info(struct modinfo *modinfop)
2225cff7825Smh27603 {
2235cff7825Smh27603 	return (mod_info(&modlinkage, modinfop));
2245cff7825Smh27603 }
2255cff7825Smh27603 
2265cff7825Smh27603 /*
2275cff7825Smh27603  * Driver attach(9e) entry point.
2285cff7825Smh27603  */
2295cff7825Smh27603 static int
2305cff7825Smh27603 cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2315cff7825Smh27603 {
2325cff7825Smh27603 	int			instance;
2335cff7825Smh27603 	cpudrv_devstate_t	*cpudsp;
2345cff7825Smh27603 	extern pri_t		maxclsyspri;
2355cff7825Smh27603 
2365cff7825Smh27603 	instance = ddi_get_instance(dip);
2375cff7825Smh27603 
2385cff7825Smh27603 	switch (cmd) {
2395cff7825Smh27603 	case DDI_ATTACH:
2405cff7825Smh27603 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
2415cff7825Smh27603 		    "DDI_ATTACH called\n", instance));
2425cff7825Smh27603 		if (ddi_soft_state_zalloc(cpudrv_state, instance) !=
2435cff7825Smh27603 		    DDI_SUCCESS) {
2445cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2455cff7825Smh27603 			    "can't allocate state", instance);
2465cff7825Smh27603 			CPUDRV_PM_DISABLE();
2475cff7825Smh27603 			return (DDI_FAILURE);
2485cff7825Smh27603 		}
2495cff7825Smh27603 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
2505cff7825Smh27603 		    NULL) {
2515cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2525cff7825Smh27603 			    "can't get state", instance);
2535cff7825Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2545cff7825Smh27603 			CPUDRV_PM_DISABLE();
2555cff7825Smh27603 			return (DDI_FAILURE);
2565cff7825Smh27603 		}
2575cff7825Smh27603 		cpudsp->dip = dip;
2585cff7825Smh27603 
2595cff7825Smh27603 		/*
2605cff7825Smh27603 		 * Find CPU number for this dev_info node.
2615cff7825Smh27603 		 */
2625cff7825Smh27603 		if (!cpudrv_pm_get_cpu_id(dip, &(cpudsp->cpu_id))) {
2635cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2645cff7825Smh27603 			    "can't convert dip to cpu_id", instance);
2655cff7825Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2665cff7825Smh27603 			CPUDRV_PM_DISABLE();
2675cff7825Smh27603 			return (DDI_FAILURE);
2685cff7825Smh27603 		}
2695cff7825Smh27603 		if (cpudrv_pm_init(cpudsp) != DDI_SUCCESS) {
2705cff7825Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2715cff7825Smh27603 			CPUDRV_PM_DISABLE();
2725cff7825Smh27603 			return (DDI_FAILURE);
2735cff7825Smh27603 		}
2745cff7825Smh27603 		if (cpudrv_pm_comp_create(cpudsp) != DDI_SUCCESS) {
2755cff7825Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2765cff7825Smh27603 			CPUDRV_PM_DISABLE();
2775cff7825Smh27603 			cpudrv_pm_free(cpudsp);
2785cff7825Smh27603 			return (DDI_FAILURE);
2795cff7825Smh27603 		}
2805cff7825Smh27603 		if (ddi_prop_update_string(DDI_DEV_T_NONE,
2815cff7825Smh27603 		    dip, "pm-class", "CPU") != DDI_PROP_SUCCESS) {
2825cff7825Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2835cff7825Smh27603 			CPUDRV_PM_DISABLE();
2845cff7825Smh27603 			cpudrv_pm_free(cpudsp);
2855cff7825Smh27603 			return (DDI_FAILURE);
2865cff7825Smh27603 		}
2875cff7825Smh27603 
2885cff7825Smh27603 		/*
2895cff7825Smh27603 		 * Taskq is used to dispatch routine to monitor CPU activities.
2905cff7825Smh27603 		 */
2915cff7825Smh27603 		cpudsp->cpudrv_pm.tq = taskq_create_instance(
2925cff7825Smh27603 		    "cpudrv_pm_monitor",
2935cff7825Smh27603 		    ddi_get_instance(dip), CPUDRV_PM_TASKQ_THREADS,
2945cff7825Smh27603 		    (maxclsyspri - 1), CPUDRV_PM_TASKQ_MIN,
2955cff7825Smh27603 		    CPUDRV_PM_TASKQ_MAX, TASKQ_PREPOPULATE|TASKQ_CPR_SAFE);
2965cff7825Smh27603 
2975cff7825Smh27603 		mutex_init(&cpudsp->lock, NULL, MUTEX_DRIVER, NULL);
2985cff7825Smh27603 		mutex_init(&cpudsp->cpudrv_pm.timeout_lock, NULL, MUTEX_DRIVER,
2995cff7825Smh27603 		    NULL);
3005cff7825Smh27603 		cv_init(&cpudsp->cpudrv_pm.timeout_cv, NULL, CV_DEFAULT, NULL);
3015cff7825Smh27603 
3025cff7825Smh27603 		/*
3035cff7825Smh27603 		 * Driver needs to assume that CPU is running at unknown speed
3045cff7825Smh27603 		 * at DDI_ATTACH and switch it to the needed speed. We assume
3055cff7825Smh27603 		 * that initial needed speed is full speed for us.
3065cff7825Smh27603 		 */
3075cff7825Smh27603 		/*
3085cff7825Smh27603 		 * We need to take the lock because cpudrv_pm_monitor()
3095cff7825Smh27603 		 * will start running in parallel with attach().
3105cff7825Smh27603 		 */
3115cff7825Smh27603 		mutex_enter(&cpudsp->lock);
3125cff7825Smh27603 		cpudsp->cpudrv_pm.cur_spd = NULL;
3135cff7825Smh27603 		cpudsp->cpudrv_pm.targ_spd = cpudsp->cpudrv_pm.head_spd;
314*68afbec1Smh27603 		cpudsp->cpudrv_pm.pm_started = B_FALSE;
3155cff7825Smh27603 		/*
3165cff7825Smh27603 		 * We don't call pm_raise_power() directly from attach because
3175cff7825Smh27603 		 * driver attach for a slave CPU node can happen before the
3185cff7825Smh27603 		 * CPU is even initialized. We just start the monitoring
3195cff7825Smh27603 		 * system which understands unknown speed and moves CPU
3205cff7825Smh27603 		 * to targ_spd when it have been initialized.
3215cff7825Smh27603 		 */
3225cff7825Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
3235cff7825Smh27603 		mutex_exit(&cpudsp->lock);
3245cff7825Smh27603 
3255cff7825Smh27603 		CPUDRV_PM_INSTALL_TOPSPEED_CHANGE_HANDLER(cpudsp, dip);
3265cff7825Smh27603 
3275cff7825Smh27603 		ddi_report_dev(dip);
3285cff7825Smh27603 		return (DDI_SUCCESS);
3295cff7825Smh27603 
3305cff7825Smh27603 	case DDI_RESUME:
3315cff7825Smh27603 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
3325cff7825Smh27603 		    "DDI_RESUME called\n", instance));
3335cff7825Smh27603 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
3345cff7825Smh27603 		    NULL) {
3355cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
3365cff7825Smh27603 			    "can't get state", instance);
3375cff7825Smh27603 			return (DDI_FAILURE);
3385cff7825Smh27603 		}
3395cff7825Smh27603 		mutex_enter(&cpudsp->lock);
3405cff7825Smh27603 		/*
3415cff7825Smh27603 		 * Driver needs to assume that CPU is running at unknown speed
3425cff7825Smh27603 		 * at DDI_RESUME and switch it to the needed speed. We assume
3435cff7825Smh27603 		 * that the needed speed is full speed for us.
3445cff7825Smh27603 		 */
3455cff7825Smh27603 		cpudsp->cpudrv_pm.cur_spd = NULL;
3465cff7825Smh27603 		cpudsp->cpudrv_pm.targ_spd = cpudsp->cpudrv_pm.head_spd;
3475cff7825Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
3485cff7825Smh27603 		mutex_exit(&cpudsp->lock);
3495cff7825Smh27603 		CPUDRV_PM_REDEFINE_TOPSPEED(dip);
3505cff7825Smh27603 		return (DDI_SUCCESS);
3515cff7825Smh27603 
3525cff7825Smh27603 	default:
3535cff7825Smh27603 		return (DDI_FAILURE);
3545cff7825Smh27603 	}
3555cff7825Smh27603 }
3565cff7825Smh27603 
3575cff7825Smh27603 /*
3585cff7825Smh27603  * Driver detach(9e) entry point.
3595cff7825Smh27603  */
3605cff7825Smh27603 static int
3615cff7825Smh27603 cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3625cff7825Smh27603 {
3635cff7825Smh27603 	int			instance;
3645cff7825Smh27603 	cpudrv_devstate_t	*cpudsp;
3655cff7825Smh27603 	cpudrv_pm_t		*cpupm;
3665cff7825Smh27603 
3675cff7825Smh27603 	instance = ddi_get_instance(dip);
3685cff7825Smh27603 
3695cff7825Smh27603 	switch (cmd) {
3705cff7825Smh27603 	case DDI_DETACH:
3715cff7825Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
3725cff7825Smh27603 		    "DDI_DETACH called\n", instance));
3735cff7825Smh27603 		/*
3745cff7825Smh27603 		 * If the only thing supported by the driver is power
3755cff7825Smh27603 		 * management, we can in future enhance the driver and
3765cff7825Smh27603 		 * framework that loads it to unload the driver when
3775cff7825Smh27603 		 * user has disabled CPU power management.
3785cff7825Smh27603 		 */
3795cff7825Smh27603 		return (DDI_FAILURE);
3805cff7825Smh27603 
3815cff7825Smh27603 	case DDI_SUSPEND:
3825cff7825Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
3835cff7825Smh27603 		    "DDI_SUSPEND called\n", instance));
3845cff7825Smh27603 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
3855cff7825Smh27603 		    NULL) {
3865cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
3875cff7825Smh27603 			    "can't get state", instance);
3885cff7825Smh27603 			return (DDI_FAILURE);
3895cff7825Smh27603 		}
3905cff7825Smh27603 		/*
3915cff7825Smh27603 		 * During a checkpoint-resume sequence, framework will
3925cff7825Smh27603 		 * stop interrupts to quiesce kernel activity. This will
3935cff7825Smh27603 		 * leave our monitoring system ineffective. Handle this
3945cff7825Smh27603 		 * by stopping our monitoring system and bringing CPU
3955cff7825Smh27603 		 * to full speed. In case we are in special direct pm
3965cff7825Smh27603 		 * mode, we leave the CPU at whatever speed it is. This
3975cff7825Smh27603 		 * is harmless other than speed.
3985cff7825Smh27603 		 */
3995cff7825Smh27603 		mutex_enter(&cpudsp->lock);
4005cff7825Smh27603 		cpupm = &(cpudsp->cpudrv_pm);
4015cff7825Smh27603 
4025cff7825Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: DDI_SUSPEND - "
4035cff7825Smh27603 		    "cur_spd %d, head_spd %d\n", instance,
4045cff7825Smh27603 		    cpupm->cur_spd->pm_level, cpupm->head_spd->pm_level));
4055cff7825Smh27603 
4065cff7825Smh27603 		CPUDRV_PM_MONITOR_FINI(cpudsp);
4075cff7825Smh27603 
4085cff7825Smh27603 		if (!cpudrv_direct_pm && (cpupm->cur_spd != cpupm->head_spd)) {
4095cff7825Smh27603 			if (cpupm->pm_busycnt < 1) {
4105cff7825Smh27603 				if ((pm_busy_component(dip, CPUDRV_PM_COMP_NUM)
4115cff7825Smh27603 				    == DDI_SUCCESS)) {
4125cff7825Smh27603 					cpupm->pm_busycnt++;
4135cff7825Smh27603 				} else {
4145cff7825Smh27603 					CPUDRV_PM_MONITOR_INIT(cpudsp);
4155cff7825Smh27603 					mutex_exit(&cpudsp->lock);
4165cff7825Smh27603 					cmn_err(CE_WARN, "cpudrv_detach: "
4175cff7825Smh27603 					    "instance %d: can't busy CPU "
4185cff7825Smh27603 					    "component", instance);
4195cff7825Smh27603 					return (DDI_FAILURE);
4205cff7825Smh27603 				}
4215cff7825Smh27603 			}
4225cff7825Smh27603 			mutex_exit(&cpudsp->lock);
4235cff7825Smh27603 			if (pm_raise_power(dip, CPUDRV_PM_COMP_NUM,
4245cff7825Smh27603 			    cpupm->head_spd->pm_level) != DDI_SUCCESS) {
4255cff7825Smh27603 				mutex_enter(&cpudsp->lock);
4265cff7825Smh27603 				CPUDRV_PM_MONITOR_INIT(cpudsp);
4275cff7825Smh27603 				mutex_exit(&cpudsp->lock);
4285cff7825Smh27603 				cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
4295cff7825Smh27603 				    "can't raise CPU power level", instance);
4305cff7825Smh27603 				return (DDI_FAILURE);
4315cff7825Smh27603 			} else {
4325cff7825Smh27603 				return (DDI_SUCCESS);
4335cff7825Smh27603 			}
4345cff7825Smh27603 		} else {
4355cff7825Smh27603 			mutex_exit(&cpudsp->lock);
4365cff7825Smh27603 			return (DDI_SUCCESS);
4375cff7825Smh27603 		}
4385cff7825Smh27603 
4395cff7825Smh27603 	default:
4405cff7825Smh27603 		return (DDI_FAILURE);
4415cff7825Smh27603 	}
4425cff7825Smh27603 }
4435cff7825Smh27603 
4445cff7825Smh27603 /*
4455cff7825Smh27603  * Driver power(9e) entry point.
4465cff7825Smh27603  *
4475cff7825Smh27603  * Driver's notion of current power is set *only* in power(9e) entry point
4485cff7825Smh27603  * after actual power change operation has been successfully completed.
4495cff7825Smh27603  */
4505cff7825Smh27603 /* ARGSUSED */
4515cff7825Smh27603 static int
4525cff7825Smh27603 cpudrv_power(dev_info_t *dip, int comp, int level)
4535cff7825Smh27603 {
4545cff7825Smh27603 	int			instance;
4555cff7825Smh27603 	cpudrv_devstate_t	*cpudsp;
4565cff7825Smh27603 	cpudrv_pm_t 		*cpupm;
4575cff7825Smh27603 	cpudrv_pm_spd_t		*new_spd;
4585cff7825Smh27603 	boolean_t		is_ready;
4595cff7825Smh27603 	int			ret;
4605cff7825Smh27603 
4615cff7825Smh27603 	instance = ddi_get_instance(dip);
4625cff7825Smh27603 
4635cff7825Smh27603 	DPRINTF(D_POWER, ("cpudrv_power: instance %d: level %d\n",
4645cff7825Smh27603 	    instance, level));
4655cff7825Smh27603 	if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) == NULL) {
4665cff7825Smh27603 		cmn_err(CE_WARN, "cpudrv_power: instance %d: can't get state",
4675cff7825Smh27603 		    instance);
4685cff7825Smh27603 		return (DDI_FAILURE);
4695cff7825Smh27603 	}
4705cff7825Smh27603 
4715cff7825Smh27603 	mutex_enter(&cpudsp->lock);
4725cff7825Smh27603 	cpupm = &(cpudsp->cpudrv_pm);
4735cff7825Smh27603 
4745cff7825Smh27603 	/*
4755cff7825Smh27603 	 * In normal operation, we fail if we are busy and request is
4765cff7825Smh27603 	 * to lower the power level. We let this go through if the driver
4775cff7825Smh27603 	 * is in special direct pm mode. On x86, we also let this through
4785cff7825Smh27603 	 * if the change is due to a request to throttle the max speed.
4795cff7825Smh27603 	 */
4805cff7825Smh27603 	if (!cpudrv_direct_pm && (cpupm->pm_busycnt >= 1) &&
4815cff7825Smh27603 	    !cpudrv_pm_is_throttle_thread(cpupm)) {
4825cff7825Smh27603 		if ((cpupm->cur_spd != NULL) &&
4835cff7825Smh27603 		    (level < cpupm->cur_spd->pm_level)) {
4845cff7825Smh27603 			mutex_exit(&cpudsp->lock);
4855cff7825Smh27603 			return (DDI_FAILURE);
4865cff7825Smh27603 		}
4875cff7825Smh27603 	}
4885cff7825Smh27603 
4895cff7825Smh27603 	for (new_spd = cpupm->head_spd; new_spd; new_spd = new_spd->down_spd) {
4905cff7825Smh27603 		if (new_spd->pm_level == level)
4915cff7825Smh27603 			break;
4925cff7825Smh27603 	}
4935cff7825Smh27603 	if (!new_spd) {
4945cff7825Smh27603 		CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
4955cff7825Smh27603 		mutex_exit(&cpudsp->lock);
4965cff7825Smh27603 		cmn_err(CE_WARN, "cpudrv_power: instance %d: "
4975cff7825Smh27603 		    "can't locate new CPU speed", instance);
4985cff7825Smh27603 		return (DDI_FAILURE);
4995cff7825Smh27603 	}
5005cff7825Smh27603 
5015cff7825Smh27603 	/*
5025cff7825Smh27603 	 * We currently refuse to power manage if the CPU is not ready to
5035cff7825Smh27603 	 * take cross calls (cross calls fail silently if CPU is not ready
5045cff7825Smh27603 	 * for it).
5055cff7825Smh27603 	 *
5065cff7825Smh27603 	 * Additionally, for x86 platforms we cannot power manage
5075cff7825Smh27603 	 * any one instance, until all instances have been initialized.
5085cff7825Smh27603 	 * That's because we don't know what the CPU domains look like
5095cff7825Smh27603 	 * until all instances have been initialized.
5105cff7825Smh27603 	 */
5115cff7825Smh27603 	is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
5125cff7825Smh27603 	if (!is_ready) {
5135cff7825Smh27603 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
5145cff7825Smh27603 		    "CPU not ready for x-calls\n", instance));
5155cff7825Smh27603 	} else if (!(is_ready = cpudrv_pm_all_instances_ready())) {
5165cff7825Smh27603 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
5175cff7825Smh27603 		    "waiting for all CPUs to be ready\n", instance));
5185cff7825Smh27603 	}
5195cff7825Smh27603 	if (!is_ready) {
5205cff7825Smh27603 		CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
5215cff7825Smh27603 		mutex_exit(&cpudsp->lock);
5225cff7825Smh27603 		return (DDI_FAILURE);
5235cff7825Smh27603 	}
5245cff7825Smh27603 
5255cff7825Smh27603 	/*
5265cff7825Smh27603 	 * Execute CPU specific routine on the requested CPU to change its
5275cff7825Smh27603 	 * speed to normal-speed/divisor.
5285cff7825Smh27603 	 */
5295cff7825Smh27603 	if ((ret = cpudrv_pm_change_speed(cpudsp, new_spd)) != DDI_SUCCESS) {
5305cff7825Smh27603 		cmn_err(CE_WARN, "cpudrv_power: cpudrv_pm_change_speed() "
5315cff7825Smh27603 		    "return = %d", ret);
5325cff7825Smh27603 		mutex_exit(&cpudsp->lock);
5335cff7825Smh27603 		return (DDI_FAILURE);
5345cff7825Smh27603 	}
5355cff7825Smh27603 
5365cff7825Smh27603 	/*
5375cff7825Smh27603 	 * Reset idle threshold time for the new power level.
5385cff7825Smh27603 	 */
5395cff7825Smh27603 	if ((cpupm->cur_spd != NULL) && (level < cpupm->cur_spd->pm_level)) {
5405cff7825Smh27603 		if (pm_idle_component(dip, CPUDRV_PM_COMP_NUM) ==
5415cff7825Smh27603 		    DDI_SUCCESS) {
5425cff7825Smh27603 			if (cpupm->pm_busycnt >= 1)
5435cff7825Smh27603 				cpupm->pm_busycnt--;
5445cff7825Smh27603 		} else
5455cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_power: instance %d: can't "
5465cff7825Smh27603 			    "idle CPU component", ddi_get_instance(dip));
5475cff7825Smh27603 	}
5485cff7825Smh27603 	/*
5495cff7825Smh27603 	 * Reset various parameters because we are now running at new speed.
5505cff7825Smh27603 	 */
5515cff7825Smh27603 	cpupm->lastquan_mstate[CMS_IDLE] = 0;
5525cff7825Smh27603 	cpupm->lastquan_mstate[CMS_SYSTEM] = 0;
5535cff7825Smh27603 	cpupm->lastquan_mstate[CMS_USER] = 0;
5545cff7825Smh27603 	cpupm->lastquan_lbolt = 0;
5555cff7825Smh27603 	cpupm->cur_spd = new_spd;
5565cff7825Smh27603 	CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
5575cff7825Smh27603 	mutex_exit(&cpudsp->lock);
5585cff7825Smh27603 
5595cff7825Smh27603 	return (DDI_SUCCESS);
5605cff7825Smh27603 }
5615cff7825Smh27603 
5625cff7825Smh27603 /*
5635cff7825Smh27603  * Initialize the field that will be used for reporting
5645cff7825Smh27603  * the supported_frequencies_Hz cpu_info kstat.
5655cff7825Smh27603  */
5665cff7825Smh27603 static void
5675cff7825Smh27603 set_supp_freqs(cpu_t *cp, cpudrv_pm_t *cpupm)
5685cff7825Smh27603 {
5695cff7825Smh27603 	char		*supp_freqs;
5705cff7825Smh27603 	char		*sfptr;
5715cff7825Smh27603 	uint64_t	*speeds;
5725cff7825Smh27603 	cpudrv_pm_spd_t	*spd;
5735cff7825Smh27603 	int		i;
5745cff7825Smh27603 #define	UINT64_MAX_STRING (sizeof ("18446744073709551615"))
5755cff7825Smh27603 
5765cff7825Smh27603 	speeds = kmem_zalloc(cpupm->num_spd * sizeof (uint64_t), KM_SLEEP);
5775cff7825Smh27603 	for (i = cpupm->num_spd - 1, spd = cpupm->head_spd; spd;
5785cff7825Smh27603 	    i--, spd = spd->down_spd) {
5795cff7825Smh27603 		speeds[i] =
5805cff7825Smh27603 		    CPUDRV_PM_SPEED_HZ(cp->cpu_type_info.pi_clock, spd->speed);
5815cff7825Smh27603 	}
5825cff7825Smh27603 
5835cff7825Smh27603 	supp_freqs = kmem_zalloc((UINT64_MAX_STRING * cpupm->num_spd),
5845cff7825Smh27603 	    KM_SLEEP);
5855cff7825Smh27603 	sfptr = supp_freqs;
5865cff7825Smh27603 	for (i = 0; i < cpupm->num_spd; i++) {
5875cff7825Smh27603 		if (i == cpupm->num_spd - 1) {
5885cff7825Smh27603 			(void) sprintf(sfptr, "%"PRIu64, speeds[i]);
5895cff7825Smh27603 		} else {
5905cff7825Smh27603 			(void) sprintf(sfptr, "%"PRIu64":", speeds[i]);
5915cff7825Smh27603 			sfptr = supp_freqs + strlen(supp_freqs);
5925cff7825Smh27603 		}
5935cff7825Smh27603 	}
594*68afbec1Smh27603 	cpu_set_supp_freqs(cp, supp_freqs);
595*68afbec1Smh27603 	kmem_free(supp_freqs, (UINT64_MAX_STRING * cpupm->num_spd));
5965cff7825Smh27603 	kmem_free(speeds, cpupm->num_spd * sizeof (uint64_t));
5975cff7825Smh27603 }
5985cff7825Smh27603 
5995cff7825Smh27603 /*
6005cff7825Smh27603  * Initialize power management data.
6015cff7825Smh27603  */
6025cff7825Smh27603 static int
6035cff7825Smh27603 cpudrv_pm_init(cpudrv_devstate_t *cpudsp)
6045cff7825Smh27603 {
6055cff7825Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
6065cff7825Smh27603 	cpudrv_pm_spd_t	*cur_spd;
6075cff7825Smh27603 	cpudrv_pm_spd_t	*prev_spd = NULL;
6085cff7825Smh27603 	int		*speeds;
6095cff7825Smh27603 	uint_t		nspeeds;
6105cff7825Smh27603 	int		idle_cnt_percent;
6115cff7825Smh27603 	int		user_cnt_percent;
6125cff7825Smh27603 	int		i;
6135cff7825Smh27603 
6145cff7825Smh27603 	if (!cpudrv_pm_init_module(cpudsp))
6155cff7825Smh27603 		return (DDI_FAILURE);
6165cff7825Smh27603 
6175cff7825Smh27603 	CPUDRV_PM_GET_SPEEDS(cpudsp, speeds, nspeeds);
6185cff7825Smh27603 	if (nspeeds < 2) {
6195cff7825Smh27603 		/* Need at least two speeds to power manage */
6205cff7825Smh27603 		CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
6215cff7825Smh27603 		cpudrv_pm_free_module(cpudsp);
6225cff7825Smh27603 		return (DDI_FAILURE);
6235cff7825Smh27603 	}
6245cff7825Smh27603 	cpupm->num_spd = nspeeds;
6255cff7825Smh27603 
6265cff7825Smh27603 	/*
6275cff7825Smh27603 	 * Calculate the watermarks and other parameters based on the
6285cff7825Smh27603 	 * supplied speeds.
6295cff7825Smh27603 	 *
6305cff7825Smh27603 	 * One of the basic assumption is that for X amount of CPU work,
6315cff7825Smh27603 	 * if CPU is slowed down by a factor of N, the time it takes to
6325cff7825Smh27603 	 * do the same work will be N * X.
6335cff7825Smh27603 	 *
6345cff7825Smh27603 	 * The driver declares that a CPU is idle and ready for slowed down,
6355cff7825Smh27603 	 * if amount of idle thread is more than the current speed idle_hwm
6365cff7825Smh27603 	 * without dropping below idle_hwm a number of consecutive sampling
6375cff7825Smh27603 	 * intervals and number of running threads in user mode are below
6385cff7825Smh27603 	 * user_lwm.  We want to set the current user_lwm such that if we
6395cff7825Smh27603 	 * just switched to the next slower speed with no change in real work
6405cff7825Smh27603 	 * load, the amount of user threads at the slower speed will be such
6415cff7825Smh27603 	 * that it falls below the slower speed's user_hwm.  If we didn't do
6425cff7825Smh27603 	 * that then we will just come back to the higher speed as soon as we
6435cff7825Smh27603 	 * go down even with no change in work load.
6445cff7825Smh27603 	 * The user_hwm is a fixed precentage and not calculated dynamically.
6455cff7825Smh27603 	 *
6465cff7825Smh27603 	 * We bring the CPU up if idle thread at current speed is less than
6475cff7825Smh27603 	 * the current speed idle_lwm for a number of consecutive sampling
6485cff7825Smh27603 	 * intervals or user threads are above the user_hwm for the current
6495cff7825Smh27603 	 * speed.
6505cff7825Smh27603 	 */
6515cff7825Smh27603 	for (i = 0; i < nspeeds; i++) {
6525cff7825Smh27603 		cur_spd = kmem_zalloc(sizeof (cpudrv_pm_spd_t), KM_SLEEP);
6535cff7825Smh27603 		cur_spd->speed = speeds[i];
6545cff7825Smh27603 		if (i == 0) {	/* normal speed */
6555cff7825Smh27603 			cpupm->head_spd = cur_spd;
6565cff7825Smh27603 			cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_NORMAL;
6575cff7825Smh27603 			cur_spd->idle_hwm =
6585cff7825Smh27603 			    (cpudrv_pm_idle_hwm * cur_spd->quant_cnt) / 100;
6595cff7825Smh27603 			/* can't speed anymore */
6605cff7825Smh27603 			cur_spd->idle_lwm = 0;
6615cff7825Smh27603 			cur_spd->user_hwm = UINT_MAX;
6625cff7825Smh27603 		} else {
6635cff7825Smh27603 			cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_OTHR;
6645cff7825Smh27603 			ASSERT(prev_spd != NULL);
6655cff7825Smh27603 			prev_spd->down_spd = cur_spd;
6665cff7825Smh27603 			cur_spd->up_spd = cpupm->head_spd;
6675cff7825Smh27603 
6685cff7825Smh27603 			/*
6695cff7825Smh27603 			 * Let's assume CPU is considered idle at full speed
6705cff7825Smh27603 			 * when it is spending I% of time in running the idle
6715cff7825Smh27603 			 * thread.  At full speed, CPU will be busy (100 - I) %
6725cff7825Smh27603 			 * of times.  This % of busyness increases by factor of
6735cff7825Smh27603 			 * N as CPU slows down.  CPU that is idle I% of times
6745cff7825Smh27603 			 * in full speed, it is idle (100 - ((100 - I) * N)) %
6755cff7825Smh27603 			 * of times in N speed.  The idle_lwm is a fixed
6765cff7825Smh27603 			 * percentage.  A large value of N may result in
6775cff7825Smh27603 			 * idle_hwm to go below idle_lwm.  We need to make sure
6785cff7825Smh27603 			 * that there is at least a buffer zone seperation
6795cff7825Smh27603 			 * between the idle_lwm and idle_hwm values.
6805cff7825Smh27603 			 */
6815cff7825Smh27603 			idle_cnt_percent = CPUDRV_PM_IDLE_CNT_PERCENT(
6825cff7825Smh27603 			    cpudrv_pm_idle_hwm, speeds, i);
6835cff7825Smh27603 			idle_cnt_percent = max(idle_cnt_percent,
6845cff7825Smh27603 			    (cpudrv_pm_idle_lwm + cpudrv_pm_idle_buf_zone));
6855cff7825Smh27603 			cur_spd->idle_hwm =
6865cff7825Smh27603 			    (idle_cnt_percent * cur_spd->quant_cnt) / 100;
6875cff7825Smh27603 			cur_spd->idle_lwm =
6885cff7825Smh27603 			    (cpudrv_pm_idle_lwm * cur_spd->quant_cnt) / 100;
6895cff7825Smh27603 
6905cff7825Smh27603 			/*
6915cff7825Smh27603 			 * The lwm for user threads are determined such that
6925cff7825Smh27603 			 * if CPU slows down, the load of work in the
6935cff7825Smh27603 			 * new speed would still keep the CPU at or below the
6945cff7825Smh27603 			 * user_hwm in the new speed.  This is to prevent
6955cff7825Smh27603 			 * the quick jump back up to higher speed.
6965cff7825Smh27603 			 */
6975cff7825Smh27603 			cur_spd->user_hwm = (cpudrv_pm_user_hwm *
6985cff7825Smh27603 			    cur_spd->quant_cnt) / 100;
6995cff7825Smh27603 			user_cnt_percent = CPUDRV_PM_USER_CNT_PERCENT(
7005cff7825Smh27603 			    cpudrv_pm_user_hwm, speeds, i);
7015cff7825Smh27603 			prev_spd->user_lwm =
7025cff7825Smh27603 			    (user_cnt_percent * prev_spd->quant_cnt) / 100;
7035cff7825Smh27603 		}
7045cff7825Smh27603 		prev_spd = cur_spd;
7055cff7825Smh27603 	}
7065cff7825Smh27603 	/* Slowest speed. Can't slow down anymore */
7075cff7825Smh27603 	cur_spd->idle_hwm = UINT_MAX;
7085cff7825Smh27603 	cur_spd->user_lwm = -1;
7095cff7825Smh27603 #ifdef	DEBUG
7105cff7825Smh27603 	DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: head_spd spd %d, "
7115cff7825Smh27603 	    "num_spd %d\n", ddi_get_instance(cpudsp->dip),
7125cff7825Smh27603 	    cpupm->head_spd->speed, cpupm->num_spd));
7135cff7825Smh27603 	for (cur_spd = cpupm->head_spd; cur_spd; cur_spd = cur_spd->down_spd) {
7145cff7825Smh27603 		DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: speed %d, "
7155cff7825Smh27603 		    "down_spd spd %d, idle_hwm %d, user_lwm %d, "
7165cff7825Smh27603 		    "up_spd spd %d, idle_lwm %d, user_hwm %d, "
7175cff7825Smh27603 		    "quant_cnt %d\n", ddi_get_instance(cpudsp->dip),
7185cff7825Smh27603 		    cur_spd->speed,
7195cff7825Smh27603 		    (cur_spd->down_spd ? cur_spd->down_spd->speed : 0),
7205cff7825Smh27603 		    cur_spd->idle_hwm, cur_spd->user_lwm,
7215cff7825Smh27603 		    (cur_spd->up_spd ? cur_spd->up_spd->speed : 0),
7225cff7825Smh27603 		    cur_spd->idle_lwm, cur_spd->user_hwm,
7235cff7825Smh27603 		    cur_spd->quant_cnt));
7245cff7825Smh27603 	}
7255cff7825Smh27603 #endif	/* DEBUG */
7265cff7825Smh27603 	CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
7275cff7825Smh27603 	return (DDI_SUCCESS);
7285cff7825Smh27603 }
7295cff7825Smh27603 
7305cff7825Smh27603 /*
7315cff7825Smh27603  * Free CPU power management data.
7325cff7825Smh27603  */
7335cff7825Smh27603 static void
7345cff7825Smh27603 cpudrv_pm_free(cpudrv_devstate_t *cpudsp)
7355cff7825Smh27603 {
7365cff7825Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
7375cff7825Smh27603 	cpudrv_pm_spd_t	*cur_spd, *next_spd;
7385cff7825Smh27603 
7395cff7825Smh27603 	cur_spd = cpupm->head_spd;
7405cff7825Smh27603 	while (cur_spd) {
7415cff7825Smh27603 		next_spd = cur_spd->down_spd;
7425cff7825Smh27603 		kmem_free(cur_spd, sizeof (cpudrv_pm_spd_t));
7435cff7825Smh27603 		cur_spd = next_spd;
7445cff7825Smh27603 	}
7455cff7825Smh27603 	bzero(cpupm, sizeof (cpudrv_pm_t));
7465cff7825Smh27603 	cpudrv_pm_free_module(cpudsp);
7475cff7825Smh27603 }
7485cff7825Smh27603 
7495cff7825Smh27603 /*
7505cff7825Smh27603  * Create pm-components property.
7515cff7825Smh27603  */
7525cff7825Smh27603 static int
7535cff7825Smh27603 cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp)
7545cff7825Smh27603 {
7555cff7825Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
7565cff7825Smh27603 	cpudrv_pm_spd_t	*cur_spd;
7575cff7825Smh27603 	char		**pmc;
7585cff7825Smh27603 	int		size;
7595cff7825Smh27603 	char		name[] = "NAME=CPU Speed";
7605cff7825Smh27603 	int		i, j;
7615cff7825Smh27603 	uint_t		comp_spd;
7625cff7825Smh27603 	int		result = DDI_FAILURE;
7635cff7825Smh27603 
7645cff7825Smh27603 	pmc = kmem_zalloc((cpupm->num_spd + 1) * sizeof (char *), KM_SLEEP);
7655cff7825Smh27603 	size = CPUDRV_PM_COMP_SIZE();
7665cff7825Smh27603 	if (cpupm->num_spd > CPUDRV_PM_COMP_MAX_VAL) {
7675cff7825Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
7685cff7825Smh27603 		    "number of speeds exceeded limits",
7695cff7825Smh27603 		    ddi_get_instance(cpudsp->dip));
7705cff7825Smh27603 		kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
7715cff7825Smh27603 		return (result);
7725cff7825Smh27603 	}
7735cff7825Smh27603 
7745cff7825Smh27603 	for (i = cpupm->num_spd, cur_spd = cpupm->head_spd; i > 0;
7755cff7825Smh27603 	    i--, cur_spd = cur_spd->down_spd) {
7765cff7825Smh27603 		cur_spd->pm_level = i;
7775cff7825Smh27603 		pmc[i] = kmem_zalloc((size * sizeof (char)), KM_SLEEP);
7785cff7825Smh27603 		comp_spd = CPUDRV_PM_COMP_SPEED(cpupm, cur_spd);
7795cff7825Smh27603 		if (comp_spd > CPUDRV_PM_COMP_MAX_VAL) {
7805cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_comp_create: "
7815cff7825Smh27603 			    "instance %d: speed exceeded limits",
7825cff7825Smh27603 			    ddi_get_instance(cpudsp->dip));
7835cff7825Smh27603 			for (j = cpupm->num_spd; j >= i; j--) {
7845cff7825Smh27603 				kmem_free(pmc[j], size * sizeof (char));
7855cff7825Smh27603 			}
7865cff7825Smh27603 			kmem_free(pmc, (cpupm->num_spd + 1) *
7875cff7825Smh27603 			    sizeof (char *));
7885cff7825Smh27603 			return (result);
7895cff7825Smh27603 		}
7905cff7825Smh27603 		CPUDRV_PM_COMP_SPRINT(pmc[i], cpupm, cur_spd, comp_spd)
7915cff7825Smh27603 		DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: "
7925cff7825Smh27603 		    "instance %d: pm-components power level %d string '%s'\n",
7935cff7825Smh27603 		    ddi_get_instance(cpudsp->dip), i, pmc[i]));
7945cff7825Smh27603 	}
7955cff7825Smh27603 	pmc[0] = kmem_zalloc(sizeof (name), KM_SLEEP);
7965cff7825Smh27603 	(void) strcat(pmc[0], name);
7975cff7825Smh27603 	DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: instance %d: "
7985cff7825Smh27603 	    "pm-components component name '%s'\n",
7995cff7825Smh27603 	    ddi_get_instance(cpudsp->dip), pmc[0]));
8005cff7825Smh27603 
8015cff7825Smh27603 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, cpudsp->dip,
8025cff7825Smh27603 	    "pm-components", pmc, cpupm->num_spd + 1) == DDI_PROP_SUCCESS) {
8035cff7825Smh27603 		result = DDI_SUCCESS;
8045cff7825Smh27603 	} else {
8055cff7825Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
8065cff7825Smh27603 		    "can't create pm-components property",
8075cff7825Smh27603 		    ddi_get_instance(cpudsp->dip));
8085cff7825Smh27603 	}
8095cff7825Smh27603 
8105cff7825Smh27603 	for (i = cpupm->num_spd; i > 0; i--) {
8115cff7825Smh27603 		kmem_free(pmc[i], size * sizeof (char));
8125cff7825Smh27603 	}
8135cff7825Smh27603 	kmem_free(pmc[0], sizeof (name));
8145cff7825Smh27603 	kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
8155cff7825Smh27603 	return (result);
8165cff7825Smh27603 }
8175cff7825Smh27603 
8185cff7825Smh27603 /*
8195cff7825Smh27603  * Mark a component idle.
8205cff7825Smh27603  */
8215cff7825Smh27603 #define	CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm) { \
8225cff7825Smh27603 	if ((cpupm)->pm_busycnt >= 1) { \
8235cff7825Smh27603 		if (pm_idle_component((dip), CPUDRV_PM_COMP_NUM) == \
8245cff7825Smh27603 		    DDI_SUCCESS) { \
8255cff7825Smh27603 			DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
8265cff7825Smh27603 			    "instance %d: pm_idle_component called\n", \
8275cff7825Smh27603 			    ddi_get_instance((dip)))); \
8285cff7825Smh27603 			(cpupm)->pm_busycnt--; \
8295cff7825Smh27603 		} else { \
8305cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
8315cff7825Smh27603 			    "can't idle CPU component", \
8325cff7825Smh27603 			    ddi_get_instance((dip))); \
8335cff7825Smh27603 		} \
8345cff7825Smh27603 	} \
8355cff7825Smh27603 }
8365cff7825Smh27603 
8375cff7825Smh27603 /*
8385cff7825Smh27603  * Marks a component busy in both PM framework and driver state structure.
8395cff7825Smh27603  */
8405cff7825Smh27603 #define	CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm) { \
8415cff7825Smh27603 	if ((cpupm)->pm_busycnt < 1) { \
8425cff7825Smh27603 		if (pm_busy_component((dip), CPUDRV_PM_COMP_NUM) == \
8435cff7825Smh27603 		    DDI_SUCCESS) { \
8445cff7825Smh27603 			DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
8455cff7825Smh27603 			    "instance %d: pm_busy_component called\n", \
8465cff7825Smh27603 			    ddi_get_instance((dip)))); \
8475cff7825Smh27603 			(cpupm)->pm_busycnt++; \
8485cff7825Smh27603 		} else { \
8495cff7825Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
8505cff7825Smh27603 			    "can't busy CPU component", \
8515cff7825Smh27603 			    ddi_get_instance((dip))); \
8525cff7825Smh27603 		} \
8535cff7825Smh27603 	} \
8545cff7825Smh27603 }
8555cff7825Smh27603 
8565cff7825Smh27603 /*
8575cff7825Smh27603  * Marks a component busy and calls pm_raise_power().
8585cff7825Smh27603  */
8595cff7825Smh27603 #define	CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm, new_level) { \
8605cff7825Smh27603 	/* \
8615cff7825Smh27603 	 * Mark driver and PM framework busy first so framework doesn't try \
8625cff7825Smh27603 	 * to bring CPU to lower speed when we need to be at higher speed. \
8635cff7825Smh27603 	 */ \
8645cff7825Smh27603 	CPUDRV_PM_MONITOR_PM_BUSY_COMP((dip), (cpupm)); \
8655cff7825Smh27603 	mutex_exit(&(cpudsp)->lock); \
8665cff7825Smh27603 	DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: " \
8675cff7825Smh27603 	    "pm_raise_power called to %d\n", ddi_get_instance((dip)), \
8685cff7825Smh27603 		(new_level))); \
8695cff7825Smh27603 	if (pm_raise_power((dip), CPUDRV_PM_COMP_NUM, (new_level)) != \
8705cff7825Smh27603 	    DDI_SUCCESS) { \
8715cff7825Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't " \
8725cff7825Smh27603 		    "raise CPU power level", ddi_get_instance((dip))); \
8735cff7825Smh27603 	} \
8745cff7825Smh27603 	mutex_enter(&(cpudsp)->lock); \
8755cff7825Smh27603 }
8765cff7825Smh27603 
8775cff7825Smh27603 /*
8785cff7825Smh27603  * In order to monitor a CPU, we need to hold cpu_lock to access CPU
8795cff7825Smh27603  * statistics. Holding cpu_lock is not allowed from a callout routine.
8805cff7825Smh27603  * We dispatch a taskq to do that job.
8815cff7825Smh27603  */
8825cff7825Smh27603 static void
8835cff7825Smh27603 cpudrv_pm_monitor_disp(void *arg)
8845cff7825Smh27603 {
8855cff7825Smh27603 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
8865cff7825Smh27603 
8875cff7825Smh27603 	/*
8885cff7825Smh27603 	 * We are here because the last task has scheduled a timeout.
8895cff7825Smh27603 	 * The queue should be empty at this time.
8905cff7825Smh27603 	 */
8915cff7825Smh27603 	mutex_enter(&cpudsp->cpudrv_pm.timeout_lock);
8925cff7825Smh27603 	if (!taskq_dispatch(cpudsp->cpudrv_pm.tq, cpudrv_pm_monitor, arg,
8935cff7825Smh27603 	    TQ_NOSLEEP)) {
8945cff7825Smh27603 		mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
8955cff7825Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor_disp: failed to "
8965cff7825Smh27603 		    "dispatch the cpudrv_pm_monitor taskq\n"));
8975cff7825Smh27603 		mutex_enter(&cpudsp->lock);
8985cff7825Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
8995cff7825Smh27603 		mutex_exit(&cpudsp->lock);
9005cff7825Smh27603 		return;
9015cff7825Smh27603 	}
9025cff7825Smh27603 	cpudsp->cpudrv_pm.timeout_count++;
9035cff7825Smh27603 	mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
9045cff7825Smh27603 }
9055cff7825Smh27603 
9065cff7825Smh27603 /*
9075cff7825Smh27603  * Get current CPU microstate times and scale them. We should probably be
9085cff7825Smh27603  * using get_cpu_mstate() to get this data, but bugs in some of the ISRs
9095cff7825Smh27603  * have led to inflated system times and prevented CPUs from being power
9105cff7825Smh27603  * managed. We can probably safely ignore time spent in ISRs when
9115cff7825Smh27603  * determining idleness.
9125cff7825Smh27603  */
9135cff7825Smh27603 static void
9145cff7825Smh27603 cpudrv_get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
9155cff7825Smh27603 {
9165cff7825Smh27603 	int i;
9175cff7825Smh27603 
9185cff7825Smh27603 	for (i = 0; i < NCMSTATES; i++) {
9195cff7825Smh27603 		times[i] = cpu->cpu_acct[i];
9205cff7825Smh27603 		scalehrtime(&times[i]);
9215cff7825Smh27603 	}
9225cff7825Smh27603 }
9235cff7825Smh27603 
9245cff7825Smh27603 /*
9255cff7825Smh27603  * Monitors each CPU for the amount of time idle thread was running in the
9265cff7825Smh27603  * last quantum and arranges for the CPU to go to the lower or higher speed.
9275cff7825Smh27603  * Called at the time interval appropriate for the current speed. The
9285cff7825Smh27603  * time interval for normal speed is CPUDRV_PM_QUANT_CNT_NORMAL. The time
9295cff7825Smh27603  * interval for other speeds (including unknown speed) is
9305cff7825Smh27603  * CPUDRV_PM_QUANT_CNT_OTHR.
9315cff7825Smh27603  */
9325cff7825Smh27603 static void
9335cff7825Smh27603 cpudrv_pm_monitor(void *arg)
9345cff7825Smh27603 {
9355cff7825Smh27603 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
9365cff7825Smh27603 	cpudrv_pm_t		*cpupm;
9375cff7825Smh27603 	cpudrv_pm_spd_t		*cur_spd, *new_spd;
9385cff7825Smh27603 	cpu_t			*cp;
9395cff7825Smh27603 	dev_info_t		*dip;
9405cff7825Smh27603 	uint_t			idle_cnt, user_cnt, system_cnt;
9415cff7825Smh27603 	clock_t			lbolt_cnt;
9425cff7825Smh27603 	hrtime_t		msnsecs[NCMSTATES];
9435cff7825Smh27603 	boolean_t		is_ready;
9445cff7825Smh27603 
9455cff7825Smh27603 #define	GET_CPU_MSTATE_CNT(state, cnt) \
9465cff7825Smh27603 	msnsecs[state] = NSEC_TO_TICK(msnsecs[state]); \
9475cff7825Smh27603 	if (cpupm->lastquan_mstate[state] > msnsecs[state]) \
9485cff7825Smh27603 		msnsecs[state] = cpupm->lastquan_mstate[state]; \
9495cff7825Smh27603 	cnt = msnsecs[state] - cpupm->lastquan_mstate[state]; \
9505cff7825Smh27603 	cpupm->lastquan_mstate[state] = msnsecs[state]
9515cff7825Smh27603 
9525cff7825Smh27603 	mutex_enter(&cpudsp->lock);
9535cff7825Smh27603 	cpupm = &(cpudsp->cpudrv_pm);
9545cff7825Smh27603 	if (cpupm->timeout_id == 0) {
9555cff7825Smh27603 		mutex_exit(&cpudsp->lock);
9565cff7825Smh27603 		goto do_return;
9575cff7825Smh27603 	}
9585cff7825Smh27603 	cur_spd = cpupm->cur_spd;
9595cff7825Smh27603 	dip = cpudsp->dip;
9605cff7825Smh27603 
9615cff7825Smh27603 	/*
9625cff7825Smh27603 	 * We assume that a CPU is initialized and has a valid cpu_t
9635cff7825Smh27603 	 * structure, if it is ready for cross calls. If this changes,
9645cff7825Smh27603 	 * additional checks might be needed.
9655cff7825Smh27603 	 *
9665cff7825Smh27603 	 * Additionally, for x86 platforms we cannot power manage
9675cff7825Smh27603 	 * any one instance, until all instances have been initialized.
9685cff7825Smh27603 	 * That's because we don't know what the CPU domains look like
9695cff7825Smh27603 	 * until all instances have been initialized.
9705cff7825Smh27603 	 */
9715cff7825Smh27603 	is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
9725cff7825Smh27603 	if (!is_ready) {
9735cff7825Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
9745cff7825Smh27603 		    "CPU not ready for x-calls\n", ddi_get_instance(dip)));
9755cff7825Smh27603 	} else if (!(is_ready = cpudrv_pm_all_instances_ready())) {
9765cff7825Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
9775cff7825Smh27603 		    "waiting for all CPUs to be ready\n",
9785cff7825Smh27603 		    ddi_get_instance(dip)));
9795cff7825Smh27603 	}
9805cff7825Smh27603 	if (!is_ready) {
9815cff7825Smh27603 		/*
9825cff7825Smh27603 		 * Make sure that we are busy so that framework doesn't
9835cff7825Smh27603 		 * try to bring us down in this situation.
9845cff7825Smh27603 		 */
9855cff7825Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
9865cff7825Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
9875cff7825Smh27603 		mutex_exit(&cpudsp->lock);
9885cff7825Smh27603 		goto do_return;
9895cff7825Smh27603 	}
9905cff7825Smh27603 
9915cff7825Smh27603 	/*
9925cff7825Smh27603 	 * Make sure that we are still not at unknown power level.
9935cff7825Smh27603 	 */
9945cff7825Smh27603 	if (cur_spd == NULL) {
9955cff7825Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
9965cff7825Smh27603 		    "cur_spd is unknown\n", ddi_get_instance(dip)));
9975cff7825Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
9985cff7825Smh27603 		    cpupm->targ_spd->pm_level);
9995cff7825Smh27603 		/*
10005cff7825Smh27603 		 * We just changed the speed. Wait till at least next
10015cff7825Smh27603 		 * call to this routine before proceeding ahead.
10025cff7825Smh27603 		 */
10035cff7825Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10045cff7825Smh27603 		mutex_exit(&cpudsp->lock);
10055cff7825Smh27603 		goto do_return;
10065cff7825Smh27603 	}
10075cff7825Smh27603 
10085cff7825Smh27603 	mutex_enter(&cpu_lock);
10095cff7825Smh27603 	if ((cp = cpu_get(cpudsp->cpu_id)) == NULL) {
10105cff7825Smh27603 		mutex_exit(&cpu_lock);
10115cff7825Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10125cff7825Smh27603 		mutex_exit(&cpudsp->lock);
10135cff7825Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't get "
10145cff7825Smh27603 		    "cpu_t", ddi_get_instance(dip));
10155cff7825Smh27603 		goto do_return;
10165cff7825Smh27603 	}
1017*68afbec1Smh27603 
1018*68afbec1Smh27603 	if (!cpupm->pm_started) {
1019*68afbec1Smh27603 		cpupm->pm_started = B_TRUE;
10205cff7825Smh27603 		set_supp_freqs(cp, cpupm);
1021*68afbec1Smh27603 	}
10225cff7825Smh27603 
10235cff7825Smh27603 	cpudrv_get_cpu_mstate(cp, msnsecs);
10245cff7825Smh27603 	GET_CPU_MSTATE_CNT(CMS_IDLE, idle_cnt);
10255cff7825Smh27603 	GET_CPU_MSTATE_CNT(CMS_USER, user_cnt);
10265cff7825Smh27603 	GET_CPU_MSTATE_CNT(CMS_SYSTEM, system_cnt);
10275cff7825Smh27603 
10285cff7825Smh27603 	/*
10295cff7825Smh27603 	 * We can't do anything when we have just switched to a state
10305cff7825Smh27603 	 * because there is no valid timestamp.
10315cff7825Smh27603 	 */
10325cff7825Smh27603 	if (cpupm->lastquan_lbolt == 0) {
10335cff7825Smh27603 		cpupm->lastquan_lbolt = lbolt;
10345cff7825Smh27603 		mutex_exit(&cpu_lock);
10355cff7825Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10365cff7825Smh27603 		mutex_exit(&cpudsp->lock);
10375cff7825Smh27603 		goto do_return;
10385cff7825Smh27603 	}
10395cff7825Smh27603 
10405cff7825Smh27603 	/*
10415cff7825Smh27603 	 * Various watermarks are based on this routine being called back
10425cff7825Smh27603 	 * exactly at the requested period. This is not guaranteed
10435cff7825Smh27603 	 * because this routine is called from a taskq that is dispatched
10445cff7825Smh27603 	 * from a timeout routine.  Handle this by finding out how many
10455cff7825Smh27603 	 * ticks have elapsed since the last call (lbolt_cnt) and adjusting
10465cff7825Smh27603 	 * the idle_cnt based on the delay added to the requested period
10475cff7825Smh27603 	 * by timeout and taskq.
10485cff7825Smh27603 	 */
10495cff7825Smh27603 	lbolt_cnt = lbolt - cpupm->lastquan_lbolt;
10505cff7825Smh27603 	cpupm->lastquan_lbolt = lbolt;
10515cff7825Smh27603 	mutex_exit(&cpu_lock);
10525cff7825Smh27603 	/*
10535cff7825Smh27603 	 * Time taken between recording the current counts and
10545cff7825Smh27603 	 * arranging the next call of this routine is an error in our
10555cff7825Smh27603 	 * calculation. We minimize the error by calling
10565cff7825Smh27603 	 * CPUDRV_PM_MONITOR_INIT() here instead of end of this routine.
10575cff7825Smh27603 	 */
10585cff7825Smh27603 	CPUDRV_PM_MONITOR_INIT(cpudsp);
10595cff7825Smh27603 	DPRINTF(D_PM_MONITOR_VERBOSE, ("cpudrv_pm_monitor: instance %d: "
10605cff7825Smh27603 	    "idle count %d, user count %d, system count %d, pm_level %d, "
10615cff7825Smh27603 	    "pm_busycnt %d\n", ddi_get_instance(dip), idle_cnt, user_cnt,
10625cff7825Smh27603 	    system_cnt, cur_spd->pm_level, cpupm->pm_busycnt));
10635cff7825Smh27603 
10645cff7825Smh27603 #ifdef	DEBUG
10655cff7825Smh27603 	/*
10665cff7825Smh27603 	 * Notify that timeout and taskq has caused delays and we need to
10675cff7825Smh27603 	 * scale our parameters accordingly.
10685cff7825Smh27603 	 *
10695cff7825Smh27603 	 * To get accurate result, don't turn on other DPRINTFs with
10705cff7825Smh27603 	 * the following DPRINTF. PROM calls generated by other
10715cff7825Smh27603 	 * DPRINTFs changes the timing.
10725cff7825Smh27603 	 */
10735cff7825Smh27603 	if (lbolt_cnt > cur_spd->quant_cnt) {
10745cff7825Smh27603 		DPRINTF(D_PM_MONITOR_DELAY, ("cpudrv_pm_monitor: instance %d: "
10755cff7825Smh27603 		    "lbolt count %ld > quantum_count %u\n",
10765cff7825Smh27603 		    ddi_get_instance(dip), lbolt_cnt, cur_spd->quant_cnt));
10775cff7825Smh27603 	}
10785cff7825Smh27603 #endif	/* DEBUG */
10795cff7825Smh27603 
10805cff7825Smh27603 	/*
10815cff7825Smh27603 	 * Adjust counts based on the delay added by timeout and taskq.
10825cff7825Smh27603 	 */
10835cff7825Smh27603 	idle_cnt = (idle_cnt * cur_spd->quant_cnt) / lbolt_cnt;
10845cff7825Smh27603 	user_cnt = (user_cnt * cur_spd->quant_cnt) / lbolt_cnt;
10855cff7825Smh27603 	if ((user_cnt > cur_spd->user_hwm) || (idle_cnt < cur_spd->idle_lwm &&
10865cff7825Smh27603 	    cur_spd->idle_blwm_cnt >= cpudrv_pm_idle_blwm_cnt_max)) {
10875cff7825Smh27603 		cur_spd->idle_blwm_cnt = 0;
10885cff7825Smh27603 		cur_spd->idle_bhwm_cnt = 0;
10895cff7825Smh27603 		/*
10905cff7825Smh27603 		 * In normal situation, arrange to go to next higher speed.
10915cff7825Smh27603 		 * If we are running in special direct pm mode, we just stay
10925cff7825Smh27603 		 * at the current speed.
10935cff7825Smh27603 		 */
10945cff7825Smh27603 		if (cur_spd == cur_spd->up_spd || cpudrv_direct_pm) {
10955cff7825Smh27603 			CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
10965cff7825Smh27603 		} else {
10975cff7825Smh27603 			new_spd = cur_spd->up_spd;
10985cff7825Smh27603 			CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
10995cff7825Smh27603 			    new_spd->pm_level);
11005cff7825Smh27603 		}
11015cff7825Smh27603 	} else if ((user_cnt <= cur_spd->user_lwm) &&
11025cff7825Smh27603 	    (idle_cnt >= cur_spd->idle_hwm) || !CPU_ACTIVE(cp)) {
11035cff7825Smh27603 		cur_spd->idle_blwm_cnt = 0;
11045cff7825Smh27603 		cur_spd->idle_bhwm_cnt = 0;
11055cff7825Smh27603 		/*
11065cff7825Smh27603 		 * Arrange to go to next lower speed by informing our idle
11075cff7825Smh27603 		 * status to the power management framework.
11085cff7825Smh27603 		 */
11095cff7825Smh27603 		CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm);
11105cff7825Smh27603 	} else {
11115cff7825Smh27603 		/*
11125cff7825Smh27603 		 * If we are between the idle water marks and have not
11135cff7825Smh27603 		 * been here enough consecutive times to be considered
11145cff7825Smh27603 		 * busy, just increment the count and return.
11155cff7825Smh27603 		 */
11165cff7825Smh27603 		if ((idle_cnt < cur_spd->idle_hwm) &&
11175cff7825Smh27603 		    (idle_cnt >= cur_spd->idle_lwm) &&
11185cff7825Smh27603 		    (cur_spd->idle_bhwm_cnt < cpudrv_pm_idle_bhwm_cnt_max)) {
11195cff7825Smh27603 			cur_spd->idle_blwm_cnt = 0;
11205cff7825Smh27603 			cur_spd->idle_bhwm_cnt++;
11215cff7825Smh27603 			mutex_exit(&cpudsp->lock);
11225cff7825Smh27603 			goto do_return;
11235cff7825Smh27603 		}
11245cff7825Smh27603 		if (idle_cnt < cur_spd->idle_lwm) {
11255cff7825Smh27603 			cur_spd->idle_blwm_cnt++;
11265cff7825Smh27603 			cur_spd->idle_bhwm_cnt = 0;
11275cff7825Smh27603 		}
11285cff7825Smh27603 		/*
11295cff7825Smh27603 		 * Arranges to stay at the current speed.
11305cff7825Smh27603 		 */
11315cff7825Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
11325cff7825Smh27603 	}
11335cff7825Smh27603 	mutex_exit(&cpudsp->lock);
11345cff7825Smh27603 do_return:
11355cff7825Smh27603 	mutex_enter(&cpupm->timeout_lock);
11365cff7825Smh27603 	ASSERT(cpupm->timeout_count > 0);
11375cff7825Smh27603 	cpupm->timeout_count--;
11385cff7825Smh27603 	cv_signal(&cpupm->timeout_cv);
11395cff7825Smh27603 	mutex_exit(&cpupm->timeout_lock);
11405cff7825Smh27603 }
1141