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");
177 acpi_tz_probe(device_t dev)
181 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
182 device_set_desc(dev, "Thermal Zone");
190 acpi_tz_attach(device_t dev)
192 struct acpi_tz_softc *sc;
193 struct acpi_softc *acpi_sc;
197 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
199 sc = device_get_softc(dev);
201 sc->tz_handle = acpi_get_handle(dev);
202 sc->tz_requested = TZ_ACTIVE_NONE;
203 sc->tz_active = TZ_ACTIVE_NONE;
204 sc->tz_thflags = TZ_THFLAG_NONE;
205 sc->tz_cooling_proc = NULL;
206 sc->tz_cooling_proc_running = FALSE;
207 sc->tz_cooling_active = FALSE;
208 sc->tz_cooling_updated = FALSE;
211 * Always attempt to enable passive cooling for tz0. Users can enable
212 * it for other zones manually for now.
214 * XXX We need to test if multiple zones conflict with each other
215 * since cpufreq currently sets all CPUs to the given frequency whereas
216 * it's possible for different thermal zones to specify independent
217 * settings for multiple CPUs.
219 sc->tz_cooling_enabled = (device_get_unit(dev) == 0);
222 * Parse the current state of the thermal zone and build control
223 * structures. We don't need to worry about interference with the
224 * control thread since we haven't fully attached this device yet.
226 if ((error = acpi_tz_establish(sc)) != 0)
230 * Register for any Notify events sent to this zone.
232 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
233 acpi_tz_notify_handler, sc);
236 * Create our sysctl nodes.
238 * XXX we need a mechanism for adding nodes under ACPI.
240 if (device_get_unit(dev) == 0) {
241 acpi_sc = acpi_device_get_parent_softc(dev);
242 sysctl_ctx_init(&acpi_tz_sysctl_ctx);
243 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
244 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
245 OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
246 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
247 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
248 OID_AUTO, "min_runtime", CTLFLAG_RW,
249 &acpi_tz_min_runtime, 0,
250 "minimum cooling run time in sec");
251 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
252 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
253 OID_AUTO, "polling_rate", CTLFLAG_RW,
254 &acpi_tz_polling_rate, 0, "monitor polling rate");
255 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
256 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
257 "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
258 "allow override of thermal settings");
260 sysctl_ctx_init(&sc->tz_sysctl_ctx);
261 sprintf(oidname, "tz%d", device_get_unit(dev));
262 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
263 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
264 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
265 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
266 OID_AUTO, "temperature", CTLFLAG_RD, &sc->tz_temperature,
267 sizeof(sc->tz_temperature), "IK",
268 "current thermal zone temperature");
269 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
270 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
271 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
272 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
273 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
274 sc, 0, acpi_tz_cooling_sysctl, "I",
275 "enable passive (speed reduction) cooling");
277 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
278 OID_AUTO, "thermal_flags", CTLFLAG_RD,
279 &sc->tz_thflags, 0, "thermal zone flags");
280 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
281 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
282 sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
283 acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
284 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
285 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
286 sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
287 acpi_tz_temp_sysctl, "IK",
288 "too hot temp setpoint (suspend now)");
289 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
290 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
291 sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
292 acpi_tz_temp_sysctl, "IK",
293 "critical temp setpoint (shutdown now)");
294 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
295 OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac,
296 sizeof(sc->tz_zone.ac), "IK", "");
297 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
298 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
299 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
300 acpi_tz_passive_sysctl, "I",
301 "thermal constant 1 for passive cooling");
302 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
303 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
304 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
305 acpi_tz_passive_sysctl, "I",
306 "thermal constant 2 for passive cooling");
307 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
308 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
309 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
310 acpi_tz_passive_sysctl, "I",
311 "thermal sampling period for passive cooling");
314 * Create thread to service all of the thermal zones. Register
315 * our power profile event handler.
317 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
318 acpi_tz_power_profile, sc, 0);
319 if (acpi_tz_proc == NULL) {
320 error = kproc_create(acpi_tz_thread, NULL, &acpi_tz_proc,
321 RFHIGHPID, 0, "acpi_thermal");
323 device_printf(sc->tz_dev, "could not create thread - %d", error);
328 /* Create a thread to handle passive cooling for each zone if enabled. */
329 if (sc->tz_cooling_enabled) {
330 if (acpi_tz_cooling_is_available(sc)) {
331 error = acpi_tz_cooling_thread_start(sc);
333 sc->tz_cooling_enabled = FALSE;
337 sc->tz_cooling_enabled = FALSE;
341 * Flag the event handler for a manual invocation by our timeout.
342 * We defer it like this so that the rest of the subsystem has time
343 * to come up. Don't bother evaluating/printing the temperature at
344 * this point; on many systems it'll be bogus until the EC is running.
346 sc->tz_flags |= TZ_FLAG_GETPROFILE;
350 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
351 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
352 acpi_tz_notify_handler);
353 sysctl_ctx_free(&sc->tz_sysctl_ctx);
355 return_VALUE (error);
359 * Parse the current state of this thermal zone and set up to use it.
361 * Note that we may have previous state, which will have to be discarded.
364 acpi_tz_establish(struct acpi_tz_softc *sc)
370 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
372 /* Erase any existing state. */
373 for (i = 0; i < TZ_NUMLEVELS; i++)
374 if (sc->tz_zone.al[i].Pointer != NULL)
375 AcpiOsFree(sc->tz_zone.al[i].Pointer);
376 if (sc->tz_zone.psl.Pointer != NULL)
377 AcpiOsFree(sc->tz_zone.psl.Pointer);
380 * XXX: We initialize only ACPI_BUFFER to avoid race condition
381 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
383 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
384 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
385 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
387 /* Evaluate thermal zone parameters. */
388 for (i = 0; i < TZ_NUMLEVELS; i++) {
389 sprintf(nbuf, "_AC%d", i);
390 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
391 sprintf(nbuf, "_AL%d", i);
392 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
393 sc->tz_zone.al[i].Pointer = NULL;
394 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
395 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
397 /* Should be a package containing a list of power objects */
398 if (obj->Type != ACPI_TYPE_PACKAGE) {
399 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
401 return_VALUE (ENXIO);
405 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
406 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
407 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
408 sc->tz_zone.psl.Pointer = NULL;
409 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
410 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
411 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
412 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
413 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
414 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
417 * Sanity-check the values we've been given.
419 * XXX what do we do about systems that give us the same value for
420 * more than one of these setpoints?
422 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
423 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
424 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
425 for (i = 0; i < TZ_NUMLEVELS; i++)
426 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
431 static char *aclevel_string[] = {
432 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
433 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
436 static __inline const char *
437 acpi_tz_aclevel_string(int active)
439 if (active < -1 || active >= TZ_NUMLEVELS)
440 return (aclevel_string[0]);
442 return (aclevel_string[active + 1]);
446 * Get the current temperature.
449 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
453 static char *tmp_name = "_TMP";
455 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
457 /* Evaluate the thermal zone's _TMP method. */
458 status = acpi_GetInteger(sc->tz_handle, tmp_name, &temp);
459 if (ACPI_FAILURE(status)) {
460 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
461 "error fetching current temperature -- %s\n",
462 AcpiFormatException(status));
466 /* Check it for validity. */
467 acpi_tz_sanity(sc, &temp, tmp_name);
471 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
472 sc->tz_temperature = temp;
477 * Evaluate the condition of a thermal zone, take appropriate actions.
480 acpi_tz_monitor(void *Context)
482 struct acpi_tz_softc *sc;
483 struct timespec curtime;
486 int newactive, newflags;
488 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
490 sc = (struct acpi_tz_softc *)Context;
492 /* Get the current temperature. */
493 if (!acpi_tz_get_temperature(sc)) {
494 /* XXX disable zone? go to max cooling? */
497 temp = sc->tz_temperature;
500 * Work out what we ought to be doing right now.
502 * Note that the _ACx levels sort from hot to cold.
504 newactive = TZ_ACTIVE_NONE;
505 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
506 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i]) {
508 if (sc->tz_active != newactive) {
509 ACPI_VPRINT(sc->tz_dev,
510 acpi_device_get_parent_softc(sc->tz_dev),
511 "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i,
512 TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i]));
518 * We are going to get _ACx level down (colder side), but give a guaranteed
519 * minimum cooling run time if requested.
521 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
522 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
524 getnanotime(&curtime);
525 timespecsub(&curtime, &sc->tz_cooling_started);
526 if (curtime.tv_sec < acpi_tz_min_runtime)
527 newactive = sc->tz_active;
530 /* Handle user override of active mode */
531 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
532 || sc->tz_requested < newactive))
533 newactive = sc->tz_requested;
535 /* update temperature-related flags */
536 newflags = TZ_THFLAG_NONE;
537 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
538 newflags |= TZ_THFLAG_PSV;
539 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
540 newflags |= TZ_THFLAG_HOT;
541 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
542 newflags |= TZ_THFLAG_CRT;
544 /* If the active cooling state has changed, we have to switch things. */
545 if (newactive != sc->tz_active) {
546 /* Turn off the cooling devices that are on, if any are */
547 if (sc->tz_active != TZ_ACTIVE_NONE)
548 acpi_ForeachPackageObject(
549 (ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer,
550 acpi_tz_switch_cooler_off, sc);
552 /* Turn on cooling devices that are required, if any are */
553 if (newactive != TZ_ACTIVE_NONE) {
554 acpi_ForeachPackageObject(
555 (ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer,
556 acpi_tz_switch_cooler_on, sc);
558 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
559 "switched from %s to %s: %d.%dC\n",
560 acpi_tz_aclevel_string(sc->tz_active),
561 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
562 sc->tz_active = newactive;
563 getnanotime(&sc->tz_cooling_started);
566 /* XXX (de)activate any passive cooling that may be required. */
569 * If the temperature is at _HOT or _CRT, increment our event count.
570 * If it has occurred enough times, shutdown the system. This is
571 * needed because some systems will report an invalid high temperature
572 * for one poll cycle. It is suspected this is due to the embedded
573 * controller timing out. A typical value is 138C for one cycle on
574 * a system that is otherwise 65C.
576 * If we're almost at that threshold, notify the user through devd(8).
578 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
579 sc->tz_validchecks++;
580 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
581 device_printf(sc->tz_dev,
582 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
583 TZ_KELVTOC(sc->tz_temperature));
584 shutdown_nice(RB_POWEROFF);
585 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
586 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
588 sc->tz_validchecks = 0;
590 sc->tz_thflags = newflags;
596 * Given an object, verify that it's a reference to a device of some sort,
597 * and try to switch it off.
600 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
604 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
606 cooler = acpi_GetReference(NULL, obj);
607 if (cooler == NULL) {
608 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
612 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
614 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
620 * Given an object, verify that it's a reference to a device of some sort,
621 * and try to switch it on.
623 * XXX replication of off/on function code is bad.
626 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
628 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
632 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
634 cooler = acpi_GetReference(NULL, obj);
635 if (cooler == NULL) {
636 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
640 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
642 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
643 if (ACPI_FAILURE(status)) {
644 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
645 "failed to activate %s - %s\n", acpi_name(cooler),
646 AcpiFormatException(status));
653 * Read/debug-print a parameter, default it to -1.
656 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
659 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
661 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
664 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
665 acpi_name(sc->tz_handle), node, *data));
672 * Sanity-check a temperature value. Assume that setpoints
673 * should be between 0C and 200C.
676 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
678 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
679 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
680 what, TZ_KELVTOC(*val));
686 * Respond to a sysctl on the active state node.
689 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
691 struct acpi_tz_softc *sc;
695 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
696 active = sc->tz_active;
697 error = sysctl_handle_int(oidp, &active, 0, req);
699 /* Error or no new value */
700 if (error != 0 || req->newptr == NULL)
702 if (active < -1 || active >= TZ_NUMLEVELS)
705 /* Set new preferred level and re-switch */
706 sc->tz_requested = active;
707 acpi_tz_signal(sc, 0);
712 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
714 struct acpi_tz_softc *sc;
717 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
718 enabled = sc->tz_cooling_enabled;
719 error = sysctl_handle_int(oidp, &enabled, 0, req);
721 /* Error or no new value */
722 if (error != 0 || req->newptr == NULL)
724 if (enabled != TRUE && enabled != FALSE)
728 if (acpi_tz_cooling_is_available(sc))
729 error = acpi_tz_cooling_thread_start(sc);
735 sc->tz_cooling_enabled = enabled;
740 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
742 struct acpi_tz_softc *sc;
747 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
749 error = sysctl_handle_int(oidp, &temp, 0, req);
751 /* Error or no new value */
752 if (error != 0 || req->newptr == NULL)
755 /* Only allow changing settings if override is set. */
756 if (!acpi_tz_override)
759 /* Check user-supplied value for sanity. */
760 acpi_tz_sanity(sc, &temp, "user-supplied temp");
769 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
771 struct acpi_tz_softc *sc;
776 val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
778 error = sysctl_handle_int(oidp, &val, 0, req);
780 /* Error or no new value */
781 if (error != 0 || req->newptr == NULL)
784 /* Only allow changing settings if override is set. */
785 if (!acpi_tz_override)
793 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
795 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
797 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
800 case TZ_NOTIFY_TEMPERATURE:
801 /* Temperature change occurred */
802 acpi_tz_signal(sc, 0);
804 case TZ_NOTIFY_DEVICES:
805 case TZ_NOTIFY_LEVELS:
806 /* Zone devices/setpoints changed */
807 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
810 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
811 "unknown Notify event 0x%x\n", notify);
815 acpi_UserNotify("Thermal", h, notify);
821 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
824 sc->tz_flags |= flags;
825 ACPI_UNLOCK(thermal);
826 wakeup(&acpi_tz_proc);
830 * Notifies can be generated asynchronously but have also been seen to be
831 * triggered by other thermal methods. One system generates a notify of
832 * 0x81 when the fan is turned on or off. Another generates it when _SCP
833 * is called. To handle these situations, we check the zone via
834 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
838 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
841 /* Check the current temperature and take action based on it */
844 /* If requested, get the power profile settings. */
845 if (flags & TZ_FLAG_GETPROFILE)
846 acpi_tz_power_profile(sc);
849 * If requested, check for new devices/setpoints. After finding them,
850 * check if we need to switch fans based on the new values.
852 if (flags & TZ_FLAG_GETSETTINGS) {
853 acpi_tz_establish(sc);
857 /* XXX passive cooling actions? */
861 * System power profile may have changed; fetch and notify the
862 * thermal zone accordingly.
864 * Since this can be called from an arbitrary eventhandler, it needs
865 * to get the ACPI lock itself.
868 acpi_tz_power_profile(void *arg)
871 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
874 state = power_profile_get_state();
875 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
878 /* check that we haven't decided there's no _SCP method */
879 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
881 /* Call _SCP to set the new profile */
882 status = acpi_SetInteger(sc->tz_handle, "_SCP",
883 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
884 if (ACPI_FAILURE(status)) {
885 if (status != AE_NOT_FOUND)
886 ACPI_VPRINT(sc->tz_dev,
887 acpi_device_get_parent_softc(sc->tz_dev),
888 "can't evaluate %s._SCP - %s\n",
889 acpi_name(sc->tz_handle),
890 AcpiFormatException(status));
891 sc->tz_flags |= TZ_FLAG_NO_SCP;
893 /* We have to re-evaluate the entire zone now */
894 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
900 * Thermal zone monitor thread.
903 acpi_tz_thread(void *arg)
908 struct acpi_tz_softc **sc;
910 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
917 /* If the number of devices has changed, re-evaluate. */
918 if (devclass_get_count(acpi_tz_devclass) != devcount) {
923 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
924 sc = malloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
926 for (i = 0; i < devcount; i++)
927 sc[i] = device_get_softc(devs[i]);
930 /* Check for temperature events and act on them. */
931 for (i = 0; i < devcount; i++) {
933 flags = sc[i]->tz_flags;
934 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
935 ACPI_UNLOCK(thermal);
936 acpi_tz_timeout(sc[i], flags);
939 /* If more work to do, don't go to sleep yet. */
941 for (i = 0; i < devcount; i++) {
942 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
947 * If we have no more work, sleep for a while, setting PDROP so that
948 * the mutex will not be reacquired. Otherwise, drop the mutex and
949 * loop to handle more events.
952 msleep(&acpi_tz_proc, &thermal_mutex, PZERO | PDROP, "tzpoll",
953 hz * acpi_tz_polling_rate);
955 ACPI_UNLOCK(thermal);
960 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
965 if (!sc->tz_cooling_updated)
967 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
969 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
970 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
971 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
972 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
974 sc->tz_cooling_updated = FALSE;
979 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
982 struct cf_level *levels;
983 int num_levels, error, freq, desired_freq, perf, i;
985 levels = malloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
990 * Find the main device, cpufreq0. We don't yet support independent
991 * CPU frequency control on SMP.
993 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
998 /* Get the current frequency. */
999 error = CPUFREQ_GET(dev, &levels[0]);
1002 freq = levels[0].total_set.freq;
1004 /* Get the current available frequency levels. */
1005 num_levels = CPUFREQ_MAX_LEVELS;
1006 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1009 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1013 /* Calculate the desired frequency as a percent of the max frequency. */
1014 perf = 100 * freq / levels[0].total_set.freq - req;
1017 else if (perf > 100)
1019 desired_freq = levels[0].total_set.freq * perf / 100;
1021 if (desired_freq < freq) {
1022 /* Find the closest available frequency, rounding down. */
1023 for (i = 0; i < num_levels; i++)
1024 if (levels[i].total_set.freq <= desired_freq)
1027 /* If we didn't find a relevant setting, use the lowest. */
1028 if (i == num_levels)
1031 /* If we didn't decrease frequency yet, don't increase it. */
1032 if (!sc->tz_cooling_updated) {
1033 sc->tz_cooling_active = FALSE;
1037 /* Use saved cpu frequency as maximum value. */
1038 if (desired_freq > sc->tz_cooling_saved_freq)
1039 desired_freq = sc->tz_cooling_saved_freq;
1041 /* Find the closest available frequency, rounding up. */
1042 for (i = num_levels - 1; i >= 0; i--)
1043 if (levels[i].total_set.freq >= desired_freq)
1046 /* If we didn't find a relevant setting, use the highest. */
1050 /* If we're going to the highest frequency, restore the old setting. */
1051 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1052 error = acpi_tz_cpufreq_restore(sc);
1054 sc->tz_cooling_active = FALSE;
1059 /* If we are going to a new frequency, activate it. */
1060 if (levels[i].total_set.freq != freq) {
1061 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1062 "temperature %d.%dC: %screasing clock speed "
1063 "from %d MHz to %d MHz\n",
1064 TZ_KELVTOC(sc->tz_temperature),
1065 (freq > levels[i].total_set.freq) ? "de" : "in",
1066 freq, levels[i].total_set.freq);
1067 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1068 if (error == 0 && !sc->tz_cooling_updated) {
1069 sc->tz_cooling_saved_freq = freq;
1070 sc->tz_cooling_updated = TRUE;
1076 free(levels, M_TEMP);
1081 * Passive cooling thread; monitors current temperature according to the
1082 * cooling interval and calculates whether to scale back CPU frequency.
1085 acpi_tz_cooling_thread(void *arg)
1087 struct acpi_tz_softc *sc;
1088 int error, perf, curr_temp, prev_temp;
1090 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1092 sc = (struct acpi_tz_softc *)arg;
1094 prev_temp = sc->tz_temperature;
1095 while (sc->tz_cooling_enabled) {
1096 if (sc->tz_cooling_active)
1097 (void)acpi_tz_get_temperature(sc);
1098 curr_temp = sc->tz_temperature;
1099 if (curr_temp >= sc->tz_zone.psv)
1100 sc->tz_cooling_active = TRUE;
1101 if (sc->tz_cooling_active) {
1102 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1103 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1107 error = acpi_tz_cpufreq_update(sc, perf);
1110 * If error and not simply a higher priority setting was
1111 * active, disable cooling.
1113 if (error != 0 && error != EPERM) {
1114 device_printf(sc->tz_dev,
1115 "failed to set new freq, disabling passive cooling\n");
1116 sc->tz_cooling_enabled = FALSE;
1120 prev_temp = curr_temp;
1121 tsleep(&sc->tz_cooling_proc, PZERO, "cooling",
1122 hz * sc->tz_zone.tsp / 10);
1124 if (sc->tz_cooling_active) {
1125 acpi_tz_cpufreq_restore(sc);
1126 sc->tz_cooling_active = FALSE;
1128 sc->tz_cooling_proc = NULL;
1130 sc->tz_cooling_proc_running = FALSE;
1131 ACPI_UNLOCK(thermal);
1136 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1137 * all CPUs for us. However, it's possible in the future _PSL will
1138 * reference non-CPU devices so we may want to support it then.
1141 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1143 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1144 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1145 sc->tz_zone.psv != -1);
1149 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1155 if (sc->tz_cooling_proc_running) {
1156 ACPI_UNLOCK(thermal);
1159 sc->tz_cooling_proc_running = TRUE;
1160 ACPI_UNLOCK(thermal);
1162 if (sc->tz_cooling_proc == NULL) {
1163 snprintf(name, sizeof(name), "acpi_cooling%d",
1164 device_get_unit(sc->tz_dev));
1165 error = kproc_create(acpi_tz_cooling_thread, sc,
1166 &sc->tz_cooling_proc, RFHIGHPID, 0, name);
1168 device_printf(sc->tz_dev, "could not create thread - %d", error);
1170 sc->tz_cooling_proc_running = FALSE;
1171 ACPI_UNLOCK(thermal);