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/eventhandler.h>
34 #include <sys/kernel.h>
37 #include <sys/kthread.h>
38 #include <sys/malloc.h>
39 #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/include/acpi.h>
49 #include <contrib/dev/acpica/include/accommon.h>
51 #include <dev/acpica/acpivar.h>
53 /* Hooks for the ACPI CA debugging infrastructure */
54 #define _COMPONENT ACPI_THERMAL
55 ACPI_MODULE_NAME("THERMAL")
58 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
60 #define TZ_NOTIFY_TEMPERATURE 0x80 /* Temperature changed. */
61 #define TZ_NOTIFY_LEVELS 0x81 /* Cooling levels changed. */
62 #define TZ_NOTIFY_DEVICES 0x82 /* Device lists changed. */
63 #define TZ_NOTIFY_CRITICAL 0xcc /* Fake notify that _CRT/_HOT reached. */
65 /* Check for temperature changes every 10 seconds by default */
66 #define TZ_POLLRATE 10
68 /* Make sure the reported temperature is valid for this number of polls. */
69 #define TZ_VALIDCHECKS 3
71 /* Notify the user we will be shutting down in one more poll cycle. */
72 #define TZ_NOTIFYCOUNT (TZ_VALIDCHECKS - 1)
74 /* ACPI spec defines this */
75 #define TZ_NUMLEVELS 10
78 ACPI_BUFFER al[TZ_NUMLEVELS];
89 struct acpi_tz_softc {
91 ACPI_HANDLE tz_handle; /*Thermal zone handle*/
92 int tz_temperature; /*Current temperature*/
93 int tz_active; /*Current active cooling*/
94 #define TZ_ACTIVE_NONE -1
95 #define TZ_ACTIVE_UNKNOWN -2
96 int tz_requested; /*Minimum active cooling*/
97 int tz_thflags; /*Current temp-related flags*/
98 #define TZ_THFLAG_NONE 0
99 #define TZ_THFLAG_PSV (1<<0)
100 #define TZ_THFLAG_HOT (1<<2)
101 #define TZ_THFLAG_CRT (1<<3)
103 #define TZ_FLAG_NO_SCP (1<<0) /*No _SCP method*/
104 #define TZ_FLAG_GETPROFILE (1<<1) /*Get power_profile in timeout*/
105 #define TZ_FLAG_GETSETTINGS (1<<2) /*Get devs/setpoints*/
106 struct timespec tz_cooling_started;
107 /*Current cooling starting time*/
109 struct sysctl_ctx_list tz_sysctl_ctx;
110 struct sysctl_oid *tz_sysctl_tree;
111 eventhandler_tag tz_event;
113 struct acpi_tz_zone tz_zone; /*Thermal zone parameters*/
115 int tz_insane_tmp_notified;
117 /* passive cooling */
118 struct proc *tz_cooling_proc;
119 int tz_cooling_proc_running;
120 int tz_cooling_enabled;
121 int tz_cooling_active;
122 int tz_cooling_updated;
123 int tz_cooling_saved_freq;
126 #define TZ_ACTIVE_LEVEL(act) ((act) >= 0 ? (act) : TZ_NUMLEVELS)
128 #define CPUFREQ_MAX_LEVELS 64 /* XXX cpufreq should export this */
130 static int acpi_tz_probe(device_t dev);
131 static int acpi_tz_attach(device_t dev);
132 static int acpi_tz_establish(struct acpi_tz_softc *sc);
133 static void acpi_tz_monitor(void *Context);
134 static void acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
135 static void acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
136 static void acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
138 static void acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
139 static int acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
140 static int acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS);
141 static int acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS);
142 static int acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
143 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
145 static void acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
146 static void acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
147 static void acpi_tz_power_profile(void *arg);
148 static void acpi_tz_thread(void *arg);
149 static int acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
150 static int acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
152 static device_method_t acpi_tz_methods[] = {
153 /* Device interface */
154 DEVMETHOD(device_probe, acpi_tz_probe),
155 DEVMETHOD(device_attach, acpi_tz_attach),
160 static driver_t acpi_tz_driver = {
163 sizeof(struct acpi_tz_softc),
166 static char *acpi_tz_tmp_name = "_TMP";
168 static devclass_t acpi_tz_devclass;
169 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0);
170 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
172 static struct sysctl_ctx_list acpi_tz_sysctl_ctx;
173 static struct sysctl_oid *acpi_tz_sysctl_tree;
175 /* Minimum cooling run time */
176 static int acpi_tz_min_runtime;
177 static int acpi_tz_polling_rate = TZ_POLLRATE;
178 static int acpi_tz_override;
180 /* Timezone polling thread */
181 static struct proc *acpi_tz_proc;
182 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
184 static int acpi_tz_cooling_unit = -1;
187 acpi_tz_probe(device_t dev)
191 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
192 device_set_desc(dev, "Thermal Zone");
200 acpi_tz_attach(device_t dev)
202 struct acpi_tz_softc *sc;
203 struct acpi_softc *acpi_sc;
207 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
209 sc = device_get_softc(dev);
211 sc->tz_handle = acpi_get_handle(dev);
212 sc->tz_requested = TZ_ACTIVE_NONE;
213 sc->tz_active = TZ_ACTIVE_UNKNOWN;
214 sc->tz_thflags = TZ_THFLAG_NONE;
215 sc->tz_cooling_proc = NULL;
216 sc->tz_cooling_proc_running = FALSE;
217 sc->tz_cooling_active = FALSE;
218 sc->tz_cooling_updated = FALSE;
219 sc->tz_cooling_enabled = FALSE;
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 interval in seconds");
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_WITH_LABEL(&sc->tz_sysctl_ctx,
263 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
264 OID_AUTO, oidname, CTLFLAG_RD, 0, "", "thermal_zone");
265 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
266 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD,
267 &sc->tz_temperature, 0, sysctl_handle_int,
268 "IK", "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_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
295 OID_AUTO, "_ACx", CTLTYPE_INT | CTLFLAG_RD,
296 &sc->tz_zone.ac, sizeof(sc->tz_zone.ac),
297 sysctl_handle_opaque, "IK", "");
298 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
299 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
300 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
301 acpi_tz_passive_sysctl, "I",
302 "thermal constant 1 for passive cooling");
303 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
304 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
305 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
306 acpi_tz_passive_sysctl, "I",
307 "thermal constant 2 for passive cooling");
308 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
309 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
310 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
311 acpi_tz_passive_sysctl, "I",
312 "thermal sampling period for passive cooling");
315 * Register our power profile event handler.
317 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
318 acpi_tz_power_profile, sc, 0);
321 * Flag the event handler for a manual invocation by our timeout.
322 * We defer it like this so that the rest of the subsystem has time
323 * to come up. Don't bother evaluating/printing the temperature at
324 * this point; on many systems it'll be bogus until the EC is running.
326 sc->tz_flags |= TZ_FLAG_GETPROFILE;
332 acpi_tz_startup(void *arg __unused)
334 struct acpi_tz_softc *sc;
336 int devcount, error, i;
338 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
345 * Create thread to service all of the thermal zones.
347 error = kproc_create(acpi_tz_thread, NULL, &acpi_tz_proc, RFHIGHPID, 0,
350 printf("acpi_tz: could not create thread - %d", error);
353 * Create a thread to handle passive cooling for 1st zone which
354 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
355 * zones manually for now.
357 * XXX We enable only one zone to avoid multiple zones conflict
358 * with each other since cpufreq currently sets all CPUs to the
359 * given frequency whereas it's possible for different thermal
360 * zones to specify independent settings for multiple CPUs.
362 for (i = 0; i < devcount; i++) {
363 sc = device_get_softc(devs[i]);
364 if (acpi_tz_cooling_is_available(sc)) {
365 sc->tz_cooling_enabled = TRUE;
366 error = acpi_tz_cooling_thread_start(sc);
368 sc->tz_cooling_enabled = FALSE;
371 acpi_tz_cooling_unit = device_get_unit(devs[i]);
377 SYSINIT(acpi_tz, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, acpi_tz_startup, NULL);
380 * Parse the current state of this thermal zone and set up to use it.
382 * Note that we may have previous state, which will have to be discarded.
385 acpi_tz_establish(struct acpi_tz_softc *sc)
391 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
393 /* Erase any existing state. */
394 for (i = 0; i < TZ_NUMLEVELS; i++)
395 if (sc->tz_zone.al[i].Pointer != NULL)
396 AcpiOsFree(sc->tz_zone.al[i].Pointer);
397 if (sc->tz_zone.psl.Pointer != NULL)
398 AcpiOsFree(sc->tz_zone.psl.Pointer);
401 * XXX: We initialize only ACPI_BUFFER to avoid race condition
402 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
404 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
405 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
406 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
408 /* Evaluate thermal zone parameters. */
409 for (i = 0; i < TZ_NUMLEVELS; i++) {
410 sprintf(nbuf, "_AC%d", i);
411 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
412 sprintf(nbuf, "_AL%d", i);
413 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
414 sc->tz_zone.al[i].Pointer = NULL;
415 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
416 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
418 /* Should be a package containing a list of power objects */
419 if (obj->Type != ACPI_TYPE_PACKAGE) {
420 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
422 return_VALUE (ENXIO);
426 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
427 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
428 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
429 sc->tz_zone.psl.Pointer = NULL;
430 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
431 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
432 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
433 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
434 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
435 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
438 * Sanity-check the values we've been given.
440 * XXX what do we do about systems that give us the same value for
441 * more than one of these setpoints?
443 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
444 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
445 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
446 for (i = 0; i < TZ_NUMLEVELS; i++)
447 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
452 static char *aclevel_string[] = {
453 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
454 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
457 static __inline const char *
458 acpi_tz_aclevel_string(int active)
460 if (active < -1 || active >= TZ_NUMLEVELS)
461 return (aclevel_string[0]);
463 return (aclevel_string[active + 1]);
467 * Get the current temperature.
470 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
475 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
477 /* Evaluate the thermal zone's _TMP method. */
478 status = acpi_GetInteger(sc->tz_handle, acpi_tz_tmp_name, &temp);
479 if (ACPI_FAILURE(status)) {
480 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
481 "error fetching current temperature -- %s\n",
482 AcpiFormatException(status));
486 /* Check it for validity. */
487 acpi_tz_sanity(sc, &temp, acpi_tz_tmp_name);
491 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
492 sc->tz_temperature = temp;
497 * Evaluate the condition of a thermal zone, take appropriate actions.
500 acpi_tz_monitor(void *Context)
502 struct acpi_tz_softc *sc;
503 struct timespec curtime;
506 int newactive, newflags;
508 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
510 sc = (struct acpi_tz_softc *)Context;
512 /* Get the current temperature. */
513 if (!acpi_tz_get_temperature(sc)) {
514 /* XXX disable zone? go to max cooling? */
517 temp = sc->tz_temperature;
520 * Work out what we ought to be doing right now.
522 * Note that the _ACx levels sort from hot to cold.
524 newactive = TZ_ACTIVE_NONE;
525 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
526 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i])
531 * We are going to get _ACx level down (colder side), but give a guaranteed
532 * minimum cooling run time if requested.
534 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
535 sc->tz_active != TZ_ACTIVE_UNKNOWN &&
536 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
538 getnanotime(&curtime);
539 timespecsub(&curtime, &sc->tz_cooling_started, &curtime);
540 if (curtime.tv_sec < acpi_tz_min_runtime)
541 newactive = sc->tz_active;
544 /* Handle user override of active mode */
545 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
546 || sc->tz_requested < newactive))
547 newactive = sc->tz_requested;
549 /* update temperature-related flags */
550 newflags = TZ_THFLAG_NONE;
551 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
552 newflags |= TZ_THFLAG_PSV;
553 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
554 newflags |= TZ_THFLAG_HOT;
555 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
556 newflags |= TZ_THFLAG_CRT;
558 /* If the active cooling state has changed, we have to switch things. */
559 if (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
561 * We don't know which cooling device is on or off,
562 * so stop them all, because we now know which
563 * should be on (if any).
565 for (i = 0; i < TZ_NUMLEVELS; i++) {
566 if (sc->tz_zone.al[i].Pointer != NULL) {
567 acpi_ForeachPackageObject(
568 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
569 acpi_tz_switch_cooler_off, sc);
572 /* now we know that all devices are off */
573 sc->tz_active = TZ_ACTIVE_NONE;
576 if (newactive != sc->tz_active) {
577 /* Turn off unneeded cooling devices that are on, if any are */
578 for (i = TZ_ACTIVE_LEVEL(sc->tz_active);
579 i < TZ_ACTIVE_LEVEL(newactive); i++) {
580 acpi_ForeachPackageObject(
581 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
582 acpi_tz_switch_cooler_off, sc);
584 /* Turn on cooling devices that are required, if any are */
585 for (i = TZ_ACTIVE_LEVEL(sc->tz_active) - 1;
586 i >= TZ_ACTIVE_LEVEL(newactive); i--) {
587 acpi_ForeachPackageObject(
588 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
589 acpi_tz_switch_cooler_on, sc);
592 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
593 "switched from %s to %s: %d.%dC\n",
594 acpi_tz_aclevel_string(sc->tz_active),
595 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
596 sc->tz_active = newactive;
597 getnanotime(&sc->tz_cooling_started);
600 /* XXX (de)activate any passive cooling that may be required. */
603 * If the temperature is at _HOT or _CRT, increment our event count.
604 * If it has occurred enough times, shutdown the system. This is
605 * needed because some systems will report an invalid high temperature
606 * for one poll cycle. It is suspected this is due to the embedded
607 * controller timing out. A typical value is 138C for one cycle on
608 * a system that is otherwise 65C.
610 * If we're almost at that threshold, notify the user through devd(8).
612 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
613 sc->tz_validchecks++;
614 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
615 device_printf(sc->tz_dev,
616 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
617 TZ_KELVTOC(sc->tz_temperature));
618 shutdown_nice(RB_POWEROFF);
619 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
620 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
622 sc->tz_validchecks = 0;
624 sc->tz_thflags = newflags;
630 * Given an object, verify that it's a reference to a device of some sort,
631 * and try to switch it off.
634 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
638 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
640 cooler = acpi_GetReference(NULL, obj);
641 if (cooler == NULL) {
642 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
646 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
648 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
654 * Given an object, verify that it's a reference to a device of some sort,
655 * and try to switch it on.
657 * XXX replication of off/on function code is bad.
660 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
662 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
666 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
668 cooler = acpi_GetReference(NULL, obj);
669 if (cooler == NULL) {
670 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
674 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
676 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
677 if (ACPI_FAILURE(status)) {
678 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
679 "failed to activate %s - %s\n", acpi_name(cooler),
680 AcpiFormatException(status));
687 * Read/debug-print a parameter, default it to -1.
690 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
693 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
695 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
698 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
699 acpi_name(sc->tz_handle), node, *data));
706 * Sanity-check a temperature value. Assume that setpoints
707 * should be between 0C and 200C.
710 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
712 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
714 * If the value we are checking is _TMP, warn the user only
715 * once. This avoids spamming messages if, for instance, the
716 * sensor is broken and always returns an invalid temperature.
718 * This is only done for _TMP; other values always emit a
721 if (what != acpi_tz_tmp_name || !sc->tz_insane_tmp_notified) {
722 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
723 what, TZ_KELVTOC(*val));
725 /* Don't warn the user again if the read value doesn't improve. */
726 if (what == acpi_tz_tmp_name)
727 sc->tz_insane_tmp_notified = 1;
733 /* This value is correct. Warn if it's incorrect again. */
734 if (what == acpi_tz_tmp_name)
735 sc->tz_insane_tmp_notified = 0;
739 * Respond to a sysctl on the active state node.
742 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
744 struct acpi_tz_softc *sc;
748 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
749 active = sc->tz_active;
750 error = sysctl_handle_int(oidp, &active, 0, req);
752 /* Error or no new value */
753 if (error != 0 || req->newptr == NULL)
755 if (active < -1 || active >= TZ_NUMLEVELS)
758 /* Set new preferred level and re-switch */
759 sc->tz_requested = active;
760 acpi_tz_signal(sc, 0);
765 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
767 struct acpi_tz_softc *sc;
770 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
771 enabled = sc->tz_cooling_enabled;
772 error = sysctl_handle_int(oidp, &enabled, 0, req);
774 /* Error or no new value */
775 if (error != 0 || req->newptr == NULL)
777 if (enabled != TRUE && enabled != FALSE)
781 if (acpi_tz_cooling_is_available(sc))
782 error = acpi_tz_cooling_thread_start(sc);
788 sc->tz_cooling_enabled = enabled;
793 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
795 struct acpi_tz_softc *sc;
800 temp_ptr = (int *)(void *)(uintptr_t)((uintptr_t)sc + oidp->oid_arg2);
802 error = sysctl_handle_int(oidp, &temp, 0, req);
804 /* Error or no new value */
805 if (error != 0 || req->newptr == NULL)
808 /* Only allow changing settings if override is set. */
809 if (!acpi_tz_override)
812 /* Check user-supplied value for sanity. */
813 acpi_tz_sanity(sc, &temp, "user-supplied temp");
822 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
824 struct acpi_tz_softc *sc;
829 val_ptr = (int *)(void *)(uintptr_t)((uintptr_t)sc + oidp->oid_arg2);
831 error = sysctl_handle_int(oidp, &val, 0, req);
833 /* Error or no new value */
834 if (error != 0 || req->newptr == NULL)
837 /* Only allow changing settings if override is set. */
838 if (!acpi_tz_override)
846 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
848 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
850 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
853 case TZ_NOTIFY_TEMPERATURE:
854 /* Temperature change occurred */
855 acpi_tz_signal(sc, 0);
857 case TZ_NOTIFY_DEVICES:
858 case TZ_NOTIFY_LEVELS:
859 /* Zone devices/setpoints changed */
860 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
863 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
864 "unknown Notify event 0x%x\n", notify);
868 acpi_UserNotify("Thermal", h, notify);
874 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
877 sc->tz_flags |= flags;
878 ACPI_UNLOCK(thermal);
879 wakeup(&acpi_tz_proc);
883 * Notifies can be generated asynchronously but have also been seen to be
884 * triggered by other thermal methods. One system generates a notify of
885 * 0x81 when the fan is turned on or off. Another generates it when _SCP
886 * is called. To handle these situations, we check the zone via
887 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
891 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
894 /* Check the current temperature and take action based on it */
897 /* If requested, get the power profile settings. */
898 if (flags & TZ_FLAG_GETPROFILE)
899 acpi_tz_power_profile(sc);
902 * If requested, check for new devices/setpoints. After finding them,
903 * check if we need to switch fans based on the new values.
905 if (flags & TZ_FLAG_GETSETTINGS) {
906 acpi_tz_establish(sc);
910 /* XXX passive cooling actions? */
914 * System power profile may have changed; fetch and notify the
915 * thermal zone accordingly.
917 * Since this can be called from an arbitrary eventhandler, it needs
918 * to get the ACPI lock itself.
921 acpi_tz_power_profile(void *arg)
924 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
927 state = power_profile_get_state();
928 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
931 /* check that we haven't decided there's no _SCP method */
932 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
934 /* Call _SCP to set the new profile */
935 status = acpi_SetInteger(sc->tz_handle, "_SCP",
936 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
937 if (ACPI_FAILURE(status)) {
938 if (status != AE_NOT_FOUND)
939 ACPI_VPRINT(sc->tz_dev,
940 acpi_device_get_parent_softc(sc->tz_dev),
941 "can't evaluate %s._SCP - %s\n",
942 acpi_name(sc->tz_handle),
943 AcpiFormatException(status));
944 sc->tz_flags |= TZ_FLAG_NO_SCP;
946 /* We have to re-evaluate the entire zone now */
947 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
953 * Thermal zone monitor thread.
956 acpi_tz_thread(void *arg)
961 struct acpi_tz_softc **sc;
963 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
970 /* If the number of devices has changed, re-evaluate. */
971 if (devclass_get_count(acpi_tz_devclass) != devcount) {
976 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
977 sc = malloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
979 for (i = 0; i < devcount; i++)
980 sc[i] = device_get_softc(devs[i]);
983 /* Check for temperature events and act on them. */
984 for (i = 0; i < devcount; i++) {
986 flags = sc[i]->tz_flags;
987 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
988 ACPI_UNLOCK(thermal);
989 acpi_tz_timeout(sc[i], flags);
992 /* If more work to do, don't go to sleep yet. */
994 for (i = 0; i < devcount; i++) {
995 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
1000 * If we have no more work, sleep for a while, setting PDROP so that
1001 * the mutex will not be reacquired. Otherwise, drop the mutex and
1002 * loop to handle more events.
1005 msleep(&acpi_tz_proc, &thermal_mutex, PZERO | PDROP, "tzpoll",
1006 hz * acpi_tz_polling_rate);
1008 ACPI_UNLOCK(thermal);
1013 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
1018 if (!sc->tz_cooling_updated)
1020 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
1022 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1023 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
1024 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
1025 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
1027 sc->tz_cooling_updated = FALSE;
1032 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1035 struct cf_level *levels;
1036 int num_levels, error, freq, desired_freq, perf, i;
1038 levels = malloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1043 * Find the main device, cpufreq0. We don't yet support independent
1044 * CPU frequency control on SMP.
1046 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1051 /* Get the current frequency. */
1052 error = CPUFREQ_GET(dev, &levels[0]);
1055 freq = levels[0].total_set.freq;
1057 /* Get the current available frequency levels. */
1058 num_levels = CPUFREQ_MAX_LEVELS;
1059 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1062 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1066 /* Calculate the desired frequency as a percent of the max frequency. */
1067 perf = 100 * freq / levels[0].total_set.freq - req;
1070 else if (perf > 100)
1072 desired_freq = levels[0].total_set.freq * perf / 100;
1074 if (desired_freq < freq) {
1075 /* Find the closest available frequency, rounding down. */
1076 for (i = 0; i < num_levels; i++)
1077 if (levels[i].total_set.freq <= desired_freq)
1080 /* If we didn't find a relevant setting, use the lowest. */
1081 if (i == num_levels)
1084 /* If we didn't decrease frequency yet, don't increase it. */
1085 if (!sc->tz_cooling_updated) {
1086 sc->tz_cooling_active = FALSE;
1090 /* Use saved cpu frequency as maximum value. */
1091 if (desired_freq > sc->tz_cooling_saved_freq)
1092 desired_freq = sc->tz_cooling_saved_freq;
1094 /* Find the closest available frequency, rounding up. */
1095 for (i = num_levels - 1; i >= 0; i--)
1096 if (levels[i].total_set.freq >= desired_freq)
1099 /* If we didn't find a relevant setting, use the highest. */
1103 /* If we're going to the highest frequency, restore the old setting. */
1104 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1105 error = acpi_tz_cpufreq_restore(sc);
1107 sc->tz_cooling_active = FALSE;
1112 /* If we are going to a new frequency, activate it. */
1113 if (levels[i].total_set.freq != freq) {
1114 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1115 "temperature %d.%dC: %screasing clock speed "
1116 "from %d MHz to %d MHz\n",
1117 TZ_KELVTOC(sc->tz_temperature),
1118 (freq > levels[i].total_set.freq) ? "de" : "in",
1119 freq, levels[i].total_set.freq);
1120 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1121 if (error == 0 && !sc->tz_cooling_updated) {
1122 sc->tz_cooling_saved_freq = freq;
1123 sc->tz_cooling_updated = TRUE;
1129 free(levels, M_TEMP);
1134 * Passive cooling thread; monitors current temperature according to the
1135 * cooling interval and calculates whether to scale back CPU frequency.
1138 acpi_tz_cooling_thread(void *arg)
1140 struct acpi_tz_softc *sc;
1141 int error, perf, curr_temp, prev_temp;
1143 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1145 sc = (struct acpi_tz_softc *)arg;
1147 prev_temp = sc->tz_temperature;
1148 while (sc->tz_cooling_enabled) {
1149 if (sc->tz_cooling_active)
1150 (void)acpi_tz_get_temperature(sc);
1151 curr_temp = sc->tz_temperature;
1152 if (curr_temp >= sc->tz_zone.psv)
1153 sc->tz_cooling_active = TRUE;
1154 if (sc->tz_cooling_active) {
1155 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1156 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1160 error = acpi_tz_cpufreq_update(sc, perf);
1163 * If error and not simply a higher priority setting was
1164 * active, disable cooling.
1166 if (error != 0 && error != EPERM) {
1167 device_printf(sc->tz_dev,
1168 "failed to set new freq, disabling passive cooling\n");
1169 sc->tz_cooling_enabled = FALSE;
1173 prev_temp = curr_temp;
1174 tsleep(&sc->tz_cooling_proc, PZERO, "cooling",
1175 hz * sc->tz_zone.tsp / 10);
1177 if (sc->tz_cooling_active) {
1178 acpi_tz_cpufreq_restore(sc);
1179 sc->tz_cooling_active = FALSE;
1181 sc->tz_cooling_proc = NULL;
1183 sc->tz_cooling_proc_running = FALSE;
1184 ACPI_UNLOCK(thermal);
1189 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1190 * all CPUs for us. However, it's possible in the future _PSL will
1191 * reference non-CPU devices so we may want to support it then.
1194 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1196 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1197 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1198 sc->tz_zone.psv != -1);
1202 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1207 if (sc->tz_cooling_proc_running) {
1208 ACPI_UNLOCK(thermal);
1211 sc->tz_cooling_proc_running = TRUE;
1212 ACPI_UNLOCK(thermal);
1214 if (sc->tz_cooling_proc == NULL) {
1215 error = kproc_create(acpi_tz_cooling_thread, sc,
1216 &sc->tz_cooling_proc, RFHIGHPID, 0, "acpi_cooling%d",
1217 device_get_unit(sc->tz_dev));
1219 device_printf(sc->tz_dev, "could not create thread - %d", error);
1221 sc->tz_cooling_proc_running = FALSE;
1222 ACPI_UNLOCK(thermal);