xref: /netbsd-src/sys/dev/acpi/acpi_tz.c (revision 4e6df137e8e14049b5a701d249962c480449c141)
1 /* $NetBSD: acpi_tz.c,v 1.61 2010/03/05 14:00:17 jruoho Exp $ */
2 
3 /*
4  * Copyright (c) 2003 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * ACPI Thermal Zone driver
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.61 2010/03/05 14:00:17 jruoho Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/callout.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 
41 #include <dev/acpi/acpireg.h>
42 #include <dev/acpi/acpivar.h>
43 
44 #define _COMPONENT          ACPI_TZ_COMPONENT
45 ACPI_MODULE_NAME            ("acpi_tz")
46 
47 /* flags */
48 #define ATZ_F_VERBOSE		0x01	/* show events to console */
49 #define ATZ_F_CRITICAL		0x02	/* zone critical */
50 #define ATZ_F_HOT		0x04	/* zone hot */
51 #define ATZ_F_PASSIVE		0x08	/* zone passive cooling */
52 #define ATZ_F_PASSIVEONLY	0x10	/* zone is passive cooling only */
53 
54 /* no active cooling level */
55 #define ATZ_ACTIVE_NONE -1
56 
57 /* constants */
58 #define ATZ_TZP_RATE	300	/* default if no _TZP CM present (30 secs) */
59 #define ATZ_NLEVELS	10	/* number of cooling levels, from ACPI spec */
60 #define ATZ_ZEROC	2732	/* 0C, measured in 0.1 Kelvin */
61 #define ATZ_TMP_INVALID	0xffffffff	/* invalid temperature */
62 #define ATZ_ZONE_EXPIRE	9000	/* zone info refetch interval (15min) */
63 
64 /* sensor indexes */
65 #define ATZ_SENSOR_TEMP	0	/* thermal zone temperature */
66 
67 static int	acpitz_match(device_t, cfdata_t, void *);
68 static void	acpitz_attach(device_t, device_t, void *);
69 
70 /*
71  * ACPI Temperature Zone information. Note all temperatures are reported
72  * in 0.1 Kelvin, and that the ACPI specification assumes that
73  * K = C + 273.2 rather than the nominal 273.15 used by envsys(4).
74  * So define an appropriate conversion.
75  */
76 
77 #define	ATZ2UKELVIN(t) ((t) * 100000 - 50000)
78 
79 struct acpitz_zone {
80 	/* Active cooling temperature threshold */
81 	UINT32 ac[ATZ_NLEVELS];
82 	/* Package of references to all active cooling devices for a level */
83 	ACPI_BUFFER al[ATZ_NLEVELS];
84 	/* Critical temperature threshold for system shutdown */
85 	UINT32 crt;
86 	/* Critical temperature threshold for S4 sleep */
87 	UINT32 hot;
88 	/* Package of references to processor objects for passive cooling */
89 	ACPI_BUFFER psl;
90 	/* Conveys if temperatures are absolute or relative values. */
91 	UINT32 rtv;
92 	/* Passive cooling temperature threshold */
93 	UINT32 psv;
94 	/* Thermal constants for use in passive cooling formulas */
95 	UINT32 tc1, tc2;
96 	/* Current temperature of the thermal zone */
97 	UINT32 prevtmp, tmp;
98 	/* Thermal sampling period for passive cooling, in tenths of seconds */
99 	UINT32 tsp;
100 	/* Package of references to devices in this TZ (optional) */
101 	ACPI_BUFFER tzd;
102 	/* Recommended TZ polling frequency, in tenths of seconds */
103 	UINT32 tzp;
104 	/* Thermal zone name */
105 	char *name;
106 	/* FAN min, max, current rpms */
107 	UINT32 fanmin, fanmax, fancurrent;
108 };
109 
110 struct acpitz_softc {
111 	struct acpi_devnode *sc_devnode;
112 	struct acpitz_zone sc_zone;
113 	struct callout sc_callout;
114 	struct sysmon_envsys *sc_sme;
115 	envsys_data_t sc_temp_sensor;
116 	envsys_data_t sc_fan_sensor;
117 	int sc_active;		/* active cooling level */
118 	int sc_flags;
119 	int sc_rate;		/* tz poll rate */
120 	int sc_zone_expire;
121 
122 	int sc_first;
123 	int sc_have_fan;	/* FAN sensor is optional */
124 };
125 
126 static void	acpitz_get_status(void *);
127 static void	acpitz_get_zone(void *, int);
128 static void	acpitz_get_zone_quiet(void *);
129 static char	*acpitz_celcius_string(int);
130 static void	acpitz_print_status(device_t);
131 static void	acpitz_power_off(struct acpitz_softc *);
132 static void	acpitz_power_zone(struct acpitz_softc *, int, int);
133 static void	acpitz_sane_temp(UINT32 *tmp);
134 static ACPI_STATUS
135 		acpitz_switch_cooler(ACPI_OBJECT *, void *);
136 static void	acpitz_notify_handler(ACPI_HANDLE, UINT32, void *);
137 static int	acpitz_get_integer(device_t, const char *, UINT32 *);
138 static void	acpitz_tick(void *);
139 static void	acpitz_init_envsys(device_t);
140 static void	acpitz_get_limits(struct sysmon_envsys *, envsys_data_t *,
141 				  sysmon_envsys_lim_t *, uint32_t *);
142 static int	acpitz_get_fanspeed(device_t, UINT32 *, UINT32 *, UINT32 *);
143 #ifdef notyet
144 static ACPI_STATUS
145 		acpitz_set_fanspeed(device_t, UINT32);
146 #endif
147 
148 CFATTACH_DECL_NEW(acpitz, sizeof(struct acpitz_softc), acpitz_match,
149     acpitz_attach, NULL, NULL);
150 
151 /*
152  * acpitz_match: autoconf(9) match routine
153  */
154 static int
155 acpitz_match(device_t parent, cfdata_t match, void *aux)
156 {
157 	struct acpi_attach_args *aa = aux;
158 
159 	if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
160 		return 0;
161 
162 	return 1;
163 }
164 
165 /*
166  * acpitz_attach: autoconf(9) attach routine
167  */
168 static void
169 acpitz_attach(device_t parent, device_t self, void *aux)
170 {
171 	struct acpitz_softc *sc = device_private(self);
172 	struct acpi_attach_args *aa = aux;
173 	ACPI_STATUS rv;
174 	ACPI_INTEGER v;
175 
176 #if 0
177 	sc->sc_flags = ATZ_F_VERBOSE;
178 #endif
179 	sc->sc_devnode = aa->aa_node;
180 
181 	aprint_naive("\n");
182 
183 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP", &v);
184 	if (ACPI_FAILURE(rv))
185 		sc->sc_zone.tzp = ATZ_TZP_RATE;
186 	else
187 		sc->sc_zone.tzp = v;
188 
189 	aprint_debug(" sample rate %d.%ds\n",
190 	    sc->sc_zone.tzp / 10, sc->sc_zone.tzp % 10);
191 
192 	/* XXX a value of 0 means "polling is not necessary" */
193 	if (sc->sc_zone.tzp == 0)
194 		sc->sc_zone.tzp = ATZ_TZP_RATE;
195 
196 	sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
197 	sc->sc_first = 1;
198 	sc->sc_have_fan = 0;
199 	if (acpitz_get_fanspeed(self,
200 	    &sc->sc_zone.fanmin, &sc->sc_zone.fanmax, &sc->sc_zone.fancurrent)
201 	    == 0)
202 		sc->sc_have_fan = 1;
203 
204 	rv = acpi_eval_string(sc->sc_devnode->ad_handle,
205 	    "REGN", &sc->sc_zone.name);
206 	if (ACPI_FAILURE(rv))
207 		sc->sc_zone.name = __UNCONST("temperature");
208 	acpitz_get_zone(self, 1);
209 	acpitz_get_status(self);
210 
211 	rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
212 	    ACPI_DEVICE_NOTIFY, acpitz_notify_handler, self);
213 
214 	if (ACPI_FAILURE(rv))
215 		return;
216 
217 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
218 	callout_setfunc(&sc->sc_callout, acpitz_tick, self);
219 
220 	acpitz_init_envsys(self);
221 
222 	if (!pmf_device_register(self, NULL, NULL))
223 		aprint_error(": couldn't establish power handler\n");
224 
225 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
226 }
227 
228 static void
229 acpitz_get_zone_quiet(void *opaque)
230 {
231 	acpitz_get_zone(opaque, 0);
232 }
233 
234 static void
235 acpitz_get_status(void *opaque)
236 {
237 	device_t dv = opaque;
238 	struct acpitz_softc *sc = device_private(dv);
239 	UINT32 tmp, active;
240 	int i, flags;
241 	UINT32 fmin, fmax, fcurrent;
242 
243 	sc->sc_zone_expire--;
244 	if (sc->sc_zone_expire <= 0) {
245 		sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
246 		if (sc->sc_flags & ATZ_F_VERBOSE)
247 			printf("%s: force refetch zone\n", device_xname(dv));
248 		acpitz_get_zone(dv, 0);
249 	}
250 
251 	if (acpitz_get_integer(dv, "_TMP", &tmp)) {
252 		aprint_error_dev(dv, "failed to evaluate _TMP\n");
253 		return;
254 	}
255 
256 	sc->sc_zone.prevtmp = sc->sc_zone.tmp;
257 	sc->sc_zone.tmp = tmp;
258 	if (sc->sc_first)
259 		sc->sc_zone.prevtmp = tmp;
260 	/* XXX sanity check for tmp here? */
261 
262 	if (acpitz_get_fanspeed(dv, &fmin, &fmax, &fcurrent) == 0) {
263 		if (fcurrent != ATZ_TMP_INVALID)
264 			sc->sc_zone.fancurrent = fcurrent;
265 	}
266 
267 	/*
268 	 * The temperature unit for envsys(4) is microKelvin, so convert to
269 	 * that from ACPI's microKelvin. Also, the ACPI specification assumes
270 	 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(4),
271 	 * so we correct for that too.
272 	 */
273 	sc->sc_temp_sensor.value_cur = ATZ2UKELVIN(sc->sc_zone.tmp);
274 	sc->sc_temp_sensor.state = ENVSYS_SVALID;
275 
276 	sc->sc_fan_sensor.value_cur = sc->sc_zone.fancurrent;
277 	sc->sc_fan_sensor.state = ENVSYS_SVALID;
278 
279 	if (sc->sc_flags & ATZ_F_VERBOSE)
280 		acpitz_print_status(dv);
281 
282 	if (sc->sc_flags & ATZ_F_PASSIVEONLY) {
283 		/* Passive Cooling: XXX not yet */
284 
285 	} else {
286 		/* Active Cooling */
287 
288 		/* temperature threshold: _AC0 > ... > _AC9 */
289 		active = ATZ_ACTIVE_NONE;
290 		for (i = ATZ_NLEVELS - 1; i >= 0; i--) {
291 			if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID)
292 				continue;
293 
294 			/* we want to keep highest cooling mode in 'active' */
295 			if (sc->sc_zone.ac[i] <= tmp)
296 				active = i;
297 		}
298 
299 		flags = sc->sc_flags &
300 		    ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
301 		if (sc->sc_zone.psv != ATZ_TMP_INVALID &&
302 		    tmp >= sc->sc_zone.psv)
303 			flags |= ATZ_F_PASSIVE;
304 		if (sc->sc_zone.hot != ATZ_TMP_INVALID &&
305 		    tmp >= sc->sc_zone.hot)
306 			flags |= ATZ_F_HOT;
307 		if (sc->sc_zone.crt != ATZ_TMP_INVALID &&
308 		    tmp >= sc->sc_zone.crt)
309 			flags |= ATZ_F_CRITICAL;
310 
311 		if (flags != sc->sc_flags) {
312 			int changed = (sc->sc_flags ^ flags) & flags;
313 			sc->sc_flags = flags;
314 			if (changed & ATZ_F_CRITICAL) {
315 				sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
316 				aprint_debug_dev(dv,
317 				    "zone went critical at temp %sC\n",
318 				    acpitz_celcius_string(tmp));
319 			} else if (changed & ATZ_F_HOT) {
320 				sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
321 				aprint_debug_dev(dv,
322 				    "zone went hot at temp %sC\n",
323 				    acpitz_celcius_string(tmp));
324 			}
325 		}
326 
327 		/* power on fans */
328 		if (sc->sc_active != active) {
329 			if (sc->sc_active != ATZ_ACTIVE_NONE)
330 				acpitz_power_zone(sc, sc->sc_active, 0);
331 
332 			if (active != ATZ_ACTIVE_NONE) {
333 				if (sc->sc_flags & ATZ_F_VERBOSE)
334 					printf("%s: active cooling level %u\n",
335 					    device_xname(dv), active);
336 				acpitz_power_zone(sc, active, 1);
337 			} else if (sc->sc_flags & ATZ_F_VERBOSE)
338 				printf("%s: no active cooling level\n",
339 				    device_xname(dv));
340 
341 			sc->sc_active = active;
342 		}
343 	}
344 
345 	return;
346 }
347 
348 static char *
349 acpitz_celcius_string(int dk)
350 {
351 	static char buf[10];
352 	int dc;
353 
354 	dc = abs(dk - ATZ_ZEROC);
355 	snprintf(buf, sizeof(buf), "%s%d.%d", (dk >= ATZ_ZEROC)?"":"-",
356 		dc / 10, dc % 10);
357 
358 	return buf;
359 }
360 
361 static void
362 acpitz_print_status(device_t dv)
363 {
364 	struct acpitz_softc *sc = device_private(dv);
365 
366 	printf("%s: zone temperature is now %sC\n", device_xname(dv),
367 	    acpitz_celcius_string(sc->sc_zone.tmp));
368 	if (sc->sc_have_fan) {
369 		printf("%s: fan rpm %u\n", device_xname(dv),
370 		    sc->sc_zone.fancurrent);
371 	}
372 
373 	return;
374 }
375 
376 static ACPI_STATUS
377 acpitz_switch_cooler(ACPI_OBJECT *obj, void *arg)
378 {
379 	ACPI_HANDLE cooler;
380 	ACPI_STATUS rv;
381 	int pwr_state, flag;
382 
383 	flag = *(int *)arg;
384 
385 	if (flag)
386 		pwr_state = ACPI_STATE_D0;
387 	else
388 		pwr_state = ACPI_STATE_D3;
389 
390 	rv = acpi_eval_reference_handle(obj, &cooler);
391 	if (ACPI_FAILURE(rv)) {
392 		aprint_error("%s: failed to get handle\n", __func__);
393 		return rv;
394 	}
395 
396 	rv = acpi_pwr_switch_consumer(cooler, pwr_state);
397 	if (rv != AE_BAD_PARAMETER && ACPI_FAILURE(rv))
398 		aprint_error("%s: failed to change state for %s: %s\n",
399 		    __func__, acpi_name(cooler), AcpiFormatException(rv));
400 
401 	return AE_OK;
402 }
403 
404 /*
405  * acpitz_power_zone:
406  *	power on or off the i:th part of the zone zone
407  */
408 static void
409 acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
410 {
411 	KASSERT(i >= 0 && i < ATZ_NLEVELS);
412 
413 	acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
414 	    acpitz_switch_cooler, &on);
415 }
416 
417 
418 /*
419  * acpitz_power_off:
420  *	power off parts of the zone
421  */
422 static void
423 acpitz_power_off(struct acpitz_softc *sc)
424 {
425 	int i;
426 
427 	for (i = 0 ; i < ATZ_NLEVELS; i++) {
428 		if (sc->sc_zone.al[i].Pointer == NULL)
429 			continue;
430 		acpitz_power_zone(sc, i, 0);
431 	}
432 	sc->sc_active = ATZ_ACTIVE_NONE;
433 	sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
434 }
435 
436 static void
437 acpitz_get_zone(void *opaque, int verbose)
438 {
439 	device_t dv = opaque;
440 	struct acpitz_softc *sc = device_private(dv);
441 	ACPI_STATUS rv;
442 	char buf[8];
443 	int i, valid_levels;
444 
445 	if (!sc->sc_first) {
446 		acpitz_power_off(sc);
447 
448 		for (i = 0; i < ATZ_NLEVELS; i++) {
449 			if (sc->sc_zone.al[i].Pointer != NULL)
450 				ACPI_FREE(sc->sc_zone.al[i].Pointer);
451 			sc->sc_zone.al[i].Pointer = NULL;
452 		}
453 	} else
454 		aprint_normal(":");
455 
456 	valid_levels = 0;
457 
458 	for (i = 0; i < ATZ_NLEVELS; i++) {
459 		ACPI_OBJECT *obj;
460 
461 		snprintf(buf, sizeof(buf), "_AC%d", i);
462 		if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
463 			continue;
464 
465 		snprintf(buf, sizeof(buf), "_AL%d", i);
466 		rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf,
467 		    &sc->sc_zone.al[i]);
468 		if (ACPI_FAILURE(rv)) {
469 			sc->sc_zone.al[i].Pointer = NULL;
470 			continue;
471 		}
472 
473 		obj = sc->sc_zone.al[i].Pointer;
474 		if (obj != NULL) {
475 			if (obj->Type != ACPI_TYPE_PACKAGE) {
476 				aprint_error("%d not package\n", i);
477 				ACPI_FREE(obj);
478 				sc->sc_zone.al[i].Pointer = NULL;
479 				continue;
480 			}
481 		}
482 
483 		if (sc->sc_first)
484 			aprint_normal(" active cooling level %d: %sC", i,
485 			    acpitz_celcius_string(sc->sc_zone.ac[i]));
486 
487 		valid_levels++;
488 	}
489 
490 	acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
491 	acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
492 	acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
493 	acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
494 	acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
495 	acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
496 
497 #if 0
498 	sc->sc_zone.psl.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
499 	sc->sc_zone.psl.Pointer = NULL;
500 	AcpiEvaluateObject(sc->sc_devnode->ad_handle,
501 	    "_PSL", NULL, &sc->sc_zone.psl);
502 #endif
503 
504 	/* ACPI spec: If _RTV is not present or present and zero,
505 	 * values are absolute. */
506 	acpitz_get_integer(dv, "_RTV", &sc->sc_zone.rtv);
507 	if (sc->sc_zone.rtv == ATZ_TMP_INVALID)
508 		sc->sc_zone.rtv = 0;
509 
510 
511 	acpitz_sane_temp(&sc->sc_zone.tmp);
512 	acpitz_sane_temp(&sc->sc_zone.crt);
513 	acpitz_sane_temp(&sc->sc_zone.hot);
514 	acpitz_sane_temp(&sc->sc_zone.psv);
515 
516 	if (verbose) {
517 		if (sc->sc_zone.crt != ATZ_TMP_INVALID)
518 			aprint_normal(" critical %sC",
519 			    acpitz_celcius_string(sc->sc_zone.crt));
520 		if (sc->sc_zone.hot != ATZ_TMP_INVALID)
521 			aprint_normal(" hot %sC",
522 			    acpitz_celcius_string(sc->sc_zone.hot));
523 		if (sc->sc_zone.psv != ATZ_TMP_INVALID)
524 			aprint_normal(" passive %sC",
525 			    acpitz_celcius_string(sc->sc_zone.psv));
526 	}
527 
528 	if (valid_levels == 0) {
529 		sc->sc_flags |= ATZ_F_PASSIVEONLY;
530 		if (sc->sc_first)
531 			aprint_normal(", passive cooling");
532 	}
533 	if (verbose)
534 		aprint_normal("\n");
535 
536 	for (i = 0; i < ATZ_NLEVELS; i++)
537 		acpitz_sane_temp(&sc->sc_zone.ac[i]);
538 
539 	acpitz_power_off(sc);
540 	sc->sc_first = 0;
541 }
542 
543 
544 static void
545 acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
546 {
547 	device_t dv = opaque;
548 	ACPI_OSD_EXEC_CALLBACK func = NULL;
549 	const char *name;
550 	ACPI_STATUS rv;
551 
552 	switch (notify) {
553 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
554 		func = acpitz_get_status;
555 		name = "status check";
556 		break;
557 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
558 	case ACPI_NOTIFY_DeviceListsChanged:
559 		func = acpitz_get_zone_quiet;
560 		name = "get zone";
561 		break;
562 	default:
563 		aprint_debug_dev(dv,
564 		    "received unhandled notify message 0x%x\n", notify);
565 		return;
566 	}
567 
568 	KASSERT(func != NULL);
569 
570 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
571 	if (ACPI_FAILURE(rv))
572 		aprint_debug_dev(dv, "unable to queue %s\n", name);
573 }
574 
575 static void
576 acpitz_sane_temp(UINT32 *tmp)
577 {
578 	/* Sane temperatures are beteen 0 and 150 C */
579 	if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
580 		*tmp = ATZ_TMP_INVALID;
581 }
582 
583 static int
584 acpitz_get_integer(device_t dv, const char *cm, UINT32 *val)
585 {
586 	struct acpitz_softc *sc = device_private(dv);
587 	ACPI_STATUS rv;
588 	ACPI_INTEGER tmp;
589 
590 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp);
591 	if (ACPI_FAILURE(rv)) {
592 #ifdef ACPI_DEBUG
593 		aprint_debug_dev(dv, "failed to evaluate %s: %s\n",
594 		    cm, AcpiFormatException(rv));
595 #endif
596 		*val = ATZ_TMP_INVALID;
597 		return 1;
598 	}
599 
600 	*val = tmp;
601 
602 	return 0;
603 }
604 
605 static int
606 acpitz_get_fanspeed(device_t dv,
607     UINT32 *fanmin, UINT32 *fanmax, UINT32 *fancurrent)
608 {
609 	struct acpitz_softc *sc = device_private(dv);
610 	ACPI_STATUS rv;
611 	ACPI_HANDLE handle;
612 	ACPI_INTEGER fmin, fmax, fcurr;
613 	int rc = 0;
614 
615 	handle = sc->sc_devnode->ad_handle;
616 	rv = acpi_eval_integer(handle, "FMIN", &fmin);
617 	if (ACPI_FAILURE(rv)) {
618 		fmin = ATZ_TMP_INVALID;
619 		rc = 1;
620 	}
621 	rv = acpi_eval_integer(handle, "FMAX", &fmax);
622 	if (ACPI_FAILURE(rv)) {
623 		fmax = ATZ_TMP_INVALID;
624 		rc = 1;
625 	}
626 	rv = acpi_eval_integer(handle, "FRSP", &fcurr);
627 	if (ACPI_FAILURE(rv)) {
628 		fcurr = ATZ_TMP_INVALID;
629 		rc = 1;
630 	}
631 
632 	if (fanmin)
633 		*fanmin = fmin;
634 	if (fanmax)
635 		*fanmax = fmax;
636 	if (fancurrent)
637 		*fancurrent = fcurr;
638 	return rc;
639 }
640 
641 #ifdef notyet
642 static ACPI_STATUS
643 acpitz_set_fanspeed(device_t dv, UINT32 fanspeed)
644 {
645 	struct acpitz_softc *sc = device_private(dv);
646 	ACPI_STATUS rv;
647 	ACPI_HANDLE handle;
648 	handle = sc->sc_devnode->ad_handle;
649 
650 	rv = acpi_eval_set_integer(handle, "FSSP", fanspeed);
651 	if (ACPI_FAILURE(rv))
652 		aprint_debug_dev(dv, "failed to set fanspeed to %u rpm: %s\n",
653 			fanspeed, AcpiFormatException(rv));
654 	return rv;
655 }
656 #endif
657 
658 static void
659 acpitz_tick(void *opaque)
660 {
661 	device_t dv = opaque;
662 	struct acpitz_softc *sc = device_private(dv);
663 
664 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
665 
666 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
667 }
668 
669 static void
670 acpitz_init_envsys(device_t dv)
671 {
672 	struct acpitz_softc *sc = device_private(dv);
673 
674 	sc->sc_sme = sysmon_envsys_create();
675 	sc->sc_sme->sme_get_limits = acpitz_get_limits;
676 	sc->sc_sme->sme_cookie = sc;
677 	sc->sc_sme->sme_name = device_xname(dv);
678 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
679 
680 	sc->sc_temp_sensor.monitor = true;
681 	sc->sc_temp_sensor.flags = ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
682 	sc->sc_temp_sensor.units = ENVSYS_STEMP;
683 	strlcpy(sc->sc_temp_sensor.desc,
684 	    sc->sc_zone.name, sizeof(sc->sc_temp_sensor.desc));
685 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_temp_sensor))
686 		goto out;
687 
688 	if (sc->sc_have_fan) {
689 		sc->sc_fan_sensor.monitor = true;
690 		sc->sc_fan_sensor.flags =
691 		    ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
692 		sc->sc_fan_sensor.units = ENVSYS_SFANRPM;
693 		strlcpy(sc->sc_fan_sensor.desc,
694 		    "FAN", sizeof(sc->sc_fan_sensor.desc));
695 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_fan_sensor))
696 			/* ignore error because fan sensor is optional */
697 			aprint_error_dev(dv, "unable to attach fan sensor\n");
698 	}
699 
700 	/* hook into sysmon */
701 	if (sysmon_envsys_register(sc->sc_sme) == 0)
702 		return;
703 
704 out:
705 	aprint_error_dev(dv, "unable to register with sysmon\n");
706 	sysmon_envsys_destroy(sc->sc_sme);
707 }
708 
709 static void
710 acpitz_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
711 		  sysmon_envsys_lim_t *limits, uint32_t *props)
712 {
713 	struct acpitz_softc *sc = sme->sme_cookie;
714 	int i;
715 
716 	switch (edata->units) {
717 	case ENVSYS_STEMP:
718 		*props = 0;
719 		if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
720 			*props |= PROP_CRITMAX;
721 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.hot);
722 		} else if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
723 			*props |= PROP_CRITMAX;
724 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.crt);
725 		}
726 		for (i = 0; i < ATZ_NLEVELS; i++) {
727 			if (sc->sc_zone.ac[i] != ATZ_TMP_INVALID) {
728 				limits->sel_warnmax =
729 				    ATZ2UKELVIN(sc->sc_zone.ac[i]);
730 				*props |= PROP_WARNMAX;
731 				break;
732 			}
733 		}
734 		break;
735 
736 	case ENVSYS_SFANRPM:
737 		*props = 0;
738 		if (sc->sc_zone.fanmin != ATZ_TMP_INVALID) {
739 			*props |= PROP_WARNMIN;
740 			limits->sel_warnmin = sc->sc_zone.fanmin;
741 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MIN;
742 		}
743 		if (sc->sc_zone.fanmax != ATZ_TMP_INVALID) {
744 			*props |= PROP_WARNMAX;
745 			limits->sel_warnmax = sc->sc_zone.fanmax;
746 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MAX;
747 		}
748 		break;
749 	}
750 }
751