1 Index: linux-2.6.20-rc4/drivers/hwmon/coretemp.c
2 ===================================================================
3 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
4 +++ linux-2.6.20-rc4/drivers/hwmon/coretemp.c 2007-01-14 21:49:52.764871760 +0100
7 + * coretemp.c - Linux kernel module for hardware monitoring
9 + * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
11 + * Inspired from many hwmon drivers
13 + * This program is free software; you can redistribute it and/or modify
14 + * it under the terms of the GNU General Public License as published by
15 + * the Free Software Foundation; either version 2 of the License, or
16 + * (at your option) any later version.
18 + * This program is distributed in the hope that it will be useful,
19 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 + * GNU General Public License for more details.
23 + * You should have received a copy of the GNU General Public License
24 + * along with this program; if not, write to the Free Software
25 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 +#include <linux/module.h>
30 +#include <linux/delay.h>
31 +#include <linux/init.h>
32 +#include <linux/slab.h>
33 +#include <linux/jiffies.h>
34 +#include <linux/hwmon.h>
35 +#include <linux/sysfs.h>
36 +#include <linux/hwmon-sysfs.h>
37 +#include <linux/err.h>
38 +#include <linux/mutex.h>
39 +#include <linux/list.h>
40 +#include <linux/platform_device.h>
41 +#include <linux/cpu.h>
43 +#include <asm/processor.h>
45 +#define DRVNAME "coretemp"
48 + Following part ripped from the msr.c. It should be merged with generic MSR
49 + infrastructure (once ready)
52 +static inline int rdmsr_eio(u32 reg, u32 *eax, u32 *edx)
56 + err = rdmsr_safe(reg, eax, edx);
71 +static void msr_smp_rdmsr(void *cmd_block)
73 + struct msr_command *cmd = (struct msr_command *)cmd_block;
75 + if (cmd->cpu == smp_processor_id())
76 + cmd->err = rdmsr_eio(cmd->reg, &cmd->data[0], &cmd->data[1]);
79 +static inline int msr_read(int cpu, u32 reg, u32 * eax, u32 * edx)
81 + struct msr_command cmd;
85 + if (cpu == smp_processor_id()) {
86 + ret = rdmsr_eio(reg, eax, edx);
91 + smp_call_function(msr_smp_rdmsr, &cmd, 1, 1);
102 +#else /* ! CONFIG_SMP */
104 +static inline int msr_read(int cpu, u32 reg, u32 *eax, u32 *edx)
106 + return rdmsr_eio(reg, eax, edx);
109 +#endif /* ! CONFIG_SMP */
113 +typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_LABEL, SHOW_NAME } SHOW;
116 + * Functions declaration
119 +static struct coretemp_data *coretemp_update_device(struct device *dev);
121 +struct coretemp_data {
122 + struct class_device *class_dev;
123 + struct mutex update_lock;
126 + char valid; /* zero until following fields are valid */
127 + unsigned long last_updated; /* in jiffies */
130 + /* registers values */
134 +static struct coretemp_data *coretemp_update_device(struct device *dev);
140 +static ssize_t show_name(struct device *dev, struct device_attribute
141 + *devattr, char *buf)
144 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
145 + struct coretemp_data *data = dev_get_drvdata(dev);
147 + if (attr->index == SHOW_NAME)
148 + ret = sprintf(buf, "%s\n", data->name);
149 + else /* show label */
150 + ret = sprintf(buf, "Core %d\n", data->id);
154 +static ssize_t show_alarm(struct device *dev, struct device_attribute
155 + *devattr, char *buf)
157 + struct coretemp_data *data = coretemp_update_device(dev);
158 + /* read the Out-of-spec log, never clear */
159 + return sprintf(buf, "%d\n", (data->therm_status >> 5) & 1);
162 +static ssize_t show_temp(struct device *dev,
163 + struct device_attribute *devattr, char *buf)
165 + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
166 + struct coretemp_data *data = coretemp_update_device(dev);
167 + return sprintf(buf, "%d\n",
169 + SHOW_TEMP ? data->temp : data->tjmax);
172 +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
174 +static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
176 +static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
177 +static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
178 +static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
180 +static struct attribute *coretemp_attributes[] = {
181 + &sensor_dev_attr_name.dev_attr.attr,
182 + &sensor_dev_attr_temp1_label.dev_attr.attr,
183 + &dev_attr_temp1_crit_alarm.attr,
184 + &sensor_dev_attr_temp1_input.dev_attr.attr,
185 + &sensor_dev_attr_temp1_crit.dev_attr.attr,
189 +static const struct attribute_group coretemp_group = {
190 + .attrs = coretemp_attributes,
193 +static struct coretemp_data *coretemp_update_device(struct device *dev)
195 + struct coretemp_data *data = dev_get_drvdata(dev);
197 + mutex_lock(&data->update_lock);
199 + if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
203 + msr_read(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
204 + data->therm_status = eax;
206 + /* update only if data has been valid */
207 + if (eax & 0x80000000) {
208 + data->temp = data->tjmax - (((data->therm_status >> 16)
212 + data->last_updated = jiffies;
215 + mutex_unlock(&data->update_lock);
219 +static int __devinit coretemp_probe(struct platform_device *pdev)
221 + struct coretemp_data *data;
222 + struct cpuinfo_x86 *c = &(cpu_data)[pdev->id];
226 + if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
228 + dev_err(&pdev->dev, "Out of memory\n");
232 + data->id = pdev->id;
233 + data->name = "coretemp";
234 + mutex_init(&data->update_lock);
235 + /* Tjmax default is 100C */
236 + data->tjmax = 100000;
238 + /* Some processors have Tjmax 85 following magic should detect it
239 + Intel won't disclose the information without signed NDA, but
240 + individuals cannot sign it. Catch(ed) 22.
243 + if (((c->x86_model == 0xf) && (c->x86_mask > 3 )) ||
244 + (c->x86_model == 0xe)) {
246 + err = msr_read(data->id, 0xee, &eax, &edx);
248 + dev_warn(&pdev->dev,
249 + "Unable to access MSR 0xEE, Tjmax left at %d\n",
251 + } else if (eax & 0x40000000) {
252 + data->tjmax = 85000;
256 + /* test if we can access the THERM_STATUS MSR */
257 + err = msr_read(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
260 + dev_err(&pdev->dev,
261 + "Unable to access THERM_STATUS MSR, giving up\n");
264 + platform_set_drvdata(pdev, data);
266 + if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
269 + data->class_dev = hwmon_device_register(&pdev->dev);
270 + if (IS_ERR(data->class_dev)) {
271 + err = PTR_ERR(data->class_dev);
272 + dev_err(&pdev->dev, "Class registration failed (%d)\n",
277 + dev_warn(&pdev->dev, "This driver uses undocumented features of Core "
278 + "CPU (Intel will not disclose the information to "
279 + "individuals). Temperature might be wrong!\n");
283 + sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
290 +static int __devexit coretemp_remove(struct platform_device *pdev)
292 + struct coretemp_data *data = platform_get_drvdata(pdev);
294 + hwmon_device_unregister(data->class_dev);
295 + sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
296 + platform_set_drvdata(pdev, NULL);
301 +static struct platform_driver coretemp_driver = {
303 + .owner = THIS_MODULE,
306 + .probe = coretemp_probe,
307 + .remove = __devexit_p(coretemp_remove),
311 + struct list_head list;
312 + struct platform_device *pdev;
316 +static LIST_HEAD(pdev_list);
317 +static DEFINE_MUTEX(pdev_list_mutex);
319 +static int __cpuinit coretemp_devices_add(unsigned int cpu)
322 + struct platform_device *pdev;
323 + struct pdev_entry *pdev_entry;
325 + pdev = platform_device_alloc(DRVNAME, cpu);
328 + printk(KERN_ERR DRVNAME ": Device allocation failed\n");
333 + pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
337 + goto exit_device_put;
340 + err = platform_device_add(pdev);
343 + printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
345 + goto exit_device_free;
348 + pdev_entry->pdev = pdev;
349 + pdev_entry->cpu = cpu;
350 + mutex_lock(&pdev_list_mutex);
351 + list_add_tail(&pdev_entry->list, &pdev_list);
352 + mutex_unlock(&pdev_list_mutex);
359 + platform_device_put(pdev);
364 +#ifdef CONFIG_HOTPLUG_CPU
365 +void coretemp_devices_remove(unsigned int cpu)
367 + struct pdev_entry *p, *n;
368 + mutex_lock(&pdev_list_mutex);
369 + list_for_each_entry_safe(p, n, &pdev_list, list) {
370 + if (p->cpu == cpu) {
371 + platform_device_unregister(p->pdev);
372 + list_del(&p->list);
376 + mutex_unlock(&pdev_list_mutex);
379 +static int coretemp_cpu_callback(struct notifier_block *nfb,
380 + unsigned long action, void *hcpu)
382 + unsigned int cpu = (unsigned long) hcpu;
386 + coretemp_devices_add(cpu);
389 + coretemp_devices_remove(cpu);
395 +static struct notifier_block __cpuinitdata coretemp_cpu_notifier = {
396 + .notifier_call = coretemp_cpu_callback,
398 +#endif /* !CONFIG_HOTPLUG_CPU */
400 +static int __init coretemp_init(void)
402 + int i, err = -ENODEV;
403 + struct pdev_entry *p, *n;
405 + /* quick check if we run Intel */
406 + if (cpu_data[0].x86_vendor != X86_VENDOR_INTEL)
409 + err = platform_driver_register(&coretemp_driver);
413 + for_each_online_cpu(i) {
414 + struct cpuinfo_x86 *c = &(cpu_data)[i];
416 + /* check if family 6, models e, f */
417 + if ((c->cpuid_level < 0) || (c->x86 != 0x6) ||
418 + !((c->x86_model == 0xe) || (c->x86_model == 0xf))) {
420 + /* supported CPU not found, but report the unknown
422 + if ((c->x86 == 0x6) && (c->x86_model > 0xf))
423 + printk(KERN_WARNING DRVNAME ": Unknown CPU, please"
424 + " report to the lm-sensors@lm-sensors.org\n");
428 + err = coretemp_devices_add(i);
432 + if (list_empty(&pdev_list)) {
434 + goto exit_driver_unreg;
437 +#ifdef CONFIG_HOTPLUG_CPU
438 + register_hotcpu_notifier(&coretemp_cpu_notifier);
443 + mutex_lock(&pdev_list_mutex);
444 + list_for_each_entry_safe(p, n, &pdev_list, list) {
445 + platform_device_unregister(p->pdev);
446 + list_del(&p->list);
449 + mutex_unlock(&pdev_list_mutex);
451 + platform_driver_unregister(&coretemp_driver);
456 +static void __exit coretemp_exit(void)
458 + struct pdev_entry *p, *n;
459 +#ifdef CONFIG_HOTPLUG_CPU
460 + unregister_hotcpu_notifier(&coretemp_cpu_notifier);
462 + mutex_lock(&pdev_list_mutex);
463 + list_for_each_entry_safe(p, n, &pdev_list, list) {
464 + platform_device_unregister(p->pdev);
465 + list_del(&p->list);
468 + mutex_unlock(&pdev_list_mutex);
469 + platform_driver_unregister(&coretemp_driver);
472 +MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
473 +MODULE_DESCRIPTION("Intel Core temperature monitor");
474 +MODULE_LICENSE("GPL");
476 +module_init(coretemp_init)
477 +module_exit(coretemp_exit)
478 Index: linux-2.6.20-rc4/drivers/hwmon/Makefile
479 ===================================================================
480 --- linux-2.6.20-rc4.orig/drivers/hwmon/Makefile 2007-01-07 06:45:51.000000000 +0100
481 +++ linux-2.6.20-rc4/drivers/hwmon/Makefile 2007-01-14 21:29:41.899868496 +0100
483 obj-$(CONFIG_SENSORS_ADM9240) += adm9240.o
484 obj-$(CONFIG_SENSORS_AMS) += ams/
485 obj-$(CONFIG_SENSORS_ATXP1) += atxp1.o
486 +obj-$(CONFIG_SENSORS_CORETEMP) += coretemp.o
487 obj-$(CONFIG_SENSORS_DS1621) += ds1621.o
488 obj-$(CONFIG_SENSORS_F71805F) += f71805f.o
489 obj-$(CONFIG_SENSORS_FSCHER) += fscher.o
490 Index: linux-2.6.20-rc4/drivers/hwmon/Kconfig
491 ===================================================================
492 --- linux-2.6.20-rc4.orig/drivers/hwmon/Kconfig 2007-01-07 06:45:51.000000000 +0100
493 +++ linux-2.6.20-rc4/drivers/hwmon/Kconfig 2007-01-14 21:40:33.336991782 +0100
495 This driver can also be built as a module. If so, the module
496 will be called atxp1.
498 +config SENSORS_CORETEMP
499 + tristate "Intel Core (2) Duo/Solo temperature sensor"
500 + depends on HWMON && X86 && EXPERIMENTAL
502 + If you say yes here you get support for the temperature
503 + sensor inside your CPU. Supported all are all known variants
504 + of Intel Core family.
506 config SENSORS_DS1621
507 tristate "Dallas Semiconductor DS1621 and DS1625"
508 depends on HWMON && I2C
509 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
510 +++ linux-2.6.20-rc4/Documentation/hwmon/coretemp 2007-01-14 22:01:58.766244221 +0100
512 +Kernel driver coretemp
513 +======================
516 + * All Intel Core family
518 + Addresses scanned: CPUID (family 0x6, models 0xe, 0xf)
519 + Datasheet: Intel 64 and IA-32 Architectures Software Developer's Manual
520 + Volume 3A: System Programming Guide
522 +Author: Rudolf Marek
523 +Contact: Rudolf Marek <r.marek@assembler.cz>
528 +This driver permits reading temperature sensor embedded inside Intel Core CPU.
529 +Temperature is measured in degrees Celsius and measurement resolution is
530 +1 degree C. Valid temperatures are from 0 to TjMax degrees C, because
531 +the actual temperature is in fact a delta from TjMax.
533 +Temperature known as TjMax is the maximum junction temperature of processor.
534 +Intel defines this temperature as 85C or 100C. At this temperature, protection
535 +mechanism will perform actions to forcibly cool down the processor. Alarm
536 +may be raised, if the temperature grows enough (more than TjMax) to trigger
537 +the Out-Of-Spec bit. Following table summarizes the exported sysfs files:
539 +temp1_input - Core temperature (in milidegrees of Celsius).
540 +temp1_crit - Maximum junction temperature (in milidegrees of Celsius).
541 +temp1_crit_alarm - Set when Out-of-spec bit is set, never clears.
542 + Correct CPU operation is no longer guaranteed.
543 +temp1_label - Contains string with the "Core X", where X is processor
546 +The TjMax temperature is set to 85C if undocumented model specific register
547 +(UMSR) 0xee has bit 30 set. If not the TjMax is 100C as documented in processor
548 +datasheet. Intel will not disclose this information to individuals.