Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / acpica5 / acpi_thermal.c
1 /*-
2  * Copyright (c) 2000, 2001 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/acpica/acpi_thermal.c,v 1.47 2004/05/30 20:08:23 phk Exp $
28  * $DragonFly: src/sys/dev/acpica5/acpi_thermal.c,v 1.7 2007/01/17 17:31:19 y0netan1 Exp $
29  */
30
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/kthread.h>
35 #include <sys/module.h>
36 #include <sys/bus.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/thread2.h>
43 #include <sys/sensors.h>
44
45 #include "acpi.h"
46 #include "accommon.h"
47 #include <dev/acpica5/acpivar.h>
48
49 /* Hooks for the ACPI CA debugging infrastructure */
50 #define _COMPONENT      ACPI_THERMAL
51 ACPI_MODULE_NAME("THERMAL")
52
53 #define TZ_ZEROC        2732
54 #define TZ_KELVTOC(x)   (((x) - TZ_ZEROC) / 10), (((x) - TZ_ZEROC) % 10)
55
56 #define TZ_NOTIFY_TEMPERATURE   0x80 /* Temperature changed. */
57 #define TZ_NOTIFY_LEVELS        0x81 /* Cooling levels changed. */
58 #define TZ_NOTIFY_DEVICES       0x82 /* Device lists changed. */
59 #define TZ_NOTIFY_CRITICAL      0xcc /* Fake notify that _CRT/_HOT reached. */
60
61 /* Check for temperature changes every 10 seconds by default */
62 #define TZ_POLLRATE     10
63
64 /* Make sure the reported temperature is valid for this number of polls. */
65 #define TZ_VALIDCHECKS  3
66
67 /* Notify the user we will be shutting down in one more poll cycle. */
68 #define TZ_NOTIFYCOUNT  (TZ_VALIDCHECKS - 1)
69
70 /* ACPI spec defines this */
71 #define TZ_NUMLEVELS    10
72 struct acpi_tz_zone {
73     int         ac[TZ_NUMLEVELS];
74     ACPI_BUFFER al[TZ_NUMLEVELS];
75     int         crt;
76     int         hot;
77     ACPI_BUFFER psl;
78     int         psv;
79     int         tc1;
80     int         tc2;
81     int         tsp;
82     int         tzp;
83 };
84
85 struct acpi_tz_softc {
86     device_t                    tz_dev;
87     ACPI_HANDLE                 tz_handle;      /*Thermal zone handle*/
88     int                         tz_temperature; /*Current temperature*/
89     int                         tz_active;      /*Current active cooling*/
90 #define TZ_ACTIVE_NONE          -1
91     int                         tz_requested;   /*Minimum active cooling*/
92     int                         tz_thflags;     /*Current temp-related flags*/
93 #define TZ_THFLAG_NONE          0
94 #define TZ_THFLAG_PSV           (1<<0)
95 #define TZ_THFLAG_HOT           (1<<2)
96 #define TZ_THFLAG_CRT           (1<<3)    
97     int                         tz_flags;
98 #define TZ_FLAG_NO_SCP          (1<<0)          /*No _SCP method*/
99 #define TZ_FLAG_GETPROFILE      (1<<1)          /*Get power_profile in timeout*/
100     struct timespec             tz_cooling_started;
101                                         /*Current cooling starting time*/
102
103     struct sysctl_ctx_list      tz_sysctl_ctx;
104     struct sysctl_oid           *tz_sysctl_tree;
105     
106     struct acpi_tz_zone         tz_zone;        /*Thermal zone parameters*/
107     int                         tz_tmp_updating;
108     int                         tz_validchecks;
109     /* sensors(9) related */
110     struct ksensordev           sensordev;
111     struct ksensor              sensor;
112 };
113
114 static int      acpi_tz_probe(device_t dev);
115 static int      acpi_tz_attach(device_t dev);
116 static int      acpi_tz_establish(struct acpi_tz_softc *sc);
117 static void     acpi_tz_monitor(void *Context);
118 static void     acpi_tz_all_off(struct acpi_tz_softc *sc);
119 static void     acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
120 static void     acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
121 static void     acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
122                                  int *data);
123 static void     acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
124 static int      acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
125 static void     acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
126                                        void *context);
127 static void     acpi_tz_timeout(struct acpi_tz_softc *sc);
128 static void     acpi_tz_power_profile(void *arg);
129 static void     acpi_tz_thread(void *arg);
130
131 static device_method_t acpi_tz_methods[] = {
132     /* Device interface */
133     DEVMETHOD(device_probe,     acpi_tz_probe),
134     DEVMETHOD(device_attach,    acpi_tz_attach),
135
136     {0, 0}
137 };
138
139 static driver_t acpi_tz_driver = {
140     "acpi_tz",
141     acpi_tz_methods,
142     sizeof(struct acpi_tz_softc),
143 };
144
145 static devclass_t acpi_tz_devclass;
146 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0);
147 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
148
149 static struct sysctl_ctx_list   acpi_tz_sysctl_ctx;
150 static struct sysctl_oid        *acpi_tz_sysctl_tree;
151
152 /* Minimum cooling run time */
153 static int                      acpi_tz_min_runtime = 0;
154 static int                      acpi_tz_polling_rate = TZ_POLLRATE;
155
156 /* Timezone polling thread */
157 static struct thread            *acpi_tz_td;
158
159 /*
160  * Match an ACPI thermal zone.
161  */
162 static int
163 acpi_tz_probe(device_t dev)
164 {
165     int         result;
166     ACPI_LOCK_DECL;
167     
168     ACPI_LOCK;
169     
170     /* No FUNCTION_TRACE - too noisy */
171
172     if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
173         device_set_desc(dev, "Thermal Zone");
174         result = -10;
175     } else {
176         result = ENXIO;
177     }
178     ACPI_UNLOCK;
179     return (result);
180 }
181
182 /*
183  * Attach to an ACPI thermal zone.
184  */
185 static int
186 acpi_tz_attach(device_t dev)
187 {
188     struct acpi_tz_softc        *sc;
189     struct acpi_softc           *acpi_sc;
190     int                         error;
191     char                        oidname[8];
192     ACPI_LOCK_DECL;
193
194     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
195
196     ACPI_LOCK;
197
198     sc = device_get_softc(dev);
199     sc->tz_dev = dev;
200     sc->tz_handle = acpi_get_handle(dev);
201     sc->tz_requested = TZ_ACTIVE_NONE;
202     sc->tz_tmp_updating = 0;
203
204     /*
205      * Parse the current state of the thermal zone and build control
206      * structures.
207      */
208     if ((error = acpi_tz_establish(sc)) != 0)
209         goto out;
210
211     /*
212      * Register for any Notify events sent to this zone.
213      */
214     AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY, 
215                              acpi_tz_notify_handler, sc);
216
217     /*
218      * Create our sysctl nodes.
219      *
220      * XXX we need a mechanism for adding nodes under ACPI.
221      */
222     if (device_get_unit(dev) == 0) {
223         acpi_sc = acpi_device_get_parent_softc(dev);
224         sysctl_ctx_init(&acpi_tz_sysctl_ctx);
225         acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
226                               SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
227                               OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
228         SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
229                        SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
230                        OID_AUTO, "min_runtime", CTLFLAG_RD | CTLFLAG_RW,
231                        &acpi_tz_min_runtime, 0,
232                        "minimum cooling run time in sec");
233         SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
234                        SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
235                        OID_AUTO, "polling_rate", CTLFLAG_RD | CTLFLAG_RW,
236                        &acpi_tz_polling_rate, 0, "monitor polling rate");
237     }
238     sysctl_ctx_init(&sc->tz_sysctl_ctx);
239     ksprintf(oidname, "tz%d", device_get_unit(dev));
240     sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
241                                          SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
242                                          OID_AUTO, oidname, CTLFLAG_RD, 0, "");
243     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
244                    OID_AUTO, "temperature", CTLFLAG_RD,
245                    &sc->tz_temperature, 0, "current thermal zone temperature");
246     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
247                     OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
248                     sc, 0, acpi_tz_active_sysctl, "I", "");
249     
250     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
251                    OID_AUTO, "thermal_flags", CTLFLAG_RD,
252                    &sc->tz_thflags, 0, "thermal zone flags");
253     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
254                    OID_AUTO, "_PSV", CTLFLAG_RD,
255                    &sc->tz_zone.psv, 0, "");
256     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
257                    OID_AUTO, "_HOT", CTLFLAG_RD,
258                    &sc->tz_zone.hot, 0, "");
259     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
260                    OID_AUTO, "_CRT", CTLFLAG_RD,
261                    &sc->tz_zone.crt, 0, "");
262     SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
263                       OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac,
264                       sizeof(sc->tz_zone.ac), "I", "");
265
266     /*
267      * Register our power profile event handler, and flag it for a manual
268      * invocation by our timeout.  We defer it like this so that the rest
269      * of the subsystem has time to come up.
270      */
271     EVENTHANDLER_REGISTER(power_profile_change, acpi_tz_power_profile, sc, 0);
272     sc->tz_flags |= TZ_FLAG_GETPROFILE;
273
274     /*
275      * Don't bother evaluating/printing the temperature at this point;
276      * on many systems it'll be bogus until the EC is running.
277      */
278
279     /*
280      * Create our thread; we only need one, it will service all of the
281      * thermal zones.
282      */
283     if (acpi_tz_td == NULL) {
284             error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_td,
285                                    RFHIGHPID, 0, "acpi_thermal");
286             if (error != 0) {
287                     device_printf(sc->tz_dev, "could not create thread - %d",
288                                   error);
289                     goto out;
290             }
291     }
292     /* Attach sensors(9). */
293     strlcpy(sc->sensordev.xname, device_get_nameunit(sc->tz_dev),
294         sizeof(sc->sensordev.xname));
295
296     sc->sensor.type = SENSOR_TEMP;
297     sensor_attach(&sc->sensordev, &sc->sensor);
298
299     sensordev_install(&sc->sensordev);
300
301 out:
302     ACPI_UNLOCK;
303
304     return_VALUE (error);
305 }
306
307 /*
308  * Parse the current state of this thermal zone and set up to use it.
309  *
310  * Note that we may have previous state, which will have to be discarded.
311  */
312 static int
313 acpi_tz_establish(struct acpi_tz_softc *sc)
314 {
315     ACPI_OBJECT *obj;
316     int         i;
317     char        nbuf[8];
318     
319     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
320
321     ACPI_ASSERTLOCK;
322
323     /* Power everything off and erase any existing state. */
324     acpi_tz_all_off(sc);
325     for (i = 0; i < TZ_NUMLEVELS; i++)
326         if (sc->tz_zone.al[i].Pointer != NULL)
327             AcpiOsFree(sc->tz_zone.al[i].Pointer);
328     if (sc->tz_zone.psl.Pointer != NULL)
329         AcpiOsFree(sc->tz_zone.psl.Pointer);
330     bzero(&sc->tz_zone, sizeof(sc->tz_zone));
331
332     /* Evaluate thermal zone parameters. */
333     for (i = 0; i < TZ_NUMLEVELS; i++) {
334         ksprintf(nbuf, "_AC%d", i);
335         acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
336         ksprintf(nbuf, "_AL%d", i);
337         sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
338         sc->tz_zone.al[i].Pointer = NULL;
339         AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
340         obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
341         if (obj != NULL) {
342             /* Should be a package containing a list of power objects */
343             if (obj->Type != ACPI_TYPE_PACKAGE) {
344                 device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
345                               nbuf, obj->Type);
346                 return_VALUE (ENXIO);
347             }
348         }
349     }
350     acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
351     acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
352     sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
353     sc->tz_zone.psl.Pointer = NULL;
354     AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
355     acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
356     acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
357     acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
358     acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
359     acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
360
361     /*
362      * Sanity-check the values we've been given.
363      *
364      * XXX what do we do about systems that give us the same value for
365      *     more than one of these setpoints?
366      */
367     acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
368     acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
369     acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
370     for (i = 0; i < TZ_NUMLEVELS; i++)
371         acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
372
373     /*
374      * Power off everything that we've just been given.
375      */
376     acpi_tz_all_off(sc);
377
378     return_VALUE (0);
379 }
380
381 static char     *aclevel_string[] =     {
382         "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
383         "_AC5", "_AC6", "_AC7", "_AC8", "_AC9" };
384
385 static __inline const char *
386 acpi_tz_aclevel_string(int active)
387 {
388         if (active < -1 || active >= TZ_NUMLEVELS)
389                 return (aclevel_string[0]);
390
391         return (aclevel_string[active+1]);
392 }
393
394 /*
395  * Evaluate the condition of a thermal zone, take appropriate actions.
396  */
397 static void
398 acpi_tz_monitor(void *Context)
399 {
400     struct acpi_tz_softc *sc;
401     struct      timespec curtime;
402     int         temp;
403     int         i;
404     int         newactive, newflags;
405     ACPI_STATUS status;
406
407     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
408
409     ACPI_ASSERTLOCK;
410
411     sc = (struct acpi_tz_softc *)Context;
412     if (sc->tz_tmp_updating)
413         goto out;
414     sc->tz_tmp_updating = 1;
415
416     /* Get the current temperature. */
417     status = acpi_GetInteger(sc->tz_handle, "_TMP", &temp);
418     if (ACPI_FAILURE(status)) {
419         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
420             "error fetching current temperature -- %s\n",
421              AcpiFormatException(status));
422         /* XXX disable zone? go to max cooling? */
423         goto out;
424     }
425
426     ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
427     sc->tz_temperature = temp;
428     /* Update sensor */
429     if(sc->tz_temperature == -1)
430         sc->sensor.flags &= ~SENSOR_FINVALID;
431     sc->sensor.value = sc->tz_temperature * 100000;
432
433     /*
434      * Work out what we ought to be doing right now.
435      *
436      * Note that the _ACx levels sort from hot to cold.
437      */
438     newactive = TZ_ACTIVE_NONE;
439     for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
440         if ((sc->tz_zone.ac[i] != -1) && (temp >= sc->tz_zone.ac[i])) {
441             newactive = i;
442             if (sc->tz_active != newactive) {
443                 ACPI_VPRINT(sc->tz_dev,
444                             acpi_device_get_parent_softc(sc->tz_dev),
445                             "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i,
446                             TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i]));
447                 getnanotime(&sc->tz_cooling_started);
448             }
449         }
450     }
451
452     /*
453      * We are going to get _ACx level down (colder side), but give a guaranteed
454      * minimum cooling run time if requested.
455      */
456     if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
457         (newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
458
459         getnanotime(&curtime);
460         timespecsub(&curtime, &sc->tz_cooling_started);
461         if (curtime.tv_sec < acpi_tz_min_runtime)
462             newactive = sc->tz_active;
463     }
464
465     /* Handle user override of active mode */
466     if (sc->tz_requested != TZ_ACTIVE_NONE && sc->tz_requested < newactive)
467         newactive = sc->tz_requested;
468
469     /* update temperature-related flags */
470     newflags = TZ_THFLAG_NONE;
471     if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
472         newflags |= TZ_THFLAG_PSV;
473     if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
474         newflags |= TZ_THFLAG_HOT;
475     if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
476         newflags |= TZ_THFLAG_CRT;
477
478     /* If the active cooling state has changed, we have to switch things. */
479     if (newactive != sc->tz_active) {
480         /* Turn off the cooling devices that are on, if any are */
481         if (sc->tz_active != TZ_ACTIVE_NONE)
482             acpi_ForeachPackageObject(
483                 (ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer,
484                 acpi_tz_switch_cooler_off, sc);
485
486         /* Turn on cooling devices that are required, if any are */
487         if (newactive != TZ_ACTIVE_NONE) {
488             acpi_ForeachPackageObject(
489                 (ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer,
490                 acpi_tz_switch_cooler_on, sc);
491         }
492         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
493                     "switched from %s to %s: %d.%dC\n",
494                     acpi_tz_aclevel_string(sc->tz_active),
495                     acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
496         sc->tz_active = newactive;
497     }
498
499     /* XXX (de)activate any passive cooling that may be required. */
500
501     /*
502      * If the temperature is at _HOT or _CRT, increment our event count.
503      * If it has occurred enough times, shutdown the system.  This is
504      * needed because some systems will report an invalid high temperature
505      * for one poll cycle.  It is suspected this is due to the embedded
506      * controller timing out.  A typical value is 138C for one cycle on
507      * a system that is otherwise 65C.
508      *
509      * If we're almost at that threshold, notify the user through devd(8).
510      */
511     if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
512         sc->tz_validchecks++;
513         if (sc->tz_validchecks == TZ_VALIDCHECKS) {
514             device_printf(sc->tz_dev,
515                 "WARNING - current temperature (%d.%dC) exceeds safe limits\n",
516                 TZ_KELVTOC(sc->tz_temperature));
517             shutdown_nice(RB_POWEROFF);
518         } else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
519             acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
520     } else {
521         sc->tz_validchecks = 0;
522     }
523     sc->tz_thflags = newflags;
524
525 out:
526     sc->tz_tmp_updating = 0;
527     return_VOID;
528 }
529
530 /*
531  * Turn off all the cooling devices.
532  */
533 static void
534 acpi_tz_all_off(struct acpi_tz_softc *sc)
535 {
536     int         i;
537
538     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
539
540     ACPI_ASSERTLOCK;
541     
542     /* Scan all the _ALx objects and turn them all off. */
543     for (i = 0; i < TZ_NUMLEVELS; i++) {
544         if (sc->tz_zone.al[i].Pointer == NULL)
545             continue;
546         acpi_ForeachPackageObject((ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
547                                   acpi_tz_switch_cooler_off, sc);
548     }
549
550     /*
551      * XXX revert any passive-cooling options.
552      */
553
554     sc->tz_active = TZ_ACTIVE_NONE;
555     sc->tz_thflags = TZ_THFLAG_NONE;
556
557     return_VOID;
558 }
559
560 /*
561  * Given an object, verify that it's a reference to a device of some sort, 
562  * and try to switch it off.
563  */
564 static void
565 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
566 {
567     ACPI_HANDLE                 cooler;
568
569     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
570
571     ACPI_ASSERTLOCK;
572
573     cooler = acpi_GetReference(NULL, obj);
574     if (cooler == NULL) {
575         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
576         return_VOID;
577     }
578
579     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
580                      acpi_name(cooler)));
581     acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
582
583     return_VOID;
584 }
585
586 /*
587  * Given an object, verify that it's a reference to a device of some sort, 
588  * and try to switch it on.
589  *
590  * XXX replication of off/on function code is bad.
591  */
592 static void
593 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
594 {
595     struct acpi_tz_softc        *sc = (struct acpi_tz_softc *)arg;
596     ACPI_HANDLE                 cooler;
597     ACPI_STATUS                 status;
598     
599     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
600
601     ACPI_ASSERTLOCK;
602
603     cooler = acpi_GetReference(NULL, obj);
604     if (cooler == NULL) {
605         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
606         return_VOID;
607     }
608
609     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
610                      acpi_name(cooler)));
611     status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
612     if (ACPI_FAILURE(status)) {
613         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
614                     "failed to activate %s - %s\n", acpi_name(cooler),
615                     AcpiFormatException(status));
616     }
617
618     return_VOID;
619 }
620
621 /*
622  * Read/debug-print a parameter, default it to -1.
623  */
624 static void
625 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
626 {
627
628     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
629
630     ACPI_ASSERTLOCK;
631
632     if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
633         *data = -1;
634     } else {
635         ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
636                          acpi_name(sc->tz_handle), node, *data));
637     }
638
639     return_VOID;    
640 }
641
642 /*
643  * Sanity-check a temperature value.  Assume that setpoints
644  * should be between 0C and 150C.
645  */
646 static void
647 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
648 {
649     if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 1500)) {
650         device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
651                       what, TZ_KELVTOC(*val));
652         *val = -1;
653     }
654 }
655
656 /*
657  * Respond to a sysctl on the active state node.
658  */    
659 static int
660 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
661 {
662     struct acpi_tz_softc        *sc;
663     int                         active;
664     int                         error;
665     ACPI_LOCK_DECL;
666
667     ACPI_LOCK;
668
669     sc = (struct acpi_tz_softc *)oidp->oid_arg1;
670     active = sc->tz_active;
671     error = sysctl_handle_int(oidp, &active, 0, req);
672
673     /* Error or no new value */
674     if (error != 0 || req->newptr == NULL)
675         goto out;
676     if (active < -1 || active >= TZ_NUMLEVELS) {
677         error = EINVAL;
678         goto out;
679     }
680
681     /* Set new preferred level and re-switch */
682     sc->tz_requested = active;
683     acpi_tz_monitor(sc);
684
685  out:
686     ACPI_UNLOCK;
687     return (error);
688 }
689
690 /*
691  * Respond to a Notify event sent to the zone.
692  */
693 static void
694 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
695 {
696     struct acpi_tz_softc        *sc = (struct acpi_tz_softc *)context;
697
698     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
699
700     ACPI_ASSERTLOCK;
701
702     switch(notify) {
703     case TZ_NOTIFY_TEMPERATURE:
704         /* Temperature change occurred */
705         AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_tz_monitor, sc);
706         break;
707     case TZ_NOTIFY_DEVICES:
708     case TZ_NOTIFY_LEVELS:
709         /* Zone devices/setpoints changed */
710         AcpiOsExecute(OSL_NOTIFY_HANDLER,
711                                 (ACPI_OSD_EXEC_CALLBACK)acpi_tz_establish, sc);
712         break;
713     default:
714         ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
715                     "unknown Notify event 0x%x\n", notify);
716         break;
717     }
718
719     acpi_UserNotify("Thermal", h, notify);
720
721     return_VOID;
722 }
723
724 /*
725  * Poll the thermal zone.
726  */
727 static void
728 acpi_tz_timeout(struct acpi_tz_softc *sc)
729 {
730     /* Do we need to get the power profile settings? */
731     if (sc->tz_flags & TZ_FLAG_GETPROFILE) {
732         acpi_tz_power_profile((void *)sc);
733         sc->tz_flags &= ~TZ_FLAG_GETPROFILE;
734     }
735
736     ACPI_ASSERTLOCK;
737
738     /* Check the current temperature and take action based on it */
739     acpi_tz_monitor(sc);
740
741     /* XXX passive cooling actions? */
742 }
743
744 /*
745  * System power profile may have changed; fetch and notify the
746  * thermal zone accordingly.
747  *
748  * Since this can be called from an arbitrary eventhandler, it needs
749  * to get the ACPI lock itself.
750  */
751 static void
752 acpi_tz_power_profile(void *arg)
753 {
754     ACPI_STATUS                 status;
755     struct acpi_tz_softc        *sc = (struct acpi_tz_softc *)arg;
756     int                         state;
757     ACPI_LOCK_DECL;
758
759     state = power_profile_get_state();
760     if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
761         return;
762
763     ACPI_LOCK;
764
765     /* check that we haven't decided there's no _SCP method */
766     if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
767
768         /* Call _SCP to set the new profile */
769         status = acpi_SetInteger(sc->tz_handle, "_SCP",
770             (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
771         if (ACPI_FAILURE(status)) {
772             if (status != AE_NOT_FOUND)
773                 ACPI_VPRINT(sc->tz_dev,
774                             acpi_device_get_parent_softc(sc->tz_dev),
775                             "can't evaluate %s._SCP - %s\n",
776                             acpi_name(sc->tz_handle),
777                             AcpiFormatException(status));
778             sc->tz_flags |= TZ_FLAG_NO_SCP;
779         } else {
780             /* We have to re-evaluate the entire zone now */
781             AcpiOsExecute(OSL_NOTIFY_HANDLER,
782                                     (ACPI_OSD_EXEC_CALLBACK)acpi_tz_establish,
783                                     sc);
784         }
785     }
786
787     ACPI_UNLOCK;
788 }
789
790 /*
791  * Thermal zone monitor thread.
792  */
793 static void
794 acpi_tz_thread(void *arg)
795 {
796     device_t    *devs;
797     int         devcount, i;
798     ACPI_LOCK_DECL;
799
800     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
801
802     devs = NULL;
803     devcount = 0;
804
805     for (;;) {
806         tsleep(&acpi_tz_td, 0, "tzpoll", hz * acpi_tz_polling_rate);
807
808 #if __FreeBSD_version >= 500000
809         mtx_lock(&Giant);
810 #endif
811
812         if (devcount == 0)
813             devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
814
815         ACPI_LOCK;
816         for (i = 0; i < devcount; i++)
817             acpi_tz_timeout(device_get_softc(devs[i]));
818         ACPI_UNLOCK;
819
820 #if __FreeBSD_version >= 500000
821         mtx_unlock(&Giant);
822 #endif
823     }
824 }