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*/
112 time_t tz_error_time; /*Lookup error timestamp*/
114 int tz_insane_tmp_notified;
116 /* passive cooling */
117 struct thread *tz_cooling_proc;
118 int tz_cooling_proc_running;
119 int tz_cooling_enabled;
120 int tz_cooling_active;
121 int tz_cooling_updated;
122 int tz_cooling_saved_freq;
123 /* sensors(9) related */
124 struct ksensordev sensordev;
125 struct ksensor sensor;
128 /* silence errors after X seconds, try again after Y seconds */
129 #define TZ_SILENCE_ERROR \
130 ((acpi_tz_polling_rate <= 0 ? TZ_POLLRATE : acpi_tz_polling_rate) * 2 + 1)
131 #define TZ_RETRY_ERROR 7200
133 #define TZ_ACTIVE_LEVEL(act) ((act) >= 0 ? (act) : TZ_NUMLEVELS)
135 #define CPUFREQ_MAX_LEVELS 64 /* XXX cpufreq should export this */
137 static int acpi_tz_probe(device_t dev);
138 static int acpi_tz_attach(device_t dev);
139 static int acpi_tz_establish(struct acpi_tz_softc *sc);
140 static void acpi_tz_monitor(void *Context);
141 static void acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
142 static void acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
143 static void acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
145 static void acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
146 static int acpi_tz_polling_sysctl(SYSCTL_HANDLER_ARGS);
147 static int acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
148 static int acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS);
149 static int acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS);
150 static int acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
151 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
153 static void acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
154 static void acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
155 static void acpi_tz_power_profile(void *arg);
156 static void acpi_tz_thread(void *arg);
157 static int acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
158 static int acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
160 static device_method_t acpi_tz_methods[] = {
161 /* Device interface */
162 DEVMETHOD(device_probe, acpi_tz_probe),
163 DEVMETHOD(device_attach, acpi_tz_attach),
168 static driver_t acpi_tz_driver = {
171 sizeof(struct acpi_tz_softc),
174 static char *acpi_tz_tmp_name = "_TMP";
176 static devclass_t acpi_tz_devclass;
177 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, NULL, NULL);
178 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
180 static struct sysctl_ctx_list acpi_tz_sysctl_ctx;
181 static struct sysctl_oid *acpi_tz_sysctl_tree;
183 /* Minimum cooling run time */
184 static int acpi_tz_min_runtime;
185 static int acpi_tz_polling_rate = TZ_POLLRATE;
186 static int acpi_tz_override;
188 /* Timezone polling thread */
189 static struct thread *acpi_tz_td;
190 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
192 static int acpi_tz_cooling_unit = -1;
195 acpi_tz_probe(device_t dev)
199 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
200 device_set_desc(dev, "Thermal Zone");
208 acpi_tz_attach(device_t dev)
210 struct acpi_tz_softc *sc;
211 struct acpi_softc *acpi_sc;
215 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
216 if (device_get_unit(dev) == 0)
217 ACPI_LOCK_INIT(thermal, "acpitz");
219 sc = device_get_softc(dev);
221 sc->tz_handle = acpi_get_handle(dev);
222 sc->tz_requested = TZ_ACTIVE_NONE;
223 sc->tz_active = TZ_ACTIVE_UNKNOWN;
224 sc->tz_thflags = TZ_THFLAG_NONE;
225 sc->tz_cooling_proc = NULL;
226 sc->tz_cooling_proc_running = FALSE;
227 sc->tz_cooling_active = FALSE;
228 sc->tz_cooling_updated = FALSE;
229 sc->tz_cooling_enabled = FALSE;
232 * Parse the current state of the thermal zone and build control
233 * structures. We don't need to worry about interference with the
234 * control thread since we haven't fully attached this device yet.
236 if ((error = acpi_tz_establish(sc)) != 0)
240 * Register for any Notify events sent to this zone.
242 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
243 acpi_tz_notify_handler, sc);
246 * Create our sysctl nodes.
248 * XXX we need a mechanism for adding nodes under ACPI.
250 if (device_get_unit(dev) == 0) {
251 acpi_sc = acpi_device_get_parent_softc(dev);
252 sysctl_ctx_init(&acpi_tz_sysctl_ctx);
253 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
254 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
255 OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
256 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
257 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
258 OID_AUTO, "min_runtime", CTLFLAG_RW,
259 &acpi_tz_min_runtime, 0,
260 "minimum cooling run time in sec");
261 SYSCTL_ADD_PROC(&acpi_tz_sysctl_ctx,
262 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
263 OID_AUTO, "polling_rate", CTLTYPE_INT | CTLFLAG_RW,
264 &acpi_tz_polling_rate, 0, acpi_tz_polling_sysctl,
265 "I", "monitor polling interval in seconds");
266 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
267 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
268 "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
269 "allow override of thermal settings");
271 sysctl_ctx_init(&sc->tz_sysctl_ctx);
272 ksprintf(oidname, "tz%d", device_get_unit(dev));
273 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
274 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
275 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
276 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
277 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD,
278 &sc->tz_temperature, 0, sysctl_handle_int,
279 "IK", "current thermal zone temperature");
280 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
281 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
282 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
283 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
284 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
285 sc, 0, acpi_tz_cooling_sysctl, "I",
286 "enable passive (speed reduction) cooling");
288 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
289 OID_AUTO, "thermal_flags", CTLFLAG_RD,
290 &sc->tz_thflags, 0, "thermal zone flags");
291 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
292 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
293 sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
294 acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
295 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
296 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
297 sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
298 acpi_tz_temp_sysctl, "IK",
299 "too hot temp setpoint (suspend now)");
300 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
301 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
302 sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
303 acpi_tz_temp_sysctl, "IK",
304 "critical temp setpoint (shutdown now)");
305 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
306 OID_AUTO, "_ACx", CTLTYPE_INT | CTLFLAG_RD,
307 &sc->tz_zone.ac, sizeof(sc->tz_zone.ac),
308 sysctl_handle_opaque, "IK", "");
309 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
310 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
311 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
312 acpi_tz_passive_sysctl, "I",
313 "thermal constant 1 for passive cooling");
314 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
315 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
316 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
317 acpi_tz_passive_sysctl, "I",
318 "thermal constant 2 for passive cooling");
319 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
320 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
321 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
322 acpi_tz_passive_sysctl, "I",
323 "thermal sampling period for passive cooling");
326 * Create thread to service all of the thermal zones. Register
327 * our power profile event handler.
329 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
330 acpi_tz_power_profile, sc, 0);
331 if (acpi_tz_td == NULL) {
332 error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_td,
335 device_printf(sc->tz_dev, "could not create thread - %d", error);
341 * Create a thread to handle passive cooling for 1st zone which
342 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
343 * zones manually for now.
345 * XXX We enable only one zone to avoid multiple zones conflict
346 * with each other since cpufreq currently sets all CPUs to the
347 * given frequency whereas it's possible for different thermal
348 * zones to specify independent settings for multiple CPUs.
350 if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
351 sc->tz_cooling_enabled = TRUE;
352 if (sc->tz_cooling_enabled) {
353 error = acpi_tz_cooling_thread_start(sc);
355 sc->tz_cooling_enabled = FALSE;
358 acpi_tz_cooling_unit = device_get_unit(dev);
362 * Flag the event handler for a manual invocation by our timeout.
363 * We defer it like this so that the rest of the subsystem has time
364 * to come up. Don't bother evaluating/printing the temperature at
365 * this point; on many systems it'll be bogus until the EC is running.
367 sc->tz_flags |= TZ_FLAG_GETPROFILE;
369 /* Attach sensors(9). */
370 strlcpy(sc->sensordev.xname, device_get_nameunit(sc->tz_dev),
371 sizeof(sc->sensordev.xname));
373 sc->sensor.type = SENSOR_TEMP;
374 sensor_attach(&sc->sensordev, &sc->sensor);
376 sensordev_install(&sc->sensordev);
380 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
381 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
382 acpi_tz_notify_handler);
383 sysctl_ctx_free(&sc->tz_sysctl_ctx);
385 return_VALUE (error);
389 * Parse the current state of this thermal zone and set up to use it.
391 * Note that we may have previous state, which will have to be discarded.
394 acpi_tz_establish(struct acpi_tz_softc *sc)
400 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
402 /* Erase any existing state. */
403 for (i = 0; i < TZ_NUMLEVELS; i++)
404 if (sc->tz_zone.al[i].Pointer != NULL)
405 AcpiOsFree(sc->tz_zone.al[i].Pointer);
406 if (sc->tz_zone.psl.Pointer != NULL)
407 AcpiOsFree(sc->tz_zone.psl.Pointer);
410 * XXX: We initialize only ACPI_BUFFER to avoid race condition
411 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
413 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
414 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
415 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
417 /* Evaluate thermal zone parameters. */
418 for (i = 0; i < TZ_NUMLEVELS; i++) {
419 ksprintf(nbuf, "_AC%d", i);
420 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
421 ksprintf(nbuf, "_AL%d", i);
422 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
423 sc->tz_zone.al[i].Pointer = NULL;
424 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
425 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
427 /* Should be a package containing a list of power objects */
428 if (obj->Type != ACPI_TYPE_PACKAGE) {
429 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
431 return_VALUE (ENXIO);
435 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
436 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
437 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
438 sc->tz_zone.psl.Pointer = NULL;
439 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
440 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
441 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
442 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
443 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
444 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
447 * Sanity-check the values we've been given.
449 * XXX what do we do about systems that give us the same value for
450 * more than one of these setpoints?
452 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
453 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
454 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
455 for (i = 0; i < TZ_NUMLEVELS; i++)
456 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
461 static char *aclevel_string[] = {
462 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
463 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
466 static __inline const char *
467 acpi_tz_aclevel_string(int active)
469 if (active < -1 || active >= TZ_NUMLEVELS)
470 return (aclevel_string[0]);
472 return (aclevel_string[active + 1]);
476 * Get the current temperature.
479 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
484 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
487 * Silence lookup errors after 10 seconds, then retry every two hours.
489 if (sc->tz_error_time &&
490 time_uptime - sc->tz_error_time > TZ_SILENCE_ERROR) {
491 if (time_uptime - sc->tz_error_time < TZ_RETRY_ERROR)
493 sc->tz_error_time = time_uptime - TZ_SILENCE_ERROR;
496 /* Evaluate the thermal zone's _TMP method. */
497 status = acpi_GetInteger(sc->tz_handle, acpi_tz_tmp_name, &temp);
498 if (ACPI_FAILURE(status)) {
499 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
500 "error fetching current temperature -- %s\n",
501 AcpiFormatException(status));
502 if (sc->tz_error_time == 0)
503 sc->tz_error_time = time_uptime;
507 /* Check it for validity. */
508 acpi_tz_sanity(sc, &temp, acpi_tz_tmp_name);
510 if (sc->tz_error_time == 0)
511 sc->tz_error_time = time_uptime;
515 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
516 sc->tz_temperature = temp;
517 sc->tz_error_time = 0;
519 if(sc->tz_temperature == -1)
520 sc->sensor.flags &= ~SENSOR_FINVALID;
521 sc->sensor.value = sc->tz_temperature * 100000 - 50000;
526 * Evaluate the condition of a thermal zone, take appropriate actions.
529 acpi_tz_monitor(void *Context)
531 struct acpi_tz_softc *sc;
532 struct timespec curtime;
535 int newactive, newflags;
537 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
539 sc = (struct acpi_tz_softc *)Context;
541 /* Get the current temperature. */
542 if (!acpi_tz_get_temperature(sc)) {
543 /* XXX disable zone? go to max cooling? */
546 temp = sc->tz_temperature;
549 * Work out what we ought to be doing right now.
551 * Note that the _ACx levels sort from hot to cold.
553 newactive = TZ_ACTIVE_NONE;
554 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
555 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i])
560 * We are going to get _ACx level down (colder side), but give a guaranteed
561 * minimum cooling run time if requested.
563 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
564 sc->tz_active != TZ_ACTIVE_UNKNOWN &&
565 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
567 getnanotime(&curtime);
568 timespecsub(&curtime, &sc->tz_cooling_started);
569 if (curtime.tv_sec < acpi_tz_min_runtime)
570 newactive = sc->tz_active;
573 /* Handle user override of active mode */
574 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
575 || sc->tz_requested < newactive))
576 newactive = sc->tz_requested;
578 /* update temperature-related flags */
579 newflags = TZ_THFLAG_NONE;
580 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
581 newflags |= TZ_THFLAG_PSV;
582 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
583 newflags |= TZ_THFLAG_HOT;
584 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
585 newflags |= TZ_THFLAG_CRT;
587 /* If the active cooling state has changed, we have to switch things. */
588 if (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
590 * We don't know which cooling device is on or off,
591 * so stop them all, because we now know which
592 * should be on (if any).
594 for (i = 0; i < TZ_NUMLEVELS; i++) {
595 if (sc->tz_zone.al[i].Pointer != NULL) {
596 acpi_ForeachPackageObject(
597 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
598 acpi_tz_switch_cooler_off, sc);
601 /* now we know that all devices are off */
602 sc->tz_active = TZ_ACTIVE_NONE;
605 if (newactive != sc->tz_active) {
606 /* Turn off unneeded cooling devices that are on, if any are */
607 for (i = TZ_ACTIVE_LEVEL(sc->tz_active);
608 i < TZ_ACTIVE_LEVEL(newactive); i++) {
609 acpi_ForeachPackageObject(
610 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
611 acpi_tz_switch_cooler_off, sc);
613 /* Turn on cooling devices that are required, if any are */
614 for (i = TZ_ACTIVE_LEVEL(sc->tz_active) - 1;
615 i >= TZ_ACTIVE_LEVEL(newactive); i--) {
616 acpi_ForeachPackageObject(
617 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
618 acpi_tz_switch_cooler_on, sc);
621 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
622 "switched from %s to %s: %d.%dC\n",
623 acpi_tz_aclevel_string(sc->tz_active),
624 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
625 sc->tz_active = newactive;
626 getnanotime(&sc->tz_cooling_started);
629 /* XXX (de)activate any passive cooling that may be required. */
632 * If the temperature is at _HOT or _CRT, increment our event count.
633 * If it has occurred enough times, shutdown the system. This is
634 * needed because some systems will report an invalid high temperature
635 * for one poll cycle. It is suspected this is due to the embedded
636 * controller timing out. A typical value is 138C for one cycle on
637 * a system that is otherwise 65C.
639 * If we're almost at that threshold, notify the user through devd(8).
641 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
642 sc->tz_validchecks++;
643 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
644 device_printf(sc->tz_dev,
645 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
646 TZ_KELVTOC(sc->tz_temperature));
647 shutdown_nice(RB_POWEROFF);
648 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
649 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
651 sc->tz_validchecks = 0;
653 sc->tz_thflags = newflags;
659 * Given an object, verify that it's a reference to a device of some sort,
660 * and try to switch it off.
663 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
667 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
669 cooler = acpi_GetReference(NULL, obj);
670 if (cooler == NULL) {
671 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
675 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
677 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
683 * Given an object, verify that it's a reference to a device of some sort,
684 * and try to switch it on.
686 * XXX replication of off/on function code is bad.
689 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
691 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
695 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
697 cooler = acpi_GetReference(NULL, obj);
698 if (cooler == NULL) {
699 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
703 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
705 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
706 if (ACPI_FAILURE(status)) {
707 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
708 "failed to activate %s - %s\n", acpi_name(cooler),
709 AcpiFormatException(status));
716 * Read/debug-print a parameter, default it to -1.
719 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
722 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
724 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
727 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
728 acpi_name(sc->tz_handle), node, *data));
735 * Handle sysctl for reading and changing the termal-zone polling rate.
738 acpi_tz_polling_sysctl(SYSCTL_HANDLER_ARGS)
742 val = acpi_tz_polling_rate;
743 error = sysctl_handle_int(oidp, &val, 0, req);
745 /* Error or no new value */
746 if (error != 0 || req->newptr == NULL)
748 if (val < 0 || val > 3600)
751 acpi_tz_polling_rate = val;
757 * Sanity-check a temperature value. Assume that setpoints
758 * should be between 0C and 200C.
761 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
763 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
765 * If the value we are checking is _TMP, warn the user only
766 * once. This avoids spamming messages if, for instance, the
767 * sensor is broken and always returns an invalid temperature.
769 * This is only done for _TMP; other values always emit a
772 if (what != acpi_tz_tmp_name || !sc->tz_insane_tmp_notified) {
773 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
774 what, TZ_KELVTOC(*val));
776 /* Don't warn the user again if the read value doesn't improve. */
777 if (what == acpi_tz_tmp_name)
778 sc->tz_insane_tmp_notified = 1;
784 /* This value is correct. Warn if it's incorrect again. */
785 if (what == acpi_tz_tmp_name)
786 sc->tz_insane_tmp_notified = 0;
790 * Respond to a sysctl on the active state node.
793 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
795 struct acpi_tz_softc *sc;
799 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
800 active = sc->tz_active;
801 error = sysctl_handle_int(oidp, &active, 0, req);
803 /* Error or no new value */
804 if (error != 0 || req->newptr == NULL)
806 if (active < -1 || active >= TZ_NUMLEVELS)
809 /* Set new preferred level and re-switch */
810 sc->tz_requested = active;
811 acpi_tz_signal(sc, 0);
816 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
818 struct acpi_tz_softc *sc;
821 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
822 enabled = sc->tz_cooling_enabled;
823 error = sysctl_handle_int(oidp, &enabled, 0, req);
825 /* Error or no new value */
826 if (error != 0 || req->newptr == NULL)
828 if (enabled != TRUE && enabled != FALSE)
832 if (acpi_tz_cooling_is_available(sc))
833 error = acpi_tz_cooling_thread_start(sc);
839 sc->tz_cooling_enabled = enabled;
844 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
846 struct acpi_tz_softc *sc;
851 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
853 error = sysctl_handle_int(oidp, &temp, 0, req);
855 /* Error or no new value */
856 if (error != 0 || req->newptr == NULL)
859 /* Only allow changing settings if override is set. */
860 if (!acpi_tz_override)
863 /* Check user-supplied value for sanity. */
864 acpi_tz_sanity(sc, &temp, "user-supplied temp");
873 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
875 struct acpi_tz_softc *sc;
880 val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
882 error = sysctl_handle_int(oidp, &val, 0, req);
884 /* Error or no new value */
885 if (error != 0 || req->newptr == NULL)
888 /* Only allow changing settings if override is set. */
889 if (!acpi_tz_override)
897 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
899 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
901 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
904 case TZ_NOTIFY_TEMPERATURE:
905 /* Temperature change occurred */
906 acpi_tz_signal(sc, 0);
908 case TZ_NOTIFY_DEVICES:
909 case TZ_NOTIFY_LEVELS:
910 /* Zone devices/setpoints changed */
911 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
914 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
915 "unknown Notify event 0x%x\n", notify);
919 acpi_UserNotify("Thermal", h, notify);
925 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
928 sc->tz_flags |= flags;
929 ACPI_UNLOCK(thermal);
934 * Notifies can be generated asynchronously but have also been seen to be
935 * triggered by other thermal methods. One system generates a notify of
936 * 0x81 when the fan is turned on or off. Another generates it when _SCP
937 * is called. To handle these situations, we check the zone via
938 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
942 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
945 /* Check the current temperature and take action based on it */
948 /* If requested, get the power profile settings. */
949 if (flags & TZ_FLAG_GETPROFILE)
950 acpi_tz_power_profile(sc);
953 * If requested, check for new devices/setpoints. After finding them,
954 * check if we need to switch fans based on the new values.
956 if (flags & TZ_FLAG_GETSETTINGS) {
957 acpi_tz_establish(sc);
961 /* XXX passive cooling actions? */
965 * System power profile may have changed; fetch and notify the
966 * thermal zone accordingly.
968 * Since this can be called from an arbitrary eventhandler, it needs
969 * to get the ACPI lock itself.
972 acpi_tz_power_profile(void *arg)
975 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
978 state = power_profile_get_state();
979 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
982 /* check that we haven't decided there's no _SCP method */
983 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
985 /* Call _SCP to set the new profile */
986 status = acpi_SetInteger(sc->tz_handle, "_SCP",
987 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
988 if (ACPI_FAILURE(status)) {
989 if (status != AE_NOT_FOUND)
990 ACPI_VPRINT(sc->tz_dev,
991 acpi_device_get_parent_softc(sc->tz_dev),
992 "can't evaluate %s._SCP - %s\n",
993 acpi_name(sc->tz_handle),
994 AcpiFormatException(status));
995 sc->tz_flags |= TZ_FLAG_NO_SCP;
997 /* We have to re-evaluate the entire zone now */
998 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
1004 * Thermal zone monitor thread.
1007 acpi_tz_thread(void *arg)
1012 struct acpi_tz_softc **sc;
1014 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1022 /* If the number of devices has changed, re-evaluate. */
1023 if (devclass_get_count(acpi_tz_devclass) != devcount) {
1025 kfree(devs, M_TEMP);
1028 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
1029 sc = kmalloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
1031 for (i = 0; i < devcount; i++)
1032 sc[i] = device_get_softc(devs[i]);
1035 /* Check for temperature events and act on them. */
1036 for (i = 0; i < devcount; i++) {
1038 flags = sc[i]->tz_flags;
1039 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
1040 ACPI_UNLOCK(thermal);
1041 acpi_tz_timeout(sc[i], flags);
1044 /* If more work to do, don't go to sleep yet. */
1046 for (i = 0; i < devcount; i++) {
1047 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
1052 * Interlocked sleep until signaled or we timeout.
1054 if (i == devcount) {
1055 tsleep_interlock(&acpi_tz_td, 0);
1056 ACPI_UNLOCK(thermal);
1057 tsleep(&acpi_tz_td, 0, "tzpoll",
1058 (acpi_tz_polling_rate <= 0 ? 0 : hz * acpi_tz_polling_rate));
1060 ACPI_UNLOCK(thermal);
1068 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
1073 if (!sc->tz_cooling_updated)
1075 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
1077 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1078 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
1079 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
1080 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
1082 sc->tz_cooling_updated = FALSE;
1087 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1090 struct cf_level *levels;
1091 int num_levels, error, freq, desired_freq, perf, i;
1093 levels = kmalloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1098 * Find the main device, cpufreq0. We don't yet support independent
1099 * CPU frequency control on SMP.
1101 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1106 /* Get the current frequency. */
1107 error = CPUFREQ_GET(dev, &levels[0]);
1110 freq = levels[0].total_set.freq;
1112 /* Get the current available frequency levels. */
1113 num_levels = CPUFREQ_MAX_LEVELS;
1114 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1117 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1121 /* Calculate the desired frequency as a percent of the max frequency. */
1122 perf = 100 * freq / levels[0].total_set.freq - req;
1125 else if (perf > 100)
1127 desired_freq = levels[0].total_set.freq * perf / 100;
1129 if (desired_freq < freq) {
1130 /* Find the closest available frequency, rounding down. */
1131 for (i = 0; i < num_levels; i++)
1132 if (levels[i].total_set.freq <= desired_freq)
1135 /* If we didn't find a relevant setting, use the lowest. */
1136 if (i == num_levels)
1139 /* If we didn't decrease frequency yet, don't increase it. */
1140 if (!sc->tz_cooling_updated) {
1141 sc->tz_cooling_active = FALSE;
1145 /* Use saved cpu frequency as maximum value. */
1146 if (desired_freq > sc->tz_cooling_saved_freq)
1147 desired_freq = sc->tz_cooling_saved_freq;
1149 /* Find the closest available frequency, rounding up. */
1150 for (i = num_levels - 1; i >= 0; i--)
1151 if (levels[i].total_set.freq >= desired_freq)
1154 /* If we didn't find a relevant setting, use the highest. */
1158 /* If we're going to the highest frequency, restore the old setting. */
1159 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1160 error = acpi_tz_cpufreq_restore(sc);
1162 sc->tz_cooling_active = FALSE;
1167 /* If we are going to a new frequency, activate it. */
1168 if (levels[i].total_set.freq != freq) {
1169 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1170 "temperature %d.%dC: %screasing clock speed "
1171 "from %d MHz to %d MHz\n",
1172 TZ_KELVTOC(sc->tz_temperature),
1173 (freq > levels[i].total_set.freq) ? "de" : "in",
1174 freq, levels[i].total_set.freq);
1175 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1176 if (error == 0 && !sc->tz_cooling_updated) {
1177 sc->tz_cooling_saved_freq = freq;
1178 sc->tz_cooling_updated = TRUE;
1184 free(levels, M_TEMP);
1190 * Passive cooling thread; monitors current temperature according to the
1191 * cooling interval and calculates whether to scale back CPU frequency.
1194 acpi_tz_cooling_thread(void *arg)
1196 struct acpi_tz_softc *sc;
1197 int perf, curr_temp, prev_temp;
1202 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1204 sc = (struct acpi_tz_softc *)arg;
1207 prev_temp = sc->tz_temperature;
1208 while (sc->tz_cooling_enabled) {
1209 if (sc->tz_cooling_active)
1210 (void)acpi_tz_get_temperature(sc);
1211 curr_temp = sc->tz_temperature;
1212 if (curr_temp >= sc->tz_zone.psv)
1213 sc->tz_cooling_active = TRUE;
1214 if (sc->tz_cooling_active) {
1215 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1216 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1221 error = acpi_tz_cpufreq_update(sc, perf);
1224 * If error and not simply a higher priority setting was
1225 * active, disable cooling.
1227 if (error != 0 && error != EPERM) {
1228 device_printf(sc->tz_dev,
1229 "failed to set new freq, disabling passive cooling\n");
1230 sc->tz_cooling_enabled = FALSE;
1235 prev_temp = curr_temp;
1236 tsleep(&sc->tz_cooling_proc, 0, "cooling",
1237 hz * sc->tz_zone.tsp / 10);
1239 if (sc->tz_cooling_active) {
1241 acpi_tz_cpufreq_restore(sc);
1243 sc->tz_cooling_active = FALSE;
1245 sc->tz_cooling_proc = NULL;
1247 sc->tz_cooling_proc_running = FALSE;
1248 ACPI_UNLOCK(thermal);
1253 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1254 * all CPUs for us. However, it's possible in the future _PSL will
1255 * reference non-CPU devices so we may want to support it then.
1258 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1260 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1261 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1262 sc->tz_zone.psv != -1);
1266 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1271 if (sc->tz_cooling_proc_running) {
1272 ACPI_UNLOCK(thermal);
1275 sc->tz_cooling_proc_running = TRUE;
1276 ACPI_UNLOCK(thermal);
1278 if (sc->tz_cooling_proc == NULL) {
1279 error = kthread_create(acpi_tz_cooling_thread, sc,
1280 &sc->tz_cooling_proc,
1281 "acpi_cooling%d", device_get_unit(sc->tz_dev));
1283 device_printf(sc->tz_dev, "could not create thread - %d", error);
1285 sc->tz_cooling_proc_running = FALSE;
1286 ACPI_UNLOCK(thermal);