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__);
210 sc = device_get_softc(dev);
212 sc->tz_handle = acpi_get_handle(dev);
213 sc->tz_requested = TZ_ACTIVE_NONE;
214 sc->tz_active = TZ_ACTIVE_UNKNOWN;
215 sc->tz_thflags = TZ_THFLAG_NONE;
216 sc->tz_cooling_proc = NULL;
217 sc->tz_cooling_proc_running = FALSE;
218 sc->tz_cooling_active = FALSE;
219 sc->tz_cooling_updated = FALSE;
220 sc->tz_cooling_enabled = FALSE;
223 * Parse the current state of the thermal zone and build control
224 * structures. We don't need to worry about interference with the
225 * control thread since we haven't fully attached this device yet.
227 if ((error = acpi_tz_establish(sc)) != 0)
231 * Register for any Notify events sent to this zone.
233 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
234 acpi_tz_notify_handler, sc);
237 * Create our sysctl nodes.
239 * XXX we need a mechanism for adding nodes under ACPI.
241 if (device_get_unit(dev) == 0) {
242 acpi_sc = acpi_device_get_parent_softc(dev);
243 sysctl_ctx_init(&acpi_tz_sysctl_ctx);
244 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
245 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
246 OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
247 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
248 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
249 OID_AUTO, "min_runtime", CTLFLAG_RW,
250 &acpi_tz_min_runtime, 0,
251 "minimum cooling run time in sec");
252 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
253 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
254 OID_AUTO, "polling_rate", CTLFLAG_RW,
255 &acpi_tz_polling_rate, 0, "monitor polling interval in seconds");
256 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
257 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
258 "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
259 "allow override of thermal settings");
261 sysctl_ctx_init(&sc->tz_sysctl_ctx);
262 ksprintf(oidname, "tz%d", device_get_unit(dev));
263 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
264 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
265 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
266 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
267 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD,
268 &sc->tz_temperature, 0, sysctl_handle_int,
269 "IK", "current thermal zone temperature");
270 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
271 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
272 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
273 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
274 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
275 sc, 0, 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,
283 sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
284 acpi_tz_temp_sysctl, "IK", "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,
287 sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
288 acpi_tz_temp_sysctl, "IK",
289 "too hot temp setpoint (suspend now)");
290 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
291 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
292 sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
293 acpi_tz_temp_sysctl, "IK",
294 "critical temp setpoint (shutdown now)");
295 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
296 OID_AUTO, "_ACx", CTLTYPE_INT | CTLFLAG_RD,
297 &sc->tz_zone.ac, sizeof(sc->tz_zone.ac),
298 sysctl_handle_opaque, "IK", "");
299 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
300 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
301 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
302 acpi_tz_passive_sysctl, "I",
303 "thermal constant 1 for passive cooling");
304 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
305 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
306 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
307 acpi_tz_passive_sysctl, "I",
308 "thermal constant 2 for passive cooling");
309 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
310 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
311 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
312 acpi_tz_passive_sysctl, "I",
313 "thermal sampling period for passive cooling");
316 * Create thread to service all of the thermal zones. Register
317 * our power profile event handler.
319 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
320 acpi_tz_power_profile, sc, 0);
321 if (acpi_tz_td == NULL) {
322 error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_td,
325 device_printf(sc->tz_dev, "could not create thread - %d", error);
331 * Create a thread to handle passive cooling for 1st zone which
332 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
333 * zones manually for now.
335 * XXX We enable only one zone to avoid multiple zones conflict
336 * with each other since cpufreq currently sets all CPUs to the
337 * given frequency whereas it's possible for different thermal
338 * zones to specify independent settings for multiple CPUs.
340 if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
341 sc->tz_cooling_enabled = TRUE;
342 if (sc->tz_cooling_enabled) {
343 error = acpi_tz_cooling_thread_start(sc);
345 sc->tz_cooling_enabled = FALSE;
348 acpi_tz_cooling_unit = device_get_unit(dev);
352 * Flag the event handler for a manual invocation by our timeout.
353 * We defer it like this so that the rest of the subsystem has time
354 * to come up. Don't bother evaluating/printing the temperature at
355 * this point; on many systems it'll be bogus until the EC is running.
357 sc->tz_flags |= TZ_FLAG_GETPROFILE;
359 /* Attach sensors(9). */
360 strlcpy(sc->sensordev.xname, device_get_nameunit(sc->tz_dev),
361 sizeof(sc->sensordev.xname));
363 sc->sensor.type = SENSOR_TEMP;
364 sensor_attach(&sc->sensordev, &sc->sensor);
366 sensordev_install(&sc->sensordev);
370 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
371 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
372 acpi_tz_notify_handler);
373 sysctl_ctx_free(&sc->tz_sysctl_ctx);
375 return_VALUE (error);
379 * Parse the current state of this thermal zone and set up to use it.
381 * Note that we may have previous state, which will have to be discarded.
384 acpi_tz_establish(struct acpi_tz_softc *sc)
390 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
392 /* Erase any existing state. */
393 for (i = 0; i < TZ_NUMLEVELS; i++)
394 if (sc->tz_zone.al[i].Pointer != NULL)
395 AcpiOsFree(sc->tz_zone.al[i].Pointer);
396 if (sc->tz_zone.psl.Pointer != NULL)
397 AcpiOsFree(sc->tz_zone.psl.Pointer);
400 * XXX: We initialize only ACPI_BUFFER to avoid race condition
401 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
403 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
404 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
405 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
407 /* Evaluate thermal zone parameters. */
408 for (i = 0; i < TZ_NUMLEVELS; i++) {
409 ksprintf(nbuf, "_AC%d", i);
410 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
411 ksprintf(nbuf, "_AL%d", i);
412 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
413 sc->tz_zone.al[i].Pointer = NULL;
414 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
415 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
417 /* Should be a package containing a list of power objects */
418 if (obj->Type != ACPI_TYPE_PACKAGE) {
419 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
421 return_VALUE (ENXIO);
425 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
426 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
427 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
428 sc->tz_zone.psl.Pointer = NULL;
429 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
430 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
431 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
432 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
433 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
434 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
437 * Sanity-check the values we've been given.
439 * XXX what do we do about systems that give us the same value for
440 * more than one of these setpoints?
442 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
443 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
444 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
445 for (i = 0; i < TZ_NUMLEVELS; i++)
446 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
451 static char *aclevel_string[] = {
452 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
453 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
456 static __inline const char *
457 acpi_tz_aclevel_string(int active)
459 if (active < -1 || active >= TZ_NUMLEVELS)
460 return (aclevel_string[0]);
462 return (aclevel_string[active + 1]);
466 * Get the current temperature.
469 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
474 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
476 /* Evaluate the thermal zone's _TMP method. */
477 status = acpi_GetInteger(sc->tz_handle, acpi_tz_tmp_name, &temp);
478 if (ACPI_FAILURE(status)) {
479 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
480 "error fetching current temperature -- %s\n",
481 AcpiFormatException(status));
485 /* Check it for validity. */
486 acpi_tz_sanity(sc, &temp, acpi_tz_tmp_name);
490 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
491 sc->tz_temperature = temp;
493 if(sc->tz_temperature == -1)
494 sc->sensor.flags &= ~SENSOR_FINVALID;
495 sc->sensor.value = sc->tz_temperature * 100000 - 50000;
500 * Evaluate the condition of a thermal zone, take appropriate actions.
503 acpi_tz_monitor(void *Context)
505 struct acpi_tz_softc *sc;
506 struct timespec curtime;
509 int newactive, newflags;
511 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
513 sc = (struct acpi_tz_softc *)Context;
515 /* Get the current temperature. */
516 if (!acpi_tz_get_temperature(sc)) {
517 /* XXX disable zone? go to max cooling? */
520 temp = sc->tz_temperature;
523 * Work out what we ought to be doing right now.
525 * Note that the _ACx levels sort from hot to cold.
527 newactive = TZ_ACTIVE_NONE;
528 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
529 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i])
534 * We are going to get _ACx level down (colder side), but give a guaranteed
535 * minimum cooling run time if requested.
537 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
538 sc->tz_active != TZ_ACTIVE_UNKNOWN &&
539 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
541 getnanotime(&curtime);
542 timespecsub(&curtime, &sc->tz_cooling_started);
543 if (curtime.tv_sec < acpi_tz_min_runtime)
544 newactive = sc->tz_active;
547 /* Handle user override of active mode */
548 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
549 || sc->tz_requested < newactive))
550 newactive = sc->tz_requested;
552 /* update temperature-related flags */
553 newflags = TZ_THFLAG_NONE;
554 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
555 newflags |= TZ_THFLAG_PSV;
556 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
557 newflags |= TZ_THFLAG_HOT;
558 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
559 newflags |= TZ_THFLAG_CRT;
561 /* If the active cooling state has changed, we have to switch things. */
562 if (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
564 * We don't know which cooling device is on or off,
565 * so stop them all, because we now know which
566 * should be on (if any).
568 for (i = 0; i < TZ_NUMLEVELS; i++) {
569 if (sc->tz_zone.al[i].Pointer != NULL) {
570 acpi_ForeachPackageObject(
571 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
572 acpi_tz_switch_cooler_off, sc);
575 /* now we know that all devices are off */
576 sc->tz_active = TZ_ACTIVE_NONE;
579 if (newactive != sc->tz_active) {
580 /* Turn off unneeded cooling devices that are on, if any are */
581 for (i = TZ_ACTIVE_LEVEL(sc->tz_active);
582 i < TZ_ACTIVE_LEVEL(newactive); i++) {
583 acpi_ForeachPackageObject(
584 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
585 acpi_tz_switch_cooler_off, sc);
587 /* Turn on cooling devices that are required, if any are */
588 for (i = TZ_ACTIVE_LEVEL(sc->tz_active) - 1;
589 i >= TZ_ACTIVE_LEVEL(newactive); i--) {
590 acpi_ForeachPackageObject(
591 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
592 acpi_tz_switch_cooler_on, sc);
595 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
596 "switched from %s to %s: %d.%dC\n",
597 acpi_tz_aclevel_string(sc->tz_active),
598 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
599 sc->tz_active = newactive;
600 getnanotime(&sc->tz_cooling_started);
603 /* XXX (de)activate any passive cooling that may be required. */
606 * If the temperature is at _HOT or _CRT, increment our event count.
607 * If it has occurred enough times, shutdown the system. This is
608 * needed because some systems will report an invalid high temperature
609 * for one poll cycle. It is suspected this is due to the embedded
610 * controller timing out. A typical value is 138C for one cycle on
611 * a system that is otherwise 65C.
613 * If we're almost at that threshold, notify the user through devd(8).
615 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
616 sc->tz_validchecks++;
617 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
618 device_printf(sc->tz_dev,
619 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
620 TZ_KELVTOC(sc->tz_temperature));
621 shutdown_nice(RB_POWEROFF);
622 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
623 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
625 sc->tz_validchecks = 0;
627 sc->tz_thflags = newflags;
633 * Given an object, verify that it's a reference to a device of some sort,
634 * and try to switch it off.
637 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
641 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
643 cooler = acpi_GetReference(NULL, obj);
644 if (cooler == NULL) {
645 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
649 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
651 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
657 * Given an object, verify that it's a reference to a device of some sort,
658 * and try to switch it on.
660 * XXX replication of off/on function code is bad.
663 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
665 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
669 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
671 cooler = acpi_GetReference(NULL, obj);
672 if (cooler == NULL) {
673 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
677 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
679 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
680 if (ACPI_FAILURE(status)) {
681 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
682 "failed to activate %s - %s\n", acpi_name(cooler),
683 AcpiFormatException(status));
690 * Read/debug-print a parameter, default it to -1.
693 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
696 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
698 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
701 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
702 acpi_name(sc->tz_handle), node, *data));
709 * Sanity-check a temperature value. Assume that setpoints
710 * should be between 0C and 200C.
713 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
715 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
717 * If the value we are checking is _TMP, warn the user only
718 * once. This avoids spamming messages if, for instance, the
719 * sensor is broken and always returns an invalid temperature.
721 * This is only done for _TMP; other values always emit a
724 if (what != acpi_tz_tmp_name || !sc->tz_insane_tmp_notified) {
725 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
726 what, TZ_KELVTOC(*val));
728 /* Don't warn the user again if the read value doesn't improve. */
729 if (what == acpi_tz_tmp_name)
730 sc->tz_insane_tmp_notified = 1;
736 /* This value is correct. Warn if it's incorrect again. */
737 if (what == acpi_tz_tmp_name)
738 sc->tz_insane_tmp_notified = 0;
742 * Respond to a sysctl on the active state node.
745 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
747 struct acpi_tz_softc *sc;
751 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
752 active = sc->tz_active;
753 error = sysctl_handle_int(oidp, &active, 0, req);
755 /* Error or no new value */
756 if (error != 0 || req->newptr == NULL)
758 if (active < -1 || active >= TZ_NUMLEVELS)
761 /* Set new preferred level and re-switch */
762 sc->tz_requested = active;
763 acpi_tz_signal(sc, 0);
768 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
770 struct acpi_tz_softc *sc;
773 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
774 enabled = sc->tz_cooling_enabled;
775 error = sysctl_handle_int(oidp, &enabled, 0, req);
777 /* Error or no new value */
778 if (error != 0 || req->newptr == NULL)
780 if (enabled != TRUE && enabled != FALSE)
784 if (acpi_tz_cooling_is_available(sc))
785 error = acpi_tz_cooling_thread_start(sc);
791 sc->tz_cooling_enabled = enabled;
796 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
798 struct acpi_tz_softc *sc;
803 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
805 error = sysctl_handle_int(oidp, &temp, 0, req);
807 /* Error or no new value */
808 if (error != 0 || req->newptr == NULL)
811 /* Only allow changing settings if override is set. */
812 if (!acpi_tz_override)
815 /* Check user-supplied value for sanity. */
816 acpi_tz_sanity(sc, &temp, "user-supplied temp");
825 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
827 struct acpi_tz_softc *sc;
832 val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
834 error = sysctl_handle_int(oidp, &val, 0, req);
836 /* Error or no new value */
837 if (error != 0 || req->newptr == NULL)
840 /* Only allow changing settings if override is set. */
841 if (!acpi_tz_override)
849 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
851 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
853 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
856 case TZ_NOTIFY_TEMPERATURE:
857 /* Temperature change occurred */
858 acpi_tz_signal(sc, 0);
860 case TZ_NOTIFY_DEVICES:
861 case TZ_NOTIFY_LEVELS:
862 /* Zone devices/setpoints changed */
863 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
866 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
867 "unknown Notify event 0x%x\n", notify);
871 acpi_UserNotify("Thermal", h, notify);
877 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
880 sc->tz_flags |= flags;
881 ACPI_UNLOCK(thermal);
886 * Notifies can be generated asynchronously but have also been seen to be
887 * triggered by other thermal methods. One system generates a notify of
888 * 0x81 when the fan is turned on or off. Another generates it when _SCP
889 * is called. To handle these situations, we check the zone via
890 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
894 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
897 /* Check the current temperature and take action based on it */
900 /* If requested, get the power profile settings. */
901 if (flags & TZ_FLAG_GETPROFILE)
902 acpi_tz_power_profile(sc);
905 * If requested, check for new devices/setpoints. After finding them,
906 * check if we need to switch fans based on the new values.
908 if (flags & TZ_FLAG_GETSETTINGS) {
909 acpi_tz_establish(sc);
913 /* XXX passive cooling actions? */
917 * System power profile may have changed; fetch and notify the
918 * thermal zone accordingly.
920 * Since this can be called from an arbitrary eventhandler, it needs
921 * to get the ACPI lock itself.
924 acpi_tz_power_profile(void *arg)
927 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
930 state = power_profile_get_state();
931 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
934 /* check that we haven't decided there's no _SCP method */
935 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
937 /* Call _SCP to set the new profile */
938 status = acpi_SetInteger(sc->tz_handle, "_SCP",
939 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
940 if (ACPI_FAILURE(status)) {
941 if (status != AE_NOT_FOUND)
942 ACPI_VPRINT(sc->tz_dev,
943 acpi_device_get_parent_softc(sc->tz_dev),
944 "can't evaluate %s._SCP - %s\n",
945 acpi_name(sc->tz_handle),
946 AcpiFormatException(status));
947 sc->tz_flags |= TZ_FLAG_NO_SCP;
949 /* We have to re-evaluate the entire zone now */
950 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
956 * Thermal zone monitor thread.
959 acpi_tz_thread(void *arg)
964 struct acpi_tz_softc **sc;
966 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
974 /* If the number of devices has changed, re-evaluate. */
975 if (devclass_get_count(acpi_tz_devclass) != devcount) {
980 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
981 sc = kmalloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
983 for (i = 0; i < devcount; i++)
984 sc[i] = device_get_softc(devs[i]);
987 /* Check for temperature events and act on them. */
988 for (i = 0; i < devcount; i++) {
990 flags = sc[i]->tz_flags;
991 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
992 ACPI_UNLOCK(thermal);
993 acpi_tz_timeout(sc[i], flags);
996 /* If more work to do, don't go to sleep yet. */
998 for (i = 0; i < devcount; i++) {
999 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
1004 * Interlocked sleep until signaled or we timeout.
1006 if (i == devcount) {
1007 tsleep_interlock(&acpi_tz_td, 0);
1008 ACPI_UNLOCK(thermal);
1009 tsleep(&acpi_tz_td, 0, "tzpoll", hz * acpi_tz_polling_rate);
1011 ACPI_UNLOCK(thermal);
1019 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
1024 if (!sc->tz_cooling_updated)
1026 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
1028 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1029 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
1030 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
1031 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
1033 sc->tz_cooling_updated = FALSE;
1038 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1041 struct cf_level *levels;
1042 int num_levels, error, freq, desired_freq, perf, i;
1044 levels = kmalloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1049 * Find the main device, cpufreq0. We don't yet support independent
1050 * CPU frequency control on SMP.
1052 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1057 /* Get the current frequency. */
1058 error = CPUFREQ_GET(dev, &levels[0]);
1061 freq = levels[0].total_set.freq;
1063 /* Get the current available frequency levels. */
1064 num_levels = CPUFREQ_MAX_LEVELS;
1065 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1068 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1072 /* Calculate the desired frequency as a percent of the max frequency. */
1073 perf = 100 * freq / levels[0].total_set.freq - req;
1076 else if (perf > 100)
1078 desired_freq = levels[0].total_set.freq * perf / 100;
1080 if (desired_freq < freq) {
1081 /* Find the closest available frequency, rounding down. */
1082 for (i = 0; i < num_levels; i++)
1083 if (levels[i].total_set.freq <= desired_freq)
1086 /* If we didn't find a relevant setting, use the lowest. */
1087 if (i == num_levels)
1090 /* If we didn't decrease frequency yet, don't increase it. */
1091 if (!sc->tz_cooling_updated) {
1092 sc->tz_cooling_active = FALSE;
1096 /* Use saved cpu frequency as maximum value. */
1097 if (desired_freq > sc->tz_cooling_saved_freq)
1098 desired_freq = sc->tz_cooling_saved_freq;
1100 /* Find the closest available frequency, rounding up. */
1101 for (i = num_levels - 1; i >= 0; i--)
1102 if (levels[i].total_set.freq >= desired_freq)
1105 /* If we didn't find a relevant setting, use the highest. */
1109 /* If we're going to the highest frequency, restore the old setting. */
1110 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1111 error = acpi_tz_cpufreq_restore(sc);
1113 sc->tz_cooling_active = FALSE;
1118 /* If we are going to a new frequency, activate it. */
1119 if (levels[i].total_set.freq != freq) {
1120 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1121 "temperature %d.%dC: %screasing clock speed "
1122 "from %d MHz to %d MHz\n",
1123 TZ_KELVTOC(sc->tz_temperature),
1124 (freq > levels[i].total_set.freq) ? "de" : "in",
1125 freq, levels[i].total_set.freq);
1126 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1127 if (error == 0 && !sc->tz_cooling_updated) {
1128 sc->tz_cooling_saved_freq = freq;
1129 sc->tz_cooling_updated = TRUE;
1135 free(levels, M_TEMP);
1141 * Passive cooling thread; monitors current temperature according to the
1142 * cooling interval and calculates whether to scale back CPU frequency.
1145 acpi_tz_cooling_thread(void *arg)
1147 struct acpi_tz_softc *sc;
1148 int perf, curr_temp, prev_temp;
1153 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1155 sc = (struct acpi_tz_softc *)arg;
1158 prev_temp = sc->tz_temperature;
1159 while (sc->tz_cooling_enabled) {
1160 if (sc->tz_cooling_active)
1161 (void)acpi_tz_get_temperature(sc);
1162 curr_temp = sc->tz_temperature;
1163 if (curr_temp >= sc->tz_zone.psv)
1164 sc->tz_cooling_active = TRUE;
1165 if (sc->tz_cooling_active) {
1166 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1167 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1172 error = acpi_tz_cpufreq_update(sc, perf);
1175 * If error and not simply a higher priority setting was
1176 * active, disable cooling.
1178 if (error != 0 && error != EPERM) {
1179 device_printf(sc->tz_dev,
1180 "failed to set new freq, disabling passive cooling\n");
1181 sc->tz_cooling_enabled = FALSE;
1186 prev_temp = curr_temp;
1187 tsleep(&sc->tz_cooling_proc, 0, "cooling",
1188 hz * sc->tz_zone.tsp / 10);
1190 if (sc->tz_cooling_active) {
1192 acpi_tz_cpufreq_restore(sc);
1194 sc->tz_cooling_active = FALSE;
1196 sc->tz_cooling_proc = NULL;
1198 sc->tz_cooling_proc_running = FALSE;
1199 ACPI_UNLOCK(thermal);
1204 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1205 * all CPUs for us. However, it's possible in the future _PSL will
1206 * reference non-CPU devices so we may want to support it then.
1209 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1211 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1212 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1213 sc->tz_zone.psv != -1);
1217 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1222 if (sc->tz_cooling_proc_running) {
1223 ACPI_UNLOCK(thermal);
1226 sc->tz_cooling_proc_running = TRUE;
1227 ACPI_UNLOCK(thermal);
1229 if (sc->tz_cooling_proc == NULL) {
1230 error = kthread_create(acpi_tz_cooling_thread, sc,
1231 &sc->tz_cooling_proc,
1232 "acpi_cooling%d", device_get_unit(sc->tz_dev));
1234 device_printf(sc->tz_dev, "could not create thread - %d", error);
1236 sc->tz_cooling_proc_running = FALSE;
1237 ACPI_UNLOCK(thermal);