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