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