1 /* $NetBSD: acpi_tz.c,v 1.41 2009/06/03 22:34:18 pgoyette Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * ACPI Thermal Zone driver 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.41 2009/06/03 22:34:18 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 <sys/mutex.h> 45 #include <dev/sysmon/sysmonvar.h> 46 47 #include <dev/acpi/acpica.h> 48 #include <dev/acpi/acpireg.h> 49 #include <dev/acpi/acpivar.h> 50 51 /* flags */ 52 #define ATZ_F_VERBOSE 0x01 /* show events to console */ 53 #define ATZ_F_CRITICAL 0x02 /* zone critical */ 54 #define ATZ_F_HOT 0x04 /* zone hot */ 55 #define ATZ_F_PASSIVE 0x08 /* zone passive cooling */ 56 #define ATZ_F_PASSIVEONLY 0x10 /* zone is passive cooling only */ 57 58 /* no active cooling level */ 59 #define ATZ_ACTIVE_NONE -1 60 61 /* constants */ 62 #define ATZ_TZP_RATE 300 /* default if no _TZP CM present (30 secs) */ 63 #define ATZ_NLEVELS 10 /* number of cooling levels, from ACPI spec */ 64 #define ATZ_ZEROC 2732 /* 0C in tenths degrees Kelvin */ 65 #define ATZ_TMP_INVALID 0xffffffff /* invalid temperature */ 66 #define ATZ_ZONE_EXPIRE 9000 /* zone info refetch interval (15min) */ 67 68 /* sensor indexes */ 69 #define ATZ_SENSOR_TEMP 0 /* thermal zone temperature */ 70 71 static int acpitz_match(device_t, cfdata_t, void *); 72 static void acpitz_attach(device_t, device_t, void *); 73 74 /* 75 * ACPI Temperature Zone information. Note all temperatures are reported 76 * in tenths of degrees Kelvin 77 */ 78 struct acpitz_zone { 79 /* Active cooling temperature threshold */ 80 UINT32 ac[ATZ_NLEVELS]; 81 /* Package of references to all active cooling devices for a level */ 82 ACPI_BUFFER al[ATZ_NLEVELS]; 83 /* Critical temperature threshold for system shutdown */ 84 UINT32 crt; 85 /* Critical temperature threshold for S4 sleep */ 86 UINT32 hot; 87 /* Package of references to processor objects for passive cooling */ 88 ACPI_BUFFER psl; 89 /* Passive cooling temperature threshold */ 90 UINT32 psv; 91 /* Thermal constants for use in passive cooling formulas */ 92 UINT32 tc1, tc2; 93 /* Current temperature of the thermal zone */ 94 UINT32 tmp; 95 /* Thermal sampling period for passive cooling, in tenths of seconds */ 96 UINT32 tsp; 97 /* Package of references to devices in this TZ (optional) */ 98 ACPI_BUFFER tzd; 99 /* Recommended TZ polling frequency, in tenths of seconds */ 100 UINT32 tzp; 101 }; 102 103 struct acpitz_softc { 104 struct acpi_devnode *sc_devnode; 105 struct acpitz_zone sc_zone; 106 struct callout sc_callout; 107 struct sysmon_envsys *sc_sme; 108 envsys_data_t sc_sensor; 109 kmutex_t sc_mtx; 110 int sc_active; /* active cooling level */ 111 int sc_flags; 112 int sc_rate; /* tz poll rate */ 113 int sc_zone_expire; 114 115 int sc_first; 116 }; 117 118 static void acpitz_get_status(void *); 119 static void acpitz_get_zone(void *, int); 120 static void acpitz_get_zone_quiet(void *); 121 static char *acpitz_celcius_string(int); 122 static void acpitz_print_status(device_t); 123 static void acpitz_power_off(struct acpitz_softc *); 124 static void acpitz_power_zone(struct acpitz_softc *, int, int); 125 static void acpitz_sane_temp(UINT32 *tmp); 126 static ACPI_STATUS 127 acpitz_switch_cooler(ACPI_OBJECT *, void *); 128 static void acpitz_notify_handler(ACPI_HANDLE, UINT32, void *); 129 static int acpitz_get_integer(device_t, const char *, UINT32 *); 130 static void acpitz_tick(void *); 131 static void acpitz_init_envsys(device_t); 132 133 CFATTACH_DECL_NEW(acpitz, sizeof(struct acpitz_softc), acpitz_match, 134 acpitz_attach, NULL, NULL); 135 136 /* 137 * acpitz_match: autoconf(9) match routine 138 */ 139 static int 140 acpitz_match(device_t parent, cfdata_t match, void *aux) 141 { 142 struct acpi_attach_args *aa = aux; 143 144 if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL) 145 return 0; 146 147 return 1; 148 } 149 150 /* 151 * acpitz_attach: autoconf(9) attach routine 152 */ 153 static void 154 acpitz_attach(device_t parent, device_t self, void *aux) 155 { 156 struct acpitz_softc *sc = device_private(self); 157 struct acpi_attach_args *aa = aux; 158 ACPI_STATUS rv; 159 ACPI_INTEGER v; 160 161 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE); 162 163 #if 0 164 sc->sc_flags = ATZ_F_VERBOSE; 165 #endif 166 sc->sc_devnode = aa->aa_node; 167 168 aprint_naive("\n"); 169 170 rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP", &v); 171 if (ACPI_FAILURE(rv)) 172 sc->sc_zone.tzp = ATZ_TZP_RATE; 173 else 174 sc->sc_zone.tzp = v; 175 176 aprint_debug(" sample rate %d.%ds\n", 177 sc->sc_zone.tzp / 10, sc->sc_zone.tzp % 10); 178 179 /* XXX a value of 0 means "polling is not necessary" */ 180 if (sc->sc_zone.tzp == 0) 181 sc->sc_zone.tzp = ATZ_TZP_RATE; 182 183 sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp; 184 sc->sc_first = 1; 185 186 acpitz_get_zone(self, 1); 187 acpitz_get_status(self); 188 189 rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle, 190 ACPI_SYSTEM_NOTIFY, acpitz_notify_handler, self); 191 if (ACPI_FAILURE(rv)) { 192 aprint_error(": unable to install SYSTEM NOTIFY handler: %s\n", 193 AcpiFormatException(rv)); 194 return; 195 } 196 197 callout_init(&sc->sc_callout, CALLOUT_MPSAFE); 198 callout_setfunc(&sc->sc_callout, acpitz_tick, self); 199 200 acpitz_init_envsys(self); 201 202 if (!pmf_device_register(self, NULL, NULL)) 203 aprint_error(": couldn't establish power handler\n"); 204 205 callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10); 206 } 207 208 static void 209 acpitz_get_zone_quiet(void *opaque) 210 { 211 acpitz_get_zone(opaque, 0); 212 } 213 214 static void 215 acpitz_get_status(void *opaque) 216 { 217 device_t dv = opaque; 218 struct acpitz_softc *sc = device_private(dv); 219 UINT32 tmp, active; 220 int i, flags; 221 222 sc->sc_zone_expire--; 223 if (sc->sc_zone_expire <= 0) { 224 sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp; 225 if (sc->sc_flags & ATZ_F_VERBOSE) 226 printf("%s: force refetch zone\n", device_xname(dv)); 227 acpitz_get_zone(dv, 0); 228 } 229 230 if (acpitz_get_integer(dv, "_TMP", &tmp)) { 231 aprint_error_dev(dv, "failed to evaluate _TMP\n"); 232 return; 233 } 234 sc->sc_zone.tmp = tmp; 235 /* XXX sanity check for tmp here? */ 236 237 /* 238 * The temperature unit for envsys(4) is microKelvin, so convert to 239 * that from ACPI's microKelvin. Also, the ACPI specification assumes 240 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(4), 241 * so we correct for that too. 242 */ 243 sc->sc_sensor.value_cur = sc->sc_zone.tmp * 100000 - 50000; 244 sc->sc_sensor.state = ENVSYS_SVALID; 245 246 if (sc->sc_flags & ATZ_F_VERBOSE) 247 acpitz_print_status(dv); 248 249 if (sc->sc_flags & ATZ_F_PASSIVEONLY) { 250 /* Passive Cooling: XXX not yet */ 251 252 } else { 253 /* Active Cooling */ 254 255 /* temperature threshold: _AC0 > ... > _AC9 */ 256 active = ATZ_ACTIVE_NONE; 257 for (i = ATZ_NLEVELS - 1; i >= 0; i--) { 258 if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID) 259 continue; 260 261 /* we want to keep highest cooling mode in 'active' */ 262 if (sc->sc_zone.ac[i] <= tmp) 263 active = i; 264 } 265 266 flags = sc->sc_flags & 267 ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE); 268 if (sc->sc_zone.psv != ATZ_TMP_INVALID && 269 tmp >= sc->sc_zone.psv) 270 flags |= ATZ_F_PASSIVE; 271 if (sc->sc_zone.hot != ATZ_TMP_INVALID && 272 tmp >= sc->sc_zone.hot) 273 flags |= ATZ_F_HOT; 274 if (sc->sc_zone.crt != ATZ_TMP_INVALID && 275 tmp >= sc->sc_zone.crt) 276 flags |= ATZ_F_CRITICAL; 277 278 if (flags != sc->sc_flags) { 279 int changed = (sc->sc_flags ^ flags) & flags; 280 sc->sc_flags = flags; 281 if (changed & ATZ_F_CRITICAL) { 282 sc->sc_sensor.state = ENVSYS_SCRITOVER; 283 aprint_debug_dev(dv, 284 "zone went critical at temp %sC\n", 285 acpitz_celcius_string(tmp)); 286 } else if (changed & ATZ_F_HOT) { 287 sc->sc_sensor.state = ENVSYS_SWARNOVER; 288 aprint_debug_dev(dv, 289 "zone went hot at temp %sC\n", 290 acpitz_celcius_string(tmp)); 291 } 292 } 293 294 /* power on fans */ 295 if (sc->sc_active != active) { 296 if (sc->sc_active != ATZ_ACTIVE_NONE) 297 acpitz_power_zone(sc, sc->sc_active, 0); 298 299 if (active != ATZ_ACTIVE_NONE) { 300 if (sc->sc_flags & ATZ_F_VERBOSE) 301 printf("%s: active cooling level %u\n", 302 device_xname(dv), active); 303 acpitz_power_zone(sc, active, 1); 304 } 305 306 sc->sc_active = active; 307 } 308 } 309 310 return; 311 } 312 313 static char * 314 acpitz_celcius_string(int dk) 315 { 316 static char buf[10]; 317 318 snprintf(buf, sizeof(buf), "%d.%d", (dk - ATZ_ZEROC) / 10, 319 (dk - ATZ_ZEROC) % 10); 320 321 return buf; 322 } 323 324 static void 325 acpitz_print_status(device_t dv) 326 { 327 struct acpitz_softc *sc = device_private(dv); 328 329 printf("%s: zone temperature is now %sC\n", device_xname(dv), 330 acpitz_celcius_string(sc->sc_zone.tmp)); 331 332 return; 333 } 334 335 static ACPI_STATUS 336 acpitz_switch_cooler(ACPI_OBJECT *obj, void *arg) 337 { 338 ACPI_HANDLE cooler; 339 ACPI_STATUS rv; 340 int pwr_state, flag; 341 342 flag = *(int *)arg; 343 344 if (flag) 345 pwr_state = ACPI_STATE_D0; 346 else 347 pwr_state = ACPI_STATE_D3; 348 349 switch(obj->Type) { 350 case ACPI_TYPE_LOCAL_REFERENCE: 351 case ACPI_TYPE_ANY: 352 cooler = obj->Reference.Handle; 353 break; 354 case ACPI_TYPE_STRING: 355 rv = AcpiGetHandle(NULL, obj->String.Pointer, &cooler); 356 if (ACPI_FAILURE(rv)) { 357 printf("acpitz_switch_cooler: " 358 "failed to get handler from %s\n", 359 obj->String.Pointer); 360 return rv; 361 } 362 break; 363 default: 364 printf("acpitz_switch_cooler: " 365 "unknown power type: %d\n", obj->Type); 366 return AE_OK; 367 } 368 369 rv = acpi_pwr_switch_consumer(cooler, pwr_state); 370 if (rv != AE_BAD_PARAMETER && ACPI_FAILURE(rv)) { 371 printf("acpitz_switch_cooler: " 372 "failed to change state for %s: %s\n", 373 acpi_name(obj->Reference.Handle), 374 AcpiFormatException(rv)); 375 } 376 377 return AE_OK; 378 } 379 380 /* 381 * acpitz_power_zone: 382 * power on or off the i:th part of the zone zone 383 */ 384 static void 385 acpitz_power_zone(struct acpitz_softc *sc, int i, int on) 386 { 387 KASSERT(i >= 0 && i < ATZ_NLEVELS); 388 389 acpi_foreach_package_object(sc->sc_zone.al[i].Pointer, 390 acpitz_switch_cooler, &on); 391 } 392 393 394 /* 395 * acpitz_power_off: 396 * power off parts of the zone 397 */ 398 static void 399 acpitz_power_off(struct acpitz_softc *sc) 400 { 401 int i; 402 403 for (i = 0 ; i < ATZ_NLEVELS; i++) { 404 if (sc->sc_zone.al[i].Pointer == NULL) 405 continue; 406 acpitz_power_zone(sc, i, 0); 407 } 408 sc->sc_active = ATZ_ACTIVE_NONE; 409 sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE); 410 } 411 412 static void 413 acpitz_get_zone(void *opaque, int verbose) 414 { 415 device_t dv = opaque; 416 struct acpitz_softc *sc = device_private(dv); 417 ACPI_STATUS rv; 418 char buf[8]; 419 int i, valid_levels; 420 421 if (!sc->sc_first) { 422 acpitz_power_off(sc); 423 424 for (i = 0; i < ATZ_NLEVELS; i++) { 425 if (sc->sc_zone.al[i].Pointer != NULL) 426 AcpiOsFree(sc->sc_zone.al[i].Pointer); 427 sc->sc_zone.al[i].Pointer = NULL; 428 } 429 } else 430 aprint_normal(":"); 431 432 valid_levels = 0; 433 434 for (i = 0; i < ATZ_NLEVELS; i++) { 435 ACPI_OBJECT *obj; 436 437 snprintf(buf, sizeof(buf), "_AC%d", i); 438 if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i])) 439 continue; 440 441 snprintf(buf, sizeof(buf), "_AL%d", i); 442 rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf, 443 &sc->sc_zone.al[i]); 444 if (ACPI_FAILURE(rv)) { 445 sc->sc_zone.al[i].Pointer = NULL; 446 continue; 447 } 448 449 obj = sc->sc_zone.al[i].Pointer; 450 if (obj != NULL) { 451 if (obj->Type != ACPI_TYPE_PACKAGE) { 452 aprint_error("%d not package\n", i); 453 AcpiOsFree(obj); 454 sc->sc_zone.al[i].Pointer = NULL; 455 continue; 456 } 457 } 458 459 if (sc->sc_first) 460 aprint_normal(" active cooling level %d: %sC", i, 461 acpitz_celcius_string(sc->sc_zone.ac[i])); 462 463 valid_levels++; 464 } 465 466 acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp); 467 acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt); 468 acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot); 469 sc->sc_zone.psl.Length = ACPI_ALLOCATE_BUFFER; 470 sc->sc_zone.psl.Pointer = NULL; 471 AcpiEvaluateObject(sc, "_PSL", NULL, &sc->sc_zone.psl); 472 acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv); 473 acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1); 474 acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2); 475 476 acpitz_sane_temp(&sc->sc_zone.tmp); 477 acpitz_sane_temp(&sc->sc_zone.crt); 478 acpitz_sane_temp(&sc->sc_zone.hot); 479 acpitz_sane_temp(&sc->sc_zone.psv); 480 481 if (verbose) { 482 if (sc->sc_zone.crt != ATZ_TMP_INVALID) 483 aprint_normal(" critical %sC", 484 acpitz_celcius_string(sc->sc_zone.crt)); 485 if (sc->sc_zone.hot != ATZ_TMP_INVALID) 486 aprint_normal(" hot %sC", 487 acpitz_celcius_string(sc->sc_zone.hot)); 488 if (sc->sc_zone.psv != ATZ_TMP_INVALID) 489 aprint_normal(" passive %sC", 490 acpitz_celcius_string(sc->sc_zone.tmp)); 491 } 492 493 if (valid_levels == 0) { 494 sc->sc_flags |= ATZ_F_PASSIVEONLY; 495 if (sc->sc_first) 496 aprint_normal(", passive cooling"); 497 } 498 if (verbose) 499 aprint_normal("\n"); 500 501 for (i = 0; i < ATZ_NLEVELS; i++) 502 acpitz_sane_temp(&sc->sc_zone.ac[i]); 503 504 acpitz_power_off(sc); 505 sc->sc_first = 0; 506 } 507 508 509 static void 510 acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque) 511 { 512 device_t dv = opaque; 513 ACPI_OSD_EXEC_CALLBACK func = NULL; 514 const char *name; 515 int rv; 516 517 switch (notify) { 518 case ACPI_NOTIFY_ThermalZoneStatusChanged: 519 func = acpitz_get_status; 520 name = "status check"; 521 break; 522 case ACPI_NOTIFY_ThermalZoneTripPointsChanged: 523 case ACPI_NOTIFY_DeviceListsChanged: 524 func = acpitz_get_zone_quiet; 525 name = "get zone"; 526 break; 527 default: 528 aprint_debug_dev(dv, 529 "received unhandled notify message 0x%x\n", notify); 530 return; 531 } 532 533 KASSERT(func != NULL); 534 535 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv); 536 if (rv != AE_OK) 537 aprint_debug_dev(dv, "unable to queue %s\n", name); 538 } 539 540 static void 541 acpitz_sane_temp(UINT32 *tmp) 542 { 543 /* Sane temperatures are beteen 0 and 150 C */ 544 if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500) 545 *tmp = ATZ_TMP_INVALID; 546 } 547 548 static int 549 acpitz_get_integer(device_t dv, const char *cm, UINT32 *val) 550 { 551 struct acpitz_softc *sc = device_private(dv); 552 ACPI_STATUS rv; 553 ACPI_INTEGER tmp; 554 555 rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp); 556 if (ACPI_FAILURE(rv)) { 557 #ifdef ACPI_DEBUG 558 aprint_debug_dev(dv, "failed to evaluate %s: %s\n", 559 cm, AcpiFormatException(rv)); 560 #endif 561 *val = ATZ_TMP_INVALID; 562 return 1; 563 } 564 565 *val = tmp; 566 567 return 0; 568 } 569 570 static void 571 acpitz_tick(void *opaque) 572 { 573 device_t dv = opaque; 574 struct acpitz_softc *sc = device_private(dv); 575 576 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv); 577 578 callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10); 579 } 580 581 static void 582 acpitz_init_envsys(device_t dv) 583 { 584 struct acpitz_softc *sc = device_private(dv); 585 586 sc->sc_sme = sysmon_envsys_create(); 587 sc->sc_sensor.monitor = true; 588 sc->sc_sensor.flags = ENVSYS_FMONLIMITS; 589 sc->sc_sensor.units = ENVSYS_STEMP; 590 strlcpy(sc->sc_sensor.desc, "temperature", sizeof(sc->sc_sensor.desc)); 591 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) { 592 sysmon_envsys_destroy(sc->sc_sme); 593 return; 594 } 595 596 /* hook into sysmon */ 597 sc->sc_sme->sme_name = device_xname(dv); 598 sc->sc_sme->sme_flags = SME_DISABLE_REFRESH; 599 600 if (sysmon_envsys_register(sc->sc_sme)) { 601 aprint_error_dev(dv, "unable to register with sysmon\n"); 602 sysmon_envsys_destroy(sc->sc_sme); 603 } 604 } 605