Sync ACPICA with Intel's version 20190215.
[dragonfly.git] / sys / dev / acpica / acpi_thermal.c
blob12a91aa436290a9270e4b75dc8a3b0321d2dd29d
1 /*-
2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
25 * SUCH DAMAGE.
27 * $FreeBSD: head/sys/dev/acpica/acpi_thermal.c 255077 2013-08-30 19:21:12Z dumbbell $
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/kthread.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/proc.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 "acpi.h"
45 #include "accommon.h"
47 #include <dev/acpica/acpivar.h>
49 /* Hooks for the ACPICA debugging infrastructure */
50 #define _COMPONENT ACPI_THERMAL
51 ACPI_MODULE_NAME("THERMAL")
53 #define TZ_ZEROC 2732
54 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
56 #define TZ_NOTIFY_TEMPERATURE 0x80 /* Temperature changed. */
57 #define TZ_NOTIFY_LEVELS 0x81 /* Cooling levels changed. */
58 #define TZ_NOTIFY_DEVICES 0x82 /* Device lists changed. */
59 #define TZ_NOTIFY_CRITICAL 0xcc /* Fake notify that _CRT/_HOT reached. */
61 /* Check for temperature changes every 10 seconds by default */
62 #define TZ_POLLRATE 10
64 /* Make sure the reported temperature is valid for this number of polls. */
65 #define TZ_VALIDCHECKS 3
67 /* Notify the user we will be shutting down in one more poll cycle. */
68 #define TZ_NOTIFYCOUNT (TZ_VALIDCHECKS - 1)
70 /* ACPI spec defines this */
71 #define TZ_NUMLEVELS 10
72 struct acpi_tz_zone {
73 int ac[TZ_NUMLEVELS];
74 ACPI_BUFFER al[TZ_NUMLEVELS];
75 int crt;
76 int hot;
77 ACPI_BUFFER psl;
78 int psv;
79 int tc1;
80 int tc2;
81 int tsp;
82 int tzp;
85 struct acpi_tz_softc {
86 device_t tz_dev;
87 ACPI_HANDLE tz_handle; /*Thermal zone handle*/
88 int tz_temperature; /*Current temperature*/
89 int tz_active; /*Current active cooling*/
90 #define TZ_ACTIVE_NONE -1
91 #define TZ_ACTIVE_UNKNOWN -2
92 int tz_requested; /*Minimum active cooling*/
93 int tz_thflags; /*Current temp-related flags*/
94 #define TZ_THFLAG_NONE 0
95 #define TZ_THFLAG_PSV (1<<0)
96 #define TZ_THFLAG_HOT (1<<2)
97 #define TZ_THFLAG_CRT (1<<3)
98 int tz_flags;
99 #define TZ_FLAG_NO_SCP (1<<0) /*No _SCP method*/
100 #define TZ_FLAG_GETPROFILE (1<<1) /*Get power_profile in timeout*/
101 #define TZ_FLAG_GETSETTINGS (1<<2) /*Get devs/setpoints*/
102 struct timespec tz_cooling_started;
103 /*Current cooling starting time*/
105 struct sysctl_ctx_list tz_sysctl_ctx;
106 struct sysctl_oid *tz_sysctl_tree;
107 eventhandler_tag tz_event;
109 struct acpi_tz_zone tz_zone; /*Thermal zone parameters*/
110 time_t tz_error_time; /*Lookup error timestamp*/
111 int tz_validchecks;
112 int tz_insane_tmp_notified;
114 /* passive cooling */
115 struct thread *tz_cooling_proc;
116 int tz_cooling_proc_running;
117 int tz_cooling_enabled;
118 int tz_cooling_active;
119 int tz_cooling_updated;
120 int tz_cooling_saved_freq;
121 /* sensors(9) related */
122 struct ksensordev sensordev;
123 struct ksensor sensor;
126 /* silence errors after X seconds, try again after Y seconds */
127 #define TZ_SILENCE_ERROR \
128 ((acpi_tz_polling_rate <= 0 ? TZ_POLLRATE : acpi_tz_polling_rate) * 2 + 1)
129 #define TZ_RETRY_ERROR 7200
131 #define TZ_ACTIVE_LEVEL(act) ((act) >= 0 ? (act) : TZ_NUMLEVELS)
133 #define CPUFREQ_MAX_LEVELS 64 /* XXX cpufreq should export this */
135 static int acpi_tz_probe(device_t dev);
136 static int acpi_tz_attach(device_t dev);
137 static int acpi_tz_establish(struct acpi_tz_softc *sc);
138 static void acpi_tz_monitor(void *Context);
139 static void acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
140 static void acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
141 static void acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
142 int *data);
143 static void acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
144 static int acpi_tz_polling_sysctl(SYSCTL_HANDLER_ARGS);
145 static int acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
146 static int acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS);
147 static int acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS);
148 static int acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
149 static void acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
150 void *context);
151 static void acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
152 static void acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
153 static void acpi_tz_power_profile(void *arg);
154 static void acpi_tz_thread(void *arg);
155 static int acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
156 static int acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
158 static device_method_t acpi_tz_methods[] = {
159 /* Device interface */
160 DEVMETHOD(device_probe, acpi_tz_probe),
161 DEVMETHOD(device_attach, acpi_tz_attach),
163 DEVMETHOD_END
166 static driver_t acpi_tz_driver = {
167 "acpi_tz",
168 acpi_tz_methods,
169 sizeof(struct acpi_tz_softc),
170 .gpri = KOBJ_GPRI_ACPI
173 static char *acpi_tz_tmp_name = "_TMP";
175 static devclass_t acpi_tz_devclass;
176 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, NULL, NULL);
177 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
179 static struct sysctl_ctx_list acpi_tz_sysctl_ctx;
180 static struct sysctl_oid *acpi_tz_sysctl_tree;
182 /* Minimum cooling run time */
183 static int acpi_tz_min_runtime;
184 static int acpi_tz_polling_rate = TZ_POLLRATE;
185 static int acpi_tz_override;
187 /* Timezone polling thread */
188 static struct thread *acpi_tz_td;
189 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
191 static int acpi_tz_cooling_unit = -1;
193 static int
194 acpi_tz_probe(device_t dev)
196 int result;
198 if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
199 device_set_desc(dev, "Thermal Zone");
200 result = -10;
201 } else
202 result = ENXIO;
203 return (result);
206 static int
207 acpi_tz_attach(device_t dev)
209 struct acpi_tz_softc *sc;
210 struct acpi_softc *acpi_sc;
211 int error;
212 char oidname[8];
214 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
215 if (device_get_unit(dev) == 0)
216 ACPI_LOCK_INIT(thermal, "acpitz");
218 ACPI_LOCK(thermal);
220 sc = device_get_softc(dev);
221 sc->tz_dev = dev;
222 sc->tz_handle = acpi_get_handle(dev);
223 sc->tz_requested = TZ_ACTIVE_NONE;
224 sc->tz_active = TZ_ACTIVE_UNKNOWN;
225 sc->tz_thflags = TZ_THFLAG_NONE;
226 sc->tz_cooling_proc = NULL;
227 sc->tz_cooling_proc_running = FALSE;
228 sc->tz_cooling_active = FALSE;
229 sc->tz_cooling_updated = FALSE;
230 sc->tz_cooling_enabled = FALSE;
233 * Parse the current state of the thermal zone and build control
234 * structures. We don't need to worry about interference with the
235 * control thread since we haven't fully attached this device yet.
237 if ((error = acpi_tz_establish(sc)) != 0) {
238 ACPI_UNLOCK(thermal);
239 return (error);
243 * Register for any Notify events sent to this zone.
245 AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
246 acpi_tz_notify_handler, sc);
249 * Create our sysctl nodes.
251 * XXX we need a mechanism for adding nodes under ACPI.
253 if (device_get_unit(dev) == 0) {
254 acpi_sc = acpi_device_get_parent_softc(dev);
255 sysctl_ctx_init(&acpi_tz_sysctl_ctx);
256 acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
257 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
258 OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
259 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
260 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
261 OID_AUTO, "min_runtime", CTLFLAG_RW,
262 &acpi_tz_min_runtime, 0,
263 "minimum cooling run time in sec");
264 SYSCTL_ADD_PROC(&acpi_tz_sysctl_ctx,
265 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
266 OID_AUTO, "polling_rate", CTLTYPE_INT | CTLFLAG_RW,
267 &acpi_tz_polling_rate, 0, acpi_tz_polling_sysctl,
268 "I", "monitor polling interval in seconds");
269 SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
270 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
271 "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
272 "allow override of thermal settings");
274 sysctl_ctx_init(&sc->tz_sysctl_ctx);
275 ksprintf(oidname, "tz%d", device_get_unit(dev));
276 sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
277 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
278 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
279 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
280 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD,
281 &sc->tz_temperature, 0, sysctl_handle_int,
282 "IK", "current thermal zone temperature");
283 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
284 OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
285 sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
286 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
287 OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
288 sc, 0, acpi_tz_cooling_sysctl, "I",
289 "enable passive (speed reduction) cooling");
291 SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
292 OID_AUTO, "thermal_flags", CTLFLAG_RD,
293 &sc->tz_thflags, 0, "thermal zone flags");
294 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
295 OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
296 sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
297 acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
298 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
299 OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
300 sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
301 acpi_tz_temp_sysctl, "IK",
302 "too hot temp setpoint (suspend now)");
303 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
304 OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
305 sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
306 acpi_tz_temp_sysctl, "IK",
307 "critical temp setpoint (shutdown now)");
308 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
309 OID_AUTO, "_ACx", CTLTYPE_INT | CTLFLAG_RD,
310 &sc->tz_zone.ac, sizeof(sc->tz_zone.ac),
311 sysctl_handle_opaque, "IK", "");
312 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
313 OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
314 sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
315 acpi_tz_passive_sysctl, "I",
316 "thermal constant 1 for passive cooling");
317 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
318 OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
319 sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
320 acpi_tz_passive_sysctl, "I",
321 "thermal constant 2 for passive cooling");
322 SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
323 OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
324 sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
325 acpi_tz_passive_sysctl, "I",
326 "thermal sampling period for passive cooling");
329 * Create thread to service all of the thermal zones. Register
330 * our power profile event handler.
332 sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
333 acpi_tz_power_profile, sc, 0);
334 if (acpi_tz_td == NULL) {
335 error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_td,
336 "acpi_thermal");
337 if (error != 0) {
338 device_printf(sc->tz_dev, "could not create thread - %d", error);
339 goto out;
344 * Create a thread to handle passive cooling for 1st zone which
345 * has _PSV, _TSP, _TC1 and _TC2. Users can enable it for other
346 * zones manually for now.
348 * XXX We enable only one zone to avoid multiple zones conflict
349 * with each other since cpufreq currently sets all CPUs to the
350 * given frequency whereas it's possible for different thermal
351 * zones to specify independent settings for multiple CPUs.
353 if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
354 sc->tz_cooling_enabled = TRUE;
355 if (sc->tz_cooling_enabled) {
356 error = acpi_tz_cooling_thread_start(sc);
357 if (error != 0) {
358 sc->tz_cooling_enabled = FALSE;
359 goto out;
361 acpi_tz_cooling_unit = device_get_unit(dev);
365 * Flag the event handler for a manual invocation by our timeout.
366 * We defer it like this so that the rest of the subsystem has time
367 * to come up. Don't bother evaluating/printing the temperature at
368 * this point; on many systems it'll be bogus until the EC is running.
370 sc->tz_flags |= TZ_FLAG_GETPROFILE;
372 /* Attach sensors(9). */
373 strlcpy(sc->sensordev.xname, device_get_nameunit(sc->tz_dev),
374 sizeof(sc->sensordev.xname));
376 sc->sensor.type = SENSOR_TEMP;
377 sensor_attach(&sc->sensordev, &sc->sensor);
379 sensordev_install(&sc->sensordev);
381 out:
382 if (error != 0) {
383 EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
384 AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
385 acpi_tz_notify_handler);
386 sysctl_ctx_free(&sc->tz_sysctl_ctx);
388 ACPI_UNLOCK(thermal);
390 return_VALUE (error);
394 * Parse the current state of this thermal zone and set up to use it.
396 * Note that we may have previous state, which will have to be discarded.
398 static int
399 acpi_tz_establish(struct acpi_tz_softc *sc)
401 ACPI_OBJECT *obj;
402 int i;
403 char nbuf[8];
405 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
407 /* Erase any existing state. */
408 for (i = 0; i < TZ_NUMLEVELS; i++)
409 if (sc->tz_zone.al[i].Pointer != NULL)
410 AcpiOsFree(sc->tz_zone.al[i].Pointer);
411 if (sc->tz_zone.psl.Pointer != NULL)
412 AcpiOsFree(sc->tz_zone.psl.Pointer);
415 * XXX: We initialize only ACPI_BUFFER to avoid race condition
416 * with passive cooling thread which refers psv, tc1, tc2 and tsp.
418 bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
419 bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
420 bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
422 /* Evaluate thermal zone parameters. */
423 for (i = 0; i < TZ_NUMLEVELS; i++) {
424 ksprintf(nbuf, "_AC%d", i);
425 acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
426 ksprintf(nbuf, "_AL%d", i);
427 sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
428 sc->tz_zone.al[i].Pointer = NULL;
429 AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
430 obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
431 if (obj != NULL) {
432 /* Should be a package containing a list of power objects */
433 if (obj->Type != ACPI_TYPE_PACKAGE) {
434 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
435 nbuf, obj->Type);
436 return_VALUE (ENXIO);
440 acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
441 acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
442 sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
443 sc->tz_zone.psl.Pointer = NULL;
444 AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
445 acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
446 acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
447 acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
448 acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
449 acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
452 * Sanity-check the values we've been given.
454 * XXX what do we do about systems that give us the same value for
455 * more than one of these setpoints?
457 acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
458 acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
459 acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
460 for (i = 0; i < TZ_NUMLEVELS; i++)
461 acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
463 return_VALUE (0);
466 static char *aclevel_string[] = {
467 "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
468 "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
471 static __inline const char *
472 acpi_tz_aclevel_string(int active)
474 if (active < -1 || active >= TZ_NUMLEVELS)
475 return (aclevel_string[0]);
477 return (aclevel_string[active + 1]);
481 * Get the current temperature.
483 static int
484 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
486 int temp;
487 ACPI_STATUS status;
489 ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
492 * Silence lookup errors after 10 seconds, then retry every two hours.
494 if (sc->tz_error_time &&
495 time_uptime - sc->tz_error_time > TZ_SILENCE_ERROR) {
496 if (time_uptime - sc->tz_error_time < TZ_RETRY_ERROR)
497 return (FALSE);
498 sc->tz_error_time = time_uptime - TZ_SILENCE_ERROR;
501 /* Evaluate the thermal zone's _TMP method. */
502 status = acpi_GetInteger(sc->tz_handle, acpi_tz_tmp_name, &temp);
503 if (ACPI_FAILURE(status)) {
504 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
505 "error fetching current temperature -- %s\n",
506 AcpiFormatException(status));
507 if (sc->tz_error_time == 0)
508 sc->tz_error_time = time_uptime;
509 return (FALSE);
512 /* Check it for validity. */
513 acpi_tz_sanity(sc, &temp, acpi_tz_tmp_name);
514 if (temp == -1) {
515 if (sc->tz_error_time == 0)
516 sc->tz_error_time = time_uptime;
517 return (FALSE);
520 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
521 sc->tz_temperature = temp;
522 sc->tz_error_time = 0;
523 /* Update sensor */
524 if(sc->tz_temperature == -1)
525 sc->sensor.flags &= ~SENSOR_FINVALID;
526 sc->sensor.value = sc->tz_temperature * 100000 - 50000;
527 return (TRUE);
531 * Evaluate the condition of a thermal zone, take appropriate actions.
533 static void
534 acpi_tz_monitor(void *Context)
536 struct acpi_tz_softc *sc;
537 struct timespec curtime;
538 int temp;
539 int i;
540 int newactive, newflags;
542 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
544 sc = (struct acpi_tz_softc *)Context;
546 /* Get the current temperature. */
547 if (!acpi_tz_get_temperature(sc)) {
548 /* XXX disable zone? go to max cooling? */
549 return_VOID;
551 temp = sc->tz_temperature;
554 * Work out what we ought to be doing right now.
556 * Note that the _ACx levels sort from hot to cold.
558 newactive = TZ_ACTIVE_NONE;
559 for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
560 if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i])
561 newactive = i;
565 * We are going to get _ACx level down (colder side), but give a guaranteed
566 * minimum cooling run time if requested.
568 if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
569 sc->tz_active != TZ_ACTIVE_UNKNOWN &&
570 (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
572 getnanotime(&curtime);
573 timespecsub(&curtime, &sc->tz_cooling_started);
574 if (curtime.tv_sec < acpi_tz_min_runtime)
575 newactive = sc->tz_active;
578 /* Handle user override of active mode */
579 if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
580 || sc->tz_requested < newactive))
581 newactive = sc->tz_requested;
583 /* update temperature-related flags */
584 newflags = TZ_THFLAG_NONE;
585 if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
586 newflags |= TZ_THFLAG_PSV;
587 if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
588 newflags |= TZ_THFLAG_HOT;
589 if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
590 newflags |= TZ_THFLAG_CRT;
592 /* If the active cooling state has changed, we have to switch things. */
593 if (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
595 * We don't know which cooling device is on or off,
596 * so stop them all, because we now know which
597 * should be on (if any).
599 for (i = 0; i < TZ_NUMLEVELS; i++) {
600 if (sc->tz_zone.al[i].Pointer != NULL) {
601 acpi_ForeachPackageObject(
602 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
603 acpi_tz_switch_cooler_off, sc);
606 /* now we know that all devices are off */
607 sc->tz_active = TZ_ACTIVE_NONE;
610 if (newactive != sc->tz_active) {
611 /* Turn off unneeded cooling devices that are on, if any are */
612 for (i = TZ_ACTIVE_LEVEL(sc->tz_active);
613 i < TZ_ACTIVE_LEVEL(newactive); i++) {
614 acpi_ForeachPackageObject(
615 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
616 acpi_tz_switch_cooler_off, sc);
618 /* Turn on cooling devices that are required, if any are */
619 for (i = TZ_ACTIVE_LEVEL(sc->tz_active) - 1;
620 i >= TZ_ACTIVE_LEVEL(newactive); i--) {
621 acpi_ForeachPackageObject(
622 (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
623 acpi_tz_switch_cooler_on, sc);
626 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
627 "switched from %s to %s: %d.%dC\n",
628 acpi_tz_aclevel_string(sc->tz_active),
629 acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
630 sc->tz_active = newactive;
631 getnanotime(&sc->tz_cooling_started);
634 /* XXX (de)activate any passive cooling that may be required. */
637 * If the temperature is at _HOT or _CRT, increment our event count.
638 * If it has occurred enough times, shutdown the system. This is
639 * needed because some systems will report an invalid high temperature
640 * for one poll cycle. It is suspected this is due to the embedded
641 * controller timing out. A typical value is 138C for one cycle on
642 * a system that is otherwise 65C.
644 * If we're almost at that threshold, notify the user through devd(8).
646 if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
647 sc->tz_validchecks++;
648 if (sc->tz_validchecks == TZ_VALIDCHECKS) {
649 device_printf(sc->tz_dev,
650 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
651 TZ_KELVTOC(sc->tz_temperature));
652 shutdown_nice(RB_POWEROFF);
653 } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
654 acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
655 } else {
656 sc->tz_validchecks = 0;
658 sc->tz_thflags = newflags;
660 return_VOID;
664 * Given an object, verify that it's a reference to a device of some sort,
665 * and try to switch it off.
667 static void
668 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
670 ACPI_HANDLE cooler;
672 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
674 cooler = acpi_GetReference(NULL, obj);
675 if (cooler == NULL) {
676 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
677 return_VOID;
680 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
681 acpi_name(cooler)));
682 acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
684 return_VOID;
688 * Given an object, verify that it's a reference to a device of some sort,
689 * and try to switch it on.
691 * XXX replication of off/on function code is bad.
693 static void
694 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
696 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
697 ACPI_HANDLE cooler;
698 ACPI_STATUS status;
700 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
702 cooler = acpi_GetReference(NULL, obj);
703 if (cooler == NULL) {
704 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
705 return_VOID;
708 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
709 acpi_name(cooler)));
710 status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
711 if (ACPI_FAILURE(status)) {
712 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
713 "failed to activate %s - %s\n", acpi_name(cooler),
714 AcpiFormatException(status));
717 return_VOID;
721 * Read/debug-print a parameter, default it to -1.
723 static void
724 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
727 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
729 if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
730 *data = -1;
731 } else {
732 ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
733 acpi_name(sc->tz_handle), node, *data));
736 return_VOID;
740 * Handle sysctl for reading and changing the thermal-zone polling rate.
742 static int
743 acpi_tz_polling_sysctl(SYSCTL_HANDLER_ARGS)
745 int val, error;
747 val = acpi_tz_polling_rate;
748 error = sysctl_handle_int(oidp, &val, 0, req);
750 /* Error or no new value */
751 if (error != 0 || req->newptr == NULL)
752 return (error);
753 if (val < 0 || val > 3600)
754 return (EINVAL);
756 acpi_tz_polling_rate = val;
757 wakeup(&acpi_tz_td);
758 return (error);
762 * Sanity-check a temperature value. Assume that setpoints
763 * should be between 0C and 200C.
765 static void
766 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
768 if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
770 * If the value we are checking is _TMP, warn the user only
771 * once. This avoids spamming messages if, for instance, the
772 * sensor is broken and always returns an invalid temperature.
774 * This is only done for _TMP; other values always emit a
775 * warning.
777 if (what != acpi_tz_tmp_name || !sc->tz_insane_tmp_notified) {
778 device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
779 what, TZ_KELVTOC(*val));
781 /* Don't warn the user again if the read value doesn't improve. */
782 if (what == acpi_tz_tmp_name)
783 sc->tz_insane_tmp_notified = 1;
785 *val = -1;
786 return;
789 /* This value is correct. Warn if it's incorrect again. */
790 if (what == acpi_tz_tmp_name)
791 sc->tz_insane_tmp_notified = 0;
795 * Respond to a sysctl on the active state node.
797 static int
798 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
800 struct acpi_tz_softc *sc;
801 int active;
802 int error;
804 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
805 active = sc->tz_active;
806 error = sysctl_handle_int(oidp, &active, 0, req);
808 /* Error or no new value */
809 if (error != 0 || req->newptr == NULL)
810 return (error);
811 if (active < -1 || active >= TZ_NUMLEVELS)
812 return (EINVAL);
814 /* Set new preferred level and re-switch */
815 sc->tz_requested = active;
816 acpi_tz_signal(sc, 0);
817 return (0);
820 static int
821 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
823 struct acpi_tz_softc *sc;
824 int enabled, error;
826 sc = (struct acpi_tz_softc *)oidp->oid_arg1;
827 enabled = sc->tz_cooling_enabled;
828 error = sysctl_handle_int(oidp, &enabled, 0, req);
830 /* Error or no new value */
831 if (error != 0 || req->newptr == NULL)
832 return (error);
833 if (enabled != TRUE && enabled != FALSE)
834 return (EINVAL);
836 if (enabled) {
837 if (acpi_tz_cooling_is_available(sc))
838 error = acpi_tz_cooling_thread_start(sc);
839 else
840 error = ENODEV;
841 if (error)
842 enabled = FALSE;
844 sc->tz_cooling_enabled = enabled;
845 return (error);
848 static int
849 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
851 struct acpi_tz_softc *sc;
852 int temp, *temp_ptr;
853 int error;
855 sc = oidp->oid_arg1;
856 temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
857 temp = *temp_ptr;
858 error = sysctl_handle_int(oidp, &temp, 0, req);
860 /* Error or no new value */
861 if (error != 0 || req->newptr == NULL)
862 return (error);
864 /* Only allow changing settings if override is set. */
865 if (!acpi_tz_override)
866 return (EPERM);
868 /* Check user-supplied value for sanity. */
869 acpi_tz_sanity(sc, &temp, "user-supplied temp");
870 if (temp == -1)
871 return (EINVAL);
873 *temp_ptr = temp;
874 return (0);
877 static int
878 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
880 struct acpi_tz_softc *sc;
881 int val, *val_ptr;
882 int error;
884 sc = oidp->oid_arg1;
885 val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
886 val = *val_ptr;
887 error = sysctl_handle_int(oidp, &val, 0, req);
889 /* Error or no new value */
890 if (error != 0 || req->newptr == NULL)
891 return (error);
893 /* Only allow changing settings if override is set. */
894 if (!acpi_tz_override)
895 return (EPERM);
897 *val_ptr = val;
898 return (0);
901 static void
902 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
904 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)context;
906 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
908 switch (notify) {
909 case TZ_NOTIFY_TEMPERATURE:
910 /* Temperature change occurred */
911 acpi_tz_signal(sc, 0);
912 break;
913 case TZ_NOTIFY_DEVICES:
914 case TZ_NOTIFY_LEVELS:
915 /* Zone devices/setpoints changed */
916 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
917 break;
918 default:
919 device_printf(sc->tz_dev, "unknown notify: %#x\n", notify);
920 break;
923 acpi_UserNotify("Thermal", h, notify);
925 return_VOID;
928 static void
929 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
931 ACPI_LOCK(thermal);
932 sc->tz_flags |= flags;
933 ACPI_UNLOCK(thermal);
934 wakeup(&acpi_tz_td);
938 * Notifies can be generated asynchronously but have also been seen to be
939 * triggered by other thermal methods. One system generates a notify of
940 * 0x81 when the fan is turned on or off. Another generates it when _SCP
941 * is called. To handle these situations, we check the zone via
942 * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
943 * policy.
945 static void
946 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
949 /* Check the current temperature and take action based on it */
950 acpi_tz_monitor(sc);
952 /* If requested, get the power profile settings. */
953 if (flags & TZ_FLAG_GETPROFILE)
954 acpi_tz_power_profile(sc);
957 * If requested, check for new devices/setpoints. After finding them,
958 * check if we need to switch fans based on the new values.
960 if (flags & TZ_FLAG_GETSETTINGS) {
961 acpi_tz_establish(sc);
962 acpi_tz_monitor(sc);
965 /* XXX passive cooling actions? */
969 * System power profile may have changed; fetch and notify the
970 * thermal zone accordingly.
972 * Since this can be called from an arbitrary eventhandler, it needs
973 * to get the ACPI lock itself.
975 static void
976 acpi_tz_power_profile(void *arg)
978 ACPI_STATUS status;
979 struct acpi_tz_softc *sc = (struct acpi_tz_softc *)arg;
980 int state;
982 state = power_profile_get_state();
983 if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
984 return;
986 /* check that we haven't decided there's no _SCP method */
987 if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
989 /* Call _SCP to set the new profile */
990 status = acpi_SetInteger(sc->tz_handle, "_SCP",
991 (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
992 if (ACPI_FAILURE(status)) {
993 if (status != AE_NOT_FOUND)
994 ACPI_VPRINT(sc->tz_dev,
995 acpi_device_get_parent_softc(sc->tz_dev),
996 "can't evaluate %s._SCP - %s\n",
997 acpi_name(sc->tz_handle),
998 AcpiFormatException(status));
999 sc->tz_flags |= TZ_FLAG_NO_SCP;
1000 } else {
1001 /* We have to re-evaluate the entire zone now */
1002 acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
1008 * Thermal zone monitor thread.
1010 static void
1011 acpi_tz_thread(void *arg)
1013 device_t *devs;
1014 int devcount, i;
1015 int flags;
1016 struct acpi_tz_softc **sc;
1018 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1020 devs = NULL;
1021 devcount = 0;
1022 sc = NULL;
1024 ACPI_LOCK(acpi); /* wait for ACPI to finish nominal attach */
1025 ACPI_UNLOCK(acpi);
1027 lwkt_gettoken(&acpi_token);
1028 for (;;) {
1029 /* If the number of devices has changed, re-evaluate. */
1030 if (devclass_get_count(acpi_tz_devclass) != devcount) {
1031 if (devs != NULL) {
1032 kfree(devs, M_TEMP);
1033 kfree(sc, M_TEMP);
1035 devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
1036 sc = kmalloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
1037 M_WAITOK | M_ZERO);
1038 for (i = 0; i < devcount; i++)
1039 sc[i] = device_get_softc(devs[i]);
1042 /* Check for temperature events and act on them. */
1043 for (i = 0; i < devcount; i++) {
1044 ACPI_LOCK(thermal);
1045 flags = sc[i]->tz_flags;
1046 sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
1047 ACPI_UNLOCK(thermal);
1048 acpi_tz_timeout(sc[i], flags);
1051 /* If more work to do, don't go to sleep yet. */
1052 ACPI_LOCK(thermal);
1053 for (i = 0; i < devcount; i++) {
1054 if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
1055 break;
1059 * Interlocked sleep until signaled or we timeout.
1061 if (i == devcount) {
1062 tsleep_interlock(&acpi_tz_td, 0);
1063 ACPI_UNLOCK(thermal);
1064 tsleep(&acpi_tz_td, PINTERLOCKED, "tzpoll",
1065 (acpi_tz_polling_rate <= 0 ? 0 : hz * acpi_tz_polling_rate));
1066 } else {
1067 ACPI_UNLOCK(thermal);
1070 lwkt_reltoken(&acpi_token);
1073 #ifdef __FreeBSD__
1074 static int
1075 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
1077 device_t dev;
1078 int error;
1080 if (!sc->tz_cooling_updated)
1081 return (0);
1082 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
1083 return (ENXIO);
1084 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1085 "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
1086 TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
1087 error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
1088 if (error == 0)
1089 sc->tz_cooling_updated = FALSE;
1090 return (error);
1093 static int
1094 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1096 device_t dev;
1097 struct cf_level *levels;
1098 int num_levels, error, freq, desired_freq, perf, i;
1100 levels = kmalloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1101 if (levels == NULL)
1102 return (ENOMEM);
1105 * Find the main device, cpufreq0. We don't yet support independent
1106 * CPU frequency control on SMP.
1108 if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1109 error = ENXIO;
1110 goto out;
1113 /* Get the current frequency. */
1114 error = CPUFREQ_GET(dev, &levels[0]);
1115 if (error)
1116 goto out;
1117 freq = levels[0].total_set.freq;
1119 /* Get the current available frequency levels. */
1120 num_levels = CPUFREQ_MAX_LEVELS;
1121 error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1122 if (error) {
1123 if (error == E2BIG)
1124 printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1125 goto out;
1128 /* Calculate the desired frequency as a percent of the max frequency. */
1129 perf = 100 * freq / levels[0].total_set.freq - req;
1130 if (perf < 0)
1131 perf = 0;
1132 else if (perf > 100)
1133 perf = 100;
1134 desired_freq = levels[0].total_set.freq * perf / 100;
1136 if (desired_freq < freq) {
1137 /* Find the closest available frequency, rounding down. */
1138 for (i = 0; i < num_levels; i++)
1139 if (levels[i].total_set.freq <= desired_freq)
1140 break;
1142 /* If we didn't find a relevant setting, use the lowest. */
1143 if (i == num_levels)
1144 i--;
1145 } else {
1146 /* If we didn't decrease frequency yet, don't increase it. */
1147 if (!sc->tz_cooling_updated) {
1148 sc->tz_cooling_active = FALSE;
1149 goto out;
1152 /* Use saved cpu frequency as maximum value. */
1153 if (desired_freq > sc->tz_cooling_saved_freq)
1154 desired_freq = sc->tz_cooling_saved_freq;
1156 /* Find the closest available frequency, rounding up. */
1157 for (i = num_levels - 1; i >= 0; i--)
1158 if (levels[i].total_set.freq >= desired_freq)
1159 break;
1161 /* If we didn't find a relevant setting, use the highest. */
1162 if (i == -1)
1163 i++;
1165 /* If we're going to the highest frequency, restore the old setting. */
1166 if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1167 error = acpi_tz_cpufreq_restore(sc);
1168 if (error == 0)
1169 sc->tz_cooling_active = FALSE;
1170 goto out;
1174 /* If we are going to a new frequency, activate it. */
1175 if (levels[i].total_set.freq != freq) {
1176 ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1177 "temperature %d.%dC: %screasing clock speed "
1178 "from %d MHz to %d MHz\n",
1179 TZ_KELVTOC(sc->tz_temperature),
1180 (freq > levels[i].total_set.freq) ? "de" : "in",
1181 freq, levels[i].total_set.freq);
1182 error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1183 if (error == 0 && !sc->tz_cooling_updated) {
1184 sc->tz_cooling_saved_freq = freq;
1185 sc->tz_cooling_updated = TRUE;
1189 out:
1190 if (levels)
1191 free(levels, M_TEMP);
1192 return (error);
1194 #endif
1197 * Passive cooling thread; monitors current temperature according to the
1198 * cooling interval and calculates whether to scale back CPU frequency.
1200 static void
1201 acpi_tz_cooling_thread(void *arg)
1203 struct acpi_tz_softc *sc;
1204 int perf, curr_temp, prev_temp;
1205 #ifdef __FreeBSD__
1206 int error;
1207 #endif
1209 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1211 ACPI_LOCK(acpi); /* wait for ACPI to finish nominal attach */
1212 ACPI_UNLOCK(acpi);
1214 sc = (struct acpi_tz_softc *)arg;
1215 lwkt_gettoken(&acpi_token);
1217 prev_temp = sc->tz_temperature;
1218 while (sc->tz_cooling_enabled) {
1219 if (sc->tz_cooling_active)
1220 (void)acpi_tz_get_temperature(sc);
1221 curr_temp = sc->tz_temperature;
1222 if (curr_temp >= sc->tz_zone.psv)
1223 sc->tz_cooling_active = TRUE;
1224 if (sc->tz_cooling_active) {
1225 perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1226 sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1227 perf /= 10;
1229 if (perf != 0) {
1230 #ifdef __FreeBSD__
1231 error = acpi_tz_cpufreq_update(sc, perf);
1234 * If error and not simply a higher priority setting was
1235 * active, disable cooling.
1237 if (error != 0 && error != EPERM) {
1238 device_printf(sc->tz_dev,
1239 "failed to set new freq, disabling passive cooling\n");
1240 sc->tz_cooling_enabled = FALSE;
1242 #endif
1245 prev_temp = curr_temp;
1246 tsleep(&sc->tz_cooling_proc, 0, "cooling",
1247 hz * sc->tz_zone.tsp / 10);
1249 if (sc->tz_cooling_active) {
1250 #ifdef __FreeBSD__
1251 acpi_tz_cpufreq_restore(sc);
1252 #endif
1253 sc->tz_cooling_active = FALSE;
1255 sc->tz_cooling_proc = NULL;
1256 ACPI_LOCK(thermal);
1257 sc->tz_cooling_proc_running = FALSE;
1258 ACPI_UNLOCK(thermal);
1260 lwkt_reltoken(&acpi_token);
1264 * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1265 * all CPUs for us. However, it's possible in the future _PSL will
1266 * reference non-CPU devices so we may want to support it then.
1268 static int
1269 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1271 return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1272 sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1273 sc->tz_zone.psv != -1);
1276 static int
1277 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1279 int error;
1281 ACPI_LOCK(thermal);
1282 if (sc->tz_cooling_proc_running) {
1283 ACPI_UNLOCK(thermal);
1284 return (0);
1286 sc->tz_cooling_proc_running = TRUE;
1287 ACPI_UNLOCK(thermal);
1288 error = 0;
1289 if (sc->tz_cooling_proc == NULL) {
1290 error = kthread_create(acpi_tz_cooling_thread, sc,
1291 &sc->tz_cooling_proc,
1292 "acpi_cooling%d", device_get_unit(sc->tz_dev));
1293 if (error != 0) {
1294 device_printf(sc->tz_dev, "could not create thread - %d", error);
1295 ACPI_LOCK(thermal);
1296 sc->tz_cooling_proc_running = FALSE;
1297 ACPI_UNLOCK(thermal);
1300 return (error);