The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/acpica/acpi_thermal.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 77cd919072bbe263d61bfdba54abf979


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.