2 * coretemp.c - Linux kernel module for hardware monitoring
4 * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
6 * Inspired from many hwmon drivers
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/hwmon.h>
30 #include <linux/sysfs.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <linux/list.h>
35 #include <linux/platform_device.h>
36 #include <linux/cpu.h>
37 #include <linux/pci.h>
38 #include <linux/smp.h>
39 #include <linux/moduleparam.h>
41 #include <asm/cpu_device_id.h>
43 #include "../xen/core/domctl.h"
45 #define DRVNAME "coretemp"
48 * force_tjmax only matters when TjMax can't be read from the CPU itself.
49 * When set, it replaces the driver's suboptimal heuristic.
51 static int force_tjmax;
52 module_param_named(tjmax, force_tjmax, int, 0444);
53 MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
55 #define BASE_SYSFS_ATTR_NO 2 /* Sysfs Base attr no for coretemp */
56 #define NUM_REAL_CORES 16 /* Number of Real cores per cpu */
57 #define CORETEMP_NAME_LENGTH 17 /* String Length of attrs */
58 #define MAX_CORE_ATTRS 4 /* Maximum no of basic attrs */
59 #define TOTAL_ATTRS (MAX_CORE_ATTRS + 1)
60 #define MAX_CORE_DATA (NUM_REAL_CORES + BASE_SYSFS_ATTR_NO)
62 #define TO_PHYS_ID(cpu) ({ \
64 !xen_get_topology_info(cpu, NULL, &ppid, NULL) ? ppid : ~0; \
66 #define CORE_ATTR_NO(ccid) ((ccid) + BASE_SYSFS_ATTR_NO)
69 * Per-Core Temperature Data
70 * @last_updated: The time when the current temperature value was updated
71 * earlier (in jiffies).
72 * @cpu_core_id: The CPU Core from which temperature values should be read
73 * This value is passed as "id" field to rdmsr/wrmsr functions.
74 * @status_reg: One of IA32_THERM_STATUS or IA32_PACKAGE_THERM_STATUS,
75 * from where the temperature values should be read.
76 * @attr_size: Total number of pre-core attrs displayed in the sysfs.
77 * @is_pkg_data: If this is 1, the temp_data holds pkgtemp data.
78 * Otherwise, temp_data holds coretemp data.
79 * @valid: If this is 1, the current temperature is valid.
85 unsigned long last_updated;
92 struct sensor_device_attribute sd_attrs[TOTAL_ATTRS];
93 char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH];
94 struct mutex update_lock;
97 /* Platform Data per Physical CPU */
98 struct platform_data {
99 struct device *hwmon_dev;
101 u8 x86_model, x86_mask;
102 struct temp_data *core_data[MAX_CORE_DATA];
103 struct device_attribute name_attr;
107 struct list_head list;
108 struct platform_device *pdev;
113 struct platform_device *pdev;
114 u32 cpuid_6_eax, microcode;
115 u32 phys_proc_id, cpu_core_id;
116 u8 x86_model, x86_mask;
119 static LIST_HEAD(pdev_list);
120 static DEFINE_MUTEX(pdev_list_mutex);
122 static ssize_t show_name(struct device *dev,
123 struct device_attribute *devattr, char *buf)
125 return sprintf(buf, "%s\n", DRVNAME);
128 static ssize_t show_label(struct device *dev,
129 struct device_attribute *devattr, char *buf)
131 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
132 struct platform_data *pdata = dev_get_drvdata(dev);
133 struct temp_data *tdata = pdata->core_data[attr->index];
135 if (tdata->is_pkg_data)
136 return sprintf(buf, "Physical id %u\n", pdata->phys_proc_id);
138 return sprintf(buf, "Core %u\n", tdata->cpu_core_id);
141 static ssize_t show_crit_alarm(struct device *dev,
142 struct device_attribute *devattr, char *buf)
145 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
146 struct platform_data *pdata = dev_get_drvdata(dev);
147 struct temp_data *tdata = pdata->core_data[attr->index];
149 if (rdmsr_safe_on_pcpu(tdata->cpu, tdata->status_reg, &eax, &edx) < 0)
150 return sprintf(buf, "\n");
152 return sprintf(buf, "%d\n", (eax >> 5) & 1);
155 static ssize_t show_tjmax(struct device *dev,
156 struct device_attribute *devattr, char *buf)
158 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
159 struct platform_data *pdata = dev_get_drvdata(dev);
161 return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);
164 static ssize_t show_ttarget(struct device *dev,
165 struct device_attribute *devattr, char *buf)
167 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
168 struct platform_data *pdata = dev_get_drvdata(dev);
170 return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget);
173 static ssize_t show_temp(struct device *dev,
174 struct device_attribute *devattr, char *buf)
177 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
178 struct platform_data *pdata = dev_get_drvdata(dev);
179 struct temp_data *tdata = pdata->core_data[attr->index];
181 mutex_lock(&tdata->update_lock);
183 /* Check whether the time interval has elapsed */
184 if (!tdata->valid || time_after(jiffies, tdata->last_updated + HZ)) {
185 if (rdmsr_safe_on_pcpu(tdata->cpu, tdata->status_reg,
189 /* Check whether the data is valid */
190 if (eax & 0x80000000) {
191 tdata->temp = tdata->tjmax -
192 ((eax >> 16) & 0x7f) * 1000;
195 tdata->last_updated = jiffies;
198 mutex_unlock(&tdata->update_lock);
199 return tdata->valid ? sprintf(buf, "%d\n", tdata->temp) : -EAGAIN;
202 static int adjust_tjmax(struct platform_data *c, u32 id, struct device *dev)
204 /* The 100C is default for both mobile and non mobile CPUs */
207 int tjmax_ee = 85000;
211 struct pci_dev *host_bridge;
213 /* Early chips have no MSR for TjMax */
215 if (c->x86_model == 0xf && c->x86_mask < 4)
220 if (c->x86_model == 0x1c) {
223 host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
225 if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL
226 && (host_bridge->device == 0xa000 /* NM10 based nettop */
227 || host_bridge->device == 0xa010)) /* NM10 based netbook */
232 pci_dev_put(host_bridge);
235 if (c->x86_model > 0xe && usemsr_ee) {
239 * Now we can detect the mobile CPU using Intel provided table
240 * http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
241 * For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
243 err = rdmsr_safe_on_pcpu(id, 0x17, &eax, &edx);
246 "Unable to access MSR 0x17, assuming desktop"
249 } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
251 * Trust bit 28 up to Penryn, I could not find any
252 * documentation on that; if you happen to know
253 * someone at Intel please ask
257 /* Platform ID bits 52:50 (EDX starts at bit 32) */
258 platform_id = (edx >> 18) & 0x7;
261 * Mobile Penryn CPU seems to be platform ID 7 or 5
264 if (c->x86_model == 0x17 &&
265 (platform_id == 5 || platform_id == 7)) {
267 * If MSR EE bit is set, set it to 90 degrees C,
268 * otherwise 105 degrees C
277 err = rdmsr_safe_on_pcpu(id, 0xee, &eax, &edx);
280 "Unable to access MSR 0xEE, for Tjmax, left"
282 } else if (eax & 0x40000000) {
285 } else if (tjmax == 100000) {
287 * If we don't use msr EE it means we are desktop CPU
288 * (with exeception of Atom)
290 dev_warn(dev, "Using relative temperature scale!\n");
296 static int get_tjmax(struct platform_data *c, u32 id, struct device *dev)
303 * A new feature of current Intel(R) processors, the
304 * IA32_TEMPERATURE_TARGET contains the TjMax value
306 err = rdmsr_safe_on_pcpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
308 if (c->x86_model > 0xe && c->x86_model != 0x1c)
309 dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);
311 val = (eax >> 16) & 0xff;
313 * If the TjMax is not plausible, an assumption
317 dev_dbg(dev, "TjMax is %d degrees C\n", val);
323 dev_notice(dev, "TjMax forced to %d degrees C by user\n",
325 return force_tjmax * 1000;
329 * An assumption is made for early CPUs and unreadable MSR.
330 * NOTE: the calculated value may not be correct.
332 return adjust_tjmax(c, id, dev);
335 static int create_name_attr(struct platform_data *pdata, struct device *dev)
337 sysfs_attr_init(&pdata->name_attr.attr);
338 pdata->name_attr.attr.name = "name";
339 pdata->name_attr.attr.mode = S_IRUGO;
340 pdata->name_attr.show = show_name;
341 return device_create_file(dev, &pdata->name_attr);
344 static int create_core_attrs(struct temp_data *tdata, struct device *dev,
348 static ssize_t (*const rd_ptr[TOTAL_ATTRS]) (struct device *dev,
349 struct device_attribute *devattr, char *buf) = {
350 show_label, show_crit_alarm, show_temp, show_tjmax,
352 static const char *const names[TOTAL_ATTRS] = {
353 "temp%d_label", "temp%d_crit_alarm",
354 "temp%d_input", "temp%d_crit",
357 for (i = 0; i < tdata->attr_size; i++) {
358 snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH, names[i],
360 sysfs_attr_init(&tdata->sd_attrs[i].dev_attr.attr);
361 tdata->sd_attrs[i].dev_attr.attr.name = tdata->attr_name[i];
362 tdata->sd_attrs[i].dev_attr.attr.mode = S_IRUGO;
363 tdata->sd_attrs[i].dev_attr.show = rd_ptr[i];
364 tdata->sd_attrs[i].index = attr_no;
365 err = device_create_file(dev, &tdata->sd_attrs[i].dev_attr);
373 device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
378 static int chk_ucode_version(unsigned int cpu, const struct cpu_info *c)
381 * Check if we have problem with errata AE18 of Core processors:
382 * Readings might stop update when processor visited too deep sleep,
383 * fixed for stepping D0 (6EC).
385 if (c->x86_model == 0xe && c->x86_mask < 0xc && c->microcode < 0x39) {
386 pr_err("Errata AE18 not fixed, update BIOS or "
387 "microcode of the CPU!\n");
393 static struct platform_device *coretemp_get_pdev(unsigned int cpu)
395 u16 phys_proc_id = TO_PHYS_ID(cpu);
396 struct pdev_entry *p;
398 mutex_lock(&pdev_list_mutex);
400 list_for_each_entry(p, &pdev_list, list)
401 if (p->phys_proc_id == phys_proc_id) {
402 mutex_unlock(&pdev_list_mutex);
406 mutex_unlock(&pdev_list_mutex);
410 static struct temp_data *init_temp_data(unsigned int cpu,
411 const struct cpu_info *c,
414 struct temp_data *tdata;
416 tdata = kzalloc(sizeof(struct temp_data), GFP_KERNEL);
420 tdata->status_reg = pkg_flag ? MSR_IA32_PACKAGE_THERM_STATUS :
421 MSR_IA32_THERM_STATUS;
422 tdata->is_pkg_data = pkg_flag;
424 tdata->cpu_core_id = c->cpu_core_id;
425 tdata->attr_size = MAX_CORE_ATTRS;
426 mutex_init(&tdata->update_lock);
430 static int create_core_data(struct platform_device *pdev,
432 const struct cpu_info *c, int pkg_flag)
434 struct temp_data *tdata;
435 struct platform_data *pdata = platform_get_drvdata(pdev);
440 * Find attr number for sysfs:
441 * We map the attr number to core id of the CPU
442 * The attr number is always core id + 2
443 * The Pkgtemp will always show up as temp1_*, if available
445 attr_no = pkg_flag ? 1 : CORE_ATTR_NO(c->cpu_core_id);
447 if (attr_no > MAX_CORE_DATA - 1)
451 * Provide a single set of attributes for all HT siblings of a core
452 * to avoid duplicate sensors (the processor ID and core ID of all
453 * HT siblings of a core are the same).
454 * Skip if a HT sibling of this core is already registered.
455 * This is not an error.
457 if (pdata->core_data[attr_no] != NULL)
460 tdata = init_temp_data(cpu, c, pkg_flag);
464 /* Test if we can access the status register */
465 err = rdmsr_safe_on_pcpu(cpu, tdata->status_reg, &eax, &edx);
469 /* We can access status register. Get Critical Temperature */
470 tdata->tjmax = get_tjmax(pdata, cpu, &pdev->dev);
473 * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
474 * The target temperature is available on older CPUs but not in this
475 * register. Atoms don't have the register at all.
477 if (c->x86_model > 0xe && c->x86_model != 0x1c) {
478 err = rdmsr_safe_on_pcpu(cpu, MSR_IA32_TEMPERATURE_TARGET,
482 = tdata->tjmax - ((eax >> 8) & 0xff) * 1000;
487 pdata->core_data[attr_no] = tdata;
489 /* Create sysfs interfaces */
490 err = create_core_attrs(tdata, &pdev->dev, attr_no);
496 pdata->core_data[attr_no] = NULL;
501 static void coretemp_add_core(unsigned int cpu,
502 const struct cpu_info *c, int pkg_flag)
504 struct platform_device *pdev = c->pdev;
507 err = create_core_data(pdev, cpu, c, pkg_flag);
509 dev_err(&pdev->dev, "Adding Core %u failed\n", cpu);
512 static void coretemp_remove_core(struct platform_data *pdata,
513 struct device *dev, int indx)
516 struct temp_data *tdata = pdata->core_data[indx];
518 /* Remove the sysfs attributes */
519 for (i = 0; i < tdata->attr_size; i++)
520 device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
522 kfree(pdata->core_data[indx]);
523 pdata->core_data[indx] = NULL;
526 static int coretemp_probe(struct platform_device *pdev)
528 struct platform_data *pdata = platform_get_drvdata(pdev);
531 /* Initialize the per-package data structures */
532 err = create_name_attr(pdata, &pdev->dev);
536 pdata->hwmon_dev = hwmon_device_register(&pdev->dev);
537 if (IS_ERR(pdata->hwmon_dev)) {
538 err = PTR_ERR(pdata->hwmon_dev);
539 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
545 device_remove_file(&pdev->dev, &pdata->name_attr);
549 static int coretemp_remove(struct platform_device *pdev)
551 struct platform_data *pdata = platform_get_drvdata(pdev);
554 for (i = MAX_CORE_DATA - 1; i >= 0; --i)
555 if (pdata->core_data[i])
556 coretemp_remove_core(pdata, &pdev->dev, i);
558 device_remove_file(&pdev->dev, &pdata->name_attr);
559 hwmon_device_unregister(pdata->hwmon_dev);
560 platform_set_drvdata(pdev, NULL);
565 static struct platform_driver coretemp_driver = {
567 .owner = THIS_MODULE,
570 .probe = coretemp_probe,
571 .remove = coretemp_remove,
574 static int coretemp_device_add(unsigned int cpu, struct cpu_info *c)
577 struct platform_device *pdev;
578 struct pdev_entry *pdev_entry;
579 struct platform_data *pdata = NULL;
581 mutex_lock(&pdev_list_mutex);
583 pdev = platform_device_alloc(DRVNAME, c->phys_proc_id);
586 pr_err("Device allocation failed\n");
590 pdata = kzalloc(sizeof(struct platform_data), GFP_KERNEL);
593 goto exit_device_put;
596 pdata->phys_proc_id = c->phys_proc_id;
597 pdata->x86_model = c->x86_model;
598 pdata->x86_mask = c->x86_mask;
599 platform_set_drvdata(pdev, pdata);
601 pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
604 goto exit_device_put;
607 err = platform_device_add(pdev);
609 pr_err("Device addition failed (%d)\n", err);
610 goto exit_device_free;
613 pdev_entry->pdev = pdev;
614 pdev_entry->phys_proc_id = c->phys_proc_id;
617 list_add_tail(&pdev_entry->list, &pdev_list);
618 mutex_unlock(&pdev_list_mutex);
625 platform_device_put(pdev);
628 mutex_unlock(&pdev_list_mutex);
632 static void coretemp_device_remove(unsigned int cpu)
634 struct pdev_entry *p, *n;
635 u16 phys_proc_id = TO_PHYS_ID(cpu);
637 mutex_lock(&pdev_list_mutex);
638 list_for_each_entry_safe(p, n, &pdev_list, list) {
639 if (p->phys_proc_id != phys_proc_id)
641 platform_device_unregister(p->pdev);
645 mutex_unlock(&pdev_list_mutex);
648 static bool is_any_core_online(struct platform_data *pdata)
652 /* Find online cores, except pkgtemp data */
653 for (i = MAX_CORE_DATA - 1; i >= 0; --i) {
654 if (pdata->core_data[i] &&
655 !pdata->core_data[i]->is_pkg_data) {
662 static void get_cpuid_info(void *arg)
664 struct cpu_info *info = arg;
665 u32 val = cpuid_eax(1);
667 info->x86_model = ((val >> 4) & 0xf) | ((val >> 12) & 0xf0);
668 info->x86_mask = val & 0xf;
670 if (((val >> 8) & 0xf) != 6 || ((val >> 20) & 0xff)
672 || wrmsr_safe(MSR_IA32_UCODE_REV, 0, 0) < 0
673 || (sync_core(), rdmsr_safe(MSR_IA32_UCODE_REV,
674 &val, &info->microcode)) < 0)
677 info->cpuid_6_eax = cpuid_eax(0) >= 6 ? cpuid_eax(6) : 0;
680 static void get_core_online(unsigned int cpu)
682 struct cpu_info info;
683 struct platform_device *pdev = coretemp_get_pdev(cpu);
688 err = xen_set_physical_cpu_affinity(cpu);
690 get_cpuid_info(&info);
691 WARN_ON_ONCE(xen_set_physical_cpu_affinity(-1));
692 } else if (err > 0) {
697 pr_warn(DRVNAME "Cannot set physical CPU affinity"
698 " (assuming use of dom0_vcpus_pin)\n");
700 err = smp_call_function_single(cpu, get_cpuid_info, &info, 1);
706 * CPUID.06H.EAX[0] indicates whether the CPU has thermal
707 * sensors. We check this bit only, all the early CPUs
708 * without thermal sensors will be filtered out.
710 if (!(info.cpuid_6_eax & 0x1))
713 err = xen_get_topology_info(cpu, &info.cpu_core_id,
714 &info.phys_proc_id, NULL);
719 /* Check the microcode version of the CPU */
720 if (chk_ucode_version(cpu, &info))
724 * Alright, we have DTS support.
725 * We are bringing the _first_ core in this pkg
726 * online. So, initialize per-pkg data structures and
727 * then bring this core online.
729 err = coretemp_device_add(cpu, &info);
733 * Check whether pkgtemp support is available.
734 * If so, add interfaces for pkgtemp.
736 if (info.cpuid_6_eax & 0x40)
737 coretemp_add_core(cpu, &info, 1);
740 * Physical CPU device already exists.
741 * So, just add interfaces for this core.
743 coretemp_add_core(cpu, &info, 0);
746 static void put_core_offline(unsigned int cpu)
749 struct platform_data *pdata;
750 struct platform_device *pdev = coretemp_get_pdev(cpu);
751 u32 cpu_core_id, phys_proc_id;
753 /* If the physical CPU device does not exist, just return */
757 pdata = platform_get_drvdata(pdev);
759 if (xen_get_topology_info(cpu, &cpu_core_id, &phys_proc_id, NULL))
761 indx = CORE_ATTR_NO(cpu_core_id);
763 if (pdata->core_data[indx] && pdata->core_data[indx]->cpu == cpu)
764 coretemp_remove_core(pdata, &pdev->dev, indx);
767 * If a HT sibling of a core is taken offline, but another HT sibling
768 * of the same core is still online, register the alternate sibling.
769 * This ensures that exactly one set of attributes is provided as long
770 * as at least one HT sibling of a core is online.
777 err = xen_get_topology_info(i, &cid, &pid, NULL);
782 if (pid != phys_proc_id || cid != cpu_core_id)
786 * Display temperature sensor data for one HT sibling
787 * per core only, so abort the loop after one such
788 * sibling has been found.
794 * If all cores in this pkg are offline, remove the device.
795 * coretemp_device_remove calls unregister_platform_device,
796 * which in turn calls coretemp_remove. This removes the
797 * pkgtemp entry and does other clean ups.
799 if (!is_any_core_online(pdata))
800 coretemp_device_remove(cpu);
803 static int coretemp_cpu_callback(struct notifier_block *nfb,
804 unsigned long action, void *hcpu)
806 unsigned int cpu = (unsigned long) hcpu;
810 get_core_online(cpu);
813 put_core_offline(cpu);
819 static struct notifier_block coretemp_cpu_notifier = {
820 .notifier_call = coretemp_cpu_callback,
823 static const struct x86_cpu_id coretemp_ids[] = {
824 { X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_DTS },
827 MODULE_DEVICE_TABLE(x86cpu, coretemp_ids);
829 static int __init coretemp_init(void)
833 if (!is_initial_xendomain())
837 * CPUID.06H.EAX[0] indicates whether the CPU has thermal
838 * sensors. We check this bit only, all the early CPUs
839 * without thermal sensors will be filtered out.
841 if (!x86_match_cpu(coretemp_ids))
844 err = platform_driver_register(&coretemp_driver);
848 err = register_pcpu_notifier(&coretemp_cpu_notifier);
850 goto exit_driver_unreg;
852 #ifndef CONFIG_ACPI_HOTPLUG_CPU
853 if (list_empty(&pdev_list)) {
854 unregister_pcpu_notifier(&coretemp_cpu_notifier);
856 goto exit_driver_unreg;
863 platform_driver_unregister(&coretemp_driver);
868 static void __exit coretemp_exit(void)
870 struct pdev_entry *p, *n;
872 unregister_pcpu_notifier(&coretemp_cpu_notifier);
873 mutex_lock(&pdev_list_mutex);
874 list_for_each_entry_safe(p, n, &pdev_list, list) {
875 platform_device_unregister(p->pdev);
879 mutex_unlock(&pdev_list_mutex);
880 platform_driver_unregister(&coretemp_driver);
883 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
884 MODULE_DESCRIPTION("Intel Core temperature monitor");
885 MODULE_LICENSE("GPL");
887 module_init(coretemp_init)
888 module_exit(coretemp_exit)