acpi_thermal: sync with FreeBSD 8
[dragonfly.git] / sys / dev / acpica5 / acpi_thermal.c
1 /*-
2  * Copyright (c) 2000, 2001 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
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.
14  *
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.
26  * $FreeBSD: src/sys/dev/acpica/acpi_thermal.c,v 1.73 2009/08/20 19:17:53 jhb
27  */
28
29 #include <sys/cdefs.h>
30
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/kthread.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/proc.h>
40 #include <sys/reboot.h>
41 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/power.h>
44 #include <sys/sensors.h>
45
46 #include "acpi.h"
47 #include "accommon.h"
48
49 #include <dev/acpica5/acpivar.h>
50
51 /* Hooks for the ACPI CA debugging infrastructure */
52 #define _COMPONENT      ACPI_THERMAL
53 ACPI_MODULE_NAME("THERMAL")
54
55 #define TZ_ZEROC        2732
56 #define TZ_KELVTOC(x)   (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
57
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. */
62
63 /* Check for temperature changes every 10 seconds by default */
64 #define TZ_POLLRATE     10
65
66 /* Make sure the reported temperature is valid for this number of polls. */
67 #define TZ_VALIDCHECKS  3
68
69 /* Notify the user we will be shutting down in one more poll cycle. */
70 #define TZ_NOTIFYCOUNT  (TZ_VALIDCHECKS - 1)
71
72 #define abs(x) ( x < 0 ? -x : x )
73
74 /* ACPI spec defines this */
75 #define TZ_NUMLEVELS    10
76 struct acpi_tz_zone {
77     int         ac[TZ_NUMLEVELS];
78     ACPI_BUFFER al[TZ_NUMLEVELS];
79     int         crt;
80     int         hot;
81     ACPI_BUFFER psl;
82     int         psv;
83     int         tc1;
84     int         tc2;
85     int         tsp;
86     int         tzp;
87 };
88
89 struct acpi_tz_softc {
90     device_t                    tz_dev;
91     ACPI_HANDLE                 tz_handle;      /*Thermal zone handle*/
92     int                         tz_temperature; /*Current temperature*/
93     int                         tz_active;      /*Current active cooling*/
94 #define TZ_ACTIVE_NONE          -1
95 #define TZ_ACTIVE_UNKNOWN       -2
96     int                         tz_requested;   /*Minimum active cooling*/
97     int                         tz_thflags;     /*Current temp-related flags*/
98 #define TZ_THFLAG_NONE          0
99 #define TZ_THFLAG_PSV           (1<<0)
100 #define TZ_THFLAG_HOT           (1<<2)
101 #define TZ_THFLAG_CRT           (1<<3)
102     int                         tz_flags;
103 #define TZ_FLAG_NO_SCP          (1<<0)          /*No _SCP method*/
104 #define TZ_FLAG_GETPROFILE      (1<<1)          /*Get power_profile in timeout*/
105 #define TZ_FLAG_GETSETTINGS     (1<<2)          /*Get devs/setpoints*/
106     struct timespec             tz_cooling_started;
107                                         /*Current cooling starting time*/
108
109     struct sysctl_ctx_list      tz_sysctl_ctx;
110     struct sysctl_oid           *tz_sysctl_tree;
111     eventhandler_tag            tz_event;
112
113     struct acpi_tz_zone         tz_zone;        /*Thermal zone parameters*/
114     int                         tz_validchecks;
115
116     /* passive cooling */
117     struct thread               *tz_cooling_proc;
118     int                         tz_cooling_proc_running;
119     int                         tz_cooling_enabled;
120     int                         tz_cooling_active;
121     int                         tz_cooling_updated;
122     int                         tz_cooling_saved_freq;
123     /* sensors(9) related */
124     struct ksensordev           sensordev;
125     struct ksensor              sensor;
126 };
127
128 #define CPUFREQ_MAX_LEVELS      64 /* XXX cpufreq should export this */
129
130 static int      acpi_tz_probe(device_t dev);
131 static int      acpi_tz_attach(device_t dev);
132 static int      acpi_tz_establish(struct acpi_tz_softc *sc);
133 static void     acpi_tz_monitor(void *Context);
134 static void     acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
135 static void     acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
136 static void     acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
137                                  int *data);
138 static void     acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
139 static int      acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
140 static int      acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS);
141 static int      acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS);
142 static int      acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
143 static void     acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
144                                        void *context);
145 static void     acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
146 static void     acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
147 static void     acpi_tz_power_profile(void *arg);
148 static void     acpi_tz_thread(void *arg);
149 static int      acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
150 static int      acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
151
152 static device_method_t acpi_tz_methods[] = {
153     /* Device interface */
154     DEVMETHOD(device_probe,     acpi_tz_probe),
155     DEVMETHOD(device_attach,    acpi_tz_attach),
156
157     {0, 0}
158 };
159
160 static driver_t acpi_tz_driver = {
161     "acpi_tz",
162     acpi_tz_methods,
163     sizeof(struct acpi_tz_softc),
164 };
165
166 static devclass_t acpi_tz_devclass;
167 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0);
168 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
169
170 static struct sysctl_ctx_list   acpi_tz_sysctl_ctx;
171 static struct sysctl_oid        *acpi_tz_sysctl_tree;
172
173 /* Minimum cooling run time */
174 static int                      acpi_tz_min_runtime;
175 static int                      acpi_tz_polling_rate = TZ_POLLRATE;
176 static int                      acpi_tz_override;
177
178 /* Timezone polling thread */
179 static struct thread            *acpi_tz_td;
180 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
181
182 static int                      acpi_tz_cooling_unit = -1;
183
184 static int
185 acpi_tz_probe(device_t dev)
186 {
187     int         result;
188
189     if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
190         device_set_desc(dev, "Thermal Zone");
191         result = -10;
192     } else
193         result = ENXIO;
194     return (result);
195 }
196
197 static int
198 acpi_tz_attach(device_t dev)
199 {
200     struct acpi_tz_softc        *sc;
201     struct acpi_softc           *acpi_sc;
202     int                         error;
203     char                        oidname[8];
204
205     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
206
207     sc = device_get_softc(dev);
208     sc->tz_dev = dev;
209     sc->tz_handle = acpi_get_handle(dev);
210     sc->tz_requested = TZ_ACTIVE_NONE;
211     sc->tz_active = TZ_ACTIVE_UNKNOWN;
212     sc->tz_thflags = TZ_THFLAG_NONE;
213     sc->tz_cooling_proc = NULL;
214     sc->tz_cooling_proc_running = FALSE;
215     sc->tz_cooling_active = FALSE;
216     sc->tz_cooling_updated = FALSE;
217     sc->tz_cooling_enabled = FALSE;
218
219     /*
220      * Parse the current state of the thermal zone and build control
221      * structures.  We don't need to worry about interference with the
222      * control thread since we haven't fully attached this device yet.
223      */
224     if ((error = acpi_tz_establish(sc)) != 0)
225         return (error);
226
227     /*
228      * Register for any Notify events sent to this zone.
229      */
230     AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
231                              acpi_tz_notify_handler, sc);
232
233     /*
234      * Create our sysctl nodes.
235      *
236      * XXX we need a mechanism for adding nodes under ACPI.
237      */
238     if (device_get_unit(dev) == 0) {
239         acpi_sc = acpi_device_get_parent_softc(dev);
240         sysctl_ctx_init(&acpi_tz_sysctl_ctx);
241         acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
242                               SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
243                               OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
244         SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
245                        SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
246                        OID_AUTO, "min_runtime", CTLFLAG_RW,
247                        &acpi_tz_min_runtime, 0,
248                        "minimum cooling run time in sec");
249         SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
250                        SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
251                        OID_AUTO, "polling_rate", CTLFLAG_RW,
252                        &acpi_tz_polling_rate, 0, "monitor polling rate");
253         SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
254                        SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
255                        "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
256                        "allow override of thermal settings");
257     }
258     sysctl_ctx_init(&sc->tz_sysctl_ctx);
259     ksprintf(oidname, "tz%d", device_get_unit(dev));
260     sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
261                                          SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
262                                          OID_AUTO, oidname, CTLFLAG_RD, 0, "");
263     SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
264                       OID_AUTO, "temperature", CTLFLAG_RD, &sc->tz_temperature,
265                       sizeof(sc->tz_temperature), "IK",
266                       "current thermal zone temperature");
267     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
268                     OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
269                     sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
270     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
271                     OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
272                     sc, 0, acpi_tz_cooling_sysctl, "I",
273                     "enable passive (speed reduction) cooling");
274
275     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
276                    OID_AUTO, "thermal_flags", CTLFLAG_RD,
277                    &sc->tz_thflags, 0, "thermal zone flags");
278     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
279                     OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
280                     sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
281                     acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
282     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
283                     OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
284                     sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
285                     acpi_tz_temp_sysctl, "IK",
286                     "too hot temp setpoint (suspend now)");
287     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
288                     OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
289                     sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
290                     acpi_tz_temp_sysctl, "IK",
291                     "critical temp setpoint (shutdown now)");
292     SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
293                       OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac,
294                       sizeof(sc->tz_zone.ac), "IK", "");
295     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
296                     OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
297                     sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
298                     acpi_tz_passive_sysctl, "I",
299                     "thermal constant 1 for passive cooling");
300     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
301                     OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
302                     sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
303                     acpi_tz_passive_sysctl, "I",
304                     "thermal constant 2 for passive cooling");
305     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
306                     OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
307                     sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
308                     acpi_tz_passive_sysctl, "I",
309                     "thermal sampling period for passive cooling");
310
311     /*
312      * Create thread to service all of the thermal zones.  Register
313      * our power profile event handler.
314      */
315     sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
316         acpi_tz_power_profile, sc, 0);
317     if (acpi_tz_td == NULL) {
318         error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_td,
319             RFHIGHPID, 0, "acpi_thermal");
320         if (error != 0) {
321             device_printf(sc->tz_dev, "could not create thread - %d", error);
322             goto out;
323         }
324     }
325
326     /*
327      * Create a thread to handle passive cooling for 1st zone which
328      * has _PSV, _TSP, _TC1 and _TC2.  Users can enable it for other
329      * zones manually for now.
330      *
331      * XXX We enable only one zone to avoid multiple zones conflict
332      * with each other since cpufreq currently sets all CPUs to the
333      * given frequency whereas it's possible for different thermal
334      * zones to specify independent settings for multiple CPUs.
335      */
336     if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
337         sc->tz_cooling_enabled = TRUE;
338     if (sc->tz_cooling_enabled) {
339         error = acpi_tz_cooling_thread_start(sc);
340         if (error != 0) {
341             sc->tz_cooling_enabled = FALSE;
342             goto out;
343         }
344         acpi_tz_cooling_unit = device_get_unit(dev);
345     }
346
347     /*
348      * Flag the event handler for a manual invocation by our timeout.
349      * We defer it like this so that the rest of the subsystem has time
350      * to come up.  Don't bother evaluating/printing the temperature at
351      * this point; on many systems it'll be bogus until the EC is running.
352      */
353     sc->tz_flags |= TZ_FLAG_GETPROFILE;
354
355     /* Attach sensors(9). */
356     strlcpy(sc->sensordev.xname, device_get_nameunit(sc->tz_dev),
357         sizeof(sc->sensordev.xname));
358
359     sc->sensor.type = SENSOR_TEMP;
360     sensor_attach(&sc->sensordev, &sc->sensor);
361
362     sensordev_install(&sc->sensordev);
363
364 out:
365     if (error != 0) {
366         EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
367         AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
368             acpi_tz_notify_handler);
369         sysctl_ctx_free(&sc->tz_sysctl_ctx);
370     }
371     return_VALUE (error);
372 }
373
374 /*
375  * Parse the current state of this thermal zone and set up to use it.
376  *
377  * Note that we may have previous state, which will have to be discarded.
378  */
379 static int
380 acpi_tz_establish(struct acpi_tz_softc *sc)
381 {
382     ACPI_OBJECT *obj;
383     int         i;
384     char        nbuf[8];
385
386     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
387
388     /* Erase any existing state. */
389     for (i = 0; i < TZ_NUMLEVELS; i++)
390         if (sc->tz_zone.al[i].Pointer != NULL)
391             AcpiOsFree(sc->tz_zone.al[i].Pointer);
392     if (sc->tz_zone.psl.Pointer != NULL)
393         AcpiOsFree(sc->tz_zone.psl.Pointer);
394
395     /*
396      * XXX: We initialize only ACPI_BUFFER to avoid race condition
397      * with passive cooling thread which refers psv, tc1, tc2 and tsp.
398      */
399     bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
400     bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
401     bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
402
403     /* Evaluate thermal zone parameters. */
404     for (i = 0; i < TZ_NUMLEVELS; i++) {
405         ksprintf(nbuf, "_AC%d", i);
406         acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
407         ksprintf(nbuf, "_AL%d", i);
408         sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
409         sc->tz_zone.al[i].Pointer = NULL;
410         AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
411         obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
412         if (obj != NULL) {
413             /* Should be a package containing a list of power objects */
414             if (obj->Type != ACPI_TYPE_PACKAGE) {
415                 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
416                               nbuf, obj->Type);
417                 return_VALUE (ENXIO);
418             }
419         }
420     }
421     acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
422     acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
423     sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
424     sc->tz_zone.psl.Pointer = NULL;
425     AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
426     acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
427     acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
428     acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
429     acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
430     acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
431
432     /*
433      * Sanity-check the values we've been given.
434      *
435      * XXX what do we do about systems that give us the same value for
436      *     more than one of these setpoints?
437      */
438     acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
439     acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
440     acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
441     for (i = 0; i < TZ_NUMLEVELS; i++)
442         acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
443
444     return_VALUE (0);
445 }
446
447 static char *aclevel_string[] = {
448     "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
449     "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
450 };
451
452 static __inline const char *
453 acpi_tz_aclevel_string(int active)
454 {
455     if (active < -1 || active >= TZ_NUMLEVELS)
456         return (aclevel_string[0]);
457
458     return (aclevel_string[active + 1]);
459 }
460
461 /*
462  * Get the current temperature.
463  */
464 static int
465 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
466 {
467     int         temp;
468     ACPI_STATUS status;
469     static char *tmp_name = "_TMP";
470
471     ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
472
473     /* Evaluate the thermal zone's _TMP method. */
474     status = acpi_GetInteger(sc->tz_handle, tmp_name, &temp);
475     if (ACPI_FAILURE(status)) {
476         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
477             "error fetching current temperature -- %s\n",
478              AcpiFormatException(status));
479         return (FALSE);
480     }
481
482     /* Check it for validity. */
483     acpi_tz_sanity(sc, &temp, tmp_name);
484     if (temp == -1)
485         return (FALSE);
486
487     ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
488     sc->tz_temperature = temp;
489     /* Update sensor */
490     if(sc->tz_temperature == -1)
491         sc->sensor.flags &= ~SENSOR_FINVALID;
492     sc->sensor.value = sc->tz_temperature * 100000;
493     return (TRUE);
494 }
495
496 /*
497  * Evaluate the condition of a thermal zone, take appropriate actions.
498  */
499 static void
500 acpi_tz_monitor(void *Context)
501 {
502     struct acpi_tz_softc *sc;
503     struct      timespec curtime;
504     int         temp;
505     int         i;
506     int         newactive, newflags;
507
508     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
509
510     sc = (struct acpi_tz_softc *)Context;
511
512     /* Get the current temperature. */
513     if (!acpi_tz_get_temperature(sc)) {
514         /* XXX disable zone? go to max cooling? */
515         return_VOID;
516     }
517     temp = sc->tz_temperature;
518
519     /*
520      * Work out what we ought to be doing right now.
521      *
522      * Note that the _ACx levels sort from hot to cold.
523      */
524     newactive = TZ_ACTIVE_NONE;
525     for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
526         if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i]) {
527             newactive = i;
528             if (sc->tz_active != newactive) {
529                 ACPI_VPRINT(sc->tz_dev,
530                             acpi_device_get_parent_softc(sc->tz_dev),
531                             "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i,
532                             TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i]));
533             }
534         }
535     }
536
537     /*
538      * We are going to get _ACx level down (colder side), but give a guaranteed
539      * minimum cooling run time if requested.
540      */
541     if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
542         sc->tz_active != TZ_ACTIVE_UNKNOWN &&
543         (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
544
545         getnanotime(&curtime);
546         timespecsub(&curtime, &sc->tz_cooling_started);
547         if (curtime.tv_sec < acpi_tz_min_runtime)
548             newactive = sc->tz_active;
549     }
550
551     /* Handle user override of active mode */
552     if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
553         || sc->tz_requested < newactive))
554         newactive = sc->tz_requested;
555
556     /* update temperature-related flags */
557     newflags = TZ_THFLAG_NONE;
558     if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
559         newflags |= TZ_THFLAG_PSV;
560     if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
561         newflags |= TZ_THFLAG_HOT;
562     if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
563         newflags |= TZ_THFLAG_CRT;
564
565     /* If the active cooling state has changed, we have to switch things. */
566     if (sc->tz_active == TZ_ACTIVE_UNKNOWN) {
567         /*
568          * We don't know which cooling device is on or off,
569          * so stop them all, because we now know which
570          * should be on (if any).
571          */
572         for (i = 0; i < TZ_NUMLEVELS; i++) {
573             if (sc->tz_zone.al[i].Pointer != NULL) {
574                 acpi_ForeachPackageObject(
575                     (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
576                     acpi_tz_switch_cooler_off, sc);
577             }
578         }
579         /* now we know that all devices are off */
580         sc->tz_active = TZ_ACTIVE_NONE;
581     }
582
583     if (newactive != sc->tz_active) {
584         /* Turn off the cooling devices that are on, if any are */
585         if (sc->tz_active != TZ_ACTIVE_NONE)
586             acpi_ForeachPackageObject(
587                 (ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer,
588                 acpi_tz_switch_cooler_off, sc);
589
590         /* Turn on cooling devices that are required, if any are */
591         if (newactive != TZ_ACTIVE_NONE) {
592             acpi_ForeachPackageObject(
593                 (ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer,
594                 acpi_tz_switch_cooler_on, sc);
595         }
596         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
597                     "switched from %s to %s: %d.%dC\n",
598                     acpi_tz_aclevel_string(sc->tz_active),
599                     acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
600         sc->tz_active = newactive;
601         getnanotime(&sc->tz_cooling_started);
602     }
603
604     /* XXX (de)activate any passive cooling that may be required. */
605
606     /*
607      * If the temperature is at _HOT or _CRT, increment our event count.
608      * If it has occurred enough times, shutdown the system.  This is
609      * needed because some systems will report an invalid high temperature
610      * for one poll cycle.  It is suspected this is due to the embedded
611      * controller timing out.  A typical value is 138C for one cycle on
612      * a system that is otherwise 65C.
613      *
614      * If we're almost at that threshold, notify the user through devd(8).
615      */
616     if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
617         sc->tz_validchecks++;
618         if (sc->tz_validchecks == TZ_VALIDCHECKS) {
619             device_printf(sc->tz_dev,
620                 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
621                 TZ_KELVTOC(sc->tz_temperature));
622             shutdown_nice(RB_POWEROFF);
623         } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
624             acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
625     } else {
626         sc->tz_validchecks = 0;
627     }
628     sc->tz_thflags = newflags;
629
630     return_VOID;
631 }
632
633 /*
634  * Given an object, verify that it's a reference to a device of some sort,
635  * and try to switch it off.
636  */
637 static void
638 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
639 {
640     ACPI_HANDLE                 cooler;
641
642     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
643
644     cooler = acpi_GetReference(NULL, obj);
645     if (cooler == NULL) {
646         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
647         return_VOID;
648     }
649
650     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
651                      acpi_name(cooler)));
652     acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
653
654     return_VOID;
655 }
656
657 /*
658  * Given an object, verify that it's a reference to a device of some sort,
659  * and try to switch it on.
660  *
661  * XXX replication of off/on function code is bad.
662  */
663 static void
664 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
665 {
666     struct acpi_tz_softc        *sc = (struct acpi_tz_softc *)arg;
667     ACPI_HANDLE                 cooler;
668     ACPI_STATUS                 status;
669
670     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
671
672     cooler = acpi_GetReference(NULL, obj);
673     if (cooler == NULL) {
674         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
675         return_VOID;
676     }
677
678     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
679                      acpi_name(cooler)));
680     status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
681     if (ACPI_FAILURE(status)) {
682         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
683                     "failed to activate %s - %s\n", acpi_name(cooler),
684                     AcpiFormatException(status));
685     }
686
687     return_VOID;
688 }
689
690 /*
691  * Read/debug-print a parameter, default it to -1.
692  */
693 static void
694 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
695 {
696
697     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
698
699     if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
700         *data = -1;
701     } else {
702         ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
703                          acpi_name(sc->tz_handle), node, *data));
704     }
705
706     return_VOID;
707 }
708
709 /*
710  * Sanity-check a temperature value.  Assume that setpoints
711  * should be between 0C and 200C.
712  */
713 static void
714 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
715 {
716     if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
717         device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
718                       what, TZ_KELVTOC(*val));
719         *val = -1;
720     }
721 }
722
723 /*
724  * Respond to a sysctl on the active state node.
725  */
726 static int
727 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
728 {
729     struct acpi_tz_softc        *sc;
730     int                         active;
731     int                         error;
732
733     sc = (struct acpi_tz_softc *)oidp->oid_arg1;
734     active = sc->tz_active;
735     error = sysctl_handle_int(oidp, &active, 0, req);
736
737     /* Error or no new value */
738     if (error != 0 || req->newptr == NULL)
739         return (error);
740     if (active < -1 || active >= TZ_NUMLEVELS)
741         return (EINVAL);
742
743     /* Set new preferred level and re-switch */
744     sc->tz_requested = active;
745     acpi_tz_signal(sc, 0);
746     return (0);
747 }
748
749 static int
750 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
751 {
752     struct acpi_tz_softc *sc;
753     int enabled, error;
754
755     sc = (struct acpi_tz_softc *)oidp->oid_arg1;
756     enabled = sc->tz_cooling_enabled;
757     error = sysctl_handle_int(oidp, &enabled, 0, req);
758
759     /* Error or no new value */
760     if (error != 0 || req->newptr == NULL)
761         return (error);
762     if (enabled != TRUE && enabled != FALSE)
763         return (EINVAL);
764
765     if (enabled) {
766         if (acpi_tz_cooling_is_available(sc))
767             error = acpi_tz_cooling_thread_start(sc);
768         else
769             error = ENODEV;
770         if (error)
771             enabled = FALSE;
772     }
773     sc->tz_cooling_enabled = enabled;
774     return (error);
775 }
776
777 static int
778 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
779 {
780     struct acpi_tz_softc        *sc;
781     int                         temp, *temp_ptr;
782     int                         error;
783
784     sc = oidp->oid_arg1;
785     temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
786     temp = *temp_ptr;
787     error = sysctl_handle_int(oidp, &temp, 0, req);
788
789     /* Error or no new value */
790     if (error != 0 || req->newptr == NULL)
791         return (error);
792
793     /* Only allow changing settings if override is set. */
794     if (!acpi_tz_override)
795         return (EPERM);
796
797     /* Check user-supplied value for sanity. */
798     acpi_tz_sanity(sc, &temp, "user-supplied temp");
799     if (temp == -1)
800         return (EINVAL);
801
802     *temp_ptr = temp;
803     return (0);
804 }
805
806 static int
807 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
808 {
809     struct acpi_tz_softc        *sc;
810     int                         val, *val_ptr;
811     int                         error;
812
813     sc = oidp->oid_arg1;
814     val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
815     val = *val_ptr;
816     error = sysctl_handle_int(oidp, &val, 0, req);
817
818     /* Error or no new value */
819     if (error != 0 || req->newptr == NULL)
820         return (error);
821
822     /* Only allow changing settings if override is set. */
823     if (!acpi_tz_override)
824         return (EPERM);
825
826     *val_ptr = val;
827     return (0);
828 }
829
830 static void
831 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
832 {
833     struct acpi_tz_softc        *sc = (struct acpi_tz_softc *)context;
834
835     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
836
837     switch (notify) {
838     case TZ_NOTIFY_TEMPERATURE:
839         /* Temperature change occurred */
840         acpi_tz_signal(sc, 0);
841         break;
842     case TZ_NOTIFY_DEVICES:
843     case TZ_NOTIFY_LEVELS:
844         /* Zone devices/setpoints changed */
845         acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
846         break;
847     default:
848         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
849                     "unknown Notify event 0x%x\n", notify);
850         break;
851     }
852
853     acpi_UserNotify("Thermal", h, notify);
854
855     return_VOID;
856 }
857
858 static void
859 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
860 {
861     ACPI_LOCK(thermal);
862     sc->tz_flags |= flags;
863     ACPI_UNLOCK(thermal);
864     wakeup(&acpi_tz_td);
865 }
866
867 /*
868  * Notifies can be generated asynchronously but have also been seen to be
869  * triggered by other thermal methods.  One system generates a notify of
870  * 0x81 when the fan is turned on or off.  Another generates it when _SCP
871  * is called.  To handle these situations, we check the zone via
872  * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
873  * policy.
874  */
875 static void
876 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
877 {
878
879     /* Check the current temperature and take action based on it */
880     acpi_tz_monitor(sc);
881
882     /* If requested, get the power profile settings. */
883     if (flags & TZ_FLAG_GETPROFILE)
884         acpi_tz_power_profile(sc);
885
886     /*
887      * If requested, check for new devices/setpoints.  After finding them,
888      * check if we need to switch fans based on the new values.
889      */
890     if (flags & TZ_FLAG_GETSETTINGS) {
891         acpi_tz_establish(sc);
892         acpi_tz_monitor(sc);
893     }
894
895     /* XXX passive cooling actions? */
896 }
897
898 /*
899  * System power profile may have changed; fetch and notify the
900  * thermal zone accordingly.
901  *
902  * Since this can be called from an arbitrary eventhandler, it needs
903  * to get the ACPI lock itself.
904  */
905 static void
906 acpi_tz_power_profile(void *arg)
907 {
908     ACPI_STATUS                 status;
909     struct acpi_tz_softc        *sc = (struct acpi_tz_softc *)arg;
910     int                         state;
911
912     state = power_profile_get_state();
913     if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
914         return;
915
916     /* check that we haven't decided there's no _SCP method */
917     if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
918
919         /* Call _SCP to set the new profile */
920         status = acpi_SetInteger(sc->tz_handle, "_SCP",
921             (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
922         if (ACPI_FAILURE(status)) {
923             if (status != AE_NOT_FOUND)
924                 ACPI_VPRINT(sc->tz_dev,
925                             acpi_device_get_parent_softc(sc->tz_dev),
926                             "can't evaluate %s._SCP - %s\n",
927                             acpi_name(sc->tz_handle),
928                             AcpiFormatException(status));
929             sc->tz_flags |= TZ_FLAG_NO_SCP;
930         } else {
931             /* We have to re-evaluate the entire zone now */
932             acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
933         }
934     }
935 }
936
937 /*
938  * Thermal zone monitor thread.
939  */
940 static void
941 acpi_tz_thread(void *arg)
942 {
943     device_t    *devs;
944     int         devcount, i;
945     int         flags;
946     struct acpi_tz_softc **sc;
947
948     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
949
950     devs = NULL;
951     devcount = 0;
952     sc = NULL;
953
954     for (;;) {
955         /* If the number of devices has changed, re-evaluate. */
956         if (devclass_get_count(acpi_tz_devclass) != devcount) {
957             if (devs != NULL) {
958                 kfree(devs, M_TEMP);
959                 kfree(sc, M_TEMP);
960             }
961             devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
962             sc = kmalloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
963                         M_WAITOK | M_ZERO);
964             for (i = 0; i < devcount; i++)
965                 sc[i] = device_get_softc(devs[i]);
966         }
967
968         /* Check for temperature events and act on them. */
969         for (i = 0; i < devcount; i++) {
970             ACPI_LOCK(thermal);
971             flags = sc[i]->tz_flags;
972             sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
973             ACPI_UNLOCK(thermal);
974             acpi_tz_timeout(sc[i], flags);
975         }
976
977         /* If more work to do, don't go to sleep yet. */
978         ACPI_LOCK(thermal);
979         for (i = 0; i < devcount; i++) {
980             if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
981                 break;
982         }
983
984         /*
985          * If we have no more work, sleep for a while, setting PDROP so that
986          * the mutex will not be reacquired.  Otherwise, drop the mutex and
987          * loop to handle more events.
988          */
989         if (i == devcount)
990             tsleep(&acpi_tz_td, 0, "tzpoll",
991                 hz * acpi_tz_polling_rate);
992         else
993             ACPI_UNLOCK(thermal);
994     }
995 }
996
997 #ifdef __FreeBSD__
998 static int
999 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
1000 {
1001     device_t dev;
1002     int error;
1003
1004     if (!sc->tz_cooling_updated)
1005         return (0);
1006     if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
1007         return (ENXIO);
1008     ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1009         "temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
1010         TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
1011     error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
1012     if (error == 0)
1013         sc->tz_cooling_updated = FALSE;
1014     return (error);
1015 }
1016
1017 static int
1018 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
1019 {
1020     device_t dev;
1021     struct cf_level *levels;
1022     int num_levels, error, freq, desired_freq, perf, i;
1023
1024     levels = kmalloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
1025     if (levels == NULL)
1026         return (ENOMEM);
1027
1028     /*
1029      * Find the main device, cpufreq0.  We don't yet support independent
1030      * CPU frequency control on SMP.
1031      */
1032     if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
1033         error = ENXIO;
1034         goto out;
1035     }
1036
1037     /* Get the current frequency. */
1038     error = CPUFREQ_GET(dev, &levels[0]);
1039     if (error)
1040         goto out;
1041     freq = levels[0].total_set.freq;
1042
1043     /* Get the current available frequency levels. */
1044     num_levels = CPUFREQ_MAX_LEVELS;
1045     error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1046     if (error) {
1047         if (error == E2BIG)
1048             printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1049         goto out;
1050     }
1051
1052     /* Calculate the desired frequency as a percent of the max frequency. */
1053     perf = 100 * freq / levels[0].total_set.freq - req;
1054     if (perf < 0)
1055         perf = 0;
1056     else if (perf > 100)
1057         perf = 100;
1058     desired_freq = levels[0].total_set.freq * perf / 100;
1059
1060     if (desired_freq < freq) {
1061         /* Find the closest available frequency, rounding down. */
1062         for (i = 0; i < num_levels; i++)
1063             if (levels[i].total_set.freq <= desired_freq)
1064                 break;
1065
1066         /* If we didn't find a relevant setting, use the lowest. */
1067         if (i == num_levels)
1068             i--;
1069     } else {
1070         /* If we didn't decrease frequency yet, don't increase it. */
1071         if (!sc->tz_cooling_updated) {
1072             sc->tz_cooling_active = FALSE;
1073             goto out;
1074         }
1075
1076         /* Use saved cpu frequency as maximum value. */
1077         if (desired_freq > sc->tz_cooling_saved_freq)
1078             desired_freq = sc->tz_cooling_saved_freq;
1079
1080         /* Find the closest available frequency, rounding up. */
1081         for (i = num_levels - 1; i >= 0; i--)
1082             if (levels[i].total_set.freq >= desired_freq)
1083                 break;
1084
1085         /* If we didn't find a relevant setting, use the highest. */
1086         if (i == -1)
1087             i++;
1088
1089         /* If we're going to the highest frequency, restore the old setting. */
1090         if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1091             error = acpi_tz_cpufreq_restore(sc);
1092             if (error == 0)
1093                 sc->tz_cooling_active = FALSE;
1094             goto out;
1095         }
1096     }
1097
1098     /* If we are going to a new frequency, activate it. */
1099     if (levels[i].total_set.freq != freq) {
1100         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1101             "temperature %d.%dC: %screasing clock speed "
1102             "from %d MHz to %d MHz\n",
1103             TZ_KELVTOC(sc->tz_temperature),
1104             (freq > levels[i].total_set.freq) ? "de" : "in",
1105             freq, levels[i].total_set.freq);
1106         error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1107         if (error == 0 && !sc->tz_cooling_updated) {
1108             sc->tz_cooling_saved_freq = freq;
1109             sc->tz_cooling_updated = TRUE;
1110         }
1111     }
1112
1113 out:
1114     if (levels)
1115         free(levels, M_TEMP);
1116     return (error);
1117 }
1118 #endif
1119
1120 /*
1121  * Passive cooling thread; monitors current temperature according to the
1122  * cooling interval and calculates whether to scale back CPU frequency.
1123  */
1124 static void
1125 acpi_tz_cooling_thread(void *arg)
1126 {
1127     struct acpi_tz_softc *sc;
1128     int error, perf, curr_temp, prev_temp;
1129
1130     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1131
1132     sc = (struct acpi_tz_softc *)arg;
1133
1134     prev_temp = sc->tz_temperature;
1135     while (sc->tz_cooling_enabled) {
1136         if (sc->tz_cooling_active)
1137             (void)acpi_tz_get_temperature(sc);
1138         curr_temp = sc->tz_temperature;
1139         if (curr_temp >= sc->tz_zone.psv)
1140             sc->tz_cooling_active = TRUE;
1141         if (sc->tz_cooling_active) {
1142             perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1143                    sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1144             perf /= 10;
1145
1146             if (perf != 0) {
1147 #ifdef __FreeBSD__
1148                 error = acpi_tz_cpufreq_update(sc, perf);
1149
1150                 /*
1151                  * If error and not simply a higher priority setting was
1152                  * active, disable cooling.
1153                  */
1154                 if (error != 0 && error != EPERM) {
1155                     device_printf(sc->tz_dev,
1156                         "failed to set new freq, disabling passive cooling\n");
1157                     sc->tz_cooling_enabled = FALSE;
1158                 }
1159 #endif
1160             }
1161         }
1162         prev_temp = curr_temp;
1163         tsleep(&sc->tz_cooling_proc, 0, "cooling",
1164             hz * sc->tz_zone.tsp / 10);
1165     }
1166     if (sc->tz_cooling_active) {
1167 #ifdef __FreeBSD__
1168         acpi_tz_cpufreq_restore(sc);
1169 #endif
1170         sc->tz_cooling_active = FALSE;
1171     }
1172     sc->tz_cooling_proc = NULL;
1173     ACPI_LOCK(thermal);
1174     sc->tz_cooling_proc_running = FALSE;
1175     ACPI_UNLOCK(thermal);
1176     kthread_exit();
1177 }
1178
1179 /*
1180  * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1181  * all CPUs for us.  However, it's possible in the future _PSL will
1182  * reference non-CPU devices so we may want to support it then.
1183  */
1184 static int
1185 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1186 {
1187     return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1188         sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1189         sc->tz_zone.psv != -1);
1190 }
1191
1192 static int
1193 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1194 {
1195     int error;
1196     char name[16];
1197
1198     ACPI_LOCK(thermal);
1199     if (sc->tz_cooling_proc_running) {
1200         ACPI_UNLOCK(thermal);
1201         return (0);
1202     }
1203     sc->tz_cooling_proc_running = TRUE;
1204     ACPI_UNLOCK(thermal);
1205     error = 0;
1206     if (sc->tz_cooling_proc == NULL) {
1207         ksnprintf(name, sizeof(name), "acpi_cooling%d",
1208             device_get_unit(sc->tz_dev));
1209         error = kthread_create(acpi_tz_cooling_thread, sc,
1210             &sc->tz_cooling_proc, RFHIGHPID, 0, name);
1211         if (error != 0) {
1212             device_printf(sc->tz_dev, "could not create thread - %d", error);
1213             ACPI_LOCK(thermal);
1214             sc->tz_cooling_proc_running = FALSE;
1215             ACPI_UNLOCK(thermal);
1216         }
1217     }
1218     return (error);
1219 }