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