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 void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
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);
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),
153 static driver_t acpi_tz_driver = {
156 sizeof(struct acpi_tz_softc),
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);
163 static struct sysctl_ctx_list acpi_tz_sysctl_ctx;
164 static struct sysctl_oid *acpi_tz_sysctl_tree;
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;
171 /* Timezone polling thread */
172 static struct proc *acpi_tz_proc;
173 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
176 acpi_tz_probe(device_t dev)
180 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
181 device_set_desc(dev, "Thermal Zone");
189 acpi_tz_attach(device_t dev)
191 struct acpi_tz_softc *sc;
192 struct acpi_softc *acpi_sc;
196 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
198 sc = device_get_softc(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;
210 * Always attempt to enable passive cooling for tz0. Users can enable
211 * it for other zones manually for now.
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.
218 sc->tz_cooling_enabled = (device_get_unit(dev) == 0);
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.
225 if ((error = acpi_tz_establish(sc)) != 0)
229 * Register for any Notify events sent to this zone.
231 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
232 acpi_tz_notify_handler, sc);
235 * Create our sysctl nodes.
237 * XXX we need a mechanism for adding nodes under ACPI.
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");
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");
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", "");
298 * Create thread to service all of the thermal zones. Register
299 * our power profile event handler.
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");
307 device_printf(sc->tz_dev, "could not create thread - %d", error);
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);
317 sc->tz_cooling_enabled = FALSE;
321 sc->tz_cooling_enabled = FALSE;
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.
330 sc->tz_flags |= TZ_FLAG_GETPROFILE;
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);
339 return_VALUE (error);
343 * Parse the current state of this thermal zone and set up to use it.
345 * Note that we may have previous state, which will have to be discarded.
348 acpi_tz_establish(struct acpi_tz_softc *sc)
354 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
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);
364 * XXX: We initialize only ACPI_BUFFER to avoid race condition
365 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
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));
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;
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",
385 return_VALUE (ENXIO);
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);
401 * Sanity-check the values we've been given.
403 * XXX what do we do about systems that give us the same value for
404 * more than one of these setpoints?
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");
415 static char *aclevel_string[] = {
416 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
417 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
420 static __inline const char *
421 acpi_tz_aclevel_string(int active)
423 if (active < -1 || active >= TZ_NUMLEVELS)
424 return (aclevel_string[0]);
426 return (aclevel_string[active + 1]);
430 * Get the current temperature.
433 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
437 static char *tmp_name = "_TMP";
439 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
441 /* Evaluate the thermal zone's _TMP method. */
442 status = acpi_GetInteger(sc->tz_handle, tmp_name, &temp);
443 if (ACPI_FAILURE(status)) {
444 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
445 "error fetching current temperature -- %s\n",
446 AcpiFormatException(status));
450 /* Check it for validity. */
451 acpi_tz_sanity(sc, &temp, tmp_name);
455 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
456 sc->tz_temperature = temp;
461 * Evaluate the condition of a thermal zone, take appropriate actions.
464 acpi_tz_monitor(void *Context)
466 struct acpi_tz_softc *sc;
467 struct timespec curtime;
470 int newactive, newflags;
472 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
474 sc = (struct acpi_tz_softc *)Context;
476 /* Get the current temperature. */
477 if (!acpi_tz_get_temperature(sc)) {
478 /* XXX disable zone? go to max cooling? */
481 temp = sc->tz_temperature;
484 * Work out what we ought to be doing right now.
486 * Note that the _ACx levels sort from hot to cold.
488 newactive = TZ_ACTIVE_NONE;
489 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
490 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i]) {
492 if (sc->tz_active != newactive) {
493 ACPI_VPRINT(sc->tz_dev,
494 acpi_device_get_parent_softc(sc->tz_dev),
495 "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i,
496 TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i]));
502 * We are going to get _ACx level down (colder side), but give a guaranteed
503 * minimum cooling run time if requested.
505 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
506 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
508 getnanotime(&curtime);
509 timespecsub(&curtime, &sc->tz_cooling_started);
510 if (curtime.tv_sec < acpi_tz_min_runtime)
511 newactive = sc->tz_active;
514 /* Handle user override of active mode */
515 if (sc->tz_requested != TZ_ACTIVE_NONE && sc->tz_requested < newactive)
516 newactive = sc->tz_requested;
518 /* update temperature-related flags */
519 newflags = TZ_THFLAG_NONE;
520 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
521 newflags |= TZ_THFLAG_PSV;
522 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
523 newflags |= TZ_THFLAG_HOT;
524 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
525 newflags |= TZ_THFLAG_CRT;
527 /* If the active cooling state has changed, we have to switch things. */
528 if (newactive != sc->tz_active) {
529 /* Turn off the cooling devices that are on, if any are */
530 if (sc->tz_active != TZ_ACTIVE_NONE)
531 acpi_ForeachPackageObject(
532 (ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer,
533 acpi_tz_switch_cooler_off, sc);
535 /* Turn on cooling devices that are required, if any are */
536 if (newactive != TZ_ACTIVE_NONE) {
537 acpi_ForeachPackageObject(
538 (ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer,
539 acpi_tz_switch_cooler_on, sc);
541 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
542 "switched from %s to %s: %d.%dC\n",
543 acpi_tz_aclevel_string(sc->tz_active),
544 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
545 sc->tz_active = newactive;
546 getnanotime(&sc->tz_cooling_started);
549 /* XXX (de)activate any passive cooling that may be required. */
552 * If the temperature is at _HOT or _CRT, increment our event count.
553 * If it has occurred enough times, shutdown the system. This is
554 * needed because some systems will report an invalid high temperature
555 * for one poll cycle. It is suspected this is due to the embedded
556 * controller timing out. A typical value is 138C for one cycle on
557 * a system that is otherwise 65C.
559 * If we're almost at that threshold, notify the user through devd(8).
561 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
562 sc->tz_validchecks++;
563 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
564 device_printf(sc->tz_dev,
565 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
566 TZ_KELVTOC(sc->tz_temperature));
567 shutdown_nice(RB_POWEROFF);
568 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
569 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
571 sc->tz_validchecks = 0;
573 sc->tz_thflags = newflags;
579 * Given an object, verify that it's a reference to a device of some sort,
580 * and try to switch it off.
583 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
587 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
589 cooler = acpi_GetReference(NULL, obj);
590 if (cooler == NULL) {
591 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
595 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
597 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
603 * Given an object, verify that it's a reference to a device of some sort,
604 * and try to switch it on.
606 * XXX replication of off/on function code is bad.
609 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
611 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
615 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
617 cooler = acpi_GetReference(NULL, obj);
618 if (cooler == NULL) {
619 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
623 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
625 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
626 if (ACPI_FAILURE(status)) {
627 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
628 "failed to activate %s - %s\n", acpi_name(cooler),
629 AcpiFormatException(status));
636 * Read/debug-print a parameter, default it to -1.
639 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
642 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
644 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
647 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
648 acpi_name(sc->tz_handle), node, *data));
655 * Sanity-check a temperature value. Assume that setpoints
656 * should be between 0C and 200C.
659 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
661 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
662 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
663 what, TZ_KELVTOC(*val));
669 * Respond to a sysctl on the active state node.
672 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
674 struct acpi_tz_softc *sc;
678 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
679 active = sc->tz_active;
680 error = sysctl_handle_int(oidp, &active, 0, req);
682 /* Error or no new value */
683 if (error != 0 || req->newptr == NULL)
685 if (active < -1 || active >= TZ_NUMLEVELS)
688 /* Set new preferred level and re-switch */
689 sc->tz_requested = active;
690 acpi_tz_signal(sc, 0);
695 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
697 struct acpi_tz_softc *sc;
700 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
701 enabled = sc->tz_cooling_enabled;
702 error = sysctl_handle_int(oidp, &enabled, 0, req);
704 /* Error or no new value */
705 if (error != 0 || req->newptr == NULL)
707 if (enabled != TRUE && enabled != FALSE)
711 if (acpi_tz_cooling_is_available(sc))
712 error = acpi_tz_cooling_thread_start(sc);
718 sc->tz_cooling_enabled = enabled;
723 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
725 struct acpi_tz_softc *sc;
730 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
732 error = sysctl_handle_int(oidp, &temp, 0, req);
734 /* Error or no new value */
735 if (error != 0 || req->newptr == NULL)
738 /* Only allow changing settings if override is set. */
739 if (!acpi_tz_override)
742 /* Check user-supplied value for sanity. */
743 acpi_tz_sanity(sc, &temp, "user-supplied temp");
752 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
754 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
756 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
759 case TZ_NOTIFY_TEMPERATURE:
760 /* Temperature change occurred */
761 acpi_tz_signal(sc, 0);
763 case TZ_NOTIFY_DEVICES:
764 case TZ_NOTIFY_LEVELS:
765 /* Zone devices/setpoints changed */
766 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
769 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
770 "unknown Notify event 0x%x\n", notify);
774 acpi_UserNotify("Thermal", h, notify);
780 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
783 sc->tz_flags |= flags;
784 ACPI_UNLOCK(thermal);
785 wakeup(&acpi_tz_proc);
789 * Notifies can be generated asynchronously but have also been seen to be
790 * triggered by other thermal methods. One system generates a notify of
791 * 0x81 when the fan is turned on or off. Another generates it when _SCP
792 * is called. To handle these situations, we check the zone via
793 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
797 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
800 /* Check the current temperature and take action based on it */
803 /* If requested, get the power profile settings. */
804 if (flags & TZ_FLAG_GETPROFILE)
805 acpi_tz_power_profile(sc);
808 * If requested, check for new devices/setpoints. After finding them,
809 * check if we need to switch fans based on the new values.
811 if (flags & TZ_FLAG_GETSETTINGS) {
812 acpi_tz_establish(sc);
816 /* XXX passive cooling actions? */
820 * System power profile may have changed; fetch and notify the
821 * thermal zone accordingly.
823 * Since this can be called from an arbitrary eventhandler, it needs
824 * to get the ACPI lock itself.
827 acpi_tz_power_profile(void *arg)
830 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
833 state = power_profile_get_state();
834 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
837 /* check that we haven't decided there's no _SCP method */
838 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
840 /* Call _SCP to set the new profile */
841 status = acpi_SetInteger(sc->tz_handle, "_SCP",
842 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
843 if (ACPI_FAILURE(status)) {
844 if (status != AE_NOT_FOUND)
845 ACPI_VPRINT(sc->tz_dev,
846 acpi_device_get_parent_softc(sc->tz_dev),
847 "can't evaluate %s._SCP - %s\n",
848 acpi_name(sc->tz_handle),
849 AcpiFormatException(status));
850 sc->tz_flags |= TZ_FLAG_NO_SCP;
852 /* We have to re-evaluate the entire zone now */
853 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
859 * Thermal zone monitor thread.
862 acpi_tz_thread(void *arg)
867 struct acpi_tz_softc **sc;
869 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
876 /* If the number of devices has changed, re-evaluate. */
877 if (devclass_get_maxunit(acpi_tz_devclass) != devcount) {
882 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
883 sc = malloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
885 for (i = 0; i < devcount; i++)
886 sc[i] = device_get_softc(devs[i]);
889 /* Check for temperature events and act on them. */
890 for (i = 0; i < devcount; i++) {
892 flags = sc[i]->tz_flags;
893 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
894 ACPI_UNLOCK(thermal);
895 acpi_tz_timeout(sc[i], flags);
898 /* If more work to do, don't go to sleep yet. */
900 for (i = 0; i < devcount; i++) {
901 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
906 * If we have no more work, sleep for a while, setting PDROP so that
907 * the mutex will not be reacquired. Otherwise, drop the mutex and
908 * loop to handle more events.
911 msleep(&acpi_tz_proc, &thermal_mutex, PZERO | PDROP, "tzpoll",
912 hz * acpi_tz_polling_rate);
914 ACPI_UNLOCK(thermal);
919 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
924 if (!sc->tz_cooling_updated)
926 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
928 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
929 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
930 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
931 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
933 sc->tz_cooling_updated = FALSE;
938 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
941 struct cf_level *levels;
942 int num_levels, error, freq, desired_freq, perf, i;
944 levels = malloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
949 * Find the main device, cpufreq0. We don't yet support independent
950 * CPU frequency control on SMP.
952 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
957 /* Get the current frequency. */
958 error = CPUFREQ_GET(dev, &levels[0]);
961 freq = levels[0].total_set.freq;
963 /* Get the current available frequency levels. */
964 num_levels = CPUFREQ_MAX_LEVELS;
965 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
968 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
972 /* Calculate the desired frequency as a percent of the max frequency. */
973 perf = 100 * freq / levels[0].total_set.freq - req;
978 desired_freq = levels[0].total_set.freq * perf / 100;
980 if (desired_freq < freq) {
981 /* Find the closest available frequency, rounding down. */
982 for (i = 0; i < num_levels; i++)
983 if (levels[i].total_set.freq <= desired_freq)
986 /* If we didn't find a relevant setting, use the lowest. */
990 /* If we didn't decrease frequency yet, don't increase it. */
991 if (!sc->tz_cooling_updated) {
992 sc->tz_cooling_active = FALSE;
996 /* Use saved cpu frequency as maximum value. */
997 if (desired_freq > sc->tz_cooling_saved_freq)
998 desired_freq = sc->tz_cooling_saved_freq;
1000 /* Find the closest available frequency, rounding up. */
1001 for (i = num_levels - 1; i >= 0; i--)
1002 if (levels[i].total_set.freq >= desired_freq)
1005 /* If we didn't find a relevant setting, use the highest. */
1009 /* If we're going to the highest frequency, restore the old setting. */
1010 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1011 error = acpi_tz_cpufreq_restore(sc);
1013 sc->tz_cooling_active = FALSE;
1018 /* If we are going to a new frequency, activate it. */
1019 if (levels[i].total_set.freq != freq) {
1020 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1021 "temperature %d.%dC: %screasing clock speed "
1022 "from %d MHz to %d MHz\n",
1023 TZ_KELVTOC(sc->tz_temperature),
1024 (freq > levels[i].total_set.freq) ? "de" : "in",
1025 freq, levels[i].total_set.freq);
1026 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1027 if (error == 0 && !sc->tz_cooling_updated) {
1028 sc->tz_cooling_saved_freq = freq;
1029 sc->tz_cooling_updated = TRUE;
1035 free(levels, M_TEMP);
1040 * Passive cooling thread; monitors current temperature according to the
1041 * cooling interval and calculates whether to scale back CPU frequency.
1044 acpi_tz_cooling_thread(void *arg)
1046 struct acpi_tz_softc *sc;
1047 int error, perf, curr_temp, prev_temp;
1049 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1051 sc = (struct acpi_tz_softc *)arg;
1053 prev_temp = sc->tz_temperature;
1054 while (sc->tz_cooling_enabled) {
1055 if (sc->tz_cooling_active)
1056 (void)acpi_tz_get_temperature(sc);
1057 curr_temp = sc->tz_temperature;
1058 if (curr_temp >= sc->tz_zone.psv)
1059 sc->tz_cooling_active = TRUE;
1060 if (sc->tz_cooling_active) {
1061 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1062 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1066 error = acpi_tz_cpufreq_update(sc, perf);
1069 * If error and not simply a higher priority setting was
1070 * active, disable cooling.
1072 if (error != 0 && error != EPERM) {
1073 device_printf(sc->tz_dev,
1074 "failed to set new freq, disabling passive cooling\n");
1075 sc->tz_cooling_enabled = FALSE;
1079 prev_temp = curr_temp;
1080 tsleep(&sc->tz_cooling_proc, PZERO, "cooling",
1081 hz * sc->tz_zone.tsp / 10);
1083 if (sc->tz_cooling_active) {
1084 acpi_tz_cpufreq_restore(sc);
1085 sc->tz_cooling_active = FALSE;
1087 sc->tz_cooling_proc = NULL;
1089 sc->tz_cooling_proc_running = FALSE;
1090 ACPI_UNLOCK(thermal);
1095 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1096 * all CPUs for us. However, it's possible in the future _PSL will
1097 * reference non-CPU devices so we may want to support it then.
1100 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1102 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1103 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1104 sc->tz_zone.psv != -1);
1108 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1114 if (sc->tz_cooling_proc_running) {
1115 ACPI_UNLOCK(thermal);
1118 sc->tz_cooling_proc_running = TRUE;
1119 ACPI_UNLOCK(thermal);
1121 if (sc->tz_cooling_proc == NULL) {
1122 snprintf(name, sizeof(name), "acpi_cooling%d",
1123 device_get_unit(sc->tz_dev));
1124 error = kthread_create(acpi_tz_cooling_thread, sc,
1125 &sc->tz_cooling_proc, RFHIGHPID, 0, name);
1127 device_printf(sc->tz_dev, "could not create thread - %d", error);
1129 sc->tz_cooling_proc_running = FALSE;
1130 ACPI_UNLOCK(thermal);