blob: 93c17223b527018271b0febf9b96b4af4d030670 [file] [log] [blame]
Rudolf Marekbebe4672007-05-08 17:22:02 +02001/*
2 * coretemp.c - Linux kernel module for hardware monitoring
3 *
4 * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
5 *
6 * Inspired from many hwmon drivers
7 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA.
21 */
22
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 <asm/msr.h>
37#include <asm/processor.h>
38
39#define DRVNAME "coretemp"
40
Rudolf Marek6369a2882008-01-18 00:42:54 +010041typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL,
42 SHOW_NAME } SHOW;
Rudolf Marekbebe4672007-05-08 17:22:02 +020043
44/*
45 * Functions declaration
46 */
47
48static struct coretemp_data *coretemp_update_device(struct device *dev);
49
50struct coretemp_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070051 struct device *hwmon_dev;
Rudolf Marekbebe4672007-05-08 17:22:02 +020052 struct mutex update_lock;
53 const char *name;
54 u32 id;
55 char valid; /* zero until following fields are valid */
56 unsigned long last_updated; /* in jiffies */
57 int temp;
58 int tjmax;
Rudolf Marek6369a2882008-01-18 00:42:54 +010059 int ttarget;
Rudolf Marekbebe4672007-05-08 17:22:02 +020060 u8 alarm;
61};
62
Rudolf Marekbebe4672007-05-08 17:22:02 +020063/*
64 * Sysfs stuff
65 */
66
67static ssize_t show_name(struct device *dev, struct device_attribute
68 *devattr, char *buf)
69{
70 int ret;
71 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
72 struct coretemp_data *data = dev_get_drvdata(dev);
73
74 if (attr->index == SHOW_NAME)
75 ret = sprintf(buf, "%s\n", data->name);
76 else /* show label */
77 ret = sprintf(buf, "Core %d\n", data->id);
78 return ret;
79}
80
81static ssize_t show_alarm(struct device *dev, struct device_attribute
82 *devattr, char *buf)
83{
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);
87}
88
89static ssize_t show_temp(struct device *dev,
90 struct device_attribute *devattr, char *buf)
91{
92 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
93 struct coretemp_data *data = coretemp_update_device(dev);
94 int err;
95
96 if (attr->index == SHOW_TEMP)
97 err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN;
Rudolf Marek6369a2882008-01-18 00:42:54 +010098 else if (attr->index == SHOW_TJMAX)
Rudolf Marekbebe4672007-05-08 17:22:02 +020099 err = sprintf(buf, "%d\n", data->tjmax);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100100 else
101 err = sprintf(buf, "%d\n", data->ttarget);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200102 return err;
103}
104
105static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
106 SHOW_TEMP);
107static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
108 SHOW_TJMAX);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100109static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL,
110 SHOW_TTARGET);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200111static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
112static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
113static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
114
115static 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,
121 NULL
122};
123
124static const struct attribute_group coretemp_group = {
125 .attrs = coretemp_attributes,
126};
127
128static struct coretemp_data *coretemp_update_device(struct device *dev)
129{
130 struct coretemp_data *data = dev_get_drvdata(dev);
131
132 mutex_lock(&data->update_lock);
133
134 if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
135 u32 eax, edx;
136
137 data->valid = 0;
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)
143 & 0x7f) * 1000);
144 data->valid = 1;
145 } else {
146 dev_dbg(dev, "Temperature data invalid (0x%x)\n", eax);
147 }
148 data->last_updated = jiffies;
149 }
150
151 mutex_unlock(&data->update_lock);
152 return data;
153}
154
Rudolf Marek118a8872008-02-17 22:59:39 +0100155static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
156{
157 /* The 100C is default for both mobile and non mobile CPUs */
158
159 int tjmax = 100000;
160 int ismobile = 1;
161 int err;
162 u32 eax, edx;
163
164 /* Early chips have no MSR for TjMax */
165
166 if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
167 ismobile = 0;
168 }
169
170 if ((c->x86_model > 0xe) && (ismobile)) {
171
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
175 */
176
177 err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
178 if (err) {
179 dev_warn(dev,
180 "Unable to access MSR 0x17, assuming desktop"
181 " CPU\n");
182 ismobile = 0;
183 } else if (!(eax & 0x10000000)) {
184 ismobile = 0;
185 }
186 }
187
188 if (ismobile) {
189
190 err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
191 if (err) {
192 dev_warn(dev,
193 "Unable to access MSR 0xEE, for Tjmax, left"
194 " at default");
195 } else if (eax & 0x40000000) {
196 tjmax = 85000;
197 }
198 } else {
199 dev_warn(dev, "Using relative temperature scale!\n");
200 }
201
202 return tjmax;
203}
204
Rudolf Marekbebe4672007-05-08 17:22:02 +0200205static int __devinit coretemp_probe(struct platform_device *pdev)
206{
207 struct coretemp_data *data;
Mike Travis92cb7612007-10-19 20:35:04 +0200208 struct cpuinfo_x86 *c = &cpu_data(pdev->id);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200209 int err;
210 u32 eax, edx;
211
212 if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
213 err = -ENOMEM;
214 dev_err(&pdev->dev, "Out of memory\n");
215 goto exit;
216 }
217
218 data->id = pdev->id;
219 data->name = "coretemp";
220 mutex_init(&data->update_lock);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200221
222 /* test if we can access the THERM_STATUS MSR */
223 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
224 if (err) {
225 dev_err(&pdev->dev,
226 "Unable to access THERM_STATUS MSR, giving up\n");
227 goto exit_free;
228 }
229
Rudolf Marek67f363b2007-05-27 22:17:43 +0200230 /* 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).
233 */
234
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);
238 if (edx < 0x39) {
Jean Delvare6d79af72007-06-23 17:16:24 -0700239 err = -ENODEV;
Rudolf Marek67f363b2007-05-27 22:17:43 +0200240 dev_err(&pdev->dev,
241 "Errata AE18 not fixed, update BIOS or "
242 "microcode of the CPU!\n");
243 goto exit_free;
244 }
245 }
246
Rudolf Marek118a8872008-02-17 22:59:39 +0100247 data->tjmax = adjust_tjmax(c, data->id, &pdev->dev);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200248 platform_set_drvdata(pdev, data);
249
Rudolf Marek6369a2882008-01-18 00:42:54 +0100250 /* read the still undocumented IA32_TEMPERATURE_TARGET it exists
251 on older CPUs but not in this register */
252
253 if (c->x86_model > 0xe) {
254 err = rdmsr_safe_on_cpu(data->id, 0x1a2, &eax, &edx);
255 if (err) {
256 dev_warn(&pdev->dev, "Unable to read"
257 " IA32_TEMPERATURE_TARGET MSR\n");
258 } else {
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);
263 if (err)
264 goto exit_free;
265 }
266 }
267
Rudolf Marekbebe4672007-05-08 17:22:02 +0200268 if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
Rudolf Marek6369a2882008-01-18 00:42:54 +0100269 goto exit_dev;
Rudolf Marekbebe4672007-05-08 17:22:02 +0200270
Tony Jones1beeffe2007-08-20 13:46:20 -0700271 data->hwmon_dev = hwmon_device_register(&pdev->dev);
272 if (IS_ERR(data->hwmon_dev)) {
273 err = PTR_ERR(data->hwmon_dev);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200274 dev_err(&pdev->dev, "Class registration failed (%d)\n",
275 err);
276 goto exit_class;
277 }
278
279 return 0;
280
281exit_class:
282 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100283exit_dev:
284 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200285exit_free:
286 kfree(data);
287exit:
288 return err;
289}
290
291static int __devexit coretemp_remove(struct platform_device *pdev)
292{
293 struct coretemp_data *data = platform_get_drvdata(pdev);
294
Tony Jones1beeffe2007-08-20 13:46:20 -0700295 hwmon_device_unregister(data->hwmon_dev);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200296 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100297 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200298 platform_set_drvdata(pdev, NULL);
299 kfree(data);
300 return 0;
301}
302
303static struct platform_driver coretemp_driver = {
304 .driver = {
305 .owner = THIS_MODULE,
306 .name = DRVNAME,
307 },
308 .probe = coretemp_probe,
309 .remove = __devexit_p(coretemp_remove),
310};
311
312struct pdev_entry {
313 struct list_head list;
314 struct platform_device *pdev;
315 unsigned int cpu;
316};
317
318static LIST_HEAD(pdev_list);
319static DEFINE_MUTEX(pdev_list_mutex);
320
321static int __cpuinit coretemp_device_add(unsigned int cpu)
322{
323 int err;
324 struct platform_device *pdev;
325 struct pdev_entry *pdev_entry;
326
327 pdev = platform_device_alloc(DRVNAME, cpu);
328 if (!pdev) {
329 err = -ENOMEM;
330 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
331 goto exit;
332 }
333
334 pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
335 if (!pdev_entry) {
336 err = -ENOMEM;
337 goto exit_device_put;
338 }
339
340 err = platform_device_add(pdev);
341 if (err) {
342 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
343 err);
344 goto exit_device_free;
345 }
346
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);
352
353 return 0;
354
355exit_device_free:
356 kfree(pdev_entry);
357exit_device_put:
358 platform_device_put(pdev);
359exit:
360 return err;
361}
362
363#ifdef CONFIG_HOTPLUG_CPU
Adrian Bunkd2bc7b12007-07-06 01:23:06 +0200364static void coretemp_device_remove(unsigned int cpu)
Rudolf Marekbebe4672007-05-08 17:22:02 +0200365{
366 struct pdev_entry *p, *n;
367 mutex_lock(&pdev_list_mutex);
368 list_for_each_entry_safe(p, n, &pdev_list, list) {
369 if (p->cpu == cpu) {
370 platform_device_unregister(p->pdev);
371 list_del(&p->list);
372 kfree(p);
373 }
374 }
375 mutex_unlock(&pdev_list_mutex);
376}
377
Sam Ravnborgba7c1922008-02-17 13:22:51 +0100378static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
Rudolf Marekbebe4672007-05-08 17:22:02 +0200379 unsigned long action, void *hcpu)
380{
381 unsigned int cpu = (unsigned long) hcpu;
382
383 switch (action) {
384 case CPU_ONLINE:
Rafael J. Wysocki561d9a92007-12-03 18:01:50 +0100385 case CPU_DOWN_FAILED:
Rudolf Marekbebe4672007-05-08 17:22:02 +0200386 coretemp_device_add(cpu);
387 break;
Rafael J. Wysocki561d9a92007-12-03 18:01:50 +0100388 case CPU_DOWN_PREPARE:
Rudolf Marekbebe4672007-05-08 17:22:02 +0200389 coretemp_device_remove(cpu);
390 break;
391 }
392 return NOTIFY_OK;
393}
394
Sam Ravnborgba7c1922008-02-17 13:22:51 +0100395static struct notifier_block coretemp_cpu_notifier __refdata = {
Rudolf Marekbebe4672007-05-08 17:22:02 +0200396 .notifier_call = coretemp_cpu_callback,
397};
398#endif /* !CONFIG_HOTPLUG_CPU */
399
400static int __init coretemp_init(void)
401{
402 int i, err = -ENODEV;
403 struct pdev_entry *p, *n;
404
Rudolf Marekbebe4672007-05-08 17:22:02 +0200405 /* quick check if we run Intel */
Mike Travis92cb7612007-10-19 20:35:04 +0200406 if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
Rudolf Marekbebe4672007-05-08 17:22:02 +0200407 goto exit;
408
409 err = platform_driver_register(&coretemp_driver);
410 if (err)
411 goto exit;
412
413 for_each_online_cpu(i) {
Mike Travis92cb7612007-10-19 20:35:04 +0200414 struct cpuinfo_x86 *c = &cpu_data(i);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200415
Darrick J. Wong34c86c12008-08-15 00:40:41 -0700416 /* check if family 6, models 0xe, 0xf, 0x16, 0x17, 0x1A */
Rudolf Marekbebe4672007-05-08 17:22:02 +0200417 if ((c->cpuid_level < 0) || (c->x86 != 0x6) ||
Rudolf Marekc9403362007-10-07 13:42:09 +0200418 !((c->x86_model == 0xe) || (c->x86_model == 0xf) ||
Darrick J. Wong34c86c12008-08-15 00:40:41 -0700419 (c->x86_model == 0x16) || (c->x86_model == 0x17) ||
420 (c->x86_model == 0x1A))) {
Rudolf Marekbebe4672007-05-08 17:22:02 +0200421
422 /* supported CPU not found, but report the unknown
423 family 6 CPU */
424 if ((c->x86 == 0x6) && (c->x86_model > 0xf))
425 printk(KERN_WARNING DRVNAME ": Unknown CPU "
426 "model %x\n", c->x86_model);
427 continue;
428 }
429
430 err = coretemp_device_add(i);
431 if (err)
432 goto exit_devices_unreg;
433 }
434 if (list_empty(&pdev_list)) {
435 err = -ENODEV;
436 goto exit_driver_unreg;
437 }
438
439#ifdef CONFIG_HOTPLUG_CPU
440 register_hotcpu_notifier(&coretemp_cpu_notifier);
441#endif
442 return 0;
443
444exit_devices_unreg:
445 mutex_lock(&pdev_list_mutex);
446 list_for_each_entry_safe(p, n, &pdev_list, list) {
447 platform_device_unregister(p->pdev);
448 list_del(&p->list);
449 kfree(p);
450 }
451 mutex_unlock(&pdev_list_mutex);
452exit_driver_unreg:
453 platform_driver_unregister(&coretemp_driver);
454exit:
455 return err;
456}
457
458static void __exit coretemp_exit(void)
459{
460 struct pdev_entry *p, *n;
461#ifdef CONFIG_HOTPLUG_CPU
462 unregister_hotcpu_notifier(&coretemp_cpu_notifier);
463#endif
464 mutex_lock(&pdev_list_mutex);
465 list_for_each_entry_safe(p, n, &pdev_list, list) {
466 platform_device_unregister(p->pdev);
467 list_del(&p->list);
468 kfree(p);
469 }
470 mutex_unlock(&pdev_list_mutex);
471 platform_driver_unregister(&coretemp_driver);
472}
473
474MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
475MODULE_DESCRIPTION("Intel Core temperature monitor");
476MODULE_LICENSE("GPL");
477
478module_init(coretemp_init)
479module_exit(coretemp_exit)