hwmon: (coretemp) Skip duplicate CPU entries
[linux/fpc-iii.git] / drivers / hwmon / coretemp.c
blob585219167fa754ac72f9e0a96c15d278cc36fc67
1 /*
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
20 * 02110-1301 USA.
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 <linux/pci.h>
37 #include <asm/msr.h>
38 #include <asm/processor.h>
40 #define DRVNAME "coretemp"
42 typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL,
43 SHOW_NAME } SHOW;
46 * Functions declaration
49 static struct coretemp_data *coretemp_update_device(struct device *dev);
51 struct coretemp_data {
52 struct device *hwmon_dev;
53 struct mutex update_lock;
54 const char *name;
55 u32 id;
56 u16 core_id;
57 char valid; /* zero until following fields are valid */
58 unsigned long last_updated; /* in jiffies */
59 int temp;
60 int tjmax;
61 int ttarget;
62 u8 alarm;
66 * Sysfs stuff
69 static ssize_t show_name(struct device *dev, struct device_attribute
70 *devattr, char *buf)
72 int ret;
73 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
74 struct coretemp_data *data = dev_get_drvdata(dev);
76 if (attr->index == SHOW_NAME)
77 ret = sprintf(buf, "%s\n", data->name);
78 else /* show label */
79 ret = sprintf(buf, "Core %d\n", data->core_id);
80 return ret;
83 static ssize_t show_alarm(struct device *dev, struct device_attribute
84 *devattr, char *buf)
86 struct coretemp_data *data = coretemp_update_device(dev);
87 /* read the Out-of-spec log, never clear */
88 return sprintf(buf, "%d\n", data->alarm);
91 static ssize_t show_temp(struct device *dev,
92 struct device_attribute *devattr, char *buf)
94 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
95 struct coretemp_data *data = coretemp_update_device(dev);
96 int err;
98 if (attr->index == SHOW_TEMP)
99 err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN;
100 else if (attr->index == SHOW_TJMAX)
101 err = sprintf(buf, "%d\n", data->tjmax);
102 else
103 err = sprintf(buf, "%d\n", data->ttarget);
104 return err;
107 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
108 SHOW_TEMP);
109 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
110 SHOW_TJMAX);
111 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL,
112 SHOW_TTARGET);
113 static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
114 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
115 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
117 static struct attribute *coretemp_attributes[] = {
118 &sensor_dev_attr_name.dev_attr.attr,
119 &sensor_dev_attr_temp1_label.dev_attr.attr,
120 &dev_attr_temp1_crit_alarm.attr,
121 &sensor_dev_attr_temp1_input.dev_attr.attr,
122 &sensor_dev_attr_temp1_crit.dev_attr.attr,
123 NULL
126 static const struct attribute_group coretemp_group = {
127 .attrs = coretemp_attributes,
130 static struct coretemp_data *coretemp_update_device(struct device *dev)
132 struct coretemp_data *data = dev_get_drvdata(dev);
134 mutex_lock(&data->update_lock);
136 if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
137 u32 eax, edx;
139 data->valid = 0;
140 rdmsr_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
141 data->alarm = (eax >> 5) & 1;
142 /* update only if data has been valid */
143 if (eax & 0x80000000) {
144 data->temp = data->tjmax - (((eax >> 16)
145 & 0x7f) * 1000);
146 data->valid = 1;
147 } else {
148 dev_dbg(dev, "Temperature data invalid (0x%x)\n", eax);
150 data->last_updated = jiffies;
153 mutex_unlock(&data->update_lock);
154 return data;
157 static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
159 /* The 100C is default for both mobile and non mobile CPUs */
161 int tjmax = 100000;
162 int tjmax_ee = 85000;
163 int usemsr_ee = 1;
164 int err;
165 u32 eax, edx;
166 struct pci_dev *host_bridge;
168 /* Early chips have no MSR for TjMax */
170 if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
171 usemsr_ee = 0;
174 /* Atom CPUs */
176 if (c->x86_model == 0x1c) {
177 usemsr_ee = 0;
179 host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
181 if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL
182 && (host_bridge->device == 0xa000 /* NM10 based nettop */
183 || host_bridge->device == 0xa010)) /* NM10 based netbook */
184 tjmax = 100000;
185 else
186 tjmax = 90000;
188 pci_dev_put(host_bridge);
191 if ((c->x86_model > 0xe) && (usemsr_ee)) {
192 u8 platform_id;
194 /* Now we can detect the mobile CPU using Intel provided table
195 http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
196 For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
199 err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
200 if (err) {
201 dev_warn(dev,
202 "Unable to access MSR 0x17, assuming desktop"
203 " CPU\n");
204 usemsr_ee = 0;
205 } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
206 /* Trust bit 28 up to Penryn, I could not find any
207 documentation on that; if you happen to know
208 someone at Intel please ask */
209 usemsr_ee = 0;
210 } else {
211 /* Platform ID bits 52:50 (EDX starts at bit 32) */
212 platform_id = (edx >> 18) & 0x7;
214 /* Mobile Penryn CPU seems to be platform ID 7 or 5
215 (guesswork) */
216 if ((c->x86_model == 0x17) &&
217 ((platform_id == 5) || (platform_id == 7))) {
218 /* If MSR EE bit is set, set it to 90 degrees C,
219 otherwise 105 degrees C */
220 tjmax_ee = 90000;
221 tjmax = 105000;
226 if (usemsr_ee) {
228 err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
229 if (err) {
230 dev_warn(dev,
231 "Unable to access MSR 0xEE, for Tjmax, left"
232 " at default\n");
233 } else if (eax & 0x40000000) {
234 tjmax = tjmax_ee;
236 /* if we dont use msr EE it means we are desktop CPU (with exeception
237 of Atom) */
238 } else if (tjmax == 100000) {
239 dev_warn(dev, "Using relative temperature scale!\n");
242 return tjmax;
245 static int __devinit coretemp_probe(struct platform_device *pdev)
247 struct coretemp_data *data;
248 struct cpuinfo_x86 *c = &cpu_data(pdev->id);
249 int err;
250 u32 eax, edx;
252 if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
253 err = -ENOMEM;
254 dev_err(&pdev->dev, "Out of memory\n");
255 goto exit;
258 data->id = pdev->id;
259 #ifdef CONFIG_SMP
260 data->core_id = c->cpu_core_id;
261 #endif
262 data->name = "coretemp";
263 mutex_init(&data->update_lock);
265 /* test if we can access the THERM_STATUS MSR */
266 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
267 if (err) {
268 dev_err(&pdev->dev,
269 "Unable to access THERM_STATUS MSR, giving up\n");
270 goto exit_free;
273 /* Check if we have problem with errata AE18 of Core processors:
274 Readings might stop update when processor visited too deep sleep,
275 fixed for stepping D0 (6EC).
278 if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
279 /* check for microcode update */
280 rdmsr_on_cpu(data->id, MSR_IA32_UCODE_REV, &eax, &edx);
281 if (edx < 0x39) {
282 err = -ENODEV;
283 dev_err(&pdev->dev,
284 "Errata AE18 not fixed, update BIOS or "
285 "microcode of the CPU!\n");
286 goto exit_free;
290 data->tjmax = adjust_tjmax(c, data->id, &pdev->dev);
291 platform_set_drvdata(pdev, data);
293 /* read the still undocumented IA32_TEMPERATURE_TARGET it exists
294 on older CPUs but not in this register, Atoms don't have it either */
296 if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) {
297 err = rdmsr_safe_on_cpu(data->id, 0x1a2, &eax, &edx);
298 if (err) {
299 dev_warn(&pdev->dev, "Unable to read"
300 " IA32_TEMPERATURE_TARGET MSR\n");
301 } else {
302 data->ttarget = data->tjmax -
303 (((eax >> 8) & 0xff) * 1000);
304 err = device_create_file(&pdev->dev,
305 &sensor_dev_attr_temp1_max.dev_attr);
306 if (err)
307 goto exit_free;
311 if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
312 goto exit_dev;
314 data->hwmon_dev = hwmon_device_register(&pdev->dev);
315 if (IS_ERR(data->hwmon_dev)) {
316 err = PTR_ERR(data->hwmon_dev);
317 dev_err(&pdev->dev, "Class registration failed (%d)\n",
318 err);
319 goto exit_class;
322 return 0;
324 exit_class:
325 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
326 exit_dev:
327 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
328 exit_free:
329 kfree(data);
330 exit:
331 return err;
334 static int __devexit coretemp_remove(struct platform_device *pdev)
336 struct coretemp_data *data = platform_get_drvdata(pdev);
338 hwmon_device_unregister(data->hwmon_dev);
339 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
340 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
341 platform_set_drvdata(pdev, NULL);
342 kfree(data);
343 return 0;
346 static struct platform_driver coretemp_driver = {
347 .driver = {
348 .owner = THIS_MODULE,
349 .name = DRVNAME,
351 .probe = coretemp_probe,
352 .remove = __devexit_p(coretemp_remove),
355 struct pdev_entry {
356 struct list_head list;
357 struct platform_device *pdev;
358 unsigned int cpu;
359 #ifdef CONFIG_SMP
360 u16 phys_proc_id;
361 u16 cpu_core_id;
362 #endif
365 static LIST_HEAD(pdev_list);
366 static DEFINE_MUTEX(pdev_list_mutex);
368 static int __cpuinit coretemp_device_add(unsigned int cpu)
370 int err;
371 struct platform_device *pdev;
372 struct pdev_entry *pdev_entry;
373 #ifdef CONFIG_SMP
374 struct cpuinfo_x86 *c = &cpu_data(cpu);
375 #endif
377 mutex_lock(&pdev_list_mutex);
379 #ifdef CONFIG_SMP
380 /* Skip second HT entry of each core */
381 list_for_each_entry(pdev_entry, &pdev_list, list) {
382 if (c->phys_proc_id == pdev_entry->phys_proc_id &&
383 c->cpu_core_id == pdev_entry->cpu_core_id) {
384 err = 0; /* Not an error */
385 goto exit;
388 #endif
390 pdev = platform_device_alloc(DRVNAME, cpu);
391 if (!pdev) {
392 err = -ENOMEM;
393 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
394 goto exit;
397 pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
398 if (!pdev_entry) {
399 err = -ENOMEM;
400 goto exit_device_put;
403 err = platform_device_add(pdev);
404 if (err) {
405 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
406 err);
407 goto exit_device_free;
410 pdev_entry->pdev = pdev;
411 pdev_entry->cpu = cpu;
412 #ifdef CONFIG_SMP
413 pdev_entry->phys_proc_id = c->phys_proc_id;
414 pdev_entry->cpu_core_id = c->cpu_core_id;
415 #endif
416 list_add_tail(&pdev_entry->list, &pdev_list);
417 mutex_unlock(&pdev_list_mutex);
419 return 0;
421 exit_device_free:
422 kfree(pdev_entry);
423 exit_device_put:
424 platform_device_put(pdev);
425 exit:
426 mutex_unlock(&pdev_list_mutex);
427 return err;
430 #ifdef CONFIG_HOTPLUG_CPU
431 static void coretemp_device_remove(unsigned int cpu)
433 struct pdev_entry *p, *n;
434 mutex_lock(&pdev_list_mutex);
435 list_for_each_entry_safe(p, n, &pdev_list, list) {
436 if (p->cpu == cpu) {
437 platform_device_unregister(p->pdev);
438 list_del(&p->list);
439 kfree(p);
442 mutex_unlock(&pdev_list_mutex);
445 static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
446 unsigned long action, void *hcpu)
448 unsigned int cpu = (unsigned long) hcpu;
450 switch (action) {
451 case CPU_ONLINE:
452 case CPU_DOWN_FAILED:
453 coretemp_device_add(cpu);
454 break;
455 case CPU_DOWN_PREPARE:
456 coretemp_device_remove(cpu);
457 break;
459 return NOTIFY_OK;
462 static struct notifier_block coretemp_cpu_notifier __refdata = {
463 .notifier_call = coretemp_cpu_callback,
465 #endif /* !CONFIG_HOTPLUG_CPU */
467 static int __init coretemp_init(void)
469 int i, err = -ENODEV;
470 struct pdev_entry *p, *n;
472 /* quick check if we run Intel */
473 if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
474 goto exit;
476 err = platform_driver_register(&coretemp_driver);
477 if (err)
478 goto exit;
480 for_each_online_cpu(i) {
481 struct cpuinfo_x86 *c = &cpu_data(i);
483 /* check if family 6, models 0xe (Pentium M DC),
484 0xf (Core 2 DC 65nm), 0x16 (Core 2 SC 65nm),
485 0x17 (Penryn 45nm), 0x1a (Nehalem), 0x1c (Atom),
486 0x1e (Lynnfield) */
487 if ((c->cpuid_level < 0) || (c->x86 != 0x6) ||
488 !((c->x86_model == 0xe) || (c->x86_model == 0xf) ||
489 (c->x86_model == 0x16) || (c->x86_model == 0x17) ||
490 (c->x86_model == 0x1a) || (c->x86_model == 0x1c) ||
491 (c->x86_model == 0x1e))) {
493 /* supported CPU not found, but report the unknown
494 family 6 CPU */
495 if ((c->x86 == 0x6) && (c->x86_model > 0xf))
496 printk(KERN_WARNING DRVNAME ": Unknown CPU "
497 "model %x\n", c->x86_model);
498 continue;
501 err = coretemp_device_add(i);
502 if (err)
503 goto exit_devices_unreg;
505 if (list_empty(&pdev_list)) {
506 err = -ENODEV;
507 goto exit_driver_unreg;
510 #ifdef CONFIG_HOTPLUG_CPU
511 register_hotcpu_notifier(&coretemp_cpu_notifier);
512 #endif
513 return 0;
515 exit_devices_unreg:
516 mutex_lock(&pdev_list_mutex);
517 list_for_each_entry_safe(p, n, &pdev_list, list) {
518 platform_device_unregister(p->pdev);
519 list_del(&p->list);
520 kfree(p);
522 mutex_unlock(&pdev_list_mutex);
523 exit_driver_unreg:
524 platform_driver_unregister(&coretemp_driver);
525 exit:
526 return err;
529 static void __exit coretemp_exit(void)
531 struct pdev_entry *p, *n;
532 #ifdef CONFIG_HOTPLUG_CPU
533 unregister_hotcpu_notifier(&coretemp_cpu_notifier);
534 #endif
535 mutex_lock(&pdev_list_mutex);
536 list_for_each_entry_safe(p, n, &pdev_list, list) {
537 platform_device_unregister(p->pdev);
538 list_del(&p->list);
539 kfree(p);
541 mutex_unlock(&pdev_list_mutex);
542 platform_driver_unregister(&coretemp_driver);
545 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
546 MODULE_DESCRIPTION("Intel Core temperature monitor");
547 MODULE_LICENSE("GPL");
549 module_init(coretemp_init)
550 module_exit(coretemp_exit)