xref: /onnv-gate/usr/src/uts/common/io/cpudrv.c (revision 4877:5744980c78cf)
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 /*
224667Smh27603  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
234667Smh27603  * Use is subject to license terms.
244667Smh27603  */
254667Smh27603 
264667Smh27603 #pragma ident	"%Z%%M%	%I%	%E% SMI"
274667Smh27603 
284667Smh27603 /*
294667Smh27603  * CPU Device driver. The driver is not DDI-compliant.
304667Smh27603  *
314667Smh27603  * The driver supports following features:
324667Smh27603  *	- Power management.
334667Smh27603  */
344667Smh27603 
354667Smh27603 #include <sys/types.h>
364667Smh27603 #include <sys/param.h>
374667Smh27603 #include <sys/errno.h>
384667Smh27603 #include <sys/modctl.h>
394667Smh27603 #include <sys/kmem.h>
404667Smh27603 #include <sys/conf.h>
414667Smh27603 #include <sys/cmn_err.h>
424667Smh27603 #include <sys/stat.h>
434667Smh27603 #include <sys/debug.h>
444667Smh27603 #include <sys/systm.h>
454667Smh27603 #include <sys/ddi.h>
464667Smh27603 #include <sys/sunddi.h>
474667Smh27603 
484667Smh27603 #include <sys/machsystm.h>
494667Smh27603 #include <sys/x_call.h>
504667Smh27603 #include <sys/cpudrv.h>
514667Smh27603 #include <sys/cpudrv_plat.h>
524667Smh27603 #include <sys/msacct.h>
534667Smh27603 
544667Smh27603 /*
554667Smh27603  * CPU power management
564667Smh27603  *
574667Smh27603  * The supported power saving model is to slow down the CPU (on SPARC by
584667Smh27603  * dividing the CPU clock and on x86 by dropping down a P-state).
594667Smh27603  * Periodically we determine the amount of time the CPU is running
604667Smh27603  * idle thread and threads in user mode during the last quantum.  If the idle
614667Smh27603  * thread was running less than its low water mark for current speed for
624667Smh27603  * number of consecutive sampling periods, or number of running threads in
634667Smh27603  * user mode are above its high water mark, we arrange to go to the higher
644667Smh27603  * speed.  If the idle thread was running more than its high water mark without
654667Smh27603  * dropping a number of consecutive times below the mark, and number of threads
664667Smh27603  * running in user mode are below its low water mark, we arrange to go to the
674667Smh27603  * next lower speed.  While going down, we go through all the speeds.  While
684667Smh27603  * going up we go to the maximum speed to minimize impact on the user, but have
694667Smh27603  * provisions in the driver to go to other speeds.
704667Smh27603  *
714667Smh27603  * The driver does not have knowledge of a particular implementation of this
724667Smh27603  * scheme and will work with all CPUs supporting this model. On SPARC, the
734667Smh27603  * driver determines supported speeds by looking at 'clock-divisors' property
744667Smh27603  * created by OBP. On x86, the driver retrieves the supported speeds from
754667Smh27603  * ACPI.
764667Smh27603  */
774667Smh27603 
784667Smh27603 /*
794667Smh27603  * Configuration function prototypes and data structures
804667Smh27603  */
814667Smh27603 static int cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
824667Smh27603 static int cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
834667Smh27603 static int cpudrv_power(dev_info_t *dip, int comp, int level);
844667Smh27603 
854667Smh27603 struct dev_ops cpudrv_ops = {
864667Smh27603 	DEVO_REV,		/* rev */
874667Smh27603 	0,			/* refcnt */
884667Smh27603 	nodev,			/* getinfo */
894667Smh27603 	nulldev,		/* identify */
904667Smh27603 	nulldev,		/* probe */
914667Smh27603 	cpudrv_attach,		/* attach */
924667Smh27603 	cpudrv_detach,		/* detach */
934667Smh27603 	nodev,			/* reset */
944667Smh27603 	(struct cb_ops *)NULL,	/* cb_ops */
954667Smh27603 	(struct bus_ops *)NULL,	/* bus_ops */
964667Smh27603 	cpudrv_power		/* power */
974667Smh27603 };
984667Smh27603 
994667Smh27603 static struct modldrv modldrv = {
1004667Smh27603 	&mod_driverops,			/* modops */
1014667Smh27603 	"CPU Driver %I%",		/* linkinfo */
1024667Smh27603 	&cpudrv_ops,			/* dev_ops */
1034667Smh27603 };
1044667Smh27603 
1054667Smh27603 static struct modlinkage modlinkage = {
1064667Smh27603 	MODREV_1,		/* rev */
1074667Smh27603 	&modldrv,		/* linkage */
1084667Smh27603 	NULL
1094667Smh27603 };
1104667Smh27603 
1114667Smh27603 /*
1124667Smh27603  * Function prototypes
1134667Smh27603  */
1144667Smh27603 static int cpudrv_pm_init(cpudrv_devstate_t *cpudsp);
1154667Smh27603 static void cpudrv_pm_free(cpudrv_devstate_t *cpudsp);
1164667Smh27603 static int cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp);
1174667Smh27603 static void cpudrv_pm_monitor_disp(void *arg);
1184667Smh27603 static void cpudrv_pm_monitor(void *arg);
1194667Smh27603 
1204667Smh27603 /*
1214667Smh27603  * Driver global variables
1224667Smh27603  */
1234667Smh27603 uint_t cpudrv_debug = 0;
1244667Smh27603 void *cpudrv_state;
1254667Smh27603 static uint_t cpudrv_pm_idle_hwm = CPUDRV_PM_IDLE_HWM;
1264667Smh27603 static uint_t cpudrv_pm_idle_lwm = CPUDRV_PM_IDLE_LWM;
1274667Smh27603 static uint_t cpudrv_pm_idle_buf_zone = CPUDRV_PM_IDLE_BUF_ZONE;
1284667Smh27603 static uint_t cpudrv_pm_idle_bhwm_cnt_max = CPUDRV_PM_IDLE_BHWM_CNT_MAX;
1294667Smh27603 static uint_t cpudrv_pm_idle_blwm_cnt_max = CPUDRV_PM_IDLE_BLWM_CNT_MAX;
1304667Smh27603 static uint_t cpudrv_pm_user_hwm = CPUDRV_PM_USER_HWM;
1314667Smh27603 
1324667Smh27603 /*
1334667Smh27603  * cpudrv_direct_pm allows user applications to directly control the
1344667Smh27603  * power state transitions (direct pm) without following the normal
1354667Smh27603  * direct pm protocol. This is needed because the normal protocol
1364667Smh27603  * requires that a device only be lowered when it is idle, and be
1374667Smh27603  * brought up when it request to do so by calling pm_raise_power().
1384667Smh27603  * Ignoring this protocol is harmless for CPU (other than speed).
1394667Smh27603  * Moreover it might be the case that CPU is never idle or wants
1404667Smh27603  * to be at higher speed because of the addition CPU cycles required
1414667Smh27603  * to run the user application.
1424667Smh27603  *
1434667Smh27603  * The driver will still report idle/busy status to the framework. Although
1444667Smh27603  * framework will ignore this information for direct pm devices and not
1454667Smh27603  * try to bring them down when idle, user applications can still use this
1464667Smh27603  * information if they wants.
1474667Smh27603  *
1484667Smh27603  * In the future, provide an ioctl to control setting of this mode. In
1494667Smh27603  * that case, this variable should move to the state structure and
1504667Smh27603  * be protected by the lock in the state structure.
1514667Smh27603  */
1524667Smh27603 int cpudrv_direct_pm = 0;
1534667Smh27603 
1544667Smh27603 /*
1554667Smh27603  * Arranges for the handler function to be called at the interval suitable
1564667Smh27603  * for current speed.
1574667Smh27603  */
1584667Smh27603 #define	CPUDRV_PM_MONITOR_INIT(cpudsp) { \
1594667Smh27603 	ASSERT(mutex_owned(&(cpudsp)->lock)); \
1604667Smh27603 	(cpudsp)->cpudrv_pm.timeout_id = timeout(cpudrv_pm_monitor_disp, \
1614667Smh27603 	    (cpudsp), (((cpudsp)->cpudrv_pm.cur_spd == NULL) ? \
1624667Smh27603 	    CPUDRV_PM_QUANT_CNT_OTHR : \
1634667Smh27603 	    (cpudsp)->cpudrv_pm.cur_spd->quant_cnt)); \
1644667Smh27603 }
1654667Smh27603 
1664667Smh27603 /*
1674667Smh27603  * Arranges for the handler function not to be called back.
1684667Smh27603  */
1694667Smh27603 #define	CPUDRV_PM_MONITOR_FINI(cpudsp) { \
1704667Smh27603 	timeout_id_t tmp_tid; \
1714667Smh27603 	ASSERT(mutex_owned(&(cpudsp)->lock)); \
1724667Smh27603 	ASSERT((cpudsp)->cpudrv_pm.timeout_id); \
1734667Smh27603 	tmp_tid = (cpudsp)->cpudrv_pm.timeout_id; \
1744667Smh27603 	(cpudsp)->cpudrv_pm.timeout_id = 0; \
1754667Smh27603 	mutex_exit(&(cpudsp)->lock); \
1764667Smh27603 	(void) untimeout(tmp_tid); \
1774667Smh27603 	mutex_enter(&(cpudsp)->cpudrv_pm.timeout_lock); \
1784667Smh27603 	while ((cpudsp)->cpudrv_pm.timeout_count != 0) \
1794667Smh27603 		cv_wait(&(cpudsp)->cpudrv_pm.timeout_cv, \
1804667Smh27603 		    &(cpudsp)->cpudrv_pm.timeout_lock); \
1814667Smh27603 	mutex_exit(&(cpudsp)->cpudrv_pm.timeout_lock); \
1824667Smh27603 	mutex_enter(&(cpudsp)->lock); \
1834667Smh27603 }
1844667Smh27603 
1854667Smh27603 int
1864667Smh27603 _init(void)
1874667Smh27603 {
1884667Smh27603 	int	error;
1894667Smh27603 
1904667Smh27603 	DPRINTF(D_INIT, (" _init: function called\n"));
1914667Smh27603 	if ((error = ddi_soft_state_init(&cpudrv_state,
1924667Smh27603 	    sizeof (cpudrv_devstate_t), 0)) != 0) {
1934667Smh27603 		return (error);
1944667Smh27603 	}
1954667Smh27603 
1964667Smh27603 	if ((error = mod_install(&modlinkage)) != 0)  {
1974667Smh27603 		ddi_soft_state_fini(&cpudrv_state);
1984667Smh27603 	}
1994667Smh27603 
2004667Smh27603 	/*
2014667Smh27603 	 * Callbacks used by the PPM driver.
2024667Smh27603 	 */
2034667Smh27603 	CPUDRV_PM_SET_PPM_CALLBACKS();
2044667Smh27603 	return (error);
2054667Smh27603 }
2064667Smh27603 
2074667Smh27603 int
2084667Smh27603 _fini(void)
2094667Smh27603 {
2104667Smh27603 	int	error;
2114667Smh27603 
2124667Smh27603 	DPRINTF(D_FINI, (" _fini: function called\n"));
2134667Smh27603 	if ((error = mod_remove(&modlinkage)) == 0) {
2144667Smh27603 		ddi_soft_state_fini(&cpudrv_state);
2154667Smh27603 	}
2164667Smh27603 
2174667Smh27603 	return (error);
2184667Smh27603 }
2194667Smh27603 
2204667Smh27603 int
2214667Smh27603 _info(struct modinfo *modinfop)
2224667Smh27603 {
2234667Smh27603 	return (mod_info(&modlinkage, modinfop));
2244667Smh27603 }
2254667Smh27603 
2264667Smh27603 /*
2274667Smh27603  * Driver attach(9e) entry point.
2284667Smh27603  */
2294667Smh27603 static int
2304667Smh27603 cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2314667Smh27603 {
2324667Smh27603 	int			instance;
2334667Smh27603 	cpudrv_devstate_t	*cpudsp;
2344667Smh27603 	extern pri_t		maxclsyspri;
2354667Smh27603 
2364667Smh27603 	instance = ddi_get_instance(dip);
2374667Smh27603 
2384667Smh27603 	switch (cmd) {
2394667Smh27603 	case DDI_ATTACH:
2404667Smh27603 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
2414667Smh27603 		    "DDI_ATTACH called\n", instance));
2424667Smh27603 		if (ddi_soft_state_zalloc(cpudrv_state, instance) !=
2434667Smh27603 		    DDI_SUCCESS) {
2444667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2454667Smh27603 			    "can't allocate state", instance);
2464667Smh27603 			CPUDRV_PM_DISABLE();
2474667Smh27603 			return (DDI_FAILURE);
2484667Smh27603 		}
2494667Smh27603 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
2504667Smh27603 		    NULL) {
2514667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2524667Smh27603 			    "can't get state", instance);
2534667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2544667Smh27603 			CPUDRV_PM_DISABLE();
2554667Smh27603 			return (DDI_FAILURE);
2564667Smh27603 		}
2574667Smh27603 		cpudsp->dip = dip;
2584667Smh27603 
2594667Smh27603 		/*
2604667Smh27603 		 * Find CPU number for this dev_info node.
2614667Smh27603 		 */
2624667Smh27603 		if (!cpudrv_pm_get_cpu_id(dip, &(cpudsp->cpu_id))) {
2634667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
2644667Smh27603 			    "can't convert dip to cpu_id", instance);
2654667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2664667Smh27603 			CPUDRV_PM_DISABLE();
2674667Smh27603 			return (DDI_FAILURE);
2684667Smh27603 		}
2694667Smh27603 		if (cpudrv_pm_init(cpudsp) != DDI_SUCCESS) {
2704667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2714667Smh27603 			CPUDRV_PM_DISABLE();
2724667Smh27603 			return (DDI_FAILURE);
2734667Smh27603 		}
2744667Smh27603 		if (cpudrv_pm_comp_create(cpudsp) != DDI_SUCCESS) {
2754667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2764667Smh27603 			CPUDRV_PM_DISABLE();
2774667Smh27603 			cpudrv_pm_free(cpudsp);
2784667Smh27603 			return (DDI_FAILURE);
2794667Smh27603 		}
2804667Smh27603 		if (ddi_prop_update_string(DDI_DEV_T_NONE,
2814667Smh27603 		    dip, "pm-class", "CPU") != DDI_PROP_SUCCESS) {
2824667Smh27603 			ddi_soft_state_free(cpudrv_state, instance);
2834667Smh27603 			CPUDRV_PM_DISABLE();
2844667Smh27603 			cpudrv_pm_free(cpudsp);
2854667Smh27603 			return (DDI_FAILURE);
2864667Smh27603 		}
2874667Smh27603 
2884667Smh27603 		/*
2894667Smh27603 		 * Taskq is used to dispatch routine to monitor CPU activities.
2904667Smh27603 		 */
2914667Smh27603 		cpudsp->cpudrv_pm.tq = taskq_create_instance(
2924667Smh27603 		    "cpudrv_pm_monitor",
2934667Smh27603 		    ddi_get_instance(dip), CPUDRV_PM_TASKQ_THREADS,
2944667Smh27603 		    (maxclsyspri - 1), CPUDRV_PM_TASKQ_MIN,
2954667Smh27603 		    CPUDRV_PM_TASKQ_MAX, TASKQ_PREPOPULATE|TASKQ_CPR_SAFE);
2964667Smh27603 
2974667Smh27603 		mutex_init(&cpudsp->lock, NULL, MUTEX_DRIVER, NULL);
2984667Smh27603 		mutex_init(&cpudsp->cpudrv_pm.timeout_lock, NULL, MUTEX_DRIVER,
2994667Smh27603 		    NULL);
3004667Smh27603 		cv_init(&cpudsp->cpudrv_pm.timeout_cv, NULL, CV_DEFAULT, NULL);
3014667Smh27603 
3024667Smh27603 		/*
3034667Smh27603 		 * Driver needs to assume that CPU is running at unknown speed
3044667Smh27603 		 * at DDI_ATTACH and switch it to the needed speed. We assume
3054667Smh27603 		 * that initial needed speed is full speed for us.
3064667Smh27603 		 */
3074667Smh27603 		/*
3084667Smh27603 		 * We need to take the lock because cpudrv_pm_monitor()
3094667Smh27603 		 * will start running in parallel with attach().
3104667Smh27603 		 */
3114667Smh27603 		mutex_enter(&cpudsp->lock);
3124667Smh27603 		cpudsp->cpudrv_pm.cur_spd = NULL;
3134667Smh27603 		cpudsp->cpudrv_pm.targ_spd = cpudsp->cpudrv_pm.head_spd;
314*4877Smh27603 		cpudsp->cpudrv_pm.pm_started = B_FALSE;
3154667Smh27603 		/*
3164667Smh27603 		 * We don't call pm_raise_power() directly from attach because
3174667Smh27603 		 * driver attach for a slave CPU node can happen before the
3184667Smh27603 		 * CPU is even initialized. We just start the monitoring
3194667Smh27603 		 * system which understands unknown speed and moves CPU
3204667Smh27603 		 * to targ_spd when it have been initialized.
3214667Smh27603 		 */
3224667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
3234667Smh27603 		mutex_exit(&cpudsp->lock);
3244667Smh27603 
3254667Smh27603 		CPUDRV_PM_INSTALL_TOPSPEED_CHANGE_HANDLER(cpudsp, dip);
3264667Smh27603 
3274667Smh27603 		ddi_report_dev(dip);
3284667Smh27603 		return (DDI_SUCCESS);
3294667Smh27603 
3304667Smh27603 	case DDI_RESUME:
3314667Smh27603 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
3324667Smh27603 		    "DDI_RESUME called\n", instance));
3334667Smh27603 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
3344667Smh27603 		    NULL) {
3354667Smh27603 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
3364667Smh27603 			    "can't get state", instance);
3374667Smh27603 			return (DDI_FAILURE);
3384667Smh27603 		}
3394667Smh27603 		mutex_enter(&cpudsp->lock);
3404667Smh27603 		/*
3414667Smh27603 		 * Driver needs to assume that CPU is running at unknown speed
3424667Smh27603 		 * at DDI_RESUME and switch it to the needed speed. We assume
3434667Smh27603 		 * that the needed speed is full speed for us.
3444667Smh27603 		 */
3454667Smh27603 		cpudsp->cpudrv_pm.cur_spd = NULL;
3464667Smh27603 		cpudsp->cpudrv_pm.targ_spd = cpudsp->cpudrv_pm.head_spd;
3474667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
3484667Smh27603 		mutex_exit(&cpudsp->lock);
3494667Smh27603 		CPUDRV_PM_REDEFINE_TOPSPEED(dip);
3504667Smh27603 		return (DDI_SUCCESS);
3514667Smh27603 
3524667Smh27603 	default:
3534667Smh27603 		return (DDI_FAILURE);
3544667Smh27603 	}
3554667Smh27603 }
3564667Smh27603 
3574667Smh27603 /*
3584667Smh27603  * Driver detach(9e) entry point.
3594667Smh27603  */
3604667Smh27603 static int
3614667Smh27603 cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3624667Smh27603 {
3634667Smh27603 	int			instance;
3644667Smh27603 	cpudrv_devstate_t	*cpudsp;
3654667Smh27603 	cpudrv_pm_t		*cpupm;
3664667Smh27603 
3674667Smh27603 	instance = ddi_get_instance(dip);
3684667Smh27603 
3694667Smh27603 	switch (cmd) {
3704667Smh27603 	case DDI_DETACH:
3714667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
3724667Smh27603 		    "DDI_DETACH called\n", instance));
3734667Smh27603 		/*
3744667Smh27603 		 * If the only thing supported by the driver is power
3754667Smh27603 		 * management, we can in future enhance the driver and
3764667Smh27603 		 * framework that loads it to unload the driver when
3774667Smh27603 		 * user has disabled CPU power management.
3784667Smh27603 		 */
3794667Smh27603 		return (DDI_FAILURE);
3804667Smh27603 
3814667Smh27603 	case DDI_SUSPEND:
3824667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
3834667Smh27603 		    "DDI_SUSPEND called\n", instance));
3844667Smh27603 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
3854667Smh27603 		    NULL) {
3864667Smh27603 			cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
3874667Smh27603 			    "can't get state", instance);
3884667Smh27603 			return (DDI_FAILURE);
3894667Smh27603 		}
3904667Smh27603 		/*
3914667Smh27603 		 * During a checkpoint-resume sequence, framework will
3924667Smh27603 		 * stop interrupts to quiesce kernel activity. This will
3934667Smh27603 		 * leave our monitoring system ineffective. Handle this
3944667Smh27603 		 * by stopping our monitoring system and bringing CPU
3954667Smh27603 		 * to full speed. In case we are in special direct pm
3964667Smh27603 		 * mode, we leave the CPU at whatever speed it is. This
3974667Smh27603 		 * is harmless other than speed.
3984667Smh27603 		 */
3994667Smh27603 		mutex_enter(&cpudsp->lock);
4004667Smh27603 		cpupm = &(cpudsp->cpudrv_pm);
4014667Smh27603 
4024667Smh27603 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: DDI_SUSPEND - "
4034667Smh27603 		    "cur_spd %d, head_spd %d\n", instance,
4044667Smh27603 		    cpupm->cur_spd->pm_level, cpupm->head_spd->pm_level));
4054667Smh27603 
4064667Smh27603 		CPUDRV_PM_MONITOR_FINI(cpudsp);
4074667Smh27603 
4084667Smh27603 		if (!cpudrv_direct_pm && (cpupm->cur_spd != cpupm->head_spd)) {
4094667Smh27603 			if (cpupm->pm_busycnt < 1) {
4104667Smh27603 				if ((pm_busy_component(dip, CPUDRV_PM_COMP_NUM)
4114667Smh27603 				    == DDI_SUCCESS)) {
4124667Smh27603 					cpupm->pm_busycnt++;
4134667Smh27603 				} else {
4144667Smh27603 					CPUDRV_PM_MONITOR_INIT(cpudsp);
4154667Smh27603 					mutex_exit(&cpudsp->lock);
4164667Smh27603 					cmn_err(CE_WARN, "cpudrv_detach: "
4174667Smh27603 					    "instance %d: can't busy CPU "
4184667Smh27603 					    "component", instance);
4194667Smh27603 					return (DDI_FAILURE);
4204667Smh27603 				}
4214667Smh27603 			}
4224667Smh27603 			mutex_exit(&cpudsp->lock);
4234667Smh27603 			if (pm_raise_power(dip, CPUDRV_PM_COMP_NUM,
4244667Smh27603 			    cpupm->head_spd->pm_level) != DDI_SUCCESS) {
4254667Smh27603 				mutex_enter(&cpudsp->lock);
4264667Smh27603 				CPUDRV_PM_MONITOR_INIT(cpudsp);
4274667Smh27603 				mutex_exit(&cpudsp->lock);
4284667Smh27603 				cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
4294667Smh27603 				    "can't raise CPU power level", instance);
4304667Smh27603 				return (DDI_FAILURE);
4314667Smh27603 			} else {
4324667Smh27603 				return (DDI_SUCCESS);
4334667Smh27603 			}
4344667Smh27603 		} else {
4354667Smh27603 			mutex_exit(&cpudsp->lock);
4364667Smh27603 			return (DDI_SUCCESS);
4374667Smh27603 		}
4384667Smh27603 
4394667Smh27603 	default:
4404667Smh27603 		return (DDI_FAILURE);
4414667Smh27603 	}
4424667Smh27603 }
4434667Smh27603 
4444667Smh27603 /*
4454667Smh27603  * Driver power(9e) entry point.
4464667Smh27603  *
4474667Smh27603  * Driver's notion of current power is set *only* in power(9e) entry point
4484667Smh27603  * after actual power change operation has been successfully completed.
4494667Smh27603  */
4504667Smh27603 /* ARGSUSED */
4514667Smh27603 static int
4524667Smh27603 cpudrv_power(dev_info_t *dip, int comp, int level)
4534667Smh27603 {
4544667Smh27603 	int			instance;
4554667Smh27603 	cpudrv_devstate_t	*cpudsp;
4564667Smh27603 	cpudrv_pm_t 		*cpupm;
4574667Smh27603 	cpudrv_pm_spd_t		*new_spd;
4584667Smh27603 	boolean_t		is_ready;
4594667Smh27603 	int			ret;
4604667Smh27603 
4614667Smh27603 	instance = ddi_get_instance(dip);
4624667Smh27603 
4634667Smh27603 	DPRINTF(D_POWER, ("cpudrv_power: instance %d: level %d\n",
4644667Smh27603 	    instance, level));
4654667Smh27603 	if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) == NULL) {
4664667Smh27603 		cmn_err(CE_WARN, "cpudrv_power: instance %d: can't get state",
4674667Smh27603 		    instance);
4684667Smh27603 		return (DDI_FAILURE);
4694667Smh27603 	}
4704667Smh27603 
4714667Smh27603 	mutex_enter(&cpudsp->lock);
4724667Smh27603 	cpupm = &(cpudsp->cpudrv_pm);
4734667Smh27603 
4744667Smh27603 	/*
4754667Smh27603 	 * In normal operation, we fail if we are busy and request is
4764667Smh27603 	 * to lower the power level. We let this go through if the driver
4774667Smh27603 	 * is in special direct pm mode. On x86, we also let this through
4784667Smh27603 	 * if the change is due to a request to throttle the max speed.
4794667Smh27603 	 */
4804667Smh27603 	if (!cpudrv_direct_pm && (cpupm->pm_busycnt >= 1) &&
4814718Smh27603 	    !cpudrv_pm_is_throttle_thread(cpupm)) {
4824667Smh27603 		if ((cpupm->cur_spd != NULL) &&
4834667Smh27603 		    (level < cpupm->cur_spd->pm_level)) {
4844667Smh27603 			mutex_exit(&cpudsp->lock);
4854667Smh27603 			return (DDI_FAILURE);
4864667Smh27603 		}
4874667Smh27603 	}
4884667Smh27603 
4894667Smh27603 	for (new_spd = cpupm->head_spd; new_spd; new_spd = new_spd->down_spd) {
4904667Smh27603 		if (new_spd->pm_level == level)
4914667Smh27603 			break;
4924667Smh27603 	}
4934667Smh27603 	if (!new_spd) {
4944667Smh27603 		CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
4954667Smh27603 		mutex_exit(&cpudsp->lock);
4964667Smh27603 		cmn_err(CE_WARN, "cpudrv_power: instance %d: "
4974667Smh27603 		    "can't locate new CPU speed", instance);
4984667Smh27603 		return (DDI_FAILURE);
4994667Smh27603 	}
5004667Smh27603 
5014667Smh27603 	/*
5024667Smh27603 	 * We currently refuse to power manage if the CPU is not ready to
5034667Smh27603 	 * take cross calls (cross calls fail silently if CPU is not ready
5044667Smh27603 	 * for it).
5054667Smh27603 	 *
5064667Smh27603 	 * Additionally, for x86 platforms we cannot power manage
5074667Smh27603 	 * any one instance, until all instances have been initialized.
5084667Smh27603 	 * That's because we don't know what the CPU domains look like
5094667Smh27603 	 * until all instances have been initialized.
5104667Smh27603 	 */
5114667Smh27603 	is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
5124667Smh27603 	if (!is_ready) {
5134667Smh27603 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
5144667Smh27603 		    "CPU not ready for x-calls\n", instance));
5154667Smh27603 	} else if (!(is_ready = cpudrv_pm_all_instances_ready())) {
5164667Smh27603 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
5174667Smh27603 		    "waiting for all CPUs to be ready\n", instance));
5184667Smh27603 	}
5194667Smh27603 	if (!is_ready) {
5204667Smh27603 		CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
5214667Smh27603 		mutex_exit(&cpudsp->lock);
5224667Smh27603 		return (DDI_FAILURE);
5234667Smh27603 	}
5244667Smh27603 
5254667Smh27603 	/*
5264667Smh27603 	 * Execute CPU specific routine on the requested CPU to change its
5274667Smh27603 	 * speed to normal-speed/divisor.
5284667Smh27603 	 */
5294667Smh27603 	if ((ret = cpudrv_pm_change_speed(cpudsp, new_spd)) != DDI_SUCCESS) {
5304667Smh27603 		cmn_err(CE_WARN, "cpudrv_power: cpudrv_pm_change_speed() "
5314667Smh27603 		    "return = %d", ret);
5324667Smh27603 		mutex_exit(&cpudsp->lock);
5334667Smh27603 		return (DDI_FAILURE);
5344667Smh27603 	}
5354667Smh27603 
5364667Smh27603 	/*
5374667Smh27603 	 * Reset idle threshold time for the new power level.
5384667Smh27603 	 */
5394667Smh27603 	if ((cpupm->cur_spd != NULL) && (level < cpupm->cur_spd->pm_level)) {
5404667Smh27603 		if (pm_idle_component(dip, CPUDRV_PM_COMP_NUM) ==
5414667Smh27603 		    DDI_SUCCESS) {
5424667Smh27603 			if (cpupm->pm_busycnt >= 1)
5434667Smh27603 				cpupm->pm_busycnt--;
5444667Smh27603 		} else
5454667Smh27603 			cmn_err(CE_WARN, "cpudrv_power: instance %d: can't "
5464667Smh27603 			    "idle CPU component", ddi_get_instance(dip));
5474667Smh27603 	}
5484667Smh27603 	/*
5494667Smh27603 	 * Reset various parameters because we are now running at new speed.
5504667Smh27603 	 */
5514667Smh27603 	cpupm->lastquan_mstate[CMS_IDLE] = 0;
5524667Smh27603 	cpupm->lastquan_mstate[CMS_SYSTEM] = 0;
5534667Smh27603 	cpupm->lastquan_mstate[CMS_USER] = 0;
5544667Smh27603 	cpupm->lastquan_lbolt = 0;
5554667Smh27603 	cpupm->cur_spd = new_spd;
5564667Smh27603 	CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
5574667Smh27603 	mutex_exit(&cpudsp->lock);
5584667Smh27603 
5594667Smh27603 	return (DDI_SUCCESS);
5604667Smh27603 }
5614667Smh27603 
5624667Smh27603 /*
5634667Smh27603  * Initialize the field that will be used for reporting
5644667Smh27603  * the supported_frequencies_Hz cpu_info kstat.
5654667Smh27603  */
5664667Smh27603 static void
5674667Smh27603 set_supp_freqs(cpu_t *cp, cpudrv_pm_t *cpupm)
5684667Smh27603 {
5694667Smh27603 	char		*supp_freqs;
5704667Smh27603 	char		*sfptr;
5714667Smh27603 	uint64_t	*speeds;
5724667Smh27603 	cpudrv_pm_spd_t	*spd;
5734667Smh27603 	int		i;
5744667Smh27603 #define	UINT64_MAX_STRING (sizeof ("18446744073709551615"))
5754667Smh27603 
5764667Smh27603 	speeds = kmem_zalloc(cpupm->num_spd * sizeof (uint64_t), KM_SLEEP);
5774667Smh27603 	for (i = cpupm->num_spd - 1, spd = cpupm->head_spd; spd;
5784667Smh27603 	    i--, spd = spd->down_spd) {
5794667Smh27603 		speeds[i] =
5804667Smh27603 		    CPUDRV_PM_SPEED_HZ(cp->cpu_type_info.pi_clock, spd->speed);
5814667Smh27603 	}
5824667Smh27603 
5834667Smh27603 	supp_freqs = kmem_zalloc((UINT64_MAX_STRING * cpupm->num_spd),
5844667Smh27603 	    KM_SLEEP);
5854667Smh27603 	sfptr = supp_freqs;
5864667Smh27603 	for (i = 0; i < cpupm->num_spd; i++) {
5874667Smh27603 		if (i == cpupm->num_spd - 1) {
5884667Smh27603 			(void) sprintf(sfptr, "%"PRIu64, speeds[i]);
5894667Smh27603 		} else {
5904667Smh27603 			(void) sprintf(sfptr, "%"PRIu64":", speeds[i]);
5914667Smh27603 			sfptr = supp_freqs + strlen(supp_freqs);
5924667Smh27603 		}
5934667Smh27603 	}
594*4877Smh27603 	cpu_set_supp_freqs(cp, supp_freqs);
595*4877Smh27603 	kmem_free(supp_freqs, (UINT64_MAX_STRING * cpupm->num_spd));
5964667Smh27603 	kmem_free(speeds, cpupm->num_spd * sizeof (uint64_t));
5974667Smh27603 }
5984667Smh27603 
5994667Smh27603 /*
6004667Smh27603  * Initialize power management data.
6014667Smh27603  */
6024667Smh27603 static int
6034667Smh27603 cpudrv_pm_init(cpudrv_devstate_t *cpudsp)
6044667Smh27603 {
6054667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
6064667Smh27603 	cpudrv_pm_spd_t	*cur_spd;
6074667Smh27603 	cpudrv_pm_spd_t	*prev_spd = NULL;
6084667Smh27603 	int		*speeds;
6094667Smh27603 	uint_t		nspeeds;
6104667Smh27603 	int		idle_cnt_percent;
6114667Smh27603 	int		user_cnt_percent;
6124667Smh27603 	int		i;
6134667Smh27603 
6144667Smh27603 	if (!cpudrv_pm_init_module(cpudsp))
6154667Smh27603 		return (DDI_FAILURE);
6164667Smh27603 
6174667Smh27603 	CPUDRV_PM_GET_SPEEDS(cpudsp, speeds, nspeeds);
6184667Smh27603 	if (nspeeds < 2) {
6194667Smh27603 		/* Need at least two speeds to power manage */
6204667Smh27603 		CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
6214667Smh27603 		cpudrv_pm_free_module(cpudsp);
6224667Smh27603 		return (DDI_FAILURE);
6234667Smh27603 	}
6244667Smh27603 	cpupm->num_spd = nspeeds;
6254667Smh27603 
6264667Smh27603 	/*
6274667Smh27603 	 * Calculate the watermarks and other parameters based on the
6284667Smh27603 	 * supplied speeds.
6294667Smh27603 	 *
6304667Smh27603 	 * One of the basic assumption is that for X amount of CPU work,
6314667Smh27603 	 * if CPU is slowed down by a factor of N, the time it takes to
6324667Smh27603 	 * do the same work will be N * X.
6334667Smh27603 	 *
6344667Smh27603 	 * The driver declares that a CPU is idle and ready for slowed down,
6354667Smh27603 	 * if amount of idle thread is more than the current speed idle_hwm
6364667Smh27603 	 * without dropping below idle_hwm a number of consecutive sampling
6374667Smh27603 	 * intervals and number of running threads in user mode are below
6384667Smh27603 	 * user_lwm.  We want to set the current user_lwm such that if we
6394667Smh27603 	 * just switched to the next slower speed with no change in real work
6404667Smh27603 	 * load, the amount of user threads at the slower speed will be such
6414667Smh27603 	 * that it falls below the slower speed's user_hwm.  If we didn't do
6424667Smh27603 	 * that then we will just come back to the higher speed as soon as we
6434667Smh27603 	 * go down even with no change in work load.
6444667Smh27603 	 * The user_hwm is a fixed precentage and not calculated dynamically.
6454667Smh27603 	 *
6464667Smh27603 	 * We bring the CPU up if idle thread at current speed is less than
6474667Smh27603 	 * the current speed idle_lwm for a number of consecutive sampling
6484667Smh27603 	 * intervals or user threads are above the user_hwm for the current
6494667Smh27603 	 * speed.
6504667Smh27603 	 */
6514667Smh27603 	for (i = 0; i < nspeeds; i++) {
6524667Smh27603 		cur_spd = kmem_zalloc(sizeof (cpudrv_pm_spd_t), KM_SLEEP);
6534667Smh27603 		cur_spd->speed = speeds[i];
6544667Smh27603 		if (i == 0) {	/* normal speed */
6554667Smh27603 			cpupm->head_spd = cur_spd;
6564667Smh27603 			cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_NORMAL;
6574667Smh27603 			cur_spd->idle_hwm =
6584667Smh27603 			    (cpudrv_pm_idle_hwm * cur_spd->quant_cnt) / 100;
6594667Smh27603 			/* can't speed anymore */
6604667Smh27603 			cur_spd->idle_lwm = 0;
6614667Smh27603 			cur_spd->user_hwm = UINT_MAX;
6624667Smh27603 		} else {
6634667Smh27603 			cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_OTHR;
6644667Smh27603 			ASSERT(prev_spd != NULL);
6654667Smh27603 			prev_spd->down_spd = cur_spd;
6664667Smh27603 			cur_spd->up_spd = cpupm->head_spd;
6674667Smh27603 
6684667Smh27603 			/*
6694667Smh27603 			 * Let's assume CPU is considered idle at full speed
6704667Smh27603 			 * when it is spending I% of time in running the idle
6714667Smh27603 			 * thread.  At full speed, CPU will be busy (100 - I) %
6724667Smh27603 			 * of times.  This % of busyness increases by factor of
6734667Smh27603 			 * N as CPU slows down.  CPU that is idle I% of times
6744667Smh27603 			 * in full speed, it is idle (100 - ((100 - I) * N)) %
6754667Smh27603 			 * of times in N speed.  The idle_lwm is a fixed
6764667Smh27603 			 * percentage.  A large value of N may result in
6774667Smh27603 			 * idle_hwm to go below idle_lwm.  We need to make sure
6784667Smh27603 			 * that there is at least a buffer zone seperation
6794667Smh27603 			 * between the idle_lwm and idle_hwm values.
6804667Smh27603 			 */
6814667Smh27603 			idle_cnt_percent = CPUDRV_PM_IDLE_CNT_PERCENT(
6824667Smh27603 			    cpudrv_pm_idle_hwm, speeds, i);
6834667Smh27603 			idle_cnt_percent = max(idle_cnt_percent,
6844667Smh27603 			    (cpudrv_pm_idle_lwm + cpudrv_pm_idle_buf_zone));
6854667Smh27603 			cur_spd->idle_hwm =
6864667Smh27603 			    (idle_cnt_percent * cur_spd->quant_cnt) / 100;
6874667Smh27603 			cur_spd->idle_lwm =
6884667Smh27603 			    (cpudrv_pm_idle_lwm * cur_spd->quant_cnt) / 100;
6894667Smh27603 
6904667Smh27603 			/*
6914667Smh27603 			 * The lwm for user threads are determined such that
6924667Smh27603 			 * if CPU slows down, the load of work in the
6934667Smh27603 			 * new speed would still keep the CPU at or below the
6944667Smh27603 			 * user_hwm in the new speed.  This is to prevent
6954667Smh27603 			 * the quick jump back up to higher speed.
6964667Smh27603 			 */
6974667Smh27603 			cur_spd->user_hwm = (cpudrv_pm_user_hwm *
6984667Smh27603 			    cur_spd->quant_cnt) / 100;
6994667Smh27603 			user_cnt_percent = CPUDRV_PM_USER_CNT_PERCENT(
7004667Smh27603 			    cpudrv_pm_user_hwm, speeds, i);
7014667Smh27603 			prev_spd->user_lwm =
7024667Smh27603 			    (user_cnt_percent * prev_spd->quant_cnt) / 100;
7034667Smh27603 		}
7044667Smh27603 		prev_spd = cur_spd;
7054667Smh27603 	}
7064667Smh27603 	/* Slowest speed. Can't slow down anymore */
7074667Smh27603 	cur_spd->idle_hwm = UINT_MAX;
7084667Smh27603 	cur_spd->user_lwm = -1;
7094667Smh27603 #ifdef	DEBUG
7104667Smh27603 	DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: head_spd spd %d, "
7114667Smh27603 	    "num_spd %d\n", ddi_get_instance(cpudsp->dip),
7124667Smh27603 	    cpupm->head_spd->speed, cpupm->num_spd));
7134667Smh27603 	for (cur_spd = cpupm->head_spd; cur_spd; cur_spd = cur_spd->down_spd) {
7144667Smh27603 		DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: speed %d, "
7154667Smh27603 		    "down_spd spd %d, idle_hwm %d, user_lwm %d, "
7164667Smh27603 		    "up_spd spd %d, idle_lwm %d, user_hwm %d, "
7174667Smh27603 		    "quant_cnt %d\n", ddi_get_instance(cpudsp->dip),
7184667Smh27603 		    cur_spd->speed,
7194667Smh27603 		    (cur_spd->down_spd ? cur_spd->down_spd->speed : 0),
7204667Smh27603 		    cur_spd->idle_hwm, cur_spd->user_lwm,
7214667Smh27603 		    (cur_spd->up_spd ? cur_spd->up_spd->speed : 0),
7224667Smh27603 		    cur_spd->idle_lwm, cur_spd->user_hwm,
7234667Smh27603 		    cur_spd->quant_cnt));
7244667Smh27603 	}
7254667Smh27603 #endif	/* DEBUG */
7264667Smh27603 	CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
7274667Smh27603 	return (DDI_SUCCESS);
7284667Smh27603 }
7294667Smh27603 
7304667Smh27603 /*
7314667Smh27603  * Free CPU power management data.
7324667Smh27603  */
7334667Smh27603 static void
7344667Smh27603 cpudrv_pm_free(cpudrv_devstate_t *cpudsp)
7354667Smh27603 {
7364667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
7374667Smh27603 	cpudrv_pm_spd_t	*cur_spd, *next_spd;
7384667Smh27603 
7394667Smh27603 	cur_spd = cpupm->head_spd;
7404667Smh27603 	while (cur_spd) {
7414667Smh27603 		next_spd = cur_spd->down_spd;
7424667Smh27603 		kmem_free(cur_spd, sizeof (cpudrv_pm_spd_t));
7434667Smh27603 		cur_spd = next_spd;
7444667Smh27603 	}
7454667Smh27603 	bzero(cpupm, sizeof (cpudrv_pm_t));
7464667Smh27603 	cpudrv_pm_free_module(cpudsp);
7474667Smh27603 }
7484667Smh27603 
7494667Smh27603 /*
7504667Smh27603  * Create pm-components property.
7514667Smh27603  */
7524667Smh27603 static int
7534667Smh27603 cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp)
7544667Smh27603 {
7554667Smh27603 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
7564667Smh27603 	cpudrv_pm_spd_t	*cur_spd;
7574667Smh27603 	char		**pmc;
7584667Smh27603 	int		size;
7594667Smh27603 	char		name[] = "NAME=CPU Speed";
7604667Smh27603 	int		i, j;
7614667Smh27603 	uint_t		comp_spd;
7624667Smh27603 	int		result = DDI_FAILURE;
7634667Smh27603 
7644667Smh27603 	pmc = kmem_zalloc((cpupm->num_spd + 1) * sizeof (char *), KM_SLEEP);
7654667Smh27603 	size = CPUDRV_PM_COMP_SIZE();
7664667Smh27603 	if (cpupm->num_spd > CPUDRV_PM_COMP_MAX_VAL) {
7674667Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
7684667Smh27603 		    "number of speeds exceeded limits",
7694667Smh27603 		    ddi_get_instance(cpudsp->dip));
7704667Smh27603 		kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
7714667Smh27603 		return (result);
7724667Smh27603 	}
7734667Smh27603 
7744667Smh27603 	for (i = cpupm->num_spd, cur_spd = cpupm->head_spd; i > 0;
7754667Smh27603 	    i--, cur_spd = cur_spd->down_spd) {
7764667Smh27603 		cur_spd->pm_level = i;
7774667Smh27603 		pmc[i] = kmem_zalloc((size * sizeof (char)), KM_SLEEP);
7784667Smh27603 		comp_spd = CPUDRV_PM_COMP_SPEED(cpupm, cur_spd);
7794667Smh27603 		if (comp_spd > CPUDRV_PM_COMP_MAX_VAL) {
7804667Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_comp_create: "
7814667Smh27603 			    "instance %d: speed exceeded limits",
7824667Smh27603 			    ddi_get_instance(cpudsp->dip));
7834667Smh27603 			for (j = cpupm->num_spd; j >= i; j--) {
7844667Smh27603 				kmem_free(pmc[j], size * sizeof (char));
7854667Smh27603 			}
7864667Smh27603 			kmem_free(pmc, (cpupm->num_spd + 1) *
7874667Smh27603 			    sizeof (char *));
7884667Smh27603 			return (result);
7894667Smh27603 		}
7904667Smh27603 		CPUDRV_PM_COMP_SPRINT(pmc[i], cpupm, cur_spd, comp_spd)
7914667Smh27603 		DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: "
7924667Smh27603 		    "instance %d: pm-components power level %d string '%s'\n",
7934667Smh27603 		    ddi_get_instance(cpudsp->dip), i, pmc[i]));
7944667Smh27603 	}
7954667Smh27603 	pmc[0] = kmem_zalloc(sizeof (name), KM_SLEEP);
7964667Smh27603 	(void) strcat(pmc[0], name);
7974667Smh27603 	DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: instance %d: "
7984667Smh27603 	    "pm-components component name '%s'\n",
7994667Smh27603 	    ddi_get_instance(cpudsp->dip), pmc[0]));
8004667Smh27603 
8014667Smh27603 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, cpudsp->dip,
8024667Smh27603 	    "pm-components", pmc, cpupm->num_spd + 1) == DDI_PROP_SUCCESS) {
8034667Smh27603 		result = DDI_SUCCESS;
8044667Smh27603 	} else {
8054667Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
8064667Smh27603 		    "can't create pm-components property",
8074667Smh27603 		    ddi_get_instance(cpudsp->dip));
8084667Smh27603 	}
8094667Smh27603 
8104667Smh27603 	for (i = cpupm->num_spd; i > 0; i--) {
8114667Smh27603 		kmem_free(pmc[i], size * sizeof (char));
8124667Smh27603 	}
8134667Smh27603 	kmem_free(pmc[0], sizeof (name));
8144667Smh27603 	kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
8154667Smh27603 	return (result);
8164667Smh27603 }
8174667Smh27603 
8184667Smh27603 /*
8194667Smh27603  * Mark a component idle.
8204667Smh27603  */
8214667Smh27603 #define	CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm) { \
8224667Smh27603 	if ((cpupm)->pm_busycnt >= 1) { \
8234667Smh27603 		if (pm_idle_component((dip), CPUDRV_PM_COMP_NUM) == \
8244667Smh27603 		    DDI_SUCCESS) { \
8254667Smh27603 			DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
8264667Smh27603 			    "instance %d: pm_idle_component called\n", \
8274667Smh27603 			    ddi_get_instance((dip)))); \
8284667Smh27603 			(cpupm)->pm_busycnt--; \
8294667Smh27603 		} else { \
8304667Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
8314667Smh27603 			    "can't idle CPU component", \
8324667Smh27603 			    ddi_get_instance((dip))); \
8334667Smh27603 		} \
8344667Smh27603 	} \
8354667Smh27603 }
8364667Smh27603 
8374667Smh27603 /*
8384667Smh27603  * Marks a component busy in both PM framework and driver state structure.
8394667Smh27603  */
8404667Smh27603 #define	CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm) { \
8414667Smh27603 	if ((cpupm)->pm_busycnt < 1) { \
8424667Smh27603 		if (pm_busy_component((dip), CPUDRV_PM_COMP_NUM) == \
8434667Smh27603 		    DDI_SUCCESS) { \
8444667Smh27603 			DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
8454667Smh27603 			    "instance %d: pm_busy_component called\n", \
8464667Smh27603 			    ddi_get_instance((dip)))); \
8474667Smh27603 			(cpupm)->pm_busycnt++; \
8484667Smh27603 		} else { \
8494667Smh27603 			cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
8504667Smh27603 			    "can't busy CPU component", \
8514667Smh27603 			    ddi_get_instance((dip))); \
8524667Smh27603 		} \
8534667Smh27603 	} \
8544667Smh27603 }
8554667Smh27603 
8564667Smh27603 /*
8574667Smh27603  * Marks a component busy and calls pm_raise_power().
8584667Smh27603  */
8594667Smh27603 #define	CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm, new_level) { \
8604667Smh27603 	/* \
8614667Smh27603 	 * Mark driver and PM framework busy first so framework doesn't try \
8624667Smh27603 	 * to bring CPU to lower speed when we need to be at higher speed. \
8634667Smh27603 	 */ \
8644667Smh27603 	CPUDRV_PM_MONITOR_PM_BUSY_COMP((dip), (cpupm)); \
8654667Smh27603 	mutex_exit(&(cpudsp)->lock); \
8664667Smh27603 	DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: " \
8674667Smh27603 	    "pm_raise_power called to %d\n", ddi_get_instance((dip)), \
8684667Smh27603 		(new_level))); \
8694667Smh27603 	if (pm_raise_power((dip), CPUDRV_PM_COMP_NUM, (new_level)) != \
8704667Smh27603 	    DDI_SUCCESS) { \
8714667Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't " \
8724667Smh27603 		    "raise CPU power level", ddi_get_instance((dip))); \
8734667Smh27603 	} \
8744667Smh27603 	mutex_enter(&(cpudsp)->lock); \
8754667Smh27603 }
8764667Smh27603 
8774667Smh27603 /*
8784667Smh27603  * In order to monitor a CPU, we need to hold cpu_lock to access CPU
8794667Smh27603  * statistics. Holding cpu_lock is not allowed from a callout routine.
8804667Smh27603  * We dispatch a taskq to do that job.
8814667Smh27603  */
8824667Smh27603 static void
8834667Smh27603 cpudrv_pm_monitor_disp(void *arg)
8844667Smh27603 {
8854667Smh27603 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
8864667Smh27603 
8874667Smh27603 	/*
8884667Smh27603 	 * We are here because the last task has scheduled a timeout.
8894667Smh27603 	 * The queue should be empty at this time.
8904667Smh27603 	 */
8914667Smh27603 	mutex_enter(&cpudsp->cpudrv_pm.timeout_lock);
8924667Smh27603 	if (!taskq_dispatch(cpudsp->cpudrv_pm.tq, cpudrv_pm_monitor, arg,
8934667Smh27603 	    TQ_NOSLEEP)) {
8944667Smh27603 		mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
8954667Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor_disp: failed to "
8964667Smh27603 		    "dispatch the cpudrv_pm_monitor taskq\n"));
8974667Smh27603 		mutex_enter(&cpudsp->lock);
8984667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
8994667Smh27603 		mutex_exit(&cpudsp->lock);
9004667Smh27603 		return;
9014667Smh27603 	}
9024667Smh27603 	cpudsp->cpudrv_pm.timeout_count++;
9034667Smh27603 	mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
9044667Smh27603 }
9054667Smh27603 
9064667Smh27603 /*
9074667Smh27603  * Get current CPU microstate times and scale them. We should probably be
9084667Smh27603  * using get_cpu_mstate() to get this data, but bugs in some of the ISRs
9094667Smh27603  * have led to inflated system times and prevented CPUs from being power
9104667Smh27603  * managed. We can probably safely ignore time spent in ISRs when
9114667Smh27603  * determining idleness.
9124667Smh27603  */
9134667Smh27603 static void
9144667Smh27603 cpudrv_get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
9154667Smh27603 {
9164667Smh27603 	int i;
9174667Smh27603 
9184667Smh27603 	for (i = 0; i < NCMSTATES; i++) {
9194667Smh27603 		times[i] = cpu->cpu_acct[i];
9204667Smh27603 		scalehrtime(&times[i]);
9214667Smh27603 	}
9224667Smh27603 }
9234667Smh27603 
9244667Smh27603 /*
9254667Smh27603  * Monitors each CPU for the amount of time idle thread was running in the
9264667Smh27603  * last quantum and arranges for the CPU to go to the lower or higher speed.
9274667Smh27603  * Called at the time interval appropriate for the current speed. The
9284667Smh27603  * time interval for normal speed is CPUDRV_PM_QUANT_CNT_NORMAL. The time
9294667Smh27603  * interval for other speeds (including unknown speed) is
9304667Smh27603  * CPUDRV_PM_QUANT_CNT_OTHR.
9314667Smh27603  */
9324667Smh27603 static void
9334667Smh27603 cpudrv_pm_monitor(void *arg)
9344667Smh27603 {
9354667Smh27603 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
9364667Smh27603 	cpudrv_pm_t		*cpupm;
9374667Smh27603 	cpudrv_pm_spd_t		*cur_spd, *new_spd;
9384667Smh27603 	cpu_t			*cp;
9394667Smh27603 	dev_info_t		*dip;
9404667Smh27603 	uint_t			idle_cnt, user_cnt, system_cnt;
9414667Smh27603 	clock_t			lbolt_cnt;
9424667Smh27603 	hrtime_t		msnsecs[NCMSTATES];
9434667Smh27603 	boolean_t		is_ready;
9444667Smh27603 
9454667Smh27603 #define	GET_CPU_MSTATE_CNT(state, cnt) \
9464667Smh27603 	msnsecs[state] = NSEC_TO_TICK(msnsecs[state]); \
9474667Smh27603 	if (cpupm->lastquan_mstate[state] > msnsecs[state]) \
9484667Smh27603 		msnsecs[state] = cpupm->lastquan_mstate[state]; \
9494667Smh27603 	cnt = msnsecs[state] - cpupm->lastquan_mstate[state]; \
9504667Smh27603 	cpupm->lastquan_mstate[state] = msnsecs[state]
9514667Smh27603 
9524667Smh27603 	mutex_enter(&cpudsp->lock);
9534667Smh27603 	cpupm = &(cpudsp->cpudrv_pm);
9544667Smh27603 	if (cpupm->timeout_id == 0) {
9554667Smh27603 		mutex_exit(&cpudsp->lock);
9564667Smh27603 		goto do_return;
9574667Smh27603 	}
9584667Smh27603 	cur_spd = cpupm->cur_spd;
9594667Smh27603 	dip = cpudsp->dip;
9604667Smh27603 
9614667Smh27603 	/*
9624667Smh27603 	 * We assume that a CPU is initialized and has a valid cpu_t
9634667Smh27603 	 * structure, if it is ready for cross calls. If this changes,
9644667Smh27603 	 * additional checks might be needed.
9654667Smh27603 	 *
9664667Smh27603 	 * Additionally, for x86 platforms we cannot power manage
9674667Smh27603 	 * any one instance, until all instances have been initialized.
9684667Smh27603 	 * That's because we don't know what the CPU domains look like
9694667Smh27603 	 * until all instances have been initialized.
9704667Smh27603 	 */
9714667Smh27603 	is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
9724667Smh27603 	if (!is_ready) {
9734667Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
9744667Smh27603 		    "CPU not ready for x-calls\n", ddi_get_instance(dip)));
9754667Smh27603 	} else if (!(is_ready = cpudrv_pm_all_instances_ready())) {
9764667Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
9774667Smh27603 		    "waiting for all CPUs to be ready\n",
9784667Smh27603 		    ddi_get_instance(dip)));
9794667Smh27603 	}
9804667Smh27603 	if (!is_ready) {
9814667Smh27603 		/*
9824667Smh27603 		 * Make sure that we are busy so that framework doesn't
9834667Smh27603 		 * try to bring us down in this situation.
9844667Smh27603 		 */
9854667Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
9864667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
9874667Smh27603 		mutex_exit(&cpudsp->lock);
9884667Smh27603 		goto do_return;
9894667Smh27603 	}
9904667Smh27603 
9914667Smh27603 	/*
9924667Smh27603 	 * Make sure that we are still not at unknown power level.
9934667Smh27603 	 */
9944667Smh27603 	if (cur_spd == NULL) {
9954667Smh27603 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
9964667Smh27603 		    "cur_spd is unknown\n", ddi_get_instance(dip)));
9974667Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
9984667Smh27603 		    cpupm->targ_spd->pm_level);
9994667Smh27603 		/*
10004667Smh27603 		 * We just changed the speed. Wait till at least next
10014667Smh27603 		 * call to this routine before proceeding ahead.
10024667Smh27603 		 */
10034667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10044667Smh27603 		mutex_exit(&cpudsp->lock);
10054667Smh27603 		goto do_return;
10064667Smh27603 	}
10074667Smh27603 
10084667Smh27603 	mutex_enter(&cpu_lock);
10094667Smh27603 	if ((cp = cpu_get(cpudsp->cpu_id)) == NULL) {
10104667Smh27603 		mutex_exit(&cpu_lock);
10114667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10124667Smh27603 		mutex_exit(&cpudsp->lock);
10134667Smh27603 		cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't get "
10144667Smh27603 		    "cpu_t", ddi_get_instance(dip));
10154667Smh27603 		goto do_return;
10164667Smh27603 	}
1017*4877Smh27603 
1018*4877Smh27603 	if (!cpupm->pm_started) {
1019*4877Smh27603 		cpupm->pm_started = B_TRUE;
10204667Smh27603 		set_supp_freqs(cp, cpupm);
1021*4877Smh27603 	}
10224667Smh27603 
10234667Smh27603 	cpudrv_get_cpu_mstate(cp, msnsecs);
10244667Smh27603 	GET_CPU_MSTATE_CNT(CMS_IDLE, idle_cnt);
10254667Smh27603 	GET_CPU_MSTATE_CNT(CMS_USER, user_cnt);
10264667Smh27603 	GET_CPU_MSTATE_CNT(CMS_SYSTEM, system_cnt);
10274667Smh27603 
10284667Smh27603 	/*
10294667Smh27603 	 * We can't do anything when we have just switched to a state
10304667Smh27603 	 * because there is no valid timestamp.
10314667Smh27603 	 */
10324667Smh27603 	if (cpupm->lastquan_lbolt == 0) {
10334667Smh27603 		cpupm->lastquan_lbolt = lbolt;
10344667Smh27603 		mutex_exit(&cpu_lock);
10354667Smh27603 		CPUDRV_PM_MONITOR_INIT(cpudsp);
10364667Smh27603 		mutex_exit(&cpudsp->lock);
10374667Smh27603 		goto do_return;
10384667Smh27603 	}
10394667Smh27603 
10404667Smh27603 	/*
10414667Smh27603 	 * Various watermarks are based on this routine being called back
10424667Smh27603 	 * exactly at the requested period. This is not guaranteed
10434667Smh27603 	 * because this routine is called from a taskq that is dispatched
10444667Smh27603 	 * from a timeout routine.  Handle this by finding out how many
10454667Smh27603 	 * ticks have elapsed since the last call (lbolt_cnt) and adjusting
10464667Smh27603 	 * the idle_cnt based on the delay added to the requested period
10474667Smh27603 	 * by timeout and taskq.
10484667Smh27603 	 */
10494667Smh27603 	lbolt_cnt = lbolt - cpupm->lastquan_lbolt;
10504667Smh27603 	cpupm->lastquan_lbolt = lbolt;
10514667Smh27603 	mutex_exit(&cpu_lock);
10524667Smh27603 	/*
10534667Smh27603 	 * Time taken between recording the current counts and
10544667Smh27603 	 * arranging the next call of this routine is an error in our
10554667Smh27603 	 * calculation. We minimize the error by calling
10564667Smh27603 	 * CPUDRV_PM_MONITOR_INIT() here instead of end of this routine.
10574667Smh27603 	 */
10584667Smh27603 	CPUDRV_PM_MONITOR_INIT(cpudsp);
10594667Smh27603 	DPRINTF(D_PM_MONITOR_VERBOSE, ("cpudrv_pm_monitor: instance %d: "
10604667Smh27603 	    "idle count %d, user count %d, system count %d, pm_level %d, "
10614667Smh27603 	    "pm_busycnt %d\n", ddi_get_instance(dip), idle_cnt, user_cnt,
10624667Smh27603 	    system_cnt, cur_spd->pm_level, cpupm->pm_busycnt));
10634667Smh27603 
10644667Smh27603 #ifdef	DEBUG
10654667Smh27603 	/*
10664667Smh27603 	 * Notify that timeout and taskq has caused delays and we need to
10674667Smh27603 	 * scale our parameters accordingly.
10684667Smh27603 	 *
10694667Smh27603 	 * To get accurate result, don't turn on other DPRINTFs with
10704667Smh27603 	 * the following DPRINTF. PROM calls generated by other
10714667Smh27603 	 * DPRINTFs changes the timing.
10724667Smh27603 	 */
10734667Smh27603 	if (lbolt_cnt > cur_spd->quant_cnt) {
10744667Smh27603 		DPRINTF(D_PM_MONITOR_DELAY, ("cpudrv_pm_monitor: instance %d: "
10754667Smh27603 		    "lbolt count %ld > quantum_count %u\n",
10764667Smh27603 		    ddi_get_instance(dip), lbolt_cnt, cur_spd->quant_cnt));
10774667Smh27603 	}
10784667Smh27603 #endif	/* DEBUG */
10794667Smh27603 
10804667Smh27603 	/*
10814667Smh27603 	 * Adjust counts based on the delay added by timeout and taskq.
10824667Smh27603 	 */
10834667Smh27603 	idle_cnt = (idle_cnt * cur_spd->quant_cnt) / lbolt_cnt;
10844667Smh27603 	user_cnt = (user_cnt * cur_spd->quant_cnt) / lbolt_cnt;
10854667Smh27603 	if ((user_cnt > cur_spd->user_hwm) || (idle_cnt < cur_spd->idle_lwm &&
10864667Smh27603 	    cur_spd->idle_blwm_cnt >= cpudrv_pm_idle_blwm_cnt_max)) {
10874667Smh27603 		cur_spd->idle_blwm_cnt = 0;
10884667Smh27603 		cur_spd->idle_bhwm_cnt = 0;
10894667Smh27603 		/*
10904667Smh27603 		 * In normal situation, arrange to go to next higher speed.
10914667Smh27603 		 * If we are running in special direct pm mode, we just stay
10924667Smh27603 		 * at the current speed.
10934667Smh27603 		 */
10944667Smh27603 		if (cur_spd == cur_spd->up_spd || cpudrv_direct_pm) {
10954667Smh27603 			CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
10964667Smh27603 		} else {
10974667Smh27603 			new_spd = cur_spd->up_spd;
10984667Smh27603 			CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
10994667Smh27603 			    new_spd->pm_level);
11004667Smh27603 		}
11014667Smh27603 	} else if ((user_cnt <= cur_spd->user_lwm) &&
11024667Smh27603 	    (idle_cnt >= cur_spd->idle_hwm) || !CPU_ACTIVE(cp)) {
11034667Smh27603 		cur_spd->idle_blwm_cnt = 0;
11044667Smh27603 		cur_spd->idle_bhwm_cnt = 0;
11054667Smh27603 		/*
11064667Smh27603 		 * Arrange to go to next lower speed by informing our idle
11074667Smh27603 		 * status to the power management framework.
11084667Smh27603 		 */
11094667Smh27603 		CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm);
11104667Smh27603 	} else {
11114667Smh27603 		/*
11124667Smh27603 		 * If we are between the idle water marks and have not
11134667Smh27603 		 * been here enough consecutive times to be considered
11144667Smh27603 		 * busy, just increment the count and return.
11154667Smh27603 		 */
11164667Smh27603 		if ((idle_cnt < cur_spd->idle_hwm) &&
11174667Smh27603 		    (idle_cnt >= cur_spd->idle_lwm) &&
11184667Smh27603 		    (cur_spd->idle_bhwm_cnt < cpudrv_pm_idle_bhwm_cnt_max)) {
11194667Smh27603 			cur_spd->idle_blwm_cnt = 0;
11204667Smh27603 			cur_spd->idle_bhwm_cnt++;
11214667Smh27603 			mutex_exit(&cpudsp->lock);
11224667Smh27603 			goto do_return;
11234667Smh27603 		}
11244667Smh27603 		if (idle_cnt < cur_spd->idle_lwm) {
11254667Smh27603 			cur_spd->idle_blwm_cnt++;
11264667Smh27603 			cur_spd->idle_bhwm_cnt = 0;
11274667Smh27603 		}
11284667Smh27603 		/*
11294667Smh27603 		 * Arranges to stay at the current speed.
11304667Smh27603 		 */
11314667Smh27603 		CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
11324667Smh27603 	}
11334667Smh27603 	mutex_exit(&cpudsp->lock);
11344667Smh27603 do_return:
11354667Smh27603 	mutex_enter(&cpupm->timeout_lock);
11364667Smh27603 	ASSERT(cpupm->timeout_count > 0);
11374667Smh27603 	cpupm->timeout_count--;
11384667Smh27603 	cv_signal(&cpupm->timeout_cv);
11394667Smh27603 	mutex_exit(&cpupm->timeout_lock);
11404667Smh27603 }
1141