2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
36 #include <sys/kthread.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
41 #include <sys/reboot.h>
42 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/power.h>
46 #include "cpufreq_if.h"
48 #include <contrib/dev/acpica/acpi.h>
49 #include <dev/acpica/acpivar.h>
51 /* Hooks for the ACPI CA debugging infrastructure */
52 #define _COMPONENT ACPI_THERMAL
53 ACPI_MODULE_NAME("THERMAL")
56 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
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. */
63 /* Check for temperature changes every 10 seconds by default */
64 #define TZ_POLLRATE 10
66 /* Make sure the reported temperature is valid for this number of polls. */
67 #define TZ_VALIDCHECKS 3
69 /* Notify the user we will be shutting down in one more poll cycle. */
70 #define TZ_NOTIFYCOUNT (TZ_VALIDCHECKS - 1)
72 /* ACPI spec defines this */
73 #define TZ_NUMLEVELS 10
76 ACPI_BUFFER al[TZ_NUMLEVELS];
87 struct acpi_tz_softc {
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)
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*/
106 struct sysctl_ctx_list tz_sysctl_ctx;
107 struct sysctl_oid *tz_sysctl_tree;
108 eventhandler_tag tz_event;
110 struct acpi_tz_zone tz_zone; /*Thermal zone parameters*/
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;
122 #define CPUFREQ_MAX_LEVELS 64 /* XXX cpufreq should export this */
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,
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 int acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
137 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
139 static void acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
140 static void acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
141 static void acpi_tz_power_profile(void *arg);
142 static void acpi_tz_thread(void *arg);
143 static int acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
144 static int acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
146 static device_method_t acpi_tz_methods[] = {
147 /* Device interface */
148 DEVMETHOD(device_probe, acpi_tz_probe),
149 DEVMETHOD(device_attach, acpi_tz_attach),
154 static driver_t acpi_tz_driver = {
157 sizeof(struct acpi_tz_softc),
160 static devclass_t acpi_tz_devclass;
161 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0);
162 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
164 static struct sysctl_ctx_list acpi_tz_sysctl_ctx;
165 static struct sysctl_oid *acpi_tz_sysctl_tree;
167 /* Minimum cooling run time */
168 static int acpi_tz_min_runtime;
169 static int acpi_tz_polling_rate = TZ_POLLRATE;
170 static int acpi_tz_override;
172 /* Timezone polling thread */
173 static struct proc *acpi_tz_proc;
174 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
176 static int acpi_tz_cooling_unit = -1;
179 acpi_tz_probe(device_t dev)
183 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
184 device_set_desc(dev, "Thermal Zone");
192 acpi_tz_attach(device_t dev)
194 struct acpi_tz_softc *sc;
195 struct acpi_softc *acpi_sc;
199 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
201 sc = device_get_softc(dev);
203 sc->tz_handle = acpi_get_handle(dev);
204 sc->tz_requested = TZ_ACTIVE_NONE;
205 sc->tz_active = TZ_ACTIVE_NONE;
206 sc->tz_thflags = TZ_THFLAG_NONE;
207 sc->tz_cooling_proc = NULL;
208 sc->tz_cooling_proc_running = FALSE;
209 sc->tz_cooling_active = FALSE;
210 sc->tz_cooling_updated = FALSE;
211 sc->tz_cooling_enabled = FALSE;
214 * Parse the current state of the thermal zone and build control
215 * structures. We don't need to worry about interference with the
216 * control thread since we haven't fully attached this device yet.
218 if ((error = acpi_tz_establish(sc)) != 0)
222 * Register for any Notify events sent to this zone.
224 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
225 acpi_tz_notify_handler, sc);
228 * Create our sysctl nodes.
230 * XXX we need a mechanism for adding nodes under ACPI.
232 if (device_get_unit(dev) == 0) {
233 acpi_sc = acpi_device_get_parent_softc(dev);
234 sysctl_ctx_init(&acpi_tz_sysctl_ctx);
235 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
236 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
237 OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
238 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
239 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
240 OID_AUTO, "min_runtime", CTLFLAG_RW,
241 &acpi_tz_min_runtime, 0,
242 "minimum cooling run time in sec");
243 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
244 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
245 OID_AUTO, "polling_rate", CTLFLAG_RW,
246 &acpi_tz_polling_rate, 0, "monitor polling rate");
247 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
248 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
249 "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
250 "allow override of thermal settings");
252 sysctl_ctx_init(&sc->tz_sysctl_ctx);
253 sprintf(oidname, "tz%d", device_get_unit(dev));
254 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
255 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
256 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
257 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
258 OID_AUTO, "temperature", CTLFLAG_RD, &sc->tz_temperature,
259 sizeof(sc->tz_temperature), "IK",
260 "current thermal zone temperature");
261 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
262 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
263 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
264 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
265 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
266 sc, 0, acpi_tz_cooling_sysctl, "I",
267 "enable passive (speed reduction) cooling");
269 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
270 OID_AUTO, "thermal_flags", CTLFLAG_RD,
271 &sc->tz_thflags, 0, "thermal zone flags");
272 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
273 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
274 sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
275 acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
276 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
277 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
278 sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
279 acpi_tz_temp_sysctl, "IK",
280 "too hot temp setpoint (suspend now)");
281 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
282 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
283 sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
284 acpi_tz_temp_sysctl, "IK",
285 "critical temp setpoint (shutdown now)");
286 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
287 OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac,
288 sizeof(sc->tz_zone.ac), "IK", "");
289 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
290 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
291 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
292 acpi_tz_passive_sysctl, "I",
293 "thermal constant 1 for passive cooling");
294 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
295 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
296 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
297 acpi_tz_passive_sysctl, "I",
298 "thermal constant 2 for passive cooling");
299 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
300 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
301 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
302 acpi_tz_passive_sysctl, "I",
303 "thermal sampling period for passive cooling");
306 * Create thread to service all of the thermal zones. Register
307 * our power profile event handler.
309 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
310 acpi_tz_power_profile, sc, 0);
311 if (acpi_tz_proc == NULL) {
312 error = kproc_create(acpi_tz_thread, NULL, &acpi_tz_proc,
313 RFHIGHPID, 0, "acpi_thermal");
315 device_printf(sc->tz_dev, "could not create thread - %d", error);
321 * Create a thread to handle passive cooling for 1st zone which
322 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
323 * zones manually for now.
325 * XXX We enable only one zone to avoid multiple zones conflict
326 * with each other since cpufreq currently sets all CPUs to the
327 * given frequency whereas it's possible for different thermal
328 * zones to specify independent settings for multiple CPUs.
330 if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
331 sc->tz_cooling_enabled = TRUE;
332 if (sc->tz_cooling_enabled) {
333 error = acpi_tz_cooling_thread_start(sc);
335 sc->tz_cooling_enabled = FALSE;
338 acpi_tz_cooling_unit = device_get_unit(dev);
342 * Flag the event handler for a manual invocation by our timeout.
343 * We defer it like this so that the rest of the subsystem has time
344 * to come up. Don't bother evaluating/printing the temperature at
345 * this point; on many systems it'll be bogus until the EC is running.
347 sc->tz_flags |= TZ_FLAG_GETPROFILE;
351 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
352 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
353 acpi_tz_notify_handler);
354 sysctl_ctx_free(&sc->tz_sysctl_ctx);
356 return_VALUE (error);
360 * Parse the current state of this thermal zone and set up to use it.
362 * Note that we may have previous state, which will have to be discarded.
365 acpi_tz_establish(struct acpi_tz_softc *sc)
371 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
373 /* Erase any existing state. */
374 for (i = 0; i < TZ_NUMLEVELS; i++)
375 if (sc->tz_zone.al[i].Pointer != NULL)
376 AcpiOsFree(sc->tz_zone.al[i].Pointer);
377 if (sc->tz_zone.psl.Pointer != NULL)
378 AcpiOsFree(sc->tz_zone.psl.Pointer);
381 * XXX: We initialize only ACPI_BUFFER to avoid race condition
382 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
384 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
385 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
386 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
388 /* Evaluate thermal zone parameters. */
389 for (i = 0; i < TZ_NUMLEVELS; i++) {
390 sprintf(nbuf, "_AC%d", i);
391 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
392 sprintf(nbuf, "_AL%d", i);
393 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
394 sc->tz_zone.al[i].Pointer = NULL;
395 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
396 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
398 /* Should be a package containing a list of power objects */
399 if (obj->Type != ACPI_TYPE_PACKAGE) {
400 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
402 return_VALUE (ENXIO);
406 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
407 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
408 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
409 sc->tz_zone.psl.Pointer = NULL;
410 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
411 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
412 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
413 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
414 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
415 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
418 * Sanity-check the values we've been given.
420 * XXX what do we do about systems that give us the same value for
421 * more than one of these setpoints?
423 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
424 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
425 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
426 for (i = 0; i < TZ_NUMLEVELS; i++)
427 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
432 static char *aclevel_string[] = {
433 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
434 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
437 static __inline const char *
438 acpi_tz_aclevel_string(int active)
440 if (active < -1 || active >= TZ_NUMLEVELS)
441 return (aclevel_string[0]);
443 return (aclevel_string[active + 1]);
447 * Get the current temperature.
450 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
454 static char *tmp_name = "_TMP";
456 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
458 /* Evaluate the thermal zone's _TMP method. */
459 status = acpi_GetInteger(sc->tz_handle, tmp_name, &temp);
460 if (ACPI_FAILURE(status)) {
461 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
462 "error fetching current temperature -- %s\n",
463 AcpiFormatException(status));
467 /* Check it for validity. */
468 acpi_tz_sanity(sc, &temp, tmp_name);
472 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
473 sc->tz_temperature = temp;
478 * Evaluate the condition of a thermal zone, take appropriate actions.
481 acpi_tz_monitor(void *Context)
483 struct acpi_tz_softc *sc;
484 struct timespec curtime;
487 int newactive, newflags;
489 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
491 sc = (struct acpi_tz_softc *)Context;
493 /* Get the current temperature. */
494 if (!acpi_tz_get_temperature(sc)) {
495 /* XXX disable zone? go to max cooling? */
498 temp = sc->tz_temperature;
501 * Work out what we ought to be doing right now.
503 * Note that the _ACx levels sort from hot to cold.
505 newactive = TZ_ACTIVE_NONE;
506 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
507 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i]) {
509 if (sc->tz_active != newactive) {
510 ACPI_VPRINT(sc->tz_dev,
511 acpi_device_get_parent_softc(sc->tz_dev),
512 "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i,
513 TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i]));
519 * We are going to get _ACx level down (colder side), but give a guaranteed
520 * minimum cooling run time if requested.
522 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
523 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
525 getnanotime(&curtime);
526 timespecsub(&curtime, &sc->tz_cooling_started);
527 if (curtime.tv_sec < acpi_tz_min_runtime)
528 newactive = sc->tz_active;
531 /* Handle user override of active mode */
532 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
533 || sc->tz_requested < newactive))
534 newactive = sc->tz_requested;
536 /* update temperature-related flags */
537 newflags = TZ_THFLAG_NONE;
538 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
539 newflags |= TZ_THFLAG_PSV;
540 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
541 newflags |= TZ_THFLAG_HOT;
542 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
543 newflags |= TZ_THFLAG_CRT;
545 /* If the active cooling state has changed, we have to switch things. */
546 if (newactive != sc->tz_active) {
547 /* Turn off the cooling devices that are on, if any are */
548 if (sc->tz_active != TZ_ACTIVE_NONE)
549 acpi_ForeachPackageObject(
550 (ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer,
551 acpi_tz_switch_cooler_off, sc);
553 /* Turn on cooling devices that are required, if any are */
554 if (newactive != TZ_ACTIVE_NONE) {
555 acpi_ForeachPackageObject(
556 (ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer,
557 acpi_tz_switch_cooler_on, sc);
559 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
560 "switched from %s to %s: %d.%dC\n",
561 acpi_tz_aclevel_string(sc->tz_active),
562 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
563 sc->tz_active = newactive;
564 getnanotime(&sc->tz_cooling_started);
567 /* XXX (de)activate any passive cooling that may be required. */
570 * If the temperature is at _HOT or _CRT, increment our event count.
571 * If it has occurred enough times, shutdown the system. This is
572 * needed because some systems will report an invalid high temperature
573 * for one poll cycle. It is suspected this is due to the embedded
574 * controller timing out. A typical value is 138C for one cycle on
575 * a system that is otherwise 65C.
577 * If we're almost at that threshold, notify the user through devd(8).
579 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
580 sc->tz_validchecks++;
581 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
582 device_printf(sc->tz_dev,
583 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
584 TZ_KELVTOC(sc->tz_temperature));
585 shutdown_nice(RB_POWEROFF);
586 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
587 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
589 sc->tz_validchecks = 0;
591 sc->tz_thflags = newflags;
597 * Given an object, verify that it's a reference to a device of some sort,
598 * and try to switch it off.
601 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
605 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
607 cooler = acpi_GetReference(NULL, obj);
608 if (cooler == NULL) {
609 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
613 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
615 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
621 * Given an object, verify that it's a reference to a device of some sort,
622 * and try to switch it on.
624 * XXX replication of off/on function code is bad.
627 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
629 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
633 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
635 cooler = acpi_GetReference(NULL, obj);
636 if (cooler == NULL) {
637 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
641 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
643 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
644 if (ACPI_FAILURE(status)) {
645 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
646 "failed to activate %s - %s\n", acpi_name(cooler),
647 AcpiFormatException(status));
654 * Read/debug-print a parameter, default it to -1.
657 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
660 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
662 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
665 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
666 acpi_name(sc->tz_handle), node, *data));
673 * Sanity-check a temperature value. Assume that setpoints
674 * should be between 0C and 200C.
677 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
679 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
680 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
681 what, TZ_KELVTOC(*val));
687 * Respond to a sysctl on the active state node.
690 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
692 struct acpi_tz_softc *sc;
696 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
697 active = sc->tz_active;
698 error = sysctl_handle_int(oidp, &active, 0, req);
700 /* Error or no new value */
701 if (error != 0 || req->newptr == NULL)
703 if (active < -1 || active >= TZ_NUMLEVELS)
706 /* Set new preferred level and re-switch */
707 sc->tz_requested = active;
708 acpi_tz_signal(sc, 0);
713 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
715 struct acpi_tz_softc *sc;
718 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
719 enabled = sc->tz_cooling_enabled;
720 error = sysctl_handle_int(oidp, &enabled, 0, req);
722 /* Error or no new value */
723 if (error != 0 || req->newptr == NULL)
725 if (enabled != TRUE && enabled != FALSE)
729 if (acpi_tz_cooling_is_available(sc))
730 error = acpi_tz_cooling_thread_start(sc);
736 sc->tz_cooling_enabled = enabled;
741 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
743 struct acpi_tz_softc *sc;
748 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
750 error = sysctl_handle_int(oidp, &temp, 0, req);
752 /* Error or no new value */
753 if (error != 0 || req->newptr == NULL)
756 /* Only allow changing settings if override is set. */
757 if (!acpi_tz_override)
760 /* Check user-supplied value for sanity. */
761 acpi_tz_sanity(sc, &temp, "user-supplied temp");
770 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
772 struct acpi_tz_softc *sc;
777 val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
779 error = sysctl_handle_int(oidp, &val, 0, req);
781 /* Error or no new value */
782 if (error != 0 || req->newptr == NULL)
785 /* Only allow changing settings if override is set. */
786 if (!acpi_tz_override)
794 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
796 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
798 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
801 case TZ_NOTIFY_TEMPERATURE:
802 /* Temperature change occurred */
803 acpi_tz_signal(sc, 0);
805 case TZ_NOTIFY_DEVICES:
806 case TZ_NOTIFY_LEVELS:
807 /* Zone devices/setpoints changed */
808 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
811 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
812 "unknown Notify event 0x%x\n", notify);
816 acpi_UserNotify("Thermal", h, notify);
822 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
825 sc->tz_flags |= flags;
826 ACPI_UNLOCK(thermal);
827 wakeup(&acpi_tz_proc);
831 * Notifies can be generated asynchronously but have also been seen to be
832 * triggered by other thermal methods. One system generates a notify of
833 * 0x81 when the fan is turned on or off. Another generates it when _SCP
834 * is called. To handle these situations, we check the zone via
835 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
839 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
842 /* Check the current temperature and take action based on it */
845 /* If requested, get the power profile settings. */
846 if (flags & TZ_FLAG_GETPROFILE)
847 acpi_tz_power_profile(sc);
850 * If requested, check for new devices/setpoints. After finding them,
851 * check if we need to switch fans based on the new values.
853 if (flags & TZ_FLAG_GETSETTINGS) {
854 acpi_tz_establish(sc);
858 /* XXX passive cooling actions? */
862 * System power profile may have changed; fetch and notify the
863 * thermal zone accordingly.
865 * Since this can be called from an arbitrary eventhandler, it needs
866 * to get the ACPI lock itself.
869 acpi_tz_power_profile(void *arg)
872 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
875 state = power_profile_get_state();
876 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
879 /* check that we haven't decided there's no _SCP method */
880 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
882 /* Call _SCP to set the new profile */
883 status = acpi_SetInteger(sc->tz_handle, "_SCP",
884 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
885 if (ACPI_FAILURE(status)) {
886 if (status != AE_NOT_FOUND)
887 ACPI_VPRINT(sc->tz_dev,
888 acpi_device_get_parent_softc(sc->tz_dev),
889 "can't evaluate %s._SCP - %s\n",
890 acpi_name(sc->tz_handle),
891 AcpiFormatException(status));
892 sc->tz_flags |= TZ_FLAG_NO_SCP;
894 /* We have to re-evaluate the entire zone now */
895 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
901 * Thermal zone monitor thread.
904 acpi_tz_thread(void *arg)
909 struct acpi_tz_softc **sc;
911 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
918 /* If the number of devices has changed, re-evaluate. */
919 if (devclass_get_count(acpi_tz_devclass) != devcount) {
924 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
925 sc = malloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
927 for (i = 0; i < devcount; i++)
928 sc[i] = device_get_softc(devs[i]);
931 /* Check for temperature events and act on them. */
932 for (i = 0; i < devcount; i++) {
934 flags = sc[i]->tz_flags;
935 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
936 ACPI_UNLOCK(thermal);
937 acpi_tz_timeout(sc[i], flags);
940 /* If more work to do, don't go to sleep yet. */
942 for (i = 0; i < devcount; i++) {
943 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
948 * If we have no more work, sleep for a while, setting PDROP so that
949 * the mutex will not be reacquired. Otherwise, drop the mutex and
950 * loop to handle more events.
953 msleep(&acpi_tz_proc, &thermal_mutex, PZERO | PDROP, "tzpoll",
954 hz * acpi_tz_polling_rate);
956 ACPI_UNLOCK(thermal);
961 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
966 if (!sc->tz_cooling_updated)
968 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
970 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
971 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
972 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
973 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
975 sc->tz_cooling_updated = FALSE;
980 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
983 struct cf_level *levels;
984 int num_levels, error, freq, desired_freq, perf, i;
986 levels = malloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
991 * Find the main device, cpufreq0. We don't yet support independent
992 * CPU frequency control on SMP.
994 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
999 /* Get the current frequency. */
1000 error = CPUFREQ_GET(dev, &levels[0]);
1003 freq = levels[0].total_set.freq;
1005 /* Get the current available frequency levels. */
1006 num_levels = CPUFREQ_MAX_LEVELS;
1007 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1010 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1014 /* Calculate the desired frequency as a percent of the max frequency. */
1015 perf = 100 * freq / levels[0].total_set.freq - req;
1018 else if (perf > 100)
1020 desired_freq = levels[0].total_set.freq * perf / 100;
1022 if (desired_freq < freq) {
1023 /* Find the closest available frequency, rounding down. */
1024 for (i = 0; i < num_levels; i++)
1025 if (levels[i].total_set.freq <= desired_freq)
1028 /* If we didn't find a relevant setting, use the lowest. */
1029 if (i == num_levels)
1032 /* If we didn't decrease frequency yet, don't increase it. */
1033 if (!sc->tz_cooling_updated) {
1034 sc->tz_cooling_active = FALSE;
1038 /* Use saved cpu frequency as maximum value. */
1039 if (desired_freq > sc->tz_cooling_saved_freq)
1040 desired_freq = sc->tz_cooling_saved_freq;
1042 /* Find the closest available frequency, rounding up. */
1043 for (i = num_levels - 1; i >= 0; i--)
1044 if (levels[i].total_set.freq >= desired_freq)
1047 /* If we didn't find a relevant setting, use the highest. */
1051 /* If we're going to the highest frequency, restore the old setting. */
1052 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1053 error = acpi_tz_cpufreq_restore(sc);
1055 sc->tz_cooling_active = FALSE;
1060 /* If we are going to a new frequency, activate it. */
1061 if (levels[i].total_set.freq != freq) {
1062 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1063 "temperature %d.%dC: %screasing clock speed "
1064 "from %d MHz to %d MHz\n",
1065 TZ_KELVTOC(sc->tz_temperature),
1066 (freq > levels[i].total_set.freq) ? "de" : "in",
1067 freq, levels[i].total_set.freq);
1068 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1069 if (error == 0 && !sc->tz_cooling_updated) {
1070 sc->tz_cooling_saved_freq = freq;
1071 sc->tz_cooling_updated = TRUE;
1077 free(levels, M_TEMP);
1082 * Passive cooling thread; monitors current temperature according to the
1083 * cooling interval and calculates whether to scale back CPU frequency.
1086 acpi_tz_cooling_thread(void *arg)
1088 struct acpi_tz_softc *sc;
1089 int error, perf, curr_temp, prev_temp;
1091 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1093 sc = (struct acpi_tz_softc *)arg;
1095 prev_temp = sc->tz_temperature;
1096 while (sc->tz_cooling_enabled) {
1097 if (sc->tz_cooling_active)
1098 (void)acpi_tz_get_temperature(sc);
1099 curr_temp = sc->tz_temperature;
1100 if (curr_temp >= sc->tz_zone.psv)
1101 sc->tz_cooling_active = TRUE;
1102 if (sc->tz_cooling_active) {
1103 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1104 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1108 error = acpi_tz_cpufreq_update(sc, perf);
1111 * If error and not simply a higher priority setting was
1112 * active, disable cooling.
1114 if (error != 0 && error != EPERM) {
1115 device_printf(sc->tz_dev,
1116 "failed to set new freq, disabling passive cooling\n");
1117 sc->tz_cooling_enabled = FALSE;
1121 prev_temp = curr_temp;
1122 tsleep(&sc->tz_cooling_proc, PZERO, "cooling",
1123 hz * sc->tz_zone.tsp / 10);
1125 if (sc->tz_cooling_active) {
1126 acpi_tz_cpufreq_restore(sc);
1127 sc->tz_cooling_active = FALSE;
1129 sc->tz_cooling_proc = NULL;
1131 sc->tz_cooling_proc_running = FALSE;
1132 ACPI_UNLOCK(thermal);
1137 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1138 * all CPUs for us. However, it's possible in the future _PSL will
1139 * reference non-CPU devices so we may want to support it then.
1142 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1144 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1145 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1146 sc->tz_zone.psv != -1);
1150 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1156 if (sc->tz_cooling_proc_running) {
1157 ACPI_UNLOCK(thermal);
1160 sc->tz_cooling_proc_running = TRUE;
1161 ACPI_UNLOCK(thermal);
1163 if (sc->tz_cooling_proc == NULL) {
1164 snprintf(name, sizeof(name), "acpi_cooling%d",
1165 device_get_unit(sc->tz_dev));
1166 error = kproc_create(acpi_tz_cooling_thread, sc,
1167 &sc->tz_cooling_proc, RFHIGHPID, 0, name);
1169 device_printf(sc->tz_dev, "could not create thread - %d", error);
1171 sc->tz_cooling_proc_running = FALSE;
1172 ACPI_UNLOCK(thermal);