xref: /titanic_41/usr/src/uts/i86pc/io/tzmon/tzmon.c (revision ba2e4443695ee6a6f420a35cd4fc3d3346d22932)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Solaris x86 ACPI ThermalZone Monitor
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <sys/errno.h>
34 #include <sys/conf.h>
35 #include <sys/modctl.h>
36 #include <sys/open.h>
37 #include <sys/stat.h>
38 #include <sys/ddi.h>
39 #include <sys/sunddi.h>
40 #include <sys/ksynch.h>
41 #include <sys/uadmin.h>
42 #include <sys/acpi/acpi.h>
43 #include <sys/acpica.h>
44 #include <sys/sdt.h>
45 
46 #include "tzmon.h"
47 
48 
49 #define	TZMON_ENUM_TRIP_POINTS	1
50 #define	TZMON_ENUM_DEV_LISTS	2
51 #define	TZMON_ENUM_ALL		(TZMON_ENUM_TRIP_POINTS	| TZMON_ENUM_DEV_LISTS)
52 
53 /*
54  * TZ_TASKQ_NAME_LEN is precisely the length of the string "AcpiThermalMonitor"
55  * plus a two-digit instance number plus a NULL.  If the taskq name is changed
56  * (particularly if it is lengthened), then this value needs to change.
57  */
58 #define	TZ_TASKQ_NAME_LEN	21
59 
60 /*
61  * Kelvin to Celsius conversion
62  * The formula for converting degrees Kelvin to degrees Celsius is
63  * C = K - 273.15 (we round to 273.2).  The unit for thermal zone
64  * temperatures is tenths of a degree Kelvin.  Use tenth of a degree
65  * to convert, then make a whole number out of it.
66  */
67 #define	K_TO_C(temp)		(((temp) - 2732) / 10)
68 
69 
70 /* cb_ops or dev_ops forward declarations */
71 static	int	tzmon_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
72     void *arg, void **result);
73 static	int	tzmon_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
74 static	int	tzmon_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
75 
76 /* other forward declarations */
77 static void tzmon_notify_zone(ACPI_HANDLE obj, UINT32 val, void *ctx);
78 static void tzmon_eval_int(ACPI_HANDLE obj, char *method, int *rv);
79 static thermal_zone_t *tzmon_alloc_zone();
80 static void tzmon_free_zone_list();
81 static void tzmon_discard_buffers(thermal_zone_t *tzp);
82 static void tzmon_enumerate_zone(ACPI_HANDLE obj, thermal_zone_t *tzp,
83 	int enum_flag);
84 static ACPI_STATUS tzmon_zone_callback(ACPI_HANDLE obj, UINT32 nest,
85     void *ctx, void **rv);
86 static void tzmon_find_zones(void);
87 static void tzmon_monitor(void *ctx);
88 static void tzmon_set_power_device(ACPI_HANDLE dev, int on_off);
89 static void tzmon_set_power(ACPI_BUFFER devlist, int on_off);
90 static void tzmon_eval_zone(thermal_zone_t *tzp);
91 static void tzmon_do_shutdown(void);
92 
93 extern void halt(char *);
94 
95 static struct cb_ops	tzmon_cb_ops = {
96 	nodev,			/* no open routine	*/
97 	nodev,			/* no close routine	*/
98 	nodev,			/* not a block driver	*/
99 	nodev,			/* no print routine	*/
100 	nodev,			/* no dump routine	*/
101 	nodev,			/* no read routine	*/
102 	nodev,			/* no write routine	*/
103 	nodev,			/* no ioctl routine	*/
104 	nodev,			/* no devmap routine	*/
105 	nodev,			/* no mmap routine	*/
106 	nodev,			/* no segmap routine	*/
107 	nochpoll,		/* no chpoll routine	*/
108 	ddi_prop_op,
109 	0,			/* not a STREAMS driver	*/
110 	D_NEW | D_MP,		/* safe for multi-thread/multi-processor */
111 };
112 
113 static struct dev_ops tzmon_ops = {
114 	DEVO_REV,		/* devo_rev */
115 	0,			/* devo_refcnt */
116 	tzmon_getinfo,		/* devo_getinfo */
117 	nulldev,		/* devo_identify */
118 	nulldev,		/* devo_probe */
119 	tzmon_attach,		/* devo_attach */
120 	tzmon_detach,		/* devo_detach */
121 	nodev,			/* devo_reset */
122 	&tzmon_cb_ops,		/* devo_cb_ops */
123 	(struct bus_ops *)0,	/* devo_bus_ops */
124 	NULL,			/* devo_power */
125 };
126 
127 extern	struct	mod_ops mod_driverops;
128 
129 static	struct modldrv modldrv = {
130 	&mod_driverops,
131 	"ACPI Thermal Zone Monitor %I%",
132 	&tzmon_ops,
133 };
134 
135 static	struct modlinkage modlinkage = {
136 	MODREV_1,		/* MODREV_1 indicated by manual */
137 	(void *)&modldrv,
138 	NULL,			/* termination of list of linkage structures */
139 };
140 
141 /* globals for this module */
142 static dev_info_t	*tzmon_dip;
143 static thermal_zone_t	*zone_list;
144 static int		zone_count;
145 static kmutex_t		zone_list_lock;
146 static kcondvar_t	zone_list_condvar;
147 
148 
149 /*
150  * _init, _info, and _fini support loading and unloading the driver.
151  */
152 int
153 _init(void)
154 {
155 	return (mod_install(&modlinkage));
156 }
157 
158 
159 int
160 _info(struct modinfo *modinfop)
161 {
162 	return (mod_info(&modlinkage, modinfop));
163 }
164 
165 
166 int
167 _fini(void)
168 {
169 	return (mod_remove(&modlinkage));
170 }
171 
172 
173 static int
174 tzmon_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
175 {
176 	if (cmd != DDI_ATTACH)
177 		return (DDI_FAILURE);
178 
179 	if (tzmon_dip != NULL)
180 		return (DDI_FAILURE);
181 
182 	/*
183 	 * Check to see if ACPI CA services are available
184 	 */
185 	if (AcpiSubsystemStatus() != AE_OK)
186 		return (DDI_FAILURE);
187 
188 	mutex_init(&zone_list_lock, NULL, MUTEX_DRIVER, NULL);
189 	cv_init(&zone_list_condvar, NULL, CV_DRIVER, NULL);
190 
191 	tzmon_find_zones();
192 	mutex_enter(&zone_list_lock);
193 	if (zone_count < 1) {
194 		mutex_exit(&zone_list_lock);
195 		mutex_destroy(&zone_list_lock);
196 		cv_destroy(&zone_list_condvar);
197 		return (DDI_FAILURE);
198 	}
199 	mutex_exit(&zone_list_lock);
200 
201 	if (ddi_create_minor_node(dip, ddi_get_name(dip), S_IFCHR, 0,
202 	    DDI_PSEUDO, 0) == DDI_FAILURE) {
203 		tzmon_free_zone_list();
204 		mutex_destroy(&zone_list_lock);
205 		cv_destroy(&zone_list_condvar);
206 		return (DDI_FAILURE);
207 	}
208 
209 	tzmon_dip = dip;
210 
211 	ddi_report_dev(dip);
212 
213 	return (DDI_SUCCESS);
214 }
215 
216 
217 /*ARGSUSED*/
218 static int
219 tzmon_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
220 {
221 	int error;
222 
223 	switch (infocmd) {
224 	case DDI_INFO_DEVT2DEVINFO:
225 		*result = tzmon_dip;
226 		if (tzmon_dip == NULL)
227 			error = DDI_FAILURE;
228 		else
229 			error = DDI_SUCCESS;
230 		break;
231 	case DDI_INFO_DEVT2INSTANCE:
232 		*result = 0;
233 		error = DDI_SUCCESS;
234 		break;
235 	default:
236 		*result = NULL;
237 		error = DDI_FAILURE;
238 	}
239 
240 	return (error);
241 }
242 
243 
244 static int
245 tzmon_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
246 {
247 	thermal_zone_t *tzp = zone_list;
248 
249 	if (cmd != DDI_DETACH)
250 		return (DDI_FAILURE);
251 
252 	/* free allocated thermal zone name(s) */
253 	while (tzp != NULL) {
254 		AcpiOsFree(tzp->zone_name);
255 		tzp = tzp->next;
256 	}
257 
258 	/* discard zone list assets */
259 	tzmon_free_zone_list();
260 
261 	ddi_remove_minor_node(dip, NULL);
262 	tzmon_dip = NULL;
263 
264 	mutex_destroy(&zone_list_lock);
265 	cv_destroy(&zone_list_condvar);
266 
267 	return (DDI_SUCCESS);
268 }
269 
270 
271 /*
272  * tzmon_notify_zone
273  * Thermal zone notification handler.
274  */
275 static void
276 tzmon_notify_zone(ACPI_HANDLE obj, UINT32 val, void *ctx)
277 {
278 	thermal_zone_t *tzp = (thermal_zone_t *)ctx;
279 
280 	switch (val) {
281 	case 0x80:	/* Thermal Zone status changed */
282 		tzmon_eval_zone(tzp);
283 		break;
284 	case 0x81:	/* Thermal Zone trip points changed */
285 		tzmon_enumerate_zone(obj, tzp, TZMON_ENUM_TRIP_POINTS);
286 		break;
287 	case 0x82:	/* Device Lists changed */
288 		tzmon_enumerate_zone(obj, tzp, TZMON_ENUM_DEV_LISTS);
289 		break;
290 	case 0x83:	/* Thermal Relationship Table changed */
291 		/* not handling _TRT objects, so not handling this event */
292 		cmn_err(CE_CONT, "?tzmon: thermal relationship table changed");
293 		break;
294 	default:
295 		break;
296 	}
297 }
298 
299 
300 /*
301  * tzmon_eval_int
302  * Evaluate the object/method as an integer.
303  */
304 static void
305 tzmon_eval_int(ACPI_HANDLE obj, char *method, int *rv)
306 {
307 
308 	if (acpica_eval_int(obj, method, rv) != AE_OK)
309 		*rv = -1;
310 }
311 
312 
313 /*
314  * tzmon_alloc_zone
315  * Allocate memory for the zone structure and initialize it lock mutex.
316  */
317 static thermal_zone_t *
318 tzmon_alloc_zone()
319 {
320 	thermal_zone_t *tzp;
321 
322 	tzp = kmem_zalloc(sizeof (thermal_zone_t), KM_SLEEP);
323 	mutex_init(&tzp->lock, NULL, MUTEX_DRIVER, NULL);
324 
325 	return (tzp);
326 }
327 
328 
329 /*
330  * tzmon_free_zone_list
331  * Free the zone list, either because attach failed or detach initiated.
332  */
333 static void
334 tzmon_free_zone_list()
335 {
336 	thermal_zone_t *tzp = zone_list;
337 
338 	while (tzp != NULL) {
339 		thermal_zone_t *next;
340 
341 		mutex_enter(&tzp->lock);
342 
343 		/*
344 		 * Remove the notify handler for the zone.  Not much to
345 		 * do if this fails (since we are on our way out), so
346 		 * just ignore failure.
347 		 */
348 		(void) AcpiRemoveNotifyHandler(tzp->obj, ACPI_DEVICE_NOTIFY,
349 		    tzmon_notify_zone);
350 
351 		/* Shut down monitor thread, if running */
352 		if (tzp->taskq != NULL) {
353 			tzp->polling_period = 0;
354 			cv_broadcast(&zone_list_condvar);
355 
356 			/* Drop mutex to allow the thread to run */
357 			mutex_exit(&tzp->lock);
358 			ddi_taskq_destroy(tzp->taskq);
359 			mutex_enter(&tzp->lock);
360 		}
361 
362 		tzmon_discard_buffers(tzp);
363 		mutex_exit(&tzp->lock);
364 		mutex_destroy(&tzp->lock);
365 
366 		next = tzp->next;
367 		kmem_free(tzp, sizeof (thermal_zone_t));
368 		tzp = next;
369 	}
370 }
371 
372 
373 static void
374 tzmon_discard_buffers(thermal_zone_t *tzp)
375 {
376 	int level;
377 
378 	for (level = 0; level < TZ_NUM_LEVELS; level++) {
379 		if (tzp->al[level].Pointer != NULL)
380 			AcpiOsFree(tzp->al[level].Pointer);
381 	}
382 
383 	if (tzp->psl.Pointer != NULL)
384 		AcpiOsFree(tzp->psl.Pointer);
385 }
386 
387 
388 /*
389  * tzmon_enumerate_zone
390  * Enumerates the contents of a thermal zone and updates passed-in
391  * thermal_zone or creates a new one if tzp is NULL. Newly-created
392  * zones are linked into the global zone_list.
393  */
394 static void
395 tzmon_enumerate_zone(ACPI_HANDLE obj, thermal_zone_t *tzp, int enum_flag)
396 {
397 	ACPI_STATUS status;
398 	ACPI_BUFFER zone_name;
399 	int	level;
400 	int	instance;
401 	char	abuf[5];
402 
403 	/*
404 	 * Newly-created zones and existing zones both require
405 	 * some individual attention.
406 	 */
407 	if (tzp == NULL) {
408 		/* New zone required */
409 		tzp = tzmon_alloc_zone();
410 		mutex_enter(&zone_list_lock);
411 		tzp->next = zone_list;
412 		zone_list = tzp;
413 
414 		/*
415 		 * It is exceedingly unlikely that instance will exceed 99.
416 		 * However, if it does, this will cause problems when
417 		 * creating the taskq for this thermal zone.
418 		 */
419 		instance = zone_count;
420 		zone_count++;
421 		mutex_exit(&zone_list_lock);
422 		mutex_enter(&tzp->lock);
423 		tzp->obj = obj;
424 
425 		/*
426 		 * Set to a low level.  Will get set to the actual
427 		 * current power level when the thread monitor polls
428 		 * the current temperature.
429 		 */
430 		tzp->current_level = 0;
431 
432 		/* Get the zone name in case we need to display it later */
433 		zone_name.Length = ACPI_ALLOCATE_BUFFER;
434 		zone_name.Pointer = NULL;
435 
436 		status = AcpiGetName(obj, ACPI_FULL_PATHNAME, &zone_name);
437 		ASSERT(status == AE_OK);
438 
439 		tzp->zone_name = zone_name.Pointer;
440 
441 		status = AcpiInstallNotifyHandler(obj, ACPI_DEVICE_NOTIFY,
442 		    tzmon_notify_zone, (void *)tzp);
443 		ASSERT(status == AE_OK);
444 	} else {
445 		/* Existing zone - toss out allocated items */
446 		mutex_enter(&tzp->lock);
447 		ASSERT(tzp->obj == obj);
448 
449 		if (enum_flag & TZMON_ENUM_DEV_LISTS)
450 			tzmon_discard_buffers(tzp);
451 	}
452 
453 	if (enum_flag & TZMON_ENUM_TRIP_POINTS) {
454 		for (level = 0; level < TZ_NUM_LEVELS; level++) {
455 			(void) snprintf(abuf, 5, "_AC%d", level);
456 			tzmon_eval_int(obj, abuf, &tzp->ac[level]);
457 
458 		}
459 
460 		tzmon_eval_int(obj, "_CRT", &tzp->crt);
461 		tzmon_eval_int(obj, "_HOT", &tzp->hot);
462 		tzmon_eval_int(obj, "_PSV", &tzp->psv);
463 	}
464 
465 	if (enum_flag & TZMON_ENUM_DEV_LISTS) {
466 		for (level = 0; level < TZ_NUM_LEVELS; level++) {
467 			if (tzp->ac[level] == -1) {
468 				tzp->al[level].Length = 0;
469 				tzp->al[level].Pointer = NULL;
470 			} else {
471 				(void) snprintf(abuf, 5, "_AL%d", level);
472 				tzp->al[level].Length = ACPI_ALLOCATE_BUFFER;
473 				tzp->al[level].Pointer = NULL;
474 				if (AcpiEvaluateObject(obj, abuf, NULL,
475 				    &tzp->al[level]) != AE_OK) {
476 					cmn_err(CE_WARN, "tzmon: error "
477 					    "evaluating _AL object");
478 
479 					tzp->al[level].Length = 0;
480 					tzp->al[level].Pointer = NULL;
481 				}
482 			}
483 		}
484 
485 		tzp->psl.Length = ACPI_ALLOCATE_BUFFER;
486 		tzp->psl.Pointer = NULL;
487 		(void) AcpiEvaluateObject(obj, "_PSL", NULL, &tzp->psl);
488 	}
489 
490 	tzmon_eval_int(obj, "_TC1", &tzp->tc1);
491 	tzmon_eval_int(obj, "_TC2", &tzp->tc2);
492 	tzmon_eval_int(obj, "_TSP", &tzp->tsp);
493 	tzmon_eval_int(obj, "_TZP", &tzp->tzp);
494 
495 	if (tzp->tzp == 0) {
496 		tzp->polling_period = 0;
497 	} else {
498 		if (tzp->tzp < 0)
499 			tzp->polling_period = TZ_DEFAULT_PERIOD;
500 		else
501 			tzp->polling_period = tzp->tzp/10;
502 
503 		/* start monitor thread if needed */
504 		if (tzp->taskq == NULL) {
505 			char taskq_name[TZ_TASKQ_NAME_LEN];
506 
507 			(void) snprintf(taskq_name, TZ_TASKQ_NAME_LEN,
508 			    "AcpiThermalMonitor%02d", instance);
509 			tzp->taskq = ddi_taskq_create(tzmon_dip,
510 			    taskq_name, 1, TASKQ_DEFAULTPRI, 0);
511 			if (tzp->taskq == NULL) {
512 				tzp->polling_period = 0;
513 				cmn_err(CE_WARN, "tzmon: could not create"
514 				    " monitor thread - monitor by notify only");
515 			} else {
516 				(void) ddi_taskq_dispatch(tzp->taskq,
517 				    tzmon_monitor, tzp, DDI_SLEEP);
518 			}
519 		}
520 	}
521 
522 	mutex_exit(&tzp->lock);
523 }
524 
525 
526 /*
527  * tzmon_zone_callback
528  * Enumerate the thermal zone if it has a _TMP (current thermal zone
529  * operating temperature) method.
530  */
531 /*ARGSUSED*/
532 static ACPI_STATUS
533 tzmon_zone_callback(ACPI_HANDLE obj, UINT32 nest, void *ctx, void **rv)
534 {
535 	ACPI_HANDLE tmpobj;
536 
537 	/*
538 	 * We get both ThermalZone() and Scope(\_TZ) objects here;
539 	 * look for _TMP (without which a zone is invalid) to pick
540 	 * between them (and ignore invalid zones)
541 	 */
542 	if (AcpiGetHandle(obj, "_TMP", &tmpobj) == AE_OK) {
543 		tzmon_enumerate_zone(obj, NULL, TZMON_ENUM_ALL);
544 	}
545 
546 	return (AE_OK);
547 }
548 
549 
550 /*
551  * tzmon_find_zones
552  * Find all of the thermal zones by calling a ACPICA function that
553  * walks the ACPI namespace and invokes a callback for each thermal
554  * object found.
555  */
556 static void
557 tzmon_find_zones()
558 {
559 	ACPI_STATUS status;
560 	int retval;
561 
562 	status = AcpiWalkNamespace(ACPI_TYPE_THERMAL, ACPI_ROOT_OBJECT,
563 	    8, tzmon_zone_callback, NULL, (void **)&retval);
564 
565 	ASSERT(status == AE_OK);
566 }
567 
568 
569 /*
570  * tzmon_monitor
571  * Run as a separate thread, this wakes according to polling period and
572  * checks particular objects in the thermal zone.  One instance per
573  * thermal zone.
574  */
575 static void
576 tzmon_monitor(void *ctx)
577 {
578 	thermal_zone_t *tzp = (thermal_zone_t *)ctx;
579 	clock_t ticks;
580 
581 	do {
582 		/* Check out the zone */
583 		tzmon_eval_zone(tzp);
584 
585 		/* Go back to sleep */
586 		mutex_enter(&tzp->lock);
587 		ticks = drv_usectohz(tzp->polling_period * 1000000);
588 		if (ticks > 0)
589 			(void) cv_timedwait(&zone_list_condvar, &tzp->lock,
590 			    ddi_get_lbolt() + ticks);
591 		mutex_exit(&tzp->lock);
592 	} while (ticks > 0);
593 }
594 
595 
596 /*
597  * tzmon_set_power_device
598  */
599 static void
600 tzmon_set_power_device(ACPI_HANDLE dev, int on_off)
601 {
602 	ACPI_BUFFER rb;
603 	ACPI_OBJECT *pr0;
604 	ACPI_STATUS status;
605 	int i;
606 
607 	rb.Length = ACPI_ALLOCATE_BUFFER;
608 	rb.Pointer = NULL;
609 	status = AcpiEvaluateObject(dev, "_PR0", NULL, &rb);
610 	if (status != AE_OK) {
611 		cmn_err(CE_NOTE, "tzmon: can not set power");
612 		return;
613 	}
614 
615 	pr0 = ((ACPI_OBJECT *)rb.Pointer);
616 	if (pr0->Type != ACPI_TYPE_PACKAGE) {
617 		cmn_err(CE_NOTE, "tzmon: can not set power");
618 		AcpiOsFree(rb.Pointer);
619 		return;
620 	}
621 
622 	for (i = 0; i < pr0->Package.Count; i++) {
623 		status = AcpiEvaluateObject(
624 		    pr0->Package.Elements[i].Reference.Handle,
625 		    on_off ? "_ON" : "_OFF", NULL, NULL);
626 		if (status != AE_OK) {
627 			cmn_err(CE_WARN, "tz_set_pwr_dev: failed to set %d\n",
628 			    i);
629 		}
630 	}
631 
632 	AcpiOsFree(rb.Pointer);
633 }
634 
635 
636 /*
637  * tzmon_set_power
638  * Turn on or turn off all devices in the supplied list.
639  */
640 static void
641 tzmon_set_power(ACPI_BUFFER devlist, int on_off)
642 {
643 	ACPI_OBJECT *devs;
644 	int i;
645 
646 	devs = ((ACPI_OBJECT *)devlist.Pointer);
647 	if (devs->Type != ACPI_TYPE_PACKAGE) {
648 		cmn_err(CE_NOTE, "tzmon: can not set power");
649 		return;
650 	}
651 
652 	for (i = 0; i < devs->Package.Count; i++)
653 		tzmon_set_power_device(
654 		    devs->Package.Elements[i].Reference.Handle, on_off);
655 }
656 
657 
658 /*
659  * tzmon_eval_zone
660  * Evaluate the current conditions within the thermal zone.
661  */
662 static void
663 tzmon_eval_zone(thermal_zone_t *tzp)
664 {
665 	int tmp, new_level, level;
666 
667 	mutex_enter(&tzp->lock);
668 
669 	/* get the current temperature from ACPI */
670 	tzmon_eval_int(tzp->obj, "_TMP", &tmp);
671 	DTRACE_PROBE1(tzmon_tz_temp, int, tmp);
672 
673 	/* _HOT handling */
674 	if (tzp->hot > 0 && tmp >= tzp->hot) {
675 		cmn_err(CE_WARN,
676 		    "tzmon: Thermal zone (%s) is too hot (%d C); "
677 			"initiating shutdown\n",
678 		    (char *)tzp->zone_name, K_TO_C(tmp));
679 
680 		tzmon_do_shutdown();
681 	}
682 
683 	/* _CRT handling */
684 	if (tzp->crt > 0 && tmp >= tzp->crt) {
685 		cmn_err(CE_WARN,
686 		    "tzmon: Thermal zone (%s) is critically hot (%d C); "
687 			"initiating rapid shutdown\n",
688 		    (char *)tzp->zone_name, K_TO_C(tmp));
689 
690 		/* shut down (fairly) immediately */
691 		mdboot(A_REBOOT, AD_HALT, NULL, B_FALSE);
692 	}
693 
694 	/*
695 	 * use the temperature to determine whether the thermal zone
696 	 * is at a new active cooling threshold level
697 	 */
698 	for (level = 0, new_level = -1; level < TZ_NUM_LEVELS; level++) {
699 		if (tzp->ac[level] >= 0 && (tmp >= tzp->ac[level])) {
700 			new_level = level;
701 			break;
702 		}
703 	}
704 
705 	/*
706 	 * if the active cooling threshold has changed, turn off the
707 	 * devices associated with the old one and turn on the new one
708 	 */
709 	if (tzp->current_level != new_level) {
710 		if ((tzp->current_level >= 0) &&
711 		    (tzp->al[tzp->current_level].Length != 0))
712 			tzmon_set_power(tzp->al[tzp->current_level], 0);
713 
714 		if ((new_level >= 0) &&
715 		    (tzp->al[new_level].Length != 0))
716 			tzmon_set_power(tzp->al[new_level], 1);
717 
718 		tzp->current_level = new_level;
719 	}
720 
721 	mutex_exit(&tzp->lock);
722 }
723 
724 
725 /*
726  * tzmon_do_shutdown
727  * Initiates shutdown by sending a SIGPWR signal to init.
728  */
729 static void
730 tzmon_do_shutdown(void)
731 {
732 	proc_t *initpp;
733 
734 	mutex_enter(&pidlock);
735 	initpp = prfind(P_INITPID);
736 	mutex_exit(&pidlock);
737 
738 	/* if we can't find init, just halt */
739 	if (initpp == NULL) {
740 		mdboot(A_REBOOT, AD_HALT, NULL, B_FALSE);
741 	}
742 
743 	/* graceful shutdown with inittab and all getting involved */
744 	psignal(initpp, SIGPWR);
745 }
746