Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.55
      1 /* $NetBSD: acpi_tz.c,v 1.55 2010/01/18 17:09:17 jruoho 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.55 2010/01/18 17:09:17 jruoho 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 in tenths degrees 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 tenths of degrees Kelvin, and that the ACPI specification assumes
     79  * that 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 *);
    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_SYSTEM_NOTIFY, acpitz_notify_handler, self);
    219 	if (ACPI_FAILURE(rv)) {
    220 		aprint_error(": unable to install SYSTEM NOTIFY handler: %s\n",
    221 		    AcpiFormatException(rv));
    222 		return;
    223 	}
    224 
    225 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
    226 	callout_setfunc(&sc->sc_callout, acpitz_tick, self);
    227 
    228 	acpitz_init_envsys(self);
    229 
    230 	if (!pmf_device_register(self, NULL, NULL))
    231 		aprint_error(": couldn't establish power handler\n");
    232 
    233 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    234 }
    235 
    236 static void
    237 acpitz_get_zone_quiet(void *opaque)
    238 {
    239 	acpitz_get_zone(opaque, 0);
    240 }
    241 
    242 static void
    243 acpitz_get_status(void *opaque)
    244 {
    245 	device_t dv = opaque;
    246 	struct acpitz_softc *sc = device_private(dv);
    247 	UINT32 tmp, active;
    248 	int i, flags;
    249 	UINT32 fmin, fmax, fcurrent;
    250 
    251 	sc->sc_zone_expire--;
    252 	if (sc->sc_zone_expire <= 0) {
    253 		sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
    254 		if (sc->sc_flags & ATZ_F_VERBOSE)
    255 			printf("%s: force refetch zone\n", device_xname(dv));
    256 		acpitz_get_zone(dv, 0);
    257 	}
    258 
    259 	if (acpitz_get_integer(dv, "_TMP", &tmp)) {
    260 		aprint_error_dev(dv, "failed to evaluate _TMP\n");
    261 		return;
    262 	}
    263 
    264 	sc->sc_zone.prevtmp = sc->sc_zone.tmp;
    265 	sc->sc_zone.tmp = tmp;
    266 	if (sc->sc_first)
    267 		sc->sc_zone.prevtmp = tmp;
    268 	/* XXX sanity check for tmp here? */
    269 
    270 	if (acpitz_get_fanspeed(dv, &fmin, &fmax, &fcurrent) == 0) {
    271 		if (fcurrent != ATZ_TMP_INVALID)
    272 			sc->sc_zone.fancurrent = fcurrent;
    273 	}
    274 
    275 	/*
    276 	 * The temperature unit for envsys(4) is microKelvin, so convert to
    277 	 * that from ACPI's microKelvin. Also, the ACPI specification assumes
    278 	 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(4),
    279 	 * so we correct for that too.
    280 	 */
    281 	sc->sc_temp_sensor.value_cur = ATZ2UKELVIN(sc->sc_zone.tmp);
    282 	sc->sc_temp_sensor.state = ENVSYS_SVALID;
    283 
    284 	sc->sc_fan_sensor.value_cur = sc->sc_zone.fancurrent;
    285 	sc->sc_fan_sensor.state = ENVSYS_SVALID;
    286 
    287 	if (sc->sc_flags & ATZ_F_VERBOSE)
    288 		acpitz_print_status(dv);
    289 
    290 	if (sc->sc_flags & ATZ_F_PASSIVEONLY) {
    291 		/* Passive Cooling: XXX not yet */
    292 
    293 	} else {
    294 		/* Active Cooling */
    295 
    296 		/* temperature threshold: _AC0 > ... > _AC9 */
    297 		active = ATZ_ACTIVE_NONE;
    298 		for (i = ATZ_NLEVELS - 1; i >= 0; i--) {
    299 			if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID)
    300 				continue;
    301 
    302 			/* we want to keep highest cooling mode in 'active' */
    303 			if (sc->sc_zone.ac[i] <= tmp)
    304 				active = i;
    305 		}
    306 
    307 		flags = sc->sc_flags &
    308 		    ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
    309 		if (sc->sc_zone.psv != ATZ_TMP_INVALID &&
    310 		    tmp >= sc->sc_zone.psv)
    311 			flags |= ATZ_F_PASSIVE;
    312 		if (sc->sc_zone.hot != ATZ_TMP_INVALID &&
    313 		    tmp >= sc->sc_zone.hot)
    314 			flags |= ATZ_F_HOT;
    315 		if (sc->sc_zone.crt != ATZ_TMP_INVALID &&
    316 		    tmp >= sc->sc_zone.crt)
    317 			flags |= ATZ_F_CRITICAL;
    318 
    319 		if (flags != sc->sc_flags) {
    320 			int changed = (sc->sc_flags ^ flags) & flags;
    321 			sc->sc_flags = flags;
    322 			if (changed & ATZ_F_CRITICAL) {
    323 				sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
    324 				aprint_debug_dev(dv,
    325 				    "zone went critical at temp %sC\n",
    326 				    acpitz_celcius_string(tmp));
    327 			} else if (changed & ATZ_F_HOT) {
    328 				sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
    329 				aprint_debug_dev(dv,
    330 				    "zone went hot at temp %sC\n",
    331 				    acpitz_celcius_string(tmp));
    332 			}
    333 		}
    334 
    335 		/* power on fans */
    336 		if (sc->sc_active != active) {
    337 			if (sc->sc_active != ATZ_ACTIVE_NONE)
    338 				acpitz_power_zone(sc, sc->sc_active, 0);
    339 
    340 			if (active != ATZ_ACTIVE_NONE) {
    341 				if (sc->sc_flags & ATZ_F_VERBOSE)
    342 					printf("%s: active cooling level %u\n",
    343 					    device_xname(dv), active);
    344 				acpitz_power_zone(sc, active, 1);
    345 			} else if (sc->sc_flags & ATZ_F_VERBOSE)
    346 				printf("%s: no active cooling level\n",
    347 				    device_xname(dv));
    348 
    349 			sc->sc_active = active;
    350 		}
    351 	}
    352 
    353 	return;
    354 }
    355 
    356 static char *
    357 acpitz_celcius_string(int dk)
    358 {
    359 	static char buf[10];
    360 
    361 	snprintf(buf, sizeof(buf), "%d.%d", (dk - ATZ_ZEROC) / 10,
    362 	    (dk - ATZ_ZEROC) % 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 	switch(obj->Type) {
    397 	case ACPI_TYPE_LOCAL_REFERENCE:
    398 	case ACPI_TYPE_ANY:
    399 		cooler = obj->Reference.Handle;
    400 		break;
    401 	case ACPI_TYPE_STRING:
    402 		rv = AcpiGetHandle(NULL, obj->String.Pointer, &cooler);
    403 		if (ACPI_FAILURE(rv)) {
    404 			aprint_error("%s: failed to get handler from %s\n",
    405 			    __func__, obj->String.Pointer);
    406 			return rv;
    407 		}
    408 		break;
    409 	default:
    410 		aprint_error("%s: unknown power type: %u\n",
    411 		    __func__, obj->Type);
    412 		return AE_OK;
    413 	}
    414 
    415 	rv = acpi_pwr_switch_consumer(cooler, pwr_state);
    416 	if (rv != AE_BAD_PARAMETER && ACPI_FAILURE(rv))
    417 		aprint_error("%s: failed to change state for %s: %s\n",
    418 		    __func__, acpi_name(cooler), AcpiFormatException(rv));
    419 
    420 	return AE_OK;
    421 }
    422 
    423 /*
    424  * acpitz_power_zone:
    425  *	power on or off the i:th part of the zone zone
    426  */
    427 static void
    428 acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
    429 {
    430 	KASSERT(i >= 0 && i < ATZ_NLEVELS);
    431 
    432 	acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
    433 	    acpitz_switch_cooler, &on);
    434 }
    435 
    436 
    437 /*
    438  * acpitz_power_off:
    439  *	power off parts of the zone
    440  */
    441 static void
    442 acpitz_power_off(struct acpitz_softc *sc)
    443 {
    444 	int i;
    445 
    446 	for (i = 0 ; i < ATZ_NLEVELS; i++) {
    447 		if (sc->sc_zone.al[i].Pointer == NULL)
    448 			continue;
    449 		acpitz_power_zone(sc, i, 0);
    450 	}
    451 	sc->sc_active = ATZ_ACTIVE_NONE;
    452 	sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
    453 }
    454 
    455 static void
    456 acpitz_get_zone(void *opaque, int verbose)
    457 {
    458 	device_t dv = opaque;
    459 	struct acpitz_softc *sc = device_private(dv);
    460 	ACPI_STATUS rv;
    461 	char buf[8];
    462 	int i, valid_levels;
    463 
    464 	if (!sc->sc_first) {
    465 		acpitz_power_off(sc);
    466 
    467 		for (i = 0; i < ATZ_NLEVELS; i++) {
    468 			if (sc->sc_zone.al[i].Pointer != NULL)
    469 				ACPI_FREE(sc->sc_zone.al[i].Pointer);
    470 			sc->sc_zone.al[i].Pointer = NULL;
    471 		}
    472 	} else
    473 		aprint_normal(":");
    474 
    475 	valid_levels = 0;
    476 
    477 	for (i = 0; i < ATZ_NLEVELS; i++) {
    478 		ACPI_OBJECT *obj;
    479 
    480 		snprintf(buf, sizeof(buf), "_AC%d", i);
    481 		if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
    482 			continue;
    483 
    484 		snprintf(buf, sizeof(buf), "_AL%d", i);
    485 		rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf,
    486 		    &sc->sc_zone.al[i]);
    487 		if (ACPI_FAILURE(rv)) {
    488 			sc->sc_zone.al[i].Pointer = NULL;
    489 			continue;
    490 		}
    491 
    492 		obj = sc->sc_zone.al[i].Pointer;
    493 		if (obj != NULL) {
    494 			if (obj->Type != ACPI_TYPE_PACKAGE) {
    495 				aprint_error("%d not package\n", i);
    496 				ACPI_FREE(obj);
    497 				sc->sc_zone.al[i].Pointer = NULL;
    498 				continue;
    499 			}
    500 		}
    501 
    502 		if (sc->sc_first)
    503 			aprint_normal(" active cooling level %d: %sC", i,
    504 			    acpitz_celcius_string(sc->sc_zone.ac[i]));
    505 
    506 		valid_levels++;
    507 	}
    508 
    509 	acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
    510 	acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
    511 	acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
    512 	acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
    513 	acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
    514 	acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
    515 
    516 #if 0
    517 	sc->sc_zone.psl.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    518 	sc->sc_zone.psl.Pointer = NULL;
    519 	AcpiEvaluateObject(sc->sc_devnode->ad_handle,
    520 	    "_PSL", NULL, &sc->sc_zone.psl);
    521 #endif
    522 
    523 	/* ACPI spec: If _RTV is not present or present and zero,
    524 	 * values are absolute. */
    525 	acpitz_get_integer(dv, "_RTV", &sc->sc_zone.rtv);
    526 	if (sc->sc_zone.rtv == ATZ_TMP_INVALID)
    527 		sc->sc_zone.rtv = 0;
    528 
    529 
    530 	acpitz_sane_temp(&sc->sc_zone.tmp);
    531 	acpitz_sane_temp(&sc->sc_zone.crt);
    532 	acpitz_sane_temp(&sc->sc_zone.hot);
    533 	acpitz_sane_temp(&sc->sc_zone.psv);
    534 
    535 	if (verbose) {
    536 		if (sc->sc_zone.crt != ATZ_TMP_INVALID)
    537 			aprint_normal(" critical %sC",
    538 			    acpitz_celcius_string(sc->sc_zone.crt));
    539 		if (sc->sc_zone.hot != ATZ_TMP_INVALID)
    540 			aprint_normal(" hot %sC",
    541 			    acpitz_celcius_string(sc->sc_zone.hot));
    542 		if (sc->sc_zone.psv != ATZ_TMP_INVALID)
    543 			aprint_normal(" passive %sC",
    544 			    acpitz_celcius_string(sc->sc_zone.tmp));
    545 	}
    546 
    547 	if (valid_levels == 0) {
    548 		sc->sc_flags |= ATZ_F_PASSIVEONLY;
    549 		if (sc->sc_first)
    550 			aprint_normal(", passive cooling");
    551 	}
    552 	if (verbose)
    553 		aprint_normal("\n");
    554 
    555 	for (i = 0; i < ATZ_NLEVELS; i++)
    556 		acpitz_sane_temp(&sc->sc_zone.ac[i]);
    557 
    558 	acpitz_power_off(sc);
    559 	sc->sc_first = 0;
    560 }
    561 
    562 
    563 static void
    564 acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    565 {
    566 	device_t dv = opaque;
    567 	ACPI_OSD_EXEC_CALLBACK func = NULL;
    568 	const char *name;
    569 	ACPI_STATUS rv;
    570 
    571 	switch (notify) {
    572 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    573 		func = acpitz_get_status;
    574 		name = "status check";
    575 		break;
    576 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    577 	case ACPI_NOTIFY_DeviceListsChanged:
    578 		func = acpitz_get_zone_quiet;
    579 		name = "get zone";
    580 		break;
    581 	default:
    582 		aprint_debug_dev(dv,
    583 		    "received unhandled notify message 0x%x\n", notify);
    584 		return;
    585 	}
    586 
    587 	KASSERT(func != NULL);
    588 
    589 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
    590 	if (ACPI_FAILURE(rv))
    591 		aprint_debug_dev(dv, "unable to queue %s\n", name);
    592 }
    593 
    594 static void
    595 acpitz_sane_temp(UINT32 *tmp)
    596 {
    597 	/* Sane temperatures are beteen 0 and 150 C */
    598 	if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
    599 		*tmp = ATZ_TMP_INVALID;
    600 }
    601 
    602 static int
    603 acpitz_get_integer(device_t dv, const char *cm, UINT32 *val)
    604 {
    605 	struct acpitz_softc *sc = device_private(dv);
    606 	ACPI_STATUS rv;
    607 	ACPI_INTEGER tmp;
    608 
    609 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp);
    610 	if (ACPI_FAILURE(rv)) {
    611 #ifdef ACPI_DEBUG
    612 		aprint_debug_dev(dv, "failed to evaluate %s: %s\n",
    613 		    cm, AcpiFormatException(rv));
    614 #endif
    615 		*val = ATZ_TMP_INVALID;
    616 		return 1;
    617 	}
    618 
    619 	*val = tmp;
    620 
    621 	return 0;
    622 }
    623 
    624 static int
    625 acpitz_get_fanspeed(device_t dv,
    626     UINT32 *fanmin, UINT32 *fanmax, UINT32 *fancurrent)
    627 {
    628 	struct acpitz_softc *sc = device_private(dv);
    629 	ACPI_STATUS rv;
    630 	ACPI_HANDLE handle;
    631 	ACPI_INTEGER fmin, fmax, fcurr;
    632 	int rc = 0;
    633 
    634 	handle = sc->sc_devnode->ad_handle;
    635 	rv = acpi_eval_integer(handle, "FMIN", &fmin);
    636 	if (ACPI_FAILURE(rv)) {
    637 		fmin = ATZ_TMP_INVALID;
    638 		rc = 1;
    639 	}
    640 	rv = acpi_eval_integer(handle, "FMAX", &fmax);
    641 	if (ACPI_FAILURE(rv)) {
    642 		fmax = ATZ_TMP_INVALID;
    643 		rc = 1;
    644 	}
    645 	rv = acpi_eval_integer(handle, "FRSP", &fcurr);
    646 	if (ACPI_FAILURE(rv)) {
    647 		fcurr = ATZ_TMP_INVALID;
    648 		rc = 1;
    649 	}
    650 
    651 	if (fanmin)
    652 		*fanmin = fmin;
    653 	if (fanmax)
    654 		*fanmax = fmax;
    655 	if (fancurrent)
    656 		*fancurrent = fcurr;
    657 	return rc;
    658 }
    659 
    660 #ifdef notyet
    661 static ACPI_STATUS
    662 acpitz_set_fanspeed(device_t dv, UINT32 fanspeed)
    663 {
    664 	struct acpitz_softc *sc = device_private(dv);
    665 	ACPI_STATUS rv;
    666 	ACPI_HANDLE handle;
    667 	handle = sc->sc_devnode->ad_handle;
    668 
    669 	rv = acpi_eval_set_integer(handle, "FSSP", fanspeed);
    670 	if (ACPI_FAILURE(rv))
    671 		aprint_debug_dev(dv, "failed to set fanspeed to %u rpm: %s\n",
    672 			fanspeed, AcpiFormatException(rv));
    673 	return rv;
    674 }
    675 #endif
    676 
    677 static void
    678 acpitz_tick(void *opaque)
    679 {
    680 	device_t dv = opaque;
    681 	struct acpitz_softc *sc = device_private(dv);
    682 
    683 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
    684 
    685 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    686 }
    687 
    688 static void
    689 acpitz_init_envsys(device_t dv)
    690 {
    691 	struct acpitz_softc *sc = device_private(dv);
    692 
    693 	sc->sc_sme = sysmon_envsys_create();
    694 	sc->sc_sme->sme_get_limits = acpitz_get_limits;
    695 	sc->sc_sme->sme_cookie = sc;
    696 	sc->sc_sme->sme_name = device_xname(dv);
    697 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
    698 
    699 	sc->sc_temp_sensor.monitor = true;
    700 	sc->sc_temp_sensor.flags = ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
    701 	sc->sc_temp_sensor.units = ENVSYS_STEMP;
    702 	strlcpy(sc->sc_temp_sensor.desc,
    703 	    sc->sc_zone.name, sizeof(sc->sc_temp_sensor.desc));
    704 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_temp_sensor))
    705 		goto out;
    706 
    707 	if (sc->sc_have_fan) {
    708 		sc->sc_fan_sensor.monitor = true;
    709 		sc->sc_fan_sensor.flags =
    710 		    ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
    711 		sc->sc_fan_sensor.units = ENVSYS_SFANRPM;
    712 		strlcpy(sc->sc_fan_sensor.desc,
    713 		    "FAN", sizeof(sc->sc_fan_sensor.desc));
    714 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_fan_sensor))
    715 			/* ignore error because fan sensor is optional */
    716 			aprint_error_dev(dv, "unable to attach fan sensor\n");
    717 	}
    718 
    719 	/* hook into sysmon */
    720 	if (sysmon_envsys_register(sc->sc_sme) == 0)
    721 		return;
    722 
    723 out:
    724 	aprint_error_dev(dv, "unable to register with sysmon\n");
    725 	sysmon_envsys_destroy(sc->sc_sme);
    726 }
    727 
    728 static void
    729 acpitz_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    730 		  sysmon_envsys_lim_t *limits)
    731 {
    732 	struct acpitz_softc *sc = sme->sme_cookie;
    733 	int i;
    734 
    735 	switch (edata->units) {
    736 	case ENVSYS_STEMP:
    737 		limits->sel_flags = 0;
    738 		if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
    739 			limits->sel_flags |= PROP_CRITMAX;
    740 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.hot);
    741 		} else if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
    742 			limits->sel_flags |= PROP_CRITMAX;
    743 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.crt);
    744 		}
    745 		for (i = 0; i < ATZ_NLEVELS; i++) {
    746 			if (sc->sc_zone.ac[i] != ATZ_TMP_INVALID) {
    747 				limits->sel_warnmax =
    748 				    ATZ2UKELVIN(sc->sc_zone.ac[i]);
    749 				limits->sel_flags |= PROP_WARNMAX;
    750 				break;
    751 			}
    752 		}
    753 		break;
    754 
    755 	case ENVSYS_SFANRPM:
    756 		limits->sel_flags = 0;
    757 		if (sc->sc_zone.fanmin != ATZ_TMP_INVALID) {
    758 			limits->sel_flags |= PROP_WARNMIN;
    759 			limits->sel_warnmin = sc->sc_zone.fanmin;
    760 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MIN;
    761 		}
    762 		if (sc->sc_zone.fanmax != ATZ_TMP_INVALID) {
    763 			limits->sel_flags |= PROP_WARNMAX;
    764 			limits->sel_warnmax = sc->sc_zone.fanmax;
    765 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MAX;
    766 		}
    767 		break;
    768 	}
    769 }
    770