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