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

Cache object: 16470d5040c2f84e3e2819c24a6dd9f1


[ 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.