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>
40 #include <sys/reboot.h>
41 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/power.h>
45 #include "cpufreq_if.h"
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
50 #include <dev/acpica/acpivar.h>
52 /* Hooks for the ACPI CA debugging infrastructure */
53 #define _COMPONENT ACPI_THERMAL
54 ACPI_MODULE_NAME("THERMAL")
57 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
59 #define TZ_NOTIFY_TEMPERATURE 0x80 /* Temperature changed. */
60 #define TZ_NOTIFY_LEVELS 0x81 /* Cooling levels changed. */
61 #define TZ_NOTIFY_DEVICES 0x82 /* Device lists changed. */
62 #define TZ_NOTIFY_CRITICAL 0xcc /* Fake notify that _CRT/_HOT reached. */
64 /* Check for temperature changes every 10 seconds by default */
65 #define TZ_POLLRATE 10
67 /* Make sure the reported temperature is valid for this number of polls. */
68 #define TZ_VALIDCHECKS 3
70 /* Notify the user we will be shutting down in one more poll cycle. */
71 #define TZ_NOTIFYCOUNT (TZ_VALIDCHECKS - 1)
73 /* ACPI spec defines this */
74 #define TZ_NUMLEVELS 10
77 ACPI_BUFFER al[TZ_NUMLEVELS];
88 struct acpi_tz_softc {
90 ACPI_HANDLE tz_handle; /*Thermal zone handle*/
91 int tz_temperature; /*Current temperature*/
92 int tz_active; /*Current active cooling*/
93 #define TZ_ACTIVE_NONE -1
94 #define TZ_ACTIVE_UNKNOWN -2
95 int tz_requested; /*Minimum active cooling*/
96 int tz_thflags; /*Current temp-related flags*/
97 #define TZ_THFLAG_NONE 0
98 #define TZ_THFLAG_PSV (1<<0)
99 #define TZ_THFLAG_HOT (1<<2)
100 #define TZ_THFLAG_CRT (1<<3)
102 #define TZ_FLAG_NO_SCP (1<<0) /*No _SCP method*/
103 #define TZ_FLAG_GETPROFILE (1<<1) /*Get power_profile in timeout*/
104 #define TZ_FLAG_GETSETTINGS (1<<2) /*Get devs/setpoints*/
105 struct timespec tz_cooling_started;
106 /*Current cooling starting time*/
108 struct sysctl_ctx_list tz_sysctl_ctx;
109 struct sysctl_oid *tz_sysctl_tree;
110 eventhandler_tag tz_event;
112 struct acpi_tz_zone tz_zone; /*Thermal zone parameters*/
115 /* passive cooling */
116 struct proc *tz_cooling_proc;
117 int tz_cooling_proc_running;
118 int tz_cooling_enabled;
119 int tz_cooling_active;
120 int tz_cooling_updated;
121 int tz_cooling_saved_freq;
124 #define TZ_ACTIVE_LEVEL(act) ((act) >= 0 ? (act) : TZ_NUMLEVELS)
126 #define CPUFREQ_MAX_LEVELS 64 /* XXX cpufreq should export this */
128 static int acpi_tz_probe(device_t dev);
129 static int acpi_tz_attach(device_t dev);
130 static int acpi_tz_establish(struct acpi_tz_softc *sc);
131 static void acpi_tz_monitor(void *Context);
132 static void acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
133 static void acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
134 static void acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
136 static void acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
137 static int acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
138 static int acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS);
139 static int acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS);
140 static int acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
141 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
143 static void acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
144 static void acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
145 static void acpi_tz_power_profile(void *arg);
146 static void acpi_tz_thread(void *arg);
147 static int acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
148 static int acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
150 static device_method_t acpi_tz_methods[] = {
151 /* Device interface */
152 DEVMETHOD(device_probe, acpi_tz_probe),
153 DEVMETHOD(device_attach, acpi_tz_attach),
158 static driver_t acpi_tz_driver = {
161 sizeof(struct acpi_tz_softc),
164 static devclass_t acpi_tz_devclass;
165 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0);
166 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
168 static struct sysctl_ctx_list acpi_tz_sysctl_ctx;
169 static struct sysctl_oid *acpi_tz_sysctl_tree;
171 /* Minimum cooling run time */
172 static int acpi_tz_min_runtime;
173 static int acpi_tz_polling_rate = TZ_POLLRATE;
174 static int acpi_tz_override;
176 /* Timezone polling thread */
177 static struct proc *acpi_tz_proc;
178 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
180 static int acpi_tz_cooling_unit = -1;
183 acpi_tz_probe(device_t dev)
187 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
188 device_set_desc(dev, "Thermal Zone");
196 acpi_tz_attach(device_t dev)
198 struct acpi_tz_softc *sc;
199 struct acpi_softc *acpi_sc;
203 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
205 sc = device_get_softc(dev);
207 sc->tz_handle = acpi_get_handle(dev);
208 sc->tz_requested = TZ_ACTIVE_NONE;
209 sc->tz_active = TZ_ACTIVE_UNKNOWN;
210 sc->tz_thflags = TZ_THFLAG_NONE;
211 sc->tz_cooling_proc = NULL;
212 sc->tz_cooling_proc_running = FALSE;
213 sc->tz_cooling_active = FALSE;
214 sc->tz_cooling_updated = FALSE;
215 sc->tz_cooling_enabled = FALSE;
218 * Parse the current state of the thermal zone and build control
219 * structures. We don't need to worry about interference with the
220 * control thread since we haven't fully attached this device yet.
222 if ((error = acpi_tz_establish(sc)) != 0)
226 * Register for any Notify events sent to this zone.
228 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
229 acpi_tz_notify_handler, sc);
232 * Create our sysctl nodes.
234 * XXX we need a mechanism for adding nodes under ACPI.
236 if (device_get_unit(dev) == 0) {
237 acpi_sc = acpi_device_get_parent_softc(dev);
238 sysctl_ctx_init(&acpi_tz_sysctl_ctx);
239 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
240 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
241 OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
242 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
243 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
244 OID_AUTO, "min_runtime", CTLFLAG_RW,
245 &acpi_tz_min_runtime, 0,
246 "minimum cooling run time in sec");
247 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
248 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
249 OID_AUTO, "polling_rate", CTLFLAG_RW,
250 &acpi_tz_polling_rate, 0, "monitor polling interval in seconds");
251 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
252 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
253 "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
254 "allow override of thermal settings");
256 sysctl_ctx_init(&sc->tz_sysctl_ctx);
257 sprintf(oidname, "tz%d", device_get_unit(dev));
258 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
259 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
260 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
261 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
262 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD,
263 &sc->tz_temperature, 0, sysctl_handle_int,
264 "IK", "current thermal zone temperature");
265 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
266 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
267 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
268 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
269 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
270 sc, 0, acpi_tz_cooling_sysctl, "I",
271 "enable passive (speed reduction) cooling");
273 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
274 OID_AUTO, "thermal_flags", CTLFLAG_RD,
275 &sc->tz_thflags, 0, "thermal zone flags");
276 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
277 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
278 sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
279 acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
280 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
281 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
282 sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
283 acpi_tz_temp_sysctl, "IK",
284 "too hot temp setpoint (suspend now)");
285 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
286 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
287 sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
288 acpi_tz_temp_sysctl, "IK",
289 "critical temp setpoint (shutdown now)");
290 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
291 OID_AUTO, "_ACx", CTLTYPE_INT | CTLFLAG_RD,
292 &sc->tz_zone.ac, sizeof(sc->tz_zone.ac),
293 sysctl_handle_opaque, "IK", "");
294 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
295 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
296 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
297 acpi_tz_passive_sysctl, "I",
298 "thermal constant 1 for passive cooling");
299 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
300 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
301 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
302 acpi_tz_passive_sysctl, "I",
303 "thermal constant 2 for passive cooling");
304 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
305 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
306 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
307 acpi_tz_passive_sysctl, "I",
308 "thermal sampling period for passive cooling");
311 * Create thread to service all of the thermal zones. Register
312 * our power profile event handler.
314 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
315 acpi_tz_power_profile, sc, 0);
316 if (acpi_tz_proc == NULL) {
317 error = kproc_create(acpi_tz_thread, NULL, &acpi_tz_proc,
318 RFHIGHPID, 0, "acpi_thermal");
320 device_printf(sc->tz_dev, "could not create thread - %d", error);
326 * Create a thread to handle passive cooling for 1st zone which
327 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
328 * zones manually for now.
330 * XXX We enable only one zone to avoid multiple zones conflict
331 * with each other since cpufreq currently sets all CPUs to the
332 * given frequency whereas it's possible for different thermal
333 * zones to specify independent settings for multiple CPUs.
335 if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
336 sc->tz_cooling_enabled = TRUE;
337 if (sc->tz_cooling_enabled) {
338 error = acpi_tz_cooling_thread_start(sc);
340 sc->tz_cooling_enabled = FALSE;
343 acpi_tz_cooling_unit = device_get_unit(dev);
347 * Flag the event handler for a manual invocation by our timeout.
348 * We defer it like this so that the rest of the subsystem has time
349 * to come up. Don't bother evaluating/printing the temperature at
350 * this point; on many systems it'll be bogus until the EC is running.
352 sc->tz_flags |= TZ_FLAG_GETPROFILE;
356 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
357 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
358 acpi_tz_notify_handler);
359 sysctl_ctx_free(&sc->tz_sysctl_ctx);
361 return_VALUE (error);
365 * Parse the current state of this thermal zone and set up to use it.
367 * Note that we may have previous state, which will have to be discarded.
370 acpi_tz_establish(struct acpi_tz_softc *sc)
376 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
378 /* Erase any existing state. */
379 for (i = 0; i < TZ_NUMLEVELS; i++)
380 if (sc->tz_zone.al[i].Pointer != NULL)
381 AcpiOsFree(sc->tz_zone.al[i].Pointer);
382 if (sc->tz_zone.psl.Pointer != NULL)
383 AcpiOsFree(sc->tz_zone.psl.Pointer);
386 * XXX: We initialize only ACPI_BUFFER to avoid race condition
387 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
389 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
390 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
391 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
393 /* Evaluate thermal zone parameters. */
394 for (i = 0; i < TZ_NUMLEVELS; i++) {
395 sprintf(nbuf, "_AC%d", i);
396 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
397 sprintf(nbuf, "_AL%d", i);
398 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
399 sc->tz_zone.al[i].Pointer = NULL;
400 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
401 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
403 /* Should be a package containing a list of power objects */
404 if (obj->Type != ACPI_TYPE_PACKAGE) {
405 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
407 return_VALUE (ENXIO);
411 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
412 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
413 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
414 sc->tz_zone.psl.Pointer = NULL;
415 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
416 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
417 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
418 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
419 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
420 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
423 * Sanity-check the values we've been given.
425 * XXX what do we do about systems that give us the same value for
426 * more than one of these setpoints?
428 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
429 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
430 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
431 for (i = 0; i < TZ_NUMLEVELS; i++)
432 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
437 static char *aclevel_string[] = {
438 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
439 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
442 static __inline const char *
443 acpi_tz_aclevel_string(int active)
445 if (active < -1 || active >= TZ_NUMLEVELS)
446 return (aclevel_string[0]);
448 return (aclevel_string[active + 1]);
452 * Get the current temperature.
455 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
459 static char *tmp_name = "_TMP";
461 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
463 /* Evaluate the thermal zone's _TMP method. */
464 status = acpi_GetInteger(sc->tz_handle, tmp_name, &temp);
465 if (ACPI_FAILURE(status)) {
466 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
467 "error fetching current temperature -- %s\n",
468 AcpiFormatException(status));
472 /* Check it for validity. */
473 acpi_tz_sanity(sc, &temp, tmp_name);
477 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
478 sc->tz_temperature = temp;
483 * Evaluate the condition of a thermal zone, take appropriate actions.
486 acpi_tz_monitor(void *Context)
488 struct acpi_tz_softc *sc;
489 struct timespec curtime;
492 int newactive, newflags;
494 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
496 sc = (struct acpi_tz_softc *)Context;
498 /* Get the current temperature. */
499 if (!acpi_tz_get_temperature(sc)) {
500 /* XXX disable zone? go to max cooling? */
503 temp = sc->tz_temperature;
506 * Work out what we ought to be doing right now.
508 * Note that the _ACx levels sort from hot to cold.
510 newactive = TZ_ACTIVE_NONE;
511 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
512 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i])
517 * We are going to get _ACx level down (colder side), but give a guaranteed
518 * minimum cooling run time if requested.
520 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
521 sc->tz_active != TZ_ACTIVE_UNKNOWN &&
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 (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
547 * We don't know which cooling device is on or off,
548 * so stop them all, because we now know which
549 * should be on (if any).
551 for (i = 0; i < TZ_NUMLEVELS; i++) {
552 if (sc->tz_zone.al[i].Pointer != NULL) {
553 acpi_ForeachPackageObject(
554 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
555 acpi_tz_switch_cooler_off, sc);
558 /* now we know that all devices are off */
559 sc->tz_active = TZ_ACTIVE_NONE;
562 if (newactive != sc->tz_active) {
563 /* Turn off unneeded cooling devices that are on, if any are */
564 for (i = TZ_ACTIVE_LEVEL(sc->tz_active);
565 i < TZ_ACTIVE_LEVEL(newactive); i++) {
566 acpi_ForeachPackageObject(
567 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
568 acpi_tz_switch_cooler_off, sc);
570 /* Turn on cooling devices that are required, if any are */
571 for (i = TZ_ACTIVE_LEVEL(sc->tz_active) - 1;
572 i >= TZ_ACTIVE_LEVEL(newactive); i--) {
573 acpi_ForeachPackageObject(
574 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
575 acpi_tz_switch_cooler_on, sc);
578 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
579 "switched from %s to %s: %d.%dC\n",
580 acpi_tz_aclevel_string(sc->tz_active),
581 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
582 sc->tz_active = newactive;
583 getnanotime(&sc->tz_cooling_started);
586 /* XXX (de)activate any passive cooling that may be required. */
589 * If the temperature is at _HOT or _CRT, increment our event count.
590 * If it has occurred enough times, shutdown the system. This is
591 * needed because some systems will report an invalid high temperature
592 * for one poll cycle. It is suspected this is due to the embedded
593 * controller timing out. A typical value is 138C for one cycle on
594 * a system that is otherwise 65C.
596 * If we're almost at that threshold, notify the user through devd(8).
598 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
599 sc->tz_validchecks++;
600 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
601 device_printf(sc->tz_dev,
602 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
603 TZ_KELVTOC(sc->tz_temperature));
604 shutdown_nice(RB_POWEROFF);
605 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
606 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
608 sc->tz_validchecks = 0;
610 sc->tz_thflags = newflags;
616 * Given an object, verify that it's a reference to a device of some sort,
617 * and try to switch it off.
620 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
624 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
626 cooler = acpi_GetReference(NULL, obj);
627 if (cooler == NULL) {
628 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
632 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
634 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
640 * Given an object, verify that it's a reference to a device of some sort,
641 * and try to switch it on.
643 * XXX replication of off/on function code is bad.
646 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
648 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
652 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
654 cooler = acpi_GetReference(NULL, obj);
655 if (cooler == NULL) {
656 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
660 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
662 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
663 if (ACPI_FAILURE(status)) {
664 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
665 "failed to activate %s - %s\n", acpi_name(cooler),
666 AcpiFormatException(status));
673 * Read/debug-print a parameter, default it to -1.
676 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
679 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
681 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
684 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
685 acpi_name(sc->tz_handle), node, *data));
692 * Sanity-check a temperature value. Assume that setpoints
693 * should be between 0C and 200C.
696 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
698 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
699 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
700 what, TZ_KELVTOC(*val));
706 * Respond to a sysctl on the active state node.
709 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
711 struct acpi_tz_softc *sc;
715 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
716 active = sc->tz_active;
717 error = sysctl_handle_int(oidp, &active, 0, req);
719 /* Error or no new value */
720 if (error != 0 || req->newptr == NULL)
722 if (active < -1 || active >= TZ_NUMLEVELS)
725 /* Set new preferred level and re-switch */
726 sc->tz_requested = active;
727 acpi_tz_signal(sc, 0);
732 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
734 struct acpi_tz_softc *sc;
737 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
738 enabled = sc->tz_cooling_enabled;
739 error = sysctl_handle_int(oidp, &enabled, 0, req);
741 /* Error or no new value */
742 if (error != 0 || req->newptr == NULL)
744 if (enabled != TRUE && enabled != FALSE)
748 if (acpi_tz_cooling_is_available(sc))
749 error = acpi_tz_cooling_thread_start(sc);
755 sc->tz_cooling_enabled = enabled;
760 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
762 struct acpi_tz_softc *sc;
767 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
769 error = sysctl_handle_int(oidp, &temp, 0, req);
771 /* Error or no new value */
772 if (error != 0 || req->newptr == NULL)
775 /* Only allow changing settings if override is set. */
776 if (!acpi_tz_override)
779 /* Check user-supplied value for sanity. */
780 acpi_tz_sanity(sc, &temp, "user-supplied temp");
789 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
791 struct acpi_tz_softc *sc;
796 val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
798 error = sysctl_handle_int(oidp, &val, 0, req);
800 /* Error or no new value */
801 if (error != 0 || req->newptr == NULL)
804 /* Only allow changing settings if override is set. */
805 if (!acpi_tz_override)
813 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
815 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
817 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
820 case TZ_NOTIFY_TEMPERATURE:
821 /* Temperature change occurred */
822 acpi_tz_signal(sc, 0);
824 case TZ_NOTIFY_DEVICES:
825 case TZ_NOTIFY_LEVELS:
826 /* Zone devices/setpoints changed */
827 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
830 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
831 "unknown Notify event 0x%x\n", notify);
835 acpi_UserNotify("Thermal", h, notify);
841 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
844 sc->tz_flags |= flags;
845 ACPI_UNLOCK(thermal);
846 wakeup(&acpi_tz_proc);
850 * Notifies can be generated asynchronously but have also been seen to be
851 * triggered by other thermal methods. One system generates a notify of
852 * 0x81 when the fan is turned on or off. Another generates it when _SCP
853 * is called. To handle these situations, we check the zone via
854 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
858 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
861 /* Check the current temperature and take action based on it */
864 /* If requested, get the power profile settings. */
865 if (flags & TZ_FLAG_GETPROFILE)
866 acpi_tz_power_profile(sc);
869 * If requested, check for new devices/setpoints. After finding them,
870 * check if we need to switch fans based on the new values.
872 if (flags & TZ_FLAG_GETSETTINGS) {
873 acpi_tz_establish(sc);
877 /* XXX passive cooling actions? */
881 * System power profile may have changed; fetch and notify the
882 * thermal zone accordingly.
884 * Since this can be called from an arbitrary eventhandler, it needs
885 * to get the ACPI lock itself.
888 acpi_tz_power_profile(void *arg)
891 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
894 state = power_profile_get_state();
895 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
898 /* check that we haven't decided there's no _SCP method */
899 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
901 /* Call _SCP to set the new profile */
902 status = acpi_SetInteger(sc->tz_handle, "_SCP",
903 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
904 if (ACPI_FAILURE(status)) {
905 if (status != AE_NOT_FOUND)
906 ACPI_VPRINT(sc->tz_dev,
907 acpi_device_get_parent_softc(sc->tz_dev),
908 "can't evaluate %s._SCP - %s\n",
909 acpi_name(sc->tz_handle),
910 AcpiFormatException(status));
911 sc->tz_flags |= TZ_FLAG_NO_SCP;
913 /* We have to re-evaluate the entire zone now */
914 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
920 * Thermal zone monitor thread.
923 acpi_tz_thread(void *arg)
928 struct acpi_tz_softc **sc;
930 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
937 /* If the number of devices has changed, re-evaluate. */
938 if (devclass_get_count(acpi_tz_devclass) != devcount) {
943 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
944 sc = malloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
946 for (i = 0; i < devcount; i++)
947 sc[i] = device_get_softc(devs[i]);
950 /* Check for temperature events and act on them. */
951 for (i = 0; i < devcount; i++) {
953 flags = sc[i]->tz_flags;
954 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
955 ACPI_UNLOCK(thermal);
956 acpi_tz_timeout(sc[i], flags);
959 /* If more work to do, don't go to sleep yet. */
961 for (i = 0; i < devcount; i++) {
962 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
967 * If we have no more work, sleep for a while, setting PDROP so that
968 * the mutex will not be reacquired. Otherwise, drop the mutex and
969 * loop to handle more events.
972 msleep(&acpi_tz_proc, &thermal_mutex, PZERO | PDROP, "tzpoll",
973 hz * acpi_tz_polling_rate);
975 ACPI_UNLOCK(thermal);
980 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
985 if (!sc->tz_cooling_updated)
987 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
989 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
990 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
991 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
992 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
994 sc->tz_cooling_updated = FALSE;
999 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1002 struct cf_level *levels;
1003 int num_levels, error, freq, desired_freq, perf, i;
1005 levels = malloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1010 * Find the main device, cpufreq0. We don't yet support independent
1011 * CPU frequency control on SMP.
1013 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1018 /* Get the current frequency. */
1019 error = CPUFREQ_GET(dev, &levels[0]);
1022 freq = levels[0].total_set.freq;
1024 /* Get the current available frequency levels. */
1025 num_levels = CPUFREQ_MAX_LEVELS;
1026 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1029 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1033 /* Calculate the desired frequency as a percent of the max frequency. */
1034 perf = 100 * freq / levels[0].total_set.freq - req;
1037 else if (perf > 100)
1039 desired_freq = levels[0].total_set.freq * perf / 100;
1041 if (desired_freq < freq) {
1042 /* Find the closest available frequency, rounding down. */
1043 for (i = 0; i < num_levels; i++)
1044 if (levels[i].total_set.freq <= desired_freq)
1047 /* If we didn't find a relevant setting, use the lowest. */
1048 if (i == num_levels)
1051 /* If we didn't decrease frequency yet, don't increase it. */
1052 if (!sc->tz_cooling_updated) {
1053 sc->tz_cooling_active = FALSE;
1057 /* Use saved cpu frequency as maximum value. */
1058 if (desired_freq > sc->tz_cooling_saved_freq)
1059 desired_freq = sc->tz_cooling_saved_freq;
1061 /* Find the closest available frequency, rounding up. */
1062 for (i = num_levels - 1; i >= 0; i--)
1063 if (levels[i].total_set.freq >= desired_freq)
1066 /* If we didn't find a relevant setting, use the highest. */
1070 /* If we're going to the highest frequency, restore the old setting. */
1071 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1072 error = acpi_tz_cpufreq_restore(sc);
1074 sc->tz_cooling_active = FALSE;
1079 /* If we are going to a new frequency, activate it. */
1080 if (levels[i].total_set.freq != freq) {
1081 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1082 "temperature %d.%dC: %screasing clock speed "
1083 "from %d MHz to %d MHz\n",
1084 TZ_KELVTOC(sc->tz_temperature),
1085 (freq > levels[i].total_set.freq) ? "de" : "in",
1086 freq, levels[i].total_set.freq);
1087 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1088 if (error == 0 && !sc->tz_cooling_updated) {
1089 sc->tz_cooling_saved_freq = freq;
1090 sc->tz_cooling_updated = TRUE;
1096 free(levels, M_TEMP);
1101 * Passive cooling thread; monitors current temperature according to the
1102 * cooling interval and calculates whether to scale back CPU frequency.
1105 acpi_tz_cooling_thread(void *arg)
1107 struct acpi_tz_softc *sc;
1108 int error, perf, curr_temp, prev_temp;
1110 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1112 sc = (struct acpi_tz_softc *)arg;
1114 prev_temp = sc->tz_temperature;
1115 while (sc->tz_cooling_enabled) {
1116 if (sc->tz_cooling_active)
1117 (void)acpi_tz_get_temperature(sc);
1118 curr_temp = sc->tz_temperature;
1119 if (curr_temp >= sc->tz_zone.psv)
1120 sc->tz_cooling_active = TRUE;
1121 if (sc->tz_cooling_active) {
1122 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1123 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1127 error = acpi_tz_cpufreq_update(sc, perf);
1130 * If error and not simply a higher priority setting was
1131 * active, disable cooling.
1133 if (error != 0 && error != EPERM) {
1134 device_printf(sc->tz_dev,
1135 "failed to set new freq, disabling passive cooling\n");
1136 sc->tz_cooling_enabled = FALSE;
1140 prev_temp = curr_temp;
1141 tsleep(&sc->tz_cooling_proc, PZERO, "cooling",
1142 hz * sc->tz_zone.tsp / 10);
1144 if (sc->tz_cooling_active) {
1145 acpi_tz_cpufreq_restore(sc);
1146 sc->tz_cooling_active = FALSE;
1148 sc->tz_cooling_proc = NULL;
1150 sc->tz_cooling_proc_running = FALSE;
1151 ACPI_UNLOCK(thermal);
1156 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1157 * all CPUs for us. However, it's possible in the future _PSL will
1158 * reference non-CPU devices so we may want to support it then.
1161 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1163 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1164 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1165 sc->tz_zone.psv != -1);
1169 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1174 if (sc->tz_cooling_proc_running) {
1175 ACPI_UNLOCK(thermal);
1178 sc->tz_cooling_proc_running = TRUE;
1179 ACPI_UNLOCK(thermal);
1181 if (sc->tz_cooling_proc == NULL) {
1182 error = kproc_create(acpi_tz_cooling_thread, sc,
1183 &sc->tz_cooling_proc, RFHIGHPID, 0, "acpi_cooling%d",
1184 device_get_unit(sc->tz_dev));
1186 device_printf(sc->tz_dev, "could not create thread - %d", error);
1188 sc->tz_cooling_proc_running = FALSE;
1189 ACPI_UNLOCK(thermal);