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