1 /*- 2 * Copyright (c) 2000, 2001 Michael Smith 3 * Copyright (c) 2000 BSDi 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, 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 * $FreeBSD$ 28 */ 29 30 #include "opt_acpi.h" 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/kthread.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/bus.h> 37 #include <sys/proc.h> 38 #include <sys/reboot.h> 39 #include <sys/sysctl.h> 40 #include <sys/unistd.h> 41 #include <sys/power.h> 42 43 #include "acpi.h" 44 45 #include <dev/acpica/acpivar.h> 46 47 /* 48 * Hooks for the ACPI CA debugging infrastructure 49 */ 50 #define _COMPONENT ACPI_THERMAL 51 ACPI_MODULE_NAME("THERMAL") 52 53 #define TZ_ZEROC 2732 54 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), (((x) - TZ_ZEROC) % 10) 55 56 #define TZ_NOTIFY_TEMPERATURE 0x80 57 #define TZ_NOTIFY_DEVICES 0x81 58 #define TZ_NOTIFY_LEVELS 0x82 59 60 #define TZ_POLLRATE 30 /* every 30 seconds by default */ 61 62 #define TZ_NUMLEVELS 10 /* defined by ACPI spec */ 63 struct acpi_tz_zone { 64 int ac[TZ_NUMLEVELS]; 65 ACPI_BUFFER al[TZ_NUMLEVELS]; 66 int crt; 67 int hot; 68 ACPI_BUFFER psl; 69 int psv; 70 int tc1; 71 int tc2; 72 int tsp; 73 int tzp; 74 }; 75 76 77 struct acpi_tz_softc { 78 device_t tz_dev; /* device handle */ 79 ACPI_HANDLE tz_handle; /* thermal zone handle */ 80 int tz_temperature; /* current temperature */ 81 int tz_active; /* current active cooling */ 82 #define TZ_ACTIVE_NONE -1 83 int tz_requested; /* user-requested minimum active cooling */ 84 int tz_thflags; /* current temperature-related flags */ 85 #define TZ_THFLAG_NONE 0 86 #define TZ_THFLAG_PSV (1<<0) 87 #define TZ_THFLAG_HOT (1<<2) 88 #define TZ_THFLAG_CRT (1<<3) 89 int tz_flags; 90 #define TZ_FLAG_NO_SCP (1<<0) /* no _SCP method */ 91 #define TZ_FLAG_GETPROFILE (1<<1) /* fetch power_profile in timeout */ 92 struct timespec tz_cooling_started; /* current cooling starting time */ 93 94 struct sysctl_ctx_list tz_sysctl_ctx; /* sysctl tree */ 95 struct sysctl_oid *tz_sysctl_tree; 96 97 struct acpi_tz_zone tz_zone; /* thermal zone parameters */ 98 int tz_tmp_updating; 99 }; 100 101 static int acpi_tz_probe(device_t dev); 102 static int acpi_tz_attach(device_t dev); 103 static int acpi_tz_establish(struct acpi_tz_softc *sc); 104 static void acpi_tz_monitor(struct acpi_tz_softc *sc); 105 static void acpi_tz_all_off(struct acpi_tz_softc *sc); 106 static void acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg); 107 static void acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg); 108 static void acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data); 109 static void acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what); 110 static int acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS); 111 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context); 112 static void acpi_tz_timeout(struct acpi_tz_softc *sc); 113 static void acpi_tz_power_profile(void *arg); 114 115 static void acpi_tz_thread(void *arg); 116 static struct proc *acpi_tz_proc; 117 118 static device_method_t acpi_tz_methods[] = { 119 /* Device interface */ 120 DEVMETHOD(device_probe, acpi_tz_probe), 121 DEVMETHOD(device_attach, acpi_tz_attach), 122 123 {0, 0} 124 }; 125 126 static driver_t acpi_tz_driver = { 127 "acpi_tz", 128 acpi_tz_methods, 129 sizeof(struct acpi_tz_softc), 130 }; 131 132 static devclass_t acpi_tz_devclass; 133 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0); 134 135 static struct sysctl_ctx_list acpi_tz_sysctl_ctx; 136 static struct sysctl_oid *acpi_tz_sysctl_tree; 137 138 static int acpi_tz_min_runtime = 0;/* minimum cooling run time */ 139 static int acpi_tz_polling_rate = TZ_POLLRATE; 140 141 /* 142 * Match an ACPI thermal zone. 143 */ 144 static int 145 acpi_tz_probe(device_t dev) 146 { 147 int result; 148 149 ACPI_LOCK; 150 151 /* no FUNCTION_TRACE - too noisy */ 152 153 if ((acpi_get_type(dev) == ACPI_TYPE_THERMAL) && 154 !acpi_disabled("thermal")) { 155 device_set_desc(dev, "thermal zone"); 156 result = -10; 157 } else { 158 result = ENXIO; 159 } 160 ACPI_UNLOCK; 161 return(result); 162 } 163 164 /* 165 * Attach to an ACPI thermal zone. 166 */ 167 static int 168 acpi_tz_attach(device_t dev) 169 { 170 struct acpi_tz_softc *sc; 171 struct acpi_softc *acpi_sc; 172 int error; 173 char oidname[8]; 174 175 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 176 177 ACPI_LOCK; 178 179 sc = device_get_softc(dev); 180 sc->tz_dev = dev; 181 sc->tz_handle = acpi_get_handle(dev); 182 sc->tz_requested = TZ_ACTIVE_NONE; 183 sc->tz_tmp_updating = 0; 184 185 /* 186 * Parse the current state of the thermal zone and build control 187 * structures. 188 */ 189 if ((error = acpi_tz_establish(sc)) != 0) 190 goto out; 191 192 /* 193 * Register for any Notify events sent to this zone. 194 */ 195 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY, 196 acpi_tz_notify_handler, sc); 197 198 /* 199 * Create our sysctl nodes. 200 * 201 * XXX we need a mechanism for adding nodes under ACPI. 202 */ 203 if (device_get_unit(dev) == 0) { 204 acpi_sc = acpi_device_get_parent_softc(dev); 205 sysctl_ctx_init(&acpi_tz_sysctl_ctx); 206 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx, 207 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 208 OID_AUTO, "thermal", CTLFLAG_RD, 0, ""); 209 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx, 210 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), 211 OID_AUTO, "min_runtime", CTLFLAG_RD | CTLFLAG_RW, 212 &acpi_tz_min_runtime, 0, "minimum cooling run time in sec"); 213 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx, 214 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), 215 OID_AUTO, "polling_rate", CTLFLAG_RD | CTLFLAG_RW, 216 &acpi_tz_polling_rate, 0, "monitor polling rate"); 217 } 218 sysctl_ctx_init(&sc->tz_sysctl_ctx); 219 sprintf(oidname, "tz%d", device_get_unit(dev)); 220 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx, 221 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO, 222 oidname, CTLFLAG_RD, 0, ""); 223 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 224 OID_AUTO, "temperature", CTLFLAG_RD, 225 &sc->tz_temperature, 0, "current thermal zone temperature"); 226 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 227 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW, 228 sc, 0, acpi_tz_active_sysctl, "I", ""); 229 230 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 231 OID_AUTO, "thermal_flags", CTLFLAG_RD, 232 &sc->tz_thflags, 0, "thermal zone flags"); 233 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 234 OID_AUTO, "_PSV", CTLFLAG_RD, 235 &sc->tz_zone.psv, 0, ""); 236 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 237 OID_AUTO, "_HOT", CTLFLAG_RD, 238 &sc->tz_zone.hot, 0, ""); 239 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 240 OID_AUTO, "_CRT", CTLFLAG_RD, 241 &sc->tz_zone.crt, 0, ""); 242 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 243 OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac, 244 sizeof(sc->tz_zone.ac), "I", ""); 245 246 247 /* 248 * Register our power profile event handler, and flag it for a manual 249 * invocation by our timeout. We defer it like this so that the rest 250 * of the subsystem has time to come up. 251 */ 252 EVENTHANDLER_REGISTER(power_profile_change, acpi_tz_power_profile, sc, 0); 253 sc->tz_flags |= TZ_FLAG_GETPROFILE; 254 255 /* 256 * Don't bother evaluating/printing the temperature at this point; 257 * on many systems it'll be bogus until the EC is running. 258 */ 259 260 /* 261 * Create our thread; we only need one, it will service all of the 262 * thermal zones. 263 */ 264 if (acpi_tz_proc == NULL) { 265 error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_proc, 266 RFHIGHPID, 0, "acpi_thermal"); 267 if (error != 0) { 268 device_printf(sc->tz_dev, "could not create thread - %d", error); 269 goto out; 270 } 271 } 272 273 out: 274 ACPI_UNLOCK; 275 276 return_VALUE(error); 277 } 278 279 /* 280 * Parse the current state of this thermal zone and set up to use it. 281 * 282 * Note that we may have previous state, which will have to be discarded. 283 */ 284 static int 285 acpi_tz_establish(struct acpi_tz_softc *sc) 286 { 287 ACPI_OBJECT *obj; 288 int i; 289 char nbuf[8]; 290 291 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 292 293 ACPI_ASSERTLOCK; 294 295 /* 296 * Power everything off and erase any existing state. 297 */ 298 acpi_tz_all_off(sc); 299 for (i = 0; i < TZ_NUMLEVELS; i++) 300 if (sc->tz_zone.al[i].Pointer != NULL) 301 AcpiOsFree(sc->tz_zone.al[i].Pointer); 302 if (sc->tz_zone.psl.Pointer != NULL) 303 AcpiOsFree(sc->tz_zone.psl.Pointer); 304 bzero(&sc->tz_zone, sizeof(sc->tz_zone)); 305 306 /* 307 * Evaluate thermal zone parameters. 308 */ 309 for (i = 0; i < TZ_NUMLEVELS; i++) { 310 sprintf(nbuf, "_AC%d", i); 311 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]); 312 sprintf(nbuf, "_AL%d", i); 313 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER; 314 sc->tz_zone.al[i].Pointer = NULL; 315 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]); 316 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer; 317 if (obj != NULL) { 318 /* should be a package containing a list of power objects */ 319 if (obj->Type != ACPI_TYPE_PACKAGE) { 320 device_printf(sc->tz_dev, "%s has unknown object type %d, rejecting\n", 321 nbuf, obj->Type); 322 return_VALUE(ENXIO); 323 } 324 } 325 } 326 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt); 327 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot); 328 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER; 329 sc->tz_zone.psl.Pointer = NULL; 330 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl); 331 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv); 332 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1); 333 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2); 334 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp); 335 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp); 336 337 /* 338 * Sanity-check the values we've been given. 339 * 340 * XXX what do we do about systems that give us the same value for 341 * more than one of these setpoints? 342 */ 343 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT"); 344 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT"); 345 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV"); 346 for (i = 0; i < TZ_NUMLEVELS; i++) 347 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx"); 348 349 /* 350 * Power off everything that we've just been given. 351 */ 352 acpi_tz_all_off(sc); 353 354 return_VALUE(0); 355 } 356 357 static char *aclevel_string[] = { 358 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4", 359 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9" }; 360 361 static __inline const char * 362 acpi_tz_aclevel_string(int active) 363 { 364 if (active < -1 || active >= TZ_NUMLEVELS) { 365 return (aclevel_string[0]); 366 } 367 368 return (aclevel_string[active+1]); 369 } 370 371 /* 372 * Evaluate the condition of a thermal zone, take appropriate actions. 373 */ 374 static void 375 acpi_tz_monitor(struct acpi_tz_softc *sc) 376 { 377 int temp; 378 int i; 379 int newactive, newflags; 380 struct timespec curtime; 381 ACPI_STATUS status; 382 383 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 384 385 ACPI_ASSERTLOCK; 386 387 if (sc->tz_tmp_updating) { 388 goto out; 389 } 390 sc->tz_tmp_updating = 1; 391 392 /* 393 * Get the current temperature. 394 */ 395 if (ACPI_FAILURE(status = acpi_EvaluateInteger(sc->tz_handle, "_TMP", &temp))) { 396 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 397 "error fetching current temperature -- %s\n", 398 AcpiFormatException(status)); 399 /* XXX disable zone? go to max cooling? */ 400 goto out; 401 } 402 403 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp))); 404 sc->tz_temperature = temp; 405 406 /* 407 * Work out what we ought to be doing right now. 408 * 409 * Note that the _ACx levels sort from hot to cold. 410 */ 411 newactive = TZ_ACTIVE_NONE; 412 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) { 413 if ((sc->tz_zone.ac[i] != -1) && (temp >= sc->tz_zone.ac[i])) { 414 newactive = i; 415 if (sc->tz_active != newactive) { 416 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 417 "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i, 418 TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i])); 419 getnanotime(&sc->tz_cooling_started); 420 } 421 } 422 } 423 424 /* 425 * We are going to get _ACx level down (colder side), but give a guaranteed 426 * minimum cooling run time if requested. 427 */ 428 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE && 429 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) { 430 getnanotime(&curtime); 431 timespecsub(&curtime, &sc->tz_cooling_started); 432 if (curtime.tv_sec < acpi_tz_min_runtime) { 433 newactive = sc->tz_active; 434 } 435 } 436 437 /* handle user override of active mode */ 438 if (sc->tz_requested > newactive) 439 newactive = sc->tz_requested; 440 441 /* update temperature-related flags */ 442 newflags = TZ_THFLAG_NONE; 443 if ((sc->tz_zone.psv != -1) && (temp >= sc->tz_zone.psv)) 444 newflags |= TZ_THFLAG_PSV; 445 if ((sc->tz_zone.hot != -1) && (temp >= sc->tz_zone.hot)) 446 newflags |= TZ_THFLAG_HOT; 447 if ((sc->tz_zone.crt != -1) && (temp >= sc->tz_zone.crt)) 448 newflags |= TZ_THFLAG_CRT; 449 450 /* 451 * If the active cooling state has changed, we have to switch things. 452 */ 453 if (newactive != sc->tz_active) { 454 455 /* turn off the cooling devices that are on, if any are */ 456 if (sc->tz_active != TZ_ACTIVE_NONE) 457 acpi_ForeachPackageObject((ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer, 458 acpi_tz_switch_cooler_off, sc); 459 460 /* turn on cooling devices that are required, if any are */ 461 if (newactive != TZ_ACTIVE_NONE) 462 acpi_ForeachPackageObject((ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer, 463 acpi_tz_switch_cooler_on, sc); 464 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 465 "switched from %s to %s: %d.%dC\n", 466 acpi_tz_aclevel_string(sc->tz_active), 467 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp)); 468 sc->tz_active = newactive; 469 } 470 471 /* 472 * XXX (de)activate any passive cooling that may be required. 473 */ 474 475 /* 476 * If we have just become _HOT or _CRT, warn the user. 477 * 478 * We should actually shut down at this point, but it's not clear 479 * that some systems don't actually map _CRT to the same value as _AC0. 480 */ 481 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) && 482 !(sc->tz_thflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT))) { 483 device_printf(sc->tz_dev, "WARNING - current temperature (%d.%dC) exceeds system limits\n", 484 TZ_KELVTOC(sc->tz_temperature)); 485 /* shutdown_nice(RB_POWEROFF);*/ 486 } 487 sc->tz_thflags = newflags; 488 489 out: 490 sc->tz_tmp_updating = 0; 491 return_VOID; 492 } 493 494 /* 495 * Turn off all the cooling devices. 496 */ 497 static void 498 acpi_tz_all_off(struct acpi_tz_softc *sc) 499 { 500 int i; 501 502 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 503 504 ACPI_ASSERTLOCK; 505 506 /* 507 * Scan all the _ALx objects, and turn them all off. 508 */ 509 for (i = 0; i < TZ_NUMLEVELS; i++) { 510 if (sc->tz_zone.al[i].Pointer == NULL) 511 continue; 512 acpi_ForeachPackageObject((ACPI_OBJECT *)sc->tz_zone.al[i].Pointer, 513 acpi_tz_switch_cooler_off, sc); 514 } 515 516 /* 517 * XXX revert any passive-cooling options. 518 */ 519 520 sc->tz_active = TZ_ACTIVE_NONE; 521 sc->tz_thflags = TZ_THFLAG_NONE; 522 return_VOID; 523 } 524 525 /* 526 * Given an object, verify that it's a reference to a device of some sort, 527 * and try to switch it off. 528 */ 529 static void 530 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg) 531 { 532 ACPI_HANDLE cooler; 533 534 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 535 536 ACPI_ASSERTLOCK; 537 538 switch(obj->Type) { 539 case ACPI_TYPE_ANY: 540 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n", acpi_name(obj->Reference.Handle))); 541 542 acpi_pwr_switch_consumer(obj->Reference.Handle, ACPI_STATE_D3); 543 break; 544 545 case ACPI_TYPE_STRING: 546 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n", obj->String.Pointer)); 547 548 /* 549 * Find the handle for the device and turn it off. 550 * The String object here seems to contain a fully-qualified path, so we 551 * don't have to search for it in our parents. 552 * 553 * XXX This may not always be the case. 554 */ 555 if (ACPI_SUCCESS(AcpiGetHandle(NULL, obj->String.Pointer, &cooler))) 556 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3); 557 break; 558 559 default: 560 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to handle unsupported object type %d\n", 561 obj->Type)); 562 break; 563 } 564 return_VOID; 565 } 566 567 /* 568 * Given an object, verify that it's a reference to a device of some sort, 569 * and try to switch it on. 570 * 571 * XXX replication of off/on function code is bad, mmmkay? 572 */ 573 static void 574 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg) 575 { 576 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg; 577 ACPI_HANDLE cooler; 578 ACPI_STATUS status; 579 580 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 581 582 ACPI_ASSERTLOCK; 583 584 switch(obj->Type) { 585 case ACPI_TYPE_ANY: 586 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n", acpi_name(obj->Reference.Handle))); 587 588 if (ACPI_FAILURE(status = acpi_pwr_switch_consumer(obj->Reference.Handle, ACPI_STATE_D0))) { 589 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 590 "failed to activate %s - %s\n", acpi_name(obj->Reference.Handle), 591 AcpiFormatException(status)); 592 } 593 break; 594 595 case ACPI_TYPE_STRING: 596 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n", obj->String.Pointer)); 597 598 /* 599 * Find the handle for the device and turn it off. 600 * The String object here seems to contain a fully-qualified path, so we 601 * don't have to search for it in our parents. 602 * 603 * XXX This may not always be the case. 604 */ 605 if (ACPI_SUCCESS(AcpiGetHandle(NULL, obj->String.Pointer, &cooler))) { 606 if (ACPI_FAILURE(status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0))) { 607 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 608 "failed to activate %s - %s\n", 609 obj->String.Pointer, AcpiFormatException(status)); 610 } 611 } else { 612 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 613 "couldn't find %s\n", obj->String.Pointer); 614 } 615 break; 616 617 default: 618 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to handle unsupported object type %d\n", 619 obj->Type)); 620 break; 621 } 622 return_VOID; 623 } 624 625 /* 626 * Read/debug-print a parameter, default it to -1. 627 */ 628 static void 629 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data) 630 { 631 632 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 633 634 ACPI_ASSERTLOCK; 635 636 if (ACPI_FAILURE(acpi_EvaluateInteger(sc->tz_handle, node, data))) { 637 *data = -1; 638 } else { 639 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n", acpi_name(sc->tz_handle), 640 node, *data)); 641 } 642 return_VOID; 643 } 644 645 /* 646 * Sanity-check a temperature value. Assume that setpoints 647 * should be between 0C and 150C. 648 */ 649 static void 650 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what) 651 { 652 if ((*val != -1) && ((*val < TZ_ZEROC) || (*val > (TZ_ZEROC + 1500)))) { 653 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n", 654 what, TZ_KELVTOC(*val)); 655 *val = -1; 656 } 657 } 658 659 /* 660 * Respond to a sysctl on the active state node. 661 */ 662 static int 663 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS) 664 { 665 struct acpi_tz_softc *sc; 666 int active; 667 int error; 668 669 ACPI_LOCK; 670 671 sc = (struct acpi_tz_softc *)oidp->oid_arg1; 672 active = sc->tz_active; 673 error = sysctl_handle_int(oidp, &active, 0, req); 674 675 /* error or no new value */ 676 if ((error != 0) || (req->newptr == NULL)) 677 goto out; 678 679 /* range check */ 680 if ((active < -1) || (active >= TZ_NUMLEVELS)) { 681 error = EINVAL; 682 goto out; 683 } 684 685 /* set new preferred level and re-switch */ 686 sc->tz_requested = active; 687 acpi_tz_monitor(sc); 688 689 out: 690 ACPI_UNLOCK; 691 return(error); 692 } 693 694 /* 695 * Respond to a Notify event sent to the zone. 696 */ 697 static void 698 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 699 { 700 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context; 701 702 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 703 704 ACPI_ASSERTLOCK; 705 706 switch(notify) { 707 case TZ_NOTIFY_TEMPERATURE: 708 /* temperature change occurred */ 709 AcpiOsQueueForExecution(OSD_PRIORITY_HIGH, (OSD_EXECUTION_CALLBACK)acpi_tz_monitor, sc); 710 break; 711 case TZ_NOTIFY_DEVICES: 712 case TZ_NOTIFY_LEVELS: 713 /* zone devices/setpoints changed */ 714 AcpiOsQueueForExecution(OSD_PRIORITY_HIGH, (OSD_EXECUTION_CALLBACK)acpi_tz_establish, sc); 715 break; 716 default: 717 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 718 "unknown Notify event 0x%x\n", notify); 719 break; 720 } 721 return_VOID; 722 } 723 724 /* 725 * Poll the thermal zone. 726 */ 727 static void 728 acpi_tz_timeout(struct acpi_tz_softc *sc) 729 { 730 731 /* do we need to get the power profile settings? */ 732 if (sc->tz_flags & TZ_FLAG_GETPROFILE) { 733 acpi_tz_power_profile((void *)sc); 734 sc->tz_flags &= ~TZ_FLAG_GETPROFILE; 735 } 736 737 ACPI_ASSERTLOCK; 738 739 /* check the current temperature and take action based on it */ 740 acpi_tz_monitor(sc); 741 742 /* XXX passive cooling actions? */ 743 } 744 745 /* 746 * System power profile may have changed; fetch and notify the 747 * thermal zone accordingly. 748 * 749 * Since this can be called from an arbitrary eventhandler, it needs 750 * to get the ACPI lock itself. 751 */ 752 static void 753 acpi_tz_power_profile(void *arg) 754 { 755 ACPI_OBJECT_LIST args; 756 ACPI_OBJECT obj; 757 ACPI_STATUS status; 758 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg; 759 int state; 760 761 state = power_profile_get_state(); 762 if (state != POWER_PROFILE_PERFORMANCE && 763 state != POWER_PROFILE_ECONOMY) { 764 return; 765 } 766 767 ACPI_LOCK; 768 769 /* check that we haven't decided there's no _SCP method */ 770 if (!(sc->tz_flags & TZ_FLAG_NO_SCP)) { 771 772 /* call _SCP to set the new profile */ 773 obj.Type = ACPI_TYPE_INTEGER; 774 obj.Integer.Value = (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1; 775 args.Count = 1; 776 args.Pointer = &obj; 777 if (ACPI_FAILURE(status = AcpiEvaluateObject(sc->tz_handle, "_SCP", &args, NULL))) { 778 if (status != AE_NOT_FOUND) 779 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 780 "can't evaluate %s._SCP - %s\n", acpi_name(sc->tz_handle), 781 AcpiFormatException(status)); 782 sc->tz_flags |= TZ_FLAG_NO_SCP; 783 } else { 784 /* we have to re-evaluate the entire zone now */ 785 AcpiOsQueueForExecution(OSD_PRIORITY_HIGH, (OSD_EXECUTION_CALLBACK)acpi_tz_establish, sc); 786 } 787 } 788 ACPI_UNLOCK; 789 } 790 791 /* 792 * Thermal zone monitor thread. 793 */ 794 static void 795 acpi_tz_thread(void *arg) 796 { 797 device_t *devs; 798 int devcount, i; 799 800 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 801 802 803 devs = NULL; 804 devcount = 0; 805 806 for (;;) { 807 tsleep(&acpi_tz_proc, PZERO, "nothing", hz * acpi_tz_polling_rate); 808 809 mtx_lock(&Giant); 810 811 if (devcount == 0) 812 devclass_get_devices(acpi_tz_devclass, &devs, &devcount); 813 814 ACPI_LOCK; 815 for (i = 0; i < devcount; i++) 816 acpi_tz_timeout(device_get_softc(devs[i])); 817 ACPI_UNLOCK; 818 819 mtx_unlock(&Giant); 820 } 821 } 822