Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.48
      1 /* $NetBSD: acpi_tz.c,v 1.48 2009/11/23 14:42:39 cegger 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.48 2009/11/23 14:42:39 cegger 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 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 };
    111 
    112 struct acpitz_softc {
    113 	struct acpi_devnode *sc_devnode;
    114 	struct acpitz_zone sc_zone;
    115 	struct callout sc_callout;
    116 	struct sysmon_envsys *sc_sme;
    117 	envsys_data_t sc_sensor;
    118 	int sc_active;		/* active cooling level */
    119 	int sc_flags;
    120 	int sc_rate;		/* tz poll rate */
    121 	int sc_zone_expire;
    122 
    123 	int sc_first;
    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 *);
    142 
    143 CFATTACH_DECL_NEW(acpitz, sizeof(struct acpitz_softc), acpitz_match,
    144     acpitz_attach, NULL, NULL);
    145 
    146 /*
    147  * acpitz_match: autoconf(9) match routine
    148  */
    149 static int
    150 acpitz_match(device_t parent, cfdata_t match, void *aux)
    151 {
    152 	struct acpi_attach_args *aa = aux;
    153 
    154 	if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
    155 		return 0;
    156 
    157 	return 1;
    158 }
    159 
    160 /*
    161  * acpitz_attach: autoconf(9) attach routine
    162  */
    163 static void
    164 acpitz_attach(device_t parent, device_t self, void *aux)
    165 {
    166 	struct acpitz_softc *sc = device_private(self);
    167 	struct acpi_attach_args *aa = aux;
    168 	ACPI_STATUS rv;
    169 	ACPI_INTEGER v;
    170 
    171 #if 0
    172 	sc->sc_flags = ATZ_F_VERBOSE;
    173 #endif
    174 	sc->sc_devnode = aa->aa_node;
    175 
    176 	aprint_naive("\n");
    177 
    178 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP", &v);
    179 	if (ACPI_FAILURE(rv))
    180 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    181 	else
    182 		sc->sc_zone.tzp = v;
    183 
    184 	aprint_debug(" sample rate %d.%ds\n",
    185 	    sc->sc_zone.tzp / 10, sc->sc_zone.tzp % 10);
    186 
    187 	/* XXX a value of 0 means "polling is not necessary" */
    188 	if (sc->sc_zone.tzp == 0)
    189 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    190 
    191 	sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
    192 	sc->sc_first = 1;
    193 
    194 	acpitz_get_zone(self, 1);
    195 	acpitz_get_status(self);
    196 
    197 	rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
    198 	    ACPI_SYSTEM_NOTIFY, acpitz_notify_handler, self);
    199 	if (ACPI_FAILURE(rv)) {
    200 		aprint_error(": unable to install SYSTEM NOTIFY handler: %s\n",
    201 		    AcpiFormatException(rv));
    202 		return;
    203 	}
    204 
    205 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
    206 	callout_setfunc(&sc->sc_callout, acpitz_tick, self);
    207 
    208 	acpitz_init_envsys(self);
    209 
    210 	if (!pmf_device_register(self, NULL, NULL))
    211 		aprint_error(": couldn't establish power handler\n");
    212 
    213 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    214 }
    215 
    216 static void
    217 acpitz_get_zone_quiet(void *opaque)
    218 {
    219 	acpitz_get_zone(opaque, 0);
    220 }
    221 
    222 static void
    223 acpitz_get_status(void *opaque)
    224 {
    225 	device_t dv = opaque;
    226 	struct acpitz_softc *sc = device_private(dv);
    227 	UINT32 tmp, active;
    228 	int i, flags;
    229 
    230 	sc->sc_zone_expire--;
    231 	if (sc->sc_zone_expire <= 0) {
    232 		sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
    233 		if (sc->sc_flags & ATZ_F_VERBOSE)
    234 			printf("%s: force refetch zone\n", device_xname(dv));
    235 		acpitz_get_zone(dv, 0);
    236 	}
    237 
    238 	if (acpitz_get_integer(dv, "_TMP", &tmp)) {
    239 		aprint_error_dev(dv, "failed to evaluate _TMP\n");
    240 		return;
    241 	}
    242 	sc->sc_zone.tmp = tmp;
    243 	/* XXX sanity check for tmp here? */
    244 
    245 	/*
    246 	 * The temperature unit for envsys(4) is microKelvin, so convert to
    247 	 * that from ACPI's microKelvin. Also, the ACPI specification assumes
    248 	 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(4),
    249 	 * so we correct for that too.
    250 	 */
    251 	sc->sc_sensor.value_cur = ATZ2UKELVIN(sc->sc_zone.tmp);
    252 	sc->sc_sensor.state = ENVSYS_SVALID;
    253 
    254 	if (sc->sc_flags & ATZ_F_VERBOSE)
    255 		acpitz_print_status(dv);
    256 
    257 	if (sc->sc_flags & ATZ_F_PASSIVEONLY) {
    258 		/* Passive Cooling: XXX not yet */
    259 
    260 	} else {
    261 		/* Active Cooling */
    262 
    263 		/* temperature threshold: _AC0 > ... > _AC9 */
    264 		active = ATZ_ACTIVE_NONE;
    265 		for (i = ATZ_NLEVELS - 1; i >= 0; i--) {
    266 			if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID)
    267 				continue;
    268 
    269 			/* we want to keep highest cooling mode in 'active' */
    270 			if (sc->sc_zone.ac[i] <= tmp)
    271 				active = i;
    272 		}
    273 		if (active != ATZ_ACTIVE_NONE)
    274 			sc->sc_sensor.state = ENVSYS_SWARNOVER;
    275 
    276 		flags = sc->sc_flags &
    277 		    ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
    278 		if (sc->sc_zone.psv != ATZ_TMP_INVALID &&
    279 		    tmp >= sc->sc_zone.psv)
    280 			flags |= ATZ_F_PASSIVE;
    281 		if (sc->sc_zone.hot != ATZ_TMP_INVALID &&
    282 		    tmp >= sc->sc_zone.hot)
    283 			flags |= ATZ_F_HOT;
    284 		if (sc->sc_zone.crt != ATZ_TMP_INVALID &&
    285 		    tmp >= sc->sc_zone.crt)
    286 			flags |= ATZ_F_CRITICAL;
    287 
    288 		if (flags != sc->sc_flags) {
    289 			int changed = (sc->sc_flags ^ flags) & flags;
    290 			sc->sc_flags = flags;
    291 			if (changed & ATZ_F_CRITICAL) {
    292 				sc->sc_sensor.state = ENVSYS_SCRITOVER;
    293 				aprint_debug_dev(dv,
    294 				    "zone went critical at temp %sC\n",
    295 				    acpitz_celcius_string(tmp));
    296 			} else if (changed & ATZ_F_HOT) {
    297 				sc->sc_sensor.state = ENVSYS_SCRITOVER;
    298 				aprint_debug_dev(dv,
    299 				    "zone went hot at temp %sC\n",
    300 				    acpitz_celcius_string(tmp));
    301 			}
    302 		}
    303 
    304 		/* power on fans */
    305 		if (sc->sc_active != active) {
    306 			if (sc->sc_active != ATZ_ACTIVE_NONE)
    307 				acpitz_power_zone(sc, sc->sc_active, 0);
    308 
    309 			if (active != ATZ_ACTIVE_NONE) {
    310 				if (sc->sc_flags & ATZ_F_VERBOSE)
    311 					printf("%s: active cooling level %u\n",
    312 					    device_xname(dv), active);
    313 				acpitz_power_zone(sc, active, 1);
    314 			}
    315 
    316 			sc->sc_active = active;
    317 		}
    318 	}
    319 
    320 	return;
    321 }
    322 
    323 static char *
    324 acpitz_celcius_string(int dk)
    325 {
    326 	static char buf[10];
    327 
    328 	snprintf(buf, sizeof(buf), "%d.%d", (dk - ATZ_ZEROC) / 10,
    329 	    (dk - ATZ_ZEROC) % 10);
    330 
    331 	return buf;
    332 }
    333 
    334 static void
    335 acpitz_print_status(device_t dv)
    336 {
    337 	struct acpitz_softc *sc = device_private(dv);
    338 
    339 	printf("%s: zone temperature is now %sC\n", device_xname(dv),
    340 	    acpitz_celcius_string(sc->sc_zone.tmp));
    341 
    342 	return;
    343 }
    344 
    345 static ACPI_STATUS
    346 acpitz_switch_cooler(ACPI_OBJECT *obj, void *arg)
    347 {
    348 	ACPI_HANDLE cooler;
    349 	ACPI_STATUS rv;
    350 	int pwr_state, flag;
    351 
    352 	flag = *(int *)arg;
    353 
    354 	if (flag)
    355 		pwr_state = ACPI_STATE_D0;
    356 	else
    357 		pwr_state = ACPI_STATE_D3;
    358 
    359 	switch(obj->Type) {
    360 	case ACPI_TYPE_LOCAL_REFERENCE:
    361 	case ACPI_TYPE_ANY:
    362 		cooler = obj->Reference.Handle;
    363 		break;
    364 	case ACPI_TYPE_STRING:
    365 		rv = AcpiGetHandle(NULL, obj->String.Pointer, &cooler);
    366 		if (ACPI_FAILURE(rv)) {
    367 			printf("acpitz_switch_cooler: "
    368 			    "failed to get handler from %s\n",
    369 			    obj->String.Pointer);
    370 			return rv;
    371 		}
    372 		break;
    373 	default:
    374 		printf("acpitz_switch_cooler: "
    375 		    "unknown power type: %d\n", obj->Type);
    376 		return AE_OK;
    377 	}
    378 
    379 	rv = acpi_pwr_switch_consumer(cooler, pwr_state);
    380 	if (rv != AE_BAD_PARAMETER && ACPI_FAILURE(rv)) {
    381 		printf("acpitz_switch_cooler: "
    382 		    "failed to change state for %s: %s\n",
    383 		    acpi_name(obj->Reference.Handle),
    384 		    AcpiFormatException(rv));
    385 	}
    386 
    387 	return AE_OK;
    388 }
    389 
    390 /*
    391  * acpitz_power_zone:
    392  *	power on or off the i:th part of the zone zone
    393  */
    394 static void
    395 acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
    396 {
    397 	KASSERT(i >= 0 && i < ATZ_NLEVELS);
    398 
    399 	acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
    400 	    acpitz_switch_cooler, &on);
    401 }
    402 
    403 
    404 /*
    405  * acpitz_power_off:
    406  *	power off parts of the zone
    407  */
    408 static void
    409 acpitz_power_off(struct acpitz_softc *sc)
    410 {
    411 	int i;
    412 
    413 	for (i = 0 ; i < ATZ_NLEVELS; i++) {
    414 		if (sc->sc_zone.al[i].Pointer == NULL)
    415 			continue;
    416 		acpitz_power_zone(sc, i, 0);
    417 	}
    418 	sc->sc_active = ATZ_ACTIVE_NONE;
    419 	sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
    420 }
    421 
    422 static void
    423 acpitz_get_zone(void *opaque, int verbose)
    424 {
    425 	device_t dv = opaque;
    426 	struct acpitz_softc *sc = device_private(dv);
    427 	ACPI_STATUS rv;
    428 	char buf[8];
    429 	int i, valid_levels;
    430 
    431 	if (!sc->sc_first) {
    432 		acpitz_power_off(sc);
    433 
    434 		for (i = 0; i < ATZ_NLEVELS; i++) {
    435 			if (sc->sc_zone.al[i].Pointer != NULL)
    436 				ACPI_FREE(sc->sc_zone.al[i].Pointer);
    437 			sc->sc_zone.al[i].Pointer = NULL;
    438 		}
    439 	} else
    440 		aprint_normal(":");
    441 
    442 	valid_levels = 0;
    443 
    444 	for (i = 0; i < ATZ_NLEVELS; i++) {
    445 		ACPI_OBJECT *obj;
    446 
    447 		snprintf(buf, sizeof(buf), "_AC%d", i);
    448 		if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
    449 			continue;
    450 
    451 		snprintf(buf, sizeof(buf), "_AL%d", i);
    452 		rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf,
    453 		    &sc->sc_zone.al[i]);
    454 		if (ACPI_FAILURE(rv)) {
    455 			sc->sc_zone.al[i].Pointer = NULL;
    456 			continue;
    457 		}
    458 
    459 		obj = sc->sc_zone.al[i].Pointer;
    460 		if (obj != NULL) {
    461 			if (obj->Type != ACPI_TYPE_PACKAGE) {
    462 				aprint_error("%d not package\n", i);
    463 				ACPI_FREE(obj);
    464 				sc->sc_zone.al[i].Pointer = NULL;
    465 				continue;
    466 			}
    467 		}
    468 
    469 		if (sc->sc_first)
    470 			aprint_normal(" active cooling level %d: %sC", i,
    471 			    acpitz_celcius_string(sc->sc_zone.ac[i]));
    472 
    473 		valid_levels++;
    474 	}
    475 
    476 	acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
    477 	acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
    478 	acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
    479 	sc->sc_zone.psl.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    480 	sc->sc_zone.psl.Pointer = NULL;
    481 	AcpiEvaluateObject(sc, "_PSL", NULL, &sc->sc_zone.psl);
    482 	acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
    483 	acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
    484 	acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
    485 
    486 	/* ACPI spec: If _RTV is not present or present and zero,
    487 	 * values are absolute. */
    488 	acpitz_get_integer(dv, "_RTV", &sc->sc_zone.rtv);
    489 	if (sc->sc_zone.rtv == ATZ_TMP_INVALID)
    490 		sc->sc_zone.rtv = 0;
    491 
    492 
    493 	acpitz_sane_temp(&sc->sc_zone.tmp);
    494 	acpitz_sane_temp(&sc->sc_zone.crt);
    495 	acpitz_sane_temp(&sc->sc_zone.hot);
    496 	acpitz_sane_temp(&sc->sc_zone.psv);
    497 
    498 	if (verbose) {
    499 		if (sc->sc_zone.crt != ATZ_TMP_INVALID)
    500 			aprint_normal(" critical %sC",
    501 			    acpitz_celcius_string(sc->sc_zone.crt));
    502 		if (sc->sc_zone.hot != ATZ_TMP_INVALID)
    503 			aprint_normal(" hot %sC",
    504 			    acpitz_celcius_string(sc->sc_zone.hot));
    505 		if (sc->sc_zone.psv != ATZ_TMP_INVALID)
    506 			aprint_normal(" passive %sC",
    507 			    acpitz_celcius_string(sc->sc_zone.tmp));
    508 	}
    509 
    510 	if (valid_levels == 0) {
    511 		sc->sc_flags |= ATZ_F_PASSIVEONLY;
    512 		if (sc->sc_first)
    513 			aprint_normal(", passive cooling");
    514 	}
    515 	if (verbose)
    516 		aprint_normal("\n");
    517 
    518 	for (i = 0; i < ATZ_NLEVELS; i++)
    519 		acpitz_sane_temp(&sc->sc_zone.ac[i]);
    520 
    521 	acpitz_power_off(sc);
    522 	sc->sc_first = 0;
    523 }
    524 
    525 
    526 static void
    527 acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    528 {
    529 	device_t dv = opaque;
    530 	ACPI_OSD_EXEC_CALLBACK func = NULL;
    531 	const char *name;
    532 	ACPI_STATUS rv;
    533 
    534 	switch (notify) {
    535 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    536 		func = acpitz_get_status;
    537 		name = "status check";
    538 		break;
    539 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    540 	case ACPI_NOTIFY_DeviceListsChanged:
    541 		func = acpitz_get_zone_quiet;
    542 		name = "get zone";
    543 		break;
    544 	default:
    545 		aprint_debug_dev(dv,
    546 		    "received unhandled notify message 0x%x\n", notify);
    547 		return;
    548 	}
    549 
    550 	KASSERT(func != NULL);
    551 
    552 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
    553 	if (ACPI_FAILURE(rv))
    554 		aprint_debug_dev(dv, "unable to queue %s\n", name);
    555 }
    556 
    557 static void
    558 acpitz_sane_temp(UINT32 *tmp)
    559 {
    560 	/* Sane temperatures are beteen 0 and 150 C */
    561 	if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
    562 		*tmp = ATZ_TMP_INVALID;
    563 }
    564 
    565 static int
    566 acpitz_get_integer(device_t dv, const char *cm, UINT32 *val)
    567 {
    568 	struct acpitz_softc *sc = device_private(dv);
    569 	ACPI_STATUS rv;
    570 	ACPI_INTEGER tmp;
    571 
    572 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp);
    573 	if (ACPI_FAILURE(rv)) {
    574 #ifdef ACPI_DEBUG
    575 		aprint_debug_dev(dv, "failed to evaluate %s: %s\n",
    576 		    cm, AcpiFormatException(rv));
    577 #endif
    578 		*val = ATZ_TMP_INVALID;
    579 		return 1;
    580 	}
    581 
    582 	*val = tmp;
    583 
    584 	return 0;
    585 }
    586 
    587 static void
    588 acpitz_tick(void *opaque)
    589 {
    590 	device_t dv = opaque;
    591 	struct acpitz_softc *sc = device_private(dv);
    592 
    593 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
    594 
    595 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    596 }
    597 
    598 static void
    599 acpitz_init_envsys(device_t dv)
    600 {
    601 	struct acpitz_softc *sc = device_private(dv);
    602 
    603 	sc->sc_sme = sysmon_envsys_create();
    604 	sc->sc_sme->sme_get_limits = acpitz_get_limits;
    605 	sc->sc_sme->sme_cookie = sc;
    606 	sc->sc_sme->sme_name = device_xname(dv);
    607 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
    608 	sc->sc_sensor.monitor = true;
    609 	sc->sc_sensor.flags = ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
    610 	sc->sc_sensor.units = ENVSYS_STEMP;
    611 	strlcpy(sc->sc_sensor.desc, "temperature", sizeof(sc->sc_sensor.desc));
    612 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor))
    613 		goto out;
    614 
    615 	/* hook into sysmon */
    616 	if (sysmon_envsys_register(sc->sc_sme) == 0)
    617 		return;
    618 out:
    619 	aprint_error_dev(dv, "unable to register with sysmon\n");
    620 	sysmon_envsys_destroy(sc->sc_sme);
    621 }
    622 
    623 static void
    624 acpitz_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    625 		  sysmon_envsys_lim_t *limits)
    626 {
    627 	struct acpitz_softc *sc = sme->sme_cookie;
    628 	int i;
    629 
    630 	limits->sel_flags = 0;
    631 
    632 	if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
    633 		limits->sel_flags |= PROP_CRITMAX;
    634 		limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.hot);
    635 	} else if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
    636 		limits->sel_flags |= PROP_CRITMAX;
    637 		limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.crt);
    638 	}
    639 	for (i = 0; i < ATZ_NLEVELS; i++)
    640 		if (sc->sc_zone.ac[i] != ATZ_TMP_INVALID) {
    641 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.ac[i]);
    642 			limits->sel_flags |= PROP_WARNMAX;
    643 			break;
    644 		}
    645 }
    646