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