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