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/coretemp/coretemp.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2007, 2008 Rui Paulo <rpaulo@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   17  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
   18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   20  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   23  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   24  * POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 /*
   28  * Device driver for Intel's On Die thermal sensor via MSR.
   29  * First introduced in Intel's Core line of processors.
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/7.3/sys/dev/coretemp/coretemp.c 195197 2009-06-30 17:10:08Z avg $");
   34 
   35 #include <sys/param.h>
   36 #include <sys/bus.h>
   37 #include <sys/systm.h>
   38 #include <sys/types.h>
   39 #include <sys/module.h>
   40 #include <sys/conf.h>
   41 #include <sys/kernel.h>
   42 #include <sys/sysctl.h>
   43 #include <sys/proc.h>   /* for curthread */
   44 #include <sys/sched.h>
   45 
   46 #include <machine/specialreg.h>
   47 #include <machine/cpufunc.h>
   48 #include <machine/cputypes.h>
   49 #include <machine/md_var.h>
   50 
   51 struct coretemp_softc {
   52         device_t        sc_dev;
   53         int             sc_tjmax;
   54         struct sysctl_oid *sc_oid;
   55 };
   56 
   57 /*
   58  * Device methods.
   59  */
   60 static void     coretemp_identify(driver_t *driver, device_t parent);
   61 static int      coretemp_probe(device_t dev);
   62 static int      coretemp_attach(device_t dev);
   63 static int      coretemp_detach(device_t dev);
   64 
   65 static int      coretemp_get_temp(device_t dev);
   66 static int      coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS);
   67 
   68 static device_method_t coretemp_methods[] = {
   69         /* Device interface */
   70         DEVMETHOD(device_identify,      coretemp_identify),
   71         DEVMETHOD(device_probe,         coretemp_probe),
   72         DEVMETHOD(device_attach,        coretemp_attach),
   73         DEVMETHOD(device_detach,        coretemp_detach),
   74 
   75         {0, 0}
   76 };
   77 
   78 static driver_t coretemp_driver = {
   79         "coretemp",
   80         coretemp_methods,
   81         sizeof(struct coretemp_softc),
   82 };
   83 
   84 static devclass_t coretemp_devclass;
   85 DRIVER_MODULE(coretemp, cpu, coretemp_driver, coretemp_devclass, NULL, NULL);
   86 
   87 static void
   88 coretemp_identify(driver_t *driver, device_t parent)
   89 {
   90         device_t child;
   91         u_int regs[4];
   92 
   93         /* Make sure we're not being doubly invoked. */
   94         if (device_find_child(parent, "coretemp", -1) != NULL)
   95                 return;
   96 
   97         /* Check that CPUID 0x06 is supported and the vendor is Intel.*/
   98         if (cpu_high < 6 || cpu_vendor_id != CPU_VENDOR_INTEL)
   99                 return;
  100         /*
  101          * CPUID 0x06 returns 1 if the processor has on-die thermal
  102          * sensors. EBX[0:3] contains the number of sensors.
  103          */
  104         do_cpuid(0x06, regs);
  105         if ((regs[0] & 0x1) != 1)
  106                 return;
  107 
  108         /*
  109          * We add a child for each CPU since settings must be performed
  110          * on each CPU in the SMP case.
  111          */
  112         child = device_add_child(parent, "coretemp", -1);
  113         if (child == NULL)
  114                 device_printf(parent, "add coretemp child failed\n");
  115 }
  116 
  117 static int
  118 coretemp_probe(device_t dev)
  119 {
  120         if (resource_disabled("coretemp", 0))
  121                 return (ENXIO);
  122 
  123         device_set_desc(dev, "CPU On-Die Thermal Sensors");
  124 
  125         return (BUS_PROBE_GENERIC);
  126 }
  127 
  128 static int
  129 coretemp_attach(device_t dev)
  130 {
  131         struct coretemp_softc *sc = device_get_softc(dev);
  132         device_t pdev;
  133         uint64_t msr;
  134         int cpu_model;
  135         int cpu_mask;
  136 
  137         sc->sc_dev = dev;
  138         pdev = device_get_parent(dev);
  139         cpu_model = (cpu_id >> 4) & 15;
  140         /* extended model */
  141         cpu_model += ((cpu_id >> 16) & 0xf) << 4;
  142         cpu_mask = cpu_id & 15;
  143 
  144         /*
  145          * Some CPUs, namely the PIII, don't have thermal sensors, but
  146          * report them when the CPUID check is performed in
  147          * coretemp_identify(). This leads to a later GPF when the sensor
  148          * is queried via a MSR, so we stop here.
  149          */
  150         if (cpu_model < 0xe)
  151                 return (ENXIO);
  152 
  153         /*
  154          * Check for errata AE18.
  155          * "Processor Digital Thermal Sensor (DTS) Readout stops
  156          *  updating upon returning from C3/C4 state."
  157          *
  158          * Adapted from the Linux coretemp driver.
  159          */
  160         if (cpu_model == 0xe && cpu_mask < 0xc) {
  161                 msr = rdmsr(MSR_BIOS_SIGN);
  162                 msr = msr >> 32;
  163                 if (msr < 0x39) {
  164                         device_printf(dev, "not supported (Intel errata "
  165                             "AE18), try updating your BIOS\n");
  166                         return (ENXIO);
  167                 }
  168         }
  169         /*
  170          * On some Core 2 CPUs, there's an undocumented MSR that
  171          * can tell us if Tj(max) is 100 or 85.
  172          *
  173          * The if-clause for CPUs having the MSR_IA32_EXT_CONFIG was adapted
  174          * from the Linux coretemp driver.
  175          */
  176         sc->sc_tjmax = 100;
  177         if ((cpu_model == 0xf && cpu_mask >= 2) || cpu_model == 0xe) {
  178                 msr = rdmsr(MSR_IA32_EXT_CONFIG);
  179                 if (msr & (1 << 30))
  180                         sc->sc_tjmax = 85;
  181         }
  182 
  183         /*
  184          * Add the "temperature" MIB to dev.cpu.N.
  185          */
  186         sc->sc_oid = SYSCTL_ADD_PROC(device_get_sysctl_ctx(pdev),
  187             SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),
  188             OID_AUTO, "temperature",
  189             CTLTYPE_INT | CTLFLAG_RD,
  190             dev, 0, coretemp_get_temp_sysctl, "I",
  191             "Current temperature in degC");
  192 
  193         return (0);
  194 }
  195 
  196 static int
  197 coretemp_detach(device_t dev)
  198 {
  199         struct coretemp_softc *sc = device_get_softc(dev);
  200 
  201         sysctl_remove_oid(sc->sc_oid, 1, 0);
  202 
  203         return (0);
  204 }
  205 
  206 
  207 static int
  208 coretemp_get_temp(device_t dev)
  209 {
  210         uint64_t msr;
  211         int temp;
  212         int cpu = device_get_unit(dev);
  213         struct coretemp_softc *sc = device_get_softc(dev);
  214         char stemp[16];
  215 
  216         thread_lock(curthread);
  217         sched_bind(curthread, cpu);
  218         thread_unlock(curthread);
  219 
  220         /*
  221          * The digital temperature reading is located at bit 16
  222          * of MSR_THERM_STATUS.
  223          *
  224          * There is a bit on that MSR that indicates whether the
  225          * temperature is valid or not.
  226          *
  227          * The temperature is computed by subtracting the temperature
  228          * reading by Tj(max).
  229          */
  230         msr = rdmsr(MSR_THERM_STATUS);
  231 
  232         thread_lock(curthread);
  233         sched_unbind(curthread);
  234         thread_unlock(curthread);
  235 
  236         /*
  237          * Check for Thermal Status and Thermal Status Log.
  238          */
  239         if ((msr & 0x3) == 0x3)
  240                 device_printf(dev, "PROCHOT asserted\n");
  241 
  242         /*
  243          * Bit 31 contains "Reading valid"
  244          */
  245         if (((msr >> 31) & 0x1) == 1) {
  246                 /*
  247                  * Starting on bit 16 and ending on bit 22.
  248                  */
  249                 temp = sc->sc_tjmax - ((msr >> 16) & 0x7f);
  250         } else
  251                 temp = -1;
  252 
  253         /*
  254          * Check for Critical Temperature Status and Critical
  255          * Temperature Log.
  256          * It doesn't really matter if the current temperature is
  257          * invalid because the "Critical Temperature Log" bit will
  258          * tell us if the Critical Temperature has been reached in
  259          * past. It's not directly related to the current temperature.
  260          *
  261          * If we reach a critical level, allow devctl(4) to catch this
  262          * and shutdown the system.
  263          */
  264         if (((msr >> 4) & 0x3) == 0x3) {
  265                 device_printf(dev, "critical temperature detected, "
  266                     "suggest system shutdown\n");
  267                 snprintf(stemp, sizeof(stemp), "%d", temp);
  268                 devctl_notify("coretemp", "Thermal", stemp, "notify=0xcc");
  269         }
  270 
  271         return (temp);
  272 }
  273 
  274 static int
  275 coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS)
  276 {
  277         device_t dev = (device_t) arg1;
  278         int temp;
  279 
  280         temp = coretemp_get_temp(dev);
  281 
  282         return (sysctl_handle_int(oidp, &temp, 0, req));
  283 }

Cache object: 8aa69efb453303df8e51e7a53458cab6


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