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 typedef enum { SHOW_TEMP
, SHOW_TJMAX
, SHOW_TTARGET
, SHOW_LABEL
,
45 * Functions declaration
48 static struct coretemp_data
*coretemp_update_device(struct device
*dev
);
50 struct coretemp_data
{
51 struct device
*hwmon_dev
;
52 struct mutex update_lock
;
55 char valid
; /* zero until following fields are valid */
56 unsigned long last_updated
; /* in jiffies */
67 static ssize_t
show_name(struct device
*dev
, struct device_attribute
71 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
72 struct coretemp_data
*data
= dev_get_drvdata(dev
);
74 if (attr
->index
== SHOW_NAME
)
75 ret
= sprintf(buf
, "%s\n", data
->name
);
77 ret
= sprintf(buf
, "Core %d\n", data
->id
);
81 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
84 struct coretemp_data
*data
= coretemp_update_device(dev
);
85 /* read the Out-of-spec log, never clear */
86 return sprintf(buf
, "%d\n", data
->alarm
);
89 static ssize_t
show_temp(struct device
*dev
,
90 struct device_attribute
*devattr
, char *buf
)
92 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
93 struct coretemp_data
*data
= coretemp_update_device(dev
);
96 if (attr
->index
== SHOW_TEMP
)
97 err
= data
->valid
? sprintf(buf
, "%d\n", data
->temp
) : -EAGAIN
;
98 else if (attr
->index
== SHOW_TJMAX
)
99 err
= sprintf(buf
, "%d\n", data
->tjmax
);
101 err
= sprintf(buf
, "%d\n", data
->ttarget
);
105 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp
, NULL
,
107 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IRUGO
, show_temp
, NULL
,
109 static SENSOR_DEVICE_ATTR(temp1_max
, S_IRUGO
, show_temp
, NULL
,
111 static DEVICE_ATTR(temp1_crit_alarm
, S_IRUGO
, show_alarm
, NULL
);
112 static SENSOR_DEVICE_ATTR(temp1_label
, S_IRUGO
, show_name
, NULL
, SHOW_LABEL
);
113 static SENSOR_DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
, SHOW_NAME
);
115 static struct attribute
*coretemp_attributes
[] = {
116 &sensor_dev_attr_name
.dev_attr
.attr
,
117 &sensor_dev_attr_temp1_label
.dev_attr
.attr
,
118 &dev_attr_temp1_crit_alarm
.attr
,
119 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
120 &sensor_dev_attr_temp1_crit
.dev_attr
.attr
,
124 static const struct attribute_group coretemp_group
= {
125 .attrs
= coretemp_attributes
,
128 static struct coretemp_data
*coretemp_update_device(struct device
*dev
)
130 struct coretemp_data
*data
= dev_get_drvdata(dev
);
132 mutex_lock(&data
->update_lock
);
134 if (!data
->valid
|| time_after(jiffies
, data
->last_updated
+ HZ
)) {
138 rdmsr_on_cpu(data
->id
, MSR_IA32_THERM_STATUS
, &eax
, &edx
);
139 data
->alarm
= (eax
>> 5) & 1;
140 /* update only if data has been valid */
141 if (eax
& 0x80000000) {
142 data
->temp
= data
->tjmax
- (((eax
>> 16)
146 dev_dbg(dev
, "Temperature data invalid (0x%x)\n", eax
);
148 data
->last_updated
= jiffies
;
151 mutex_unlock(&data
->update_lock
);
155 static int __devinit
adjust_tjmax(struct cpuinfo_x86
*c
, u32 id
, struct device
*dev
)
157 /* The 100C is default for both mobile and non mobile CPUs */
164 /* Early chips have no MSR for TjMax */
166 if ((c
->x86_model
== 0xf) && (c
->x86_mask
< 4)) {
170 if ((c
->x86_model
> 0xe) && (ismobile
)) {
172 /* Now we can detect the mobile CPU using Intel provided table
173 http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
174 For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
177 err
= rdmsr_safe_on_cpu(id
, 0x17, &eax
, &edx
);
180 "Unable to access MSR 0x17, assuming desktop"
183 } else if (!(eax
& 0x10000000)) {
190 err
= rdmsr_safe_on_cpu(id
, 0xee, &eax
, &edx
);
193 "Unable to access MSR 0xEE, for Tjmax, left"
195 } else if (eax
& 0x40000000) {
199 dev_warn(dev
, "Using relative temperature scale!\n");
205 static int __devinit
coretemp_probe(struct platform_device
*pdev
)
207 struct coretemp_data
*data
;
208 struct cpuinfo_x86
*c
= &cpu_data(pdev
->id
);
212 if (!(data
= kzalloc(sizeof(struct coretemp_data
), GFP_KERNEL
))) {
214 dev_err(&pdev
->dev
, "Out of memory\n");
219 data
->name
= "coretemp";
220 mutex_init(&data
->update_lock
);
222 /* test if we can access the THERM_STATUS MSR */
223 err
= rdmsr_safe_on_cpu(data
->id
, MSR_IA32_THERM_STATUS
, &eax
, &edx
);
226 "Unable to access THERM_STATUS MSR, giving up\n");
230 /* Check if we have problem with errata AE18 of Core processors:
231 Readings might stop update when processor visited too deep sleep,
232 fixed for stepping D0 (6EC).
235 if ((c
->x86_model
== 0xe) && (c
->x86_mask
< 0xc)) {
236 /* check for microcode update */
237 rdmsr_on_cpu(data
->id
, MSR_IA32_UCODE_REV
, &eax
, &edx
);
241 "Errata AE18 not fixed, update BIOS or "
242 "microcode of the CPU!\n");
247 data
->tjmax
= adjust_tjmax(c
, data
->id
, &pdev
->dev
);
248 platform_set_drvdata(pdev
, data
);
250 /* read the still undocumented IA32_TEMPERATURE_TARGET it exists
251 on older CPUs but not in this register */
253 if (c
->x86_model
> 0xe) {
254 err
= rdmsr_safe_on_cpu(data
->id
, 0x1a2, &eax
, &edx
);
256 dev_warn(&pdev
->dev
, "Unable to read"
257 " IA32_TEMPERATURE_TARGET MSR\n");
259 data
->ttarget
= data
->tjmax
-
260 (((eax
>> 8) & 0xff) * 1000);
261 err
= device_create_file(&pdev
->dev
,
262 &sensor_dev_attr_temp1_max
.dev_attr
);
268 if ((err
= sysfs_create_group(&pdev
->dev
.kobj
, &coretemp_group
)))
271 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
272 if (IS_ERR(data
->hwmon_dev
)) {
273 err
= PTR_ERR(data
->hwmon_dev
);
274 dev_err(&pdev
->dev
, "Class registration failed (%d)\n",
282 sysfs_remove_group(&pdev
->dev
.kobj
, &coretemp_group
);
284 device_remove_file(&pdev
->dev
, &sensor_dev_attr_temp1_max
.dev_attr
);
291 static int __devexit
coretemp_remove(struct platform_device
*pdev
)
293 struct coretemp_data
*data
= platform_get_drvdata(pdev
);
295 hwmon_device_unregister(data
->hwmon_dev
);
296 sysfs_remove_group(&pdev
->dev
.kobj
, &coretemp_group
);
297 device_remove_file(&pdev
->dev
, &sensor_dev_attr_temp1_max
.dev_attr
);
298 platform_set_drvdata(pdev
, NULL
);
303 static struct platform_driver coretemp_driver
= {
305 .owner
= THIS_MODULE
,
308 .probe
= coretemp_probe
,
309 .remove
= __devexit_p(coretemp_remove
),
313 struct list_head list
;
314 struct platform_device
*pdev
;
318 static LIST_HEAD(pdev_list
);
319 static DEFINE_MUTEX(pdev_list_mutex
);
321 static int __cpuinit
coretemp_device_add(unsigned int cpu
)
324 struct platform_device
*pdev
;
325 struct pdev_entry
*pdev_entry
;
327 pdev
= platform_device_alloc(DRVNAME
, cpu
);
330 printk(KERN_ERR DRVNAME
": Device allocation failed\n");
334 pdev_entry
= kzalloc(sizeof(struct pdev_entry
), GFP_KERNEL
);
337 goto exit_device_put
;
340 err
= platform_device_add(pdev
);
342 printk(KERN_ERR DRVNAME
": Device addition failed (%d)\n",
344 goto exit_device_free
;
347 pdev_entry
->pdev
= pdev
;
348 pdev_entry
->cpu
= cpu
;
349 mutex_lock(&pdev_list_mutex
);
350 list_add_tail(&pdev_entry
->list
, &pdev_list
);
351 mutex_unlock(&pdev_list_mutex
);
358 platform_device_put(pdev
);
363 #ifdef CONFIG_HOTPLUG_CPU
364 static void coretemp_device_remove(unsigned int cpu
)
366 struct pdev_entry
*p
, *n
;
367 mutex_lock(&pdev_list_mutex
);
368 list_for_each_entry_safe(p
, n
, &pdev_list
, list
) {
370 platform_device_unregister(p
->pdev
);
375 mutex_unlock(&pdev_list_mutex
);
378 static int __cpuinit
coretemp_cpu_callback(struct notifier_block
*nfb
,
379 unsigned long action
, void *hcpu
)
381 unsigned int cpu
= (unsigned long) hcpu
;
385 case CPU_DOWN_FAILED
:
386 coretemp_device_add(cpu
);
388 case CPU_DOWN_PREPARE
:
389 coretemp_device_remove(cpu
);
395 static struct notifier_block coretemp_cpu_notifier __refdata
= {
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 0xe, 0xf, 0x16, 0x17 */
417 if ((c
->cpuid_level
< 0) || (c
->x86
!= 0x6) ||
418 !((c
->x86_model
== 0xe) || (c
->x86_model
== 0xf) ||
419 (c
->x86_model
== 0x16) || (c
->x86_model
== 0x17))) {
421 /* supported CPU not found, but report the unknown
423 if ((c
->x86
== 0x6) && (c
->x86_model
> 0xf))
424 printk(KERN_WARNING DRVNAME
": Unknown CPU "
425 "model %x\n", c
->x86_model
);
429 err
= coretemp_device_add(i
);
431 goto exit_devices_unreg
;
433 if (list_empty(&pdev_list
)) {
435 goto exit_driver_unreg
;
438 #ifdef CONFIG_HOTPLUG_CPU
439 register_hotcpu_notifier(&coretemp_cpu_notifier
);
444 mutex_lock(&pdev_list_mutex
);
445 list_for_each_entry_safe(p
, n
, &pdev_list
, list
) {
446 platform_device_unregister(p
->pdev
);
450 mutex_unlock(&pdev_list_mutex
);
452 platform_driver_unregister(&coretemp_driver
);
457 static void __exit
coretemp_exit(void)
459 struct pdev_entry
*p
, *n
;
460 #ifdef CONFIG_HOTPLUG_CPU
461 unregister_hotcpu_notifier(&coretemp_cpu_notifier
);
463 mutex_lock(&pdev_list_mutex
);
464 list_for_each_entry_safe(p
, n
, &pdev_list
, list
) {
465 platform_device_unregister(p
->pdev
);
469 mutex_unlock(&pdev_list_mutex
);
470 platform_driver_unregister(&coretemp_driver
);
473 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
474 MODULE_DESCRIPTION("Intel Core temperature monitor");
475 MODULE_LICENSE("GPL");
477 module_init(coretemp_init
)
478 module_exit(coretemp_exit
)