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 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  * $FreeBSD: releng/6.3/sys/dev/coretemp/coretemp.c 172376 2007-09-28 08:54:41Z des $
   27  *
   28  */
   29 
   30 /*
   31  * Device driver for Intel's On Die thermal sensor via MSR.
   32  * First introduced in Intel's Core line of processors.
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD: releng/6.3/sys/dev/coretemp/coretemp.c 172376 2007-09-28 08:54:41Z des $");
   37 
   38 #include <sys/param.h>
   39 #include <sys/bus.h>
   40 #include <sys/systm.h>
   41 #include <sys/types.h>
   42 #include <sys/module.h>
   43 #include <sys/conf.h>
   44 #include <sys/kernel.h>
   45 #include <sys/sysctl.h>
   46 #include <sys/proc.h>   /* for curthread */
   47 #include <sys/sched.h>
   48 
   49 #include <machine/specialreg.h>
   50 #include <machine/cpufunc.h>
   51 #include <machine/md_var.h>
   52 
   53 struct coretemp_softc {
   54         device_t        sc_dev;
   55         int             sc_tjmax;
   56         struct sysctl_oid *sc_oid;
   57 };
   58 
   59 /*
   60  * Device methods.
   61  */
   62 static void     coretemp_identify(driver_t *driver, device_t parent);
   63 static int      coretemp_probe(device_t dev);
   64 static int      coretemp_attach(device_t dev);
   65 static int      coretemp_detach(device_t dev);
   66 
   67 static int      coretemp_get_temp(device_t dev);
   68 static int      coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS);
   69 
   70 static device_method_t coretemp_methods[] = {
   71         /* Device interface */
   72         DEVMETHOD(device_identify,      coretemp_identify),
   73         DEVMETHOD(device_probe,         coretemp_probe),
   74         DEVMETHOD(device_attach,        coretemp_attach),
   75         DEVMETHOD(device_detach,        coretemp_detach),
   76 
   77         {0, 0}
   78 };
   79 
   80 static driver_t coretemp_driver = {
   81         "coretemp",
   82         coretemp_methods,
   83         sizeof(struct coretemp_softc),
   84 };
   85 
   86 static devclass_t coretemp_devclass;
   87 DRIVER_MODULE(coretemp, cpu, coretemp_driver, coretemp_devclass, NULL, NULL);
   88 
   89 static void
   90 coretemp_identify(driver_t *driver, device_t parent)
   91 {
   92         device_t child;
   93         u_int regs[4];
   94 
   95         /* Make sure we're not being doubly invoked. */
   96         if (device_find_child(parent, "coretemp", -1) != NULL)
   97                 return;
   98 
   99         /* Check that CPUID is supported and the vendor is Intel.*/
  100         if (cpu_high == 0 || strcmp(cpu_vendor, "GenuineIntel"))
  101                 return;
  102         /*
  103          * CPUID 0x06 returns 1 if the processor has on-die thermal
  104          * sensors. EBX[0:3] contains the number of sensors.
  105          */
  106         do_cpuid(0x06, regs);
  107         if ((regs[0] & 0x1) != 1)
  108                 return;
  109 
  110         /*
  111          * We add a child for each CPU since settings must be performed
  112          * on each CPU in the SMP case.
  113          */
  114         child = device_add_child(parent, "coretemp", -1);
  115         if (child == NULL)
  116                 device_printf(parent, "add coretemp child failed\n");
  117 }
  118 
  119 static int
  120 coretemp_probe(device_t dev)
  121 {
  122         if (resource_disabled("coretemp", 0))
  123                 return (ENXIO);
  124 
  125         device_set_desc(dev, "CPU On-Die Thermal Sensors");
  126 
  127         return (BUS_PROBE_GENERIC);
  128 }
  129 
  130 static int
  131 coretemp_attach(device_t dev)
  132 {
  133         struct coretemp_softc *sc = device_get_softc(dev);
  134         device_t pdev;
  135         uint64_t msr;
  136         int cpu_model;
  137         int cpu_mask;
  138 
  139         sc->sc_dev = dev;
  140         pdev = device_get_parent(dev);
  141         cpu_model = (cpu_id >> 4) & 15;
  142         /* extended model */
  143         cpu_model += ((cpu_id >> 16) & 0xf) << 4;
  144         cpu_mask = cpu_id & 15;
  145 
  146         /*
  147          * Check for errata AE18.
  148          * "Processor Digital Thermal Sensor (DTS) Readout stops
  149          *  updating upon returning from C3/C4 state."
  150          *
  151          * Adapted from the Linux coretemp driver.
  152          */
  153         if (cpu_model == 0xe && cpu_mask < 0xc) {
  154                 msr = rdmsr(MSR_BIOS_SIGN);
  155                 msr = msr >> 32;
  156                 if (msr < 0x39) {
  157                         device_printf(dev, "not supported (Intel errata "
  158                             "AE18), try updating your BIOS\n");
  159                         return (ENXIO);
  160                 }
  161         }
  162         /*
  163          * On some Core 2 CPUs, there's an undocumented MSR that
  164          * can tell us if Tj(max) is 100 or 85.
  165          *
  166          * The if-clause for CPUs having the MSR_IA32_EXT_CONFIG was adapted
  167          * from the Linux coretemp driver.
  168          */
  169         sc->sc_tjmax = 100;
  170         if ((cpu_model == 0xf && cpu_mask >= 2) || cpu_model == 0xe) {
  171                 msr = rdmsr(MSR_IA32_EXT_CONFIG);
  172                 if (msr & (1 << 30))
  173                         sc->sc_tjmax = 85;
  174         }
  175 
  176         /*
  177          * Add the "temperature" MIB to dev.cpu.N.
  178          */
  179         sc->sc_oid = SYSCTL_ADD_PROC(device_get_sysctl_ctx(pdev),
  180             SYSCTL_CHILDREN(device_get_sysctl_tree(pdev)),
  181             OID_AUTO, "temperature",
  182             CTLTYPE_INT | CTLFLAG_RD,
  183             dev, 0, coretemp_get_temp_sysctl, "I",
  184             "Current temperature in degC");
  185 
  186         return (0);
  187 }
  188 
  189 static int
  190 coretemp_detach(device_t dev)
  191 {
  192         struct coretemp_softc *sc = device_get_softc(dev);
  193 
  194         sysctl_remove_oid(sc->sc_oid, 1, 0);
  195 
  196         return (0);
  197 }
  198 
  199 
  200 static int
  201 coretemp_get_temp(device_t dev)
  202 {
  203         uint64_t msr;
  204         int temp;
  205         int cpu = device_get_unit(dev);
  206         struct coretemp_softc *sc = device_get_softc(dev);
  207         char stemp[16];
  208 
  209         mtx_lock_spin(&sched_lock);
  210         sched_bind(curthread, cpu);
  211         mtx_unlock_spin(&sched_lock);
  212 
  213         /*
  214          * The digital temperature reading is located at bit 16
  215          * of MSR_THERM_STATUS.
  216          *
  217          * There is a bit on that MSR that indicates whether the
  218          * temperature is valid or not.
  219          *
  220          * The temperature is computed by subtracting the temperature
  221          * reading by Tj(max).
  222          */
  223         msr = rdmsr(MSR_THERM_STATUS);
  224 
  225         mtx_lock_spin(&sched_lock);
  226         sched_unbind(curthread);
  227         mtx_unlock_spin(&sched_lock);
  228 
  229         /*
  230          * Check for Thermal Status and Thermal Status Log.
  231          */
  232         if ((msr & 0x3) == 0x3)
  233                 device_printf(dev, "PROCHOT asserted\n");
  234 
  235         /*
  236          * Bit 31 contains "Reading valid"
  237          */
  238         if (((msr >> 31) & 0x1) == 1) {
  239                 /*
  240                  * Starting on bit 16 and ending on bit 22.
  241                  */
  242                 temp = sc->sc_tjmax - ((msr >> 16) & 0x7f);
  243         } else
  244                 temp = -1;
  245 
  246         /*
  247          * Check for Critical Temperature Status and Critical
  248          * Temperature Log.
  249          * It doesn't really matter if the current temperature is
  250          * invalid because the "Critical Temperature Log" bit will
  251          * tell us if the Critical Temperature has been reached in
  252          * past. It's not directly related to the current temperature.
  253          *
  254          * If we reach a critical level, allow devctl(4) to catch this
  255          * and shutdown the system.
  256          */
  257         if (((msr >> 4) & 0x3) == 0x3) {
  258                 device_printf(dev, "critical temperature detected, "
  259                     "suggest system shutdown\n");
  260                 snprintf(stemp, sizeof(stemp), "%d", temp);
  261                 devctl_notify("coretemp", "Thermal", stemp, "notify=0xcc");
  262         }
  263 
  264         return (temp);
  265 }
  266 
  267 static int
  268 coretemp_get_temp_sysctl(SYSCTL_HANDLER_ARGS)
  269 {
  270         device_t dev = (device_t) arg1;
  271         int temp;
  272 
  273         temp = coretemp_get_temp(dev);
  274 
  275         return (sysctl_handle_int(oidp, &temp, 0, req));
  276 }

Cache object: af4f08049af396828736409b06c422dc


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