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), OID_AUTO, "thermal",
245 CTLFLAG_RD | CTLFLAG_MPSAFE, 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), OID_AUTO, oidname,
264 CTLFLAG_RD | CTLFLAG_MPSAFE, 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 | CTLFLAG_NEEDGIANT,
267 &sc->tz_temperature, 0, sysctl_handle_int, "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 | CTLFLAG_NEEDGIANT, sc,
271 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",
274 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
275 acpi_tz_cooling_sysctl, "I",
276 "enable passive (speed reduction) cooling");
278 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
279 OID_AUTO, "thermal_flags", CTLFLAG_RD,
280 &sc->tz_thflags, 0, "thermal zone flags");
281 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
282 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc,
283 offsetof(struct acpi_tz_softc, tz_zone.psv), acpi_tz_temp_sysctl, "IK",
284 "passive cooling temp setpoint");
285 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
286 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc,
287 offsetof(struct acpi_tz_softc, tz_zone.hot), 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 | CTLFLAG_NEEDGIANT, sc,
291 offsetof(struct acpi_tz_softc, tz_zone.crt), acpi_tz_temp_sysctl, "IK",
292 "critical temp setpoint (shutdown now)");
293 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
294 OID_AUTO, "_ACx", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
295 &sc->tz_zone.ac, sizeof(sc->tz_zone.ac), sysctl_handle_opaque, "IK",
297 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
298 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc,
299 offsetof(struct acpi_tz_softc, tz_zone.tc1), acpi_tz_passive_sysctl,
300 "I", "thermal constant 1 for passive cooling");
301 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
302 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc,
303 offsetof(struct acpi_tz_softc, tz_zone.tc2), acpi_tz_passive_sysctl,
304 "I", "thermal constant 2 for passive cooling");
305 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
306 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc,
307 offsetof(struct acpi_tz_softc, tz_zone.tsp), acpi_tz_passive_sysctl,
308 "I", "thermal sampling period for passive cooling");
311 * Register our power profile event handler.
313 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
314 acpi_tz_power_profile, sc, 0);
317 * Flag the event handler for a manual invocation by our timeout.
318 * We defer it like this so that the rest of the subsystem has time
319 * to come up. Don't bother evaluating/printing the temperature at
320 * this point; on many systems it'll be bogus until the EC is running.
322 sc->tz_flags |= TZ_FLAG_GETPROFILE;
328 acpi_tz_startup(void *arg __unused)
330 struct acpi_tz_softc *sc;
332 int devcount, error, i;
334 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
341 * Create thread to service all of the thermal zones.
343 error = kproc_create(acpi_tz_thread, NULL, &acpi_tz_proc, RFHIGHPID, 0,
346 printf("acpi_tz: could not create thread - %d", error);
349 * Create a thread to handle passive cooling for 1st zone which
350 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
351 * zones manually for now.
353 * XXX We enable only one zone to avoid multiple zones conflict
354 * with each other since cpufreq currently sets all CPUs to the
355 * given frequency whereas it's possible for different thermal
356 * zones to specify independent settings for multiple CPUs.
358 for (i = 0; i < devcount; i++) {
359 sc = device_get_softc(devs[i]);
360 if (acpi_tz_cooling_is_available(sc)) {
361 sc->tz_cooling_enabled = TRUE;
362 error = acpi_tz_cooling_thread_start(sc);
364 sc->tz_cooling_enabled = FALSE;
367 acpi_tz_cooling_unit = device_get_unit(devs[i]);
373 SYSINIT(acpi_tz, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, acpi_tz_startup, NULL);
376 * Parse the current state of this thermal zone and set up to use it.
378 * Note that we may have previous state, which will have to be discarded.
381 acpi_tz_establish(struct acpi_tz_softc *sc)
387 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
389 /* Erase any existing state. */
390 for (i = 0; i < TZ_NUMLEVELS; i++)
391 if (sc->tz_zone.al[i].Pointer != NULL)
392 AcpiOsFree(sc->tz_zone.al[i].Pointer);
393 if (sc->tz_zone.psl.Pointer != NULL)
394 AcpiOsFree(sc->tz_zone.psl.Pointer);
397 * XXX: We initialize only ACPI_BUFFER to avoid race condition
398 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
400 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
401 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
402 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
404 /* Evaluate thermal zone parameters. */
405 for (i = 0; i < TZ_NUMLEVELS; i++) {
406 sprintf(nbuf, "_AC%d", i);
407 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
408 sprintf(nbuf, "_AL%d", i);
409 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
410 sc->tz_zone.al[i].Pointer = NULL;
411 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
412 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
414 /* Should be a package containing a list of power objects */
415 if (obj->Type != ACPI_TYPE_PACKAGE) {
416 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
418 return_VALUE (ENXIO);
422 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
423 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
424 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
425 sc->tz_zone.psl.Pointer = NULL;
426 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
427 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
428 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
429 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
430 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
431 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
434 * Sanity-check the values we've been given.
436 * XXX what do we do about systems that give us the same value for
437 * more than one of these setpoints?
439 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
440 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
441 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
442 for (i = 0; i < TZ_NUMLEVELS; i++)
443 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
448 static char *aclevel_string[] = {
449 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
450 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
453 static __inline const char *
454 acpi_tz_aclevel_string(int active)
456 if (active < -1 || active >= TZ_NUMLEVELS)
457 return (aclevel_string[0]);
459 return (aclevel_string[active + 1]);
463 * Get the current temperature.
466 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
471 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
473 /* Evaluate the thermal zone's _TMP method. */
474 status = acpi_GetInteger(sc->tz_handle, acpi_tz_tmp_name, &temp);
475 if (ACPI_FAILURE(status)) {
476 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
477 "error fetching current temperature -- %s\n",
478 AcpiFormatException(status));
482 /* Check it for validity. */
483 acpi_tz_sanity(sc, &temp, acpi_tz_tmp_name);
487 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
488 sc->tz_temperature = temp;
493 * Evaluate the condition of a thermal zone, take appropriate actions.
496 acpi_tz_monitor(void *Context)
498 struct acpi_tz_softc *sc;
499 struct timespec curtime;
502 int newactive, newflags;
504 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
506 sc = (struct acpi_tz_softc *)Context;
508 /* Get the current temperature. */
509 if (!acpi_tz_get_temperature(sc)) {
510 /* XXX disable zone? go to max cooling? */
513 temp = sc->tz_temperature;
516 * Work out what we ought to be doing right now.
518 * Note that the _ACx levels sort from hot to cold.
520 newactive = TZ_ACTIVE_NONE;
521 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
522 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i])
527 * We are going to get _ACx level down (colder side), but give a guaranteed
528 * minimum cooling run time if requested.
530 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
531 sc->tz_active != TZ_ACTIVE_UNKNOWN &&
532 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
534 getnanotime(&curtime);
535 timespecsub(&curtime, &sc->tz_cooling_started, &curtime);
536 if (curtime.tv_sec < acpi_tz_min_runtime)
537 newactive = sc->tz_active;
540 /* Handle user override of active mode */
541 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
542 || sc->tz_requested < newactive))
543 newactive = sc->tz_requested;
545 /* update temperature-related flags */
546 newflags = TZ_THFLAG_NONE;
547 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
548 newflags |= TZ_THFLAG_PSV;
549 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
550 newflags |= TZ_THFLAG_HOT;
551 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
552 newflags |= TZ_THFLAG_CRT;
554 /* If the active cooling state has changed, we have to switch things. */
555 if (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
557 * We don't know which cooling device is on or off,
558 * so stop them all, because we now know which
559 * should be on (if any).
561 for (i = 0; i < TZ_NUMLEVELS; i++) {
562 if (sc->tz_zone.al[i].Pointer != NULL) {
563 acpi_ForeachPackageObject(
564 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
565 acpi_tz_switch_cooler_off, sc);
568 /* now we know that all devices are off */
569 sc->tz_active = TZ_ACTIVE_NONE;
572 if (newactive != sc->tz_active) {
573 /* Turn off unneeded cooling devices that are on, if any are */
574 for (i = TZ_ACTIVE_LEVEL(sc->tz_active);
575 i < TZ_ACTIVE_LEVEL(newactive); i++) {
576 acpi_ForeachPackageObject(
577 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
578 acpi_tz_switch_cooler_off, sc);
580 /* Turn on cooling devices that are required, if any are */
581 for (i = TZ_ACTIVE_LEVEL(sc->tz_active) - 1;
582 i >= TZ_ACTIVE_LEVEL(newactive); i--) {
583 acpi_ForeachPackageObject(
584 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
585 acpi_tz_switch_cooler_on, sc);
588 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
589 "switched from %s to %s: %d.%dC\n",
590 acpi_tz_aclevel_string(sc->tz_active),
591 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
592 sc->tz_active = newactive;
593 getnanotime(&sc->tz_cooling_started);
596 /* XXX (de)activate any passive cooling that may be required. */
599 * If the temperature is at _HOT or _CRT, increment our event count.
600 * If it has occurred enough times, shutdown the system. This is
601 * needed because some systems will report an invalid high temperature
602 * for one poll cycle. It is suspected this is due to the embedded
603 * controller timing out. A typical value is 138C for one cycle on
604 * a system that is otherwise 65C.
606 * If we're almost at that threshold, notify the user through devd(8).
608 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
609 sc->tz_validchecks++;
610 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
611 device_printf(sc->tz_dev,
612 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
613 TZ_KELVTOC(sc->tz_temperature));
614 shutdown_nice(RB_POWEROFF);
615 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
616 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
618 sc->tz_validchecks = 0;
620 sc->tz_thflags = newflags;
626 * Given an object, verify that it's a reference to a device of some sort,
627 * and try to switch it off.
630 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
634 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
636 cooler = acpi_GetReference(NULL, obj);
637 if (cooler == NULL) {
638 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
642 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
644 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
650 * Given an object, verify that it's a reference to a device of some sort,
651 * and try to switch it on.
653 * XXX replication of off/on function code is bad.
656 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
658 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
662 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
664 cooler = acpi_GetReference(NULL, obj);
665 if (cooler == NULL) {
666 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
670 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
672 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
673 if (ACPI_FAILURE(status)) {
674 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
675 "failed to activate %s - %s\n", acpi_name(cooler),
676 AcpiFormatException(status));
683 * Read/debug-print a parameter, default it to -1.
686 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
689 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
691 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
694 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
695 acpi_name(sc->tz_handle), node, *data));
702 * Sanity-check a temperature value. Assume that setpoints
703 * should be between 0C and 200C.
706 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
708 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
710 * If the value we are checking is _TMP, warn the user only
711 * once. This avoids spamming messages if, for instance, the
712 * sensor is broken and always returns an invalid temperature.
714 * This is only done for _TMP; other values always emit a
717 if (what != acpi_tz_tmp_name || !sc->tz_insane_tmp_notified) {
718 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
719 what, TZ_KELVTOC(*val));
721 /* Don't warn the user again if the read value doesn't improve. */
722 if (what == acpi_tz_tmp_name)
723 sc->tz_insane_tmp_notified = 1;
729 /* This value is correct. Warn if it's incorrect again. */
730 if (what == acpi_tz_tmp_name)
731 sc->tz_insane_tmp_notified = 0;
735 * Respond to a sysctl on the active state node.
738 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
740 struct acpi_tz_softc *sc;
744 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
745 active = sc->tz_active;
746 error = sysctl_handle_int(oidp, &active, 0, req);
748 /* Error or no new value */
749 if (error != 0 || req->newptr == NULL)
751 if (active < -1 || active >= TZ_NUMLEVELS)
754 /* Set new preferred level and re-switch */
755 sc->tz_requested = active;
756 acpi_tz_signal(sc, 0);
761 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
763 struct acpi_tz_softc *sc;
766 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
767 enabled = sc->tz_cooling_enabled;
768 error = sysctl_handle_int(oidp, &enabled, 0, req);
770 /* Error or no new value */
771 if (error != 0 || req->newptr == NULL)
773 if (enabled != TRUE && enabled != FALSE)
777 if (acpi_tz_cooling_is_available(sc))
778 error = acpi_tz_cooling_thread_start(sc);
784 sc->tz_cooling_enabled = enabled;
789 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
791 struct acpi_tz_softc *sc;
796 temp_ptr = (int *)(void *)(uintptr_t)((uintptr_t)sc + oidp->oid_arg2);
798 error = sysctl_handle_int(oidp, &temp, 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)
808 /* Check user-supplied value for sanity. */
809 acpi_tz_sanity(sc, &temp, "user-supplied temp");
818 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
820 struct acpi_tz_softc *sc;
825 val_ptr = (int *)(void *)(uintptr_t)((uintptr_t)sc + oidp->oid_arg2);
827 error = sysctl_handle_int(oidp, &val, 0, req);
829 /* Error or no new value */
830 if (error != 0 || req->newptr == NULL)
833 /* Only allow changing settings if override is set. */
834 if (!acpi_tz_override)
842 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
844 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
846 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
849 case TZ_NOTIFY_TEMPERATURE:
850 /* Temperature change occurred */
851 acpi_tz_signal(sc, 0);
853 case TZ_NOTIFY_DEVICES:
854 case TZ_NOTIFY_LEVELS:
855 /* Zone devices/setpoints changed */
856 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
859 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
860 "unknown Notify event 0x%x\n", notify);
864 acpi_UserNotify("Thermal", h, notify);
870 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
873 sc->tz_flags |= flags;
874 ACPI_UNLOCK(thermal);
875 wakeup(&acpi_tz_proc);
879 * Notifies can be generated asynchronously but have also been seen to be
880 * triggered by other thermal methods. One system generates a notify of
881 * 0x81 when the fan is turned on or off. Another generates it when _SCP
882 * is called. To handle these situations, we check the zone via
883 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
887 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
890 /* Check the current temperature and take action based on it */
893 /* If requested, get the power profile settings. */
894 if (flags & TZ_FLAG_GETPROFILE)
895 acpi_tz_power_profile(sc);
898 * If requested, check for new devices/setpoints. After finding them,
899 * check if we need to switch fans based on the new values.
901 if (flags & TZ_FLAG_GETSETTINGS) {
902 acpi_tz_establish(sc);
906 /* XXX passive cooling actions? */
910 * System power profile may have changed; fetch and notify the
911 * thermal zone accordingly.
913 * Since this can be called from an arbitrary eventhandler, it needs
914 * to get the ACPI lock itself.
917 acpi_tz_power_profile(void *arg)
920 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
923 state = power_profile_get_state();
924 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
927 /* check that we haven't decided there's no _SCP method */
928 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
930 /* Call _SCP to set the new profile */
931 status = acpi_SetInteger(sc->tz_handle, "_SCP",
932 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
933 if (ACPI_FAILURE(status)) {
934 if (status != AE_NOT_FOUND)
935 ACPI_VPRINT(sc->tz_dev,
936 acpi_device_get_parent_softc(sc->tz_dev),
937 "can't evaluate %s._SCP - %s\n",
938 acpi_name(sc->tz_handle),
939 AcpiFormatException(status));
940 sc->tz_flags |= TZ_FLAG_NO_SCP;
942 /* We have to re-evaluate the entire zone now */
943 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
949 * Thermal zone monitor thread.
952 acpi_tz_thread(void *arg)
957 struct acpi_tz_softc **sc;
959 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
966 /* If the number of devices has changed, re-evaluate. */
967 if (devclass_get_count(acpi_tz_devclass) != devcount) {
972 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
973 sc = malloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
975 for (i = 0; i < devcount; i++)
976 sc[i] = device_get_softc(devs[i]);
979 /* Check for temperature events and act on them. */
980 for (i = 0; i < devcount; i++) {
982 flags = sc[i]->tz_flags;
983 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
984 ACPI_UNLOCK(thermal);
985 acpi_tz_timeout(sc[i], flags);
988 /* If more work to do, don't go to sleep yet. */
990 for (i = 0; i < devcount; i++) {
991 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
996 * If we have no more work, sleep for a while, setting PDROP so that
997 * the mutex will not be reacquired. Otherwise, drop the mutex and
998 * loop to handle more events.
1001 msleep(&acpi_tz_proc, &thermal_mutex, PZERO | PDROP, "tzpoll",
1002 hz * acpi_tz_polling_rate);
1004 ACPI_UNLOCK(thermal);
1009 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
1014 if (!sc->tz_cooling_updated)
1016 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
1018 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1019 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
1020 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
1021 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
1023 sc->tz_cooling_updated = FALSE;
1028 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1031 struct cf_level *levels;
1032 int num_levels, error, freq, desired_freq, perf, i;
1034 levels = malloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1039 * Find the main device, cpufreq0. We don't yet support independent
1040 * CPU frequency control on SMP.
1042 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1047 /* Get the current frequency. */
1048 error = CPUFREQ_GET(dev, &levels[0]);
1051 freq = levels[0].total_set.freq;
1053 /* Get the current available frequency levels. */
1054 num_levels = CPUFREQ_MAX_LEVELS;
1055 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1058 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1062 /* Calculate the desired frequency as a percent of the max frequency. */
1063 perf = 100 * freq / levels[0].total_set.freq - req;
1066 else if (perf > 100)
1068 desired_freq = levels[0].total_set.freq * perf / 100;
1070 if (desired_freq < freq) {
1071 /* Find the closest available frequency, rounding down. */
1072 for (i = 0; i < num_levels; i++)
1073 if (levels[i].total_set.freq <= desired_freq)
1076 /* If we didn't find a relevant setting, use the lowest. */
1077 if (i == num_levels)
1080 /* If we didn't decrease frequency yet, don't increase it. */
1081 if (!sc->tz_cooling_updated) {
1082 sc->tz_cooling_active = FALSE;
1086 /* Use saved cpu frequency as maximum value. */
1087 if (desired_freq > sc->tz_cooling_saved_freq)
1088 desired_freq = sc->tz_cooling_saved_freq;
1090 /* Find the closest available frequency, rounding up. */
1091 for (i = num_levels - 1; i >= 0; i--)
1092 if (levels[i].total_set.freq >= desired_freq)
1095 /* If we didn't find a relevant setting, use the highest. */
1099 /* If we're going to the highest frequency, restore the old setting. */
1100 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1101 error = acpi_tz_cpufreq_restore(sc);
1103 sc->tz_cooling_active = FALSE;
1108 /* If we are going to a new frequency, activate it. */
1109 if (levels[i].total_set.freq != freq) {
1110 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1111 "temperature %d.%dC: %screasing clock speed "
1112 "from %d MHz to %d MHz\n",
1113 TZ_KELVTOC(sc->tz_temperature),
1114 (freq > levels[i].total_set.freq) ? "de" : "in",
1115 freq, levels[i].total_set.freq);
1116 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1117 if (error == 0 && !sc->tz_cooling_updated) {
1118 sc->tz_cooling_saved_freq = freq;
1119 sc->tz_cooling_updated = TRUE;
1125 free(levels, M_TEMP);
1130 * Passive cooling thread; monitors current temperature according to the
1131 * cooling interval and calculates whether to scale back CPU frequency.
1134 acpi_tz_cooling_thread(void *arg)
1136 struct acpi_tz_softc *sc;
1137 int error, perf, curr_temp, prev_temp;
1139 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1141 sc = (struct acpi_tz_softc *)arg;
1143 prev_temp = sc->tz_temperature;
1144 while (sc->tz_cooling_enabled) {
1145 if (sc->tz_cooling_active)
1146 (void)acpi_tz_get_temperature(sc);
1147 curr_temp = sc->tz_temperature;
1148 if (curr_temp >= sc->tz_zone.psv)
1149 sc->tz_cooling_active = TRUE;
1150 if (sc->tz_cooling_active) {
1151 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1152 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1156 error = acpi_tz_cpufreq_update(sc, perf);
1159 * If error and not simply a higher priority setting was
1160 * active, disable cooling.
1162 if (error != 0 && error != EPERM) {
1163 device_printf(sc->tz_dev,
1164 "failed to set new freq, disabling passive cooling\n");
1165 sc->tz_cooling_enabled = FALSE;
1169 prev_temp = curr_temp;
1170 tsleep(&sc->tz_cooling_proc, PZERO, "cooling",
1171 hz * sc->tz_zone.tsp / 10);
1173 if (sc->tz_cooling_active) {
1174 acpi_tz_cpufreq_restore(sc);
1175 sc->tz_cooling_active = FALSE;
1177 sc->tz_cooling_proc = NULL;
1179 sc->tz_cooling_proc_running = FALSE;
1180 ACPI_UNLOCK(thermal);
1185 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1186 * all CPUs for us. However, it's possible in the future _PSL will
1187 * reference non-CPU devices so we may want to support it then.
1190 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1192 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1193 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1194 sc->tz_zone.psv != -1);
1198 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1203 if (sc->tz_cooling_proc_running) {
1204 ACPI_UNLOCK(thermal);
1207 sc->tz_cooling_proc_running = TRUE;
1208 ACPI_UNLOCK(thermal);
1210 if (sc->tz_cooling_proc == NULL) {
1211 error = kproc_create(acpi_tz_cooling_thread, sc,
1212 &sc->tz_cooling_proc, RFHIGHPID, 0, "acpi_cooling%d",
1213 device_get_unit(sc->tz_dev));
1215 device_printf(sc->tz_dev, "could not create thread - %d", error);
1217 sc->tz_cooling_proc_running = FALSE;
1218 ACPI_UNLOCK(thermal);