2 * pkgtemp.c - Linux kernel module for processor package hardware monitoring
4 * Copyright (C) 2010 Fenghua Yu <fenghua.yu@intel.com>
6 * Inspired from many hwmon drivers especially coretemp.
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>
38 #include <asm/processor.h>
41 #define DRVNAME "pkgtemp"
43 enum { SHOW_TEMP
, SHOW_TJMAX
, SHOW_TTARGET
, SHOW_LABEL
, SHOW_NAME
};
46 * Functions declaration
49 static struct pkgtemp_data
*pkgtemp_update_device(struct device
*dev
);
52 struct device
*hwmon_dev
;
53 struct mutex update_lock
;
57 char valid
; /* zero until following fields are valid */
58 unsigned long last_updated
; /* in jiffies */
69 static ssize_t
show_name(struct device
*dev
, struct device_attribute
73 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
74 struct pkgtemp_data
*data
= dev_get_drvdata(dev
);
76 if (attr
->index
== SHOW_NAME
)
77 ret
= sprintf(buf
, "%s\n", data
->name
);
79 ret
= sprintf(buf
, "physical id %d\n",
84 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
87 struct pkgtemp_data
*data
= pkgtemp_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 pkgtemp_data
*data
= pkgtemp_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
, SHOW_TEMP
);
109 static SENSOR_DEVICE_ATTR(temp1_crit
, S_IRUGO
, show_temp
, NULL
, SHOW_TJMAX
);
110 static SENSOR_DEVICE_ATTR(temp1_max
, S_IRUGO
, show_temp
, NULL
, SHOW_TTARGET
);
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
*pkgtemp_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 pkgtemp_group
= {
125 .attrs
= pkgtemp_attributes
,
128 static struct pkgtemp_data
*pkgtemp_update_device(struct device
*dev
)
130 struct pkgtemp_data
*data
= dev_get_drvdata(dev
);
134 mutex_lock(&data
->update_lock
);
136 if (!data
->valid
|| time_after(jiffies
, data
->last_updated
+ HZ
)) {
141 err
= rdmsr_on_cpu(cpu
, MSR_IA32_PACKAGE_THERM_STATUS
,
144 data
->alarm
= (eax
>> 5) & 1;
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 get_tjmax(int cpu
, struct device
*dev
)
160 int default_tjmax
= 100000;
165 /* IA32_TEMPERATURE_TARGET contains the TjMax value */
166 err
= rdmsr_safe_on_cpu(cpu
, MSR_IA32_TEMPERATURE_TARGET
, &eax
, &edx
);
168 val
= (eax
>> 16) & 0xff;
169 if ((val
> 80) && (val
< 120)) {
170 dev_info(dev
, "TjMax is %d C.\n", val
);
174 dev_warn(dev
, "Unable to read TjMax from CPU.\n");
175 return default_tjmax
;
178 static int __devinit
pkgtemp_probe(struct platform_device
*pdev
)
180 struct pkgtemp_data
*data
;
184 struct cpuinfo_x86
*c
= &cpu_data(pdev
->id
);
187 data
= kzalloc(sizeof(struct pkgtemp_data
), GFP_KERNEL
);
190 dev_err(&pdev
->dev
, "Out of memory\n");
196 data
->phys_proc_id
= c
->phys_proc_id
;
198 data
->name
= "pkgtemp";
199 mutex_init(&data
->update_lock
);
201 /* test if we can access the THERM_STATUS MSR */
202 err
= rdmsr_safe_on_cpu(data
->id
, MSR_IA32_PACKAGE_THERM_STATUS
,
206 "Unable to access THERM_STATUS MSR, giving up\n");
210 data
->tjmax
= get_tjmax(data
->id
, &pdev
->dev
);
211 platform_set_drvdata(pdev
, data
);
213 err
= rdmsr_safe_on_cpu(data
->id
, MSR_IA32_TEMPERATURE_TARGET
,
216 dev_warn(&pdev
->dev
, "Unable to read"
217 " IA32_TEMPERATURE_TARGET MSR\n");
219 data
->ttarget
= data
->tjmax
- (((eax
>> 8) & 0xff) * 1000);
220 err
= device_create_file(&pdev
->dev
,
221 &sensor_dev_attr_temp1_max
.dev_attr
);
226 err
= sysfs_create_group(&pdev
->dev
.kobj
, &pkgtemp_group
);
230 data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
231 if (IS_ERR(data
->hwmon_dev
)) {
232 err
= PTR_ERR(data
->hwmon_dev
);
233 dev_err(&pdev
->dev
, "Class registration failed (%d)\n",
241 sysfs_remove_group(&pdev
->dev
.kobj
, &pkgtemp_group
);
243 device_remove_file(&pdev
->dev
, &sensor_dev_attr_temp1_max
.dev_attr
);
250 static int __devexit
pkgtemp_remove(struct platform_device
*pdev
)
252 struct pkgtemp_data
*data
= platform_get_drvdata(pdev
);
254 hwmon_device_unregister(data
->hwmon_dev
);
255 sysfs_remove_group(&pdev
->dev
.kobj
, &pkgtemp_group
);
256 device_remove_file(&pdev
->dev
, &sensor_dev_attr_temp1_max
.dev_attr
);
257 platform_set_drvdata(pdev
, NULL
);
262 static struct platform_driver pkgtemp_driver
= {
264 .owner
= THIS_MODULE
,
267 .probe
= pkgtemp_probe
,
268 .remove
= __devexit_p(pkgtemp_remove
),
272 struct list_head list
;
273 struct platform_device
*pdev
;
280 static LIST_HEAD(pdev_list
);
281 static DEFINE_MUTEX(pdev_list_mutex
);
283 static int __cpuinit
pkgtemp_device_add(unsigned int cpu
)
286 struct platform_device
*pdev
;
287 struct pdev_entry
*pdev_entry
;
288 struct cpuinfo_x86
*c
= &cpu_data(cpu
);
290 if (!cpu_has(c
, X86_FEATURE_PTS
))
293 mutex_lock(&pdev_list_mutex
);
296 /* Only keep the first entry in each package */
297 list_for_each_entry(pdev_entry
, &pdev_list
, list
) {
298 if (c
->phys_proc_id
== pdev_entry
->phys_proc_id
) {
299 err
= 0; /* Not an error */
305 pdev
= platform_device_alloc(DRVNAME
, cpu
);
308 pr_err("Device allocation failed\n");
312 pdev_entry
= kzalloc(sizeof(struct pdev_entry
), GFP_KERNEL
);
315 goto exit_device_put
;
318 err
= platform_device_add(pdev
);
320 pr_err("Device addition failed (%d)\n", err
);
321 goto exit_device_free
;
325 pdev_entry
->phys_proc_id
= c
->phys_proc_id
;
327 pdev_entry
->pdev
= pdev
;
328 pdev_entry
->cpu
= cpu
;
329 list_add_tail(&pdev_entry
->list
, &pdev_list
);
330 mutex_unlock(&pdev_list_mutex
);
337 platform_device_put(pdev
);
339 mutex_unlock(&pdev_list_mutex
);
343 static void __cpuinit
pkgtemp_device_remove(unsigned int cpu
)
345 struct pdev_entry
*p
;
349 mutex_lock(&pdev_list_mutex
);
350 list_for_each_entry(p
, &pdev_list
, list
) {
354 platform_device_unregister(p
->pdev
);
356 mutex_unlock(&pdev_list_mutex
);
358 for_each_cpu(i
, cpu_core_mask(cpu
)) {
360 err
= pkgtemp_device_add(i
);
367 mutex_unlock(&pdev_list_mutex
);
370 static int __cpuinit
pkgtemp_cpu_callback(struct notifier_block
*nfb
,
371 unsigned long action
, void *hcpu
)
373 unsigned int cpu
= (unsigned long) hcpu
;
377 case CPU_DOWN_FAILED
:
378 pkgtemp_device_add(cpu
);
380 case CPU_DOWN_PREPARE
:
381 pkgtemp_device_remove(cpu
);
387 static struct notifier_block pkgtemp_cpu_notifier __refdata
= {
388 .notifier_call
= pkgtemp_cpu_callback
,
391 static int __init
pkgtemp_init(void)
393 int i
, err
= -ENODEV
;
395 /* quick check if we run Intel */
396 if (cpu_data(0).x86_vendor
!= X86_VENDOR_INTEL
)
399 err
= platform_driver_register(&pkgtemp_driver
);
403 for_each_online_cpu(i
)
404 pkgtemp_device_add(i
);
406 #ifndef CONFIG_HOTPLUG_CPU
407 if (list_empty(&pdev_list
)) {
409 goto exit_driver_unreg
;
413 register_hotcpu_notifier(&pkgtemp_cpu_notifier
);
416 #ifndef CONFIG_HOTPLUG_CPU
418 platform_driver_unregister(&pkgtemp_driver
);
424 static void __exit
pkgtemp_exit(void)
426 struct pdev_entry
*p
, *n
;
428 unregister_hotcpu_notifier(&pkgtemp_cpu_notifier
);
429 mutex_lock(&pdev_list_mutex
);
430 list_for_each_entry_safe(p
, n
, &pdev_list
, list
) {
431 platform_device_unregister(p
->pdev
);
435 mutex_unlock(&pdev_list_mutex
);
436 platform_driver_unregister(&pkgtemp_driver
);
439 MODULE_AUTHOR("Fenghua Yu <fenghua.yu@intel.com>");
440 MODULE_DESCRIPTION("Intel processor package temperature monitor");
441 MODULE_LICENSE("GPL");
443 module_init(pkgtemp_init
)
444 module_exit(pkgtemp_exit
)