xref: /titanic_50/usr/src/uts/common/io/cpudrv.c (revision f9edfba4e2ac2531f0e52d0c4007976054e1023d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * CPU Device driver. The driver is not DDI-compliant.
30  *
31  * The driver supports following features:
32  *	- Power management.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/errno.h>
38 #include <sys/modctl.h>
39 #include <sys/kmem.h>
40 #include <sys/conf.h>
41 #include <sys/cmn_err.h>
42 #include <sys/stat.h>
43 #include <sys/debug.h>
44 #include <sys/systm.h>
45 #include <sys/ddi.h>
46 #include <sys/sunddi.h>
47 
48 #include <sys/machsystm.h>
49 #include <sys/x_call.h>
50 #include <sys/cpudrv.h>
51 #include <sys/cpudrv_plat.h>
52 #include <sys/msacct.h>
53 
54 /*
55  * CPU power management
56  *
57  * The supported power saving model is to slow down the CPU (on SPARC by
58  * dividing the CPU clock and on x86 by dropping down a P-state).
59  * Periodically we determine the amount of time the CPU is running
60  * idle thread and threads in user mode during the last quantum.  If the idle
61  * thread was running less than its low water mark for current speed for
62  * number of consecutive sampling periods, or number of running threads in
63  * user mode are above its high water mark, we arrange to go to the higher
64  * speed.  If the idle thread was running more than its high water mark without
65  * dropping a number of consecutive times below the mark, and number of threads
66  * running in user mode are below its low water mark, we arrange to go to the
67  * next lower speed.  While going down, we go through all the speeds.  While
68  * going up we go to the maximum speed to minimize impact on the user, but have
69  * provisions in the driver to go to other speeds.
70  *
71  * The driver does not have knowledge of a particular implementation of this
72  * scheme and will work with all CPUs supporting this model. On SPARC, the
73  * driver determines supported speeds by looking at 'clock-divisors' property
74  * created by OBP. On x86, the driver retrieves the supported speeds from
75  * ACPI.
76  */
77 
78 /*
79  * Configuration function prototypes and data structures
80  */
81 static int cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
82 static int cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
83 static int cpudrv_power(dev_info_t *dip, int comp, int level);
84 
85 struct dev_ops cpudrv_ops = {
86 	DEVO_REV,		/* rev */
87 	0,			/* refcnt */
88 	nodev,			/* getinfo */
89 	nulldev,		/* identify */
90 	nulldev,		/* probe */
91 	cpudrv_attach,		/* attach */
92 	cpudrv_detach,		/* detach */
93 	nodev,			/* reset */
94 	(struct cb_ops *)NULL,	/* cb_ops */
95 	(struct bus_ops *)NULL,	/* bus_ops */
96 	cpudrv_power		/* power */
97 };
98 
99 static struct modldrv modldrv = {
100 	&mod_driverops,			/* modops */
101 	"CPU Driver %I%",		/* linkinfo */
102 	&cpudrv_ops,			/* dev_ops */
103 };
104 
105 static struct modlinkage modlinkage = {
106 	MODREV_1,		/* rev */
107 	&modldrv,		/* linkage */
108 	NULL
109 };
110 
111 /*
112  * Function prototypes
113  */
114 static int cpudrv_pm_init(cpudrv_devstate_t *cpudsp);
115 static void cpudrv_pm_free(cpudrv_devstate_t *cpudsp);
116 static int cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp);
117 static void cpudrv_pm_monitor_disp(void *arg);
118 static void cpudrv_pm_monitor(void *arg);
119 
120 /*
121  * Driver global variables
122  */
123 uint_t cpudrv_debug = 0;
124 void *cpudrv_state;
125 static uint_t cpudrv_pm_idle_hwm = CPUDRV_PM_IDLE_HWM;
126 static uint_t cpudrv_pm_idle_lwm = CPUDRV_PM_IDLE_LWM;
127 static uint_t cpudrv_pm_idle_buf_zone = CPUDRV_PM_IDLE_BUF_ZONE;
128 static uint_t cpudrv_pm_idle_bhwm_cnt_max = CPUDRV_PM_IDLE_BHWM_CNT_MAX;
129 static uint_t cpudrv_pm_idle_blwm_cnt_max = CPUDRV_PM_IDLE_BLWM_CNT_MAX;
130 static uint_t cpudrv_pm_user_hwm = CPUDRV_PM_USER_HWM;
131 
132 /*
133  * cpudrv_direct_pm allows user applications to directly control the
134  * power state transitions (direct pm) without following the normal
135  * direct pm protocol. This is needed because the normal protocol
136  * requires that a device only be lowered when it is idle, and be
137  * brought up when it request to do so by calling pm_raise_power().
138  * Ignoring this protocol is harmless for CPU (other than speed).
139  * Moreover it might be the case that CPU is never idle or wants
140  * to be at higher speed because of the addition CPU cycles required
141  * to run the user application.
142  *
143  * The driver will still report idle/busy status to the framework. Although
144  * framework will ignore this information for direct pm devices and not
145  * try to bring them down when idle, user applications can still use this
146  * information if they wants.
147  *
148  * In the future, provide an ioctl to control setting of this mode. In
149  * that case, this variable should move to the state structure and
150  * be protected by the lock in the state structure.
151  */
152 int cpudrv_direct_pm = 0;
153 
154 /*
155  * Arranges for the handler function to be called at the interval suitable
156  * for current speed.
157  */
158 #define	CPUDRV_PM_MONITOR_INIT(cpudsp) { \
159 	ASSERT(mutex_owned(&(cpudsp)->lock)); \
160 	(cpudsp)->cpudrv_pm.timeout_id = timeout(cpudrv_pm_monitor_disp, \
161 	    (cpudsp), (((cpudsp)->cpudrv_pm.cur_spd == NULL) ? \
162 	    CPUDRV_PM_QUANT_CNT_OTHR : \
163 	    (cpudsp)->cpudrv_pm.cur_spd->quant_cnt)); \
164 }
165 
166 /*
167  * Arranges for the handler function not to be called back.
168  */
169 #define	CPUDRV_PM_MONITOR_FINI(cpudsp) { \
170 	timeout_id_t tmp_tid; \
171 	ASSERT(mutex_owned(&(cpudsp)->lock)); \
172 	ASSERT((cpudsp)->cpudrv_pm.timeout_id); \
173 	tmp_tid = (cpudsp)->cpudrv_pm.timeout_id; \
174 	(cpudsp)->cpudrv_pm.timeout_id = 0; \
175 	mutex_exit(&(cpudsp)->lock); \
176 	(void) untimeout(tmp_tid); \
177 	mutex_enter(&(cpudsp)->cpudrv_pm.timeout_lock); \
178 	while ((cpudsp)->cpudrv_pm.timeout_count != 0) \
179 		cv_wait(&(cpudsp)->cpudrv_pm.timeout_cv, \
180 		    &(cpudsp)->cpudrv_pm.timeout_lock); \
181 	mutex_exit(&(cpudsp)->cpudrv_pm.timeout_lock); \
182 	mutex_enter(&(cpudsp)->lock); \
183 }
184 
185 int
186 _init(void)
187 {
188 	int	error;
189 
190 	DPRINTF(D_INIT, (" _init: function called\n"));
191 	if ((error = ddi_soft_state_init(&cpudrv_state,
192 	    sizeof (cpudrv_devstate_t), 0)) != 0) {
193 		return (error);
194 	}
195 
196 	if ((error = mod_install(&modlinkage)) != 0)  {
197 		ddi_soft_state_fini(&cpudrv_state);
198 	}
199 
200 	/*
201 	 * Callbacks used by the PPM driver.
202 	 */
203 	CPUDRV_PM_SET_PPM_CALLBACKS();
204 	return (error);
205 }
206 
207 int
208 _fini(void)
209 {
210 	int	error;
211 
212 	DPRINTF(D_FINI, (" _fini: function called\n"));
213 	if ((error = mod_remove(&modlinkage)) == 0) {
214 		ddi_soft_state_fini(&cpudrv_state);
215 	}
216 
217 	return (error);
218 }
219 
220 int
221 _info(struct modinfo *modinfop)
222 {
223 	return (mod_info(&modlinkage, modinfop));
224 }
225 
226 /*
227  * Driver attach(9e) entry point.
228  */
229 static int
230 cpudrv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
231 {
232 	int			instance;
233 	cpudrv_devstate_t	*cpudsp;
234 	extern pri_t		maxclsyspri;
235 
236 	instance = ddi_get_instance(dip);
237 
238 	switch (cmd) {
239 	case DDI_ATTACH:
240 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
241 		    "DDI_ATTACH called\n", instance));
242 		if (ddi_soft_state_zalloc(cpudrv_state, instance) !=
243 		    DDI_SUCCESS) {
244 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
245 			    "can't allocate state", instance);
246 			CPUDRV_PM_DISABLE();
247 			return (DDI_FAILURE);
248 		}
249 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
250 		    NULL) {
251 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
252 			    "can't get state", instance);
253 			ddi_soft_state_free(cpudrv_state, instance);
254 			CPUDRV_PM_DISABLE();
255 			return (DDI_FAILURE);
256 		}
257 		cpudsp->dip = dip;
258 
259 		/*
260 		 * Find CPU number for this dev_info node.
261 		 */
262 		if (!cpudrv_pm_get_cpu_id(dip, &(cpudsp->cpu_id))) {
263 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
264 			    "can't convert dip to cpu_id", instance);
265 			ddi_soft_state_free(cpudrv_state, instance);
266 			CPUDRV_PM_DISABLE();
267 			return (DDI_FAILURE);
268 		}
269 		if (cpudrv_pm_init(cpudsp) != DDI_SUCCESS) {
270 			ddi_soft_state_free(cpudrv_state, instance);
271 			CPUDRV_PM_DISABLE();
272 			return (DDI_FAILURE);
273 		}
274 		if (cpudrv_pm_comp_create(cpudsp) != DDI_SUCCESS) {
275 			ddi_soft_state_free(cpudrv_state, instance);
276 			CPUDRV_PM_DISABLE();
277 			cpudrv_pm_free(cpudsp);
278 			return (DDI_FAILURE);
279 		}
280 		if (ddi_prop_update_string(DDI_DEV_T_NONE,
281 		    dip, "pm-class", "CPU") != DDI_PROP_SUCCESS) {
282 			ddi_soft_state_free(cpudrv_state, instance);
283 			CPUDRV_PM_DISABLE();
284 			cpudrv_pm_free(cpudsp);
285 			return (DDI_FAILURE);
286 		}
287 
288 		/*
289 		 * Taskq is used to dispatch routine to monitor CPU activities.
290 		 */
291 		cpudsp->cpudrv_pm.tq = taskq_create_instance(
292 		    "cpudrv_pm_monitor",
293 		    ddi_get_instance(dip), CPUDRV_PM_TASKQ_THREADS,
294 		    (maxclsyspri - 1), CPUDRV_PM_TASKQ_MIN,
295 		    CPUDRV_PM_TASKQ_MAX, TASKQ_PREPOPULATE|TASKQ_CPR_SAFE);
296 
297 		mutex_init(&cpudsp->lock, NULL, MUTEX_DRIVER, NULL);
298 		mutex_init(&cpudsp->cpudrv_pm.timeout_lock, NULL, MUTEX_DRIVER,
299 		    NULL);
300 		cv_init(&cpudsp->cpudrv_pm.timeout_cv, NULL, CV_DEFAULT, NULL);
301 
302 		/*
303 		 * Driver needs to assume that CPU is running at unknown speed
304 		 * at DDI_ATTACH and switch it to the needed speed. We assume
305 		 * that initial needed speed is full speed for us.
306 		 */
307 		/*
308 		 * We need to take the lock because cpudrv_pm_monitor()
309 		 * will start running in parallel with attach().
310 		 */
311 		mutex_enter(&cpudsp->lock);
312 		cpudsp->cpudrv_pm.cur_spd = NULL;
313 		cpudsp->cpudrv_pm.targ_spd = cpudsp->cpudrv_pm.head_spd;
314 		cpudsp->cpudrv_pm.pm_started = B_FALSE;
315 		/*
316 		 * We don't call pm_raise_power() directly from attach because
317 		 * driver attach for a slave CPU node can happen before the
318 		 * CPU is even initialized. We just start the monitoring
319 		 * system which understands unknown speed and moves CPU
320 		 * to targ_spd when it have been initialized.
321 		 */
322 		CPUDRV_PM_MONITOR_INIT(cpudsp);
323 		mutex_exit(&cpudsp->lock);
324 
325 		CPUDRV_PM_INSTALL_TOPSPEED_CHANGE_HANDLER(cpudsp, dip);
326 
327 		ddi_report_dev(dip);
328 		return (DDI_SUCCESS);
329 
330 	case DDI_RESUME:
331 		DPRINTF(D_ATTACH, ("cpudrv_attach: instance %d: "
332 		    "DDI_RESUME called\n", instance));
333 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
334 		    NULL) {
335 			cmn_err(CE_WARN, "cpudrv_attach: instance %d: "
336 			    "can't get state", instance);
337 			return (DDI_FAILURE);
338 		}
339 		mutex_enter(&cpudsp->lock);
340 		/*
341 		 * Driver needs to assume that CPU is running at unknown speed
342 		 * at DDI_RESUME and switch it to the needed speed. We assume
343 		 * that the needed speed is full speed for us.
344 		 */
345 		cpudsp->cpudrv_pm.cur_spd = NULL;
346 		cpudsp->cpudrv_pm.targ_spd = cpudsp->cpudrv_pm.head_spd;
347 		CPUDRV_PM_MONITOR_INIT(cpudsp);
348 		mutex_exit(&cpudsp->lock);
349 		CPUDRV_PM_REDEFINE_TOPSPEED(dip);
350 		return (DDI_SUCCESS);
351 
352 	default:
353 		return (DDI_FAILURE);
354 	}
355 }
356 
357 /*
358  * Driver detach(9e) entry point.
359  */
360 static int
361 cpudrv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
362 {
363 	int			instance;
364 	cpudrv_devstate_t	*cpudsp;
365 	cpudrv_pm_t		*cpupm;
366 
367 	instance = ddi_get_instance(dip);
368 
369 	switch (cmd) {
370 	case DDI_DETACH:
371 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
372 		    "DDI_DETACH called\n", instance));
373 		/*
374 		 * If the only thing supported by the driver is power
375 		 * management, we can in future enhance the driver and
376 		 * framework that loads it to unload the driver when
377 		 * user has disabled CPU power management.
378 		 */
379 		return (DDI_FAILURE);
380 
381 	case DDI_SUSPEND:
382 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: "
383 		    "DDI_SUSPEND called\n", instance));
384 		if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) ==
385 		    NULL) {
386 			cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
387 			    "can't get state", instance);
388 			return (DDI_FAILURE);
389 		}
390 		/*
391 		 * During a checkpoint-resume sequence, framework will
392 		 * stop interrupts to quiesce kernel activity. This will
393 		 * leave our monitoring system ineffective. Handle this
394 		 * by stopping our monitoring system and bringing CPU
395 		 * to full speed. In case we are in special direct pm
396 		 * mode, we leave the CPU at whatever speed it is. This
397 		 * is harmless other than speed.
398 		 */
399 		mutex_enter(&cpudsp->lock);
400 		cpupm = &(cpudsp->cpudrv_pm);
401 
402 		DPRINTF(D_DETACH, ("cpudrv_detach: instance %d: DDI_SUSPEND - "
403 		    "cur_spd %d, head_spd %d\n", instance,
404 		    cpupm->cur_spd->pm_level, cpupm->head_spd->pm_level));
405 
406 		CPUDRV_PM_MONITOR_FINI(cpudsp);
407 
408 		if (!cpudrv_direct_pm && (cpupm->cur_spd != cpupm->head_spd)) {
409 			if (cpupm->pm_busycnt < 1) {
410 				if ((pm_busy_component(dip, CPUDRV_PM_COMP_NUM)
411 				    == DDI_SUCCESS)) {
412 					cpupm->pm_busycnt++;
413 				} else {
414 					CPUDRV_PM_MONITOR_INIT(cpudsp);
415 					mutex_exit(&cpudsp->lock);
416 					cmn_err(CE_WARN, "cpudrv_detach: "
417 					    "instance %d: can't busy CPU "
418 					    "component", instance);
419 					return (DDI_FAILURE);
420 				}
421 			}
422 			mutex_exit(&cpudsp->lock);
423 			if (pm_raise_power(dip, CPUDRV_PM_COMP_NUM,
424 			    cpupm->head_spd->pm_level) != DDI_SUCCESS) {
425 				mutex_enter(&cpudsp->lock);
426 				CPUDRV_PM_MONITOR_INIT(cpudsp);
427 				mutex_exit(&cpudsp->lock);
428 				cmn_err(CE_WARN, "cpudrv_detach: instance %d: "
429 				    "can't raise CPU power level", instance);
430 				return (DDI_FAILURE);
431 			} else {
432 				return (DDI_SUCCESS);
433 			}
434 		} else {
435 			mutex_exit(&cpudsp->lock);
436 			return (DDI_SUCCESS);
437 		}
438 
439 	default:
440 		return (DDI_FAILURE);
441 	}
442 }
443 
444 /*
445  * Driver power(9e) entry point.
446  *
447  * Driver's notion of current power is set *only* in power(9e) entry point
448  * after actual power change operation has been successfully completed.
449  */
450 /* ARGSUSED */
451 static int
452 cpudrv_power(dev_info_t *dip, int comp, int level)
453 {
454 	int			instance;
455 	cpudrv_devstate_t	*cpudsp;
456 	cpudrv_pm_t 		*cpupm;
457 	cpudrv_pm_spd_t		*new_spd;
458 	boolean_t		is_ready;
459 	int			ret;
460 
461 	instance = ddi_get_instance(dip);
462 
463 	DPRINTF(D_POWER, ("cpudrv_power: instance %d: level %d\n",
464 	    instance, level));
465 	if ((cpudsp = ddi_get_soft_state(cpudrv_state, instance)) == NULL) {
466 		cmn_err(CE_WARN, "cpudrv_power: instance %d: can't get state",
467 		    instance);
468 		return (DDI_FAILURE);
469 	}
470 
471 	mutex_enter(&cpudsp->lock);
472 	cpupm = &(cpudsp->cpudrv_pm);
473 
474 	/*
475 	 * In normal operation, we fail if we are busy and request is
476 	 * to lower the power level. We let this go through if the driver
477 	 * is in special direct pm mode. On x86, we also let this through
478 	 * if the change is due to a request to throttle the max speed.
479 	 */
480 	if (!cpudrv_direct_pm && (cpupm->pm_busycnt >= 1) &&
481 	    !cpudrv_pm_is_throttle_thread(cpupm)) {
482 		if ((cpupm->cur_spd != NULL) &&
483 		    (level < cpupm->cur_spd->pm_level)) {
484 			mutex_exit(&cpudsp->lock);
485 			return (DDI_FAILURE);
486 		}
487 	}
488 
489 	for (new_spd = cpupm->head_spd; new_spd; new_spd = new_spd->down_spd) {
490 		if (new_spd->pm_level == level)
491 			break;
492 	}
493 	if (!new_spd) {
494 		CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
495 		mutex_exit(&cpudsp->lock);
496 		cmn_err(CE_WARN, "cpudrv_power: instance %d: "
497 		    "can't locate new CPU speed", instance);
498 		return (DDI_FAILURE);
499 	}
500 
501 	/*
502 	 * We currently refuse to power manage if the CPU is not ready to
503 	 * take cross calls (cross calls fail silently if CPU is not ready
504 	 * for it).
505 	 *
506 	 * Additionally, for x86 platforms we cannot power manage
507 	 * any one instance, until all instances have been initialized.
508 	 * That's because we don't know what the CPU domains look like
509 	 * until all instances have been initialized.
510 	 */
511 	is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
512 	if (!is_ready) {
513 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
514 		    "CPU not ready for x-calls\n", instance));
515 	} else if (!(is_ready = cpudrv_pm_all_instances_ready())) {
516 		DPRINTF(D_POWER, ("cpudrv_power: instance %d: "
517 		    "waiting for all CPUs to be ready\n", instance));
518 	}
519 	if (!is_ready) {
520 		CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
521 		mutex_exit(&cpudsp->lock);
522 		return (DDI_FAILURE);
523 	}
524 
525 	/*
526 	 * Execute CPU specific routine on the requested CPU to change its
527 	 * speed to normal-speed/divisor.
528 	 */
529 	if ((ret = cpudrv_pm_change_speed(cpudsp, new_spd)) != DDI_SUCCESS) {
530 		cmn_err(CE_WARN, "cpudrv_power: cpudrv_pm_change_speed() "
531 		    "return = %d", ret);
532 		mutex_exit(&cpudsp->lock);
533 		return (DDI_FAILURE);
534 	}
535 
536 	/*
537 	 * Reset idle threshold time for the new power level.
538 	 */
539 	if ((cpupm->cur_spd != NULL) && (level < cpupm->cur_spd->pm_level)) {
540 		if (pm_idle_component(dip, CPUDRV_PM_COMP_NUM) ==
541 		    DDI_SUCCESS) {
542 			if (cpupm->pm_busycnt >= 1)
543 				cpupm->pm_busycnt--;
544 		} else
545 			cmn_err(CE_WARN, "cpudrv_power: instance %d: can't "
546 			    "idle CPU component", ddi_get_instance(dip));
547 	}
548 	/*
549 	 * Reset various parameters because we are now running at new speed.
550 	 */
551 	cpupm->lastquan_mstate[CMS_IDLE] = 0;
552 	cpupm->lastquan_mstate[CMS_SYSTEM] = 0;
553 	cpupm->lastquan_mstate[CMS_USER] = 0;
554 	cpupm->lastquan_lbolt = 0;
555 	cpupm->cur_spd = new_spd;
556 	CPUDRV_PM_RESET_THROTTLE_THREAD(cpupm);
557 	mutex_exit(&cpudsp->lock);
558 
559 	return (DDI_SUCCESS);
560 }
561 
562 /*
563  * Initialize the field that will be used for reporting
564  * the supported_frequencies_Hz cpu_info kstat.
565  */
566 static void
567 set_supp_freqs(cpu_t *cp, cpudrv_pm_t *cpupm)
568 {
569 	char		*supp_freqs;
570 	char		*sfptr;
571 	uint64_t	*speeds;
572 	cpudrv_pm_spd_t	*spd;
573 	int		i;
574 #define	UINT64_MAX_STRING (sizeof ("18446744073709551615"))
575 
576 	speeds = kmem_zalloc(cpupm->num_spd * sizeof (uint64_t), KM_SLEEP);
577 	for (i = cpupm->num_spd - 1, spd = cpupm->head_spd; spd;
578 	    i--, spd = spd->down_spd) {
579 		speeds[i] =
580 		    CPUDRV_PM_SPEED_HZ(cp->cpu_type_info.pi_clock, spd->speed);
581 	}
582 
583 	supp_freqs = kmem_zalloc((UINT64_MAX_STRING * cpupm->num_spd),
584 	    KM_SLEEP);
585 	sfptr = supp_freqs;
586 	for (i = 0; i < cpupm->num_spd; i++) {
587 		if (i == cpupm->num_spd - 1) {
588 			(void) sprintf(sfptr, "%"PRIu64, speeds[i]);
589 		} else {
590 			(void) sprintf(sfptr, "%"PRIu64":", speeds[i]);
591 			sfptr = supp_freqs + strlen(supp_freqs);
592 		}
593 	}
594 	cpu_set_supp_freqs(cp, supp_freqs);
595 	kmem_free(supp_freqs, (UINT64_MAX_STRING * cpupm->num_spd));
596 	kmem_free(speeds, cpupm->num_spd * sizeof (uint64_t));
597 }
598 
599 /*
600  * Initialize power management data.
601  */
602 static int
603 cpudrv_pm_init(cpudrv_devstate_t *cpudsp)
604 {
605 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
606 	cpudrv_pm_spd_t	*cur_spd;
607 	cpudrv_pm_spd_t	*prev_spd = NULL;
608 	int		*speeds;
609 	uint_t		nspeeds;
610 	int		idle_cnt_percent;
611 	int		user_cnt_percent;
612 	int		i;
613 
614 	if (!cpudrv_pm_init_module(cpudsp))
615 		return (DDI_FAILURE);
616 
617 	CPUDRV_PM_GET_SPEEDS(cpudsp, speeds, nspeeds);
618 	if (nspeeds < 2) {
619 		/* Need at least two speeds to power manage */
620 		CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
621 		cpudrv_pm_free_module(cpudsp);
622 		return (DDI_FAILURE);
623 	}
624 	cpupm->num_spd = nspeeds;
625 
626 	/*
627 	 * Calculate the watermarks and other parameters based on the
628 	 * supplied speeds.
629 	 *
630 	 * One of the basic assumption is that for X amount of CPU work,
631 	 * if CPU is slowed down by a factor of N, the time it takes to
632 	 * do the same work will be N * X.
633 	 *
634 	 * The driver declares that a CPU is idle and ready for slowed down,
635 	 * if amount of idle thread is more than the current speed idle_hwm
636 	 * without dropping below idle_hwm a number of consecutive sampling
637 	 * intervals and number of running threads in user mode are below
638 	 * user_lwm.  We want to set the current user_lwm such that if we
639 	 * just switched to the next slower speed with no change in real work
640 	 * load, the amount of user threads at the slower speed will be such
641 	 * that it falls below the slower speed's user_hwm.  If we didn't do
642 	 * that then we will just come back to the higher speed as soon as we
643 	 * go down even with no change in work load.
644 	 * The user_hwm is a fixed precentage and not calculated dynamically.
645 	 *
646 	 * We bring the CPU up if idle thread at current speed is less than
647 	 * the current speed idle_lwm for a number of consecutive sampling
648 	 * intervals or user threads are above the user_hwm for the current
649 	 * speed.
650 	 */
651 	for (i = 0; i < nspeeds; i++) {
652 		cur_spd = kmem_zalloc(sizeof (cpudrv_pm_spd_t), KM_SLEEP);
653 		cur_spd->speed = speeds[i];
654 		if (i == 0) {	/* normal speed */
655 			cpupm->head_spd = cur_spd;
656 			cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_NORMAL;
657 			cur_spd->idle_hwm =
658 			    (cpudrv_pm_idle_hwm * cur_spd->quant_cnt) / 100;
659 			/* can't speed anymore */
660 			cur_spd->idle_lwm = 0;
661 			cur_spd->user_hwm = UINT_MAX;
662 		} else {
663 			cur_spd->quant_cnt = CPUDRV_PM_QUANT_CNT_OTHR;
664 			ASSERT(prev_spd != NULL);
665 			prev_spd->down_spd = cur_spd;
666 			cur_spd->up_spd = cpupm->head_spd;
667 
668 			/*
669 			 * Let's assume CPU is considered idle at full speed
670 			 * when it is spending I% of time in running the idle
671 			 * thread.  At full speed, CPU will be busy (100 - I) %
672 			 * of times.  This % of busyness increases by factor of
673 			 * N as CPU slows down.  CPU that is idle I% of times
674 			 * in full speed, it is idle (100 - ((100 - I) * N)) %
675 			 * of times in N speed.  The idle_lwm is a fixed
676 			 * percentage.  A large value of N may result in
677 			 * idle_hwm to go below idle_lwm.  We need to make sure
678 			 * that there is at least a buffer zone seperation
679 			 * between the idle_lwm and idle_hwm values.
680 			 */
681 			idle_cnt_percent = CPUDRV_PM_IDLE_CNT_PERCENT(
682 			    cpudrv_pm_idle_hwm, speeds, i);
683 			idle_cnt_percent = max(idle_cnt_percent,
684 			    (cpudrv_pm_idle_lwm + cpudrv_pm_idle_buf_zone));
685 			cur_spd->idle_hwm =
686 			    (idle_cnt_percent * cur_spd->quant_cnt) / 100;
687 			cur_spd->idle_lwm =
688 			    (cpudrv_pm_idle_lwm * cur_spd->quant_cnt) / 100;
689 
690 			/*
691 			 * The lwm for user threads are determined such that
692 			 * if CPU slows down, the load of work in the
693 			 * new speed would still keep the CPU at or below the
694 			 * user_hwm in the new speed.  This is to prevent
695 			 * the quick jump back up to higher speed.
696 			 */
697 			cur_spd->user_hwm = (cpudrv_pm_user_hwm *
698 			    cur_spd->quant_cnt) / 100;
699 			user_cnt_percent = CPUDRV_PM_USER_CNT_PERCENT(
700 			    cpudrv_pm_user_hwm, speeds, i);
701 			prev_spd->user_lwm =
702 			    (user_cnt_percent * prev_spd->quant_cnt) / 100;
703 		}
704 		prev_spd = cur_spd;
705 	}
706 	/* Slowest speed. Can't slow down anymore */
707 	cur_spd->idle_hwm = UINT_MAX;
708 	cur_spd->user_lwm = -1;
709 #ifdef	DEBUG
710 	DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: head_spd spd %d, "
711 	    "num_spd %d\n", ddi_get_instance(cpudsp->dip),
712 	    cpupm->head_spd->speed, cpupm->num_spd));
713 	for (cur_spd = cpupm->head_spd; cur_spd; cur_spd = cur_spd->down_spd) {
714 		DPRINTF(D_PM_INIT, ("cpudrv_pm_init: instance %d: speed %d, "
715 		    "down_spd spd %d, idle_hwm %d, user_lwm %d, "
716 		    "up_spd spd %d, idle_lwm %d, user_hwm %d, "
717 		    "quant_cnt %d\n", ddi_get_instance(cpudsp->dip),
718 		    cur_spd->speed,
719 		    (cur_spd->down_spd ? cur_spd->down_spd->speed : 0),
720 		    cur_spd->idle_hwm, cur_spd->user_lwm,
721 		    (cur_spd->up_spd ? cur_spd->up_spd->speed : 0),
722 		    cur_spd->idle_lwm, cur_spd->user_hwm,
723 		    cur_spd->quant_cnt));
724 	}
725 #endif	/* DEBUG */
726 	CPUDRV_PM_FREE_SPEEDS(speeds, nspeeds);
727 	return (DDI_SUCCESS);
728 }
729 
730 /*
731  * Free CPU power management data.
732  */
733 static void
734 cpudrv_pm_free(cpudrv_devstate_t *cpudsp)
735 {
736 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
737 	cpudrv_pm_spd_t	*cur_spd, *next_spd;
738 
739 	cur_spd = cpupm->head_spd;
740 	while (cur_spd) {
741 		next_spd = cur_spd->down_spd;
742 		kmem_free(cur_spd, sizeof (cpudrv_pm_spd_t));
743 		cur_spd = next_spd;
744 	}
745 	bzero(cpupm, sizeof (cpudrv_pm_t));
746 	cpudrv_pm_free_module(cpudsp);
747 }
748 
749 /*
750  * Create pm-components property.
751  */
752 static int
753 cpudrv_pm_comp_create(cpudrv_devstate_t *cpudsp)
754 {
755 	cpudrv_pm_t 	*cpupm = &(cpudsp->cpudrv_pm);
756 	cpudrv_pm_spd_t	*cur_spd;
757 	char		**pmc;
758 	int		size;
759 	char		name[] = "NAME=CPU Speed";
760 	int		i, j;
761 	uint_t		comp_spd;
762 	int		result = DDI_FAILURE;
763 
764 	pmc = kmem_zalloc((cpupm->num_spd + 1) * sizeof (char *), KM_SLEEP);
765 	size = CPUDRV_PM_COMP_SIZE();
766 	if (cpupm->num_spd > CPUDRV_PM_COMP_MAX_VAL) {
767 		cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
768 		    "number of speeds exceeded limits",
769 		    ddi_get_instance(cpudsp->dip));
770 		kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
771 		return (result);
772 	}
773 
774 	for (i = cpupm->num_spd, cur_spd = cpupm->head_spd; i > 0;
775 	    i--, cur_spd = cur_spd->down_spd) {
776 		cur_spd->pm_level = i;
777 		pmc[i] = kmem_zalloc((size * sizeof (char)), KM_SLEEP);
778 		comp_spd = CPUDRV_PM_COMP_SPEED(cpupm, cur_spd);
779 		if (comp_spd > CPUDRV_PM_COMP_MAX_VAL) {
780 			cmn_err(CE_WARN, "cpudrv_pm_comp_create: "
781 			    "instance %d: speed exceeded limits",
782 			    ddi_get_instance(cpudsp->dip));
783 			for (j = cpupm->num_spd; j >= i; j--) {
784 				kmem_free(pmc[j], size * sizeof (char));
785 			}
786 			kmem_free(pmc, (cpupm->num_spd + 1) *
787 			    sizeof (char *));
788 			return (result);
789 		}
790 		CPUDRV_PM_COMP_SPRINT(pmc[i], cpupm, cur_spd, comp_spd)
791 		DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: "
792 		    "instance %d: pm-components power level %d string '%s'\n",
793 		    ddi_get_instance(cpudsp->dip), i, pmc[i]));
794 	}
795 	pmc[0] = kmem_zalloc(sizeof (name), KM_SLEEP);
796 	(void) strcat(pmc[0], name);
797 	DPRINTF(D_PM_COMP_CREATE, ("cpudrv_pm_comp_create: instance %d: "
798 	    "pm-components component name '%s'\n",
799 	    ddi_get_instance(cpudsp->dip), pmc[0]));
800 
801 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, cpudsp->dip,
802 	    "pm-components", pmc, cpupm->num_spd + 1) == DDI_PROP_SUCCESS) {
803 		result = DDI_SUCCESS;
804 	} else {
805 		cmn_err(CE_WARN, "cpudrv_pm_comp_create: instance %d: "
806 		    "can't create pm-components property",
807 		    ddi_get_instance(cpudsp->dip));
808 	}
809 
810 	for (i = cpupm->num_spd; i > 0; i--) {
811 		kmem_free(pmc[i], size * sizeof (char));
812 	}
813 	kmem_free(pmc[0], sizeof (name));
814 	kmem_free(pmc, (cpupm->num_spd + 1) * sizeof (char *));
815 	return (result);
816 }
817 
818 /*
819  * Mark a component idle.
820  */
821 #define	CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm) { \
822 	if ((cpupm)->pm_busycnt >= 1) { \
823 		if (pm_idle_component((dip), CPUDRV_PM_COMP_NUM) == \
824 		    DDI_SUCCESS) { \
825 			DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
826 			    "instance %d: pm_idle_component called\n", \
827 			    ddi_get_instance((dip)))); \
828 			(cpupm)->pm_busycnt--; \
829 		} else { \
830 			cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
831 			    "can't idle CPU component", \
832 			    ddi_get_instance((dip))); \
833 		} \
834 	} \
835 }
836 
837 /*
838  * Marks a component busy in both PM framework and driver state structure.
839  */
840 #define	CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm) { \
841 	if ((cpupm)->pm_busycnt < 1) { \
842 		if (pm_busy_component((dip), CPUDRV_PM_COMP_NUM) == \
843 		    DDI_SUCCESS) { \
844 			DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: " \
845 			    "instance %d: pm_busy_component called\n", \
846 			    ddi_get_instance((dip)))); \
847 			(cpupm)->pm_busycnt++; \
848 		} else { \
849 			cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: " \
850 			    "can't busy CPU component", \
851 			    ddi_get_instance((dip))); \
852 		} \
853 	} \
854 }
855 
856 /*
857  * Marks a component busy and calls pm_raise_power().
858  */
859 #define	CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm, new_level) { \
860 	/* \
861 	 * Mark driver and PM framework busy first so framework doesn't try \
862 	 * to bring CPU to lower speed when we need to be at higher speed. \
863 	 */ \
864 	CPUDRV_PM_MONITOR_PM_BUSY_COMP((dip), (cpupm)); \
865 	mutex_exit(&(cpudsp)->lock); \
866 	DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: " \
867 	    "pm_raise_power called to %d\n", ddi_get_instance((dip)), \
868 		(new_level))); \
869 	if (pm_raise_power((dip), CPUDRV_PM_COMP_NUM, (new_level)) != \
870 	    DDI_SUCCESS) { \
871 		cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't " \
872 		    "raise CPU power level", ddi_get_instance((dip))); \
873 	} \
874 	mutex_enter(&(cpudsp)->lock); \
875 }
876 
877 /*
878  * In order to monitor a CPU, we need to hold cpu_lock to access CPU
879  * statistics. Holding cpu_lock is not allowed from a callout routine.
880  * We dispatch a taskq to do that job.
881  */
882 static void
883 cpudrv_pm_monitor_disp(void *arg)
884 {
885 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
886 
887 	/*
888 	 * We are here because the last task has scheduled a timeout.
889 	 * The queue should be empty at this time.
890 	 */
891 	mutex_enter(&cpudsp->cpudrv_pm.timeout_lock);
892 	if (!taskq_dispatch(cpudsp->cpudrv_pm.tq, cpudrv_pm_monitor, arg,
893 	    TQ_NOSLEEP)) {
894 		mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
895 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor_disp: failed to "
896 		    "dispatch the cpudrv_pm_monitor taskq\n"));
897 		mutex_enter(&cpudsp->lock);
898 		CPUDRV_PM_MONITOR_INIT(cpudsp);
899 		mutex_exit(&cpudsp->lock);
900 		return;
901 	}
902 	cpudsp->cpudrv_pm.timeout_count++;
903 	mutex_exit(&cpudsp->cpudrv_pm.timeout_lock);
904 }
905 
906 /*
907  * Get current CPU microstate times and scale them. We should probably be
908  * using get_cpu_mstate() to get this data, but bugs in some of the ISRs
909  * have led to inflated system times and prevented CPUs from being power
910  * managed. We can probably safely ignore time spent in ISRs when
911  * determining idleness.
912  */
913 static void
914 cpudrv_get_cpu_mstate(cpu_t *cpu, hrtime_t *times)
915 {
916 	int i;
917 
918 	for (i = 0; i < NCMSTATES; i++) {
919 		times[i] = cpu->cpu_acct[i];
920 		scalehrtime(&times[i]);
921 	}
922 }
923 
924 /*
925  * Monitors each CPU for the amount of time idle thread was running in the
926  * last quantum and arranges for the CPU to go to the lower or higher speed.
927  * Called at the time interval appropriate for the current speed. The
928  * time interval for normal speed is CPUDRV_PM_QUANT_CNT_NORMAL. The time
929  * interval for other speeds (including unknown speed) is
930  * CPUDRV_PM_QUANT_CNT_OTHR.
931  */
932 static void
933 cpudrv_pm_monitor(void *arg)
934 {
935 	cpudrv_devstate_t	*cpudsp = (cpudrv_devstate_t *)arg;
936 	cpudrv_pm_t		*cpupm;
937 	cpudrv_pm_spd_t		*cur_spd, *new_spd;
938 	cpu_t			*cp;
939 	dev_info_t		*dip;
940 	uint_t			idle_cnt, user_cnt, system_cnt;
941 	clock_t			lbolt_cnt;
942 	hrtime_t		msnsecs[NCMSTATES];
943 	boolean_t		is_ready;
944 
945 #define	GET_CPU_MSTATE_CNT(state, cnt) \
946 	msnsecs[state] = NSEC_TO_TICK(msnsecs[state]); \
947 	if (cpupm->lastquan_mstate[state] > msnsecs[state]) \
948 		msnsecs[state] = cpupm->lastquan_mstate[state]; \
949 	cnt = msnsecs[state] - cpupm->lastquan_mstate[state]; \
950 	cpupm->lastquan_mstate[state] = msnsecs[state]
951 
952 	mutex_enter(&cpudsp->lock);
953 	cpupm = &(cpudsp->cpudrv_pm);
954 	if (cpupm->timeout_id == 0) {
955 		mutex_exit(&cpudsp->lock);
956 		goto do_return;
957 	}
958 	cur_spd = cpupm->cur_spd;
959 	dip = cpudsp->dip;
960 
961 	/*
962 	 * We assume that a CPU is initialized and has a valid cpu_t
963 	 * structure, if it is ready for cross calls. If this changes,
964 	 * additional checks might be needed.
965 	 *
966 	 * Additionally, for x86 platforms we cannot power manage
967 	 * any one instance, until all instances have been initialized.
968 	 * That's because we don't know what the CPU domains look like
969 	 * until all instances have been initialized.
970 	 */
971 	is_ready = CPUDRV_PM_XCALL_IS_READY(cpudsp->cpu_id);
972 	if (!is_ready) {
973 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
974 		    "CPU not ready for x-calls\n", ddi_get_instance(dip)));
975 	} else if (!(is_ready = cpudrv_pm_all_instances_ready())) {
976 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
977 		    "waiting for all CPUs to be ready\n",
978 		    ddi_get_instance(dip)));
979 	}
980 	if (!is_ready) {
981 		/*
982 		 * Make sure that we are busy so that framework doesn't
983 		 * try to bring us down in this situation.
984 		 */
985 		CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
986 		CPUDRV_PM_MONITOR_INIT(cpudsp);
987 		mutex_exit(&cpudsp->lock);
988 		goto do_return;
989 	}
990 
991 	/*
992 	 * Make sure that we are still not at unknown power level.
993 	 */
994 	if (cur_spd == NULL) {
995 		DPRINTF(D_PM_MONITOR, ("cpudrv_pm_monitor: instance %d: "
996 		    "cur_spd is unknown\n", ddi_get_instance(dip)));
997 		CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
998 		    cpupm->targ_spd->pm_level);
999 		/*
1000 		 * We just changed the speed. Wait till at least next
1001 		 * call to this routine before proceeding ahead.
1002 		 */
1003 		CPUDRV_PM_MONITOR_INIT(cpudsp);
1004 		mutex_exit(&cpudsp->lock);
1005 		goto do_return;
1006 	}
1007 
1008 	mutex_enter(&cpu_lock);
1009 	if ((cp = cpu_get(cpudsp->cpu_id)) == NULL) {
1010 		mutex_exit(&cpu_lock);
1011 		CPUDRV_PM_MONITOR_INIT(cpudsp);
1012 		mutex_exit(&cpudsp->lock);
1013 		cmn_err(CE_WARN, "cpudrv_pm_monitor: instance %d: can't get "
1014 		    "cpu_t", ddi_get_instance(dip));
1015 		goto do_return;
1016 	}
1017 
1018 	if (!cpupm->pm_started) {
1019 		cpupm->pm_started = B_TRUE;
1020 		set_supp_freqs(cp, cpupm);
1021 	}
1022 
1023 	cpudrv_get_cpu_mstate(cp, msnsecs);
1024 	GET_CPU_MSTATE_CNT(CMS_IDLE, idle_cnt);
1025 	GET_CPU_MSTATE_CNT(CMS_USER, user_cnt);
1026 	GET_CPU_MSTATE_CNT(CMS_SYSTEM, system_cnt);
1027 
1028 	/*
1029 	 * We can't do anything when we have just switched to a state
1030 	 * because there is no valid timestamp.
1031 	 */
1032 	if (cpupm->lastquan_lbolt == 0) {
1033 		cpupm->lastquan_lbolt = lbolt;
1034 		mutex_exit(&cpu_lock);
1035 		CPUDRV_PM_MONITOR_INIT(cpudsp);
1036 		mutex_exit(&cpudsp->lock);
1037 		goto do_return;
1038 	}
1039 
1040 	/*
1041 	 * Various watermarks are based on this routine being called back
1042 	 * exactly at the requested period. This is not guaranteed
1043 	 * because this routine is called from a taskq that is dispatched
1044 	 * from a timeout routine.  Handle this by finding out how many
1045 	 * ticks have elapsed since the last call (lbolt_cnt) and adjusting
1046 	 * the idle_cnt based on the delay added to the requested period
1047 	 * by timeout and taskq.
1048 	 */
1049 	lbolt_cnt = lbolt - cpupm->lastquan_lbolt;
1050 	cpupm->lastquan_lbolt = lbolt;
1051 	mutex_exit(&cpu_lock);
1052 	/*
1053 	 * Time taken between recording the current counts and
1054 	 * arranging the next call of this routine is an error in our
1055 	 * calculation. We minimize the error by calling
1056 	 * CPUDRV_PM_MONITOR_INIT() here instead of end of this routine.
1057 	 */
1058 	CPUDRV_PM_MONITOR_INIT(cpudsp);
1059 	DPRINTF(D_PM_MONITOR_VERBOSE, ("cpudrv_pm_monitor: instance %d: "
1060 	    "idle count %d, user count %d, system count %d, pm_level %d, "
1061 	    "pm_busycnt %d\n", ddi_get_instance(dip), idle_cnt, user_cnt,
1062 	    system_cnt, cur_spd->pm_level, cpupm->pm_busycnt));
1063 
1064 #ifdef	DEBUG
1065 	/*
1066 	 * Notify that timeout and taskq has caused delays and we need to
1067 	 * scale our parameters accordingly.
1068 	 *
1069 	 * To get accurate result, don't turn on other DPRINTFs with
1070 	 * the following DPRINTF. PROM calls generated by other
1071 	 * DPRINTFs changes the timing.
1072 	 */
1073 	if (lbolt_cnt > cur_spd->quant_cnt) {
1074 		DPRINTF(D_PM_MONITOR_DELAY, ("cpudrv_pm_monitor: instance %d: "
1075 		    "lbolt count %ld > quantum_count %u\n",
1076 		    ddi_get_instance(dip), lbolt_cnt, cur_spd->quant_cnt));
1077 	}
1078 #endif	/* DEBUG */
1079 
1080 	/*
1081 	 * Adjust counts based on the delay added by timeout and taskq.
1082 	 */
1083 	idle_cnt = (idle_cnt * cur_spd->quant_cnt) / lbolt_cnt;
1084 	user_cnt = (user_cnt * cur_spd->quant_cnt) / lbolt_cnt;
1085 	if ((user_cnt > cur_spd->user_hwm) || (idle_cnt < cur_spd->idle_lwm &&
1086 	    cur_spd->idle_blwm_cnt >= cpudrv_pm_idle_blwm_cnt_max)) {
1087 		cur_spd->idle_blwm_cnt = 0;
1088 		cur_spd->idle_bhwm_cnt = 0;
1089 		/*
1090 		 * In normal situation, arrange to go to next higher speed.
1091 		 * If we are running in special direct pm mode, we just stay
1092 		 * at the current speed.
1093 		 */
1094 		if (cur_spd == cur_spd->up_spd || cpudrv_direct_pm) {
1095 			CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
1096 		} else {
1097 			new_spd = cur_spd->up_spd;
1098 			CPUDRV_PM_MONITOR_PM_BUSY_AND_RAISE(dip, cpudsp, cpupm,
1099 			    new_spd->pm_level);
1100 		}
1101 	} else if ((user_cnt <= cur_spd->user_lwm) &&
1102 	    (idle_cnt >= cur_spd->idle_hwm) || !CPU_ACTIVE(cp)) {
1103 		cur_spd->idle_blwm_cnt = 0;
1104 		cur_spd->idle_bhwm_cnt = 0;
1105 		/*
1106 		 * Arrange to go to next lower speed by informing our idle
1107 		 * status to the power management framework.
1108 		 */
1109 		CPUDRV_PM_MONITOR_PM_IDLE_COMP(dip, cpupm);
1110 	} else {
1111 		/*
1112 		 * If we are between the idle water marks and have not
1113 		 * been here enough consecutive times to be considered
1114 		 * busy, just increment the count and return.
1115 		 */
1116 		if ((idle_cnt < cur_spd->idle_hwm) &&
1117 		    (idle_cnt >= cur_spd->idle_lwm) &&
1118 		    (cur_spd->idle_bhwm_cnt < cpudrv_pm_idle_bhwm_cnt_max)) {
1119 			cur_spd->idle_blwm_cnt = 0;
1120 			cur_spd->idle_bhwm_cnt++;
1121 			mutex_exit(&cpudsp->lock);
1122 			goto do_return;
1123 		}
1124 		if (idle_cnt < cur_spd->idle_lwm) {
1125 			cur_spd->idle_blwm_cnt++;
1126 			cur_spd->idle_bhwm_cnt = 0;
1127 		}
1128 		/*
1129 		 * Arranges to stay at the current speed.
1130 		 */
1131 		CPUDRV_PM_MONITOR_PM_BUSY_COMP(dip, cpupm);
1132 	}
1133 	mutex_exit(&cpudsp->lock);
1134 do_return:
1135 	mutex_enter(&cpupm->timeout_lock);
1136 	ASSERT(cpupm->timeout_count > 0);
1137 	cpupm->timeout_count--;
1138 	cv_signal(&cpupm->timeout_cv);
1139 	mutex_exit(&cpupm->timeout_lock);
1140 }
1141