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: src/sys/dev/acpica/acpi_thermal.c,v 1.73 2009/08/20 19:17:53 jhb
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>
39 #include <sys/reboot.h>
40 #include <sys/sysctl.h>
41 #include <sys/unistd.h>
42 #include <sys/power.h>
43 #include <sys/sensors.h>
48 #include <dev/acpica5/acpivar.h>
50 /* Hooks for the ACPI CA debugging infrastructure */
51 #define _COMPONENT ACPI_THERMAL
52 ACPI_MODULE_NAME("THERMAL")
55 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
57 #define TZ_NOTIFY_TEMPERATURE 0x80 /* Temperature changed. */
58 #define TZ_NOTIFY_LEVELS 0x81 /* Cooling levels changed. */
59 #define TZ_NOTIFY_DEVICES 0x82 /* Device lists changed. */
60 #define TZ_NOTIFY_CRITICAL 0xcc /* Fake notify that _CRT/_HOT reached. */
62 /* Check for temperature changes every 10 seconds by default */
63 #define TZ_POLLRATE 10
65 /* Make sure the reported temperature is valid for this number of polls. */
66 #define TZ_VALIDCHECKS 3
68 /* Notify the user we will be shutting down in one more poll cycle. */
69 #define TZ_NOTIFYCOUNT (TZ_VALIDCHECKS - 1)
71 #define abs(x) ( x < 0 ? -x : x )
73 /* ACPI spec defines this */
74 #define TZ_NUMLEVELS 10
77 ACPI_BUFFER al[TZ_NUMLEVELS];
88 struct acpi_tz_softc {
90 ACPI_HANDLE tz_handle; /*Thermal zone handle*/
91 int tz_temperature; /*Current temperature*/
92 int tz_active; /*Current active cooling*/
93 #define TZ_ACTIVE_NONE -1
94 #define TZ_ACTIVE_UNKNOWN -2
95 int tz_requested; /*Minimum active cooling*/
96 int tz_thflags; /*Current temp-related flags*/
97 #define TZ_THFLAG_NONE 0
98 #define TZ_THFLAG_PSV (1<<0)
99 #define TZ_THFLAG_HOT (1<<2)
100 #define TZ_THFLAG_CRT (1<<3)
102 #define TZ_FLAG_NO_SCP (1<<0) /*No _SCP method*/
103 #define TZ_FLAG_GETPROFILE (1<<1) /*Get power_profile in timeout*/
104 #define TZ_FLAG_GETSETTINGS (1<<2) /*Get devs/setpoints*/
105 struct timespec tz_cooling_started;
106 /*Current cooling starting time*/
108 struct sysctl_ctx_list tz_sysctl_ctx;
109 struct sysctl_oid *tz_sysctl_tree;
110 eventhandler_tag tz_event;
112 struct acpi_tz_zone tz_zone; /*Thermal zone parameters*/
115 /* passive cooling */
116 struct 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 CPUFREQ_MAX_LEVELS 64 /* XXX cpufreq should export this */
129 static int acpi_tz_probe(device_t dev);
130 static int acpi_tz_attach(device_t dev);
131 static int acpi_tz_establish(struct acpi_tz_softc *sc);
132 static void acpi_tz_monitor(void *Context);
133 static void acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
134 static void acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
135 static void acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
137 static void acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
138 static int acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
139 static int acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS);
140 static int acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS);
141 static int acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
142 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
144 static void acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
145 static void acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
146 static void acpi_tz_power_profile(void *arg);
147 static void acpi_tz_thread(void *arg);
148 static int acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
149 static int acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
151 static device_method_t acpi_tz_methods[] = {
152 /* Device interface */
153 DEVMETHOD(device_probe, acpi_tz_probe),
154 DEVMETHOD(device_attach, acpi_tz_attach),
159 static driver_t acpi_tz_driver = {
162 sizeof(struct acpi_tz_softc),
165 static devclass_t acpi_tz_devclass;
166 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0);
167 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
169 static struct sysctl_ctx_list acpi_tz_sysctl_ctx;
170 static struct sysctl_oid *acpi_tz_sysctl_tree;
172 /* Minimum cooling run time */
173 static int acpi_tz_min_runtime;
174 static int acpi_tz_polling_rate = TZ_POLLRATE;
175 static int acpi_tz_override;
177 /* Timezone polling thread */
178 static struct thread *acpi_tz_td;
179 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
181 static int acpi_tz_cooling_unit = -1;
184 acpi_tz_probe(device_t dev)
188 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
189 device_set_desc(dev, "Thermal Zone");
197 acpi_tz_attach(device_t dev)
199 struct acpi_tz_softc *sc;
200 struct acpi_softc *acpi_sc;
204 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
206 sc = device_get_softc(dev);
208 sc->tz_handle = acpi_get_handle(dev);
209 sc->tz_requested = TZ_ACTIVE_NONE;
210 sc->tz_active = TZ_ACTIVE_UNKNOWN;
211 sc->tz_thflags = TZ_THFLAG_NONE;
212 sc->tz_cooling_proc = NULL;
213 sc->tz_cooling_proc_running = FALSE;
214 sc->tz_cooling_active = FALSE;
215 sc->tz_cooling_updated = FALSE;
216 sc->tz_cooling_enabled = FALSE;
219 * Parse the current state of the thermal zone and build control
220 * structures. We don't need to worry about interference with the
221 * control thread since we haven't fully attached this device yet.
223 if ((error = acpi_tz_establish(sc)) != 0)
227 * Register for any Notify events sent to this zone.
229 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
230 acpi_tz_notify_handler, sc);
233 * Create our sysctl nodes.
235 * XXX we need a mechanism for adding nodes under ACPI.
237 if (device_get_unit(dev) == 0) {
238 acpi_sc = acpi_device_get_parent_softc(dev);
239 sysctl_ctx_init(&acpi_tz_sysctl_ctx);
240 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
241 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
242 OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
243 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
244 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
245 OID_AUTO, "min_runtime", CTLFLAG_RW,
246 &acpi_tz_min_runtime, 0,
247 "minimum cooling run time in sec");
248 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
249 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
250 OID_AUTO, "polling_rate", CTLFLAG_RW,
251 &acpi_tz_polling_rate, 0, "monitor polling rate");
252 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
253 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
254 "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
255 "allow override of thermal settings");
257 sysctl_ctx_init(&sc->tz_sysctl_ctx);
258 ksprintf(oidname, "tz%d", device_get_unit(dev));
259 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
260 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
261 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
262 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
263 OID_AUTO, "temperature", CTLFLAG_RD, &sc->tz_temperature,
264 sizeof(sc->tz_temperature), "IK",
265 "current thermal zone temperature");
266 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
267 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
268 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
269 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
270 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
271 sc, 0, acpi_tz_cooling_sysctl, "I",
272 "enable passive (speed reduction) cooling");
274 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
275 OID_AUTO, "thermal_flags", CTLFLAG_RD,
276 &sc->tz_thflags, 0, "thermal zone flags");
277 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
278 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
279 sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
280 acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
281 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
282 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
283 sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
284 acpi_tz_temp_sysctl, "IK",
285 "too hot temp setpoint (suspend now)");
286 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
287 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
288 sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
289 acpi_tz_temp_sysctl, "IK",
290 "critical temp setpoint (shutdown now)");
291 SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
292 OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac,
293 sizeof(sc->tz_zone.ac), "IK", "");
294 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
295 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
296 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
297 acpi_tz_passive_sysctl, "I",
298 "thermal constant 1 for passive cooling");
299 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
300 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
301 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
302 acpi_tz_passive_sysctl, "I",
303 "thermal constant 2 for passive cooling");
304 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
305 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
306 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
307 acpi_tz_passive_sysctl, "I",
308 "thermal sampling period for passive cooling");
311 * Create thread to service all of the thermal zones. Register
312 * our power profile event handler.
314 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
315 acpi_tz_power_profile, sc, 0);
316 if (acpi_tz_td == NULL) {
317 error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_td,
318 RFHIGHPID, 0, "acpi_thermal");
320 device_printf(sc->tz_dev, "could not create thread - %d", error);
326 * Create a thread to handle passive cooling for 1st zone which
327 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
328 * zones manually for now.
330 * XXX We enable only one zone to avoid multiple zones conflict
331 * with each other since cpufreq currently sets all CPUs to the
332 * given frequency whereas it's possible for different thermal
333 * zones to specify independent settings for multiple CPUs.
335 if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
336 sc->tz_cooling_enabled = TRUE;
337 if (sc->tz_cooling_enabled) {
338 error = acpi_tz_cooling_thread_start(sc);
340 sc->tz_cooling_enabled = FALSE;
343 acpi_tz_cooling_unit = device_get_unit(dev);
347 * Flag the event handler for a manual invocation by our timeout.
348 * We defer it like this so that the rest of the subsystem has time
349 * to come up. Don't bother evaluating/printing the temperature at
350 * this point; on many systems it'll be bogus until the EC is running.
352 sc->tz_flags |= TZ_FLAG_GETPROFILE;
354 /* Attach sensors(9). */
355 strlcpy(sc->sensordev.xname, device_get_nameunit(sc->tz_dev),
356 sizeof(sc->sensordev.xname));
358 sc->sensor.type = SENSOR_TEMP;
359 sensor_attach(&sc->sensordev, &sc->sensor);
361 sensordev_install(&sc->sensordev);
365 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
366 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
367 acpi_tz_notify_handler);
368 sysctl_ctx_free(&sc->tz_sysctl_ctx);
370 return_VALUE (error);
374 * Parse the current state of this thermal zone and set up to use it.
376 * Note that we may have previous state, which will have to be discarded.
379 acpi_tz_establish(struct acpi_tz_softc *sc)
385 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
387 /* Erase any existing state. */
388 for (i = 0; i < TZ_NUMLEVELS; i++)
389 if (sc->tz_zone.al[i].Pointer != NULL)
390 AcpiOsFree(sc->tz_zone.al[i].Pointer);
391 if (sc->tz_zone.psl.Pointer != NULL)
392 AcpiOsFree(sc->tz_zone.psl.Pointer);
395 * XXX: We initialize only ACPI_BUFFER to avoid race condition
396 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
398 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
399 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
400 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
402 /* Evaluate thermal zone parameters. */
403 for (i = 0; i < TZ_NUMLEVELS; i++) {
404 ksprintf(nbuf, "_AC%d", i);
405 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
406 ksprintf(nbuf, "_AL%d", i);
407 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
408 sc->tz_zone.al[i].Pointer = NULL;
409 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
410 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
412 /* Should be a package containing a list of power objects */
413 if (obj->Type != ACPI_TYPE_PACKAGE) {
414 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
416 return_VALUE (ENXIO);
420 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
421 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
422 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
423 sc->tz_zone.psl.Pointer = NULL;
424 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
425 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
426 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
427 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
428 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
429 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
432 * Sanity-check the values we've been given.
434 * XXX what do we do about systems that give us the same value for
435 * more than one of these setpoints?
437 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
438 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
439 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
440 for (i = 0; i < TZ_NUMLEVELS; i++)
441 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
446 static char *aclevel_string[] = {
447 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
448 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
451 static __inline const char *
452 acpi_tz_aclevel_string(int active)
454 if (active < -1 || active >= TZ_NUMLEVELS)
455 return (aclevel_string[0]);
457 return (aclevel_string[active + 1]);
461 * Get the current temperature.
464 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
468 static char *tmp_name = "_TMP";
470 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
472 /* Evaluate the thermal zone's _TMP method. */
473 status = acpi_GetInteger(sc->tz_handle, tmp_name, &temp);
474 if (ACPI_FAILURE(status)) {
475 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
476 "error fetching current temperature -- %s\n",
477 AcpiFormatException(status));
481 /* Check it for validity. */
482 acpi_tz_sanity(sc, &temp, tmp_name);
486 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
487 sc->tz_temperature = temp;
489 if(sc->tz_temperature == -1)
490 sc->sensor.flags &= ~SENSOR_FINVALID;
491 sc->sensor.value = sc->tz_temperature * 100000 - 50000;
496 * Evaluate the condition of a thermal zone, take appropriate actions.
499 acpi_tz_monitor(void *Context)
501 struct acpi_tz_softc *sc;
502 struct timespec curtime;
505 int newactive, newflags;
507 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
509 sc = (struct acpi_tz_softc *)Context;
511 /* Get the current temperature. */
512 if (!acpi_tz_get_temperature(sc)) {
513 /* XXX disable zone? go to max cooling? */
516 temp = sc->tz_temperature;
519 * Work out what we ought to be doing right now.
521 * Note that the _ACx levels sort from hot to cold.
523 newactive = TZ_ACTIVE_NONE;
524 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
525 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i]) {
527 if (sc->tz_active != newactive) {
528 ACPI_VPRINT(sc->tz_dev,
529 acpi_device_get_parent_softc(sc->tz_dev),
530 "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i,
531 TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i]));
537 * We are going to get _ACx level down (colder side), but give a guaranteed
538 * minimum cooling run time if requested.
540 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
541 sc->tz_active != TZ_ACTIVE_UNKNOWN &&
542 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
544 getnanotime(&curtime);
545 timespecsub(&curtime, &sc->tz_cooling_started);
546 if (curtime.tv_sec < acpi_tz_min_runtime)
547 newactive = sc->tz_active;
550 /* Handle user override of active mode */
551 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
552 || sc->tz_requested < newactive))
553 newactive = sc->tz_requested;
555 /* update temperature-related flags */
556 newflags = TZ_THFLAG_NONE;
557 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
558 newflags |= TZ_THFLAG_PSV;
559 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
560 newflags |= TZ_THFLAG_HOT;
561 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
562 newflags |= TZ_THFLAG_CRT;
564 /* If the active cooling state has changed, we have to switch things. */
565 if (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
567 * We don't know which cooling device is on or off,
568 * so stop them all, because we now know which
569 * should be on (if any).
571 for (i = 0; i < TZ_NUMLEVELS; i++) {
572 if (sc->tz_zone.al[i].Pointer != NULL) {
573 acpi_ForeachPackageObject(
574 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
575 acpi_tz_switch_cooler_off, sc);
578 /* now we know that all devices are off */
579 sc->tz_active = TZ_ACTIVE_NONE;
582 if (newactive != sc->tz_active) {
583 /* Turn off the cooling devices that are on, if any are */
584 if (sc->tz_active != TZ_ACTIVE_NONE)
585 acpi_ForeachPackageObject(
586 (ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer,
587 acpi_tz_switch_cooler_off, sc);
589 /* Turn on cooling devices that are required, if any are */
590 if (newactive != TZ_ACTIVE_NONE) {
591 acpi_ForeachPackageObject(
592 (ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer,
593 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)) {
716 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
717 what, TZ_KELVTOC(*val));
723 * Respond to a sysctl on the active state node.
726 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
728 struct acpi_tz_softc *sc;
732 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
733 active = sc->tz_active;
734 error = sysctl_handle_int(oidp, &active, 0, req);
736 /* Error or no new value */
737 if (error != 0 || req->newptr == NULL)
739 if (active < -1 || active >= TZ_NUMLEVELS)
742 /* Set new preferred level and re-switch */
743 sc->tz_requested = active;
744 acpi_tz_signal(sc, 0);
749 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
751 struct acpi_tz_softc *sc;
754 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
755 enabled = sc->tz_cooling_enabled;
756 error = sysctl_handle_int(oidp, &enabled, 0, req);
758 /* Error or no new value */
759 if (error != 0 || req->newptr == NULL)
761 if (enabled != TRUE && enabled != FALSE)
765 if (acpi_tz_cooling_is_available(sc))
766 error = acpi_tz_cooling_thread_start(sc);
772 sc->tz_cooling_enabled = enabled;
777 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
779 struct acpi_tz_softc *sc;
784 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
786 error = sysctl_handle_int(oidp, &temp, 0, req);
788 /* Error or no new value */
789 if (error != 0 || req->newptr == NULL)
792 /* Only allow changing settings if override is set. */
793 if (!acpi_tz_override)
796 /* Check user-supplied value for sanity. */
797 acpi_tz_sanity(sc, &temp, "user-supplied temp");
806 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
808 struct acpi_tz_softc *sc;
813 val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
815 error = sysctl_handle_int(oidp, &val, 0, req);
817 /* Error or no new value */
818 if (error != 0 || req->newptr == NULL)
821 /* Only allow changing settings if override is set. */
822 if (!acpi_tz_override)
830 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
832 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
834 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
837 case TZ_NOTIFY_TEMPERATURE:
838 /* Temperature change occurred */
839 acpi_tz_signal(sc, 0);
841 case TZ_NOTIFY_DEVICES:
842 case TZ_NOTIFY_LEVELS:
843 /* Zone devices/setpoints changed */
844 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
847 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
848 "unknown Notify event 0x%x\n", notify);
852 acpi_UserNotify("Thermal", h, notify);
858 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
861 sc->tz_flags |= flags;
862 ACPI_UNLOCK(thermal);
867 * Notifies can be generated asynchronously but have also been seen to be
868 * triggered by other thermal methods. One system generates a notify of
869 * 0x81 when the fan is turned on or off. Another generates it when _SCP
870 * is called. To handle these situations, we check the zone via
871 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
875 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
878 /* Check the current temperature and take action based on it */
881 /* If requested, get the power profile settings. */
882 if (flags & TZ_FLAG_GETPROFILE)
883 acpi_tz_power_profile(sc);
886 * If requested, check for new devices/setpoints. After finding them,
887 * check if we need to switch fans based on the new values.
889 if (flags & TZ_FLAG_GETSETTINGS) {
890 acpi_tz_establish(sc);
894 /* XXX passive cooling actions? */
898 * System power profile may have changed; fetch and notify the
899 * thermal zone accordingly.
901 * Since this can be called from an arbitrary eventhandler, it needs
902 * to get the ACPI lock itself.
905 acpi_tz_power_profile(void *arg)
908 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
911 state = power_profile_get_state();
912 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
915 /* check that we haven't decided there's no _SCP method */
916 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
918 /* Call _SCP to set the new profile */
919 status = acpi_SetInteger(sc->tz_handle, "_SCP",
920 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
921 if (ACPI_FAILURE(status)) {
922 if (status != AE_NOT_FOUND)
923 ACPI_VPRINT(sc->tz_dev,
924 acpi_device_get_parent_softc(sc->tz_dev),
925 "can't evaluate %s._SCP - %s\n",
926 acpi_name(sc->tz_handle),
927 AcpiFormatException(status));
928 sc->tz_flags |= TZ_FLAG_NO_SCP;
930 /* We have to re-evaluate the entire zone now */
931 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
937 * Thermal zone monitor thread.
940 acpi_tz_thread(void *arg)
945 struct acpi_tz_softc **sc;
947 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
954 /* If the number of devices has changed, re-evaluate. */
955 if (devclass_get_count(acpi_tz_devclass) != devcount) {
960 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
961 sc = kmalloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
963 for (i = 0; i < devcount; i++)
964 sc[i] = device_get_softc(devs[i]);
967 /* Check for temperature events and act on them. */
968 for (i = 0; i < devcount; i++) {
970 flags = sc[i]->tz_flags;
971 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
972 ACPI_UNLOCK(thermal);
973 acpi_tz_timeout(sc[i], flags);
976 /* If more work to do, don't go to sleep yet. */
978 for (i = 0; i < devcount; i++) {
979 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
984 * Interlocked sleep until signaled or we timeout.
987 tsleep_interlock(&acpi_tz_td, 0);
988 ACPI_UNLOCK(thermal);
989 tsleep(&acpi_tz_td, 0, "tzpoll", hz * acpi_tz_polling_rate);
991 ACPI_UNLOCK(thermal);
998 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
1003 if (!sc->tz_cooling_updated)
1005 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
1007 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1008 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
1009 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
1010 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
1012 sc->tz_cooling_updated = FALSE;
1017 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1020 struct cf_level *levels;
1021 int num_levels, error, freq, desired_freq, perf, i;
1023 levels = kmalloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1028 * Find the main device, cpufreq0. We don't yet support independent
1029 * CPU frequency control on SMP.
1031 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1036 /* Get the current frequency. */
1037 error = CPUFREQ_GET(dev, &levels[0]);
1040 freq = levels[0].total_set.freq;
1042 /* Get the current available frequency levels. */
1043 num_levels = CPUFREQ_MAX_LEVELS;
1044 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1047 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1051 /* Calculate the desired frequency as a percent of the max frequency. */
1052 perf = 100 * freq / levels[0].total_set.freq - req;
1055 else if (perf > 100)
1057 desired_freq = levels[0].total_set.freq * perf / 100;
1059 if (desired_freq < freq) {
1060 /* Find the closest available frequency, rounding down. */
1061 for (i = 0; i < num_levels; i++)
1062 if (levels[i].total_set.freq <= desired_freq)
1065 /* If we didn't find a relevant setting, use the lowest. */
1066 if (i == num_levels)
1069 /* If we didn't decrease frequency yet, don't increase it. */
1070 if (!sc->tz_cooling_updated) {
1071 sc->tz_cooling_active = FALSE;
1075 /* Use saved cpu frequency as maximum value. */
1076 if (desired_freq > sc->tz_cooling_saved_freq)
1077 desired_freq = sc->tz_cooling_saved_freq;
1079 /* Find the closest available frequency, rounding up. */
1080 for (i = num_levels - 1; i >= 0; i--)
1081 if (levels[i].total_set.freq >= desired_freq)
1084 /* If we didn't find a relevant setting, use the highest. */
1088 /* If we're going to the highest frequency, restore the old setting. */
1089 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1090 error = acpi_tz_cpufreq_restore(sc);
1092 sc->tz_cooling_active = FALSE;
1097 /* If we are going to a new frequency, activate it. */
1098 if (levels[i].total_set.freq != freq) {
1099 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1100 "temperature %d.%dC: %screasing clock speed "
1101 "from %d MHz to %d MHz\n",
1102 TZ_KELVTOC(sc->tz_temperature),
1103 (freq > levels[i].total_set.freq) ? "de" : "in",
1104 freq, levels[i].total_set.freq);
1105 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1106 if (error == 0 && !sc->tz_cooling_updated) {
1107 sc->tz_cooling_saved_freq = freq;
1108 sc->tz_cooling_updated = TRUE;
1114 free(levels, M_TEMP);
1120 * Passive cooling thread; monitors current temperature according to the
1121 * cooling interval and calculates whether to scale back CPU frequency.
1124 acpi_tz_cooling_thread(void *arg)
1126 struct acpi_tz_softc *sc;
1127 int perf, curr_temp, prev_temp;
1132 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1134 sc = (struct acpi_tz_softc *)arg;
1136 prev_temp = sc->tz_temperature;
1137 while (sc->tz_cooling_enabled) {
1138 if (sc->tz_cooling_active)
1139 (void)acpi_tz_get_temperature(sc);
1140 curr_temp = sc->tz_temperature;
1141 if (curr_temp >= sc->tz_zone.psv)
1142 sc->tz_cooling_active = TRUE;
1143 if (sc->tz_cooling_active) {
1144 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1145 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1150 error = acpi_tz_cpufreq_update(sc, perf);
1153 * If error and not simply a higher priority setting was
1154 * active, disable cooling.
1156 if (error != 0 && error != EPERM) {
1157 device_printf(sc->tz_dev,
1158 "failed to set new freq, disabling passive cooling\n");
1159 sc->tz_cooling_enabled = FALSE;
1164 prev_temp = curr_temp;
1165 tsleep(&sc->tz_cooling_proc, 0, "cooling",
1166 hz * sc->tz_zone.tsp / 10);
1168 if (sc->tz_cooling_active) {
1170 acpi_tz_cpufreq_restore(sc);
1172 sc->tz_cooling_active = FALSE;
1174 sc->tz_cooling_proc = NULL;
1176 sc->tz_cooling_proc_running = FALSE;
1177 ACPI_UNLOCK(thermal);
1182 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1183 * all CPUs for us. However, it's possible in the future _PSL will
1184 * reference non-CPU devices so we may want to support it then.
1187 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1189 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1190 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1191 sc->tz_zone.psv != -1);
1195 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1201 if (sc->tz_cooling_proc_running) {
1202 ACPI_UNLOCK(thermal);
1205 sc->tz_cooling_proc_running = TRUE;
1206 ACPI_UNLOCK(thermal);
1208 if (sc->tz_cooling_proc == NULL) {
1209 ksnprintf(name, sizeof(name), "acpi_cooling%d",
1210 device_get_unit(sc->tz_dev));
1211 error = kthread_create(acpi_tz_cooling_thread, sc,
1212 &sc->tz_cooling_proc, RFHIGHPID, 0, name);
1214 device_printf(sc->tz_dev, "could not create thread - %d", error);
1216 sc->tz_cooling_proc_running = FALSE;
1217 ACPI_UNLOCK(thermal);