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
27 * $FreeBSD: head/sys/dev/acpica/acpi_thermal.c 255077 2013-08-30 19:21:12Z dumbbell $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
34 #include <sys/kthread.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <sys/reboot.h>
39 #include <sys/sysctl.h>
40 #include <sys/unistd.h>
41 #include <sys/power.h>
42 #include <sys/sensors.h>
44 #include <sys/mplock2.h>
49 #include <dev/acpica/acpivar.h>
51 /* Hooks for the ACPICA debugging infrastructure */
52 #define _COMPONENT ACPI_THERMAL
53 ACPI_MODULE_NAME("THERMAL")
56 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
58 #define TZ_NOTIFY_TEMPERATURE 0x80 /* Temperature changed. */
59 #define TZ_NOTIFY_LEVELS 0x81 /* Cooling levels changed. */
60 #define TZ_NOTIFY_DEVICES 0x82 /* Device lists changed. */
61 #define TZ_NOTIFY_CRITICAL 0xcc /* Fake notify that _CRT/_HOT reached. */
63 /* Check for temperature changes every 10 seconds by default */
64 #define TZ_POLLRATE 10
66 /* Make sure the reported temperature is valid for this number of polls. */
67 #define TZ_VALIDCHECKS 3
69 /* Notify the user we will be shutting down in one more poll cycle. */
70 #define TZ_NOTIFYCOUNT (TZ_VALIDCHECKS - 1)
72 /* ACPI spec defines this */
73 #define TZ_NUMLEVELS 10
76 ACPI_BUFFER al[TZ_NUMLEVELS];
87 struct acpi_tz_softc {
89 ACPI_HANDLE tz_handle; /*Thermal zone handle*/
90 int tz_temperature; /*Current temperature*/
91 int tz_active; /*Current active cooling*/
92 #define TZ_ACTIVE_NONE -1
93 #define TZ_ACTIVE_UNKNOWN -2
94 int tz_requested; /*Minimum active cooling*/
95 int tz_thflags; /*Current temp-related flags*/
96 #define TZ_THFLAG_NONE 0
97 #define TZ_THFLAG_PSV (1<<0)
98 #define TZ_THFLAG_HOT (1<<2)
99 #define TZ_THFLAG_CRT (1<<3)
101 #define TZ_FLAG_NO_SCP (1<<0) /*No _SCP method*/
102 #define TZ_FLAG_GETPROFILE (1<<1) /*Get power_profile in timeout*/
103 #define TZ_FLAG_GETSETTINGS (1<<2) /*Get devs/setpoints*/
104 struct timespec tz_cooling_started;
105 /*Current cooling starting time*/
107 struct sysctl_ctx_list tz_sysctl_ctx;
108 struct sysctl_oid *tz_sysctl_tree;
109 eventhandler_tag tz_event;
111 struct acpi_tz_zone tz_zone; /*Thermal zone parameters*/
113 int tz_insane_tmp_notified;
115 /* passive cooling */
116 struct thread *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;
122 /* sensors(9) related */
123 struct ksensordev sensordev;
124 struct ksensor sensor;
127 #define TZ_ACTIVE_LEVEL(act) ((act) >= 0 ? (act) : TZ_NUMLEVELS)
129 #define CPUFREQ_MAX_LEVELS 64 /* XXX cpufreq should export this */
131 static int acpi_tz_probe(device_t dev);
132 static int acpi_tz_attach(device_t dev);
133 static int acpi_tz_establish(struct acpi_tz_softc *sc);
134 static void acpi_tz_monitor(void *Context);
135 static void acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
136 static void acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
137 static void acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
139 static void acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
140 static int acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
141 static int acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS);
142 static int acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS);
143 static int acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
144 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
146 static void acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
147 static void acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
148 static void acpi_tz_power_profile(void *arg);
149 static void acpi_tz_thread(void *arg);
150 static int acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
151 static int acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
153 static device_method_t acpi_tz_methods[] = {
154 /* Device interface */
155 DEVMETHOD(device_probe, acpi_tz_probe),
156 DEVMETHOD(device_attach, acpi_tz_attach),
161 static driver_t acpi_tz_driver = {
164 sizeof(struct acpi_tz_softc),
167 static char *acpi_tz_tmp_name = "_TMP";
169 static devclass_t acpi_tz_devclass;
170 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, NULL, NULL);
171 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
173 static struct sysctl_ctx_list acpi_tz_sysctl_ctx;
174 static struct sysctl_oid *acpi_tz_sysctl_tree;
176 /* Minimum cooling run time */
177 static int acpi_tz_min_runtime;
178 static int acpi_tz_polling_rate = TZ_POLLRATE;
179 static int acpi_tz_override;
181 /* Timezone polling thread */
182 static struct thread *acpi_tz_td;
183 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
185 static int acpi_tz_cooling_unit = -1;
188 acpi_tz_probe(device_t dev)
192 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
193 device_set_desc(dev, "Thermal Zone");
201 acpi_tz_attach(device_t dev)
203 struct acpi_tz_softc *sc;
204 struct acpi_softc *acpi_sc;
208 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
209 if (device_get_unit(dev) == 0)
210 ACPI_LOCK_INIT(thermal, "acpitz");
212 sc = device_get_softc(dev);
214 sc->tz_handle = acpi_get_handle(dev);
215 sc->tz_requested = TZ_ACTIVE_NONE;
216 sc->tz_active = TZ_ACTIVE_UNKNOWN;
217 sc->tz_thflags = TZ_THFLAG_NONE;
218 sc->tz_cooling_proc = NULL;
219 sc->tz_cooling_proc_running = FALSE;
220 sc->tz_cooling_active = FALSE;
221 sc->tz_cooling_updated = FALSE;
222 sc->tz_cooling_enabled = FALSE;
225 * Parse the current state of the thermal zone and build control
226 * structures. We don't need to worry about interference with the
227 * control thread since we haven't fully attached this device yet.
229 if ((error = acpi_tz_establish(sc)) != 0)
233 * Register for any Notify events sent to this zone.
235 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
236 acpi_tz_notify_handler, sc);
239 * Create our sysctl nodes.
241 * XXX we need a mechanism for adding nodes under ACPI.
243 if (device_get_unit(dev) == 0) {
244 acpi_sc = acpi_device_get_parent_softc(dev);
245 sysctl_ctx_init(&acpi_tz_sysctl_ctx);
246 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
247 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
248 OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
249 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
250 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
251 OID_AUTO, "min_runtime", CTLFLAG_RW,
252 &acpi_tz_min_runtime, 0,
253 "minimum cooling run time in sec");
254 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
255 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
256 OID_AUTO, "polling_rate", CTLFLAG_RW,
257 &acpi_tz_polling_rate, 0, "monitor polling interval in seconds");
258 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
259 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
260 "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
261 "allow override of thermal settings");
263 sysctl_ctx_init(&sc->tz_sysctl_ctx);
264 ksprintf(oidname, "tz%d", device_get_unit(dev));
265 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
266 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
267 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
268 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
269 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD,
270 &sc->tz_temperature, 0, sysctl_handle_int,
271 "IK", "current thermal zone temperature");
272 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
273 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
274 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
275 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
276 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
277 sc, 0, acpi_tz_cooling_sysctl, "I",
278 "enable passive (speed reduction) cooling");
280 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
281 OID_AUTO, "thermal_flags", CTLFLAG_RD,
282 &sc->tz_thflags, 0, "thermal zone flags");
283 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
284 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
285 sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
286 acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
287 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
288 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
289 sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
290 acpi_tz_temp_sysctl, "IK",
291 "too hot temp setpoint (suspend now)");
292 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
293 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
294 sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
295 acpi_tz_temp_sysctl, "IK",
296 "critical temp setpoint (shutdown now)");
297 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
298 OID_AUTO, "_ACx", CTLTYPE_INT | CTLFLAG_RD,
299 &sc->tz_zone.ac, sizeof(sc->tz_zone.ac),
300 sysctl_handle_opaque, "IK", "");
301 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
302 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
303 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
304 acpi_tz_passive_sysctl, "I",
305 "thermal constant 1 for passive cooling");
306 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
307 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
308 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
309 acpi_tz_passive_sysctl, "I",
310 "thermal constant 2 for passive cooling");
311 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
312 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
313 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
314 acpi_tz_passive_sysctl, "I",
315 "thermal sampling period for passive cooling");
318 * Create thread to service all of the thermal zones. Register
319 * our power profile event handler.
321 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
322 acpi_tz_power_profile, sc, 0);
323 if (acpi_tz_td == NULL) {
324 error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_td,
327 device_printf(sc->tz_dev, "could not create thread - %d", error);
333 * Create a thread to handle passive cooling for 1st zone which
334 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
335 * zones manually for now.
337 * XXX We enable only one zone to avoid multiple zones conflict
338 * with each other since cpufreq currently sets all CPUs to the
339 * given frequency whereas it's possible for different thermal
340 * zones to specify independent settings for multiple CPUs.
342 if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
343 sc->tz_cooling_enabled = TRUE;
344 if (sc->tz_cooling_enabled) {
345 error = acpi_tz_cooling_thread_start(sc);
347 sc->tz_cooling_enabled = FALSE;
350 acpi_tz_cooling_unit = device_get_unit(dev);
354 * Flag the event handler for a manual invocation by our timeout.
355 * We defer it like this so that the rest of the subsystem has time
356 * to come up. Don't bother evaluating/printing the temperature at
357 * this point; on many systems it'll be bogus until the EC is running.
359 sc->tz_flags |= TZ_FLAG_GETPROFILE;
361 /* Attach sensors(9). */
362 strlcpy(sc->sensordev.xname, device_get_nameunit(sc->tz_dev),
363 sizeof(sc->sensordev.xname));
365 sc->sensor.type = SENSOR_TEMP;
366 sensor_attach(&sc->sensordev, &sc->sensor);
368 sensordev_install(&sc->sensordev);
372 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
373 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
374 acpi_tz_notify_handler);
375 sysctl_ctx_free(&sc->tz_sysctl_ctx);
377 return_VALUE (error);
381 * Parse the current state of this thermal zone and set up to use it.
383 * Note that we may have previous state, which will have to be discarded.
386 acpi_tz_establish(struct acpi_tz_softc *sc)
392 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
394 /* Erase any existing state. */
395 for (i = 0; i < TZ_NUMLEVELS; i++)
396 if (sc->tz_zone.al[i].Pointer != NULL)
397 AcpiOsFree(sc->tz_zone.al[i].Pointer);
398 if (sc->tz_zone.psl.Pointer != NULL)
399 AcpiOsFree(sc->tz_zone.psl.Pointer);
402 * XXX: We initialize only ACPI_BUFFER to avoid race condition
403 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
405 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
406 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
407 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
409 /* Evaluate thermal zone parameters. */
410 for (i = 0; i < TZ_NUMLEVELS; i++) {
411 ksprintf(nbuf, "_AC%d", i);
412 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
413 ksprintf(nbuf, "_AL%d", i);
414 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
415 sc->tz_zone.al[i].Pointer = NULL;
416 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
417 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
419 /* Should be a package containing a list of power objects */
420 if (obj->Type != ACPI_TYPE_PACKAGE) {
421 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
423 return_VALUE (ENXIO);
427 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
428 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
429 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
430 sc->tz_zone.psl.Pointer = NULL;
431 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
432 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
433 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
434 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
435 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
436 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
439 * Sanity-check the values we've been given.
441 * XXX what do we do about systems that give us the same value for
442 * more than one of these setpoints?
444 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
445 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
446 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
447 for (i = 0; i < TZ_NUMLEVELS; i++)
448 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
453 static char *aclevel_string[] = {
454 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
455 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
458 static __inline const char *
459 acpi_tz_aclevel_string(int active)
461 if (active < -1 || active >= TZ_NUMLEVELS)
462 return (aclevel_string[0]);
464 return (aclevel_string[active + 1]);
468 * Get the current temperature.
471 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
476 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
478 /* Evaluate the thermal zone's _TMP method. */
479 status = acpi_GetInteger(sc->tz_handle, acpi_tz_tmp_name, &temp);
480 if (ACPI_FAILURE(status)) {
481 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
482 "error fetching current temperature -- %s\n",
483 AcpiFormatException(status));
487 /* Check it for validity. */
488 acpi_tz_sanity(sc, &temp, acpi_tz_tmp_name);
492 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
493 sc->tz_temperature = temp;
495 if(sc->tz_temperature == -1)
496 sc->sensor.flags &= ~SENSOR_FINVALID;
497 sc->sensor.value = sc->tz_temperature * 100000 - 50000;
502 * Evaluate the condition of a thermal zone, take appropriate actions.
505 acpi_tz_monitor(void *Context)
507 struct acpi_tz_softc *sc;
508 struct timespec curtime;
511 int newactive, newflags;
513 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
515 sc = (struct acpi_tz_softc *)Context;
517 /* Get the current temperature. */
518 if (!acpi_tz_get_temperature(sc)) {
519 /* XXX disable zone? go to max cooling? */
522 temp = sc->tz_temperature;
525 * Work out what we ought to be doing right now.
527 * Note that the _ACx levels sort from hot to cold.
529 newactive = TZ_ACTIVE_NONE;
530 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
531 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i])
536 * We are going to get _ACx level down (colder side), but give a guaranteed
537 * minimum cooling run time if requested.
539 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
540 sc->tz_active != TZ_ACTIVE_UNKNOWN &&
541 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
543 getnanotime(&curtime);
544 timespecsub(&curtime, &sc->tz_cooling_started);
545 if (curtime.tv_sec < acpi_tz_min_runtime)
546 newactive = sc->tz_active;
549 /* Handle user override of active mode */
550 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
551 || sc->tz_requested < newactive))
552 newactive = sc->tz_requested;
554 /* update temperature-related flags */
555 newflags = TZ_THFLAG_NONE;
556 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
557 newflags |= TZ_THFLAG_PSV;
558 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
559 newflags |= TZ_THFLAG_HOT;
560 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
561 newflags |= TZ_THFLAG_CRT;
563 /* If the active cooling state has changed, we have to switch things. */
564 if (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
566 * We don't know which cooling device is on or off,
567 * so stop them all, because we now know which
568 * should be on (if any).
570 for (i = 0; i < TZ_NUMLEVELS; i++) {
571 if (sc->tz_zone.al[i].Pointer != NULL) {
572 acpi_ForeachPackageObject(
573 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
574 acpi_tz_switch_cooler_off, sc);
577 /* now we know that all devices are off */
578 sc->tz_active = TZ_ACTIVE_NONE;
581 if (newactive != sc->tz_active) {
582 /* Turn off unneeded cooling devices that are on, if any are */
583 for (i = TZ_ACTIVE_LEVEL(sc->tz_active);
584 i < TZ_ACTIVE_LEVEL(newactive); i++) {
585 acpi_ForeachPackageObject(
586 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
587 acpi_tz_switch_cooler_off, sc);
589 /* Turn on cooling devices that are required, if any are */
590 for (i = TZ_ACTIVE_LEVEL(sc->tz_active) - 1;
591 i >= TZ_ACTIVE_LEVEL(newactive); i--) {
592 acpi_ForeachPackageObject(
593 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
594 acpi_tz_switch_cooler_on, sc);
597 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
598 "switched from %s to %s: %d.%dC\n",
599 acpi_tz_aclevel_string(sc->tz_active),
600 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
601 sc->tz_active = newactive;
602 getnanotime(&sc->tz_cooling_started);
605 /* XXX (de)activate any passive cooling that may be required. */
608 * If the temperature is at _HOT or _CRT, increment our event count.
609 * If it has occurred enough times, shutdown the system. This is
610 * needed because some systems will report an invalid high temperature
611 * for one poll cycle. It is suspected this is due to the embedded
612 * controller timing out. A typical value is 138C for one cycle on
613 * a system that is otherwise 65C.
615 * If we're almost at that threshold, notify the user through devd(8).
617 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
618 sc->tz_validchecks++;
619 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
620 device_printf(sc->tz_dev,
621 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
622 TZ_KELVTOC(sc->tz_temperature));
623 shutdown_nice(RB_POWEROFF);
624 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
625 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
627 sc->tz_validchecks = 0;
629 sc->tz_thflags = newflags;
635 * Given an object, verify that it's a reference to a device of some sort,
636 * and try to switch it off.
639 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
643 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
645 cooler = acpi_GetReference(NULL, obj);
646 if (cooler == NULL) {
647 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
651 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
653 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
659 * Given an object, verify that it's a reference to a device of some sort,
660 * and try to switch it on.
662 * XXX replication of off/on function code is bad.
665 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
667 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
671 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
673 cooler = acpi_GetReference(NULL, obj);
674 if (cooler == NULL) {
675 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
679 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
681 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
682 if (ACPI_FAILURE(status)) {
683 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
684 "failed to activate %s - %s\n", acpi_name(cooler),
685 AcpiFormatException(status));
692 * Read/debug-print a parameter, default it to -1.
695 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
698 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
700 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
703 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
704 acpi_name(sc->tz_handle), node, *data));
711 * Sanity-check a temperature value. Assume that setpoints
712 * should be between 0C and 200C.
715 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
717 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
719 * If the value we are checking is _TMP, warn the user only
720 * once. This avoids spamming messages if, for instance, the
721 * sensor is broken and always returns an invalid temperature.
723 * This is only done for _TMP; other values always emit a
726 if (what != acpi_tz_tmp_name || !sc->tz_insane_tmp_notified) {
727 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
728 what, TZ_KELVTOC(*val));
730 /* Don't warn the user again if the read value doesn't improve. */
731 if (what == acpi_tz_tmp_name)
732 sc->tz_insane_tmp_notified = 1;
738 /* This value is correct. Warn if it's incorrect again. */
739 if (what == acpi_tz_tmp_name)
740 sc->tz_insane_tmp_notified = 0;
744 * Respond to a sysctl on the active state node.
747 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
749 struct acpi_tz_softc *sc;
753 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
754 active = sc->tz_active;
755 error = sysctl_handle_int(oidp, &active, 0, req);
757 /* Error or no new value */
758 if (error != 0 || req->newptr == NULL)
760 if (active < -1 || active >= TZ_NUMLEVELS)
763 /* Set new preferred level and re-switch */
764 sc->tz_requested = active;
765 acpi_tz_signal(sc, 0);
770 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
772 struct acpi_tz_softc *sc;
775 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
776 enabled = sc->tz_cooling_enabled;
777 error = sysctl_handle_int(oidp, &enabled, 0, req);
779 /* Error or no new value */
780 if (error != 0 || req->newptr == NULL)
782 if (enabled != TRUE && enabled != FALSE)
786 if (acpi_tz_cooling_is_available(sc))
787 error = acpi_tz_cooling_thread_start(sc);
793 sc->tz_cooling_enabled = enabled;
798 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
800 struct acpi_tz_softc *sc;
805 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
807 error = sysctl_handle_int(oidp, &temp, 0, req);
809 /* Error or no new value */
810 if (error != 0 || req->newptr == NULL)
813 /* Only allow changing settings if override is set. */
814 if (!acpi_tz_override)
817 /* Check user-supplied value for sanity. */
818 acpi_tz_sanity(sc, &temp, "user-supplied temp");
827 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
829 struct acpi_tz_softc *sc;
834 val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
836 error = sysctl_handle_int(oidp, &val, 0, req);
838 /* Error or no new value */
839 if (error != 0 || req->newptr == NULL)
842 /* Only allow changing settings if override is set. */
843 if (!acpi_tz_override)
851 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
853 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
855 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
858 case TZ_NOTIFY_TEMPERATURE:
859 /* Temperature change occurred */
860 acpi_tz_signal(sc, 0);
862 case TZ_NOTIFY_DEVICES:
863 case TZ_NOTIFY_LEVELS:
864 /* Zone devices/setpoints changed */
865 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
868 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
869 "unknown Notify event 0x%x\n", notify);
873 acpi_UserNotify("Thermal", h, notify);
879 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
882 sc->tz_flags |= flags;
883 ACPI_UNLOCK(thermal);
888 * Notifies can be generated asynchronously but have also been seen to be
889 * triggered by other thermal methods. One system generates a notify of
890 * 0x81 when the fan is turned on or off. Another generates it when _SCP
891 * is called. To handle these situations, we check the zone via
892 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
896 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
899 /* Check the current temperature and take action based on it */
902 /* If requested, get the power profile settings. */
903 if (flags & TZ_FLAG_GETPROFILE)
904 acpi_tz_power_profile(sc);
907 * If requested, check for new devices/setpoints. After finding them,
908 * check if we need to switch fans based on the new values.
910 if (flags & TZ_FLAG_GETSETTINGS) {
911 acpi_tz_establish(sc);
915 /* XXX passive cooling actions? */
919 * System power profile may have changed; fetch and notify the
920 * thermal zone accordingly.
922 * Since this can be called from an arbitrary eventhandler, it needs
923 * to get the ACPI lock itself.
926 acpi_tz_power_profile(void *arg)
929 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
932 state = power_profile_get_state();
933 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
936 /* check that we haven't decided there's no _SCP method */
937 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
939 /* Call _SCP to set the new profile */
940 status = acpi_SetInteger(sc->tz_handle, "_SCP",
941 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
942 if (ACPI_FAILURE(status)) {
943 if (status != AE_NOT_FOUND)
944 ACPI_VPRINT(sc->tz_dev,
945 acpi_device_get_parent_softc(sc->tz_dev),
946 "can't evaluate %s._SCP - %s\n",
947 acpi_name(sc->tz_handle),
948 AcpiFormatException(status));
949 sc->tz_flags |= TZ_FLAG_NO_SCP;
951 /* We have to re-evaluate the entire zone now */
952 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
958 * Thermal zone monitor thread.
961 acpi_tz_thread(void *arg)
966 struct acpi_tz_softc **sc;
968 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
976 /* If the number of devices has changed, re-evaluate. */
977 if (devclass_get_count(acpi_tz_devclass) != devcount) {
982 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
983 sc = kmalloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
985 for (i = 0; i < devcount; i++)
986 sc[i] = device_get_softc(devs[i]);
989 /* Check for temperature events and act on them. */
990 for (i = 0; i < devcount; i++) {
992 flags = sc[i]->tz_flags;
993 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
994 ACPI_UNLOCK(thermal);
995 acpi_tz_timeout(sc[i], flags);
998 /* If more work to do, don't go to sleep yet. */
1000 for (i = 0; i < devcount; i++) {
1001 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
1006 * Interlocked sleep until signaled or we timeout.
1008 if (i == devcount) {
1009 tsleep_interlock(&acpi_tz_td, 0);
1010 ACPI_UNLOCK(thermal);
1011 tsleep(&acpi_tz_td, 0, "tzpoll", hz * acpi_tz_polling_rate);
1013 ACPI_UNLOCK(thermal);
1021 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
1026 if (!sc->tz_cooling_updated)
1028 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
1030 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1031 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
1032 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
1033 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
1035 sc->tz_cooling_updated = FALSE;
1040 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1043 struct cf_level *levels;
1044 int num_levels, error, freq, desired_freq, perf, i;
1046 levels = kmalloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1051 * Find the main device, cpufreq0. We don't yet support independent
1052 * CPU frequency control on SMP.
1054 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1059 /* Get the current frequency. */
1060 error = CPUFREQ_GET(dev, &levels[0]);
1063 freq = levels[0].total_set.freq;
1065 /* Get the current available frequency levels. */
1066 num_levels = CPUFREQ_MAX_LEVELS;
1067 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1070 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1074 /* Calculate the desired frequency as a percent of the max frequency. */
1075 perf = 100 * freq / levels[0].total_set.freq - req;
1078 else if (perf > 100)
1080 desired_freq = levels[0].total_set.freq * perf / 100;
1082 if (desired_freq < freq) {
1083 /* Find the closest available frequency, rounding down. */
1084 for (i = 0; i < num_levels; i++)
1085 if (levels[i].total_set.freq <= desired_freq)
1088 /* If we didn't find a relevant setting, use the lowest. */
1089 if (i == num_levels)
1092 /* If we didn't decrease frequency yet, don't increase it. */
1093 if (!sc->tz_cooling_updated) {
1094 sc->tz_cooling_active = FALSE;
1098 /* Use saved cpu frequency as maximum value. */
1099 if (desired_freq > sc->tz_cooling_saved_freq)
1100 desired_freq = sc->tz_cooling_saved_freq;
1102 /* Find the closest available frequency, rounding up. */
1103 for (i = num_levels - 1; i >= 0; i--)
1104 if (levels[i].total_set.freq >= desired_freq)
1107 /* If we didn't find a relevant setting, use the highest. */
1111 /* If we're going to the highest frequency, restore the old setting. */
1112 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1113 error = acpi_tz_cpufreq_restore(sc);
1115 sc->tz_cooling_active = FALSE;
1120 /* If we are going to a new frequency, activate it. */
1121 if (levels[i].total_set.freq != freq) {
1122 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1123 "temperature %d.%dC: %screasing clock speed "
1124 "from %d MHz to %d MHz\n",
1125 TZ_KELVTOC(sc->tz_temperature),
1126 (freq > levels[i].total_set.freq) ? "de" : "in",
1127 freq, levels[i].total_set.freq);
1128 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1129 if (error == 0 && !sc->tz_cooling_updated) {
1130 sc->tz_cooling_saved_freq = freq;
1131 sc->tz_cooling_updated = TRUE;
1137 free(levels, M_TEMP);
1143 * Passive cooling thread; monitors current temperature according to the
1144 * cooling interval and calculates whether to scale back CPU frequency.
1147 acpi_tz_cooling_thread(void *arg)
1149 struct acpi_tz_softc *sc;
1150 int perf, curr_temp, prev_temp;
1155 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1157 sc = (struct acpi_tz_softc *)arg;
1160 prev_temp = sc->tz_temperature;
1161 while (sc->tz_cooling_enabled) {
1162 if (sc->tz_cooling_active)
1163 (void)acpi_tz_get_temperature(sc);
1164 curr_temp = sc->tz_temperature;
1165 if (curr_temp >= sc->tz_zone.psv)
1166 sc->tz_cooling_active = TRUE;
1167 if (sc->tz_cooling_active) {
1168 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1169 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1174 error = acpi_tz_cpufreq_update(sc, perf);
1177 * If error and not simply a higher priority setting was
1178 * active, disable cooling.
1180 if (error != 0 && error != EPERM) {
1181 device_printf(sc->tz_dev,
1182 "failed to set new freq, disabling passive cooling\n");
1183 sc->tz_cooling_enabled = FALSE;
1188 prev_temp = curr_temp;
1189 tsleep(&sc->tz_cooling_proc, 0, "cooling",
1190 hz * sc->tz_zone.tsp / 10);
1192 if (sc->tz_cooling_active) {
1194 acpi_tz_cpufreq_restore(sc);
1196 sc->tz_cooling_active = FALSE;
1198 sc->tz_cooling_proc = NULL;
1200 sc->tz_cooling_proc_running = FALSE;
1201 ACPI_UNLOCK(thermal);
1206 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1207 * all CPUs for us. However, it's possible in the future _PSL will
1208 * reference non-CPU devices so we may want to support it then.
1211 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1213 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1214 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1215 sc->tz_zone.psv != -1);
1219 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1224 if (sc->tz_cooling_proc_running) {
1225 ACPI_UNLOCK(thermal);
1228 sc->tz_cooling_proc_running = TRUE;
1229 ACPI_UNLOCK(thermal);
1231 if (sc->tz_cooling_proc == NULL) {
1232 error = kthread_create(acpi_tz_cooling_thread, sc,
1233 &sc->tz_cooling_proc,
1234 "acpi_cooling%d", device_get_unit(sc->tz_dev));
1236 device_printf(sc->tz_dev, "could not create thread - %d", error);
1238 sc->tz_cooling_proc_running = FALSE;
1239 ACPI_UNLOCK(thermal);