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 #include <linux/module.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/hwmon.h>
29 #include <linux/sysfs.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33 #include <linux/list.h>
34 #include <linux/platform_device.h>
35 #include <linux/cpu.h>
36 #include <linux/pci.h>
38 #include <asm/processor.h>
41 #define DRVNAME "coretemp"
43 typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL,
47 * Functions declaration
50 static struct coretemp_data *coretemp_update_device(struct device *dev);
52 struct coretemp_data {
53 struct device *hwmon_dev;
54 struct mutex update_lock;
58 char valid; /* zero until following fields are valid */
59 unsigned long last_updated; /* in jiffies */
70 static ssize_t show_name(struct device *dev, struct device_attribute
74 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
75 struct coretemp_data *data = dev_get_drvdata(dev);
77 if (attr->index == SHOW_NAME)
78 ret = sprintf(buf, "%s\n", data->name);
80 ret = sprintf(buf, "Core %d\n", data->core_id);
84 static ssize_t show_alarm(struct device *dev, struct device_attribute
87 struct coretemp_data *data = coretemp_update_device(dev);
88 /* read the Out-of-spec log, never clear */
89 return sprintf(buf, "%d\n", data->alarm);
92 static ssize_t show_temp(struct device *dev,
93 struct device_attribute *devattr, char *buf)
95 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
96 struct coretemp_data *data = coretemp_update_device(dev);
99 if (attr->index == SHOW_TEMP)
100 err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN;
101 else if (attr->index == SHOW_TJMAX)
102 err = sprintf(buf, "%d\n", data->tjmax);
104 err = sprintf(buf, "%d\n", data->ttarget);
108 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
110 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
112 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL,
114 static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
115 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
116 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
118 static struct attribute *coretemp_attributes[] = {
119 &sensor_dev_attr_name.dev_attr.attr,
120 &sensor_dev_attr_temp1_label.dev_attr.attr,
121 &dev_attr_temp1_crit_alarm.attr,
122 &sensor_dev_attr_temp1_input.dev_attr.attr,
123 &sensor_dev_attr_temp1_crit.dev_attr.attr,
127 static const struct attribute_group coretemp_group = {
128 .attrs = coretemp_attributes,
131 static struct coretemp_data *coretemp_update_device(struct device *dev)
133 struct coretemp_data *data = dev_get_drvdata(dev);
135 mutex_lock(&data->update_lock);
137 if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
141 rdmsr_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
142 data->alarm = (eax >> 5) & 1;
143 /* update only if data has been valid */
144 if (eax & 0x80000000) {
145 data->temp = data->tjmax - (((eax >> 16)
149 dev_dbg(dev, "Temperature data invalid (0x%x)\n", eax);
151 data->last_updated = jiffies;
154 mutex_unlock(&data->update_lock);
158 static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
160 /* The 100C is default for both mobile and non mobile CPUs */
163 int tjmax_ee = 85000;
167 struct pci_dev *host_bridge;
169 /* Early chips have no MSR for TjMax */
171 if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
177 if (c->x86_model == 0x1c) {
180 host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
182 if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL
183 && (host_bridge->device == 0xa000 /* NM10 based nettop */
184 || host_bridge->device == 0xa010)) /* NM10 based netbook */
189 pci_dev_put(host_bridge);
192 if ((c->x86_model > 0xe) && (usemsr_ee)) {
195 /* Now we can detect the mobile CPU using Intel provided table
196 http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
197 For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
200 err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
203 "Unable to access MSR 0x17, assuming desktop"
206 } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
207 /* Trust bit 28 up to Penryn, I could not find any
208 documentation on that; if you happen to know
209 someone at Intel please ask */
212 /* Platform ID bits 52:50 (EDX starts at bit 32) */
213 platform_id = (edx >> 18) & 0x7;
215 /* Mobile Penryn CPU seems to be platform ID 7 or 5
217 if ((c->x86_model == 0x17) &&
218 ((platform_id == 5) || (platform_id == 7))) {
219 /* If MSR EE bit is set, set it to 90 degrees C,
220 otherwise 105 degrees C */
229 err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
232 "Unable to access MSR 0xEE, for Tjmax, left"
234 } else if (eax & 0x40000000) {
237 /* if we dont use msr EE it means we are desktop CPU (with exeception
239 } else if (tjmax == 100000) {
240 dev_warn(dev, "Using relative temperature scale!\n");
246 static int __devinit get_tjmax(struct cpuinfo_x86 *c, u32 id,
249 /* The 100C is default for both mobile and non mobile CPUs */
254 /* A new feature of current Intel(R) processors, the
255 IA32_TEMPERATURE_TARGET contains the TjMax value */
256 err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
258 dev_warn(dev, "Unable to read TjMax from CPU.\n");
260 val = (eax >> 16) & 0xff;
262 * If the TjMax is not plausible, an assumption
265 if ((val > 80) && (val < 120)) {
266 dev_info(dev, "TjMax is %d C.\n", val);
272 * An assumption is made for early CPUs and unreadable MSR.
273 * NOTE: the given value may not be correct.
276 switch (c->x86_model) {
281 dev_warn(dev, "TjMax is assumed as 100 C!\n");
284 case 0x1c: /* Atom CPUs */
285 return adjust_tjmax(c, id, dev);
287 dev_warn(dev, "CPU (model=0x%x) is not supported yet,"
288 " using default TjMax of 100C.\n", c->x86_model);
293 static int __devinit coretemp_probe(struct platform_device *pdev)
295 struct coretemp_data *data;
296 struct cpuinfo_x86 *c = &cpu_data(pdev->id);
300 if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
302 dev_err(&pdev->dev, "Out of memory\n");
308 data->core_id = c->cpu_core_id;
310 data->name = "coretemp";
311 mutex_init(&data->update_lock);
313 /* test if we can access the THERM_STATUS MSR */
314 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
317 "Unable to access THERM_STATUS MSR, giving up\n");
321 /* Check if we have problem with errata AE18 of Core processors:
322 Readings might stop update when processor visited too deep sleep,
323 fixed for stepping D0 (6EC).
326 if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
327 /* check for microcode update */
328 rdmsr_on_cpu(data->id, MSR_IA32_UCODE_REV, &eax, &edx);
332 "Errata AE18 not fixed, update BIOS or "
333 "microcode of the CPU!\n");
338 data->tjmax = get_tjmax(c, data->id, &pdev->dev);
339 platform_set_drvdata(pdev, data);
342 * read the still undocumented IA32_TEMPERATURE_TARGET. It exists
343 * on older CPUs but not in this register,
344 * Atoms don't have it either.
347 if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) {
348 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET,
351 dev_warn(&pdev->dev, "Unable to read"
352 " IA32_TEMPERATURE_TARGET MSR\n");
354 data->ttarget = data->tjmax -
355 (((eax >> 8) & 0xff) * 1000);
356 err = device_create_file(&pdev->dev,
357 &sensor_dev_attr_temp1_max.dev_attr);
363 if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
366 data->hwmon_dev = hwmon_device_register(&pdev->dev);
367 if (IS_ERR(data->hwmon_dev)) {
368 err = PTR_ERR(data->hwmon_dev);
369 dev_err(&pdev->dev, "Class registration failed (%d)\n",
377 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
379 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
386 static int __devexit coretemp_remove(struct platform_device *pdev)
388 struct coretemp_data *data = platform_get_drvdata(pdev);
390 hwmon_device_unregister(data->hwmon_dev);
391 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
392 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
393 platform_set_drvdata(pdev, NULL);
398 static struct platform_driver coretemp_driver = {
400 .owner = THIS_MODULE,
403 .probe = coretemp_probe,
404 .remove = __devexit_p(coretemp_remove),
408 struct list_head list;
409 struct platform_device *pdev;
417 static LIST_HEAD(pdev_list);
418 static DEFINE_MUTEX(pdev_list_mutex);
420 static int __cpuinit coretemp_device_add(unsigned int cpu)
423 struct platform_device *pdev;
424 struct pdev_entry *pdev_entry;
425 struct cpuinfo_x86 *c = &cpu_data(cpu);
428 * CPUID.06H.EAX[0] indicates whether the CPU has thermal
429 * sensors. We check this bit only, all the early CPUs
430 * without thermal sensors will be filtered out.
432 if (!cpu_has(c, X86_FEATURE_DTS)) {
433 printk(KERN_INFO DRVNAME ": CPU (model=0x%x)"
434 " has no thermal sensor.\n", c->x86_model);
438 mutex_lock(&pdev_list_mutex);
441 /* Skip second HT entry of each core */
442 list_for_each_entry(pdev_entry, &pdev_list, list) {
443 if (c->phys_proc_id == pdev_entry->phys_proc_id &&
444 c->cpu_core_id == pdev_entry->cpu_core_id) {
445 err = 0; /* Not an error */
451 pdev = platform_device_alloc(DRVNAME, cpu);
454 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
458 pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
461 goto exit_device_put;
464 err = platform_device_add(pdev);
466 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
468 goto exit_device_free;
471 pdev_entry->pdev = pdev;
472 pdev_entry->cpu = cpu;
474 pdev_entry->phys_proc_id = c->phys_proc_id;
475 pdev_entry->cpu_core_id = c->cpu_core_id;
477 list_add_tail(&pdev_entry->list, &pdev_list);
478 mutex_unlock(&pdev_list_mutex);
485 platform_device_put(pdev);
487 mutex_unlock(&pdev_list_mutex);
491 static void __cpuinit coretemp_device_remove(unsigned int cpu)
493 struct pdev_entry *p;
496 mutex_lock(&pdev_list_mutex);
497 list_for_each_entry(p, &pdev_list, list) {
501 platform_device_unregister(p->pdev);
503 mutex_unlock(&pdev_list_mutex);
505 for_each_cpu(i, cpu_sibling_mask(cpu))
506 if (i != cpu && !coretemp_device_add(i))
510 mutex_unlock(&pdev_list_mutex);
513 static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
514 unsigned long action, void *hcpu)
516 unsigned int cpu = (unsigned long) hcpu;
520 case CPU_DOWN_FAILED:
521 coretemp_device_add(cpu);
523 case CPU_DOWN_PREPARE:
524 coretemp_device_remove(cpu);
530 static struct notifier_block coretemp_cpu_notifier __refdata = {
531 .notifier_call = coretemp_cpu_callback,
534 static int __init coretemp_init(void)
536 int i, err = -ENODEV;
538 /* quick check if we run Intel */
539 if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
542 err = platform_driver_register(&coretemp_driver);
546 for_each_online_cpu(i)
547 coretemp_device_add(i);
549 #ifndef CONFIG_HOTPLUG_CPU
550 if (list_empty(&pdev_list)) {
552 goto exit_driver_unreg;
556 register_hotcpu_notifier(&coretemp_cpu_notifier);
559 #ifndef CONFIG_HOTPLUG_CPU
561 platform_driver_unregister(&coretemp_driver);
567 static void __exit coretemp_exit(void)
569 struct pdev_entry *p, *n;
570 #ifdef CONFIG_HOTPLUG_CPU
571 unregister_hotcpu_notifier(&coretemp_cpu_notifier);
573 mutex_lock(&pdev_list_mutex);
574 list_for_each_entry_safe(p, n, &pdev_list, list) {
575 platform_device_unregister(p->pdev);
579 mutex_unlock(&pdev_list_mutex);
580 platform_driver_unregister(&coretemp_driver);
583 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
584 MODULE_DESCRIPTION("Intel Core temperature monitor");
585 MODULE_LICENSE("GPL");
587 module_init(coretemp_init)
588 module_exit(coretemp_exit)