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 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_acpi.h" 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/cpu.h> 36 #include <sys/kthread.h> 37 #include <sys/malloc.h> 38 #include <sys/module.h> 39 #include <sys/bus.h> 40 #include <sys/proc.h> 41 #include <sys/reboot.h> 42 #include <sys/sysctl.h> 43 #include <sys/unistd.h> 44 #include <sys/power.h> 45 46 #include "cpufreq_if.h" 47 48 #include <contrib/dev/acpica/acpi.h> 49 #include <dev/acpica/acpivar.h> 50 51 /* Hooks for the ACPI CA debugging infrastructure */ 52 #define _COMPONENT ACPI_THERMAL 53 ACPI_MODULE_NAME("THERMAL") 54 55 #define TZ_ZEROC 2732 56 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10) 57 58 #define TZ_NOTIFY_TEMPERATURE 0x80 /* Temperature changed. */ 59 #define TZ_NOTIFY_LEVELS 0x81 /* Cooling levels changed. */ 60 #define TZ_NOTIFY_DEVICES 0x82 /* Device lists changed. */ 61 #define TZ_NOTIFY_CRITICAL 0xcc /* Fake notify that _CRT/_HOT reached. */ 62 63 /* Check for temperature changes every 10 seconds by default */ 64 #define TZ_POLLRATE 10 65 66 /* Make sure the reported temperature is valid for this number of polls. */ 67 #define TZ_VALIDCHECKS 3 68 69 /* Notify the user we will be shutting down in one more poll cycle. */ 70 #define TZ_NOTIFYCOUNT (TZ_VALIDCHECKS - 1) 71 72 /* ACPI spec defines this */ 73 #define TZ_NUMLEVELS 10 74 struct acpi_tz_zone { 75 int ac[TZ_NUMLEVELS]; 76 ACPI_BUFFER al[TZ_NUMLEVELS]; 77 int crt; 78 int hot; 79 ACPI_BUFFER psl; 80 int psv; 81 int tc1; 82 int tc2; 83 int tsp; 84 int tzp; 85 }; 86 87 struct acpi_tz_softc { 88 device_t tz_dev; 89 ACPI_HANDLE tz_handle; /*Thermal zone handle*/ 90 int tz_temperature; /*Current temperature*/ 91 int tz_active; /*Current active cooling*/ 92 #define TZ_ACTIVE_NONE -1 93 int tz_requested; /*Minimum active cooling*/ 94 int tz_thflags; /*Current temp-related flags*/ 95 #define TZ_THFLAG_NONE 0 96 #define TZ_THFLAG_PSV (1<<0) 97 #define TZ_THFLAG_HOT (1<<2) 98 #define TZ_THFLAG_CRT (1<<3) 99 int tz_flags; 100 #define TZ_FLAG_NO_SCP (1<<0) /*No _SCP method*/ 101 #define TZ_FLAG_GETPROFILE (1<<1) /*Get power_profile in timeout*/ 102 #define TZ_FLAG_GETSETTINGS (1<<2) /*Get devs/setpoints*/ 103 struct timespec tz_cooling_started; 104 /*Current cooling starting time*/ 105 106 struct sysctl_ctx_list tz_sysctl_ctx; 107 struct sysctl_oid *tz_sysctl_tree; 108 eventhandler_tag tz_event; 109 110 struct acpi_tz_zone tz_zone; /*Thermal zone parameters*/ 111 int tz_validchecks; 112 113 /* passive cooling */ 114 struct proc *tz_cooling_proc; 115 int tz_cooling_proc_running; 116 int tz_cooling_enabled; 117 int tz_cooling_active; 118 int tz_cooling_updated; 119 int tz_cooling_saved_freq; 120 }; 121 122 #define CPUFREQ_MAX_LEVELS 64 /* XXX cpufreq should export this */ 123 124 static int acpi_tz_probe(device_t dev); 125 static int acpi_tz_attach(device_t dev); 126 static int acpi_tz_establish(struct acpi_tz_softc *sc); 127 static void acpi_tz_monitor(void *Context); 128 static void acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg); 129 static void acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg); 130 static void acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, 131 int *data); 132 static void acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what); 133 static int acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS); 134 static int acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS); 135 static int acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS); 136 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, 137 void *context); 138 static void acpi_tz_signal(struct acpi_tz_softc *sc, int flags); 139 static void acpi_tz_timeout(struct acpi_tz_softc *sc, int flags); 140 static void acpi_tz_power_profile(void *arg); 141 static void acpi_tz_thread(void *arg); 142 static int acpi_tz_cooling_is_available(struct acpi_tz_softc *sc); 143 static int acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc); 144 145 static device_method_t acpi_tz_methods[] = { 146 /* Device interface */ 147 DEVMETHOD(device_probe, acpi_tz_probe), 148 DEVMETHOD(device_attach, acpi_tz_attach), 149 150 {0, 0} 151 }; 152 153 static driver_t acpi_tz_driver = { 154 "acpi_tz", 155 acpi_tz_methods, 156 sizeof(struct acpi_tz_softc), 157 }; 158 159 static devclass_t acpi_tz_devclass; 160 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0); 161 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1); 162 163 static struct sysctl_ctx_list acpi_tz_sysctl_ctx; 164 static struct sysctl_oid *acpi_tz_sysctl_tree; 165 166 /* Minimum cooling run time */ 167 static int acpi_tz_min_runtime; 168 static int acpi_tz_polling_rate = TZ_POLLRATE; 169 static int acpi_tz_override; 170 171 /* Timezone polling thread */ 172 static struct proc *acpi_tz_proc; 173 ACPI_LOCK_DECL(thermal, "ACPI thermal zone"); 174 175 static int 176 acpi_tz_probe(device_t dev) 177 { 178 int result; 179 180 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) { 181 device_set_desc(dev, "Thermal Zone"); 182 result = -10; 183 } else 184 result = ENXIO; 185 return (result); 186 } 187 188 static int 189 acpi_tz_attach(device_t dev) 190 { 191 struct acpi_tz_softc *sc; 192 struct acpi_softc *acpi_sc; 193 int error; 194 char oidname[8]; 195 196 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 197 198 sc = device_get_softc(dev); 199 sc->tz_dev = dev; 200 sc->tz_handle = acpi_get_handle(dev); 201 sc->tz_requested = TZ_ACTIVE_NONE; 202 sc->tz_active = TZ_ACTIVE_NONE; 203 sc->tz_thflags = TZ_THFLAG_NONE; 204 sc->tz_cooling_proc = NULL; 205 sc->tz_cooling_proc_running = FALSE; 206 sc->tz_cooling_active = FALSE; 207 sc->tz_cooling_updated = FALSE; 208 209 /* 210 * Always attempt to enable passive cooling for tz0. Users can enable 211 * it for other zones manually for now. 212 * 213 * XXX We need to test if multiple zones conflict with each other 214 * since cpufreq currently sets all CPUs to the given frequency whereas 215 * it's possible for different thermal zones to specify independent 216 * settings for multiple CPUs. 217 */ 218 sc->tz_cooling_enabled = (device_get_unit(dev) == 0); 219 220 /* 221 * Parse the current state of the thermal zone and build control 222 * structures. We don't need to worry about interference with the 223 * control thread since we haven't fully attached this device yet. 224 */ 225 if ((error = acpi_tz_establish(sc)) != 0) 226 return (error); 227 228 /* 229 * Register for any Notify events sent to this zone. 230 */ 231 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY, 232 acpi_tz_notify_handler, sc); 233 234 /* 235 * Create our sysctl nodes. 236 * 237 * XXX we need a mechanism for adding nodes under ACPI. 238 */ 239 if (device_get_unit(dev) == 0) { 240 acpi_sc = acpi_device_get_parent_softc(dev); 241 sysctl_ctx_init(&acpi_tz_sysctl_ctx); 242 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx, 243 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), 244 OID_AUTO, "thermal", CTLFLAG_RD, 0, ""); 245 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx, 246 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), 247 OID_AUTO, "min_runtime", CTLFLAG_RW, 248 &acpi_tz_min_runtime, 0, 249 "minimum cooling run time in sec"); 250 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx, 251 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), 252 OID_AUTO, "polling_rate", CTLFLAG_RW, 253 &acpi_tz_polling_rate, 0, "monitor polling rate"); 254 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx, 255 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO, 256 "user_override", CTLFLAG_RW, &acpi_tz_override, 0, 257 "allow override of thermal settings"); 258 } 259 sysctl_ctx_init(&sc->tz_sysctl_ctx); 260 sprintf(oidname, "tz%d", device_get_unit(dev)); 261 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx, 262 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), 263 OID_AUTO, oidname, CTLFLAG_RD, 0, ""); 264 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 265 OID_AUTO, "temperature", CTLFLAG_RD, &sc->tz_temperature, 266 sizeof(sc->tz_temperature), "IK", 267 "current thermal zone temperature"); 268 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 269 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW, 270 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active"); 271 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 272 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW, 273 sc, 0, acpi_tz_cooling_sysctl, "I", 274 "enable passive (speed reduction) cooling"); 275 276 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 277 OID_AUTO, "thermal_flags", CTLFLAG_RD, 278 &sc->tz_thflags, 0, "thermal zone flags"); 279 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 280 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW, 281 sc, offsetof(struct acpi_tz_softc, tz_zone.psv), 282 acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint"); 283 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 284 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW, 285 sc, offsetof(struct acpi_tz_softc, tz_zone.hot), 286 acpi_tz_temp_sysctl, "IK", 287 "too hot temp setpoint (suspend now)"); 288 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 289 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW, 290 sc, offsetof(struct acpi_tz_softc, tz_zone.crt), 291 acpi_tz_temp_sysctl, "IK", 292 "critical temp setpoint (shutdown now)"); 293 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree), 294 OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac, 295 sizeof(sc->tz_zone.ac), "IK", ""); 296 297 /* 298 * Create thread to service all of the thermal zones. Register 299 * our power profile event handler. 300 */ 301 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change, 302 acpi_tz_power_profile, sc, 0); 303 if (acpi_tz_proc == NULL) { 304 error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_proc, 305 RFHIGHPID, 0, "acpi_thermal"); 306 if (error != 0) { 307 device_printf(sc->tz_dev, "could not create thread - %d", error); 308 goto out; 309 } 310 } 311 312 /* Create a thread to handle passive cooling for each zone if enabled. */ 313 if (sc->tz_cooling_enabled) { 314 if (acpi_tz_cooling_is_available(sc)) { 315 error = acpi_tz_cooling_thread_start(sc); 316 if (error != 0) { 317 sc->tz_cooling_enabled = FALSE; 318 goto out; 319 } 320 } else 321 sc->tz_cooling_enabled = FALSE; 322 } 323 324 /* 325 * Flag the event handler for a manual invocation by our timeout. 326 * We defer it like this so that the rest of the subsystem has time 327 * to come up. Don't bother evaluating/printing the temperature at 328 * this point; on many systems it'll be bogus until the EC is running. 329 */ 330 sc->tz_flags |= TZ_FLAG_GETPROFILE; 331 332 out: 333 if (error != 0) { 334 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event); 335 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY, 336 acpi_tz_notify_handler); 337 sysctl_ctx_free(&sc->tz_sysctl_ctx); 338 } 339 return_VALUE (error); 340 } 341 342 /* 343 * Parse the current state of this thermal zone and set up to use it. 344 * 345 * Note that we may have previous state, which will have to be discarded. 346 */ 347 static int 348 acpi_tz_establish(struct acpi_tz_softc *sc) 349 { 350 ACPI_OBJECT *obj; 351 int i; 352 char nbuf[8]; 353 354 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 355 356 /* Erase any existing state. */ 357 for (i = 0; i < TZ_NUMLEVELS; i++) 358 if (sc->tz_zone.al[i].Pointer != NULL) 359 AcpiOsFree(sc->tz_zone.al[i].Pointer); 360 if (sc->tz_zone.psl.Pointer != NULL) 361 AcpiOsFree(sc->tz_zone.psl.Pointer); 362 363 /* 364 * XXX: We initialize only ACPI_BUFFER to avoid race condition 365 * with passive cooling thread which refers psv, tc1, tc2 and tsp. 366 */ 367 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac)); 368 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al)); 369 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl)); 370 371 /* Evaluate thermal zone parameters. */ 372 for (i = 0; i < TZ_NUMLEVELS; i++) { 373 sprintf(nbuf, "_AC%d", i); 374 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]); 375 sprintf(nbuf, "_AL%d", i); 376 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER; 377 sc->tz_zone.al[i].Pointer = NULL; 378 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]); 379 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer; 380 if (obj != NULL) { 381 /* Should be a package containing a list of power objects */ 382 if (obj->Type != ACPI_TYPE_PACKAGE) { 383 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n", 384 nbuf, obj->Type); 385 return_VALUE (ENXIO); 386 } 387 } 388 } 389 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt); 390 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot); 391 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER; 392 sc->tz_zone.psl.Pointer = NULL; 393 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl); 394 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv); 395 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1); 396 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2); 397 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp); 398 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp); 399 400 /* 401 * Sanity-check the values we've been given. 402 * 403 * XXX what do we do about systems that give us the same value for 404 * more than one of these setpoints? 405 */ 406 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT"); 407 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT"); 408 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV"); 409 for (i = 0; i < TZ_NUMLEVELS; i++) 410 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx"); 411 412 return_VALUE (0); 413 } 414 415 static char *aclevel_string[] = { 416 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4", 417 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9" 418 }; 419 420 static __inline const char * 421 acpi_tz_aclevel_string(int active) 422 { 423 if (active < -1 || active >= TZ_NUMLEVELS) 424 return (aclevel_string[0]); 425 426 return (aclevel_string[active + 1]); 427 } 428 429 /* 430 * Get the current temperature. 431 */ 432 static int 433 acpi_tz_get_temperature(struct acpi_tz_softc *sc) 434 { 435 int temp; 436 ACPI_STATUS status; 437 438 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature"); 439 440 status = acpi_GetInteger(sc->tz_handle, "_TMP", &temp); 441 if (ACPI_FAILURE(status)) { 442 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 443 "error fetching current temperature -- %s\n", 444 AcpiFormatException(status)); 445 return (FALSE); 446 } 447 448 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp))); 449 sc->tz_temperature = temp; 450 return (TRUE); 451 } 452 453 /* 454 * Evaluate the condition of a thermal zone, take appropriate actions. 455 */ 456 static void 457 acpi_tz_monitor(void *Context) 458 { 459 struct acpi_tz_softc *sc; 460 struct timespec curtime; 461 int temp; 462 int i; 463 int newactive, newflags; 464 465 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 466 467 sc = (struct acpi_tz_softc *)Context; 468 469 /* Get the current temperature. */ 470 if (!acpi_tz_get_temperature(sc)) { 471 /* XXX disable zone? go to max cooling? */ 472 return_VOID; 473 } 474 temp = sc->tz_temperature; 475 476 /* 477 * Work out what we ought to be doing right now. 478 * 479 * Note that the _ACx levels sort from hot to cold. 480 */ 481 newactive = TZ_ACTIVE_NONE; 482 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) { 483 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i]) { 484 newactive = i; 485 if (sc->tz_active != newactive) { 486 ACPI_VPRINT(sc->tz_dev, 487 acpi_device_get_parent_softc(sc->tz_dev), 488 "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i, 489 TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i])); 490 } 491 } 492 } 493 494 /* 495 * We are going to get _ACx level down (colder side), but give a guaranteed 496 * minimum cooling run time if requested. 497 */ 498 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE && 499 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) { 500 501 getnanotime(&curtime); 502 timespecsub(&curtime, &sc->tz_cooling_started); 503 if (curtime.tv_sec < acpi_tz_min_runtime) 504 newactive = sc->tz_active; 505 } 506 507 /* Handle user override of active mode */ 508 if (sc->tz_requested != TZ_ACTIVE_NONE && sc->tz_requested < newactive) 509 newactive = sc->tz_requested; 510 511 /* update temperature-related flags */ 512 newflags = TZ_THFLAG_NONE; 513 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv) 514 newflags |= TZ_THFLAG_PSV; 515 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot) 516 newflags |= TZ_THFLAG_HOT; 517 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt) 518 newflags |= TZ_THFLAG_CRT; 519 520 /* If the active cooling state has changed, we have to switch things. */ 521 if (newactive != sc->tz_active) { 522 /* Turn off the cooling devices that are on, if any are */ 523 if (sc->tz_active != TZ_ACTIVE_NONE) 524 acpi_ForeachPackageObject( 525 (ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer, 526 acpi_tz_switch_cooler_off, sc); 527 528 /* Turn on cooling devices that are required, if any are */ 529 if (newactive != TZ_ACTIVE_NONE) { 530 acpi_ForeachPackageObject( 531 (ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer, 532 acpi_tz_switch_cooler_on, sc); 533 } 534 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 535 "switched from %s to %s: %d.%dC\n", 536 acpi_tz_aclevel_string(sc->tz_active), 537 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp)); 538 sc->tz_active = newactive; 539 getnanotime(&sc->tz_cooling_started); 540 } 541 542 /* XXX (de)activate any passive cooling that may be required. */ 543 544 /* 545 * If the temperature is at _HOT or _CRT, increment our event count. 546 * If it has occurred enough times, shutdown the system. This is 547 * needed because some systems will report an invalid high temperature 548 * for one poll cycle. It is suspected this is due to the embedded 549 * controller timing out. A typical value is 138C for one cycle on 550 * a system that is otherwise 65C. 551 * 552 * If we're almost at that threshold, notify the user through devd(8). 553 */ 554 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) { 555 sc->tz_validchecks++; 556 if (sc->tz_validchecks == TZ_VALIDCHECKS) { 557 device_printf(sc->tz_dev, 558 "WARNING - current temperature (%d.%dC) exceeds safe limits\n", 559 TZ_KELVTOC(sc->tz_temperature)); 560 shutdown_nice(RB_POWEROFF); 561 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT) 562 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL); 563 } else { 564 sc->tz_validchecks = 0; 565 } 566 sc->tz_thflags = newflags; 567 568 return_VOID; 569 } 570 571 /* 572 * Given an object, verify that it's a reference to a device of some sort, 573 * and try to switch it off. 574 */ 575 static void 576 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg) 577 { 578 ACPI_HANDLE cooler; 579 580 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 581 582 cooler = acpi_GetReference(NULL, obj); 583 if (cooler == NULL) { 584 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n")); 585 return_VOID; 586 } 587 588 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n", 589 acpi_name(cooler))); 590 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3); 591 592 return_VOID; 593 } 594 595 /* 596 * Given an object, verify that it's a reference to a device of some sort, 597 * and try to switch it on. 598 * 599 * XXX replication of off/on function code is bad. 600 */ 601 static void 602 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg) 603 { 604 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg; 605 ACPI_HANDLE cooler; 606 ACPI_STATUS status; 607 608 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 609 610 cooler = acpi_GetReference(NULL, obj); 611 if (cooler == NULL) { 612 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n")); 613 return_VOID; 614 } 615 616 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n", 617 acpi_name(cooler))); 618 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0); 619 if (ACPI_FAILURE(status)) { 620 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 621 "failed to activate %s - %s\n", acpi_name(cooler), 622 AcpiFormatException(status)); 623 } 624 625 return_VOID; 626 } 627 628 /* 629 * Read/debug-print a parameter, default it to -1. 630 */ 631 static void 632 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data) 633 { 634 635 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 636 637 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) { 638 *data = -1; 639 } else { 640 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n", 641 acpi_name(sc->tz_handle), node, *data)); 642 } 643 644 return_VOID; 645 } 646 647 /* 648 * Sanity-check a temperature value. Assume that setpoints 649 * should be between 0C and 150C. 650 */ 651 static void 652 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what) 653 { 654 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 1500)) { 655 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n", 656 what, TZ_KELVTOC(*val)); 657 *val = -1; 658 } 659 } 660 661 /* 662 * Respond to a sysctl on the active state node. 663 */ 664 static int 665 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS) 666 { 667 struct acpi_tz_softc *sc; 668 int active; 669 int error; 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 return (error); 678 if (active < -1 || active >= TZ_NUMLEVELS) 679 return (EINVAL); 680 681 /* Set new preferred level and re-switch */ 682 sc->tz_requested = active; 683 acpi_tz_signal(sc, 0); 684 return (0); 685 } 686 687 static int 688 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS) 689 { 690 struct acpi_tz_softc *sc; 691 int enabled, error; 692 693 sc = (struct acpi_tz_softc *)oidp->oid_arg1; 694 enabled = sc->tz_cooling_enabled; 695 error = sysctl_handle_int(oidp, &enabled, 0, req); 696 697 /* Error or no new value */ 698 if (error != 0 || req->newptr == NULL) 699 return (error); 700 if (enabled != TRUE && enabled != FALSE) 701 return (EINVAL); 702 703 if (enabled) { 704 if (acpi_tz_cooling_is_available(sc)) 705 error = acpi_tz_cooling_thread_start(sc); 706 else 707 error = ENODEV; 708 if (error) 709 enabled = FALSE; 710 } 711 sc->tz_cooling_enabled = enabled; 712 return (error); 713 } 714 715 static int 716 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS) 717 { 718 struct acpi_tz_softc *sc; 719 int temp, *temp_ptr; 720 int error; 721 722 sc = oidp->oid_arg1; 723 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2); 724 temp = *temp_ptr; 725 error = sysctl_handle_int(oidp, &temp, 0, req); 726 727 /* Error or no new value */ 728 if (error != 0 || req->newptr == NULL) 729 return (error); 730 731 /* Only allow changing settings if override is set. */ 732 if (!acpi_tz_override) 733 return (EPERM); 734 735 /* Check user-supplied value for sanity. */ 736 temp = (temp * 10) + TZ_ZEROC; 737 acpi_tz_sanity(sc, &temp, "user-supplied temp"); 738 if (temp == -1) 739 return (EINVAL); 740 741 *temp_ptr = temp; 742 return (0); 743 } 744 745 static void 746 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context) 747 { 748 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context; 749 750 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 751 752 switch (notify) { 753 case TZ_NOTIFY_TEMPERATURE: 754 /* Temperature change occurred */ 755 acpi_tz_signal(sc, 0); 756 break; 757 case TZ_NOTIFY_DEVICES: 758 case TZ_NOTIFY_LEVELS: 759 /* Zone devices/setpoints changed */ 760 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS); 761 break; 762 default: 763 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 764 "unknown Notify event 0x%x\n", notify); 765 break; 766 } 767 768 acpi_UserNotify("Thermal", h, notify); 769 770 return_VOID; 771 } 772 773 static void 774 acpi_tz_signal(struct acpi_tz_softc *sc, int flags) 775 { 776 ACPI_LOCK(thermal); 777 sc->tz_flags |= flags; 778 ACPI_UNLOCK(thermal); 779 wakeup(&acpi_tz_proc); 780 } 781 782 /* 783 * Notifies can be generated asynchronously but have also been seen to be 784 * triggered by other thermal methods. One system generates a notify of 785 * 0x81 when the fan is turned on or off. Another generates it when _SCP 786 * is called. To handle these situations, we check the zone via 787 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling 788 * policy. 789 */ 790 static void 791 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags) 792 { 793 794 /* Check the current temperature and take action based on it */ 795 acpi_tz_monitor(sc); 796 797 /* If requested, get the power profile settings. */ 798 if (flags & TZ_FLAG_GETPROFILE) 799 acpi_tz_power_profile(sc); 800 801 /* 802 * If requested, check for new devices/setpoints. After finding them, 803 * check if we need to switch fans based on the new values. 804 */ 805 if (flags & TZ_FLAG_GETSETTINGS) { 806 acpi_tz_establish(sc); 807 acpi_tz_monitor(sc); 808 } 809 810 /* XXX passive cooling actions? */ 811 } 812 813 /* 814 * System power profile may have changed; fetch and notify the 815 * thermal zone accordingly. 816 * 817 * Since this can be called from an arbitrary eventhandler, it needs 818 * to get the ACPI lock itself. 819 */ 820 static void 821 acpi_tz_power_profile(void *arg) 822 { 823 ACPI_STATUS status; 824 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg; 825 int state; 826 827 state = power_profile_get_state(); 828 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY) 829 return; 830 831 /* check that we haven't decided there's no _SCP method */ 832 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) { 833 834 /* Call _SCP to set the new profile */ 835 status = acpi_SetInteger(sc->tz_handle, "_SCP", 836 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1); 837 if (ACPI_FAILURE(status)) { 838 if (status != AE_NOT_FOUND) 839 ACPI_VPRINT(sc->tz_dev, 840 acpi_device_get_parent_softc(sc->tz_dev), 841 "can't evaluate %s._SCP - %s\n", 842 acpi_name(sc->tz_handle), 843 AcpiFormatException(status)); 844 sc->tz_flags |= TZ_FLAG_NO_SCP; 845 } else { 846 /* We have to re-evaluate the entire zone now */ 847 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS); 848 } 849 } 850 } 851 852 /* 853 * Thermal zone monitor thread. 854 */ 855 static void 856 acpi_tz_thread(void *arg) 857 { 858 device_t *devs; 859 int devcount, i; 860 int flags; 861 struct acpi_tz_softc **sc; 862 863 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 864 865 devs = NULL; 866 devcount = 0; 867 sc = NULL; 868 869 for (;;) { 870 /* If the number of devices has changed, re-evaluate. */ 871 if (devclass_get_maxunit(acpi_tz_devclass) != devcount) { 872 if (devs != NULL) { 873 free(devs, M_TEMP); 874 free(sc, M_TEMP); 875 } 876 devclass_get_devices(acpi_tz_devclass, &devs, &devcount); 877 sc = malloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP, 878 M_WAITOK | M_ZERO); 879 for (i = 0; i < devcount; i++) 880 sc[i] = device_get_softc(devs[i]); 881 } 882 883 /* Check for temperature events and act on them. */ 884 for (i = 0; i < devcount; i++) { 885 ACPI_LOCK(thermal); 886 flags = sc[i]->tz_flags; 887 sc[i]->tz_flags &= TZ_FLAG_NO_SCP; 888 ACPI_UNLOCK(thermal); 889 acpi_tz_timeout(sc[i], flags); 890 } 891 892 /* If more work to do, don't go to sleep yet. */ 893 ACPI_LOCK(thermal); 894 for (i = 0; i < devcount; i++) { 895 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP) 896 break; 897 } 898 899 /* 900 * If we have no more work, sleep for a while, setting PDROP so that 901 * the mutex will not be reacquired. Otherwise, drop the mutex and 902 * loop to handle more events. 903 */ 904 if (i == devcount) 905 msleep(&acpi_tz_proc, &thermal_mutex, PZERO | PDROP, "tzpoll", 906 hz * acpi_tz_polling_rate); 907 else 908 ACPI_UNLOCK(thermal); 909 } 910 } 911 912 static int 913 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc) 914 { 915 device_t dev; 916 int error; 917 918 if (!sc->tz_cooling_updated) 919 return (0); 920 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) 921 return (ENXIO); 922 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 923 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n", 924 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq); 925 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN); 926 if (error == 0) 927 sc->tz_cooling_updated = FALSE; 928 return (error); 929 } 930 931 static int 932 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req) 933 { 934 device_t dev; 935 struct cf_level *levels; 936 int num_levels, error, freq, desired_freq, perf, i; 937 938 levels = malloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT); 939 if (levels == NULL) 940 return (ENOMEM); 941 942 /* 943 * Find the main device, cpufreq0. We don't yet support independent 944 * CPU frequency control on SMP. 945 */ 946 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) { 947 error = ENXIO; 948 goto out; 949 } 950 951 /* Get the current frequency. */ 952 error = CPUFREQ_GET(dev, &levels[0]); 953 if (error) 954 goto out; 955 freq = levels[0].total_set.freq; 956 957 /* Get the current available frequency levels. */ 958 num_levels = CPUFREQ_MAX_LEVELS; 959 error = CPUFREQ_LEVELS(dev, levels, &num_levels); 960 if (error) { 961 if (error == E2BIG) 962 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n"); 963 goto out; 964 } 965 966 /* Calculate the desired frequency as a percent of the max frequency. */ 967 perf = 100 * freq / levels[0].total_set.freq - req; 968 if (perf < 0) 969 perf = 0; 970 else if (perf > 100) 971 perf = 100; 972 desired_freq = levels[0].total_set.freq * perf / 100; 973 974 if (desired_freq < freq) { 975 /* Find the closest available frequency, rounding down. */ 976 for (i = 0; i < num_levels; i++) 977 if (levels[i].total_set.freq <= desired_freq) 978 break; 979 980 /* If we didn't find a relevant setting, use the lowest. */ 981 if (i == num_levels) 982 i--; 983 } else { 984 /* If we didn't decrease frequency yet, don't increase it. */ 985 if (!sc->tz_cooling_updated) { 986 sc->tz_cooling_active = FALSE; 987 goto out; 988 } 989 990 /* Use saved cpu frequency as maximum value. */ 991 if (desired_freq > sc->tz_cooling_saved_freq) 992 desired_freq = sc->tz_cooling_saved_freq; 993 994 /* Find the closest available frequency, rounding up. */ 995 for (i = num_levels - 1; i >= 0; i--) 996 if (levels[i].total_set.freq >= desired_freq) 997 break; 998 999 /* If we didn't find a relevant setting, use the highest. */ 1000 if (i == -1) 1001 i++; 1002 1003 /* If we're going to the highest frequency, restore the old setting. */ 1004 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) { 1005 error = acpi_tz_cpufreq_restore(sc); 1006 if (error == 0) 1007 sc->tz_cooling_active = FALSE; 1008 goto out; 1009 } 1010 } 1011 1012 /* If we are going to a new frequency, activate it. */ 1013 if (levels[i].total_set.freq != freq) { 1014 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev), 1015 "temperature %d.%dC: %screasing clock speed " 1016 "from %d MHz to %d MHz\n", 1017 TZ_KELVTOC(sc->tz_temperature), 1018 (freq > levels[i].total_set.freq) ? "de" : "in", 1019 freq, levels[i].total_set.freq); 1020 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN); 1021 if (error == 0 && !sc->tz_cooling_updated) { 1022 sc->tz_cooling_saved_freq = freq; 1023 sc->tz_cooling_updated = TRUE; 1024 } 1025 } 1026 1027 out: 1028 if (levels) 1029 free(levels, M_TEMP); 1030 return (error); 1031 } 1032 1033 /* 1034 * Passive cooling thread; monitors current temperature according to the 1035 * cooling interval and calculates whether to scale back CPU frequency. 1036 */ 1037 static void 1038 acpi_tz_cooling_thread(void *arg) 1039 { 1040 struct acpi_tz_softc *sc; 1041 int error, perf, curr_temp, prev_temp; 1042 1043 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1044 1045 sc = (struct acpi_tz_softc *)arg; 1046 1047 prev_temp = sc->tz_temperature; 1048 while (sc->tz_cooling_enabled) { 1049 if (sc->tz_cooling_active) 1050 (void)acpi_tz_get_temperature(sc); 1051 curr_temp = sc->tz_temperature; 1052 if (curr_temp >= sc->tz_zone.psv) 1053 sc->tz_cooling_active = TRUE; 1054 if (sc->tz_cooling_active) { 1055 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) + 1056 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv); 1057 perf /= 10; 1058 1059 if (perf != 0) { 1060 error = acpi_tz_cpufreq_update(sc, perf); 1061 1062 /* 1063 * If error and not simply a higher priority setting was 1064 * active, disable cooling. 1065 */ 1066 if (error != 0 && error != EPERM) { 1067 device_printf(sc->tz_dev, 1068 "failed to set new freq, disabling passive cooling\n"); 1069 sc->tz_cooling_enabled = FALSE; 1070 } 1071 } 1072 } 1073 prev_temp = curr_temp; 1074 tsleep(&sc->tz_cooling_proc, PZERO, "cooling", 1075 hz * sc->tz_zone.tsp / 10); 1076 } 1077 if (sc->tz_cooling_active) { 1078 acpi_tz_cpufreq_restore(sc); 1079 sc->tz_cooling_active = FALSE; 1080 } 1081 sc->tz_cooling_proc = NULL; 1082 ACPI_LOCK(thermal); 1083 sc->tz_cooling_proc_running = FALSE; 1084 ACPI_UNLOCK(thermal); 1085 kthread_exit(0); 1086 } 1087 1088 /* 1089 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates 1090 * all CPUs for us. However, it's possible in the future _PSL will 1091 * reference non-CPU devices so we may want to support it then. 1092 */ 1093 static int 1094 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc) 1095 { 1096 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 && 1097 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 && 1098 sc->tz_zone.psv != -1); 1099 } 1100 1101 static int 1102 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc) 1103 { 1104 int error; 1105 char name[16]; 1106 1107 ACPI_LOCK(thermal); 1108 if (sc->tz_cooling_proc_running) { 1109 ACPI_UNLOCK(thermal); 1110 return (0); 1111 } 1112 sc->tz_cooling_proc_running = TRUE; 1113 ACPI_UNLOCK(thermal); 1114 error = 0; 1115 if (sc->tz_cooling_proc == NULL) { 1116 snprintf(name, sizeof(name), "acpi_cooling%d", 1117 device_get_unit(sc->tz_dev)); 1118 error = kthread_create(acpi_tz_cooling_thread, sc, 1119 &sc->tz_cooling_proc, RFHIGHPID, 0, name); 1120 if (error != 0) { 1121 device_printf(sc->tz_dev, "could not create thread - %d", error); 1122 ACPI_LOCK(thermal); 1123 sc->tz_cooling_proc_running = FALSE; 1124 ACPI_UNLOCK(thermal); 1125 } 1126 } 1127 return (error); 1128 } 1129