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