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>
37 #include <asm/processor.h>
39 #define DRVNAME "coretemp"
41 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
42 typedef enum { SHOW_TEMP
, SHOW_TJMAX
, SHOW_LABEL
, SHOW_NAME
} SHOW
;
44 typedef enum { SHOW_TEMP
, SHOW_TJMAX
, SHOW_TTARGET
, SHOW_LABEL
,
46 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
49 * Functions declaration
52 static struct coretemp_data
*coretemp_update_device(struct device
*dev
);
54 struct coretemp_data
{
55 struct device
*hwmon_dev
;
56 struct mutex update_lock
;
59 char valid
; /* zero until following fields are valid */
60 unsigned long last_updated
; /* in jiffies */
63 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
66 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
74 static ssize_t
show_name(struct device
*dev
, struct device_attribute
78 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
79 struct coretemp_data
*data
= dev_get_drvdata(dev
);
81 if (attr
->index
== SHOW_NAME
)
82 ret
= sprintf(buf
, "%s\n", data
->name
);
84 ret
= sprintf(buf
, "Core %d\n", data
->id
);
88 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
91 struct coretemp_data
*data
= coretemp_update_device(dev
);
92 /* read the Out-of-spec log, never clear */
93 return sprintf(buf
, "%d\n", data
->alarm
);
96 static ssize_t
show_temp(struct device
*dev
,
97 struct device_attribute
*devattr
, char *buf
)
99 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
100 struct coretemp_data
*data
= coretemp_update_device(dev
);
103 if (attr
->index
== SHOW_TEMP
)
104 err
= data
->valid
? sprintf(buf
, "%d\n", data
->temp
) : -EAGAIN
;
105 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
108 else if (attr
->index
== SHOW_TJMAX
)
109 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
110 err
= sprintf(buf
, "%d\n", data
->tjmax
);
111 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
115 err
= sprintf(buf
, "%d\n", data
->ttarget
);
116 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
120 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp
, NULL
,
122 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IRUGO
, show_temp
, NULL
,
124 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
126 static SENSOR_DEVICE_ATTR(temp1_max
, S_IRUGO
, show_temp
, NULL
,
128 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
129 static DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
);
130 static SENSOR_DEVICE_ATTR(temp1_label
, S_IRUGO
, show_name
, NULL
, SHOW_LABEL
);
131 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
, SHOW_NAME
);
133 static struct attribute
*coretemp_attributes
[] = {
134 &sensor_dev_attr_name
.dev_attr
.attr
,
135 &sensor_dev_attr_temp1_label
.dev_attr
.attr
,
136 &dev_attr_temp1_crit_alarm
.attr
,
137 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
138 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
142 static const struct attribute_group coretemp_group
= {
143 .attrs
= coretemp_attributes
,
146 static struct coretemp_data
*coretemp_update_device(struct device
*dev
)
148 struct coretemp_data
*data
= dev_get_drvdata(dev
);
150 mutex_lock(&data
->update_lock
);
152 if (!data
->valid
|| time_after(jiffies
, data
->last_updated
+ HZ
)) {
156 rdmsr_on_cpu(data
->id
, MSR_IA32_THERM_STATUS
, &eax
, &edx
);
157 data
->alarm
= (eax
>> 5) & 1;
158 /* update only if data has been valid */
159 if (eax
& 0x80000000) {
160 data
->temp
= data
->tjmax
- (((eax
>> 16)
164 dev_dbg(dev
, "Temperature data invalid (0x%x)\n", eax
);
166 data
->last_updated
= jiffies
;
169 mutex_unlock(&data
->update_lock
);
173 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
175 static int __devinit
adjust_tjmax(struct cpuinfo_x86
*c
, u32 id
, struct device
*dev
)
177 /* The 100C is default for both mobile and non mobile CPUs */
184 /* Early chips have no MSR for TjMax */
186 if ((c
->x86_model
== 0xf) && (c
->x86_mask
< 4)) {
190 if ((c
->x86_model
> 0xe) && (ismobile
)) {
192 /* Now we can detect the mobile CPU using Intel provided table
193 http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
194 For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
197 err
= rdmsr_safe_on_cpu(id
, 0x17, &eax
, &edx
);
200 "Unable to access MSR 0x17, assuming desktop"
203 } else if (!(eax
& 0x10000000)) {
210 err
= rdmsr_safe_on_cpu(id
, 0xee, &eax
, &edx
);
213 "Unable to access MSR 0xEE, for Tjmax, left"
215 } else if (eax
& 0x40000000) {
219 dev_warn(dev
, "Using relative temperature scale!\n");
225 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
226 static int __devinit
coretemp_probe(struct platform_device
*pdev
)
228 struct coretemp_data
*data
;
229 struct cpuinfo_x86
*c
= &cpu_data(pdev
->id
);
233 if (!(data
= kzalloc(sizeof(struct coretemp_data
), GFP_KERNEL
))) {
235 dev_err(&pdev
->dev
, "Out of memory\n");
240 data
->name
= "coretemp";
241 mutex_init(&data
->update_lock
);
242 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
243 /* Tjmax default is 100 degrees C */
244 data
->tjmax
= 100000;
246 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
248 /* test if we can access the THERM_STATUS MSR */
249 err
= rdmsr_safe_on_cpu(data
->id
, MSR_IA32_THERM_STATUS
, &eax
, &edx
);
252 "Unable to access THERM_STATUS MSR, giving up\n");
256 /* Check if we have problem with errata AE18 of Core processors:
257 Readings might stop update when processor visited too deep sleep,
258 fixed for stepping D0 (6EC).
261 if ((c
->x86_model
== 0xe) && (c
->x86_mask
< 0xc)) {
262 /* check for microcode update */
263 rdmsr_on_cpu(data
->id
, MSR_IA32_UCODE_REV
, &eax
, &edx
);
267 "Errata AE18 not fixed, update BIOS or "
268 "microcode of the CPU!\n");
273 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
274 /* Some processors have Tjmax 85 following magic should detect it
275 Intel won't disclose the information without signed NDA, but
276 individuals cannot sign it. Catch(ed) 22.
279 data
->tjmax
= adjust_tjmax(c
, data
->id
, &pdev
->dev
);
280 platform_set_drvdata(pdev
, data
);
281 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
283 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
284 if (((c
->x86_model
== 0xf) && (c
->x86_mask
> 3)) ||
285 (c
->x86_model
== 0xe)) {
286 err
= rdmsr_safe_on_cpu(data
->id
, 0xee, &eax
, &edx
);
288 /* read the still undocumented IA32_TEMPERATURE_TARGET it exists
289 on older CPUs but not in this register */
291 if (c
->x86_model
> 0xe) {
292 err
= rdmsr_safe_on_cpu(data
->id
, 0x1a2, &eax
, &edx
);
293 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
295 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
297 "Unable to access MSR 0xEE, Tjmax left at %d "
298 "degrees C\n", data
->tjmax
/1000);
299 } else if (eax
& 0x40000000) {
302 dev_warn(&pdev
->dev
, "Unable to read"
303 " IA32_TEMPERATURE_TARGET MSR\n");
305 data
->ttarget
= data
->tjmax
-
306 (((eax
>> 8) & 0xff) * 1000);
307 err
= device_create_file(&pdev
->dev
,
308 &sensor_dev_attr_temp1_max
.dev_attr
);
311 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
315 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
316 /* Intel says that above should not work for desktop Core2 processors,
317 but it seems to work. There is no other way how get the absolute
318 readings. Warn the user about this. First check if are desktop,
319 bit 50 of MSR_IA32_PLATFORM_ID should be 0.
322 rdmsr_safe_on_cpu(data
->id
, MSR_IA32_PLATFORM_ID
, &eax
, &edx
);
324 if ((c
->x86_model
== 0xf) && (!(edx
& 0x00040000))) {
325 dev_warn(&pdev
->dev
, "Using undocumented features, absolute "
326 "temperature might be wrong!\n");
329 platform_set_drvdata(pdev
, data
);
332 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
333 if ((err
= sysfs_create_group(&pdev
->dev
.kobj
, &coretemp_group
)))
334 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
338 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
340 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
341 if (IS_ERR(data
->hwmon_dev
)) {
342 err
= PTR_ERR(data
->hwmon_dev
);
343 dev_err(&pdev
->dev
, "Class registration failed (%d)\n",
351 sysfs_remove_group(&pdev
->dev
.kobj
, &coretemp_group
);
352 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
355 device_remove_file(&pdev
->dev
, &sensor_dev_attr_temp1_max
.dev_attr
);
356 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
363 static int __devexit
coretemp_remove(struct platform_device
*pdev
)
365 struct coretemp_data
*data
= platform_get_drvdata(pdev
);
367 hwmon_device_unregister(data
->hwmon_dev
);
368 sysfs_remove_group(&pdev
->dev
.kobj
, &coretemp_group
);
369 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
371 device_remove_file(&pdev
->dev
, &sensor_dev_attr_temp1_max
.dev_attr
);
372 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
373 platform_set_drvdata(pdev
, NULL
);
378 static struct platform_driver coretemp_driver
= {
380 .owner
= THIS_MODULE
,
383 .probe
= coretemp_probe
,
384 .remove
= __devexit_p(coretemp_remove
),
388 struct list_head list
;
389 struct platform_device
*pdev
;
393 static LIST_HEAD(pdev_list
);
394 static DEFINE_MUTEX(pdev_list_mutex
);
396 static int __cpuinit
coretemp_device_add(unsigned int cpu
)
399 struct platform_device
*pdev
;
400 struct pdev_entry
*pdev_entry
;
402 pdev
= platform_device_alloc(DRVNAME
, cpu
);
405 printk(KERN_ERR DRVNAME
": Device allocation failed\n");
409 pdev_entry
= kzalloc(sizeof(struct pdev_entry
), GFP_KERNEL
);
412 goto exit_device_put
;
415 err
= platform_device_add(pdev
);
417 printk(KERN_ERR DRVNAME
": Device addition failed (%d)\n",
419 goto exit_device_free
;
422 pdev_entry
->pdev
= pdev
;
423 pdev_entry
->cpu
= cpu
;
424 mutex_lock(&pdev_list_mutex
);
425 list_add_tail(&pdev_entry
->list
, &pdev_list
);
426 mutex_unlock(&pdev_list_mutex
);
433 platform_device_put(pdev
);
438 #ifdef CONFIG_HOTPLUG_CPU
439 static void coretemp_device_remove(unsigned int cpu
)
441 struct pdev_entry
*p
, *n
;
442 mutex_lock(&pdev_list_mutex
);
443 list_for_each_entry_safe(p
, n
, &pdev_list
, list
) {
445 platform_device_unregister(p
->pdev
);
450 mutex_unlock(&pdev_list_mutex
);
453 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
454 static int coretemp_cpu_callback(struct notifier_block
*nfb
,
456 static int __cpuinit
coretemp_cpu_callback(struct notifier_block
*nfb
,
457 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
458 unsigned long action
, void *hcpu
)
460 unsigned int cpu
= (unsigned long) hcpu
;
464 case CPU_DOWN_FAILED
:
465 coretemp_device_add(cpu
);
467 case CPU_DOWN_PREPARE
:
468 coretemp_device_remove(cpu
);
474 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
475 static struct notifier_block coretemp_cpu_notifier
= {
477 static struct notifier_block coretemp_cpu_notifier __refdata
= {
478 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
479 .notifier_call
= coretemp_cpu_callback
,
481 #endif /* !CONFIG_HOTPLUG_CPU */
483 static int __init
coretemp_init(void)
485 int i
, err
= -ENODEV
;
486 struct pdev_entry
*p
, *n
;
488 /* quick check if we run Intel */
489 if (cpu_data(0).x86_vendor
!= X86_VENDOR_INTEL
)
492 err
= platform_driver_register(&coretemp_driver
);
496 for_each_online_cpu(i
) {
497 struct cpuinfo_x86
*c
= &cpu_data(i
);
499 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
500 /* check if family 6, models e, f, 16 */
502 /* check if family 6, models 0xe, 0xf, 0x16, 0x17 */
503 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
504 if ((c
->cpuid_level
< 0) || (c
->x86
!= 0x6) ||
505 !((c
->x86_model
== 0xe) || (c
->x86_model
== 0xf) ||
506 <<<<<<< HEAD
:drivers
/hwmon
/coretemp
.c
507 (c
->x86_model
== 0x16))) {
509 (c
->x86_model
== 0x16) || (c
->x86_model
== 0x17))) {
510 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/hwmon
/coretemp
.c
512 /* supported CPU not found, but report the unknown
514 if ((c
->x86
== 0x6) && (c
->x86_model
> 0xf))
515 printk(KERN_WARNING DRVNAME
": Unknown CPU "
516 "model %x\n", c
->x86_model
);
520 err
= coretemp_device_add(i
);
522 goto exit_devices_unreg
;
524 if (list_empty(&pdev_list
)) {
526 goto exit_driver_unreg
;
529 #ifdef CONFIG_HOTPLUG_CPU
530 register_hotcpu_notifier(&coretemp_cpu_notifier
);
535 mutex_lock(&pdev_list_mutex
);
536 list_for_each_entry_safe(p
, n
, &pdev_list
, list
) {
537 platform_device_unregister(p
->pdev
);
541 mutex_unlock(&pdev_list_mutex
);
543 platform_driver_unregister(&coretemp_driver
);
548 static void __exit
coretemp_exit(void)
550 struct pdev_entry
*p
, *n
;
551 #ifdef CONFIG_HOTPLUG_CPU
552 unregister_hotcpu_notifier(&coretemp_cpu_notifier
);
554 mutex_lock(&pdev_list_mutex
);
555 list_for_each_entry_safe(p
, n
, &pdev_list
, list
) {
556 platform_device_unregister(p
->pdev
);
560 mutex_unlock(&pdev_list_mutex
);
561 platform_driver_unregister(&coretemp_driver
);
564 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
565 MODULE_DESCRIPTION("Intel Core temperature monitor");
566 MODULE_LICENSE("GPL");
568 module_init(coretemp_init
)
569 module_exit(coretemp_exit
)